blob: fe10fe46afd5e0f197eb478866e01f20ac6e62a1 [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
buzbeeeaf09bc2012-11-15 14:51:41 -080021//TODO: remove decl.
22void genInvoke(CompilationUnit* cUnit, CallInfo* info);
23
buzbee31a4a6f2012-02-28 15:36:15 -080024/*
25 * This source files contains "gen" codegen routines that should
26 * be applicable to most targets. Only mid-level support utilities
27 * and "op" calls may be used here.
28 */
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);
buzbeef0504cd2012-11-13 16:31:10 -080066 loadConstant(cUnit, targetReg(kArg0), arg0);
buzbeeb046e162012-10-30 15:48:42 -070067 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);
buzbeef0504cd2012-11-13 16:31:10 -080073 opRegCopy(cUnit, targetReg(kArg0), arg0);
Bill Buzbeea114add2012-05-03 15:00:40 -070074 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) {
buzbeef0504cd2012-11-13 16:31:10 -080082 loadValueDirectFixed(cUnit, arg0, targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -070083 } else {
buzbeef0504cd2012-11-13 16:31:10 -080084 loadValueDirectWideFixed(cUnit, arg0, targetReg(kArg0), targetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -070085 }
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);
buzbeef0504cd2012-11-13 16:31:10 -080093 loadConstant(cUnit, targetReg(kArg0), arg0);
94 loadConstant(cUnit, targetReg(kArg1), arg1);
Bill Buzbeea114add2012-05-03 15:00:40 -070095 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) {
buzbeef0504cd2012-11-13 16:31:10 -0800103 loadValueDirectFixed(cUnit, arg1, targetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800105 loadValueDirectWideFixed(cUnit, arg1, targetReg(kArg1), targetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 }
buzbeef0504cd2012-11-13 16:31:10 -0800107 loadConstant(cUnit, targetReg(kArg0), arg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 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);
buzbeef0504cd2012-11-13 16:31:10 -0800115 loadValueDirectFixed(cUnit, arg0, targetReg(kArg0));
116 loadConstant(cUnit, targetReg(kArg1), arg1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700117 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);
buzbeef0504cd2012-11-13 16:31:10 -0800124 opRegCopy(cUnit, targetReg(kArg1), arg1);
125 loadConstant(cUnit, targetReg(kArg0), arg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 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);
buzbeef0504cd2012-11-13 16:31:10 -0800133 opRegCopy(cUnit, targetReg(kArg0), arg0);
134 loadConstant(cUnit, targetReg(kArg1), arg1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700135 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);
buzbeef0504cd2012-11-13 16:31:10 -0800141 loadCurrMethodDirect(cUnit, targetReg(kArg1));
142 loadConstant(cUnit, targetReg(kArg0), arg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 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) {
buzbeef0504cd2012-11-13 16:31:10 -0800151 loadValueDirectFixed(cUnit, arg0, arg0.fp ? targetReg(kFArg0) : targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 if (arg1.wide == 0) {
buzbeeb046e162012-10-30 15:48:42 -0700153 if (cUnit->instructionSet == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800154 loadValueDirectFixed(cUnit, arg1, arg1.fp ? targetReg(kFArg2) : targetReg(kArg1));
buzbeeb046e162012-10-30 15:48:42 -0700155 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800156 loadValueDirectFixed(cUnit, arg1, targetReg(kArg1));
buzbeeb046e162012-10-30 15:48:42 -0700157 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700158 } else {
buzbeeb046e162012-10-30 15:48:42 -0700159 if (cUnit->instructionSet == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800160 loadValueDirectWideFixed(cUnit, arg1, arg1.fp ? targetReg(kFArg2) : targetReg(kArg1), arg1.fp ? targetReg(kFArg3) : targetReg(kArg2));
buzbeeb046e162012-10-30 15:48:42 -0700161 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800162 loadValueDirectWideFixed(cUnit, arg1, targetReg(kArg1), targetReg(kArg2));
buzbeeb046e162012-10-30 15:48:42 -0700163 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700164 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800166 loadValueDirectWideFixed(cUnit, arg0, arg0.fp ? targetReg(kFArg0) : targetReg(kArg0), arg0.fp ? targetReg(kFArg1) : targetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 if (arg1.wide == 0) {
buzbeef0504cd2012-11-13 16:31:10 -0800168 loadValueDirectFixed(cUnit, arg1, arg1.fp ? targetReg(kFArg2) : targetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800170 loadValueDirectWideFixed(cUnit, arg1, arg1.fp ? targetReg(kFArg2) : targetReg(kArg2), arg1.fp ? targetReg(kFArg3) : targetReg(kArg3));
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);
buzbeef0504cd2012-11-13 16:31:10 -0800180 DCHECK_NE((int)targetReg(kArg0), arg1); // check copy into arg0 won't clobber arg1
181 opRegCopy(cUnit, targetReg(kArg0), arg0);
182 opRegCopy(cUnit, targetReg(kArg1), arg1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 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);
buzbeef0504cd2012-11-13 16:31:10 -0800190 DCHECK_NE((int)targetReg(kArg0), arg1); // check copy into arg0 won't clobber arg1
191 opRegCopy(cUnit, targetReg(kArg0), arg0);
192 opRegCopy(cUnit, targetReg(kArg1), arg1);
193 loadConstant(cUnit, targetReg(kArg2), arg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 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);
buzbeef0504cd2012-11-13 16:31:10 -0800201 loadValueDirectFixed(cUnit, arg2, targetReg(kArg2));
202 loadCurrMethodDirect(cUnit, targetReg(kArg1));
203 loadConstant(cUnit, targetReg(kArg0), arg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 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);
buzbeef0504cd2012-11-13 16:31:10 -0800211 loadCurrMethodDirect(cUnit, targetReg(kArg1));
212 loadConstant(cUnit, targetReg(kArg2), arg2);
213 loadConstant(cUnit, targetReg(kArg0), arg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 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);
buzbeef0504cd2012-11-13 16:31:10 -0800222 loadValueDirectFixed(cUnit, arg1, targetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700223 if (arg2.wide == 0) {
buzbeef0504cd2012-11-13 16:31:10 -0800224 loadValueDirectFixed(cUnit, arg2, targetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800226 loadValueDirectWideFixed(cUnit, arg2, targetReg(kArg2), targetReg(kArg3));
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 }
buzbeef0504cd2012-11-13 16:31:10 -0800228 loadConstant(cUnit, targetReg(kArg0), arg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 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);
buzbeef0504cd2012-11-13 16:31:10 -0800454 oatFreeTemp(cUnit, targetReg(kArg2));
455 oatFreeTemp(cUnit, targetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 /*
457 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
458 * return region. Because AllocFromCode placed the new array
buzbeef0504cd2012-11-13 16:31:10 -0800459 * in kRet0, we'll just lock it into place. When debugger support is
Bill Buzbeea114add2012-05-03 15:00:40 -0700460 * added, it may be necessary to additionally copy all return
461 * values to a home location in thread-local storage
462 */
buzbeef0504cd2012-11-13 16:31:10 -0800463 oatLockTemp(cUnit, targetReg(kRet0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700464
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) {
buzbeef0504cd2012-11-13 16:31:10 -0800482 storeBaseDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, loc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 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:
buzbeef0504cd2012-11-13 16:31:10 -0800497 rVal = targetReg(kLr);
buzbeeb046e162012-10-30 15:48:42 -0700498 break;
499 case kX86:
buzbeef0504cd2012-11-13 16:31:10 -0800500 oatFreeTemp(cUnit, targetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700501 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];
buzbeef0504cd2012-11-13 16:31:10 -0800510 opRegRegImm(cUnit, kOpAdd, rSrc, targetReg(kSp),
Bill Buzbeea114add2012-05-03 15:00:40 -0700511 oatSRegOffset(cUnit, rlFirst.sRegLow));
512 // Set up the target pointer
buzbeef0504cd2012-11-13 16:31:10 -0800513 opRegRegImm(cUnit, kOpAdd, rDst, targetReg(kRet0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 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
buzbeef0504cd2012-11-13 16:31:10 -0800526 opRegRegImm(cUnit, kOpAdd, targetReg(kRet0), rDst, -Array::DataOffset(component_size).Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700527 }
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);
buzbeef0504cd2012-11-13 16:31:10 -0800532 storeBaseDisp(cUnit, targetReg(kRet0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700533 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.
buzbeef0504cd2012-11-13 16:31:10 -0800582 int rMethod = targetReg(kArg1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700583 oatLockTemp(cUnit, rMethod);
584 loadCurrMethodDirect(cUnit, rMethod);
buzbeef0504cd2012-11-13 16:31:10 -0800585 rBase = targetReg(kArg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700586 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);
buzbeef0504cd2012-11-13 16:31:10 -0800597 loadConstant(cUnit, targetReg(kArg0), 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) {
buzbeef0504cd2012-11-13 16:31:10 -0800600 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
601 opRegCopy(cUnit, rBase, targetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700602 }
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
buzbeef0504cd2012-11-13 16:31:10 -0800674 int rMethod = targetReg(kArg1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700675 oatLockTemp(cUnit, rMethod);
676 loadCurrMethodDirect(cUnit, rMethod);
buzbeef0504cd2012-11-13 16:31:10 -0800677 rBase = targetReg(kArg0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 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) {
buzbeef0504cd2012-11-13 16:31:10 -0800691 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
692 opRegCopy(cUnit, rBase, targetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700693 }
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";
buzbeef0504cd2012-11-13 16:31:10 -0800736 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, targetReg(kInvokeTgt), 0, NULL);
737 loadWordDisp(cUnit, targetReg(kSelf), ENTRYPOINT_OFFSET(pDebugMe), targetReg(kInvokeTgt));
Bill Buzbeea114add2012-05-03 15:00:40 -0700738 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:
buzbeef0504cd2012-11-13 16:31:10 -0800799 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
800 if (v2 != targetReg(kArg0)) {
801 opRegCopy(cUnit, targetReg(kArg0), 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
buzbeef0504cd2012-11-13 16:31:10 -0800804 opRegMem(cUnit, kOpMov, targetReg(kArg1), v2, Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700805 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800806 opRegCopy(cUnit, targetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700807 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700808 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800809 if (v1 == targetReg(kArg1)) {
810 // Swap v1 and v2, using kArg2 as a temp
811 opRegCopy(cUnit, targetReg(kArg2), 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
buzbeef0504cd2012-11-13 16:31:10 -0800814 opRegMem(cUnit, kOpMov, targetReg(kArg1), v2, Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700815 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800816 opRegCopy(cUnit, targetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700817 }
buzbeef0504cd2012-11-13 16:31:10 -0800818 opRegCopy(cUnit, targetReg(kArg0), targetReg(kArg2));
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
buzbeef0504cd2012-11-13 16:31:10 -0800822 opRegMem(cUnit, kOpMov, targetReg(kArg1), v2, Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700823 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800824 opRegCopy(cUnit, targetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700825 }
buzbeef0504cd2012-11-13 16:31:10 -0800826 opRegCopy(cUnit, targetReg(kArg0), v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 }
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:
buzbeef0504cd2012-11-13 16:31:10 -0800835 opRegCopy(cUnit, targetReg(kArg0), v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700836 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) {
buzbeef0504cd2012-11-13 16:31:10 -0800843 opRegImm(cUnit, kOpAdd, targetReg(kSp), cUnit->frameSize);
buzbeeb046e162012-10-30 15:48:42 -0700844 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800845 opRegImm(cUnit, kOpAdd, targetReg(kSp), (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeb046e162012-10-30 15:48:42 -0700846 }
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.
buzbeef0504cd2012-11-13 16:31:10 -0800993 // Resolved type returned in kRet0.
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);
buzbeef0504cd2012-11-13 16:31:10 -08001022 // Call out to helper, which will return resolved type in kArg0
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
buzbeef0504cd2012-11-13 16:31:10 -08001054 loadCurrMethodDirect(cUnit, targetReg(kArg2));
1055 loadWordDisp(cUnit, targetReg(kArg2),
1056 AbstractMethod::DexCacheStringsOffset().Int32Value(), targetReg(kArg0));
1057 // Might call out to helper, which will return resolved string in kRet0
buzbeeb046e162012-10-30 15:48:42 -07001058 int rTgt = callHelperSetup(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
buzbeef0504cd2012-11-13 16:31:10 -08001059 loadWordDisp(cUnit, targetReg(kArg0), offset_of_string, targetReg(kRet0));
1060 loadConstant(cUnit, targetReg(kArg1), string_idx);
buzbeeb046e162012-10-30 15:48:42 -07001061 if (cUnit->instructionSet == kThumb2) {
buzbeef0504cd2012-11-13 16:31:10 -08001062 opRegImm(cUnit, kOpCmp, targetReg(kRet0), 0); // Is resolved?
buzbeeb046e162012-10-30 15:48:42 -07001063 genBarrier(cUnit);
1064 // For testing, always force through helper
1065 if (!EXERCISE_SLOWEST_STRING_PATH) {
1066 opIT(cUnit, kArmCondEq, "T");
1067 }
buzbeef0504cd2012-11-13 16:31:10 -08001068 opRegCopy(cUnit, targetReg(kArg0), targetReg(kArg2)); // .eq
buzbeeb046e162012-10-30 15:48:42 -07001069 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) {
buzbeef0504cd2012-11-13 16:31:10 -08001073 LIR* branch = opCmpImmBranch(cUnit, kCondNe, targetReg(kRet0), 0, NULL);
1074 opRegCopy(cUnit, targetReg(kArg0), targetReg(kArg2)); // .eq
buzbeeb046e162012-10-30 15:48:42 -07001075 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);
buzbeef0504cd2012-11-13 16:31:10 -08001082 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode), targetReg(kArg2), targetReg(kArg1), 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.
buzbeef0504cd2012-11-13 16:31:10 -08001124 callRuntimeHelperReg(cUnit, funcOffset, targetReg(kArg0), false);
buzbeeb046e162012-10-30 15:48:42 -07001125 } else {
buzbeef0504cd2012-11-13 16:31:10 -08001126 callRuntimeHelperReg(cUnit, funcOffset, targetReg(kSelf), false);
buzbeeb046e162012-10-30 15:48:42 -07001127 }
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);
buzbeef0504cd2012-11-13 16:31:10 -08001144 loadCurrMethodDirect(cUnit, targetReg(kArg1)); // kArg1 <= current Method*
1145 int classReg = targetReg(kArg2); // kArg2 will hold the Class*
Bill Buzbeea114add2012-05-03 15:00:40 -07001146 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,
buzbeef0504cd2012-11-13 16:31:10 -08001150 // returns Class* in kArg0
buzbee8320f382012-09-11 16:29:42 -07001151 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1152 type_idx, true);
buzbeef0504cd2012-11-13 16:31:10 -08001153 opRegCopy(cUnit, classReg, targetReg(kRet0)); // Align usage with fast path
1154 loadValueDirectFixed(cUnit, rlSrc, targetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001155 } else {
buzbeef0504cd2012-11-13 16:31:10 -08001156 // Load dex cache entry into classReg (kArg2)
1157 loadValueDirectFixed(cUnit, rlSrc, targetReg(kArg0)); // kArg0 <= ref
1158 loadWordDisp(cUnit, targetReg(kArg1),
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
buzbeef0504cd2012-11-13 16:31:10 -08001169 // Call out to helper, which will return resolved type in kRet0
buzbee8320f382012-09-11 16:29:42 -07001170 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
buzbeef0504cd2012-11-13 16:31:10 -08001171 opRegCopy(cUnit, targetReg(kArg2), targetReg(kRet0)); // Align usage with fast path
1172 loadValueDirectFixed(cUnit, rlSrc, targetReg(kArg0)); /* reload Ref */
Bill Buzbeea114add2012-05-03 15:00:40 -07001173 // 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 }
buzbeef0504cd2012-11-13 16:31:10 -08001178 /* kArg0 is ref, kArg2 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 }
buzbeef0504cd2012-11-13 16:31:10 -08001183 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, targetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001184 /* load object->klass_ */
1185 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbeef0504cd2012-11-13 16:31:10 -08001186 loadWordDisp(cUnit, targetReg(kArg0), Object::ClassOffset().Int32Value(), targetReg(kArg1));
1187 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 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));
buzbeef0504cd2012-11-13 16:31:10 -08001193 opRegReg(cUnit, kOpCmp, targetReg(kArg1), targetReg(kArg2)); // Same?
buzbeeb046e162012-10-30 15:48:42 -07001194 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
buzbeef0504cd2012-11-13 16:31:10 -08001195 loadConstant(cUnit, targetReg(kArg0), 1); // .eq case - load true
1196 opRegCopy(cUnit, targetReg(kArg0), targetReg(kArg2)); // .ne case - arg0 <= class
buzbeeb046e162012-10-30 15:48:42 -07001197 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
buzbeef0504cd2012-11-13 16:31:10 -08001202 branchover = opCmpBranch(cUnit, kCondEq, targetReg(kArg1), targetReg(kArg2), NULL);
buzbeeb046e162012-10-30 15:48:42 -07001203 if (cUnit->instructionSet != kX86) {
1204 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
buzbeef0504cd2012-11-13 16:31:10 -08001205 opRegCopy(cUnit, targetReg(kArg0), targetReg(kArg2)); // .ne case - arg0 <= class
buzbeeb046e162012-10-30 15:48:42 -07001206 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1207 oatFreeTemp(cUnit, rTgt);
1208 } else {
buzbeef0504cd2012-11-13 16:31:10 -08001209 opRegCopy(cUnit, targetReg(kArg0), targetReg(kArg2));
buzbeeb046e162012-10-30 15:48:42 -07001210 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);
buzbeef0504cd2012-11-13 16:31:10 -08001229 loadCurrMethodDirect(cUnit, targetReg(kArg1)); // kArg1 <= current Method*
1230 int classReg = targetReg(kArg2); // kArg2 will hold the Class*
Bill Buzbeea114add2012-05-03 15:00:40 -07001231 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,
buzbeef0504cd2012-11-13 16:31:10 -08001235 // returns Class* in kRet0
Bill Buzbeea114add2012-05-03 15:00:40 -07001236 // InitializeTypeAndVerifyAccess(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001237 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbeef0504cd2012-11-13 16:31:10 -08001238 type_idx, targetReg(kArg1), true);
1239 opRegCopy(cUnit, classReg, targetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001240 } else {
buzbeef0504cd2012-11-13 16:31:10 -08001241 // Load dex cache entry into classReg (kArg2)
1242 loadWordDisp(cUnit, targetReg(kArg1),
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
buzbeef0504cd2012-11-13 16:31:10 -08001253 // Call out to helper, which will return resolved type in kArg0
Bill Buzbeea114add2012-05-03 15:00:40 -07001254 // InitializeTypeFromCode(idx, method)
buzbeef0504cd2012-11-13 16:31:10 -08001255 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, targetReg(kArg1),
buzbee8320f382012-09-11 16:29:42 -07001256 true);
buzbeef0504cd2012-11-13 16:31:10 -08001257 opRegCopy(cUnit, classReg, targetReg(kRet0)); // 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 }
buzbeef0504cd2012-11-13 16:31:10 -08001263 // At this point, classReg (kArg2) has class
1264 loadValueDirectFixed(cUnit, rlSrc, targetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001265 /* Null is OK - continue */
buzbeef0504cd2012-11-13 16:31:10 -08001266 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, targetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001267 /* load object->klass_ */
1268 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbeef0504cd2012-11-13 16:31:10 -08001269 loadWordDisp(cUnit, targetReg(kArg0), Object::ClassOffset().Int32Value(), targetReg(kArg1));
1270 /* kArg1 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));
buzbeef0504cd2012-11-13 16:31:10 -08001274 opRegReg(cUnit, kOpCmp, targetReg(kArg1), classReg);
buzbeeb046e162012-10-30 15:48:42 -07001275 branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
buzbeef0504cd2012-11-13 16:31:10 -08001276 opRegCopy(cUnit, targetReg(kArg0), targetReg(kArg1));
1277 opRegCopy(cUnit, targetReg(kArg1), targetReg(kArg2));
buzbeeb046e162012-10-30 15:48:42 -07001278 oatClobberCalleeSave(cUnit);
1279 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
1280 markSafepointPC(cUnit, callInst);
1281 oatFreeTemp(cUnit, rTgt);
1282 } else {
buzbeef0504cd2012-11-13 16:31:10 -08001283 branch2 = opCmpBranch(cUnit, kCondEq, targetReg(kArg1), classReg, NULL);
1284 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode), targetReg(kArg1), targetReg(kArg2), true);
buzbeeb046e162012-10-30 15:48:42 -07001285 }
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
buzbeef0504cd2012-11-13 16:31:10 -08001305 int rValue = targetReg(kArg0); // Register holding value
1306 int rArrayClass = targetReg(kArg1); // Register holding array's Class
1307 int rArray = targetReg(kArg2); // Register holding array
1308 int rIndex = targetReg(kArg3); // 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
buzbeef0504cd2012-11-13 16:31:10 -08001335 oatFreeTemp(cUnit, targetReg(kArg1));
buzbeeb046e162012-10-30 15:48:42 -07001336 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) {
buzbeef0504cd2012-11-13 16:31:10 -08001346 regLen = targetReg(kArg1);
buzbeeb046e162012-10-30 15:48:42 -07001347 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 */
buzbeef0504cd2012-11-13 16:31:10 -08001572 oatMarkTemp(cUnit, targetReg(kLr)); // Add lr to the temp pool
1573 oatFreeTemp(cUnit, targetReg(kLr)); // and make it available
buzbeeb046e162012-10-30 15:48:42 -07001574 }
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
buzbeea4a970a2012-11-08 07:54:03 -08001579 if ((rlResult.lowReg == rlSrc1.highReg) || (rlResult.lowReg == rlSrc2.highReg)){
Bill Buzbeea114add2012-05-03 15:00:40 -07001580 int tReg = oatAllocTemp(cUnit);
buzbeea4a970a2012-11-08 07:54:03 -08001581 opRegRegReg(cUnit, firstOp, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
1582 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg, rlSrc2.highReg);
1583 opRegCopy(cUnit, rlResult.lowReg, tReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001584 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) {
buzbeef0504cd2012-11-13 16:31:10 -08001601 oatClobber(cUnit, targetReg(kLr));
1602 oatUnmarkTemp(cUnit, targetReg(kLr)); // Remove lr from the temp pool
buzbeeb046e162012-10-30 15:48:42 -07001603 }
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;
buzbeef0504cd2012-11-13 16:31:10 -08001673 /* NOTE: returns in kArg1 */
Bill Buzbeea114add2012-05-03 15:00:40 -07001674 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
buzbeef0504cd2012-11-13 16:31:10 -08001721 tReg = targetReg(kCount); // rCX
buzbeeb046e162012-10-30 15:48:42 -07001722 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 */
buzbeef0504cd2012-11-13 16:31:10 -08001751 loadValueDirectFixed(cUnit, rlSrc2, targetReg(kArg1));
buzbeeb046e162012-10-30 15:48:42 -07001752 int rTgt = callHelperSetup(cUnit, funcOffset);
buzbeef0504cd2012-11-13 16:31:10 -08001753 loadValueDirectFixed(cUnit, rlSrc1, targetReg(kArg0));
buzbeeb046e162012-10-30 15:48:42 -07001754 if (checkZero) {
buzbeef0504cd2012-11-13 16:31:10 -08001755 genImmedCheck(cUnit, kCondEq, targetReg(kArg1), 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001756 }
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{
buzbee0f79d722012-11-01 15:35:27 -07001806 if ((lit < 2) || ((cUnit->instructionSet != kThumb2) && !isPowerOfTwo(lit))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001807 return false;
buzbee0f79d722012-11-01 15:35:27 -07001808 }
buzbeeb046e162012-10-30 15:48:42 -07001809 // No divide instruction for Arm, so check for more special cases
1810 if ((cUnit->instructionSet == kThumb2) && !isPowerOfTwo(lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001811 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1812 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001813 int k = lowestSetBit(lit);
1814 if (k >= 30) {
1815 // Avoid special cases.
1816 return false;
1817 }
1818 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1819 dalvikOpcode == Instruction::DIV_INT_LIT16);
1820 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1821 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1822 if (div) {
1823 int tReg = oatAllocTemp(cUnit);
1824 if (lit == 2) {
1825 // Division by 2 is by far the most common division by constant.
1826 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1827 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1828 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001829 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001830 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1831 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1832 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1833 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001834 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001835 } else {
1836 int tReg1 = oatAllocTemp(cUnit);
1837 int tReg2 = oatAllocTemp(cUnit);
1838 if (lit == 2) {
1839 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1840 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1841 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1842 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1843 } else {
1844 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1845 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1846 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1847 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1848 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1849 }
1850 }
1851 storeValue(cUnit, rlDest, rlResult);
1852 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001853}
1854
buzbee31a4a6f2012-02-28 15:36:15 -08001855// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1856// and store the result in 'rlDest'.
1857bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1858 RegLocation rlDest, int lit)
1859{
Bill Buzbeea114add2012-05-03 15:00:40 -07001860 // Can we simplify this multiplication?
1861 bool powerOfTwo = false;
1862 bool popCountLE2 = false;
1863 bool powerOfTwoMinusOne = false;
1864 if (lit < 2) {
1865 // Avoid special cases.
1866 return false;
1867 } else if (isPowerOfTwo(lit)) {
1868 powerOfTwo = true;
1869 } else if (isPopCountLE2(lit)) {
1870 popCountLE2 = true;
1871 } else if (isPowerOfTwo(lit + 1)) {
1872 powerOfTwoMinusOne = true;
1873 } else {
1874 return false;
1875 }
1876 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1877 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1878 if (powerOfTwo) {
1879 // Shift.
1880 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1881 lowestSetBit(lit));
1882 } else if (popCountLE2) {
1883 // Shift and add and shift.
1884 int firstBit = lowestSetBit(lit);
1885 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1886 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1887 firstBit, secondBit);
1888 } else {
1889 // Reverse subtract: (src << (shift + 1)) - src.
1890 DCHECK(powerOfTwoMinusOne);
1891 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1892 int tReg = oatAllocTemp(cUnit);
1893 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1894 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1895 }
1896 storeValue(cUnit, rlDest, rlResult);
1897 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001898}
1899
buzbee408ad162012-06-06 16:45:18 -07001900bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
1901 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001902{
Bill Buzbeea114add2012-05-03 15:00:40 -07001903 RegLocation rlResult;
1904 OpKind op = (OpKind)0; /* Make gcc happy */
1905 int shiftOp = false;
1906 bool isDiv = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001907
buzbee408ad162012-06-06 16:45:18 -07001908 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001909 case Instruction::RSUB_INT_LIT8:
1910 case Instruction::RSUB_INT: {
1911 int tReg;
1912 //TUNING: add support for use of Arm rsub op
1913 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1914 tReg = oatAllocTemp(cUnit);
1915 loadConstant(cUnit, tReg, lit);
1916 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1917 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1918 storeValue(cUnit, rlDest, rlResult);
1919 return false;
1920 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001921 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001922
1923 case Instruction::ADD_INT_LIT8:
1924 case Instruction::ADD_INT_LIT16:
1925 op = kOpAdd;
1926 break;
1927 case Instruction::MUL_INT_LIT8:
1928 case Instruction::MUL_INT_LIT16: {
1929 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1930 return false;
1931 }
1932 op = kOpMul;
1933 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001934 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001935 case Instruction::AND_INT_LIT8:
1936 case Instruction::AND_INT_LIT16:
1937 op = kOpAnd;
1938 break;
1939 case Instruction::OR_INT_LIT8:
1940 case Instruction::OR_INT_LIT16:
1941 op = kOpOr;
1942 break;
1943 case Instruction::XOR_INT_LIT8:
1944 case Instruction::XOR_INT_LIT16:
1945 op = kOpXor;
1946 break;
1947 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001948 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07001949 lit &= 31;
1950 shiftOp = true;
1951 op = kOpLsl;
1952 break;
1953 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001954 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07001955 lit &= 31;
1956 shiftOp = true;
1957 op = kOpAsr;
1958 break;
1959 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001960 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07001961 lit &= 31;
1962 shiftOp = true;
1963 op = kOpLsr;
1964 break;
1965
1966 case Instruction::DIV_INT_LIT8:
1967 case Instruction::DIV_INT_LIT16:
1968 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001969 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001970 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07001971 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001972 return false;
1973 }
buzbee408ad162012-06-06 16:45:18 -07001974 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001975 return false;
1976 }
buzbee408ad162012-06-06 16:45:18 -07001977 if ((opcode == Instruction::DIV_INT_LIT8) ||
1978 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001979 isDiv = true;
1980 } else {
1981 isDiv = false;
1982 }
buzbeeb046e162012-10-30 15:48:42 -07001983 if (cUnit->instructionSet == kMips) {
1984 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1985 rlResult = genDivRemLit(cUnit, rlDest, rlSrc.lowReg, lit, isDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001986 } else {
buzbeeb046e162012-10-30 15:48:42 -07001987 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbeef0504cd2012-11-13 16:31:10 -08001988 loadValueDirectFixed(cUnit, rlSrc, targetReg(kArg0));
1989 oatClobber(cUnit, targetReg(kArg0));
buzbeeb046e162012-10-30 15:48:42 -07001990 int funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08001991 callRuntimeHelperRegImm(cUnit, funcOffset, targetReg(kArg0), lit, false);
buzbeeb046e162012-10-30 15:48:42 -07001992 if (isDiv)
1993 rlResult = oatGetReturn(cUnit, false);
1994 else
1995 rlResult = oatGetReturnAlt(cUnit);
jeffhao4f8f04a2012-10-02 18:10:35 -07001996 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001997 storeValue(cUnit, rlDest, rlResult);
1998 return false;
1999 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07002000 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002001 default:
2002 return true;
2003 }
2004 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2005 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2006 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2007 if (shiftOp && (lit == 0)) {
2008 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2009 } else {
2010 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2011 }
2012 storeValue(cUnit, rlDest, rlResult);
2013 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002014}
2015
buzbee408ad162012-06-06 16:45:18 -07002016bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002017 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002018{
Bill Buzbeea114add2012-05-03 15:00:40 -07002019 RegLocation rlResult;
2020 OpKind firstOp = kOpBkpt;
2021 OpKind secondOp = kOpBkpt;
2022 bool callOut = false;
2023 bool checkZero = false;
2024 int funcOffset;
buzbeef0504cd2012-11-13 16:31:10 -08002025 int retReg = targetReg(kRet0);
buzbee31a4a6f2012-02-28 15:36:15 -08002026
buzbee408ad162012-06-06 16:45:18 -07002027 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002028 case Instruction::NOT_LONG:
2029 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2030 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2031 // Check for destructive overlap
2032 if (rlResult.lowReg == rlSrc2.highReg) {
2033 int tReg = oatAllocTemp(cUnit);
2034 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2035 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2036 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2037 oatFreeTemp(cUnit, tReg);
2038 } else {
2039 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2040 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2041 }
2042 storeValueWide(cUnit, rlDest, rlResult);
2043 return false;
2044 break;
2045 case Instruction::ADD_LONG:
2046 case Instruction::ADD_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002047 if (cUnit->instructionSet != kThumb2) {
2048 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
2049 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002050 firstOp = kOpAdd;
2051 secondOp = kOpAdc;
2052 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002053 case Instruction::SUB_LONG:
2054 case Instruction::SUB_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002055 if (cUnit->instructionSet != kThumb2) {
2056 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
2057 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002058 firstOp = kOpSub;
2059 secondOp = kOpSbc;
2060 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002061 case Instruction::MUL_LONG:
2062 case Instruction::MUL_LONG_2ADDR:
2063 callOut = true;
buzbeef0504cd2012-11-13 16:31:10 -08002064 retReg = targetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002065 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2066 break;
2067 case Instruction::DIV_LONG:
2068 case Instruction::DIV_LONG_2ADDR:
2069 callOut = true;
2070 checkZero = true;
buzbeef0504cd2012-11-13 16:31:10 -08002071 retReg = targetReg(kRet0);
jeffhao644d5312012-05-03 19:04:49 -07002072 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002073 break;
2074 case Instruction::REM_LONG:
2075 case Instruction::REM_LONG_2ADDR:
2076 callOut = true;
2077 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002078 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08002079 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
2080 retReg = (cUnit->instructionSet == kThumb2) ? targetReg(kArg2) : targetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002081 break;
2082 case Instruction::AND_LONG_2ADDR:
2083 case Instruction::AND_LONG:
buzbeeb046e162012-10-30 15:48:42 -07002084 if (cUnit->instructionSet == kX86) {
2085 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
2086 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002087 firstOp = kOpAnd;
2088 secondOp = kOpAnd;
2089 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002090 case Instruction::OR_LONG:
2091 case Instruction::OR_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002092 if (cUnit->instructionSet == kX86) {
2093 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
2094 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002095 firstOp = kOpOr;
2096 secondOp = kOpOr;
2097 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002098 case Instruction::XOR_LONG:
2099 case Instruction::XOR_LONG_2ADDR:
buzbeeb046e162012-10-30 15:48:42 -07002100 if (cUnit->instructionSet == kX86) {
2101 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
2102 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002103 firstOp = kOpXor;
2104 secondOp = kOpXor;
2105 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07002106 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002107 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002108 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002109 default:
2110 LOG(FATAL) << "Invalid long arith op";
2111 }
2112 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002113 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002114 } else {
2115 oatFlushAllRegs(cUnit); /* Send everything to home location */
2116 if (checkZero) {
buzbeef0504cd2012-11-13 16:31:10 -08002117 loadValueDirectWideFixed(cUnit, rlSrc2, targetReg(kArg2), targetReg(kArg3));
buzbeeb046e162012-10-30 15:48:42 -07002118 int rTgt = callHelperSetup(cUnit, funcOffset);
buzbeef0504cd2012-11-13 16:31:10 -08002119 genDivZeroCheck(cUnit, targetReg(kArg2), targetReg(kArg3));
2120 loadValueDirectWideFixed(cUnit, rlSrc1, targetReg(kArg0), targetReg(kArg1));
buzbee8320f382012-09-11 16:29:42 -07002121 // NOTE: callout here is not a safepoint
buzbeeb046e162012-10-30 15:48:42 -07002122 callHelper(cUnit, rTgt, funcOffset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08002123 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002124 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
buzbee8320f382012-09-11 16:29:42 -07002125 rlSrc1, rlSrc2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002126 }
buzbeef0504cd2012-11-13 16:31:10 -08002127 // Adjust return regs in to handle case of rem returning kArg2/kArg3
2128 if (retReg == targetReg(kRet0))
Bill Buzbeea114add2012-05-03 15:00:40 -07002129 rlResult = oatGetReturnWide(cUnit, false);
2130 else
2131 rlResult = oatGetReturnWideAlt(cUnit);
2132 storeValueWide(cUnit, rlDest, rlResult);
2133 }
2134 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002135}
2136
buzbee408ad162012-06-06 16:45:18 -07002137bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2138 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002139{
Bill Buzbeea114add2012-05-03 15:00:40 -07002140 /*
2141 * Don't optimize the register usage since it calls out to support
2142 * functions
2143 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002144 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002145 if (rlSrc.wide) {
buzbeef0504cd2012-11-13 16:31:10 -08002146 loadValueDirectWideFixed(cUnit, rlSrc, rlSrc.fp ? targetReg(kFArg0) : targetReg(kArg0),
2147 rlSrc.fp ? targetReg(kFArg1) : targetReg(kArg1));
buzbee408ad162012-06-06 16:45:18 -07002148 } else {
buzbeef0504cd2012-11-13 16:31:10 -08002149 loadValueDirectFixed(cUnit, rlSrc, rlSrc.fp ? targetReg(kFArg0) : targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -07002150 }
buzbee8320f382012-09-11 16:29:42 -07002151 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc, false);
buzbee408ad162012-06-06 16:45:18 -07002152 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002153 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002154 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2155 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002156 } else {
2157 RegLocation rlResult;
2158 rlResult = oatGetReturn(cUnit, rlDest.fp);
2159 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002160 }
2161 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002162}
2163
buzbee408ad162012-06-06 16:45:18 -07002164bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002165 RegLocation rlDest, RegLocation rlSrc1,
2166 RegLocation rlSrc2)
2167{
Bill Buzbeea114add2012-05-03 15:00:40 -07002168 RegLocation rlResult;
2169 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002170
buzbee408ad162012-06-06 16:45:18 -07002171 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002172 case Instruction::ADD_FLOAT_2ADDR:
2173 case Instruction::ADD_FLOAT:
2174 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2175 break;
2176 case Instruction::SUB_FLOAT_2ADDR:
2177 case Instruction::SUB_FLOAT:
2178 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2179 break;
2180 case Instruction::DIV_FLOAT_2ADDR:
2181 case Instruction::DIV_FLOAT:
2182 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2183 break;
2184 case Instruction::MUL_FLOAT_2ADDR:
2185 case Instruction::MUL_FLOAT:
2186 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2187 break;
2188 case Instruction::REM_FLOAT_2ADDR:
2189 case Instruction::REM_FLOAT:
2190 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2191 break;
2192 case Instruction::NEG_FLOAT: {
2193 genNegFloat(cUnit, rlDest, rlSrc1);
2194 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002195 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002196 default:
2197 return true;
2198 }
2199 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002200 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002201 rlResult = oatGetReturn(cUnit, true);
2202 storeValue(cUnit, rlDest, rlResult);
2203 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002204}
2205
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