blob: 7f62816f3154c61929208fad7c5fe514bd387fde [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{
buzbee18fba342011-01-19 15:31:15 -0800907 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
908 TEMPLATE_RETURN_PROF :
909 TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700910#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700911 gDvmJit.returnOp++;
912#endif
913 int dPC = (int) (cUnit->method->insns + mir->offset);
914 /* Insert branch, but defer setting of target */
915 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
916 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800917 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800918 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700919 pcrLabel->operands[0] = dPC;
920 pcrLabel->operands[1] = mir->offset;
921 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -0700922 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Bill Buzbee1465db52009-09-23 17:17:35 -0700923 /* Branch to the PC reconstruction code */
924 branch->generic.target = (LIR *) pcrLabel;
925}
926
Ben Chengba4fc8b2009-06-01 13:00:29 -0700927static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
928 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700929 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700930{
931 unsigned int i;
932 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700933 RegLocation rlArg;
934 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700935
Bill Buzbee1465db52009-09-23 17:17:35 -0700936 /*
937 * Load arguments to r0..r4. Note that these registers may contain
938 * live values, so we clobber them immediately after loading to prevent
939 * them from being used as sources for subsequent loads.
940 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800941 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 for (i = 0; i < dInsn->vA; i++) {
943 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800944 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700945 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700946 }
947 if (regMask) {
948 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700949 opRegRegImm(cUnit, kOpSub, r7, rFP,
950 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700951 /* generate null check */
952 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800953 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700954 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700955 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700956 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700957 }
958}
959
960static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
961 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700962 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700963{
964 int srcOffset = dInsn->vC << 2;
965 int numArgs = dInsn->vA;
966 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700967
968 /*
969 * Note: here, all promoted registers will have been flushed
970 * back to the Dalvik base locations, so register usage restrictins
971 * are lifted. All parms loaded from original Dalvik register
972 * region - even though some might conceivably have valid copies
973 * cached in a preserved register.
974 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800975 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700976
Ben Chengba4fc8b2009-06-01 13:00:29 -0700977 /*
978 * r4PC : &rFP[vC]
979 * r7: &newFP[0]
980 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700981 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982 /* load [r0 .. min(numArgs,4)] */
983 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700984 /*
985 * Protect the loadMultiple instruction from being reordered with other
986 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -0800987 *
988 * This code is also shared by the invoke jumbo instructions, and this
989 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -0700990 */
jeffhao71eee1f2011-01-04 14:18:54 -0800991 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700992
Bill Buzbee1465db52009-09-23 17:17:35 -0700993 opRegRegImm(cUnit, kOpSub, r7, rFP,
994 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700995 /* generate null check */
996 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800997 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700998 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700999 }
1000
1001 /*
1002 * Handle remaining 4n arguments:
1003 * store previously loaded 4 values and load the next 4 values
1004 */
1005 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001006 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007 /*
1008 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001009 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001010 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001011 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001012 /* No need to generate the loop structure if numArgs <= 11 */
1013 if (numArgs > 11) {
1014 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001015 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001016 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001017 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001018 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001019 /*
1020 * Protect the loadMultiple instruction from being reordered with other
1021 * Dalvik stack accesses.
1022 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001023 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001024 /* No need to generate the loop structure if numArgs <= 11 */
1025 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001026 opRegImm(cUnit, kOpSub, rFP, 4);
1027 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001028 }
1029 }
1030
1031 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001032 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001033
1034 /* Generate the loop epilogue - don't use r0 */
1035 if ((numArgs > 4) && (numArgs % 4)) {
1036 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001037 /*
1038 * Protect the loadMultiple instruction from being reordered with other
1039 * Dalvik stack accesses.
1040 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001041 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042 }
1043 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001044 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001045
1046 /* Save the modulo 4 arguments */
1047 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001048 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001049 }
1050}
1051
Ben Cheng38329f52009-07-07 14:19:20 -07001052/*
1053 * Generate code to setup the call stack then jump to the chaining cell if it
1054 * is not a native method.
1055 */
1056static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001057 BasicBlock *bb, ArmLIR *labelList,
1058 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001059 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001060{
Bill Buzbee1465db52009-09-23 17:17:35 -07001061 /*
1062 * Note: all Dalvik register state should be flushed to
1063 * memory by the point, so register usage restrictions no
1064 * longer apply. All temp & preserved registers may be used.
1065 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001066 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001067 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001068
1069 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001070 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001071
Ben Chengba4fc8b2009-06-01 13:00:29 -07001072 /* r4PC = dalvikCallsite */
1073 loadConstant(cUnit, r4PC,
1074 (int) (cUnit->method->insns + mir->offset));
1075 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001076
1077 /* r7 = calleeMethod->registersSize */
1078 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001079 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001080 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001081 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001082 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001083 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001084 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001085 */
1086 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001087 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1088 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1089 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001090#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001091 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001092#endif
1093 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001094 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1095 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001096 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1097 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1098 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001099#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001100 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001101#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001102 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001103 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1104 }
1105 /* Handle exceptions using the interpreter */
1106 genTrap(cUnit, mir->offset, pcrLabel);
1107}
1108
Ben Cheng38329f52009-07-07 14:19:20 -07001109/*
1110 * Generate code to check the validity of a predicted chain and take actions
1111 * based on the result.
1112 *
1113 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1114 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1115 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1116 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1117 * 0x426a99b2 : blx_2 see above --+
1118 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1119 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1120 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1121 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1122 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001123 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001124 * 0x426a99c0 : blx r7 --+
1125 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1126 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1127 * 0x426a99c6 : blx_2 see above --+
1128 */
1129static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1130 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001131 ArmLIR *retChainingCell,
1132 ArmLIR *predChainingCell,
1133 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001134{
Bill Buzbee1465db52009-09-23 17:17:35 -07001135 /*
1136 * Note: all Dalvik register state should be flushed to
1137 * memory by the point, so register usage restrictions no
1138 * longer apply. Lock temps to prevent them from being
1139 * allocated by utility routines.
1140 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001141 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001142
Ben Cheng38329f52009-07-07 14:19:20 -07001143 /* "this" is already left in r0 by genProcessArgs* */
1144
1145 /* r4PC = dalvikCallsite */
1146 loadConstant(cUnit, r4PC,
1147 (int) (cUnit->method->insns + mir->offset));
1148
1149 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001150 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001151 addrRetChain->generic.target = (LIR *) retChainingCell;
1152
1153 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001154 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001155 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1156
buzbee18fba342011-01-19 15:31:15 -08001157 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1158 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1159 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001160
1161 /* return through lr - jump to the chaining cell */
1162 genUnconditionalBranch(cUnit, predChainingCell);
1163
1164 /*
1165 * null-check on "this" may have been eliminated, but we still need a PC-
1166 * reconstruction label for stack overflow bailout.
1167 */
1168 if (pcrLabel == NULL) {
1169 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001170 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001171 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001172 pcrLabel->operands[0] = dPC;
1173 pcrLabel->operands[1] = mir->offset;
1174 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001175 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1176 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001177 }
1178
1179 /* return through lr+2 - punt to the interpreter */
1180 genUnconditionalBranch(cUnit, pcrLabel);
1181
1182 /*
1183 * return through lr+4 - fully resolve the callee method.
1184 * r1 <- count
1185 * r2 <- &predictedChainCell
1186 * r3 <- this->class
1187 * r4 <- dPC
1188 * r7 <- this->class->vtable
1189 */
1190
1191 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001192 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001193
1194 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001195 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001196
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001197 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001198
Ben Chengb88ec3c2010-05-17 12:50:33 -07001199 genRegCopy(cUnit, r1, rGLUE);
1200
Ben Cheng38329f52009-07-07 14:19:20 -07001201 /*
1202 * r0 = calleeMethod
1203 * r2 = &predictedChainingCell
1204 * r3 = class
1205 *
1206 * &returnChainingCell has been loaded into r1 but is not needed
1207 * when patching the chaining cell and will be clobbered upon
1208 * returning so it will be reconstructed again.
1209 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001210 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001211
1212 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001213 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001214 addrRetChain->generic.target = (LIR *) retChainingCell;
1215
1216 bypassRechaining->generic.target = (LIR *) addrRetChain;
1217 /*
1218 * r0 = calleeMethod,
1219 * r1 = &ChainingCell,
1220 * r4PC = callsiteDPC,
1221 */
buzbee18fba342011-01-19 15:31:15 -08001222 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1223 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1224 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001225#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001226 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001227#endif
1228 /* Handle exceptions using the interpreter */
1229 genTrap(cUnit, mir->offset, pcrLabel);
1230}
1231
Ben Chengba4fc8b2009-06-01 13:00:29 -07001232/* Geneate a branch to go back to the interpreter */
1233static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1234{
1235 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001236 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001237 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001238 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1239 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001240 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001241}
1242
1243/*
1244 * Attempt to single step one instruction using the interpreter and return
1245 * to the compiled code for the next Dalvik instruction
1246 */
1247static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1248{
Dan Bornsteine4852762010-12-02 12:45:00 -08001249 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001250 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1251 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001252
Bill Buzbee45273872010-03-11 11:12:15 -08001253 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001254 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001255 return;
1256
Bill Buzbee1465db52009-09-23 17:17:35 -07001257 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001258 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001259
Ben Chengba4fc8b2009-06-01 13:00:29 -07001260 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1261 genPuntToInterp(cUnit, mir->offset);
1262 return;
1263 }
1264 int entryAddr = offsetof(InterpState,
1265 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001266 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001267 /* r0 = dalvik pc */
1268 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1269 /* r1 = dalvik pc of following instruction */
1270 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001271 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001272}
1273
Ben Chengfc075c22010-05-28 15:20:08 -07001274#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1275 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001276/*
1277 * To prevent a thread in a monitor wait from blocking the Jit from
1278 * resetting the code cache, heavyweight monitor lock will not
1279 * be allowed to return to an existing translation. Instead, we will
1280 * handle them by branching to a handler, which will in turn call the
1281 * runtime lock routine and then branch directly back to the
1282 * interpreter main loop. Given the high cost of the heavyweight
1283 * lock operation, this additional cost should be slight (especially when
1284 * considering that we expect the vast majority of lock operations to
1285 * use the fast-path thin lock bypass).
1286 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001287static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001288{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001289 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001290 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001291 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1292 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001293 loadValueDirectFixed(cUnit, rlSrc, r1);
1294 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001295 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001296 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001297 /* Get dPC of next insn */
1298 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001299 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001300#if defined(WITH_DEADLOCK_PREDICTION)
1301 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1302#else
1303 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1304#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001305 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001306 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001307 /* Do the call */
1308 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001309 /* Did we throw? */
1310 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001311 loadConstant(cUnit, r0,
1312 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001313 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001314 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1315 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1316 target->defMask = ENCODE_ALL;
1317 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001318 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001319 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001320}
Ben Chengfc075c22010-05-28 15:20:08 -07001321#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001322
Ben Chengba4fc8b2009-06-01 13:00:29 -07001323/*
1324 * The following are the first-level codegen routines that analyze the format
1325 * of each bytecode then either dispatch special purpose codegen routines
1326 * or produce corresponding Thumb instructions directly.
1327 */
1328
1329static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001330 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001331{
1332 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1333 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1334 return false;
1335}
1336
1337static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1338{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001339 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1340 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1341 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001342 return true;
1343 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001344 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001345 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001346 dvmCompilerGenMemBarrier(cUnit, kST);
1347 // Intentional fallthrough
1348 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001349 genReturnCommon(cUnit,mir);
1350 break;
1351 case OP_UNUSED_73:
1352 case OP_UNUSED_79:
1353 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001354 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001355 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356 return true;
1357 case OP_NOP:
1358 break;
1359 default:
1360 return true;
1361 }
1362 return false;
1363}
1364
1365static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1366{
Bill Buzbee1465db52009-09-23 17:17:35 -07001367 RegLocation rlDest;
1368 RegLocation rlResult;
1369 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001370 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001371 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001372 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001373 }
Ben Chenge9695e52009-06-16 16:11:47 -07001374
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001375 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001376 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001377 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001378 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001379 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001380 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001381 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001382 }
1383 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001384 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001385 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001386 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001387 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001388 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1389 rlResult.lowReg, 31);
1390 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001391 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001392 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001393 default:
1394 return true;
1395 }
1396 return false;
1397}
1398
1399static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1400{
Bill Buzbee1465db52009-09-23 17:17:35 -07001401 RegLocation rlDest;
1402 RegLocation rlResult;
1403 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001404 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001405 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001406 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001407 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001408 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001409
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001410 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001411 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001412 loadConstantNoClobber(cUnit, rlResult.lowReg,
1413 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001414 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001415 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001416 }
1417 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001418 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1419 0, mir->dalvikInsn.vB << 16);
1420 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001421 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001422 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001423 default:
1424 return true;
1425 }
1426 return false;
1427}
1428
jeffhao71eee1f2011-01-04 14:18:54 -08001429static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001430{
jeffhao71eee1f2011-01-04 14:18:54 -08001431 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001432 genInterpSingleStep(cUnit, mir);
1433 return false;
1434}
1435
jeffhao71eee1f2011-01-04 14:18:54 -08001436static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001437{
Bill Buzbee1465db52009-09-23 17:17:35 -07001438 RegLocation rlResult;
1439 RegLocation rlDest;
1440 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001441
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001442 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001443 case OP_CONST_STRING_JUMBO:
1444 case OP_CONST_STRING: {
1445 void *strPtr = (void*)
1446 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001447
1448 if (strPtr == NULL) {
1449 LOGE("Unexpected null string");
1450 dvmAbort();
1451 }
1452
Bill Buzbeec6f10662010-02-09 11:16:15 -08001453 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1454 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001455 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001456 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001457 break;
1458 }
jeffhao71eee1f2011-01-04 14:18:54 -08001459 case OP_CONST_CLASS:
1460 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001461 void *classPtr = (void*)
1462 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001463
1464 if (classPtr == NULL) {
1465 LOGE("Unexpected null class");
1466 dvmAbort();
1467 }
1468
Bill Buzbeec6f10662010-02-09 11:16:15 -08001469 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1470 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001471 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001472 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001473 break;
1474 }
jeffhao71eee1f2011-01-04 14:18:54 -08001475 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001476 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001477 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001478 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001479 case OP_SGET_OBJECT_VOLATILE:
1480 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001481 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001482 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001483 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001484 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001485 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001486 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001487 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001488 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001489 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001490 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001491 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001492 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1493 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001494 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001495 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001496
1497 if (fieldPtr == NULL) {
1498 LOGE("Unexpected null static field");
1499 dvmAbort();
1500 }
1501
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001502 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1503 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001504 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001505
Bill Buzbeec6f10662010-02-09 11:16:15 -08001506 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1507 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001508 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001509
buzbeeecf8f6e2010-07-20 14:53:42 -07001510 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001511 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001512 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001513 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001514 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001515 HEAP_ACCESS_SHADOW(false);
1516
Bill Buzbee1465db52009-09-23 17:17:35 -07001517 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001518 break;
1519 }
jeffhao71eee1f2011-01-04 14:18:54 -08001520 case OP_SGET_WIDE:
1521 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001522 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001523 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1524 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001525 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001526 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001527
1528 if (fieldPtr == NULL) {
1529 LOGE("Unexpected null static field");
1530 dvmAbort();
1531 }
1532
Bill Buzbeec6f10662010-02-09 11:16:15 -08001533 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001534 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1535 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001536 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001537
1538 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001539 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001540 HEAP_ACCESS_SHADOW(false);
1541
Bill Buzbee1465db52009-09-23 17:17:35 -07001542 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001543 break;
1544 }
jeffhao71eee1f2011-01-04 14:18:54 -08001545 case OP_SPUT:
1546 case OP_SPUT_VOLATILE:
1547 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001548 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001549 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001550 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001551 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001552 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001553 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001554 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001555 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001556 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001557 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001558 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001559 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001560 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001561 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001562 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001563 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001564 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1565 mir->meta.calleeMethod : cUnit->method;
1566 void *fieldPtr = (void*)
1567 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001568
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001569 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1570 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001571 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001572
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001573 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001574 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001575 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001576
Ben Chengdd6e8702010-05-07 13:05:47 -07001577 if (fieldPtr == NULL) {
1578 LOGE("Unexpected null static field");
1579 dvmAbort();
1580 }
1581
Bill Buzbeec6f10662010-02-09 11:16:15 -08001582 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001583 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001584 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001585 if (isSputObject) {
1586 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001587 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001588 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001589 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001590 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001591 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001592 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001593 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001594 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001595 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001596 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001597 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001598 markCard(cUnit, rlSrc.lowReg, objHead);
1599 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001600 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001601
Ben Chengba4fc8b2009-06-01 13:00:29 -07001602 break;
1603 }
jeffhao71eee1f2011-01-04 14:18:54 -08001604 case OP_SPUT_WIDE:
1605 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001606 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001607 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001608 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1609 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001610 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001611 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001612
Ben Chengdd6e8702010-05-07 13:05:47 -07001613 if (fieldPtr == NULL) {
1614 LOGE("Unexpected null static field");
1615 dvmAbort();
1616 }
1617
Bill Buzbeec6f10662010-02-09 11:16:15 -08001618 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001619 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1620 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001621
1622 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001623 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001624 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001625 break;
1626 }
jeffhao71eee1f2011-01-04 14:18:54 -08001627 case OP_NEW_INSTANCE:
1628 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001629 /*
1630 * Obey the calling convention and don't mess with the register
1631 * usage.
1632 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001633 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001634 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001635
1636 if (classPtr == NULL) {
1637 LOGE("Unexpected null class");
1638 dvmAbort();
1639 }
1640
Ben Cheng79d173c2009-09-29 16:12:51 -07001641 /*
1642 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001643 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001644 */
1645 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001646 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001647 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001648 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001649 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001650 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001651 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001652 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001653 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001654 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001655 /*
1656 * OOM exception needs to be thrown here and cannot re-execute
1657 */
1658 loadConstant(cUnit, r0,
1659 (int) (cUnit->method->insns + mir->offset));
1660 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1661 /* noreturn */
1662
Bill Buzbee1465db52009-09-23 17:17:35 -07001663 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001664 target->defMask = ENCODE_ALL;
1665 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001666 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1667 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001668 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001669 break;
1670 }
jeffhao71eee1f2011-01-04 14:18:54 -08001671 case OP_CHECK_CAST:
1672 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001673 /*
1674 * Obey the calling convention and don't mess with the register
1675 * usage.
1676 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001677 ClassObject *classPtr =
1678 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001679 /*
1680 * Note: It is possible that classPtr is NULL at this point,
1681 * even though this instruction has been successfully interpreted.
1682 * If the previous interpretation had a null source, the
1683 * interpreter would not have bothered to resolve the clazz.
1684 * Bail out to the interpreter in this case, and log it
1685 * so that we can tell if it happens frequently.
1686 */
1687 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001688 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001689 genInterpSingleStep(cUnit, mir);
1690 return false;
1691 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001692 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001693 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001694 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001695 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001696 /* Null? */
1697 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1698 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001699 /*
1700 * rlSrc.lowReg now contains object->clazz. Note that
1701 * it could have been allocated r0, but we're okay so long
1702 * as we don't do anything desctructive until r0 is loaded
1703 * with clazz.
1704 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001705 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001706 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001707 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001708 opRegReg(cUnit, kOpCmp, r0, r1);
1709 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1710 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001711 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001712 /*
1713 * If null, check cast failed - punt to the interpreter. Because
1714 * interpreter will be the one throwing, we don't need to
1715 * genExportPC() here.
1716 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001717 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001718 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001719 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001720 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001721 branch1->generic.target = (LIR *)target;
1722 branch2->generic.target = (LIR *)target;
1723 break;
1724 }
buzbee4d92e682010-07-29 15:24:14 -07001725 case OP_SGET_WIDE_VOLATILE:
1726 case OP_SPUT_WIDE_VOLATILE:
1727 genInterpSingleStep(cUnit, mir);
1728 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001729 default:
1730 return true;
1731 }
1732 return false;
1733}
1734
Ben Cheng7a2697d2010-06-07 13:44:23 -07001735/*
1736 * A typical example of inlined getter/setter from a monomorphic callsite:
1737 *
1738 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1739 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1740 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1741 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1742 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1743 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1744 * D/dalvikvm( 289): L0x0003:
1745 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1746 *
1747 * Note the invoke-static and move-result-object with the (I) notation are
1748 * turned into no-op.
1749 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001750static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1751{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001752 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001753 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001754 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001755 case OP_MOVE_EXCEPTION: {
1756 int offset = offsetof(InterpState, self);
1757 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001758 int selfReg = dvmCompilerAllocTemp(cUnit);
1759 int resetReg = dvmCompilerAllocTemp(cUnit);
1760 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1761 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001762 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001763 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001764 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001765 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001766 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001767 break;
1768 }
1769 case OP_MOVE_RESULT:
1770 case OP_MOVE_RESULT_OBJECT: {
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 = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001775 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1776 rlSrc.fp = rlDest.fp;
1777 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001778 break;
1779 }
1780 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001781 /* An inlined move result is effectively no-op */
1782 if (mir->OptimizationFlags & MIR_INLINED)
1783 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001784 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1786 rlSrc.fp = rlDest.fp;
1787 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001788 break;
1789 }
1790 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001791 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001792 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1793 rlDest.fp = rlSrc.fp;
1794 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001795 genReturnCommon(cUnit,mir);
1796 break;
1797 }
1798 case OP_RETURN:
1799 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001800 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001801 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1802 rlDest.fp = rlSrc.fp;
1803 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001804 genReturnCommon(cUnit,mir);
1805 break;
1806 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001807 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001808 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001809#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001810 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001811#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001812 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001813#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001814 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001815 case OP_THROW: {
1816 genInterpSingleStep(cUnit, mir);
1817 break;
1818 }
1819 default:
1820 return true;
1821 }
1822 return false;
1823}
1824
Bill Buzbeed45ba372009-06-15 17:00:57 -07001825static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1826{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001827 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001828 RegLocation rlDest;
1829 RegLocation rlSrc;
1830 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001831
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001832 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001833 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001834 }
1835
Bill Buzbee1465db52009-09-23 17:17:35 -07001836 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001837 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001838 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001839 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001840 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001841 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001842 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001843 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001844
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001845 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001846 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001847 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001849 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001850 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001852 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001853 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001856 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 case OP_NEG_INT:
1858 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001859 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001860 case OP_NEG_LONG:
1861 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001862 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001863 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001864 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001865 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001866 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001867 case OP_MOVE_WIDE:
1868 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001869 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001870 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001871 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1872 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001873 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001874 if (rlSrc.location == kLocPhysReg) {
1875 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1876 } else {
1877 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1878 }
1879 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1880 rlResult.lowReg, 31);
1881 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001882 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001883 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001884 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1885 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001886 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001887 case OP_MOVE:
1888 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001889 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001890 break;
1891 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001892 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001893 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001894 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1895 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001896 break;
1897 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001898 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001899 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001900 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1901 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001902 break;
1903 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001904 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001905 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001906 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1907 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001908 break;
1909 case OP_ARRAY_LENGTH: {
1910 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001911 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1912 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1913 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001914 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001915 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1916 rlResult.lowReg);
1917 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001918 break;
1919 }
1920 default:
1921 return true;
1922 }
1923 return false;
1924}
1925
1926static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1927{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001928 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001929 RegLocation rlDest;
1930 RegLocation rlResult;
1931 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001932 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001933 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1934 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001935 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001936 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001937 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1938 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001939 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001940 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1941 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001942 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001943 storeValue(cUnit, rlDest, rlResult);
1944 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001945 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001946 return false;
1947}
1948
1949/* Compare agaist zero */
1950static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001951 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001952{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001953 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001954 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001955 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001956 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1957 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001958
Bill Buzbee270c1d62009-08-13 16:58:07 -07001959//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001960 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001961 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001962 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001963 break;
1964 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001965 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001966 break;
1967 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001968 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001969 break;
1970 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001971 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001972 break;
1973 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001974 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001975 break;
1976 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001977 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978 break;
1979 default:
1980 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001981 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001982 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001983 }
1984 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1985 /* This mostly likely will be optimized away in a later phase */
1986 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1987 return false;
1988}
1989
Elliott Hughesb4c05972010-02-24 16:36:18 -08001990static bool isPowerOfTwo(int x)
1991{
1992 return (x & (x - 1)) == 0;
1993}
1994
1995// Returns true if no more than two bits are set in 'x'.
1996static bool isPopCountLE2(unsigned int x)
1997{
1998 x &= x - 1;
1999 return (x & (x - 1)) == 0;
2000}
2001
2002// Returns the index of the lowest set bit in 'x'.
2003static int lowestSetBit(unsigned int x) {
2004 int bit_posn = 0;
2005 while ((x & 0xf) == 0) {
2006 bit_posn += 4;
2007 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002008 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002009 while ((x & 1) == 0) {
2010 bit_posn++;
2011 x >>= 1;
2012 }
2013 return bit_posn;
2014}
2015
Elliott Hughes672511b2010-04-26 17:40:13 -07002016// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2017// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002018static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002019 RegLocation rlSrc, RegLocation rlDest, int lit)
2020{
2021 if (lit < 2 || !isPowerOfTwo(lit)) {
2022 return false;
2023 }
2024 int k = lowestSetBit(lit);
2025 if (k >= 30) {
2026 // Avoid special cases.
2027 return false;
2028 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002029 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002030 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2031 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002032 if (div) {
2033 int tReg = dvmCompilerAllocTemp(cUnit);
2034 if (lit == 2) {
2035 // Division by 2 is by far the most common division by constant.
2036 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2037 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2038 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2039 } else {
2040 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2041 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2042 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2043 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2044 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002045 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002046 int cReg = dvmCompilerAllocTemp(cUnit);
2047 loadConstant(cUnit, cReg, lit - 1);
2048 int tReg1 = dvmCompilerAllocTemp(cUnit);
2049 int tReg2 = dvmCompilerAllocTemp(cUnit);
2050 if (lit == 2) {
2051 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2052 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2053 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2054 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2055 } else {
2056 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2057 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2058 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2059 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2060 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2061 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002062 }
2063 storeValue(cUnit, rlDest, rlResult);
2064 return true;
2065}
2066
Elliott Hughesb4c05972010-02-24 16:36:18 -08002067// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2068// and store the result in 'rlDest'.
2069static bool handleEasyMultiply(CompilationUnit *cUnit,
2070 RegLocation rlSrc, RegLocation rlDest, int lit)
2071{
2072 // Can we simplify this multiplication?
2073 bool powerOfTwo = false;
2074 bool popCountLE2 = false;
2075 bool powerOfTwoMinusOne = false;
2076 if (lit < 2) {
2077 // Avoid special cases.
2078 return false;
2079 } else if (isPowerOfTwo(lit)) {
2080 powerOfTwo = true;
2081 } else if (isPopCountLE2(lit)) {
2082 popCountLE2 = true;
2083 } else if (isPowerOfTwo(lit + 1)) {
2084 powerOfTwoMinusOne = true;
2085 } else {
2086 return false;
2087 }
2088 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2089 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2090 if (powerOfTwo) {
2091 // Shift.
2092 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2093 lowestSetBit(lit));
2094 } else if (popCountLE2) {
2095 // Shift and add and shift.
2096 int firstBit = lowestSetBit(lit);
2097 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2098 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2099 firstBit, secondBit);
2100 } else {
2101 // Reverse subtract: (src << (shift + 1)) - src.
2102 assert(powerOfTwoMinusOne);
2103 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2104 int tReg = dvmCompilerAllocTemp(cUnit);
2105 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2106 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2107 }
2108 storeValue(cUnit, rlDest, rlResult);
2109 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002110}
2111
Ben Chengba4fc8b2009-06-01 13:00:29 -07002112static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2113{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002114 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002115 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2116 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002118 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002119 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002120 int shiftOp = false;
2121 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002122
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002123 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002124 case OP_RSUB_INT_LIT8:
2125 case OP_RSUB_INT: {
2126 int tReg;
2127 //TUNING: add support for use of Arm rsub op
2128 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002129 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002130 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002131 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2133 tReg, rlSrc.lowReg);
2134 storeValue(cUnit, rlDest, rlResult);
2135 return false;
2136 break;
2137 }
2138
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 case OP_ADD_INT_LIT8:
2140 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002141 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002142 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002143 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002144 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002145 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2146 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002147 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002148 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002149 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002150 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002151 case OP_AND_INT_LIT8:
2152 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002153 op = kOpAnd;
2154 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002155 case OP_OR_INT_LIT8:
2156 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002157 op = kOpOr;
2158 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002159 case OP_XOR_INT_LIT8:
2160 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002161 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002162 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002163 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002164 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 shiftOp = true;
2166 op = kOpLsl;
2167 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002168 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002169 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002170 shiftOp = true;
2171 op = kOpAsr;
2172 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002174 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002175 shiftOp = true;
2176 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002177 break;
2178
2179 case OP_DIV_INT_LIT8:
2180 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002181 case OP_REM_INT_LIT8:
2182 case OP_REM_INT_LIT16:
2183 if (lit == 0) {
2184 /* Let the interpreter deal with div by 0 */
2185 genInterpSingleStep(cUnit, mir);
2186 return false;
2187 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002188 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002189 return false;
2190 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002191 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002192 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002193 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002194 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2195 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002196 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002197 isDiv = true;
2198 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002199 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 isDiv = false;
2201 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002202 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002203 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002204 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002205 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002206 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002208 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002209 storeValue(cUnit, rlDest, rlResult);
2210 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002211 break;
2212 default:
2213 return true;
2214 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002215 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002216 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002217 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2218 if (shiftOp && (lit == 0)) {
2219 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2220 } else {
2221 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2222 }
2223 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002224 return false;
2225}
2226
jeffhao71eee1f2011-01-04 14:18:54 -08002227static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002228{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002229 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002230 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002231 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002232 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002233 /*
2234 * Wide volatiles currently handled via single step.
2235 * Add them here if generating in-line code.
2236 * case OP_IGET_WIDE_VOLATILE:
2237 * case OP_IPUT_WIDE_VOLATILE:
2238 */
2239 case OP_IGET:
2240 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002241 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002242 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002243 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002244 case OP_IGET_OBJECT:
2245 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002246 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002247 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002248 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002249 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002250 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002251 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002252 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002253 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002254 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002255 case OP_IPUT:
2256 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002257 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002258 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002259 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002260 case OP_IPUT_OBJECT:
2261 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002262 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002263 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002264 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002265 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002266 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002267 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002268 case OP_IPUT_CHAR_JUMBO:
2269 case OP_IPUT_SHORT:
2270 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002271 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2272 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002273 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002274 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002275
buzbee4d92e682010-07-29 15:24:14 -07002276 if (fieldPtr == NULL) {
2277 LOGE("Unexpected null instance field");
2278 dvmAbort();
2279 }
2280 isVolatile = dvmIsVolatileField(fieldPtr);
2281 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2282 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002283 }
buzbee4d92e682010-07-29 15:24:14 -07002284 default:
2285 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002286 }
buzbee4d92e682010-07-29 15:24:14 -07002287
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002288 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002289 case OP_NEW_ARRAY:
2290 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002291 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002292 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2293 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002294 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295 void *classPtr = (void*)
2296 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002297
2298 if (classPtr == NULL) {
2299 LOGE("Unexpected null class");
2300 dvmAbort();
2301 }
2302
Bill Buzbeec6f10662010-02-09 11:16:15 -08002303 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002304 genExportPC(cUnit, mir);
2305 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002306 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002307 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002308 /*
2309 * "len < 0": bail to the interpreter to re-execute the
2310 * instruction
2311 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002312 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002313 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002314 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002315 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002316 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002317 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002318 /*
2319 * OOM exception needs to be thrown here and cannot re-execute
2320 */
2321 loadConstant(cUnit, r0,
2322 (int) (cUnit->method->insns + mir->offset));
2323 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2324 /* noreturn */
2325
Bill Buzbee1465db52009-09-23 17:17:35 -07002326 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002327 target->defMask = ENCODE_ALL;
2328 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002329 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002331 break;
2332 }
jeffhao71eee1f2011-01-04 14:18:54 -08002333 case OP_INSTANCE_OF:
2334 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002335 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002336 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2337 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002338 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002339 ClassObject *classPtr =
2340 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002341 /*
2342 * Note: It is possible that classPtr is NULL at this point,
2343 * even though this instruction has been successfully interpreted.
2344 * If the previous interpretation had a null source, the
2345 * interpreter would not have bothered to resolve the clazz.
2346 * Bail out to the interpreter in this case, and log it
2347 * so that we can tell if it happens frequently.
2348 */
2349 if (classPtr == NULL) {
2350 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2351 genInterpSingleStep(cUnit, mir);
2352 break;
2353 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002354 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002355 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002356 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002357 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002358 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002359 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002360 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002361 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002362 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002363 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002364 opRegReg(cUnit, kOpCmp, r1, r2);
2365 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2366 genRegCopy(cUnit, r0, r1);
2367 genRegCopy(cUnit, r1, r2);
2368 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002369 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002370 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002371 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002372 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002373 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002374 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375 branch1->generic.target = (LIR *)target;
2376 branch2->generic.target = (LIR *)target;
2377 break;
2378 }
2379 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002380 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002381 genIGetWide(cUnit, mir, fieldOffset);
2382 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002383 case OP_IGET_VOLATILE:
2384 case OP_IGET_OBJECT_VOLATILE:
2385 isVolatile = true;
2386 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002387 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002388 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002389 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002390 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002391 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002392 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002393 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002394 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002396 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002397 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002398 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002399 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002400 break;
2401 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002402 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002403 genIPutWide(cUnit, mir, fieldOffset);
2404 break;
2405 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002406 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002407 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002408 case OP_IPUT_BOOLEAN_JUMBO:
2409 case OP_IPUT_BYTE:
2410 case OP_IPUT_BYTE_JUMBO:
2411 case OP_IPUT_CHAR:
2412 case OP_IPUT_CHAR_JUMBO:
2413 case OP_IPUT_SHORT:
2414 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002415 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002416 break;
buzbee4d92e682010-07-29 15:24:14 -07002417 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002418 case OP_IPUT_OBJECT_VOLATILE:
2419 isVolatile = true;
2420 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002422 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002423 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002424 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002425 case OP_IGET_WIDE_VOLATILE:
2426 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002427 genInterpSingleStep(cUnit, mir);
2428 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002429 default:
2430 return true;
2431 }
2432 return false;
2433}
2434
2435static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2436{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002437 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002438 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002439 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002440 case OP_IGET_QUICK:
2441 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002442 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002443 break;
2444 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002445 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002446 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002448 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002449 break;
2450 case OP_IGET_WIDE_QUICK:
2451 genIGetWide(cUnit, mir, fieldOffset);
2452 break;
2453 case OP_IPUT_WIDE_QUICK:
2454 genIPutWide(cUnit, mir, fieldOffset);
2455 break;
2456 default:
2457 return true;
2458 }
2459 return false;
2460
2461}
2462
2463/* Compare agaist zero */
2464static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002465 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002466{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002467 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002468 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002469 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2470 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002471
Bill Buzbee1465db52009-09-23 17:17:35 -07002472 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2473 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2474 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002475
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002476 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002477 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002478 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002479 break;
2480 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002481 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002482 break;
2483 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002484 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002485 break;
2486 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002487 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002488 break;
2489 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002490 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002491 break;
2492 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002493 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002494 break;
2495 default:
2496 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002497 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002498 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002499 }
2500 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2501 /* This mostly likely will be optimized away in a later phase */
2502 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2503 return false;
2504}
2505
2506static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2507{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002508 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002509
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002510 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002511 case OP_MOVE_16:
2512 case OP_MOVE_OBJECT_16:
2513 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002514 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002515 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2516 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002517 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002518 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002519 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002520 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002521 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2522 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002524 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002525 default:
2526 return true;
2527 }
2528 return false;
2529}
2530
2531static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2532{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002533 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002534 RegLocation rlSrc1;
2535 RegLocation rlSrc2;
2536 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002537
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002538 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002539 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002540 }
2541
Bill Buzbee1465db52009-09-23 17:17:35 -07002542 /* APUTs have 3 sources and no targets */
2543 if (mir->ssaRep->numDefs == 0) {
2544 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002545 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2546 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2547 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002548 } else {
2549 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002550 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2551 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2552 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002553 }
2554 } else {
2555 /* Two sources and 1 dest. Deduce the operand sizes */
2556 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002557 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2558 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002559 } else {
2560 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002561 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2562 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002563 }
2564 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002565 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002566 } else {
2567 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002568 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002569 }
2570 }
2571
2572
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002573 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002574 case OP_CMPL_FLOAT:
2575 case OP_CMPG_FLOAT:
2576 case OP_CMPL_DOUBLE:
2577 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002578 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002579 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002580 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002581 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002583 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002584 break;
2585 case OP_AGET:
2586 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002587 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002588 break;
2589 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002590 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002591 break;
2592 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002593 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002594 break;
2595 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002596 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002597 break;
2598 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002599 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002600 break;
2601 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002602 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002603 break;
2604 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002605 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002606 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002607 case OP_APUT_OBJECT:
2608 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2609 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002610 case OP_APUT_SHORT:
2611 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002612 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002613 break;
2614 case OP_APUT_BYTE:
2615 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002616 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002617 break;
2618 default:
2619 return true;
2620 }
2621 return false;
2622}
2623
Ben Cheng6c10a972009-10-29 14:39:18 -07002624/*
2625 * Find the matching case.
2626 *
2627 * return values:
2628 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2629 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2630 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2631 * above MAX_CHAINED_SWITCH_CASES).
2632 *
2633 * Instructions around the call are:
2634 *
2635 * mov r2, pc
2636 * blx &findPackedSwitchIndex
2637 * mov pc, r0
2638 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002639 * chaining cell for case 0 [12 bytes]
2640 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002641 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002642 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002643 * chaining cell for case default [8 bytes]
2644 * noChain exit
2645 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002646static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002647{
2648 int size;
2649 int firstKey;
2650 const int *entries;
2651 int index;
2652 int jumpIndex;
2653 int caseDPCOffset = 0;
2654 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2655 int chainingPC = (pc + 4) & ~3;
2656
2657 /*
2658 * Packed switch data format:
2659 * ushort ident = 0x0100 magic value
2660 * ushort size number of entries in the table
2661 * int first_key first (and lowest) switch case value
2662 * int targets[size] branch targets, relative to switch opcode
2663 *
2664 * Total size is (4+size*2) 16-bit code units.
2665 */
2666 size = switchData[1];
2667 assert(size > 0);
2668
2669 firstKey = switchData[2];
2670 firstKey |= switchData[3] << 16;
2671
2672
2673 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2674 * we can treat them as a native int array.
2675 */
2676 entries = (const int*) &switchData[4];
2677 assert(((u4)entries & 0x3) == 0);
2678
2679 index = testVal - firstKey;
2680
2681 /* Jump to the default cell */
2682 if (index < 0 || index >= size) {
2683 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2684 /* Jump to the non-chaining exit point */
2685 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2686 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2687 caseDPCOffset = entries[index];
2688 /* Jump to the inline chaining cell */
2689 } else {
2690 jumpIndex = index;
2691 }
2692
Bill Buzbeebd047242010-05-13 13:02:53 -07002693 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002694 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2695}
2696
2697/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002698static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002699{
2700 int size;
2701 const int *keys;
2702 const int *entries;
2703 int chainingPC = (pc + 4) & ~3;
2704 int i;
2705
2706 /*
2707 * Sparse switch data format:
2708 * ushort ident = 0x0200 magic value
2709 * ushort size number of entries in the table; > 0
2710 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2711 * int targets[size] branch targets, relative to switch opcode
2712 *
2713 * Total size is (2+size*4) 16-bit code units.
2714 */
2715
2716 size = switchData[1];
2717 assert(size > 0);
2718
2719 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2720 * we can treat them as a native int array.
2721 */
2722 keys = (const int*) &switchData[2];
2723 assert(((u4)keys & 0x3) == 0);
2724
2725 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2726 * we can treat them as a native int array.
2727 */
2728 entries = keys + size;
2729 assert(((u4)entries & 0x3) == 0);
2730
2731 /*
2732 * Run through the list of keys, which are guaranteed to
2733 * be sorted low-to-high.
2734 *
2735 * Most tables have 3-4 entries. Few have more than 10. A binary
2736 * search here is probably not useful.
2737 */
2738 for (i = 0; i < size; i++) {
2739 int k = keys[i];
2740 if (k == testVal) {
2741 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2742 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2743 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002744 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002745 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2746 } else if (k > testVal) {
2747 break;
2748 }
2749 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002750 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2751 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002752}
2753
Ben Chengba4fc8b2009-06-01 13:00:29 -07002754static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2755{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002756 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2757 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002758 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002759 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002760 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002761 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002762 genExportPC(cUnit, mir);
2763 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002764 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002765 loadConstant(cUnit, r1,
2766 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002767 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002768 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002769 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002770 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002771 loadConstant(cUnit, r0,
2772 (int) (cUnit->method->insns + mir->offset));
2773 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2774 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2775 target->defMask = ENCODE_ALL;
2776 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002777 break;
2778 }
2779 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002780 * Compute the goto target of up to
2781 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2782 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002783 */
2784 case OP_PACKED_SWITCH:
2785 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002786 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2787 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002788 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002789 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002790 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002791 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002792 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002793 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002794 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002795 /* r0 <- Addr of the switch data */
2796 loadConstant(cUnit, r0,
2797 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2798 /* r2 <- pc of the instruction following the blx */
2799 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002800 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002801 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002802 /* pc <- computed goto target */
2803 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002804 break;
2805 }
2806 default:
2807 return true;
2808 }
2809 return false;
2810}
2811
Ben Cheng7a2697d2010-06-07 13:44:23 -07002812/*
2813 * See the example of predicted inlining listed before the
2814 * genValidationForPredictedInline function. The function here takes care the
2815 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2816 */
2817static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2818 BasicBlock *bb,
2819 ArmLIR *labelList)
2820{
2821 BasicBlock *fallThrough = bb->fallThrough;
2822
2823 /* Bypass the move-result block if there is one */
2824 if (fallThrough->firstMIRInsn) {
2825 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2826 fallThrough = fallThrough->fallThrough;
2827 }
2828 /* Generate a branch over if the predicted inlining is correct */
2829 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2830
2831 /* Reset the register state */
2832 dvmCompilerResetRegPool(cUnit);
2833 dvmCompilerClobberAllRegs(cUnit);
2834 dvmCompilerResetNullCheck(cUnit);
2835
2836 /* Target for the slow invoke path */
2837 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2838 target->defMask = ENCODE_ALL;
2839 /* Hook up the target to the verification branch */
2840 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2841}
2842
jeffhao71eee1f2011-01-04 14:18:54 -08002843static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2844 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002845{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002846 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002847 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002848
Ben Cheng7a2697d2010-06-07 13:44:23 -07002849 /* An invoke with the MIR_INLINED is effectively a no-op */
2850 if (mir->OptimizationFlags & MIR_INLINED)
2851 return false;
2852
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002853 if (bb->fallThrough != NULL)
2854 retChainingCell = &labelList[bb->fallThrough->id];
2855
Ben Chengba4fc8b2009-06-01 13:00:29 -07002856 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002857 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002858 /*
2859 * calleeMethod = this->clazz->vtable[
2860 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2861 * ]
2862 */
2863 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002864 case OP_INVOKE_VIRTUAL_RANGE:
2865 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002866 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002867 int methodIndex =
2868 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2869 methodIndex;
2870
Ben Cheng7a2697d2010-06-07 13:44:23 -07002871 /*
2872 * If the invoke has non-null misPredBranchOver, we need to generate
2873 * the non-inlined version of the invoke here to handle the
2874 * mispredicted case.
2875 */
2876 if (mir->meta.callsiteInfo->misPredBranchOver) {
2877 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
2878 }
2879
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002880 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002881 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2882 else
2883 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2884
Ben Cheng38329f52009-07-07 14:19:20 -07002885 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2886 retChainingCell,
2887 predChainingCell,
2888 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002889 break;
2890 }
2891 /*
2892 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2893 * ->pResMethods[BBBB]->methodIndex]
2894 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002895 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08002896 case OP_INVOKE_SUPER_RANGE:
2897 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002898 /* Grab the method ptr directly from what the interpreter sees */
2899 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2900 assert(calleeMethod == cUnit->method->clazz->super->vtable[
2901 cUnit->method->clazz->pDvmDex->
2902 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002903
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002904 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002905 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2906 else
2907 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2908
2909 /* r0 = calleeMethod */
2910 loadConstant(cUnit, r0, (int) calleeMethod);
2911
Ben Cheng38329f52009-07-07 14:19:20 -07002912 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2913 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002914 break;
2915 }
2916 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2917 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002918 case OP_INVOKE_DIRECT_RANGE:
2919 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002920 /* Grab the method ptr directly from what the interpreter sees */
2921 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2922 assert(calleeMethod ==
2923 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002924
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002925 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002926 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2927 else
2928 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2929
2930 /* r0 = calleeMethod */
2931 loadConstant(cUnit, r0, (int) calleeMethod);
2932
Ben Cheng38329f52009-07-07 14:19:20 -07002933 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2934 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002935 break;
2936 }
2937 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2938 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08002939 case OP_INVOKE_STATIC_RANGE:
2940 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002941 /* Grab the method ptr directly from what the interpreter sees */
2942 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2943 assert(calleeMethod ==
2944 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002945
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002946 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002947 genProcessArgsNoRange(cUnit, mir, dInsn,
2948 NULL /* no null check */);
2949 else
2950 genProcessArgsRange(cUnit, mir, dInsn,
2951 NULL /* no null check */);
2952
2953 /* r0 = calleeMethod */
2954 loadConstant(cUnit, r0, (int) calleeMethod);
2955
Ben Cheng38329f52009-07-07 14:19:20 -07002956 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2957 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002958 break;
2959 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002960 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002961 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2962 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002963 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002964 * The following is an example of generated code for
2965 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002966 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002967 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2968 * 0x47357e36 : ldr r0, [r5, #0] --+
2969 * 0x47357e38 : sub r7,r5,#24 |
2970 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2971 * 0x47357e3e : beq 0x47357e82 |
2972 * 0x47357e40 : stmia r7, <r0> --+
2973 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2974 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2975 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2976 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2977 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2978 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2979 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2980 * 0x47357e50 : mov r8, r1 --+
2981 * 0x47357e52 : mov r9, r2 |
2982 * 0x47357e54 : ldr r2, [pc, #96] |
2983 * 0x47357e56 : mov r10, r3 |
2984 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2985 * 0x47357e5a : ldr r3, [pc, #88] |
2986 * 0x47357e5c : ldr r7, [pc, #80] |
2987 * 0x47357e5e : mov r1, #1452 |
2988 * 0x47357e62 : blx r7 --+
2989 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2990 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2991 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2992 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2993 * 0x47357e6c : blx_2 see above --+ COMMON
2994 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2995 * 0x47357e70 : cmp r1, #0 --> compare against 0
2996 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08002997 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07002998 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2999 * 0x47357e78 : mov r3, r10 |
3000 * 0x47357e7a : blx r7 --+
3001 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
3002 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3003 * 0x47357e80 : blx_2 see above --+
3004 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3005 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003006 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003007 * 0x47357e84 : ldr r1, [r6, #92]
3008 * 0x47357e86 : blx r1
3009 * 0x47357e88 : .align4
3010 * -------- chaining cell (hot): 0x000b
3011 * 0x47357e88 : ldr r0, [r6, #104]
3012 * 0x47357e8a : blx r0
3013 * 0x47357e8c : data 0x19e2(6626)
3014 * 0x47357e8e : data 0x4257(16983)
3015 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003016 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003017 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3018 * 0x47357e92 : data 0x0000(0)
3019 * 0x47357e94 : data 0x0000(0) --> class
3020 * 0x47357e96 : data 0x0000(0)
3021 * 0x47357e98 : data 0x0000(0) --> method
3022 * 0x47357e9a : data 0x0000(0)
3023 * 0x47357e9c : data 0x0000(0) --> rechain count
3024 * 0x47357e9e : data 0x0000(0)
3025 * -------- end of chaining cells (0x006c)
3026 * 0x47357eb0 : .word (0xad03e369)
3027 * 0x47357eb4 : .word (0x28a90)
3028 * 0x47357eb8 : .word (0x41a63394)
3029 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003030 */
3031 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003032 case OP_INVOKE_INTERFACE_RANGE:
3033 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003034 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003035
Ben Cheng7a2697d2010-06-07 13:44:23 -07003036 /*
3037 * If the invoke has non-null misPredBranchOver, we need to generate
3038 * the non-inlined version of the invoke here to handle the
3039 * mispredicted case.
3040 */
3041 if (mir->meta.callsiteInfo->misPredBranchOver) {
3042 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3043 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003044
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003045 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3047 else
3048 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3049
Ben Cheng38329f52009-07-07 14:19:20 -07003050 /* "this" is already left in r0 by genProcessArgs* */
3051
3052 /* r4PC = dalvikCallsite */
3053 loadConstant(cUnit, r4PC,
3054 (int) (cUnit->method->insns + mir->offset));
3055
3056 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003057 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003058 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003059 addrRetChain->generic.target = (LIR *) retChainingCell;
3060
3061 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003062 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003063 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003064 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3065
buzbee18fba342011-01-19 15:31:15 -08003066 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3067 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3068 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003069
3070 /* return through lr - jump to the chaining cell */
3071 genUnconditionalBranch(cUnit, predChainingCell);
3072
3073 /*
3074 * null-check on "this" may have been eliminated, but we still need
3075 * a PC-reconstruction label for stack overflow bailout.
3076 */
3077 if (pcrLabel == NULL) {
3078 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003079 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003080 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003081 pcrLabel->operands[0] = dPC;
3082 pcrLabel->operands[1] = mir->offset;
3083 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003084 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3085 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003086 }
3087
3088 /* return through lr+2 - punt to the interpreter */
3089 genUnconditionalBranch(cUnit, pcrLabel);
3090
3091 /*
3092 * return through lr+4 - fully resolve the callee method.
3093 * r1 <- count
3094 * r2 <- &predictedChainCell
3095 * r3 <- this->class
3096 * r4 <- dPC
3097 * r7 <- this->class->vtable
3098 */
3099
3100 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003101 genRegCopy(cUnit, r8, r1);
3102 genRegCopy(cUnit, r9, r2);
3103 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003104
Ben Chengba4fc8b2009-06-01 13:00:29 -07003105 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003106 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003107
3108 /* r1 = BBBB */
3109 loadConstant(cUnit, r1, dInsn->vB);
3110
3111 /* r2 = method (caller) */
3112 loadConstant(cUnit, r2, (int) cUnit->method);
3113
3114 /* r3 = pDvmDex */
3115 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3116
Ben Chengbd1326d2010-04-02 15:04:53 -07003117 LOAD_FUNC_ADDR(cUnit, r7,
3118 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003119 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003120 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3121
Ben Cheng09e50c92010-05-02 10:45:32 -07003122 dvmCompilerClobberCallRegs(cUnit);
3123 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003124 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003125 /*
3126 * calleeMethod == NULL -> throw
3127 */
3128 loadConstant(cUnit, r0,
3129 (int) (cUnit->method->insns + mir->offset));
3130 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3131 /* noreturn */
3132
3133 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3134 target->defMask = ENCODE_ALL;
3135 branchOver->generic.target = (LIR *) target;
3136
Bill Buzbee1465db52009-09-23 17:17:35 -07003137 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003138
Ben Cheng38329f52009-07-07 14:19:20 -07003139 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003140 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3141 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003142
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003143 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003144
Ben Chengb88ec3c2010-05-17 12:50:33 -07003145 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07003146 genRegCopy(cUnit, r2, r9);
3147 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003148
3149 /*
3150 * r0 = calleeMethod
3151 * r2 = &predictedChainingCell
3152 * r3 = class
3153 *
3154 * &returnChainingCell has been loaded into r1 but is not needed
3155 * when patching the chaining cell and will be clobbered upon
3156 * returning so it will be reconstructed again.
3157 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003158 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003159
3160 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003161 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003162 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003163
3164 bypassRechaining->generic.target = (LIR *) addrRetChain;
3165
Ben Chengba4fc8b2009-06-01 13:00:29 -07003166 /*
3167 * r0 = this, r1 = calleeMethod,
3168 * r1 = &ChainingCell,
3169 * r4PC = callsiteDPC,
3170 */
buzbee18fba342011-01-19 15:31:15 -08003171 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3172 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3173 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003174#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003175 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003176#endif
3177 /* Handle exceptions using the interpreter */
3178 genTrap(cUnit, mir->offset, pcrLabel);
3179 break;
3180 }
3181 /* NOP */
3182 case OP_INVOKE_DIRECT_EMPTY: {
buzbee18fba342011-01-19 15:31:15 -08003183 if (gDvmJit.methodTraceSupport)
3184 genInterpSingleStep(cUnit, mir);
3185 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003186 }
3187 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003188 case OP_FILLED_NEW_ARRAY_RANGE:
3189 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003190 /* Just let the interpreter deal with these */
3191 genInterpSingleStep(cUnit, mir);
3192 break;
3193 }
3194 default:
3195 return true;
3196 }
3197 return false;
3198}
3199
3200static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003201 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003202{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003203 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003204
Ben Cheng7a2697d2010-06-07 13:44:23 -07003205 /* An invoke with the MIR_INLINED is effectively a no-op */
3206 if (mir->OptimizationFlags & MIR_INLINED)
3207 return false;
3208
Ben Chengba4fc8b2009-06-01 13:00:29 -07003209 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003210 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003211 /* calleeMethod = this->clazz->vtable[BBBB] */
3212 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3213 case OP_INVOKE_VIRTUAL_QUICK: {
3214 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003215 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3216 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003217
3218 /*
3219 * If the invoke has non-null misPredBranchOver, we need to generate
3220 * the non-inlined version of the invoke here to handle the
3221 * mispredicted case.
3222 */
3223 if (mir->meta.callsiteInfo->misPredBranchOver) {
3224 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3225 }
3226
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003227 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003228 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3229 else
3230 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3231
Ben Cheng38329f52009-07-07 14:19:20 -07003232 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3233 retChainingCell,
3234 predChainingCell,
3235 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003236 break;
3237 }
3238 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3239 case OP_INVOKE_SUPER_QUICK:
3240 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003241 /* Grab the method ptr directly from what the interpreter sees */
3242 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3243 assert(calleeMethod ==
3244 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003245
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003246 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003247 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3248 else
3249 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3250
3251 /* r0 = calleeMethod */
3252 loadConstant(cUnit, r0, (int) calleeMethod);
3253
Ben Cheng38329f52009-07-07 14:19:20 -07003254 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3255 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003256 break;
3257 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003258 default:
3259 return true;
3260 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003261 return false;
3262}
3263
3264/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003265 * This operation is complex enough that we'll do it partly inline
3266 * and partly with a handler. NOTE: the handler uses hardcoded
3267 * values for string object offsets and must be revisitied if the
3268 * layout changes.
3269 */
3270static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3271{
3272#if defined(USE_GLOBAL_STRING_DEFS)
3273 return false;
3274#else
3275 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003276 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3277 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003278
3279 loadValueDirectFixed(cUnit, rlThis, r0);
3280 loadValueDirectFixed(cUnit, rlComp, r1);
3281 /* Test objects for NULL */
3282 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3283 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3284 /*
3285 * TUNING: we could check for object pointer equality before invoking
3286 * handler. Unclear whether the gain would be worth the added code size
3287 * expansion.
3288 */
3289 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003290 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3291 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003292 return true;
3293#endif
3294}
3295
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003296static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003297{
3298#if defined(USE_GLOBAL_STRING_DEFS)
3299 return false;
3300#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003301 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3302 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003303
3304 loadValueDirectFixed(cUnit, rlThis, r0);
3305 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003306 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3307 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003308 /* Test objects for NULL */
3309 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3310 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003311 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3312 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003313 return true;
3314#endif
3315}
3316
Elliott Hughesee34f592010-04-05 18:13:52 -07003317// Generates an inlined String.isEmpty or String.length.
3318static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3319 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003320{
Elliott Hughesee34f592010-04-05 18:13:52 -07003321 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003322 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3323 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3324 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3325 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3326 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3327 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3328 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003329 if (isEmpty) {
3330 // dst = (dst == 0);
3331 int tReg = dvmCompilerAllocTemp(cUnit);
3332 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3333 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3334 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003335 storeValue(cUnit, rlDest, rlResult);
3336 return false;
3337}
3338
Elliott Hughesee34f592010-04-05 18:13:52 -07003339static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3340{
3341 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3342}
3343
3344static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3345{
3346 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3347}
3348
Bill Buzbee1f748632010-03-02 16:14:41 -08003349static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3350{
3351 int contents = offsetof(ArrayObject, contents);
3352 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3353 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3354 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3355 RegLocation rlResult;
3356 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3357 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3358 int regMax = dvmCompilerAllocTemp(cUnit);
3359 int regOff = dvmCompilerAllocTemp(cUnit);
3360 int regPtr = dvmCompilerAllocTemp(cUnit);
3361 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3362 mir->offset, NULL);
3363 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3364 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3365 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3366 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3367 dvmCompilerFreeTemp(cUnit, regMax);
3368 opRegImm(cUnit, kOpAdd, regPtr, contents);
3369 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3370 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3371 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3372 storeValue(cUnit, rlDest, rlResult);
3373 return false;
3374}
3375
3376static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3377{
3378 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3379 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003380 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003381 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3382 int signReg = dvmCompilerAllocTemp(cUnit);
3383 /*
3384 * abs(x) = y<=x>>31, (x+y)^y.
3385 * Thumb2's IT block also yields 3 instructions, but imposes
3386 * scheduling constraints.
3387 */
3388 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3389 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3390 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3391 storeValue(cUnit, rlDest, rlResult);
3392 return false;
3393}
3394
3395static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3396{
3397 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3398 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3399 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3400 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3401 int signReg = dvmCompilerAllocTemp(cUnit);
3402 /*
3403 * abs(x) = y<=x>>31, (x+y)^y.
3404 * Thumb2 IT block allows slightly shorter sequence,
3405 * but introduces a scheduling barrier. Stick with this
3406 * mechanism for now.
3407 */
3408 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3409 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3410 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3411 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3412 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3413 storeValueWide(cUnit, rlDest, rlResult);
3414 return false;
3415}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003416
Elliott Hughese22bd842010-08-20 18:47:36 -07003417static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3418{
3419 // Just move from source to destination...
3420 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3421 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3422 storeValue(cUnit, rlDest, rlSrc);
3423 return false;
3424}
3425
3426static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3427{
3428 // Just move from source to destination...
3429 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3430 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3431 storeValueWide(cUnit, rlDest, rlSrc);
3432 return false;
3433}
3434
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003435/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003436 * NOTE: Handles both range and non-range versions (arguments
3437 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003438 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003439static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003440{
3441 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003442 switch( mir->dalvikInsn.opcode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003443 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003444 case OP_EXECUTE_INLINE: {
3445 unsigned int i;
3446 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003447 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003448 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003449 switch (operation) {
3450 case INLINE_EMPTYINLINEMETHOD:
3451 return false; /* Nop */
3452 case INLINE_STRING_LENGTH:
3453 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003454 case INLINE_STRING_IS_EMPTY:
3455 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003456 case INLINE_MATH_ABS_INT:
3457 return genInlinedAbsInt(cUnit, mir);
3458 case INLINE_MATH_ABS_LONG:
3459 return genInlinedAbsLong(cUnit, mir);
3460 case INLINE_MATH_MIN_INT:
3461 return genInlinedMinMaxInt(cUnit, mir, true);
3462 case INLINE_MATH_MAX_INT:
3463 return genInlinedMinMaxInt(cUnit, mir, false);
3464 case INLINE_STRING_CHARAT:
3465 return genInlinedStringCharAt(cUnit, mir);
3466 case INLINE_MATH_SQRT:
3467 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003468 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003469 else
3470 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003471 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003472 if (genInlinedAbsFloat(cUnit, mir))
3473 return false;
3474 else
3475 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003476 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003477 if (genInlinedAbsDouble(cUnit, mir))
3478 return false;
3479 else
3480 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003481 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003482 if (genInlinedCompareTo(cUnit, mir))
3483 return false;
3484 else
3485 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003486 case INLINE_STRING_FASTINDEXOF_II:
3487 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003488 return false;
3489 else
3490 break;
Elliott Hughese22bd842010-08-20 18:47:36 -07003491 case INLINE_FLOAT_TO_RAW_INT_BITS:
3492 case INLINE_INT_BITS_TO_FLOAT:
3493 return genInlinedIntFloatConversion(cUnit, mir);
3494 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3495 case INLINE_LONG_BITS_TO_DOUBLE:
3496 return genInlinedLongDoubleConversion(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003497 case INLINE_STRING_EQUALS:
3498 case INLINE_MATH_COS:
3499 case INLINE_MATH_SIN:
Elliott Hughese22bd842010-08-20 18:47:36 -07003500 case INLINE_FLOAT_TO_INT_BITS:
3501 case INLINE_DOUBLE_TO_LONG_BITS:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003502 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003503 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003504 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003505 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003506 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003507 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003508 dvmCompilerClobber(cUnit, r4PC);
3509 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003510 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3511 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003512 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003513 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003514 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003515 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003516 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003517 opReg(cUnit, kOpBlx, r4PC);
3518 opRegImm(cUnit, kOpAdd, r13, 8);
buzbee8f8109a2010-08-31 10:16:35 -07003519 /* NULL? */
3520 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeece46c942009-11-20 15:41:34 -08003521 loadConstant(cUnit, r0,
3522 (int) (cUnit->method->insns + mir->offset));
3523 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3524 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3525 target->defMask = ENCODE_ALL;
3526 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003527 break;
3528 }
3529 default:
3530 return true;
3531 }
3532 return false;
3533}
3534
3535static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3536{
Bill Buzbee1465db52009-09-23 17:17:35 -07003537 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003538 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3539 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003540 loadConstantNoClobber(cUnit, rlResult.lowReg,
3541 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3542 loadConstantNoClobber(cUnit, rlResult.highReg,
3543 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003544 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003545 return false;
3546}
3547
Ben Chengba4fc8b2009-06-01 13:00:29 -07003548/*
3549 * The following are special processing routines that handle transfer of
3550 * controls between compiled code and the interpreter. Certain VM states like
3551 * Dalvik PC and special-purpose registers are reconstructed here.
3552 */
3553
Bill Buzbeebd047242010-05-13 13:02:53 -07003554/*
3555 * Insert a
3556 * b .+4
3557 * nop
3558 * pair at the beginning of a chaining cell. This serves as the
3559 * switch branch that selects between reverting to the interpreter or
3560 * not. Once the cell is chained to a translation, the cell will
3561 * contain a 32-bit branch. Subsequent chain/unchain operations will
3562 * then only alter that first 16-bits - the "b .+4" for unchaining,
3563 * and the restoration of the first half of the 32-bit branch for
3564 * rechaining.
3565 */
3566static void insertChainingSwitch(CompilationUnit *cUnit)
3567{
3568 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3569 newLIR2(cUnit, kThumbOrr, r0, r0);
3570 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3571 target->defMask = ENCODE_ALL;
3572 branch->generic.target = (LIR *) target;
3573}
3574
Ben Cheng1efc9c52009-06-08 18:25:27 -07003575/* Chaining cell for code that may need warmup. */
3576static void handleNormalChainingCell(CompilationUnit *cUnit,
3577 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003578{
Ben Cheng11d8f142010-03-24 15:24:19 -07003579 /*
3580 * Use raw instruction constructors to guarantee that the generated
3581 * instructions fit the predefined cell size.
3582 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003583 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003584 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3585 offsetof(InterpState,
3586 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3587 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003588 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3589}
3590
3591/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003592 * Chaining cell for instructions that immediately following already translated
3593 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003594 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003595static void handleHotChainingCell(CompilationUnit *cUnit,
3596 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003597{
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);
Ben Cheng11d8f142010-03-24 15:24:19 -07003603 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3604 offsetof(InterpState,
3605 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3606 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003607 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3608}
3609
Jeff Hao97319a82009-08-12 16:57:15 -07003610/* Chaining cell for branches that branch back into the same basic block */
3611static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3612 unsigned int offset)
3613{
Ben Cheng11d8f142010-03-24 15:24:19 -07003614 /*
3615 * Use raw instruction constructors to guarantee that the generated
3616 * instructions fit the predefined cell size.
3617 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003618 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003619#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003620 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003621 offsetof(InterpState,
3622 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003623#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003625 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3626#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003627 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003628 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3629}
3630
Ben Chengba4fc8b2009-06-01 13:00:29 -07003631/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003632static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3633 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003634{
Ben Cheng11d8f142010-03-24 15:24:19 -07003635 /*
3636 * Use raw instruction constructors to guarantee that the generated
3637 * instructions fit the predefined cell size.
3638 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003639 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003640 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3641 offsetof(InterpState,
3642 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3643 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003644 addWordData(cUnit, (int) (callee->insns), true);
3645}
3646
Ben Cheng38329f52009-07-07 14:19:20 -07003647/* Chaining cell for monomorphic method invocations. */
3648static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3649{
3650
3651 /* Should not be executed in the initial state */
3652 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3653 /* To be filled: class */
3654 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3655 /* To be filled: method */
3656 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3657 /*
3658 * Rechain count. The initial value of 0 here will trigger chaining upon
3659 * the first invocation of this callsite.
3660 */
3661 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3662}
3663
Ben Chengba4fc8b2009-06-01 13:00:29 -07003664/* Load the Dalvik PC into r0 and jump to the specified target */
3665static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003666 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003667{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003668 ArmLIR **pcrLabel =
3669 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003670 int numElems = cUnit->pcReconstructionList.numUsed;
3671 int i;
3672 for (i = 0; i < numElems; i++) {
3673 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3674 /* r0 = dalvik PC */
3675 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3676 genUnconditionalBranch(cUnit, targetLabel);
3677 }
3678}
3679
Bill Buzbee1465db52009-09-23 17:17:35 -07003680static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3681 "kMirOpPhi",
3682 "kMirOpNullNRangeUpCheck",
3683 "kMirOpNullNRangeDownCheck",
3684 "kMirOpLowerBound",
3685 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003686 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003687};
3688
3689/*
3690 * vA = arrayReg;
3691 * vB = idxReg;
3692 * vC = endConditionReg;
3693 * arg[0] = maxC
3694 * arg[1] = minC
3695 * arg[2] = loopBranchConditionCode
3696 */
3697static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3698{
Bill Buzbee1465db52009-09-23 17:17:35 -07003699 /*
3700 * NOTE: these synthesized blocks don't have ssa names assigned
3701 * for Dalvik registers. However, because they dominate the following
3702 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3703 * ssa name.
3704 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003705 DecodedInstruction *dInsn = &mir->dalvikInsn;
3706 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003707 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003708 int regLength;
3709 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3710 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003711
3712 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003713 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3714 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3715 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003716 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3717
3718 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003719 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003720 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003721
3722 int delta = maxC;
3723 /*
3724 * If the loop end condition is ">=" instead of ">", then the largest value
3725 * of the index is "endCondition - 1".
3726 */
3727 if (dInsn->arg[2] == OP_IF_GE) {
3728 delta--;
3729 }
3730
3731 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003732 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003733 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3734 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003735 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003736 }
3737 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003738 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003739 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003740}
3741
3742/*
3743 * vA = arrayReg;
3744 * vB = idxReg;
3745 * vC = endConditionReg;
3746 * arg[0] = maxC
3747 * arg[1] = minC
3748 * arg[2] = loopBranchConditionCode
3749 */
3750static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3751{
3752 DecodedInstruction *dInsn = &mir->dalvikInsn;
3753 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003754 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003755 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003756 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3757 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003758
3759 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003760 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3761 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3762 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003763 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3764
3765 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003766 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003767
3768 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003769 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003770 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3771 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003772 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003773 }
3774
3775 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003776 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003777 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003778}
3779
3780/*
3781 * vA = idxReg;
3782 * vB = minC;
3783 */
3784static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3785{
3786 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003787 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003788 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003789
3790 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003791 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003792
3793 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003794 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003795 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3796}
3797
Ben Cheng7a2697d2010-06-07 13:44:23 -07003798/*
3799 * vC = this
3800 *
3801 * A predicted inlining target looks like the following, where instructions
3802 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3803 * matches "this", and the verificaion code is generated by this routine.
3804 *
3805 * (C) means the instruction is inlined from the callee, and (PI) means the
3806 * instruction is the predicted inlined invoke, whose corresponding
3807 * instructions are still generated to handle the mispredicted case.
3808 *
3809 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3810 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3811 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3812 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3813 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3814 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3815 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3816 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3817 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3818 * v4, v17, (#8)
3819 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3820 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3821 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3822 * +invoke-virtual-quick/range (PI) v17..v17
3823 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3824 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3825 * D/dalvikvm( 86): -------- BARRIER
3826 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3827 * D/dalvikvm( 86): -------- BARRIER
3828 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3829 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3830 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3831 * D/dalvikvm( 86): -------- BARRIER
3832 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3833 * D/dalvikvm( 86): -------- BARRIER
3834 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3835 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3836 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3837 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3838 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3839 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3840 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3841 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3842 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3843 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
3844 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
3845 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
3846 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
3847 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
3848 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
3849 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
3850 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
3851 * D/dalvikvm( 86): 0x4858deac (0048): .align4
3852 * D/dalvikvm( 86): L0x004f:
3853 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
3854 * v4, (#0), (#0)
3855 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
3856 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
3857 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
3858 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3859 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
3860 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
3861 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3862 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
3863 * D/dalvikvm( 86): Exception_Handling:
3864 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
3865 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
3866 * D/dalvikvm( 86): 0x4858debc (0058): .align4
3867 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
3868 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
3869 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
3870 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
3871 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
3872 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
3873 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
3874 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
3875 * D/dalvikvm( 86): -------- chaining cell (predicted)
3876 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
3877 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
3878 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
3879 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
3880 * :
3881 */
3882static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
3883{
3884 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3885 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
3886
3887 rlThis = loadValue(cUnit, rlThis, kCoreReg);
3888 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
3889 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
3890 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
3891 NULL);/* null object? */
3892 int regActualClass = dvmCompilerAllocTemp(cUnit);
3893 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
3894 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
3895 /*
3896 * Set the misPredBranchOver target so that it will be generated when the
3897 * code for the non-optimized invoke is generated.
3898 */
3899 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
3900}
3901
Ben Cheng4238ec22009-08-24 16:32:22 -07003902/* Extended MIR instructions like PHI */
3903static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3904{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003905 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003906 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3907 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07003908 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003909 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003910
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003911 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003912 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003913 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003914 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003915 break;
3916 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003917 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003918 genHoistedChecksForCountUpLoop(cUnit, mir);
3919 break;
3920 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003921 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003922 genHoistedChecksForCountDownLoop(cUnit, mir);
3923 break;
3924 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003925 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003926 genHoistedLowerBoundCheck(cUnit, mir);
3927 break;
3928 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003929 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003930 genUnconditionalBranch(cUnit,
3931 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3932 break;
3933 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07003934 case kMirOpCheckInlinePrediction: {
3935 genValidationForPredictedInline(cUnit, mir);
3936 break;
3937 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003938 default:
3939 break;
3940 }
3941}
3942
3943/*
3944 * Create a PC-reconstruction cell for the starting offset of this trace.
3945 * Since the PCR cell is placed near the end of the compiled code which is
3946 * usually out of range for a conditional branch, we put two branches (one
3947 * branch over to the loop body and one layover branch to the actual PCR) at the
3948 * end of the entry block.
3949 */
3950static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3951 ArmLIR *bodyLabel)
3952{
3953 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003954 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003955 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003956 pcrLabel->operands[0] =
3957 (int) (cUnit->method->insns + entry->startOffset);
3958 pcrLabel->operands[1] = entry->startOffset;
3959 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003960 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07003961
3962 /*
3963 * Next, create two branches - one branch over to the loop body and the
3964 * other branch to the PCR cell to punt.
3965 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003966 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003967 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003968 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003969 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003970 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3971
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003972 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003973 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003974 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003975 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003976 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3977}
3978
Ben Chengd5adae12010-03-26 17:45:28 -07003979#if defined(WITH_SELF_VERIFICATION)
3980static bool selfVerificationPuntOps(MIR *mir)
3981{
3982 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003983 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07003984
Ben Chengd5adae12010-03-26 17:45:28 -07003985 /*
3986 * All opcodes that can throw exceptions and use the
3987 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3988 * under self-verification mode.
3989 */
3990 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3991 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3992 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3993 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07003994 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07003995}
3996#endif
3997
Ben Chengba4fc8b2009-06-01 13:00:29 -07003998void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3999{
4000 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004001 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004002 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004003 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004004 int i;
4005
4006 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004007 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004008 */
Ben Chengcec26f62010-01-15 15:29:33 -08004009 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004010 dvmInitGrowableList(&chainingListByType[i], 2);
4011 }
4012
Ben Cheng00603072010-10-28 11:13:58 -07004013 GrowableListIterator iterator;
4014 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004015
buzbee2e152ba2010-12-15 16:32:35 -08004016 /* Traces start with a profiling entry point. Generate it here */
4017 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004018
Ben Chengba4fc8b2009-06-01 13:00:29 -07004019 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004020 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004021 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004022 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4023 if (bb == NULL) break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004024
Ben Cheng00603072010-10-28 11:13:58 -07004025 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004026
Ben Cheng00603072010-10-28 11:13:58 -07004027 if (bb->blockType >= kChainingCellGap) {
4028 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004029 /* Align this block first since it is a return chaining cell */
4030 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4031 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004032 /*
4033 * Append the label pseudo LIR first. Chaining cells will be handled
4034 * separately afterwards.
4035 */
4036 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4037 }
4038
Ben Cheng00603072010-10-28 11:13:58 -07004039 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004040 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004041 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004042 continue;
4043 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004044 setupLoopEntryBlock(cUnit, bb,
4045 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004046 }
Ben Cheng00603072010-10-28 11:13:58 -07004047 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004048 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004049 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004050 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004051 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004052 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004053 dvmCompilerResetRegPool(cUnit);
4054 dvmCompilerClobberAllRegs(cUnit);
4055 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004056 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004057 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004058 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004059 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004060 /* handle the codegen later */
4061 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004062 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004063 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004064 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004065 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004066 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004067 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004068 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004069 /* handle the codegen later */
4070 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004071 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004072 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004073 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004074 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004075 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004076 /* handle the codegen later */
4077 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004078 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004079 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004080 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004081 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004082 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004083 /* handle the codegen later */
4084 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004085 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004086 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004087 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004088 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004089 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004090 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004091 assert (i == cUnit->numBlocks - 2);
4092 handlePCReconstruction(cUnit, &labelList[i+1]);
4093 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004094 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004095 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004096 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07004097 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4098 jitToInterpEntries.dvmJitToInterpPunt),
4099 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004100 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004101 }
4102 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004103 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004104 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004105 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004106 /* handle the codegen later */
4107 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004108 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004109 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004110 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004111 default:
4112 break;
4113 }
4114 continue;
4115 }
Ben Chenge9695e52009-06-16 16:11:47 -07004116
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004117 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07004118
Ben Cheng00603072010-10-28 11:13:58 -07004119 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004120
Bill Buzbeec6f10662010-02-09 11:16:15 -08004121 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004122 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004123 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004124 }
4125
4126 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004127 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004128 }
4129
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004130 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004131 handleExtendedMIR(cUnit, mir);
4132 continue;
4133 }
4134
Bill Buzbee1465db52009-09-23 17:17:35 -07004135
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004136 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Dan Bornsteine4852762010-12-02 12:45:00 -08004137 InstructionFormat dalvikFormat = dexGetFormatFromOpcode(dalvikOpcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004138 char *note;
4139 if (mir->OptimizationFlags & MIR_INLINED) {
4140 note = " (I)";
4141 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4142 note = " (PI)";
4143 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4144 note = " (C)";
4145 } else {
4146 note = NULL;
4147 }
4148
Ben Cheng80211d22011-01-14 10:23:37 -08004149 ArmLIR *boundaryLIR;
4150
4151 /*
4152 * Don't generate the boundary LIR unless we are debugging this
4153 * trace or we need a scheduling barrier.
4154 */
4155 if (headLIR == NULL || cUnit->printMe == true) {
4156 boundaryLIR =
4157 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4158 mir->offset,
4159 (int) dvmCompilerGetDalvikDisassembly(
4160 &mir->dalvikInsn, note));
4161 /* Remember the first LIR for this block */
4162 if (headLIR == NULL) {
4163 headLIR = boundaryLIR;
4164 /* Set the first boundaryLIR as a scheduling barrier */
4165 headLIR->defMask = ENCODE_ALL;
4166 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004167 }
4168
Ben Cheng80211d22011-01-14 10:23:37 -08004169 /* Don't generate the SSA annotation unless verbose mode is on */
4170 if (cUnit->printMe && mir->ssaRep) {
4171 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
4172 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Chenge9695e52009-06-16 16:11:47 -07004173 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004174
Ben Chengba4fc8b2009-06-01 13:00:29 -07004175 bool notHandled;
4176 /*
4177 * Debugging: screen the opcode first to see if it is in the
4178 * do[-not]-compile list
4179 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004180 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004181#if defined(WITH_SELF_VERIFICATION)
4182 if (singleStepMe == false) {
4183 singleStepMe = selfVerificationPuntOps(mir);
4184 }
4185#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004186 if (singleStepMe || cUnit->allSingleStep) {
4187 notHandled = false;
4188 genInterpSingleStep(cUnit, mir);
4189 } else {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004190 opcodeCoverage[dalvikOpcode]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004191 switch (dalvikFormat) {
4192 case kFmt10t:
4193 case kFmt20t:
4194 case kFmt30t:
4195 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004196 mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004197 break;
4198 case kFmt10x:
4199 notHandled = handleFmt10x(cUnit, mir);
4200 break;
4201 case kFmt11n:
4202 case kFmt31i:
4203 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4204 break;
4205 case kFmt11x:
4206 notHandled = handleFmt11x(cUnit, mir);
4207 break;
4208 case kFmt12x:
4209 notHandled = handleFmt12x(cUnit, mir);
4210 break;
4211 case kFmt20bc:
jeffhao71eee1f2011-01-04 14:18:54 -08004212 case kFmt40sc:
4213 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004214 break;
4215 case kFmt21c:
4216 case kFmt31c:
jeffhao71eee1f2011-01-04 14:18:54 -08004217 case kFmt41c:
4218 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004219 break;
4220 case kFmt21h:
4221 notHandled = handleFmt21h(cUnit, mir);
4222 break;
4223 case kFmt21s:
4224 notHandled = handleFmt21s(cUnit, mir);
4225 break;
4226 case kFmt21t:
Ben Cheng00603072010-10-28 11:13:58 -07004227 notHandled = handleFmt21t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004228 break;
4229 case kFmt22b:
4230 case kFmt22s:
4231 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4232 break;
4233 case kFmt22c:
jeffhao71eee1f2011-01-04 14:18:54 -08004234 case kFmt52c:
4235 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004236 break;
4237 case kFmt22cs:
4238 notHandled = handleFmt22cs(cUnit, mir);
4239 break;
4240 case kFmt22t:
Ben Cheng00603072010-10-28 11:13:58 -07004241 notHandled = handleFmt22t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004242 break;
4243 case kFmt22x:
4244 case kFmt32x:
4245 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4246 break;
4247 case kFmt23x:
4248 notHandled = handleFmt23x(cUnit, mir);
4249 break;
4250 case kFmt31t:
4251 notHandled = handleFmt31t(cUnit, mir);
4252 break;
4253 case kFmt3rc:
4254 case kFmt35c:
jeffhao71eee1f2011-01-04 14:18:54 -08004255 case kFmt5rc:
4256 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004257 labelList);
4258 break;
4259 case kFmt3rms:
4260 case kFmt35ms:
Ben Cheng00603072010-10-28 11:13:58 -07004261 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004262 labelList);
4263 break;
Dan Bornstein7b3e9b02010-11-09 17:15:10 -08004264 case kFmt35mi:
4265 case kFmt3rmi:
Bill Buzbeece46c942009-11-20 15:41:34 -08004266 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08004267 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004268 case kFmt51l:
4269 notHandled = handleFmt51l(cUnit, mir);
4270 break;
4271 default:
4272 notHandled = true;
4273 break;
4274 }
4275 }
4276 if (notHandled) {
4277 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4278 mir->offset,
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004279 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07004280 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004281 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004282 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004283 }
4284 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004285
Ben Cheng00603072010-10-28 11:13:58 -07004286 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004287 dvmCompilerAppendLIR(cUnit,
4288 (LIR *) cUnit->loopAnalysis->branchToBody);
4289 dvmCompilerAppendLIR(cUnit,
4290 (LIR *) cUnit->loopAnalysis->branchToPCR);
4291 }
4292
4293 if (headLIR) {
4294 /*
4295 * Eliminate redundant loads/stores and delay stores into later
4296 * slots
4297 */
4298 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4299 cUnit->lastLIRInsn);
4300 }
4301
4302gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004303 /*
4304 * Check if the block is terminated due to trace length constraint -
4305 * insert an unconditional branch to the chaining cell.
4306 */
Ben Cheng00603072010-10-28 11:13:58 -07004307 if (bb->needFallThroughBranch) {
Ben Cheng1efc9c52009-06-08 18:25:27 -07004308 genUnconditionalBranch(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004309 &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004310 }
4311
Ben Chengba4fc8b2009-06-01 13:00:29 -07004312 }
4313
Ben Chenge9695e52009-06-16 16:11:47 -07004314 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004315 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004316 size_t j;
4317 int *blockIdList = (int *) chainingListByType[i].elemList;
4318
4319 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4320
4321 /* No chaining cells of this type */
4322 if (cUnit->numChainingCells[i] == 0)
4323 continue;
4324
4325 /* Record the first LIR for a new type of chaining cell */
4326 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4327
4328 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4329 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004330 BasicBlock *chainingBlock =
4331 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4332 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004333
4334 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004335 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004336
4337 /* Insert the pseudo chaining instruction */
4338 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4339
4340
Ben Cheng00603072010-10-28 11:13:58 -07004341 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004342 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004343 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004344 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004345 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004346 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004347 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004348 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004349 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004350 handleInvokePredictedChainingCell(cUnit);
4351 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004352 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004353 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004354 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004355 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004356 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004357 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004358 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004359 default:
Ben Cheng00603072010-10-28 11:13:58 -07004360 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004361 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004362 }
4363 }
4364 }
Ben Chenge9695e52009-06-16 16:11:47 -07004365
Ben Chengcec26f62010-01-15 15:29:33 -08004366 /* Mark the bottom of chaining cells */
4367 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4368
Ben Cheng6c10a972009-10-29 14:39:18 -07004369 /*
4370 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4371 * of all chaining cells for the overflow cases.
4372 */
4373 if (cUnit->switchOverflowPad) {
4374 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4375 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4376 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4377 opRegReg(cUnit, kOpAdd, r1, r1);
4378 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004379#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004380 loadConstant(cUnit, r0, kSwitchOverflow);
4381#endif
4382 opReg(cUnit, kOpBlx, r2);
4383 }
4384
Ben Chenge9695e52009-06-16 16:11:47 -07004385 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004386
4387#if defined(WITH_SELF_VERIFICATION)
4388 selfVerificationBranchInsertPass(cUnit);
4389#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004390}
4391
buzbee2e152ba2010-12-15 16:32:35 -08004392/*
4393 * Accept the work and start compiling. Returns true if compilation
4394 * is attempted.
4395 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004396bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004397{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004398 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004399 bool isCompile;
4400 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004401
Ben Cheng6999d842010-01-26 16:46:15 -08004402 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004403 return false;
4404 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004405
Ben Chengccd6c012009-10-15 14:52:45 -07004406 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004407 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004408 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004409 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004410 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004411 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4412 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004413 break;
4414 case kWorkOrderTraceDebug: {
4415 bool oldPrintMe = gDvmJit.printMe;
4416 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004417 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004418 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004419 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004420 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4421 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004422 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004423 break;
4424 }
buzbee2e152ba2010-12-15 16:32:35 -08004425 case kWorkOrderProfileMode:
4426 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4427 isCompile = false;
4428 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004429 default:
buzbee2e152ba2010-12-15 16:32:35 -08004430 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004431 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004432 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004433 }
buzbee2e152ba2010-12-15 16:32:35 -08004434 if (!success)
4435 work->result.codeAddress = NULL;
4436 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004437}
4438
Ben Chengba4fc8b2009-06-01 13:00:29 -07004439/* Architectural-specific debugging helpers go here */
4440void dvmCompilerArchDump(void)
4441{
4442 /* Print compiled opcode in this VM instance */
4443 int i, start, streak;
4444 char buf[1024];
4445
4446 streak = i = 0;
4447 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004448 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004449 i++;
4450 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004451 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004452 return;
4453 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004454 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004455 if (opcodeCoverage[i]) {
4456 streak++;
4457 } else {
4458 if (streak == 1) {
4459 sprintf(buf+strlen(buf), "%x,", start);
4460 } else {
4461 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4462 }
4463 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004464 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004465 i++;
4466 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004467 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004468 streak = 1;
4469 start = i;
4470 }
4471 }
4472 }
4473 if (streak) {
4474 if (streak == 1) {
4475 sprintf(buf+strlen(buf), "%x", start);
4476 } else {
4477 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4478 }
4479 }
4480 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004481 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004482 }
4483}
Ben Chengd7d426a2009-09-22 11:23:36 -07004484
4485/* Common initialization routine for an architecture family */
4486bool dvmCompilerArchInit()
4487{
4488 int i;
4489
Bill Buzbee1465db52009-09-23 17:17:35 -07004490 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004491 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004492 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004493 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004494 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004495 }
4496 }
4497
Ben Cheng5d90c202009-11-22 23:31:11 -08004498 return dvmCompilerArchVariantInit();
4499}
4500
4501void *dvmCompilerGetInterpretTemplate()
4502{
4503 return (void*) ((int)gDvmJit.codeCache +
4504 templateEntryOffsets[TEMPLATE_INTERPRET]);
4505}
4506
buzbeebff121a2010-08-04 15:25:06 -07004507/* Needed by the Assembler */
4508void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4509{
4510 setupResourceMasks(lir);
4511}
4512
Ben Cheng5d90c202009-11-22 23:31:11 -08004513/* Needed by the ld/st optmizatons */
4514ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4515{
4516 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4517}
4518
4519/* Needed by the register allocator */
4520ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4521{
4522 return genRegCopy(cUnit, rDest, rSrc);
4523}
4524
4525/* Needed by the register allocator */
4526void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4527 int srcLo, int srcHi)
4528{
4529 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4530}
4531
4532void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4533 int displacement, int rSrc, OpSize size)
4534{
4535 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4536}
4537
4538void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4539 int displacement, int rSrcLo, int rSrcHi)
4540{
4541 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004542}