blob: 6868d0bc9a27914990572bf3c70bdee9eee5b7f6 [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
Ian Rogers474b6da2012-09-25 00:20:38 -07001263void genMoveException(CompilationUnit* cUnit, RegLocation rlDest)
1264{
1265 oatFlushAllRegs(cUnit); /* Everything to home location */
1266 int funcOffset = ENTRYPOINT_OFFSET(pGetAndClearException);
1267#if defined(TARGET_X86)
1268 // Runtime helper will load argument for x86.
1269 callRuntimeHelperReg(cUnit, funcOffset, rARG0, false);
1270#else
1271 callRuntimeHelperReg(cUnit, funcOffset, rSELF, false);
1272#endif
1273 RegLocation rlResult = oatGetReturn(cUnit, false);
1274 storeValue(cUnit, rlDest, rlResult);
1275}
1276
buzbee408ad162012-06-06 16:45:18 -07001277void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001278{
Bill Buzbeea114add2012-05-03 15:00:40 -07001279 oatFlushAllRegs(cUnit);
buzbee8320f382012-09-11 16:29:42 -07001280 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException), rlSrc, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001281}
1282
buzbee408ad162012-06-06 16:45:18 -07001283void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001284 RegLocation rlSrc)
1285{
Bill Buzbeea114add2012-05-03 15:00:40 -07001286 oatFlushAllRegs(cUnit);
1287 // May generate a call - use explicit registers
1288 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001289 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1290 int classReg = rARG2; // rARG2 will hold the Class*
1291 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001292 *cUnit->dex_file,
1293 type_idx)) {
1294 // Check we have access to type_idx and if not throw IllegalAccessError,
1295 // returns Class* in rARG0
buzbee8320f382012-09-11 16:29:42 -07001296 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1297 type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001298 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1299 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1300 } else {
1301 // Load dex cache entry into classReg (rARG2)
1302 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1303 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001304 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001305 int32_t offset_of_type =
1306 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1307 * type_idx);
1308 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1309 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001311 // Need to test presence of type in dex cache at runtime
1312 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1313 // Not resolved
1314 // Call out to helper, which will return resolved type in rRET0
buzbee8320f382012-09-11 16:29:42 -07001315 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001316 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1317 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1318 // Rejoin code paths
1319 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1320 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001321 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001322 }
1323 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
1324 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1325 /* load object->klass_ */
1326 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1327 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1328 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee8320f382012-09-11 16:29:42 -07001329 LIR* callInst;
buzbee0398c422012-03-02 15:22:47 -08001330#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001331 /* Uses conditional nullification */
1332 int rTgt = loadHelper(cUnit,
1333 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1334 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1335 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1336 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1337 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee8320f382012-09-11 16:29:42 -07001338 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Bill Buzbeea114add2012-05-03 15:00:40 -07001339 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001340#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001341 /* Uses branchovers */
1342 loadConstant(cUnit, rARG0, 1); // assume true
1343 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001344#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001345 int rTgt = loadHelper(cUnit,
1346 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1347 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
buzbee8320f382012-09-11 16:29:42 -07001348 callInst = opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
Bill Buzbeea114add2012-05-03 15:00:40 -07001349 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001350#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001351 opRegCopy(cUnit, rARG0, rARG2);
buzbee8320f382012-09-11 16:29:42 -07001352 callInst = opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001353#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001354#endif
buzbee8320f382012-09-11 16:29:42 -07001355 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001356 oatClobberCalleeSave(cUnit);
1357 /* branch targets here */
1358 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1359 RegLocation rlResult = oatGetReturn(cUnit, false);
1360 storeValue(cUnit, rlDest, rlResult);
1361 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001362#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001363 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001364#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001365}
1366
buzbee408ad162012-06-06 16:45:18 -07001367void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001368{
Bill Buzbeea114add2012-05-03 15:00:40 -07001369 oatFlushAllRegs(cUnit);
1370 // May generate a call - use explicit registers
1371 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001372 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1373 int classReg = rARG2; // rARG2 will hold the Class*
1374 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001375 *cUnit->dex_file,
1376 type_idx)) {
1377 // Check we have access to type_idx and if not throw IllegalAccessError,
1378 // returns Class* in rRET0
1379 // InitializeTypeAndVerifyAccess(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001380 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1381 type_idx, rARG1, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001382 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1383 } else {
1384 // Load dex cache entry into classReg (rARG2)
1385 loadWordDisp(cUnit, rARG1,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001386 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), classReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001387 int32_t offset_of_type =
1388 Array::DataOffset(sizeof(Class*)).Int32Value() +
1389 (sizeof(Class*) * type_idx);
1390 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1391 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001392 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001393 // Need to test presence of type in dex cache at runtime
1394 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1395 // Not resolved
1396 // Call out to helper, which will return resolved type in rARG0
1397 // InitializeTypeFromCode(idx, method)
buzbee8320f382012-09-11 16:29:42 -07001398 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, rARG1,
1399 true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001400 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1401 // Rejoin code paths
1402 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1403 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001404 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001405 }
1406 // At this point, classReg (rARG2) has class
1407 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1408 /* Null is OK - continue */
1409 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1410 /* load object->klass_ */
1411 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1412 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1413 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001414#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001415 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
buzbee8320f382012-09-11 16:29:42 -07001416 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode), rARG1, rARG2, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001417#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001418 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1419 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1420 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1421 opRegCopy(cUnit, rARG0, rARG1);
1422 opRegCopy(cUnit, rARG1, rARG2);
1423 oatClobberCalleeSave(cUnit);
buzbee8320f382012-09-11 16:29:42 -07001424 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
1425 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07001426 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001427#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001428 /* branch target here */
1429 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1430 branch1->target = target;
1431 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001432}
1433
buzbee31a4a6f2012-02-28 15:36:15 -08001434/*
1435 * Generate array store
1436 *
1437 */
buzbee408ad162012-06-06 16:45:18 -07001438void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001439 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001440{
Bill Buzbeea114add2012-05-03 15:00:40 -07001441 int lenOffset = Array::LengthOffset().Int32Value();
1442 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001443
Bill Buzbeea114add2012-05-03 15:00:40 -07001444 oatFlushAllRegs(cUnit); // Use explicit registers
1445 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001446
Bill Buzbeea114add2012-05-03 15:00:40 -07001447 int rValue = rARG0; // Register holding value
1448 int rArrayClass = rARG1; // Register holding array's Class
1449 int rArray = rARG2; // Register holding array
1450 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001451
Bill Buzbeea114add2012-05-03 15:00:40 -07001452 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1453 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1454 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001455
buzbee408ad162012-06-06 16:45:18 -07001456 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001457
Bill Buzbeea114add2012-05-03 15:00:40 -07001458 // Store of null?
1459 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001460
Bill Buzbeea114add2012-05-03 15:00:40 -07001461 // Get the array's class.
1462 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
buzbee8320f382012-09-11 16:29:42 -07001463 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode), rValue,
1464 rArrayClass, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001465 // Redo loadValues in case they didn't survive the call.
1466 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1467 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1468 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1469 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001470
Bill Buzbeea114add2012-05-03 15:00:40 -07001471 // Branch here if value to be stored == null
1472 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1473 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001474
Ian Rogersb41b33b2012-03-20 14:22:54 -07001475#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001476 // make an extra temp available for card mark below
1477 oatFreeTemp(cUnit, rARG1);
buzbee408ad162012-06-06 16:45:18 -07001478 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001479 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
buzbee408ad162012-06-06 16:45:18 -07001480 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001481 }
buzbee408ad162012-06-06 16:45:18 -07001482 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001483 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001484#else
buzbee408ad162012-06-06 16:45:18 -07001485 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001486 int regLen = INVALID_REG;
1487 if (needsRangeCheck) {
1488 regLen = rARG1;
buzbeef1f86362012-07-10 15:18:31 -07001489 loadWordDisp(cUnit, rArray, lenOffset, regLen); // Get len
Bill Buzbeea114add2012-05-03 15:00:40 -07001490 }
1491 /* rPtr -> array data */
1492 int rPtr = oatAllocTemp(cUnit);
1493 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1494 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001495 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001496 }
1497 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1498 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001499#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001500 oatFreeTemp(cUnit, rIndex);
1501 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001502}
1503
1504/*
1505 * Generate array load
1506 */
buzbee408ad162012-06-06 16:45:18 -07001507void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001508 RegLocation rlArray, RegLocation rlIndex,
1509 RegLocation rlDest, int scale)
1510{
Bill Buzbeea114add2012-05-03 15:00:40 -07001511 RegisterClass regClass = oatRegClassBySize(size);
1512 int lenOffset = Array::LengthOffset().Int32Value();
1513 int dataOffset;
1514 RegLocation rlResult;
1515 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1516 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001517
Bill Buzbeea114add2012-05-03 15:00:40 -07001518 if (size == kLong || size == kDouble) {
1519 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1520 } else {
1521 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1522 }
buzbee31a4a6f2012-02-28 15:36:15 -08001523
Bill Buzbeea114add2012-05-03 15:00:40 -07001524 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001525 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001526
Ian Rogersb5d09b22012-03-06 22:14:17 -08001527#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001528 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001529 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1530 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001531 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001532 }
1533 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001534 int regAddr = oatAllocTemp(cUnit);
1535 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1536 oatFreeTemp(cUnit, rlArray.lowReg);
1537 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001538 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001539 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
jeffhao21e12712012-05-25 19:06:18 -07001540 rlResult.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001541 storeValueWide(cUnit, rlDest, rlResult);
1542 } else {
1543 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001544
buzbee408ad162012-06-06 16:45:18 -07001545 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001546 dataOffset, rlResult.lowReg, INVALID_REG, size,
1547 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001548
Bill Buzbeea114add2012-05-03 15:00:40 -07001549 storeValue(cUnit, rlDest, rlResult);
1550 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001551#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001552 int regPtr = oatAllocTemp(cUnit);
buzbee408ad162012-06-06 16:45:18 -07001553 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001554 int regLen = INVALID_REG;
1555 if (needsRangeCheck) {
1556 regLen = oatAllocTemp(cUnit);
1557 /* Get len */
1558 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1559 }
1560 /* regPtr -> array data */
1561 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1562 oatFreeTemp(cUnit, rlArray.lowReg);
1563 if ((size == kLong) || (size == kDouble)) {
1564 if (scale) {
1565 int rNewIndex = oatAllocTemp(cUnit);
1566 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1567 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1568 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001569 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001570 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001571 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001572 oatFreeTemp(cUnit, rlIndex.lowReg);
1573 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1574
1575 if (needsRangeCheck) {
1576 // TODO: change kCondCS to a more meaningful name, is the sense of
1577 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001578 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001579 oatFreeTemp(cUnit, regLen);
1580 }
1581 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1582
1583 oatFreeTemp(cUnit, regPtr);
1584 storeValueWide(cUnit, rlDest, rlResult);
1585 } else {
1586 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1587
1588 if (needsRangeCheck) {
1589 // TODO: change kCondCS to a more meaningful name, is the sense of
1590 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001591 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001592 oatFreeTemp(cUnit, regLen);
1593 }
1594 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1595 scale, size);
1596
1597 oatFreeTemp(cUnit, regPtr);
1598 storeValue(cUnit, rlDest, rlResult);
1599 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001600#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001601}
1602
1603/*
1604 * Generate array store
1605 *
1606 */
buzbee408ad162012-06-06 16:45:18 -07001607void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001608 RegLocation rlArray, RegLocation rlIndex,
1609 RegLocation rlSrc, int scale)
1610{
Bill Buzbeea114add2012-05-03 15:00:40 -07001611 RegisterClass regClass = oatRegClassBySize(size);
1612 int lenOffset = Array::LengthOffset().Int32Value();
1613 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001614
Bill Buzbeea114add2012-05-03 15:00:40 -07001615 if (size == kLong || size == kDouble) {
1616 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1617 } else {
1618 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1619 }
buzbee31a4a6f2012-02-28 15:36:15 -08001620
Bill Buzbeea114add2012-05-03 15:00:40 -07001621 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1622 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001623#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001624 int regPtr;
1625 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1626 oatClobber(cUnit, rlArray.lowReg);
1627 regPtr = rlArray.lowReg;
1628 } else {
1629 regPtr = oatAllocTemp(cUnit);
1630 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1631 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001632#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001633
Bill Buzbeea114add2012-05-03 15:00:40 -07001634 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001635 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001636
Ian Rogersb41b33b2012-03-20 14:22:54 -07001637#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001638 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001639 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1640 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001641 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001642 }
1643 if ((size == kLong) || (size == kDouble)) {
1644 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1645 } else {
1646 rlSrc = loadValue(cUnit, rlSrc, regClass);
1647 }
jeffhao703f2cd2012-07-13 17:25:52 -07001648 // If the src reg can't be byte accessed, move it to a temp first.
1649 if ((size == kSignedByte || size == kUnsignedByte) && rlSrc.lowReg >= 4) {
1650 int temp = oatAllocTemp(cUnit);
1651 opRegCopy(cUnit, temp, rlSrc.lowReg);
1652 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1653 dataOffset, temp, INVALID_REG, size,
1654 INVALID_SREG);
1655 } else {
1656 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1657 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1658 INVALID_SREG);
1659 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001660#else
buzbee408ad162012-06-06 16:45:18 -07001661 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001662 int regLen = INVALID_REG;
1663 if (needsRangeCheck) {
1664 regLen = oatAllocTemp(cUnit);
1665 //NOTE: max live temps(4) here.
1666 /* Get len */
1667 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1668 }
1669 /* regPtr -> array data */
1670 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1671 /* at this point, regPtr points to array, 2 live temps */
1672 if ((size == kLong) || (size == kDouble)) {
1673 //TUNING: specific wide routine that can handle fp regs
1674 if (scale) {
1675 int rNewIndex = oatAllocTemp(cUnit);
1676 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1677 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1678 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001679 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001680 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001681 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001682 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1683
1684 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001685 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001686 oatFreeTemp(cUnit, regLen);
1687 }
1688
jeffhao41005dd2012-05-09 17:58:52 -07001689 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001690
1691 oatFreeTemp(cUnit, regPtr);
1692 } else {
1693 rlSrc = loadValue(cUnit, rlSrc, regClass);
1694 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001695 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001696 oatFreeTemp(cUnit, regLen);
1697 }
1698 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1699 scale, size);
1700 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001701#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001702}
1703
buzbee408ad162012-06-06 16:45:18 -07001704void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001705 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001706 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001707{
Bill Buzbeea114add2012-05-03 15:00:40 -07001708 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001709#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001710 /*
1711 * NOTE: This is the one place in the code in which we might have
1712 * as many as six live temporary registers. There are 5 in the normal
1713 * set for Arm. Until we have spill capabilities, temporarily add
1714 * lr to the temp set. It is safe to do this locally, but note that
1715 * lr is used explicitly elsewhere in the code generator and cannot
1716 * normally be used as a general temp register.
1717 */
1718 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1719 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001720#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001721 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1722 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1723 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1724 // The longs may overlap - use intermediate temp if so
1725 if (rlResult.lowReg == rlSrc1.highReg) {
1726 int tReg = oatAllocTemp(cUnit);
1727 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1728 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1729 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1730 oatFreeTemp(cUnit, tReg);
1731 } else {
1732 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1733 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1734 rlSrc2.highReg);
1735 }
1736 /*
1737 * NOTE: If rlDest refers to a frame variable in a large frame, the
1738 * following storeValueWide might need to allocate a temp register.
1739 * To further work around the lack of a spill capability, explicitly
1740 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1741 * Remove when spill is functional.
1742 */
1743 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1744 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1745 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001746#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001747 oatClobber(cUnit, rLR);
1748 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001749#endif
1750}
1751
1752
buzbee408ad162012-06-06 16:45:18 -07001753bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001754 RegLocation rlSrc1, RegLocation rlShift)
1755{
Bill Buzbeea114add2012-05-03 15:00:40 -07001756 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001757
buzbee408ad162012-06-06 16:45:18 -07001758 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001759 case Instruction::SHL_LONG:
1760 case Instruction::SHL_LONG_2ADDR:
1761 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1762 break;
1763 case Instruction::SHR_LONG:
1764 case Instruction::SHR_LONG_2ADDR:
1765 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1766 break;
1767 case Instruction::USHR_LONG:
1768 case Instruction::USHR_LONG_2ADDR:
1769 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1770 break;
1771 default:
1772 LOG(FATAL) << "Unexpected case";
1773 return true;
1774 }
1775 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07001776 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001777 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1778 storeValueWide(cUnit, rlDest, rlResult);
1779 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001780}
1781
1782
buzbee408ad162012-06-06 16:45:18 -07001783bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001784 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001785{
Bill Buzbeea114add2012-05-03 15:00:40 -07001786 OpKind op = kOpBkpt;
1787 bool callOut = false;
1788 bool checkZero = false;
1789 bool unary = false;
1790 RegLocation rlResult;
1791 bool shiftOp = false;
1792 int funcOffset;
1793 int retReg = rRET0;
buzbee408ad162012-06-06 16:45:18 -07001794 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001795 case Instruction::NEG_INT:
1796 op = kOpNeg;
1797 unary = true;
1798 break;
1799 case Instruction::NOT_INT:
1800 op = kOpMvn;
1801 unary = true;
1802 break;
1803 case Instruction::ADD_INT:
1804 case Instruction::ADD_INT_2ADDR:
1805 op = kOpAdd;
1806 break;
1807 case Instruction::SUB_INT:
1808 case Instruction::SUB_INT_2ADDR:
1809 op = kOpSub;
1810 break;
1811 case Instruction::MUL_INT:
1812 case Instruction::MUL_INT_2ADDR:
1813 op = kOpMul;
1814 break;
1815 case Instruction::DIV_INT:
1816 case Instruction::DIV_INT_2ADDR:
1817 checkZero = true;
1818 op = kOpDiv;
1819 callOut = true;
1820 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1821 retReg = rRET0;
1822 break;
1823 /* NOTE: returns in rARG1 */
1824 case Instruction::REM_INT:
1825 case Instruction::REM_INT_2ADDR:
1826 checkZero = true;
1827 op = kOpRem;
1828 callOut = true;
1829 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1830 retReg = rRET1;
1831 break;
1832 case Instruction::AND_INT:
1833 case Instruction::AND_INT_2ADDR:
1834 op = kOpAnd;
1835 break;
1836 case Instruction::OR_INT:
1837 case Instruction::OR_INT_2ADDR:
1838 op = kOpOr;
1839 break;
1840 case Instruction::XOR_INT:
1841 case Instruction::XOR_INT_2ADDR:
1842 op = kOpXor;
1843 break;
1844 case Instruction::SHL_INT:
1845 case Instruction::SHL_INT_2ADDR:
1846 shiftOp = true;
1847 op = kOpLsl;
1848 break;
1849 case Instruction::SHR_INT:
1850 case Instruction::SHR_INT_2ADDR:
1851 shiftOp = true;
1852 op = kOpAsr;
1853 break;
1854 case Instruction::USHR_INT:
1855 case Instruction::USHR_INT_2ADDR:
1856 shiftOp = true;
1857 op = kOpLsr;
1858 break;
1859 default:
1860 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001861 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001862 }
1863 if (!callOut) {
1864 if (unary) {
1865 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1866 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1867 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001868 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001869 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001870#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001871 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1872 int tReg = oatAllocTemp(cUnit);
1873 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001874#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001875 // X86 doesn't require masking and must use ECX
1876 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1877 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001878#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001879 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1880 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1881 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1882 oatFreeTemp(cUnit, tReg);
1883 } else {
1884 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1885 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1886 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1887 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1888 }
buzbee31a4a6f2012-02-28 15:36:15 -08001889 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001890 storeValue(cUnit, rlDest, rlResult);
1891 } else {
1892 RegLocation rlResult;
1893 oatFlushAllRegs(cUnit); /* Send everything to home location */
1894 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1895#if !defined(TARGET_X86)
1896 int rTgt = loadHelper(cUnit, funcOffset);
1897#endif
1898 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1899 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001900 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001901 }
buzbee8320f382012-09-11 16:29:42 -07001902 // NOTE: callout here is not a safepoint
Bill Buzbeea114add2012-05-03 15:00:40 -07001903#if !defined(TARGET_X86)
1904 opReg(cUnit, kOpBlx, rTgt);
1905 oatFreeTemp(cUnit, rTgt);
1906#else
1907 opThreadMem(cUnit, kOpBlx, funcOffset);
1908#endif
1909 if (retReg == rRET0)
1910 rlResult = oatGetReturn(cUnit, false);
1911 else
1912 rlResult = oatGetReturnAlt(cUnit);
1913 storeValue(cUnit, rlDest, rlResult);
1914 }
1915 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001916}
1917
1918/*
1919 * The following are the first-level codegen routines that analyze the format
1920 * of each bytecode then either dispatch special purpose codegen routines
1921 * or produce corresponding Thumb instructions directly.
1922 */
1923
1924bool isPowerOfTwo(int x)
1925{
Bill Buzbeea114add2012-05-03 15:00:40 -07001926 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001927}
1928
1929// Returns true if no more than two bits are set in 'x'.
1930bool isPopCountLE2(unsigned int x)
1931{
Bill Buzbeea114add2012-05-03 15:00:40 -07001932 x &= x - 1;
1933 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001934}
1935
1936// Returns the index of the lowest set bit in 'x'.
1937int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001938 int bit_posn = 0;
1939 while ((x & 0xf) == 0) {
1940 bit_posn += 4;
1941 x >>= 4;
1942 }
1943 while ((x & 1) == 0) {
1944 bit_posn++;
1945 x >>= 1;
1946 }
1947 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001948}
1949
1950// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1951// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001952bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001953 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001954{
buzbeef3aac972012-04-11 16:33:36 -07001955#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001956 // No divide instruction for Arm, so check for more special cases
1957 if (lit < 2) {
1958 return false;
1959 }
1960 if (!isPowerOfTwo(lit)) {
1961 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1962 }
buzbeef3aac972012-04-11 16:33:36 -07001963#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001964 if (lit < 2 || !isPowerOfTwo(lit)) {
1965 return false;
1966 }
buzbeef3aac972012-04-11 16:33:36 -07001967#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001968 int k = lowestSetBit(lit);
1969 if (k >= 30) {
1970 // Avoid special cases.
1971 return false;
1972 }
1973 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1974 dalvikOpcode == Instruction::DIV_INT_LIT16);
1975 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1976 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1977 if (div) {
1978 int tReg = oatAllocTemp(cUnit);
1979 if (lit == 2) {
1980 // Division by 2 is by far the most common division by constant.
1981 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1982 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1983 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001984 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001985 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1986 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1987 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1988 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001989 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001990 } else {
1991 int tReg1 = oatAllocTemp(cUnit);
1992 int tReg2 = oatAllocTemp(cUnit);
1993 if (lit == 2) {
1994 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1995 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1996 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1997 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1998 } else {
1999 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2000 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2001 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2002 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
2003 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2004 }
2005 }
2006 storeValue(cUnit, rlDest, rlResult);
2007 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002008}
2009
2010void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
2011 RegLocation rlResult, int lit,
2012 int firstBit, int secondBit)
2013{
buzbee0398c422012-03-02 15:22:47 -08002014#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002015 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
2016 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08002017#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002018 int tReg = oatAllocTemp(cUnit);
2019 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
2020 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
2021 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08002022#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002023 if (firstBit != 0) {
2024 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
2025 }
buzbee31a4a6f2012-02-28 15:36:15 -08002026}
2027
2028// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2029// and store the result in 'rlDest'.
2030bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
2031 RegLocation rlDest, int lit)
2032{
Bill Buzbeea114add2012-05-03 15:00:40 -07002033 // Can we simplify this multiplication?
2034 bool powerOfTwo = false;
2035 bool popCountLE2 = false;
2036 bool powerOfTwoMinusOne = false;
2037 if (lit < 2) {
2038 // Avoid special cases.
2039 return false;
2040 } else if (isPowerOfTwo(lit)) {
2041 powerOfTwo = true;
2042 } else if (isPopCountLE2(lit)) {
2043 popCountLE2 = true;
2044 } else if (isPowerOfTwo(lit + 1)) {
2045 powerOfTwoMinusOne = true;
2046 } else {
2047 return false;
2048 }
2049 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2050 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2051 if (powerOfTwo) {
2052 // Shift.
2053 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2054 lowestSetBit(lit));
2055 } else if (popCountLE2) {
2056 // Shift and add and shift.
2057 int firstBit = lowestSetBit(lit);
2058 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2059 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2060 firstBit, secondBit);
2061 } else {
2062 // Reverse subtract: (src << (shift + 1)) - src.
2063 DCHECK(powerOfTwoMinusOne);
2064 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2065 int tReg = oatAllocTemp(cUnit);
2066 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2067 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2068 }
2069 storeValue(cUnit, rlDest, rlResult);
2070 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002071}
2072
buzbee408ad162012-06-06 16:45:18 -07002073bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2074 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002075{
Bill Buzbeea114add2012-05-03 15:00:40 -07002076 RegLocation rlResult;
2077 OpKind op = (OpKind)0; /* Make gcc happy */
2078 int shiftOp = false;
2079 bool isDiv = false;
2080 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002081
buzbee408ad162012-06-06 16:45:18 -07002082 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002083 case Instruction::RSUB_INT_LIT8:
2084 case Instruction::RSUB_INT: {
2085 int tReg;
2086 //TUNING: add support for use of Arm rsub op
2087 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2088 tReg = oatAllocTemp(cUnit);
2089 loadConstant(cUnit, tReg, lit);
2090 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2091 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2092 storeValue(cUnit, rlDest, rlResult);
2093 return false;
2094 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002095 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002096
2097 case Instruction::ADD_INT_LIT8:
2098 case Instruction::ADD_INT_LIT16:
2099 op = kOpAdd;
2100 break;
2101 case Instruction::MUL_INT_LIT8:
2102 case Instruction::MUL_INT_LIT16: {
2103 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2104 return false;
2105 }
2106 op = kOpMul;
2107 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002108 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002109 case Instruction::AND_INT_LIT8:
2110 case Instruction::AND_INT_LIT16:
2111 op = kOpAnd;
2112 break;
2113 case Instruction::OR_INT_LIT8:
2114 case Instruction::OR_INT_LIT16:
2115 op = kOpOr;
2116 break;
2117 case Instruction::XOR_INT_LIT8:
2118 case Instruction::XOR_INT_LIT16:
2119 op = kOpXor;
2120 break;
2121 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002122 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002123 lit &= 31;
2124 shiftOp = true;
2125 op = kOpLsl;
2126 break;
2127 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002128 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002129 lit &= 31;
2130 shiftOp = true;
2131 op = kOpAsr;
2132 break;
2133 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002134 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002135 lit &= 31;
2136 shiftOp = true;
2137 op = kOpLsr;
2138 break;
2139
2140 case Instruction::DIV_INT_LIT8:
2141 case Instruction::DIV_INT_LIT16:
2142 case Instruction::REM_INT_LIT8:
2143 case Instruction::REM_INT_LIT16:
2144 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002145 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002146 return false;
2147 }
buzbee408ad162012-06-06 16:45:18 -07002148 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002149 return false;
2150 }
2151 oatFlushAllRegs(cUnit); /* Everything to home location */
2152 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2153 oatClobber(cUnit, rARG0);
2154 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee408ad162012-06-06 16:45:18 -07002155 if ((opcode == Instruction::DIV_INT_LIT8) ||
2156 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002157 isDiv = true;
2158 } else {
2159 isDiv = false;
2160 }
buzbee8320f382012-09-11 16:29:42 -07002161 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002162 if (isDiv)
2163 rlResult = oatGetReturn(cUnit, false);
2164 else
2165 rlResult = oatGetReturnAlt(cUnit);
2166 storeValue(cUnit, rlDest, rlResult);
2167 return false;
2168 break;
2169 default:
2170 return true;
2171 }
2172 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2173 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2174 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2175 if (shiftOp && (lit == 0)) {
2176 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2177 } else {
2178 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2179 }
2180 storeValue(cUnit, rlDest, rlResult);
2181 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002182}
2183
buzbee408ad162012-06-06 16:45:18 -07002184bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002185 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002186{
Bill Buzbeea114add2012-05-03 15:00:40 -07002187 RegLocation rlResult;
2188 OpKind firstOp = kOpBkpt;
2189 OpKind secondOp = kOpBkpt;
2190 bool callOut = false;
2191 bool checkZero = false;
2192 int funcOffset;
2193 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002194
buzbee408ad162012-06-06 16:45:18 -07002195 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002196 case Instruction::NOT_LONG:
2197 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2198 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2199 // Check for destructive overlap
2200 if (rlResult.lowReg == rlSrc2.highReg) {
2201 int tReg = oatAllocTemp(cUnit);
2202 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2203 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2204 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2205 oatFreeTemp(cUnit, tReg);
2206 } else {
2207 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2208 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2209 }
2210 storeValueWide(cUnit, rlDest, rlResult);
2211 return false;
2212 break;
2213 case Instruction::ADD_LONG:
2214 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002215#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002216 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002217#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002218 firstOp = kOpAdd;
2219 secondOp = kOpAdc;
2220 break;
buzbeec5159d52012-03-03 11:48:39 -08002221#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002222 case Instruction::SUB_LONG:
2223 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002224#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002225 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002226#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002227 firstOp = kOpSub;
2228 secondOp = kOpSbc;
2229 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002230#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002231 case Instruction::MUL_LONG:
2232 case Instruction::MUL_LONG_2ADDR:
2233 callOut = true;
2234 retReg = rRET0;
2235 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2236 break;
2237 case Instruction::DIV_LONG:
2238 case Instruction::DIV_LONG_2ADDR:
2239 callOut = true;
2240 checkZero = true;
2241 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002242 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002243 break;
2244 case Instruction::REM_LONG:
2245 case Instruction::REM_LONG_2ADDR:
2246 callOut = true;
2247 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002248 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002249#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002250 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2251 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002252#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002253 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002254#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002255 break;
2256 case Instruction::AND_LONG_2ADDR:
2257 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002258#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002259 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002260#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002261 firstOp = kOpAnd;
2262 secondOp = kOpAnd;
2263 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002264#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002265 case Instruction::OR_LONG:
2266 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002267#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002268 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002269#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002270 firstOp = kOpOr;
2271 secondOp = kOpOr;
2272 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002273#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002274 case Instruction::XOR_LONG:
2275 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002276#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002277 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002278#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002279 firstOp = kOpXor;
2280 secondOp = kOpXor;
2281 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002282#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002283 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002284 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002285 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002286 default:
2287 LOG(FATAL) << "Invalid long arith op";
2288 }
2289 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002290 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002291 } else {
2292 oatFlushAllRegs(cUnit); /* Send everything to home location */
2293 if (checkZero) {
2294 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2295#if !defined(TARGET_X86)
2296 int rTgt = loadHelper(cUnit, funcOffset);
2297#endif
2298 int tReg = oatAllocTemp(cUnit);
2299#if defined(TARGET_ARM)
2300 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2301 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002302 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002303#else
2304 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2305#endif
buzbee408ad162012-06-06 16:45:18 -07002306 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002307 oatFreeTemp(cUnit, tReg);
2308 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
buzbee8320f382012-09-11 16:29:42 -07002309 // NOTE: callout here is not a safepoint
Bill Buzbeea114add2012-05-03 15:00:40 -07002310#if !defined(TARGET_X86)
2311 opReg(cUnit, kOpBlx, rTgt);
2312 oatFreeTemp(cUnit, rTgt);
2313#else
2314 opThreadMem(cUnit, kOpBlx, funcOffset);
2315#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002316 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002317 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
buzbee8320f382012-09-11 16:29:42 -07002318 rlSrc1, rlSrc2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002319 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002320 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2321 if (retReg == rRET0)
2322 rlResult = oatGetReturnWide(cUnit, false);
2323 else
2324 rlResult = oatGetReturnWideAlt(cUnit);
2325 storeValueWide(cUnit, rlDest, rlResult);
2326 }
2327 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002328}
2329
buzbee408ad162012-06-06 16:45:18 -07002330bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2331 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002332{
Bill Buzbeea114add2012-05-03 15:00:40 -07002333 /*
2334 * Don't optimize the register usage since it calls out to support
2335 * functions
2336 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002337 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002338 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002339 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002340 } else {
2341 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002342 }
buzbee8320f382012-09-11 16:29:42 -07002343 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc, false);
buzbee408ad162012-06-06 16:45:18 -07002344 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002345 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002346 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2347 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002348 } else {
2349 RegLocation rlResult;
2350 rlResult = oatGetReturn(cUnit, rlDest.fp);
2351 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002352 }
2353 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002354}
2355
2356void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002357bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002358 RegLocation rlDest, RegLocation rlSrc1,
2359 RegLocation rlSrc2)
2360{
Bill Buzbeea114add2012-05-03 15:00:40 -07002361 RegLocation rlResult;
2362 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002363
buzbee408ad162012-06-06 16:45:18 -07002364 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002365 case Instruction::ADD_FLOAT_2ADDR:
2366 case Instruction::ADD_FLOAT:
2367 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2368 break;
2369 case Instruction::SUB_FLOAT_2ADDR:
2370 case Instruction::SUB_FLOAT:
2371 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2372 break;
2373 case Instruction::DIV_FLOAT_2ADDR:
2374 case Instruction::DIV_FLOAT:
2375 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2376 break;
2377 case Instruction::MUL_FLOAT_2ADDR:
2378 case Instruction::MUL_FLOAT:
2379 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2380 break;
2381 case Instruction::REM_FLOAT_2ADDR:
2382 case Instruction::REM_FLOAT:
2383 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2384 break;
2385 case Instruction::NEG_FLOAT: {
2386 genNegFloat(cUnit, rlDest, rlSrc1);
2387 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002388 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002389 default:
2390 return true;
2391 }
2392 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002393 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002394 rlResult = oatGetReturn(cUnit, true);
2395 storeValue(cUnit, rlDest, rlResult);
2396 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002397}
2398
2399void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002400bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002401 RegLocation rlDest, RegLocation rlSrc1,
2402 RegLocation rlSrc2)
2403{
Bill Buzbeea114add2012-05-03 15:00:40 -07002404 RegLocation rlResult;
2405 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002406
buzbee408ad162012-06-06 16:45:18 -07002407 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002408 case Instruction::ADD_DOUBLE_2ADDR:
2409 case Instruction::ADD_DOUBLE:
2410 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2411 break;
2412 case Instruction::SUB_DOUBLE_2ADDR:
2413 case Instruction::SUB_DOUBLE:
2414 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2415 break;
2416 case Instruction::DIV_DOUBLE_2ADDR:
2417 case Instruction::DIV_DOUBLE:
2418 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2419 break;
2420 case Instruction::MUL_DOUBLE_2ADDR:
2421 case Instruction::MUL_DOUBLE:
2422 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2423 break;
2424 case Instruction::REM_DOUBLE_2ADDR:
2425 case Instruction::REM_DOUBLE:
2426 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2427 break;
2428 case Instruction::NEG_DOUBLE: {
2429 genNegDouble(cUnit, rlDest, rlSrc1);
2430 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002431 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002432 default:
2433 return true;
2434 }
2435 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002436 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002437 rlResult = oatGetReturnWide(cUnit, true);
2438 storeValueWide(cUnit, rlDest, rlResult);
2439 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002440}
2441
buzbee408ad162012-06-06 16:45:18 -07002442bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2443 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002444{
buzbee31a4a6f2012-02-28 15:36:15 -08002445
Bill Buzbeea114add2012-05-03 15:00:40 -07002446 switch (opcode) {
2447 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002448 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2449 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002450 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002451 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2452 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002453 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002454 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2455 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002456 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002457 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2458 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002459 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002460 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2461 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002462 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002463 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2464 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002465 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002466 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2467 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002468 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002469 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2470 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002471 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002472 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2473 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002474 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002475 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2476 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002477 default:
2478 return true;
2479 }
2480 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002481}
2482
2483/*
2484 * Generate callout to updateDebugger. Note that we're overloading
2485 * the use of rSUSPEND here. When the debugger is active, this
2486 * register holds the address of the update function. So, if it's
2487 * non-null, we call out to it.
2488 *
2489 * Note also that rRET0 and rRET1 must be preserved across this
2490 * code. This must be handled by the stub.
2491 */
2492void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2493{
Bill Buzbeea114add2012-05-03 15:00:40 -07002494 // Following DCHECK verifies that dPC is in range of single load immediate
2495 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2496 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2497 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002498#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002499 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2500 opIT(cUnit, kArmCondNe, "T");
2501 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
buzbee8320f382012-09-11 16:29:42 -07002502 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2503 markSafepointPC(cUnit, callInst);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002504#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002505 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002506#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002507 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2508 loadConstant(cUnit, rARG2, offset);
buzbee8320f382012-09-11 16:29:42 -07002509 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2510 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002511 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2512 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002513#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002514 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002515}
2516
2517/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002518void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002519{
buzbee408ad162012-06-06 16:45:18 -07002520 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002521 return;
2522 }
2523 oatFlushAllRegs(cUnit);
2524 if (cUnit->genDebugger) {
2525 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002526#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002527 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002528#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002529 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
buzbee8320f382012-09-11 16:29:42 -07002530 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
2531 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002532 // Refresh rSUSPEND
2533 loadWordDisp(cUnit, rSELF,
2534 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2535 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002536#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002537 } else {
2538 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002539#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002540 // In non-debug case, only check periodically
2541 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2542 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002543#elif defined(TARGET_X86)
Ian Rogers474b6da2012-09-25 00:20:38 -07002544 newLIR2(cUnit, kX86Cmp16TI8, Thread::ThreadFlagsOffset().Int32Value(), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002545 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002546#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002547 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2548 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002549#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002550 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2551 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002552 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002553 branch->target = (LIR*)target;
2554 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2555 }
buzbee31a4a6f2012-02-28 15:36:15 -08002556}
2557
buzbeefead2932012-03-30 14:02:01 -07002558/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002559void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002560{
buzbee408ad162012-06-06 16:45:18 -07002561 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002562 opUnconditionalBranch(cUnit, target);
2563 return;
2564 }
2565 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002566 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002567 opUnconditionalBranch(cUnit, target);
2568 } else {
buzbeefead2932012-03-30 14:02:01 -07002569#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002570 // In non-debug case, only check periodically
2571 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2572 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002573#elif defined(TARGET_X86)
Ian Rogers474b6da2012-09-25 00:20:38 -07002574 newLIR2(cUnit, kX86Cmp16TI8, Thread::ThreadFlagsOffset().Int32Value(), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002575 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002576#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002577 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2578 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002579#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002580 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002581 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002582 oatFlushAllRegs(cUnit);
2583 opUnconditionalBranch(cUnit, launchPad);
2584 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2585 (intptr_t)launchPad);
2586 }
buzbeefead2932012-03-30 14:02:01 -07002587}
2588
buzbee31a4a6f2012-02-28 15:36:15 -08002589} // namespace art