blob: 79019a04412539e4b329a6e5a7fb7c832e4ce618 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
buzbee919eb062010-07-12 12:59:22 -070027/*
28 * Mark garbage collection card. Skip if the value we're storing is null.
29 */
30static void markCard(CompilationUnit *cUnit, int valReg, int tgtAddrReg)
31{
32 int regCardBase = dvmCompilerAllocTemp(cUnit);
33 int regCardNo = dvmCompilerAllocTemp(cUnit);
34 opRegImm(cUnit, kOpCmp, valReg, 0); /* storing null? */
35 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
36 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, cardTable),
37 regCardBase);
38 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
39 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
40 kUnsignedByte);
41 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
42 target->defMask = ENCODE_ALL;
43 branchOver->generic.target = (LIR *)target;
buzbeebaf196a2010-08-04 10:13:15 -070044 dvmCompilerFreeTemp(cUnit, regCardBase);
45 dvmCompilerFreeTemp(cUnit, regCardNo);
buzbee919eb062010-07-12 12:59:22 -070046}
47
Ben Cheng5d90c202009-11-22 23:31:11 -080048static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
49 int srcSize, int tgtSize)
50{
51 /*
52 * Don't optimize the register usage since it calls out to template
53 * functions
54 */
55 RegLocation rlSrc;
56 RegLocation rlDest;
Bill Buzbeec6f10662010-02-09 11:16:15 -080057 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -080058 if (srcSize == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -080059 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Ben Cheng5d90c202009-11-22 23:31:11 -080060 loadValueDirectFixed(cUnit, rlSrc, r0);
61 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -080062 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Ben Cheng5d90c202009-11-22 23:31:11 -080063 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
64 }
Ben Chengbd1326d2010-04-02 15:04:53 -070065 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -080066 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -080067 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080068 if (tgtSize == 1) {
69 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080070 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
71 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080072 storeValue(cUnit, rlDest, rlResult);
73 } else {
74 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -080075 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
76 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -080077 storeValueWide(cUnit, rlDest, rlResult);
78 }
79 return false;
80}
Ben Chengba4fc8b2009-06-01 13:00:29 -070081
Ben Cheng5d90c202009-11-22 23:31:11 -080082static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
83 RegLocation rlDest, RegLocation rlSrc1,
84 RegLocation rlSrc2)
85{
86 RegLocation rlResult;
87 void* funct;
88
Ben Cheng5d90c202009-11-22 23:31:11 -080089 switch (mir->dalvikInsn.opCode) {
90 case OP_ADD_FLOAT_2ADDR:
91 case OP_ADD_FLOAT:
92 funct = (void*) __aeabi_fadd;
93 break;
94 case OP_SUB_FLOAT_2ADDR:
95 case OP_SUB_FLOAT:
96 funct = (void*) __aeabi_fsub;
97 break;
98 case OP_DIV_FLOAT_2ADDR:
99 case OP_DIV_FLOAT:
100 funct = (void*) __aeabi_fdiv;
101 break;
102 case OP_MUL_FLOAT_2ADDR:
103 case OP_MUL_FLOAT:
104 funct = (void*) __aeabi_fmul;
105 break;
106 case OP_REM_FLOAT_2ADDR:
107 case OP_REM_FLOAT:
108 funct = (void*) fmodf;
109 break;
110 case OP_NEG_FLOAT: {
111 genNegFloat(cUnit, rlDest, rlSrc1);
112 return false;
113 }
114 default:
115 return true;
116 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800117 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Cheng5d90c202009-11-22 23:31:11 -0800118 loadValueDirectFixed(cUnit, rlSrc1, r0);
119 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700120 LOAD_FUNC_ADDR(cUnit, r2, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800121 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800122 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800123 rlResult = dvmCompilerGetReturn(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800124 storeValue(cUnit, rlDest, rlResult);
125 return false;
126}
127
128static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
129 RegLocation rlDest, RegLocation rlSrc1,
130 RegLocation rlSrc2)
131{
132 RegLocation rlResult;
133 void* funct;
134
Ben Cheng5d90c202009-11-22 23:31:11 -0800135 switch (mir->dalvikInsn.opCode) {
136 case OP_ADD_DOUBLE_2ADDR:
137 case OP_ADD_DOUBLE:
138 funct = (void*) __aeabi_dadd;
139 break;
140 case OP_SUB_DOUBLE_2ADDR:
141 case OP_SUB_DOUBLE:
142 funct = (void*) __aeabi_dsub;
143 break;
144 case OP_DIV_DOUBLE_2ADDR:
145 case OP_DIV_DOUBLE:
146 funct = (void*) __aeabi_ddiv;
147 break;
148 case OP_MUL_DOUBLE_2ADDR:
149 case OP_MUL_DOUBLE:
150 funct = (void*) __aeabi_dmul;
151 break;
152 case OP_REM_DOUBLE_2ADDR:
153 case OP_REM_DOUBLE:
154 funct = (void*) fmod;
155 break;
156 case OP_NEG_DOUBLE: {
157 genNegDouble(cUnit, rlDest, rlSrc1);
158 return false;
159 }
160 default:
161 return true;
162 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800163 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Ben Chengbd1326d2010-04-02 15:04:53 -0700164 LOAD_FUNC_ADDR(cUnit, rlr, (int)funct);
Ben Cheng5d90c202009-11-22 23:31:11 -0800165 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
166 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
167 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800168 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800169 rlResult = dvmCompilerGetReturnWide(cUnit);
Ben Cheng5d90c202009-11-22 23:31:11 -0800170 storeValueWide(cUnit, rlDest, rlResult);
171 return false;
172}
173
174static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
175{
176 OpCode opCode = mir->dalvikInsn.opCode;
177
Ben Cheng5d90c202009-11-22 23:31:11 -0800178 switch (opCode) {
179 case OP_INT_TO_FLOAT:
180 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
181 case OP_FLOAT_TO_INT:
182 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
183 case OP_DOUBLE_TO_FLOAT:
184 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
185 case OP_FLOAT_TO_DOUBLE:
186 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
187 case OP_INT_TO_DOUBLE:
188 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
189 case OP_DOUBLE_TO_INT:
190 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
191 case OP_FLOAT_TO_LONG:
192 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
193 case OP_LONG_TO_FLOAT:
194 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
195 case OP_DOUBLE_TO_LONG:
196 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
197 case OP_LONG_TO_DOUBLE:
198 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
199 default:
200 return true;
201 }
202 return false;
203}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700204
Jeff Hao97319a82009-08-12 16:57:15 -0700205#if defined(WITH_SELF_VERIFICATION)
jeffhao9e45c0b2010-02-03 10:24:05 -0800206static void selfVerificationBranchInsert(LIR *currentLIR, ArmOpCode opCode,
207 int dest, int src1)
Jeff Hao97319a82009-08-12 16:57:15 -0700208{
jeffhao9e45c0b2010-02-03 10:24:05 -0800209 ArmLIR *insn = dvmCompilerNew(sizeof(ArmLIR), true);
210 insn->opCode = opCode;
211 insn->operands[0] = dest;
212 insn->operands[1] = src1;
213 setupResourceMasks(insn);
214 dvmCompilerInsertLIRBefore(currentLIR, (LIR *) insn);
Jeff Hao97319a82009-08-12 16:57:15 -0700215}
216
jeffhao9e45c0b2010-02-03 10:24:05 -0800217static void selfVerificationBranchInsertPass(CompilationUnit *cUnit)
Jeff Hao97319a82009-08-12 16:57:15 -0700218{
jeffhao9e45c0b2010-02-03 10:24:05 -0800219 ArmLIR *thisLIR;
jeffhao9e45c0b2010-02-03 10:24:05 -0800220 TemplateOpCode opCode = TEMPLATE_MEM_OP_DECODE;
Jeff Hao97319a82009-08-12 16:57:15 -0700221
jeffhao9e45c0b2010-02-03 10:24:05 -0800222 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
223 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
224 thisLIR = NEXT_LIR(thisLIR)) {
225 if (thisLIR->branchInsertSV) {
226 /* Branch to mem op decode template */
227 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx1,
228 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
229 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
230 selfVerificationBranchInsert((LIR *) thisLIR, kThumbBlx2,
231 (int) gDvmJit.codeCache + templateEntryOffsets[opCode],
232 (int) gDvmJit.codeCache + templateEntryOffsets[opCode]);
Jeff Hao97319a82009-08-12 16:57:15 -0700233 }
234 }
Jeff Hao97319a82009-08-12 16:57:15 -0700235}
Jeff Hao97319a82009-08-12 16:57:15 -0700236#endif
237
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800238/* Generate conditional branch instructions */
239static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
240 ArmConditionCode cond,
241 ArmLIR *target)
242{
243 ArmLIR *branch = opCondBranch(cUnit, cond);
244 branch->generic.target = (LIR *) target;
245 return branch;
246}
247
Ben Chengba4fc8b2009-06-01 13:00:29 -0700248/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700249static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
250 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700251{
Bill Buzbee1465db52009-09-23 17:17:35 -0700252 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700253 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
254}
255
256/* Load a wide field from an object instance */
257static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
258{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800259 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
260 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700261 RegLocation rlResult;
262 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800263 int regPtr = dvmCompilerAllocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700264
Bill Buzbee1465db52009-09-23 17:17:35 -0700265 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700266
Bill Buzbee1465db52009-09-23 17:17:35 -0700267 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
268 NULL);/* null object? */
269 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800270 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700271
272 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700273 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700274 HEAP_ACCESS_SHADOW(false);
275
Bill Buzbeec6f10662010-02-09 11:16:15 -0800276 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700277 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700278}
279
280/* Store a wide field to an object instance */
281static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
282{
Bill Buzbeec6f10662010-02-09 11:16:15 -0800283 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
284 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700285 rlObj = loadValue(cUnit, rlObj, kCoreReg);
286 int regPtr;
287 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
288 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
289 NULL);/* null object? */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800290 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700291 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -0700292
293 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700294 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700295 HEAP_ACCESS_SHADOW(false);
296
Bill Buzbeec6f10662010-02-09 11:16:15 -0800297 dvmCompilerFreeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700298}
299
300/*
301 * Load a field from an object instance
302 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700303 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700304static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700305 int fieldOffset, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700306{
Bill Buzbee1465db52009-09-23 17:17:35 -0700307 RegLocation rlResult;
Bill Buzbee749e8162010-07-07 06:55:56 -0700308 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800309 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
310 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700311 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700312 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700313 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
314 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700315
316 HEAP_ACCESS_SHADOW(true);
Ben Cheng5d90c202009-11-22 23:31:11 -0800317 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
318 size, rlObj.sRegLow);
Ben Cheng11d8f142010-03-24 15:24:19 -0700319 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -0700320 if (isVolatile) {
321 dvmCompilerGenMemBarrier(cUnit);
322 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700323
Bill Buzbee1465db52009-09-23 17:17:35 -0700324 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325}
326
327/*
328 * Store a field to an object instance
329 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700330 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700331static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
buzbeeecf8f6e2010-07-20 14:53:42 -0700332 int fieldOffset, bool isObject, bool isVolatile)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700333{
Bill Buzbee749e8162010-07-07 06:55:56 -0700334 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800335 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
336 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700337 rlObj = loadValue(cUnit, rlObj, kCoreReg);
Bill Buzbee749e8162010-07-07 06:55:56 -0700338 rlSrc = loadValue(cUnit, rlSrc, regClass);
Bill Buzbee1465db52009-09-23 17:17:35 -0700339 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
340 NULL);/* null object? */
Ben Cheng11d8f142010-03-24 15:24:19 -0700341
buzbeeecf8f6e2010-07-20 14:53:42 -0700342 if (isVolatile) {
343 dvmCompilerGenMemBarrier(cUnit);
344 }
Ben Cheng11d8f142010-03-24 15:24:19 -0700345 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700346 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700347 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700348 if (isObject) {
349 /* NOTE: marking card based on object head */
350 markCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
351 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700352}
353
354
Ben Chengba4fc8b2009-06-01 13:00:29 -0700355/*
356 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700357 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700358static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700359 RegLocation rlArray, RegLocation rlIndex,
360 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700361{
Bill Buzbee749e8162010-07-07 06:55:56 -0700362 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700363 int lenOffset = offsetof(ArrayObject, length);
364 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700365 RegLocation rlResult;
366 rlArray = loadValue(cUnit, rlArray, kCoreReg);
367 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
368 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700369
370 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700371 ArmLIR * pcrLabel = NULL;
372
373 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700374 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
375 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700376 }
377
Bill Buzbeec6f10662010-02-09 11:16:15 -0800378 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700379
Ben Cheng4238ec22009-08-24 16:32:22 -0700380 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800381 int regLen = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700382 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700383 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
384 /* regPtr -> array data */
385 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
386 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
387 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800388 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700389 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700390 /* regPtr -> array data */
391 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700392 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700393 if ((size == kLong) || (size == kDouble)) {
394 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800395 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700396 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
397 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800398 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700399 } else {
400 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
401 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700402 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700403
404 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700405 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700406 HEAP_ACCESS_SHADOW(false);
407
Bill Buzbeec6f10662010-02-09 11:16:15 -0800408 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700409 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700410 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700411 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, regClass, true);
Ben Cheng11d8f142010-03-24 15:24:19 -0700412
413 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700414 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
415 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700416 HEAP_ACCESS_SHADOW(false);
417
Bill Buzbeec6f10662010-02-09 11:16:15 -0800418 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee1465db52009-09-23 17:17:35 -0700419 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700420 }
421}
422
Ben Chengba4fc8b2009-06-01 13:00:29 -0700423/*
424 * Generate array store
425 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700426 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700427static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700428 RegLocation rlArray, RegLocation rlIndex,
429 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700430{
Bill Buzbee749e8162010-07-07 06:55:56 -0700431 RegisterClass regClass = dvmCompilerRegClassBySize(size);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700432 int lenOffset = offsetof(ArrayObject, length);
433 int dataOffset = offsetof(ArrayObject, contents);
434
Bill Buzbee1465db52009-09-23 17:17:35 -0700435 int regPtr;
436 rlArray = loadValue(cUnit, rlArray, kCoreReg);
437 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700438
Bill Buzbeec6f10662010-02-09 11:16:15 -0800439 if (dvmCompilerIsTemp(cUnit, rlArray.lowReg)) {
440 dvmCompilerClobber(cUnit, rlArray.lowReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700441 regPtr = rlArray.lowReg;
442 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800443 regPtr = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700444 genRegCopy(cUnit, regPtr, rlArray.lowReg);
445 }
Ben Chenge9695e52009-06-16 16:11:47 -0700446
Ben Cheng1efc9c52009-06-08 18:25:27 -0700447 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700448 ArmLIR * pcrLabel = NULL;
449
450 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700451 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
452 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700453 }
454
455 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800456 int regLen = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700457 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700458 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700459 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
460 /* regPtr -> array data */
461 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
462 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
463 pcrLabel);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800464 dvmCompilerFreeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700465 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700466 /* regPtr -> array data */
467 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700468 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700469 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee1465db52009-09-23 17:17:35 -0700470 if ((size == kLong) || (size == kDouble)) {
471 //TODO: need specific wide routine that can handle fp regs
472 if (scale) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800473 int rNewIndex = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700474 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
475 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800476 dvmCompilerFreeTemp(cUnit, rNewIndex);
Bill Buzbee1465db52009-09-23 17:17:35 -0700477 } else {
478 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
479 }
Bill Buzbee749e8162010-07-07 06:55:56 -0700480 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700481
482 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700483 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -0700484 HEAP_ACCESS_SHADOW(false);
485
Bill Buzbeec6f10662010-02-09 11:16:15 -0800486 dvmCompilerFreeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700487 } else {
Bill Buzbee749e8162010-07-07 06:55:56 -0700488 rlSrc = loadValue(cUnit, rlSrc, regClass);
Ben Cheng11d8f142010-03-24 15:24:19 -0700489
490 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700491 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
492 scale, size);
Ben Cheng11d8f142010-03-24 15:24:19 -0700493 HEAP_ACCESS_SHADOW(false);
jeffhao9e45c0b2010-02-03 10:24:05 -0800494 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700495}
496
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800497/*
498 * Generate array object store
499 * Must use explicit register allocation here because of
500 * call-out to dvmCanPutArrayElement
501 */
502static void genArrayObjectPut(CompilationUnit *cUnit, MIR *mir,
503 RegLocation rlArray, RegLocation rlIndex,
504 RegLocation rlSrc, int scale)
505{
506 int lenOffset = offsetof(ArrayObject, length);
507 int dataOffset = offsetof(ArrayObject, contents);
508
509 dvmCompilerFlushAllRegs(cUnit);
510
511 int regLen = r0;
512 int regPtr = r4PC; /* Preserved across call */
513 int regArray = r1;
514 int regIndex = r7; /* Preserved across call */
515
516 loadValueDirectFixed(cUnit, rlArray, regArray);
517 loadValueDirectFixed(cUnit, rlIndex, regIndex);
518
519 /* null object? */
520 ArmLIR * pcrLabel = NULL;
521
522 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
523 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, regArray,
524 mir->offset, NULL);
525 }
526
527 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
528 /* Get len */
529 loadWordDisp(cUnit, regArray, lenOffset, regLen);
530 /* regPtr -> array data */
531 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
532 genBoundsCheck(cUnit, regIndex, regLen, mir->offset,
533 pcrLabel);
534 } else {
535 /* regPtr -> array data */
536 opRegRegImm(cUnit, kOpAdd, regPtr, regArray, dataOffset);
537 }
538
539 /* Get object to store */
540 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -0700541 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmCanPutArrayElement);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800542
543 /* Are we storing null? If so, avoid check */
544 opRegImm(cUnit, kOpCmp, r0, 0);
545 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
546
547 /* Make sure the types are compatible */
548 loadWordDisp(cUnit, regArray, offsetof(Object, clazz), r1);
549 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r0);
550 opReg(cUnit, kOpBlx, r2);
551 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700552
553 /*
554 * Using fixed registers here, and counting on r4 and r7 being
555 * preserved across the above call. Tell the register allocation
556 * utilities about the regs we are using directly
557 */
558 dvmCompilerLockTemp(cUnit, regPtr); // r4PC
559 dvmCompilerLockTemp(cUnit, regIndex); // r7
560 dvmCompilerLockTemp(cUnit, r0);
buzbee919eb062010-07-12 12:59:22 -0700561 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee900a3af2010-03-16 12:41:43 -0700562
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800563 /* Bad? - roll back and re-execute if so */
564 genRegImmCheck(cUnit, kArmCondEq, r0, 0, mir->offset, pcrLabel);
565
buzbee919eb062010-07-12 12:59:22 -0700566 /* Resume here - must reload element & array, regPtr & index preserved */
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800567 loadValueDirectFixed(cUnit, rlSrc, r0);
buzbee919eb062010-07-12 12:59:22 -0700568 loadValueDirectFixed(cUnit, rlArray, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800569
570 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
571 target->defMask = ENCODE_ALL;
572 branchOver->generic.target = (LIR *) target;
573
Ben Cheng11d8f142010-03-24 15:24:19 -0700574 HEAP_ACCESS_SHADOW(true);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800575 storeBaseIndexed(cUnit, regPtr, regIndex, r0,
576 scale, kWord);
Ben Cheng11d8f142010-03-24 15:24:19 -0700577 HEAP_ACCESS_SHADOW(false);
buzbee919eb062010-07-12 12:59:22 -0700578
buzbeebaf196a2010-08-04 10:13:15 -0700579 dvmCompilerFreeTemp(cUnit, regPtr);
580 dvmCompilerFreeTemp(cUnit, regIndex);
581
buzbee919eb062010-07-12 12:59:22 -0700582 /* NOTE: marking card here based on object head */
583 markCard(cUnit, r0, r1);
Bill Buzbeebe6534f2010-03-12 16:01:35 -0800584}
585
Ben Cheng5d90c202009-11-22 23:31:11 -0800586static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
587 RegLocation rlDest, RegLocation rlSrc1,
588 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700589{
Ben Chenge9695e52009-06-16 16:11:47 -0700590 /*
591 * Don't mess with the regsiters here as there is a particular calling
592 * convention to the out-of-line handler.
593 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700594 RegLocation rlResult;
595
596 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
597 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700598 switch( mir->dalvikInsn.opCode) {
599 case OP_SHL_LONG:
600 case OP_SHL_LONG_2ADDR:
601 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
602 break;
603 case OP_SHR_LONG:
604 case OP_SHR_LONG_2ADDR:
605 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
606 break;
607 case OP_USHR_LONG:
608 case OP_USHR_LONG_2ADDR:
609 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
610 break;
611 default:
612 return true;
613 }
Bill Buzbeec6f10662010-02-09 11:16:15 -0800614 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700615 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700616 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700617}
Ben Chenge9695e52009-06-16 16:11:47 -0700618
Ben Cheng5d90c202009-11-22 23:31:11 -0800619static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
620 RegLocation rlDest, RegLocation rlSrc1,
621 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700622{
Bill Buzbee1465db52009-09-23 17:17:35 -0700623 RegLocation rlResult;
624 OpKind firstOp = kOpBkpt;
625 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700626 bool callOut = false;
627 void *callTgt;
628 int retReg = r0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700629
630 switch (mir->dalvikInsn.opCode) {
631 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -0700632 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800633 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700634 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
635 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
636 storeValueWide(cUnit, rlDest, rlResult);
637 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700638 break;
639 case OP_ADD_LONG:
640 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700641 firstOp = kOpAdd;
642 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700643 break;
644 case OP_SUB_LONG:
645 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700646 firstOp = kOpSub;
647 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700648 break;
649 case OP_MUL_LONG:
650 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700651 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700652 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700653 case OP_DIV_LONG:
654 case OP_DIV_LONG_2ADDR:
655 callOut = true;
656 retReg = r0;
657 callTgt = (void*)__aeabi_ldivmod;
658 break;
659 /* NOTE - result is in r2/r3 instead of r0/r1 */
660 case OP_REM_LONG:
661 case OP_REM_LONG_2ADDR:
662 callOut = true;
663 callTgt = (void*)__aeabi_ldivmod;
664 retReg = r2;
665 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700666 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700667 case OP_AND_LONG:
668 firstOp = kOpAnd;
669 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700670 break;
671 case OP_OR_LONG:
672 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 firstOp = kOpOr;
674 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700675 break;
676 case OP_XOR_LONG:
677 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700678 firstOp = kOpXor;
679 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700680 break;
Ben Chenge9695e52009-06-16 16:11:47 -0700681 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -0800682 //TUNING: can improve this using Thumb2 code
Bill Buzbeec6f10662010-02-09 11:16:15 -0800683 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700684 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800685 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -0700686 loadConstantNoClobber(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700687 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -0800688 tReg, rlSrc2.lowReg);
689 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
690 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -0700691 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 return false;
Ben Chenge9695e52009-06-16 16:11:47 -0700693 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700694 default:
695 LOGE("Invalid long arith op");
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800696 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700697 }
698 if (!callOut) {
Bill Buzbee80cef862010-03-25 10:38:34 -0700699 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700700 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700701 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeec6f10662010-02-09 11:16:15 -0800702 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700703 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700704 LOAD_FUNC_ADDR(cUnit, rlr, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700705 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
706 opReg(cUnit, kOpBlx, rlr);
Elliott Hughes6a555132010-02-25 15:41:42 -0800707 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700708 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800709 rlResult = dvmCompilerGetReturnWide(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700710 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800711 rlResult = dvmCompilerGetReturnWideAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700712 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700713 }
714 return false;
715}
716
Ben Cheng5d90c202009-11-22 23:31:11 -0800717static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
718 RegLocation rlDest, RegLocation rlSrc1,
719 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700720{
Bill Buzbee1465db52009-09-23 17:17:35 -0700721 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700722 bool callOut = false;
723 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -0700724 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700725 int retReg = r0;
726 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -0700727 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -0800728 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700729
Ben Chengba4fc8b2009-06-01 13:00:29 -0700730 switch (mir->dalvikInsn.opCode) {
731 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700732 op = kOpNeg;
733 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700734 break;
735 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -0700736 op = kOpMvn;
737 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700738 break;
739 case OP_ADD_INT:
740 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700741 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700742 break;
743 case OP_SUB_INT:
744 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700745 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700746 break;
747 case OP_MUL_INT:
748 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700749 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700750 break;
751 case OP_DIV_INT:
752 case OP_DIV_INT_2ADDR:
753 callOut = true;
754 checkZero = true;
755 callTgt = __aeabi_idiv;
756 retReg = r0;
757 break;
758 /* NOTE: returns in r1 */
759 case OP_REM_INT:
760 case OP_REM_INT_2ADDR:
761 callOut = true;
762 checkZero = true;
763 callTgt = __aeabi_idivmod;
764 retReg = r1;
765 break;
766 case OP_AND_INT:
767 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700768 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700769 break;
770 case OP_OR_INT:
771 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700772 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700773 break;
774 case OP_XOR_INT:
775 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -0700776 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700777 break;
778 case OP_SHL_INT:
779 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800780 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700781 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700782 break;
783 case OP_SHR_INT:
784 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800785 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700786 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700787 break;
788 case OP_USHR_INT:
789 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -0800790 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -0700791 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 break;
793 default:
794 LOGE("Invalid word arith op: 0x%x(%d)",
795 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -0800796 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700797 }
798 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700799 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
800 if (unary) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800801 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700802 opRegReg(cUnit, op, rlResult.lowReg,
803 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700804 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700805 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800806 if (shiftOp) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800807 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee0e605272009-12-01 14:28:05 -0800808 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800809 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800810 opRegRegReg(cUnit, op, rlResult.lowReg,
811 rlSrc1.lowReg, tReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800812 dvmCompilerFreeTemp(cUnit, tReg);
Bill Buzbee0e605272009-12-01 14:28:05 -0800813 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800814 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee0e605272009-12-01 14:28:05 -0800815 opRegRegReg(cUnit, op, rlResult.lowReg,
816 rlSrc1.lowReg, rlSrc2.lowReg);
817 }
Ben Chenge9695e52009-06-16 16:11:47 -0700818 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700819 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700820 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 RegLocation rlResult;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800822 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -0700823 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengbd1326d2010-04-02 15:04:53 -0700824 LOAD_FUNC_ADDR(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -0700825 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700826 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700827 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700828 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700829 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -0800830 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700831 if (retReg == r0)
Bill Buzbeec6f10662010-02-09 11:16:15 -0800832 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700833 else
Bill Buzbeec6f10662010-02-09 11:16:15 -0800834 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700835 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700836 }
837 return false;
838}
839
Ben Cheng5d90c202009-11-22 23:31:11 -0800840static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700841{
842 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -0700843 RegLocation rlDest;
844 RegLocation rlSrc1;
845 RegLocation rlSrc2;
846 /* Deduce sizes of operands */
847 if (mir->ssaRep->numUses == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800848 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
849 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700850 } else if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800851 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
852 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -0700853 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800854 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
855 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -0700856 assert(mir->ssaRep->numUses == 4);
857 }
858 if (mir->ssaRep->numDefs == 1) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800859 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -0700860 } else {
861 assert(mir->ssaRep->numDefs == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -0800862 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -0700863 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700864
865 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800866 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700867 }
868 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800869 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700870 }
871 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800872 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700873 }
874 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800875 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700876 }
877 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800878 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700879 }
880 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800881 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700882 }
883 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800884 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700885 }
886 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800887 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700888 }
889 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800890 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700891 }
892 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -0800893 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700894 }
895 return true;
896}
897
Bill Buzbee1465db52009-09-23 17:17:35 -0700898/* Generate unconditional branch instructions */
899static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
900{
901 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
902 branch->generic.target = (LIR *) target;
903 return branch;
904}
905
Bill Buzbee1465db52009-09-23 17:17:35 -0700906/* Perform the actual operation for OP_RETURN_* */
907static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
908{
909 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
Ben Cheng978738d2010-05-13 13:45:57 -0700910#if defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -0700911 gDvmJit.returnOp++;
912#endif
913 int dPC = (int) (cUnit->method->insns + mir->offset);
914 /* Insert branch, but defer setting of target */
915 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
916 /* Set up the place holder to reconstruct this Dalvik PC */
917 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -0700918 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Bill Buzbee1465db52009-09-23 17:17:35 -0700919 pcrLabel->operands[0] = dPC;
920 pcrLabel->operands[1] = mir->offset;
921 /* Insert the place holder to the growable list */
922 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
923 /* Branch to the PC reconstruction code */
924 branch->generic.target = (LIR *) pcrLabel;
925}
926
Ben Chengba4fc8b2009-06-01 13:00:29 -0700927static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
928 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700929 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700930{
931 unsigned int i;
932 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -0700933 RegLocation rlArg;
934 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700935
Bill Buzbee1465db52009-09-23 17:17:35 -0700936 /*
937 * Load arguments to r0..r4. Note that these registers may contain
938 * live values, so we clobber them immediately after loading to prevent
939 * them from being used as sources for subsequent loads.
940 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800941 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 for (i = 0; i < dInsn->vA; i++) {
943 regMask |= 1 << i;
Bill Buzbeec6f10662010-02-09 11:16:15 -0800944 rlArg = dvmCompilerGetSrc(cUnit, mir, numDone++);
Bill Buzbee1465db52009-09-23 17:17:35 -0700945 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700946 }
947 if (regMask) {
948 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -0700949 opRegRegImm(cUnit, kOpSub, r7, rFP,
950 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700951 /* generate null check */
952 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800953 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700954 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700955 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700956 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700957 }
958}
959
960static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
961 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700962 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700963{
964 int srcOffset = dInsn->vC << 2;
965 int numArgs = dInsn->vA;
966 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -0700967
968 /*
969 * Note: here, all promoted registers will have been flushed
970 * back to the Dalvik base locations, so register usage restrictins
971 * are lifted. All parms loaded from original Dalvik register
972 * region - even though some might conceivably have valid copies
973 * cached in a preserved register.
974 */
Bill Buzbeec6f10662010-02-09 11:16:15 -0800975 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -0700976
Ben Chengba4fc8b2009-06-01 13:00:29 -0700977 /*
978 * r4PC : &rFP[vC]
979 * r7: &newFP[0]
980 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700981 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700982 /* load [r0 .. min(numArgs,4)] */
983 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -0700984 /*
985 * Protect the loadMultiple instruction from being reordered with other
986 * Dalvik stack accesses.
987 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700988 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989
Bill Buzbee1465db52009-09-23 17:17:35 -0700990 opRegRegImm(cUnit, kOpSub, r7, rFP,
991 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700992 /* generate null check */
993 if (pcrLabel) {
Bill Buzbeec6f10662010-02-09 11:16:15 -0800994 *pcrLabel = genNullCheck(cUnit, dvmCompilerSSASrc(mir, 0), r0,
Bill Buzbee1465db52009-09-23 17:17:35 -0700995 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700996 }
997
998 /*
999 * Handle remaining 4n arguments:
1000 * store previously loaded 4 values and load the next 4 values
1001 */
1002 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001003 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001004 /*
1005 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001006 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001008 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001009 /* No need to generate the loop structure if numArgs <= 11 */
1010 if (numArgs > 11) {
1011 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001012 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001013 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001014 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001015 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001016 /*
1017 * Protect the loadMultiple instruction from being reordered with other
1018 * Dalvik stack accesses.
1019 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001020 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001021 /* No need to generate the loop structure if numArgs <= 11 */
1022 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001023 opRegImm(cUnit, kOpSub, rFP, 4);
1024 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001025 }
1026 }
1027
1028 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001029 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001030
1031 /* Generate the loop epilogue - don't use r0 */
1032 if ((numArgs > 4) && (numArgs % 4)) {
1033 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001034 /*
1035 * Protect the loadMultiple instruction from being reordered with other
1036 * Dalvik stack accesses.
1037 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001038 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039 }
1040 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001041 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042
1043 /* Save the modulo 4 arguments */
1044 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001045 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 }
1047}
1048
Ben Cheng38329f52009-07-07 14:19:20 -07001049/*
1050 * Generate code to setup the call stack then jump to the chaining cell if it
1051 * is not a native method.
1052 */
1053static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001054 BasicBlock *bb, ArmLIR *labelList,
1055 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001056 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001057{
Bill Buzbee1465db52009-09-23 17:17:35 -07001058 /*
1059 * Note: all Dalvik register state should be flushed to
1060 * memory by the point, so register usage restrictions no
1061 * longer apply. All temp & preserved registers may be used.
1062 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001063 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001064 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001065
1066 /* r1 = &retChainingCell */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001067 dvmCompilerLockTemp(cUnit, r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001068 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001069 /* r4PC = dalvikCallsite */
1070 loadConstant(cUnit, r4PC,
1071 (int) (cUnit->method->insns + mir->offset));
1072 addrRetChain->generic.target = (LIR *) retChainingCell;
1073 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001074 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001075 * r1 = &ChainingCell
1076 * r4PC = callsiteDPC
1077 */
1078 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001079 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Cheng978738d2010-05-13 13:45:57 -07001080#if defined(WITH_JIT_TUNING)
Ben Cheng38329f52009-07-07 14:19:20 -07001081 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001082#endif
1083 } else {
1084 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
Ben Cheng978738d2010-05-13 13:45:57 -07001085#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001086 gDvmJit.invokeMonomorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001087#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001088 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1090 }
1091 /* Handle exceptions using the interpreter */
1092 genTrap(cUnit, mir->offset, pcrLabel);
1093}
1094
Ben Cheng38329f52009-07-07 14:19:20 -07001095/*
1096 * Generate code to check the validity of a predicted chain and take actions
1097 * based on the result.
1098 *
1099 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1100 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1101 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1102 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1103 * 0x426a99b2 : blx_2 see above --+
1104 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1105 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1106 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1107 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1108 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1109 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1110 * 0x426a99c0 : blx r7 --+
1111 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1112 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1113 * 0x426a99c6 : blx_2 see above --+
1114 */
1115static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1116 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001117 ArmLIR *retChainingCell,
1118 ArmLIR *predChainingCell,
1119 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001120{
Bill Buzbee1465db52009-09-23 17:17:35 -07001121 /*
1122 * Note: all Dalvik register state should be flushed to
1123 * memory by the point, so register usage restrictions no
1124 * longer apply. Lock temps to prevent them from being
1125 * allocated by utility routines.
1126 */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001127 dvmCompilerLockAllTemps(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001128
Ben Cheng38329f52009-07-07 14:19:20 -07001129 /* "this" is already left in r0 by genProcessArgs* */
1130
1131 /* r4PC = dalvikCallsite */
1132 loadConstant(cUnit, r4PC,
1133 (int) (cUnit->method->insns + mir->offset));
1134
1135 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001136 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001137 addrRetChain->generic.target = (LIR *) retChainingCell;
1138
1139 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001140 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001141 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1142
1143 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1144
1145 /* return through lr - jump to the chaining cell */
1146 genUnconditionalBranch(cUnit, predChainingCell);
1147
1148 /*
1149 * null-check on "this" may have been eliminated, but we still need a PC-
1150 * reconstruction label for stack overflow bailout.
1151 */
1152 if (pcrLabel == NULL) {
1153 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001154 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07001155 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07001156 pcrLabel->operands[0] = dPC;
1157 pcrLabel->operands[1] = mir->offset;
1158 /* Insert the place holder to the growable list */
1159 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1160 }
1161
1162 /* return through lr+2 - punt to the interpreter */
1163 genUnconditionalBranch(cUnit, pcrLabel);
1164
1165 /*
1166 * return through lr+4 - fully resolve the callee method.
1167 * r1 <- count
1168 * r2 <- &predictedChainCell
1169 * r3 <- this->class
1170 * r4 <- dPC
1171 * r7 <- this->class->vtable
1172 */
1173
1174 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001175 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001176
1177 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001178 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001179
Bill Buzbee1465db52009-09-23 17:17:35 -07001180 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001181
Bill Buzbee270c1d62009-08-13 16:58:07 -07001182 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1183 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001184
Ben Chengb88ec3c2010-05-17 12:50:33 -07001185 genRegCopy(cUnit, r1, rGLUE);
1186
Ben Cheng38329f52009-07-07 14:19:20 -07001187 /*
1188 * r0 = calleeMethod
1189 * r2 = &predictedChainingCell
1190 * r3 = class
1191 *
1192 * &returnChainingCell has been loaded into r1 but is not needed
1193 * when patching the chaining cell and will be clobbered upon
1194 * returning so it will be reconstructed again.
1195 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001196 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001197
1198 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001199 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001200 addrRetChain->generic.target = (LIR *) retChainingCell;
1201
1202 bypassRechaining->generic.target = (LIR *) addrRetChain;
1203 /*
1204 * r0 = calleeMethod,
1205 * r1 = &ChainingCell,
1206 * r4PC = callsiteDPC,
1207 */
1208 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07001209#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08001210 gDvmJit.invokePolymorphic++;
Ben Cheng38329f52009-07-07 14:19:20 -07001211#endif
1212 /* Handle exceptions using the interpreter */
1213 genTrap(cUnit, mir->offset, pcrLabel);
1214}
1215
Ben Chengba4fc8b2009-06-01 13:00:29 -07001216/* Geneate a branch to go back to the interpreter */
1217static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1218{
1219 /* r0 = dalvik pc */
Bill Buzbeec6f10662010-02-09 11:16:15 -08001220 dvmCompilerFlushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001221 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001222 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1223 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1224 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001225 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001226}
1227
1228/*
1229 * Attempt to single step one instruction using the interpreter and return
1230 * to the compiled code for the next Dalvik instruction
1231 */
1232static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1233{
1234 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1235 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1236 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001237
Bill Buzbee45273872010-03-11 11:12:15 -08001238 //If already optimized out, just ignore
1239 if (mir->dalvikInsn.opCode == OP_NOP)
1240 return;
1241
Bill Buzbee1465db52009-09-23 17:17:35 -07001242 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
Bill Buzbeec6f10662010-02-09 11:16:15 -08001243 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001244
Ben Chengba4fc8b2009-06-01 13:00:29 -07001245 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1246 genPuntToInterp(cUnit, mir->offset);
1247 return;
1248 }
1249 int entryAddr = offsetof(InterpState,
1250 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001251 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001252 /* r0 = dalvik pc */
1253 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1254 /* r1 = dalvik pc of following instruction */
1255 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001256 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001257}
1258
Ben Chengfc075c22010-05-28 15:20:08 -07001259#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING) || \
1260 defined(_ARMV5TE) || defined(_ARMV5TE_VFP)
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001261/*
1262 * To prevent a thread in a monitor wait from blocking the Jit from
1263 * resetting the code cache, heavyweight monitor lock will not
1264 * be allowed to return to an existing translation. Instead, we will
1265 * handle them by branching to a handler, which will in turn call the
1266 * runtime lock routine and then branch directly back to the
1267 * interpreter main loop. Given the high cost of the heavyweight
1268 * lock operation, this additional cost should be slight (especially when
1269 * considering that we expect the vast majority of lock operations to
1270 * use the fast-path thin lock bypass).
1271 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001272static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001273{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001274 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001275 genExportPC(cUnit, mir);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001276 dvmCompilerFlushAllRegs(cUnit); /* Send everything to home location */
1277 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001278 loadValueDirectFixed(cUnit, rlSrc, r1);
1279 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001280 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001281 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001282 /* Get dPC of next insn */
1283 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1284 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1285#if defined(WITH_DEADLOCK_PREDICTION)
1286 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1287#else
1288 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1289#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001290 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07001291 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001292 /* Do the call */
1293 opReg(cUnit, kOpBlx, r2);
Bill Buzbee6bbdd6b2010-02-16 14:40:01 -08001294 opRegImm(cUnit, kOpCmp, r0, 0); /* Did we throw? */
1295 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
1296 loadConstant(cUnit, r0,
1297 (int) (cUnit->method->insns + mir->offset +
1298 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_EXIT)));
1299 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1300 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1301 target->defMask = ENCODE_ALL;
1302 branchOver->generic.target = (LIR *) target;
Elliott Hughes6a555132010-02-25 15:41:42 -08001303 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001304 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001305}
Ben Chengfc075c22010-05-28 15:20:08 -07001306#endif
Bill Buzbee270c1d62009-08-13 16:58:07 -07001307
Ben Chengba4fc8b2009-06-01 13:00:29 -07001308/*
1309 * The following are the first-level codegen routines that analyze the format
1310 * of each bytecode then either dispatch special purpose codegen routines
1311 * or produce corresponding Thumb instructions directly.
1312 */
1313
1314static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001315 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001316{
1317 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1318 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1319 return false;
1320}
1321
1322static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1323{
1324 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001325 if ((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001326 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1327 return true;
1328 }
1329 switch (dalvikOpCode) {
1330 case OP_RETURN_VOID:
1331 genReturnCommon(cUnit,mir);
1332 break;
1333 case OP_UNUSED_73:
1334 case OP_UNUSED_79:
1335 case OP_UNUSED_7A:
Andy McFaddenc35a2ef2010-06-17 12:36:00 -07001336 case OP_UNUSED_F1:
1337 case OP_UNUSED_FF:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1339 return true;
1340 case OP_NOP:
1341 break;
1342 default:
1343 return true;
1344 }
1345 return false;
1346}
1347
1348static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1349{
Bill Buzbee1465db52009-09-23 17:17:35 -07001350 RegLocation rlDest;
1351 RegLocation rlResult;
1352 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001353 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001354 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001355 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001356 }
Ben Chenge9695e52009-06-16 16:11:47 -07001357
Ben Chengba4fc8b2009-06-01 13:00:29 -07001358 switch (mir->dalvikInsn.opCode) {
1359 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001360 case OP_CONST_4: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001361 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001362 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001363 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001364 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001365 }
1366 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001367 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001368 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbeec6f10662010-02-09 11:16:15 -08001369 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001370 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001371 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1372 rlResult.lowReg, 31);
1373 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001374 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001375 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001376 default:
1377 return true;
1378 }
1379 return false;
1380}
1381
1382static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1383{
Bill Buzbee1465db52009-09-23 17:17:35 -07001384 RegLocation rlDest;
1385 RegLocation rlResult;
1386 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001387 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001388 } else {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001389 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001390 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001391 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001392
Ben Chengba4fc8b2009-06-01 13:00:29 -07001393 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001394 case OP_CONST_HIGH16: {
Ben Chengbd1326d2010-04-02 15:04:53 -07001395 loadConstantNoClobber(cUnit, rlResult.lowReg,
1396 mir->dalvikInsn.vB << 16);
Bill Buzbee1465db52009-09-23 17:17:35 -07001397 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001398 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001399 }
1400 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001401 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1402 0, mir->dalvikInsn.vB << 16);
1403 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001404 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001405 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001406 default:
1407 return true;
1408 }
1409 return false;
1410}
1411
1412static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1413{
1414 /* For OP_THROW_VERIFICATION_ERROR */
1415 genInterpSingleStep(cUnit, mir);
1416 return false;
1417}
1418
1419static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1420{
Bill Buzbee1465db52009-09-23 17:17:35 -07001421 RegLocation rlResult;
1422 RegLocation rlDest;
1423 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001424
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001426 case OP_CONST_STRING_JUMBO:
1427 case OP_CONST_STRING: {
1428 void *strPtr = (void*)
1429 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001430
1431 if (strPtr == NULL) {
1432 LOGE("Unexpected null string");
1433 dvmAbort();
1434 }
1435
Bill Buzbeec6f10662010-02-09 11:16:15 -08001436 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1437 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001438 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) strPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001439 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440 break;
1441 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001442 case OP_CONST_CLASS: {
1443 void *classPtr = (void*)
1444 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001445
1446 if (classPtr == NULL) {
1447 LOGE("Unexpected null class");
1448 dvmAbort();
1449 }
1450
Bill Buzbeec6f10662010-02-09 11:16:15 -08001451 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1452 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001453 loadConstantNoClobber(cUnit, rlResult.lowReg, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001454 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001455 break;
1456 }
buzbeeecf8f6e2010-07-20 14:53:42 -07001457 case OP_SGET_VOLATILE:
1458 case OP_SGET_OBJECT_VOLATILE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001459 case OP_SGET_OBJECT:
1460 case OP_SGET_BOOLEAN:
1461 case OP_SGET_CHAR:
1462 case OP_SGET_BYTE:
1463 case OP_SGET_SHORT:
1464 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001465 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001466 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001467 bool isVolatile;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001468 void *fieldPtr = (void*)
1469 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001470
1471 if (fieldPtr == NULL) {
1472 LOGE("Unexpected null static field");
1473 dvmAbort();
1474 }
1475
buzbeeecf8f6e2010-07-20 14:53:42 -07001476 isVolatile = (mir->dalvikInsn.opCode == OP_SGET_VOLATILE) ||
1477 (mir->dalvikInsn.opCode == OP_SGET_OBJECT_VOLATILE) ||
1478 dvmIsVolatileField(fieldPtr);
1479
Bill Buzbeec6f10662010-02-09 11:16:15 -08001480 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1481 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001482 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001483
buzbeeecf8f6e2010-07-20 14:53:42 -07001484 if (isVolatile) {
1485 dvmCompilerGenMemBarrier(cUnit);
1486 }
Ben Cheng11d8f142010-03-24 15:24:19 -07001487 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001488 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001489 HEAP_ACCESS_SHADOW(false);
1490
Bill Buzbee1465db52009-09-23 17:17:35 -07001491 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001492 break;
1493 }
1494 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001495 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001496 void *fieldPtr = (void*)
1497 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001498
1499 if (fieldPtr == NULL) {
1500 LOGE("Unexpected null static field");
1501 dvmAbort();
1502 }
1503
Bill Buzbeec6f10662010-02-09 11:16:15 -08001504 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001505 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
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
1509 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001510 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001511 HEAP_ACCESS_SHADOW(false);
1512
Bill Buzbee1465db52009-09-23 17:17:35 -07001513 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001514 break;
1515 }
1516 case OP_SPUT_OBJECT:
1517 case OP_SPUT_BOOLEAN:
1518 case OP_SPUT_CHAR:
1519 case OP_SPUT_BYTE:
1520 case OP_SPUT_SHORT:
1521 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001522 int valOffset = offsetof(StaticField, value);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001523 int tReg = dvmCompilerAllocTemp(cUnit);
buzbeeecf8f6e2010-07-20 14:53:42 -07001524 bool isVolatile;
1525 Field *fieldPtr =
Ben Chengba4fc8b2009-06-01 13:00:29 -07001526 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001527
buzbeeecf8f6e2010-07-20 14:53:42 -07001528 isVolatile = (mir->dalvikInsn.opCode == OP_SPUT_VOLATILE) ||
1529 (mir->dalvikInsn.opCode == OP_SPUT_OBJECT_VOLATILE) ||
1530 dvmIsVolatileField(fieldPtr);
1531
Ben Chengdd6e8702010-05-07 13:05:47 -07001532 if (fieldPtr == NULL) {
1533 LOGE("Unexpected null static field");
1534 dvmAbort();
1535 }
1536
Bill Buzbeec6f10662010-02-09 11:16:15 -08001537 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001538 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1539 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001540
1541 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001542 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001543 HEAP_ACCESS_SHADOW(false);
buzbeeecf8f6e2010-07-20 14:53:42 -07001544 if (isVolatile) {
1545 dvmCompilerGenMemBarrier(cUnit);
1546 }
buzbee919eb062010-07-12 12:59:22 -07001547 if (mir->dalvikInsn.opCode == OP_SPUT_OBJECT) {
1548 /* NOTE: marking card based on field address */
1549 markCard(cUnit, rlSrc.lowReg, tReg);
1550 }
buzbeebaf196a2010-08-04 10:13:15 -07001551 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001552
Ben Chengba4fc8b2009-06-01 13:00:29 -07001553 break;
1554 }
1555 case OP_SPUT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001556 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001557 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001558 void *fieldPtr = (void*)
1559 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001560
Ben Chengdd6e8702010-05-07 13:05:47 -07001561 if (fieldPtr == NULL) {
1562 LOGE("Unexpected null static field");
1563 dvmAbort();
1564 }
1565
Bill Buzbeec6f10662010-02-09 11:16:15 -08001566 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001567 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1568 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Ben Cheng11d8f142010-03-24 15:24:19 -07001569
1570 HEAP_ACCESS_SHADOW(true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001571 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Ben Cheng11d8f142010-03-24 15:24:19 -07001572 HEAP_ACCESS_SHADOW(false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001573 break;
1574 }
1575 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001576 /*
1577 * Obey the calling convention and don't mess with the register
1578 * usage.
1579 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001580 ClassObject *classPtr = (void*)
1581 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Ben Chengdd6e8702010-05-07 13:05:47 -07001582
1583 if (classPtr == NULL) {
1584 LOGE("Unexpected null class");
1585 dvmAbort();
1586 }
1587
Ben Cheng79d173c2009-09-29 16:12:51 -07001588 /*
1589 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001590 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001591 */
1592 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001593 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001594 genExportPC(cUnit, mir);
Ben Chengbd1326d2010-04-02 15:04:53 -07001595 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001596 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001597 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001598 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001599 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001600 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001601 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1602 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001603 /*
1604 * OOM exception needs to be thrown here and cannot re-execute
1605 */
1606 loadConstant(cUnit, r0,
1607 (int) (cUnit->method->insns + mir->offset));
1608 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1609 /* noreturn */
1610
Bill Buzbee1465db52009-09-23 17:17:35 -07001611 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001612 target->defMask = ENCODE_ALL;
1613 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001614 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1615 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001616 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001617 break;
1618 }
1619 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001620 /*
1621 * Obey the calling convention and don't mess with the register
1622 * usage.
1623 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001624 ClassObject *classPtr =
1625 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001626 /*
1627 * Note: It is possible that classPtr is NULL at this point,
1628 * even though this instruction has been successfully interpreted.
1629 * If the previous interpretation had a null source, the
1630 * interpreter would not have bothered to resolve the clazz.
1631 * Bail out to the interpreter in this case, and log it
1632 * so that we can tell if it happens frequently.
1633 */
1634 if (classPtr == NULL) {
Ben Cheng11d8f142010-03-24 15:24:19 -07001635 LOGVV("null clazz in OP_CHECK_CAST, single-stepping");
Bill Buzbee4df41a52009-11-12 17:07:16 -08001636 genInterpSingleStep(cUnit, mir);
1637 return false;
1638 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08001639 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001640 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbeec6f10662010-02-09 11:16:15 -08001641 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001642 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1643 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
1644 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
1645 /*
1646 * rlSrc.lowReg now contains object->clazz. Note that
1647 * it could have been allocated r0, but we're okay so long
1648 * as we don't do anything desctructive until r0 is loaded
1649 * with clazz.
1650 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001651 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07001652 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07001653 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInstanceofNonTrivial);
Bill Buzbee1465db52009-09-23 17:17:35 -07001654 opRegReg(cUnit, kOpCmp, r0, r1);
1655 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
1656 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08001657 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001658 /*
1659 * If null, check cast failed - punt to the interpreter. Because
1660 * interpreter will be the one throwing, we don't need to
1661 * genExportPC() here.
1662 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001663 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001664 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07001665 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001666 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001667 branch1->generic.target = (LIR *)target;
1668 branch2->generic.target = (LIR *)target;
1669 break;
1670 }
buzbee4d92e682010-07-29 15:24:14 -07001671 case OP_SGET_WIDE_VOLATILE:
1672 case OP_SPUT_WIDE_VOLATILE:
1673 genInterpSingleStep(cUnit, mir);
1674 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001675 default:
1676 return true;
1677 }
1678 return false;
1679}
1680
1681static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
1682{
1683 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001684 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001685 switch (dalvikOpCode) {
1686 case OP_MOVE_EXCEPTION: {
1687 int offset = offsetof(InterpState, self);
1688 int exOffset = offsetof(Thread, exception);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001689 int selfReg = dvmCompilerAllocTemp(cUnit);
1690 int resetReg = dvmCompilerAllocTemp(cUnit);
1691 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1692 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001693 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001694 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001695 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08001696 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001697 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001698 break;
1699 }
1700 case OP_MOVE_RESULT:
1701 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001702 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001703 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
1704 rlSrc.fp = rlDest.fp;
1705 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001706 break;
1707 }
1708 case OP_MOVE_RESULT_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001709 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001710 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
1711 rlSrc.fp = rlDest.fp;
1712 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001713 break;
1714 }
1715 case OP_RETURN_WIDE: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001716 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001717 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
1718 rlDest.fp = rlSrc.fp;
1719 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001720 genReturnCommon(cUnit,mir);
1721 break;
1722 }
1723 case OP_RETURN:
1724 case OP_RETURN_OBJECT: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001725 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001726 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
1727 rlDest.fp = rlSrc.fp;
1728 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001729 genReturnCommon(cUnit,mir);
1730 break;
1731 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001732 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08001734#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08001735 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001736#else
Ben Cheng5d90c202009-11-22 23:31:11 -08001737 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07001738#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001739 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740 case OP_THROW: {
1741 genInterpSingleStep(cUnit, mir);
1742 break;
1743 }
1744 default:
1745 return true;
1746 }
1747 return false;
1748}
1749
Bill Buzbeed45ba372009-06-15 17:00:57 -07001750static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
1751{
1752 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001753 RegLocation rlDest;
1754 RegLocation rlSrc;
1755 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07001756
Ben Chengba4fc8b2009-06-01 13:00:29 -07001757 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001758 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07001759 }
1760
Bill Buzbee1465db52009-09-23 17:17:35 -07001761 if (mir->ssaRep->numUses == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001762 rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001763 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001764 rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001765 if (mir->ssaRep->numDefs == 2)
Bill Buzbeec6f10662010-02-09 11:16:15 -08001766 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001767 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08001768 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07001769
Ben Chengba4fc8b2009-06-01 13:00:29 -07001770 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001771 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001772 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001773 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001774 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001775 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001776 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001777 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001778 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001779 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07001780 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001781 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 case OP_NEG_INT:
1783 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001784 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001785 case OP_NEG_LONG:
1786 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08001787 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001788 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08001789 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001790 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08001791 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001792 case OP_MOVE_WIDE:
1793 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001794 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001795 case OP_INT_TO_LONG:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001796 rlSrc = dvmCompilerUpdateLoc(cUnit, rlSrc);
1797 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001798 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07001799 if (rlSrc.location == kLocPhysReg) {
1800 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1801 } else {
1802 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
1803 }
1804 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1805 rlResult.lowReg, 31);
1806 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001807 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07001808 case OP_LONG_TO_INT:
Bill Buzbeec6f10662010-02-09 11:16:15 -08001809 rlSrc = dvmCompilerUpdateLocWide(cUnit, rlSrc);
1810 rlSrc = dvmCompilerWideToNarrow(cUnit, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07001812 case OP_MOVE:
1813 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001814 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001815 break;
1816 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001818 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001819 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
1820 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001821 break;
1822 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001823 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001824 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001825 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
1826 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001827 break;
1828 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001829 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001830 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001831 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
1832 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001833 break;
1834 case OP_ARRAY_LENGTH: {
1835 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07001836 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1837 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
1838 mir->offset, NULL);
Bill Buzbeec6f10662010-02-09 11:16:15 -08001839 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001840 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
1841 rlResult.lowReg);
1842 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001843 break;
1844 }
1845 default:
1846 return true;
1847 }
1848 return false;
1849}
1850
1851static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
1852{
1853 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001854 RegLocation rlDest;
1855 RegLocation rlResult;
1856 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001857 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001858 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
1859 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001860 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001861 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001862 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1863 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08001865 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
1866 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07001867 loadConstantNoClobber(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee1465db52009-09-23 17:17:35 -07001868 storeValue(cUnit, rlDest, rlResult);
1869 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07001870 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001871 return false;
1872}
1873
1874/* Compare agaist zero */
1875static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001876 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001877{
1878 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001879 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08001880 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001881 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1882 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001883
Bill Buzbee270c1d62009-08-13 16:58:07 -07001884//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07001885 switch (dalvikOpCode) {
1886 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001888 break;
1889 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001890 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001891 break;
1892 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001893 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001894 break;
1895 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001896 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001897 break;
1898 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001899 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001900 break;
1901 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07001902 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001903 break;
1904 default:
1905 cond = 0;
1906 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08001907 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001908 }
1909 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1910 /* This mostly likely will be optimized away in a later phase */
1911 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1912 return false;
1913}
1914
Elliott Hughesb4c05972010-02-24 16:36:18 -08001915static bool isPowerOfTwo(int x)
1916{
1917 return (x & (x - 1)) == 0;
1918}
1919
1920// Returns true if no more than two bits are set in 'x'.
1921static bool isPopCountLE2(unsigned int x)
1922{
1923 x &= x - 1;
1924 return (x & (x - 1)) == 0;
1925}
1926
1927// Returns the index of the lowest set bit in 'x'.
1928static int lowestSetBit(unsigned int x) {
1929 int bit_posn = 0;
1930 while ((x & 0xf) == 0) {
1931 bit_posn += 4;
1932 x >>= 4;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08001933 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08001934 while ((x & 1) == 0) {
1935 bit_posn++;
1936 x >>= 1;
1937 }
1938 return bit_posn;
1939}
1940
Elliott Hughes672511b2010-04-26 17:40:13 -07001941// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1942// and store the result in 'rlDest'.
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07001943static bool handleEasyDivide(CompilationUnit *cUnit, OpCode dalvikOpCode,
Elliott Hughes672511b2010-04-26 17:40:13 -07001944 RegLocation rlSrc, RegLocation rlDest, int lit)
1945{
1946 if (lit < 2 || !isPowerOfTwo(lit)) {
1947 return false;
1948 }
1949 int k = lowestSetBit(lit);
1950 if (k >= 30) {
1951 // Avoid special cases.
1952 return false;
1953 }
Elliott Hughes9c457022010-04-28 16:15:38 -07001954 bool div = (dalvikOpCode == OP_DIV_INT_LIT8 || dalvikOpCode == OP_DIV_INT_LIT16);
Elliott Hughes672511b2010-04-26 17:40:13 -07001955 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1956 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Elliott Hughes9c457022010-04-28 16:15:38 -07001957 if (div) {
1958 int tReg = dvmCompilerAllocTemp(cUnit);
1959 if (lit == 2) {
1960 // Division by 2 is by far the most common division by constant.
1961 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1962 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1963 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1964 } else {
1965 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1966 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1967 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1968 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1969 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001970 } else {
Elliott Hughes9c457022010-04-28 16:15:38 -07001971 int cReg = dvmCompilerAllocTemp(cUnit);
1972 loadConstant(cUnit, cReg, lit - 1);
1973 int tReg1 = dvmCompilerAllocTemp(cUnit);
1974 int tReg2 = dvmCompilerAllocTemp(cUnit);
1975 if (lit == 2) {
1976 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1977 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1978 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1979 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1980 } else {
1981 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1982 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1983 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1984 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1985 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1986 }
Elliott Hughes672511b2010-04-26 17:40:13 -07001987 }
1988 storeValue(cUnit, rlDest, rlResult);
1989 return true;
1990}
1991
Elliott Hughesb4c05972010-02-24 16:36:18 -08001992// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1993// and store the result in 'rlDest'.
1994static bool handleEasyMultiply(CompilationUnit *cUnit,
1995 RegLocation rlSrc, RegLocation rlDest, int lit)
1996{
1997 // Can we simplify this multiplication?
1998 bool powerOfTwo = false;
1999 bool popCountLE2 = false;
2000 bool powerOfTwoMinusOne = false;
2001 if (lit < 2) {
2002 // Avoid special cases.
2003 return false;
2004 } else if (isPowerOfTwo(lit)) {
2005 powerOfTwo = true;
2006 } else if (isPopCountLE2(lit)) {
2007 popCountLE2 = true;
2008 } else if (isPowerOfTwo(lit + 1)) {
2009 powerOfTwoMinusOne = true;
2010 } else {
2011 return false;
2012 }
2013 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2014 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
2015 if (powerOfTwo) {
2016 // Shift.
2017 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2018 lowestSetBit(lit));
2019 } else if (popCountLE2) {
2020 // Shift and add and shift.
2021 int firstBit = lowestSetBit(lit);
2022 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2023 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2024 firstBit, secondBit);
2025 } else {
2026 // Reverse subtract: (src << (shift + 1)) - src.
2027 assert(powerOfTwoMinusOne);
2028 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2029 int tReg = dvmCompilerAllocTemp(cUnit);
2030 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2031 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2032 }
2033 storeValue(cUnit, rlDest, rlResult);
2034 return true;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002035}
2036
Ben Chengba4fc8b2009-06-01 13:00:29 -07002037static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2038{
2039 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002040 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2041 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002042 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002043 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002044 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002045 int shiftOp = false;
2046 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002047
Ben Chengba4fc8b2009-06-01 13:00:29 -07002048 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 case OP_RSUB_INT_LIT8:
2050 case OP_RSUB_INT: {
2051 int tReg;
2052 //TUNING: add support for use of Arm rsub op
2053 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002054 tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002055 loadConstant(cUnit, tReg, lit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002056 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002057 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2058 tReg, rlSrc.lowReg);
2059 storeValue(cUnit, rlDest, rlResult);
2060 return false;
2061 break;
2062 }
2063
Ben Chengba4fc8b2009-06-01 13:00:29 -07002064 case OP_ADD_INT_LIT8:
2065 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002066 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002067 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002068 case OP_MUL_INT_LIT8:
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002069 case OP_MUL_INT_LIT16: {
Elliott Hughesb4c05972010-02-24 16:36:18 -08002070 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2071 return false;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002072 }
Elliott Hughesb4c05972010-02-24 16:36:18 -08002073 op = kOpMul;
Bill Buzbee1465db52009-09-23 17:17:35 -07002074 break;
Bill Buzbee78cb0e22010-02-11 14:04:53 -08002075 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002076 case OP_AND_INT_LIT8:
2077 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002078 op = kOpAnd;
2079 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002080 case OP_OR_INT_LIT8:
2081 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002082 op = kOpOr;
2083 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002084 case OP_XOR_INT_LIT8:
2085 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002086 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002088 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002089 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002090 shiftOp = true;
2091 op = kOpLsl;
2092 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002093 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002094 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002095 shiftOp = true;
2096 op = kOpAsr;
2097 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002098 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002099 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002100 shiftOp = true;
2101 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002102 break;
2103
2104 case OP_DIV_INT_LIT8:
2105 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002106 case OP_REM_INT_LIT8:
2107 case OP_REM_INT_LIT16:
2108 if (lit == 0) {
2109 /* Let the interpreter deal with div by 0 */
2110 genInterpSingleStep(cUnit, mir);
2111 return false;
2112 }
Elliott Hughesc7ad9b22010-04-28 13:52:02 -07002113 if (handleEasyDivide(cUnit, dalvikOpCode, rlSrc, rlDest, lit)) {
Elliott Hughes672511b2010-04-26 17:40:13 -07002114 return false;
2115 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002116 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002117 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002118 dvmCompilerClobber(cUnit, r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002119 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2120 (dalvikOpCode == OP_DIV_INT_LIT16)) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002121 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idiv);
Bill Buzbee1465db52009-09-23 17:17:35 -07002122 isDiv = true;
2123 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002124 LOAD_FUNC_ADDR(cUnit, r2, (int)__aeabi_idivmod);
Bill Buzbee1465db52009-09-23 17:17:35 -07002125 isDiv = false;
2126 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002127 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002128 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002129 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002130 if (isDiv)
Bill Buzbeec6f10662010-02-09 11:16:15 -08002131 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 else
Bill Buzbeec6f10662010-02-09 11:16:15 -08002133 rlResult = dvmCompilerGetReturnAlt(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002134 storeValue(cUnit, rlDest, rlResult);
2135 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 break;
2137 default:
2138 return true;
2139 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002141 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07002142 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2143 if (shiftOp && (lit == 0)) {
2144 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2145 } else {
2146 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2147 }
2148 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002149 return false;
2150}
2151
2152static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2153{
2154 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
buzbee4d92e682010-07-29 15:24:14 -07002155 int fieldOffset = -1;
buzbeeecf8f6e2010-07-20 14:53:42 -07002156 bool isVolatile = false;
buzbee4d92e682010-07-29 15:24:14 -07002157 switch (dalvikOpCode) {
2158 /*
2159 * Wide volatiles currently handled via single step.
2160 * Add them here if generating in-line code.
2161 * case OP_IGET_WIDE_VOLATILE:
2162 * case OP_IPUT_WIDE_VOLATILE:
2163 */
2164 case OP_IGET:
2165 case OP_IGET_VOLATILE:
2166 case OP_IGET_WIDE:
2167 case OP_IGET_OBJECT:
2168 case OP_IGET_OBJECT_VOLATILE:
2169 case OP_IGET_BOOLEAN:
2170 case OP_IGET_BYTE:
2171 case OP_IGET_CHAR:
2172 case OP_IGET_SHORT:
2173 case OP_IPUT:
2174 case OP_IPUT_VOLATILE:
2175 case OP_IPUT_WIDE:
2176 case OP_IPUT_OBJECT:
2177 case OP_IPUT_OBJECT_VOLATILE:
2178 case OP_IPUT_BOOLEAN:
2179 case OP_IPUT_BYTE:
2180 case OP_IPUT_CHAR:
2181 case OP_IPUT_SHORT: {
2182 Field *fieldPtr =
2183 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002184
buzbee4d92e682010-07-29 15:24:14 -07002185 if (fieldPtr == NULL) {
2186 LOGE("Unexpected null instance field");
2187 dvmAbort();
2188 }
2189 isVolatile = dvmIsVolatileField(fieldPtr);
2190 fieldOffset = ((InstField *)fieldPtr)->byteOffset;
2191 break;
Ben Chengdd6e8702010-05-07 13:05:47 -07002192 }
buzbee4d92e682010-07-29 15:24:14 -07002193 default:
2194 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002195 }
buzbee4d92e682010-07-29 15:24:14 -07002196
Ben Chengba4fc8b2009-06-01 13:00:29 -07002197 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002198 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002199 // Generates a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002200 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2201 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002203 void *classPtr = (void*)
2204 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Ben Chengdd6e8702010-05-07 13:05:47 -07002205
2206 if (classPtr == NULL) {
2207 LOGE("Unexpected null class");
2208 dvmAbort();
2209 }
2210
Bill Buzbeec6f10662010-02-09 11:16:15 -08002211 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002212 genExportPC(cUnit, mir);
2213 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002214 loadConstant(cUnit, r0, (int) classPtr );
Ben Chengbd1326d2010-04-02 15:04:53 -07002215 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002216 /*
2217 * "len < 0": bail to the interpreter to re-execute the
2218 * instruction
2219 */
Carl Shapiroe3c01da2010-05-20 22:54:18 -07002220 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002221 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002222 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002223 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002224 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002225 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2226 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002227 /*
2228 * OOM exception needs to be thrown here and cannot re-execute
2229 */
2230 loadConstant(cUnit, r0,
2231 (int) (cUnit->method->insns + mir->offset));
2232 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2233 /* noreturn */
2234
Bill Buzbee1465db52009-09-23 17:17:35 -07002235 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002236 target->defMask = ENCODE_ALL;
2237 branchOver->generic.target = (LIR *) target;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002238 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002239 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002240 break;
2241 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002242 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002243 // May generate a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002244 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2245 RegLocation rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002246 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002247 ClassObject *classPtr =
2248 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002249 /*
2250 * Note: It is possible that classPtr is NULL at this point,
2251 * even though this instruction has been successfully interpreted.
2252 * If the previous interpretation had a null source, the
2253 * interpreter would not have bothered to resolve the clazz.
2254 * Bail out to the interpreter in this case, and log it
2255 * so that we can tell if it happens frequently.
2256 */
2257 if (classPtr == NULL) {
2258 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2259 genInterpSingleStep(cUnit, mir);
2260 break;
2261 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08002262 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002263 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002264 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002265//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002266 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002267 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002268 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002269 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002270 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002271 /* r1 now contains object->clazz */
Ben Chengbd1326d2010-04-02 15:04:53 -07002272 LOAD_FUNC_ADDR(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002273 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002274 opRegReg(cUnit, kOpCmp, r1, r2);
2275 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2276 genRegCopy(cUnit, r0, r1);
2277 genRegCopy(cUnit, r1, r2);
2278 opReg(cUnit, kOpBlx, r3);
Elliott Hughes6a555132010-02-25 15:41:42 -08002279 dvmCompilerClobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002280 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002281 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002282 target->defMask = ENCODE_ALL;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002283 rlResult = dvmCompilerGetReturn(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002284 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002285 branch1->generic.target = (LIR *)target;
2286 branch2->generic.target = (LIR *)target;
2287 break;
2288 }
2289 case OP_IGET_WIDE:
2290 genIGetWide(cUnit, mir, fieldOffset);
2291 break;
buzbeeecf8f6e2010-07-20 14:53:42 -07002292 case OP_IGET_VOLATILE:
2293 case OP_IGET_OBJECT_VOLATILE:
2294 isVolatile = true;
2295 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002296 case OP_IGET:
2297 case OP_IGET_OBJECT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002298 genIGet(cUnit, mir, kWord, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002299 break;
2300 case OP_IGET_BOOLEAN:
buzbeeecf8f6e2010-07-20 14:53:42 -07002301 genIGet(cUnit, mir, kUnsignedByte, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002302 break;
2303 case OP_IGET_BYTE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002304 genIGet(cUnit, mir, kSignedByte, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 break;
2306 case OP_IGET_CHAR:
buzbeeecf8f6e2010-07-20 14:53:42 -07002307 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002308 break;
2309 case OP_IGET_SHORT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002310 genIGet(cUnit, mir, kSignedHalf, fieldOffset, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002311 break;
2312 case OP_IPUT_WIDE:
2313 genIPutWide(cUnit, mir, fieldOffset);
2314 break;
2315 case OP_IPUT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002316 genIPut(cUnit, mir, kWord, fieldOffset, false, isVolatile);
buzbee919eb062010-07-12 12:59:22 -07002317 break;
buzbee4d92e682010-07-29 15:24:14 -07002318 case OP_IPUT_VOLATILE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002319 case OP_IPUT_OBJECT_VOLATILE:
2320 isVolatile = true;
2321 // NOTE: intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002322 case OP_IPUT_OBJECT:
buzbeeecf8f6e2010-07-20 14:53:42 -07002323 genIPut(cUnit, mir, kWord, fieldOffset, true, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002324 break;
2325 case OP_IPUT_SHORT:
2326 case OP_IPUT_CHAR:
buzbeeecf8f6e2010-07-20 14:53:42 -07002327 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset, false, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002328 break;
2329 case OP_IPUT_BYTE:
buzbeeecf8f6e2010-07-20 14:53:42 -07002330 genIPut(cUnit, mir, kSignedByte, fieldOffset, false, isVolatile);
2331 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002332 case OP_IPUT_BOOLEAN:
buzbeeecf8f6e2010-07-20 14:53:42 -07002333 genIPut(cUnit, mir, kUnsignedByte, fieldOffset, false, isVolatile);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002334 break;
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002335 case OP_IGET_WIDE_VOLATILE:
2336 case OP_IPUT_WIDE_VOLATILE:
Bill Buzbeeb16344a2010-03-15 17:19:12 -07002337 genInterpSingleStep(cUnit, mir);
2338 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002339 default:
2340 return true;
2341 }
2342 return false;
2343}
2344
2345static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2346{
2347 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2348 int fieldOffset = mir->dalvikInsn.vC;
2349 switch (dalvikOpCode) {
2350 case OP_IGET_QUICK:
2351 case OP_IGET_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002352 genIGet(cUnit, mir, kWord, fieldOffset, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002353 break;
2354 case OP_IPUT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002355 genIPut(cUnit, mir, kWord, fieldOffset, false, false);
buzbee919eb062010-07-12 12:59:22 -07002356 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002357 case OP_IPUT_OBJECT_QUICK:
buzbeeecf8f6e2010-07-20 14:53:42 -07002358 genIPut(cUnit, mir, kWord, fieldOffset, true, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002359 break;
2360 case OP_IGET_WIDE_QUICK:
2361 genIGetWide(cUnit, mir, fieldOffset);
2362 break;
2363 case OP_IPUT_WIDE_QUICK:
2364 genIPutWide(cUnit, mir, fieldOffset);
2365 break;
2366 default:
2367 return true;
2368 }
2369 return false;
2370
2371}
2372
2373/* Compare agaist zero */
2374static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002375 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002376{
2377 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002378 ArmConditionCode cond;
Bill Buzbeec6f10662010-02-09 11:16:15 -08002379 RegLocation rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2380 RegLocation rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002381
Bill Buzbee1465db52009-09-23 17:17:35 -07002382 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2383 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2384 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002385
2386 switch (dalvikOpCode) {
2387 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002388 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002389 break;
2390 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002391 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002392 break;
2393 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002394 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 break;
2396 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002397 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002398 break;
2399 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002400 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002401 break;
2402 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002403 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002404 break;
2405 default:
2406 cond = 0;
2407 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08002408 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002409 }
2410 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2411 /* This mostly likely will be optimized away in a later phase */
2412 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2413 return false;
2414}
2415
2416static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2417{
2418 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002419
2420 switch (opCode) {
2421 case OP_MOVE_16:
2422 case OP_MOVE_OBJECT_16:
2423 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002424 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002425 storeValue(cUnit, dvmCompilerGetDest(cUnit, mir, 0),
2426 dvmCompilerGetSrc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002427 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002428 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002429 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002430 case OP_MOVE_WIDE_FROM16: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002431 storeValueWide(cUnit, dvmCompilerGetDestWide(cUnit, mir, 0, 1),
2432 dvmCompilerGetSrcWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002433 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002434 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002435 default:
2436 return true;
2437 }
2438 return false;
2439}
2440
2441static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2442{
2443 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002444 RegLocation rlSrc1;
2445 RegLocation rlSrc2;
2446 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447
2448 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002449 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002450 }
2451
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 /* APUTs have 3 sources and no targets */
2453 if (mir->ssaRep->numDefs == 0) {
2454 if (mir->ssaRep->numUses == 3) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002455 rlDest = dvmCompilerGetSrc(cUnit, mir, 0);
2456 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 1);
2457 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07002458 } else {
2459 assert(mir->ssaRep->numUses == 4);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002460 rlDest = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2461 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 2);
2462 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002463 }
2464 } else {
2465 /* Two sources and 1 dest. Deduce the operand sizes */
2466 if (mir->ssaRep->numUses == 4) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002467 rlSrc1 = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
2468 rlSrc2 = dvmCompilerGetSrcWide(cUnit, mir, 2, 3);
Bill Buzbee1465db52009-09-23 17:17:35 -07002469 } else {
2470 assert(mir->ssaRep->numUses == 2);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002471 rlSrc1 = dvmCompilerGetSrc(cUnit, mir, 0);
2472 rlSrc2 = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002473 }
2474 if (mir->ssaRep->numDefs == 2) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002475 rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002476 } else {
2477 assert(mir->ssaRep->numDefs == 1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002478 rlDest = dvmCompilerGetDest(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002479 }
2480 }
2481
2482
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002484 case OP_CMPL_FLOAT:
2485 case OP_CMPG_FLOAT:
2486 case OP_CMPL_DOUBLE:
2487 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002488 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002489 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002490 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002491 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002492 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002493 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002494 break;
2495 case OP_AGET:
2496 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002497 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002498 break;
2499 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002500 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002501 break;
2502 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002503 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002504 break;
2505 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002506 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002507 break;
2508 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002509 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002510 break;
2511 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002512 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002513 break;
2514 case OP_APUT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002515 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002516 break;
Bill Buzbeebe6534f2010-03-12 16:01:35 -08002517 case OP_APUT_OBJECT:
2518 genArrayObjectPut(cUnit, mir, rlSrc1, rlSrc2, rlDest, 2);
2519 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002520 case OP_APUT_SHORT:
2521 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002522 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 break;
2524 case OP_APUT_BYTE:
2525 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002526 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002527 break;
2528 default:
2529 return true;
2530 }
2531 return false;
2532}
2533
Ben Cheng6c10a972009-10-29 14:39:18 -07002534/*
2535 * Find the matching case.
2536 *
2537 * return values:
2538 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2539 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2540 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2541 * above MAX_CHAINED_SWITCH_CASES).
2542 *
2543 * Instructions around the call are:
2544 *
2545 * mov r2, pc
2546 * blx &findPackedSwitchIndex
2547 * mov pc, r0
2548 * .align4
Bill Buzbeebd047242010-05-13 13:02:53 -07002549 * chaining cell for case 0 [12 bytes]
2550 * chaining cell for case 1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002551 * :
Bill Buzbeebd047242010-05-13 13:02:53 -07002552 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [12 bytes]
Ben Cheng6c10a972009-10-29 14:39:18 -07002553 * chaining cell for case default [8 bytes]
2554 * noChain exit
2555 */
Ben Chengbd1326d2010-04-02 15:04:53 -07002556static s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002557{
2558 int size;
2559 int firstKey;
2560 const int *entries;
2561 int index;
2562 int jumpIndex;
2563 int caseDPCOffset = 0;
2564 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2565 int chainingPC = (pc + 4) & ~3;
2566
2567 /*
2568 * Packed switch data format:
2569 * ushort ident = 0x0100 magic value
2570 * ushort size number of entries in the table
2571 * int first_key first (and lowest) switch case value
2572 * int targets[size] branch targets, relative to switch opcode
2573 *
2574 * Total size is (4+size*2) 16-bit code units.
2575 */
2576 size = switchData[1];
2577 assert(size > 0);
2578
2579 firstKey = switchData[2];
2580 firstKey |= switchData[3] << 16;
2581
2582
2583 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2584 * we can treat them as a native int array.
2585 */
2586 entries = (const int*) &switchData[4];
2587 assert(((u4)entries & 0x3) == 0);
2588
2589 index = testVal - firstKey;
2590
2591 /* Jump to the default cell */
2592 if (index < 0 || index >= size) {
2593 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2594 /* Jump to the non-chaining exit point */
2595 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2596 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2597 caseDPCOffset = entries[index];
2598 /* Jump to the inline chaining cell */
2599 } else {
2600 jumpIndex = index;
2601 }
2602
Bill Buzbeebd047242010-05-13 13:02:53 -07002603 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002604 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2605}
2606
2607/* See comments for findPackedSwitchIndex */
Ben Chengbd1326d2010-04-02 15:04:53 -07002608static s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
Ben Cheng6c10a972009-10-29 14:39:18 -07002609{
2610 int size;
2611 const int *keys;
2612 const int *entries;
2613 int chainingPC = (pc + 4) & ~3;
2614 int i;
2615
2616 /*
2617 * Sparse switch data format:
2618 * ushort ident = 0x0200 magic value
2619 * ushort size number of entries in the table; > 0
2620 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2621 * int targets[size] branch targets, relative to switch opcode
2622 *
2623 * Total size is (2+size*4) 16-bit code units.
2624 */
2625
2626 size = switchData[1];
2627 assert(size > 0);
2628
2629 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2630 * we can treat them as a native int array.
2631 */
2632 keys = (const int*) &switchData[2];
2633 assert(((u4)keys & 0x3) == 0);
2634
2635 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2636 * we can treat them as a native int array.
2637 */
2638 entries = keys + size;
2639 assert(((u4)entries & 0x3) == 0);
2640
2641 /*
2642 * Run through the list of keys, which are guaranteed to
2643 * be sorted low-to-high.
2644 *
2645 * Most tables have 3-4 entries. Few have more than 10. A binary
2646 * search here is probably not useful.
2647 */
2648 for (i = 0; i < size; i++) {
2649 int k = keys[i];
2650 if (k == testVal) {
2651 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2652 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2653 i : MAX_CHAINED_SWITCH_CASES + 1;
Bill Buzbeebd047242010-05-13 13:02:53 -07002654 chainingPC += jumpIndex * CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002655 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2656 } else if (k > testVal) {
2657 break;
2658 }
2659 }
Bill Buzbeebd047242010-05-13 13:02:53 -07002660 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) *
2661 CHAIN_CELL_NORMAL_SIZE;
Ben Cheng6c10a972009-10-29 14:39:18 -07002662}
2663
Ben Chengba4fc8b2009-06-01 13:00:29 -07002664static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2665{
2666 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2667 switch (dalvikOpCode) {
2668 case OP_FILL_ARRAY_DATA: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002669 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002670 // Making a call - use explicit registers
Bill Buzbeec6f10662010-02-09 11:16:15 -08002671 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002672 genExportPC(cUnit, mir);
2673 loadValueDirectFixed(cUnit, rlSrc, r0);
Ben Chengbd1326d2010-04-02 15:04:53 -07002674 LOAD_FUNC_ADDR(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002675 loadConstant(cUnit, r1,
2676 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002677 opReg(cUnit, kOpBlx, r2);
Elliott Hughes6a555132010-02-25 15:41:42 -08002678 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002679 /* generate a branch over if successful */
2680 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2681 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2682 loadConstant(cUnit, r0,
2683 (int) (cUnit->method->insns + mir->offset));
2684 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2685 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2686 target->defMask = ENCODE_ALL;
2687 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 break;
2689 }
2690 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002691 * Compute the goto target of up to
2692 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2693 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002694 */
2695 case OP_PACKED_SWITCH:
2696 case OP_SPARSE_SWITCH: {
Bill Buzbeec6f10662010-02-09 11:16:15 -08002697 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
2698 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002699 loadValueDirectFixed(cUnit, rlSrc, r1);
Bill Buzbeec6f10662010-02-09 11:16:15 -08002700 dvmCompilerLockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002701 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Chengbd1326d2010-04-02 15:04:53 -07002702 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002703 } else {
Ben Chengbd1326d2010-04-02 15:04:53 -07002704 LOAD_FUNC_ADDR(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002705 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002706 /* r0 <- Addr of the switch data */
2707 loadConstant(cUnit, r0,
2708 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2709 /* r2 <- pc of the instruction following the blx */
2710 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002711 opReg(cUnit, kOpBlx, r4PC);
Elliott Hughes6a555132010-02-25 15:41:42 -08002712 dvmCompilerClobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002713 /* pc <- computed goto target */
2714 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002715 break;
2716 }
2717 default:
2718 return true;
2719 }
2720 return false;
2721}
2722
2723static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002724 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002725{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002726 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002727 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002728
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002729 if (bb->fallThrough != NULL)
2730 retChainingCell = &labelList[bb->fallThrough->id];
2731
Ben Chengba4fc8b2009-06-01 13:00:29 -07002732 DecodedInstruction *dInsn = &mir->dalvikInsn;
2733 switch (mir->dalvikInsn.opCode) {
2734 /*
2735 * calleeMethod = this->clazz->vtable[
2736 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2737 * ]
2738 */
2739 case OP_INVOKE_VIRTUAL:
2740 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002741 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002742 int methodIndex =
2743 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2744 methodIndex;
2745
2746 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2747 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2748 else
2749 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2750
Ben Cheng38329f52009-07-07 14:19:20 -07002751 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2752 retChainingCell,
2753 predChainingCell,
2754 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002755 break;
2756 }
2757 /*
2758 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2759 * ->pResMethods[BBBB]->methodIndex]
2760 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002761 case OP_INVOKE_SUPER:
2762 case OP_INVOKE_SUPER_RANGE: {
2763 int mIndex = cUnit->method->clazz->pDvmDex->
2764 pResMethods[dInsn->vB]->methodIndex;
2765 const Method *calleeMethod =
2766 cUnit->method->clazz->super->vtable[mIndex];
2767
2768 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2769 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2770 else
2771 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2772
2773 /* r0 = calleeMethod */
2774 loadConstant(cUnit, r0, (int) calleeMethod);
2775
Ben Cheng38329f52009-07-07 14:19:20 -07002776 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2777 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002778 break;
2779 }
2780 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2781 case OP_INVOKE_DIRECT:
2782 case OP_INVOKE_DIRECT_RANGE: {
2783 const Method *calleeMethod =
2784 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2785
2786 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2787 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2788 else
2789 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2790
2791 /* r0 = calleeMethod */
2792 loadConstant(cUnit, r0, (int) calleeMethod);
2793
Ben Cheng38329f52009-07-07 14:19:20 -07002794 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2795 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002796 break;
2797 }
2798 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2799 case OP_INVOKE_STATIC:
2800 case OP_INVOKE_STATIC_RANGE: {
2801 const Method *calleeMethod =
2802 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2803
2804 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2805 genProcessArgsNoRange(cUnit, mir, dInsn,
2806 NULL /* no null check */);
2807 else
2808 genProcessArgsRange(cUnit, mir, dInsn,
2809 NULL /* no null check */);
2810
2811 /* r0 = calleeMethod */
2812 loadConstant(cUnit, r0, (int) calleeMethod);
2813
Ben Cheng38329f52009-07-07 14:19:20 -07002814 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2815 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002816 break;
2817 }
Ben Cheng09e50c92010-05-02 10:45:32 -07002818 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002819 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2820 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002821 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002822 * The following is an example of generated code for
2823 * "invoke-interface v0"
Ben Cheng38329f52009-07-07 14:19:20 -07002824 *
Ben Cheng09e50c92010-05-02 10:45:32 -07002825 * -------- dalvik offset: 0x0008 @ invoke-interface v0
2826 * 0x47357e36 : ldr r0, [r5, #0] --+
2827 * 0x47357e38 : sub r7,r5,#24 |
2828 * 0x47357e3c : cmp r0, #0 | genProcessArgsNoRange
2829 * 0x47357e3e : beq 0x47357e82 |
2830 * 0x47357e40 : stmia r7, <r0> --+
2831 * 0x47357e42 : ldr r4, [pc, #120] --> r4 <- dalvikPC of this invoke
2832 * 0x47357e44 : add r1, pc, #64 --> r1 <- &retChainingCell
2833 * 0x47357e46 : add r2, pc, #72 --> r2 <- &predictedChainingCell
2834 * 0x47357e48 : blx_1 0x47348190 --+ TEMPLATE_INVOKE_METHOD_
2835 * 0x47357e4a : blx_2 see above --+ PREDICTED_CHAIN
2836 * 0x47357e4c : b 0x47357e90 --> off to the predicted chain
2837 * 0x47357e4e : b 0x47357e82 --> punt to the interpreter
2838 * 0x47357e50 : mov r8, r1 --+
2839 * 0x47357e52 : mov r9, r2 |
2840 * 0x47357e54 : ldr r2, [pc, #96] |
2841 * 0x47357e56 : mov r10, r3 |
2842 * 0x47357e58 : movs r0, r3 | dvmFindInterfaceMethodInCache
2843 * 0x47357e5a : ldr r3, [pc, #88] |
2844 * 0x47357e5c : ldr r7, [pc, #80] |
2845 * 0x47357e5e : mov r1, #1452 |
2846 * 0x47357e62 : blx r7 --+
2847 * 0x47357e64 : cmp r0, #0 --> calleeMethod == NULL?
2848 * 0x47357e66 : bne 0x47357e6e --> branch over the throw if !r0
2849 * 0x47357e68 : ldr r0, [pc, #80] --> load Dalvik PC of the invoke
2850 * 0x47357e6a : blx_1 0x47348494 --+ TEMPLATE_THROW_EXCEPTION_
2851 * 0x47357e6c : blx_2 see above --+ COMMON
2852 * 0x47357e6e : mov r1, r8 --> r1 <- &retChainingCell
2853 * 0x47357e70 : cmp r1, #0 --> compare against 0
2854 * 0x47357e72 : bgt 0x47357e7c --> >=0? don't rechain
2855 * 0x47357e74 : ldr r7, [r6, #108] --+
2856 * 0x47357e76 : mov r2, r9 | dvmJitToPatchPredictedChain
2857 * 0x47357e78 : mov r3, r10 |
2858 * 0x47357e7a : blx r7 --+
2859 * 0x47357e7c : add r1, pc, #8 --> r1 <- &retChainingCell
2860 * 0x47357e7e : blx_1 0x4734809c --+ TEMPLATE_INVOKE_METHOD_NO_OPT
2861 * 0x47357e80 : blx_2 see above --+
2862 * -------- reconstruct dalvik PC : 0x425719dc @ +0x0008
2863 * 0x47357e82 : ldr r0, [pc, #56]
Ben Cheng38329f52009-07-07 14:19:20 -07002864 * Exception_Handling:
Ben Cheng09e50c92010-05-02 10:45:32 -07002865 * 0x47357e84 : ldr r1, [r6, #92]
2866 * 0x47357e86 : blx r1
2867 * 0x47357e88 : .align4
2868 * -------- chaining cell (hot): 0x000b
2869 * 0x47357e88 : ldr r0, [r6, #104]
2870 * 0x47357e8a : blx r0
2871 * 0x47357e8c : data 0x19e2(6626)
2872 * 0x47357e8e : data 0x4257(16983)
2873 * 0x47357e90 : .align4
Ben Cheng38329f52009-07-07 14:19:20 -07002874 * -------- chaining cell (predicted)
Ben Cheng09e50c92010-05-02 10:45:32 -07002875 * 0x47357e90 : data 0xe7fe(59390) --> will be patched into bx
2876 * 0x47357e92 : data 0x0000(0)
2877 * 0x47357e94 : data 0x0000(0) --> class
2878 * 0x47357e96 : data 0x0000(0)
2879 * 0x47357e98 : data 0x0000(0) --> method
2880 * 0x47357e9a : data 0x0000(0)
2881 * 0x47357e9c : data 0x0000(0) --> rechain count
2882 * 0x47357e9e : data 0x0000(0)
2883 * -------- end of chaining cells (0x006c)
2884 * 0x47357eb0 : .word (0xad03e369)
2885 * 0x47357eb4 : .word (0x28a90)
2886 * 0x47357eb8 : .word (0x41a63394)
2887 * 0x47357ebc : .word (0x425719dc)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002888 */
2889 case OP_INVOKE_INTERFACE:
2890 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002891 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002892
Bill Buzbee1465db52009-09-23 17:17:35 -07002893 /* Ensure that nothing is both live and dirty */
Bill Buzbeec6f10662010-02-09 11:16:15 -08002894 dvmCompilerFlushAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002895
Ben Chengba4fc8b2009-06-01 13:00:29 -07002896 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
2897 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2898 else
2899 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2900
Ben Cheng38329f52009-07-07 14:19:20 -07002901 /* "this" is already left in r0 by genProcessArgs* */
2902
2903 /* r4PC = dalvikCallsite */
2904 loadConstant(cUnit, r4PC,
2905 (int) (cUnit->method->insns + mir->offset));
2906
2907 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002908 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07002909 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002910 addrRetChain->generic.target = (LIR *) retChainingCell;
2911
2912 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002913 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07002914 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002915 predictedChainingCell->generic.target = (LIR *) predChainingCell;
2916
2917 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
2918
2919 /* return through lr - jump to the chaining cell */
2920 genUnconditionalBranch(cUnit, predChainingCell);
2921
2922 /*
2923 * null-check on "this" may have been eliminated, but we still need
2924 * a PC-reconstruction label for stack overflow bailout.
2925 */
2926 if (pcrLabel == NULL) {
2927 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002928 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07002929 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng38329f52009-07-07 14:19:20 -07002930 pcrLabel->operands[0] = dPC;
2931 pcrLabel->operands[1] = mir->offset;
2932 /* Insert the place holder to the growable list */
2933 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
2934 }
2935
2936 /* return through lr+2 - punt to the interpreter */
2937 genUnconditionalBranch(cUnit, pcrLabel);
2938
2939 /*
2940 * return through lr+4 - fully resolve the callee method.
2941 * r1 <- count
2942 * r2 <- &predictedChainCell
2943 * r3 <- this->class
2944 * r4 <- dPC
2945 * r7 <- this->class->vtable
2946 */
2947
2948 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07002949 genRegCopy(cUnit, r8, r1);
2950 genRegCopy(cUnit, r9, r2);
2951 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07002952
Ben Chengba4fc8b2009-06-01 13:00:29 -07002953 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002954 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002955
2956 /* r1 = BBBB */
2957 loadConstant(cUnit, r1, dInsn->vB);
2958
2959 /* r2 = method (caller) */
2960 loadConstant(cUnit, r2, (int) cUnit->method);
2961
2962 /* r3 = pDvmDex */
2963 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
2964
Ben Chengbd1326d2010-04-02 15:04:53 -07002965 LOAD_FUNC_ADDR(cUnit, r7,
2966 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07002967 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002968 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
2969
Ben Cheng09e50c92010-05-02 10:45:32 -07002970 dvmCompilerClobberCallRegs(cUnit);
2971 /* generate a branch over if the interface method is resolved */
2972 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2973 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2974 /*
2975 * calleeMethod == NULL -> throw
2976 */
2977 loadConstant(cUnit, r0,
2978 (int) (cUnit->method->insns + mir->offset));
2979 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2980 /* noreturn */
2981
2982 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2983 target->defMask = ENCODE_ALL;
2984 branchOver->generic.target = (LIR *) target;
2985
Bill Buzbee1465db52009-09-23 17:17:35 -07002986 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002987
Ben Cheng38329f52009-07-07 14:19:20 -07002988 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07002989 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07002990
Bill Buzbee1465db52009-09-23 17:17:35 -07002991 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07002992
Bill Buzbee270c1d62009-08-13 16:58:07 -07002993 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
2994 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07002995
Ben Chengb88ec3c2010-05-17 12:50:33 -07002996 genRegCopy(cUnit, r1, rGLUE);
Bill Buzbee1465db52009-09-23 17:17:35 -07002997 genRegCopy(cUnit, r2, r9);
2998 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07002999
3000 /*
3001 * r0 = calleeMethod
3002 * r2 = &predictedChainingCell
3003 * r3 = class
3004 *
3005 * &returnChainingCell has been loaded into r1 but is not needed
3006 * when patching the chaining cell and will be clobbered upon
3007 * returning so it will be reconstructed again.
3008 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003009 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003010
3011 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003012 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003013 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003014
3015 bypassRechaining->generic.target = (LIR *) addrRetChain;
3016
Ben Chengba4fc8b2009-06-01 13:00:29 -07003017 /*
3018 * r0 = this, r1 = calleeMethod,
3019 * r1 = &ChainingCell,
3020 * r4PC = callsiteDPC,
3021 */
3022 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
Ben Cheng978738d2010-05-13 13:45:57 -07003023#if defined(WITH_JIT_TUNING)
Ben Cheng86717f72010-03-05 15:27:21 -08003024 gDvmJit.invokePolymorphic++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003025#endif
3026 /* Handle exceptions using the interpreter */
3027 genTrap(cUnit, mir->offset, pcrLabel);
3028 break;
3029 }
3030 /* NOP */
3031 case OP_INVOKE_DIRECT_EMPTY: {
3032 return false;
3033 }
3034 case OP_FILLED_NEW_ARRAY:
3035 case OP_FILLED_NEW_ARRAY_RANGE: {
3036 /* Just let the interpreter deal with these */
3037 genInterpSingleStep(cUnit, mir);
3038 break;
3039 }
3040 default:
3041 return true;
3042 }
3043 return false;
3044}
3045
3046static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003047 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003048{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003049 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3050 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3051 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003052
3053 DecodedInstruction *dInsn = &mir->dalvikInsn;
3054 switch (mir->dalvikInsn.opCode) {
3055 /* calleeMethod = this->clazz->vtable[BBBB] */
3056 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3057 case OP_INVOKE_VIRTUAL_QUICK: {
3058 int methodIndex = dInsn->vB;
3059 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3060 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3061 else
3062 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3063
Ben Cheng38329f52009-07-07 14:19:20 -07003064 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3065 retChainingCell,
3066 predChainingCell,
3067 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003068 break;
3069 }
3070 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3071 case OP_INVOKE_SUPER_QUICK:
3072 case OP_INVOKE_SUPER_QUICK_RANGE: {
3073 const Method *calleeMethod =
3074 cUnit->method->clazz->super->vtable[dInsn->vB];
3075
3076 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3077 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3078 else
3079 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3080
3081 /* r0 = calleeMethod */
3082 loadConstant(cUnit, r0, (int) calleeMethod);
3083
Ben Cheng38329f52009-07-07 14:19:20 -07003084 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3085 calleeMethod);
3086 /* Handle exceptions using the interpreter */
3087 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003088 break;
3089 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003090 default:
3091 return true;
3092 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003093 return false;
3094}
3095
3096/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003097 * This operation is complex enough that we'll do it partly inline
3098 * and partly with a handler. NOTE: the handler uses hardcoded
3099 * values for string object offsets and must be revisitied if the
3100 * layout changes.
3101 */
3102static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3103{
3104#if defined(USE_GLOBAL_STRING_DEFS)
3105 return false;
3106#else
3107 ArmLIR *rollback;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003108 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3109 RegLocation rlComp = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003110
3111 loadValueDirectFixed(cUnit, rlThis, r0);
3112 loadValueDirectFixed(cUnit, rlComp, r1);
3113 /* Test objects for NULL */
3114 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3115 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3116 /*
3117 * TUNING: we could check for object pointer equality before invoking
3118 * handler. Unclear whether the gain would be worth the added code size
3119 * expansion.
3120 */
3121 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003122 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3123 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003124 return true;
3125#endif
3126}
3127
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003128static bool genInlinedFastIndexOf(CompilationUnit *cUnit, MIR *mir)
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003129{
3130#if defined(USE_GLOBAL_STRING_DEFS)
3131 return false;
3132#else
Bill Buzbeec6f10662010-02-09 11:16:15 -08003133 RegLocation rlThis = dvmCompilerGetSrc(cUnit, mir, 0);
3134 RegLocation rlChar = dvmCompilerGetSrc(cUnit, mir, 1);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003135
3136 loadValueDirectFixed(cUnit, rlThis, r0);
3137 loadValueDirectFixed(cUnit, rlChar, r1);
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003138 RegLocation rlStart = dvmCompilerGetSrc(cUnit, mir, 2);
3139 loadValueDirectFixed(cUnit, rlStart, r2);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003140 /* Test objects for NULL */
3141 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3142 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003143 storeValue(cUnit, inlinedTarget(cUnit, mir, false),
3144 dvmCompilerGetReturn(cUnit));
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003145 return true;
3146#endif
3147}
3148
Elliott Hughesee34f592010-04-05 18:13:52 -07003149// Generates an inlined String.isEmpty or String.length.
3150static bool genInlinedStringIsEmptyOrLength(CompilationUnit *cUnit, MIR *mir,
3151 bool isEmpty)
Bill Buzbee1f748632010-03-02 16:14:41 -08003152{
Elliott Hughesee34f592010-04-05 18:13:52 -07003153 // dst = src.length();
Bill Buzbee1f748632010-03-02 16:14:41 -08003154 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3155 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3156 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3157 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3158 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset, NULL);
3159 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count,
3160 rlResult.lowReg);
Elliott Hughesee34f592010-04-05 18:13:52 -07003161 if (isEmpty) {
3162 // dst = (dst == 0);
3163 int tReg = dvmCompilerAllocTemp(cUnit);
3164 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
3165 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
3166 }
Bill Buzbee1f748632010-03-02 16:14:41 -08003167 storeValue(cUnit, rlDest, rlResult);
3168 return false;
3169}
3170
Elliott Hughesee34f592010-04-05 18:13:52 -07003171static bool genInlinedStringLength(CompilationUnit *cUnit, MIR *mir)
3172{
3173 return genInlinedStringIsEmptyOrLength(cUnit, mir, false);
3174}
3175
3176static bool genInlinedStringIsEmpty(CompilationUnit *cUnit, MIR *mir)
3177{
3178 return genInlinedStringIsEmptyOrLength(cUnit, mir, true);
3179}
3180
Bill Buzbee1f748632010-03-02 16:14:41 -08003181static bool genInlinedStringCharAt(CompilationUnit *cUnit, MIR *mir)
3182{
3183 int contents = offsetof(ArrayObject, contents);
3184 RegLocation rlObj = dvmCompilerGetSrc(cUnit, mir, 0);
3185 RegLocation rlIdx = dvmCompilerGetSrc(cUnit, mir, 1);
3186 RegLocation rlDest = inlinedTarget(cUnit, mir, false);
3187 RegLocation rlResult;
3188 rlObj = loadValue(cUnit, rlObj, kCoreReg);
3189 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
3190 int regMax = dvmCompilerAllocTemp(cUnit);
3191 int regOff = dvmCompilerAllocTemp(cUnit);
3192 int regPtr = dvmCompilerAllocTemp(cUnit);
3193 ArmLIR *pcrLabel = genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg,
3194 mir->offset, NULL);
3195 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_count, regMax);
3196 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_offset, regOff);
3197 loadWordDisp(cUnit, rlObj.lowReg, gDvm.offJavaLangString_value, regPtr);
3198 genBoundsCheck(cUnit, rlIdx.lowReg, regMax, mir->offset, pcrLabel);
3199 dvmCompilerFreeTemp(cUnit, regMax);
3200 opRegImm(cUnit, kOpAdd, regPtr, contents);
3201 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
3202 rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3203 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
3204 storeValue(cUnit, rlDest, rlResult);
3205 return false;
3206}
3207
3208static bool genInlinedAbsInt(CompilationUnit *cUnit, MIR *mir)
3209{
3210 RegLocation rlSrc = dvmCompilerGetSrc(cUnit, mir, 0);
3211 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
3212 RegLocation rlDest = inlinedTarget(cUnit, mir, false);;
3213 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3214 int signReg = dvmCompilerAllocTemp(cUnit);
3215 /*
3216 * abs(x) = y<=x>>31, (x+y)^y.
3217 * Thumb2's IT block also yields 3 instructions, but imposes
3218 * scheduling constraints.
3219 */
3220 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
3221 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3222 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3223 storeValue(cUnit, rlDest, rlResult);
3224 return false;
3225}
3226
3227static bool genInlinedAbsLong(CompilationUnit *cUnit, MIR *mir)
3228{
3229 RegLocation rlSrc = dvmCompilerGetSrcWide(cUnit, mir, 0, 1);
3230 RegLocation rlDest = inlinedTargetWide(cUnit, mir, false);
3231 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
3232 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
3233 int signReg = dvmCompilerAllocTemp(cUnit);
3234 /*
3235 * abs(x) = y<=x>>31, (x+y)^y.
3236 * Thumb2 IT block allows slightly shorter sequence,
3237 * but introduces a scheduling barrier. Stick with this
3238 * mechanism for now.
3239 */
3240 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
3241 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
3242 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
3243 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
3244 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
3245 storeValueWide(cUnit, rlDest, rlResult);
3246 return false;
3247}
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003248
3249/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003250 * NOTE: Handles both range and non-range versions (arguments
3251 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003252 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003253static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003254{
3255 DecodedInstruction *dInsn = &mir->dalvikInsn;
3256 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003257 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003258 case OP_EXECUTE_INLINE: {
3259 unsigned int i;
3260 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003261 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003262 int operation = dInsn->vB;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003263 switch (operation) {
3264 case INLINE_EMPTYINLINEMETHOD:
3265 return false; /* Nop */
3266 case INLINE_STRING_LENGTH:
3267 return genInlinedStringLength(cUnit, mir);
Elliott Hughesee34f592010-04-05 18:13:52 -07003268 case INLINE_STRING_IS_EMPTY:
3269 return genInlinedStringIsEmpty(cUnit, mir);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003270 case INLINE_MATH_ABS_INT:
3271 return genInlinedAbsInt(cUnit, mir);
3272 case INLINE_MATH_ABS_LONG:
3273 return genInlinedAbsLong(cUnit, mir);
3274 case INLINE_MATH_MIN_INT:
3275 return genInlinedMinMaxInt(cUnit, mir, true);
3276 case INLINE_MATH_MAX_INT:
3277 return genInlinedMinMaxInt(cUnit, mir, false);
3278 case INLINE_STRING_CHARAT:
3279 return genInlinedStringCharAt(cUnit, mir);
3280 case INLINE_MATH_SQRT:
3281 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003282 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003283 else
3284 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003285 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003286 if (genInlinedAbsFloat(cUnit, mir))
3287 return false;
3288 else
3289 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003290 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003291 if (genInlinedAbsDouble(cUnit, mir))
3292 return false;
3293 else
3294 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003295 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003296 if (genInlinedCompareTo(cUnit, mir))
3297 return false;
3298 else
3299 break;
Elliott Hughes2bdbcb62010-04-12 14:29:37 -07003300 case INLINE_STRING_FASTINDEXOF_II:
3301 if (genInlinedFastIndexOf(cUnit, mir))
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003302 return false;
3303 else
3304 break;
3305 case INLINE_STRING_EQUALS:
3306 case INLINE_MATH_COS:
3307 case INLINE_MATH_SIN:
3308 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003309 default:
Bill Buzbeefc519dc2010-03-06 23:30:57 -08003310 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003311 }
Bill Buzbeec6f10662010-02-09 11:16:15 -08003312 dvmCompilerFlushAllRegs(cUnit); /* Everything to home location */
Elliott Hughes6a555132010-02-25 15:41:42 -08003313 dvmCompilerClobberCallRegs(cUnit);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003314 dvmCompilerClobber(cUnit, r4PC);
3315 dvmCompilerClobber(cUnit, r7);
Bill Buzbee1465db52009-09-23 17:17:35 -07003316 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3317 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengbd1326d2010-04-02 15:04:53 -07003318 LOAD_FUNC_ADDR(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003319 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003320 for (i=0; i < dInsn->vA; i++) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003321 loadValueDirect(cUnit, dvmCompilerGetSrc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003322 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003323 opReg(cUnit, kOpBlx, r4PC);
3324 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003325 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3326 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3327 loadConstant(cUnit, r0,
3328 (int) (cUnit->method->insns + mir->offset));
3329 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3330 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3331 target->defMask = ENCODE_ALL;
3332 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003333 break;
3334 }
3335 default:
3336 return true;
3337 }
3338 return false;
3339}
3340
3341static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3342{
Bill Buzbee1465db52009-09-23 17:17:35 -07003343 //TUNING: We're using core regs here - not optimal when target is a double
Bill Buzbeec6f10662010-02-09 11:16:15 -08003344 RegLocation rlDest = dvmCompilerGetDestWide(cUnit, mir, 0, 1);
3345 RegLocation rlResult = dvmCompilerEvalLoc(cUnit, rlDest, kCoreReg, true);
Ben Chengbd1326d2010-04-02 15:04:53 -07003346 loadConstantNoClobber(cUnit, rlResult.lowReg,
3347 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3348 loadConstantNoClobber(cUnit, rlResult.highReg,
3349 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
Bill Buzbee1465db52009-09-23 17:17:35 -07003350 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003351 return false;
3352}
3353
Ben Chengba4fc8b2009-06-01 13:00:29 -07003354/*
3355 * The following are special processing routines that handle transfer of
3356 * controls between compiled code and the interpreter. Certain VM states like
3357 * Dalvik PC and special-purpose registers are reconstructed here.
3358 */
3359
Bill Buzbeebd047242010-05-13 13:02:53 -07003360/*
3361 * Insert a
3362 * b .+4
3363 * nop
3364 * pair at the beginning of a chaining cell. This serves as the
3365 * switch branch that selects between reverting to the interpreter or
3366 * not. Once the cell is chained to a translation, the cell will
3367 * contain a 32-bit branch. Subsequent chain/unchain operations will
3368 * then only alter that first 16-bits - the "b .+4" for unchaining,
3369 * and the restoration of the first half of the 32-bit branch for
3370 * rechaining.
3371 */
3372static void insertChainingSwitch(CompilationUnit *cUnit)
3373{
3374 ArmLIR *branch = newLIR0(cUnit, kThumbBUncond);
3375 newLIR2(cUnit, kThumbOrr, r0, r0);
3376 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3377 target->defMask = ENCODE_ALL;
3378 branch->generic.target = (LIR *) target;
3379}
3380
Ben Cheng1efc9c52009-06-08 18:25:27 -07003381/* Chaining cell for code that may need warmup. */
3382static void handleNormalChainingCell(CompilationUnit *cUnit,
3383 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003384{
Ben Cheng11d8f142010-03-24 15:24:19 -07003385 /*
3386 * Use raw instruction constructors to guarantee that the generated
3387 * instructions fit the predefined cell size.
3388 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003389 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003390 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3391 offsetof(InterpState,
3392 jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3393 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003394 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3395}
3396
3397/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003398 * Chaining cell for instructions that immediately following already translated
3399 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003400 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003401static void handleHotChainingCell(CompilationUnit *cUnit,
3402 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003403{
Ben Cheng11d8f142010-03-24 15:24:19 -07003404 /*
3405 * Use raw instruction constructors to guarantee that the generated
3406 * instructions fit the predefined cell size.
3407 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003408 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003409 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3410 offsetof(InterpState,
3411 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3412 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003413 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3414}
3415
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003416#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003417/* Chaining cell for branches that branch back into the same basic block */
3418static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3419 unsigned int offset)
3420{
Ben Cheng11d8f142010-03-24 15:24:19 -07003421 /*
3422 * Use raw instruction constructors to guarantee that the generated
3423 * instructions fit the predefined cell size.
3424 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003425 insertChainingSwitch(cUnit);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003426#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003427 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Ben Cheng40094c12010-02-24 20:58:44 -08003428 offsetof(InterpState,
3429 jitToInterpEntries.dvmJitToInterpBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003430#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003431 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003432 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3433#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003434 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003435 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3436}
3437
3438#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003439/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003440static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3441 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003442{
Ben Cheng11d8f142010-03-24 15:24:19 -07003443 /*
3444 * Use raw instruction constructors to guarantee that the generated
3445 * instructions fit the predefined cell size.
3446 */
Bill Buzbeebd047242010-05-13 13:02:53 -07003447 insertChainingSwitch(cUnit);
Ben Cheng11d8f142010-03-24 15:24:19 -07003448 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
3449 offsetof(InterpState,
3450 jitToInterpEntries.dvmJitToInterpTraceSelect) >> 2);
3451 newLIR1(cUnit, kThumbBlxR, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003452 addWordData(cUnit, (int) (callee->insns), true);
3453}
3454
Ben Cheng38329f52009-07-07 14:19:20 -07003455/* Chaining cell for monomorphic method invocations. */
3456static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3457{
3458
3459 /* Should not be executed in the initial state */
3460 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3461 /* To be filled: class */
3462 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3463 /* To be filled: method */
3464 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3465 /*
3466 * Rechain count. The initial value of 0 here will trigger chaining upon
3467 * the first invocation of this callsite.
3468 */
3469 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3470}
3471
Ben Chengba4fc8b2009-06-01 13:00:29 -07003472/* Load the Dalvik PC into r0 and jump to the specified target */
3473static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003474 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003475{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003476 ArmLIR **pcrLabel =
3477 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003478 int numElems = cUnit->pcReconstructionList.numUsed;
3479 int i;
3480 for (i = 0; i < numElems; i++) {
3481 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3482 /* r0 = dalvik PC */
3483 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3484 genUnconditionalBranch(cUnit, targetLabel);
3485 }
3486}
3487
Bill Buzbee1465db52009-09-23 17:17:35 -07003488static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3489 "kMirOpPhi",
3490 "kMirOpNullNRangeUpCheck",
3491 "kMirOpNullNRangeDownCheck",
3492 "kMirOpLowerBound",
3493 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003494};
3495
3496/*
3497 * vA = arrayReg;
3498 * vB = idxReg;
3499 * vC = endConditionReg;
3500 * arg[0] = maxC
3501 * arg[1] = minC
3502 * arg[2] = loopBranchConditionCode
3503 */
3504static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3505{
Bill Buzbee1465db52009-09-23 17:17:35 -07003506 /*
3507 * NOTE: these synthesized blocks don't have ssa names assigned
3508 * for Dalvik registers. However, because they dominate the following
3509 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3510 * ssa name.
3511 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003512 DecodedInstruction *dInsn = &mir->dalvikInsn;
3513 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003514 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003515 int regLength;
3516 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3517 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003518
3519 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003520 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3521 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3522 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003523 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3524
3525 /* regLength <- len(arrayRef) */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003526 regLength = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003527 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003528
3529 int delta = maxC;
3530 /*
3531 * If the loop end condition is ">=" instead of ">", then the largest value
3532 * of the index is "endCondition - 1".
3533 */
3534 if (dInsn->arg[2] == OP_IF_GE) {
3535 delta--;
3536 }
3537
3538 if (delta) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003539 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003540 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3541 rlIdxEnd.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003542 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003543 }
3544 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003545 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003546 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003547}
3548
3549/*
3550 * vA = arrayReg;
3551 * vB = idxReg;
3552 * vC = endConditionReg;
3553 * arg[0] = maxC
3554 * arg[1] = minC
3555 * arg[2] = loopBranchConditionCode
3556 */
3557static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3558{
3559 DecodedInstruction *dInsn = &mir->dalvikInsn;
3560 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbeec6f10662010-02-09 11:16:15 -08003561 const int regLength = dvmCompilerAllocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003562 const int maxC = dInsn->arg[0];
Bill Buzbee1465db52009-09-23 17:17:35 -07003563 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3564 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003565
3566 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003567 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3568 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3569 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003570 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3571
3572 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003573 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003574
3575 if (maxC) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003576 int tReg = dvmCompilerAllocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003577 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3578 rlIdxInit.lowReg = tReg;
Bill Buzbeec6f10662010-02-09 11:16:15 -08003579 dvmCompilerFreeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003580 }
3581
3582 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003583 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003584 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003585}
3586
3587/*
3588 * vA = idxReg;
3589 * vB = minC;
3590 */
3591static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3592{
3593 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003594 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003595 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003596
3597 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003598 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003599
3600 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003601 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003602 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3603}
3604
3605/* Extended MIR instructions like PHI */
3606static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3607{
Bill Buzbee1465db52009-09-23 17:17:35 -07003608 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003609 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3610 false);
3611 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003612 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003613
3614 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003615 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003616 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003617 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003618 break;
3619 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003620 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003621 genHoistedChecksForCountUpLoop(cUnit, mir);
3622 break;
3623 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003625 genHoistedChecksForCountDownLoop(cUnit, mir);
3626 break;
3627 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003628 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003629 genHoistedLowerBoundCheck(cUnit, mir);
3630 break;
3631 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003632 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003633 genUnconditionalBranch(cUnit,
3634 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3635 break;
3636 }
3637 default:
3638 break;
3639 }
3640}
3641
3642/*
3643 * Create a PC-reconstruction cell for the starting offset of this trace.
3644 * Since the PCR cell is placed near the end of the compiled code which is
3645 * usually out of range for a conditional branch, we put two branches (one
3646 * branch over to the loop body and one layover branch to the actual PCR) at the
3647 * end of the entry block.
3648 */
3649static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3650 ArmLIR *bodyLabel)
3651{
3652 /* Set up the place holder to reconstruct this Dalvik PC */
3653 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Ben Chenga4973592010-03-31 11:59:18 -07003654 pcrLabel->opCode = kArmPseudoPCReconstructionCell;
Ben Cheng4238ec22009-08-24 16:32:22 -07003655 pcrLabel->operands[0] =
3656 (int) (cUnit->method->insns + entry->startOffset);
3657 pcrLabel->operands[1] = entry->startOffset;
3658 /* Insert the place holder to the growable list */
3659 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3660
3661 /*
3662 * Next, create two branches - one branch over to the loop body and the
3663 * other branch to the PCR cell to punt.
3664 */
3665 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003666 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003667 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003668 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003669 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3670
3671 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003672 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003673 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003674 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003675 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3676}
3677
Ben Chengd5adae12010-03-26 17:45:28 -07003678#if defined(WITH_SELF_VERIFICATION)
3679static bool selfVerificationPuntOps(MIR *mir)
3680{
3681 DecodedInstruction *decInsn = &mir->dalvikInsn;
3682 OpCode op = decInsn->opCode;
3683 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
3684 /*
3685 * All opcodes that can throw exceptions and use the
3686 * TEMPLATE_THROW_EXCEPTION_COMMON template should be excluded in the trace
3687 * under self-verification mode.
3688 */
3689 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
3690 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
3691 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
3692 op == OP_FILL_ARRAY_DATA || op == OP_EXECUTE_INLINE ||
3693 op == OP_EXECUTE_INLINE_RANGE ||
3694 (flags & kInstrInvoke));
3695}
3696#endif
3697
Ben Chengba4fc8b2009-06-01 13:00:29 -07003698void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3699{
3700 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003701 ArmLIR *labelList =
3702 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003703 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003704 int i;
3705
3706 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003707 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003708 */
Ben Chengcec26f62010-01-15 15:29:33 -08003709 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003710 dvmInitGrowableList(&chainingListByType[i], 2);
3711 }
3712
3713 BasicBlock **blockList = cUnit->blockList;
3714
Bill Buzbee6e963e12009-06-17 16:56:19 -07003715 if (cUnit->executionCount) {
3716 /*
3717 * Reserve 6 bytes at the beginning of the trace
3718 * +----------------------------+
3719 * | execution count (4 bytes) |
3720 * +----------------------------+
3721 * | chain cell offset (2 bytes)|
3722 * +----------------------------+
3723 * ...and then code to increment the execution
3724 * count:
3725 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3726 * sub r0, #10 @ back up to addr of executionCount
3727 * ldr r1, [r0]
3728 * add r1, #1
3729 * str r1, [r0]
3730 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003731 newLIR1(cUnit, kArm16BitData, 0);
3732 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003733 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003734 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003735 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003736 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003737 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3738 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3739 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3740 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3741 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003742 } else {
3743 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003744 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003745 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003746 cUnit->headerSize = 2;
3747 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003748
Ben Chengba4fc8b2009-06-01 13:00:29 -07003749 /* Handle the content in each basic block */
3750 for (i = 0; i < cUnit->numBlocks; i++) {
3751 blockList[i]->visited = true;
3752 MIR *mir;
3753
3754 labelList[i].operands[0] = blockList[i]->startOffset;
3755
Ben Chengcec26f62010-01-15 15:29:33 -08003756 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengd44faf52010-06-02 15:33:51 -07003757 if (blockList[i]->firstMIRInsn != NULL &&
3758 ((blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3759 OP_MOVE_RESULT) ||
3760 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3761 OP_MOVE_RESULT_WIDE) ||
3762 (blockList[i]->firstMIRInsn->dalvikInsn.opCode ==
3763 OP_MOVE_RESULT_OBJECT))) {
3764 /* Align this block first since it is a return chaining cell */
3765 newLIR0(cUnit, kArmPseudoPseudoAlign4);
3766 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003767 /*
3768 * Append the label pseudo LIR first. Chaining cells will be handled
3769 * separately afterwards.
3770 */
3771 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3772 }
3773
Bill Buzbee1465db52009-09-23 17:17:35 -07003774 if (blockList[i]->blockType == kEntryBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003775 labelList[i].opCode = kArmPseudoEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003776 if (blockList[i]->firstMIRInsn == NULL) {
3777 continue;
3778 } else {
3779 setupLoopEntryBlock(cUnit, blockList[i],
3780 &labelList[blockList[i]->fallThrough->id]);
3781 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003782 } else if (blockList[i]->blockType == kExitBlock) {
Ben Chenga4973592010-03-31 11:59:18 -07003783 labelList[i].opCode = kArmPseudoExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003784 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003785 } else if (blockList[i]->blockType == kDalvikByteCode) {
3786 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003787 /* Reset the register state */
Bill Buzbeec6f10662010-02-09 11:16:15 -08003788 dvmCompilerResetRegPool(cUnit);
3789 dvmCompilerClobberAllRegs(cUnit);
3790 dvmCompilerResetNullCheck(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003791 } else {
3792 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003793 case kChainingCellNormal:
Ben Chenga4973592010-03-31 11:59:18 -07003794 labelList[i].opCode = kArmPseudoChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003795 /* handle the codegen later */
3796 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003797 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003798 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003799 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003800 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003801 kArmPseudoChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003802 labelList[i].operands[0] =
3803 (int) blockList[i]->containingMethod;
3804 /* handle the codegen later */
3805 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003806 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003807 (void *) i);
3808 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003809 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003810 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003811 kArmPseudoChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003812 /* handle the codegen later */
3813 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003814 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003815 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003816 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003817 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003818 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003819 kArmPseudoChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003820 /* handle the codegen later */
3821 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003822 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003823 (void *) i);
3824 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003825 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003826 /* Make sure exception handling block is next */
3827 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003828 kArmPseudoPCReconstructionBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003829 assert (i == cUnit->numBlocks - 2);
3830 handlePCReconstruction(cUnit, &labelList[i+1]);
3831 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003832 case kExceptionHandling:
3833 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003834 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003835 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3836 jitToInterpEntries.dvmJitToInterpPunt),
3837 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003838 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003839 }
3840 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003841#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003842 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003843 labelList[i].opCode =
Ben Chenga4973592010-03-31 11:59:18 -07003844 kArmPseudoChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003845 /* handle the codegen later */
3846 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003847 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003848 (void *) i);
3849 break;
3850#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003851 default:
3852 break;
3853 }
3854 continue;
3855 }
Ben Chenge9695e52009-06-16 16:11:47 -07003856
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003857 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003858
Ben Chengba4fc8b2009-06-01 13:00:29 -07003859 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003860
Bill Buzbeec6f10662010-02-09 11:16:15 -08003861 dvmCompilerResetRegPool(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003862 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003863 dvmCompilerClobberAllRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003864 }
3865
3866 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
Bill Buzbeec6f10662010-02-09 11:16:15 -08003867 dvmCompilerResetDefTracking(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07003868 }
3869
3870 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003871 handleExtendedMIR(cUnit, mir);
3872 continue;
3873 }
3874
Bill Buzbee1465db52009-09-23 17:17:35 -07003875
Ben Chengba4fc8b2009-06-01 13:00:29 -07003876 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3877 InstructionFormat dalvikFormat =
3878 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003879 ArmLIR *boundaryLIR =
Ben Chenga4973592010-03-31 11:59:18 -07003880 newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
Ben Chengccd6c012009-10-15 14:52:45 -07003881 mir->offset,
3882 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3883 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003884 if (mir->ssaRep) {
3885 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003886 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003887 }
3888
Ben Chenge9695e52009-06-16 16:11:47 -07003889 /* Remember the first LIR for this block */
3890 if (headLIR == NULL) {
3891 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003892 /* Set the first boundaryLIR as a scheduling barrier */
3893 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003894 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003895
Ben Chengba4fc8b2009-06-01 13:00:29 -07003896 bool notHandled;
3897 /*
3898 * Debugging: screen the opcode first to see if it is in the
3899 * do[-not]-compile list
3900 */
3901 bool singleStepMe =
3902 gDvmJit.includeSelectedOp !=
3903 ((gDvmJit.opList[dalvikOpCode >> 3] &
3904 (1 << (dalvikOpCode & 0x7))) !=
3905 0);
Ben Chengd5adae12010-03-26 17:45:28 -07003906#if defined(WITH_SELF_VERIFICATION)
3907 if (singleStepMe == false) {
3908 singleStepMe = selfVerificationPuntOps(mir);
3909 }
3910#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003911 if (singleStepMe || cUnit->allSingleStep) {
3912 notHandled = false;
3913 genInterpSingleStep(cUnit, mir);
3914 } else {
3915 opcodeCoverage[dalvikOpCode]++;
3916 switch (dalvikFormat) {
3917 case kFmt10t:
3918 case kFmt20t:
3919 case kFmt30t:
3920 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3921 mir, blockList[i], labelList);
3922 break;
3923 case kFmt10x:
3924 notHandled = handleFmt10x(cUnit, mir);
3925 break;
3926 case kFmt11n:
3927 case kFmt31i:
3928 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3929 break;
3930 case kFmt11x:
3931 notHandled = handleFmt11x(cUnit, mir);
3932 break;
3933 case kFmt12x:
3934 notHandled = handleFmt12x(cUnit, mir);
3935 break;
3936 case kFmt20bc:
3937 notHandled = handleFmt20bc(cUnit, mir);
3938 break;
3939 case kFmt21c:
3940 case kFmt31c:
3941 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3942 break;
3943 case kFmt21h:
3944 notHandled = handleFmt21h(cUnit, mir);
3945 break;
3946 case kFmt21s:
3947 notHandled = handleFmt21s(cUnit, mir);
3948 break;
3949 case kFmt21t:
3950 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3951 labelList);
3952 break;
3953 case kFmt22b:
3954 case kFmt22s:
3955 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3956 break;
3957 case kFmt22c:
3958 notHandled = handleFmt22c(cUnit, mir);
3959 break;
3960 case kFmt22cs:
3961 notHandled = handleFmt22cs(cUnit, mir);
3962 break;
3963 case kFmt22t:
3964 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3965 labelList);
3966 break;
3967 case kFmt22x:
3968 case kFmt32x:
3969 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3970 break;
3971 case kFmt23x:
3972 notHandled = handleFmt23x(cUnit, mir);
3973 break;
3974 case kFmt31t:
3975 notHandled = handleFmt31t(cUnit, mir);
3976 break;
3977 case kFmt3rc:
3978 case kFmt35c:
3979 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3980 labelList);
3981 break;
3982 case kFmt3rms:
3983 case kFmt35ms:
3984 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3985 labelList);
3986 break;
3987 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003988 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003989 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003990 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003991 case kFmt51l:
3992 notHandled = handleFmt51l(cUnit, mir);
3993 break;
3994 default:
3995 notHandled = true;
3996 break;
3997 }
3998 }
3999 if (notHandled) {
4000 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4001 mir->offset,
Andy McFaddenc6b25c72010-06-22 11:01:20 -07004002 dalvikOpCode, dexGetOpcodeName(dalvikOpCode),
Ben Chengba4fc8b2009-06-01 13:00:29 -07004003 dalvikFormat);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004004 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004005 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004006 }
4007 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004008
Bill Buzbee1465db52009-09-23 17:17:35 -07004009 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004010 dvmCompilerAppendLIR(cUnit,
4011 (LIR *) cUnit->loopAnalysis->branchToBody);
4012 dvmCompilerAppendLIR(cUnit,
4013 (LIR *) cUnit->loopAnalysis->branchToPCR);
4014 }
4015
4016 if (headLIR) {
4017 /*
4018 * Eliminate redundant loads/stores and delay stores into later
4019 * slots
4020 */
4021 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4022 cUnit->lastLIRInsn);
4023 }
4024
4025gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004026 /*
4027 * Check if the block is terminated due to trace length constraint -
4028 * insert an unconditional branch to the chaining cell.
4029 */
4030 if (blockList[i]->needFallThroughBranch) {
4031 genUnconditionalBranch(cUnit,
4032 &labelList[blockList[i]->fallThrough->id]);
4033 }
4034
Ben Chengba4fc8b2009-06-01 13:00:29 -07004035 }
4036
Ben Chenge9695e52009-06-16 16:11:47 -07004037 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004038 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004039 size_t j;
4040 int *blockIdList = (int *) chainingListByType[i].elemList;
4041
4042 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4043
4044 /* No chaining cells of this type */
4045 if (cUnit->numChainingCells[i] == 0)
4046 continue;
4047
4048 /* Record the first LIR for a new type of chaining cell */
4049 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4050
4051 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4052 int blockId = blockIdList[j];
4053
4054 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004055 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004056
4057 /* Insert the pseudo chaining instruction */
4058 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4059
4060
4061 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004062 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004063 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004064 blockList[blockId]->startOffset);
4065 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004066 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004067 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004068 blockList[blockId]->containingMethod);
4069 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004070 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004071 handleInvokePredictedChainingCell(cUnit);
4072 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004073 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004074 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004075 blockList[blockId]->startOffset);
4076 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004077#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004078 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004079 handleBackwardBranchChainingCell(cUnit,
4080 blockList[blockId]->startOffset);
4081 break;
4082#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004083 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004084 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004085 dvmCompilerAbort(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004086 }
4087 }
4088 }
Ben Chenge9695e52009-06-16 16:11:47 -07004089
Ben Chengcec26f62010-01-15 15:29:33 -08004090 /* Mark the bottom of chaining cells */
4091 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4092
Ben Cheng6c10a972009-10-29 14:39:18 -07004093 /*
4094 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4095 * of all chaining cells for the overflow cases.
4096 */
4097 if (cUnit->switchOverflowPad) {
4098 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4099 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4100 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4101 opRegReg(cUnit, kOpAdd, r1, r1);
4102 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
Ben Cheng978738d2010-05-13 13:45:57 -07004103#if defined(WITH_JIT_TUNING)
Ben Cheng6c10a972009-10-29 14:39:18 -07004104 loadConstant(cUnit, r0, kSwitchOverflow);
4105#endif
4106 opReg(cUnit, kOpBlx, r2);
4107 }
4108
Ben Chenge9695e52009-06-16 16:11:47 -07004109 dvmCompilerApplyGlobalOptimizations(cUnit);
jeffhao9e45c0b2010-02-03 10:24:05 -08004110
4111#if defined(WITH_SELF_VERIFICATION)
4112 selfVerificationBranchInsertPass(cUnit);
4113#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004114}
4115
4116/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004117bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004118{
Ben Chengccd6c012009-10-15 14:52:45 -07004119 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004120
Ben Cheng6999d842010-01-26 16:46:15 -08004121 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004122 return false;
4123 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004124
Ben Chengccd6c012009-10-15 14:52:45 -07004125 switch (work->kind) {
4126 case kWorkOrderMethod:
4127 res = dvmCompileMethod(work->info, &work->result);
4128 break;
4129 case kWorkOrderTrace:
4130 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004131 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4132 work->bailPtr);
Ben Chengccd6c012009-10-15 14:52:45 -07004133 break;
4134 case kWorkOrderTraceDebug: {
4135 bool oldPrintMe = gDvmJit.printMe;
4136 gDvmJit.printMe = true;
4137 /* Start compilation with maximally allowed trace length */
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004138 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result,
4139 work->bailPtr);
Elliott Hughes672511b2010-04-26 17:40:13 -07004140 gDvmJit.printMe = oldPrintMe;
Ben Chengccd6c012009-10-15 14:52:45 -07004141 break;
4142 }
4143 default:
4144 res = false;
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004145 LOGE("Jit: unknown work order type");
Elliott Hughes672511b2010-04-26 17:40:13 -07004146 assert(0); // Bail if debug build, discard otherwise
Ben Chengccd6c012009-10-15 14:52:45 -07004147 }
4148 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004149}
4150
Ben Chengba4fc8b2009-06-01 13:00:29 -07004151/* Architectural-specific debugging helpers go here */
4152void dvmCompilerArchDump(void)
4153{
4154 /* Print compiled opcode in this VM instance */
4155 int i, start, streak;
4156 char buf[1024];
4157
4158 streak = i = 0;
4159 buf[0] = 0;
4160 while (opcodeCoverage[i] == 0 && i < 256) {
4161 i++;
4162 }
4163 if (i == 256) {
4164 return;
4165 }
4166 for (start = i++, streak = 1; i < 256; i++) {
4167 if (opcodeCoverage[i]) {
4168 streak++;
4169 } else {
4170 if (streak == 1) {
4171 sprintf(buf+strlen(buf), "%x,", start);
4172 } else {
4173 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4174 }
4175 streak = 0;
4176 while (opcodeCoverage[i] == 0 && i < 256) {
4177 i++;
4178 }
4179 if (i < 256) {
4180 streak = 1;
4181 start = i;
4182 }
4183 }
4184 }
4185 if (streak) {
4186 if (streak == 1) {
4187 sprintf(buf+strlen(buf), "%x", start);
4188 } else {
4189 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4190 }
4191 }
4192 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004193 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004194 }
4195}
Ben Chengd7d426a2009-09-22 11:23:36 -07004196
4197/* Common initialization routine for an architecture family */
4198bool dvmCompilerArchInit()
4199{
4200 int i;
4201
Bill Buzbee1465db52009-09-23 17:17:35 -07004202 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004203 if (EncodingMap[i].opCode != i) {
4204 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4205 EncodingMap[i].name, i, EncodingMap[i].opCode);
Bill Buzbeefc519dc2010-03-06 23:30:57 -08004206 dvmAbort(); // OK to dvmAbort - build error
Ben Chengd7d426a2009-09-22 11:23:36 -07004207 }
4208 }
4209
Ben Cheng5d90c202009-11-22 23:31:11 -08004210 return dvmCompilerArchVariantInit();
4211}
4212
4213void *dvmCompilerGetInterpretTemplate()
4214{
4215 return (void*) ((int)gDvmJit.codeCache +
4216 templateEntryOffsets[TEMPLATE_INTERPRET]);
4217}
4218
4219/* Needed by the ld/st optmizatons */
4220ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4221{
4222 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4223}
4224
4225/* Needed by the register allocator */
4226ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4227{
4228 return genRegCopy(cUnit, rDest, rSrc);
4229}
4230
4231/* Needed by the register allocator */
4232void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4233 int srcLo, int srcHi)
4234{
4235 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4236}
4237
4238void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4239 int displacement, int rSrc, OpSize size)
4240{
4241 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4242}
4243
4244void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4245 int displacement, int rSrcLo, int rSrcHi)
4246{
4247 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004248}