blob: 21f824dfd60aa341526cd1644793debd07c94af9 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers57b86d42012-03-27 16:05:41 -070017#include "oat/runtime/oat_support_entrypoints.h"
18
buzbee31a4a6f2012-02-28 15:36:15 -080019namespace art {
20
21/*
22 * This source files contains "gen" codegen routines that should
23 * be applicable to most targets. Only mid-level support utilities
24 * and "op" calls may be used here.
25 */
buzbee3b3dbdd2012-06-13 13:39:34 -070026void genInvoke(CompilationUnit* cUnit, CallInfo* info);
buzbee31a4a6f2012-02-28 15:36:15 -080027#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080028LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbeef3aac972012-04-11 16:33:36 -070029bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
30 RegLocation rlSrc, RegLocation rlDest, int lit);
buzbee31a4a6f2012-02-28 15:36:15 -080031#endif
32
buzbee8320f382012-09-11 16:29:42 -070033void markSafepointPC(CompilationUnit* cUnit, LIR* inst)
34{
35 inst->defMask = ENCODE_ALL;
36 LIR* safepointPC = newLIR0(cUnit, kPseudoSafepointPC);
37 DCHECK_EQ(safepointPC->defMask, ENCODE_ALL);
38}
39
40void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0, bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -070041#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070042 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070043#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070044 loadConstant(cUnit, rARG0, arg0);
45 oatClobberCalleeSave(cUnit);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070046#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -070047 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070048 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070049#else
buzbee8320f382012-09-11 16:29:42 -070050 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070051#endif
buzbee8320f382012-09-11 16:29:42 -070052 if (safepointPC) {
53 markSafepointPC(cUnit, callInst);
54 }
Ian Rogersab2b55d2012-03-18 00:06:11 -070055}
56
buzbee8320f382012-09-11 16:29:42 -070057void callRuntimeHelperReg(CompilationUnit* cUnit, int helperOffset, int arg0, bool safepointPC) {
Ian Rogers7caad772012-03-30 01:07:54 -070058#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070059 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070060#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070061 opRegCopy(cUnit, rARG0, arg0);
62 oatClobberCalleeSave(cUnit);
Ian Rogers7caad772012-03-30 01:07:54 -070063#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -070064 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070065 oatFreeTemp(cUnit, rTgt);
Ian Rogers7caad772012-03-30 01:07:54 -070066#else
buzbee8320f382012-09-11 16:29:42 -070067 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070068#endif
buzbee8320f382012-09-11 16:29:42 -070069 if (safepointPC) {
70 markSafepointPC(cUnit, callInst);
71 }
Ian Rogers7caad772012-03-30 01:07:54 -070072}
73
buzbee8320f382012-09-11 16:29:42 -070074void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset, RegLocation arg0,
75 bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -070076#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070077 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070078#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070079 if (arg0.wide == 0) {
80 loadValueDirectFixed(cUnit, arg0, rARG0);
81 } else {
82 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
83 }
84 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -070085#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -070086 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070087 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -070088#else
buzbee8320f382012-09-11 16:29:42 -070089 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070090#endif
buzbee8320f382012-09-11 16:29:42 -070091 if (safepointPC) {
92 markSafepointPC(cUnit, callInst);
93 }
Ian Rogersab2b55d2012-03-18 00:06:11 -070094}
95
buzbee8320f382012-09-11 16:29:42 -070096void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
97 bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -070098#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070099 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700100#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700101 loadConstant(cUnit, rARG0, arg0);
102 loadConstant(cUnit, rARG1, arg1);
103 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700104#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700105 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700107#else
buzbee8320f382012-09-11 16:29:42 -0700108 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700109#endif
buzbee8320f382012-09-11 16:29:42 -0700110 if (safepointPC) {
111 markSafepointPC(cUnit, callInst);
112 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700113}
114
buzbee8320f382012-09-11 16:29:42 -0700115void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset, int arg0,
116 RegLocation arg1, bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700117#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700119#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 if (arg1.wide == 0) {
121 loadValueDirectFixed(cUnit, arg1, rARG1);
122 } else {
123 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
124 }
125 loadConstant(cUnit, rARG0, arg0);
126 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700127#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700128 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700130#else
buzbee8320f382012-09-11 16:29:42 -0700131 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700132#endif
buzbee8320f382012-09-11 16:29:42 -0700133 if (safepointPC) {
134 markSafepointPC(cUnit, callInst);
135 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700136}
137
buzbee8320f382012-09-11 16:29:42 -0700138void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset, RegLocation arg0,
139 int arg1, bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700140#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700142#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 loadValueDirectFixed(cUnit, arg0, rARG0);
144 loadConstant(cUnit, rARG1, arg1);
145 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700146#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700147 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700149#else
buzbee8320f382012-09-11 16:29:42 -0700150 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700151#endif
buzbee8320f382012-09-11 16:29:42 -0700152 if (safepointPC) {
153 markSafepointPC(cUnit, callInst);
154 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700155}
156
buzbee8320f382012-09-11 16:29:42 -0700157void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
158 bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700159#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700161#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 opRegCopy(cUnit, rARG1, arg1);
163 loadConstant(cUnit, rARG0, arg0);
164 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700165#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700166 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700168#else
buzbee8320f382012-09-11 16:29:42 -0700169 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700170#endif
buzbee8320f382012-09-11 16:29:42 -0700171 if (safepointPC) {
172 markSafepointPC(cUnit, callInst);
173 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700174}
175
buzbee8320f382012-09-11 16:29:42 -0700176void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
177 bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700178#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700180#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700181 opRegCopy(cUnit, rARG0, arg0);
182 loadConstant(cUnit, rARG1, arg1);
183 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700184#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700185 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700187#else
buzbee8320f382012-09-11 16:29:42 -0700188 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700189#endif
buzbee8320f382012-09-11 16:29:42 -0700190 if (safepointPC) {
191 markSafepointPC(cUnit, callInst);
192 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700193}
194
buzbee8320f382012-09-11 16:29:42 -0700195void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset, int arg0, bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700196#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700198#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 loadCurrMethodDirect(cUnit, rARG1);
200 loadConstant(cUnit, rARG0, arg0);
201 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700202#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700203 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700205#else
buzbee8320f382012-09-11 16:29:42 -0700206 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700207#endif
buzbee8320f382012-09-11 16:29:42 -0700208 if (safepointPC) {
209 markSafepointPC(cUnit, callInst);
210 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700211}
212
buzbee8320f382012-09-11 16:29:42 -0700213void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit, int helperOffset,
214 RegLocation arg0, RegLocation arg1, bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700215#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700217#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700218 if (arg0.wide == 0) {
219 loadValueDirectFixed(cUnit, arg0, rARG0);
220 if (arg1.wide == 0) {
221 loadValueDirectFixed(cUnit, arg1, rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700222 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700223 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700224 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 } else {
226 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
227 if (arg1.wide == 0) {
228 loadValueDirectFixed(cUnit, arg1, rARG2);
229 } else {
230 loadValueDirectWideFixed(cUnit, arg1, rARG2, rARG3);
231 }
232 }
233 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700234#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700235 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700237#else
buzbee8320f382012-09-11 16:29:42 -0700238 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700239#endif
buzbee8320f382012-09-11 16:29:42 -0700240 if (safepointPC) {
241 markSafepointPC(cUnit, callInst);
242 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700243}
244
buzbee8320f382012-09-11 16:29:42 -0700245void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
246 bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700247#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700249#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
251 opRegCopy(cUnit, rARG0, arg0);
252 opRegCopy(cUnit, rARG1, arg1);
253 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700254#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700255 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700257#else
buzbee8320f382012-09-11 16:29:42 -0700258 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700259#endif
buzbee8320f382012-09-11 16:29:42 -0700260 if (safepointPC) {
261 markSafepointPC(cUnit, callInst);
262 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700263}
264
buzbee8320f382012-09-11 16:29:42 -0700265void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg1,
266 int arg2, bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700267#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700269#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
271 opRegCopy(cUnit, rARG0, arg0);
272 opRegCopy(cUnit, rARG1, arg1);
273 loadConstant(cUnit, rARG2, arg2);
274 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700275#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700276 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700278#else
buzbee8320f382012-09-11 16:29:42 -0700279 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700280#endif
buzbee8320f382012-09-11 16:29:42 -0700281 if (safepointPC) {
282 markSafepointPC(cUnit, callInst);
283 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700284}
285
buzbee8320f382012-09-11 16:29:42 -0700286void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit, int helperOffset, int arg0,
287 RegLocation arg2, bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700288#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700290#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 loadValueDirectFixed(cUnit, arg2, rARG2);
292 loadCurrMethodDirect(cUnit, rARG1);
293 loadConstant(cUnit, rARG0, arg0);
294 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700295#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700296 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700298#else
buzbee8320f382012-09-11 16:29:42 -0700299 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700300#endif
buzbee8320f382012-09-11 16:29:42 -0700301 if (safepointPC) {
302 markSafepointPC(cUnit, callInst);
303 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700304}
305
buzbee8320f382012-09-11 16:29:42 -0700306void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset, int arg0, int arg2,
307 bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700308#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700310#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 loadCurrMethodDirect(cUnit, rARG1);
312 loadConstant(cUnit, rARG2, arg2);
313 loadConstant(cUnit, rARG0, arg0);
314 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700315#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700316 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700318#else
buzbee8320f382012-09-11 16:29:42 -0700319 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700320#endif
buzbee8320f382012-09-11 16:29:42 -0700321 if (safepointPC) {
322 markSafepointPC(cUnit, callInst);
323 }
Ian Rogersab2b55d2012-03-18 00:06:11 -0700324}
325
buzbee8320f382012-09-11 16:29:42 -0700326void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit, int helperOffset,
327 int arg0, RegLocation arg1, RegLocation arg2,
328 bool safepointPC) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700329#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700331#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 loadValueDirectFixed(cUnit, arg1, rARG1);
333 if (arg2.wide == 0) {
334 loadValueDirectFixed(cUnit, arg2, rARG2);
335 } else {
336 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
337 }
338 loadConstant(cUnit, rARG0, arg0);
339 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700340#if !defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700341 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700343#else
buzbee8320f382012-09-11 16:29:42 -0700344 LIR* callInst = opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700345#endif
buzbee8320f382012-09-11 16:29:42 -0700346 if (safepointPC) {
347 markSafepointPC(cUnit, callInst);
348 }
buzbee31a4a6f2012-02-28 15:36:15 -0800349}
350
351/*
352 * Generate an kPseudoBarrier marker to indicate the boundary of special
353 * blocks.
354 */
355void genBarrier(CompilationUnit* cUnit)
356{
Bill Buzbeea114add2012-05-03 15:00:40 -0700357 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
358 /* Mark all resources as being clobbered */
359 barrier->defMask = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800360}
361
buzbee31a4a6f2012-02-28 15:36:15 -0800362
363/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800364LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800365{
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
367 branch->target = (LIR*) target;
368 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800369}
370
buzbee5de34942012-03-01 14:51:57 -0800371// FIXME: need to do some work to split out targets with
372// condition codes and those without
373#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -0700374LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee31a4a6f2012-02-28 15:36:15 -0800375 ThrowKind kind)
376{
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700378 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 LIR* branch = opCondBranch(cUnit, cCode, tgt);
380 // Remember branch target - will process later
381 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
382 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800383}
buzbee5de34942012-03-01 14:51:57 -0800384#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800385
386LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700387 int reg, int immVal, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800388{
buzbee408ad162012-06-06 16:45:18 -0700389 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
390 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700391 LIR* branch;
392 if (cCode == kCondAl) {
393 branch = opUnconditionalBranch(cUnit, tgt);
394 } else {
395 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
396 }
397 // Remember branch target - will process later
398 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
399 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800400}
401
402/* Perform null-check on a register. */
buzbee408ad162012-06-06 16:45:18 -0700403LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -0800404{
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
buzbee408ad162012-06-06 16:45:18 -0700406 optFlags & MIR_IGNORE_NULL_CHECK) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700407 return NULL;
408 }
buzbee408ad162012-06-06 16:45:18 -0700409 return genImmedCheck(cUnit, kCondEq, mReg, 0, kThrowNullPointer);
buzbee31a4a6f2012-02-28 15:36:15 -0800410}
411
412/* Perform check on two registers */
413LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700414 int reg1, int reg2, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800415{
Bill Buzbeea114add2012-05-03 15:00:40 -0700416 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700417 cUnit->currentDalvikOffset, reg1, reg2);
buzbee5de34942012-03-01 14:51:57 -0800418#if defined(TARGET_MIPS)
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800420#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 opRegReg(cUnit, kOpCmp, reg1, reg2);
422 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800423#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 // Remember branch target - will process later
425 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
426 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800427}
428
buzbee3b3dbdd2012-06-13 13:39:34 -0700429void genCompareAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
430 RegLocation rlSrc1, RegLocation rlSrc2, LIR* taken,
431 LIR* fallThrough)
buzbee31a4a6f2012-02-28 15:36:15 -0800432{
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 ConditionCode cond;
434 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
435 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 switch (opcode) {
437 case Instruction::IF_EQ:
438 cond = kCondEq;
439 break;
440 case Instruction::IF_NE:
441 cond = kCondNe;
442 break;
443 case Instruction::IF_LT:
444 cond = kCondLt;
445 break;
446 case Instruction::IF_GE:
447 cond = kCondGe;
448 break;
449 case Instruction::IF_GT:
450 cond = kCondGt;
451 break;
452 case Instruction::IF_LE:
453 cond = kCondLe;
454 break;
455 default:
456 cond = (ConditionCode)0;
457 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
458 }
buzbee5de34942012-03-01 14:51:57 -0800459#if defined(TARGET_MIPS)
buzbee3b3dbdd2012-06-13 13:39:34 -0700460 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg, taken);
buzbee5de34942012-03-01 14:51:57 -0800461#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee3b3dbdd2012-06-13 13:39:34 -0700463 opCondBranch(cUnit, cond, taken);
buzbee5de34942012-03-01 14:51:57 -0800464#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700465 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800466}
467
buzbee3b3dbdd2012-06-13 13:39:34 -0700468void genCompareZeroAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
469 RegLocation rlSrc, LIR* taken, LIR* fallThrough)
buzbee31a4a6f2012-02-28 15:36:15 -0800470{
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 ConditionCode cond;
472 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700473 switch (opcode) {
474 case Instruction::IF_EQZ:
475 cond = kCondEq;
476 break;
477 case Instruction::IF_NEZ:
478 cond = kCondNe;
479 break;
480 case Instruction::IF_LTZ:
481 cond = kCondLt;
482 break;
483 case Instruction::IF_GEZ:
484 cond = kCondGe;
485 break;
486 case Instruction::IF_GTZ:
487 cond = kCondGt;
488 break;
489 case Instruction::IF_LEZ:
490 cond = kCondLe;
491 break;
492 default:
493 cond = (ConditionCode)0;
494 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
495 }
Ian Rogers7caad772012-03-30 01:07:54 -0700496#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee3b3dbdd2012-06-13 13:39:34 -0700497 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, taken);
buzbee5de34942012-03-01 14:51:57 -0800498#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee3b3dbdd2012-06-13 13:39:34 -0700500 opCondBranch(cUnit, cond, taken);
buzbee5de34942012-03-01 14:51:57 -0800501#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700502 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800503}
504
buzbee408ad162012-06-06 16:45:18 -0700505void genIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800506 RegLocation rlSrc)
507{
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
509 if (rlSrc.location == kLocPhysReg) {
510 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
511 } else {
512 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
513 }
514 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
515 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800516}
517
buzbee408ad162012-06-06 16:45:18 -0700518void genIntNarrowing(CompilationUnit* cUnit, Instruction::Code opcode,
519 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800520{
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
522 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
523 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700524 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 case Instruction::INT_TO_BYTE:
526 op = kOp2Byte;
527 break;
528 case Instruction::INT_TO_SHORT:
529 op = kOp2Short;
530 break;
531 case Instruction::INT_TO_CHAR:
532 op = kOp2Char;
533 break;
534 default:
535 LOG(ERROR) << "Bad int conversion type";
536 }
537 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
538 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800539}
540
541/*
542 * Let helper function take care of everything. Will call
543 * Array::AllocFromCode(type_idx, method, count);
544 * Note: AllocFromCode will handle checks for errNegativeArraySize.
545 */
buzbee408ad162012-06-06 16:45:18 -0700546void genNewArray(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800547 RegLocation rlSrc)
548{
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700550 int funcOffset;
551 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700552 *cUnit->dex_file,
553 type_idx)) {
554 funcOffset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
555 } else {
556 funcOffset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
557 }
buzbee8320f382012-09-11 16:29:42 -0700558 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 RegLocation rlResult = oatGetReturn(cUnit, false);
560 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800561}
562
563/*
564 * Similar to genNewArray, but with post-allocation initialization.
565 * Verifier guarantees we're dealing with an array class. Current
566 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
567 * Current code also throws internal unimp if not 'L', '[' or 'I'.
568 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700569void genFilledNewArray(CompilationUnit* cUnit, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800570{
buzbee3b3dbdd2012-06-13 13:39:34 -0700571 int elems = info->numArgWords;
572 int typeIdx = info->index;
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 oatFlushAllRegs(cUnit); /* Everything to home location */
574 int funcOffset;
575 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 *cUnit->dex_file,
577 typeIdx)) {
578 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
579 } else {
580 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
581 }
buzbee8320f382012-09-11 16:29:42 -0700582 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700583 oatFreeTemp(cUnit, rARG2);
584 oatFreeTemp(cUnit, rARG1);
585 /*
586 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
587 * return region. Because AllocFromCode placed the new array
588 * in rRET0, we'll just lock it into place. When debugger support is
589 * added, it may be necessary to additionally copy all return
590 * values to a home location in thread-local storage
591 */
592 oatLockTemp(cUnit, rRET0);
593
594 // TODO: use the correct component size, currently all supported types
595 // share array alignment with ints (see comment at head of function)
596 size_t component_size = sizeof(int32_t);
597
598 // Having a range of 0 is legal
buzbee3b3dbdd2012-06-13 13:39:34 -0700599 if (info->isRange && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800600 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 * Bit of ugliness here. We're going generate a mem copy loop
602 * on the register range, but it is possible that some regs
603 * in the range have been promoted. This is unlikely, but
604 * before generating the copy, we'll just force a flush
605 * of any regs in the source range that have been promoted to
606 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800607 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700608 for (int i = 0; i < elems; i++) {
609 RegLocation loc = oatUpdateLoc(cUnit, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700610 if (loc.location == kLocPhysReg) {
611 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
612 loc.lowReg, kWord);
613 }
buzbee31a4a6f2012-02-28 15:36:15 -0800614 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700615 /*
616 * TUNING note: generated code here could be much improved, but
617 * this is an uncommon operation and isn't especially performance
618 * critical.
619 */
620 int rSrc = oatAllocTemp(cUnit);
621 int rDst = oatAllocTemp(cUnit);
622 int rIdx = oatAllocTemp(cUnit);
623#if defined(TARGET_ARM)
624 int rVal = rLR; // Using a lot of temps, rLR is known free here
625#elif defined(TARGET_X86)
jeffhao5772bab2012-05-18 11:51:26 -0700626 oatFreeTemp(cUnit, rRET0);
627 int rVal = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700628#else
629 int rVal = oatAllocTemp(cUnit);
630#endif
631 // Set up source pointer
buzbee3b3dbdd2012-06-13 13:39:34 -0700632 RegLocation rlFirst = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
634 oatSRegOffset(cUnit, rlFirst.sRegLow));
635 // Set up the target pointer
636 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
637 Array::DataOffset(component_size).Int32Value());
638 // Set up the loop counter (known to be > 0)
buzbee3b3dbdd2012-06-13 13:39:34 -0700639 loadConstant(cUnit, rIdx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 // Generate the copy loop. Going backwards for convenience
641 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
642 // Copy next element
643 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
644 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
645#if defined(TARGET_ARM)
646 // Combine sub & test using sub setflags encoding here
647 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
648 opCondBranch(cUnit, kCondGe, target);
649#else
650 oatFreeTemp(cUnit, rVal);
651 opRegImm(cUnit, kOpSub, rIdx, 1);
652 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
653#endif
jeffhao5772bab2012-05-18 11:51:26 -0700654#if defined(TARGET_X86)
655 // Restore the target pointer
656 opRegRegImm(cUnit, kOpAdd, rRET0, rDst,
657 -Array::DataOffset(component_size).Int32Value());
658#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700659 } else if (!info->isRange) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700661 for (int i = 0; i < elems; i++) {
662 RegLocation rlArg = loadValue(cUnit, info->args[i], kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 storeBaseDisp(cUnit, rRET0,
664 Array::DataOffset(component_size).Int32Value() +
665 i * 4, rlArg.lowReg, kWord);
666 // If the loadValue caused a temp to be allocated, free it
667 if (oatIsTemp(cUnit, rlArg.lowReg)) {
668 oatFreeTemp(cUnit, rlArg.lowReg);
669 }
670 }
671 }
buzbeee5f01222012-06-14 15:19:35 -0700672 if (info->result.location != kLocInvalid) {
673 storeValue(cUnit, info->result, oatGetReturn(cUnit, false /* not fp */));
674 }
buzbee31a4a6f2012-02-28 15:36:15 -0800675}
676
buzbee408ad162012-06-06 16:45:18 -0700677void genSput(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800679{
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 int fieldOffset;
681 int ssbIndex;
682 bool isVolatile;
683 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800684
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker, *cUnit->dex_file,
686 cUnit->code_item, cUnit->method_idx, cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800687
Bill Buzbeea114add2012-05-03 15:00:40 -0700688 bool fastPath =
689 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
690 fieldOffset, ssbIndex,
691 isReferrersClass, isVolatile,
692 true);
693 if (fastPath && !SLOW_FIELD_PATH) {
694 DCHECK_GE(fieldOffset, 0);
695 int rBase;
696 if (isReferrersClass) {
697 // Fast path, static storage base is this method's class
698 RegLocation rlMethod = loadCurrMethod(cUnit);
699 rBase = oatAllocTemp(cUnit);
700 loadWordDisp(cUnit, rlMethod.lowReg,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700701 AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700702 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
703 oatFreeTemp(cUnit, rlMethod.lowReg);
704 }
buzbee31a4a6f2012-02-28 15:36:15 -0800705 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700706 // Medium path, static storage base in a different class which
707 // requires checks that the other class is initialized.
708 DCHECK_GE(ssbIndex, 0);
709 // May do runtime call so everything to home locations.
710 oatFlushAllRegs(cUnit);
711 // Using fixed register to sync with possible call to runtime
712 // support.
713 int rMethod = rARG1;
714 oatLockTemp(cUnit, rMethod);
715 loadCurrMethodDirect(cUnit, rMethod);
716 rBase = rARG0;
717 oatLockTemp(cUnit, rBase);
718 loadWordDisp(cUnit, rMethod,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700719 AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700720 rBase);
721 loadWordDisp(cUnit, rBase,
722 Array::DataOffset(sizeof(Object*)).Int32Value() +
723 sizeof(int32_t*) * ssbIndex, rBase);
724 // rBase now points at appropriate static storage base (Class*)
725 // or NULL if not initialized. Check for NULL and call helper if NULL.
726 // TUNING: fast path should fall through
727 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
728 loadConstant(cUnit, rARG0, ssbIndex);
buzbee8320f382012-09-11 16:29:42 -0700729 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssbIndex, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700730#if defined(TARGET_MIPS)
731 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
732 opRegCopy(cUnit, rBase, rRET0);
733#endif
734 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
735 branchOver->target = (LIR*)skipTarget;
736 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800737 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700738 // rBase now holds static storage base
739 if (isLongOrDouble) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700740 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
741 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700742 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
743 }
744//FIXME: need to generalize the barrier call
745 if (isVolatile) {
746 oatGenMemBarrier(cUnit, kST);
747 }
748 if (isLongOrDouble) {
749 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
750 rlSrc.highReg);
751 } else {
752 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
753 }
754 if (isVolatile) {
755 oatGenMemBarrier(cUnit, kSY);
756 }
757 if (isObject) {
758 markGCCard(cUnit, rlSrc.lowReg, rBase);
759 }
760 oatFreeTemp(cUnit, rBase);
761 } else {
762 oatFlushAllRegs(cUnit); // Everything to home locations
763 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Static) :
764 (isObject ? ENTRYPOINT_OFFSET(pSetObjStatic)
765 : ENTRYPOINT_OFFSET(pSet32Static));
buzbee8320f382012-09-11 16:29:42 -0700766 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700767 }
buzbee31a4a6f2012-02-28 15:36:15 -0800768}
769
buzbee408ad162012-06-06 16:45:18 -0700770void genSget(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700771 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800772{
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 int fieldOffset;
774 int ssbIndex;
775 bool isVolatile;
776 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800777
Bill Buzbeea114add2012-05-03 15:00:40 -0700778 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700779 *cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700780 cUnit->code_item, cUnit->method_idx,
781 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800782
Bill Buzbeea114add2012-05-03 15:00:40 -0700783 bool fastPath =
784 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
785 fieldOffset, ssbIndex,
786 isReferrersClass, isVolatile,
787 false);
788 if (fastPath && !SLOW_FIELD_PATH) {
789 DCHECK_GE(fieldOffset, 0);
790 int rBase;
791 if (isReferrersClass) {
792 // Fast path, static storage base is this method's class
793 RegLocation rlMethod = loadCurrMethod(cUnit);
794 rBase = oatAllocTemp(cUnit);
795 loadWordDisp(cUnit, rlMethod.lowReg,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700796 AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800797 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700798 // Medium path, static storage base in a different class which
799 // requires checks that the other class is initialized
800 DCHECK_GE(ssbIndex, 0);
801 // May do runtime call so everything to home locations.
802 oatFlushAllRegs(cUnit);
803 // Using fixed register to sync with possible call to runtime
804 // support
805 int rMethod = rARG1;
806 oatLockTemp(cUnit, rMethod);
807 loadCurrMethodDirect(cUnit, rMethod);
808 rBase = rARG0;
809 oatLockTemp(cUnit, rBase);
810 loadWordDisp(cUnit, rMethod,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700811 AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700812 rBase);
813 loadWordDisp(cUnit, rBase,
814 Array::DataOffset(sizeof(Object*)).Int32Value() +
815 sizeof(int32_t*) * ssbIndex, rBase);
816 // rBase now points at appropriate static storage base (Class*)
817 // or NULL if not initialized. Check for NULL and call helper if NULL.
818 // TUNING: fast path should fall through
819 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
buzbee8320f382012-09-11 16:29:42 -0700820 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssbIndex, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700821#if defined(TARGET_MIPS)
822 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
823 opRegCopy(cUnit, rBase, rRET0);
824#endif
825 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
826 branchOver->target = (LIR*)skipTarget;
827 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800828 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 // rBase now holds static storage base
Bill Buzbeea114add2012-05-03 15:00:40 -0700830 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
831 if (isVolatile) {
832 oatGenMemBarrier(cUnit, kSY);
833 }
834 if (isLongOrDouble) {
buzbee408ad162012-06-06 16:45:18 -0700835 loadBaseDispWide(cUnit, rBase, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700836 rlResult.highReg, INVALID_SREG);
837 } else {
838 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
839 }
840 oatFreeTemp(cUnit, rBase);
841 if (isLongOrDouble) {
842 storeValueWide(cUnit, rlDest, rlResult);
843 } else {
844 storeValue(cUnit, rlDest, rlResult);
845 }
846 } else {
847 oatFlushAllRegs(cUnit); // Everything to home locations
848 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Static) :
849 (isObject ? ENTRYPOINT_OFFSET(pGetObjStatic)
850 : ENTRYPOINT_OFFSET(pGet32Static));
buzbee8320f382012-09-11 16:29:42 -0700851 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 if (isLongOrDouble) {
853 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
854 storeValueWide(cUnit, rlDest, rlResult);
855 } else {
856 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
857 storeValue(cUnit, rlDest, rlResult);
858 }
859 }
buzbee31a4a6f2012-02-28 15:36:15 -0800860}
861
862
863// Debugging routine - if null target, branch to DebugMe
864void genShowTarget(CompilationUnit* cUnit)
865{
buzbeea7678db2012-03-05 15:35:46 -0800866#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700867 UNIMPLEMENTED(WARNING) << "genShowTarget";
buzbeea7678db2012-03-05 15:35:46 -0800868#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700869 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
870 loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pDebugMe), rINVOKE_TGT);
871 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
872 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800873#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800874}
875
buzbee31a4a6f2012-02-28 15:36:15 -0800876void handleSuspendLaunchpads(CompilationUnit *cUnit)
877{
Bill Buzbeea114add2012-05-03 15:00:40 -0700878 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
879 int numElems = cUnit->suspendLaunchpads.numUsed;
880 for (int i = 0; i < numElems; i++) {
881 oatResetRegPool(cUnit);
882 oatResetDefTracking(cUnit);
883 LIR* lab = suspendLabel[i];
884 LIR* resumeLab = (LIR*)lab->operands[0];
885 cUnit->currentDalvikOffset = lab->operands[1];
886 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700887#if defined(TARGET_X86)
buzbee8320f382012-09-11 16:29:42 -0700888 LIR* callInst = opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700889#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700890 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
buzbee8320f382012-09-11 16:29:42 -0700891 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700892#endif
buzbee8320f382012-09-11 16:29:42 -0700893 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -0700894 opUnconditionalBranch(cUnit, resumeLab);
895 }
buzbee31a4a6f2012-02-28 15:36:15 -0800896}
897
buzbeefc9e6fa2012-03-23 15:14:29 -0700898void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
899{
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
901 int numElems = cUnit->intrinsicLaunchpads.numUsed;
902 for (int i = 0; i < numElems; i++) {
903 oatResetRegPool(cUnit);
904 oatResetDefTracking(cUnit);
905 LIR* lab = intrinsicLabel[i];
buzbee3b3dbdd2012-06-13 13:39:34 -0700906 CallInfo* info = (CallInfo*)lab->operands[0];
buzbee15bf9802012-06-12 17:49:27 -0700907 cUnit->currentDalvikOffset = info->offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 oatAppendLIR(cUnit, lab);
buzbee8320f382012-09-11 16:29:42 -0700909 // NOTE: genInvoke handles markSafepointPC
buzbee15bf9802012-06-12 17:49:27 -0700910 genInvoke(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 LIR* resumeLab = (LIR*)lab->operands[2];
912 if (resumeLab != NULL) {
913 opUnconditionalBranch(cUnit, resumeLab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700914 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700915 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700916}
917
buzbee31a4a6f2012-02-28 15:36:15 -0800918void handleThrowLaunchpads(CompilationUnit *cUnit)
919{
Bill Buzbeea114add2012-05-03 15:00:40 -0700920 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
921 int numElems = cUnit->throwLaunchpads.numUsed;
922 for (int i = 0; i < numElems; i++) {
923 oatResetRegPool(cUnit);
924 oatResetDefTracking(cUnit);
925 LIR* lab = throwLabel[i];
926 cUnit->currentDalvikOffset = lab->operands[1];
927 oatAppendLIR(cUnit, lab);
928 int funcOffset = 0;
929 int v1 = lab->operands[2];
930 int v2 = lab->operands[3];
931 switch (lab->operands[0]) {
932 case kThrowNullPointer:
933 funcOffset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
934 break;
935 case kThrowArrayBounds:
jeffhao44335e12012-06-18 13:48:25 -0700936 // Move v1 (array index) to rARG0 and v2 (array length) to rARG1
Bill Buzbeea114add2012-05-03 15:00:40 -0700937 if (v2 != rARG0) {
938 opRegCopy(cUnit, rARG0, v1);
jeffhao703f2cd2012-07-13 17:25:52 -0700939#if defined (TARGET_X86)
940 // x86 leaves the array pointer in v2, so load the array length that the handler expects
941 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
942#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700943 opRegCopy(cUnit, rARG1, v2);
jeffhao703f2cd2012-07-13 17:25:52 -0700944#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 } else {
946 if (v1 == rARG1) {
jeffhao44335e12012-06-18 13:48:25 -0700947 // Swap v1 and v2, using rARG2 as a temp
948 opRegCopy(cUnit, rARG2, v1);
jeffhao703f2cd2012-07-13 17:25:52 -0700949#if defined (TARGET_X86)
950 // x86 leaves the array pointer in v2, so load the array length that the handler expects
951 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
952#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700953 opRegCopy(cUnit, rARG1, v2);
jeffhao703f2cd2012-07-13 17:25:52 -0700954#endif
jeffhao44335e12012-06-18 13:48:25 -0700955 opRegCopy(cUnit, rARG0, rARG2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700956 } else {
jeffhao703f2cd2012-07-13 17:25:52 -0700957#if defined (TARGET_X86)
958 // x86 leaves the array pointer in v2, so load the array length that the handler expects
959 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
960#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700961 opRegCopy(cUnit, rARG1, v2);
jeffhao703f2cd2012-07-13 17:25:52 -0700962#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700963 opRegCopy(cUnit, rARG0, v1);
964 }
buzbee31a4a6f2012-02-28 15:36:15 -0800965 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700966 funcOffset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
967 break;
968 case kThrowDivZero:
969 funcOffset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
970 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700971 case kThrowNoSuchMethod:
972 opRegCopy(cUnit, rARG0, v1);
973 funcOffset =
974 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
975 break;
976 case kThrowStackOverflow:
977 funcOffset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
978 // Restore stack alignment
Ian Rogersab2b55d2012-03-18 00:06:11 -0700979#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 opRegImm(cUnit, kOpAdd, rSP,
981 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700982#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700983 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700984#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700985 break;
986 default:
987 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800988 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700989 oatClobberCalleeSave(cUnit);
990#if !defined(TARGET_X86)
991 int rTgt = loadHelper(cUnit, funcOffset);
buzbee8320f382012-09-11 16:29:42 -0700992 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700993 oatFreeTemp(cUnit, rTgt);
994#else
buzbee8320f382012-09-11 16:29:42 -0700995 LIR* callInst = opThreadMem(cUnit, kOpBlx, funcOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700996#endif
buzbee8320f382012-09-11 16:29:42 -0700997 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 }
buzbee31a4a6f2012-02-28 15:36:15 -0800999}
1000
1001/* Needed by the Assembler */
1002void oatSetupResourceMasks(LIR* lir)
1003{
Bill Buzbeea114add2012-05-03 15:00:40 -07001004 setupResourceMasks(lir);
buzbee31a4a6f2012-02-28 15:36:15 -08001005}
1006
buzbee16da88c2012-03-20 10:38:17 -07001007bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
1008 int& fieldOffset, bool& isVolatile, bool isPut)
1009{
Bill Buzbeea114add2012-05-03 15:00:40 -07001010 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 *cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001012 cUnit->code_item, cUnit->method_idx,
1013 cUnit->access_flags);
1014 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
1015 fieldOffset, isVolatile, isPut);
buzbee16da88c2012-03-20 10:38:17 -07001016}
1017
buzbee408ad162012-06-06 16:45:18 -07001018void genIGet(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001019 RegLocation rlDest, RegLocation rlObj,
Bill Buzbeea114add2012-05-03 15:00:40 -07001020 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -08001021{
Bill Buzbeea114add2012-05-03 15:00:40 -07001022 int fieldOffset;
1023 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -08001024
Bill Buzbeea114add2012-05-03 15:00:40 -07001025 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001026
Bill Buzbeea114add2012-05-03 15:00:40 -07001027 if (fastPath && !SLOW_FIELD_PATH) {
1028 RegLocation rlResult;
1029 RegisterClass regClass = oatRegClassBySize(size);
1030 DCHECK_GE(fieldOffset, 0);
1031 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1032 if (isLongOrDouble) {
1033 DCHECK(rlDest.wide);
buzbee408ad162012-06-06 16:45:18 -07001034 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001035#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001037 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
1038 loadBaseDispWide(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -07001039 rlResult.highReg, rlObj.sRegLow);
1040 if (isVolatile) {
1041 oatGenMemBarrier(cUnit, kSY);
1042 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001043#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 int regPtr = oatAllocTemp(cUnit);
1045 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1046 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1047 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1048 if (isVolatile) {
1049 oatGenMemBarrier(cUnit, kSY);
1050 }
1051 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001052#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001053 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001054 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001055 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001056 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
1057 loadBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -07001058 kWord, rlObj.sRegLow);
1059 if (isVolatile) {
1060 oatGenMemBarrier(cUnit, kSY);
1061 }
1062 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001063 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001064 } else {
1065 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Instance) :
1066 (isObject ? ENTRYPOINT_OFFSET(pGetObjInstance)
1067 : ENTRYPOINT_OFFSET(pGet32Instance));
buzbee8320f382012-09-11 16:29:42 -07001068 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001069 if (isLongOrDouble) {
1070 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
1071 storeValueWide(cUnit, rlDest, rlResult);
1072 } else {
1073 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
1074 storeValue(cUnit, rlDest, rlResult);
1075 }
1076 }
buzbee31a4a6f2012-02-28 15:36:15 -08001077}
1078
buzbee408ad162012-06-06 16:45:18 -07001079void genIPut(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
1080 RegLocation rlSrc, RegLocation rlObj, bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -08001081{
Bill Buzbeea114add2012-05-03 15:00:40 -07001082 int fieldOffset;
1083 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -08001084
Bill Buzbeea114add2012-05-03 15:00:40 -07001085 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1086 true);
1087 if (fastPath && !SLOW_FIELD_PATH) {
1088 RegisterClass regClass = oatRegClassBySize(size);
1089 DCHECK_GE(fieldOffset, 0);
1090 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1091 if (isLongOrDouble) {
1092 int regPtr;
1093 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee408ad162012-06-06 16:45:18 -07001094 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001095 regPtr = oatAllocTemp(cUnit);
1096 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1097 if (isVolatile) {
1098 oatGenMemBarrier(cUnit, kST);
1099 }
jeffhao41005dd2012-05-09 17:58:52 -07001100 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001101 if (isVolatile) {
1102 oatGenMemBarrier(cUnit, kSY);
1103 }
1104 oatFreeTemp(cUnit, regPtr);
buzbee31a4a6f2012-02-28 15:36:15 -08001105 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001106 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee408ad162012-06-06 16:45:18 -07001107 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001108 if (isVolatile) {
1109 oatGenMemBarrier(cUnit, kST);
1110 }
1111 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1112 if (isVolatile) {
1113 oatGenMemBarrier(cUnit, kSY);
1114 }
1115 if (isObject) {
1116 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1117 }
buzbee31a4a6f2012-02-28 15:36:15 -08001118 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001119 } else {
1120 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Instance) :
1121 (isObject ? ENTRYPOINT_OFFSET(pSetObjInstance)
1122 : ENTRYPOINT_OFFSET(pSet32Instance));
buzbee8320f382012-09-11 16:29:42 -07001123 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset, fieldIdx, rlObj, rlSrc, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001124 }
buzbee31a4a6f2012-02-28 15:36:15 -08001125}
1126
buzbee6969d502012-06-15 16:40:31 -07001127void genConstClass(CompilationUnit* cUnit, uint32_t type_idx,
1128 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001129{
Bill Buzbeea114add2012-05-03 15:00:40 -07001130 RegLocation rlMethod = loadCurrMethod(cUnit);
1131 int resReg = oatAllocTemp(cUnit);
1132 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1133 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001134 *cUnit->dex_file,
1135 type_idx)) {
1136 // Call out to helper which resolves type and verifies access.
1137 // Resolved type returned in rRET0.
buzbee8320f382012-09-11 16:29:42 -07001138 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1139 type_idx, rlMethod.lowReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001140 RegLocation rlResult = oatGetReturn(cUnit, false);
1141 storeValue(cUnit, rlDest, rlResult);
1142 } else {
1143 // We're don't need access checks, load type from dex cache
1144 int32_t dex_cache_offset =
Mathieu Chartier66f19252012-09-18 08:57:04 -07001145 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -07001146 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
1147 int32_t offset_of_type =
1148 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1149 * type_idx);
1150 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(*cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001152 type_idx) || SLOW_TYPE_PATH) {
1153 // Slow path, at runtime test if type is null and if so initialize
1154 oatFlushAllRegs(cUnit);
1155 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, NULL);
1156 // Resolved, store and hop over following code
1157 storeValue(cUnit, rlDest, rlResult);
1158 /*
1159 * Because we have stores of the target value on two paths,
1160 * clobber temp tracking for the destination using the ssa name
1161 */
1162 oatClobberSReg(cUnit, rlDest.sRegLow);
1163 LIR* branch2 = opUnconditionalBranch(cUnit,0);
1164 // TUNING: move slow path to end & remove unconditional branch
1165 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
1166 // Call out to helper, which will return resolved type in rARG0
buzbee8320f382012-09-11 16:29:42 -07001167 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx,
1168 rlMethod.lowReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001169 RegLocation rlResult = oatGetReturn(cUnit, false);
1170 storeValue(cUnit, rlDest, rlResult);
1171 /*
1172 * Because we have stores of the target value on two paths,
1173 * clobber temp tracking for the destination using the ssa name
1174 */
1175 oatClobberSReg(cUnit, rlDest.sRegLow);
1176 // Rejoin code paths
1177 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
1178 branch1->target = (LIR*)target1;
1179 branch2->target = (LIR*)target2;
buzbee31a4a6f2012-02-28 15:36:15 -08001180 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001181 // Fast path, we're done - just store result
1182 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001183 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001184 }
buzbee31a4a6f2012-02-28 15:36:15 -08001185}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001186
buzbee6969d502012-06-15 16:40:31 -07001187void genConstString(CompilationUnit* cUnit, uint32_t string_idx,
1188 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001189{
Bill Buzbeea114add2012-05-03 15:00:40 -07001190 /* NOTE: Most strings should be available at compile time */
Bill Buzbeea114add2012-05-03 15:00:40 -07001191 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1192 (sizeof(String*) * string_idx);
1193 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194 *cUnit->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001195 // slow path, resolve string if not in dex cache
1196 oatFlushAllRegs(cUnit);
1197 oatLockCallTemps(cUnit); // Using explicit registers
1198 loadCurrMethodDirect(cUnit, rARG2);
1199 loadWordDisp(cUnit, rARG2,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001200 AbstractMethod::DexCacheStringsOffset().Int32Value(), rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001201 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001202#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001203 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001204#endif
jeffhaofa147e22012-10-12 17:03:32 -07001205 loadWordDisp(cUnit, rARG0, offset_of_string, rRET0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001206 loadConstant(cUnit, rARG1, string_idx);
buzbee31a4a6f2012-02-28 15:36:15 -08001207#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001208 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1209 genBarrier(cUnit);
1210 // For testing, always force through helper
1211 if (!EXERCISE_SLOWEST_STRING_PATH) {
1212 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001213 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee8320f382012-09-11 16:29:42 -07001215 LIR* callInst = opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
1216 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001217 oatFreeTemp(cUnit, rTgt);
1218#elif defined(TARGET_MIPS)
1219 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1220 opRegCopy(cUnit, rARG0, rARG2); // .eq
buzbee8320f382012-09-11 16:29:42 -07001221 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
1222 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001223 oatFreeTemp(cUnit, rTgt);
1224 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1225 branch->target = target;
1226#else
buzbee8320f382012-09-11 16:29:42 -07001227 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode), rARG2, rARG1, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001228#endif
1229 genBarrier(cUnit);
1230 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
1231 } else {
1232 RegLocation rlMethod = loadCurrMethod(cUnit);
1233 int resReg = oatAllocTemp(cUnit);
1234 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1235 loadWordDisp(cUnit, rlMethod.lowReg,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001236 AbstractMethod::DexCacheStringsOffset().Int32Value(), resReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001237 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1238 storeValue(cUnit, rlDest, rlResult);
1239 }
buzbee31a4a6f2012-02-28 15:36:15 -08001240}
1241
1242/*
1243 * Let helper function take care of everything. Will
1244 * call Class::NewInstanceFromCode(type_idx, method);
1245 */
buzbee408ad162012-06-06 16:45:18 -07001246void genNewInstance(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001247{
Bill Buzbeea114add2012-05-03 15:00:40 -07001248 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -07001249 // alloc will always check for resolution, do we also need to verify
1250 // access because the verifier was unable to?
1251 int funcOffset;
1252 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253 cUnit->method_idx, *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001254 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
1255 } else {
1256 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
1257 }
buzbee8320f382012-09-11 16:29:42 -07001258 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001259 RegLocation rlResult = oatGetReturn(cUnit, false);
1260 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001261}
1262
Ian Rogers474b6da2012-09-25 00:20:38 -07001263void genMoveException(CompilationUnit* cUnit, RegLocation rlDest)
1264{
1265 oatFlushAllRegs(cUnit); /* Everything to home location */
1266 int funcOffset = ENTRYPOINT_OFFSET(pGetAndClearException);
1267#if defined(TARGET_X86)
1268 // Runtime helper will load argument for x86.
1269 callRuntimeHelperReg(cUnit, funcOffset, rARG0, false);
1270#else
1271 callRuntimeHelperReg(cUnit, funcOffset, rSELF, false);
1272#endif
1273 RegLocation rlResult = oatGetReturn(cUnit, false);
1274 storeValue(cUnit, rlDest, rlResult);
1275}
1276
buzbee408ad162012-06-06 16:45:18 -07001277void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001278{
Bill Buzbeea114add2012-05-03 15:00:40 -07001279 oatFlushAllRegs(cUnit);
buzbee8320f382012-09-11 16:29:42 -07001280 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException), rlSrc, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001281}
1282
buzbee408ad162012-06-06 16:45:18 -07001283void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001284 RegLocation rlSrc)
1285{
Bill Buzbeea114add2012-05-03 15:00:40 -07001286 oatFlushAllRegs(cUnit);
1287 // May generate a call - use explicit registers
1288 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001289 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1290 int classReg = rARG2; // rARG2 will hold the Class*
1291 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001292 *cUnit->dex_file,
1293 type_idx)) {
1294 // Check we have access to type_idx and if not throw IllegalAccessError,
1295 // returns Class* in rARG0
buzbee8320f382012-09-11 16:29:42 -07001296 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1297 type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001298 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1299 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1300 } else {
1301 // Load dex cache entry into classReg (rARG2)
1302 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1303 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001304 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001305 int32_t offset_of_type =
1306 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1307 * type_idx);
1308 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1309 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001311 // Need to test presence of type in dex cache at runtime
1312 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1313 // Not resolved
1314 // Call out to helper, which will return resolved type in rRET0
buzbee8320f382012-09-11 16:29:42 -07001315 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001316 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1317 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1318 // Rejoin code paths
1319 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1320 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001321 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001322 }
1323 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
jeffhao4eb68ed2012-10-17 16:41:07 -07001324 RegLocation rlResult = oatGetReturn(cUnit, false);
1325#if defined(TARGET_MIPS)
1326 opRegCopy(cUnit, rlResult.lowReg, r_ZERO); // store false result for if branch is taken
1327#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001328 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1329 /* load object->klass_ */
1330 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1331 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1332 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee8320f382012-09-11 16:29:42 -07001333 LIR* callInst;
buzbee0398c422012-03-02 15:22:47 -08001334#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001335 /* Uses conditional nullification */
1336 int rTgt = loadHelper(cUnit,
1337 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1338 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1339 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1340 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1341 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee8320f382012-09-11 16:29:42 -07001342 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Bill Buzbeea114add2012-05-03 15:00:40 -07001343 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001344#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001345 /* Uses branchovers */
jeffhao4eb68ed2012-10-17 16:41:07 -07001346 loadConstant(cUnit, rlResult.lowReg, 1); // assume true
Bill Buzbeea114add2012-05-03 15:00:40 -07001347 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001348#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001349 int rTgt = loadHelper(cUnit,
1350 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1351 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee8320f382012-09-11 16:29:42 -07001352 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Bill Buzbeea114add2012-05-03 15:00:40 -07001353 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001354#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001355 opRegCopy(cUnit, rARG0, rARG2);
buzbee8320f382012-09-11 16:29:42 -07001356 callInst = opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001357#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001358#endif
buzbee8320f382012-09-11 16:29:42 -07001359 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001360 oatClobberCalleeSave(cUnit);
1361 /* branch targets here */
1362 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001363 storeValue(cUnit, rlDest, rlResult);
1364 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001365#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001366 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001367#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001368}
1369
buzbee408ad162012-06-06 16:45:18 -07001370void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001371{
Bill Buzbeea114add2012-05-03 15:00:40 -07001372 oatFlushAllRegs(cUnit);
1373 // May generate a call - use explicit registers
1374 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001375 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1376 int classReg = rARG2; // rARG2 will hold the Class*
1377 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001378 *cUnit->dex_file,
1379 type_idx)) {
1380 // Check we have access to type_idx and if not throw IllegalAccessError,
1381 // returns Class* in rRET0
1382 // InitializeTypeAndVerifyAccess(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001383 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1384 type_idx, rARG1, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001385 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1386 } else {
1387 // Load dex cache entry into classReg (rARG2)
1388 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001389 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001390 int32_t offset_of_type =
1391 Array::DataOffset(sizeof(Class*)).Int32Value() +
1392 (sizeof(Class*) * type_idx);
1393 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1394 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001395 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001396 // Need to test presence of type in dex cache at runtime
1397 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1398 // Not resolved
1399 // Call out to helper, which will return resolved type in rARG0
1400 // InitializeTypeFromCode(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001401 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, rARG1,
1402 true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001403 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1404 // Rejoin code paths
1405 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1406 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001407 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001408 }
1409 // At this point, classReg (rARG2) has class
1410 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1411 /* Null is OK - continue */
1412 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1413 /* load object->klass_ */
1414 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1415 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1416 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001417#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001418 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
buzbee8320f382012-09-11 16:29:42 -07001419 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode), rARG1, rARG2, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001420#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001421 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1422 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1423 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1424 opRegCopy(cUnit, rARG0, rARG1);
1425 opRegCopy(cUnit, rARG1, rARG2);
1426 oatClobberCalleeSave(cUnit);
buzbee8320f382012-09-11 16:29:42 -07001427 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
1428 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001429 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001430#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001431 /* branch target here */
1432 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1433 branch1->target = target;
1434 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001435}
1436
buzbee31a4a6f2012-02-28 15:36:15 -08001437/*
1438 * Generate array store
1439 *
1440 */
buzbee408ad162012-06-06 16:45:18 -07001441void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001442 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001443{
Bill Buzbeea114add2012-05-03 15:00:40 -07001444 int lenOffset = Array::LengthOffset().Int32Value();
1445 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001446
Bill Buzbeea114add2012-05-03 15:00:40 -07001447 oatFlushAllRegs(cUnit); // Use explicit registers
1448 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001449
Bill Buzbeea114add2012-05-03 15:00:40 -07001450 int rValue = rARG0; // Register holding value
1451 int rArrayClass = rARG1; // Register holding array's Class
1452 int rArray = rARG2; // Register holding array
1453 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001454
Bill Buzbeea114add2012-05-03 15:00:40 -07001455 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1456 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1457 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001458
buzbee408ad162012-06-06 16:45:18 -07001459 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001460
Bill Buzbeea114add2012-05-03 15:00:40 -07001461 // Store of null?
1462 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001463
Bill Buzbeea114add2012-05-03 15:00:40 -07001464 // Get the array's class.
1465 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
buzbee8320f382012-09-11 16:29:42 -07001466 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode), rValue,
1467 rArrayClass, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001468 // Redo loadValues in case they didn't survive the call.
1469 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1470 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1471 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1472 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001473
Bill Buzbeea114add2012-05-03 15:00:40 -07001474 // Branch here if value to be stored == null
1475 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1476 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001477
Ian Rogersb41b33b2012-03-20 14:22:54 -07001478#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001479 // make an extra temp available for card mark below
1480 oatFreeTemp(cUnit, rARG1);
buzbee408ad162012-06-06 16:45:18 -07001481 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001482 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
buzbee408ad162012-06-06 16:45:18 -07001483 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001484 }
buzbee408ad162012-06-06 16:45:18 -07001485 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001486 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001487#else
buzbee408ad162012-06-06 16:45:18 -07001488 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001489 int regLen = INVALID_REG;
1490 if (needsRangeCheck) {
1491 regLen = rARG1;
buzbeef1f86362012-07-10 15:18:31 -07001492 loadWordDisp(cUnit, rArray, lenOffset, regLen); // Get len
Bill Buzbeea114add2012-05-03 15:00:40 -07001493 }
1494 /* rPtr -> array data */
1495 int rPtr = oatAllocTemp(cUnit);
1496 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1497 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001498 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001499 }
1500 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1501 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001502#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001503 oatFreeTemp(cUnit, rIndex);
1504 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001505}
1506
1507/*
1508 * Generate array load
1509 */
buzbee408ad162012-06-06 16:45:18 -07001510void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001511 RegLocation rlArray, RegLocation rlIndex,
1512 RegLocation rlDest, int scale)
1513{
Bill Buzbeea114add2012-05-03 15:00:40 -07001514 RegisterClass regClass = oatRegClassBySize(size);
1515 int lenOffset = Array::LengthOffset().Int32Value();
1516 int dataOffset;
1517 RegLocation rlResult;
1518 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1519 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001520
Bill Buzbeea114add2012-05-03 15:00:40 -07001521 if (size == kLong || size == kDouble) {
1522 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1523 } else {
1524 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1525 }
buzbee31a4a6f2012-02-28 15:36:15 -08001526
Bill Buzbeea114add2012-05-03 15:00:40 -07001527 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001528 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001529
Ian Rogersb5d09b22012-03-06 22:14:17 -08001530#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001531 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001532 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1533 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001534 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001535 }
1536 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001537 int regAddr = oatAllocTemp(cUnit);
1538 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1539 oatFreeTemp(cUnit, rlArray.lowReg);
1540 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001541 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001542 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
jeffhao21e12712012-05-25 19:06:18 -07001543 rlResult.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001544 storeValueWide(cUnit, rlDest, rlResult);
1545 } else {
1546 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001547
buzbee408ad162012-06-06 16:45:18 -07001548 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001549 dataOffset, rlResult.lowReg, INVALID_REG, size,
1550 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001551
Bill Buzbeea114add2012-05-03 15:00:40 -07001552 storeValue(cUnit, rlDest, rlResult);
1553 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001554#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001555 int regPtr = oatAllocTemp(cUnit);
buzbee408ad162012-06-06 16:45:18 -07001556 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001557 int regLen = INVALID_REG;
1558 if (needsRangeCheck) {
1559 regLen = oatAllocTemp(cUnit);
1560 /* Get len */
1561 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1562 }
1563 /* regPtr -> array data */
1564 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1565 oatFreeTemp(cUnit, rlArray.lowReg);
1566 if ((size == kLong) || (size == kDouble)) {
1567 if (scale) {
1568 int rNewIndex = oatAllocTemp(cUnit);
1569 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1570 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1571 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001572 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001573 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001574 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001575 oatFreeTemp(cUnit, rlIndex.lowReg);
1576 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1577
1578 if (needsRangeCheck) {
1579 // TODO: change kCondCS to a more meaningful name, is the sense of
1580 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001581 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001582 oatFreeTemp(cUnit, regLen);
1583 }
1584 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1585
1586 oatFreeTemp(cUnit, regPtr);
1587 storeValueWide(cUnit, rlDest, rlResult);
1588 } else {
1589 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1590
1591 if (needsRangeCheck) {
1592 // TODO: change kCondCS to a more meaningful name, is the sense of
1593 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001594 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001595 oatFreeTemp(cUnit, regLen);
1596 }
1597 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1598 scale, size);
1599
1600 oatFreeTemp(cUnit, regPtr);
1601 storeValue(cUnit, rlDest, rlResult);
1602 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001603#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001604}
1605
1606/*
1607 * Generate array store
1608 *
1609 */
buzbee408ad162012-06-06 16:45:18 -07001610void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001611 RegLocation rlArray, RegLocation rlIndex,
1612 RegLocation rlSrc, int scale)
1613{
Bill Buzbeea114add2012-05-03 15:00:40 -07001614 RegisterClass regClass = oatRegClassBySize(size);
1615 int lenOffset = Array::LengthOffset().Int32Value();
1616 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001617
Bill Buzbeea114add2012-05-03 15:00:40 -07001618 if (size == kLong || size == kDouble) {
1619 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1620 } else {
1621 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1622 }
buzbee31a4a6f2012-02-28 15:36:15 -08001623
Bill Buzbeea114add2012-05-03 15:00:40 -07001624 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1625 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001626#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001627 int regPtr;
1628 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1629 oatClobber(cUnit, rlArray.lowReg);
1630 regPtr = rlArray.lowReg;
1631 } else {
1632 regPtr = oatAllocTemp(cUnit);
1633 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1634 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001635#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001636
Bill Buzbeea114add2012-05-03 15:00:40 -07001637 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001638 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001639
Ian Rogersb41b33b2012-03-20 14:22:54 -07001640#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001641 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001642 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1643 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001644 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001645 }
1646 if ((size == kLong) || (size == kDouble)) {
1647 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1648 } else {
1649 rlSrc = loadValue(cUnit, rlSrc, regClass);
1650 }
jeffhao703f2cd2012-07-13 17:25:52 -07001651 // If the src reg can't be byte accessed, move it to a temp first.
1652 if ((size == kSignedByte || size == kUnsignedByte) && rlSrc.lowReg >= 4) {
1653 int temp = oatAllocTemp(cUnit);
1654 opRegCopy(cUnit, temp, rlSrc.lowReg);
1655 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1656 dataOffset, temp, INVALID_REG, size,
1657 INVALID_SREG);
1658 } else {
1659 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1660 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1661 INVALID_SREG);
1662 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001663#else
buzbee408ad162012-06-06 16:45:18 -07001664 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001665 int regLen = INVALID_REG;
1666 if (needsRangeCheck) {
1667 regLen = oatAllocTemp(cUnit);
1668 //NOTE: max live temps(4) here.
1669 /* Get len */
1670 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1671 }
1672 /* regPtr -> array data */
1673 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1674 /* at this point, regPtr points to array, 2 live temps */
1675 if ((size == kLong) || (size == kDouble)) {
1676 //TUNING: specific wide routine that can handle fp regs
1677 if (scale) {
1678 int rNewIndex = oatAllocTemp(cUnit);
1679 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1680 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1681 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001682 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001683 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001684 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001685 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1686
1687 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001688 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001689 oatFreeTemp(cUnit, regLen);
1690 }
1691
jeffhao41005dd2012-05-09 17:58:52 -07001692 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001693
1694 oatFreeTemp(cUnit, regPtr);
1695 } else {
1696 rlSrc = loadValue(cUnit, rlSrc, regClass);
1697 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001698 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001699 oatFreeTemp(cUnit, regLen);
1700 }
1701 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1702 scale, size);
1703 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001704#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001705}
1706
buzbee408ad162012-06-06 16:45:18 -07001707void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001708 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001709 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001710{
Bill Buzbeea114add2012-05-03 15:00:40 -07001711 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001712#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001713 /*
1714 * NOTE: This is the one place in the code in which we might have
1715 * as many as six live temporary registers. There are 5 in the normal
1716 * set for Arm. Until we have spill capabilities, temporarily add
1717 * lr to the temp set. It is safe to do this locally, but note that
1718 * lr is used explicitly elsewhere in the code generator and cannot
1719 * normally be used as a general temp register.
1720 */
1721 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1722 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001723#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001724 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1725 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1726 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1727 // The longs may overlap - use intermediate temp if so
1728 if (rlResult.lowReg == rlSrc1.highReg) {
1729 int tReg = oatAllocTemp(cUnit);
1730 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1731 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1732 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1733 oatFreeTemp(cUnit, tReg);
1734 } else {
1735 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1736 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1737 rlSrc2.highReg);
1738 }
1739 /*
1740 * NOTE: If rlDest refers to a frame variable in a large frame, the
1741 * following storeValueWide might need to allocate a temp register.
1742 * To further work around the lack of a spill capability, explicitly
1743 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1744 * Remove when spill is functional.
1745 */
1746 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1747 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1748 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001749#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001750 oatClobber(cUnit, rLR);
1751 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001752#endif
1753}
1754
1755
buzbee408ad162012-06-06 16:45:18 -07001756bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001757 RegLocation rlSrc1, RegLocation rlShift)
1758{
Bill Buzbeea114add2012-05-03 15:00:40 -07001759 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001760
buzbee408ad162012-06-06 16:45:18 -07001761 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001762 case Instruction::SHL_LONG:
1763 case Instruction::SHL_LONG_2ADDR:
1764 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1765 break;
1766 case Instruction::SHR_LONG:
1767 case Instruction::SHR_LONG_2ADDR:
1768 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1769 break;
1770 case Instruction::USHR_LONG:
1771 case Instruction::USHR_LONG_2ADDR:
1772 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1773 break;
1774 default:
1775 LOG(FATAL) << "Unexpected case";
1776 return true;
1777 }
1778 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07001779 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001780 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1781 storeValueWide(cUnit, rlDest, rlResult);
1782 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001783}
1784
1785
buzbee408ad162012-06-06 16:45:18 -07001786bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001787 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001788{
Bill Buzbeea114add2012-05-03 15:00:40 -07001789 OpKind op = kOpBkpt;
jeffhao4f8f04a2012-10-02 18:10:35 -07001790 bool isDivRem = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001791 bool checkZero = false;
1792 bool unary = false;
1793 RegLocation rlResult;
1794 bool shiftOp = false;
buzbee408ad162012-06-06 16:45:18 -07001795 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001796 case Instruction::NEG_INT:
1797 op = kOpNeg;
1798 unary = true;
1799 break;
1800 case Instruction::NOT_INT:
1801 op = kOpMvn;
1802 unary = true;
1803 break;
1804 case Instruction::ADD_INT:
1805 case Instruction::ADD_INT_2ADDR:
1806 op = kOpAdd;
1807 break;
1808 case Instruction::SUB_INT:
1809 case Instruction::SUB_INT_2ADDR:
1810 op = kOpSub;
1811 break;
1812 case Instruction::MUL_INT:
1813 case Instruction::MUL_INT_2ADDR:
1814 op = kOpMul;
1815 break;
1816 case Instruction::DIV_INT:
1817 case Instruction::DIV_INT_2ADDR:
1818 checkZero = true;
1819 op = kOpDiv;
jeffhao4f8f04a2012-10-02 18:10:35 -07001820 isDivRem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001821 break;
1822 /* NOTE: returns in rARG1 */
1823 case Instruction::REM_INT:
1824 case Instruction::REM_INT_2ADDR:
1825 checkZero = true;
1826 op = kOpRem;
jeffhao4f8f04a2012-10-02 18:10:35 -07001827 isDivRem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001828 break;
1829 case Instruction::AND_INT:
1830 case Instruction::AND_INT_2ADDR:
1831 op = kOpAnd;
1832 break;
1833 case Instruction::OR_INT:
1834 case Instruction::OR_INT_2ADDR:
1835 op = kOpOr;
1836 break;
1837 case Instruction::XOR_INT:
1838 case Instruction::XOR_INT_2ADDR:
1839 op = kOpXor;
1840 break;
1841 case Instruction::SHL_INT:
1842 case Instruction::SHL_INT_2ADDR:
1843 shiftOp = true;
1844 op = kOpLsl;
1845 break;
1846 case Instruction::SHR_INT:
1847 case Instruction::SHR_INT_2ADDR:
1848 shiftOp = true;
1849 op = kOpAsr;
1850 break;
1851 case Instruction::USHR_INT:
1852 case Instruction::USHR_INT_2ADDR:
1853 shiftOp = true;
1854 op = kOpLsr;
1855 break;
1856 default:
1857 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001858 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001859 }
jeffhao4f8f04a2012-10-02 18:10:35 -07001860 if (!isDivRem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001861 if (unary) {
1862 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1863 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1864 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001865 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001866 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001867#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001868 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1869 int tReg = oatAllocTemp(cUnit);
1870 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001871#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001872 // X86 doesn't require masking and must use ECX
1873 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1874 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001875#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001876 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1877 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1878 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1879 oatFreeTemp(cUnit, tReg);
1880 } else {
1881 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1882 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1883 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1884 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1885 }
buzbee31a4a6f2012-02-28 15:36:15 -08001886 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001887 storeValue(cUnit, rlDest, rlResult);
1888 } else {
jeffhao4f8f04a2012-10-02 18:10:35 -07001889#if defined(TARGET_MIPS)
jeffhao4eb68ed2012-10-17 16:41:07 -07001890 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
jeffhao4f8f04a2012-10-02 18:10:35 -07001891 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1892 if (checkZero) {
jeffhao4eb68ed2012-10-17 16:41:07 -07001893 genImmedCheck(cUnit, kCondEq, rlSrc2.lowReg, 0, kThrowDivZero);
jeffhao4f8f04a2012-10-02 18:10:35 -07001894 }
1895 newLIR4(cUnit, kMipsDiv, r_HI, r_LO, rlSrc1.lowReg, rlSrc2.lowReg);
1896 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1897 if (op == kOpDiv) {
1898 newLIR2(cUnit, kMipsMflo, rlResult.lowReg, r_LO);
1899 } else {
1900 newLIR2(cUnit, kMipsMfhi, rlResult.lowReg, r_HI);
1901 }
1902#else
1903 int funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
Bill Buzbeea114add2012-05-03 15:00:40 -07001904 RegLocation rlResult;
1905 oatFlushAllRegs(cUnit); /* Send everything to home location */
1906 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
jeffhao4f8f04a2012-10-02 18:10:35 -07001907#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001908 int rTgt = loadHelper(cUnit, funcOffset);
1909#endif
1910 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1911 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001912 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001913 }
buzbee8320f382012-09-11 16:29:42 -07001914 // NOTE: callout here is not a safepoint
jeffhao4f8f04a2012-10-02 18:10:35 -07001915#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001916 opReg(cUnit, kOpBlx, rTgt);
1917 oatFreeTemp(cUnit, rTgt);
1918#else
1919 opThreadMem(cUnit, kOpBlx, funcOffset);
1920#endif
jeffhao4f8f04a2012-10-02 18:10:35 -07001921 if (op == kOpDiv)
Bill Buzbeea114add2012-05-03 15:00:40 -07001922 rlResult = oatGetReturn(cUnit, false);
1923 else
1924 rlResult = oatGetReturnAlt(cUnit);
jeffhao4f8f04a2012-10-02 18:10:35 -07001925#endif
jeffhao4eb68ed2012-10-17 16:41:07 -07001926 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07001927 }
1928 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001929}
1930
1931/*
1932 * The following are the first-level codegen routines that analyze the format
1933 * of each bytecode then either dispatch special purpose codegen routines
1934 * or produce corresponding Thumb instructions directly.
1935 */
1936
1937bool isPowerOfTwo(int x)
1938{
Bill Buzbeea114add2012-05-03 15:00:40 -07001939 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001940}
1941
1942// Returns true if no more than two bits are set in 'x'.
1943bool isPopCountLE2(unsigned int x)
1944{
Bill Buzbeea114add2012-05-03 15:00:40 -07001945 x &= x - 1;
1946 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001947}
1948
1949// Returns the index of the lowest set bit in 'x'.
1950int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001951 int bit_posn = 0;
1952 while ((x & 0xf) == 0) {
1953 bit_posn += 4;
1954 x >>= 4;
1955 }
1956 while ((x & 1) == 0) {
1957 bit_posn++;
1958 x >>= 1;
1959 }
1960 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001961}
1962
1963// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1964// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001965bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001966 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001967{
buzbeef3aac972012-04-11 16:33:36 -07001968#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001969 // No divide instruction for Arm, so check for more special cases
1970 if (lit < 2) {
1971 return false;
1972 }
1973 if (!isPowerOfTwo(lit)) {
1974 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1975 }
buzbeef3aac972012-04-11 16:33:36 -07001976#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001977 if (lit < 2 || !isPowerOfTwo(lit)) {
1978 return false;
1979 }
buzbeef3aac972012-04-11 16:33:36 -07001980#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001981 int k = lowestSetBit(lit);
1982 if (k >= 30) {
1983 // Avoid special cases.
1984 return false;
1985 }
1986 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1987 dalvikOpcode == Instruction::DIV_INT_LIT16);
1988 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1989 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1990 if (div) {
1991 int tReg = oatAllocTemp(cUnit);
1992 if (lit == 2) {
1993 // Division by 2 is by far the most common division by constant.
1994 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1995 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1996 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001997 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001998 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1999 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
2000 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
2001 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08002002 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002003 } else {
2004 int tReg1 = oatAllocTemp(cUnit);
2005 int tReg2 = oatAllocTemp(cUnit);
2006 if (lit == 2) {
2007 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2008 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2009 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
2010 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2011 } else {
2012 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2013 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2014 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2015 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
2016 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2017 }
2018 }
2019 storeValue(cUnit, rlDest, rlResult);
2020 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002021}
2022
2023void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
2024 RegLocation rlResult, int lit,
2025 int firstBit, int secondBit)
2026{
buzbee0398c422012-03-02 15:22:47 -08002027#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002028 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
2029 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08002030#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002031 int tReg = oatAllocTemp(cUnit);
2032 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
2033 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
2034 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08002035#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002036 if (firstBit != 0) {
2037 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
2038 }
buzbee31a4a6f2012-02-28 15:36:15 -08002039}
2040
2041// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2042// and store the result in 'rlDest'.
2043bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
2044 RegLocation rlDest, int lit)
2045{
Bill Buzbeea114add2012-05-03 15:00:40 -07002046 // Can we simplify this multiplication?
2047 bool powerOfTwo = false;
2048 bool popCountLE2 = false;
2049 bool powerOfTwoMinusOne = false;
2050 if (lit < 2) {
2051 // Avoid special cases.
2052 return false;
2053 } else if (isPowerOfTwo(lit)) {
2054 powerOfTwo = true;
2055 } else if (isPopCountLE2(lit)) {
2056 popCountLE2 = true;
2057 } else if (isPowerOfTwo(lit + 1)) {
2058 powerOfTwoMinusOne = true;
2059 } else {
2060 return false;
2061 }
2062 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2063 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2064 if (powerOfTwo) {
2065 // Shift.
2066 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2067 lowestSetBit(lit));
2068 } else if (popCountLE2) {
2069 // Shift and add and shift.
2070 int firstBit = lowestSetBit(lit);
2071 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2072 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2073 firstBit, secondBit);
2074 } else {
2075 // Reverse subtract: (src << (shift + 1)) - src.
2076 DCHECK(powerOfTwoMinusOne);
2077 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2078 int tReg = oatAllocTemp(cUnit);
2079 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2080 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2081 }
2082 storeValue(cUnit, rlDest, rlResult);
2083 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002084}
2085
buzbee408ad162012-06-06 16:45:18 -07002086bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2087 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002088{
Bill Buzbeea114add2012-05-03 15:00:40 -07002089 RegLocation rlResult;
2090 OpKind op = (OpKind)0; /* Make gcc happy */
2091 int shiftOp = false;
2092 bool isDiv = false;
buzbee31a4a6f2012-02-28 15:36:15 -08002093
buzbee408ad162012-06-06 16:45:18 -07002094 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002095 case Instruction::RSUB_INT_LIT8:
2096 case Instruction::RSUB_INT: {
2097 int tReg;
2098 //TUNING: add support for use of Arm rsub op
2099 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2100 tReg = oatAllocTemp(cUnit);
2101 loadConstant(cUnit, tReg, lit);
2102 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2103 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2104 storeValue(cUnit, rlDest, rlResult);
2105 return false;
2106 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002107 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002108
2109 case Instruction::ADD_INT_LIT8:
2110 case Instruction::ADD_INT_LIT16:
2111 op = kOpAdd;
2112 break;
2113 case Instruction::MUL_INT_LIT8:
2114 case Instruction::MUL_INT_LIT16: {
2115 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2116 return false;
2117 }
2118 op = kOpMul;
2119 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002120 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002121 case Instruction::AND_INT_LIT8:
2122 case Instruction::AND_INT_LIT16:
2123 op = kOpAnd;
2124 break;
2125 case Instruction::OR_INT_LIT8:
2126 case Instruction::OR_INT_LIT16:
2127 op = kOpOr;
2128 break;
2129 case Instruction::XOR_INT_LIT8:
2130 case Instruction::XOR_INT_LIT16:
2131 op = kOpXor;
2132 break;
2133 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002134 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002135 lit &= 31;
2136 shiftOp = true;
2137 op = kOpLsl;
2138 break;
2139 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002140 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002141 lit &= 31;
2142 shiftOp = true;
2143 op = kOpAsr;
2144 break;
2145 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002146 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002147 lit &= 31;
2148 shiftOp = true;
2149 op = kOpLsr;
2150 break;
2151
2152 case Instruction::DIV_INT_LIT8:
2153 case Instruction::DIV_INT_LIT16:
2154 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07002155 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07002156 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002157 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002158 return false;
2159 }
buzbee408ad162012-06-06 16:45:18 -07002160 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002161 return false;
2162 }
buzbee408ad162012-06-06 16:45:18 -07002163 if ((opcode == Instruction::DIV_INT_LIT8) ||
2164 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002165 isDiv = true;
2166 } else {
2167 isDiv = false;
2168 }
jeffhao4f8f04a2012-10-02 18:10:35 -07002169#if defined(TARGET_MIPS)
2170 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2171 int tReg = oatAllocTemp(cUnit);
2172 newLIR3(cUnit, kMipsAddiu, tReg, r_ZERO, lit);
2173 newLIR4(cUnit, kMipsDiv, r_HI, r_LO, rlSrc.lowReg, tReg);
2174 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2175 if (isDiv) {
2176 newLIR2(cUnit, kMipsMflo, rlResult.lowReg, r_LO);
2177 } else {
2178 newLIR2(cUnit, kMipsMfhi, rlResult.lowReg, r_HI);
2179 }
2180 oatFreeTemp(cUnit, tReg);
2181#else
2182 oatFlushAllRegs(cUnit); /* Everything to home location */
2183 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2184 oatClobber(cUnit, rARG0);
2185 int funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee8320f382012-09-11 16:29:42 -07002186 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002187 if (isDiv)
2188 rlResult = oatGetReturn(cUnit, false);
2189 else
2190 rlResult = oatGetReturnAlt(cUnit);
jeffhao4f8f04a2012-10-02 18:10:35 -07002191#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002192 storeValue(cUnit, rlDest, rlResult);
2193 return false;
2194 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07002195 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002196 default:
2197 return true;
2198 }
2199 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2200 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2201 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2202 if (shiftOp && (lit == 0)) {
2203 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2204 } else {
2205 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2206 }
2207 storeValue(cUnit, rlDest, rlResult);
2208 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002209}
2210
buzbee408ad162012-06-06 16:45:18 -07002211bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002212 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002213{
Bill Buzbeea114add2012-05-03 15:00:40 -07002214 RegLocation rlResult;
2215 OpKind firstOp = kOpBkpt;
2216 OpKind secondOp = kOpBkpt;
2217 bool callOut = false;
2218 bool checkZero = false;
2219 int funcOffset;
2220 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002221
buzbee408ad162012-06-06 16:45:18 -07002222 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002223 case Instruction::NOT_LONG:
2224 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2225 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2226 // Check for destructive overlap
2227 if (rlResult.lowReg == rlSrc2.highReg) {
2228 int tReg = oatAllocTemp(cUnit);
2229 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2230 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2231 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2232 oatFreeTemp(cUnit, tReg);
2233 } else {
2234 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2235 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2236 }
2237 storeValueWide(cUnit, rlDest, rlResult);
2238 return false;
2239 break;
2240 case Instruction::ADD_LONG:
2241 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002242#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002243 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002244#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002245 firstOp = kOpAdd;
2246 secondOp = kOpAdc;
2247 break;
buzbeec5159d52012-03-03 11:48:39 -08002248#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002249 case Instruction::SUB_LONG:
2250 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002251#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002252 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002253#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002254 firstOp = kOpSub;
2255 secondOp = kOpSbc;
2256 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002257#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002258 case Instruction::MUL_LONG:
2259 case Instruction::MUL_LONG_2ADDR:
2260 callOut = true;
2261 retReg = rRET0;
2262 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2263 break;
2264 case Instruction::DIV_LONG:
2265 case Instruction::DIV_LONG_2ADDR:
2266 callOut = true;
2267 checkZero = true;
2268 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002269 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002270 break;
2271 case Instruction::REM_LONG:
2272 case Instruction::REM_LONG_2ADDR:
2273 callOut = true;
2274 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002275 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002276#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002277 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2278 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002279#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002280 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002281#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002282 break;
2283 case Instruction::AND_LONG_2ADDR:
2284 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002285#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002286 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002287#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002288 firstOp = kOpAnd;
2289 secondOp = kOpAnd;
2290 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002291#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002292 case Instruction::OR_LONG:
2293 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002294#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002295 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002296#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002297 firstOp = kOpOr;
2298 secondOp = kOpOr;
2299 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002300#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002301 case Instruction::XOR_LONG:
2302 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002303#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002304 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002305#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002306 firstOp = kOpXor;
2307 secondOp = kOpXor;
2308 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002309#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002310 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002311 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002312 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002313 default:
2314 LOG(FATAL) << "Invalid long arith op";
2315 }
2316 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002317 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002318 } else {
2319 oatFlushAllRegs(cUnit); /* Send everything to home location */
2320 if (checkZero) {
2321 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2322#if !defined(TARGET_X86)
2323 int rTgt = loadHelper(cUnit, funcOffset);
2324#endif
2325 int tReg = oatAllocTemp(cUnit);
2326#if defined(TARGET_ARM)
2327 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2328 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002329 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002330#else
2331 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2332#endif
buzbee408ad162012-06-06 16:45:18 -07002333 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002334 oatFreeTemp(cUnit, tReg);
2335 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
buzbee8320f382012-09-11 16:29:42 -07002336 // NOTE: callout here is not a safepoint
Bill Buzbeea114add2012-05-03 15:00:40 -07002337#if !defined(TARGET_X86)
2338 opReg(cUnit, kOpBlx, rTgt);
2339 oatFreeTemp(cUnit, rTgt);
2340#else
2341 opThreadMem(cUnit, kOpBlx, funcOffset);
2342#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002343 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002344 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
buzbee8320f382012-09-11 16:29:42 -07002345 rlSrc1, rlSrc2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002346 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002347 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2348 if (retReg == rRET0)
2349 rlResult = oatGetReturnWide(cUnit, false);
2350 else
2351 rlResult = oatGetReturnWideAlt(cUnit);
2352 storeValueWide(cUnit, rlDest, rlResult);
2353 }
2354 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002355}
2356
buzbee408ad162012-06-06 16:45:18 -07002357bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2358 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002359{
Bill Buzbeea114add2012-05-03 15:00:40 -07002360 /*
2361 * Don't optimize the register usage since it calls out to support
2362 * functions
2363 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002364 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002365 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002366 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002367 } else {
2368 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002369 }
buzbee8320f382012-09-11 16:29:42 -07002370 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc, false);
buzbee408ad162012-06-06 16:45:18 -07002371 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002372 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002373 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2374 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002375 } else {
2376 RegLocation rlResult;
2377 rlResult = oatGetReturn(cUnit, rlDest.fp);
2378 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002379 }
2380 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002381}
2382
2383void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002384bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002385 RegLocation rlDest, RegLocation rlSrc1,
2386 RegLocation rlSrc2)
2387{
Bill Buzbeea114add2012-05-03 15:00:40 -07002388 RegLocation rlResult;
2389 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002390
buzbee408ad162012-06-06 16:45:18 -07002391 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002392 case Instruction::ADD_FLOAT_2ADDR:
2393 case Instruction::ADD_FLOAT:
2394 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2395 break;
2396 case Instruction::SUB_FLOAT_2ADDR:
2397 case Instruction::SUB_FLOAT:
2398 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2399 break;
2400 case Instruction::DIV_FLOAT_2ADDR:
2401 case Instruction::DIV_FLOAT:
2402 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2403 break;
2404 case Instruction::MUL_FLOAT_2ADDR:
2405 case Instruction::MUL_FLOAT:
2406 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2407 break;
2408 case Instruction::REM_FLOAT_2ADDR:
2409 case Instruction::REM_FLOAT:
2410 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2411 break;
2412 case Instruction::NEG_FLOAT: {
2413 genNegFloat(cUnit, rlDest, rlSrc1);
2414 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002415 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002416 default:
2417 return true;
2418 }
2419 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002420 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002421 rlResult = oatGetReturn(cUnit, true);
2422 storeValue(cUnit, rlDest, rlResult);
2423 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002424}
2425
2426void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002427bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002428 RegLocation rlDest, RegLocation rlSrc1,
2429 RegLocation rlSrc2)
2430{
Bill Buzbeea114add2012-05-03 15:00:40 -07002431 RegLocation rlResult;
2432 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002433
buzbee408ad162012-06-06 16:45:18 -07002434 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002435 case Instruction::ADD_DOUBLE_2ADDR:
2436 case Instruction::ADD_DOUBLE:
2437 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2438 break;
2439 case Instruction::SUB_DOUBLE_2ADDR:
2440 case Instruction::SUB_DOUBLE:
2441 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2442 break;
2443 case Instruction::DIV_DOUBLE_2ADDR:
2444 case Instruction::DIV_DOUBLE:
2445 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2446 break;
2447 case Instruction::MUL_DOUBLE_2ADDR:
2448 case Instruction::MUL_DOUBLE:
2449 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2450 break;
2451 case Instruction::REM_DOUBLE_2ADDR:
2452 case Instruction::REM_DOUBLE:
2453 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2454 break;
2455 case Instruction::NEG_DOUBLE: {
2456 genNegDouble(cUnit, rlDest, rlSrc1);
2457 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002458 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002459 default:
2460 return true;
2461 }
2462 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002463 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002464 rlResult = oatGetReturnWide(cUnit, true);
2465 storeValueWide(cUnit, rlDest, rlResult);
2466 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002467}
2468
buzbee408ad162012-06-06 16:45:18 -07002469bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2470 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002471{
buzbee31a4a6f2012-02-28 15:36:15 -08002472
Bill Buzbeea114add2012-05-03 15:00:40 -07002473 switch (opcode) {
2474 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002475 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2476 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002477 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002478 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2479 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002480 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002481 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2482 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002483 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002484 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2485 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002486 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002487 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2488 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002489 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002490 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2491 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002492 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002493 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2494 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002495 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002496 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2497 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002498 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002499 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2500 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002501 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002502 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2503 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002504 default:
2505 return true;
2506 }
2507 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002508}
2509
2510/*
2511 * Generate callout to updateDebugger. Note that we're overloading
2512 * the use of rSUSPEND here. When the debugger is active, this
2513 * register holds the address of the update function. So, if it's
2514 * non-null, we call out to it.
2515 *
2516 * Note also that rRET0 and rRET1 must be preserved across this
2517 * code. This must be handled by the stub.
2518 */
2519void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2520{
Bill Buzbeea114add2012-05-03 15:00:40 -07002521 // Following DCHECK verifies that dPC is in range of single load immediate
2522 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2523 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2524 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002525#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002526 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2527 opIT(cUnit, kArmCondNe, "T");
2528 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
buzbee8320f382012-09-11 16:29:42 -07002529 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2530 markSafepointPC(cUnit, callInst);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002531#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002532 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002533#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002534 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2535 loadConstant(cUnit, rARG2, offset);
buzbee8320f382012-09-11 16:29:42 -07002536 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2537 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002538 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2539 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002540#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002541 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002542}
2543
2544/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002545void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002546{
buzbee408ad162012-06-06 16:45:18 -07002547 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002548 return;
2549 }
2550 oatFlushAllRegs(cUnit);
2551 if (cUnit->genDebugger) {
2552 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002553#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002554 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002555#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002556 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
buzbee8320f382012-09-11 16:29:42 -07002557 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
2558 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002559 // Refresh rSUSPEND
2560 loadWordDisp(cUnit, rSELF,
2561 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2562 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002563#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002564 } else {
2565 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002566#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002567 // In non-debug case, only check periodically
2568 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2569 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002570#elif defined(TARGET_X86)
Ian Rogers474b6da2012-09-25 00:20:38 -07002571 newLIR2(cUnit, kX86Cmp16TI8, Thread::ThreadFlagsOffset().Int32Value(), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002572 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002573#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002574 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2575 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002576#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002577 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2578 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002579 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002580 branch->target = (LIR*)target;
2581 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2582 }
buzbee31a4a6f2012-02-28 15:36:15 -08002583}
2584
buzbeefead2932012-03-30 14:02:01 -07002585/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002586void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002587{
buzbee408ad162012-06-06 16:45:18 -07002588 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002589 opUnconditionalBranch(cUnit, target);
2590 return;
2591 }
2592 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002593 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002594 opUnconditionalBranch(cUnit, target);
2595 } else {
buzbeefead2932012-03-30 14:02:01 -07002596#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002597 // In non-debug case, only check periodically
2598 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2599 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002600#elif defined(TARGET_X86)
Ian Rogers474b6da2012-09-25 00:20:38 -07002601 newLIR2(cUnit, kX86Cmp16TI8, Thread::ThreadFlagsOffset().Int32Value(), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002602 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002603#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002604 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2605 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002606#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002607 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002608 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002609 oatFlushAllRegs(cUnit);
2610 opUnconditionalBranch(cUnit, launchPad);
2611 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2612 (intptr_t)launchPad);
2613 }
buzbeefead2932012-03-30 14:02:01 -07002614}
2615
buzbee31a4a6f2012-02-28 15:36:15 -08002616} // namespace art