blob: 035617e8f3a3453aeec95c2d32c5e58cc97b3d41 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
2 * Copyright (C) 2012 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
Ian Rogers57b86d42012-03-27 16:05:41 -070017#include "oat/runtime/oat_support_entrypoints.h"
18
buzbee31a4a6f2012-02-28 15:36:15 -080019namespace art {
20
21/*
22 * This source files contains "gen" codegen routines that should
23 * be applicable to most targets. Only mid-level support utilities
24 * and "op" calls may be used here.
25 */
buzbee3b3dbdd2012-06-13 13:39:34 -070026void genInvoke(CompilationUnit* cUnit, CallInfo* info);
buzbeef3aac972012-04-11 16:33:36 -070027bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
28 RegLocation rlSrc, RegLocation rlDest, int lit);
buzbee31a4a6f2012-02-28 15:36:15 -080029
buzbee8320f382012-09-11 16:29:42 -070030void markSafepointPC(CompilationUnit* cUnit, LIR* inst)
31{
32 inst->defMask = ENCODE_ALL;
33 LIR* safepointPC = newLIR0(cUnit, kPseudoSafepointPC);
34 DCHECK_EQ(safepointPC->defMask, ENCODE_ALL);
35}
36
buzbeeb046e162012-10-30 15:48:42 -070037/*
38 * To save scheduling time, helper calls are broken into two parts: generation of
39 * the helper target address, and the actuall call to the helper. Because x86
40 * has a memory call operation, part 1 is a NOP for x86. For other targets,
41 * load arguments between the two parts.
42 */
43int callHelperSetup(CompilationUnit* cUnit, int helperOffset)
44{
45 return (cUnit->instructionSet == kX86) ? 0 : loadHelper(cUnit, helperOffset);
46}
47
48/* NOTE: if rTgt is a temp, it will be freed following use */
49LIR* callHelper(CompilationUnit* cUnit, int rTgt, int helperOffset, bool safepointPC)
50{
51 LIR* callInst;
52 if (cUnit->instructionSet == kX86) {
53 callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
54 } else {
55 callInst = opReg(cUnit, kOpBlx, rTgt);
56 oatFreeTemp(cUnit, rTgt);
57 }
buzbee8320f382012-09-11 16:29:42 -070058 if (safepointPC) {
59 markSafepointPC(cUnit, callInst);
60 }
buzbeeb046e162012-10-30 15:48:42 -070061 return callInst;
62}
63
64void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0, bool safepointPC) {
65 int rTgt = callHelperSetup(cUnit, helperOffset);
66 loadConstant(cUnit, rARG0, arg0);
67 oatClobberCalleeSave(cUnit);
68 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -070069}
70
buzbee8320f382012-09-11 16:29:42 -070071void callRuntimeHelperReg(CompilationUnit* cUnit, int helperOffset, int arg0, bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -070072 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -070073 opRegCopy(cUnit, rARG0, arg0);
74 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -070075 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogers7caad772012-03-30 01:07:54 -070076}
77
buzbee8320f382012-09-11 16:29:42 -070078void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset, RegLocation arg0,
79 bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -070080 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -070081 if (arg0.wide == 0) {
82 loadValueDirectFixed(cUnit, arg0, rARG0);
83 } else {
84 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
85 }
86 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -070087 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -070088}
89
buzbee8320f382012-09-11 16:29:42 -070090void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
91 bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -070092 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -070093 loadConstant(cUnit, rARG0, arg0);
94 loadConstant(cUnit, rARG1, arg1);
95 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -070096 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -070097}
98
buzbee8320f382012-09-11 16:29:42 -070099void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset, int arg0,
100 RegLocation arg1, bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700101 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 if (arg1.wide == 0) {
103 loadValueDirectFixed(cUnit, arg1, rARG1);
104 } else {
105 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
106 }
107 loadConstant(cUnit, rARG0, arg0);
108 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700109 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700110}
111
buzbee8320f382012-09-11 16:29:42 -0700112void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset, RegLocation arg0,
113 int arg1, bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700114 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700115 loadValueDirectFixed(cUnit, arg0, rARG0);
116 loadConstant(cUnit, rARG1, arg1);
117 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700118 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700119}
120
buzbee8320f382012-09-11 16:29:42 -0700121void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
122 bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700123 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700124 opRegCopy(cUnit, rARG1, arg1);
125 loadConstant(cUnit, rARG0, arg0);
126 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700127 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700128}
129
buzbee8320f382012-09-11 16:29:42 -0700130void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
131 bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700132 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700133 opRegCopy(cUnit, rARG0, arg0);
134 loadConstant(cUnit, rARG1, arg1);
135 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700136 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700137}
138
buzbee8320f382012-09-11 16:29:42 -0700139void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset, int arg0, bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700140 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 loadCurrMethodDirect(cUnit, rARG1);
142 loadConstant(cUnit, rARG0, arg0);
143 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700144 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700145}
146
buzbee8320f382012-09-11 16:29:42 -0700147void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit, int helperOffset,
148 RegLocation arg0, RegLocation arg1, bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700149 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 if (arg0.wide == 0) {
jeffhao30a33172012-10-22 18:16:22 -0700151 loadValueDirectFixed(cUnit, arg0, arg0.fp ? rFARG0 : rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 if (arg1.wide == 0) {
buzbeeb046e162012-10-30 15:48:42 -0700153 if (cUnit->instructionSet == kMips) {
154 loadValueDirectFixed(cUnit, arg1, arg1.fp ? rFARG2 : rARG1);
155 } else {
156 loadValueDirectFixed(cUnit, arg1, rARG1);
157 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700158 } else {
buzbeeb046e162012-10-30 15:48:42 -0700159 if (cUnit->instructionSet == kMips) {
160 loadValueDirectWideFixed(cUnit, arg1, arg1.fp ? rFARG2 : rARG1, arg1.fp ? rFARG3 : rARG2);
161 } else {
162 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
163 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700164 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 } else {
jeffhao30a33172012-10-22 18:16:22 -0700166 loadValueDirectWideFixed(cUnit, arg0, arg0.fp ? rFARG0 : rARG0, arg0.fp ? rFARG1 : rARG1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 if (arg1.wide == 0) {
jeffhao30a33172012-10-22 18:16:22 -0700168 loadValueDirectFixed(cUnit, arg1, arg1.fp ? rFARG2 : rARG2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 } else {
jeffhao30a33172012-10-22 18:16:22 -0700170 loadValueDirectWideFixed(cUnit, arg1, arg1.fp ? rFARG2 : rARG2, arg1.fp ? rFARG3 : rARG3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 }
172 }
173 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700174 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700175}
176
buzbee8320f382012-09-11 16:29:42 -0700177void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
178 bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700179 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
181 opRegCopy(cUnit, rARG0, arg0);
182 opRegCopy(cUnit, rARG1, arg1);
183 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700184 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700185}
186
buzbee8320f382012-09-11 16:29:42 -0700187void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
188 int arg2, bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700189 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700190 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
191 opRegCopy(cUnit, rARG0, arg0);
192 opRegCopy(cUnit, rARG1, arg1);
193 loadConstant(cUnit, rARG2, arg2);
194 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700195 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700196}
197
buzbee8320f382012-09-11 16:29:42 -0700198void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit, int helperOffset, int arg0,
199 RegLocation arg2, bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700200 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 loadValueDirectFixed(cUnit, arg2, rARG2);
202 loadCurrMethodDirect(cUnit, rARG1);
203 loadConstant(cUnit, rARG0, arg0);
204 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700205 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700206}
207
buzbee8320f382012-09-11 16:29:42 -0700208void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg2,
209 bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700210 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 loadCurrMethodDirect(cUnit, rARG1);
212 loadConstant(cUnit, rARG2, arg2);
213 loadConstant(cUnit, rARG0, arg0);
214 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700215 callHelper(cUnit, rTgt, helperOffset, safepointPC);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700216}
217
buzbee8320f382012-09-11 16:29:42 -0700218void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit, int helperOffset,
219 int arg0, RegLocation arg1, RegLocation arg2,
220 bool safepointPC) {
buzbeeb046e162012-10-30 15:48:42 -0700221 int rTgt = callHelperSetup(cUnit, helperOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 loadValueDirectFixed(cUnit, arg1, rARG1);
223 if (arg2.wide == 0) {
224 loadValueDirectFixed(cUnit, arg2, rARG2);
225 } else {
226 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
227 }
228 loadConstant(cUnit, rARG0, arg0);
229 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700230 callHelper(cUnit, rTgt, helperOffset, safepointPC);
buzbee31a4a6f2012-02-28 15:36:15 -0800231}
232
233/*
234 * Generate an kPseudoBarrier marker to indicate the boundary of special
235 * blocks.
236 */
237void genBarrier(CompilationUnit* cUnit)
238{
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
240 /* Mark all resources as being clobbered */
241 barrier->defMask = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800242}
243
buzbee31a4a6f2012-02-28 15:36:15 -0800244
245/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800246LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800247{
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
249 branch->target = (LIR*) target;
250 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800251}
252
buzbee5de34942012-03-01 14:51:57 -0800253// FIXME: need to do some work to split out targets with
254// condition codes and those without
buzbee408ad162012-06-06 16:45:18 -0700255LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee31a4a6f2012-02-28 15:36:15 -0800256 ThrowKind kind)
257{
buzbeeb046e162012-10-30 15:48:42 -0700258 DCHECK_NE(cUnit->instructionSet, kMips);
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700260 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 LIR* branch = opCondBranch(cUnit, cCode, tgt);
262 // Remember branch target - will process later
263 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
264 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800265}
266
267LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700268 int reg, int immVal, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800269{
buzbee408ad162012-06-06 16:45:18 -0700270 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
271 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 LIR* branch;
273 if (cCode == kCondAl) {
274 branch = opUnconditionalBranch(cUnit, tgt);
275 } else {
276 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
277 }
278 // Remember branch target - will process later
279 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
280 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800281}
282
283/* Perform null-check on a register. */
buzbee408ad162012-06-06 16:45:18 -0700284LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -0800285{
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
buzbee408ad162012-06-06 16:45:18 -0700287 optFlags & MIR_IGNORE_NULL_CHECK) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 return NULL;
289 }
buzbee408ad162012-06-06 16:45:18 -0700290 return genImmedCheck(cUnit, kCondEq, mReg, 0, kThrowNullPointer);
buzbee31a4a6f2012-02-28 15:36:15 -0800291}
292
293/* Perform check on two registers */
294LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700295 int reg1, int reg2, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800296{
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700298 cUnit->currentDalvikOffset, reg1, reg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700299 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 // Remember branch target - will process later
301 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
302 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800303}
304
buzbee3b3dbdd2012-06-13 13:39:34 -0700305void genCompareAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
306 RegLocation rlSrc1, RegLocation rlSrc2, LIR* taken,
307 LIR* fallThrough)
buzbee31a4a6f2012-02-28 15:36:15 -0800308{
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 ConditionCode cond;
310 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
311 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 switch (opcode) {
313 case Instruction::IF_EQ:
314 cond = kCondEq;
315 break;
316 case Instruction::IF_NE:
317 cond = kCondNe;
318 break;
319 case Instruction::IF_LT:
320 cond = kCondLt;
321 break;
322 case Instruction::IF_GE:
323 cond = kCondGe;
324 break;
325 case Instruction::IF_GT:
326 cond = kCondGt;
327 break;
328 case Instruction::IF_LE:
329 cond = kCondLe;
330 break;
331 default:
332 cond = (ConditionCode)0;
333 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
334 }
buzbee3b3dbdd2012-06-13 13:39:34 -0700335 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg, taken);
buzbee3b3dbdd2012-06-13 13:39:34 -0700336 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800337}
338
buzbee3b3dbdd2012-06-13 13:39:34 -0700339void genCompareZeroAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
340 RegLocation rlSrc, LIR* taken, LIR* fallThrough)
buzbee31a4a6f2012-02-28 15:36:15 -0800341{
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 ConditionCode cond;
343 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 switch (opcode) {
345 case Instruction::IF_EQZ:
346 cond = kCondEq;
347 break;
348 case Instruction::IF_NEZ:
349 cond = kCondNe;
350 break;
351 case Instruction::IF_LTZ:
352 cond = kCondLt;
353 break;
354 case Instruction::IF_GEZ:
355 cond = kCondGe;
356 break;
357 case Instruction::IF_GTZ:
358 cond = kCondGt;
359 break;
360 case Instruction::IF_LEZ:
361 cond = kCondLe;
362 break;
363 default:
364 cond = (ConditionCode)0;
365 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
366 }
buzbeeb046e162012-10-30 15:48:42 -0700367 if (cUnit->instructionSet == kThumb2) {
368 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
369 opCondBranch(cUnit, cond, taken);
370 } else {
371 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, taken);
372 }
buzbee3b3dbdd2012-06-13 13:39:34 -0700373 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800374}
375
buzbee408ad162012-06-06 16:45:18 -0700376void genIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800377 RegLocation rlSrc)
378{
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
380 if (rlSrc.location == kLocPhysReg) {
381 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
382 } else {
383 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
384 }
385 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
386 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800387}
388
buzbee408ad162012-06-06 16:45:18 -0700389void genIntNarrowing(CompilationUnit* cUnit, Instruction::Code opcode,
390 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800391{
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
393 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
394 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700395 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 case Instruction::INT_TO_BYTE:
397 op = kOp2Byte;
398 break;
399 case Instruction::INT_TO_SHORT:
400 op = kOp2Short;
401 break;
402 case Instruction::INT_TO_CHAR:
403 op = kOp2Char;
404 break;
405 default:
406 LOG(ERROR) << "Bad int conversion type";
407 }
408 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
409 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800410}
411
412/*
413 * Let helper function take care of everything. Will call
414 * Array::AllocFromCode(type_idx, method, count);
415 * Note: AllocFromCode will handle checks for errNegativeArraySize.
416 */
buzbee408ad162012-06-06 16:45:18 -0700417void genNewArray(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800418 RegLocation rlSrc)
419{
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 int funcOffset;
422 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 *cUnit->dex_file,
424 type_idx)) {
425 funcOffset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
426 } else {
427 funcOffset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
428 }
buzbee8320f382012-09-11 16:29:42 -0700429 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 RegLocation rlResult = oatGetReturn(cUnit, false);
431 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800432}
433
434/*
435 * Similar to genNewArray, but with post-allocation initialization.
436 * Verifier guarantees we're dealing with an array class. Current
437 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
438 * Current code also throws internal unimp if not 'L', '[' or 'I'.
439 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700440void genFilledNewArray(CompilationUnit* cUnit, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800441{
buzbee3b3dbdd2012-06-13 13:39:34 -0700442 int elems = info->numArgWords;
443 int typeIdx = info->index;
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 oatFlushAllRegs(cUnit); /* Everything to home location */
445 int funcOffset;
446 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 *cUnit->dex_file,
448 typeIdx)) {
449 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
450 } else {
451 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
452 }
buzbee8320f382012-09-11 16:29:42 -0700453 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 oatFreeTemp(cUnit, rARG2);
455 oatFreeTemp(cUnit, rARG1);
456 /*
457 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
458 * return region. Because AllocFromCode placed the new array
459 * in rRET0, we'll just lock it into place. When debugger support is
460 * added, it may be necessary to additionally copy all return
461 * values to a home location in thread-local storage
462 */
463 oatLockTemp(cUnit, rRET0);
464
465 // TODO: use the correct component size, currently all supported types
466 // share array alignment with ints (see comment at head of function)
467 size_t component_size = sizeof(int32_t);
468
469 // Having a range of 0 is legal
buzbee3b3dbdd2012-06-13 13:39:34 -0700470 if (info->isRange && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800471 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 * Bit of ugliness here. We're going generate a mem copy loop
473 * on the register range, but it is possible that some regs
474 * in the range have been promoted. This is unlikely, but
475 * before generating the copy, we'll just force a flush
476 * of any regs in the source range that have been promoted to
477 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800478 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700479 for (int i = 0; i < elems; i++) {
480 RegLocation loc = oatUpdateLoc(cUnit, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 if (loc.location == kLocPhysReg) {
482 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
483 loc.lowReg, kWord);
484 }
buzbee31a4a6f2012-02-28 15:36:15 -0800485 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 /*
487 * TUNING note: generated code here could be much improved, but
488 * this is an uncommon operation and isn't especially performance
489 * critical.
490 */
491 int rSrc = oatAllocTemp(cUnit);
492 int rDst = oatAllocTemp(cUnit);
493 int rIdx = oatAllocTemp(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700494 int rVal = INVALID_REG;
495 switch(cUnit->instructionSet) {
496 case kThumb2:
497 rVal = rLR;
498 break;
499 case kX86:
500 oatFreeTemp(cUnit, rRET0);
501 rVal = oatAllocTemp(cUnit);
502 break;
503 case kMips:
504 rVal = oatAllocTemp(cUnit);
505 break;
506 default: LOG(FATAL) << "Unexpected instruction set: " << cUnit->instructionSet;
507 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 // Set up source pointer
buzbee3b3dbdd2012-06-13 13:39:34 -0700509 RegLocation rlFirst = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
511 oatSRegOffset(cUnit, rlFirst.sRegLow));
512 // Set up the target pointer
513 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
514 Array::DataOffset(component_size).Int32Value());
515 // Set up the loop counter (known to be > 0)
buzbee3b3dbdd2012-06-13 13:39:34 -0700516 loadConstant(cUnit, rIdx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 // Generate the copy loop. Going backwards for convenience
518 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
519 // Copy next element
520 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
521 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 oatFreeTemp(cUnit, rVal);
buzbeeb046e162012-10-30 15:48:42 -0700523 opDecAndBranch(cUnit, kCondGe, rIdx, target);
524 if (cUnit->instructionSet == kX86) {
525 // Restore the target pointer
526 opRegRegImm(cUnit, kOpAdd, rRET0, rDst, -Array::DataOffset(component_size).Int32Value());
527 }
buzbee3b3dbdd2012-06-13 13:39:34 -0700528 } else if (!info->isRange) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700530 for (int i = 0; i < elems; i++) {
531 RegLocation rlArg = loadValue(cUnit, info->args[i], kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700532 storeBaseDisp(cUnit, rRET0,
533 Array::DataOffset(component_size).Int32Value() +
534 i * 4, rlArg.lowReg, kWord);
535 // If the loadValue caused a temp to be allocated, free it
536 if (oatIsTemp(cUnit, rlArg.lowReg)) {
537 oatFreeTemp(cUnit, rlArg.lowReg);
538 }
539 }
540 }
buzbeee5f01222012-06-14 15:19:35 -0700541 if (info->result.location != kLocInvalid) {
542 storeValue(cUnit, info->result, oatGetReturn(cUnit, false /* not fp */));
543 }
buzbee31a4a6f2012-02-28 15:36:15 -0800544}
545
buzbee408ad162012-06-06 16:45:18 -0700546void genSput(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700547 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800548{
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 int fieldOffset;
550 int ssbIndex;
551 bool isVolatile;
552 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800553
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700554 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker, *cUnit->dex_file,
555 cUnit->code_item, cUnit->method_idx, cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800556
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 bool fastPath =
558 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
559 fieldOffset, ssbIndex,
560 isReferrersClass, isVolatile,
561 true);
562 if (fastPath && !SLOW_FIELD_PATH) {
563 DCHECK_GE(fieldOffset, 0);
564 int rBase;
565 if (isReferrersClass) {
566 // Fast path, static storage base is this method's class
567 RegLocation rlMethod = loadCurrMethod(cUnit);
568 rBase = oatAllocTemp(cUnit);
569 loadWordDisp(cUnit, rlMethod.lowReg,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700570 AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
572 oatFreeTemp(cUnit, rlMethod.lowReg);
573 }
buzbee31a4a6f2012-02-28 15:36:15 -0800574 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 // Medium path, static storage base in a different class which
576 // requires checks that the other class is initialized.
577 DCHECK_GE(ssbIndex, 0);
578 // May do runtime call so everything to home locations.
579 oatFlushAllRegs(cUnit);
580 // Using fixed register to sync with possible call to runtime
581 // support.
582 int rMethod = rARG1;
583 oatLockTemp(cUnit, rMethod);
584 loadCurrMethodDirect(cUnit, rMethod);
585 rBase = rARG0;
586 oatLockTemp(cUnit, rBase);
587 loadWordDisp(cUnit, rMethod,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700588 AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700589 rBase);
590 loadWordDisp(cUnit, rBase,
591 Array::DataOffset(sizeof(Object*)).Int32Value() +
592 sizeof(int32_t*) * ssbIndex, rBase);
593 // rBase now points at appropriate static storage base (Class*)
594 // or NULL if not initialized. Check for NULL and call helper if NULL.
595 // TUNING: fast path should fall through
596 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
597 loadConstant(cUnit, rARG0, ssbIndex);
buzbee8320f382012-09-11 16:29:42 -0700598 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssbIndex, true);
buzbeeb046e162012-10-30 15:48:42 -0700599 if (cUnit->instructionSet == kMips) {
600 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
601 opRegCopy(cUnit, rBase, rRET0);
602 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700603 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
604 branchOver->target = (LIR*)skipTarget;
605 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800606 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 // rBase now holds static storage base
608 if (isLongOrDouble) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
610 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700611 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
612 }
613//FIXME: need to generalize the barrier call
614 if (isVolatile) {
615 oatGenMemBarrier(cUnit, kST);
616 }
617 if (isLongOrDouble) {
618 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
619 rlSrc.highReg);
620 } else {
621 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
622 }
623 if (isVolatile) {
624 oatGenMemBarrier(cUnit, kSY);
625 }
626 if (isObject) {
627 markGCCard(cUnit, rlSrc.lowReg, rBase);
628 }
629 oatFreeTemp(cUnit, rBase);
630 } else {
631 oatFlushAllRegs(cUnit); // Everything to home locations
632 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Static) :
633 (isObject ? ENTRYPOINT_OFFSET(pSetObjStatic)
634 : ENTRYPOINT_OFFSET(pSet32Static));
buzbee8320f382012-09-11 16:29:42 -0700635 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 }
buzbee31a4a6f2012-02-28 15:36:15 -0800637}
638
buzbee408ad162012-06-06 16:45:18 -0700639void genSget(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800641{
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 int fieldOffset;
643 int ssbIndex;
644 bool isVolatile;
645 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800646
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 *cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700649 cUnit->code_item, cUnit->method_idx,
650 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800651
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 bool fastPath =
653 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
654 fieldOffset, ssbIndex,
655 isReferrersClass, isVolatile,
656 false);
657 if (fastPath && !SLOW_FIELD_PATH) {
658 DCHECK_GE(fieldOffset, 0);
659 int rBase;
660 if (isReferrersClass) {
661 // Fast path, static storage base is this method's class
662 RegLocation rlMethod = loadCurrMethod(cUnit);
663 rBase = oatAllocTemp(cUnit);
664 loadWordDisp(cUnit, rlMethod.lowReg,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700665 AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800666 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700667 // Medium path, static storage base in a different class which
668 // requires checks that the other class is initialized
669 DCHECK_GE(ssbIndex, 0);
670 // May do runtime call so everything to home locations.
671 oatFlushAllRegs(cUnit);
672 // Using fixed register to sync with possible call to runtime
673 // support
674 int rMethod = rARG1;
675 oatLockTemp(cUnit, rMethod);
676 loadCurrMethodDirect(cUnit, rMethod);
677 rBase = rARG0;
678 oatLockTemp(cUnit, rBase);
679 loadWordDisp(cUnit, rMethod,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700680 AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 rBase);
682 loadWordDisp(cUnit, rBase,
683 Array::DataOffset(sizeof(Object*)).Int32Value() +
684 sizeof(int32_t*) * ssbIndex, rBase);
685 // rBase now points at appropriate static storage base (Class*)
686 // or NULL if not initialized. Check for NULL and call helper if NULL.
687 // TUNING: fast path should fall through
688 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee8320f382012-09-11 16:29:42 -0700689 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssbIndex, true);
buzbeeb046e162012-10-30 15:48:42 -0700690 if (cUnit->instructionSet == kMips) {
691 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
692 opRegCopy(cUnit, rBase, rRET0);
693 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700694 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
695 branchOver->target = (LIR*)skipTarget;
696 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800697 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 // rBase now holds static storage base
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
700 if (isVolatile) {
701 oatGenMemBarrier(cUnit, kSY);
702 }
703 if (isLongOrDouble) {
buzbee408ad162012-06-06 16:45:18 -0700704 loadBaseDispWide(cUnit, rBase, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 rlResult.highReg, INVALID_SREG);
706 } else {
707 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
708 }
709 oatFreeTemp(cUnit, rBase);
710 if (isLongOrDouble) {
711 storeValueWide(cUnit, rlDest, rlResult);
712 } else {
713 storeValue(cUnit, rlDest, rlResult);
714 }
715 } else {
716 oatFlushAllRegs(cUnit); // Everything to home locations
717 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Static) :
718 (isObject ? ENTRYPOINT_OFFSET(pGetObjStatic)
719 : ENTRYPOINT_OFFSET(pGet32Static));
buzbee8320f382012-09-11 16:29:42 -0700720 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 if (isLongOrDouble) {
722 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
723 storeValueWide(cUnit, rlDest, rlResult);
724 } else {
725 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
726 storeValue(cUnit, rlDest, rlResult);
727 }
728 }
buzbee31a4a6f2012-02-28 15:36:15 -0800729}
730
731
732// Debugging routine - if null target, branch to DebugMe
733void genShowTarget(CompilationUnit* cUnit)
734{
buzbeeb046e162012-10-30 15:48:42 -0700735 DCHECK_NE(cUnit->instructionSet, kX86) << "unimplemented genShowTarget";
Bill Buzbeea114add2012-05-03 15:00:40 -0700736 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
737 loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pDebugMe), rINVOKE_TGT);
738 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
739 branchOver->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -0800740}
741
buzbee31a4a6f2012-02-28 15:36:15 -0800742void handleSuspendLaunchpads(CompilationUnit *cUnit)
743{
Bill Buzbeea114add2012-05-03 15:00:40 -0700744 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
745 int numElems = cUnit->suspendLaunchpads.numUsed;
buzbeeb046e162012-10-30 15:48:42 -0700746 int helperOffset = ENTRYPOINT_OFFSET(pTestSuspendFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700747 for (int i = 0; i < numElems; i++) {
748 oatResetRegPool(cUnit);
749 oatResetDefTracking(cUnit);
750 LIR* lab = suspendLabel[i];
751 LIR* resumeLab = (LIR*)lab->operands[0];
752 cUnit->currentDalvikOffset = lab->operands[1];
753 oatAppendLIR(cUnit, lab);
buzbeeb046e162012-10-30 15:48:42 -0700754 int rTgt = callHelperSetup(cUnit, helperOffset);
755 callHelper(cUnit, rTgt, helperOffset, true /* markSafepointPC */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700756 opUnconditionalBranch(cUnit, resumeLab);
757 }
buzbee31a4a6f2012-02-28 15:36:15 -0800758}
759
buzbeefc9e6fa2012-03-23 15:14:29 -0700760void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
761{
Bill Buzbeea114add2012-05-03 15:00:40 -0700762 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
763 int numElems = cUnit->intrinsicLaunchpads.numUsed;
764 for (int i = 0; i < numElems; i++) {
765 oatResetRegPool(cUnit);
766 oatResetDefTracking(cUnit);
767 LIR* lab = intrinsicLabel[i];
buzbee3b3dbdd2012-06-13 13:39:34 -0700768 CallInfo* info = (CallInfo*)lab->operands[0];
buzbee15bf9802012-06-12 17:49:27 -0700769 cUnit->currentDalvikOffset = info->offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700770 oatAppendLIR(cUnit, lab);
buzbee8320f382012-09-11 16:29:42 -0700771 // NOTE: genInvoke handles markSafepointPC
buzbee15bf9802012-06-12 17:49:27 -0700772 genInvoke(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 LIR* resumeLab = (LIR*)lab->operands[2];
774 if (resumeLab != NULL) {
775 opUnconditionalBranch(cUnit, resumeLab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700776 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700777 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700778}
779
buzbee31a4a6f2012-02-28 15:36:15 -0800780void handleThrowLaunchpads(CompilationUnit *cUnit)
781{
Bill Buzbeea114add2012-05-03 15:00:40 -0700782 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
783 int numElems = cUnit->throwLaunchpads.numUsed;
784 for (int i = 0; i < numElems; i++) {
785 oatResetRegPool(cUnit);
786 oatResetDefTracking(cUnit);
787 LIR* lab = throwLabel[i];
788 cUnit->currentDalvikOffset = lab->operands[1];
789 oatAppendLIR(cUnit, lab);
790 int funcOffset = 0;
791 int v1 = lab->operands[2];
792 int v2 = lab->operands[3];
buzbeeb046e162012-10-30 15:48:42 -0700793 bool targetX86 = (cUnit->instructionSet == kX86);
Bill Buzbeea114add2012-05-03 15:00:40 -0700794 switch (lab->operands[0]) {
795 case kThrowNullPointer:
796 funcOffset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
797 break;
798 case kThrowArrayBounds:
jeffhao44335e12012-06-18 13:48:25 -0700799 // Move v1 (array index) to rARG0 and v2 (array length) to rARG1
Bill Buzbeea114add2012-05-03 15:00:40 -0700800 if (v2 != rARG0) {
801 opRegCopy(cUnit, rARG0, v1);
buzbeeb046e162012-10-30 15:48:42 -0700802 if (targetX86) {
803 // x86 leaves the array pointer in v2, so load the array length that the handler expects
804 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
805 } else {
806 opRegCopy(cUnit, rARG1, v2);
807 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700808 } else {
809 if (v1 == rARG1) {
jeffhao44335e12012-06-18 13:48:25 -0700810 // Swap v1 and v2, using rARG2 as a temp
811 opRegCopy(cUnit, rARG2, v1);
buzbeeb046e162012-10-30 15:48:42 -0700812 if (targetX86) {
813 // x86 leaves the array pointer in v2; load the array length that the handler expects
814 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
815 } else {
816 opRegCopy(cUnit, rARG1, v2);
817 }
jeffhao44335e12012-06-18 13:48:25 -0700818 opRegCopy(cUnit, rARG0, rARG2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700819 } else {
buzbeeb046e162012-10-30 15:48:42 -0700820 if (targetX86) {
821 // x86 leaves the array pointer in v2; load the array length that the handler expects
822 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
823 } else {
824 opRegCopy(cUnit, rARG1, v2);
825 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700826 opRegCopy(cUnit, rARG0, v1);
827 }
buzbee31a4a6f2012-02-28 15:36:15 -0800828 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 funcOffset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
830 break;
831 case kThrowDivZero:
832 funcOffset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
833 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 case kThrowNoSuchMethod:
835 opRegCopy(cUnit, rARG0, v1);
836 funcOffset =
837 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
838 break;
839 case kThrowStackOverflow:
840 funcOffset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
841 // Restore stack alignment
buzbeeb046e162012-10-30 15:48:42 -0700842 if (targetX86) {
843 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
844 } else {
845 opRegImm(cUnit, kOpAdd, rSP, (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
846 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 break;
848 default:
849 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800850 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700851 oatClobberCalleeSave(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700852 int rTgt = callHelperSetup(cUnit, funcOffset);
853 callHelper(cUnit, rTgt, funcOffset, true /* markSafepointPC */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700854 }
buzbee31a4a6f2012-02-28 15:36:15 -0800855}
856
857/* Needed by the Assembler */
buzbeeb046e162012-10-30 15:48:42 -0700858void oatSetupResourceMasks(CompilationUnit* cUnit, LIR* lir)
buzbee31a4a6f2012-02-28 15:36:15 -0800859{
buzbeeb046e162012-10-30 15:48:42 -0700860 setupResourceMasks(cUnit, lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800861}
862
buzbee16da88c2012-03-20 10:38:17 -0700863bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
864 int& fieldOffset, bool& isVolatile, bool isPut)
865{
Bill Buzbeea114add2012-05-03 15:00:40 -0700866 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 *cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700868 cUnit->code_item, cUnit->method_idx,
869 cUnit->access_flags);
870 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
871 fieldOffset, isVolatile, isPut);
buzbee16da88c2012-03-20 10:38:17 -0700872}
873
buzbee408ad162012-06-06 16:45:18 -0700874void genIGet(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -0800875 RegLocation rlDest, RegLocation rlObj,
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800877{
Bill Buzbeea114add2012-05-03 15:00:40 -0700878 int fieldOffset;
879 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800880
Bill Buzbeea114add2012-05-03 15:00:40 -0700881 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800882
Bill Buzbeea114add2012-05-03 15:00:40 -0700883 if (fastPath && !SLOW_FIELD_PATH) {
884 RegLocation rlResult;
885 RegisterClass regClass = oatRegClassBySize(size);
886 DCHECK_GE(fieldOffset, 0);
887 rlObj = loadValue(cUnit, rlObj, kCoreReg);
888 if (isLongOrDouble) {
889 DCHECK(rlDest.wide);
buzbee408ad162012-06-06 16:45:18 -0700890 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
buzbeeb046e162012-10-30 15:48:42 -0700891 if (cUnit->instructionSet == kX86) {
892 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
893 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
894 loadBaseDispWide(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
895 rlResult.highReg, rlObj.sRegLow);
896 if (isVolatile) {
897 oatGenMemBarrier(cUnit, kSY);
898 }
899 } else {
900 int regPtr = oatAllocTemp(cUnit);
901 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
902 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
903 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
904 if (isVolatile) {
905 oatGenMemBarrier(cUnit, kSY);
906 }
907 oatFreeTemp(cUnit, regPtr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700909 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800910 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -0700912 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
913 loadBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700914 kWord, rlObj.sRegLow);
915 if (isVolatile) {
916 oatGenMemBarrier(cUnit, kSY);
917 }
918 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800919 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700920 } else {
921 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Instance) :
922 (isObject ? ENTRYPOINT_OFFSET(pGetObjInstance)
923 : ENTRYPOINT_OFFSET(pGet32Instance));
buzbee8320f382012-09-11 16:29:42 -0700924 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700925 if (isLongOrDouble) {
926 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
927 storeValueWide(cUnit, rlDest, rlResult);
928 } else {
929 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
930 storeValue(cUnit, rlDest, rlResult);
931 }
932 }
buzbee31a4a6f2012-02-28 15:36:15 -0800933}
934
buzbee408ad162012-06-06 16:45:18 -0700935void genIPut(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
936 RegLocation rlSrc, RegLocation rlObj, bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800937{
Bill Buzbeea114add2012-05-03 15:00:40 -0700938 int fieldOffset;
939 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800940
Bill Buzbeea114add2012-05-03 15:00:40 -0700941 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
942 true);
943 if (fastPath && !SLOW_FIELD_PATH) {
944 RegisterClass regClass = oatRegClassBySize(size);
945 DCHECK_GE(fieldOffset, 0);
946 rlObj = loadValue(cUnit, rlObj, kCoreReg);
947 if (isLongOrDouble) {
948 int regPtr;
949 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee408ad162012-06-06 16:45:18 -0700950 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700951 regPtr = oatAllocTemp(cUnit);
952 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
953 if (isVolatile) {
954 oatGenMemBarrier(cUnit, kST);
955 }
jeffhao41005dd2012-05-09 17:58:52 -0700956 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 if (isVolatile) {
958 oatGenMemBarrier(cUnit, kSY);
959 }
960 oatFreeTemp(cUnit, regPtr);
buzbee31a4a6f2012-02-28 15:36:15 -0800961 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee408ad162012-06-06 16:45:18 -0700963 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 if (isVolatile) {
965 oatGenMemBarrier(cUnit, kST);
966 }
967 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
968 if (isVolatile) {
969 oatGenMemBarrier(cUnit, kSY);
970 }
971 if (isObject) {
972 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
973 }
buzbee31a4a6f2012-02-28 15:36:15 -0800974 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700975 } else {
976 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Instance) :
977 (isObject ? ENTRYPOINT_OFFSET(pSetObjInstance)
978 : ENTRYPOINT_OFFSET(pSet32Instance));
buzbee8320f382012-09-11 16:29:42 -0700979 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset, fieldIdx, rlObj, rlSrc, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 }
buzbee31a4a6f2012-02-28 15:36:15 -0800981}
982
buzbee6969d502012-06-15 16:40:31 -0700983void genConstClass(CompilationUnit* cUnit, uint32_t type_idx,
984 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -0800985{
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 RegLocation rlMethod = loadCurrMethod(cUnit);
987 int resReg = oatAllocTemp(cUnit);
988 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
989 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700990 *cUnit->dex_file,
991 type_idx)) {
992 // Call out to helper which resolves type and verifies access.
993 // Resolved type returned in rRET0.
buzbee8320f382012-09-11 16:29:42 -0700994 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
995 type_idx, rlMethod.lowReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 RegLocation rlResult = oatGetReturn(cUnit, false);
997 storeValue(cUnit, rlDest, rlResult);
998 } else {
999 // We're don't need access checks, load type from dex cache
1000 int32_t dex_cache_offset =
Mathieu Chartier66f19252012-09-18 08:57:04 -07001001 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -07001002 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
1003 int32_t offset_of_type =
1004 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1005 * type_idx);
1006 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(*cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 type_idx) || SLOW_TYPE_PATH) {
1009 // Slow path, at runtime test if type is null and if so initialize
1010 oatFlushAllRegs(cUnit);
1011 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, NULL);
1012 // Resolved, store and hop over following code
1013 storeValue(cUnit, rlDest, rlResult);
1014 /*
1015 * Because we have stores of the target value on two paths,
1016 * clobber temp tracking for the destination using the ssa name
1017 */
1018 oatClobberSReg(cUnit, rlDest.sRegLow);
1019 LIR* branch2 = opUnconditionalBranch(cUnit,0);
1020 // TUNING: move slow path to end & remove unconditional branch
1021 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
1022 // Call out to helper, which will return resolved type in rARG0
buzbee8320f382012-09-11 16:29:42 -07001023 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx,
1024 rlMethod.lowReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001025 RegLocation rlResult = oatGetReturn(cUnit, false);
1026 storeValue(cUnit, rlDest, rlResult);
1027 /*
1028 * Because we have stores of the target value on two paths,
1029 * clobber temp tracking for the destination using the ssa name
1030 */
1031 oatClobberSReg(cUnit, rlDest.sRegLow);
1032 // Rejoin code paths
1033 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
1034 branch1->target = (LIR*)target1;
1035 branch2->target = (LIR*)target2;
buzbee31a4a6f2012-02-28 15:36:15 -08001036 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001037 // Fast path, we're done - just store result
1038 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001039 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 }
buzbee31a4a6f2012-02-28 15:36:15 -08001041}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001042
buzbee6969d502012-06-15 16:40:31 -07001043void genConstString(CompilationUnit* cUnit, uint32_t string_idx,
1044 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001045{
Bill Buzbeea114add2012-05-03 15:00:40 -07001046 /* NOTE: Most strings should be available at compile time */
Bill Buzbeea114add2012-05-03 15:00:40 -07001047 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1048 (sizeof(String*) * string_idx);
1049 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 *cUnit->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001051 // slow path, resolve string if not in dex cache
1052 oatFlushAllRegs(cUnit);
1053 oatLockCallTemps(cUnit); // Using explicit registers
1054 loadCurrMethodDirect(cUnit, rARG2);
1055 loadWordDisp(cUnit, rARG2,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001056 AbstractMethod::DexCacheStringsOffset().Int32Value(), rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001057 // Might call out to helper, which will return resolved string in rRET0
buzbeeb046e162012-10-30 15:48:42 -07001058 int rTgt = callHelperSetup(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
jeffhaofa147e22012-10-12 17:03:32 -07001059 loadWordDisp(cUnit, rARG0, offset_of_string, rRET0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001060 loadConstant(cUnit, rARG1, string_idx);
buzbeeb046e162012-10-30 15:48:42 -07001061 if (cUnit->instructionSet == kThumb2) {
1062 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1063 genBarrier(cUnit);
1064 // For testing, always force through helper
1065 if (!EXERCISE_SLOWEST_STRING_PATH) {
1066 opIT(cUnit, kArmCondEq, "T");
1067 }
1068 opRegCopy(cUnit, rARG0, rARG2); // .eq
1069 LIR* callInst = opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
1070 markSafepointPC(cUnit, callInst);
1071 oatFreeTemp(cUnit, rTgt);
1072 } else if (cUnit->instructionSet == kMips) {
1073 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1074 opRegCopy(cUnit, rARG0, rARG2); // .eq
1075 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
1076 markSafepointPC(cUnit, callInst);
1077 oatFreeTemp(cUnit, rTgt);
1078 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1079 branch->target = target;
1080 } else {
1081 DCHECK_EQ(cUnit->instructionSet, kX86);
1082 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode), rARG2, rARG1, true);
buzbee31a4a6f2012-02-28 15:36:15 -08001083 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001084 genBarrier(cUnit);
1085 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
1086 } else {
1087 RegLocation rlMethod = loadCurrMethod(cUnit);
1088 int resReg = oatAllocTemp(cUnit);
1089 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1090 loadWordDisp(cUnit, rlMethod.lowReg,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001091 AbstractMethod::DexCacheStringsOffset().Int32Value(), resReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001092 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1093 storeValue(cUnit, rlDest, rlResult);
1094 }
buzbee31a4a6f2012-02-28 15:36:15 -08001095}
1096
1097/*
1098 * Let helper function take care of everything. Will
1099 * call Class::NewInstanceFromCode(type_idx, method);
1100 */
buzbee408ad162012-06-06 16:45:18 -07001101void genNewInstance(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001102{
Bill Buzbeea114add2012-05-03 15:00:40 -07001103 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -07001104 // alloc will always check for resolution, do we also need to verify
1105 // access because the verifier was unable to?
1106 int funcOffset;
1107 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 cUnit->method_idx, *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001109 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
1110 } else {
1111 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
1112 }
buzbee8320f382012-09-11 16:29:42 -07001113 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001114 RegLocation rlResult = oatGetReturn(cUnit, false);
1115 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001116}
1117
Ian Rogers474b6da2012-09-25 00:20:38 -07001118void genMoveException(CompilationUnit* cUnit, RegLocation rlDest)
1119{
1120 oatFlushAllRegs(cUnit); /* Everything to home location */
1121 int funcOffset = ENTRYPOINT_OFFSET(pGetAndClearException);
buzbeeb046e162012-10-30 15:48:42 -07001122 if (cUnit->instructionSet == kX86) {
1123 // Runtime helper will load argument for x86.
1124 callRuntimeHelperReg(cUnit, funcOffset, rARG0, false);
1125 } else {
1126 callRuntimeHelperReg(cUnit, funcOffset, rSELF, false);
1127 }
Ian Rogers474b6da2012-09-25 00:20:38 -07001128 RegLocation rlResult = oatGetReturn(cUnit, false);
1129 storeValue(cUnit, rlDest, rlResult);
1130}
1131
buzbee408ad162012-06-06 16:45:18 -07001132void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001133{
Bill Buzbeea114add2012-05-03 15:00:40 -07001134 oatFlushAllRegs(cUnit);
buzbee8320f382012-09-11 16:29:42 -07001135 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException), rlSrc, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001136}
1137
buzbee408ad162012-06-06 16:45:18 -07001138void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001139 RegLocation rlSrc)
1140{
Bill Buzbeea114add2012-05-03 15:00:40 -07001141 oatFlushAllRegs(cUnit);
1142 // May generate a call - use explicit registers
1143 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001144 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1145 int classReg = rARG2; // rARG2 will hold the Class*
1146 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001147 *cUnit->dex_file,
1148 type_idx)) {
1149 // Check we have access to type_idx and if not throw IllegalAccessError,
1150 // returns Class* in rARG0
buzbee8320f382012-09-11 16:29:42 -07001151 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1152 type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001153 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1154 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1155 } else {
1156 // Load dex cache entry into classReg (rARG2)
1157 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1158 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001159 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001160 int32_t offset_of_type =
1161 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1162 * type_idx);
1163 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1164 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 // Need to test presence of type in dex cache at runtime
1167 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1168 // Not resolved
1169 // Call out to helper, which will return resolved type in rRET0
buzbee8320f382012-09-11 16:29:42 -07001170 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001171 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1172 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1173 // Rejoin code paths
1174 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1175 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001176 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001177 }
1178 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
jeffhao4eb68ed2012-10-17 16:41:07 -07001179 RegLocation rlResult = oatGetReturn(cUnit, false);
buzbeeb046e162012-10-30 15:48:42 -07001180 if (cUnit->instructionSet == kMips) {
1181 loadConstant(cUnit, rlResult.lowReg, 0); // store false result for if branch is taken
1182 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001183 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1184 /* load object->klass_ */
1185 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1186 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1187 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee8320f382012-09-11 16:29:42 -07001188 LIR* callInst;
buzbeeb046e162012-10-30 15:48:42 -07001189 LIR* branchover = NULL;
1190 if (cUnit->instructionSet == kThumb2) {
1191 /* Uses conditional nullification */
1192 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1193 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1194 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1195 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1196 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1197 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1198 oatFreeTemp(cUnit, rTgt);
1199 } else {
1200 /* Uses branchovers */
1201 loadConstant(cUnit, rlResult.lowReg, 1); // assume true
1202 branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
1203 if (cUnit->instructionSet != kX86) {
1204 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1205 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1206 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1207 oatFreeTemp(cUnit, rTgt);
1208 } else {
1209 opRegCopy(cUnit, rARG0, rARG2);
1210 callInst = opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1211 }
1212 }
buzbee8320f382012-09-11 16:29:42 -07001213 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 oatClobberCalleeSave(cUnit);
1215 /* branch targets here */
1216 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001217 storeValue(cUnit, rlDest, rlResult);
1218 branch1->target = target;
buzbeeb046e162012-10-30 15:48:42 -07001219 if (cUnit->instructionSet != kThumb2) {
1220 branchover->target = target;
1221 }
buzbee31a4a6f2012-02-28 15:36:15 -08001222}
1223
buzbee408ad162012-06-06 16:45:18 -07001224void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001225{
Bill Buzbeea114add2012-05-03 15:00:40 -07001226 oatFlushAllRegs(cUnit);
1227 // May generate a call - use explicit registers
1228 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001229 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1230 int classReg = rARG2; // rARG2 will hold the Class*
1231 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001232 *cUnit->dex_file,
1233 type_idx)) {
1234 // Check we have access to type_idx and if not throw IllegalAccessError,
1235 // returns Class* in rRET0
1236 // InitializeTypeAndVerifyAccess(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001237 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1238 type_idx, rARG1, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001239 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1240 } else {
1241 // Load dex cache entry into classReg (rARG2)
1242 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001243 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001244 int32_t offset_of_type =
1245 Array::DataOffset(sizeof(Class*)).Int32Value() +
1246 (sizeof(Class*) * type_idx);
1247 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1248 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001250 // Need to test presence of type in dex cache at runtime
1251 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1252 // Not resolved
1253 // Call out to helper, which will return resolved type in rARG0
1254 // InitializeTypeFromCode(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001255 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, rARG1,
1256 true);
jeffhao30a33172012-10-22 18:16:22 -07001257 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001258 // Rejoin code paths
1259 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1260 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001261 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001262 }
1263 // At this point, classReg (rARG2) has class
1264 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1265 /* Null is OK - continue */
1266 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1267 /* load object->klass_ */
1268 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1269 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1270 /* rARG1 now contains object->klass_ */
buzbeeb046e162012-10-30 15:48:42 -07001271 LIR* branch2;
1272 if (cUnit->instructionSet == kThumb2) {
1273 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1274 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1275 branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1276 opRegCopy(cUnit, rARG0, rARG1);
1277 opRegCopy(cUnit, rARG1, rARG2);
1278 oatClobberCalleeSave(cUnit);
1279 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
1280 markSafepointPC(cUnit, callInst);
1281 oatFreeTemp(cUnit, rTgt);
1282 } else {
1283 branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
1284 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode), rARG1, rARG2, true);
1285 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001286 /* branch target here */
1287 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1288 branch1->target = target;
1289 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001290}
1291
buzbee31a4a6f2012-02-28 15:36:15 -08001292/*
1293 * Generate array store
1294 *
1295 */
buzbee408ad162012-06-06 16:45:18 -07001296void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001297 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001298{
Bill Buzbeea114add2012-05-03 15:00:40 -07001299 int lenOffset = Array::LengthOffset().Int32Value();
1300 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001301
Bill Buzbeea114add2012-05-03 15:00:40 -07001302 oatFlushAllRegs(cUnit); // Use explicit registers
1303 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001304
Bill Buzbeea114add2012-05-03 15:00:40 -07001305 int rValue = rARG0; // Register holding value
1306 int rArrayClass = rARG1; // Register holding array's Class
1307 int rArray = rARG2; // Register holding array
1308 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001309
Bill Buzbeea114add2012-05-03 15:00:40 -07001310 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1311 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1312 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001313
buzbee408ad162012-06-06 16:45:18 -07001314 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001315
Bill Buzbeea114add2012-05-03 15:00:40 -07001316 // Store of null?
1317 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001318
Bill Buzbeea114add2012-05-03 15:00:40 -07001319 // Get the array's class.
1320 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
buzbee8320f382012-09-11 16:29:42 -07001321 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode), rValue,
1322 rArrayClass, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001323 // Redo loadValues in case they didn't survive the call.
1324 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1325 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1326 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1327 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001328
Bill Buzbeea114add2012-05-03 15:00:40 -07001329 // Branch here if value to be stored == null
1330 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1331 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001332
buzbeeb046e162012-10-30 15:48:42 -07001333 if (cUnit->instructionSet == kX86) {
1334 // make an extra temp available for card mark below
1335 oatFreeTemp(cUnit, rARG1);
1336 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
1337 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1338 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
1339 }
1340 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
1341 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
1342 } else {
1343 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
1344 int regLen = INVALID_REG;
1345 if (needsRangeCheck) {
1346 regLen = rARG1;
1347 loadWordDisp(cUnit, rArray, lenOffset, regLen); // Get len
1348 }
1349 /* rPtr -> array data */
1350 int rPtr = oatAllocTemp(cUnit);
1351 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1352 if (needsRangeCheck) {
1353 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
1354 }
1355 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1356 oatFreeTemp(cUnit, rPtr);
Bill Buzbeea114add2012-05-03 15:00:40 -07001357 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001358 oatFreeTemp(cUnit, rIndex);
1359 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001360}
1361
1362/*
1363 * Generate array load
1364 */
buzbee408ad162012-06-06 16:45:18 -07001365void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001366 RegLocation rlArray, RegLocation rlIndex,
1367 RegLocation rlDest, int scale)
1368{
Bill Buzbeea114add2012-05-03 15:00:40 -07001369 RegisterClass regClass = oatRegClassBySize(size);
1370 int lenOffset = Array::LengthOffset().Int32Value();
1371 int dataOffset;
1372 RegLocation rlResult;
1373 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1374 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001375
Bill Buzbeea114add2012-05-03 15:00:40 -07001376 if (size == kLong || size == kDouble) {
1377 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1378 } else {
1379 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1380 }
buzbee31a4a6f2012-02-28 15:36:15 -08001381
Bill Buzbeea114add2012-05-03 15:00:40 -07001382 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001383 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001384
buzbeeb046e162012-10-30 15:48:42 -07001385 if (cUnit->instructionSet == kX86) {
1386 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
1387 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1388 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1389 lenOffset, kThrowArrayBounds);
1390 }
1391 if ((size == kLong) || (size == kDouble)) {
1392 int regAddr = oatAllocTemp(cUnit);
1393 opLea(cUnit, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1394 oatFreeTemp(cUnit, rlArray.lowReg);
1395 oatFreeTemp(cUnit, rlIndex.lowReg);
1396 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1397 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
1398 rlResult.highReg, size, INVALID_SREG);
1399 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001400 } else {
buzbeeb046e162012-10-30 15:48:42 -07001401 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001402
buzbeeb046e162012-10-30 15:48:42 -07001403 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1404 dataOffset, rlResult.lowReg, INVALID_REG, size,
1405 INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001406
buzbeeb046e162012-10-30 15:48:42 -07001407 storeValue(cUnit, rlDest, rlResult);
1408 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001409 } else {
buzbeeb046e162012-10-30 15:48:42 -07001410 int regPtr = oatAllocTemp(cUnit);
1411 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
1412 int regLen = INVALID_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -07001413 if (needsRangeCheck) {
buzbeeb046e162012-10-30 15:48:42 -07001414 regLen = oatAllocTemp(cUnit);
1415 /* Get len */
1416 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
Bill Buzbeea114add2012-05-03 15:00:40 -07001417 }
buzbeeb046e162012-10-30 15:48:42 -07001418 /* regPtr -> array data */
1419 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1420 oatFreeTemp(cUnit, rlArray.lowReg);
1421 if ((size == kLong) || (size == kDouble)) {
1422 if (scale) {
1423 int rNewIndex = oatAllocTemp(cUnit);
1424 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1425 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1426 oatFreeTemp(cUnit, rNewIndex);
1427 } else {
1428 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1429 }
1430 oatFreeTemp(cUnit, rlIndex.lowReg);
1431 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001432
buzbeeb046e162012-10-30 15:48:42 -07001433 if (needsRangeCheck) {
1434 // TODO: change kCondCS to a more meaningful name, is the sense of
1435 // carry-set/clear flipped?
1436 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
1437 oatFreeTemp(cUnit, regLen);
1438 }
1439 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1440
1441 oatFreeTemp(cUnit, regPtr);
1442 storeValueWide(cUnit, rlDest, rlResult);
1443 } else {
1444 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1445
1446 if (needsRangeCheck) {
1447 // TODO: change kCondCS to a more meaningful name, is the sense of
1448 // carry-set/clear flipped?
1449 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
1450 oatFreeTemp(cUnit, regLen);
1451 }
1452 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg, scale, size);
1453
1454 oatFreeTemp(cUnit, regPtr);
1455 storeValue(cUnit, rlDest, rlResult);
1456 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001457 }
buzbee31a4a6f2012-02-28 15:36:15 -08001458}
1459
1460/*
1461 * Generate array store
1462 *
1463 */
buzbee408ad162012-06-06 16:45:18 -07001464void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001465 RegLocation rlArray, RegLocation rlIndex,
1466 RegLocation rlSrc, int scale)
1467{
Bill Buzbeea114add2012-05-03 15:00:40 -07001468 RegisterClass regClass = oatRegClassBySize(size);
1469 int lenOffset = Array::LengthOffset().Int32Value();
1470 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001471
Bill Buzbeea114add2012-05-03 15:00:40 -07001472 if (size == kLong || size == kDouble) {
1473 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1474 } else {
1475 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1476 }
buzbee31a4a6f2012-02-28 15:36:15 -08001477
Bill Buzbeea114add2012-05-03 15:00:40 -07001478 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1479 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbeeb046e162012-10-30 15:48:42 -07001480 int regPtr = INVALID_REG;
1481 if (cUnit->instructionSet != kX86) {
1482 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1483 oatClobber(cUnit, rlArray.lowReg);
1484 regPtr = rlArray.lowReg;
1485 } else {
1486 regPtr = oatAllocTemp(cUnit);
1487 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1488 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001489 }
buzbee31a4a6f2012-02-28 15:36:15 -08001490
Bill Buzbeea114add2012-05-03 15:00:40 -07001491 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001492 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001493
buzbeeb046e162012-10-30 15:48:42 -07001494 if (cUnit->instructionSet == kX86) {
1495 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
1496 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1497 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg, lenOffset, kThrowArrayBounds);
1498 }
1499 if ((size == kLong) || (size == kDouble)) {
1500 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
buzbee31a4a6f2012-02-28 15:36:15 -08001501 } else {
buzbeeb046e162012-10-30 15:48:42 -07001502 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee31a4a6f2012-02-28 15:36:15 -08001503 }
buzbeeb046e162012-10-30 15:48:42 -07001504 // If the src reg can't be byte accessed, move it to a temp first.
1505 if ((size == kSignedByte || size == kUnsignedByte) && rlSrc.lowReg >= 4) {
1506 int temp = oatAllocTemp(cUnit);
1507 opRegCopy(cUnit, temp, rlSrc.lowReg);
1508 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset, temp,
1509 INVALID_REG, size, INVALID_SREG);
1510 } else {
1511 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset, rlSrc.lowReg,
1512 rlSrc.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001513 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001514 } else {
buzbeeb046e162012-10-30 15:48:42 -07001515 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
1516 int regLen = INVALID_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -07001517 if (needsRangeCheck) {
buzbeeb046e162012-10-30 15:48:42 -07001518 regLen = oatAllocTemp(cUnit);
1519 //NOTE: max live temps(4) here.
1520 /* Get len */
1521 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
Bill Buzbeea114add2012-05-03 15:00:40 -07001522 }
buzbeeb046e162012-10-30 15:48:42 -07001523 /* regPtr -> array data */
1524 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1525 /* at this point, regPtr points to array, 2 live temps */
1526 if ((size == kLong) || (size == kDouble)) {
1527 //TUNING: specific wide routine that can handle fp regs
1528 if (scale) {
1529 int rNewIndex = oatAllocTemp(cUnit);
1530 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1531 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1532 oatFreeTemp(cUnit, rNewIndex);
1533 } else {
1534 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1535 }
1536 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1537
1538 if (needsRangeCheck) {
1539 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
1540 oatFreeTemp(cUnit, regLen);
1541 }
1542
1543 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
1544
1545 oatFreeTemp(cUnit, regPtr);
1546 } else {
1547 rlSrc = loadValue(cUnit, rlSrc, regClass);
1548 if (needsRangeCheck) {
1549 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
1550 oatFreeTemp(cUnit, regLen);
1551 }
1552 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1553 scale, size);
1554 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001555 }
buzbee31a4a6f2012-02-28 15:36:15 -08001556}
1557
buzbee408ad162012-06-06 16:45:18 -07001558void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001559 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001560 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001561{
Bill Buzbeea114add2012-05-03 15:00:40 -07001562 RegLocation rlResult;
buzbeeb046e162012-10-30 15:48:42 -07001563 if (cUnit->instructionSet == kThumb2) {
1564 /*
1565 * NOTE: This is the one place in the code in which we might have
1566 * as many as six live temporary registers. There are 5 in the normal
1567 * set for Arm. Until we have spill capabilities, temporarily add
1568 * lr to the temp set. It is safe to do this locally, but note that
1569 * lr is used explicitly elsewhere in the code generator and cannot
1570 * normally be used as a general temp register.
1571 */
1572 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1573 oatFreeTemp(cUnit, rLR); // and make it available
1574 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001575 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1576 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1577 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1578 // The longs may overlap - use intermediate temp if so
1579 if (rlResult.lowReg == rlSrc1.highReg) {
1580 int tReg = oatAllocTemp(cUnit);
1581 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1582 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1583 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1584 oatFreeTemp(cUnit, tReg);
1585 } else {
1586 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1587 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1588 rlSrc2.highReg);
1589 }
1590 /*
1591 * NOTE: If rlDest refers to a frame variable in a large frame, the
1592 * following storeValueWide might need to allocate a temp register.
1593 * To further work around the lack of a spill capability, explicitly
1594 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1595 * Remove when spill is functional.
1596 */
1597 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1598 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1599 storeValueWide(cUnit, rlDest, rlResult);
buzbeeb046e162012-10-30 15:48:42 -07001600 if (cUnit->instructionSet == kThumb2) {
1601 oatClobber(cUnit, rLR);
1602 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
1603 }
buzbee31a4a6f2012-02-28 15:36:15 -08001604}
1605
1606
buzbee408ad162012-06-06 16:45:18 -07001607bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001608 RegLocation rlSrc1, RegLocation rlShift)
1609{
Bill Buzbeea114add2012-05-03 15:00:40 -07001610 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001611
buzbee408ad162012-06-06 16:45:18 -07001612 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001613 case Instruction::SHL_LONG:
1614 case Instruction::SHL_LONG_2ADDR:
1615 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1616 break;
1617 case Instruction::SHR_LONG:
1618 case Instruction::SHR_LONG_2ADDR:
1619 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1620 break;
1621 case Instruction::USHR_LONG:
1622 case Instruction::USHR_LONG_2ADDR:
1623 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1624 break;
1625 default:
1626 LOG(FATAL) << "Unexpected case";
1627 return true;
1628 }
1629 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07001630 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001631 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1632 storeValueWide(cUnit, rlDest, rlResult);
1633 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001634}
1635
1636
buzbee408ad162012-06-06 16:45:18 -07001637bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001638 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001639{
Bill Buzbeea114add2012-05-03 15:00:40 -07001640 OpKind op = kOpBkpt;
jeffhao4f8f04a2012-10-02 18:10:35 -07001641 bool isDivRem = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001642 bool checkZero = false;
1643 bool unary = false;
1644 RegLocation rlResult;
1645 bool shiftOp = false;
buzbee408ad162012-06-06 16:45:18 -07001646 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001647 case Instruction::NEG_INT:
1648 op = kOpNeg;
1649 unary = true;
1650 break;
1651 case Instruction::NOT_INT:
1652 op = kOpMvn;
1653 unary = true;
1654 break;
1655 case Instruction::ADD_INT:
1656 case Instruction::ADD_INT_2ADDR:
1657 op = kOpAdd;
1658 break;
1659 case Instruction::SUB_INT:
1660 case Instruction::SUB_INT_2ADDR:
1661 op = kOpSub;
1662 break;
1663 case Instruction::MUL_INT:
1664 case Instruction::MUL_INT_2ADDR:
1665 op = kOpMul;
1666 break;
1667 case Instruction::DIV_INT:
1668 case Instruction::DIV_INT_2ADDR:
1669 checkZero = true;
1670 op = kOpDiv;
jeffhao4f8f04a2012-10-02 18:10:35 -07001671 isDivRem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001672 break;
1673 /* NOTE: returns in rARG1 */
1674 case Instruction::REM_INT:
1675 case Instruction::REM_INT_2ADDR:
1676 checkZero = true;
1677 op = kOpRem;
jeffhao4f8f04a2012-10-02 18:10:35 -07001678 isDivRem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001679 break;
1680 case Instruction::AND_INT:
1681 case Instruction::AND_INT_2ADDR:
1682 op = kOpAnd;
1683 break;
1684 case Instruction::OR_INT:
1685 case Instruction::OR_INT_2ADDR:
1686 op = kOpOr;
1687 break;
1688 case Instruction::XOR_INT:
1689 case Instruction::XOR_INT_2ADDR:
1690 op = kOpXor;
1691 break;
1692 case Instruction::SHL_INT:
1693 case Instruction::SHL_INT_2ADDR:
1694 shiftOp = true;
1695 op = kOpLsl;
1696 break;
1697 case Instruction::SHR_INT:
1698 case Instruction::SHR_INT_2ADDR:
1699 shiftOp = true;
1700 op = kOpAsr;
1701 break;
1702 case Instruction::USHR_INT:
1703 case Instruction::USHR_INT_2ADDR:
1704 shiftOp = true;
1705 op = kOpLsr;
1706 break;
1707 default:
1708 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001709 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001710 }
jeffhao4f8f04a2012-10-02 18:10:35 -07001711 if (!isDivRem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001712 if (unary) {
1713 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1714 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1715 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001716 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001717 if (shiftOp) {
buzbeeb046e162012-10-30 15:48:42 -07001718 int tReg = INVALID_REG;
1719 if (cUnit->instructionSet == kX86) {
1720 // X86 doesn't require masking and must use ECX
1721 tReg = rCOUNT; // rCX
1722 loadValueDirectFixed(cUnit, rlSrc2, tReg);
1723 } else {
1724 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1725 tReg = oatAllocTemp(cUnit);
1726 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1727 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001728 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1729 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1730 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1731 oatFreeTemp(cUnit, tReg);
1732 } else {
1733 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1734 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1735 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1736 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1737 }
buzbee31a4a6f2012-02-28 15:36:15 -08001738 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001739 storeValue(cUnit, rlDest, rlResult);
1740 } else {
buzbeeb046e162012-10-30 15:48:42 -07001741 if (cUnit->instructionSet == kMips) {
1742 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1743 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1744 if (checkZero) {
1745 genImmedCheck(cUnit, kCondEq, rlSrc2.lowReg, 0, kThrowDivZero);
1746 }
1747 rlResult = genDivRem(cUnit, rlDest, rlSrc1.lowReg, rlSrc2.lowReg, op == kOpDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001748 } else {
buzbeeb046e162012-10-30 15:48:42 -07001749 int funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1750 oatFlushAllRegs(cUnit); /* Send everything to home location */
1751 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1752 int rTgt = callHelperSetup(cUnit, funcOffset);
1753 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1754 if (checkZero) {
1755 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
1756 }
1757 // NOTE: callout here is not a safepoint
1758 callHelper(cUnit, rTgt, funcOffset, false /* not a safepoint */ );
1759 if (op == kOpDiv)
1760 rlResult = oatGetReturn(cUnit, false);
1761 else
1762 rlResult = oatGetReturnAlt(cUnit);
jeffhao4f8f04a2012-10-02 18:10:35 -07001763 }
jeffhao4eb68ed2012-10-17 16:41:07 -07001764 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07001765 }
1766 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001767}
1768
1769/*
1770 * The following are the first-level codegen routines that analyze the format
1771 * of each bytecode then either dispatch special purpose codegen routines
1772 * or produce corresponding Thumb instructions directly.
1773 */
1774
1775bool isPowerOfTwo(int x)
1776{
Bill Buzbeea114add2012-05-03 15:00:40 -07001777 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001778}
1779
1780// Returns true if no more than two bits are set in 'x'.
1781bool isPopCountLE2(unsigned int x)
1782{
Bill Buzbeea114add2012-05-03 15:00:40 -07001783 x &= x - 1;
1784 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001785}
1786
1787// Returns the index of the lowest set bit in 'x'.
1788int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001789 int bit_posn = 0;
1790 while ((x & 0xf) == 0) {
1791 bit_posn += 4;
1792 x >>= 4;
1793 }
1794 while ((x & 1) == 0) {
1795 bit_posn++;
1796 x >>= 1;
1797 }
1798 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001799}
1800
1801// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1802// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001803bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001804 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001805{
buzbeeb046e162012-10-30 15:48:42 -07001806 if (lit < 2)
Bill Buzbeea114add2012-05-03 15:00:40 -07001807 return false;
buzbeeb046e162012-10-30 15:48:42 -07001808 // No divide instruction for Arm, so check for more special cases
1809 if ((cUnit->instructionSet == kThumb2) && !isPowerOfTwo(lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001810 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1811 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001812 int k = lowestSetBit(lit);
1813 if (k >= 30) {
1814 // Avoid special cases.
1815 return false;
1816 }
1817 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1818 dalvikOpcode == Instruction::DIV_INT_LIT16);
1819 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1820 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1821 if (div) {
1822 int tReg = oatAllocTemp(cUnit);
1823 if (lit == 2) {
1824 // Division by 2 is by far the most common division by constant.
1825 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1826 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1827 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001828 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001829 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1830 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1831 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1832 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001833 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001834 } else {
1835 int tReg1 = oatAllocTemp(cUnit);
1836 int tReg2 = oatAllocTemp(cUnit);
1837 if (lit == 2) {
1838 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1839 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1840 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1841 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1842 } else {
1843 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1844 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1845 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1846 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1847 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1848 }
1849 }
1850 storeValue(cUnit, rlDest, rlResult);
1851 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001852}
1853
buzbee31a4a6f2012-02-28 15:36:15 -08001854// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1855// and store the result in 'rlDest'.
1856bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1857 RegLocation rlDest, int lit)
1858{
Bill Buzbeea114add2012-05-03 15:00:40 -07001859 // Can we simplify this multiplication?
1860 bool powerOfTwo = false;
1861 bool popCountLE2 = false;
1862 bool powerOfTwoMinusOne = false;
1863 if (lit < 2) {
1864 // Avoid special cases.
1865 return false;
1866 } else if (isPowerOfTwo(lit)) {
1867 powerOfTwo = true;
1868 } else if (isPopCountLE2(lit)) {
1869 popCountLE2 = true;
1870 } else if (isPowerOfTwo(lit + 1)) {
1871 powerOfTwoMinusOne = true;
1872 } else {
1873 return false;
1874 }
1875 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1876 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1877 if (powerOfTwo) {
1878 // Shift.
1879 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1880 lowestSetBit(lit));
1881 } else if (popCountLE2) {
1882 // Shift and add and shift.
1883 int firstBit = lowestSetBit(lit);
1884 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1885 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1886 firstBit, secondBit);
1887 } else {
1888 // Reverse subtract: (src << (shift + 1)) - src.
1889 DCHECK(powerOfTwoMinusOne);
1890 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1891 int tReg = oatAllocTemp(cUnit);
1892 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1893 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1894 }
1895 storeValue(cUnit, rlDest, rlResult);
1896 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001897}
1898
buzbee408ad162012-06-06 16:45:18 -07001899bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
1900 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001901{
Bill Buzbeea114add2012-05-03 15:00:40 -07001902 RegLocation rlResult;
1903 OpKind op = (OpKind)0; /* Make gcc happy */
1904 int shiftOp = false;
1905 bool isDiv = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001906
buzbee408ad162012-06-06 16:45:18 -07001907 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001908 case Instruction::RSUB_INT_LIT8:
1909 case Instruction::RSUB_INT: {
1910 int tReg;
1911 //TUNING: add support for use of Arm rsub op
1912 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1913 tReg = oatAllocTemp(cUnit);
1914 loadConstant(cUnit, tReg, lit);
1915 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1916 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1917 storeValue(cUnit, rlDest, rlResult);
1918 return false;
1919 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001920 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001921
1922 case Instruction::ADD_INT_LIT8:
1923 case Instruction::ADD_INT_LIT16:
1924 op = kOpAdd;
1925 break;
1926 case Instruction::MUL_INT_LIT8:
1927 case Instruction::MUL_INT_LIT16: {
1928 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1929 return false;
1930 }
1931 op = kOpMul;
1932 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001933 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001934 case Instruction::AND_INT_LIT8:
1935 case Instruction::AND_INT_LIT16:
1936 op = kOpAnd;
1937 break;
1938 case Instruction::OR_INT_LIT8:
1939 case Instruction::OR_INT_LIT16:
1940 op = kOpOr;
1941 break;
1942 case Instruction::XOR_INT_LIT8:
1943 case Instruction::XOR_INT_LIT16:
1944 op = kOpXor;
1945 break;
1946 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001947 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07001948 lit &= 31;
1949 shiftOp = true;
1950 op = kOpLsl;
1951 break;
1952 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001953 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07001954 lit &= 31;
1955 shiftOp = true;
1956 op = kOpAsr;
1957 break;
1958 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001959 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07001960 lit &= 31;
1961 shiftOp = true;
1962 op = kOpLsr;
1963 break;
1964
1965 case Instruction::DIV_INT_LIT8:
1966 case Instruction::DIV_INT_LIT16:
1967 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001968 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001969 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07001970 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001971 return false;
1972 }
buzbee408ad162012-06-06 16:45:18 -07001973 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001974 return false;
1975 }
buzbee408ad162012-06-06 16:45:18 -07001976 if ((opcode == Instruction::DIV_INT_LIT8) ||
1977 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001978 isDiv = true;
1979 } else {
1980 isDiv = false;
1981 }
buzbeeb046e162012-10-30 15:48:42 -07001982 if (cUnit->instructionSet == kMips) {
1983 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1984 rlResult = genDivRemLit(cUnit, rlDest, rlSrc.lowReg, lit, isDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001985 } else {
buzbeeb046e162012-10-30 15:48:42 -07001986 oatFlushAllRegs(cUnit); /* Everything to home location */
1987 loadValueDirectFixed(cUnit, rlSrc, rARG0);
1988 oatClobber(cUnit, rARG0);
1989 int funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1990 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit, false);
1991 if (isDiv)
1992 rlResult = oatGetReturn(cUnit, false);
1993 else
1994 rlResult = oatGetReturnAlt(cUnit);
jeffhao4f8f04a2012-10-02 18:10:35 -07001995 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001996 storeValue(cUnit, rlDest, rlResult);
1997 return false;
1998 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07001999 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002000 default:
2001 return true;
2002 }
2003 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2004 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2005 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2006 if (shiftOp && (lit == 0)) {
2007 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2008 } else {
2009 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2010 }
2011 storeValue(cUnit, rlDest, rlResult);
2012 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002013}
2014
buzbee408ad162012-06-06 16:45:18 -07002015bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002016 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002017{
Bill Buzbeea114add2012-05-03 15:00:40 -07002018 RegLocation rlResult;
2019 OpKind firstOp = kOpBkpt;
2020 OpKind secondOp = kOpBkpt;
2021 bool callOut = false;
2022 bool checkZero = false;
2023 int funcOffset;
2024 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002025
buzbee408ad162012-06-06 16:45:18 -07002026 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002027 case Instruction::NOT_LONG:
2028 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2029 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2030 // Check for destructive overlap
2031 if (rlResult.lowReg == rlSrc2.highReg) {
2032 int tReg = oatAllocTemp(cUnit);
2033 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2034 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2035 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2036 oatFreeTemp(cUnit, tReg);
2037 } else {
2038 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2039 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2040 }
2041 storeValueWide(cUnit, rlDest, rlResult);
2042 return false;
2043 break;
2044 case Instruction::ADD_LONG:
2045 case Instruction::ADD_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002046 if (cUnit->instructionSet != kThumb2) {
2047 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
2048 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002049 firstOp = kOpAdd;
2050 secondOp = kOpAdc;
2051 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002052 case Instruction::SUB_LONG:
2053 case Instruction::SUB_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002054 if (cUnit->instructionSet != kThumb2) {
2055 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
2056 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002057 firstOp = kOpSub;
2058 secondOp = kOpSbc;
2059 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002060 case Instruction::MUL_LONG:
2061 case Instruction::MUL_LONG_2ADDR:
2062 callOut = true;
2063 retReg = rRET0;
2064 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2065 break;
2066 case Instruction::DIV_LONG:
2067 case Instruction::DIV_LONG_2ADDR:
2068 callOut = true;
2069 checkZero = true;
2070 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002071 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002072 break;
2073 case Instruction::REM_LONG:
2074 case Instruction::REM_LONG_2ADDR:
2075 callOut = true;
2076 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002077 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeeb046e162012-10-30 15:48:42 -07002078 /* NOTE - for Arm, result is in rARG2/rARG3 instead of rRET0/rRET1 */
2079 retReg = (cUnit->instructionSet == kThumb2) ? rARG2 : rRET0;
Bill Buzbeea114add2012-05-03 15:00:40 -07002080 break;
2081 case Instruction::AND_LONG_2ADDR:
2082 case Instruction::AND_LONG:
buzbeeb046e162012-10-30 15:48:42 -07002083 if (cUnit->instructionSet == kX86) {
2084 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
2085 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002086 firstOp = kOpAnd;
2087 secondOp = kOpAnd;
2088 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002089 case Instruction::OR_LONG:
2090 case Instruction::OR_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002091 if (cUnit->instructionSet == kX86) {
2092 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
2093 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002094 firstOp = kOpOr;
2095 secondOp = kOpOr;
2096 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002097 case Instruction::XOR_LONG:
2098 case Instruction::XOR_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002099 if (cUnit->instructionSet == kX86) {
2100 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
2101 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002102 firstOp = kOpXor;
2103 secondOp = kOpXor;
2104 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002105 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002106 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002107 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002108 default:
2109 LOG(FATAL) << "Invalid long arith op";
2110 }
2111 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002112 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002113 } else {
2114 oatFlushAllRegs(cUnit); /* Send everything to home location */
2115 if (checkZero) {
2116 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
buzbeeb046e162012-10-30 15:48:42 -07002117 int rTgt = callHelperSetup(cUnit, funcOffset);
2118 genDivZeroCheck(cUnit, rARG2, rARG3);
Bill Buzbeea114add2012-05-03 15:00:40 -07002119 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
buzbee8320f382012-09-11 16:29:42 -07002120 // NOTE: callout here is not a safepoint
buzbeeb046e162012-10-30 15:48:42 -07002121 callHelper(cUnit, rTgt, funcOffset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08002122 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002123 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
buzbee8320f382012-09-11 16:29:42 -07002124 rlSrc1, rlSrc2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002125 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002126 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2127 if (retReg == rRET0)
2128 rlResult = oatGetReturnWide(cUnit, false);
2129 else
2130 rlResult = oatGetReturnWideAlt(cUnit);
2131 storeValueWide(cUnit, rlDest, rlResult);
2132 }
2133 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002134}
2135
buzbee408ad162012-06-06 16:45:18 -07002136bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2137 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002138{
Bill Buzbeea114add2012-05-03 15:00:40 -07002139 /*
2140 * Don't optimize the register usage since it calls out to support
2141 * functions
2142 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002143 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002144 if (rlSrc.wide) {
jeffhao30a33172012-10-22 18:16:22 -07002145 loadValueDirectWideFixed(cUnit, rlSrc, rlSrc.fp ? rFARG0 : rARG0, rlSrc.fp ? rFARG1 : rARG1);
buzbee408ad162012-06-06 16:45:18 -07002146 } else {
jeffhao30a33172012-10-22 18:16:22 -07002147 loadValueDirectFixed(cUnit, rlSrc, rlSrc.fp ? rFARG0 : rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002148 }
buzbee8320f382012-09-11 16:29:42 -07002149 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc, false);
buzbee408ad162012-06-06 16:45:18 -07002150 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002151 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002152 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2153 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002154 } else {
2155 RegLocation rlResult;
2156 rlResult = oatGetReturn(cUnit, rlDest.fp);
2157 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002158 }
2159 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002160}
2161
2162void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002163bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002164 RegLocation rlDest, RegLocation rlSrc1,
2165 RegLocation rlSrc2)
2166{
Bill Buzbeea114add2012-05-03 15:00:40 -07002167 RegLocation rlResult;
2168 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002169
buzbee408ad162012-06-06 16:45:18 -07002170 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002171 case Instruction::ADD_FLOAT_2ADDR:
2172 case Instruction::ADD_FLOAT:
2173 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2174 break;
2175 case Instruction::SUB_FLOAT_2ADDR:
2176 case Instruction::SUB_FLOAT:
2177 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2178 break;
2179 case Instruction::DIV_FLOAT_2ADDR:
2180 case Instruction::DIV_FLOAT:
2181 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2182 break;
2183 case Instruction::MUL_FLOAT_2ADDR:
2184 case Instruction::MUL_FLOAT:
2185 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2186 break;
2187 case Instruction::REM_FLOAT_2ADDR:
2188 case Instruction::REM_FLOAT:
2189 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2190 break;
2191 case Instruction::NEG_FLOAT: {
2192 genNegFloat(cUnit, rlDest, rlSrc1);
2193 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002194 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002195 default:
2196 return true;
2197 }
2198 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002199 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002200 rlResult = oatGetReturn(cUnit, true);
2201 storeValue(cUnit, rlDest, rlResult);
2202 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002203}
2204
2205void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002206bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002207 RegLocation rlDest, RegLocation rlSrc1,
2208 RegLocation rlSrc2)
2209{
Bill Buzbeea114add2012-05-03 15:00:40 -07002210 RegLocation rlResult;
2211 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002212
buzbee408ad162012-06-06 16:45:18 -07002213 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002214 case Instruction::ADD_DOUBLE_2ADDR:
2215 case Instruction::ADD_DOUBLE:
2216 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2217 break;
2218 case Instruction::SUB_DOUBLE_2ADDR:
2219 case Instruction::SUB_DOUBLE:
2220 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2221 break;
2222 case Instruction::DIV_DOUBLE_2ADDR:
2223 case Instruction::DIV_DOUBLE:
2224 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2225 break;
2226 case Instruction::MUL_DOUBLE_2ADDR:
2227 case Instruction::MUL_DOUBLE:
2228 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2229 break;
2230 case Instruction::REM_DOUBLE_2ADDR:
2231 case Instruction::REM_DOUBLE:
2232 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2233 break;
2234 case Instruction::NEG_DOUBLE: {
2235 genNegDouble(cUnit, rlDest, rlSrc1);
2236 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002237 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002238 default:
2239 return true;
2240 }
2241 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002242 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002243 rlResult = oatGetReturnWide(cUnit, true);
2244 storeValueWide(cUnit, rlDest, rlResult);
2245 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002246}
2247
buzbee408ad162012-06-06 16:45:18 -07002248bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2249 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002250{
buzbee31a4a6f2012-02-28 15:36:15 -08002251
Bill Buzbeea114add2012-05-03 15:00:40 -07002252 switch (opcode) {
2253 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002254 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2255 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002256 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002257 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2258 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002259 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002260 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2261 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002262 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002263 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2264 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002265 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002266 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2267 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002268 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002269 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2270 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002271 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002272 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2273 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002274 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002275 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2276 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002277 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002278 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2279 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002280 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002281 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2282 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002283 default:
2284 return true;
2285 }
2286 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002287}
2288
buzbee31a4a6f2012-02-28 15:36:15 -08002289/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002290void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002291{
buzbee408ad162012-06-06 16:45:18 -07002292 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002293 return;
2294 }
2295 oatFlushAllRegs(cUnit);
buzbeeb046e162012-10-30 15:48:42 -07002296 LIR* branch = opTestSuspend(cUnit, NULL);
2297 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2298 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset, kPseudoSuspendTarget,
2299 (intptr_t)retLab, cUnit->currentDalvikOffset);
2300 branch->target = (LIR*)target;
2301 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
buzbee31a4a6f2012-02-28 15:36:15 -08002302}
2303
buzbeefead2932012-03-30 14:02:01 -07002304/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002305void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002306{
buzbee408ad162012-06-06 16:45:18 -07002307 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002308 opUnconditionalBranch(cUnit, target);
2309 return;
2310 }
buzbeeb046e162012-10-30 15:48:42 -07002311 opTestSuspend(cUnit, target);
2312 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset, kPseudoSuspendTarget, (intptr_t)target,
2313 cUnit->currentDalvikOffset);
2314 oatFlushAllRegs(cUnit);
2315 opUnconditionalBranch(cUnit, launchPad);
2316 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)launchPad);
buzbeefead2932012-03-30 14:02:01 -07002317}
2318
buzbee31a4a6f2012-02-28 15:36:15 -08002319} // namespace art