blob: 99a76da00725d5afacc8499fe6adc12b35717811 [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
Bill Buzbeea114add2012-05-03 15:00:40 -07001205 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1206 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
buzbee408ad162012-06-06 16:45:18 -07001263void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001264{
Bill Buzbeea114add2012-05-03 15:00:40 -07001265 oatFlushAllRegs(cUnit);
buzbee8320f382012-09-11 16:29:42 -07001266 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException), rlSrc, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001267}
1268
buzbee408ad162012-06-06 16:45:18 -07001269void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001270 RegLocation rlSrc)
1271{
Bill Buzbeea114add2012-05-03 15:00:40 -07001272 oatFlushAllRegs(cUnit);
1273 // May generate a call - use explicit registers
1274 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001275 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1276 int classReg = rARG2; // rARG2 will hold the Class*
1277 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001278 *cUnit->dex_file,
1279 type_idx)) {
1280 // Check we have access to type_idx and if not throw IllegalAccessError,
1281 // returns Class* in rARG0
buzbee8320f382012-09-11 16:29:42 -07001282 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1283 type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001284 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1285 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1286 } else {
1287 // Load dex cache entry into classReg (rARG2)
1288 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1289 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001290 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001291 int32_t offset_of_type =
1292 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1293 * type_idx);
1294 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1295 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001296 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001297 // Need to test presence of type in dex cache at runtime
1298 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1299 // Not resolved
1300 // Call out to helper, which will return resolved type in rRET0
buzbee8320f382012-09-11 16:29:42 -07001301 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001302 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1303 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1304 // Rejoin code paths
1305 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1306 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001307 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001308 }
1309 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
1310 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1311 /* load object->klass_ */
1312 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1313 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1314 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee8320f382012-09-11 16:29:42 -07001315 LIR* callInst;
buzbee0398c422012-03-02 15:22:47 -08001316#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001317 /* Uses conditional nullification */
1318 int rTgt = loadHelper(cUnit,
1319 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1320 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1321 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1322 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1323 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee8320f382012-09-11 16:29:42 -07001324 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Bill Buzbeea114add2012-05-03 15:00:40 -07001325 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001326#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001327 /* Uses branchovers */
1328 loadConstant(cUnit, rARG0, 1); // assume true
1329 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001330#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001331 int rTgt = loadHelper(cUnit,
1332 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1333 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee8320f382012-09-11 16:29:42 -07001334 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Bill Buzbeea114add2012-05-03 15:00:40 -07001335 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001336#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001337 opRegCopy(cUnit, rARG0, rARG2);
buzbee8320f382012-09-11 16:29:42 -07001338 callInst = opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001339#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001340#endif
buzbee8320f382012-09-11 16:29:42 -07001341 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001342 oatClobberCalleeSave(cUnit);
1343 /* branch targets here */
1344 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1345 RegLocation rlResult = oatGetReturn(cUnit, false);
1346 storeValue(cUnit, rlDest, rlResult);
1347 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001348#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001349 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001350#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001351}
1352
buzbee408ad162012-06-06 16:45:18 -07001353void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001354{
Bill Buzbeea114add2012-05-03 15:00:40 -07001355 oatFlushAllRegs(cUnit);
1356 // May generate a call - use explicit registers
1357 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001358 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1359 int classReg = rARG2; // rARG2 will hold the Class*
1360 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001361 *cUnit->dex_file,
1362 type_idx)) {
1363 // Check we have access to type_idx and if not throw IllegalAccessError,
1364 // returns Class* in rRET0
1365 // InitializeTypeAndVerifyAccess(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001366 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1367 type_idx, rARG1, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001368 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1369 } else {
1370 // Load dex cache entry into classReg (rARG2)
1371 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001372 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001373 int32_t offset_of_type =
1374 Array::DataOffset(sizeof(Class*)).Int32Value() +
1375 (sizeof(Class*) * type_idx);
1376 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1377 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001378 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001379 // Need to test presence of type in dex cache at runtime
1380 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1381 // Not resolved
1382 // Call out to helper, which will return resolved type in rARG0
1383 // InitializeTypeFromCode(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001384 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, rARG1,
1385 true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001386 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1387 // Rejoin code paths
1388 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1389 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001390 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001391 }
1392 // At this point, classReg (rARG2) has class
1393 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1394 /* Null is OK - continue */
1395 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1396 /* load object->klass_ */
1397 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1398 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1399 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001400#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001401 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
buzbee8320f382012-09-11 16:29:42 -07001402 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode), rARG1, rARG2, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001403#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001404 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1405 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1406 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1407 opRegCopy(cUnit, rARG0, rARG1);
1408 opRegCopy(cUnit, rARG1, rARG2);
1409 oatClobberCalleeSave(cUnit);
buzbee8320f382012-09-11 16:29:42 -07001410 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
1411 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001412 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001413#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001414 /* branch target here */
1415 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1416 branch1->target = target;
1417 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001418}
1419
buzbee31a4a6f2012-02-28 15:36:15 -08001420/*
1421 * Generate array store
1422 *
1423 */
buzbee408ad162012-06-06 16:45:18 -07001424void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001425 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001426{
Bill Buzbeea114add2012-05-03 15:00:40 -07001427 int lenOffset = Array::LengthOffset().Int32Value();
1428 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001429
Bill Buzbeea114add2012-05-03 15:00:40 -07001430 oatFlushAllRegs(cUnit); // Use explicit registers
1431 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001432
Bill Buzbeea114add2012-05-03 15:00:40 -07001433 int rValue = rARG0; // Register holding value
1434 int rArrayClass = rARG1; // Register holding array's Class
1435 int rArray = rARG2; // Register holding array
1436 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001437
Bill Buzbeea114add2012-05-03 15:00:40 -07001438 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1439 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1440 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001441
buzbee408ad162012-06-06 16:45:18 -07001442 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001443
Bill Buzbeea114add2012-05-03 15:00:40 -07001444 // Store of null?
1445 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001446
Bill Buzbeea114add2012-05-03 15:00:40 -07001447 // Get the array's class.
1448 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
buzbee8320f382012-09-11 16:29:42 -07001449 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode), rValue,
1450 rArrayClass, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001451 // Redo loadValues in case they didn't survive the call.
1452 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1453 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1454 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1455 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001456
Bill Buzbeea114add2012-05-03 15:00:40 -07001457 // Branch here if value to be stored == null
1458 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1459 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001460
Ian Rogersb41b33b2012-03-20 14:22:54 -07001461#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001462 // make an extra temp available for card mark below
1463 oatFreeTemp(cUnit, rARG1);
buzbee408ad162012-06-06 16:45:18 -07001464 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001465 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
buzbee408ad162012-06-06 16:45:18 -07001466 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001467 }
buzbee408ad162012-06-06 16:45:18 -07001468 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001469 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001470#else
buzbee408ad162012-06-06 16:45:18 -07001471 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001472 int regLen = INVALID_REG;
1473 if (needsRangeCheck) {
1474 regLen = rARG1;
buzbeef1f86362012-07-10 15:18:31 -07001475 loadWordDisp(cUnit, rArray, lenOffset, regLen); // Get len
Bill Buzbeea114add2012-05-03 15:00:40 -07001476 }
1477 /* rPtr -> array data */
1478 int rPtr = oatAllocTemp(cUnit);
1479 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1480 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001481 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001482 }
1483 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1484 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001485#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001486 oatFreeTemp(cUnit, rIndex);
1487 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001488}
1489
1490/*
1491 * Generate array load
1492 */
buzbee408ad162012-06-06 16:45:18 -07001493void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001494 RegLocation rlArray, RegLocation rlIndex,
1495 RegLocation rlDest, int scale)
1496{
Bill Buzbeea114add2012-05-03 15:00:40 -07001497 RegisterClass regClass = oatRegClassBySize(size);
1498 int lenOffset = Array::LengthOffset().Int32Value();
1499 int dataOffset;
1500 RegLocation rlResult;
1501 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1502 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001503
Bill Buzbeea114add2012-05-03 15:00:40 -07001504 if (size == kLong || size == kDouble) {
1505 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1506 } else {
1507 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1508 }
buzbee31a4a6f2012-02-28 15:36:15 -08001509
Bill Buzbeea114add2012-05-03 15:00:40 -07001510 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001511 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001512
Ian Rogersb5d09b22012-03-06 22:14:17 -08001513#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001514 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001515 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1516 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001517 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001518 }
1519 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001520 int regAddr = oatAllocTemp(cUnit);
1521 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1522 oatFreeTemp(cUnit, rlArray.lowReg);
1523 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001524 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001525 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
jeffhao21e12712012-05-25 19:06:18 -07001526 rlResult.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001527 storeValueWide(cUnit, rlDest, rlResult);
1528 } else {
1529 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001530
buzbee408ad162012-06-06 16:45:18 -07001531 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001532 dataOffset, rlResult.lowReg, INVALID_REG, size,
1533 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001534
Bill Buzbeea114add2012-05-03 15:00:40 -07001535 storeValue(cUnit, rlDest, rlResult);
1536 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001537#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001538 int regPtr = oatAllocTemp(cUnit);
buzbee408ad162012-06-06 16:45:18 -07001539 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001540 int regLen = INVALID_REG;
1541 if (needsRangeCheck) {
1542 regLen = oatAllocTemp(cUnit);
1543 /* Get len */
1544 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1545 }
1546 /* regPtr -> array data */
1547 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1548 oatFreeTemp(cUnit, rlArray.lowReg);
1549 if ((size == kLong) || (size == kDouble)) {
1550 if (scale) {
1551 int rNewIndex = oatAllocTemp(cUnit);
1552 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1553 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1554 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001555 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001556 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001557 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001558 oatFreeTemp(cUnit, rlIndex.lowReg);
1559 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1560
1561 if (needsRangeCheck) {
1562 // TODO: change kCondCS to a more meaningful name, is the sense of
1563 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001564 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001565 oatFreeTemp(cUnit, regLen);
1566 }
1567 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1568
1569 oatFreeTemp(cUnit, regPtr);
1570 storeValueWide(cUnit, rlDest, rlResult);
1571 } else {
1572 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1573
1574 if (needsRangeCheck) {
1575 // TODO: change kCondCS to a more meaningful name, is the sense of
1576 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001577 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001578 oatFreeTemp(cUnit, regLen);
1579 }
1580 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1581 scale, size);
1582
1583 oatFreeTemp(cUnit, regPtr);
1584 storeValue(cUnit, rlDest, rlResult);
1585 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001586#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001587}
1588
1589/*
1590 * Generate array store
1591 *
1592 */
buzbee408ad162012-06-06 16:45:18 -07001593void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001594 RegLocation rlArray, RegLocation rlIndex,
1595 RegLocation rlSrc, int scale)
1596{
Bill Buzbeea114add2012-05-03 15:00:40 -07001597 RegisterClass regClass = oatRegClassBySize(size);
1598 int lenOffset = Array::LengthOffset().Int32Value();
1599 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001600
Bill Buzbeea114add2012-05-03 15:00:40 -07001601 if (size == kLong || size == kDouble) {
1602 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1603 } else {
1604 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1605 }
buzbee31a4a6f2012-02-28 15:36:15 -08001606
Bill Buzbeea114add2012-05-03 15:00:40 -07001607 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1608 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001609#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001610 int regPtr;
1611 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1612 oatClobber(cUnit, rlArray.lowReg);
1613 regPtr = rlArray.lowReg;
1614 } else {
1615 regPtr = oatAllocTemp(cUnit);
1616 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1617 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001618#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001619
Bill Buzbeea114add2012-05-03 15:00:40 -07001620 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001621 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001622
Ian Rogersb41b33b2012-03-20 14:22:54 -07001623#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001624 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001625 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1626 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001627 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001628 }
1629 if ((size == kLong) || (size == kDouble)) {
1630 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1631 } else {
1632 rlSrc = loadValue(cUnit, rlSrc, regClass);
1633 }
jeffhao703f2cd2012-07-13 17:25:52 -07001634 // If the src reg can't be byte accessed, move it to a temp first.
1635 if ((size == kSignedByte || size == kUnsignedByte) && rlSrc.lowReg >= 4) {
1636 int temp = oatAllocTemp(cUnit);
1637 opRegCopy(cUnit, temp, rlSrc.lowReg);
1638 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1639 dataOffset, temp, INVALID_REG, size,
1640 INVALID_SREG);
1641 } else {
1642 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1643 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1644 INVALID_SREG);
1645 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001646#else
buzbee408ad162012-06-06 16:45:18 -07001647 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001648 int regLen = INVALID_REG;
1649 if (needsRangeCheck) {
1650 regLen = oatAllocTemp(cUnit);
1651 //NOTE: max live temps(4) here.
1652 /* Get len */
1653 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1654 }
1655 /* regPtr -> array data */
1656 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1657 /* at this point, regPtr points to array, 2 live temps */
1658 if ((size == kLong) || (size == kDouble)) {
1659 //TUNING: specific wide routine that can handle fp regs
1660 if (scale) {
1661 int rNewIndex = oatAllocTemp(cUnit);
1662 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1663 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1664 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001665 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001666 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001667 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001668 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1669
1670 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001671 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001672 oatFreeTemp(cUnit, regLen);
1673 }
1674
jeffhao41005dd2012-05-09 17:58:52 -07001675 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001676
1677 oatFreeTemp(cUnit, regPtr);
1678 } else {
1679 rlSrc = loadValue(cUnit, rlSrc, regClass);
1680 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001681 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001682 oatFreeTemp(cUnit, regLen);
1683 }
1684 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1685 scale, size);
1686 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001687#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001688}
1689
buzbee408ad162012-06-06 16:45:18 -07001690void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001691 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001692 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001693{
Bill Buzbeea114add2012-05-03 15:00:40 -07001694 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001695#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001696 /*
1697 * NOTE: This is the one place in the code in which we might have
1698 * as many as six live temporary registers. There are 5 in the normal
1699 * set for Arm. Until we have spill capabilities, temporarily add
1700 * lr to the temp set. It is safe to do this locally, but note that
1701 * lr is used explicitly elsewhere in the code generator and cannot
1702 * normally be used as a general temp register.
1703 */
1704 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1705 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001706#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001707 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1708 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1709 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1710 // The longs may overlap - use intermediate temp if so
1711 if (rlResult.lowReg == rlSrc1.highReg) {
1712 int tReg = oatAllocTemp(cUnit);
1713 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1714 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1715 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1716 oatFreeTemp(cUnit, tReg);
1717 } else {
1718 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1719 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1720 rlSrc2.highReg);
1721 }
1722 /*
1723 * NOTE: If rlDest refers to a frame variable in a large frame, the
1724 * following storeValueWide might need to allocate a temp register.
1725 * To further work around the lack of a spill capability, explicitly
1726 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1727 * Remove when spill is functional.
1728 */
1729 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1730 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1731 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001732#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001733 oatClobber(cUnit, rLR);
1734 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001735#endif
1736}
1737
1738
buzbee408ad162012-06-06 16:45:18 -07001739bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001740 RegLocation rlSrc1, RegLocation rlShift)
1741{
Bill Buzbeea114add2012-05-03 15:00:40 -07001742 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001743
buzbee408ad162012-06-06 16:45:18 -07001744 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001745 case Instruction::SHL_LONG:
1746 case Instruction::SHL_LONG_2ADDR:
1747 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1748 break;
1749 case Instruction::SHR_LONG:
1750 case Instruction::SHR_LONG_2ADDR:
1751 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1752 break;
1753 case Instruction::USHR_LONG:
1754 case Instruction::USHR_LONG_2ADDR:
1755 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1756 break;
1757 default:
1758 LOG(FATAL) << "Unexpected case";
1759 return true;
1760 }
1761 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07001762 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001763 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1764 storeValueWide(cUnit, rlDest, rlResult);
1765 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001766}
1767
1768
buzbee408ad162012-06-06 16:45:18 -07001769bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001770 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001771{
Bill Buzbeea114add2012-05-03 15:00:40 -07001772 OpKind op = kOpBkpt;
1773 bool callOut = false;
1774 bool checkZero = false;
1775 bool unary = false;
1776 RegLocation rlResult;
1777 bool shiftOp = false;
1778 int funcOffset;
1779 int retReg = rRET0;
buzbee408ad162012-06-06 16:45:18 -07001780 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001781 case Instruction::NEG_INT:
1782 op = kOpNeg;
1783 unary = true;
1784 break;
1785 case Instruction::NOT_INT:
1786 op = kOpMvn;
1787 unary = true;
1788 break;
1789 case Instruction::ADD_INT:
1790 case Instruction::ADD_INT_2ADDR:
1791 op = kOpAdd;
1792 break;
1793 case Instruction::SUB_INT:
1794 case Instruction::SUB_INT_2ADDR:
1795 op = kOpSub;
1796 break;
1797 case Instruction::MUL_INT:
1798 case Instruction::MUL_INT_2ADDR:
1799 op = kOpMul;
1800 break;
1801 case Instruction::DIV_INT:
1802 case Instruction::DIV_INT_2ADDR:
1803 checkZero = true;
1804 op = kOpDiv;
1805 callOut = true;
1806 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1807 retReg = rRET0;
1808 break;
1809 /* NOTE: returns in rARG1 */
1810 case Instruction::REM_INT:
1811 case Instruction::REM_INT_2ADDR:
1812 checkZero = true;
1813 op = kOpRem;
1814 callOut = true;
1815 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1816 retReg = rRET1;
1817 break;
1818 case Instruction::AND_INT:
1819 case Instruction::AND_INT_2ADDR:
1820 op = kOpAnd;
1821 break;
1822 case Instruction::OR_INT:
1823 case Instruction::OR_INT_2ADDR:
1824 op = kOpOr;
1825 break;
1826 case Instruction::XOR_INT:
1827 case Instruction::XOR_INT_2ADDR:
1828 op = kOpXor;
1829 break;
1830 case Instruction::SHL_INT:
1831 case Instruction::SHL_INT_2ADDR:
1832 shiftOp = true;
1833 op = kOpLsl;
1834 break;
1835 case Instruction::SHR_INT:
1836 case Instruction::SHR_INT_2ADDR:
1837 shiftOp = true;
1838 op = kOpAsr;
1839 break;
1840 case Instruction::USHR_INT:
1841 case Instruction::USHR_INT_2ADDR:
1842 shiftOp = true;
1843 op = kOpLsr;
1844 break;
1845 default:
1846 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001847 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001848 }
1849 if (!callOut) {
1850 if (unary) {
1851 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1852 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1853 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001854 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001855 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001856#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001857 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1858 int tReg = oatAllocTemp(cUnit);
1859 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001860#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001861 // X86 doesn't require masking and must use ECX
1862 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1863 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001864#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001865 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1866 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1867 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1868 oatFreeTemp(cUnit, tReg);
1869 } else {
1870 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1871 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1872 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1873 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1874 }
buzbee31a4a6f2012-02-28 15:36:15 -08001875 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001876 storeValue(cUnit, rlDest, rlResult);
1877 } else {
1878 RegLocation rlResult;
1879 oatFlushAllRegs(cUnit); /* Send everything to home location */
1880 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1881#if !defined(TARGET_X86)
1882 int rTgt = loadHelper(cUnit, funcOffset);
1883#endif
1884 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1885 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001886 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001887 }
buzbee8320f382012-09-11 16:29:42 -07001888 // NOTE: callout here is not a safepoint
Bill Buzbeea114add2012-05-03 15:00:40 -07001889#if !defined(TARGET_X86)
1890 opReg(cUnit, kOpBlx, rTgt);
1891 oatFreeTemp(cUnit, rTgt);
1892#else
1893 opThreadMem(cUnit, kOpBlx, funcOffset);
1894#endif
1895 if (retReg == rRET0)
1896 rlResult = oatGetReturn(cUnit, false);
1897 else
1898 rlResult = oatGetReturnAlt(cUnit);
1899 storeValue(cUnit, rlDest, rlResult);
1900 }
1901 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001902}
1903
1904/*
1905 * The following are the first-level codegen routines that analyze the format
1906 * of each bytecode then either dispatch special purpose codegen routines
1907 * or produce corresponding Thumb instructions directly.
1908 */
1909
1910bool isPowerOfTwo(int x)
1911{
Bill Buzbeea114add2012-05-03 15:00:40 -07001912 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001913}
1914
1915// Returns true if no more than two bits are set in 'x'.
1916bool isPopCountLE2(unsigned int x)
1917{
Bill Buzbeea114add2012-05-03 15:00:40 -07001918 x &= x - 1;
1919 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001920}
1921
1922// Returns the index of the lowest set bit in 'x'.
1923int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001924 int bit_posn = 0;
1925 while ((x & 0xf) == 0) {
1926 bit_posn += 4;
1927 x >>= 4;
1928 }
1929 while ((x & 1) == 0) {
1930 bit_posn++;
1931 x >>= 1;
1932 }
1933 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001934}
1935
1936// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1937// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001938bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001939 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001940{
buzbeef3aac972012-04-11 16:33:36 -07001941#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001942 // No divide instruction for Arm, so check for more special cases
1943 if (lit < 2) {
1944 return false;
1945 }
1946 if (!isPowerOfTwo(lit)) {
1947 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1948 }
buzbeef3aac972012-04-11 16:33:36 -07001949#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001950 if (lit < 2 || !isPowerOfTwo(lit)) {
1951 return false;
1952 }
buzbeef3aac972012-04-11 16:33:36 -07001953#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001954 int k = lowestSetBit(lit);
1955 if (k >= 30) {
1956 // Avoid special cases.
1957 return false;
1958 }
1959 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1960 dalvikOpcode == Instruction::DIV_INT_LIT16);
1961 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1962 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1963 if (div) {
1964 int tReg = oatAllocTemp(cUnit);
1965 if (lit == 2) {
1966 // Division by 2 is by far the most common division by constant.
1967 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1968 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1969 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001970 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001971 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1972 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1973 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1974 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001975 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001976 } else {
1977 int tReg1 = oatAllocTemp(cUnit);
1978 int tReg2 = oatAllocTemp(cUnit);
1979 if (lit == 2) {
1980 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1981 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1982 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1983 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1984 } else {
1985 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1986 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1987 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1988 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1989 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1990 }
1991 }
1992 storeValue(cUnit, rlDest, rlResult);
1993 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001994}
1995
1996void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1997 RegLocation rlResult, int lit,
1998 int firstBit, int secondBit)
1999{
buzbee0398c422012-03-02 15:22:47 -08002000#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002001 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
2002 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08002003#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002004 int tReg = oatAllocTemp(cUnit);
2005 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
2006 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
2007 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08002008#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002009 if (firstBit != 0) {
2010 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
2011 }
buzbee31a4a6f2012-02-28 15:36:15 -08002012}
2013
2014// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2015// and store the result in 'rlDest'.
2016bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
2017 RegLocation rlDest, int lit)
2018{
Bill Buzbeea114add2012-05-03 15:00:40 -07002019 // Can we simplify this multiplication?
2020 bool powerOfTwo = false;
2021 bool popCountLE2 = false;
2022 bool powerOfTwoMinusOne = false;
2023 if (lit < 2) {
2024 // Avoid special cases.
2025 return false;
2026 } else if (isPowerOfTwo(lit)) {
2027 powerOfTwo = true;
2028 } else if (isPopCountLE2(lit)) {
2029 popCountLE2 = true;
2030 } else if (isPowerOfTwo(lit + 1)) {
2031 powerOfTwoMinusOne = true;
2032 } else {
2033 return false;
2034 }
2035 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2036 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2037 if (powerOfTwo) {
2038 // Shift.
2039 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2040 lowestSetBit(lit));
2041 } else if (popCountLE2) {
2042 // Shift and add and shift.
2043 int firstBit = lowestSetBit(lit);
2044 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2045 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2046 firstBit, secondBit);
2047 } else {
2048 // Reverse subtract: (src << (shift + 1)) - src.
2049 DCHECK(powerOfTwoMinusOne);
2050 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2051 int tReg = oatAllocTemp(cUnit);
2052 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2053 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2054 }
2055 storeValue(cUnit, rlDest, rlResult);
2056 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002057}
2058
buzbee408ad162012-06-06 16:45:18 -07002059bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2060 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002061{
Bill Buzbeea114add2012-05-03 15:00:40 -07002062 RegLocation rlResult;
2063 OpKind op = (OpKind)0; /* Make gcc happy */
2064 int shiftOp = false;
2065 bool isDiv = false;
2066 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002067
buzbee408ad162012-06-06 16:45:18 -07002068 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002069 case Instruction::RSUB_INT_LIT8:
2070 case Instruction::RSUB_INT: {
2071 int tReg;
2072 //TUNING: add support for use of Arm rsub op
2073 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2074 tReg = oatAllocTemp(cUnit);
2075 loadConstant(cUnit, tReg, lit);
2076 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2077 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2078 storeValue(cUnit, rlDest, rlResult);
2079 return false;
2080 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002081 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002082
2083 case Instruction::ADD_INT_LIT8:
2084 case Instruction::ADD_INT_LIT16:
2085 op = kOpAdd;
2086 break;
2087 case Instruction::MUL_INT_LIT8:
2088 case Instruction::MUL_INT_LIT16: {
2089 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2090 return false;
2091 }
2092 op = kOpMul;
2093 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002094 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002095 case Instruction::AND_INT_LIT8:
2096 case Instruction::AND_INT_LIT16:
2097 op = kOpAnd;
2098 break;
2099 case Instruction::OR_INT_LIT8:
2100 case Instruction::OR_INT_LIT16:
2101 op = kOpOr;
2102 break;
2103 case Instruction::XOR_INT_LIT8:
2104 case Instruction::XOR_INT_LIT16:
2105 op = kOpXor;
2106 break;
2107 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002108 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002109 lit &= 31;
2110 shiftOp = true;
2111 op = kOpLsl;
2112 break;
2113 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002114 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002115 lit &= 31;
2116 shiftOp = true;
2117 op = kOpAsr;
2118 break;
2119 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002120 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002121 lit &= 31;
2122 shiftOp = true;
2123 op = kOpLsr;
2124 break;
2125
2126 case Instruction::DIV_INT_LIT8:
2127 case Instruction::DIV_INT_LIT16:
2128 case Instruction::REM_INT_LIT8:
2129 case Instruction::REM_INT_LIT16:
2130 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002131 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002132 return false;
2133 }
buzbee408ad162012-06-06 16:45:18 -07002134 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002135 return false;
2136 }
2137 oatFlushAllRegs(cUnit); /* Everything to home location */
2138 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2139 oatClobber(cUnit, rARG0);
2140 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee408ad162012-06-06 16:45:18 -07002141 if ((opcode == Instruction::DIV_INT_LIT8) ||
2142 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002143 isDiv = true;
2144 } else {
2145 isDiv = false;
2146 }
buzbee8320f382012-09-11 16:29:42 -07002147 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002148 if (isDiv)
2149 rlResult = oatGetReturn(cUnit, false);
2150 else
2151 rlResult = oatGetReturnAlt(cUnit);
2152 storeValue(cUnit, rlDest, rlResult);
2153 return false;
2154 break;
2155 default:
2156 return true;
2157 }
2158 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2159 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2160 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2161 if (shiftOp && (lit == 0)) {
2162 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2163 } else {
2164 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2165 }
2166 storeValue(cUnit, rlDest, rlResult);
2167 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002168}
2169
buzbee408ad162012-06-06 16:45:18 -07002170bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002171 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002172{
Bill Buzbeea114add2012-05-03 15:00:40 -07002173 RegLocation rlResult;
2174 OpKind firstOp = kOpBkpt;
2175 OpKind secondOp = kOpBkpt;
2176 bool callOut = false;
2177 bool checkZero = false;
2178 int funcOffset;
2179 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002180
buzbee408ad162012-06-06 16:45:18 -07002181 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002182 case Instruction::NOT_LONG:
2183 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2184 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2185 // Check for destructive overlap
2186 if (rlResult.lowReg == rlSrc2.highReg) {
2187 int tReg = oatAllocTemp(cUnit);
2188 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2189 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2190 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2191 oatFreeTemp(cUnit, tReg);
2192 } else {
2193 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2194 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2195 }
2196 storeValueWide(cUnit, rlDest, rlResult);
2197 return false;
2198 break;
2199 case Instruction::ADD_LONG:
2200 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002201#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002202 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002203#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002204 firstOp = kOpAdd;
2205 secondOp = kOpAdc;
2206 break;
buzbeec5159d52012-03-03 11:48:39 -08002207#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002208 case Instruction::SUB_LONG:
2209 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002210#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002211 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002212#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002213 firstOp = kOpSub;
2214 secondOp = kOpSbc;
2215 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002216#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002217 case Instruction::MUL_LONG:
2218 case Instruction::MUL_LONG_2ADDR:
2219 callOut = true;
2220 retReg = rRET0;
2221 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2222 break;
2223 case Instruction::DIV_LONG:
2224 case Instruction::DIV_LONG_2ADDR:
2225 callOut = true;
2226 checkZero = true;
2227 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002228 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002229 break;
2230 case Instruction::REM_LONG:
2231 case Instruction::REM_LONG_2ADDR:
2232 callOut = true;
2233 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002234 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002235#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002236 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2237 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002238#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002239 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002240#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002241 break;
2242 case Instruction::AND_LONG_2ADDR:
2243 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002244#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002245 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002246#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002247 firstOp = kOpAnd;
2248 secondOp = kOpAnd;
2249 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002250#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002251 case Instruction::OR_LONG:
2252 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002253#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002254 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002255#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002256 firstOp = kOpOr;
2257 secondOp = kOpOr;
2258 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002259#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002260 case Instruction::XOR_LONG:
2261 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002262#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002263 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002264#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002265 firstOp = kOpXor;
2266 secondOp = kOpXor;
2267 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002268#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002269 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002270 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002271 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002272 default:
2273 LOG(FATAL) << "Invalid long arith op";
2274 }
2275 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002276 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002277 } else {
2278 oatFlushAllRegs(cUnit); /* Send everything to home location */
2279 if (checkZero) {
2280 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2281#if !defined(TARGET_X86)
2282 int rTgt = loadHelper(cUnit, funcOffset);
2283#endif
2284 int tReg = oatAllocTemp(cUnit);
2285#if defined(TARGET_ARM)
2286 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2287 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002288 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002289#else
2290 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2291#endif
buzbee408ad162012-06-06 16:45:18 -07002292 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002293 oatFreeTemp(cUnit, tReg);
2294 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
buzbee8320f382012-09-11 16:29:42 -07002295 // NOTE: callout here is not a safepoint
Bill Buzbeea114add2012-05-03 15:00:40 -07002296#if !defined(TARGET_X86)
2297 opReg(cUnit, kOpBlx, rTgt);
2298 oatFreeTemp(cUnit, rTgt);
2299#else
2300 opThreadMem(cUnit, kOpBlx, funcOffset);
2301#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002302 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002303 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
buzbee8320f382012-09-11 16:29:42 -07002304 rlSrc1, rlSrc2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002305 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002306 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2307 if (retReg == rRET0)
2308 rlResult = oatGetReturnWide(cUnit, false);
2309 else
2310 rlResult = oatGetReturnWideAlt(cUnit);
2311 storeValueWide(cUnit, rlDest, rlResult);
2312 }
2313 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002314}
2315
buzbee408ad162012-06-06 16:45:18 -07002316bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2317 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002318{
Bill Buzbeea114add2012-05-03 15:00:40 -07002319 /*
2320 * Don't optimize the register usage since it calls out to support
2321 * functions
2322 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002323 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002324 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002325 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002326 } else {
2327 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002328 }
buzbee8320f382012-09-11 16:29:42 -07002329 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc, false);
buzbee408ad162012-06-06 16:45:18 -07002330 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002331 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002332 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2333 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002334 } else {
2335 RegLocation rlResult;
2336 rlResult = oatGetReturn(cUnit, rlDest.fp);
2337 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002338 }
2339 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002340}
2341
2342void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002343bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002344 RegLocation rlDest, RegLocation rlSrc1,
2345 RegLocation rlSrc2)
2346{
Bill Buzbeea114add2012-05-03 15:00:40 -07002347 RegLocation rlResult;
2348 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002349
buzbee408ad162012-06-06 16:45:18 -07002350 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002351 case Instruction::ADD_FLOAT_2ADDR:
2352 case Instruction::ADD_FLOAT:
2353 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2354 break;
2355 case Instruction::SUB_FLOAT_2ADDR:
2356 case Instruction::SUB_FLOAT:
2357 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2358 break;
2359 case Instruction::DIV_FLOAT_2ADDR:
2360 case Instruction::DIV_FLOAT:
2361 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2362 break;
2363 case Instruction::MUL_FLOAT_2ADDR:
2364 case Instruction::MUL_FLOAT:
2365 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2366 break;
2367 case Instruction::REM_FLOAT_2ADDR:
2368 case Instruction::REM_FLOAT:
2369 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2370 break;
2371 case Instruction::NEG_FLOAT: {
2372 genNegFloat(cUnit, rlDest, rlSrc1);
2373 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002374 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002375 default:
2376 return true;
2377 }
2378 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002379 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002380 rlResult = oatGetReturn(cUnit, true);
2381 storeValue(cUnit, rlDest, rlResult);
2382 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002383}
2384
2385void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002386bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002387 RegLocation rlDest, RegLocation rlSrc1,
2388 RegLocation rlSrc2)
2389{
Bill Buzbeea114add2012-05-03 15:00:40 -07002390 RegLocation rlResult;
2391 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002392
buzbee408ad162012-06-06 16:45:18 -07002393 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002394 case Instruction::ADD_DOUBLE_2ADDR:
2395 case Instruction::ADD_DOUBLE:
2396 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2397 break;
2398 case Instruction::SUB_DOUBLE_2ADDR:
2399 case Instruction::SUB_DOUBLE:
2400 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2401 break;
2402 case Instruction::DIV_DOUBLE_2ADDR:
2403 case Instruction::DIV_DOUBLE:
2404 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2405 break;
2406 case Instruction::MUL_DOUBLE_2ADDR:
2407 case Instruction::MUL_DOUBLE:
2408 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2409 break;
2410 case Instruction::REM_DOUBLE_2ADDR:
2411 case Instruction::REM_DOUBLE:
2412 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2413 break;
2414 case Instruction::NEG_DOUBLE: {
2415 genNegDouble(cUnit, rlDest, rlSrc1);
2416 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002417 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002418 default:
2419 return true;
2420 }
2421 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002422 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002423 rlResult = oatGetReturnWide(cUnit, true);
2424 storeValueWide(cUnit, rlDest, rlResult);
2425 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002426}
2427
buzbee408ad162012-06-06 16:45:18 -07002428bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2429 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002430{
buzbee31a4a6f2012-02-28 15:36:15 -08002431
Bill Buzbeea114add2012-05-03 15:00:40 -07002432 switch (opcode) {
2433 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002434 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2435 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002436 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002437 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2438 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002439 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002440 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2441 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002442 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002443 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2444 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002445 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002446 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2447 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002448 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002449 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2450 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002451 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002452 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2453 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002454 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002455 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2456 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002457 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002458 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2459 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002460 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002461 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2462 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002463 default:
2464 return true;
2465 }
2466 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002467}
2468
2469/*
2470 * Generate callout to updateDebugger. Note that we're overloading
2471 * the use of rSUSPEND here. When the debugger is active, this
2472 * register holds the address of the update function. So, if it's
2473 * non-null, we call out to it.
2474 *
2475 * Note also that rRET0 and rRET1 must be preserved across this
2476 * code. This must be handled by the stub.
2477 */
2478void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2479{
Bill Buzbeea114add2012-05-03 15:00:40 -07002480 // Following DCHECK verifies that dPC is in range of single load immediate
2481 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2482 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2483 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002484#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002485 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2486 opIT(cUnit, kArmCondNe, "T");
2487 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
buzbee8320f382012-09-11 16:29:42 -07002488 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2489 markSafepointPC(cUnit, callInst);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002490#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002491 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002492#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002493 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2494 loadConstant(cUnit, rARG2, offset);
buzbee8320f382012-09-11 16:29:42 -07002495 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2496 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002497 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2498 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002499#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002500 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002501}
2502
2503/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002504void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002505{
buzbee408ad162012-06-06 16:45:18 -07002506 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002507 return;
2508 }
2509 oatFlushAllRegs(cUnit);
2510 if (cUnit->genDebugger) {
2511 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002512#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002513 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002514#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002515 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
buzbee8320f382012-09-11 16:29:42 -07002516 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
2517 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002518 // Refresh rSUSPEND
2519 loadWordDisp(cUnit, rSELF,
2520 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2521 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002522#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002523 } else {
2524 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002525#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002526 // In non-debug case, only check periodically
2527 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2528 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002529#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002530 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2531 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002532#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002533 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2534 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002535#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002536 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2537 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002538 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002539 branch->target = (LIR*)target;
2540 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2541 }
buzbee31a4a6f2012-02-28 15:36:15 -08002542}
2543
buzbeefead2932012-03-30 14:02:01 -07002544/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002545void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002546{
buzbee408ad162012-06-06 16:45:18 -07002547 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002548 opUnconditionalBranch(cUnit, target);
2549 return;
2550 }
2551 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002552 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002553 opUnconditionalBranch(cUnit, target);
2554 } else {
buzbeefead2932012-03-30 14:02:01 -07002555#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002556 // In non-debug case, only check periodically
2557 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2558 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002559#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002560 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2561 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002562#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002563 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2564 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002565#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002566 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002567 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002568 oatFlushAllRegs(cUnit);
2569 opUnconditionalBranch(cUnit, launchPad);
2570 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2571 (intptr_t)launchPad);
2572 }
buzbeefead2932012-03-30 14:02:01 -07002573}
2574
buzbee31a4a6f2012-02-28 15:36:15 -08002575} // namespace art