blob: e9c348a3f2973bd3f9f9fa884d896a80b0625023 [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 */
buzbeefc9e6fa2012-03-23 15:14:29 -070026void genInvoke(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
Bill Buzbeea114add2012-05-03 15:00:40 -070027 InvokeType type, bool isRange);
buzbee31a4a6f2012-02-28 15:36:15 -080028#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080029LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbeef3aac972012-04-11 16:33:36 -070030bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
31 RegLocation rlSrc, RegLocation rlDest, int lit);
buzbee31a4a6f2012-02-28 15:36:15 -080032#endif
33
Ian Rogersab2b55d2012-03-18 00:06:11 -070034void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0) {
35#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070036 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070037#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070038 loadConstant(cUnit, rARG0, arg0);
39 oatClobberCalleeSave(cUnit);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070040#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070041 opReg(cUnit, kOpBlx, rTgt);
42 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070043#else
Bill Buzbeea114add2012-05-03 15:00:40 -070044 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070045#endif
46}
47
Ian Rogers7caad772012-03-30 01:07:54 -070048void callRuntimeHelperReg(CompilationUnit* cUnit, int helperOffset, int arg0) {
49#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070050 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070051#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070052 opRegCopy(cUnit, rARG0, arg0);
53 oatClobberCalleeSave(cUnit);
Ian Rogers7caad772012-03-30 01:07:54 -070054#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070055 opReg(cUnit, kOpBlx, rTgt);
56 oatFreeTemp(cUnit, rTgt);
Ian Rogers7caad772012-03-30 01:07:54 -070057#else
Bill Buzbeea114add2012-05-03 15:00:40 -070058 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070059#endif
60}
61
Ian Rogersab2b55d2012-03-18 00:06:11 -070062void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset,
63 RegLocation arg0) {
64#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070065 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070066#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070067 if (arg0.wide == 0) {
68 loadValueDirectFixed(cUnit, arg0, rARG0);
69 } else {
70 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
71 }
72 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -070073#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070074 opReg(cUnit, kOpBlx, rTgt);
75 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -070076#else
Bill Buzbeea114add2012-05-03 15:00:40 -070077 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070078#endif
79}
80
81void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset,
82 int arg0, int arg1) {
83#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070084 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070085#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070086 loadConstant(cUnit, rARG0, arg0);
87 loadConstant(cUnit, rARG1, arg1);
88 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -070089#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070090 opReg(cUnit, kOpBlx, rTgt);
91 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -070092#else
Bill Buzbeea114add2012-05-03 15:00:40 -070093 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070094#endif
95}
96
97void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset,
98 int arg0, RegLocation arg1) {
99#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700101#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 if (arg1.wide == 0) {
103 loadValueDirectFixed(cUnit, arg1, rARG1);
104 } else {
105 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
106 }
107 loadConstant(cUnit, rARG0, arg0);
108 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700109#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 opReg(cUnit, kOpBlx, rTgt);
111 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700112#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700114#endif
115}
116
117void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset,
118 RegLocation arg0, int arg1) {
119#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700121#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700122 loadValueDirectFixed(cUnit, arg0, rARG0);
123 loadConstant(cUnit, rARG1, arg1);
124 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700125#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 opReg(cUnit, kOpBlx, rTgt);
127 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700128#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700130#endif
131}
132
133void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset,
134 int arg0, int arg1) {
135#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700137#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700138 opRegCopy(cUnit, rARG1, arg1);
139 loadConstant(cUnit, rARG0, arg0);
140 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700141#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 opReg(cUnit, kOpBlx, rTgt);
143 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700144#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700146#endif
147}
148
149void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset,
150 int arg0, int arg1) {
151#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700153#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 opRegCopy(cUnit, rARG0, arg0);
155 loadConstant(cUnit, rARG1, arg1);
156 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700157#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 opReg(cUnit, kOpBlx, rTgt);
159 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700160#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700162#endif
163}
164
165void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset,
166 int arg0) {
167#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700169#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 loadCurrMethodDirect(cUnit, rARG1);
171 loadConstant(cUnit, rARG0, arg0);
172 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700173#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 opReg(cUnit, kOpBlx, rTgt);
175 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700176#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700178#endif
179}
180
181void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit,
182 int helperOffset,
183 RegLocation arg0,
184 RegLocation arg1) {
185#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700187#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 if (arg0.wide == 0) {
189 loadValueDirectFixed(cUnit, arg0, rARG0);
190 if (arg1.wide == 0) {
191 loadValueDirectFixed(cUnit, arg1, rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700192 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700193 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700194 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 } else {
196 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
197 if (arg1.wide == 0) {
198 loadValueDirectFixed(cUnit, arg1, rARG2);
199 } else {
200 loadValueDirectWideFixed(cUnit, arg1, rARG2, rARG3);
201 }
202 }
203 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700204#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 opReg(cUnit, kOpBlx, rTgt);
206 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700207#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700209#endif
210}
211
212void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
213 int arg0, int arg1) {
214#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700216#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700217 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
218 opRegCopy(cUnit, rARG0, arg0);
219 opRegCopy(cUnit, rARG1, arg1);
220 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700221#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 opReg(cUnit, kOpBlx, rTgt);
223 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700224#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700226#endif
227}
228
229void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset,
230 int arg0, int arg1, int arg2) {
231#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700233#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
235 opRegCopy(cUnit, rARG0, arg0);
236 opRegCopy(cUnit, rARG1, arg1);
237 loadConstant(cUnit, rARG2, arg2);
238 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700239#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 opReg(cUnit, kOpBlx, rTgt);
241 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700242#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700244#endif
245}
246
Bill Buzbeea114add2012-05-03 15:00:40 -0700247void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit,
248 int helperOffset,
249 int arg0, RegLocation arg2) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700250#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700252#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 loadValueDirectFixed(cUnit, arg2, rARG2);
254 loadCurrMethodDirect(cUnit, rARG1);
255 loadConstant(cUnit, rARG0, arg0);
256 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700257#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 opReg(cUnit, kOpBlx, rTgt);
259 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700260#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700262#endif
263}
264
265void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset,
266 int arg0, int arg2) {
267#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 loadCurrMethodDirect(cUnit, rARG1);
271 loadConstant(cUnit, rARG2, arg2);
272 loadConstant(cUnit, rARG0, arg0);
273 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700274#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 opReg(cUnit, kOpBlx, rTgt);
276 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700277#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700279#endif
280}
281
282void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit,
283 int helperOffset,
284 int arg0, RegLocation arg1,
285 RegLocation arg2) {
286#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700288#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 loadValueDirectFixed(cUnit, arg1, rARG1);
290 if (arg2.wide == 0) {
291 loadValueDirectFixed(cUnit, arg2, rARG2);
292 } else {
293 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
294 }
295 loadConstant(cUnit, rARG0, arg0);
296 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700297#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 opReg(cUnit, kOpBlx, rTgt);
299 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700300#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700302#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800303}
304
305/*
306 * Generate an kPseudoBarrier marker to indicate the boundary of special
307 * blocks.
308 */
309void genBarrier(CompilationUnit* cUnit)
310{
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
312 /* Mark all resources as being clobbered */
313 barrier->defMask = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800314}
315
buzbee31a4a6f2012-02-28 15:36:15 -0800316
317/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800318LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800319{
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
321 branch->target = (LIR*) target;
322 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800323}
324
buzbee5de34942012-03-01 14:51:57 -0800325// FIXME: need to do some work to split out targets with
326// condition codes and those without
327#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800328LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode, MIR* mir,
329 ThrowKind kind)
330{
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
332 mir ? mir->offset : 0);
333 LIR* branch = opCondBranch(cUnit, cCode, tgt);
334 // Remember branch target - will process later
335 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
336 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800337}
buzbee5de34942012-03-01 14:51:57 -0800338#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800339
340LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
341 int reg, int immVal, MIR* mir, ThrowKind kind)
342{
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind, mir->offset);
344 LIR* branch;
345 if (cCode == kCondAl) {
346 branch = opUnconditionalBranch(cUnit, tgt);
347 } else {
348 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
349 }
350 // Remember branch target - will process later
351 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
352 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800353}
354
355/* Perform null-check on a register. */
356LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, MIR* mir)
357{
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
359 mir->optimizationFlags & MIR_IGNORE_NULL_CHECK) {
360 return NULL;
361 }
362 return genImmedCheck(cUnit, kCondEq, mReg, 0, mir, kThrowNullPointer);
buzbee31a4a6f2012-02-28 15:36:15 -0800363}
364
365/* Perform check on two registers */
366LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 int reg1, int reg2, MIR* mir, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800368{
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
370 mir ? mir->offset : 0, reg1, reg2);
buzbee5de34942012-03-01 14:51:57 -0800371#if defined(TARGET_MIPS)
Bill Buzbeea114add2012-05-03 15:00:40 -0700372 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800373#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 opRegReg(cUnit, kOpCmp, reg1, reg2);
375 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800376#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 // Remember branch target - will process later
378 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
379 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800380}
381
382void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
383 RegLocation rlSrc1, RegLocation rlSrc2, LIR* labelList)
384{
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 ConditionCode cond;
386 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
387 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
388 Instruction::Code opcode = mir->dalvikInsn.opcode;
389 switch (opcode) {
390 case Instruction::IF_EQ:
391 cond = kCondEq;
392 break;
393 case Instruction::IF_NE:
394 cond = kCondNe;
395 break;
396 case Instruction::IF_LT:
397 cond = kCondLt;
398 break;
399 case Instruction::IF_GE:
400 cond = kCondGe;
401 break;
402 case Instruction::IF_GT:
403 cond = kCondGt;
404 break;
405 case Instruction::IF_LE:
406 cond = kCondLe;
407 break;
408 default:
409 cond = (ConditionCode)0;
410 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
411 }
buzbee5de34942012-03-01 14:51:57 -0800412#if defined(TARGET_MIPS)
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg,
414 &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800415#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700416 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
417 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800418#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800420}
421
422void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
423 RegLocation rlSrc, LIR* labelList)
424{
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 ConditionCode cond;
426 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
427 Instruction::Code opcode = mir->dalvikInsn.opcode;
428 switch (opcode) {
429 case Instruction::IF_EQZ:
430 cond = kCondEq;
431 break;
432 case Instruction::IF_NEZ:
433 cond = kCondNe;
434 break;
435 case Instruction::IF_LTZ:
436 cond = kCondLt;
437 break;
438 case Instruction::IF_GEZ:
439 cond = kCondGe;
440 break;
441 case Instruction::IF_GTZ:
442 cond = kCondGt;
443 break;
444 case Instruction::IF_LEZ:
445 cond = kCondLe;
446 break;
447 default:
448 cond = (ConditionCode)0;
449 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
450 }
Ian Rogers7caad772012-03-30 01:07:54 -0700451#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800453#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
455 opCondBranch(cUnit, cond, &labelList[bb->taken->id]);
buzbee5de34942012-03-01 14:51:57 -0800456#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 opUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
buzbee31a4a6f2012-02-28 15:36:15 -0800458}
459
460void genIntToLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
461 RegLocation rlSrc)
462{
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
464 if (rlSrc.location == kLocPhysReg) {
465 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
466 } else {
467 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
468 }
469 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
470 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800471}
472
473void genIntNarrowing(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
474 RegLocation rlSrc)
475{
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
477 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
478 OpKind op = kOpInvalid;
479 switch (mir->dalvikInsn.opcode) {
480 case Instruction::INT_TO_BYTE:
481 op = kOp2Byte;
482 break;
483 case Instruction::INT_TO_SHORT:
484 op = kOp2Short;
485 break;
486 case Instruction::INT_TO_CHAR:
487 op = kOp2Char;
488 break;
489 default:
490 LOG(ERROR) << "Bad int conversion type";
491 }
492 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
493 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800494}
495
496/*
497 * Let helper function take care of everything. Will call
498 * Array::AllocFromCode(type_idx, method, count);
499 * Note: AllocFromCode will handle checks for errNegativeArraySize.
500 */
501void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
502 RegLocation rlSrc)
503{
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 oatFlushAllRegs(cUnit); /* Everything to home location */
505 uint32_t type_idx = mir->dalvikInsn.vC;
506 int funcOffset;
507 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
508 cUnit->dex_cache,
509 *cUnit->dex_file,
510 type_idx)) {
511 funcOffset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
512 } else {
513 funcOffset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
514 }
515 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
516 RegLocation rlResult = oatGetReturn(cUnit, false);
517 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800518}
519
520/*
521 * Similar to genNewArray, but with post-allocation initialization.
522 * Verifier guarantees we're dealing with an array class. Current
523 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
524 * Current code also throws internal unimp if not 'L', '[' or 'I'.
525 */
526void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
527{
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 DecodedInstruction* dInsn = &mir->dalvikInsn;
529 int elems = dInsn->vA;
530 int typeIdx = dInsn->vB;
531 oatFlushAllRegs(cUnit); /* Everything to home location */
532 int funcOffset;
533 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
534 cUnit->dex_cache,
535 *cUnit->dex_file,
536 typeIdx)) {
537 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
538 } else {
539 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
540 }
541 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
542 oatFreeTemp(cUnit, rARG2);
543 oatFreeTemp(cUnit, rARG1);
544 /*
545 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
546 * return region. Because AllocFromCode placed the new array
547 * in rRET0, we'll just lock it into place. When debugger support is
548 * added, it may be necessary to additionally copy all return
549 * values to a home location in thread-local storage
550 */
551 oatLockTemp(cUnit, rRET0);
552
553 // TODO: use the correct component size, currently all supported types
554 // share array alignment with ints (see comment at head of function)
555 size_t component_size = sizeof(int32_t);
556
557 // Having a range of 0 is legal
558 if (isRange && (dInsn->vA > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800559 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 * Bit of ugliness here. We're going generate a mem copy loop
561 * on the register range, but it is possible that some regs
562 * in the range have been promoted. This is unlikely, but
563 * before generating the copy, we'll just force a flush
564 * of any regs in the source range that have been promoted to
565 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800566 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700567 for (unsigned int i = 0; i < dInsn->vA; i++) {
568 RegLocation loc = oatUpdateLoc(cUnit, oatGetSrc(cUnit, mir, i));
569 if (loc.location == kLocPhysReg) {
570 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
571 loc.lowReg, kWord);
572 }
buzbee31a4a6f2012-02-28 15:36:15 -0800573 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 /*
575 * TUNING note: generated code here could be much improved, but
576 * this is an uncommon operation and isn't especially performance
577 * critical.
578 */
579 int rSrc = oatAllocTemp(cUnit);
580 int rDst = oatAllocTemp(cUnit);
581 int rIdx = oatAllocTemp(cUnit);
582#if defined(TARGET_ARM)
583 int rVal = rLR; // Using a lot of temps, rLR is known free here
584#elif defined(TARGET_X86)
jeffhao5772bab2012-05-18 11:51:26 -0700585 oatFreeTemp(cUnit, rRET0);
586 int rVal = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700587#else
588 int rVal = oatAllocTemp(cUnit);
589#endif
590 // Set up source pointer
591 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
592 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
593 oatSRegOffset(cUnit, rlFirst.sRegLow));
594 // Set up the target pointer
595 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
596 Array::DataOffset(component_size).Int32Value());
597 // Set up the loop counter (known to be > 0)
598 loadConstant(cUnit, rIdx, dInsn->vA - 1);
599 // Generate the copy loop. Going backwards for convenience
600 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
601 // Copy next element
602 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
603 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
604#if defined(TARGET_ARM)
605 // Combine sub & test using sub setflags encoding here
606 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
607 opCondBranch(cUnit, kCondGe, target);
608#else
609 oatFreeTemp(cUnit, rVal);
610 opRegImm(cUnit, kOpSub, rIdx, 1);
611 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
612#endif
jeffhao5772bab2012-05-18 11:51:26 -0700613#if defined(TARGET_X86)
614 // Restore the target pointer
615 opRegRegImm(cUnit, kOpAdd, rRET0, rDst,
616 -Array::DataOffset(component_size).Int32Value());
617#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 } else if (!isRange) {
619 // TUNING: interleave
620 for (unsigned int i = 0; i < dInsn->vA; i++) {
621 RegLocation rlArg = loadValue(cUnit, oatGetSrc(cUnit, mir, i), kCoreReg);
622 storeBaseDisp(cUnit, rRET0,
623 Array::DataOffset(component_size).Int32Value() +
624 i * 4, rlArg.lowReg, kWord);
625 // If the loadValue caused a temp to be allocated, free it
626 if (oatIsTemp(cUnit, rlArg.lowReg)) {
627 oatFreeTemp(cUnit, rlArg.lowReg);
628 }
629 }
630 }
buzbee31a4a6f2012-02-28 15:36:15 -0800631}
632
633void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700634 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800635{
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 int fieldOffset;
637 int ssbIndex;
638 bool isVolatile;
639 bool isReferrersClass;
640 uint32_t fieldIdx = mir->dalvikInsn.vB;
buzbee31a4a6f2012-02-28 15:36:15 -0800641
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
643 *cUnit->dex_file, *cUnit->dex_cache,
644 cUnit->code_item, cUnit->method_idx,
645 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800646
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 bool fastPath =
648 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
649 fieldOffset, ssbIndex,
650 isReferrersClass, isVolatile,
651 true);
652 if (fastPath && !SLOW_FIELD_PATH) {
653 DCHECK_GE(fieldOffset, 0);
654 int rBase;
655 if (isReferrersClass) {
656 // Fast path, static storage base is this method's class
657 RegLocation rlMethod = loadCurrMethod(cUnit);
658 rBase = oatAllocTemp(cUnit);
659 loadWordDisp(cUnit, rlMethod.lowReg,
660 Method::DeclaringClassOffset().Int32Value(), rBase);
661 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
662 oatFreeTemp(cUnit, rlMethod.lowReg);
663 }
buzbee31a4a6f2012-02-28 15:36:15 -0800664 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 // Medium path, static storage base in a different class which
666 // requires checks that the other class is initialized.
667 DCHECK_GE(ssbIndex, 0);
668 // May do runtime call so everything to home locations.
669 oatFlushAllRegs(cUnit);
670 // Using fixed register to sync with possible call to runtime
671 // support.
672 int rMethod = rARG1;
673 oatLockTemp(cUnit, rMethod);
674 loadCurrMethodDirect(cUnit, rMethod);
675 rBase = rARG0;
676 oatLockTemp(cUnit, rBase);
677 loadWordDisp(cUnit, rMethod,
678 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
679 rBase);
680 loadWordDisp(cUnit, rBase,
681 Array::DataOffset(sizeof(Object*)).Int32Value() +
682 sizeof(int32_t*) * ssbIndex, rBase);
683 // rBase now points at appropriate static storage base (Class*)
684 // or NULL if not initialized. Check for NULL and call helper if NULL.
685 // TUNING: fast path should fall through
686 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
687 loadConstant(cUnit, rARG0, ssbIndex);
688 callRuntimeHelperImm(cUnit,
689 ENTRYPOINT_OFFSET(pInitializeStaticStorage),
690 ssbIndex);
691#if defined(TARGET_MIPS)
692 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
693 opRegCopy(cUnit, rBase, rRET0);
694#endif
695 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
696 branchOver->target = (LIR*)skipTarget;
697 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800698 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 // rBase now holds static storage base
700 if (isLongOrDouble) {
701 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
702 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
703 } else {
704 rlSrc = oatGetSrc(cUnit, mir, 0);
705 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
706 }
707//FIXME: need to generalize the barrier call
708 if (isVolatile) {
709 oatGenMemBarrier(cUnit, kST);
710 }
711 if (isLongOrDouble) {
712 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
713 rlSrc.highReg);
714 } else {
715 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
716 }
717 if (isVolatile) {
718 oatGenMemBarrier(cUnit, kSY);
719 }
720 if (isObject) {
721 markGCCard(cUnit, rlSrc.lowReg, rBase);
722 }
723 oatFreeTemp(cUnit, rBase);
724 } else {
725 oatFlushAllRegs(cUnit); // Everything to home locations
726 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Static) :
727 (isObject ? ENTRYPOINT_OFFSET(pSetObjStatic)
728 : ENTRYPOINT_OFFSET(pSet32Static));
729 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
730 }
buzbee31a4a6f2012-02-28 15:36:15 -0800731}
732
733void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700734 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800735{
Bill Buzbeea114add2012-05-03 15:00:40 -0700736 int fieldOffset;
737 int ssbIndex;
738 bool isVolatile;
739 bool isReferrersClass;
740 uint32_t fieldIdx = mir->dalvikInsn.vB;
buzbee31a4a6f2012-02-28 15:36:15 -0800741
Bill Buzbeea114add2012-05-03 15:00:40 -0700742 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
743 *cUnit->dex_file, *cUnit->dex_cache,
744 cUnit->code_item, cUnit->method_idx,
745 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800746
Bill Buzbeea114add2012-05-03 15:00:40 -0700747 bool fastPath =
748 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
749 fieldOffset, ssbIndex,
750 isReferrersClass, isVolatile,
751 false);
752 if (fastPath && !SLOW_FIELD_PATH) {
753 DCHECK_GE(fieldOffset, 0);
754 int rBase;
755 if (isReferrersClass) {
756 // Fast path, static storage base is this method's class
757 RegLocation rlMethod = loadCurrMethod(cUnit);
758 rBase = oatAllocTemp(cUnit);
759 loadWordDisp(cUnit, rlMethod.lowReg,
760 Method::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800761 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700762 // Medium path, static storage base in a different class which
763 // requires checks that the other class is initialized
764 DCHECK_GE(ssbIndex, 0);
765 // May do runtime call so everything to home locations.
766 oatFlushAllRegs(cUnit);
767 // Using fixed register to sync with possible call to runtime
768 // support
769 int rMethod = rARG1;
770 oatLockTemp(cUnit, rMethod);
771 loadCurrMethodDirect(cUnit, rMethod);
772 rBase = rARG0;
773 oatLockTemp(cUnit, rBase);
774 loadWordDisp(cUnit, rMethod,
775 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
776 rBase);
777 loadWordDisp(cUnit, rBase,
778 Array::DataOffset(sizeof(Object*)).Int32Value() +
779 sizeof(int32_t*) * ssbIndex, rBase);
780 // rBase now points at appropriate static storage base (Class*)
781 // or NULL if not initialized. Check for NULL and call helper if NULL.
782 // TUNING: fast path should fall through
783 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
784 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage),
785 ssbIndex);
786#if defined(TARGET_MIPS)
787 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
788 opRegCopy(cUnit, rBase, rRET0);
789#endif
790 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
791 branchOver->target = (LIR*)skipTarget;
792 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800793 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700794 // rBase now holds static storage base
795 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
796 : oatGetDest(cUnit, mir, 0);
797 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
798 if (isVolatile) {
799 oatGenMemBarrier(cUnit, kSY);
800 }
801 if (isLongOrDouble) {
802 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
803 rlResult.highReg, INVALID_SREG);
804 } else {
805 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
806 }
807 oatFreeTemp(cUnit, rBase);
808 if (isLongOrDouble) {
809 storeValueWide(cUnit, rlDest, rlResult);
810 } else {
811 storeValue(cUnit, rlDest, rlResult);
812 }
813 } else {
814 oatFlushAllRegs(cUnit); // Everything to home locations
815 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Static) :
816 (isObject ? ENTRYPOINT_OFFSET(pGetObjStatic)
817 : ENTRYPOINT_OFFSET(pGet32Static));
818 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
819 if (isLongOrDouble) {
820 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
821 storeValueWide(cUnit, rlDest, rlResult);
822 } else {
823 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
824 storeValue(cUnit, rlDest, rlResult);
825 }
826 }
buzbee31a4a6f2012-02-28 15:36:15 -0800827}
828
829
830// Debugging routine - if null target, branch to DebugMe
831void genShowTarget(CompilationUnit* cUnit)
832{
buzbeea7678db2012-03-05 15:35:46 -0800833#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 UNIMPLEMENTED(WARNING) << "genShowTarget";
buzbeea7678db2012-03-05 15:35:46 -0800835#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700836 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
837 loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pDebugMe), rINVOKE_TGT);
838 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
839 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800840#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800841}
842
843void genThrowVerificationError(CompilationUnit* cUnit, MIR* mir)
844{
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 callRuntimeHelperImmImm(cUnit,
846 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode),
847 mir->dalvikInsn.vA, mir->dalvikInsn.vB);
buzbee31a4a6f2012-02-28 15:36:15 -0800848}
849
850void handleSuspendLaunchpads(CompilationUnit *cUnit)
851{
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
853 int numElems = cUnit->suspendLaunchpads.numUsed;
854 for (int i = 0; i < numElems; i++) {
855 oatResetRegPool(cUnit);
856 oatResetDefTracking(cUnit);
857 LIR* lab = suspendLabel[i];
858 LIR* resumeLab = (LIR*)lab->operands[0];
859 cUnit->currentDalvikOffset = lab->operands[1];
860 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700861#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700862 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700863#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700864 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
865 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700866#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700867 opUnconditionalBranch(cUnit, resumeLab);
868 }
buzbee31a4a6f2012-02-28 15:36:15 -0800869}
870
buzbeefc9e6fa2012-03-23 15:14:29 -0700871void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
872{
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
874 int numElems = cUnit->intrinsicLaunchpads.numUsed;
875 for (int i = 0; i < numElems; i++) {
876 oatResetRegPool(cUnit);
877 oatResetDefTracking(cUnit);
878 LIR* lab = intrinsicLabel[i];
879 MIR* mir = (MIR*)lab->operands[0];
880 InvokeType type = (InvokeType)lab->operands[1];
881 BasicBlock* bb = (BasicBlock*)lab->operands[3];
882 cUnit->currentDalvikOffset = mir->offset;
883 oatAppendLIR(cUnit, lab);
884 genInvoke(cUnit, bb, mir, type, false /* isRange */);
885 LIR* resumeLab = (LIR*)lab->operands[2];
886 if (resumeLab != NULL) {
887 opUnconditionalBranch(cUnit, resumeLab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700888 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700889 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700890}
891
buzbee31a4a6f2012-02-28 15:36:15 -0800892void handleThrowLaunchpads(CompilationUnit *cUnit)
893{
Bill Buzbeea114add2012-05-03 15:00:40 -0700894 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
895 int numElems = cUnit->throwLaunchpads.numUsed;
896 for (int i = 0; i < numElems; i++) {
897 oatResetRegPool(cUnit);
898 oatResetDefTracking(cUnit);
899 LIR* lab = throwLabel[i];
900 cUnit->currentDalvikOffset = lab->operands[1];
901 oatAppendLIR(cUnit, lab);
902 int funcOffset = 0;
903 int v1 = lab->operands[2];
904 int v2 = lab->operands[3];
905 switch (lab->operands[0]) {
906 case kThrowNullPointer:
907 funcOffset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
908 break;
909 case kThrowArrayBounds:
910 if (v2 != rARG0) {
911 opRegCopy(cUnit, rARG0, v1);
912 opRegCopy(cUnit, rARG1, v2);
913 } else {
914 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800915#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700916 int rTmp = r12;
buzbee31a4a6f2012-02-28 15:36:15 -0800917#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700918 int rTmp = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800919#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700920 opRegCopy(cUnit, rTmp, v1);
921 opRegCopy(cUnit, rARG1, v2);
922 opRegCopy(cUnit, rARG0, rTmp);
923 } else {
924 opRegCopy(cUnit, rARG1, v2);
925 opRegCopy(cUnit, rARG0, v1);
926 }
buzbee31a4a6f2012-02-28 15:36:15 -0800927 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700928 funcOffset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
929 break;
930 case kThrowDivZero:
931 funcOffset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
932 break;
933 case kThrowVerificationError:
934 loadConstant(cUnit, rARG0, v1);
935 loadConstant(cUnit, rARG1, v2);
936 funcOffset =
937 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode);
938 break;
939 case kThrowNoSuchMethod:
940 opRegCopy(cUnit, rARG0, v1);
941 funcOffset =
942 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
943 break;
944 case kThrowStackOverflow:
945 funcOffset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
946 // Restore stack alignment
Ian Rogersab2b55d2012-03-18 00:06:11 -0700947#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 opRegImm(cUnit, kOpAdd, rSP,
949 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700950#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700951 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700952#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700953 break;
954 default:
955 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800956 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 oatClobberCalleeSave(cUnit);
958#if !defined(TARGET_X86)
959 int rTgt = loadHelper(cUnit, funcOffset);
960 opReg(cUnit, kOpBlx, rTgt);
961 oatFreeTemp(cUnit, rTgt);
962#else
963 opThreadMem(cUnit, kOpBlx, funcOffset);
964#endif
965 }
buzbee31a4a6f2012-02-28 15:36:15 -0800966}
967
968/* Needed by the Assembler */
969void oatSetupResourceMasks(LIR* lir)
970{
Bill Buzbeea114add2012-05-03 15:00:40 -0700971 setupResourceMasks(lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800972}
973
buzbee16da88c2012-03-20 10:38:17 -0700974bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
975 int& fieldOffset, bool& isVolatile, bool isPut)
976{
Bill Buzbeea114add2012-05-03 15:00:40 -0700977 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
978 *cUnit->dex_file, *cUnit->dex_cache,
979 cUnit->code_item, cUnit->method_idx,
980 cUnit->access_flags);
981 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
982 fieldOffset, isVolatile, isPut);
buzbee16da88c2012-03-20 10:38:17 -0700983}
984
buzbee31a4a6f2012-02-28 15:36:15 -0800985void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
986 RegLocation rlDest, RegLocation rlObj,
Bill Buzbeea114add2012-05-03 15:00:40 -0700987 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800988{
Bill Buzbeea114add2012-05-03 15:00:40 -0700989 int fieldOffset;
990 bool isVolatile;
991 uint32_t fieldIdx = mir->dalvikInsn.vC;
buzbee31a4a6f2012-02-28 15:36:15 -0800992
Bill Buzbeea114add2012-05-03 15:00:40 -0700993 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800994
Bill Buzbeea114add2012-05-03 15:00:40 -0700995 if (fastPath && !SLOW_FIELD_PATH) {
996 RegLocation rlResult;
997 RegisterClass regClass = oatRegClassBySize(size);
998 DCHECK_GE(fieldOffset, 0);
999 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1000 if (isLongOrDouble) {
1001 DCHECK(rlDest.wide);
1002 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
Ian Rogersb5d09b22012-03-06 22:14:17 -08001003#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001004 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1005 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1006 loadBaseDispWide(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
1007 rlResult.highReg, rlObj.sRegLow);
1008 if (isVolatile) {
1009 oatGenMemBarrier(cUnit, kSY);
1010 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001011#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001012 int regPtr = oatAllocTemp(cUnit);
1013 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1014 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1015 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1016 if (isVolatile) {
1017 oatGenMemBarrier(cUnit, kSY);
1018 }
1019 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001020#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001021 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001022 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001023 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1024 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1025 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
1026 kWord, rlObj.sRegLow);
1027 if (isVolatile) {
1028 oatGenMemBarrier(cUnit, kSY);
1029 }
1030 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001031 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001032 } else {
1033 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Instance) :
1034 (isObject ? ENTRYPOINT_OFFSET(pGetObjInstance)
1035 : ENTRYPOINT_OFFSET(pGet32Instance));
1036 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
1037 if (isLongOrDouble) {
1038 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
1039 storeValueWide(cUnit, rlDest, rlResult);
1040 } else {
1041 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
1042 storeValue(cUnit, rlDest, rlResult);
1043 }
1044 }
buzbee31a4a6f2012-02-28 15:36:15 -08001045}
1046
1047void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -07001048 RegLocation rlObj, bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -08001049{
Bill Buzbeea114add2012-05-03 15:00:40 -07001050 int fieldOffset;
1051 bool isVolatile;
1052 uint32_t fieldIdx = mir->dalvikInsn.vC;
buzbee31a4a6f2012-02-28 15:36:15 -08001053
Bill Buzbeea114add2012-05-03 15:00:40 -07001054 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1055 true);
1056 if (fastPath && !SLOW_FIELD_PATH) {
1057 RegisterClass regClass = oatRegClassBySize(size);
1058 DCHECK_GE(fieldOffset, 0);
1059 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1060 if (isLongOrDouble) {
1061 int regPtr;
1062 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1063 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1064 regPtr = oatAllocTemp(cUnit);
1065 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1066 if (isVolatile) {
1067 oatGenMemBarrier(cUnit, kST);
1068 }
jeffhao41005dd2012-05-09 17:58:52 -07001069 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001070 if (isVolatile) {
1071 oatGenMemBarrier(cUnit, kSY);
1072 }
1073 oatFreeTemp(cUnit, regPtr);
buzbee31a4a6f2012-02-28 15:36:15 -08001074 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001075 rlSrc = loadValue(cUnit, rlSrc, regClass);
1076 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null? */
1077 if (isVolatile) {
1078 oatGenMemBarrier(cUnit, kST);
1079 }
1080 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1081 if (isVolatile) {
1082 oatGenMemBarrier(cUnit, kSY);
1083 }
1084 if (isObject) {
1085 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1086 }
buzbee31a4a6f2012-02-28 15:36:15 -08001087 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001088 } else {
1089 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Instance) :
1090 (isObject ? ENTRYPOINT_OFFSET(pSetObjInstance)
1091 : ENTRYPOINT_OFFSET(pSet32Instance));
1092 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1093 fieldIdx, rlObj, rlSrc);
1094 }
buzbee31a4a6f2012-02-28 15:36:15 -08001095}
1096
1097void genConstClass(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1098 RegLocation rlSrc)
1099{
Bill Buzbeea114add2012-05-03 15:00:40 -07001100 uint32_t type_idx = mir->dalvikInsn.vB;
1101 RegLocation rlMethod = loadCurrMethod(cUnit);
1102 int resReg = oatAllocTemp(cUnit);
1103 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1104 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1105 cUnit->dex_cache,
1106 *cUnit->dex_file,
1107 type_idx)) {
1108 // Call out to helper which resolves type and verifies access.
1109 // Resolved type returned in rRET0.
1110 callRuntimeHelperImmReg(cUnit,
1111 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1112 type_idx, rlMethod.lowReg);
1113 RegLocation rlResult = oatGetReturn(cUnit, false);
1114 storeValue(cUnit, rlDest, rlResult);
1115 } else {
1116 // We're don't need access checks, load type from dex cache
1117 int32_t dex_cache_offset =
1118 Method::DexCacheResolvedTypesOffset().Int32Value();
1119 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
1120 int32_t offset_of_type =
1121 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1122 * type_idx);
1123 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1124 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1125 type_idx) || SLOW_TYPE_PATH) {
1126 // Slow path, at runtime test if type is null and if so initialize
1127 oatFlushAllRegs(cUnit);
1128 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, NULL);
1129 // Resolved, store and hop over following code
1130 storeValue(cUnit, rlDest, rlResult);
1131 /*
1132 * Because we have stores of the target value on two paths,
1133 * clobber temp tracking for the destination using the ssa name
1134 */
1135 oatClobberSReg(cUnit, rlDest.sRegLow);
1136 LIR* branch2 = opUnconditionalBranch(cUnit,0);
1137 // TUNING: move slow path to end & remove unconditional branch
1138 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
1139 // Call out to helper, which will return resolved type in rARG0
1140 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1141 type_idx, rlMethod.lowReg);
1142 RegLocation rlResult = oatGetReturn(cUnit, false);
1143 storeValue(cUnit, rlDest, rlResult);
1144 /*
1145 * Because we have stores of the target value on two paths,
1146 * clobber temp tracking for the destination using the ssa name
1147 */
1148 oatClobberSReg(cUnit, rlDest.sRegLow);
1149 // Rejoin code paths
1150 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
1151 branch1->target = (LIR*)target1;
1152 branch2->target = (LIR*)target2;
buzbee31a4a6f2012-02-28 15:36:15 -08001153 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001154 // Fast path, we're done - just store result
1155 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001156 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001157 }
buzbee31a4a6f2012-02-28 15:36:15 -08001158}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001159
buzbee31a4a6f2012-02-28 15:36:15 -08001160void genConstString(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001161 RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001162{
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 /* NOTE: Most strings should be available at compile time */
1164 uint32_t string_idx = mir->dalvikInsn.vB;
1165 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1166 (sizeof(String*) * string_idx);
1167 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1168 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1169 // slow path, resolve string if not in dex cache
1170 oatFlushAllRegs(cUnit);
1171 oatLockCallTemps(cUnit); // Using explicit registers
1172 loadCurrMethodDirect(cUnit, rARG2);
1173 loadWordDisp(cUnit, rARG2,
1174 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1175 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001176#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001177 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001178#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001179 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1180 loadConstant(cUnit, rARG1, string_idx);
buzbee31a4a6f2012-02-28 15:36:15 -08001181#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001182 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1183 genBarrier(cUnit);
1184 // For testing, always force through helper
1185 if (!EXERCISE_SLOWEST_STRING_PATH) {
1186 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001187 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001188 opRegCopy(cUnit, rARG0, rARG2); // .eq
1189 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
1190 oatFreeTemp(cUnit, rTgt);
1191#elif defined(TARGET_MIPS)
1192 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1193 opRegCopy(cUnit, rARG0, rARG2); // .eq
1194 opReg(cUnit, kOpBlx, rTgt);
1195 oatFreeTemp(cUnit, rTgt);
1196 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1197 branch->target = target;
1198#else
1199 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode),
1200 rARG2, rARG1);
1201#endif
1202 genBarrier(cUnit);
1203 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
1204 } else {
1205 RegLocation rlMethod = loadCurrMethod(cUnit);
1206 int resReg = oatAllocTemp(cUnit);
1207 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1208 loadWordDisp(cUnit, rlMethod.lowReg,
1209 Method::DexCacheStringsOffset().Int32Value(), resReg);
1210 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1211 storeValue(cUnit, rlDest, rlResult);
1212 }
buzbee31a4a6f2012-02-28 15:36:15 -08001213}
1214
1215/*
1216 * Let helper function take care of everything. Will
1217 * call Class::NewInstanceFromCode(type_idx, method);
1218 */
1219void genNewInstance(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest)
1220{
Bill Buzbeea114add2012-05-03 15:00:40 -07001221 oatFlushAllRegs(cUnit); /* Everything to home location */
1222 uint32_t type_idx = mir->dalvikInsn.vB;
1223 // alloc will always check for resolution, do we also need to verify
1224 // access because the verifier was unable to?
1225 int funcOffset;
1226 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1227 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
1228 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
1229 } else {
1230 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
1231 }
1232 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
1233 RegLocation rlResult = oatGetReturn(cUnit, false);
1234 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001235}
1236
Ian Rogersab2b55d2012-03-18 00:06:11 -07001237void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1238{
Bill Buzbeea114add2012-05-03 15:00:40 -07001239 oatFlushAllRegs(cUnit);
1240 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException),
1241 rlSrc);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001242}
1243
buzbee31a4a6f2012-02-28 15:36:15 -08001244void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1245 RegLocation rlSrc)
1246{
Bill Buzbeea114add2012-05-03 15:00:40 -07001247 oatFlushAllRegs(cUnit);
1248 // May generate a call - use explicit registers
1249 oatLockCallTemps(cUnit);
1250 uint32_t type_idx = mir->dalvikInsn.vC;
1251 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1252 int classReg = rARG2; // rARG2 will hold the Class*
1253 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1254 cUnit->dex_cache,
1255 *cUnit->dex_file,
1256 type_idx)) {
1257 // Check we have access to type_idx and if not throw IllegalAccessError,
1258 // returns Class* in rARG0
1259 callRuntimeHelperImm(cUnit,
1260 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1261 type_idx);
1262 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1263 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1264 } else {
1265 // Load dex cache entry into classReg (rARG2)
1266 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1267 loadWordDisp(cUnit, rARG1,
1268 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1269 int32_t offset_of_type =
1270 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1271 * type_idx);
1272 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1273 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1274 cUnit->dex_cache, type_idx)) {
1275 // Need to test presence of type in dex cache at runtime
1276 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1277 // Not resolved
1278 // Call out to helper, which will return resolved type in rRET0
1279 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1280 type_idx);
1281 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1282 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1283 // Rejoin code paths
1284 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1285 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001286 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001287 }
1288 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
1289 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1290 /* load object->klass_ */
1291 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1292 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1293 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001294#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001295 /* Uses conditional nullification */
1296 int rTgt = loadHelper(cUnit,
1297 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1298 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1299 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1300 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1301 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1302 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1303 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001304#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001305 /* Uses branchovers */
1306 loadConstant(cUnit, rARG0, 1); // assume true
1307 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001308#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001309 int rTgt = loadHelper(cUnit,
1310 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1311 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1312 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1313 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001314#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001315 opRegCopy(cUnit, rARG0, rARG2);
1316 opThreadMem(cUnit, kOpBlx,
1317 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001318#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001319#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001320 oatClobberCalleeSave(cUnit);
1321 /* branch targets here */
1322 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1323 RegLocation rlResult = oatGetReturn(cUnit, false);
1324 storeValue(cUnit, rlDest, rlResult);
1325 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001326#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001327 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001328#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001329}
1330
1331void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
1332{
Bill Buzbeea114add2012-05-03 15:00:40 -07001333 oatFlushAllRegs(cUnit);
1334 // May generate a call - use explicit registers
1335 oatLockCallTemps(cUnit);
1336 uint32_t type_idx = mir->dalvikInsn.vB;
1337 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1338 int classReg = rARG2; // rARG2 will hold the Class*
1339 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1340 cUnit->dex_cache,
1341 *cUnit->dex_file,
1342 type_idx)) {
1343 // Check we have access to type_idx and if not throw IllegalAccessError,
1344 // returns Class* in rRET0
1345 // InitializeTypeAndVerifyAccess(idx, method)
1346 callRuntimeHelperImmReg(cUnit,
1347 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1348 type_idx, rARG1);
1349 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1350 } else {
1351 // Load dex cache entry into classReg (rARG2)
1352 loadWordDisp(cUnit, rARG1,
1353 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1354 int32_t offset_of_type =
1355 Array::DataOffset(sizeof(Class*)).Int32Value() +
1356 (sizeof(Class*) * type_idx);
1357 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1358 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1359 cUnit->dex_cache, type_idx)) {
1360 // Need to test presence of type in dex cache at runtime
1361 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1362 // Not resolved
1363 // Call out to helper, which will return resolved type in rARG0
1364 // InitializeTypeFromCode(idx, method)
1365 callRuntimeHelperImmReg(cUnit,
1366 ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1367 type_idx, rARG1);
1368 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1369 // Rejoin code paths
1370 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1371 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001372 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001373 }
1374 // At this point, classReg (rARG2) has class
1375 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1376 /* Null is OK - continue */
1377 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1378 /* load object->klass_ */
1379 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1380 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1381 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001382#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001383 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
1384 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode),
1385 rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001386#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001387 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1388 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1389 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1390 opRegCopy(cUnit, rARG0, rARG1);
1391 opRegCopy(cUnit, rARG1, rARG2);
1392 oatClobberCalleeSave(cUnit);
1393 opReg(cUnit, kOpBlx, rTgt);
1394 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001395#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001396 /* branch target here */
1397 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1398 branch1->target = target;
1399 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001400}
1401
buzbee31a4a6f2012-02-28 15:36:15 -08001402/*
1403 * Generate array store
1404 *
1405 */
1406void genArrayObjPut(CompilationUnit* cUnit, MIR* mir, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001407 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001408{
Bill Buzbeea114add2012-05-03 15:00:40 -07001409 int lenOffset = Array::LengthOffset().Int32Value();
1410 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001411
Bill Buzbeea114add2012-05-03 15:00:40 -07001412 oatFlushAllRegs(cUnit); // Use explicit registers
1413 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001414
Bill Buzbeea114add2012-05-03 15:00:40 -07001415 int rValue = rARG0; // Register holding value
1416 int rArrayClass = rARG1; // Register holding array's Class
1417 int rArray = rARG2; // Register holding array
1418 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001419
Bill Buzbeea114add2012-05-03 15:00:40 -07001420 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1421 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1422 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001423
Bill Buzbeea114add2012-05-03 15:00:40 -07001424 genNullCheck(cUnit, rlArray.sRegLow, rArray, mir); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001425
Bill Buzbeea114add2012-05-03 15:00:40 -07001426 // Store of null?
1427 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001428
Bill Buzbeea114add2012-05-03 15:00:40 -07001429 // Get the array's class.
1430 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
1431 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode),
1432 rValue, rArrayClass);
1433 // Redo loadValues in case they didn't survive the call.
1434 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1435 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1436 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1437 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001438
Bill Buzbeea114add2012-05-03 15:00:40 -07001439 // Branch here if value to be stored == null
1440 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1441 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001442
Ian Rogersb41b33b2012-03-20 14:22:54 -07001443#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001444 // make an extra temp available for card mark below
1445 oatFreeTemp(cUnit, rARG1);
1446 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1447 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1448 genRegMemCheck(cUnit, kCondUge, rIndex, rArray,
1449 lenOffset, mir, kThrowArrayBounds);
1450 }
1451 storeBaseIndexedDisp(cUnit, NULL, rArray, rIndex, scale,
1452 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001453#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001454 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1455 int regLen = INVALID_REG;
1456 if (needsRangeCheck) {
1457 regLen = rARG1;
1458 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); // Get len
1459 }
1460 /* rPtr -> array data */
1461 int rPtr = oatAllocTemp(cUnit);
1462 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1463 if (needsRangeCheck) {
1464 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, mir,
1465 kThrowArrayBounds);
1466 }
1467 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1468 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001469#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001470 oatFreeTemp(cUnit, rIndex);
1471 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001472}
1473
1474/*
1475 * Generate array load
1476 */
1477void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1478 RegLocation rlArray, RegLocation rlIndex,
1479 RegLocation rlDest, int scale)
1480{
Bill Buzbeea114add2012-05-03 15:00:40 -07001481 RegisterClass regClass = oatRegClassBySize(size);
1482 int lenOffset = Array::LengthOffset().Int32Value();
1483 int dataOffset;
1484 RegLocation rlResult;
1485 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1486 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001487
Bill Buzbeea114add2012-05-03 15:00:40 -07001488 if (size == kLong || size == kDouble) {
1489 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1490 } else {
1491 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1492 }
buzbee31a4a6f2012-02-28 15:36:15 -08001493
Bill Buzbeea114add2012-05-03 15:00:40 -07001494 /* null object? */
1495 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee31a4a6f2012-02-28 15:36:15 -08001496
Ian Rogersb5d09b22012-03-06 22:14:17 -08001497#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001498 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1499 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1500 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1501 lenOffset, mir, kThrowArrayBounds);
1502 }
1503 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001504 int regAddr = oatAllocTemp(cUnit);
1505 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1506 oatFreeTemp(cUnit, rlArray.lowReg);
1507 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001508 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
jeffhao3f9ace82012-05-25 11:25:36 -07001509 loadPair(cUnit, regAddr, rlResult.lowReg, rlResult.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001510 storeValueWide(cUnit, rlDest, rlResult);
1511 } else {
1512 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001513
Bill Buzbeea114add2012-05-03 15:00:40 -07001514 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1515 dataOffset, rlResult.lowReg, INVALID_REG, size,
1516 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001517
Bill Buzbeea114add2012-05-03 15:00:40 -07001518 storeValue(cUnit, rlDest, rlResult);
1519 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001520#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001521 int regPtr = oatAllocTemp(cUnit);
1522 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1523 int regLen = INVALID_REG;
1524 if (needsRangeCheck) {
1525 regLen = oatAllocTemp(cUnit);
1526 /* Get len */
1527 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1528 }
1529 /* regPtr -> array data */
1530 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1531 oatFreeTemp(cUnit, rlArray.lowReg);
1532 if ((size == kLong) || (size == kDouble)) {
1533 if (scale) {
1534 int rNewIndex = oatAllocTemp(cUnit);
1535 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1536 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1537 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001538 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001539 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001540 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001541 oatFreeTemp(cUnit, rlIndex.lowReg);
1542 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1543
1544 if (needsRangeCheck) {
1545 // TODO: change kCondCS to a more meaningful name, is the sense of
1546 // carry-set/clear flipped?
1547 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1548 kThrowArrayBounds);
1549 oatFreeTemp(cUnit, regLen);
1550 }
1551 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1552
1553 oatFreeTemp(cUnit, regPtr);
1554 storeValueWide(cUnit, rlDest, rlResult);
1555 } else {
1556 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1557
1558 if (needsRangeCheck) {
1559 // TODO: change kCondCS to a more meaningful name, is the sense of
1560 // carry-set/clear flipped?
1561 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1562 kThrowArrayBounds);
1563 oatFreeTemp(cUnit, regLen);
1564 }
1565 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1566 scale, size);
1567
1568 oatFreeTemp(cUnit, regPtr);
1569 storeValue(cUnit, rlDest, rlResult);
1570 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001571#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001572}
1573
1574/*
1575 * Generate array store
1576 *
1577 */
1578void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1579 RegLocation rlArray, RegLocation rlIndex,
1580 RegLocation rlSrc, int scale)
1581{
Bill Buzbeea114add2012-05-03 15:00:40 -07001582 RegisterClass regClass = oatRegClassBySize(size);
1583 int lenOffset = Array::LengthOffset().Int32Value();
1584 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001585
Bill Buzbeea114add2012-05-03 15:00:40 -07001586 if (size == kLong || size == kDouble) {
1587 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1588 } else {
1589 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1590 }
buzbee31a4a6f2012-02-28 15:36:15 -08001591
Bill Buzbeea114add2012-05-03 15:00:40 -07001592 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1593 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001594#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001595 int regPtr;
1596 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1597 oatClobber(cUnit, rlArray.lowReg);
1598 regPtr = rlArray.lowReg;
1599 } else {
1600 regPtr = oatAllocTemp(cUnit);
1601 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1602 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001603#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001604
Bill Buzbeea114add2012-05-03 15:00:40 -07001605 /* null object? */
1606 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee31a4a6f2012-02-28 15:36:15 -08001607
Ian Rogersb41b33b2012-03-20 14:22:54 -07001608#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001609 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1610 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1611 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1612 lenOffset, mir, kThrowArrayBounds);
1613 }
1614 if ((size == kLong) || (size == kDouble)) {
1615 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1616 } else {
1617 rlSrc = loadValue(cUnit, rlSrc, regClass);
1618 }
1619 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1620 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1621 INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001622#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001623 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1624 int regLen = INVALID_REG;
1625 if (needsRangeCheck) {
1626 regLen = oatAllocTemp(cUnit);
1627 //NOTE: max live temps(4) here.
1628 /* Get len */
1629 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1630 }
1631 /* regPtr -> array data */
1632 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1633 /* at this point, regPtr points to array, 2 live temps */
1634 if ((size == kLong) || (size == kDouble)) {
1635 //TUNING: specific wide routine that can handle fp regs
1636 if (scale) {
1637 int rNewIndex = oatAllocTemp(cUnit);
1638 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1639 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1640 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001641 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001642 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001643 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001644 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1645
1646 if (needsRangeCheck) {
1647 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1648 kThrowArrayBounds);
1649 oatFreeTemp(cUnit, regLen);
1650 }
1651
jeffhao41005dd2012-05-09 17:58:52 -07001652 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001653
1654 oatFreeTemp(cUnit, regPtr);
1655 } else {
1656 rlSrc = loadValue(cUnit, rlSrc, regClass);
1657 if (needsRangeCheck) {
1658 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1659 kThrowArrayBounds);
1660 oatFreeTemp(cUnit, regLen);
1661 }
1662 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1663 scale, size);
1664 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001665#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001666}
1667
1668void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1669 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001670 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001671{
Bill Buzbeea114add2012-05-03 15:00:40 -07001672 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001673#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001674 /*
1675 * NOTE: This is the one place in the code in which we might have
1676 * as many as six live temporary registers. There are 5 in the normal
1677 * set for Arm. Until we have spill capabilities, temporarily add
1678 * lr to the temp set. It is safe to do this locally, but note that
1679 * lr is used explicitly elsewhere in the code generator and cannot
1680 * normally be used as a general temp register.
1681 */
1682 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1683 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001684#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001685 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1686 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1687 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1688 // The longs may overlap - use intermediate temp if so
1689 if (rlResult.lowReg == rlSrc1.highReg) {
1690 int tReg = oatAllocTemp(cUnit);
1691 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1692 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1693 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1694 oatFreeTemp(cUnit, tReg);
1695 } else {
1696 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1697 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1698 rlSrc2.highReg);
1699 }
1700 /*
1701 * NOTE: If rlDest refers to a frame variable in a large frame, the
1702 * following storeValueWide might need to allocate a temp register.
1703 * To further work around the lack of a spill capability, explicitly
1704 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1705 * Remove when spill is functional.
1706 */
1707 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1708 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1709 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001710#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001711 oatClobber(cUnit, rLR);
1712 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001713#endif
1714}
1715
1716
1717bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1718 RegLocation rlSrc1, RegLocation rlShift)
1719{
Bill Buzbeea114add2012-05-03 15:00:40 -07001720 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001721
Bill Buzbeea114add2012-05-03 15:00:40 -07001722 switch (mir->dalvikInsn.opcode) {
1723 case Instruction::SHL_LONG:
1724 case Instruction::SHL_LONG_2ADDR:
1725 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1726 break;
1727 case Instruction::SHR_LONG:
1728 case Instruction::SHR_LONG_2ADDR:
1729 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1730 break;
1731 case Instruction::USHR_LONG:
1732 case Instruction::USHR_LONG_2ADDR:
1733 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1734 break;
1735 default:
1736 LOG(FATAL) << "Unexpected case";
1737 return true;
1738 }
1739 oatFlushAllRegs(cUnit); /* Send everything to home location */
1740 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
1741 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1742 storeValueWide(cUnit, rlDest, rlResult);
1743 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001744}
1745
1746
1747bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001748 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001749{
Bill Buzbeea114add2012-05-03 15:00:40 -07001750 OpKind op = kOpBkpt;
1751 bool callOut = false;
1752 bool checkZero = false;
1753 bool unary = false;
1754 RegLocation rlResult;
1755 bool shiftOp = false;
1756 int funcOffset;
1757 int retReg = rRET0;
1758 switch (mir->dalvikInsn.opcode) {
1759 case Instruction::NEG_INT:
1760 op = kOpNeg;
1761 unary = true;
1762 break;
1763 case Instruction::NOT_INT:
1764 op = kOpMvn;
1765 unary = true;
1766 break;
1767 case Instruction::ADD_INT:
1768 case Instruction::ADD_INT_2ADDR:
1769 op = kOpAdd;
1770 break;
1771 case Instruction::SUB_INT:
1772 case Instruction::SUB_INT_2ADDR:
1773 op = kOpSub;
1774 break;
1775 case Instruction::MUL_INT:
1776 case Instruction::MUL_INT_2ADDR:
1777 op = kOpMul;
1778 break;
1779 case Instruction::DIV_INT:
1780 case Instruction::DIV_INT_2ADDR:
1781 checkZero = true;
1782 op = kOpDiv;
1783 callOut = true;
1784 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1785 retReg = rRET0;
1786 break;
1787 /* NOTE: returns in rARG1 */
1788 case Instruction::REM_INT:
1789 case Instruction::REM_INT_2ADDR:
1790 checkZero = true;
1791 op = kOpRem;
1792 callOut = true;
1793 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1794 retReg = rRET1;
1795 break;
1796 case Instruction::AND_INT:
1797 case Instruction::AND_INT_2ADDR:
1798 op = kOpAnd;
1799 break;
1800 case Instruction::OR_INT:
1801 case Instruction::OR_INT_2ADDR:
1802 op = kOpOr;
1803 break;
1804 case Instruction::XOR_INT:
1805 case Instruction::XOR_INT_2ADDR:
1806 op = kOpXor;
1807 break;
1808 case Instruction::SHL_INT:
1809 case Instruction::SHL_INT_2ADDR:
1810 shiftOp = true;
1811 op = kOpLsl;
1812 break;
1813 case Instruction::SHR_INT:
1814 case Instruction::SHR_INT_2ADDR:
1815 shiftOp = true;
1816 op = kOpAsr;
1817 break;
1818 case Instruction::USHR_INT:
1819 case Instruction::USHR_INT_2ADDR:
1820 shiftOp = true;
1821 op = kOpLsr;
1822 break;
1823 default:
1824 LOG(FATAL) << "Invalid word arith op: " <<
1825 (int)mir->dalvikInsn.opcode;
1826 }
1827 if (!callOut) {
1828 if (unary) {
1829 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1830 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1831 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001832 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001833 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001834#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001835 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1836 int tReg = oatAllocTemp(cUnit);
1837 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001838#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001839 // X86 doesn't require masking and must use ECX
1840 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1841 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001842#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001843 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1844 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1845 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1846 oatFreeTemp(cUnit, tReg);
1847 } else {
1848 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1849 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1850 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1851 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1852 }
buzbee31a4a6f2012-02-28 15:36:15 -08001853 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001854 storeValue(cUnit, rlDest, rlResult);
1855 } else {
1856 RegLocation rlResult;
1857 oatFlushAllRegs(cUnit); /* Send everything to home location */
1858 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1859#if !defined(TARGET_X86)
1860 int rTgt = loadHelper(cUnit, funcOffset);
1861#endif
1862 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1863 if (checkZero) {
1864 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1865 }
1866#if !defined(TARGET_X86)
1867 opReg(cUnit, kOpBlx, rTgt);
1868 oatFreeTemp(cUnit, rTgt);
1869#else
1870 opThreadMem(cUnit, kOpBlx, funcOffset);
1871#endif
1872 if (retReg == rRET0)
1873 rlResult = oatGetReturn(cUnit, false);
1874 else
1875 rlResult = oatGetReturnAlt(cUnit);
1876 storeValue(cUnit, rlDest, rlResult);
1877 }
1878 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001879}
1880
1881/*
1882 * The following are the first-level codegen routines that analyze the format
1883 * of each bytecode then either dispatch special purpose codegen routines
1884 * or produce corresponding Thumb instructions directly.
1885 */
1886
1887bool isPowerOfTwo(int x)
1888{
Bill Buzbeea114add2012-05-03 15:00:40 -07001889 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001890}
1891
1892// Returns true if no more than two bits are set in 'x'.
1893bool isPopCountLE2(unsigned int x)
1894{
Bill Buzbeea114add2012-05-03 15:00:40 -07001895 x &= x - 1;
1896 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001897}
1898
1899// Returns the index of the lowest set bit in 'x'.
1900int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001901 int bit_posn = 0;
1902 while ((x & 0xf) == 0) {
1903 bit_posn += 4;
1904 x >>= 4;
1905 }
1906 while ((x & 1) == 0) {
1907 bit_posn++;
1908 x >>= 1;
1909 }
1910 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001911}
1912
1913// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1914// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001915bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001916 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001917{
buzbeef3aac972012-04-11 16:33:36 -07001918#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001919 // No divide instruction for Arm, so check for more special cases
1920 if (lit < 2) {
1921 return false;
1922 }
1923 if (!isPowerOfTwo(lit)) {
1924 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1925 }
buzbeef3aac972012-04-11 16:33:36 -07001926#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001927 if (lit < 2 || !isPowerOfTwo(lit)) {
1928 return false;
1929 }
buzbeef3aac972012-04-11 16:33:36 -07001930#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001931 int k = lowestSetBit(lit);
1932 if (k >= 30) {
1933 // Avoid special cases.
1934 return false;
1935 }
1936 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1937 dalvikOpcode == Instruction::DIV_INT_LIT16);
1938 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1939 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1940 if (div) {
1941 int tReg = oatAllocTemp(cUnit);
1942 if (lit == 2) {
1943 // Division by 2 is by far the most common division by constant.
1944 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1945 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1946 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001947 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001948 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1949 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1950 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1951 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001952 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001953 } else {
1954 int tReg1 = oatAllocTemp(cUnit);
1955 int tReg2 = oatAllocTemp(cUnit);
1956 if (lit == 2) {
1957 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1958 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1959 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1960 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1961 } else {
1962 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1963 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1964 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1965 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1966 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1967 }
1968 }
1969 storeValue(cUnit, rlDest, rlResult);
1970 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001971}
1972
1973void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1974 RegLocation rlResult, int lit,
1975 int firstBit, int secondBit)
1976{
buzbee0398c422012-03-02 15:22:47 -08001977#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001978 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1979 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001980#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001981 int tReg = oatAllocTemp(cUnit);
1982 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1983 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1984 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001985#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001986 if (firstBit != 0) {
1987 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1988 }
buzbee31a4a6f2012-02-28 15:36:15 -08001989}
1990
1991// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1992// and store the result in 'rlDest'.
1993bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1994 RegLocation rlDest, int lit)
1995{
Bill Buzbeea114add2012-05-03 15:00:40 -07001996 // Can we simplify this multiplication?
1997 bool powerOfTwo = false;
1998 bool popCountLE2 = false;
1999 bool powerOfTwoMinusOne = false;
2000 if (lit < 2) {
2001 // Avoid special cases.
2002 return false;
2003 } else if (isPowerOfTwo(lit)) {
2004 powerOfTwo = true;
2005 } else if (isPopCountLE2(lit)) {
2006 popCountLE2 = true;
2007 } else if (isPowerOfTwo(lit + 1)) {
2008 powerOfTwoMinusOne = true;
2009 } else {
2010 return false;
2011 }
2012 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2013 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2014 if (powerOfTwo) {
2015 // Shift.
2016 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2017 lowestSetBit(lit));
2018 } else if (popCountLE2) {
2019 // Shift and add and shift.
2020 int firstBit = lowestSetBit(lit);
2021 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2022 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2023 firstBit, secondBit);
2024 } else {
2025 // Reverse subtract: (src << (shift + 1)) - src.
2026 DCHECK(powerOfTwoMinusOne);
2027 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2028 int tReg = oatAllocTemp(cUnit);
2029 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2030 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2031 }
2032 storeValue(cUnit, rlDest, rlResult);
2033 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002034}
2035
2036bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2037 RegLocation rlSrc, int lit)
2038{
Bill Buzbeea114add2012-05-03 15:00:40 -07002039 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
2040 RegLocation rlResult;
2041 OpKind op = (OpKind)0; /* Make gcc happy */
2042 int shiftOp = false;
2043 bool isDiv = false;
2044 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002045
Bill Buzbeea114add2012-05-03 15:00:40 -07002046 switch (dalvikOpcode) {
2047 case Instruction::RSUB_INT_LIT8:
2048 case Instruction::RSUB_INT: {
2049 int tReg;
2050 //TUNING: add support for use of Arm rsub op
2051 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2052 tReg = oatAllocTemp(cUnit);
2053 loadConstant(cUnit, tReg, lit);
2054 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2055 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2056 storeValue(cUnit, rlDest, rlResult);
2057 return false;
2058 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002059 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002060
2061 case Instruction::ADD_INT_LIT8:
2062 case Instruction::ADD_INT_LIT16:
2063 op = kOpAdd;
2064 break;
2065 case Instruction::MUL_INT_LIT8:
2066 case Instruction::MUL_INT_LIT16: {
2067 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2068 return false;
2069 }
2070 op = kOpMul;
2071 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002072 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002073 case Instruction::AND_INT_LIT8:
2074 case Instruction::AND_INT_LIT16:
2075 op = kOpAnd;
2076 break;
2077 case Instruction::OR_INT_LIT8:
2078 case Instruction::OR_INT_LIT16:
2079 op = kOpOr;
2080 break;
2081 case Instruction::XOR_INT_LIT8:
2082 case Instruction::XOR_INT_LIT16:
2083 op = kOpXor;
2084 break;
2085 case Instruction::SHL_INT_LIT8:
2086 lit &= 31;
2087 shiftOp = true;
2088 op = kOpLsl;
2089 break;
2090 case Instruction::SHR_INT_LIT8:
2091 lit &= 31;
2092 shiftOp = true;
2093 op = kOpAsr;
2094 break;
2095 case Instruction::USHR_INT_LIT8:
2096 lit &= 31;
2097 shiftOp = true;
2098 op = kOpLsr;
2099 break;
2100
2101 case Instruction::DIV_INT_LIT8:
2102 case Instruction::DIV_INT_LIT16:
2103 case Instruction::REM_INT_LIT8:
2104 case Instruction::REM_INT_LIT16:
2105 if (lit == 0) {
2106 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
2107 return false;
2108 }
2109 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2110 return false;
2111 }
2112 oatFlushAllRegs(cUnit); /* Everything to home location */
2113 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2114 oatClobber(cUnit, rARG0);
2115 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
2116 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
2117 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
2118 isDiv = true;
2119 } else {
2120 isDiv = false;
2121 }
2122 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
2123 if (isDiv)
2124 rlResult = oatGetReturn(cUnit, false);
2125 else
2126 rlResult = oatGetReturnAlt(cUnit);
2127 storeValue(cUnit, rlDest, rlResult);
2128 return false;
2129 break;
2130 default:
2131 return true;
2132 }
2133 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2134 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2135 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2136 if (shiftOp && (lit == 0)) {
2137 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2138 } else {
2139 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2140 }
2141 storeValue(cUnit, rlDest, rlResult);
2142 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002143}
2144
2145bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002146 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002147{
Bill Buzbeea114add2012-05-03 15:00:40 -07002148 RegLocation rlResult;
2149 OpKind firstOp = kOpBkpt;
2150 OpKind secondOp = kOpBkpt;
2151 bool callOut = false;
2152 bool checkZero = false;
2153 int funcOffset;
2154 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002155
Bill Buzbeea114add2012-05-03 15:00:40 -07002156 switch (mir->dalvikInsn.opcode) {
2157 case Instruction::NOT_LONG:
2158 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2159 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2160 // Check for destructive overlap
2161 if (rlResult.lowReg == rlSrc2.highReg) {
2162 int tReg = oatAllocTemp(cUnit);
2163 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2164 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2165 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2166 oatFreeTemp(cUnit, tReg);
2167 } else {
2168 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2169 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2170 }
2171 storeValueWide(cUnit, rlDest, rlResult);
2172 return false;
2173 break;
2174 case Instruction::ADD_LONG:
2175 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002176#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002177 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002178#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002179 firstOp = kOpAdd;
2180 secondOp = kOpAdc;
2181 break;
buzbeec5159d52012-03-03 11:48:39 -08002182#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002183 case Instruction::SUB_LONG:
2184 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002185#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002186 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002187#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002188 firstOp = kOpSub;
2189 secondOp = kOpSbc;
2190 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002191#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002192 case Instruction::MUL_LONG:
2193 case Instruction::MUL_LONG_2ADDR:
2194 callOut = true;
2195 retReg = rRET0;
2196 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2197 break;
2198 case Instruction::DIV_LONG:
2199 case Instruction::DIV_LONG_2ADDR:
2200 callOut = true;
2201 checkZero = true;
2202 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002203 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002204 break;
2205 case Instruction::REM_LONG:
2206 case Instruction::REM_LONG_2ADDR:
2207 callOut = true;
2208 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002209 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002210#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002211 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2212 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002213#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002214 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002215#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002216 break;
2217 case Instruction::AND_LONG_2ADDR:
2218 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002219#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002220 return genAndLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002221#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002222 firstOp = kOpAnd;
2223 secondOp = kOpAnd;
2224 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002225#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002226 case Instruction::OR_LONG:
2227 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002228#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002229 return genOrLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002230#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002231 firstOp = kOpOr;
2232 secondOp = kOpOr;
2233 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002234#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002235 case Instruction::XOR_LONG:
2236 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002237#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002238 return genXorLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002239#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002240 firstOp = kOpXor;
2241 secondOp = kOpXor;
2242 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002243#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002244 case Instruction::NEG_LONG: {
2245 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002246 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002247 default:
2248 LOG(FATAL) << "Invalid long arith op";
2249 }
2250 if (!callOut) {
2251 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
2252 } else {
2253 oatFlushAllRegs(cUnit); /* Send everything to home location */
2254 if (checkZero) {
2255 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2256#if !defined(TARGET_X86)
2257 int rTgt = loadHelper(cUnit, funcOffset);
2258#endif
2259 int tReg = oatAllocTemp(cUnit);
2260#if defined(TARGET_ARM)
2261 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2262 oatFreeTemp(cUnit, tReg);
2263 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
2264#else
2265 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2266#endif
2267 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
2268 oatFreeTemp(cUnit, tReg);
2269 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2270#if !defined(TARGET_X86)
2271 opReg(cUnit, kOpBlx, rTgt);
2272 oatFreeTemp(cUnit, rTgt);
2273#else
2274 opThreadMem(cUnit, kOpBlx, funcOffset);
2275#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002276 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002277 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2278 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002279 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002280 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2281 if (retReg == rRET0)
2282 rlResult = oatGetReturnWide(cUnit, false);
2283 else
2284 rlResult = oatGetReturnWideAlt(cUnit);
2285 storeValueWide(cUnit, rlDest, rlResult);
2286 }
2287 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002288}
2289
2290bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
Bill Buzbeea114add2012-05-03 15:00:40 -07002291 int srcSize, int tgtSize)
buzbee31a4a6f2012-02-28 15:36:15 -08002292{
Bill Buzbeea114add2012-05-03 15:00:40 -07002293 /*
2294 * Don't optimize the register usage since it calls out to support
2295 * functions
2296 */
2297 RegLocation rlSrc;
2298 RegLocation rlDest;
2299 oatFlushAllRegs(cUnit); /* Send everything to home location */
2300 if (srcSize == 1) {
2301 rlSrc = oatGetSrc(cUnit, mir, 0);
2302 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2303 } else {
2304 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
2305 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
2306 }
2307 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
2308 if (tgtSize == 1) {
2309 RegLocation rlResult;
2310 rlDest = oatGetDest(cUnit, mir, 0);
2311 rlResult = oatGetReturn(cUnit, rlDest.fp);
2312 storeValue(cUnit, rlDest, rlResult);
2313 } else {
2314 RegLocation rlResult;
2315 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
2316 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2317 storeValueWide(cUnit, rlDest, rlResult);
2318 }
2319 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002320}
2321
2322void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
2323bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
2324 RegLocation rlDest, RegLocation rlSrc1,
2325 RegLocation rlSrc2)
2326{
Bill Buzbeea114add2012-05-03 15:00:40 -07002327 RegLocation rlResult;
2328 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002329
Bill Buzbeea114add2012-05-03 15:00:40 -07002330 switch (mir->dalvikInsn.opcode) {
2331 case Instruction::ADD_FLOAT_2ADDR:
2332 case Instruction::ADD_FLOAT:
2333 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2334 break;
2335 case Instruction::SUB_FLOAT_2ADDR:
2336 case Instruction::SUB_FLOAT:
2337 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2338 break;
2339 case Instruction::DIV_FLOAT_2ADDR:
2340 case Instruction::DIV_FLOAT:
2341 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2342 break;
2343 case Instruction::MUL_FLOAT_2ADDR:
2344 case Instruction::MUL_FLOAT:
2345 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2346 break;
2347 case Instruction::REM_FLOAT_2ADDR:
2348 case Instruction::REM_FLOAT:
2349 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2350 break;
2351 case Instruction::NEG_FLOAT: {
2352 genNegFloat(cUnit, rlDest, rlSrc1);
2353 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002354 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002355 default:
2356 return true;
2357 }
2358 oatFlushAllRegs(cUnit); /* Send everything to home location */
2359 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2360 rlResult = oatGetReturn(cUnit, true);
2361 storeValue(cUnit, rlDest, rlResult);
2362 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002363}
2364
2365void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2366bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2367 RegLocation rlDest, RegLocation rlSrc1,
2368 RegLocation rlSrc2)
2369{
Bill Buzbeea114add2012-05-03 15:00:40 -07002370 RegLocation rlResult;
2371 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002372
Bill Buzbeea114add2012-05-03 15:00:40 -07002373 switch (mir->dalvikInsn.opcode) {
2374 case Instruction::ADD_DOUBLE_2ADDR:
2375 case Instruction::ADD_DOUBLE:
2376 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2377 break;
2378 case Instruction::SUB_DOUBLE_2ADDR:
2379 case Instruction::SUB_DOUBLE:
2380 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2381 break;
2382 case Instruction::DIV_DOUBLE_2ADDR:
2383 case Instruction::DIV_DOUBLE:
2384 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2385 break;
2386 case Instruction::MUL_DOUBLE_2ADDR:
2387 case Instruction::MUL_DOUBLE:
2388 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2389 break;
2390 case Instruction::REM_DOUBLE_2ADDR:
2391 case Instruction::REM_DOUBLE:
2392 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2393 break;
2394 case Instruction::NEG_DOUBLE: {
2395 genNegDouble(cUnit, rlDest, rlSrc1);
2396 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002397 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002398 default:
2399 return true;
2400 }
2401 oatFlushAllRegs(cUnit); /* Send everything to home location */
2402 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2403 rlResult = oatGetReturnWide(cUnit, true);
2404 storeValueWide(cUnit, rlDest, rlResult);
2405 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002406}
2407
2408bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2409{
Bill Buzbeea114add2012-05-03 15:00:40 -07002410 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002411
Bill Buzbeea114add2012-05-03 15:00:40 -07002412 switch (opcode) {
2413 case Instruction::INT_TO_FLOAT:
2414 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pI2f),
2415 1, 1);
2416 case Instruction::FLOAT_TO_INT:
2417 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pF2iz),
2418 1, 1);
2419 case Instruction::DOUBLE_TO_FLOAT:
2420 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pD2f),
2421 2, 1);
2422 case Instruction::FLOAT_TO_DOUBLE:
2423 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pF2d),
2424 1, 2);
2425 case Instruction::INT_TO_DOUBLE:
2426 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pI2d),
2427 1, 2);
2428 case Instruction::DOUBLE_TO_INT:
2429 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pD2iz),
2430 2, 1);
2431 case Instruction::FLOAT_TO_LONG:
2432 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pF2l),
2433 1, 2);
2434 case Instruction::LONG_TO_FLOAT:
2435 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pL2f),
2436 2, 1);
2437 case Instruction::DOUBLE_TO_LONG:
2438 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pD2l),
2439 2, 2);
2440 case Instruction::LONG_TO_DOUBLE:
2441 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pL2d),
2442 2, 2);
2443 default:
2444 return true;
2445 }
2446 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002447}
2448
2449/*
2450 * Generate callout to updateDebugger. Note that we're overloading
2451 * the use of rSUSPEND here. When the debugger is active, this
2452 * register holds the address of the update function. So, if it's
2453 * non-null, we call out to it.
2454 *
2455 * Note also that rRET0 and rRET1 must be preserved across this
2456 * code. This must be handled by the stub.
2457 */
2458void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2459{
Bill Buzbeea114add2012-05-03 15:00:40 -07002460 // Following DCHECK verifies that dPC is in range of single load immediate
2461 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2462 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2463 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002464#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002465 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2466 opIT(cUnit, kArmCondNe, "T");
2467 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2468 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002469#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002470 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002471#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002472 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2473 loadConstant(cUnit, rARG2, offset);
2474 opReg(cUnit, kOpBlx, rSUSPEND);
2475 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2476 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002477#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002478 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002479}
2480
2481/* Check if we need to check for pending suspend request */
2482void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2483{
Bill Buzbeea114add2012-05-03 15:00:40 -07002484 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2485 return;
2486 }
2487 oatFlushAllRegs(cUnit);
2488 if (cUnit->genDebugger) {
2489 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002490#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002491 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002492#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002493 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
2494 opReg(cUnit, kOpBlx, rTgt);
2495 // Refresh rSUSPEND
2496 loadWordDisp(cUnit, rSELF,
2497 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2498 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002499#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002500 } else {
2501 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002502#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002503 // In non-debug case, only check periodically
2504 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2505 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002506#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002507 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2508 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002509#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002510 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2511 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002512#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002513 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2514 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2515 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2516 branch->target = (LIR*)target;
2517 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2518 }
buzbee31a4a6f2012-02-28 15:36:15 -08002519}
2520
buzbeefead2932012-03-30 14:02:01 -07002521/* Check if we need to check for pending suspend request */
2522void genSuspendTestAndBranch(CompilationUnit* cUnit, MIR* mir, LIR* target)
2523{
Bill Buzbeea114add2012-05-03 15:00:40 -07002524 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2525 opUnconditionalBranch(cUnit, target);
2526 return;
2527 }
2528 if (cUnit->genDebugger) {
2529 genSuspendTest(cUnit, mir);
2530 opUnconditionalBranch(cUnit, target);
2531 } else {
buzbeefead2932012-03-30 14:02:01 -07002532#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002533 // In non-debug case, only check periodically
2534 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2535 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002536#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002537 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2538 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002539#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002540 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2541 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002542#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002543 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
2544 kPseudoSuspendTarget, (intptr_t)target, mir->offset);
2545 oatFlushAllRegs(cUnit);
2546 opUnconditionalBranch(cUnit, launchPad);
2547 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2548 (intptr_t)launchPad);
2549 }
buzbeefead2932012-03-30 14:02:01 -07002550}
2551
buzbee31a4a6f2012-02-28 15:36:15 -08002552} // namespace art