blob: d948d2d8f2b61342da2e4b334167c43e8f6838ad [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;
jeffhao4f8f04a2012-10-02 18:10:35 -07001787 bool isDivRem = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001788 bool checkZero = false;
1789 bool unary = false;
1790 RegLocation rlResult;
1791 bool shiftOp = false;
buzbee408ad162012-06-06 16:45:18 -07001792 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001793 case Instruction::NEG_INT:
1794 op = kOpNeg;
1795 unary = true;
1796 break;
1797 case Instruction::NOT_INT:
1798 op = kOpMvn;
1799 unary = true;
1800 break;
1801 case Instruction::ADD_INT:
1802 case Instruction::ADD_INT_2ADDR:
1803 op = kOpAdd;
1804 break;
1805 case Instruction::SUB_INT:
1806 case Instruction::SUB_INT_2ADDR:
1807 op = kOpSub;
1808 break;
1809 case Instruction::MUL_INT:
1810 case Instruction::MUL_INT_2ADDR:
1811 op = kOpMul;
1812 break;
1813 case Instruction::DIV_INT:
1814 case Instruction::DIV_INT_2ADDR:
1815 checkZero = true;
1816 op = kOpDiv;
jeffhao4f8f04a2012-10-02 18:10:35 -07001817 isDivRem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001818 break;
1819 /* NOTE: returns in rARG1 */
1820 case Instruction::REM_INT:
1821 case Instruction::REM_INT_2ADDR:
1822 checkZero = true;
1823 op = kOpRem;
jeffhao4f8f04a2012-10-02 18:10:35 -07001824 isDivRem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001825 break;
1826 case Instruction::AND_INT:
1827 case Instruction::AND_INT_2ADDR:
1828 op = kOpAnd;
1829 break;
1830 case Instruction::OR_INT:
1831 case Instruction::OR_INT_2ADDR:
1832 op = kOpOr;
1833 break;
1834 case Instruction::XOR_INT:
1835 case Instruction::XOR_INT_2ADDR:
1836 op = kOpXor;
1837 break;
1838 case Instruction::SHL_INT:
1839 case Instruction::SHL_INT_2ADDR:
1840 shiftOp = true;
1841 op = kOpLsl;
1842 break;
1843 case Instruction::SHR_INT:
1844 case Instruction::SHR_INT_2ADDR:
1845 shiftOp = true;
1846 op = kOpAsr;
1847 break;
1848 case Instruction::USHR_INT:
1849 case Instruction::USHR_INT_2ADDR:
1850 shiftOp = true;
1851 op = kOpLsr;
1852 break;
1853 default:
1854 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001855 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001856 }
jeffhao4f8f04a2012-10-02 18:10:35 -07001857 if (!isDivRem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001858 if (unary) {
1859 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1860 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1861 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001862 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001863 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001864#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001865 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1866 int tReg = oatAllocTemp(cUnit);
1867 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001868#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001869 // X86 doesn't require masking and must use ECX
1870 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1871 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001872#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001873 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1874 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1875 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1876 oatFreeTemp(cUnit, tReg);
1877 } else {
1878 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1879 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1880 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1881 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1882 }
buzbee31a4a6f2012-02-28 15:36:15 -08001883 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001884 storeValue(cUnit, rlDest, rlResult);
1885 } else {
jeffhao4f8f04a2012-10-02 18:10:35 -07001886#if defined(TARGET_MIPS)
1887 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1888 if (checkZero) {
1889 genNullCheck(cUnit, rlSrc2.sRegLow, rlSrc2.lowReg, 0);
1890 }
1891 newLIR4(cUnit, kMipsDiv, r_HI, r_LO, rlSrc1.lowReg, rlSrc2.lowReg);
1892 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1893 if (op == kOpDiv) {
1894 newLIR2(cUnit, kMipsMflo, rlResult.lowReg, r_LO);
1895 } else {
1896 newLIR2(cUnit, kMipsMfhi, rlResult.lowReg, r_HI);
1897 }
1898#else
1899 int funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
Bill Buzbeea114add2012-05-03 15:00:40 -07001900 RegLocation rlResult;
1901 oatFlushAllRegs(cUnit); /* Send everything to home location */
1902 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
jeffhao4f8f04a2012-10-02 18:10:35 -07001903#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001904 int rTgt = loadHelper(cUnit, funcOffset);
1905#endif
1906 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1907 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001908 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001909 }
buzbee8320f382012-09-11 16:29:42 -07001910 // NOTE: callout here is not a safepoint
jeffhao4f8f04a2012-10-02 18:10:35 -07001911#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001912 opReg(cUnit, kOpBlx, rTgt);
1913 oatFreeTemp(cUnit, rTgt);
1914#else
1915 opThreadMem(cUnit, kOpBlx, funcOffset);
1916#endif
jeffhao4f8f04a2012-10-02 18:10:35 -07001917 if (op == kOpDiv)
Bill Buzbeea114add2012-05-03 15:00:40 -07001918 rlResult = oatGetReturn(cUnit, false);
1919 else
1920 rlResult = oatGetReturnAlt(cUnit);
1921 storeValue(cUnit, rlDest, rlResult);
jeffhao4f8f04a2012-10-02 18:10:35 -07001922#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001923 }
1924 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001925}
1926
1927/*
1928 * The following are the first-level codegen routines that analyze the format
1929 * of each bytecode then either dispatch special purpose codegen routines
1930 * or produce corresponding Thumb instructions directly.
1931 */
1932
1933bool isPowerOfTwo(int x)
1934{
Bill Buzbeea114add2012-05-03 15:00:40 -07001935 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001936}
1937
1938// Returns true if no more than two bits are set in 'x'.
1939bool isPopCountLE2(unsigned int x)
1940{
Bill Buzbeea114add2012-05-03 15:00:40 -07001941 x &= x - 1;
1942 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001943}
1944
1945// Returns the index of the lowest set bit in 'x'.
1946int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001947 int bit_posn = 0;
1948 while ((x & 0xf) == 0) {
1949 bit_posn += 4;
1950 x >>= 4;
1951 }
1952 while ((x & 1) == 0) {
1953 bit_posn++;
1954 x >>= 1;
1955 }
1956 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001957}
1958
1959// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1960// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001961bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001962 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001963{
buzbeef3aac972012-04-11 16:33:36 -07001964#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001965 // No divide instruction for Arm, so check for more special cases
1966 if (lit < 2) {
1967 return false;
1968 }
1969 if (!isPowerOfTwo(lit)) {
1970 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1971 }
buzbeef3aac972012-04-11 16:33:36 -07001972#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001973 if (lit < 2 || !isPowerOfTwo(lit)) {
1974 return false;
1975 }
buzbeef3aac972012-04-11 16:33:36 -07001976#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001977 int k = lowestSetBit(lit);
1978 if (k >= 30) {
1979 // Avoid special cases.
1980 return false;
1981 }
1982 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1983 dalvikOpcode == Instruction::DIV_INT_LIT16);
1984 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1985 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1986 if (div) {
1987 int tReg = oatAllocTemp(cUnit);
1988 if (lit == 2) {
1989 // Division by 2 is by far the most common division by constant.
1990 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1991 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1992 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001993 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001994 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1995 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1996 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1997 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001998 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001999 } else {
2000 int tReg1 = oatAllocTemp(cUnit);
2001 int tReg2 = oatAllocTemp(cUnit);
2002 if (lit == 2) {
2003 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
2004 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2005 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
2006 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2007 } else {
2008 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
2009 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
2010 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
2011 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
2012 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
2013 }
2014 }
2015 storeValue(cUnit, rlDest, rlResult);
2016 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002017}
2018
2019void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
2020 RegLocation rlResult, int lit,
2021 int firstBit, int secondBit)
2022{
buzbee0398c422012-03-02 15:22:47 -08002023#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002024 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
2025 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08002026#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002027 int tReg = oatAllocTemp(cUnit);
2028 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
2029 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
2030 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08002031#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002032 if (firstBit != 0) {
2033 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
2034 }
buzbee31a4a6f2012-02-28 15:36:15 -08002035}
2036
2037// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
2038// and store the result in 'rlDest'.
2039bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
2040 RegLocation rlDest, int lit)
2041{
Bill Buzbeea114add2012-05-03 15:00:40 -07002042 // Can we simplify this multiplication?
2043 bool powerOfTwo = false;
2044 bool popCountLE2 = false;
2045 bool powerOfTwoMinusOne = false;
2046 if (lit < 2) {
2047 // Avoid special cases.
2048 return false;
2049 } else if (isPowerOfTwo(lit)) {
2050 powerOfTwo = true;
2051 } else if (isPopCountLE2(lit)) {
2052 popCountLE2 = true;
2053 } else if (isPowerOfTwo(lit + 1)) {
2054 powerOfTwoMinusOne = true;
2055 } else {
2056 return false;
2057 }
2058 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2059 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2060 if (powerOfTwo) {
2061 // Shift.
2062 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2063 lowestSetBit(lit));
2064 } else if (popCountLE2) {
2065 // Shift and add and shift.
2066 int firstBit = lowestSetBit(lit);
2067 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2068 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2069 firstBit, secondBit);
2070 } else {
2071 // Reverse subtract: (src << (shift + 1)) - src.
2072 DCHECK(powerOfTwoMinusOne);
2073 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2074 int tReg = oatAllocTemp(cUnit);
2075 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2076 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2077 }
2078 storeValue(cUnit, rlDest, rlResult);
2079 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002080}
2081
buzbee408ad162012-06-06 16:45:18 -07002082bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2083 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002084{
Bill Buzbeea114add2012-05-03 15:00:40 -07002085 RegLocation rlResult;
2086 OpKind op = (OpKind)0; /* Make gcc happy */
2087 int shiftOp = false;
2088 bool isDiv = false;
buzbee31a4a6f2012-02-28 15:36:15 -08002089
buzbee408ad162012-06-06 16:45:18 -07002090 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002091 case Instruction::RSUB_INT_LIT8:
2092 case Instruction::RSUB_INT: {
2093 int tReg;
2094 //TUNING: add support for use of Arm rsub op
2095 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2096 tReg = oatAllocTemp(cUnit);
2097 loadConstant(cUnit, tReg, lit);
2098 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2099 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2100 storeValue(cUnit, rlDest, rlResult);
2101 return false;
2102 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002103 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002104
2105 case Instruction::ADD_INT_LIT8:
2106 case Instruction::ADD_INT_LIT16:
2107 op = kOpAdd;
2108 break;
2109 case Instruction::MUL_INT_LIT8:
2110 case Instruction::MUL_INT_LIT16: {
2111 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2112 return false;
2113 }
2114 op = kOpMul;
2115 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002116 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002117 case Instruction::AND_INT_LIT8:
2118 case Instruction::AND_INT_LIT16:
2119 op = kOpAnd;
2120 break;
2121 case Instruction::OR_INT_LIT8:
2122 case Instruction::OR_INT_LIT16:
2123 op = kOpOr;
2124 break;
2125 case Instruction::XOR_INT_LIT8:
2126 case Instruction::XOR_INT_LIT16:
2127 op = kOpXor;
2128 break;
2129 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002130 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002131 lit &= 31;
2132 shiftOp = true;
2133 op = kOpLsl;
2134 break;
2135 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002136 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002137 lit &= 31;
2138 shiftOp = true;
2139 op = kOpAsr;
2140 break;
2141 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002142 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002143 lit &= 31;
2144 shiftOp = true;
2145 op = kOpLsr;
2146 break;
2147
2148 case Instruction::DIV_INT_LIT8:
2149 case Instruction::DIV_INT_LIT16:
2150 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07002151 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07002152 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002153 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002154 return false;
2155 }
buzbee408ad162012-06-06 16:45:18 -07002156 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002157 return false;
2158 }
buzbee408ad162012-06-06 16:45:18 -07002159 if ((opcode == Instruction::DIV_INT_LIT8) ||
2160 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002161 isDiv = true;
2162 } else {
2163 isDiv = false;
2164 }
jeffhao4f8f04a2012-10-02 18:10:35 -07002165#if defined(TARGET_MIPS)
2166 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2167 int tReg = oatAllocTemp(cUnit);
2168 newLIR3(cUnit, kMipsAddiu, tReg, r_ZERO, lit);
2169 newLIR4(cUnit, kMipsDiv, r_HI, r_LO, rlSrc.lowReg, tReg);
2170 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2171 if (isDiv) {
2172 newLIR2(cUnit, kMipsMflo, rlResult.lowReg, r_LO);
2173 } else {
2174 newLIR2(cUnit, kMipsMfhi, rlResult.lowReg, r_HI);
2175 }
2176 oatFreeTemp(cUnit, tReg);
2177#else
2178 oatFlushAllRegs(cUnit); /* Everything to home location */
2179 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2180 oatClobber(cUnit, rARG0);
2181 int funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee8320f382012-09-11 16:29:42 -07002182 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002183 if (isDiv)
2184 rlResult = oatGetReturn(cUnit, false);
2185 else
2186 rlResult = oatGetReturnAlt(cUnit);
jeffhao4f8f04a2012-10-02 18:10:35 -07002187#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002188 storeValue(cUnit, rlDest, rlResult);
2189 return false;
2190 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07002191 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002192 default:
2193 return true;
2194 }
2195 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2196 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2197 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2198 if (shiftOp && (lit == 0)) {
2199 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2200 } else {
2201 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2202 }
2203 storeValue(cUnit, rlDest, rlResult);
2204 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002205}
2206
buzbee408ad162012-06-06 16:45:18 -07002207bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002208 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002209{
Bill Buzbeea114add2012-05-03 15:00:40 -07002210 RegLocation rlResult;
2211 OpKind firstOp = kOpBkpt;
2212 OpKind secondOp = kOpBkpt;
2213 bool callOut = false;
2214 bool checkZero = false;
2215 int funcOffset;
2216 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002217
buzbee408ad162012-06-06 16:45:18 -07002218 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002219 case Instruction::NOT_LONG:
2220 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2221 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2222 // Check for destructive overlap
2223 if (rlResult.lowReg == rlSrc2.highReg) {
2224 int tReg = oatAllocTemp(cUnit);
2225 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2226 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2227 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2228 oatFreeTemp(cUnit, tReg);
2229 } else {
2230 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2231 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2232 }
2233 storeValueWide(cUnit, rlDest, rlResult);
2234 return false;
2235 break;
2236 case Instruction::ADD_LONG:
2237 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002238#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002239 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002240#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002241 firstOp = kOpAdd;
2242 secondOp = kOpAdc;
2243 break;
buzbeec5159d52012-03-03 11:48:39 -08002244#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002245 case Instruction::SUB_LONG:
2246 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002247#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002248 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002249#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002250 firstOp = kOpSub;
2251 secondOp = kOpSbc;
2252 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002253#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002254 case Instruction::MUL_LONG:
2255 case Instruction::MUL_LONG_2ADDR:
2256 callOut = true;
2257 retReg = rRET0;
2258 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2259 break;
2260 case Instruction::DIV_LONG:
2261 case Instruction::DIV_LONG_2ADDR:
2262 callOut = true;
2263 checkZero = true;
2264 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002265 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002266 break;
2267 case Instruction::REM_LONG:
2268 case Instruction::REM_LONG_2ADDR:
2269 callOut = true;
2270 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002271 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002272#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002273 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2274 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002275#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002276 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002277#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002278 break;
2279 case Instruction::AND_LONG_2ADDR:
2280 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002281#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002282 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002283#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002284 firstOp = kOpAnd;
2285 secondOp = kOpAnd;
2286 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002287#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002288 case Instruction::OR_LONG:
2289 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002290#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002291 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002292#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002293 firstOp = kOpOr;
2294 secondOp = kOpOr;
2295 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002296#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002297 case Instruction::XOR_LONG:
2298 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002299#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002300 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002301#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002302 firstOp = kOpXor;
2303 secondOp = kOpXor;
2304 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002305#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002306 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002307 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002308 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002309 default:
2310 LOG(FATAL) << "Invalid long arith op";
2311 }
2312 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002313 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002314 } else {
2315 oatFlushAllRegs(cUnit); /* Send everything to home location */
2316 if (checkZero) {
2317 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2318#if !defined(TARGET_X86)
2319 int rTgt = loadHelper(cUnit, funcOffset);
2320#endif
2321 int tReg = oatAllocTemp(cUnit);
2322#if defined(TARGET_ARM)
2323 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2324 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002325 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002326#else
2327 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2328#endif
buzbee408ad162012-06-06 16:45:18 -07002329 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002330 oatFreeTemp(cUnit, tReg);
2331 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
buzbee8320f382012-09-11 16:29:42 -07002332 // NOTE: callout here is not a safepoint
Bill Buzbeea114add2012-05-03 15:00:40 -07002333#if !defined(TARGET_X86)
2334 opReg(cUnit, kOpBlx, rTgt);
2335 oatFreeTemp(cUnit, rTgt);
2336#else
2337 opThreadMem(cUnit, kOpBlx, funcOffset);
2338#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002339 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002340 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
buzbee8320f382012-09-11 16:29:42 -07002341 rlSrc1, rlSrc2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08002342 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002343 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2344 if (retReg == rRET0)
2345 rlResult = oatGetReturnWide(cUnit, false);
2346 else
2347 rlResult = oatGetReturnWideAlt(cUnit);
2348 storeValueWide(cUnit, rlDest, rlResult);
2349 }
2350 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002351}
2352
buzbee408ad162012-06-06 16:45:18 -07002353bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2354 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002355{
Bill Buzbeea114add2012-05-03 15:00:40 -07002356 /*
2357 * Don't optimize the register usage since it calls out to support
2358 * functions
2359 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002360 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002361 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002362 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002363 } else {
2364 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002365 }
buzbee8320f382012-09-11 16:29:42 -07002366 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc, false);
buzbee408ad162012-06-06 16:45:18 -07002367 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002368 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002369 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2370 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002371 } else {
2372 RegLocation rlResult;
2373 rlResult = oatGetReturn(cUnit, rlDest.fp);
2374 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002375 }
2376 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002377}
2378
2379void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002380bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002381 RegLocation rlDest, RegLocation rlSrc1,
2382 RegLocation rlSrc2)
2383{
Bill Buzbeea114add2012-05-03 15:00:40 -07002384 RegLocation rlResult;
2385 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002386
buzbee408ad162012-06-06 16:45:18 -07002387 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002388 case Instruction::ADD_FLOAT_2ADDR:
2389 case Instruction::ADD_FLOAT:
2390 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2391 break;
2392 case Instruction::SUB_FLOAT_2ADDR:
2393 case Instruction::SUB_FLOAT:
2394 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2395 break;
2396 case Instruction::DIV_FLOAT_2ADDR:
2397 case Instruction::DIV_FLOAT:
2398 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2399 break;
2400 case Instruction::MUL_FLOAT_2ADDR:
2401 case Instruction::MUL_FLOAT:
2402 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2403 break;
2404 case Instruction::REM_FLOAT_2ADDR:
2405 case Instruction::REM_FLOAT:
2406 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2407 break;
2408 case Instruction::NEG_FLOAT: {
2409 genNegFloat(cUnit, rlDest, rlSrc1);
2410 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002411 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002412 default:
2413 return true;
2414 }
2415 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002416 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002417 rlResult = oatGetReturn(cUnit, true);
2418 storeValue(cUnit, rlDest, rlResult);
2419 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002420}
2421
2422void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002423bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002424 RegLocation rlDest, RegLocation rlSrc1,
2425 RegLocation rlSrc2)
2426{
Bill Buzbeea114add2012-05-03 15:00:40 -07002427 RegLocation rlResult;
2428 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002429
buzbee408ad162012-06-06 16:45:18 -07002430 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002431 case Instruction::ADD_DOUBLE_2ADDR:
2432 case Instruction::ADD_DOUBLE:
2433 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2434 break;
2435 case Instruction::SUB_DOUBLE_2ADDR:
2436 case Instruction::SUB_DOUBLE:
2437 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2438 break;
2439 case Instruction::DIV_DOUBLE_2ADDR:
2440 case Instruction::DIV_DOUBLE:
2441 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2442 break;
2443 case Instruction::MUL_DOUBLE_2ADDR:
2444 case Instruction::MUL_DOUBLE:
2445 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2446 break;
2447 case Instruction::REM_DOUBLE_2ADDR:
2448 case Instruction::REM_DOUBLE:
2449 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2450 break;
2451 case Instruction::NEG_DOUBLE: {
2452 genNegDouble(cUnit, rlDest, rlSrc1);
2453 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002454 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002455 default:
2456 return true;
2457 }
2458 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee8320f382012-09-11 16:29:42 -07002459 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07002460 rlResult = oatGetReturnWide(cUnit, true);
2461 storeValueWide(cUnit, rlDest, rlResult);
2462 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002463}
2464
buzbee408ad162012-06-06 16:45:18 -07002465bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2466 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002467{
buzbee31a4a6f2012-02-28 15:36:15 -08002468
Bill Buzbeea114add2012-05-03 15:00:40 -07002469 switch (opcode) {
2470 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002471 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2472 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002473 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002474 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2475 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002476 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002477 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2478 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002479 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002480 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2481 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002482 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002483 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2484 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002485 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002486 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2487 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002488 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002489 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2490 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002491 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002492 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2493 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002494 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002495 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2496 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002497 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002498 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2499 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002500 default:
2501 return true;
2502 }
2503 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002504}
2505
2506/*
2507 * Generate callout to updateDebugger. Note that we're overloading
2508 * the use of rSUSPEND here. When the debugger is active, this
2509 * register holds the address of the update function. So, if it's
2510 * non-null, we call out to it.
2511 *
2512 * Note also that rRET0 and rRET1 must be preserved across this
2513 * code. This must be handled by the stub.
2514 */
2515void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2516{
Bill Buzbeea114add2012-05-03 15:00:40 -07002517 // Following DCHECK verifies that dPC is in range of single load immediate
2518 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2519 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2520 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002521#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002522 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2523 opIT(cUnit, kArmCondNe, "T");
2524 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
buzbee8320f382012-09-11 16:29:42 -07002525 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2526 markSafepointPC(cUnit, callInst);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002527#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002528 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002529#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002530 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2531 loadConstant(cUnit, rARG2, offset);
buzbee8320f382012-09-11 16:29:42 -07002532 LIR* callInst = opReg(cUnit, kOpBlx, rSUSPEND);
2533 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002534 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2535 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002536#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002537 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002538}
2539
2540/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002541void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002542{
buzbee408ad162012-06-06 16:45:18 -07002543 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002544 return;
2545 }
2546 oatFlushAllRegs(cUnit);
2547 if (cUnit->genDebugger) {
2548 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002549#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002550 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002551#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002552 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
buzbee8320f382012-09-11 16:29:42 -07002553 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
2554 markSafepointPC(cUnit, callInst);
Bill Buzbeea114add2012-05-03 15:00:40 -07002555 // Refresh rSUSPEND
2556 loadWordDisp(cUnit, rSELF,
2557 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2558 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002559#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002560 } else {
2561 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002562#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002563 // In non-debug case, only check periodically
2564 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2565 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002566#elif defined(TARGET_X86)
Ian Rogers474b6da2012-09-25 00:20:38 -07002567 newLIR2(cUnit, kX86Cmp16TI8, Thread::ThreadFlagsOffset().Int32Value(), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002568 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002569#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002570 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2571 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002572#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002573 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2574 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002575 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002576 branch->target = (LIR*)target;
2577 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2578 }
buzbee31a4a6f2012-02-28 15:36:15 -08002579}
2580
buzbeefead2932012-03-30 14:02:01 -07002581/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002582void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002583{
buzbee408ad162012-06-06 16:45:18 -07002584 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002585 opUnconditionalBranch(cUnit, target);
2586 return;
2587 }
2588 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002589 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002590 opUnconditionalBranch(cUnit, target);
2591 } else {
buzbeefead2932012-03-30 14:02:01 -07002592#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002593 // In non-debug case, only check periodically
2594 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2595 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002596#elif defined(TARGET_X86)
Ian Rogers474b6da2012-09-25 00:20:38 -07002597 newLIR2(cUnit, kX86Cmp16TI8, Thread::ThreadFlagsOffset().Int32Value(), 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002598 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002599#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002600 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2601 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002602#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002603 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002604 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002605 oatFlushAllRegs(cUnit);
2606 opUnconditionalBranch(cUnit, launchPad);
2607 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2608 (intptr_t)launchPad);
2609 }
buzbeefead2932012-03-30 14:02:01 -07002610}
2611
buzbee31a4a6f2012-02-28 15:36:15 -08002612} // namespace art