blob: 22090849344f7713928303212c8b923fc8c01b54 [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)) {
1504 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1505 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1506 dataOffset, rlResult.lowReg, rlResult.highReg, size,
1507 INVALID_SREG);
buzbee31a4a6f2012-02-28 15:36:15 -08001508
Bill Buzbeea114add2012-05-03 15:00:40 -07001509 storeValueWide(cUnit, rlDest, rlResult);
1510 } else {
1511 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001512
Bill Buzbeea114add2012-05-03 15:00:40 -07001513 loadBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1514 dataOffset, rlResult.lowReg, INVALID_REG, size,
1515 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001516
Bill Buzbeea114add2012-05-03 15:00:40 -07001517 storeValue(cUnit, rlDest, rlResult);
1518 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001519#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001520 int regPtr = oatAllocTemp(cUnit);
1521 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1522 int regLen = INVALID_REG;
1523 if (needsRangeCheck) {
1524 regLen = oatAllocTemp(cUnit);
1525 /* Get len */
1526 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1527 }
1528 /* regPtr -> array data */
1529 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1530 oatFreeTemp(cUnit, rlArray.lowReg);
1531 if ((size == kLong) || (size == kDouble)) {
1532 if (scale) {
1533 int rNewIndex = oatAllocTemp(cUnit);
1534 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1535 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1536 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001537 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001538 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001539 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001540 oatFreeTemp(cUnit, rlIndex.lowReg);
1541 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1542
1543 if (needsRangeCheck) {
1544 // TODO: change kCondCS to a more meaningful name, is the sense of
1545 // carry-set/clear flipped?
1546 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1547 kThrowArrayBounds);
1548 oatFreeTemp(cUnit, regLen);
1549 }
1550 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1551
1552 oatFreeTemp(cUnit, regPtr);
1553 storeValueWide(cUnit, rlDest, rlResult);
1554 } else {
1555 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1556
1557 if (needsRangeCheck) {
1558 // TODO: change kCondCS to a more meaningful name, is the sense of
1559 // carry-set/clear flipped?
1560 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1561 kThrowArrayBounds);
1562 oatFreeTemp(cUnit, regLen);
1563 }
1564 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1565 scale, size);
1566
1567 oatFreeTemp(cUnit, regPtr);
1568 storeValue(cUnit, rlDest, rlResult);
1569 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001570#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001571}
1572
1573/*
1574 * Generate array store
1575 *
1576 */
1577void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1578 RegLocation rlArray, RegLocation rlIndex,
1579 RegLocation rlSrc, int scale)
1580{
Bill Buzbeea114add2012-05-03 15:00:40 -07001581 RegisterClass regClass = oatRegClassBySize(size);
1582 int lenOffset = Array::LengthOffset().Int32Value();
1583 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001584
Bill Buzbeea114add2012-05-03 15:00:40 -07001585 if (size == kLong || size == kDouble) {
1586 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1587 } else {
1588 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1589 }
buzbee31a4a6f2012-02-28 15:36:15 -08001590
Bill Buzbeea114add2012-05-03 15:00:40 -07001591 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1592 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001593#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001594 int regPtr;
1595 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1596 oatClobber(cUnit, rlArray.lowReg);
1597 regPtr = rlArray.lowReg;
1598 } else {
1599 regPtr = oatAllocTemp(cUnit);
1600 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1601 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001602#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001603
Bill Buzbeea114add2012-05-03 15:00:40 -07001604 /* null object? */
1605 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee31a4a6f2012-02-28 15:36:15 -08001606
Ian Rogersb41b33b2012-03-20 14:22:54 -07001607#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001608 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1609 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1610 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
1611 lenOffset, mir, kThrowArrayBounds);
1612 }
1613 if ((size == kLong) || (size == kDouble)) {
1614 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1615 } else {
1616 rlSrc = loadValue(cUnit, rlSrc, regClass);
1617 }
1618 storeBaseIndexedDisp(cUnit, NULL, rlArray.lowReg, rlIndex.lowReg, scale,
1619 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1620 INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001621#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001622 bool needsRangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
1623 int regLen = INVALID_REG;
1624 if (needsRangeCheck) {
1625 regLen = oatAllocTemp(cUnit);
1626 //NOTE: max live temps(4) here.
1627 /* Get len */
1628 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1629 }
1630 /* regPtr -> array data */
1631 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1632 /* at this point, regPtr points to array, 2 live temps */
1633 if ((size == kLong) || (size == kDouble)) {
1634 //TUNING: specific wide routine that can handle fp regs
1635 if (scale) {
1636 int rNewIndex = oatAllocTemp(cUnit);
1637 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1638 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1639 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001640 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001641 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001642 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001643 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1644
1645 if (needsRangeCheck) {
1646 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1647 kThrowArrayBounds);
1648 oatFreeTemp(cUnit, regLen);
1649 }
1650
jeffhao41005dd2012-05-09 17:58:52 -07001651 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001652
1653 oatFreeTemp(cUnit, regPtr);
1654 } else {
1655 rlSrc = loadValue(cUnit, rlSrc, regClass);
1656 if (needsRangeCheck) {
1657 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, mir,
1658 kThrowArrayBounds);
1659 oatFreeTemp(cUnit, regLen);
1660 }
1661 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1662 scale, size);
1663 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001664#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001665}
1666
1667void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
1668 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001669 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001670{
Bill Buzbeea114add2012-05-03 15:00:40 -07001671 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001672#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001673 /*
1674 * NOTE: This is the one place in the code in which we might have
1675 * as many as six live temporary registers. There are 5 in the normal
1676 * set for Arm. Until we have spill capabilities, temporarily add
1677 * lr to the temp set. It is safe to do this locally, but note that
1678 * lr is used explicitly elsewhere in the code generator and cannot
1679 * normally be used as a general temp register.
1680 */
1681 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1682 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001683#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001684 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1685 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1686 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1687 // The longs may overlap - use intermediate temp if so
1688 if (rlResult.lowReg == rlSrc1.highReg) {
1689 int tReg = oatAllocTemp(cUnit);
1690 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1691 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1692 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1693 oatFreeTemp(cUnit, tReg);
1694 } else {
1695 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1696 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1697 rlSrc2.highReg);
1698 }
1699 /*
1700 * NOTE: If rlDest refers to a frame variable in a large frame, the
1701 * following storeValueWide might need to allocate a temp register.
1702 * To further work around the lack of a spill capability, explicitly
1703 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1704 * Remove when spill is functional.
1705 */
1706 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1707 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1708 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001709#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001710 oatClobber(cUnit, rLR);
1711 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001712#endif
1713}
1714
1715
1716bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
1717 RegLocation rlSrc1, RegLocation rlShift)
1718{
Bill Buzbeea114add2012-05-03 15:00:40 -07001719 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001720
Bill Buzbeea114add2012-05-03 15:00:40 -07001721 switch (mir->dalvikInsn.opcode) {
1722 case Instruction::SHL_LONG:
1723 case Instruction::SHL_LONG_2ADDR:
1724 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1725 break;
1726 case Instruction::SHR_LONG:
1727 case Instruction::SHR_LONG_2ADDR:
1728 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1729 break;
1730 case Instruction::USHR_LONG:
1731 case Instruction::USHR_LONG_2ADDR:
1732 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1733 break;
1734 default:
1735 LOG(FATAL) << "Unexpected case";
1736 return true;
1737 }
1738 oatFlushAllRegs(cUnit); /* Send everything to home location */
1739 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
1740 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1741 storeValueWide(cUnit, rlDest, rlResult);
1742 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001743}
1744
1745
1746bool genArithOpInt(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001747 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001748{
Bill Buzbeea114add2012-05-03 15:00:40 -07001749 OpKind op = kOpBkpt;
1750 bool callOut = false;
1751 bool checkZero = false;
1752 bool unary = false;
1753 RegLocation rlResult;
1754 bool shiftOp = false;
1755 int funcOffset;
1756 int retReg = rRET0;
1757 switch (mir->dalvikInsn.opcode) {
1758 case Instruction::NEG_INT:
1759 op = kOpNeg;
1760 unary = true;
1761 break;
1762 case Instruction::NOT_INT:
1763 op = kOpMvn;
1764 unary = true;
1765 break;
1766 case Instruction::ADD_INT:
1767 case Instruction::ADD_INT_2ADDR:
1768 op = kOpAdd;
1769 break;
1770 case Instruction::SUB_INT:
1771 case Instruction::SUB_INT_2ADDR:
1772 op = kOpSub;
1773 break;
1774 case Instruction::MUL_INT:
1775 case Instruction::MUL_INT_2ADDR:
1776 op = kOpMul;
1777 break;
1778 case Instruction::DIV_INT:
1779 case Instruction::DIV_INT_2ADDR:
1780 checkZero = true;
1781 op = kOpDiv;
1782 callOut = true;
1783 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1784 retReg = rRET0;
1785 break;
1786 /* NOTE: returns in rARG1 */
1787 case Instruction::REM_INT:
1788 case Instruction::REM_INT_2ADDR:
1789 checkZero = true;
1790 op = kOpRem;
1791 callOut = true;
1792 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1793 retReg = rRET1;
1794 break;
1795 case Instruction::AND_INT:
1796 case Instruction::AND_INT_2ADDR:
1797 op = kOpAnd;
1798 break;
1799 case Instruction::OR_INT:
1800 case Instruction::OR_INT_2ADDR:
1801 op = kOpOr;
1802 break;
1803 case Instruction::XOR_INT:
1804 case Instruction::XOR_INT_2ADDR:
1805 op = kOpXor;
1806 break;
1807 case Instruction::SHL_INT:
1808 case Instruction::SHL_INT_2ADDR:
1809 shiftOp = true;
1810 op = kOpLsl;
1811 break;
1812 case Instruction::SHR_INT:
1813 case Instruction::SHR_INT_2ADDR:
1814 shiftOp = true;
1815 op = kOpAsr;
1816 break;
1817 case Instruction::USHR_INT:
1818 case Instruction::USHR_INT_2ADDR:
1819 shiftOp = true;
1820 op = kOpLsr;
1821 break;
1822 default:
1823 LOG(FATAL) << "Invalid word arith op: " <<
1824 (int)mir->dalvikInsn.opcode;
1825 }
1826 if (!callOut) {
1827 if (unary) {
1828 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1829 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1830 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001831 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001832 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001833#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001834 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1835 int tReg = oatAllocTemp(cUnit);
1836 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001837#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001838 // X86 doesn't require masking and must use ECX
1839 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1840 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001841#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001842 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1843 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1844 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1845 oatFreeTemp(cUnit, tReg);
1846 } else {
1847 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1848 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1849 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1850 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1851 }
buzbee31a4a6f2012-02-28 15:36:15 -08001852 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001853 storeValue(cUnit, rlDest, rlResult);
1854 } else {
1855 RegLocation rlResult;
1856 oatFlushAllRegs(cUnit); /* Send everything to home location */
1857 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1858#if !defined(TARGET_X86)
1859 int rTgt = loadHelper(cUnit, funcOffset);
1860#endif
1861 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1862 if (checkZero) {
1863 genImmedCheck(cUnit, kCondEq, rARG1, 0, mir, kThrowDivZero);
1864 }
1865#if !defined(TARGET_X86)
1866 opReg(cUnit, kOpBlx, rTgt);
1867 oatFreeTemp(cUnit, rTgt);
1868#else
1869 opThreadMem(cUnit, kOpBlx, funcOffset);
1870#endif
1871 if (retReg == rRET0)
1872 rlResult = oatGetReturn(cUnit, false);
1873 else
1874 rlResult = oatGetReturnAlt(cUnit);
1875 storeValue(cUnit, rlDest, rlResult);
1876 }
1877 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001878}
1879
1880/*
1881 * The following are the first-level codegen routines that analyze the format
1882 * of each bytecode then either dispatch special purpose codegen routines
1883 * or produce corresponding Thumb instructions directly.
1884 */
1885
1886bool isPowerOfTwo(int x)
1887{
Bill Buzbeea114add2012-05-03 15:00:40 -07001888 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001889}
1890
1891// Returns true if no more than two bits are set in 'x'.
1892bool isPopCountLE2(unsigned int x)
1893{
Bill Buzbeea114add2012-05-03 15:00:40 -07001894 x &= x - 1;
1895 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001896}
1897
1898// Returns the index of the lowest set bit in 'x'.
1899int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001900 int bit_posn = 0;
1901 while ((x & 0xf) == 0) {
1902 bit_posn += 4;
1903 x >>= 4;
1904 }
1905 while ((x & 1) == 0) {
1906 bit_posn++;
1907 x >>= 1;
1908 }
1909 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001910}
1911
1912// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1913// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001914bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001915 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001916{
buzbeef3aac972012-04-11 16:33:36 -07001917#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001918 // No divide instruction for Arm, so check for more special cases
1919 if (lit < 2) {
1920 return false;
1921 }
1922 if (!isPowerOfTwo(lit)) {
1923 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1924 }
buzbeef3aac972012-04-11 16:33:36 -07001925#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001926 if (lit < 2 || !isPowerOfTwo(lit)) {
1927 return false;
1928 }
buzbeef3aac972012-04-11 16:33:36 -07001929#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001930 int k = lowestSetBit(lit);
1931 if (k >= 30) {
1932 // Avoid special cases.
1933 return false;
1934 }
1935 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1936 dalvikOpcode == Instruction::DIV_INT_LIT16);
1937 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1938 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1939 if (div) {
1940 int tReg = oatAllocTemp(cUnit);
1941 if (lit == 2) {
1942 // Division by 2 is by far the most common division by constant.
1943 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1944 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1945 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001946 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001947 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1948 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1949 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1950 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001951 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001952 } else {
1953 int tReg1 = oatAllocTemp(cUnit);
1954 int tReg2 = oatAllocTemp(cUnit);
1955 if (lit == 2) {
1956 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1957 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1958 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1959 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1960 } else {
1961 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1962 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1963 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1964 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1965 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1966 }
1967 }
1968 storeValue(cUnit, rlDest, rlResult);
1969 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001970}
1971
1972void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1973 RegLocation rlResult, int lit,
1974 int firstBit, int secondBit)
1975{
buzbee0398c422012-03-02 15:22:47 -08001976#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001977 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1978 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001979#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001980 int tReg = oatAllocTemp(cUnit);
1981 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1982 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1983 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001984#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001985 if (firstBit != 0) {
1986 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1987 }
buzbee31a4a6f2012-02-28 15:36:15 -08001988}
1989
1990// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1991// and store the result in 'rlDest'.
1992bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1993 RegLocation rlDest, int lit)
1994{
Bill Buzbeea114add2012-05-03 15:00:40 -07001995 // Can we simplify this multiplication?
1996 bool powerOfTwo = false;
1997 bool popCountLE2 = false;
1998 bool powerOfTwoMinusOne = false;
1999 if (lit < 2) {
2000 // Avoid special cases.
2001 return false;
2002 } else if (isPowerOfTwo(lit)) {
2003 powerOfTwo = true;
2004 } else if (isPopCountLE2(lit)) {
2005 popCountLE2 = true;
2006 } else if (isPowerOfTwo(lit + 1)) {
2007 powerOfTwoMinusOne = true;
2008 } else {
2009 return false;
2010 }
2011 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2012 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2013 if (powerOfTwo) {
2014 // Shift.
2015 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2016 lowestSetBit(lit));
2017 } else if (popCountLE2) {
2018 // Shift and add and shift.
2019 int firstBit = lowestSetBit(lit);
2020 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2021 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2022 firstBit, secondBit);
2023 } else {
2024 // Reverse subtract: (src << (shift + 1)) - src.
2025 DCHECK(powerOfTwoMinusOne);
2026 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2027 int tReg = oatAllocTemp(cUnit);
2028 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2029 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2030 }
2031 storeValue(cUnit, rlDest, rlResult);
2032 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002033}
2034
2035bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
2036 RegLocation rlSrc, int lit)
2037{
Bill Buzbeea114add2012-05-03 15:00:40 -07002038 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
2039 RegLocation rlResult;
2040 OpKind op = (OpKind)0; /* Make gcc happy */
2041 int shiftOp = false;
2042 bool isDiv = false;
2043 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002044
Bill Buzbeea114add2012-05-03 15:00:40 -07002045 switch (dalvikOpcode) {
2046 case Instruction::RSUB_INT_LIT8:
2047 case Instruction::RSUB_INT: {
2048 int tReg;
2049 //TUNING: add support for use of Arm rsub op
2050 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2051 tReg = oatAllocTemp(cUnit);
2052 loadConstant(cUnit, tReg, lit);
2053 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2054 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2055 storeValue(cUnit, rlDest, rlResult);
2056 return false;
2057 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002058 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002059
2060 case Instruction::ADD_INT_LIT8:
2061 case Instruction::ADD_INT_LIT16:
2062 op = kOpAdd;
2063 break;
2064 case Instruction::MUL_INT_LIT8:
2065 case Instruction::MUL_INT_LIT16: {
2066 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2067 return false;
2068 }
2069 op = kOpMul;
2070 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002071 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002072 case Instruction::AND_INT_LIT8:
2073 case Instruction::AND_INT_LIT16:
2074 op = kOpAnd;
2075 break;
2076 case Instruction::OR_INT_LIT8:
2077 case Instruction::OR_INT_LIT16:
2078 op = kOpOr;
2079 break;
2080 case Instruction::XOR_INT_LIT8:
2081 case Instruction::XOR_INT_LIT16:
2082 op = kOpXor;
2083 break;
2084 case Instruction::SHL_INT_LIT8:
2085 lit &= 31;
2086 shiftOp = true;
2087 op = kOpLsl;
2088 break;
2089 case Instruction::SHR_INT_LIT8:
2090 lit &= 31;
2091 shiftOp = true;
2092 op = kOpAsr;
2093 break;
2094 case Instruction::USHR_INT_LIT8:
2095 lit &= 31;
2096 shiftOp = true;
2097 op = kOpLsr;
2098 break;
2099
2100 case Instruction::DIV_INT_LIT8:
2101 case Instruction::DIV_INT_LIT16:
2102 case Instruction::REM_INT_LIT8:
2103 case Instruction::REM_INT_LIT16:
2104 if (lit == 0) {
2105 genImmedCheck(cUnit, kCondAl, 0, 0, mir, kThrowDivZero);
2106 return false;
2107 }
2108 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2109 return false;
2110 }
2111 oatFlushAllRegs(cUnit); /* Everything to home location */
2112 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2113 oatClobber(cUnit, rARG0);
2114 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
2115 if ((dalvikOpcode == Instruction::DIV_INT_LIT8) ||
2116 (dalvikOpcode == Instruction::DIV_INT_LIT16)) {
2117 isDiv = true;
2118 } else {
2119 isDiv = false;
2120 }
2121 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
2122 if (isDiv)
2123 rlResult = oatGetReturn(cUnit, false);
2124 else
2125 rlResult = oatGetReturnAlt(cUnit);
2126 storeValue(cUnit, rlDest, rlResult);
2127 return false;
2128 break;
2129 default:
2130 return true;
2131 }
2132 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2133 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2134 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2135 if (shiftOp && (lit == 0)) {
2136 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2137 } else {
2138 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2139 }
2140 storeValue(cUnit, rlDest, rlResult);
2141 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002142}
2143
2144bool genArithOpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002145 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002146{
Bill Buzbeea114add2012-05-03 15:00:40 -07002147 RegLocation rlResult;
2148 OpKind firstOp = kOpBkpt;
2149 OpKind secondOp = kOpBkpt;
2150 bool callOut = false;
2151 bool checkZero = false;
2152 int funcOffset;
2153 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002154
Bill Buzbeea114add2012-05-03 15:00:40 -07002155 switch (mir->dalvikInsn.opcode) {
2156 case Instruction::NOT_LONG:
2157 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2158 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2159 // Check for destructive overlap
2160 if (rlResult.lowReg == rlSrc2.highReg) {
2161 int tReg = oatAllocTemp(cUnit);
2162 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2163 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2164 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2165 oatFreeTemp(cUnit, tReg);
2166 } else {
2167 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2168 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2169 }
2170 storeValueWide(cUnit, rlDest, rlResult);
2171 return false;
2172 break;
2173 case Instruction::ADD_LONG:
2174 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002175#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002176 return genAddLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002177#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002178 firstOp = kOpAdd;
2179 secondOp = kOpAdc;
2180 break;
buzbeec5159d52012-03-03 11:48:39 -08002181#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002182 case Instruction::SUB_LONG:
2183 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002184#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002185 return genSubLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002186#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002187 firstOp = kOpSub;
2188 secondOp = kOpSbc;
2189 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002190#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002191 case Instruction::MUL_LONG:
2192 case Instruction::MUL_LONG_2ADDR:
2193 callOut = true;
2194 retReg = rRET0;
2195 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2196 break;
2197 case Instruction::DIV_LONG:
2198 case Instruction::DIV_LONG_2ADDR:
2199 callOut = true;
2200 checkZero = true;
2201 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002202 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002203 break;
2204 case Instruction::REM_LONG:
2205 case Instruction::REM_LONG_2ADDR:
2206 callOut = true;
2207 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002208 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002209#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002210 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2211 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002212#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002213 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002214#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002215 break;
2216 case Instruction::AND_LONG_2ADDR:
2217 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002218#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002219 return genAndLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002220#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002221 firstOp = kOpAnd;
2222 secondOp = kOpAnd;
2223 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002224#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002225 case Instruction::OR_LONG:
2226 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002227#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002228 return genOrLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002229#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002230 firstOp = kOpOr;
2231 secondOp = kOpOr;
2232 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002233#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002234 case Instruction::XOR_LONG:
2235 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002236#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002237 return genXorLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002238#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002239 firstOp = kOpXor;
2240 secondOp = kOpXor;
2241 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002242#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002243 case Instruction::NEG_LONG: {
2244 return genNegLong(cUnit, mir, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002245 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002246 default:
2247 LOG(FATAL) << "Invalid long arith op";
2248 }
2249 if (!callOut) {
2250 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
2251 } else {
2252 oatFlushAllRegs(cUnit); /* Send everything to home location */
2253 if (checkZero) {
2254 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2255#if !defined(TARGET_X86)
2256 int rTgt = loadHelper(cUnit, funcOffset);
2257#endif
2258 int tReg = oatAllocTemp(cUnit);
2259#if defined(TARGET_ARM)
2260 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2261 oatFreeTemp(cUnit, tReg);
2262 genCheck(cUnit, kCondEq, mir, kThrowDivZero);
2263#else
2264 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2265#endif
2266 genImmedCheck(cUnit, kCondEq, tReg, 0, mir, kThrowDivZero);
2267 oatFreeTemp(cUnit, tReg);
2268 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2269#if !defined(TARGET_X86)
2270 opReg(cUnit, kOpBlx, rTgt);
2271 oatFreeTemp(cUnit, rTgt);
2272#else
2273 opThreadMem(cUnit, kOpBlx, funcOffset);
2274#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002275 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002276 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2277 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002278 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002279 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2280 if (retReg == rRET0)
2281 rlResult = oatGetReturnWide(cUnit, false);
2282 else
2283 rlResult = oatGetReturnWideAlt(cUnit);
2284 storeValueWide(cUnit, rlDest, rlResult);
2285 }
2286 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002287}
2288
2289bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
Bill Buzbeea114add2012-05-03 15:00:40 -07002290 int srcSize, int tgtSize)
buzbee31a4a6f2012-02-28 15:36:15 -08002291{
Bill Buzbeea114add2012-05-03 15:00:40 -07002292 /*
2293 * Don't optimize the register usage since it calls out to support
2294 * functions
2295 */
2296 RegLocation rlSrc;
2297 RegLocation rlDest;
2298 oatFlushAllRegs(cUnit); /* Send everything to home location */
2299 if (srcSize == 1) {
2300 rlSrc = oatGetSrc(cUnit, mir, 0);
2301 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2302 } else {
2303 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
2304 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
2305 }
2306 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
2307 if (tgtSize == 1) {
2308 RegLocation rlResult;
2309 rlDest = oatGetDest(cUnit, mir, 0);
2310 rlResult = oatGetReturn(cUnit, rlDest.fp);
2311 storeValue(cUnit, rlDest, rlResult);
2312 } else {
2313 RegLocation rlResult;
2314 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
2315 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2316 storeValueWide(cUnit, rlDest, rlResult);
2317 }
2318 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002319}
2320
2321void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
2322bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
2323 RegLocation rlDest, RegLocation rlSrc1,
2324 RegLocation rlSrc2)
2325{
Bill Buzbeea114add2012-05-03 15:00:40 -07002326 RegLocation rlResult;
2327 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002328
Bill Buzbeea114add2012-05-03 15:00:40 -07002329 switch (mir->dalvikInsn.opcode) {
2330 case Instruction::ADD_FLOAT_2ADDR:
2331 case Instruction::ADD_FLOAT:
2332 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2333 break;
2334 case Instruction::SUB_FLOAT_2ADDR:
2335 case Instruction::SUB_FLOAT:
2336 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2337 break;
2338 case Instruction::DIV_FLOAT_2ADDR:
2339 case Instruction::DIV_FLOAT:
2340 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2341 break;
2342 case Instruction::MUL_FLOAT_2ADDR:
2343 case Instruction::MUL_FLOAT:
2344 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2345 break;
2346 case Instruction::REM_FLOAT_2ADDR:
2347 case Instruction::REM_FLOAT:
2348 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2349 break;
2350 case Instruction::NEG_FLOAT: {
2351 genNegFloat(cUnit, rlDest, rlSrc1);
2352 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002353 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002354 default:
2355 return true;
2356 }
2357 oatFlushAllRegs(cUnit); /* Send everything to home location */
2358 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2359 rlResult = oatGetReturn(cUnit, true);
2360 storeValue(cUnit, rlDest, rlResult);
2361 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002362}
2363
2364void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
2365bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
2366 RegLocation rlDest, RegLocation rlSrc1,
2367 RegLocation rlSrc2)
2368{
Bill Buzbeea114add2012-05-03 15:00:40 -07002369 RegLocation rlResult;
2370 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002371
Bill Buzbeea114add2012-05-03 15:00:40 -07002372 switch (mir->dalvikInsn.opcode) {
2373 case Instruction::ADD_DOUBLE_2ADDR:
2374 case Instruction::ADD_DOUBLE:
2375 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2376 break;
2377 case Instruction::SUB_DOUBLE_2ADDR:
2378 case Instruction::SUB_DOUBLE:
2379 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2380 break;
2381 case Instruction::DIV_DOUBLE_2ADDR:
2382 case Instruction::DIV_DOUBLE:
2383 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2384 break;
2385 case Instruction::MUL_DOUBLE_2ADDR:
2386 case Instruction::MUL_DOUBLE:
2387 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2388 break;
2389 case Instruction::REM_DOUBLE_2ADDR:
2390 case Instruction::REM_DOUBLE:
2391 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2392 break;
2393 case Instruction::NEG_DOUBLE: {
2394 genNegDouble(cUnit, rlDest, rlSrc1);
2395 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002396 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002397 default:
2398 return true;
2399 }
2400 oatFlushAllRegs(cUnit); /* Send everything to home location */
2401 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2402 rlResult = oatGetReturnWide(cUnit, true);
2403 storeValueWide(cUnit, rlDest, rlResult);
2404 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002405}
2406
2407bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
2408{
Bill Buzbeea114add2012-05-03 15:00:40 -07002409 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbee31a4a6f2012-02-28 15:36:15 -08002410
Bill Buzbeea114add2012-05-03 15:00:40 -07002411 switch (opcode) {
2412 case Instruction::INT_TO_FLOAT:
2413 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pI2f),
2414 1, 1);
2415 case Instruction::FLOAT_TO_INT:
2416 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pF2iz),
2417 1, 1);
2418 case Instruction::DOUBLE_TO_FLOAT:
2419 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pD2f),
2420 2, 1);
2421 case Instruction::FLOAT_TO_DOUBLE:
2422 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pF2d),
2423 1, 2);
2424 case Instruction::INT_TO_DOUBLE:
2425 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pI2d),
2426 1, 2);
2427 case Instruction::DOUBLE_TO_INT:
2428 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pD2iz),
2429 2, 1);
2430 case Instruction::FLOAT_TO_LONG:
2431 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pF2l),
2432 1, 2);
2433 case Instruction::LONG_TO_FLOAT:
2434 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pL2f),
2435 2, 1);
2436 case Instruction::DOUBLE_TO_LONG:
2437 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pD2l),
2438 2, 2);
2439 case Instruction::LONG_TO_DOUBLE:
2440 return genConversionCall(cUnit, mir, ENTRYPOINT_OFFSET(pL2d),
2441 2, 2);
2442 default:
2443 return true;
2444 }
2445 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002446}
2447
2448/*
2449 * Generate callout to updateDebugger. Note that we're overloading
2450 * the use of rSUSPEND here. When the debugger is active, this
2451 * register holds the address of the update function. So, if it's
2452 * non-null, we call out to it.
2453 *
2454 * Note also that rRET0 and rRET1 must be preserved across this
2455 * code. This must be handled by the stub.
2456 */
2457void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2458{
Bill Buzbeea114add2012-05-03 15:00:40 -07002459 // Following DCHECK verifies that dPC is in range of single load immediate
2460 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2461 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2462 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002463#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002464 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2465 opIT(cUnit, kArmCondNe, "T");
2466 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2467 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002468#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002469 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002470#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002471 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2472 loadConstant(cUnit, rARG2, offset);
2473 opReg(cUnit, kOpBlx, rSUSPEND);
2474 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2475 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002476#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002477 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002478}
2479
2480/* Check if we need to check for pending suspend request */
2481void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
2482{
Bill Buzbeea114add2012-05-03 15:00:40 -07002483 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2484 return;
2485 }
2486 oatFlushAllRegs(cUnit);
2487 if (cUnit->genDebugger) {
2488 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002489#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002490 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002491#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002492 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
2493 opReg(cUnit, kOpBlx, rTgt);
2494 // Refresh rSUSPEND
2495 loadWordDisp(cUnit, rSELF,
2496 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2497 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002498#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002499 } else {
2500 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002501#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002502 // In non-debug case, only check periodically
2503 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2504 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002505#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002506 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2507 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002508#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002509 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2510 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002511#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002512 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2513 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
2514 kPseudoSuspendTarget, (intptr_t)retLab, mir->offset);
2515 branch->target = (LIR*)target;
2516 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2517 }
buzbee31a4a6f2012-02-28 15:36:15 -08002518}
2519
buzbeefead2932012-03-30 14:02:01 -07002520/* Check if we need to check for pending suspend request */
2521void genSuspendTestAndBranch(CompilationUnit* cUnit, MIR* mir, LIR* target)
2522{
Bill Buzbeea114add2012-05-03 15:00:40 -07002523 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
2524 opUnconditionalBranch(cUnit, target);
2525 return;
2526 }
2527 if (cUnit->genDebugger) {
2528 genSuspendTest(cUnit, mir);
2529 opUnconditionalBranch(cUnit, target);
2530 } else {
buzbeefead2932012-03-30 14:02:01 -07002531#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002532 // In non-debug case, only check periodically
2533 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2534 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002535#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002536 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2537 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002538#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002539 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2540 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002541#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002542 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
2543 kPseudoSuspendTarget, (intptr_t)target, mir->offset);
2544 oatFlushAllRegs(cUnit);
2545 opUnconditionalBranch(cUnit, launchPad);
2546 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2547 (intptr_t)launchPad);
2548 }
buzbeefead2932012-03-30 14:02:01 -07002549}
2550
buzbee31a4a6f2012-02-28 15:36:15 -08002551} // namespace art