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