blob: 1b7c6ed02bfee736c2cc57a2ba1a430691b15c06 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
buzbee8f8109a2010-08-31 10:16:35 -070034 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
buzbee919eb062010-07-12 12:59:22 -070035 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, cardTable),
36 regCardBase);
37 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
38 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
39 kUnsignedByte);
40 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
41 target->defMask = ENCODE_ALL;
42 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070043 dvmCompilerFreeTemp(cUnit, regCardBase);
44 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070045}
46
Ben Cheng5d90c202009-11-22 23:31:11 -080047static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
48 int srcSize, int tgtSize)
49{
50 /*
51 * Don't optimize the register usage since it calls out to template
52 * functions
53 */
54 RegLocation rlSrc;
55 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080056 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080057 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080058 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080059 loadValueDirectFixed(cUnit, rlSrc, r0);
60 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080061 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080062 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
63 }
Ben Chengbd1326d2010-04-02 15:04:53 -070064 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080065 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080066 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080067 if (tgtSize == 1) {
68 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080069 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
70 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080071 storeValue(cUnit, rlDest, rlResult);
72 } else {
73 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080074 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
75 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080076 storeValueWide(cUnit, rlDest, rlResult);
77 }
78 return false;
79}
Ben Chengba4fc8b2009-06-01 13:00:29 -070080
Ben Cheng5d90c202009-11-22 23:31:11 -080081static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
82 RegLocation rlDest, RegLocation rlSrc1,
83 RegLocation rlSrc2)
84{
85 RegLocation rlResult;
86 void* funct;
87
Dan Bornstein9a1f8162010-12-01 17:02:26 -080088 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -080089 case OP_ADD_FLOAT_2ADDR:
90 case OP_ADD_FLOAT:
91 funct = (void*) __aeabi_fadd;
92 break;
93 case OP_SUB_FLOAT_2ADDR:
94 case OP_SUB_FLOAT:
95 funct = (void*) __aeabi_fsub;
96 break;
97 case OP_DIV_FLOAT_2ADDR:
98 case OP_DIV_FLOAT:
99 funct = (void*) __aeabi_fdiv;
100 break;
101 case OP_MUL_FLOAT_2ADDR:
102 case OP_MUL_FLOAT:
103 funct = (void*) __aeabi_fmul;
104 break;
105 case OP_REM_FLOAT_2ADDR:
106 case OP_REM_FLOAT:
107 funct = (void*) fmodf;
108 break;
109 case OP_NEG_FLOAT: {
110 genNegFloat(cUnit, rlDest, rlSrc1);
111 return false;
112 }
113 default:
114 return true;
115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800116 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800117 loadValueDirectFixed(cUnit, rlSrc1, r0);
118 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700119 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800120 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800121 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800122 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800123 storeValue(cUnit, rlDest, rlResult);
124 return false;
125}
126
127static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
128 RegLocation rlDest, RegLocation rlSrc1,
129 RegLocation rlSrc2)
130{
131 RegLocation rlResult;
132 void* funct;
133
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800134 switch (mir->dalvikInsn.opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 case OP_ADD_DOUBLE_2ADDR:
136 case OP_ADD_DOUBLE:
137 funct = (void*) __aeabi_dadd;
138 break;
139 case OP_SUB_DOUBLE_2ADDR:
140 case OP_SUB_DOUBLE:
141 funct = (void*) __aeabi_dsub;
142 break;
143 case OP_DIV_DOUBLE_2ADDR:
144 case OP_DIV_DOUBLE:
145 funct = (void*) __aeabi_ddiv;
146 break;
147 case OP_MUL_DOUBLE_2ADDR:
148 case OP_MUL_DOUBLE:
149 funct = (void*) __aeabi_dmul;
150 break;
151 case OP_REM_DOUBLE_2ADDR:
152 case OP_REM_DOUBLE:
153 funct = (void*) fmod;
154 break;
155 case OP_NEG_DOUBLE: {
156 genNegDouble(cUnit, rlDest, rlSrc1);
157 return false;
158 }
159 default:
160 return true;
161 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800162 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700163 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800164 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
165 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
166 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800167 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800168 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800169 storeValueWide(cUnit, rlDest, rlResult);
170 return false;
171}
172
173static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
174{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800175 Opcode opcode = mir->dalvikInsn.opcode;
Ben Cheng5d90c202009-11-22 23:31:11 -0800176
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800177 switch (opcode) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800178 case OP_INT_TO_FLOAT:
179 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
180 case OP_FLOAT_TO_INT:
181 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
182 case OP_DOUBLE_TO_FLOAT:
183 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
184 case OP_FLOAT_TO_DOUBLE:
185 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
186 case OP_INT_TO_DOUBLE:
187 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
188 case OP_DOUBLE_TO_INT:
189 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
190 case OP_FLOAT_TO_LONG:
191 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
192 case OP_LONG_TO_FLOAT:
193 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
194 case OP_DOUBLE_TO_LONG:
195 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
196 case OP_LONG_TO_DOUBLE:
197 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
198 default:
199 return true;
200 }
201 return false;
202}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700203
Jeff Hao97319a82009-08-12 16:57:15 -0700204#if defined(WITH_SELF_VERIFICATION)
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800205static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpcode opcode,
jeffhao9e45c0b2010-02-03 10:24:05 -0800206 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700207{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800208 ArmLIR *insn = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800209 insn->opcode = opcode;
jeffhao9e45c0b2010-02-03 10:24:05 -0800210 insn->operands[0] = dest;
211 insn->operands[1] = src1;
212 setupResourceMasks(insn);
213 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700214}
215
jeffhao9e45c0b2010-02-03 10:24:05 -0800216static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700217{
jeffhao9e45c0b2010-02-03 10:24:05 -0800218 ArmLIR *thisLIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800219 TemplateOpcode opcode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700220
jeffhao9e45c0b2010-02-03 10:24:05 -0800221 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
222 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
223 thisLIR = NEXT_LIR(thisLIR)) {
224 if (thisLIR->branchInsertSV) {
225 /* Branch to mem op decode template */
226 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800227 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
228 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
jeffhao9e45c0b2010-02-03 10:24:05 -0800229 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800230 (int) gDvmJit.codeCache + templateEntryOffsets[opcode],
231 (int) gDvmJit.codeCache + templateEntryOffsets[opcode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700232 }
233 }
Jeff Hao97319a82009-08-12 16:57:15 -0700234}
Jeff Hao97319a82009-08-12 16:57:15 -0700235#endif
236
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800237/* Generate conditional branch instructions */
238static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
239 ArmConditionCode cond,
240 ArmLIR *target)
241{
242 ArmLIR *branch = opCondBranch(cUnit, cond);
243 branch->generic.target = (LIR *) target;
244 return branch;
245}
246
Ben Chengba4fc8b2009-06-01 13:00:29 -0700247/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700248static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
249 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700250{
Bill Buzbee1465db52009-09-23 17:17:35 -0700251 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700252 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
253}
254
255/* Load a wide field from an object instance */
256static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
257{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800258 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
259 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700260 RegLocation rlResult;
261 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800262 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700263
Bill Buzbee1465db52009-09-23 17:17:35 -0700264 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700265
Bill Buzbee1465db52009-09-23 17:17:35 -0700266 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
267 NULL);/* null object? */
268 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800269 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700270
271 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700272 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700273 HEAP_ACCESS_SHADOW(false);
274
Bill Buzbeec6f10662010-02-09 11:16:15 -0800275 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700276 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700277}
278
279/* Store a wide field to an object instance */
280static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
281{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800282 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
283 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700284 rlObj = loadValue(cUnit, rlObj, kCoreReg);
285 int regPtr;
286 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
287 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
288 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800289 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700290 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700291
292 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700293 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700294 HEAP_ACCESS_SHADOW(false);
295
Bill Buzbeec6f10662010-02-09 11:16:15 -0800296 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700297}
298
299/*
300 * Load a field from an object instance
301 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700302 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700303static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700304 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700305{
Bill Buzbee1465db52009-09-23 17:17:35 -0700306 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700307 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800308 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
309 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700310 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700311 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700312 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
313 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700314
315 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800316 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
317 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700318 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700319 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700320 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700321 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700322
Bill Buzbee1465db52009-09-23 17:17:35 -0700323 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700324}
325
326/*
327 * Store a field to an object instance
328 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700329 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700330static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700331 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700332{
Bill Buzbee749e8162010-07-07 06:55:56 -0700333 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800334 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
335 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700336 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700337 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700338 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
339 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700340
buzbeeecf8f6e2010-07-20 14:53:42 -0700341 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -0700342 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -0700343 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700344 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700345 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700346 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700347 if (isObject) {
348 /* NOTE: marking card based on object head */
349 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
350 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700351}
352
353
Ben Chengba4fc8b2009-06-01 13:00:29 -0700354/*
355 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700356 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700357static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700358 RegLocation rlArray, RegLocation rlIndex,
359 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700360{
Bill Buzbee749e8162010-07-07 06:55:56 -0700361 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700362 int lenOffset = offsetof(ArrayObject, length);
363 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700364 RegLocation rlResult;
365 rlArray = loadValue(cUnit, rlArray, kCoreReg);
366 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
367 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700368
369 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700370 ArmLIR * pcrLabel = NULL;
371
372 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700373 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
374 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700375 }
376
Bill Buzbeec6f10662010-02-09 11:16:15 -0800377 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700378
Ben Cheng4238ec22009-08-24 16:32:22 -0700379 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800380 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700381 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700382 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
383 /* regPtr -> array data */
384 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
385 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
386 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800387 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700388 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700389 /* regPtr -> array data */
390 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700391 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700392 if ((size == kLong) || (size == kDouble)) {
393 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800394 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700395 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
396 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800397 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700398 } else {
399 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
400 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700401 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700402
403 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700404 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700405 HEAP_ACCESS_SHADOW(false);
406
Bill Buzbeec6f10662010-02-09 11:16:15 -0800407 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700408 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700409 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700410 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700411
412 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700413 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
414 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700415 HEAP_ACCESS_SHADOW(false);
416
Bill Buzbeec6f10662010-02-09 11:16:15 -0800417 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700418 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700419 }
420}
421
Ben Chengba4fc8b2009-06-01 13:00:29 -0700422/*
423 * Generate array store
424 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700425 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700426static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700427 RegLocation rlArray, RegLocation rlIndex,
428 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700429{
Bill Buzbee749e8162010-07-07 06:55:56 -0700430 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700431 int lenOffset = offsetof(ArrayObject, length);
432 int dataOffset = offsetof(ArrayObject, contents);
433
Bill Buzbee1465db52009-09-23 17:17:35 -0700434 int regPtr;
435 rlArray = loadValue(cUnit, rlArray, kCoreReg);
436 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700437
Bill Buzbeec6f10662010-02-09 11:16:15 -0800438 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
439 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700440 regPtr = rlArray.lowReg;
441 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800442 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700443 genRegCopy(cUnit, regPtr, rlArray.lowReg);
444 }
Ben Chenge9695e52009-06-16 16:11:47 -0700445
Ben Cheng1efc9c52009-06-08 18:25:27 -0700446 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700447 ArmLIR * pcrLabel = NULL;
448
449 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700450 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
451 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700452 }
453
454 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800455 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700456 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700457 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700458 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
459 /* regPtr -> array data */
460 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
461 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
462 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800463 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700464 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700465 /* regPtr -> array data */
466 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700467 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700468 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700469 if ((size == kLong) || (size == kDouble)) {
470 //TODO: need specific wide routine that can handle fp regs
471 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800472 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700473 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
474 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800475 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700476 } else {
477 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
478 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700479 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700480
481 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700482 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700483 HEAP_ACCESS_SHADOW(false);
484
Bill Buzbeec6f10662010-02-09 11:16:15 -0800485 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700486 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700487 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700488
489 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700490 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
491 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700492 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800493 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700494}
495
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800496/*
497 * Generate array object store
498 * Must use explicit register allocation here because of
499 * call-out to dvmCanPutArrayElement
500 */
501static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
502 RegLocation rlArray, RegLocation rlIndex,
503 RegLocation rlSrc, int scale)
504{
505 int lenOffset = offsetof(ArrayObject, length);
506 int dataOffset = offsetof(ArrayObject, contents);
507
508 dvmCompilerFlushAllRegs(cUnit);
509
510 int regLen = r0;
511 int regPtr = r4PC; /* Preserved across call */
512 int regArray = r1;
513 int regIndex = r7; /* Preserved across call */
514
515 loadValueDirectFixed(cUnit, rlArray, regArray);
516 loadValueDirectFixed(cUnit, rlIndex, regIndex);
517
518 /* null object? */
519 ArmLIR * pcrLabel = NULL;
520
521 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
522 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
523 mir->offset, NULL);
524 }
525
526 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
527 /* Get len */
528 loadWordDisp(cUnit, regArray, lenOffset, regLen);
529 /* regPtr -> array data */
530 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
531 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
532 pcrLabel);
533 } else {
534 /* regPtr -> array data */
535 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
536 }
537
538 /* Get object to store */
539 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700540 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800541
542 /* Are we storing null? If so, avoid check */
buzbee8f8109a2010-08-31 10:16:35 -0700543 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800544
545 /* Make sure the types are compatible */
546 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
547 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
548 opReg(cUnit, kOpBlx, r2);
549 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700550
551 /*
552 * Using fixed registers here, and counting on r4 and r7 being
553 * preserved across the above call. Tell the register allocation
554 * utilities about the regs we are using directly
555 */
556 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
557 dvmCompilerLockTemp(cUnit, regIndex); // r7
558 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700559 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700560
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800561 /* Bad? - roll back and re-execute if so */
562 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
563
buzbee919eb062010-07-12 12:59:22 -0700564 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800565 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700566 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800567
568 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
569 target->defMask = ENCODE_ALL;
570 branchOver->generic.target = (LIR *) target;
571
Ben Cheng11d8f142010-03-24 15:24:19 -0700572 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800573 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
574 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700575 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700576
buzbeebaf196a2010-08-04 10:13:15 -0700577 dvmCompilerFreeTemp(cUnit, regPtr);
578 dvmCompilerFreeTemp(cUnit, regIndex);
579
buzbee919eb062010-07-12 12:59:22 -0700580 /* NOTE: marking card here based on object head */
581 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800582}
583
Ben Cheng5d90c202009-11-22 23:31:11 -0800584static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
585 RegLocation rlDest, RegLocation rlSrc1,
586 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700587{
Ben Chenge9695e52009-06-16 16:11:47 -0700588 /*
589 * Don't mess with the regsiters here as there is a particular calling
590 * convention to the out-of-line handler.
591 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700592 RegLocation rlResult;
593
594 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
595 loadValueDirect(cUnit, rlShift, r2);
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800596 switch( mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -0700597 case OP_SHL_LONG:
598 case OP_SHL_LONG_2ADDR:
599 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
600 break;
601 case OP_SHR_LONG:
602 case OP_SHR_LONG_2ADDR:
603 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
604 break;
605 case OP_USHR_LONG:
606 case OP_USHR_LONG_2ADDR:
607 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
608 break;
609 default:
610 return true;
611 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800612 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700613 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700614 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700615}
Ben Chenge9695e52009-06-16 16:11:47 -0700616
Ben Cheng5d90c202009-11-22 23:31:11 -0800617static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
618 RegLocation rlDest, RegLocation rlSrc1,
619 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700620{
Bill Buzbee1465db52009-09-23 17:17:35 -0700621 RegLocation rlResult;
622 OpKind firstOp = kOpBkpt;
623 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700624 bool callOut = false;
625 void *callTgt;
626 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700627
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800628 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700629 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700630 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800631 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700632 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
633 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
634 storeValueWide(cUnit, rlDest, rlResult);
635 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700636 break;
637 case OP_ADD_LONG:
638 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700639 firstOp = kOpAdd;
640 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700641 break;
642 case OP_SUB_LONG:
643 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700644 firstOp = kOpSub;
645 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700646 break;
647 case OP_MUL_LONG:
648 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700650 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700651 case OP_DIV_LONG:
652 case OP_DIV_LONG_2ADDR:
653 callOut = true;
654 retReg = r0;
655 callTgt = (void*)__aeabi_ldivmod;
656 break;
657 /* NOTE - result is in r2/r3 instead of r0/r1 */
658 case OP_REM_LONG:
659 case OP_REM_LONG_2ADDR:
660 callOut = true;
661 callTgt = (void*)__aeabi_ldivmod;
662 retReg = r2;
663 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700664 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700665 case OP_AND_LONG:
666 firstOp = kOpAnd;
667 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700668 break;
669 case OP_OR_LONG:
670 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700671 firstOp = kOpOr;
672 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700673 break;
674 case OP_XOR_LONG:
675 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700676 firstOp = kOpXor;
677 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700678 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700679 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800680 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800681 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700682 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800683 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700684 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700685 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800686 tReg, rlSrc2.lowReg);
687 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
688 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700689 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700690 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700691 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 default:
693 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800694 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700695 }
696 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700697 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700698 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700699 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800700 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700701 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700702 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700703 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
704 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800705 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700706 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800707 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700708 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800709 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 }
712 return false;
713}
714
Ben Cheng5d90c202009-11-22 23:31:11 -0800715static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
716 RegLocation rlDest, RegLocation rlSrc1,
717 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700718{
Bill Buzbee1465db52009-09-23 17:17:35 -0700719 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700720 bool callOut = false;
721 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700722 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700723 int retReg = r0;
724 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700725 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800726 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700727
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800728 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700729 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 op = kOpNeg;
731 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700732 break;
733 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700734 op = kOpMvn;
735 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700736 break;
737 case OP_ADD_INT:
738 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700740 break;
741 case OP_SUB_INT:
742 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700743 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700744 break;
745 case OP_MUL_INT:
746 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700747 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700748 break;
749 case OP_DIV_INT:
750 case OP_DIV_INT_2ADDR:
751 callOut = true;
752 checkZero = true;
753 callTgt = __aeabi_idiv;
754 retReg = r0;
755 break;
756 /* NOTE: returns in r1 */
757 case OP_REM_INT:
758 case OP_REM_INT_2ADDR:
759 callOut = true;
760 checkZero = true;
761 callTgt = __aeabi_idivmod;
762 retReg = r1;
763 break;
764 case OP_AND_INT:
765 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700766 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700767 break;
768 case OP_OR_INT:
769 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700770 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700771 break;
772 case OP_XOR_INT:
773 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700774 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700775 break;
776 case OP_SHL_INT:
777 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800778 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700779 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700780 break;
781 case OP_SHR_INT:
782 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800783 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700784 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700785 break;
786 case OP_USHR_INT:
787 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800788 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700789 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700790 break;
791 default:
792 LOGE("Invalid word arith op: 0x%x(%d)",
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800793 mir->dalvikInsn.opcode, mir->dalvikInsn.opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800794 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700795 }
796 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700797 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
798 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800799 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700800 opRegReg(cUnit, op, rlResult.lowReg,
801 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700802 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700803 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800804 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800805 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800806 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800807 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800808 opRegRegReg(cUnit, op, rlResult.lowReg,
809 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800810 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800811 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800812 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800813 opRegRegReg(cUnit, op, rlResult.lowReg,
814 rlSrc1.lowReg, rlSrc2.lowReg);
815 }
Ben Chenge9695e52009-06-16 16:11:47 -0700816 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700817 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700818 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700819 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800820 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700822 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700823 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700824 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700826 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800828 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700829 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800830 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700831 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800832 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700833 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700834 }
835 return false;
836}
837
Ben Cheng5d90c202009-11-22 23:31:11 -0800838static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700839{
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800840 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700841 RegLocation rlDest;
842 RegLocation rlSrc1;
843 RegLocation rlSrc2;
844 /* Deduce sizes of operands */
845 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800846 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
847 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700848 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800849 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
850 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700851 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800852 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
853 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700854 assert(mir->ssaRep->numUses == 4);
855 }
856 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800857 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700858 } else {
859 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800860 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700861 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700862
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800863 if ((opcode >= OP_ADD_LONG_2ADDR) && (opcode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800864 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700865 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800866 if ((opcode >= OP_ADD_LONG) && (opcode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800867 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700868 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800869 if ((opcode >= OP_SHL_LONG_2ADDR) && (opcode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800870 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700871 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800872 if ((opcode >= OP_SHL_LONG) && (opcode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800873 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700874 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800875 if ((opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800876 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700877 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800878 if ((opcode >= OP_ADD_INT) && (opcode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800879 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700880 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800881 if ((opcode >= OP_ADD_FLOAT_2ADDR) && (opcode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800882 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700883 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800884 if ((opcode >= OP_ADD_FLOAT) && (opcode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800885 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700886 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800887 if ((opcode >= OP_ADD_DOUBLE_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800888 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700889 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800890 if ((opcode >= OP_ADD_DOUBLE) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800891 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700892 }
893 return true;
894}
895
Bill Buzbee1465db52009-09-23 17:17:35 -0700896/* Generate unconditional branch instructions */
897static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
898{
899 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
900 branch->generic.target = (LIR *) target;
901 return branch;
902}
903
Bill Buzbee1465db52009-09-23 17:17:35 -0700904/* Perform the actual operation for OP_RETURN_* */
905static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
906{
Ben Chengcfdeca32011-01-14 11:36:46 -0800907 if (!cUnit->methodJitMode) {
908 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
909 TEMPLATE_RETURN_PROF :
910 TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700911#if defined(WITH_JIT_TUNING)
Ben Chengcfdeca32011-01-14 11:36:46 -0800912 gDvmJit.returnOp++;
Bill Buzbee1465db52009-09-23 17:17:35 -0700913#endif
Ben Chengcfdeca32011-01-14 11:36:46 -0800914 int dPC = (int) (cUnit->method->insns + mir->offset);
915 /* Insert branch, but defer setting of target */
916 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
917 /* Set up the place holder to reconstruct this Dalvik PC */
918 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
919 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
920 pcrLabel->operands[0] = dPC;
921 pcrLabel->operands[1] = mir->offset;
922 /* Insert the place holder to the growable list */
923 dvmInsertGrowableList(&cUnit->pcReconstructionList,
924 (intptr_t) pcrLabel);
925 /* Branch to the PC reconstruction code */
926 branch->generic.target = (LIR *) pcrLabel;
927 }
928 /* TODO: Move result to InterpState for non-void returns */
Bill Buzbee1465db52009-09-23 17:17:35 -0700929}
930
Ben Chengba4fc8b2009-06-01 13:00:29 -0700931static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
932 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700933 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934{
935 unsigned int i;
936 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700937 RegLocation rlArg;
938 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700939
Bill Buzbee1465db52009-09-23 17:17:35 -0700940 /*
941 * Load arguments to r0..r4. Note that these registers may contain
942 * live values, so we clobber them immediately after loading to prevent
943 * them from being used as sources for subsequent loads.
944 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800945 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700946 for (i = 0; i < dInsn->vA; i++) {
947 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800948 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700949 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700950 }
951 if (regMask) {
952 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700953 opRegRegImm(cUnit, kOpSub, r7, rFP,
954 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700955 /* generate null check */
956 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800957 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700958 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700959 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700960 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700961 }
962}
963
964static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
965 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700966 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700967{
968 int srcOffset = dInsn->vC << 2;
969 int numArgs = dInsn->vA;
970 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700971
972 /*
973 * Note: here, all promoted registers will have been flushed
974 * back to the Dalvik base locations, so register usage restrictins
975 * are lifted. All parms loaded from original Dalvik register
976 * region - even though some might conceivably have valid copies
977 * cached in a preserved register.
978 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800979 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700980
Ben Chengba4fc8b2009-06-01 13:00:29 -0700981 /*
982 * r4PC : &rFP[vC]
983 * r7: &newFP[0]
984 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700985 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700986 /* load [r0 .. min(numArgs,4)] */
987 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700988 /*
989 * Protect the loadMultiple instruction from being reordered with other
990 * Dalvik stack accesses.
jeffhao71eee1f2011-01-04 14:18:54 -0800991 *
992 * This code is also shared by the invoke jumbo instructions, and this
993 * does not need to be done if the invoke jumbo has no arguments.
Ben Chengd7d426a2009-09-22 11:23:36 -0700994 */
jeffhao71eee1f2011-01-04 14:18:54 -0800995 if (numArgs != 0) loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700996
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 opRegRegImm(cUnit, kOpSub, r7, rFP,
998 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700999 /* generate null check */
1000 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001001 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -07001002 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001003 }
1004
1005 /*
1006 * Handle remaining 4n arguments:
1007 * store previously loaded 4 values and load the next 4 values
1008 */
1009 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001010 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 /*
1012 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001013 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001014 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001015 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001016 /* No need to generate the loop structure if numArgs <= 11 */
1017 if (numArgs > 11) {
1018 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001019 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001020 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001021 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001022 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001023 /*
1024 * Protect the loadMultiple instruction from being reordered with other
1025 * Dalvik stack accesses.
1026 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001027 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001028 /* No need to generate the loop structure if numArgs <= 11 */
1029 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001030 opRegImm(cUnit, kOpSub, rFP, 4);
1031 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001032 }
1033 }
1034
1035 /* Save the last batch of loaded values */
jeffhao71eee1f2011-01-04 14:18:54 -08001036 if (numArgs != 0) storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001037
1038 /* Generate the loop epilogue - don't use r0 */
1039 if ((numArgs > 4) && (numArgs % 4)) {
1040 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001041 /*
1042 * Protect the loadMultiple instruction from being reordered with other
1043 * Dalvik stack accesses.
1044 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001045 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001048 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001049
1050 /* Save the modulo 4 arguments */
1051 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001052 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001053 }
1054}
1055
Ben Cheng38329f52009-07-07 14:19:20 -07001056/*
1057 * Generate code to setup the call stack then jump to the chaining cell if it
1058 * is not a native method.
1059 */
1060static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001061 BasicBlock *bb, ArmLIR *labelList,
1062 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001063 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001064{
Bill Buzbee1465db52009-09-23 17:17:35 -07001065 /*
1066 * Note: all Dalvik register state should be flushed to
1067 * memory by the point, so register usage restrictions no
1068 * longer apply. All temp & preserved registers may be used.
1069 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001070 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001071 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001072
1073 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001074 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengc8293e72010-10-12 11:50:10 -07001075
Ben Chengba4fc8b2009-06-01 13:00:29 -07001076 /* r4PC = dalvikCallsite */
1077 loadConstant(cUnit, r4PC,
1078 (int) (cUnit->method->insns + mir->offset));
1079 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Chengc8293e72010-10-12 11:50:10 -07001080
1081 /* r7 = calleeMethod->registersSize */
1082 loadConstant(cUnit, r7, calleeMethod->registersSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001083 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001084 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001085 * r1 = &ChainingCell
Ben Chengc8293e72010-10-12 11:50:10 -07001086 * r2 = calleeMethod->outsSize (to be loaded later for Java callees)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001087 * r4PC = callsiteDPC
Ben Chengc8293e72010-10-12 11:50:10 -07001088 * r7 = calleeMethod->registersSize
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089 */
1090 if (dvmIsNativeMethod(calleeMethod)) {
buzbee18fba342011-01-19 15:31:15 -08001091 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1092 TEMPLATE_INVOKE_METHOD_NATIVE_PROF :
1093 TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001094#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001095 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001096#endif
1097 } else {
Ben Chengc8293e72010-10-12 11:50:10 -07001098 /* For Java callees, set up r2 to be calleeMethod->outsSize */
1099 loadConstant(cUnit, r2, calleeMethod->outsSize);
buzbee18fba342011-01-19 15:31:15 -08001100 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1101 TEMPLATE_INVOKE_METHOD_CHAIN_PROF :
1102 TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001103#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001104 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001105#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001106 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1108 }
1109 /* Handle exceptions using the interpreter */
1110 genTrap(cUnit, mir->offset, pcrLabel);
1111}
1112
Ben Cheng38329f52009-07-07 14:19:20 -07001113/*
1114 * Generate code to check the validity of a predicted chain and take actions
1115 * based on the result.
1116 *
1117 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1118 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1119 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1120 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1121 * 0x426a99b2 : blx_2 see above --+
1122 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1123 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1124 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1125 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1126 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001127 * 0x426a99be : ldr r7, [pc, #off]--+ dvmJitToPatchPredictedChain
Ben Cheng38329f52009-07-07 14:19:20 -07001128 * 0x426a99c0 : blx r7 --+
1129 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1130 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1131 * 0x426a99c6 : blx_2 see above --+
1132 */
1133static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1134 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001135 ArmLIR *retChainingCell,
1136 ArmLIR *predChainingCell,
1137 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001138{
Bill Buzbee1465db52009-09-23 17:17:35 -07001139 /*
1140 * Note: all Dalvik register state should be flushed to
1141 * memory by the point, so register usage restrictions no
1142 * longer apply. Lock temps to prevent them from being
1143 * allocated by utility routines.
1144 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001145 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001146
Ben Cheng38329f52009-07-07 14:19:20 -07001147 /* "this" is already left in r0 by genProcessArgs* */
1148
1149 /* r4PC = dalvikCallsite */
1150 loadConstant(cUnit, r4PC,
1151 (int) (cUnit->method->insns + mir->offset));
1152
1153 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001154 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001155 addrRetChain->generic.target = (LIR *) retChainingCell;
1156
1157 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001158 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001159 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1160
buzbee18fba342011-01-19 15:31:15 -08001161 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1162 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
1163 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07001164
1165 /* return through lr - jump to the chaining cell */
1166 genUnconditionalBranch(cUnit, predChainingCell);
1167
1168 /*
1169 * null-check on "this" may have been eliminated, but we still need a PC-
1170 * reconstruction label for stack overflow bailout.
1171 */
1172 if (pcrLabel == NULL) {
1173 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001174 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001175 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001176 pcrLabel->operands[0] = dPC;
1177 pcrLabel->operands[1] = mir->offset;
1178 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07001179 dvmInsertGrowableList(&cUnit->pcReconstructionList,
1180 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07001181 }
1182
1183 /* return through lr+2 - punt to the interpreter */
1184 genUnconditionalBranch(cUnit, pcrLabel);
1185
1186 /*
1187 * return through lr+4 - fully resolve the callee method.
1188 * r1 <- count
1189 * r2 <- &predictedChainCell
1190 * r3 <- this->class
1191 * r4 <- dPC
1192 * r7 <- this->class->vtable
1193 */
1194
1195 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001196 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001197
1198 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07001199 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001200
Ben Chengaf5aa1f2011-01-04 15:37:04 -08001201 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07001202
Ben Chengb88ec3c2010-05-17 12:50:33 -07001203 genRegCopy(cUnit, r1, rGLUE);
1204
Ben Cheng38329f52009-07-07 14:19:20 -07001205 /*
1206 * r0 = calleeMethod
1207 * r2 = &predictedChainingCell
1208 * r3 = class
1209 *
1210 * &returnChainingCell has been loaded into r1 but is not needed
1211 * when patching the chaining cell and will be clobbered upon
1212 * returning so it will be reconstructed again.
1213 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001214 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001215
1216 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001217 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001218 addrRetChain->generic.target = (LIR *) retChainingCell;
1219
1220 bypassRechaining->generic.target = (LIR *) addrRetChain;
1221 /*
1222 * r0 = calleeMethod,
1223 * r1 = &ChainingCell,
1224 * r4PC = callsiteDPC,
1225 */
buzbee18fba342011-01-19 15:31:15 -08001226 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
1227 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
1228 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001229#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001230 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001231#endif
1232 /* Handle exceptions using the interpreter */
1233 genTrap(cUnit, mir->offset, pcrLabel);
1234}
1235
Ben Chengba4fc8b2009-06-01 13:00:29 -07001236/* Geneate a branch to go back to the interpreter */
1237static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1238{
1239 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001240 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001241 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001242 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1243 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001244 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001245}
1246
1247/*
1248 * Attempt to single step one instruction using the interpreter and return
1249 * to the compiled code for the next Dalvik instruction
1250 */
1251static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1252{
Dan Bornsteine4852762010-12-02 12:45:00 -08001253 int flags = dexGetFlagsFromOpcode(mir->dalvikInsn.opcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001254 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1255 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001256
Bill Buzbee45273872010-03-11 11:12:15 -08001257 //If already optimized out, just ignore
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001258 if (mir->dalvikInsn.opcode == OP_NOP)
Bill Buzbee45273872010-03-11 11:12:15 -08001259 return;
1260
Bill Buzbee1465db52009-09-23 17:17:35 -07001261 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001262 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001263
Ben Chengba4fc8b2009-06-01 13:00:29 -07001264 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1265 genPuntToInterp(cUnit, mir->offset);
1266 return;
1267 }
1268 int entryAddr = offsetof(InterpState,
1269 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001270 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001271 /* r0 = dalvik pc */
1272 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1273 /* r1 = dalvik pc of following instruction */
1274 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001275 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001276}
1277
Carl Shapiro01605d22011-02-01 11:32:44 -08001278#if defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001279/*
1280 * To prevent a thread in a monitor wait from blocking the Jit from
1281 * resetting the code cache, heavyweight monitor lock will not
1282 * be allowed to return to an existing translation. Instead, we will
1283 * handle them by branching to a handler, which will in turn call the
1284 * runtime lock routine and then branch directly back to the
1285 * interpreter main loop. Given the high cost of the heavyweight
1286 * lock operation, this additional cost should be slight (especially when
1287 * considering that we expect the vast majority of lock operations to
1288 * use the fast-path thin lock bypass).
1289 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001290static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001291{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001292 bool isEnter = (mir->dalvikInsn.opcode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001293 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001294 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1295 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001296 loadValueDirectFixed(cUnit, rlSrc, r1);
1297 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001298 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001299 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001300 /* Get dPC of next insn */
1301 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001302 dexGetWidthFromOpcode(OP_MONITOR_ENTER)));
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001303 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001304 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001305 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001306 /* Do the call */
1307 opReg(cUnit, kOpBlx, r2);
buzbee8f8109a2010-08-31 10:16:35 -07001308 /* Did we throw? */
1309 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001310 loadConstant(cUnit, r0,
1311 (int) (cUnit->method->insns + mir->offset +
Dan Bornsteine4852762010-12-02 12:45:00 -08001312 dexGetWidthFromOpcode(OP_MONITOR_EXIT)));
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001313 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1314 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1315 target->defMask = ENCODE_ALL;
1316 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001317 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001318 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001319}
Ben Chengfc075c22010-05-28 15:20:08 -07001320#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001321
Ben Chengba4fc8b2009-06-01 13:00:29 -07001322/*
1323 * The following are the first-level codegen routines that analyze the format
1324 * of each bytecode then either dispatch special purpose codegen routines
1325 * or produce corresponding Thumb instructions directly.
1326 */
1327
1328static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001329 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001330{
1331 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1332 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1333 return false;
1334}
1335
1336static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1337{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001338 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1339 if ((dalvikOpcode >= OP_UNUSED_3E) && (dalvikOpcode <= OP_UNUSED_43)) {
1340 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001341 return true;
1342 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001343 switch (dalvikOpcode) {
Andy McFadden291758c2010-09-10 08:04:52 -07001344 case OP_RETURN_VOID_BARRIER:
buzbee2ce33c92010-11-01 15:53:27 -07001345 dvmCompilerGenMemBarrier(cUnit, kST);
1346 // Intentional fallthrough
1347 case OP_RETURN_VOID:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001348 genReturnCommon(cUnit,mir);
1349 break;
1350 case OP_UNUSED_73:
1351 case OP_UNUSED_79:
1352 case OP_UNUSED_7A:
Dan Bornstein90f15432010-12-02 16:46:25 -08001353 case OP_DISPATCH_FF:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001354 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpcode);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001355 return true;
1356 case OP_NOP:
1357 break;
1358 default:
1359 return true;
1360 }
1361 return false;
1362}
1363
1364static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1365{
Bill Buzbee1465db52009-09-23 17:17:35 -07001366 RegLocation rlDest;
1367 RegLocation rlResult;
1368 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001369 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001370 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001371 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001372 }
Ben Chenge9695e52009-06-16 16:11:47 -07001373
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001374 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001375 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001376 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001377 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001378 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001379 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001380 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001381 }
1382 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001383 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001384 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001385 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001386 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001387 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1388 rlResult.lowReg, 31);
1389 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001390 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001391 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001392 default:
1393 return true;
1394 }
1395 return false;
1396}
1397
1398static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1399{
Bill Buzbee1465db52009-09-23 17:17:35 -07001400 RegLocation rlDest;
1401 RegLocation rlResult;
1402 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001403 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001404 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001405 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001406 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001407 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001408
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001409 switch (mir->dalvikInsn.opcode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001410 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001411 loadConstantNoClobber(cUnit, rlResult.lowReg,
1412 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001413 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001414 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001415 }
1416 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001417 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1418 0, mir->dalvikInsn.vB << 16);
1419 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001420 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001421 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001422 default:
1423 return true;
1424 }
1425 return false;
1426}
1427
jeffhao71eee1f2011-01-04 14:18:54 -08001428static bool handleFmt20bc_Fmt40sc(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001429{
jeffhao71eee1f2011-01-04 14:18:54 -08001430 /* For OP_THROW_VERIFICATION_ERROR & OP_THROW_VERIFICATION_ERROR_JUMBO */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001431 genInterpSingleStep(cUnit, mir);
1432 return false;
1433}
1434
jeffhao71eee1f2011-01-04 14:18:54 -08001435static bool handleFmt21c_Fmt31c_Fmt41c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001436{
Bill Buzbee1465db52009-09-23 17:17:35 -07001437 RegLocation rlResult;
1438 RegLocation rlDest;
1439 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001440
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001441 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001442 case OP_CONST_STRING_JUMBO:
1443 case OP_CONST_STRING: {
1444 void *strPtr = (void*)
1445 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001446
1447 if (strPtr == NULL) {
1448 LOGE("Unexpected null string");
1449 dvmAbort();
1450 }
1451
Bill Buzbeec6f10662010-02-09 11:16:15 -08001452 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1453 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001454 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001455 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001456 break;
1457 }
jeffhao71eee1f2011-01-04 14:18:54 -08001458 case OP_CONST_CLASS:
1459 case OP_CONST_CLASS_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001460 void *classPtr = (void*)
1461 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001462
1463 if (classPtr == NULL) {
1464 LOGE("Unexpected null class");
1465 dvmAbort();
1466 }
1467
Bill Buzbeec6f10662010-02-09 11:16:15 -08001468 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1469 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001470 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001471 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001472 break;
1473 }
jeffhao71eee1f2011-01-04 14:18:54 -08001474 case OP_SGET:
buzbeeecf8f6e2010-07-20 14:53:42 -07001475 case OP_SGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001476 case OP_SGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001477 case OP_SGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08001478 case OP_SGET_OBJECT_VOLATILE:
1479 case OP_SGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001480 case OP_SGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001481 case OP_SGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001482 case OP_SGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001483 case OP_SGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001484 case OP_SGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001485 case OP_SGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001486 case OP_SGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001487 case OP_SGET_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001488 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001489 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001490 bool isVolatile;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001491 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1492 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001493 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001494 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001495
1496 if (fieldPtr == NULL) {
1497 LOGE("Unexpected null static field");
1498 dvmAbort();
1499 }
1500
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001501 isVolatile = (mir->dalvikInsn.opcode == OP_SGET_VOLATILE) ||
1502 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001503 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001504
Bill Buzbeec6f10662010-02-09 11:16:15 -08001505 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1506 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001507 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001508
buzbeeecf8f6e2010-07-20 14:53:42 -07001509 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001510 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001511 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001512 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001513 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001514 HEAP_ACCESS_SHADOW(false);
1515
Bill Buzbee1465db52009-09-23 17:17:35 -07001516 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001517 break;
1518 }
jeffhao71eee1f2011-01-04 14:18:54 -08001519 case OP_SGET_WIDE:
1520 case OP_SGET_WIDE_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001521 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001522 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1523 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001524 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001525 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001526
1527 if (fieldPtr == NULL) {
1528 LOGE("Unexpected null static field");
1529 dvmAbort();
1530 }
1531
Bill Buzbeec6f10662010-02-09 11:16:15 -08001532 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001533 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1534 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001535 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001536
1537 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001538 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001539 HEAP_ACCESS_SHADOW(false);
1540
Bill Buzbee1465db52009-09-23 17:17:35 -07001541 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001542 break;
1543 }
jeffhao71eee1f2011-01-04 14:18:54 -08001544 case OP_SPUT:
1545 case OP_SPUT_VOLATILE:
1546 case OP_SPUT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001547 case OP_SPUT_OBJECT:
buzbeeddc7d292010-09-02 17:16:24 -07001548 case OP_SPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08001549 case OP_SPUT_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001550 case OP_SPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08001551 case OP_SPUT_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001552 case OP_SPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08001553 case OP_SPUT_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001554 case OP_SPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08001555 case OP_SPUT_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001556 case OP_SPUT_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08001557 case OP_SPUT_SHORT_JUMBO: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001558 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001559 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeed3b0a4b2010-09-27 11:30:22 -07001560 int objHead;
buzbeeecf8f6e2010-07-20 14:53:42 -07001561 bool isVolatile;
buzbeed3b0a4b2010-09-27 11:30:22 -07001562 bool isSputObject;
Ben Cheng7a2697d2010-06-07 13:44:23 -07001563 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1564 mir->meta.calleeMethod : cUnit->method;
1565 void *fieldPtr = (void*)
1566 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001567
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001568 isVolatile = (mir->dalvikInsn.opcode == OP_SPUT_VOLATILE) ||
1569 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE) ||
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001570 dvmIsVolatileField((Field *) fieldPtr);
buzbeeecf8f6e2010-07-20 14:53:42 -07001571
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001572 isSputObject = (mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
jeffhao71eee1f2011-01-04 14:18:54 -08001573 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_JUMBO) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001574 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE);
buzbeed3b0a4b2010-09-27 11:30:22 -07001575
Ben Chengdd6e8702010-05-07 13:05:47 -07001576 if (fieldPtr == NULL) {
1577 LOGE("Unexpected null static field");
1578 dvmAbort();
1579 }
1580
Bill Buzbeec6f10662010-02-09 11:16:15 -08001581 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001582 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbeeb78c76f2010-09-30 19:08:20 -07001583 loadConstant(cUnit, tReg, (int) fieldPtr);
buzbeed3b0a4b2010-09-27 11:30:22 -07001584 if (isSputObject) {
1585 objHead = dvmCompilerAllocTemp(cUnit);
buzbeeb78c76f2010-09-30 19:08:20 -07001586 loadWordDisp(cUnit, tReg, offsetof(Field, clazz), objHead);
buzbeed3b0a4b2010-09-27 11:30:22 -07001587 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001588 HEAP_ACCESS_SHADOW(true);
buzbeeb78c76f2010-09-30 19:08:20 -07001589 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
buzbeed3b0a4b2010-09-27 11:30:22 -07001590 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001591 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001592 if (isVolatile) {
buzbee2ce33c92010-11-01 15:53:27 -07001593 dvmCompilerGenMemBarrier(cUnit, kSY);
buzbeeecf8f6e2010-07-20 14:53:42 -07001594 }
buzbeed3b0a4b2010-09-27 11:30:22 -07001595 if (isSputObject) {
buzbeeb78c76f2010-09-30 19:08:20 -07001596 /* NOTE: marking card based sfield->clazz */
buzbeed3b0a4b2010-09-27 11:30:22 -07001597 markCard(cUnit, rlSrc.lowReg, objHead);
1598 dvmCompilerFreeTemp(cUnit, objHead);
buzbee919eb062010-07-12 12:59:22 -07001599 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001600
Ben Chengba4fc8b2009-06-01 13:00:29 -07001601 break;
1602 }
jeffhao71eee1f2011-01-04 14:18:54 -08001603 case OP_SPUT_WIDE:
1604 case OP_SPUT_WIDE_JUMBO: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001605 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001606 int valOffset = offsetof(StaticField, value);
Ben Cheng7a2697d2010-06-07 13:44:23 -07001607 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
1608 mir->meta.calleeMethod : cUnit->method;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001609 void *fieldPtr = (void*)
Ben Cheng7a2697d2010-06-07 13:44:23 -07001610 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001611
Ben Chengdd6e8702010-05-07 13:05:47 -07001612 if (fieldPtr == NULL) {
1613 LOGE("Unexpected null static field");
1614 dvmAbort();
1615 }
1616
Bill Buzbeec6f10662010-02-09 11:16:15 -08001617 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001618 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1619 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001620
1621 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001622 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001623 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001624 break;
1625 }
jeffhao71eee1f2011-01-04 14:18:54 -08001626 case OP_NEW_INSTANCE:
1627 case OP_NEW_INSTANCE_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001628 /*
1629 * Obey the calling convention and don't mess with the register
1630 * usage.
1631 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08001632 ClassObject *classPtr = (ClassObject *)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001633 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001634
1635 if (classPtr == NULL) {
1636 LOGE("Unexpected null class");
1637 dvmAbort();
1638 }
1639
Ben Cheng79d173c2009-09-29 16:12:51 -07001640 /*
1641 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001642 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001643 */
1644 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001645 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001646 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001647 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001648 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001649 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001650 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001651 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001652 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07001653 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07001654 /*
1655 * OOM exception needs to be thrown here and cannot re-execute
1656 */
1657 loadConstant(cUnit, r0,
1658 (int) (cUnit->method->insns + mir->offset));
1659 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1660 /* noreturn */
1661
Bill Buzbee1465db52009-09-23 17:17:35 -07001662 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001663 target->defMask = ENCODE_ALL;
1664 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001665 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1666 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001667 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001668 break;
1669 }
jeffhao71eee1f2011-01-04 14:18:54 -08001670 case OP_CHECK_CAST:
1671 case OP_CHECK_CAST_JUMBO: {
Ben Chenge9695e52009-06-16 16:11:47 -07001672 /*
1673 * Obey the calling convention and don't mess with the register
1674 * usage.
1675 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001676 ClassObject *classPtr =
1677 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001678 /*
1679 * Note: It is possible that classPtr is NULL at this point,
1680 * even though this instruction has been successfully interpreted.
1681 * If the previous interpretation had a null source, the
1682 * interpreter would not have bothered to resolve the clazz.
1683 * Bail out to the interpreter in this case, and log it
1684 * so that we can tell if it happens frequently.
1685 */
1686 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001687 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001688 genInterpSingleStep(cUnit, mir);
1689 return false;
1690 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001691 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001692 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001693 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001694 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee8f8109a2010-08-31 10:16:35 -07001695 /* Null? */
1696 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq,
1697 rlSrc.lowReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001698 /*
1699 * rlSrc.lowReg now contains object->clazz. Note that
1700 * it could have been allocated r0, but we're okay so long
1701 * as we don't do anything desctructive until r0 is loaded
1702 * with clazz.
1703 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001704 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001705 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001706 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001707 opRegReg(cUnit, kOpCmp, r0, r1);
1708 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1709 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001710 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001711 /*
1712 * If null, check cast failed - punt to the interpreter. Because
1713 * interpreter will be the one throwing, we don't need to
1714 * genExportPC() here.
1715 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001716 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001717 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001718 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001719 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001720 branch1->generic.target = (LIR *)target;
1721 branch2->generic.target = (LIR *)target;
1722 break;
1723 }
buzbee4d92e682010-07-29 15:24:14 -07001724 case OP_SGET_WIDE_VOLATILE:
1725 case OP_SPUT_WIDE_VOLATILE:
1726 genInterpSingleStep(cUnit, mir);
1727 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001728 default:
1729 return true;
1730 }
1731 return false;
1732}
1733
Ben Cheng7a2697d2010-06-07 13:44:23 -07001734/*
1735 * A typical example of inlined getter/setter from a monomorphic callsite:
1736 *
1737 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ invoke-static (I)
1738 * D/dalvikvm( 289): -------- dalvik offset: 0x0000 @ sget-object (C) v0, ...
1739 * D/dalvikvm( 289): 0x4427fc22 (0002): ldr r0, [pc, #56]
1740 * D/dalvikvm( 289): 0x4427fc24 (0004): ldr r1, [r0, #0]
1741 * D/dalvikvm( 289): 0x4427fc26 (0006): str r1, [r5, #0]
1742 * D/dalvikvm( 289): 0x4427fc28 (0008): .align4
1743 * D/dalvikvm( 289): L0x0003:
1744 * D/dalvikvm( 289): -------- dalvik offset: 0x0003 @ move-result-object (I) v0
1745 *
1746 * Note the invoke-static and move-result-object with the (I) notation are
1747 * turned into no-op.
1748 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001749static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1750{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001751 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001752 RegLocation rlResult;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001753 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001754 case OP_MOVE_EXCEPTION: {
1755 int offset = offsetof(InterpState, self);
1756 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001757 int selfReg = dvmCompilerAllocTemp(cUnit);
1758 int resetReg = dvmCompilerAllocTemp(cUnit);
1759 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1760 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001761 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001762 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001763 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001764 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001765 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001766 break;
1767 }
1768 case OP_MOVE_RESULT:
1769 case OP_MOVE_RESULT_OBJECT: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001770 /* An inlined move result is effectively no-op */
1771 if (mir->OptimizationFlags & MIR_INLINED)
1772 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001773 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001774 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1775 rlSrc.fp = rlDest.fp;
1776 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001777 break;
1778 }
1779 case OP_MOVE_RESULT_WIDE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07001780 /* An inlined move result is effectively no-op */
1781 if (mir->OptimizationFlags & MIR_INLINED)
1782 break;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001783 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001784 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1785 rlSrc.fp = rlDest.fp;
1786 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001787 break;
1788 }
1789 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001790 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1792 rlDest.fp = rlSrc.fp;
1793 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001794 genReturnCommon(cUnit,mir);
1795 break;
1796 }
1797 case OP_RETURN:
1798 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001799 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001800 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1801 rlDest.fp = rlSrc.fp;
1802 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001803 genReturnCommon(cUnit,mir);
1804 break;
1805 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001806 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001807 case OP_MONITOR_ENTER:
Ben Cheng5d90c202009-11-22 23:31:11 -08001808 genMonitor(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001809 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001810 case OP_THROW: {
1811 genInterpSingleStep(cUnit, mir);
1812 break;
1813 }
1814 default:
1815 return true;
1816 }
1817 return false;
1818}
1819
Bill Buzbeed45ba372009-06-15 17:00:57 -07001820static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1821{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001822 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 RegLocation rlDest;
1824 RegLocation rlSrc;
1825 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001826
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001827 if ( (opcode >= OP_ADD_INT_2ADDR) && (opcode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001828 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001829 }
1830
Bill Buzbee1465db52009-09-23 17:17:35 -07001831 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001832 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001833 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001834 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001835 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001836 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001837 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001838 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001839
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001840 switch (opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001841 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001842 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001844 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001845 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001846 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001847 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001849 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001850 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001851 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001852 case OP_NEG_INT:
1853 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001854 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 case OP_NEG_LONG:
1856 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001857 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001859 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001860 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001861 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001862 case OP_MOVE_WIDE:
1863 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001865 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001866 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1867 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001868 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001869 if (rlSrc.location == kLocPhysReg) {
1870 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1871 } else {
1872 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1873 }
1874 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1875 rlResult.lowReg, 31);
1876 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001878 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001879 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1880 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001881 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001882 case OP_MOVE:
1883 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001884 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001885 break;
1886 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001888 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001889 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1890 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001891 break;
1892 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001893 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001894 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001895 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1896 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001897 break;
1898 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001899 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001900 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001901 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1902 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001903 break;
1904 case OP_ARRAY_LENGTH: {
1905 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001906 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1907 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1908 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001909 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001910 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1911 rlResult.lowReg);
1912 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001913 break;
1914 }
1915 default:
1916 return true;
1917 }
1918 return false;
1919}
1920
1921static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1922{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001923 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001924 RegLocation rlDest;
1925 RegLocation rlResult;
1926 int BBBB = mir->dalvikInsn.vB;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001927 if (dalvikOpcode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001928 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1929 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001930 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001931 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001932 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1933 storeValueWide(cUnit, rlDest, rlResult);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001934 } else if (dalvikOpcode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001935 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1936 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001937 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001938 storeValue(cUnit, rlDest, rlResult);
1939 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001940 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001941 return false;
1942}
1943
1944/* Compare agaist zero */
1945static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001946 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001947{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001948 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001949 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001950 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001951 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1952 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001953
Bill Buzbee270c1d62009-08-13 16:58:07 -07001954//TUNING: break this out to allow use of Thumb2 CB[N]Z
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001955 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001956 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001957 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001958 break;
1959 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001960 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001961 break;
1962 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001963 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001964 break;
1965 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001967 break;
1968 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001969 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001970 break;
1971 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001973 break;
1974 default:
1975 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08001976 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001977 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001978 }
1979 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1980 /* This mostly likely will be optimized away in a later phase */
1981 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1982 return false;
1983}
1984
Elliott Hughesb4c05972010-02-24 16:36:18 -08001985static bool isPowerOfTwo(int x)
1986{
1987 return (x & (x - 1)) == 0;
1988}
1989
1990// Returns true if no more than two bits are set in 'x'.
1991static bool isPopCountLE2(unsigned int x)
1992{
1993 x &= x - 1;
1994 return (x & (x - 1)) == 0;
1995}
1996
1997// Returns the index of the lowest set bit in 'x'.
1998static int lowestSetBit(unsigned int x) {
1999 int bit_posn = 0;
2000 while ((x & 0xf) == 0) {
2001 bit_posn += 4;
2002 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002003 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002004 while ((x & 1) == 0) {
2005 bit_posn++;
2006 x >>= 1;
2007 }
2008 return bit_posn;
2009}
2010
Elliott Hughes672511b2010-04-26 17:40:13 -07002011// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
2012// and store the result in 'rlDest'.
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002013static bool handleEasyDivide(CompilationUnit *cUnit, Opcode dalvikOpcode,
Elliott Hughes672511b2010-04-26 17:40:13 -07002014 RegLocation rlSrc, RegLocation rlDest, int lit)
2015{
2016 if (lit < 2 || !isPowerOfTwo(lit)) {
2017 return false;
2018 }
2019 int k = lowestSetBit(lit);
2020 if (k >= 30) {
2021 // Avoid special cases.
2022 return false;
2023 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002024 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 || dalvikOpcode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07002025 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2026 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07002027 if (div) {
2028 int tReg = dvmCompilerAllocTemp(cUnit);
2029 if (lit == 2) {
2030 // Division by 2 is by far the most common division by constant.
2031 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
2032 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2033 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2034 } else {
2035 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
2036 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2037 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2038 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
2039 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002040 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07002041 int cReg = dvmCompilerAllocTemp(cUnit);
2042 loadConstant(cUnit, cReg, lit - 1);
2043 int tReg1 = dvmCompilerAllocTemp(cUnit);
2044 int tReg2 = dvmCompilerAllocTemp(cUnit);
2045 if (lit == 2) {
2046 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2047 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2048 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2049 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2050 } else {
2051 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2052 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2053 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2054 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
2055 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2056 }
Elliott Hughes672511b2010-04-26 17:40:13 -07002057 }
2058 storeValue(cUnit, rlDest, rlResult);
2059 return true;
2060}
2061
Elliott Hughesb4c05972010-02-24 16:36:18 -08002062// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2063// and store the result in 'rlDest'.
2064static bool handleEasyMultiply(CompilationUnit *cUnit,
2065 RegLocation rlSrc, RegLocation rlDest, int lit)
2066{
2067 // Can we simplify this multiplication?
2068 bool powerOfTwo = false;
2069 bool popCountLE2 = false;
2070 bool powerOfTwoMinusOne = false;
2071 if (lit < 2) {
2072 // Avoid special cases.
2073 return false;
2074 } else if (isPowerOfTwo(lit)) {
2075 powerOfTwo = true;
2076 } else if (isPopCountLE2(lit)) {
2077 popCountLE2 = true;
2078 } else if (isPowerOfTwo(lit + 1)) {
2079 powerOfTwoMinusOne = true;
2080 } else {
2081 return false;
2082 }
2083 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2084 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2085 if (powerOfTwo) {
2086 // Shift.
2087 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2088 lowestSetBit(lit));
2089 } else if (popCountLE2) {
2090 // Shift and add and shift.
2091 int firstBit = lowestSetBit(lit);
2092 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2093 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2094 firstBit, secondBit);
2095 } else {
2096 // Reverse subtract: (src << (shift + 1)) - src.
2097 assert(powerOfTwoMinusOne);
2098 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2099 int tReg = dvmCompilerAllocTemp(cUnit);
2100 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2101 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2102 }
2103 storeValue(cUnit, rlDest, rlResult);
2104 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002105}
2106
Ben Chengba4fc8b2009-06-01 13:00:29 -07002107static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2108{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002109 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002110 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2111 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002112 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002113 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002114 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002115 int shiftOp = false;
2116 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002117
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002118 switch (dalvikOpcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002119 case OP_RSUB_INT_LIT8:
2120 case OP_RSUB_INT: {
2121 int tReg;
2122 //TUNING: add support for use of Arm rsub op
2123 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002124 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002125 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002126 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002127 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2128 tReg, rlSrc.lowReg);
2129 storeValue(cUnit, rlDest, rlResult);
2130 return false;
2131 break;
2132 }
2133
Ben Chengba4fc8b2009-06-01 13:00:29 -07002134 case OP_ADD_INT_LIT8:
2135 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002136 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002137 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002138 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002139 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002140 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2141 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002142 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002143 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002144 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002145 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 case OP_AND_INT_LIT8:
2147 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002148 op = kOpAnd;
2149 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002150 case OP_OR_INT_LIT8:
2151 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002152 op = kOpOr;
2153 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002154 case OP_XOR_INT_LIT8:
2155 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002156 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002157 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002158 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002159 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002160 shiftOp = true;
2161 op = kOpLsl;
2162 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002163 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002164 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 shiftOp = true;
2166 op = kOpAsr;
2167 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002168 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002169 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002170 shiftOp = true;
2171 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002172 break;
2173
2174 case OP_DIV_INT_LIT8:
2175 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002176 case OP_REM_INT_LIT8:
2177 case OP_REM_INT_LIT16:
2178 if (lit == 0) {
2179 /* Let the interpreter deal with div by 0 */
2180 genInterpSingleStep(cUnit, mir);
2181 return false;
2182 }
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002183 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002184 return false;
2185 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002186 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002188 dvmCompilerClobber(cUnit, r0);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002189 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2190 (dalvikOpcode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002191 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002192 isDiv = true;
2193 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002194 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002195 isDiv = false;
2196 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002197 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002198 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002199 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002200 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002201 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002203 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002204 storeValue(cUnit, rlDest, rlResult);
2205 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002206 break;
2207 default:
2208 return true;
2209 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002210 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002211 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002212 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2213 if (shiftOp && (lit == 0)) {
2214 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2215 } else {
2216 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2217 }
2218 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002219 return false;
2220}
2221
jeffhao71eee1f2011-01-04 14:18:54 -08002222static bool handleFmt22c_Fmt52c(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002223{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002224 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
buzbee4d92e682010-07-29 15:24:14 -07002225 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002226 bool isVolatile = false;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002227 switch (dalvikOpcode) {
buzbee4d92e682010-07-29 15:24:14 -07002228 /*
2229 * Wide volatiles currently handled via single step.
2230 * Add them here if generating in-line code.
2231 * case OP_IGET_WIDE_VOLATILE:
2232 * case OP_IPUT_WIDE_VOLATILE:
2233 */
2234 case OP_IGET:
2235 case OP_IGET_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002236 case OP_IGET_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002237 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002238 case OP_IGET_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002239 case OP_IGET_OBJECT:
2240 case OP_IGET_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002241 case OP_IGET_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002242 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002243 case OP_IGET_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002244 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002245 case OP_IGET_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002246 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002247 case OP_IGET_CHAR_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002248 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002249 case OP_IGET_SHORT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002250 case OP_IPUT:
2251 case OP_IPUT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002252 case OP_IPUT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002253 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002254 case OP_IPUT_WIDE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002255 case OP_IPUT_OBJECT:
2256 case OP_IPUT_OBJECT_VOLATILE:
jeffhao71eee1f2011-01-04 14:18:54 -08002257 case OP_IPUT_OBJECT_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002258 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002259 case OP_IPUT_BOOLEAN_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002260 case OP_IPUT_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002261 case OP_IPUT_BYTE_JUMBO:
buzbee4d92e682010-07-29 15:24:14 -07002262 case OP_IPUT_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002263 case OP_IPUT_CHAR_JUMBO:
2264 case OP_IPUT_SHORT:
2265 case OP_IPUT_SHORT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002266 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
2267 mir->meta.calleeMethod : cUnit->method;
buzbee4d92e682010-07-29 15:24:14 -07002268 Field *fieldPtr =
Ben Cheng7a2697d2010-06-07 13:44:23 -07002269 method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002270
buzbee4d92e682010-07-29 15:24:14 -07002271 if (fieldPtr == NULL) {
2272 LOGE("Unexpected null instance field");
2273 dvmAbort();
2274 }
2275 isVolatile = dvmIsVolatileField(fieldPtr);
2276 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2277 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002278 }
buzbee4d92e682010-07-29 15:24:14 -07002279 default:
2280 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281 }
buzbee4d92e682010-07-29 15:24:14 -07002282
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002283 switch (dalvikOpcode) {
jeffhao71eee1f2011-01-04 14:18:54 -08002284 case OP_NEW_ARRAY:
2285 case OP_NEW_ARRAY_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002286 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002287 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2288 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002289 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290 void *classPtr = (void*)
2291 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002292
2293 if (classPtr == NULL) {
2294 LOGE("Unexpected null class");
2295 dvmAbort();
2296 }
2297
Bill Buzbeec6f10662010-02-09 11:16:15 -08002298 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002299 genExportPC(cUnit, mir);
2300 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002302 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002303 /*
2304 * "len < 0": bail to the interpreter to re-execute the
2305 * instruction
2306 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002307 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002308 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002309 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002310 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002311 /* generate a branch over if allocation is successful */
buzbee8f8109a2010-08-31 10:16:35 -07002312 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng4f489172009-09-27 17:08:35 -07002313 /*
2314 * OOM exception needs to be thrown here and cannot re-execute
2315 */
2316 loadConstant(cUnit, r0,
2317 (int) (cUnit->method->insns + mir->offset));
2318 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2319 /* noreturn */
2320
Bill Buzbee1465db52009-09-23 17:17:35 -07002321 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002322 target->defMask = ENCODE_ALL;
2323 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002324 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002325 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002326 break;
2327 }
jeffhao71eee1f2011-01-04 14:18:54 -08002328 case OP_INSTANCE_OF:
2329 case OP_INSTANCE_OF_JUMBO: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002331 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2332 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002334 ClassObject *classPtr =
2335 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002336 /*
2337 * Note: It is possible that classPtr is NULL at this point,
2338 * even though this instruction has been successfully interpreted.
2339 * If the previous interpretation had a null source, the
2340 * interpreter would not have bothered to resolve the clazz.
2341 * Bail out to the interpreter in this case, and log it
2342 * so that we can tell if it happens frequently.
2343 */
2344 if (classPtr == NULL) {
2345 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2346 genInterpSingleStep(cUnit, mir);
2347 break;
2348 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002349 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002350 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002351 loadConstant(cUnit, r2, (int) classPtr );
Ben Cheng752c7942009-06-22 10:50:07 -07002352 /* When taken r0 has NULL which can be used for store directly */
buzbee8f8109a2010-08-31 10:16:35 -07002353 ArmLIR *branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002354 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002355 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002356 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002357 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002358 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002359 opRegReg(cUnit, kOpCmp, r1, r2);
2360 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2361 genRegCopy(cUnit, r0, r1);
2362 genRegCopy(cUnit, r1, r2);
2363 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002364 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002365 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002366 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002367 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002368 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002369 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002370 branch1->generic.target = (LIR *)target;
2371 branch2->generic.target = (LIR *)target;
2372 break;
2373 }
2374 case OP_IGET_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002375 case OP_IGET_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002376 genIGetWide(cUnit, mir, fieldOffset);
2377 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002378 case OP_IGET_VOLATILE:
2379 case OP_IGET_OBJECT_VOLATILE:
2380 isVolatile = true;
2381 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002382 case OP_IGET:
jeffhao71eee1f2011-01-04 14:18:54 -08002383 case OP_IGET_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002384 case OP_IGET_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002385 case OP_IGET_OBJECT_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002386 case OP_IGET_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002387 case OP_IGET_BOOLEAN_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002388 case OP_IGET_BYTE:
jeffhao71eee1f2011-01-04 14:18:54 -08002389 case OP_IGET_BYTE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002390 case OP_IGET_CHAR:
jeffhao71eee1f2011-01-04 14:18:54 -08002391 case OP_IGET_CHAR_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002392 case OP_IGET_SHORT:
jeffhao71eee1f2011-01-04 14:18:54 -08002393 case OP_IGET_SHORT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002394 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 break;
2396 case OP_IPUT_WIDE:
jeffhao71eee1f2011-01-04 14:18:54 -08002397 case OP_IPUT_WIDE_JUMBO:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002398 genIPutWide(cUnit, mir, fieldOffset);
2399 break;
2400 case OP_IPUT:
jeffhao71eee1f2011-01-04 14:18:54 -08002401 case OP_IPUT_JUMBO:
buzbee3272e2f2010-09-09 14:07:01 -07002402 case OP_IPUT_BOOLEAN:
jeffhao71eee1f2011-01-04 14:18:54 -08002403 case OP_IPUT_BOOLEAN_JUMBO:
2404 case OP_IPUT_BYTE:
2405 case OP_IPUT_BYTE_JUMBO:
2406 case OP_IPUT_CHAR:
2407 case OP_IPUT_CHAR_JUMBO:
2408 case OP_IPUT_SHORT:
2409 case OP_IPUT_SHORT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002410 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002411 break;
buzbee4d92e682010-07-29 15:24:14 -07002412 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002413 case OP_IPUT_OBJECT_VOLATILE:
2414 isVolatile = true;
2415 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002416 case OP_IPUT_OBJECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002417 case OP_IPUT_OBJECT_JUMBO:
buzbeeecf8f6e2010-07-20 14:53:42 -07002418 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002419 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002420 case OP_IGET_WIDE_VOLATILE:
2421 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002422 genInterpSingleStep(cUnit, mir);
2423 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002424 default:
2425 return true;
2426 }
2427 return false;
2428}
2429
2430static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2431{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002432 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002433 int fieldOffset = mir->dalvikInsn.vC;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002434 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002435 case OP_IGET_QUICK:
2436 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002437 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002438 break;
2439 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002440 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002441 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002442 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002443 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002444 break;
2445 case OP_IGET_WIDE_QUICK:
2446 genIGetWide(cUnit, mir, fieldOffset);
2447 break;
2448 case OP_IPUT_WIDE_QUICK:
2449 genIPutWide(cUnit, mir, fieldOffset);
2450 break;
2451 default:
2452 return true;
2453 }
2454 return false;
2455
2456}
2457
2458/* Compare agaist zero */
2459static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002460 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002461{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002462 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002463 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002464 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2465 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002466
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2468 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2469 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002470
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002471 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002472 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002473 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002474 break;
2475 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002476 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002477 break;
2478 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002479 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002480 break;
2481 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002482 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 break;
2484 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002485 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002486 break;
2487 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002488 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002489 break;
2490 default:
2491 cond = 0;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002492 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002493 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002494 }
2495 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2496 /* This mostly likely will be optimized away in a later phase */
2497 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2498 return false;
2499}
2500
2501static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2502{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002503 Opcode opcode = mir->dalvikInsn.opcode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002504
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002505 switch (opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002506 case OP_MOVE_16:
2507 case OP_MOVE_OBJECT_16:
2508 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002509 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002510 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2511 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002512 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002513 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002515 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002516 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2517 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002518 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002519 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002520 default:
2521 return true;
2522 }
2523 return false;
2524}
2525
2526static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2527{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002528 Opcode opcode = mir->dalvikInsn.opcode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002529 RegLocation rlSrc1;
2530 RegLocation rlSrc2;
2531 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002532
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002533 if ( (opcode >= OP_ADD_INT) && (opcode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002534 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002535 }
2536
Bill Buzbee1465db52009-09-23 17:17:35 -07002537 /* APUTs have 3 sources and no targets */
2538 if (mir->ssaRep->numDefs == 0) {
2539 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002540 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2541 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2542 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002543 } else {
2544 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002545 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2546 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2547 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002548 }
2549 } else {
2550 /* Two sources and 1 dest. Deduce the operand sizes */
2551 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002552 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2553 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002554 } else {
2555 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002556 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2557 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002558 }
2559 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002560 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002561 } else {
2562 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002563 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002564 }
2565 }
2566
2567
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002568 switch (opcode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002569 case OP_CMPL_FLOAT:
2570 case OP_CMPG_FLOAT:
2571 case OP_CMPL_DOUBLE:
2572 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002573 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002574 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002575 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002576 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002577 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002578 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002579 break;
2580 case OP_AGET:
2581 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002582 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002583 break;
2584 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002585 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002586 break;
2587 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002588 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002589 break;
2590 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002591 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002592 break;
2593 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002594 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002595 break;
2596 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002597 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598 break;
2599 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002600 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002601 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002602 case OP_APUT_OBJECT:
2603 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2604 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002605 case OP_APUT_SHORT:
2606 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002607 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002608 break;
2609 case OP_APUT_BYTE:
2610 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002611 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002612 break;
2613 default:
2614 return true;
2615 }
2616 return false;
2617}
2618
Ben Cheng6c10a972009-10-29 14:39:18 -07002619/*
2620 * Find the matching case.
2621 *
2622 * return values:
2623 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2624 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2625 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2626 * above MAX_CHAINED_SWITCH_CASES).
2627 *
2628 * Instructions around the call are:
2629 *
2630 * mov r2, pc
2631 * blx &findPackedSwitchIndex
2632 * mov pc, r0
2633 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002634 * chaining cell for case 0 [12 bytes]
2635 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002636 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002637 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002638 * chaining cell for case default [8 bytes]
2639 * noChain exit
2640 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002641static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002642{
2643 int size;
2644 int firstKey;
2645 const int *entries;
2646 int index;
2647 int jumpIndex;
2648 int caseDPCOffset = 0;
2649 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2650 int chainingPC = (pc + 4) & ~3;
2651
2652 /*
2653 * Packed switch data format:
2654 * ushort ident = 0x0100 magic value
2655 * ushort size number of entries in the table
2656 * int first_key first (and lowest) switch case value
2657 * int targets[size] branch targets, relative to switch opcode
2658 *
2659 * Total size is (4+size*2) 16-bit code units.
2660 */
2661 size = switchData[1];
2662 assert(size > 0);
2663
2664 firstKey = switchData[2];
2665 firstKey |= switchData[3] << 16;
2666
2667
2668 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2669 * we can treat them as a native int array.
2670 */
2671 entries = (const int*) &switchData[4];
2672 assert(((u4)entries & 0x3) == 0);
2673
2674 index = testVal - firstKey;
2675
2676 /* Jump to the default cell */
2677 if (index < 0 || index >= size) {
2678 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2679 /* Jump to the non-chaining exit point */
2680 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2681 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2682 caseDPCOffset = entries[index];
2683 /* Jump to the inline chaining cell */
2684 } else {
2685 jumpIndex = index;
2686 }
2687
Bill Buzbeebd047242010-05-13 13:02:53 -07002688 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002689 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2690}
2691
2692/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002693static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002694{
2695 int size;
2696 const int *keys;
2697 const int *entries;
2698 int chainingPC = (pc + 4) & ~3;
2699 int i;
2700
2701 /*
2702 * Sparse switch data format:
2703 * ushort ident = 0x0200 magic value
2704 * ushort size number of entries in the table; > 0
2705 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2706 * int targets[size] branch targets, relative to switch opcode
2707 *
2708 * Total size is (2+size*4) 16-bit code units.
2709 */
2710
2711 size = switchData[1];
2712 assert(size > 0);
2713
2714 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2715 * we can treat them as a native int array.
2716 */
2717 keys = (const int*) &switchData[2];
2718 assert(((u4)keys & 0x3) == 0);
2719
2720 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2721 * we can treat them as a native int array.
2722 */
2723 entries = keys + size;
2724 assert(((u4)entries & 0x3) == 0);
2725
2726 /*
2727 * Run through the list of keys, which are guaranteed to
2728 * be sorted low-to-high.
2729 *
2730 * Most tables have 3-4 entries. Few have more than 10. A binary
2731 * search here is probably not useful.
2732 */
2733 for (i = 0; i < size; i++) {
2734 int k = keys[i];
2735 if (k == testVal) {
2736 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2737 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2738 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002739 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002740 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2741 } else if (k > testVal) {
2742 break;
2743 }
2744 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002745 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2746 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002747}
2748
Ben Chengba4fc8b2009-06-01 13:00:29 -07002749static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2750{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002751 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2752 switch (dalvikOpcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002753 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002754 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002755 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002756 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002757 genExportPC(cUnit, mir);
2758 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002759 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002760 loadConstant(cUnit, r1,
2761 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002762 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002763 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002764 /* generate a branch over if successful */
buzbee8f8109a2010-08-31 10:16:35 -07002765 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002766 loadConstant(cUnit, r0,
2767 (int) (cUnit->method->insns + mir->offset));
2768 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2769 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2770 target->defMask = ENCODE_ALL;
2771 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002772 break;
2773 }
2774 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002775 * Compute the goto target of up to
2776 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2777 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002778 */
2779 case OP_PACKED_SWITCH:
2780 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002781 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2782 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002783 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002784 dvmCompilerLockAllTemps(cUnit);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002785 if (dalvikOpcode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002786 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002787 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002788 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002789 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002790 /* r0 <- Addr of the switch data */
2791 loadConstant(cUnit, r0,
2792 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2793 /* r2 <- pc of the instruction following the blx */
2794 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002795 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002796 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002797 /* pc <- computed goto target */
2798 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002799 break;
2800 }
2801 default:
2802 return true;
2803 }
2804 return false;
2805}
2806
Ben Cheng7a2697d2010-06-07 13:44:23 -07002807/*
2808 * See the example of predicted inlining listed before the
2809 * genValidationForPredictedInline function. The function here takes care the
2810 * branch over at 0x4858de78 and the misprediction target at 0x4858de7a.
2811 */
2812static void genLandingPadForMispredictedCallee(CompilationUnit *cUnit, MIR *mir,
2813 BasicBlock *bb,
2814 ArmLIR *labelList)
2815{
2816 BasicBlock *fallThrough = bb->fallThrough;
2817
2818 /* Bypass the move-result block if there is one */
2819 if (fallThrough->firstMIRInsn) {
2820 assert(fallThrough->firstMIRInsn->OptimizationFlags & MIR_INLINED_PRED);
2821 fallThrough = fallThrough->fallThrough;
2822 }
2823 /* Generate a branch over if the predicted inlining is correct */
2824 genUnconditionalBranch(cUnit, &labelList[fallThrough->id]);
2825
2826 /* Reset the register state */
2827 dvmCompilerResetRegPool(cUnit);
2828 dvmCompilerClobberAllRegs(cUnit);
2829 dvmCompilerResetNullCheck(cUnit);
2830
2831 /* Target for the slow invoke path */
2832 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2833 target->defMask = ENCODE_ALL;
2834 /* Hook up the target to the verification branch */
2835 mir->meta.callsiteInfo->misPredBranchOver->target = (LIR *) target;
2836}
2837
jeffhao71eee1f2011-01-04 14:18:54 -08002838static bool handleFmt35c_3rc_5rc(CompilationUnit *cUnit, MIR *mir,
2839 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002840{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002841 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002842 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002843
Ben Cheng7a2697d2010-06-07 13:44:23 -07002844 /* An invoke with the MIR_INLINED is effectively a no-op */
2845 if (mir->OptimizationFlags & MIR_INLINED)
2846 return false;
2847
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002848 if (bb->fallThrough != NULL)
2849 retChainingCell = &labelList[bb->fallThrough->id];
2850
Ben Chengba4fc8b2009-06-01 13:00:29 -07002851 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002852 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002853 /*
2854 * calleeMethod = this->clazz->vtable[
2855 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2856 * ]
2857 */
2858 case OP_INVOKE_VIRTUAL:
jeffhao71eee1f2011-01-04 14:18:54 -08002859 case OP_INVOKE_VIRTUAL_RANGE:
2860 case OP_INVOKE_VIRTUAL_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002861 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002862 int methodIndex =
2863 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2864 methodIndex;
2865
Ben Cheng7a2697d2010-06-07 13:44:23 -07002866 /*
2867 * If the invoke has non-null misPredBranchOver, we need to generate
2868 * the non-inlined version of the invoke here to handle the
2869 * mispredicted case.
2870 */
2871 if (mir->meta.callsiteInfo->misPredBranchOver) {
2872 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
2873 }
2874
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002875 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002876 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2877 else
2878 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2879
Ben Cheng38329f52009-07-07 14:19:20 -07002880 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2881 retChainingCell,
2882 predChainingCell,
2883 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002884 break;
2885 }
2886 /*
2887 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2888 * ->pResMethods[BBBB]->methodIndex]
2889 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002890 case OP_INVOKE_SUPER:
jeffhao71eee1f2011-01-04 14:18:54 -08002891 case OP_INVOKE_SUPER_RANGE:
2892 case OP_INVOKE_SUPER_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002893 /* Grab the method ptr directly from what the interpreter sees */
2894 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2895 assert(calleeMethod == cUnit->method->clazz->super->vtable[
2896 cUnit->method->clazz->pDvmDex->
2897 pResMethods[dInsn->vB]->methodIndex]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002898
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002899 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002900 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2901 else
2902 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2903
2904 /* r0 = calleeMethod */
2905 loadConstant(cUnit, r0, (int) calleeMethod);
2906
Ben Cheng38329f52009-07-07 14:19:20 -07002907 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2908 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002909 break;
2910 }
2911 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2912 case OP_INVOKE_DIRECT:
jeffhao71eee1f2011-01-04 14:18:54 -08002913 case OP_INVOKE_DIRECT_RANGE:
2914 case OP_INVOKE_DIRECT_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002915 /* Grab the method ptr directly from what the interpreter sees */
2916 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2917 assert(calleeMethod ==
2918 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002919
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002920 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002921 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2922 else
2923 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2924
2925 /* r0 = calleeMethod */
2926 loadConstant(cUnit, r0, (int) calleeMethod);
2927
Ben Cheng38329f52009-07-07 14:19:20 -07002928 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2929 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002930 break;
2931 }
2932 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2933 case OP_INVOKE_STATIC:
jeffhao71eee1f2011-01-04 14:18:54 -08002934 case OP_INVOKE_STATIC_RANGE:
2935 case OP_INVOKE_STATIC_JUMBO: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07002936 /* Grab the method ptr directly from what the interpreter sees */
2937 const Method *calleeMethod = mir->meta.callsiteInfo->method;
2938 assert(calleeMethod ==
2939 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002940
Dan Bornstein9a1f8162010-12-01 17:02:26 -08002941 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002942 genProcessArgsNoRange(cUnit, mir, dInsn,
2943 NULL /* no null check */);
2944 else
2945 genProcessArgsRange(cUnit, mir, dInsn,
2946 NULL /* no null check */);
2947
2948 /* r0 = calleeMethod */
2949 loadConstant(cUnit, r0, (int) calleeMethod);
2950
Ben Cheng38329f52009-07-07 14:19:20 -07002951 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2952 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002953 break;
2954 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002955 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002956 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2957 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002958 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002959 * The following is an example of generated code for
2960 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002961 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002962 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2963 * 0x47357e36 : ldr r0, [r5, #0] --+
2964 * 0x47357e38 : sub r7,r5,#24 |
2965 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2966 * 0x47357e3e : beq 0x47357e82 |
2967 * 0x47357e40 : stmia r7, <r0> --+
2968 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2969 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2970 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2971 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2972 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2973 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2974 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2975 * 0x47357e50 : mov r8, r1 --+
2976 * 0x47357e52 : mov r9, r2 |
2977 * 0x47357e54 : ldr r2, [pc, #96] |
2978 * 0x47357e56 : mov r10, r3 |
2979 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2980 * 0x47357e5a : ldr r3, [pc, #88] |
2981 * 0x47357e5c : ldr r7, [pc, #80] |
2982 * 0x47357e5e : mov r1, #1452 |
2983 * 0x47357e62 : blx r7 --+
2984 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2985 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2986 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2987 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2988 * 0x47357e6c : blx_2 see above --+ COMMON
2989 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2990 * 0x47357e70 : cmp r1, #0 --> compare against 0
2991 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
Ben Chengaf5aa1f2011-01-04 15:37:04 -08002992 * 0x47357e74 : ldr r7, [pc, #off] --+
Ben Cheng09e50c92010-05-02 10:45:32 -07002993 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2994 * 0x47357e78 : mov r3, r10 |
2995 * 0x47357e7a : blx r7 --+
2996 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2997 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2998 * 0x47357e80 : blx_2 see above --+
2999 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
3000 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07003001 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07003002 * 0x47357e84 : ldr r1, [r6, #92]
3003 * 0x47357e86 : blx r1
3004 * 0x47357e88 : .align4
3005 * -------- chaining cell (hot): 0x000b
3006 * 0x47357e88 : ldr r0, [r6, #104]
3007 * 0x47357e8a : blx r0
3008 * 0x47357e8c : data 0x19e2(6626)
3009 * 0x47357e8e : data 0x4257(16983)
3010 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07003011 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07003012 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
3013 * 0x47357e92 : data 0x0000(0)
3014 * 0x47357e94 : data 0x0000(0) --> class
3015 * 0x47357e96 : data 0x0000(0)
3016 * 0x47357e98 : data 0x0000(0) --> method
3017 * 0x47357e9a : data 0x0000(0)
3018 * 0x47357e9c : data 0x0000(0) --> rechain count
3019 * 0x47357e9e : data 0x0000(0)
3020 * -------- end of chaining cells (0x006c)
3021 * 0x47357eb0 : .word (0xad03e369)
3022 * 0x47357eb4 : .word (0x28a90)
3023 * 0x47357eb8 : .word (0x41a63394)
3024 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003025 */
3026 case OP_INVOKE_INTERFACE:
jeffhao71eee1f2011-01-04 14:18:54 -08003027 case OP_INVOKE_INTERFACE_RANGE:
3028 case OP_INVOKE_INTERFACE_JUMBO: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003029 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003030
Ben Cheng7a2697d2010-06-07 13:44:23 -07003031 /*
3032 * If the invoke has non-null misPredBranchOver, we need to generate
3033 * the non-inlined version of the invoke here to handle the
3034 * mispredicted case.
3035 */
3036 if (mir->meta.callsiteInfo->misPredBranchOver) {
3037 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3038 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003039
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003040 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003041 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3042 else
3043 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3044
Ben Cheng38329f52009-07-07 14:19:20 -07003045 /* "this" is already left in r0 by genProcessArgs* */
3046
3047 /* r4PC = dalvikCallsite */
3048 loadConstant(cUnit, r4PC,
3049 (int) (cUnit->method->insns + mir->offset));
3050
3051 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003052 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003053 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003054 addrRetChain->generic.target = (LIR *) retChainingCell;
3055
3056 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003057 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003058 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003059 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3060
buzbee18fba342011-01-19 15:31:15 -08003061 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3062 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN_PROF :
3063 TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
Ben Cheng38329f52009-07-07 14:19:20 -07003064
3065 /* return through lr - jump to the chaining cell */
3066 genUnconditionalBranch(cUnit, predChainingCell);
3067
3068 /*
3069 * null-check on "this" may have been eliminated, but we still need
3070 * a PC-reconstruction label for stack overflow bailout.
3071 */
3072 if (pcrLabel == NULL) {
3073 int dPC = (int) (cUnit->method->insns + mir->offset);
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003074 pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003075 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003076 pcrLabel->operands[0] = dPC;
3077 pcrLabel->operands[1] = mir->offset;
3078 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003079 dvmInsertGrowableList(&cUnit->pcReconstructionList,
3080 (intptr_t) pcrLabel);
Ben Cheng38329f52009-07-07 14:19:20 -07003081 }
3082
3083 /* return through lr+2 - punt to the interpreter */
3084 genUnconditionalBranch(cUnit, pcrLabel);
3085
3086 /*
3087 * return through lr+4 - fully resolve the callee method.
3088 * r1 <- count
3089 * r2 <- &predictedChainCell
3090 * r3 <- this->class
3091 * r4 <- dPC
3092 * r7 <- this->class->vtable
3093 */
3094
3095 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003096 genRegCopy(cUnit, r8, r1);
3097 genRegCopy(cUnit, r9, r2);
3098 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003099
Ben Chengba4fc8b2009-06-01 13:00:29 -07003100 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003101 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003102
3103 /* r1 = BBBB */
3104 loadConstant(cUnit, r1, dInsn->vB);
3105
3106 /* r2 = method (caller) */
3107 loadConstant(cUnit, r2, (int) cUnit->method);
3108
3109 /* r3 = pDvmDex */
3110 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3111
Ben Chengbd1326d2010-04-02 15:04:53 -07003112 LOAD_FUNC_ADDR(cUnit, r7,
3113 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003114 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003115 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3116
Ben Cheng09e50c92010-05-02 10:45:32 -07003117 dvmCompilerClobberCallRegs(cUnit);
3118 /* generate a branch over if the interface method is resolved */
buzbee8f8109a2010-08-31 10:16:35 -07003119 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
Ben Cheng09e50c92010-05-02 10:45:32 -07003120 /*
3121 * calleeMethod == NULL -> throw
3122 */
3123 loadConstant(cUnit, r0,
3124 (int) (cUnit->method->insns + mir->offset));
3125 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3126 /* noreturn */
3127
3128 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3129 target->defMask = ENCODE_ALL;
3130 branchOver->generic.target = (LIR *) target;
3131
Bill Buzbee1465db52009-09-23 17:17:35 -07003132 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003133
Ben Cheng38329f52009-07-07 14:19:20 -07003134 /* Check if rechain limit is reached */
buzbee8f8109a2010-08-31 10:16:35 -07003135 ArmLIR *bypassRechaining = genCmpImmBranch(cUnit, kArmCondGt,
3136 r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003137
Ben Chengaf5aa1f2011-01-04 15:37:04 -08003138 LOAD_FUNC_ADDR(cUnit, r7, (int) dvmJitToPatchPredictedChain);
Ben Cheng38329f52009-07-07 14:19:20 -07003139
Ben Chengb88ec3c2010-05-17 12:50:33 -07003140 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07003141 genRegCopy(cUnit, r2, r9);
3142 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003143
3144 /*
3145 * r0 = calleeMethod
3146 * r2 = &predictedChainingCell
3147 * r3 = class
3148 *
3149 * &returnChainingCell has been loaded into r1 but is not needed
3150 * when patching the chaining cell and will be clobbered upon
3151 * returning so it will be reconstructed again.
3152 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003153 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003154
3155 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003156 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003157 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003158
3159 bypassRechaining->generic.target = (LIR *) addrRetChain;
3160
Ben Chengba4fc8b2009-06-01 13:00:29 -07003161 /*
3162 * r0 = this, r1 = calleeMethod,
3163 * r1 = &ChainingCell,
3164 * r4PC = callsiteDPC,
3165 */
buzbee18fba342011-01-19 15:31:15 -08003166 genDispatchToHandler(cUnit, gDvmJit.methodTraceSupport ?
3167 TEMPLATE_INVOKE_METHOD_NO_OPT_PROF :
3168 TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003169#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003170 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003171#endif
3172 /* Handle exceptions using the interpreter */
3173 genTrap(cUnit, mir->offset, pcrLabel);
3174 break;
3175 }
3176 /* NOP */
3177 case OP_INVOKE_DIRECT_EMPTY: {
buzbee18fba342011-01-19 15:31:15 -08003178 if (gDvmJit.methodTraceSupport)
3179 genInterpSingleStep(cUnit, mir);
3180 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003181 }
3182 case OP_FILLED_NEW_ARRAY:
jeffhao71eee1f2011-01-04 14:18:54 -08003183 case OP_FILLED_NEW_ARRAY_RANGE:
3184 case OP_FILLED_NEW_ARRAY_JUMBO: {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003185 /* Just let the interpreter deal with these */
3186 genInterpSingleStep(cUnit, mir);
3187 break;
3188 }
3189 default:
3190 return true;
3191 }
3192 return false;
3193}
3194
Ben Chengcfdeca32011-01-14 11:36:46 -08003195/* "this" pointer is already in r0 */
3196static void genValidationForMethodCallee(CompilationUnit *cUnit, MIR *mir,
3197 ArmLIR **classCheck)
3198{
3199 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3200 dvmCompilerLockAllTemps(cUnit);
3201
3202 loadConstant(cUnit, r1, (int) callsiteInfo->clazz);
3203
3204 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r2);
3205 /* Branch to the slow path if classes are not equal */
3206 opRegReg(cUnit, kOpCmp, r1, r2);
3207 /*
3208 * Set the misPredBranchOver target so that it will be generated when the
3209 * code for the non-optimized invoke is generated.
3210 */
3211 *classCheck = opCondBranch(cUnit, kArmCondNe);
3212}
3213
Ben Chengba4fc8b2009-06-01 13:00:29 -07003214static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003215 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003216{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003217 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003218
Ben Cheng7a2697d2010-06-07 13:44:23 -07003219 /* An invoke with the MIR_INLINED is effectively a no-op */
3220 if (mir->OptimizationFlags & MIR_INLINED)
3221 return false;
3222
Ben Chengba4fc8b2009-06-01 13:00:29 -07003223 DecodedInstruction *dInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003224 switch (mir->dalvikInsn.opcode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003225 /* calleeMethod = this->clazz->vtable[BBBB] */
3226 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3227 case OP_INVOKE_VIRTUAL_QUICK: {
3228 int methodIndex = dInsn->vB;
Bill Buzbeea8589332010-12-27 09:31:21 -08003229 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3230 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Cheng7a2697d2010-06-07 13:44:23 -07003231
3232 /*
3233 * If the invoke has non-null misPredBranchOver, we need to generate
3234 * the non-inlined version of the invoke here to handle the
3235 * mispredicted case.
3236 */
3237 if (mir->meta.callsiteInfo->misPredBranchOver) {
3238 genLandingPadForMispredictedCallee(cUnit, mir, bb, labelList);
3239 }
3240
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003241 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003242 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3243 else
3244 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3245
Ben Chengcfdeca32011-01-14 11:36:46 -08003246
3247 if (mir->OptimizationFlags & MIR_INVOKE_METHOD_JIT) {
3248 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3249 void *calleeAddr = dvmJitGetMethodAddr(calleeMethod->insns);
3250 if (calleeAddr) {
3251 ArmLIR *classCheck;
3252 cUnit->printMe = true;
3253 genValidationForMethodCallee(cUnit, mir, &classCheck);
3254 newLIR2(cUnit, kThumbBl1, (int) calleeAddr,
3255 (int) calleeAddr);
3256 newLIR2(cUnit, kThumbBl2, (int) calleeAddr,
3257 (int) calleeAddr);
3258 genUnconditionalBranch(cUnit, retChainingCell);
3259
3260 /* Target of slow path */
3261 ArmLIR *slowPathLabel = newLIR0(cUnit,
3262 kArmPseudoTargetLabel);
3263
3264 slowPathLabel->defMask = ENCODE_ALL;
3265 classCheck->generic.target = (LIR *) slowPathLabel;
3266 }
3267 }
3268
Ben Cheng38329f52009-07-07 14:19:20 -07003269 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3270 retChainingCell,
3271 predChainingCell,
3272 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003273 break;
3274 }
3275 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3276 case OP_INVOKE_SUPER_QUICK:
3277 case OP_INVOKE_SUPER_QUICK_RANGE: {
Ben Cheng7a2697d2010-06-07 13:44:23 -07003278 /* Grab the method ptr directly from what the interpreter sees */
3279 const Method *calleeMethod = mir->meta.callsiteInfo->method;
3280 assert(calleeMethod ==
3281 cUnit->method->clazz->super->vtable[dInsn->vB]);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003282
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003283 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER_QUICK)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003284 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3285 else
3286 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3287
3288 /* r0 = calleeMethod */
3289 loadConstant(cUnit, r0, (int) calleeMethod);
3290
Ben Cheng38329f52009-07-07 14:19:20 -07003291 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3292 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003293 break;
3294 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003295 default:
3296 return true;
3297 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003298 return false;
3299}
3300
3301/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003302 * This operation is complex enough that we'll do it partly inline
3303 * and partly with a handler. NOTE: the handler uses hardcoded
3304 * values for string object offsets and must be revisitied if the
3305 * layout changes.
3306 */
3307static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3308{
3309#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003310 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003311#else
3312 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003313 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3314 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003315
3316 loadValueDirectFixed(cUnit, rlThis, r0);
3317 loadValueDirectFixed(cUnit, rlComp, r1);
3318 /* Test objects for NULL */
3319 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3320 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3321 /*
3322 * TUNING: we could check for object pointer equality before invoking
3323 * handler. Unclear whether the gain would be worth the added code size
3324 * expansion.
3325 */
3326 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003327 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3328 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003329 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003330#endif
3331}
3332
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003333static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003334{
3335#if defined(USE_GLOBAL_STRING_DEFS)
Elliott Hughes7e914f12011-01-19 18:18:42 -08003336 return handleExecuteInlineC(cUnit, mir);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003337#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003338 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3339 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003340
3341 loadValueDirectFixed(cUnit, rlThis, r0);
3342 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003343 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3344 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003345 /* Test objects for NULL */
3346 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3347 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003348 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3349 dvmCompilerGetReturn(cUnit));
Elliott Hughes7e914f12011-01-19 18:18:42 -08003350 return false;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003351#endif
3352}
3353
Elliott Hughesee34f592010-04-05 18:13:52 -07003354// Generates an inlined String.isEmpty or String.length.
3355static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3356 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003357{
Elliott Hughesee34f592010-04-05 18:13:52 -07003358 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003359 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3360 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3361 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3362 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3363 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3364 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3365 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003366 if (isEmpty) {
3367 // dst = (dst == 0);
3368 int tReg = dvmCompilerAllocTemp(cUnit);
3369 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3370 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3371 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003372 storeValue(cUnit, rlDest, rlResult);
3373 return false;
3374}
3375
Elliott Hughesee34f592010-04-05 18:13:52 -07003376static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3377{
3378 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3379}
3380
3381static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3382{
3383 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3384}
3385
Bill Buzbee1f748632010-03-02 16:14:41 -08003386static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3387{
3388 int contents = offsetof(ArrayObject, contents);
3389 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3390 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3391 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3392 RegLocation rlResult;
3393 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3394 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3395 int regMax = dvmCompilerAllocTemp(cUnit);
3396 int regOff = dvmCompilerAllocTemp(cUnit);
3397 int regPtr = dvmCompilerAllocTemp(cUnit);
3398 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3399 mir->offset, NULL);
3400 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3401 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3402 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3403 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3404 dvmCompilerFreeTemp(cUnit, regMax);
3405 opRegImm(cUnit, kOpAdd, regPtr, contents);
3406 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3407 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3408 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3409 storeValue(cUnit, rlDest, rlResult);
3410 return false;
3411}
3412
3413static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3414{
3415 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3416 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Elliott Hughese22bd842010-08-20 18:47:36 -07003417 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
Bill Buzbee1f748632010-03-02 16:14:41 -08003418 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3419 int signReg = dvmCompilerAllocTemp(cUnit);
3420 /*
3421 * abs(x) = y<=x>>31, (x+y)^y.
3422 * Thumb2's IT block also yields 3 instructions, but imposes
3423 * scheduling constraints.
3424 */
3425 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3426 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3427 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3428 storeValue(cUnit, rlDest, rlResult);
3429 return false;
3430}
3431
3432static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3433{
3434 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3435 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3436 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3437 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3438 int signReg = dvmCompilerAllocTemp(cUnit);
3439 /*
3440 * abs(x) = y<=x>>31, (x+y)^y.
3441 * Thumb2 IT block allows slightly shorter sequence,
3442 * but introduces a scheduling barrier. Stick with this
3443 * mechanism for now.
3444 */
3445 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3446 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3447 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3448 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3449 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3450 storeValueWide(cUnit, rlDest, rlResult);
3451 return false;
3452}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003453
Elliott Hughese22bd842010-08-20 18:47:36 -07003454static bool genInlinedIntFloatConversion(CompilationUnit *cUnit, MIR *mir)
3455{
3456 // Just move from source to destination...
3457 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3458 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3459 storeValue(cUnit, rlDest, rlSrc);
3460 return false;
3461}
3462
3463static bool genInlinedLongDoubleConversion(CompilationUnit *cUnit, MIR *mir)
3464{
3465 // Just move from source to destination...
3466 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3467 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3468 storeValueWide(cUnit, rlDest, rlSrc);
3469 return false;
3470}
3471
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003472/*
Elliott Hughes7e914f12011-01-19 18:18:42 -08003473 * JITs a call to a C function.
3474 * TODO: use this for faster native method invocation for simple native
3475 * methods (http://b/3069458).
3476 */
3477static bool handleExecuteInlineC(CompilationUnit *cUnit, MIR *mir)
3478{
3479 DecodedInstruction *dInsn = &mir->dalvikInsn;
3480 int operation = dInsn->vB;
3481 unsigned int i;
3482 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
3483 uintptr_t fn = (int) inLineTable[operation].func;
3484 if (fn == 0) {
3485 dvmCompilerAbort(cUnit);
3486 }
3487 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
3488 dvmCompilerClobberCallRegs(cUnit);
3489 dvmCompilerClobber(cUnit, r4PC);
3490 dvmCompilerClobber(cUnit, r7);
3491 int offset = offsetof(InterpState, retval);
3492 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3493 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
3494 LOAD_FUNC_ADDR(cUnit, r4PC, fn);
3495 genExportPC(cUnit, mir);
3496 for (i=0; i < dInsn->vA; i++) {
3497 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
3498 }
3499 opReg(cUnit, kOpBlx, r4PC);
3500 opRegImm(cUnit, kOpAdd, r13, 8);
3501 /* NULL? */
3502 ArmLIR *branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
3503 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
3504 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3505 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3506 target->defMask = ENCODE_ALL;
3507 branchOver->generic.target = (LIR *) target;
3508 return false;
3509}
3510
3511/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003512 * NOTE: Handles both range and non-range versions (arguments
3513 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003514 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003515static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003516{
3517 DecodedInstruction *dInsn = &mir->dalvikInsn;
Elliott Hughes7e914f12011-01-19 18:18:42 -08003518 assert(dInsn->opcode == OP_EXECUTE_INLINE_RANGE ||
3519 dInsn->opcode == OP_EXECUTE_INLINE);
3520 switch (dInsn->vB) {
3521 case INLINE_EMPTYINLINEMETHOD:
3522 return false; /* Nop */
3523
3524 /* These ones we potentially JIT inline. */
3525 case INLINE_STRING_LENGTH:
3526 return genInlinedStringLength(cUnit, mir);
3527 case INLINE_STRING_IS_EMPTY:
3528 return genInlinedStringIsEmpty(cUnit, mir);
3529 case INLINE_MATH_ABS_INT:
3530 return genInlinedAbsInt(cUnit, mir);
3531 case INLINE_MATH_ABS_LONG:
3532 return genInlinedAbsLong(cUnit, mir);
3533 case INLINE_MATH_MIN_INT:
3534 return genInlinedMinMaxInt(cUnit, mir, true);
3535 case INLINE_MATH_MAX_INT:
3536 return genInlinedMinMaxInt(cUnit, mir, false);
3537 case INLINE_STRING_CHARAT:
3538 return genInlinedStringCharAt(cUnit, mir);
3539 case INLINE_MATH_SQRT:
3540 return genInlineSqrt(cUnit, mir);
3541 case INLINE_MATH_ABS_FLOAT:
3542 return genInlinedAbsFloat(cUnit, mir);
3543 case INLINE_MATH_ABS_DOUBLE:
3544 return genInlinedAbsDouble(cUnit, mir);
3545 case INLINE_STRING_COMPARETO:
3546 return genInlinedCompareTo(cUnit, mir);
3547 case INLINE_STRING_FASTINDEXOF_II:
3548 return genInlinedFastIndexOf(cUnit, mir);
3549 case INLINE_FLOAT_TO_RAW_INT_BITS:
3550 case INLINE_INT_BITS_TO_FLOAT:
3551 return genInlinedIntFloatConversion(cUnit, mir);
3552 case INLINE_DOUBLE_TO_RAW_LONG_BITS:
3553 case INLINE_LONG_BITS_TO_DOUBLE:
3554 return genInlinedLongDoubleConversion(cUnit, mir);
3555
3556 /*
3557 * These ones we just JIT a call to a C function for.
3558 * TODO: special-case these in the other "invoke" call paths.
3559 */
3560 case INLINE_STRING_EQUALS:
3561 case INLINE_MATH_COS:
3562 case INLINE_MATH_SIN:
3563 case INLINE_FLOAT_TO_INT_BITS:
3564 case INLINE_DOUBLE_TO_LONG_BITS:
3565 return handleExecuteInlineC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003566 }
Elliott Hughes7e914f12011-01-19 18:18:42 -08003567 dvmCompilerAbort(cUnit);
3568 return false; // Not reachable; keeps compiler happy.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003569}
3570
3571static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3572{
Bill Buzbee1465db52009-09-23 17:17:35 -07003573 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003574 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3575 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003576 loadConstantNoClobber(cUnit, rlResult.lowReg,
3577 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3578 loadConstantNoClobber(cUnit, rlResult.highReg,
3579 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003580 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003581 return false;
3582}
3583
Ben Chengba4fc8b2009-06-01 13:00:29 -07003584/*
3585 * The following are special processing routines that handle transfer of
3586 * controls between compiled code and the interpreter. Certain VM states like
3587 * Dalvik PC and special-purpose registers are reconstructed here.
3588 */
3589
Bill Buzbeebd047242010-05-13 13:02:53 -07003590/*
3591 * Insert a
3592 * b .+4
3593 * nop
3594 * pair at the beginning of a chaining cell. This serves as the
3595 * switch branch that selects between reverting to the interpreter or
3596 * not. Once the cell is chained to a translation, the cell will
3597 * contain a 32-bit branch. Subsequent chain/unchain operations will
3598 * then only alter that first 16-bits - the "b .+4" for unchaining,
3599 * and the restoration of the first half of the 32-bit branch for
3600 * rechaining.
3601 */
3602static void insertChainingSwitch(CompilationUnit *cUnit)
3603{
3604 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3605 newLIR2(cUnit, kThumbOrr, r0, r0);
3606 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3607 target->defMask = ENCODE_ALL;
3608 branch->generic.target = (LIR *) target;
3609}
3610
Ben Cheng1efc9c52009-06-08 18:25:27 -07003611/* Chaining cell for code that may need warmup. */
3612static void handleNormalChainingCell(CompilationUnit *cUnit,
3613 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003614{
Ben Cheng11d8f142010-03-24 15:24:19 -07003615 /*
3616 * Use raw instruction constructors to guarantee that the generated
3617 * instructions fit the predefined cell size.
3618 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003619 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003620 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3621 offsetof(InterpState,
3622 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3623 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003624 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3625}
3626
3627/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003628 * Chaining cell for instructions that immediately following already translated
3629 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003630 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003631static void handleHotChainingCell(CompilationUnit *cUnit,
3632 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003633{
Ben Cheng11d8f142010-03-24 15:24:19 -07003634 /*
3635 * Use raw instruction constructors to guarantee that the generated
3636 * instructions fit the predefined cell size.
3637 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003638 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003639 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3640 offsetof(InterpState,
3641 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3642 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003643 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3644}
3645
Jeff Hao97319a82009-08-12 16:57:15 -07003646/* Chaining cell for branches that branch back into the same basic block */
3647static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3648 unsigned int offset)
3649{
Ben Cheng11d8f142010-03-24 15:24:19 -07003650 /*
3651 * Use raw instruction constructors to guarantee that the generated
3652 * instructions fit the predefined cell size.
3653 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003654 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003655#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003656 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003657 offsetof(InterpState,
3658 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003659#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003660 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003661 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3662#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003663 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003664 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3665}
3666
Ben Chengba4fc8b2009-06-01 13:00:29 -07003667/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003668static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3669 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003670{
Ben Cheng11d8f142010-03-24 15:24:19 -07003671 /*
3672 * Use raw instruction constructors to guarantee that the generated
3673 * instructions fit the predefined cell size.
3674 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003675 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003676 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3677 offsetof(InterpState,
3678 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3679 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003680 addWordData(cUnit, (int) (callee->insns), true);
3681}
3682
Ben Cheng38329f52009-07-07 14:19:20 -07003683/* Chaining cell for monomorphic method invocations. */
3684static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3685{
3686
3687 /* Should not be executed in the initial state */
3688 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3689 /* To be filled: class */
3690 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3691 /* To be filled: method */
3692 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3693 /*
3694 * Rechain count. The initial value of 0 here will trigger chaining upon
3695 * the first invocation of this callsite.
3696 */
3697 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3698}
3699
Ben Chengba4fc8b2009-06-01 13:00:29 -07003700/* Load the Dalvik PC into r0 and jump to the specified target */
3701static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003702 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003703{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003704 ArmLIR **pcrLabel =
3705 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003706 int numElems = cUnit->pcReconstructionList.numUsed;
3707 int i;
3708 for (i = 0; i < numElems; i++) {
3709 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3710 /* r0 = dalvik PC */
3711 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3712 genUnconditionalBranch(cUnit, targetLabel);
3713 }
3714}
3715
Bill Buzbee1465db52009-09-23 17:17:35 -07003716static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3717 "kMirOpPhi",
3718 "kMirOpNullNRangeUpCheck",
3719 "kMirOpNullNRangeDownCheck",
3720 "kMirOpLowerBound",
3721 "kMirOpPunt",
Ben Cheng7a2697d2010-06-07 13:44:23 -07003722 "kMirOpCheckInlinePrediction",
Ben Cheng4238ec22009-08-24 16:32:22 -07003723};
3724
3725/*
3726 * vA = arrayReg;
3727 * vB = idxReg;
3728 * vC = endConditionReg;
3729 * arg[0] = maxC
3730 * arg[1] = minC
3731 * arg[2] = loopBranchConditionCode
3732 */
3733static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3734{
Bill Buzbee1465db52009-09-23 17:17:35 -07003735 /*
3736 * NOTE: these synthesized blocks don't have ssa names assigned
3737 * for Dalvik registers. However, because they dominate the following
3738 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3739 * ssa name.
3740 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003741 DecodedInstruction *dInsn = &mir->dalvikInsn;
3742 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003743 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003744 int regLength;
3745 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3746 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003747
3748 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003749 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3750 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3751 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003752 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3753
3754 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003755 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003756 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003757
3758 int delta = maxC;
3759 /*
3760 * If the loop end condition is ">=" instead of ">", then the largest value
3761 * of the index is "endCondition - 1".
3762 */
3763 if (dInsn->arg[2] == OP_IF_GE) {
3764 delta--;
3765 }
3766
3767 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003768 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003769 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3770 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003771 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003772 }
3773 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003774 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003775 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003776}
3777
3778/*
3779 * vA = arrayReg;
3780 * vB = idxReg;
3781 * vC = endConditionReg;
3782 * arg[0] = maxC
3783 * arg[1] = minC
3784 * arg[2] = loopBranchConditionCode
3785 */
3786static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3787{
3788 DecodedInstruction *dInsn = &mir->dalvikInsn;
3789 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003790 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003791 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003792 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3793 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003794
3795 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003796 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3797 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3798 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003799 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3800
3801 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003802 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003803
3804 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003805 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003806 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3807 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003808 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003809 }
3810
3811 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003812 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003813 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003814}
3815
3816/*
3817 * vA = idxReg;
3818 * vB = minC;
3819 */
3820static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3821{
3822 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003823 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003824 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003825
3826 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003827 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003828
3829 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003830 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003831 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3832}
3833
Ben Cheng7a2697d2010-06-07 13:44:23 -07003834/*
3835 * vC = this
3836 *
3837 * A predicted inlining target looks like the following, where instructions
3838 * between 0x4858de66 and 0x4858de72 are checking if the predicted class
3839 * matches "this", and the verificaion code is generated by this routine.
3840 *
3841 * (C) means the instruction is inlined from the callee, and (PI) means the
3842 * instruction is the predicted inlined invoke, whose corresponding
3843 * instructions are still generated to handle the mispredicted case.
3844 *
3845 * D/dalvikvm( 86): -------- kMirOpCheckInlinePrediction
3846 * D/dalvikvm( 86): 0x4858de66 (0002): ldr r0, [r5, #68]
3847 * D/dalvikvm( 86): 0x4858de68 (0004): ldr r1, [pc, #140]
3848 * D/dalvikvm( 86): 0x4858de6a (0006): cmp r0, #0
3849 * D/dalvikvm( 86): 0x4858de6c (0008): beq 0x4858deb2
3850 * D/dalvikvm( 86): 0x4858de6e (000a): ldr r2, [r0, #0]
3851 * D/dalvikvm( 86): 0x4858de70 (000c): cmp r1, r2
3852 * D/dalvikvm( 86): 0x4858de72 (000e): bne 0x4858de7a
3853 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @ +iget-object-quick (C)
3854 * v4, v17, (#8)
3855 * D/dalvikvm( 86): 0x4858de74 (0010): ldr r3, [r0, #8]
3856 * D/dalvikvm( 86): 0x4858de76 (0012): str r3, [r5, #16]
3857 * D/dalvikvm( 86): -------- dalvik offset: 0x004c @
3858 * +invoke-virtual-quick/range (PI) v17..v17
3859 * D/dalvikvm( 86): 0x4858de78 (0014): b 0x4858debc
3860 * D/dalvikvm( 86): 0x4858de7a (0016): add r4,r5,#68
3861 * D/dalvikvm( 86): -------- BARRIER
3862 * D/dalvikvm( 86): 0x4858de7e (001a): ldmia r4, <r0>
3863 * D/dalvikvm( 86): -------- BARRIER
3864 * D/dalvikvm( 86): 0x4858de80 (001c): sub r7,r5,#24
3865 * D/dalvikvm( 86): 0x4858de84 (0020): cmp r0, #0
3866 * D/dalvikvm( 86): 0x4858de86 (0022): beq 0x4858deb6
3867 * D/dalvikvm( 86): -------- BARRIER
3868 * D/dalvikvm( 86): 0x4858de88 (0024): stmia r7, <r0>
3869 * D/dalvikvm( 86): -------- BARRIER
3870 * D/dalvikvm( 86): 0x4858de8a (0026): ldr r4, [pc, #104]
3871 * D/dalvikvm( 86): 0x4858de8c (0028): add r1, pc, #28
3872 * D/dalvikvm( 86): 0x4858de8e (002a): add r2, pc, #56
3873 * D/dalvikvm( 86): 0x4858de90 (002c): blx_1 0x48589198
3874 * D/dalvikvm( 86): 0x4858de92 (002e): blx_2 see above
3875 * D/dalvikvm( 86): 0x4858de94 (0030): b 0x4858dec8
3876 * D/dalvikvm( 86): 0x4858de96 (0032): b 0x4858deb6
3877 * D/dalvikvm( 86): 0x4858de98 (0034): ldr r0, [r7, #72]
3878 * D/dalvikvm( 86): 0x4858de9a (0036): cmp r1, #0
3879 * D/dalvikvm( 86): 0x4858de9c (0038): bgt 0x4858dea4
3880 * D/dalvikvm( 86): 0x4858de9e (003a): ldr r7, [r6, #116]
3881 * D/dalvikvm( 86): 0x4858dea0 (003c): movs r1, r6
3882 * D/dalvikvm( 86): 0x4858dea2 (003e): blx r7
3883 * D/dalvikvm( 86): 0x4858dea4 (0040): add r1, pc, #4
3884 * D/dalvikvm( 86): 0x4858dea6 (0042): blx_1 0x485890a0
3885 * D/dalvikvm( 86): 0x4858dea8 (0044): blx_2 see above
3886 * D/dalvikvm( 86): 0x4858deaa (0046): b 0x4858deb6
3887 * D/dalvikvm( 86): 0x4858deac (0048): .align4
3888 * D/dalvikvm( 86): L0x004f:
3889 * D/dalvikvm( 86): -------- dalvik offset: 0x004f @ move-result-object (PI)
3890 * v4, (#0), (#0)
3891 * D/dalvikvm( 86): 0x4858deac (0048): ldr r4, [r6, #8]
3892 * D/dalvikvm( 86): 0x4858deae (004a): str r4, [r5, #16]
3893 * D/dalvikvm( 86): 0x4858deb0 (004c): b 0x4858debc
3894 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3895 * D/dalvikvm( 86): 0x4858deb2 (004e): ldr r0, [pc, #64]
3896 * D/dalvikvm( 86): 0x4858deb4 (0050): b 0x4858deb8
3897 * D/dalvikvm( 86): -------- reconstruct dalvik PC : 0x42beefcc @ +0x004c
3898 * D/dalvikvm( 86): 0x4858deb6 (0052): ldr r0, [pc, #60]
3899 * D/dalvikvm( 86): Exception_Handling:
3900 * D/dalvikvm( 86): 0x4858deb8 (0054): ldr r1, [r6, #100]
3901 * D/dalvikvm( 86): 0x4858deba (0056): blx r1
3902 * D/dalvikvm( 86): 0x4858debc (0058): .align4
3903 * D/dalvikvm( 86): -------- chaining cell (hot): 0x0050
3904 * D/dalvikvm( 86): 0x4858debc (0058): b 0x4858dec0
3905 * D/dalvikvm( 86): 0x4858debe (005a): orrs r0, r0
3906 * D/dalvikvm( 86): 0x4858dec0 (005c): ldr r0, [r6, #112]
3907 * D/dalvikvm( 86): 0x4858dec2 (005e): blx r0
3908 * D/dalvikvm( 86): 0x4858dec4 (0060): data 0xefd4(61396)
3909 * D/dalvikvm( 86): 0x4858dec6 (0062): data 0x42be(17086)
3910 * D/dalvikvm( 86): 0x4858dec8 (0064): .align4
3911 * D/dalvikvm( 86): -------- chaining cell (predicted)
3912 * D/dalvikvm( 86): 0x4858dec8 (0064): data 0xe7fe(59390)
3913 * D/dalvikvm( 86): 0x4858deca (0066): data 0x0000(0)
3914 * D/dalvikvm( 86): 0x4858decc (0068): data 0x0000(0)
3915 * D/dalvikvm( 86): 0x4858dece (006a): data 0x0000(0)
3916 * :
3917 */
3918static void genValidationForPredictedInline(CompilationUnit *cUnit, MIR *mir)
3919{
3920 CallsiteInfo *callsiteInfo = mir->meta.callsiteInfo;
3921 RegLocation rlThis = cUnit->regLocation[mir->dalvikInsn.vC];
3922
3923 rlThis = loadValue(cUnit, rlThis, kCoreReg);
3924 int regPredictedClass = dvmCompilerAllocTemp(cUnit);
3925 loadConstant(cUnit, regPredictedClass, (int) callsiteInfo->clazz);
3926 genNullCheck(cUnit, rlThis.sRegLow, rlThis.lowReg, mir->offset,
3927 NULL);/* null object? */
3928 int regActualClass = dvmCompilerAllocTemp(cUnit);
3929 loadWordDisp(cUnit, rlThis.lowReg, offsetof(Object, clazz), regActualClass);
3930 opRegReg(cUnit, kOpCmp, regPredictedClass, regActualClass);
3931 /*
3932 * Set the misPredBranchOver target so that it will be generated when the
3933 * code for the non-optimized invoke is generated.
3934 */
3935 callsiteInfo->misPredBranchOver = (LIR *) opCondBranch(cUnit, kArmCondNe);
3936}
3937
Ben Cheng4238ec22009-08-24 16:32:22 -07003938/* Extended MIR instructions like PHI */
3939static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3940{
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003941 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003942 char *msg = (char *)dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3943 false);
Ben Cheng4238ec22009-08-24 16:32:22 -07003944 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003945 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003946
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003947 switch (mir->dalvikInsn.opcode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003948 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003949 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003950 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003951 break;
3952 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003953 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003954 genHoistedChecksForCountUpLoop(cUnit, mir);
3955 break;
3956 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003957 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003958 genHoistedChecksForCountDownLoop(cUnit, mir);
3959 break;
3960 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003961 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003962 genHoistedLowerBoundCheck(cUnit, mir);
3963 break;
3964 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003965 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003966 genUnconditionalBranch(cUnit,
3967 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3968 break;
3969 }
Ben Cheng7a2697d2010-06-07 13:44:23 -07003970 case kMirOpCheckInlinePrediction: {
3971 genValidationForPredictedInline(cUnit, mir);
3972 break;
3973 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003974 default:
3975 break;
3976 }
3977}
3978
3979/*
3980 * Create a PC-reconstruction cell for the starting offset of this trace.
3981 * Since the PCR cell is placed near the end of the compiled code which is
3982 * usually out of range for a conditional branch, we put two branches (one
3983 * branch over to the loop body and one layover branch to the actual PCR) at the
3984 * end of the entry block.
3985 */
3986static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3987 ArmLIR *bodyLabel)
3988{
3989 /* Set up the place holder to reconstruct this Dalvik PC */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08003990 ArmLIR *pcrLabel = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08003991 pcrLabel->opcode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003992 pcrLabel->operands[0] =
3993 (int) (cUnit->method->insns + entry->startOffset);
3994 pcrLabel->operands[1] = entry->startOffset;
3995 /* Insert the place holder to the growable list */
Ben Cheng00603072010-10-28 11:13:58 -07003996 dvmInsertGrowableList(&cUnit->pcReconstructionList, (intptr_t) pcrLabel);
Ben Cheng4238ec22009-08-24 16:32:22 -07003997
3998 /*
3999 * Next, create two branches - one branch over to the loop body and the
4000 * other branch to the PCR cell to punt.
4001 */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004002 ArmLIR *branchToBody = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004003 branchToBody->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004004 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004005 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07004006 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
4007
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004008 ArmLIR *branchToPCR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004009 branchToPCR->opcode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07004010 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07004011 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07004012 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
4013}
4014
Ben Chengd5adae12010-03-26 17:45:28 -07004015#if defined(WITH_SELF_VERIFICATION)
4016static bool selfVerificationPuntOps(MIR *mir)
4017{
4018 DecodedInstruction *decInsn = &mir->dalvikInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004019 Opcode op = decInsn->opcode;
Ben Cheng7a2697d2010-06-07 13:44:23 -07004020
Ben Chengd5adae12010-03-26 17:45:28 -07004021 /*
4022 * All opcodes that can throw exceptions and use the
4023 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
4024 * under self-verification mode.
4025 */
4026 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
4027 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
4028 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
4029 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
Ben Cheng7a2697d2010-06-07 13:44:23 -07004030 op == OP_EXECUTE_INLINE_RANGE);
Ben Chengd5adae12010-03-26 17:45:28 -07004031}
4032#endif
4033
Ben Chengba4fc8b2009-06-01 13:00:29 -07004034void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
4035{
4036 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004037 ArmLIR *labelList =
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004038 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08004039 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07004040 int i;
4041
4042 /*
Ben Cheng38329f52009-07-07 14:19:20 -07004043 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07004044 */
Ben Chengcec26f62010-01-15 15:29:33 -08004045 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004046 dvmInitGrowableList(&chainingListByType[i], 2);
4047 }
4048
Ben Cheng00603072010-10-28 11:13:58 -07004049 GrowableListIterator iterator;
4050 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004051
buzbee2e152ba2010-12-15 16:32:35 -08004052 /* Traces start with a profiling entry point. Generate it here */
4053 cUnit->profileCodeSize = genTraceProfileEntry(cUnit);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004054
Ben Chengba4fc8b2009-06-01 13:00:29 -07004055 /* Handle the content in each basic block */
Ben Cheng00603072010-10-28 11:13:58 -07004056 for (i = 0; ; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004057 MIR *mir;
Ben Cheng00603072010-10-28 11:13:58 -07004058 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
4059 if (bb == NULL) break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004060
Ben Cheng00603072010-10-28 11:13:58 -07004061 labelList[i].operands[0] = bb->startOffset;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004062
Ben Cheng00603072010-10-28 11:13:58 -07004063 if (bb->blockType >= kChainingCellGap) {
4064 if (bb->isFallThroughFromInvoke == true) {
Ben Chengd44faf52010-06-02 15:33:51 -07004065 /* Align this block first since it is a return chaining cell */
4066 newLIR0(cUnit, kArmPseudoPseudoAlign4);
4067 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004068 /*
4069 * Append the label pseudo LIR first. Chaining cells will be handled
4070 * separately afterwards.
4071 */
4072 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
4073 }
4074
Ben Cheng00603072010-10-28 11:13:58 -07004075 if (bb->blockType == kTraceEntryBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004076 labelList[i].opcode = kArmPseudoEntryBlock;
Ben Cheng00603072010-10-28 11:13:58 -07004077 if (bb->firstMIRInsn == NULL) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004078 continue;
4079 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004080 setupLoopEntryBlock(cUnit, bb,
4081 &labelList[bb->fallThrough->id]);
Ben Cheng4238ec22009-08-24 16:32:22 -07004082 }
Ben Cheng00603072010-10-28 11:13:58 -07004083 } else if (bb->blockType == kTraceExitBlock) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004084 labelList[i].opcode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07004085 goto gen_fallthrough;
Ben Cheng00603072010-10-28 11:13:58 -07004086 } else if (bb->blockType == kDalvikByteCode) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004087 labelList[i].opcode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07004088 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08004089 dvmCompilerResetRegPool(cUnit);
4090 dvmCompilerClobberAllRegs(cUnit);
4091 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004092 } else {
Ben Cheng00603072010-10-28 11:13:58 -07004093 switch (bb->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004094 case kChainingCellNormal:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004095 labelList[i].opcode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004096 /* handle the codegen later */
4097 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004098 &chainingListByType[kChainingCellNormal], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004099 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004100 case kChainingCellInvokeSingleton:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004101 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004102 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004103 labelList[i].operands[0] =
Ben Cheng00603072010-10-28 11:13:58 -07004104 (int) bb->containingMethod;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004105 /* handle the codegen later */
4106 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004107 &chainingListByType[kChainingCellInvokeSingleton], i);
Ben Cheng38329f52009-07-07 14:19:20 -07004108 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004109 case kChainingCellInvokePredicted:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004110 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004111 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07004112 /* handle the codegen later */
4113 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004114 &chainingListByType[kChainingCellInvokePredicted], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004115 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004116 case kChainingCellHot:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004117 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004118 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004119 /* handle the codegen later */
4120 dvmInsertGrowableList(
Ben Cheng00603072010-10-28 11:13:58 -07004121 &chainingListByType[kChainingCellHot], i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004122 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004123 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07004124 /* Make sure exception handling block is next */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004125 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004126 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004127 assert (i == cUnit->numBlocks - 2);
4128 handlePCReconstruction(cUnit, &labelList[i+1]);
4129 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004130 case kExceptionHandling:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004131 labelList[i].opcode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004132 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07004133 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4134 jitToInterpEntries.dvmJitToInterpPunt),
4135 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07004136 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004137 }
4138 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004139 case kChainingCellBackwardBranch:
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004140 labelList[i].opcode =
Ben Chenga4973592010-03-31 11:59:18 -07004141 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07004142 /* handle the codegen later */
4143 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07004144 &chainingListByType[kChainingCellBackwardBranch],
Ben Cheng00603072010-10-28 11:13:58 -07004145 i);
Jeff Hao97319a82009-08-12 16:57:15 -07004146 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004147 default:
4148 break;
4149 }
4150 continue;
4151 }
Ben Chenge9695e52009-06-16 16:11:47 -07004152
Bill Buzbee89efc3d2009-07-28 11:22:22 -07004153 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07004154
Ben Cheng00603072010-10-28 11:13:58 -07004155 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004156
Bill Buzbeec6f10662010-02-09 11:16:15 -08004157 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004158 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004159 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004160 }
4161
4162 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08004163 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07004164 }
4165
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004166 if (mir->dalvikInsn.opcode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004167 handleExtendedMIR(cUnit, mir);
4168 continue;
4169 }
4170
Bill Buzbee1465db52009-09-23 17:17:35 -07004171
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004172 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
Dan Bornsteine4852762010-12-02 12:45:00 -08004173 InstructionFormat dalvikFormat = dexGetFormatFromOpcode(dalvikOpcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -07004174 char *note;
4175 if (mir->OptimizationFlags & MIR_INLINED) {
4176 note = " (I)";
4177 } else if (mir->OptimizationFlags & MIR_INLINED_PRED) {
4178 note = " (PI)";
4179 } else if (mir->OptimizationFlags & MIR_CALLEE) {
4180 note = " (C)";
4181 } else {
4182 note = NULL;
4183 }
4184
Ben Cheng80211d22011-01-14 10:23:37 -08004185 ArmLIR *boundaryLIR;
4186
4187 /*
4188 * Don't generate the boundary LIR unless we are debugging this
4189 * trace or we need a scheduling barrier.
4190 */
4191 if (headLIR == NULL || cUnit->printMe == true) {
4192 boundaryLIR =
4193 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
4194 mir->offset,
4195 (int) dvmCompilerGetDalvikDisassembly(
4196 &mir->dalvikInsn, note));
4197 /* Remember the first LIR for this block */
4198 if (headLIR == NULL) {
4199 headLIR = boundaryLIR;
4200 /* Set the first boundaryLIR as a scheduling barrier */
4201 headLIR->defMask = ENCODE_ALL;
4202 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004203 }
4204
Ben Cheng80211d22011-01-14 10:23:37 -08004205 /* Don't generate the SSA annotation unless verbose mode is on */
4206 if (cUnit->printMe && mir->ssaRep) {
4207 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
4208 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Chenge9695e52009-06-16 16:11:47 -07004209 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004210
Ben Chengba4fc8b2009-06-01 13:00:29 -07004211 bool notHandled;
4212 /*
4213 * Debugging: screen the opcode first to see if it is in the
4214 * do[-not]-compile list
4215 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004216 bool singleStepMe = SINGLE_STEP_OP(dalvikOpcode);
Ben Chengd5adae12010-03-26 17:45:28 -07004217#if defined(WITH_SELF_VERIFICATION)
4218 if (singleStepMe == false) {
4219 singleStepMe = selfVerificationPuntOps(mir);
4220 }
4221#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004222 if (singleStepMe || cUnit->allSingleStep) {
4223 notHandled = false;
4224 genInterpSingleStep(cUnit, mir);
4225 } else {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004226 opcodeCoverage[dalvikOpcode]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004227 switch (dalvikFormat) {
4228 case kFmt10t:
4229 case kFmt20t:
4230 case kFmt30t:
4231 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004232 mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004233 break;
4234 case kFmt10x:
4235 notHandled = handleFmt10x(cUnit, mir);
4236 break;
4237 case kFmt11n:
4238 case kFmt31i:
4239 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
4240 break;
4241 case kFmt11x:
4242 notHandled = handleFmt11x(cUnit, mir);
4243 break;
4244 case kFmt12x:
4245 notHandled = handleFmt12x(cUnit, mir);
4246 break;
4247 case kFmt20bc:
jeffhao71eee1f2011-01-04 14:18:54 -08004248 case kFmt40sc:
4249 notHandled = handleFmt20bc_Fmt40sc(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004250 break;
4251 case kFmt21c:
4252 case kFmt31c:
jeffhao71eee1f2011-01-04 14:18:54 -08004253 case kFmt41c:
4254 notHandled = handleFmt21c_Fmt31c_Fmt41c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004255 break;
4256 case kFmt21h:
4257 notHandled = handleFmt21h(cUnit, mir);
4258 break;
4259 case kFmt21s:
4260 notHandled = handleFmt21s(cUnit, mir);
4261 break;
4262 case kFmt21t:
Ben Cheng00603072010-10-28 11:13:58 -07004263 notHandled = handleFmt21t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004264 break;
4265 case kFmt22b:
4266 case kFmt22s:
4267 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
4268 break;
4269 case kFmt22c:
jeffhao71eee1f2011-01-04 14:18:54 -08004270 case kFmt52c:
4271 notHandled = handleFmt22c_Fmt52c(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004272 break;
4273 case kFmt22cs:
4274 notHandled = handleFmt22cs(cUnit, mir);
4275 break;
4276 case kFmt22t:
Ben Cheng00603072010-10-28 11:13:58 -07004277 notHandled = handleFmt22t(cUnit, mir, bb, labelList);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004278 break;
4279 case kFmt22x:
4280 case kFmt32x:
4281 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
4282 break;
4283 case kFmt23x:
4284 notHandled = handleFmt23x(cUnit, mir);
4285 break;
4286 case kFmt31t:
4287 notHandled = handleFmt31t(cUnit, mir);
4288 break;
4289 case kFmt3rc:
4290 case kFmt35c:
jeffhao71eee1f2011-01-04 14:18:54 -08004291 case kFmt5rc:
4292 notHandled = handleFmt35c_3rc_5rc(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004293 labelList);
4294 break;
4295 case kFmt3rms:
4296 case kFmt35ms:
Ben Cheng00603072010-10-28 11:13:58 -07004297 notHandled = handleFmt35ms_3rms(cUnit, mir, bb,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004298 labelList);
4299 break;
Dan Bornstein7b3e9b02010-11-09 17:15:10 -08004300 case kFmt35mi:
4301 case kFmt3rmi:
Bill Buzbeece46c942009-11-20 15:41:34 -08004302 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08004303 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004304 case kFmt51l:
4305 notHandled = handleFmt51l(cUnit, mir);
4306 break;
4307 default:
4308 notHandled = true;
4309 break;
4310 }
4311 }
4312 if (notHandled) {
4313 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4314 mir->offset,
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004315 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07004316 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004317 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004318 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004319 }
4320 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004321
Ben Cheng00603072010-10-28 11:13:58 -07004322 if (bb->blockType == kTraceEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004323 dvmCompilerAppendLIR(cUnit,
4324 (LIR *) cUnit->loopAnalysis->branchToBody);
4325 dvmCompilerAppendLIR(cUnit,
4326 (LIR *) cUnit->loopAnalysis->branchToPCR);
4327 }
4328
4329 if (headLIR) {
4330 /*
4331 * Eliminate redundant loads/stores and delay stores into later
4332 * slots
4333 */
4334 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4335 cUnit->lastLIRInsn);
4336 }
4337
4338gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004339 /*
4340 * Check if the block is terminated due to trace length constraint -
4341 * insert an unconditional branch to the chaining cell.
4342 */
Ben Cheng00603072010-10-28 11:13:58 -07004343 if (bb->needFallThroughBranch) {
Ben Cheng1efc9c52009-06-08 18:25:27 -07004344 genUnconditionalBranch(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004345 &labelList[bb->fallThrough->id]);
Ben Cheng1efc9c52009-06-08 18:25:27 -07004346 }
4347
Ben Chengba4fc8b2009-06-01 13:00:29 -07004348 }
4349
Ben Chenge9695e52009-06-16 16:11:47 -07004350 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004351 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004352 size_t j;
4353 int *blockIdList = (int *) chainingListByType[i].elemList;
4354
4355 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4356
4357 /* No chaining cells of this type */
4358 if (cUnit->numChainingCells[i] == 0)
4359 continue;
4360
4361 /* Record the first LIR for a new type of chaining cell */
4362 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4363
4364 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4365 int blockId = blockIdList[j];
Ben Cheng00603072010-10-28 11:13:58 -07004366 BasicBlock *chainingBlock =
4367 (BasicBlock *) dvmGrowableListGetElement(&cUnit->blockList,
4368 blockId);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004369
4370 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004371 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004372
4373 /* Insert the pseudo chaining instruction */
4374 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4375
4376
Ben Cheng00603072010-10-28 11:13:58 -07004377 switch (chainingBlock->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004378 case kChainingCellNormal:
Ben Cheng00603072010-10-28 11:13:58 -07004379 handleNormalChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004380 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004381 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004382 handleInvokeSingletonChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004383 chainingBlock->containingMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004384 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004385 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004386 handleInvokePredictedChainingCell(cUnit);
4387 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004388 case kChainingCellHot:
Ben Cheng00603072010-10-28 11:13:58 -07004389 handleHotChainingCell(cUnit, chainingBlock->startOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004390 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004391 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004392 handleBackwardBranchChainingCell(cUnit,
Ben Cheng00603072010-10-28 11:13:58 -07004393 chainingBlock->startOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07004394 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004395 default:
Ben Cheng00603072010-10-28 11:13:58 -07004396 LOGE("Bad blocktype %d", chainingBlock->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004397 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004398 }
4399 }
4400 }
Ben Chenge9695e52009-06-16 16:11:47 -07004401
Ben Chengcec26f62010-01-15 15:29:33 -08004402 /* Mark the bottom of chaining cells */
4403 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4404
Ben Cheng6c10a972009-10-29 14:39:18 -07004405 /*
4406 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4407 * of all chaining cells for the overflow cases.
4408 */
4409 if (cUnit->switchOverflowPad) {
4410 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4411 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4412 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4413 opRegReg(cUnit, kOpAdd, r1, r1);
4414 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004415#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004416 loadConstant(cUnit, r0, kSwitchOverflow);
4417#endif
4418 opReg(cUnit, kOpBlx, r2);
4419 }
4420
Ben Chenge9695e52009-06-16 16:11:47 -07004421 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004422
4423#if defined(WITH_SELF_VERIFICATION)
4424 selfVerificationBranchInsertPass(cUnit);
4425#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004426}
4427
buzbee2e152ba2010-12-15 16:32:35 -08004428/*
4429 * Accept the work and start compiling. Returns true if compilation
4430 * is attempted.
4431 */
Bill Buzbee716f1202009-07-23 13:22:09 -07004432bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004433{
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004434 JitTraceDescription *desc;
buzbee2e152ba2010-12-15 16:32:35 -08004435 bool isCompile;
4436 bool success = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004437
Ben Cheng6999d842010-01-26 16:46:15 -08004438 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004439 return false;
4440 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004441
Ben Chengccd6c012009-10-15 14:52:45 -07004442 switch (work->kind) {
Ben Chengccd6c012009-10-15 14:52:45 -07004443 case kWorkOrderTrace:
buzbee2e152ba2010-12-15 16:32:35 -08004444 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004445 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004446 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004447 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4448 work->bailPtr, 0 /* no hints */);
Ben Chengccd6c012009-10-15 14:52:45 -07004449 break;
4450 case kWorkOrderTraceDebug: {
4451 bool oldPrintMe = gDvmJit.printMe;
4452 gDvmJit.printMe = true;
buzbee2e152ba2010-12-15 16:32:35 -08004453 isCompile = true;
Ben Chengccd6c012009-10-15 14:52:45 -07004454 /* Start compilation with maximally allowed trace length */
Carl Shapirofc75f3e2010-12-07 11:43:38 -08004455 desc = (JitTraceDescription *)work->info;
buzbee2e152ba2010-12-15 16:32:35 -08004456 success = dvmCompileTrace(desc, JIT_MAX_TRACE_LEN, &work->result,
4457 work->bailPtr, 0 /* no hints */);
Elliott Hughes672511b2010-04-26 17:40:13 -07004458 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004459 break;
4460 }
buzbee2e152ba2010-12-15 16:32:35 -08004461 case kWorkOrderProfileMode:
4462 dvmJitChangeProfileMode((TraceProfilingModes)work->info);
4463 isCompile = false;
4464 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004465 default:
buzbee2e152ba2010-12-15 16:32:35 -08004466 isCompile = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004467 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004468 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004469 }
buzbee2e152ba2010-12-15 16:32:35 -08004470 if (!success)
4471 work->result.codeAddress = NULL;
4472 return isCompile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004473}
4474
Ben Chengba4fc8b2009-06-01 13:00:29 -07004475/* Architectural-specific debugging helpers go here */
4476void dvmCompilerArchDump(void)
4477{
4478 /* Print compiled opcode in this VM instance */
4479 int i, start, streak;
4480 char buf[1024];
4481
4482 streak = i = 0;
4483 buf[0] = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004484 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004485 i++;
4486 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004487 if (i == kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004488 return;
4489 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004490 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004491 if (opcodeCoverage[i]) {
4492 streak++;
4493 } else {
4494 if (streak == 1) {
4495 sprintf(buf+strlen(buf), "%x,", start);
4496 } else {
4497 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4498 }
4499 streak = 0;
Dan Bornsteinccaab182010-12-03 15:32:40 -08004500 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004501 i++;
4502 }
Dan Bornsteinccaab182010-12-03 15:32:40 -08004503 if (i < kNumPackedOpcodes) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004504 streak = 1;
4505 start = i;
4506 }
4507 }
4508 }
4509 if (streak) {
4510 if (streak == 1) {
4511 sprintf(buf+strlen(buf), "%x", start);
4512 } else {
4513 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4514 }
4515 }
4516 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004517 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004518 }
4519}
Ben Chengd7d426a2009-09-22 11:23:36 -07004520
4521/* Common initialization routine for an architecture family */
4522bool dvmCompilerArchInit()
4523{
4524 int i;
4525
Bill Buzbee1465db52009-09-23 17:17:35 -07004526 for (i = 0; i < kArmLast; i++) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004527 if (EncodingMap[i].opcode != i) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004528 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
Dan Bornstein9a1f8162010-12-01 17:02:26 -08004529 EncodingMap[i].name, i, EncodingMap[i].opcode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004530 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004531 }
4532 }
4533
Ben Cheng5d90c202009-11-22 23:31:11 -08004534 return dvmCompilerArchVariantInit();
4535}
4536
4537void *dvmCompilerGetInterpretTemplate()
4538{
4539 return (void*) ((int)gDvmJit.codeCache +
4540 templateEntryOffsets[TEMPLATE_INTERPRET]);
4541}
4542
Bill Buzbee1b3da592011-02-03 07:38:22 -08004543JitInstructionSetType dvmCompilerGetInterpretTemplateSet()
4544{
4545 return DALVIK_JIT_ARM;
4546}
4547
buzbeebff121a2010-08-04 15:25:06 -07004548/* Needed by the Assembler */
4549void dvmCompilerSetupResourceMasks(ArmLIR *lir)
4550{
4551 setupResourceMasks(lir);
4552}
4553
Ben Cheng5d90c202009-11-22 23:31:11 -08004554/* Needed by the ld/st optmizatons */
4555ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4556{
4557 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4558}
4559
4560/* Needed by the register allocator */
4561ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4562{
4563 return genRegCopy(cUnit, rDest, rSrc);
4564}
4565
4566/* Needed by the register allocator */
4567void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4568 int srcLo, int srcHi)
4569{
4570 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4571}
4572
4573void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4574 int displacement, int rSrc, OpSize size)
4575{
4576 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4577}
4578
4579void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4580 int displacement, int rSrcLo, int rSrcHi)
4581{
4582 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004583}