blob: 36fa5dc6716b68ed61a25dfb1807040eb7ca90db [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{
907 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700908#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700909 gDvmJit.returnOp++;
910#endif
911 int dPC = (int) (cUnit->method->insns + mir->offset);
912 /* Insert branch, but defer setting of target */
913 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
914 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800915 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800916 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700917 pcrLabel->operands[0] = dPC;
918 pcrLabel->operands[1] = mir->offset;
919 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -0700920 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Bill Buzbee1465db52009-09-23 17:17:35 -0700921 /* Branch to the PC reconstruction code */
922 branch->generic.target = (LIR *) pcrLabel;
923}
924
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
926 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700927 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700928{
929 unsigned int i;
930 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700931 RegLocation rlArg;
932 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700933
Bill Buzbee1465db52009-09-23 17:17:35 -0700934 /*
935 * Load arguments to r0..r4. Note that these registers may contain
936 * live values, so we clobber them immediately after loading to prevent
937 * them from being used as sources for subsequent loads.
938 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800939 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700940 for (i = 0; i < dInsn->vA; i++) {
941 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800942 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700943 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700944 }
945 if (regMask) {
946 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700947 opRegRegImm(cUnit, kOpSub, r7, rFP,
948 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700949 /* generate null check */
950 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800951 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700952 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700953 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700954 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700955 }
956}
957
958static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
959 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700960 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961{
962 int srcOffset = dInsn->vC << 2;
963 int numArgs = dInsn->vA;
964 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700965
966 /*
967 * Note: here, all promoted registers will have been flushed
968 * back to the Dalvik base locations, so register usage restrictins
969 * are lifted. All parms loaded from original Dalvik register
970 * region - even though some might conceivably have valid copies
971 * cached in a preserved register.
972 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800973 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700974
Ben Chengba4fc8b2009-06-01 13:00:29 -0700975 /*
976 * r4PC : &rFP[vC]
977 * r7: &newFP[0]
978 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700979 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700980 /* load [r0 .. min(numArgs,4)] */
981 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700982 /*
983 * Protect the loadMultiple instruction from being reordered with other
984 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -0800985 *
986 * This code is also shared by the invoke jumbo instructions, and this
987 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -0700988 */
jeffhao71eee1f2011-01-04 14:18:54 -0800989 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700990
Bill Buzbee1465db52009-09-23 17:17:35 -0700991 opRegRegImm(cUnit, kOpSub, r7, rFP,
992 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 /* generate null check */
994 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800995 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700996 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700997 }
998
999 /*
1000 * Handle remaining 4n arguments:
1001 * store previously loaded 4 values and load the next 4 values
1002 */
1003 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001004 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001005 /*
1006 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001007 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001008 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001009 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010 /* No need to generate the loop structure if numArgs <= 11 */
1011 if (numArgs > 11) {
1012 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001013 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001014 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001015 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001016 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001017 /*
1018 * Protect the loadMultiple instruction from being reordered with other
1019 * Dalvik stack accesses.
1020 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001021 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022 /* No need to generate the loop structure if numArgs <= 11 */
1023 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001024 opRegImm(cUnit, kOpSub, rFP, 4);
1025 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001026 }
1027 }
1028
1029 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001030 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001031
1032 /* Generate the loop epilogue - don't use r0 */
1033 if ((numArgs > 4) && (numArgs % 4)) {
1034 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001035 /*
1036 * Protect the loadMultiple instruction from being reordered with other
1037 * Dalvik stack accesses.
1038 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001039 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040 }
1041 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001042 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001043
1044 /* Save the modulo 4 arguments */
1045 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001046 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001047 }
1048}
1049
Ben Cheng38329f52009-07-07 14:19:20 -07001050/*
1051 * Generate code to setup the call stack then jump to the chaining cell if it
1052 * is not a native method.
1053 */
1054static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001055 BasicBlock *bb, ArmLIR *labelList,
1056 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001057 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001058{
Bill Buzbee1465db52009-09-23 17:17:35 -07001059 /*
1060 * Note: all Dalvik register state should be flushed to
1061 * memory by the point, so register usage restrictions no
1062 * longer apply. All temp & preserved registers may be used.
1063 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001064 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001065 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001066
1067 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001068 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001069
Ben Chengba4fc8b2009-06-01 13:00:29 -07001070 /* r4PC = dalvikCallsite */
1071 loadConstant(cUnit, r4PC,
1072 (int) (cUnit->method->insns + mir->offset));
1073 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001074
1075 /* r7 = calleeMethod->registersSize */
1076 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001077 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001078 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001079 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001080 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001081 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001082 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001083 */
1084 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001085 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001086#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001087 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001088#endif
1089 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001090 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1091 loadConstant(cUnit, r2, calleeMethod->outsSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001092 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001093#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001094 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001095#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001096 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001097 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1098 }
1099 /* Handle exceptions using the interpreter */
1100 genTrap(cUnit, mir->offset, pcrLabel);
1101}
1102
Ben Cheng38329f52009-07-07 14:19:20 -07001103/*
1104 * Generate code to check the validity of a predicted chain and take actions
1105 * based on the result.
1106 *
1107 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1108 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1109 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1110 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1111 * 0x426a99b2 : blx_2 see above --+
1112 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1113 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1114 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1115 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1116 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001117 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001118 * 0x426a99c0 : blx r7 --+
1119 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1120 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1121 * 0x426a99c6 : blx_2 see above --+
1122 */
1123static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1124 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001125 ArmLIR *retChainingCell,
1126 ArmLIR *predChainingCell,
1127 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001128{
Bill Buzbee1465db52009-09-23 17:17:35 -07001129 /*
1130 * Note: all Dalvik register state should be flushed to
1131 * memory by the point, so register usage restrictions no
1132 * longer apply. Lock temps to prevent them from being
1133 * allocated by utility routines.
1134 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001135 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001136
Ben Cheng38329f52009-07-07 14:19:20 -07001137 /* "this" is already left in r0 by genProcessArgs* */
1138
1139 /* r4PC = dalvikCallsite */
1140 loadConstant(cUnit, r4PC,
1141 (int) (cUnit->method->insns + mir->offset));
1142
1143 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001144 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001145 addrRetChain->generic.target = (LIR *) retChainingCell;
1146
1147 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001148 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001149 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1150
1151 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1152
1153 /* return through lr - jump to the chaining cell */
1154 genUnconditionalBranch(cUnit, predChainingCell);
1155
1156 /*
1157 * null-check on "this" may have been eliminated, but we still need a PC-
1158 * reconstruction label for stack overflow bailout.
1159 */
1160 if (pcrLabel == NULL) {
1161 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001162 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001163 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001164 pcrLabel->operands[0] = dPC;
1165 pcrLabel->operands[1] = mir->offset;
1166 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001167 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1168 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001169 }
1170
1171 /* return through lr+2 - punt to the interpreter */
1172 genUnconditionalBranch(cUnit, pcrLabel);
1173
1174 /*
1175 * return through lr+4 - fully resolve the callee method.
1176 * r1 <- count
1177 * r2 <- &predictedChainCell
1178 * r3 <- this->class
1179 * r4 <- dPC
1180 * r7 <- this->class->vtable
1181 */
1182
1183 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001184 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001185
1186 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001187 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001188
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001189 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001190
Ben Chengb88ec3c2010-05-17 12:50:33 -07001191 genRegCopy(cUnit, r1, rGLUE);
1192
Ben Cheng38329f52009-07-07 14:19:20 -07001193 /*
1194 * r0 = calleeMethod
1195 * r2 = &predictedChainingCell
1196 * r3 = class
1197 *
1198 * &returnChainingCell has been loaded into r1 but is not needed
1199 * when patching the chaining cell and will be clobbered upon
1200 * returning so it will be reconstructed again.
1201 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001202 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001203
1204 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001205 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001206 addrRetChain->generic.target = (LIR *) retChainingCell;
1207
1208 bypassRechaining->generic.target = (LIR *) addrRetChain;
1209 /*
1210 * r0 = calleeMethod,
1211 * r1 = &ChainingCell,
1212 * r4PC = callsiteDPC,
1213 */
1214 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001215#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001216 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001217#endif
1218 /* Handle exceptions using the interpreter */
1219 genTrap(cUnit, mir->offset, pcrLabel);
1220}
1221
Ben Chengba4fc8b2009-06-01 13:00:29 -07001222/* Geneate a branch to go back to the interpreter */
1223static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1224{
1225 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001226 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001227 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001228 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1229 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001230 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001231}
1232
1233/*
1234 * Attempt to single step one instruction using the interpreter and return
1235 * to the compiled code for the next Dalvik instruction
1236 */
1237static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1238{
Dan Bornsteine4852762010-12-02 12:45:00 -08001239 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001240 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1241 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001242
Bill Buzbee45273872010-03-11 11:12:15 -08001243 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001244 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001245 return;
1246
Bill Buzbee1465db52009-09-23 17:17:35 -07001247 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001248 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001249
Ben Chengba4fc8b2009-06-01 13:00:29 -07001250 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1251 genPuntToInterp(cUnit, mir->offset);
1252 return;
1253 }
1254 int entryAddr = offsetof(InterpState,
1255 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001256 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001257 /* r0 = dalvik pc */
1258 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1259 /* r1 = dalvik pc of following instruction */
1260 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001261 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001262}
1263
Ben Chengfc075c22010-05-28 15:20:08 -07001264#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1265 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001266/*
1267 * To prevent a thread in a monitor wait from blocking the Jit from
1268 * resetting the code cache, heavyweight monitor lock will not
1269 * be allowed to return to an existing translation. Instead, we will
1270 * handle them by branching to a handler, which will in turn call the
1271 * runtime lock routine and then branch directly back to the
1272 * interpreter main loop. Given the high cost of the heavyweight
1273 * lock operation, this additional cost should be slight (especially when
1274 * considering that we expect the vast majority of lock operations to
1275 * use the fast-path thin lock bypass).
1276 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001277static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001278{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001279 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001280 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001281 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1282 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001283 loadValueDirectFixed(cUnit, rlSrc, r1);
1284 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001285 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001286 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001287 /* Get dPC of next insn */
1288 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001289 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001290#if defined(WITH_DEADLOCK_PREDICTION)
1291 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1292#else
1293 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1294#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001295 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001296 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001297 /* Do the call */
1298 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001299 /* Did we throw? */
1300 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001301 loadConstant(cUnit, r0,
1302 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001303 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001304 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1305 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1306 target->defMask = ENCODE_ALL;
1307 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001308 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001309 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001310}
Ben Chengfc075c22010-05-28 15:20:08 -07001311#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001312
Ben Chengba4fc8b2009-06-01 13:00:29 -07001313/*
1314 * The following are the first-level codegen routines that analyze the format
1315 * of each bytecode then either dispatch special purpose codegen routines
1316 * or produce corresponding Thumb instructions directly.
1317 */
1318
1319static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001320 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001321{
1322 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1323 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1324 return false;
1325}
1326
1327static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1328{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001329 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1330 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1331 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001332 return true;
1333 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001334 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001335 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001336 dvmCompilerGenMemBarrier(cUnit, kST);
1337 // Intentional fallthrough
1338 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001339 genReturnCommon(cUnit,mir);
1340 break;
1341 case OP_UNUSED_73:
1342 case OP_UNUSED_79:
1343 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001344 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001345 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001346 return true;
1347 case OP_NOP:
1348 break;
1349 default:
1350 return true;
1351 }
1352 return false;
1353}
1354
1355static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1356{
Bill Buzbee1465db52009-09-23 17:17:35 -07001357 RegLocation rlDest;
1358 RegLocation rlResult;
1359 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001360 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001361 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001362 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001363 }
Ben Chenge9695e52009-06-16 16:11:47 -07001364
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001365 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001366 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001367 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001368 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001369 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001370 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001371 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001372 }
1373 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001374 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001375 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001376 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001377 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001378 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1379 rlResult.lowReg, 31);
1380 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001381 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001382 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 default:
1384 return true;
1385 }
1386 return false;
1387}
1388
1389static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1390{
Bill Buzbee1465db52009-09-23 17:17:35 -07001391 RegLocation rlDest;
1392 RegLocation rlResult;
1393 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001394 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001395 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001396 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001397 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001398 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001399
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001400 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001401 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001402 loadConstantNoClobber(cUnit, rlResult.lowReg,
1403 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001404 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001405 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001406 }
1407 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001408 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1409 0, mir->dalvikInsn.vB << 16);
1410 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001411 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001412 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001413 default:
1414 return true;
1415 }
1416 return false;
1417}
1418
jeffhao71eee1f2011-01-04 14:18:54 -08001419static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001420{
jeffhao71eee1f2011-01-04 14:18:54 -08001421 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001422 genInterpSingleStep(cUnit, mir);
1423 return false;
1424}
1425
jeffhao71eee1f2011-01-04 14:18:54 -08001426static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001427{
Bill Buzbee1465db52009-09-23 17:17:35 -07001428 RegLocation rlResult;
1429 RegLocation rlDest;
1430 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001431
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001432 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001433 case OP_CONST_STRING_JUMBO:
1434 case OP_CONST_STRING: {
1435 void *strPtr = (void*)
1436 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001437
1438 if (strPtr == NULL) {
1439 LOGE("Unexpected null string");
1440 dvmAbort();
1441 }
1442
Bill Buzbeec6f10662010-02-09 11:16:15 -08001443 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1444 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001445 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001446 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001447 break;
1448 }
jeffhao71eee1f2011-01-04 14:18:54 -08001449 case OP_CONST_CLASS:
1450 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001451 void *classPtr = (void*)
1452 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001453
1454 if (classPtr == NULL) {
1455 LOGE("Unexpected null class");
1456 dvmAbort();
1457 }
1458
Bill Buzbeec6f10662010-02-09 11:16:15 -08001459 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1460 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001461 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001462 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001463 break;
1464 }
jeffhao71eee1f2011-01-04 14:18:54 -08001465 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001466 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001467 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001468 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001469 case OP_SGET_OBJECT_VOLATILE:
1470 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001471 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001472 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001473 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001474 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001475 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001476 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001477 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001478 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001479 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001480 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001481 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001482 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1483 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001484 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001485 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001486
1487 if (fieldPtr == NULL) {
1488 LOGE("Unexpected null static field");
1489 dvmAbort();
1490 }
1491
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001492 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1493 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001494 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001495
Bill Buzbeec6f10662010-02-09 11:16:15 -08001496 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1497 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001498 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001499
buzbeeecf8f6e2010-07-20 14:53:42 -07001500 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001501 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001502 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001503 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001504 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001505 HEAP_ACCESS_SHADOW(false);
1506
Bill Buzbee1465db52009-09-23 17:17:35 -07001507 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001508 break;
1509 }
jeffhao71eee1f2011-01-04 14:18:54 -08001510 case OP_SGET_WIDE:
1511 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001512 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001513 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1514 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001515 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001516 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001517
1518 if (fieldPtr == NULL) {
1519 LOGE("Unexpected null static field");
1520 dvmAbort();
1521 }
1522
Bill Buzbeec6f10662010-02-09 11:16:15 -08001523 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001524 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1525 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001526 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001527
1528 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001529 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001530 HEAP_ACCESS_SHADOW(false);
1531
Bill Buzbee1465db52009-09-23 17:17:35 -07001532 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001533 break;
1534 }
jeffhao71eee1f2011-01-04 14:18:54 -08001535 case OP_SPUT:
1536 case OP_SPUT_VOLATILE:
1537 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001538 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001539 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001540 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001541 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001542 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001543 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001544 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001545 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001546 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001547 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001548 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001549 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001550 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001551 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001552 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001553 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001554 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1555 mir->meta.calleeMethod : cUnit->method;
1556 void *fieldPtr = (void*)
1557 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001558
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001559 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1560 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001561 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001562
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001563 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001564 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001565 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001566
Ben Chengdd6e8702010-05-07 13:05:47 -07001567 if (fieldPtr == NULL) {
1568 LOGE("Unexpected null static field");
1569 dvmAbort();
1570 }
1571
Bill Buzbeec6f10662010-02-09 11:16:15 -08001572 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001573 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001574 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001575 if (isSputObject) {
1576 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001577 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001578 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001579 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001580 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001581 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001582 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001583 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001584 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001585 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001586 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001587 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001588 markCard(cUnit, rlSrc.lowReg, objHead);
1589 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001590 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001591
Ben Chengba4fc8b2009-06-01 13:00:29 -07001592 break;
1593 }
jeffhao71eee1f2011-01-04 14:18:54 -08001594 case OP_SPUT_WIDE:
1595 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001596 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001597 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001598 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1599 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001600 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001601 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001602
Ben Chengdd6e8702010-05-07 13:05:47 -07001603 if (fieldPtr == NULL) {
1604 LOGE("Unexpected null static field");
1605 dvmAbort();
1606 }
1607
Bill Buzbeec6f10662010-02-09 11:16:15 -08001608 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001609 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1610 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001611
1612 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001613 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001614 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001615 break;
1616 }
jeffhao71eee1f2011-01-04 14:18:54 -08001617 case OP_NEW_INSTANCE:
1618 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001619 /*
1620 * Obey the calling convention and don't mess with the register
1621 * usage.
1622 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001623 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001624 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001625
1626 if (classPtr == NULL) {
1627 LOGE("Unexpected null class");
1628 dvmAbort();
1629 }
1630
Ben Cheng79d173c2009-09-29 16:12:51 -07001631 /*
1632 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001633 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001634 */
1635 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001636 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001637 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001638 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001639 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001640 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001641 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001642 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001643 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001644 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001645 /*
1646 * OOM exception needs to be thrown here and cannot re-execute
1647 */
1648 loadConstant(cUnit, r0,
1649 (int) (cUnit->method->insns + mir->offset));
1650 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1651 /* noreturn */
1652
Bill Buzbee1465db52009-09-23 17:17:35 -07001653 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001654 target->defMask = ENCODE_ALL;
1655 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001656 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1657 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001658 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001659 break;
1660 }
jeffhao71eee1f2011-01-04 14:18:54 -08001661 case OP_CHECK_CAST:
1662 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001663 /*
1664 * Obey the calling convention and don't mess with the register
1665 * usage.
1666 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001667 ClassObject *classPtr =
1668 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001669 /*
1670 * Note: It is possible that classPtr is NULL at this point,
1671 * even though this instruction has been successfully interpreted.
1672 * If the previous interpretation had a null source, the
1673 * interpreter would not have bothered to resolve the clazz.
1674 * Bail out to the interpreter in this case, and log it
1675 * so that we can tell if it happens frequently.
1676 */
1677 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001678 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001679 genInterpSingleStep(cUnit, mir);
1680 return false;
1681 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001682 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001683 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001684 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001685 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001686 /* Null? */
1687 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1688 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001689 /*
1690 * rlSrc.lowReg now contains object->clazz. Note that
1691 * it could have been allocated r0, but we're okay so long
1692 * as we don't do anything desctructive until r0 is loaded
1693 * with clazz.
1694 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001695 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001696 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001697 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698 opRegReg(cUnit, kOpCmp, r0, r1);
1699 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1700 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001701 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001702 /*
1703 * If null, check cast failed - punt to the interpreter. Because
1704 * interpreter will be the one throwing, we don't need to
1705 * genExportPC() here.
1706 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001707 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001708 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001709 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001710 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001711 branch1->generic.target = (LIR *)target;
1712 branch2->generic.target = (LIR *)target;
1713 break;
1714 }
buzbee4d92e682010-07-29 15:24:14 -07001715 case OP_SGET_WIDE_VOLATILE:
1716 case OP_SPUT_WIDE_VOLATILE:
1717 genInterpSingleStep(cUnit, mir);
1718 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001719 default:
1720 return true;
1721 }
1722 return false;
1723}
1724
Ben Cheng7a2697d2010-06-07 13:44:23 -07001725/*
1726 * A typical example of inlined getter/setter from a monomorphic callsite:
1727 *
1728 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1729 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1730 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1731 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1732 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1733 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1734 * D/dalvikvm( 289): L0x0003:
1735 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1736 *
1737 * Note the invoke-static and move-result-object with the (I) notation are
1738 * turned into no-op.
1739 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1741{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001742 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001743 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001744 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001745 case OP_MOVE_EXCEPTION: {
1746 int offset = offsetof(InterpState, self);
1747 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001748 int selfReg = dvmCompilerAllocTemp(cUnit);
1749 int resetReg = dvmCompilerAllocTemp(cUnit);
1750 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1751 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001753 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001754 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001755 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001756 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001757 break;
1758 }
1759 case OP_MOVE_RESULT:
1760 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001761 /* An inlined move result is effectively no-op */
1762 if (mir->OptimizationFlags & MIR_INLINED)
1763 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001764 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001765 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1766 rlSrc.fp = rlDest.fp;
1767 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001768 break;
1769 }
1770 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001771 /* An inlined move result is effectively no-op */
1772 if (mir->OptimizationFlags & MIR_INLINED)
1773 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001774 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001775 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1776 rlSrc.fp = rlDest.fp;
1777 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001778 break;
1779 }
1780 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001781 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001782 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1783 rlDest.fp = rlSrc.fp;
1784 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001785 genReturnCommon(cUnit,mir);
1786 break;
1787 }
1788 case OP_RETURN:
1789 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001790 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1792 rlDest.fp = rlSrc.fp;
1793 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001794 genReturnCommon(cUnit,mir);
1795 break;
1796 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001797 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001798 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001799#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001800 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001801#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001802 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001803#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001804 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001805 case OP_THROW: {
1806 genInterpSingleStep(cUnit, mir);
1807 break;
1808 }
1809 default:
1810 return true;
1811 }
1812 return false;
1813}
1814
Bill Buzbeed45ba372009-06-15 17:00:57 -07001815static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1816{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001817 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001818 RegLocation rlDest;
1819 RegLocation rlSrc;
1820 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001821
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001822 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001823 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001824 }
1825
Bill Buzbee1465db52009-09-23 17:17:35 -07001826 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001827 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001829 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001830 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001831 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001832 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001833 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001834
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001835 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001836 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001837 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001838 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001839 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001840 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001841 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001842 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001844 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001845 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001846 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001847 case OP_NEG_INT:
1848 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001849 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001850 case OP_NEG_LONG:
1851 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001852 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001853 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001854 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001856 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001857 case OP_MOVE_WIDE:
1858 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001859 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001860 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001861 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1862 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001863 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001864 if (rlSrc.location == kLocPhysReg) {
1865 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1866 } else {
1867 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1868 }
1869 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1870 rlResult.lowReg, 31);
1871 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001872 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001873 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001874 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1875 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001876 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877 case OP_MOVE:
1878 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001879 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001880 break;
1881 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001882 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001883 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001884 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1885 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001886 break;
1887 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001888 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001889 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001890 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1891 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 break;
1893 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001894 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001895 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001896 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1897 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001898 break;
1899 case OP_ARRAY_LENGTH: {
1900 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001901 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1902 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1903 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001904 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001905 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1906 rlResult.lowReg);
1907 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001908 break;
1909 }
1910 default:
1911 return true;
1912 }
1913 return false;
1914}
1915
1916static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1917{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001918 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001919 RegLocation rlDest;
1920 RegLocation rlResult;
1921 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001922 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001923 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1924 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001925 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001926 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001927 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1928 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001929 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001930 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1931 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001932 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001933 storeValue(cUnit, rlDest, rlResult);
1934 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001935 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001936 return false;
1937}
1938
1939/* Compare agaist zero */
1940static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001941 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001942{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001943 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001944 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001945 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001946 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1947 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001948
Bill Buzbee270c1d62009-08-13 16:58:07 -07001949//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001950 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001951 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001952 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001953 break;
1954 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001955 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001956 break;
1957 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001958 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001959 break;
1960 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001961 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001962 break;
1963 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001964 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001965 break;
1966 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001967 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001968 break;
1969 default:
1970 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001971 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001972 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 }
1974 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1975 /* This mostly likely will be optimized away in a later phase */
1976 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1977 return false;
1978}
1979
Elliott Hughesb4c05972010-02-24 16:36:18 -08001980static bool isPowerOfTwo(int x)
1981{
1982 return (x & (x - 1)) == 0;
1983}
1984
1985// Returns true if no more than two bits are set in 'x'.
1986static bool isPopCountLE2(unsigned int x)
1987{
1988 x &= x - 1;
1989 return (x & (x - 1)) == 0;
1990}
1991
1992// Returns the index of the lowest set bit in 'x'.
1993static int lowestSetBit(unsigned int x) {
1994 int bit_posn = 0;
1995 while ((x & 0xf) == 0) {
1996 bit_posn += 4;
1997 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001998 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001999 while ((x & 1) == 0) {
2000 bit_posn++;
2001 x >>= 1;
2002 }
2003 return bit_posn;
2004}
2005
Elliott Hughes672511b2010-04-26 17:40:13 -07002006// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2007// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002008static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002009 RegLocation rlSrc, RegLocation rlDest, int lit)
2010{
2011 if (lit < 2 || !isPowerOfTwo(lit)) {
2012 return false;
2013 }
2014 int k = lowestSetBit(lit);
2015 if (k >= 30) {
2016 // Avoid special cases.
2017 return false;
2018 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002019 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002020 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2021 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002022 if (div) {
2023 int tReg = dvmCompilerAllocTemp(cUnit);
2024 if (lit == 2) {
2025 // Division by 2 is by far the most common division by constant.
2026 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2027 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2028 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2029 } else {
2030 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2031 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2032 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2033 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2034 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002035 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002036 int cReg = dvmCompilerAllocTemp(cUnit);
2037 loadConstant(cUnit, cReg, lit - 1);
2038 int tReg1 = dvmCompilerAllocTemp(cUnit);
2039 int tReg2 = dvmCompilerAllocTemp(cUnit);
2040 if (lit == 2) {
2041 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2042 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2043 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2044 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2045 } else {
2046 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2047 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2048 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2049 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2050 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2051 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002052 }
2053 storeValue(cUnit, rlDest, rlResult);
2054 return true;
2055}
2056
Elliott Hughesb4c05972010-02-24 16:36:18 -08002057// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2058// and store the result in 'rlDest'.
2059static bool handleEasyMultiply(CompilationUnit *cUnit,
2060 RegLocation rlSrc, RegLocation rlDest, int lit)
2061{
2062 // Can we simplify this multiplication?
2063 bool powerOfTwo = false;
2064 bool popCountLE2 = false;
2065 bool powerOfTwoMinusOne = false;
2066 if (lit < 2) {
2067 // Avoid special cases.
2068 return false;
2069 } else if (isPowerOfTwo(lit)) {
2070 powerOfTwo = true;
2071 } else if (isPopCountLE2(lit)) {
2072 popCountLE2 = true;
2073 } else if (isPowerOfTwo(lit + 1)) {
2074 powerOfTwoMinusOne = true;
2075 } else {
2076 return false;
2077 }
2078 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2079 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2080 if (powerOfTwo) {
2081 // Shift.
2082 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2083 lowestSetBit(lit));
2084 } else if (popCountLE2) {
2085 // Shift and add and shift.
2086 int firstBit = lowestSetBit(lit);
2087 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2088 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2089 firstBit, secondBit);
2090 } else {
2091 // Reverse subtract: (src << (shift + 1)) - src.
2092 assert(powerOfTwoMinusOne);
2093 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2094 int tReg = dvmCompilerAllocTemp(cUnit);
2095 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2096 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2097 }
2098 storeValue(cUnit, rlDest, rlResult);
2099 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002100}
2101
Ben Chengba4fc8b2009-06-01 13:00:29 -07002102static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2103{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002104 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002105 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2106 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002108 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002109 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002110 int shiftOp = false;
2111 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002112
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002113 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002114 case OP_RSUB_INT_LIT8:
2115 case OP_RSUB_INT: {
2116 int tReg;
2117 //TUNING: add support for use of Arm rsub op
2118 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002119 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002120 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002121 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002122 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2123 tReg, rlSrc.lowReg);
2124 storeValue(cUnit, rlDest, rlResult);
2125 return false;
2126 break;
2127 }
2128
Ben Chengba4fc8b2009-06-01 13:00:29 -07002129 case OP_ADD_INT_LIT8:
2130 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002131 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002132 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002133 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002134 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002135 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2136 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002137 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002138 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002139 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002140 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 case OP_AND_INT_LIT8:
2142 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002143 op = kOpAnd;
2144 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002145 case OP_OR_INT_LIT8:
2146 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002147 op = kOpOr;
2148 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002149 case OP_XOR_INT_LIT8:
2150 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002151 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002153 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002154 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002155 shiftOp = true;
2156 op = kOpLsl;
2157 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002158 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002159 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002160 shiftOp = true;
2161 op = kOpAsr;
2162 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002163 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002164 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 shiftOp = true;
2166 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002167 break;
2168
2169 case OP_DIV_INT_LIT8:
2170 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002171 case OP_REM_INT_LIT8:
2172 case OP_REM_INT_LIT16:
2173 if (lit == 0) {
2174 /* Let the interpreter deal with div by 0 */
2175 genInterpSingleStep(cUnit, mir);
2176 return false;
2177 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002178 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002179 return false;
2180 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002181 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002182 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002183 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002184 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2185 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002186 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 isDiv = true;
2188 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002189 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002190 isDiv = false;
2191 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002192 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002193 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002194 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002195 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002196 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002197 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002198 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002199 storeValue(cUnit, rlDest, rlResult);
2200 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002201 break;
2202 default:
2203 return true;
2204 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002205 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002206 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2208 if (shiftOp && (lit == 0)) {
2209 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2210 } else {
2211 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2212 }
2213 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002214 return false;
2215}
2216
jeffhao71eee1f2011-01-04 14:18:54 -08002217static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002218{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002219 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002220 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002221 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002222 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002223 /*
2224 * Wide volatiles currently handled via single step.
2225 * Add them here if generating in-line code.
2226 * case OP_IGET_WIDE_VOLATILE:
2227 * case OP_IPUT_WIDE_VOLATILE:
2228 */
2229 case OP_IGET:
2230 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002231 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002232 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002233 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002234 case OP_IGET_OBJECT:
2235 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002236 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002237 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002238 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002239 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002240 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002241 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002242 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002243 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002244 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002245 case OP_IPUT:
2246 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002247 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002248 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002249 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002250 case OP_IPUT_OBJECT:
2251 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002252 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002253 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002254 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002255 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002256 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002257 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002258 case OP_IPUT_CHAR_JUMBO:
2259 case OP_IPUT_SHORT:
2260 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002261 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2262 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002263 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002264 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265
buzbee4d92e682010-07-29 15:24:14 -07002266 if (fieldPtr == NULL) {
2267 LOGE("Unexpected null instance field");
2268 dvmAbort();
2269 }
2270 isVolatile = dvmIsVolatileField(fieldPtr);
2271 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2272 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002273 }
buzbee4d92e682010-07-29 15:24:14 -07002274 default:
2275 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002276 }
buzbee4d92e682010-07-29 15:24:14 -07002277
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002278 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002279 case OP_NEW_ARRAY:
2280 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002281 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002282 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2283 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002284 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002285 void *classPtr = (void*)
2286 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002287
2288 if (classPtr == NULL) {
2289 LOGE("Unexpected null class");
2290 dvmAbort();
2291 }
2292
Bill Buzbeec6f10662010-02-09 11:16:15 -08002293 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 genExportPC(cUnit, mir);
2295 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002297 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002298 /*
2299 * "len < 0": bail to the interpreter to re-execute the
2300 * instruction
2301 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002302 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002303 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002304 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002305 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002306 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002307 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002308 /*
2309 * OOM exception needs to be thrown here and cannot re-execute
2310 */
2311 loadConstant(cUnit, r0,
2312 (int) (cUnit->method->insns + mir->offset));
2313 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2314 /* noreturn */
2315
Bill Buzbee1465db52009-09-23 17:17:35 -07002316 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002317 target->defMask = ENCODE_ALL;
2318 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002319 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002320 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 break;
2322 }
jeffhao71eee1f2011-01-04 14:18:54 -08002323 case OP_INSTANCE_OF:
2324 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002325 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002326 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2327 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002328 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002329 ClassObject *classPtr =
2330 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002331 /*
2332 * Note: It is possible that classPtr is NULL at this point,
2333 * even though this instruction has been successfully interpreted.
2334 * If the previous interpretation had a null source, the
2335 * interpreter would not have bothered to resolve the clazz.
2336 * Bail out to the interpreter in this case, and log it
2337 * so that we can tell if it happens frequently.
2338 */
2339 if (classPtr == NULL) {
2340 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2341 genInterpSingleStep(cUnit, mir);
2342 break;
2343 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002344 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002345 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002346 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002347 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002348 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002349 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002350 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002351 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002352 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002353 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002354 opRegReg(cUnit, kOpCmp, r1, r2);
2355 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2356 genRegCopy(cUnit, r0, r1);
2357 genRegCopy(cUnit, r1, r2);
2358 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002359 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002360 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002361 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002362 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002363 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002364 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002365 branch1->generic.target = (LIR *)target;
2366 branch2->generic.target = (LIR *)target;
2367 break;
2368 }
2369 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002370 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002371 genIGetWide(cUnit, mir, fieldOffset);
2372 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002373 case OP_IGET_VOLATILE:
2374 case OP_IGET_OBJECT_VOLATILE:
2375 isVolatile = true;
2376 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002377 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002378 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002379 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002380 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002381 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002382 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002384 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002385 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002386 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002387 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002388 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002389 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002390 break;
2391 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002392 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002393 genIPutWide(cUnit, mir, fieldOffset);
2394 break;
2395 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002396 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002397 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002398 case OP_IPUT_BOOLEAN_JUMBO:
2399 case OP_IPUT_BYTE:
2400 case OP_IPUT_BYTE_JUMBO:
2401 case OP_IPUT_CHAR:
2402 case OP_IPUT_CHAR_JUMBO:
2403 case OP_IPUT_SHORT:
2404 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002405 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002406 break;
buzbee4d92e682010-07-29 15:24:14 -07002407 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002408 case OP_IPUT_OBJECT_VOLATILE:
2409 isVolatile = true;
2410 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002411 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002412 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002413 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002414 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002415 case OP_IGET_WIDE_VOLATILE:
2416 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002417 genInterpSingleStep(cUnit, mir);
2418 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002419 default:
2420 return true;
2421 }
2422 return false;
2423}
2424
2425static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2426{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002427 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002428 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002429 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002430 case OP_IGET_QUICK:
2431 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002432 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002433 break;
2434 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002435 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002436 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002437 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002438 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002439 break;
2440 case OP_IGET_WIDE_QUICK:
2441 genIGetWide(cUnit, mir, fieldOffset);
2442 break;
2443 case OP_IPUT_WIDE_QUICK:
2444 genIPutWide(cUnit, mir, fieldOffset);
2445 break;
2446 default:
2447 return true;
2448 }
2449 return false;
2450
2451}
2452
2453/* Compare agaist zero */
2454static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002455 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002456{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002457 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002458 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002459 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2460 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002461
Bill Buzbee1465db52009-09-23 17:17:35 -07002462 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2463 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2464 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002465
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002466 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002467 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002468 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002469 break;
2470 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002471 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002472 break;
2473 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002474 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002475 break;
2476 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002477 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002478 break;
2479 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002480 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002481 break;
2482 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002483 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002484 break;
2485 default:
2486 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002487 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002488 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002489 }
2490 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2491 /* This mostly likely will be optimized away in a later phase */
2492 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2493 return false;
2494}
2495
2496static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2497{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002498 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002499
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002500 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002501 case OP_MOVE_16:
2502 case OP_MOVE_OBJECT_16:
2503 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002504 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002505 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2506 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002507 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002508 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002509 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002510 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002511 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2512 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002513 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002514 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002515 default:
2516 return true;
2517 }
2518 return false;
2519}
2520
2521static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2522{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002523 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002524 RegLocation rlSrc1;
2525 RegLocation rlSrc2;
2526 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002527
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002528 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002529 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002530 }
2531
Bill Buzbee1465db52009-09-23 17:17:35 -07002532 /* APUTs have 3 sources and no targets */
2533 if (mir->ssaRep->numDefs == 0) {
2534 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002535 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2536 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2537 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002538 } else {
2539 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002540 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2541 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2542 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002543 }
2544 } else {
2545 /* Two sources and 1 dest. Deduce the operand sizes */
2546 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002547 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2548 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002549 } else {
2550 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002551 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2552 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002553 }
2554 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002555 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002556 } else {
2557 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002558 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002559 }
2560 }
2561
2562
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002563 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002564 case OP_CMPL_FLOAT:
2565 case OP_CMPG_FLOAT:
2566 case OP_CMPL_DOUBLE:
2567 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002568 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002569 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002570 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002571 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002572 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002573 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002574 break;
2575 case OP_AGET:
2576 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002577 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002578 break;
2579 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002580 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002581 break;
2582 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002583 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002584 break;
2585 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002586 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002587 break;
2588 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002589 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002590 break;
2591 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002592 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002593 break;
2594 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002595 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002596 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002597 case OP_APUT_OBJECT:
2598 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2599 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002600 case OP_APUT_SHORT:
2601 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002602 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002603 break;
2604 case OP_APUT_BYTE:
2605 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002606 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002607 break;
2608 default:
2609 return true;
2610 }
2611 return false;
2612}
2613
Ben Cheng6c10a972009-10-29 14:39:18 -07002614/*
2615 * Find the matching case.
2616 *
2617 * return values:
2618 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2619 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2620 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2621 * above MAX_CHAINED_SWITCH_CASES).
2622 *
2623 * Instructions around the call are:
2624 *
2625 * mov r2, pc
2626 * blx &findPackedSwitchIndex
2627 * mov pc, r0
2628 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002629 * chaining cell for case 0 [12 bytes]
2630 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002631 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002632 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002633 * chaining cell for case default [8 bytes]
2634 * noChain exit
2635 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002636static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002637{
2638 int size;
2639 int firstKey;
2640 const int *entries;
2641 int index;
2642 int jumpIndex;
2643 int caseDPCOffset = 0;
2644 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2645 int chainingPC = (pc + 4) & ~3;
2646
2647 /*
2648 * Packed switch data format:
2649 * ushort ident = 0x0100 magic value
2650 * ushort size number of entries in the table
2651 * int first_key first (and lowest) switch case value
2652 * int targets[size] branch targets, relative to switch opcode
2653 *
2654 * Total size is (4+size*2) 16-bit code units.
2655 */
2656 size = switchData[1];
2657 assert(size > 0);
2658
2659 firstKey = switchData[2];
2660 firstKey |= switchData[3] << 16;
2661
2662
2663 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2664 * we can treat them as a native int array.
2665 */
2666 entries = (const int*) &switchData[4];
2667 assert(((u4)entries & 0x3) == 0);
2668
2669 index = testVal - firstKey;
2670
2671 /* Jump to the default cell */
2672 if (index < 0 || index >= size) {
2673 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2674 /* Jump to the non-chaining exit point */
2675 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2676 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2677 caseDPCOffset = entries[index];
2678 /* Jump to the inline chaining cell */
2679 } else {
2680 jumpIndex = index;
2681 }
2682
Bill Buzbeebd047242010-05-13 13:02:53 -07002683 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002684 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2685}
2686
2687/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002688static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002689{
2690 int size;
2691 const int *keys;
2692 const int *entries;
2693 int chainingPC = (pc + 4) & ~3;
2694 int i;
2695
2696 /*
2697 * Sparse switch data format:
2698 * ushort ident = 0x0200 magic value
2699 * ushort size number of entries in the table; > 0
2700 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2701 * int targets[size] branch targets, relative to switch opcode
2702 *
2703 * Total size is (2+size*4) 16-bit code units.
2704 */
2705
2706 size = switchData[1];
2707 assert(size > 0);
2708
2709 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2710 * we can treat them as a native int array.
2711 */
2712 keys = (const int*) &switchData[2];
2713 assert(((u4)keys & 0x3) == 0);
2714
2715 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2716 * we can treat them as a native int array.
2717 */
2718 entries = keys + size;
2719 assert(((u4)entries & 0x3) == 0);
2720
2721 /*
2722 * Run through the list of keys, which are guaranteed to
2723 * be sorted low-to-high.
2724 *
2725 * Most tables have 3-4 entries. Few have more than 10. A binary
2726 * search here is probably not useful.
2727 */
2728 for (i = 0; i < size; i++) {
2729 int k = keys[i];
2730 if (k == testVal) {
2731 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2732 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2733 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002734 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002735 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2736 } else if (k > testVal) {
2737 break;
2738 }
2739 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002740 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2741 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002742}
2743
Ben Chengba4fc8b2009-06-01 13:00:29 -07002744static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2745{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002746 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2747 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002748 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002749 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002750 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002751 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002752 genExportPC(cUnit, mir);
2753 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002754 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002755 loadConstant(cUnit, r1,
2756 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002757 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002758 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002759 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002760 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002761 loadConstant(cUnit, r0,
2762 (int) (cUnit->method->insns + mir->offset));
2763 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2764 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2765 target->defMask = ENCODE_ALL;
2766 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002767 break;
2768 }
2769 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002770 * Compute the goto target of up to
2771 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2772 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002773 */
2774 case OP_PACKED_SWITCH:
2775 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002776 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2777 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002778 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002779 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002780 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002781 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002782 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002783 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002784 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002785 /* r0 <- Addr of the switch data */
2786 loadConstant(cUnit, r0,
2787 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2788 /* r2 <- pc of the instruction following the blx */
2789 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002790 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002791 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002792 /* pc <- computed goto target */
2793 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002794 break;
2795 }
2796 default:
2797 return true;
2798 }
2799 return false;
2800}
2801
Ben Cheng7a2697d2010-06-07 13:44:23 -07002802/*
2803 * See the example of predicted inlining listed before the
2804 * genValidationForPredictedInline function. The function here takes care the
2805 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2806 */
2807static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2808 BasicBlock *bb,
2809 ArmLIR *labelList)
2810{
2811 BasicBlock *fallThrough = bb->fallThrough;
2812
2813 /* Bypass the move-result block if there is one */
2814 if (fallThrough->firstMIRInsn) {
2815 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2816 fallThrough = fallThrough->fallThrough;
2817 }
2818 /* Generate a branch over if the predicted inlining is correct */
2819 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2820
2821 /* Reset the register state */
2822 dvmCompilerResetRegPool(cUnit);
2823 dvmCompilerClobberAllRegs(cUnit);
2824 dvmCompilerResetNullCheck(cUnit);
2825
2826 /* Target for the slow invoke path */
2827 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2828 target->defMask = ENCODE_ALL;
2829 /* Hook up the target to the verification branch */
2830 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2831}
2832
jeffhao71eee1f2011-01-04 14:18:54 -08002833static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2834 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002835{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002836 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002837 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002838
Ben Cheng7a2697d2010-06-07 13:44:23 -07002839 /* An invoke with the MIR_INLINED is effectively a no-op */
2840 if (mir->OptimizationFlags & MIR_INLINED)
2841 return false;
2842
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002843 if (bb->fallThrough != NULL)
2844 retChainingCell = &labelList[bb->fallThrough->id];
2845
Ben Chengba4fc8b2009-06-01 13:00:29 -07002846 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002847 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002848 /*
2849 * calleeMethod = this->clazz->vtable[
2850 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2851 * ]
2852 */
2853 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002854 case OP_INVOKE_VIRTUAL_RANGE:
2855 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002856 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002857 int methodIndex =
2858 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2859 methodIndex;
2860
Ben Cheng7a2697d2010-06-07 13:44:23 -07002861 /*
2862 * If the invoke has non-null misPredBranchOver, we need to generate
2863 * the non-inlined version of the invoke here to handle the
2864 * mispredicted case.
2865 */
2866 if (mir->meta.callsiteInfo->misPredBranchOver) {
2867 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
2868 }
2869
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002870 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002871 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2872 else
2873 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2874
Ben Cheng38329f52009-07-07 14:19:20 -07002875 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2876 retChainingCell,
2877 predChainingCell,
2878 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002879 break;
2880 }
2881 /*
2882 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2883 * ->pResMethods[BBBB]->methodIndex]
2884 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002885 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08002886 case OP_INVOKE_SUPER_RANGE:
2887 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002888 /* Grab the method ptr directly from what the interpreter sees */
2889 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2890 assert(calleeMethod == cUnit->method->clazz->super->vtable[
2891 cUnit->method->clazz->pDvmDex->
2892 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002893
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002894 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002895 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2896 else
2897 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2898
2899 /* r0 = calleeMethod */
2900 loadConstant(cUnit, r0, (int) calleeMethod);
2901
Ben Cheng38329f52009-07-07 14:19:20 -07002902 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2903 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002904 break;
2905 }
2906 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2907 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002908 case OP_INVOKE_DIRECT_RANGE:
2909 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002910 /* Grab the method ptr directly from what the interpreter sees */
2911 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2912 assert(calleeMethod ==
2913 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002914
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002915 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002916 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2917 else
2918 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2919
2920 /* r0 = calleeMethod */
2921 loadConstant(cUnit, r0, (int) calleeMethod);
2922
Ben Cheng38329f52009-07-07 14:19:20 -07002923 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2924 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002925 break;
2926 }
2927 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2928 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08002929 case OP_INVOKE_STATIC_RANGE:
2930 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002931 /* Grab the method ptr directly from what the interpreter sees */
2932 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2933 assert(calleeMethod ==
2934 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002935
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002936 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002937 genProcessArgsNoRange(cUnit, mir, dInsn,
2938 NULL /* no null check */);
2939 else
2940 genProcessArgsRange(cUnit, mir, dInsn,
2941 NULL /* no null check */);
2942
2943 /* r0 = calleeMethod */
2944 loadConstant(cUnit, r0, (int) calleeMethod);
2945
Ben Cheng38329f52009-07-07 14:19:20 -07002946 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2947 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002948 break;
2949 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002950 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002951 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2952 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002953 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002954 * The following is an example of generated code for
2955 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002956 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002957 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2958 * 0x47357e36 : ldr r0, [r5, #0] --+
2959 * 0x47357e38 : sub r7,r5,#24 |
2960 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2961 * 0x47357e3e : beq 0x47357e82 |
2962 * 0x47357e40 : stmia r7, <r0> --+
2963 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2964 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2965 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2966 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2967 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2968 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2969 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2970 * 0x47357e50 : mov r8, r1 --+
2971 * 0x47357e52 : mov r9, r2 |
2972 * 0x47357e54 : ldr r2, [pc, #96] |
2973 * 0x47357e56 : mov r10, r3 |
2974 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2975 * 0x47357e5a : ldr r3, [pc, #88] |
2976 * 0x47357e5c : ldr r7, [pc, #80] |
2977 * 0x47357e5e : mov r1, #1452 |
2978 * 0x47357e62 : blx r7 --+
2979 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2980 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2981 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2982 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2983 * 0x47357e6c : blx_2 see above --+ COMMON
2984 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2985 * 0x47357e70 : cmp r1, #0 --> compare against 0
2986 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08002987 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07002988 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2989 * 0x47357e78 : mov r3, r10 |
2990 * 0x47357e7a : blx r7 --+
2991 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2992 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2993 * 0x47357e80 : blx_2 see above --+
2994 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2995 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002996 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002997 * 0x47357e84 : ldr r1, [r6, #92]
2998 * 0x47357e86 : blx r1
2999 * 0x47357e88 : .align4
3000 * -------- chaining cell (hot): 0x000b
3001 * 0x47357e88 : ldr r0, [r6, #104]
3002 * 0x47357e8a : blx r0
3003 * 0x47357e8c : data 0x19e2(6626)
3004 * 0x47357e8e : data 0x4257(16983)
3005 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003006 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003007 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3008 * 0x47357e92 : data 0x0000(0)
3009 * 0x47357e94 : data 0x0000(0) --> class
3010 * 0x47357e96 : data 0x0000(0)
3011 * 0x47357e98 : data 0x0000(0) --> method
3012 * 0x47357e9a : data 0x0000(0)
3013 * 0x47357e9c : data 0x0000(0) --> rechain count
3014 * 0x47357e9e : data 0x0000(0)
3015 * -------- end of chaining cells (0x006c)
3016 * 0x47357eb0 : .word (0xad03e369)
3017 * 0x47357eb4 : .word (0x28a90)
3018 * 0x47357eb8 : .word (0x41a63394)
3019 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003020 */
3021 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003022 case OP_INVOKE_INTERFACE_RANGE:
3023 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003024 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003025
Ben Cheng7a2697d2010-06-07 13:44:23 -07003026 /*
3027 * If the invoke has non-null misPredBranchOver, we need to generate
3028 * the non-inlined version of the invoke here to handle the
3029 * mispredicted case.
3030 */
3031 if (mir->meta.callsiteInfo->misPredBranchOver) {
3032 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3033 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003034
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003035 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003036 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3037 else
3038 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3039
Ben Cheng38329f52009-07-07 14:19:20 -07003040 /* "this" is already left in r0 by genProcessArgs* */
3041
3042 /* r4PC = dalvikCallsite */
3043 loadConstant(cUnit, r4PC,
3044 (int) (cUnit->method->insns + mir->offset));
3045
3046 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003047 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003048 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003049 addrRetChain->generic.target = (LIR *) retChainingCell;
3050
3051 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003052 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003053 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003054 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3055
3056 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
3057
3058 /* return through lr - jump to the chaining cell */
3059 genUnconditionalBranch(cUnit, predChainingCell);
3060
3061 /*
3062 * null-check on "this" may have been eliminated, but we still need
3063 * a PC-reconstruction label for stack overflow bailout.
3064 */
3065 if (pcrLabel == NULL) {
3066 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003067 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003068 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003069 pcrLabel->operands[0] = dPC;
3070 pcrLabel->operands[1] = mir->offset;
3071 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003072 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3073 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003074 }
3075
3076 /* return through lr+2 - punt to the interpreter */
3077 genUnconditionalBranch(cUnit, pcrLabel);
3078
3079 /*
3080 * return through lr+4 - fully resolve the callee method.
3081 * r1 <- count
3082 * r2 <- &predictedChainCell
3083 * r3 <- this->class
3084 * r4 <- dPC
3085 * r7 <- this->class->vtable
3086 */
3087
3088 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003089 genRegCopy(cUnit, r8, r1);
3090 genRegCopy(cUnit, r9, r2);
3091 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003092
Ben Chengba4fc8b2009-06-01 13:00:29 -07003093 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003094 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003095
3096 /* r1 = BBBB */
3097 loadConstant(cUnit, r1, dInsn->vB);
3098
3099 /* r2 = method (caller) */
3100 loadConstant(cUnit, r2, (int) cUnit->method);
3101
3102 /* r3 = pDvmDex */
3103 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3104
Ben Chengbd1326d2010-04-02 15:04:53 -07003105 LOAD_FUNC_ADDR(cUnit, r7,
3106 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003107 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003108 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3109
Ben Cheng09e50c92010-05-02 10:45:32 -07003110 dvmCompilerClobberCallRegs(cUnit);
3111 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003112 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003113 /*
3114 * calleeMethod == NULL -> throw
3115 */
3116 loadConstant(cUnit, r0,
3117 (int) (cUnit->method->insns + mir->offset));
3118 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3119 /* noreturn */
3120
3121 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3122 target->defMask = ENCODE_ALL;
3123 branchOver->generic.target = (LIR *) target;
3124
Bill Buzbee1465db52009-09-23 17:17:35 -07003125 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003126
Ben Cheng38329f52009-07-07 14:19:20 -07003127 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003128 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3129 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003130
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003131 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003132
Ben Chengb88ec3c2010-05-17 12:50:33 -07003133 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07003134 genRegCopy(cUnit, r2, r9);
3135 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003136
3137 /*
3138 * r0 = calleeMethod
3139 * r2 = &predictedChainingCell
3140 * r3 = class
3141 *
3142 * &returnChainingCell has been loaded into r1 but is not needed
3143 * when patching the chaining cell and will be clobbered upon
3144 * returning so it will be reconstructed again.
3145 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003146 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003147
3148 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003149 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003150 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003151
3152 bypassRechaining->generic.target = (LIR *) addrRetChain;
3153
Ben Chengba4fc8b2009-06-01 13:00:29 -07003154 /*
3155 * r0 = this, r1 = calleeMethod,
3156 * r1 = &ChainingCell,
3157 * r4PC = callsiteDPC,
3158 */
3159 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003160#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003161 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003162#endif
3163 /* Handle exceptions using the interpreter */
3164 genTrap(cUnit, mir->offset, pcrLabel);
3165 break;
3166 }
3167 /* NOP */
3168 case OP_INVOKE_DIRECT_EMPTY: {
3169 return false;
3170 }
3171 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003172 case OP_FILLED_NEW_ARRAY_RANGE:
3173 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003174 /* Just let the interpreter deal with these */
3175 genInterpSingleStep(cUnit, mir);
3176 break;
3177 }
3178 default:
3179 return true;
3180 }
3181 return false;
3182}
3183
3184static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003185 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003186{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003187 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003188
Ben Cheng7a2697d2010-06-07 13:44:23 -07003189 /* An invoke with the MIR_INLINED is effectively a no-op */
3190 if (mir->OptimizationFlags & MIR_INLINED)
3191 return false;
3192
Ben Chengba4fc8b2009-06-01 13:00:29 -07003193 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003194 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003195 /* calleeMethod = this->clazz->vtable[BBBB] */
3196 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3197 case OP_INVOKE_VIRTUAL_QUICK: {
3198 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003199 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3200 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003201
3202 /*
3203 * If the invoke has non-null misPredBranchOver, we need to generate
3204 * the non-inlined version of the invoke here to handle the
3205 * mispredicted case.
3206 */
3207 if (mir->meta.callsiteInfo->misPredBranchOver) {
3208 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3209 }
3210
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003211 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003212 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3213 else
3214 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3215
Ben Cheng38329f52009-07-07 14:19:20 -07003216 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3217 retChainingCell,
3218 predChainingCell,
3219 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003220 break;
3221 }
3222 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3223 case OP_INVOKE_SUPER_QUICK:
3224 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003225 /* Grab the method ptr directly from what the interpreter sees */
3226 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3227 assert(calleeMethod ==
3228 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003229
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003230 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003231 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3232 else
3233 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3234
3235 /* r0 = calleeMethod */
3236 loadConstant(cUnit, r0, (int) calleeMethod);
3237
Ben Cheng38329f52009-07-07 14:19:20 -07003238 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3239 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003240 break;
3241 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003242 default:
3243 return true;
3244 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003245 return false;
3246}
3247
3248/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003249 * This operation is complex enough that we'll do it partly inline
3250 * and partly with a handler. NOTE: the handler uses hardcoded
3251 * values for string object offsets and must be revisitied if the
3252 * layout changes.
3253 */
3254static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3255{
3256#if defined(USE_GLOBAL_STRING_DEFS)
3257 return false;
3258#else
3259 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003260 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3261 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003262
3263 loadValueDirectFixed(cUnit, rlThis, r0);
3264 loadValueDirectFixed(cUnit, rlComp, r1);
3265 /* Test objects for NULL */
3266 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3267 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3268 /*
3269 * TUNING: we could check for object pointer equality before invoking
3270 * handler. Unclear whether the gain would be worth the added code size
3271 * expansion.
3272 */
3273 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003274 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3275 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003276 return true;
3277#endif
3278}
3279
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003280static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003281{
3282#if defined(USE_GLOBAL_STRING_DEFS)
3283 return false;
3284#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003285 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3286 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003287
3288 loadValueDirectFixed(cUnit, rlThis, r0);
3289 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003290 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3291 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003292 /* Test objects for NULL */
3293 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3294 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003295 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3296 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003297 return true;
3298#endif
3299}
3300
Elliott Hughesee34f592010-04-05 18:13:52 -07003301// Generates an inlined String.isEmpty or String.length.
3302static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3303 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003304{
Elliott Hughesee34f592010-04-05 18:13:52 -07003305 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003306 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3307 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3308 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3309 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3310 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3311 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3312 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003313 if (isEmpty) {
3314 // dst = (dst == 0);
3315 int tReg = dvmCompilerAllocTemp(cUnit);
3316 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3317 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3318 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003319 storeValue(cUnit, rlDest, rlResult);
3320 return false;
3321}
3322
Elliott Hughesee34f592010-04-05 18:13:52 -07003323static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3324{
3325 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3326}
3327
3328static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3329{
3330 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3331}
3332
Bill Buzbee1f748632010-03-02 16:14:41 -08003333static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3334{
3335 int contents = offsetof(ArrayObject, contents);
3336 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3337 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3338 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3339 RegLocation rlResult;
3340 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3341 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3342 int regMax = dvmCompilerAllocTemp(cUnit);
3343 int regOff = dvmCompilerAllocTemp(cUnit);
3344 int regPtr = dvmCompilerAllocTemp(cUnit);
3345 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3346 mir->offset, NULL);
3347 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3348 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3349 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3350 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3351 dvmCompilerFreeTemp(cUnit, regMax);
3352 opRegImm(cUnit, kOpAdd, regPtr, contents);
3353 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3354 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3355 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3356 storeValue(cUnit, rlDest, rlResult);
3357 return false;
3358}
3359
3360static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3361{
3362 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3363 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003364 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003365 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3366 int signReg = dvmCompilerAllocTemp(cUnit);
3367 /*
3368 * abs(x) = y<=x>>31, (x+y)^y.
3369 * Thumb2's IT block also yields 3 instructions, but imposes
3370 * scheduling constraints.
3371 */
3372 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3373 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3374 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3375 storeValue(cUnit, rlDest, rlResult);
3376 return false;
3377}
3378
3379static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3380{
3381 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3382 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3383 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3384 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3385 int signReg = dvmCompilerAllocTemp(cUnit);
3386 /*
3387 * abs(x) = y<=x>>31, (x+y)^y.
3388 * Thumb2 IT block allows slightly shorter sequence,
3389 * but introduces a scheduling barrier. Stick with this
3390 * mechanism for now.
3391 */
3392 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3393 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3394 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3395 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3396 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3397 storeValueWide(cUnit, rlDest, rlResult);
3398 return false;
3399}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003400
Elliott Hughese22bd842010-08-20 18:47:36 -07003401static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3402{
3403 // Just move from source to destination...
3404 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3405 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3406 storeValue(cUnit, rlDest, rlSrc);
3407 return false;
3408}
3409
3410static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3411{
3412 // Just move from source to destination...
3413 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3414 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3415 storeValueWide(cUnit, rlDest, rlSrc);
3416 return false;
3417}
3418
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003419/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003420 * NOTE: Handles both range and non-range versions (arguments
3421 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003422 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003423static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003424{
3425 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003426 switch( mir->dalvikInsn.opcode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003427 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003428 case OP_EXECUTE_INLINE: {
3429 unsigned int i;
3430 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003431 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003432 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003433 switch (operation) {
3434 case INLINE_EMPTYINLINEMETHOD:
3435 return false; /* Nop */
3436 case INLINE_STRING_LENGTH:
3437 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003438 case INLINE_STRING_IS_EMPTY:
3439 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003440 case INLINE_MATH_ABS_INT:
3441 return genInlinedAbsInt(cUnit, mir);
3442 case INLINE_MATH_ABS_LONG:
3443 return genInlinedAbsLong(cUnit, mir);
3444 case INLINE_MATH_MIN_INT:
3445 return genInlinedMinMaxInt(cUnit, mir, true);
3446 case INLINE_MATH_MAX_INT:
3447 return genInlinedMinMaxInt(cUnit, mir, false);
3448 case INLINE_STRING_CHARAT:
3449 return genInlinedStringCharAt(cUnit, mir);
3450 case INLINE_MATH_SQRT:
3451 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003452 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003453 else
3454 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003455 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003456 if (genInlinedAbsFloat(cUnit, mir))
3457 return false;
3458 else
3459 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003460 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003461 if (genInlinedAbsDouble(cUnit, mir))
3462 return false;
3463 else
3464 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003465 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003466 if (genInlinedCompareTo(cUnit, mir))
3467 return false;
3468 else
3469 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003470 case INLINE_STRING_FASTINDEXOF_II:
3471 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003472 return false;
3473 else
3474 break;
Elliott Hughese22bd842010-08-20 18:47:36 -07003475 case INLINE_FLOAT_TO_RAW_INT_BITS:
3476 case INLINE_INT_BITS_TO_FLOAT:
3477 return genInlinedIntFloatConversion(cUnit, mir);
3478 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3479 case INLINE_LONG_BITS_TO_DOUBLE:
3480 return genInlinedLongDoubleConversion(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003481 case INLINE_STRING_EQUALS:
3482 case INLINE_MATH_COS:
3483 case INLINE_MATH_SIN:
Elliott Hughese22bd842010-08-20 18:47:36 -07003484 case INLINE_FLOAT_TO_INT_BITS:
3485 case INLINE_DOUBLE_TO_LONG_BITS:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003486 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003487 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003488 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003489 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003490 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003491 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003492 dvmCompilerClobber(cUnit, r4PC);
3493 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003494 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3495 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003496 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003497 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003498 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003499 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003500 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003501 opReg(cUnit, kOpBlx, r4PC);
3502 opRegImm(cUnit, kOpAdd, r13, 8);
buzbee8f8109a2010-08-31 10:16:35 -07003503 /* NULL? */
3504 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeece46c942009-11-20 15:41:34 -08003505 loadConstant(cUnit, r0,
3506 (int) (cUnit->method->insns + mir->offset));
3507 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3508 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3509 target->defMask = ENCODE_ALL;
3510 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003511 break;
3512 }
3513 default:
3514 return true;
3515 }
3516 return false;
3517}
3518
3519static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3520{
Bill Buzbee1465db52009-09-23 17:17:35 -07003521 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003522 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3523 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003524 loadConstantNoClobber(cUnit, rlResult.lowReg,
3525 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3526 loadConstantNoClobber(cUnit, rlResult.highReg,
3527 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003528 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003529 return false;
3530}
3531
Ben Chengba4fc8b2009-06-01 13:00:29 -07003532/*
3533 * The following are special processing routines that handle transfer of
3534 * controls between compiled code and the interpreter. Certain VM states like
3535 * Dalvik PC and special-purpose registers are reconstructed here.
3536 */
3537
Bill Buzbeebd047242010-05-13 13:02:53 -07003538/*
3539 * Insert a
3540 * b .+4
3541 * nop
3542 * pair at the beginning of a chaining cell. This serves as the
3543 * switch branch that selects between reverting to the interpreter or
3544 * not. Once the cell is chained to a translation, the cell will
3545 * contain a 32-bit branch. Subsequent chain/unchain operations will
3546 * then only alter that first 16-bits - the "b .+4" for unchaining,
3547 * and the restoration of the first half of the 32-bit branch for
3548 * rechaining.
3549 */
3550static void insertChainingSwitch(CompilationUnit *cUnit)
3551{
3552 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3553 newLIR2(cUnit, kThumbOrr, r0, r0);
3554 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3555 target->defMask = ENCODE_ALL;
3556 branch->generic.target = (LIR *) target;
3557}
3558
Ben Cheng1efc9c52009-06-08 18:25:27 -07003559/* Chaining cell for code that may need warmup. */
3560static void handleNormalChainingCell(CompilationUnit *cUnit,
3561 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003562{
Ben Cheng11d8f142010-03-24 15:24:19 -07003563 /*
3564 * Use raw instruction constructors to guarantee that the generated
3565 * instructions fit the predefined cell size.
3566 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003567 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003568 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3569 offsetof(InterpState,
3570 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3571 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003572 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3573}
3574
3575/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003576 * Chaining cell for instructions that immediately following already translated
3577 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003578 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003579static void handleHotChainingCell(CompilationUnit *cUnit,
3580 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003581{
Ben Cheng11d8f142010-03-24 15:24:19 -07003582 /*
3583 * Use raw instruction constructors to guarantee that the generated
3584 * instructions fit the predefined cell size.
3585 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003586 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003587 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3588 offsetof(InterpState,
3589 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3590 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003591 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3592}
3593
Jeff Hao97319a82009-08-12 16:57:15 -07003594/* Chaining cell for branches that branch back into the same basic block */
3595static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3596 unsigned int offset)
3597{
Ben Cheng11d8f142010-03-24 15:24:19 -07003598 /*
3599 * Use raw instruction constructors to guarantee that the generated
3600 * instructions fit the predefined cell size.
3601 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003602 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003603#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003604 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003605 offsetof(InterpState,
3606 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003607#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003608 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003609 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3610#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003611 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003612 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3613}
3614
Ben Chengba4fc8b2009-06-01 13:00:29 -07003615/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003616static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3617 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003618{
Ben Cheng11d8f142010-03-24 15:24:19 -07003619 /*
3620 * Use raw instruction constructors to guarantee that the generated
3621 * instructions fit the predefined cell size.
3622 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003623 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003624 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3625 offsetof(InterpState,
3626 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3627 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003628 addWordData(cUnit, (int) (callee->insns), true);
3629}
3630
Ben Cheng38329f52009-07-07 14:19:20 -07003631/* Chaining cell for monomorphic method invocations. */
3632static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3633{
3634
3635 /* Should not be executed in the initial state */
3636 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3637 /* To be filled: class */
3638 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3639 /* To be filled: method */
3640 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3641 /*
3642 * Rechain count. The initial value of 0 here will trigger chaining upon
3643 * the first invocation of this callsite.
3644 */
3645 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3646}
3647
Ben Chengba4fc8b2009-06-01 13:00:29 -07003648/* Load the Dalvik PC into r0 and jump to the specified target */
3649static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003650 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003651{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003652 ArmLIR **pcrLabel =
3653 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003654 int numElems = cUnit->pcReconstructionList.numUsed;
3655 int i;
3656 for (i = 0; i < numElems; i++) {
3657 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3658 /* r0 = dalvik PC */
3659 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3660 genUnconditionalBranch(cUnit, targetLabel);
3661 }
3662}
3663
Bill Buzbee1465db52009-09-23 17:17:35 -07003664static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3665 "kMirOpPhi",
3666 "kMirOpNullNRangeUpCheck",
3667 "kMirOpNullNRangeDownCheck",
3668 "kMirOpLowerBound",
3669 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003670 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003671};
3672
3673/*
3674 * vA = arrayReg;
3675 * vB = idxReg;
3676 * vC = endConditionReg;
3677 * arg[0] = maxC
3678 * arg[1] = minC
3679 * arg[2] = loopBranchConditionCode
3680 */
3681static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3682{
Bill Buzbee1465db52009-09-23 17:17:35 -07003683 /*
3684 * NOTE: these synthesized blocks don't have ssa names assigned
3685 * for Dalvik registers. However, because they dominate the following
3686 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3687 * ssa name.
3688 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003689 DecodedInstruction *dInsn = &mir->dalvikInsn;
3690 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003691 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003692 int regLength;
3693 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3694 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003695
3696 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003697 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3698 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3699 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003700 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3701
3702 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003703 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003704 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003705
3706 int delta = maxC;
3707 /*
3708 * If the loop end condition is ">=" instead of ">", then the largest value
3709 * of the index is "endCondition - 1".
3710 */
3711 if (dInsn->arg[2] == OP_IF_GE) {
3712 delta--;
3713 }
3714
3715 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003716 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003717 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3718 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003719 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003720 }
3721 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003722 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003723 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003724}
3725
3726/*
3727 * vA = arrayReg;
3728 * vB = idxReg;
3729 * vC = endConditionReg;
3730 * arg[0] = maxC
3731 * arg[1] = minC
3732 * arg[2] = loopBranchConditionCode
3733 */
3734static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3735{
3736 DecodedInstruction *dInsn = &mir->dalvikInsn;
3737 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003738 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003739 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003740 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3741 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003742
3743 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003744 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3745 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3746 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003747 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3748
3749 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003750 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003751
3752 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003753 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003754 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3755 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003756 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003757 }
3758
3759 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003760 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003761 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003762}
3763
3764/*
3765 * vA = idxReg;
3766 * vB = minC;
3767 */
3768static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3769{
3770 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003771 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003772 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003773
3774 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003775 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003776
3777 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003778 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003779 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3780}
3781
Ben Cheng7a2697d2010-06-07 13:44:23 -07003782/*
3783 * vC = this
3784 *
3785 * A predicted inlining target looks like the following, where instructions
3786 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3787 * matches "this", and the verificaion code is generated by this routine.
3788 *
3789 * (C) means the instruction is inlined from the callee, and (PI) means the
3790 * instruction is the predicted inlined invoke, whose corresponding
3791 * instructions are still generated to handle the mispredicted case.
3792 *
3793 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3794 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3795 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3796 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3797 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3798 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3799 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3800 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3801 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3802 * v4, v17, (#8)
3803 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3804 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3805 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3806 * +invoke-virtual-quick/range (PI) v17..v17
3807 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3808 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3809 * D/dalvikvm( 86): -------- BARRIER
3810 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3811 * D/dalvikvm( 86): -------- BARRIER
3812 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3813 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3814 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3815 * D/dalvikvm( 86): -------- BARRIER
3816 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3817 * D/dalvikvm( 86): -------- BARRIER
3818 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3819 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3820 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3821 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3822 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3823 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3824 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3825 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3826 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3827 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
3828 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
3829 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
3830 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
3831 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
3832 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
3833 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
3834 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
3835 * D/dalvikvm( 86): 0x4858deac (0048): .align4
3836 * D/dalvikvm( 86): L0x004f:
3837 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
3838 * v4, (#0), (#0)
3839 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
3840 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
3841 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
3842 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3843 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
3844 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
3845 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3846 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
3847 * D/dalvikvm( 86): Exception_Handling:
3848 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
3849 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
3850 * D/dalvikvm( 86): 0x4858debc (0058): .align4
3851 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
3852 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
3853 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
3854 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
3855 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
3856 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
3857 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
3858 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
3859 * D/dalvikvm( 86): -------- chaining cell (predicted)
3860 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
3861 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
3862 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
3863 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
3864 * :
3865 */
3866static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
3867{
3868 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3869 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
3870
3871 rlThis = loadValue(cUnit, rlThis, kCoreReg);
3872 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
3873 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
3874 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
3875 NULL);/* null object? */
3876 int regActualClass = dvmCompilerAllocTemp(cUnit);
3877 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
3878 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
3879 /*
3880 * Set the misPredBranchOver target so that it will be generated when the
3881 * code for the non-optimized invoke is generated.
3882 */
3883 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
3884}
3885
Ben Cheng4238ec22009-08-24 16:32:22 -07003886/* Extended MIR instructions like PHI */
3887static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3888{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003889 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003890 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3891 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07003892 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003893 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003894
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003895 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003896 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003897 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003898 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003899 break;
3900 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003901 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003902 genHoistedChecksForCountUpLoop(cUnit, mir);
3903 break;
3904 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003905 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003906 genHoistedChecksForCountDownLoop(cUnit, mir);
3907 break;
3908 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003909 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003910 genHoistedLowerBoundCheck(cUnit, mir);
3911 break;
3912 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003913 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003914 genUnconditionalBranch(cUnit,
3915 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3916 break;
3917 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07003918 case kMirOpCheckInlinePrediction: {
3919 genValidationForPredictedInline(cUnit, mir);
3920 break;
3921 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003922 default:
3923 break;
3924 }
3925}
3926
3927/*
3928 * Create a PC-reconstruction cell for the starting offset of this trace.
3929 * Since the PCR cell is placed near the end of the compiled code which is
3930 * usually out of range for a conditional branch, we put two branches (one
3931 * branch over to the loop body and one layover branch to the actual PCR) at the
3932 * end of the entry block.
3933 */
3934static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3935 ArmLIR *bodyLabel)
3936{
3937 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003938 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003939 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003940 pcrLabel->operands[0] =
3941 (int) (cUnit->method->insns + entry->startOffset);
3942 pcrLabel->operands[1] = entry->startOffset;
3943 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003944 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07003945
3946 /*
3947 * Next, create two branches - one branch over to the loop body and the
3948 * other branch to the PCR cell to punt.
3949 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003950 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003951 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003952 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003953 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003954 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3955
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003956 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003957 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003958 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003959 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003960 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3961}
3962
Ben Chengd5adae12010-03-26 17:45:28 -07003963#if defined(WITH_SELF_VERIFICATION)
3964static bool selfVerificationPuntOps(MIR *mir)
3965{
3966 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003967 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07003968
Ben Chengd5adae12010-03-26 17:45:28 -07003969 /*
3970 * All opcodes that can throw exceptions and use the
3971 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3972 * under self-verification mode.
3973 */
3974 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3975 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3976 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3977 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07003978 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07003979}
3980#endif
3981
Ben Chengba4fc8b2009-06-01 13:00:29 -07003982void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3983{
3984 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003985 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003986 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003987 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003988 int i;
3989
3990 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003991 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003992 */
Ben Chengcec26f62010-01-15 15:29:33 -08003993 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003994 dvmInitGrowableList(&chainingListByType[i], 2);
3995 }
3996
Ben Cheng00603072010-10-28 11:13:58 -07003997 GrowableListIterator iterator;
3998 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003999
buzbee2e152ba2010-12-15 16:32:35 -08004000 /* Traces start with a profiling entry point. Generate it here */
4001 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004002
Ben Chengba4fc8b2009-06-01 13:00:29 -07004003 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004004 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004005 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004006 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4007 if (bb == NULL) break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004008
Ben Cheng00603072010-10-28 11:13:58 -07004009 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004010
Ben Cheng00603072010-10-28 11:13:58 -07004011 if (bb->blockType >= kChainingCellGap) {
4012 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004013 /* Align this block first since it is a return chaining cell */
4014 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4015 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004016 /*
4017 * Append the label pseudo LIR first. Chaining cells will be handled
4018 * separately afterwards.
4019 */
4020 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4021 }
4022
Ben Cheng00603072010-10-28 11:13:58 -07004023 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004024 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004025 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004026 continue;
4027 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004028 setupLoopEntryBlock(cUnit, bb,
4029 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004030 }
Ben Cheng00603072010-10-28 11:13:58 -07004031 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004032 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004033 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004034 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004035 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004036 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004037 dvmCompilerResetRegPool(cUnit);
4038 dvmCompilerClobberAllRegs(cUnit);
4039 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004040 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004041 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004042 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004043 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004044 /* handle the codegen later */
4045 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004046 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004047 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004048 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004049 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004050 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004051 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004052 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004053 /* handle the codegen later */
4054 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004055 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004056 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004057 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004058 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004059 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004060 /* handle the codegen later */
4061 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004062 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004063 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004064 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004065 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004066 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004067 /* handle the codegen later */
4068 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004069 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004070 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004071 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004072 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004073 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004074 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004075 assert (i == cUnit->numBlocks - 2);
4076 handlePCReconstruction(cUnit, &labelList[i+1]);
4077 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004078 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004079 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004080 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07004081 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4082 jitToInterpEntries.dvmJitToInterpPunt),
4083 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004084 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004085 }
4086 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004087 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004088 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004089 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004090 /* handle the codegen later */
4091 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004092 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004093 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004094 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004095 default:
4096 break;
4097 }
4098 continue;
4099 }
Ben Chenge9695e52009-06-16 16:11:47 -07004100
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004101 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07004102
Ben Cheng00603072010-10-28 11:13:58 -07004103 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004104
Bill Buzbeec6f10662010-02-09 11:16:15 -08004105 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004106 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004107 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004108 }
4109
4110 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004111 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004112 }
4113
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004114 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004115 handleExtendedMIR(cUnit, mir);
4116 continue;
4117 }
4118
Bill Buzbee1465db52009-09-23 17:17:35 -07004119
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004120 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Dan Bornsteine4852762010-12-02 12:45:00 -08004121 InstructionFormat dalvikFormat = dexGetFormatFromOpcode(dalvikOpcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004122 char *note;
4123 if (mir->OptimizationFlags & MIR_INLINED) {
4124 note = " (I)";
4125 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4126 note = " (PI)";
4127 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4128 note = " (C)";
4129 } else {
4130 note = NULL;
4131 }
4132
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004133 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07004134 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07004135 mir->offset,
Ben Cheng7a2697d2010-06-07 13:44:23 -07004136 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn,
4137 note));
Ben Cheng4238ec22009-08-24 16:32:22 -07004138 if (mir->ssaRep) {
4139 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07004140 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07004141 }
4142
Ben Chenge9695e52009-06-16 16:11:47 -07004143 /* Remember the first LIR for this block */
4144 if (headLIR == NULL) {
4145 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07004146 /* Set the first boundaryLIR as a scheduling barrier */
4147 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07004148 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004149
Ben Chengba4fc8b2009-06-01 13:00:29 -07004150 bool notHandled;
4151 /*
4152 * Debugging: screen the opcode first to see if it is in the
4153 * do[-not]-compile list
4154 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004155 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004156#if defined(WITH_SELF_VERIFICATION)
4157 if (singleStepMe == false) {
4158 singleStepMe = selfVerificationPuntOps(mir);
4159 }
4160#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004161 if (singleStepMe || cUnit->allSingleStep) {
4162 notHandled = false;
4163 genInterpSingleStep(cUnit, mir);
4164 } else {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004165 opcodeCoverage[dalvikOpcode]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004166 switch (dalvikFormat) {
4167 case kFmt10t:
4168 case kFmt20t:
4169 case kFmt30t:
4170 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004171 mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004172 break;
4173 case kFmt10x:
4174 notHandled = handleFmt10x(cUnit, mir);
4175 break;
4176 case kFmt11n:
4177 case kFmt31i:
4178 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4179 break;
4180 case kFmt11x:
4181 notHandled = handleFmt11x(cUnit, mir);
4182 break;
4183 case kFmt12x:
4184 notHandled = handleFmt12x(cUnit, mir);
4185 break;
4186 case kFmt20bc:
jeffhao71eee1f2011-01-04 14:18:54 -08004187 case kFmt40sc:
4188 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004189 break;
4190 case kFmt21c:
4191 case kFmt31c:
jeffhao71eee1f2011-01-04 14:18:54 -08004192 case kFmt41c:
4193 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004194 break;
4195 case kFmt21h:
4196 notHandled = handleFmt21h(cUnit, mir);
4197 break;
4198 case kFmt21s:
4199 notHandled = handleFmt21s(cUnit, mir);
4200 break;
4201 case kFmt21t:
Ben Cheng00603072010-10-28 11:13:58 -07004202 notHandled = handleFmt21t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004203 break;
4204 case kFmt22b:
4205 case kFmt22s:
4206 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4207 break;
4208 case kFmt22c:
jeffhao71eee1f2011-01-04 14:18:54 -08004209 case kFmt52c:
4210 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004211 break;
4212 case kFmt22cs:
4213 notHandled = handleFmt22cs(cUnit, mir);
4214 break;
4215 case kFmt22t:
Ben Cheng00603072010-10-28 11:13:58 -07004216 notHandled = handleFmt22t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004217 break;
4218 case kFmt22x:
4219 case kFmt32x:
4220 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4221 break;
4222 case kFmt23x:
4223 notHandled = handleFmt23x(cUnit, mir);
4224 break;
4225 case kFmt31t:
4226 notHandled = handleFmt31t(cUnit, mir);
4227 break;
4228 case kFmt3rc:
4229 case kFmt35c:
jeffhao71eee1f2011-01-04 14:18:54 -08004230 case kFmt5rc:
4231 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004232 labelList);
4233 break;
4234 case kFmt3rms:
4235 case kFmt35ms:
Ben Cheng00603072010-10-28 11:13:58 -07004236 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004237 labelList);
4238 break;
Dan Bornstein7b3e9b02010-11-09 17:15:10 -08004239 case kFmt35mi:
4240 case kFmt3rmi:
Bill Buzbeece46c942009-11-20 15:41:34 -08004241 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08004242 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004243 case kFmt51l:
4244 notHandled = handleFmt51l(cUnit, mir);
4245 break;
4246 default:
4247 notHandled = true;
4248 break;
4249 }
4250 }
4251 if (notHandled) {
4252 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4253 mir->offset,
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004254 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07004255 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004256 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004257 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004258 }
4259 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004260
Ben Cheng00603072010-10-28 11:13:58 -07004261 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004262 dvmCompilerAppendLIR(cUnit,
4263 (LIR *) cUnit->loopAnalysis->branchToBody);
4264 dvmCompilerAppendLIR(cUnit,
4265 (LIR *) cUnit->loopAnalysis->branchToPCR);
4266 }
4267
4268 if (headLIR) {
4269 /*
4270 * Eliminate redundant loads/stores and delay stores into later
4271 * slots
4272 */
4273 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4274 cUnit->lastLIRInsn);
4275 }
4276
4277gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004278 /*
4279 * Check if the block is terminated due to trace length constraint -
4280 * insert an unconditional branch to the chaining cell.
4281 */
Ben Cheng00603072010-10-28 11:13:58 -07004282 if (bb->needFallThroughBranch) {
Ben Cheng1efc9c52009-06-08 18:25:27 -07004283 genUnconditionalBranch(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004284 &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004285 }
4286
Ben Chengba4fc8b2009-06-01 13:00:29 -07004287 }
4288
Ben Chenge9695e52009-06-16 16:11:47 -07004289 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004290 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004291 size_t j;
4292 int *blockIdList = (int *) chainingListByType[i].elemList;
4293
4294 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4295
4296 /* No chaining cells of this type */
4297 if (cUnit->numChainingCells[i] == 0)
4298 continue;
4299
4300 /* Record the first LIR for a new type of chaining cell */
4301 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4302
4303 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4304 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004305 BasicBlock *chainingBlock =
4306 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4307 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004308
4309 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004310 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004311
4312 /* Insert the pseudo chaining instruction */
4313 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4314
4315
Ben Cheng00603072010-10-28 11:13:58 -07004316 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004317 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004318 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004319 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004320 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004321 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004322 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004323 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004324 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004325 handleInvokePredictedChainingCell(cUnit);
4326 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004327 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004328 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004329 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004330 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004331 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004332 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004333 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004334 default:
Ben Cheng00603072010-10-28 11:13:58 -07004335 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004336 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004337 }
4338 }
4339 }
Ben Chenge9695e52009-06-16 16:11:47 -07004340
Ben Chengcec26f62010-01-15 15:29:33 -08004341 /* Mark the bottom of chaining cells */
4342 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4343
Ben Cheng6c10a972009-10-29 14:39:18 -07004344 /*
4345 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4346 * of all chaining cells for the overflow cases.
4347 */
4348 if (cUnit->switchOverflowPad) {
4349 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4350 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4351 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4352 opRegReg(cUnit, kOpAdd, r1, r1);
4353 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004354#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004355 loadConstant(cUnit, r0, kSwitchOverflow);
4356#endif
4357 opReg(cUnit, kOpBlx, r2);
4358 }
4359
Ben Chenge9695e52009-06-16 16:11:47 -07004360 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004361
4362#if defined(WITH_SELF_VERIFICATION)
4363 selfVerificationBranchInsertPass(cUnit);
4364#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004365}
4366
buzbee2e152ba2010-12-15 16:32:35 -08004367/*
4368 * Accept the work and start compiling. Returns true if compilation
4369 * is attempted.
4370 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004371bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004372{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004373 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004374 bool isCompile;
4375 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004376
Ben Cheng6999d842010-01-26 16:46:15 -08004377 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004378 return false;
4379 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004380
Ben Chengccd6c012009-10-15 14:52:45 -07004381 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004382 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004383 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004384 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004385 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004386 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4387 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004388 break;
4389 case kWorkOrderTraceDebug: {
4390 bool oldPrintMe = gDvmJit.printMe;
4391 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004392 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004393 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004394 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004395 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4396 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004397 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004398 break;
4399 }
buzbee2e152ba2010-12-15 16:32:35 -08004400 case kWorkOrderProfileMode:
4401 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4402 isCompile = false;
4403 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004404 default:
buzbee2e152ba2010-12-15 16:32:35 -08004405 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004406 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004407 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004408 }
buzbee2e152ba2010-12-15 16:32:35 -08004409 if (!success)
4410 work->result.codeAddress = NULL;
4411 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004412}
4413
Ben Chengba4fc8b2009-06-01 13:00:29 -07004414/* Architectural-specific debugging helpers go here */
4415void dvmCompilerArchDump(void)
4416{
4417 /* Print compiled opcode in this VM instance */
4418 int i, start, streak;
4419 char buf[1024];
4420
4421 streak = i = 0;
4422 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004423 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004424 i++;
4425 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004426 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004427 return;
4428 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004429 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004430 if (opcodeCoverage[i]) {
4431 streak++;
4432 } else {
4433 if (streak == 1) {
4434 sprintf(buf+strlen(buf), "%x,", start);
4435 } else {
4436 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4437 }
4438 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004439 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004440 i++;
4441 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004442 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004443 streak = 1;
4444 start = i;
4445 }
4446 }
4447 }
4448 if (streak) {
4449 if (streak == 1) {
4450 sprintf(buf+strlen(buf), "%x", start);
4451 } else {
4452 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4453 }
4454 }
4455 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004456 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004457 }
4458}
Ben Chengd7d426a2009-09-22 11:23:36 -07004459
4460/* Common initialization routine for an architecture family */
4461bool dvmCompilerArchInit()
4462{
4463 int i;
4464
Bill Buzbee1465db52009-09-23 17:17:35 -07004465 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004466 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004467 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004468 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004469 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004470 }
4471 }
4472
Ben Cheng5d90c202009-11-22 23:31:11 -08004473 return dvmCompilerArchVariantInit();
4474}
4475
4476void *dvmCompilerGetInterpretTemplate()
4477{
4478 return (void*) ((int)gDvmJit.codeCache +
4479 templateEntryOffsets[TEMPLATE_INTERPRET]);
4480}
4481
buzbeebff121a2010-08-04 15:25:06 -07004482/* Needed by the Assembler */
4483void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4484{
4485 setupResourceMasks(lir);
4486}
4487
Ben Cheng5d90c202009-11-22 23:31:11 -08004488/* Needed by the ld/st optmizatons */
4489ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4490{
4491 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4492}
4493
4494/* Needed by the register allocator */
4495ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4496{
4497 return genRegCopy(cUnit, rDest, rSrc);
4498}
4499
4500/* Needed by the register allocator */
4501void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4502 int srcLo, int srcHi)
4503{
4504 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4505}
4506
4507void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4508 int displacement, int rSrc, OpSize size)
4509{
4510 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4511}
4512
4513void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4514 int displacement, int rSrcLo, int rSrcHi)
4515{
4516 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004517}