blob: 2ca90466eea849b8bbf346f54b773a78c70e5050 [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 */
buzbee15bf9802012-06-12 17:49:27 -070026void genInvoke(CompilationUnit* cUnit, InvokeInfo* info);
buzbee31a4a6f2012-02-28 15:36:15 -080027#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080028LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbeef3aac972012-04-11 16:33:36 -070029bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
30 RegLocation rlSrc, RegLocation rlDest, int lit);
buzbee31a4a6f2012-02-28 15:36:15 -080031#endif
32
Ian Rogersab2b55d2012-03-18 00:06:11 -070033void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0) {
34#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070035 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070036#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070037 loadConstant(cUnit, rARG0, arg0);
38 oatClobberCalleeSave(cUnit);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070039#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070040 opReg(cUnit, kOpBlx, rTgt);
41 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070042#else
Bill Buzbeea114add2012-05-03 15:00:40 -070043 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070044#endif
45}
46
Ian Rogers7caad772012-03-30 01:07:54 -070047void callRuntimeHelperReg(CompilationUnit* cUnit, int helperOffset, int arg0) {
48#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070049 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070050#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070051 opRegCopy(cUnit, rARG0, arg0);
52 oatClobberCalleeSave(cUnit);
Ian Rogers7caad772012-03-30 01:07:54 -070053#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070054 opReg(cUnit, kOpBlx, rTgt);
55 oatFreeTemp(cUnit, rTgt);
Ian Rogers7caad772012-03-30 01:07:54 -070056#else
Bill Buzbeea114add2012-05-03 15:00:40 -070057 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070058#endif
59}
60
Ian Rogersab2b55d2012-03-18 00:06:11 -070061void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset,
62 RegLocation arg0) {
63#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070064 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070065#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070066 if (arg0.wide == 0) {
67 loadValueDirectFixed(cUnit, arg0, rARG0);
68 } else {
69 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
70 }
71 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -070072#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070073 opReg(cUnit, kOpBlx, rTgt);
74 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -070075#else
Bill Buzbeea114add2012-05-03 15:00:40 -070076 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070077#endif
78}
79
80void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset,
81 int arg0, int arg1) {
82#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070083 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070084#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070085 loadConstant(cUnit, rARG0, arg0);
86 loadConstant(cUnit, rARG1, arg1);
87 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -070088#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070089 opReg(cUnit, kOpBlx, rTgt);
90 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -070091#else
Bill Buzbeea114add2012-05-03 15:00:40 -070092 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070093#endif
94}
95
96void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset,
97 int arg0, RegLocation arg1) {
98#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070099 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700100#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700101 if (arg1.wide == 0) {
102 loadValueDirectFixed(cUnit, arg1, rARG1);
103 } else {
104 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
105 }
106 loadConstant(cUnit, rARG0, arg0);
107 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700108#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 opReg(cUnit, kOpBlx, rTgt);
110 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700111#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700113#endif
114}
115
116void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset,
117 RegLocation arg0, int arg1) {
118#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700120#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700121 loadValueDirectFixed(cUnit, arg0, rARG0);
122 loadConstant(cUnit, rARG1, arg1);
123 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700124#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 opReg(cUnit, kOpBlx, rTgt);
126 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700127#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700129#endif
130}
131
132void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset,
133 int arg0, int arg1) {
134#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700135 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700136#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 opRegCopy(cUnit, rARG1, arg1);
138 loadConstant(cUnit, rARG0, arg0);
139 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700140#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 opReg(cUnit, kOpBlx, rTgt);
142 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700143#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700145#endif
146}
147
148void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset,
149 int arg0, int arg1) {
150#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700152#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 opRegCopy(cUnit, rARG0, arg0);
154 loadConstant(cUnit, rARG1, arg1);
155 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700156#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 opReg(cUnit, kOpBlx, rTgt);
158 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700159#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700161#endif
162}
163
164void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset,
165 int arg0) {
166#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700168#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 loadCurrMethodDirect(cUnit, rARG1);
170 loadConstant(cUnit, rARG0, arg0);
171 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700172#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 opReg(cUnit, kOpBlx, rTgt);
174 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700175#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700177#endif
178}
179
180void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit,
181 int helperOffset,
182 RegLocation arg0,
183 RegLocation arg1) {
184#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700186#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 if (arg0.wide == 0) {
188 loadValueDirectFixed(cUnit, arg0, rARG0);
189 if (arg1.wide == 0) {
190 loadValueDirectFixed(cUnit, arg1, rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700191 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700193 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 } else {
195 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
196 if (arg1.wide == 0) {
197 loadValueDirectFixed(cUnit, arg1, rARG2);
198 } else {
199 loadValueDirectWideFixed(cUnit, arg1, rARG2, rARG3);
200 }
201 }
202 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700203#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 opReg(cUnit, kOpBlx, rTgt);
205 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700206#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700208#endif
209}
210
211void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
212 int arg0, int arg1) {
213#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700215#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
217 opRegCopy(cUnit, rARG0, arg0);
218 opRegCopy(cUnit, rARG1, arg1);
219 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700220#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700221 opReg(cUnit, kOpBlx, rTgt);
222 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700223#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700225#endif
226}
227
228void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset,
229 int arg0, int arg1, int arg2) {
230#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700232#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
234 opRegCopy(cUnit, rARG0, arg0);
235 opRegCopy(cUnit, rARG1, arg1);
236 loadConstant(cUnit, rARG2, arg2);
237 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700238#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 opReg(cUnit, kOpBlx, rTgt);
240 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700241#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700243#endif
244}
245
Bill Buzbeea114add2012-05-03 15:00:40 -0700246void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit,
247 int helperOffset,
248 int arg0, RegLocation arg2) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700249#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700251#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 loadValueDirectFixed(cUnit, arg2, rARG2);
253 loadCurrMethodDirect(cUnit, rARG1);
254 loadConstant(cUnit, rARG0, arg0);
255 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700256#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 opReg(cUnit, kOpBlx, rTgt);
258 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700259#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700261#endif
262}
263
264void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset,
265 int arg0, int arg2) {
266#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700268#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 loadCurrMethodDirect(cUnit, rARG1);
270 loadConstant(cUnit, rARG2, arg2);
271 loadConstant(cUnit, rARG0, arg0);
272 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700273#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 opReg(cUnit, kOpBlx, rTgt);
275 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700276#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700278#endif
279}
280
281void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit,
282 int helperOffset,
283 int arg0, RegLocation arg1,
284 RegLocation arg2) {
285#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700287#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 loadValueDirectFixed(cUnit, arg1, rARG1);
289 if (arg2.wide == 0) {
290 loadValueDirectFixed(cUnit, arg2, rARG2);
291 } else {
292 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
293 }
294 loadConstant(cUnit, rARG0, arg0);
295 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700296#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 opReg(cUnit, kOpBlx, rTgt);
298 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700299#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700301#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800302}
303
304/*
305 * Generate an kPseudoBarrier marker to indicate the boundary of special
306 * blocks.
307 */
308void genBarrier(CompilationUnit* cUnit)
309{
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
311 /* Mark all resources as being clobbered */
312 barrier->defMask = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800313}
314
buzbee31a4a6f2012-02-28 15:36:15 -0800315
316/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800317LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800318{
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
320 branch->target = (LIR*) target;
321 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800322}
323
buzbee5de34942012-03-01 14:51:57 -0800324// FIXME: need to do some work to split out targets with
325// condition codes and those without
326#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -0700327LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee31a4a6f2012-02-28 15:36:15 -0800328 ThrowKind kind)
329{
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700331 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 LIR* branch = opCondBranch(cUnit, cCode, tgt);
333 // Remember branch target - will process later
334 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
335 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800336}
buzbee5de34942012-03-01 14:51:57 -0800337#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800338
339LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700340 int reg, int immVal, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800341{
buzbee408ad162012-06-06 16:45:18 -0700342 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
343 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 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. */
buzbee408ad162012-06-06 16:45:18 -0700356LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -0800357{
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
buzbee408ad162012-06-06 16:45:18 -0700359 optFlags & MIR_IGNORE_NULL_CHECK) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 return NULL;
361 }
buzbee408ad162012-06-06 16:45:18 -0700362 return genImmedCheck(cUnit, kCondEq, mReg, 0, kThrowNullPointer);
buzbee31a4a6f2012-02-28 15:36:15 -0800363}
364
365/* Perform check on two registers */
366LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700367 int reg1, int reg2, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800368{
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700370 cUnit->currentDalvikOffset, 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
buzbee408ad162012-06-06 16:45:18 -0700382void genCompareAndBranch(CompilationUnit* cUnit, BasicBlock* bb,
383 Instruction::Code opcode, RegLocation rlSrc1,
384 RegLocation rlSrc2, LIR* labelList)
buzbee31a4a6f2012-02-28 15:36:15 -0800385{
Bill Buzbeea114add2012-05-03 15:00:40 -0700386 ConditionCode cond;
387 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
388 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 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
buzbee408ad162012-06-06 16:45:18 -0700422void genCompareZeroAndBranch(CompilationUnit* cUnit, BasicBlock* bb,
423 Instruction::Code opcode, RegLocation rlSrc,
424 LIR* labelList)
buzbee31a4a6f2012-02-28 15:36:15 -0800425{
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 ConditionCode cond;
427 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 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
buzbee408ad162012-06-06 16:45:18 -0700460void genIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800461 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
buzbee408ad162012-06-06 16:45:18 -0700473void genIntNarrowing(CompilationUnit* cUnit, Instruction::Code opcode,
474 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800475{
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;
buzbee408ad162012-06-06 16:45:18 -0700479 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 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 */
buzbee408ad162012-06-06 16:45:18 -0700501void genNewArray(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800502 RegLocation rlSrc)
503{
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 int funcOffset;
506 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
507 cUnit->dex_cache,
508 *cUnit->dex_file,
509 type_idx)) {
510 funcOffset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
511 } else {
512 funcOffset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
513 }
514 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
515 RegLocation rlResult = oatGetReturn(cUnit, false);
516 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800517}
518
519/*
520 * Similar to genNewArray, but with post-allocation initialization.
521 * Verifier guarantees we're dealing with an array class. Current
522 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
523 * Current code also throws internal unimp if not 'L', '[' or 'I'.
524 */
525void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
526{
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 DecodedInstruction* dInsn = &mir->dalvikInsn;
528 int elems = dInsn->vA;
529 int typeIdx = dInsn->vB;
530 oatFlushAllRegs(cUnit); /* Everything to home location */
531 int funcOffset;
532 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
533 cUnit->dex_cache,
534 *cUnit->dex_file,
535 typeIdx)) {
536 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
537 } else {
538 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
539 }
540 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
541 oatFreeTemp(cUnit, rARG2);
542 oatFreeTemp(cUnit, rARG1);
543 /*
544 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
545 * return region. Because AllocFromCode placed the new array
546 * in rRET0, we'll just lock it into place. When debugger support is
547 * added, it may be necessary to additionally copy all return
548 * values to a home location in thread-local storage
549 */
550 oatLockTemp(cUnit, rRET0);
551
552 // TODO: use the correct component size, currently all supported types
553 // share array alignment with ints (see comment at head of function)
554 size_t component_size = sizeof(int32_t);
555
556 // Having a range of 0 is legal
557 if (isRange && (dInsn->vA > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800558 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 * Bit of ugliness here. We're going generate a mem copy loop
560 * on the register range, but it is possible that some regs
561 * in the range have been promoted. This is unlikely, but
562 * before generating the copy, we'll just force a flush
563 * of any regs in the source range that have been promoted to
564 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800565 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 for (unsigned int i = 0; i < dInsn->vA; i++) {
567 RegLocation loc = oatUpdateLoc(cUnit, oatGetSrc(cUnit, mir, i));
568 if (loc.location == kLocPhysReg) {
569 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
570 loc.lowReg, kWord);
571 }
buzbee31a4a6f2012-02-28 15:36:15 -0800572 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 /*
574 * TUNING note: generated code here could be much improved, but
575 * this is an uncommon operation and isn't especially performance
576 * critical.
577 */
578 int rSrc = oatAllocTemp(cUnit);
579 int rDst = oatAllocTemp(cUnit);
580 int rIdx = oatAllocTemp(cUnit);
581#if defined(TARGET_ARM)
582 int rVal = rLR; // Using a lot of temps, rLR is known free here
583#elif defined(TARGET_X86)
jeffhao5772bab2012-05-18 11:51:26 -0700584 oatFreeTemp(cUnit, rRET0);
585 int rVal = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700586#else
587 int rVal = oatAllocTemp(cUnit);
588#endif
589 // Set up source pointer
590 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
591 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
592 oatSRegOffset(cUnit, rlFirst.sRegLow));
593 // Set up the target pointer
594 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
595 Array::DataOffset(component_size).Int32Value());
596 // Set up the loop counter (known to be > 0)
597 loadConstant(cUnit, rIdx, dInsn->vA - 1);
598 // Generate the copy loop. Going backwards for convenience
599 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
600 // Copy next element
601 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
602 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
603#if defined(TARGET_ARM)
604 // Combine sub & test using sub setflags encoding here
605 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
606 opCondBranch(cUnit, kCondGe, target);
607#else
608 oatFreeTemp(cUnit, rVal);
609 opRegImm(cUnit, kOpSub, rIdx, 1);
610 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
611#endif
jeffhao5772bab2012-05-18 11:51:26 -0700612#if defined(TARGET_X86)
613 // Restore the target pointer
614 opRegRegImm(cUnit, kOpAdd, rRET0, rDst,
615 -Array::DataOffset(component_size).Int32Value());
616#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700617 } else if (!isRange) {
618 // TUNING: interleave
619 for (unsigned int i = 0; i < dInsn->vA; i++) {
620 RegLocation rlArg = loadValue(cUnit, oatGetSrc(cUnit, mir, i), kCoreReg);
621 storeBaseDisp(cUnit, rRET0,
622 Array::DataOffset(component_size).Int32Value() +
623 i * 4, rlArg.lowReg, kWord);
624 // If the loadValue caused a temp to be allocated, free it
625 if (oatIsTemp(cUnit, rlArg.lowReg)) {
626 oatFreeTemp(cUnit, rlArg.lowReg);
627 }
628 }
629 }
buzbee31a4a6f2012-02-28 15:36:15 -0800630}
631
buzbee408ad162012-06-06 16:45:18 -0700632void genSput(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800634{
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 int fieldOffset;
636 int ssbIndex;
637 bool isVolatile;
638 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800639
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
641 *cUnit->dex_file, *cUnit->dex_cache,
642 cUnit->code_item, cUnit->method_idx,
643 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800644
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 bool fastPath =
646 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
647 fieldOffset, ssbIndex,
648 isReferrersClass, isVolatile,
649 true);
650 if (fastPath && !SLOW_FIELD_PATH) {
651 DCHECK_GE(fieldOffset, 0);
652 int rBase;
653 if (isReferrersClass) {
654 // Fast path, static storage base is this method's class
655 RegLocation rlMethod = loadCurrMethod(cUnit);
656 rBase = oatAllocTemp(cUnit);
657 loadWordDisp(cUnit, rlMethod.lowReg,
658 Method::DeclaringClassOffset().Int32Value(), rBase);
659 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
660 oatFreeTemp(cUnit, rlMethod.lowReg);
661 }
buzbee31a4a6f2012-02-28 15:36:15 -0800662 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 // Medium path, static storage base in a different class which
664 // requires checks that the other class is initialized.
665 DCHECK_GE(ssbIndex, 0);
666 // May do runtime call so everything to home locations.
667 oatFlushAllRegs(cUnit);
668 // Using fixed register to sync with possible call to runtime
669 // support.
670 int rMethod = rARG1;
671 oatLockTemp(cUnit, rMethod);
672 loadCurrMethodDirect(cUnit, rMethod);
673 rBase = rARG0;
674 oatLockTemp(cUnit, rBase);
675 loadWordDisp(cUnit, rMethod,
676 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
677 rBase);
678 loadWordDisp(cUnit, rBase,
679 Array::DataOffset(sizeof(Object*)).Int32Value() +
680 sizeof(int32_t*) * ssbIndex, rBase);
681 // rBase now points at appropriate static storage base (Class*)
682 // or NULL if not initialized. Check for NULL and call helper if NULL.
683 // TUNING: fast path should fall through
684 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
685 loadConstant(cUnit, rARG0, ssbIndex);
686 callRuntimeHelperImm(cUnit,
687 ENTRYPOINT_OFFSET(pInitializeStaticStorage),
688 ssbIndex);
689#if defined(TARGET_MIPS)
690 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
691 opRegCopy(cUnit, rBase, rRET0);
692#endif
693 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
694 branchOver->target = (LIR*)skipTarget;
695 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800696 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700697 // rBase now holds static storage base
698 if (isLongOrDouble) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
700 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
702 }
703//FIXME: need to generalize the barrier call
704 if (isVolatile) {
705 oatGenMemBarrier(cUnit, kST);
706 }
707 if (isLongOrDouble) {
708 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
709 rlSrc.highReg);
710 } else {
711 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
712 }
713 if (isVolatile) {
714 oatGenMemBarrier(cUnit, kSY);
715 }
716 if (isObject) {
717 markGCCard(cUnit, rlSrc.lowReg, rBase);
718 }
719 oatFreeTemp(cUnit, rBase);
720 } else {
721 oatFlushAllRegs(cUnit); // Everything to home locations
722 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Static) :
723 (isObject ? ENTRYPOINT_OFFSET(pSetObjStatic)
724 : ENTRYPOINT_OFFSET(pSet32Static));
725 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
726 }
buzbee31a4a6f2012-02-28 15:36:15 -0800727}
728
buzbee408ad162012-06-06 16:45:18 -0700729void genSget(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700730 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800731{
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 int fieldOffset;
733 int ssbIndex;
734 bool isVolatile;
735 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800736
Bill Buzbeea114add2012-05-03 15:00:40 -0700737 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
738 *cUnit->dex_file, *cUnit->dex_cache,
739 cUnit->code_item, cUnit->method_idx,
740 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800741
Bill Buzbeea114add2012-05-03 15:00:40 -0700742 bool fastPath =
743 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
744 fieldOffset, ssbIndex,
745 isReferrersClass, isVolatile,
746 false);
747 if (fastPath && !SLOW_FIELD_PATH) {
748 DCHECK_GE(fieldOffset, 0);
749 int rBase;
750 if (isReferrersClass) {
751 // Fast path, static storage base is this method's class
752 RegLocation rlMethod = loadCurrMethod(cUnit);
753 rBase = oatAllocTemp(cUnit);
754 loadWordDisp(cUnit, rlMethod.lowReg,
755 Method::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800756 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700757 // Medium path, static storage base in a different class which
758 // requires checks that the other class is initialized
759 DCHECK_GE(ssbIndex, 0);
760 // May do runtime call so everything to home locations.
761 oatFlushAllRegs(cUnit);
762 // Using fixed register to sync with possible call to runtime
763 // support
764 int rMethod = rARG1;
765 oatLockTemp(cUnit, rMethod);
766 loadCurrMethodDirect(cUnit, rMethod);
767 rBase = rARG0;
768 oatLockTemp(cUnit, rBase);
769 loadWordDisp(cUnit, rMethod,
770 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
771 rBase);
772 loadWordDisp(cUnit, rBase,
773 Array::DataOffset(sizeof(Object*)).Int32Value() +
774 sizeof(int32_t*) * ssbIndex, rBase);
775 // rBase now points at appropriate static storage base (Class*)
776 // or NULL if not initialized. Check for NULL and call helper if NULL.
777 // TUNING: fast path should fall through
778 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
779 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage),
780 ssbIndex);
781#if defined(TARGET_MIPS)
782 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
783 opRegCopy(cUnit, rBase, rRET0);
784#endif
785 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
786 branchOver->target = (LIR*)skipTarget;
787 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800788 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700789 // rBase now holds static storage base
Bill Buzbeea114add2012-05-03 15:00:40 -0700790 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
791 if (isVolatile) {
792 oatGenMemBarrier(cUnit, kSY);
793 }
794 if (isLongOrDouble) {
buzbee408ad162012-06-06 16:45:18 -0700795 loadBaseDispWide(cUnit, rBase, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700796 rlResult.highReg, INVALID_SREG);
797 } else {
798 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
799 }
800 oatFreeTemp(cUnit, rBase);
801 if (isLongOrDouble) {
802 storeValueWide(cUnit, rlDest, rlResult);
803 } else {
804 storeValue(cUnit, rlDest, rlResult);
805 }
806 } else {
807 oatFlushAllRegs(cUnit); // Everything to home locations
808 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Static) :
809 (isObject ? ENTRYPOINT_OFFSET(pGetObjStatic)
810 : ENTRYPOINT_OFFSET(pGet32Static));
811 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
812 if (isLongOrDouble) {
813 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
814 storeValueWide(cUnit, rlDest, rlResult);
815 } else {
816 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
817 storeValue(cUnit, rlDest, rlResult);
818 }
819 }
buzbee31a4a6f2012-02-28 15:36:15 -0800820}
821
822
823// Debugging routine - if null target, branch to DebugMe
824void genShowTarget(CompilationUnit* cUnit)
825{
buzbeea7678db2012-03-05 15:35:46 -0800826#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 UNIMPLEMENTED(WARNING) << "genShowTarget";
buzbeea7678db2012-03-05 15:35:46 -0800828#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
830 loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pDebugMe), rINVOKE_TGT);
831 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
832 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800833#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800834}
835
buzbee408ad162012-06-06 16:45:18 -0700836void genThrowVerificationError(CompilationUnit* cUnit, int info1, int info2)
buzbee31a4a6f2012-02-28 15:36:15 -0800837{
Bill Buzbeea114add2012-05-03 15:00:40 -0700838 callRuntimeHelperImmImm(cUnit,
839 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode),
buzbee408ad162012-06-06 16:45:18 -0700840 info1, info2);
buzbee31a4a6f2012-02-28 15:36:15 -0800841}
842
843void handleSuspendLaunchpads(CompilationUnit *cUnit)
844{
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
846 int numElems = cUnit->suspendLaunchpads.numUsed;
847 for (int i = 0; i < numElems; i++) {
848 oatResetRegPool(cUnit);
849 oatResetDefTracking(cUnit);
850 LIR* lab = suspendLabel[i];
851 LIR* resumeLab = (LIR*)lab->operands[0];
852 cUnit->currentDalvikOffset = lab->operands[1];
853 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700854#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700855 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700856#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700857 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
858 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700859#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700860 opUnconditionalBranch(cUnit, resumeLab);
861 }
buzbee31a4a6f2012-02-28 15:36:15 -0800862}
863
buzbeefc9e6fa2012-03-23 15:14:29 -0700864void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
865{
Bill Buzbeea114add2012-05-03 15:00:40 -0700866 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
867 int numElems = cUnit->intrinsicLaunchpads.numUsed;
868 for (int i = 0; i < numElems; i++) {
869 oatResetRegPool(cUnit);
870 oatResetDefTracking(cUnit);
871 LIR* lab = intrinsicLabel[i];
buzbee15bf9802012-06-12 17:49:27 -0700872 InvokeInfo* info = (InvokeInfo*)lab->operands[0];
873 cUnit->currentDalvikOffset = info->offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700874 oatAppendLIR(cUnit, lab);
buzbee15bf9802012-06-12 17:49:27 -0700875 genInvoke(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 LIR* resumeLab = (LIR*)lab->operands[2];
877 if (resumeLab != NULL) {
878 opUnconditionalBranch(cUnit, resumeLab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700879 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700881}
882
buzbee31a4a6f2012-02-28 15:36:15 -0800883void handleThrowLaunchpads(CompilationUnit *cUnit)
884{
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
886 int numElems = cUnit->throwLaunchpads.numUsed;
887 for (int i = 0; i < numElems; i++) {
888 oatResetRegPool(cUnit);
889 oatResetDefTracking(cUnit);
890 LIR* lab = throwLabel[i];
891 cUnit->currentDalvikOffset = lab->operands[1];
892 oatAppendLIR(cUnit, lab);
893 int funcOffset = 0;
894 int v1 = lab->operands[2];
895 int v2 = lab->operands[3];
896 switch (lab->operands[0]) {
897 case kThrowNullPointer:
898 funcOffset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
899 break;
900 case kThrowArrayBounds:
901 if (v2 != rARG0) {
902 opRegCopy(cUnit, rARG0, v1);
903 opRegCopy(cUnit, rARG1, v2);
904 } else {
905 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800906#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700907 int rTmp = r12;
buzbee31a4a6f2012-02-28 15:36:15 -0800908#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700909 int rTmp = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800910#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 opRegCopy(cUnit, rTmp, v1);
912 opRegCopy(cUnit, rARG1, v2);
913 opRegCopy(cUnit, rARG0, rTmp);
914 } else {
915 opRegCopy(cUnit, rARG1, v2);
916 opRegCopy(cUnit, rARG0, v1);
917 }
buzbee31a4a6f2012-02-28 15:36:15 -0800918 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 funcOffset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
920 break;
921 case kThrowDivZero:
922 funcOffset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
923 break;
924 case kThrowVerificationError:
925 loadConstant(cUnit, rARG0, v1);
926 loadConstant(cUnit, rARG1, v2);
927 funcOffset =
928 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode);
929 break;
930 case kThrowNoSuchMethod:
931 opRegCopy(cUnit, rARG0, v1);
932 funcOffset =
933 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
934 break;
935 case kThrowStackOverflow:
936 funcOffset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
937 // Restore stack alignment
Ian Rogersab2b55d2012-03-18 00:06:11 -0700938#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700939 opRegImm(cUnit, kOpAdd, rSP,
940 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700941#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700943#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700944 break;
945 default:
946 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800947 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 oatClobberCalleeSave(cUnit);
949#if !defined(TARGET_X86)
950 int rTgt = loadHelper(cUnit, funcOffset);
951 opReg(cUnit, kOpBlx, rTgt);
952 oatFreeTemp(cUnit, rTgt);
953#else
954 opThreadMem(cUnit, kOpBlx, funcOffset);
955#endif
956 }
buzbee31a4a6f2012-02-28 15:36:15 -0800957}
958
959/* Needed by the Assembler */
960void oatSetupResourceMasks(LIR* lir)
961{
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 setupResourceMasks(lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800963}
964
buzbee16da88c2012-03-20 10:38:17 -0700965bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
966 int& fieldOffset, bool& isVolatile, bool isPut)
967{
Bill Buzbeea114add2012-05-03 15:00:40 -0700968 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
969 *cUnit->dex_file, *cUnit->dex_cache,
970 cUnit->code_item, cUnit->method_idx,
971 cUnit->access_flags);
972 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
973 fieldOffset, isVolatile, isPut);
buzbee16da88c2012-03-20 10:38:17 -0700974}
975
buzbee408ad162012-06-06 16:45:18 -0700976void genIGet(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -0800977 RegLocation rlDest, RegLocation rlObj,
Bill Buzbeea114add2012-05-03 15:00:40 -0700978 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800979{
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 int fieldOffset;
981 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800982
Bill Buzbeea114add2012-05-03 15:00:40 -0700983 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800984
Bill Buzbeea114add2012-05-03 15:00:40 -0700985 if (fastPath && !SLOW_FIELD_PATH) {
986 RegLocation rlResult;
987 RegisterClass regClass = oatRegClassBySize(size);
988 DCHECK_GE(fieldOffset, 0);
989 rlObj = loadValue(cUnit, rlObj, kCoreReg);
990 if (isLongOrDouble) {
991 DCHECK(rlDest.wide);
buzbee408ad162012-06-06 16:45:18 -0700992 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800993#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700994 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -0700995 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
996 loadBaseDispWide(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700997 rlResult.highReg, rlObj.sRegLow);
998 if (isVolatile) {
999 oatGenMemBarrier(cUnit, kSY);
1000 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001001#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001002 int regPtr = oatAllocTemp(cUnit);
1003 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1004 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1005 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1006 if (isVolatile) {
1007 oatGenMemBarrier(cUnit, kSY);
1008 }
1009 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001010#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001011 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001012 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001013 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001014 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
1015 loadBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -07001016 kWord, rlObj.sRegLow);
1017 if (isVolatile) {
1018 oatGenMemBarrier(cUnit, kSY);
1019 }
1020 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001021 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001022 } else {
1023 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Instance) :
1024 (isObject ? ENTRYPOINT_OFFSET(pGetObjInstance)
1025 : ENTRYPOINT_OFFSET(pGet32Instance));
1026 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
1027 if (isLongOrDouble) {
1028 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
1029 storeValueWide(cUnit, rlDest, rlResult);
1030 } else {
1031 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
1032 storeValue(cUnit, rlDest, rlResult);
1033 }
1034 }
buzbee31a4a6f2012-02-28 15:36:15 -08001035}
1036
buzbee408ad162012-06-06 16:45:18 -07001037void genIPut(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
1038 RegLocation rlSrc, RegLocation rlObj, bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -08001039{
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 int fieldOffset;
1041 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -08001042
Bill Buzbeea114add2012-05-03 15:00:40 -07001043 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1044 true);
1045 if (fastPath && !SLOW_FIELD_PATH) {
1046 RegisterClass regClass = oatRegClassBySize(size);
1047 DCHECK_GE(fieldOffset, 0);
1048 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1049 if (isLongOrDouble) {
1050 int regPtr;
1051 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee408ad162012-06-06 16:45:18 -07001052 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001053 regPtr = oatAllocTemp(cUnit);
1054 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1055 if (isVolatile) {
1056 oatGenMemBarrier(cUnit, kST);
1057 }
jeffhao41005dd2012-05-09 17:58:52 -07001058 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 if (isVolatile) {
1060 oatGenMemBarrier(cUnit, kSY);
1061 }
1062 oatFreeTemp(cUnit, regPtr);
buzbee31a4a6f2012-02-28 15:36:15 -08001063 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001064 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee408ad162012-06-06 16:45:18 -07001065 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001066 if (isVolatile) {
1067 oatGenMemBarrier(cUnit, kST);
1068 }
1069 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1070 if (isVolatile) {
1071 oatGenMemBarrier(cUnit, kSY);
1072 }
1073 if (isObject) {
1074 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1075 }
buzbee31a4a6f2012-02-28 15:36:15 -08001076 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001077 } else {
1078 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Instance) :
1079 (isObject ? ENTRYPOINT_OFFSET(pSetObjInstance)
1080 : ENTRYPOINT_OFFSET(pSet32Instance));
1081 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1082 fieldIdx, rlObj, rlSrc);
1083 }
buzbee31a4a6f2012-02-28 15:36:15 -08001084}
1085
buzbee408ad162012-06-06 16:45:18 -07001086void genConstClass(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001087 RegLocation rlSrc)
1088{
Bill Buzbeea114add2012-05-03 15:00:40 -07001089 RegLocation rlMethod = loadCurrMethod(cUnit);
1090 int resReg = oatAllocTemp(cUnit);
1091 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1092 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1093 cUnit->dex_cache,
1094 *cUnit->dex_file,
1095 type_idx)) {
1096 // Call out to helper which resolves type and verifies access.
1097 // Resolved type returned in rRET0.
1098 callRuntimeHelperImmReg(cUnit,
1099 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1100 type_idx, rlMethod.lowReg);
1101 RegLocation rlResult = oatGetReturn(cUnit, false);
1102 storeValue(cUnit, rlDest, rlResult);
1103 } else {
1104 // We're don't need access checks, load type from dex cache
1105 int32_t dex_cache_offset =
1106 Method::DexCacheResolvedTypesOffset().Int32Value();
1107 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
1108 int32_t offset_of_type =
1109 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1110 * type_idx);
1111 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1112 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1113 type_idx) || SLOW_TYPE_PATH) {
1114 // Slow path, at runtime test if type is null and if so initialize
1115 oatFlushAllRegs(cUnit);
1116 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, NULL);
1117 // Resolved, store and hop over following code
1118 storeValue(cUnit, rlDest, rlResult);
1119 /*
1120 * Because we have stores of the target value on two paths,
1121 * clobber temp tracking for the destination using the ssa name
1122 */
1123 oatClobberSReg(cUnit, rlDest.sRegLow);
1124 LIR* branch2 = opUnconditionalBranch(cUnit,0);
1125 // TUNING: move slow path to end & remove unconditional branch
1126 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
1127 // Call out to helper, which will return resolved type in rARG0
1128 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1129 type_idx, rlMethod.lowReg);
1130 RegLocation rlResult = oatGetReturn(cUnit, false);
1131 storeValue(cUnit, rlDest, rlResult);
1132 /*
1133 * Because we have stores of the target value on two paths,
1134 * clobber temp tracking for the destination using the ssa name
1135 */
1136 oatClobberSReg(cUnit, rlDest.sRegLow);
1137 // Rejoin code paths
1138 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
1139 branch1->target = (LIR*)target1;
1140 branch2->target = (LIR*)target2;
buzbee31a4a6f2012-02-28 15:36:15 -08001141 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001142 // Fast path, we're done - just store result
1143 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001144 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001145 }
buzbee31a4a6f2012-02-28 15:36:15 -08001146}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001147
buzbee408ad162012-06-06 16:45:18 -07001148void genConstString(CompilationUnit* cUnit, uint32_t string_idx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001149 RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001150{
Bill Buzbeea114add2012-05-03 15:00:40 -07001151 /* NOTE: Most strings should be available at compile time */
Bill Buzbeea114add2012-05-03 15:00:40 -07001152 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1153 (sizeof(String*) * string_idx);
1154 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1155 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1156 // slow path, resolve string if not in dex cache
1157 oatFlushAllRegs(cUnit);
1158 oatLockCallTemps(cUnit); // Using explicit registers
1159 loadCurrMethodDirect(cUnit, rARG2);
1160 loadWordDisp(cUnit, rARG2,
1161 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1162 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001163#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001164 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001165#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1167 loadConstant(cUnit, rARG1, string_idx);
buzbee31a4a6f2012-02-28 15:36:15 -08001168#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001169 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1170 genBarrier(cUnit);
1171 // For testing, always force through helper
1172 if (!EXERCISE_SLOWEST_STRING_PATH) {
1173 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001174 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001175 opRegCopy(cUnit, rARG0, rARG2); // .eq
1176 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
1177 oatFreeTemp(cUnit, rTgt);
1178#elif defined(TARGET_MIPS)
1179 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1180 opRegCopy(cUnit, rARG0, rARG2); // .eq
1181 opReg(cUnit, kOpBlx, rTgt);
1182 oatFreeTemp(cUnit, rTgt);
1183 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1184 branch->target = target;
1185#else
1186 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode),
1187 rARG2, rARG1);
1188#endif
1189 genBarrier(cUnit);
1190 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
1191 } else {
1192 RegLocation rlMethod = loadCurrMethod(cUnit);
1193 int resReg = oatAllocTemp(cUnit);
1194 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1195 loadWordDisp(cUnit, rlMethod.lowReg,
1196 Method::DexCacheStringsOffset().Int32Value(), resReg);
1197 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1198 storeValue(cUnit, rlDest, rlResult);
1199 }
buzbee31a4a6f2012-02-28 15:36:15 -08001200}
1201
1202/*
1203 * Let helper function take care of everything. Will
1204 * call Class::NewInstanceFromCode(type_idx, method);
1205 */
buzbee408ad162012-06-06 16:45:18 -07001206void genNewInstance(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001207{
Bill Buzbeea114add2012-05-03 15:00:40 -07001208 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -07001209 // alloc will always check for resolution, do we also need to verify
1210 // access because the verifier was unable to?
1211 int funcOffset;
1212 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1213 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
1214 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
1215 } else {
1216 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
1217 }
1218 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
1219 RegLocation rlResult = oatGetReturn(cUnit, false);
1220 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001221}
1222
buzbee408ad162012-06-06 16:45:18 -07001223void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001224{
Bill Buzbeea114add2012-05-03 15:00:40 -07001225 oatFlushAllRegs(cUnit);
1226 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException),
1227 rlSrc);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001228}
1229
buzbee408ad162012-06-06 16:45:18 -07001230void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001231 RegLocation rlSrc)
1232{
Bill Buzbeea114add2012-05-03 15:00:40 -07001233 oatFlushAllRegs(cUnit);
1234 // May generate a call - use explicit registers
1235 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001236 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1237 int classReg = rARG2; // rARG2 will hold the Class*
1238 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1239 cUnit->dex_cache,
1240 *cUnit->dex_file,
1241 type_idx)) {
1242 // Check we have access to type_idx and if not throw IllegalAccessError,
1243 // returns Class* in rARG0
1244 callRuntimeHelperImm(cUnit,
1245 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1246 type_idx);
1247 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1248 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1249 } else {
1250 // Load dex cache entry into classReg (rARG2)
1251 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1252 loadWordDisp(cUnit, rARG1,
1253 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1254 int32_t offset_of_type =
1255 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1256 * type_idx);
1257 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1258 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1259 cUnit->dex_cache, type_idx)) {
1260 // Need to test presence of type in dex cache at runtime
1261 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1262 // Not resolved
1263 // Call out to helper, which will return resolved type in rRET0
1264 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1265 type_idx);
1266 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1267 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1268 // Rejoin code paths
1269 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1270 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001271 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001272 }
1273 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
1274 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1275 /* load object->klass_ */
1276 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1277 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1278 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001279#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001280 /* Uses conditional nullification */
1281 int rTgt = loadHelper(cUnit,
1282 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1283 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1284 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1285 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1286 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1287 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1288 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001289#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001290 /* Uses branchovers */
1291 loadConstant(cUnit, rARG0, 1); // assume true
1292 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001293#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001294 int rTgt = loadHelper(cUnit,
1295 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1296 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1297 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1298 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001299#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001300 opRegCopy(cUnit, rARG0, rARG2);
1301 opThreadMem(cUnit, kOpBlx,
1302 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001303#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001304#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001305 oatClobberCalleeSave(cUnit);
1306 /* branch targets here */
1307 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1308 RegLocation rlResult = oatGetReturn(cUnit, false);
1309 storeValue(cUnit, rlDest, rlResult);
1310 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001311#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001312 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001313#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001314}
1315
buzbee408ad162012-06-06 16:45:18 -07001316void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001317{
Bill Buzbeea114add2012-05-03 15:00:40 -07001318 oatFlushAllRegs(cUnit);
1319 // May generate a call - use explicit registers
1320 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001321 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1322 int classReg = rARG2; // rARG2 will hold the Class*
1323 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1324 cUnit->dex_cache,
1325 *cUnit->dex_file,
1326 type_idx)) {
1327 // Check we have access to type_idx and if not throw IllegalAccessError,
1328 // returns Class* in rRET0
1329 // InitializeTypeAndVerifyAccess(idx, method)
1330 callRuntimeHelperImmReg(cUnit,
1331 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1332 type_idx, rARG1);
1333 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1334 } else {
1335 // Load dex cache entry into classReg (rARG2)
1336 loadWordDisp(cUnit, rARG1,
1337 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1338 int32_t offset_of_type =
1339 Array::DataOffset(sizeof(Class*)).Int32Value() +
1340 (sizeof(Class*) * type_idx);
1341 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1342 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1343 cUnit->dex_cache, type_idx)) {
1344 // Need to test presence of type in dex cache at runtime
1345 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1346 // Not resolved
1347 // Call out to helper, which will return resolved type in rARG0
1348 // InitializeTypeFromCode(idx, method)
1349 callRuntimeHelperImmReg(cUnit,
1350 ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1351 type_idx, rARG1);
1352 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1353 // Rejoin code paths
1354 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1355 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001356 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001357 }
1358 // At this point, classReg (rARG2) has class
1359 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1360 /* Null is OK - continue */
1361 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1362 /* load object->klass_ */
1363 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1364 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1365 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001366#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001367 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
1368 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode),
1369 rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001370#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001371 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1372 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1373 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1374 opRegCopy(cUnit, rARG0, rARG1);
1375 opRegCopy(cUnit, rARG1, rARG2);
1376 oatClobberCalleeSave(cUnit);
1377 opReg(cUnit, kOpBlx, rTgt);
1378 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001379#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001380 /* branch target here */
1381 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1382 branch1->target = target;
1383 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001384}
1385
buzbee31a4a6f2012-02-28 15:36:15 -08001386/*
1387 * Generate array store
1388 *
1389 */
buzbee408ad162012-06-06 16:45:18 -07001390void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001391 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001392{
Bill Buzbeea114add2012-05-03 15:00:40 -07001393 int lenOffset = Array::LengthOffset().Int32Value();
1394 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001395
Bill Buzbeea114add2012-05-03 15:00:40 -07001396 oatFlushAllRegs(cUnit); // Use explicit registers
1397 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001398
Bill Buzbeea114add2012-05-03 15:00:40 -07001399 int rValue = rARG0; // Register holding value
1400 int rArrayClass = rARG1; // Register holding array's Class
1401 int rArray = rARG2; // Register holding array
1402 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001403
Bill Buzbeea114add2012-05-03 15:00:40 -07001404 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1405 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1406 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001407
buzbee408ad162012-06-06 16:45:18 -07001408 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001409
Bill Buzbeea114add2012-05-03 15:00:40 -07001410 // Store of null?
1411 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001412
Bill Buzbeea114add2012-05-03 15:00:40 -07001413 // Get the array's class.
1414 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
1415 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode),
1416 rValue, rArrayClass);
1417 // Redo loadValues in case they didn't survive the call.
1418 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1419 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1420 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1421 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001422
Bill Buzbeea114add2012-05-03 15:00:40 -07001423 // Branch here if value to be stored == null
1424 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1425 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001426
Ian Rogersb41b33b2012-03-20 14:22:54 -07001427#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001428 // make an extra temp available for card mark below
1429 oatFreeTemp(cUnit, rARG1);
buzbee408ad162012-06-06 16:45:18 -07001430 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001431 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
buzbee408ad162012-06-06 16:45:18 -07001432 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001433 }
buzbee408ad162012-06-06 16:45:18 -07001434 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001435 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001436#else
buzbee408ad162012-06-06 16:45:18 -07001437 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001438 int regLen = INVALID_REG;
1439 if (needsRangeCheck) {
1440 regLen = rARG1;
1441 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); // Get len
1442 }
1443 /* rPtr -> array data */
1444 int rPtr = oatAllocTemp(cUnit);
1445 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1446 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001447 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001448 }
1449 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1450 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001451#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001452 oatFreeTemp(cUnit, rIndex);
1453 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001454}
1455
1456/*
1457 * Generate array load
1458 */
buzbee408ad162012-06-06 16:45:18 -07001459void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001460 RegLocation rlArray, RegLocation rlIndex,
1461 RegLocation rlDest, int scale)
1462{
Bill Buzbeea114add2012-05-03 15:00:40 -07001463 RegisterClass regClass = oatRegClassBySize(size);
1464 int lenOffset = Array::LengthOffset().Int32Value();
1465 int dataOffset;
1466 RegLocation rlResult;
1467 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1468 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001469
Bill Buzbeea114add2012-05-03 15:00:40 -07001470 if (size == kLong || size == kDouble) {
1471 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1472 } else {
1473 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1474 }
buzbee31a4a6f2012-02-28 15:36:15 -08001475
Bill Buzbeea114add2012-05-03 15:00:40 -07001476 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001477 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001478
Ian Rogersb5d09b22012-03-06 22:14:17 -08001479#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001480 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001481 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1482 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001483 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001484 }
1485 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001486 int regAddr = oatAllocTemp(cUnit);
1487 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1488 oatFreeTemp(cUnit, rlArray.lowReg);
1489 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001490 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001491 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
jeffhao21e12712012-05-25 19:06:18 -07001492 rlResult.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001493 storeValueWide(cUnit, rlDest, rlResult);
1494 } else {
1495 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001496
buzbee408ad162012-06-06 16:45:18 -07001497 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001498 dataOffset, rlResult.lowReg, INVALID_REG, size,
1499 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001500
Bill Buzbeea114add2012-05-03 15:00:40 -07001501 storeValue(cUnit, rlDest, rlResult);
1502 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001503#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001504 int regPtr = oatAllocTemp(cUnit);
buzbee408ad162012-06-06 16:45:18 -07001505 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001506 int regLen = INVALID_REG;
1507 if (needsRangeCheck) {
1508 regLen = oatAllocTemp(cUnit);
1509 /* Get len */
1510 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1511 }
1512 /* regPtr -> array data */
1513 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1514 oatFreeTemp(cUnit, rlArray.lowReg);
1515 if ((size == kLong) || (size == kDouble)) {
1516 if (scale) {
1517 int rNewIndex = oatAllocTemp(cUnit);
1518 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1519 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1520 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001521 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001522 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001523 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001524 oatFreeTemp(cUnit, rlIndex.lowReg);
1525 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1526
1527 if (needsRangeCheck) {
1528 // TODO: change kCondCS to a more meaningful name, is the sense of
1529 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001530 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001531 oatFreeTemp(cUnit, regLen);
1532 }
1533 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1534
1535 oatFreeTemp(cUnit, regPtr);
1536 storeValueWide(cUnit, rlDest, rlResult);
1537 } else {
1538 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1539
1540 if (needsRangeCheck) {
1541 // TODO: change kCondCS to a more meaningful name, is the sense of
1542 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001543 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001544 oatFreeTemp(cUnit, regLen);
1545 }
1546 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1547 scale, size);
1548
1549 oatFreeTemp(cUnit, regPtr);
1550 storeValue(cUnit, rlDest, rlResult);
1551 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001552#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001553}
1554
1555/*
1556 * Generate array store
1557 *
1558 */
buzbee408ad162012-06-06 16:45:18 -07001559void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001560 RegLocation rlArray, RegLocation rlIndex,
1561 RegLocation rlSrc, int scale)
1562{
Bill Buzbeea114add2012-05-03 15:00:40 -07001563 RegisterClass regClass = oatRegClassBySize(size);
1564 int lenOffset = Array::LengthOffset().Int32Value();
1565 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001566
Bill Buzbeea114add2012-05-03 15:00:40 -07001567 if (size == kLong || size == kDouble) {
1568 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1569 } else {
1570 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1571 }
buzbee31a4a6f2012-02-28 15:36:15 -08001572
Bill Buzbeea114add2012-05-03 15:00:40 -07001573 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1574 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001575#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001576 int regPtr;
1577 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1578 oatClobber(cUnit, rlArray.lowReg);
1579 regPtr = rlArray.lowReg;
1580 } else {
1581 regPtr = oatAllocTemp(cUnit);
1582 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1583 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001584#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001585
Bill Buzbeea114add2012-05-03 15:00:40 -07001586 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001587 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001588
Ian Rogersb41b33b2012-03-20 14:22:54 -07001589#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001590 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001591 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1592 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001593 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001594 }
1595 if ((size == kLong) || (size == kDouble)) {
1596 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1597 } else {
1598 rlSrc = loadValue(cUnit, rlSrc, regClass);
1599 }
buzbee408ad162012-06-06 16:45:18 -07001600 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001601 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1602 INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001603#else
buzbee408ad162012-06-06 16:45:18 -07001604 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001605 int regLen = INVALID_REG;
1606 if (needsRangeCheck) {
1607 regLen = oatAllocTemp(cUnit);
1608 //NOTE: max live temps(4) here.
1609 /* Get len */
1610 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1611 }
1612 /* regPtr -> array data */
1613 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1614 /* at this point, regPtr points to array, 2 live temps */
1615 if ((size == kLong) || (size == kDouble)) {
1616 //TUNING: specific wide routine that can handle fp regs
1617 if (scale) {
1618 int rNewIndex = oatAllocTemp(cUnit);
1619 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1620 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1621 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001622 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001623 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001624 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001625 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1626
1627 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001628 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001629 oatFreeTemp(cUnit, regLen);
1630 }
1631
jeffhao41005dd2012-05-09 17:58:52 -07001632 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001633
1634 oatFreeTemp(cUnit, regPtr);
1635 } else {
1636 rlSrc = loadValue(cUnit, rlSrc, regClass);
1637 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001638 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001639 oatFreeTemp(cUnit, regLen);
1640 }
1641 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1642 scale, size);
1643 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001644#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001645}
1646
buzbee408ad162012-06-06 16:45:18 -07001647void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001648 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001649 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001650{
Bill Buzbeea114add2012-05-03 15:00:40 -07001651 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001652#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001653 /*
1654 * NOTE: This is the one place in the code in which we might have
1655 * as many as six live temporary registers. There are 5 in the normal
1656 * set for Arm. Until we have spill capabilities, temporarily add
1657 * lr to the temp set. It is safe to do this locally, but note that
1658 * lr is used explicitly elsewhere in the code generator and cannot
1659 * normally be used as a general temp register.
1660 */
1661 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1662 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001663#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001664 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1665 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1666 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1667 // The longs may overlap - use intermediate temp if so
1668 if (rlResult.lowReg == rlSrc1.highReg) {
1669 int tReg = oatAllocTemp(cUnit);
1670 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1671 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1672 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1673 oatFreeTemp(cUnit, tReg);
1674 } else {
1675 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1676 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1677 rlSrc2.highReg);
1678 }
1679 /*
1680 * NOTE: If rlDest refers to a frame variable in a large frame, the
1681 * following storeValueWide might need to allocate a temp register.
1682 * To further work around the lack of a spill capability, explicitly
1683 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1684 * Remove when spill is functional.
1685 */
1686 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1687 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1688 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001689#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001690 oatClobber(cUnit, rLR);
1691 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001692#endif
1693}
1694
1695
buzbee408ad162012-06-06 16:45:18 -07001696bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001697 RegLocation rlSrc1, RegLocation rlShift)
1698{
Bill Buzbeea114add2012-05-03 15:00:40 -07001699 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001700
buzbee408ad162012-06-06 16:45:18 -07001701 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001702 case Instruction::SHL_LONG:
1703 case Instruction::SHL_LONG_2ADDR:
1704 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1705 break;
1706 case Instruction::SHR_LONG:
1707 case Instruction::SHR_LONG_2ADDR:
1708 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1709 break;
1710 case Instruction::USHR_LONG:
1711 case Instruction::USHR_LONG_2ADDR:
1712 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1713 break;
1714 default:
1715 LOG(FATAL) << "Unexpected case";
1716 return true;
1717 }
1718 oatFlushAllRegs(cUnit); /* Send everything to home location */
1719 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
1720 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1721 storeValueWide(cUnit, rlDest, rlResult);
1722 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001723}
1724
1725
buzbee408ad162012-06-06 16:45:18 -07001726bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001727 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001728{
Bill Buzbeea114add2012-05-03 15:00:40 -07001729 OpKind op = kOpBkpt;
1730 bool callOut = false;
1731 bool checkZero = false;
1732 bool unary = false;
1733 RegLocation rlResult;
1734 bool shiftOp = false;
1735 int funcOffset;
1736 int retReg = rRET0;
buzbee408ad162012-06-06 16:45:18 -07001737 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001738 case Instruction::NEG_INT:
1739 op = kOpNeg;
1740 unary = true;
1741 break;
1742 case Instruction::NOT_INT:
1743 op = kOpMvn;
1744 unary = true;
1745 break;
1746 case Instruction::ADD_INT:
1747 case Instruction::ADD_INT_2ADDR:
1748 op = kOpAdd;
1749 break;
1750 case Instruction::SUB_INT:
1751 case Instruction::SUB_INT_2ADDR:
1752 op = kOpSub;
1753 break;
1754 case Instruction::MUL_INT:
1755 case Instruction::MUL_INT_2ADDR:
1756 op = kOpMul;
1757 break;
1758 case Instruction::DIV_INT:
1759 case Instruction::DIV_INT_2ADDR:
1760 checkZero = true;
1761 op = kOpDiv;
1762 callOut = true;
1763 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1764 retReg = rRET0;
1765 break;
1766 /* NOTE: returns in rARG1 */
1767 case Instruction::REM_INT:
1768 case Instruction::REM_INT_2ADDR:
1769 checkZero = true;
1770 op = kOpRem;
1771 callOut = true;
1772 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1773 retReg = rRET1;
1774 break;
1775 case Instruction::AND_INT:
1776 case Instruction::AND_INT_2ADDR:
1777 op = kOpAnd;
1778 break;
1779 case Instruction::OR_INT:
1780 case Instruction::OR_INT_2ADDR:
1781 op = kOpOr;
1782 break;
1783 case Instruction::XOR_INT:
1784 case Instruction::XOR_INT_2ADDR:
1785 op = kOpXor;
1786 break;
1787 case Instruction::SHL_INT:
1788 case Instruction::SHL_INT_2ADDR:
1789 shiftOp = true;
1790 op = kOpLsl;
1791 break;
1792 case Instruction::SHR_INT:
1793 case Instruction::SHR_INT_2ADDR:
1794 shiftOp = true;
1795 op = kOpAsr;
1796 break;
1797 case Instruction::USHR_INT:
1798 case Instruction::USHR_INT_2ADDR:
1799 shiftOp = true;
1800 op = kOpLsr;
1801 break;
1802 default:
1803 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001804 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001805 }
1806 if (!callOut) {
1807 if (unary) {
1808 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1809 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1810 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001811 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001812 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001813#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001814 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1815 int tReg = oatAllocTemp(cUnit);
1816 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001817#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001818 // X86 doesn't require masking and must use ECX
1819 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1820 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001821#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001822 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1823 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1824 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1825 oatFreeTemp(cUnit, tReg);
1826 } else {
1827 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1828 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1829 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1830 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1831 }
buzbee31a4a6f2012-02-28 15:36:15 -08001832 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001833 storeValue(cUnit, rlDest, rlResult);
1834 } else {
1835 RegLocation rlResult;
1836 oatFlushAllRegs(cUnit); /* Send everything to home location */
1837 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1838#if !defined(TARGET_X86)
1839 int rTgt = loadHelper(cUnit, funcOffset);
1840#endif
1841 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1842 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001843 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001844 }
1845#if !defined(TARGET_X86)
1846 opReg(cUnit, kOpBlx, rTgt);
1847 oatFreeTemp(cUnit, rTgt);
1848#else
1849 opThreadMem(cUnit, kOpBlx, funcOffset);
1850#endif
1851 if (retReg == rRET0)
1852 rlResult = oatGetReturn(cUnit, false);
1853 else
1854 rlResult = oatGetReturnAlt(cUnit);
1855 storeValue(cUnit, rlDest, rlResult);
1856 }
1857 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001858}
1859
1860/*
1861 * The following are the first-level codegen routines that analyze the format
1862 * of each bytecode then either dispatch special purpose codegen routines
1863 * or produce corresponding Thumb instructions directly.
1864 */
1865
1866bool isPowerOfTwo(int x)
1867{
Bill Buzbeea114add2012-05-03 15:00:40 -07001868 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001869}
1870
1871// Returns true if no more than two bits are set in 'x'.
1872bool isPopCountLE2(unsigned int x)
1873{
Bill Buzbeea114add2012-05-03 15:00:40 -07001874 x &= x - 1;
1875 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001876}
1877
1878// Returns the index of the lowest set bit in 'x'.
1879int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001880 int bit_posn = 0;
1881 while ((x & 0xf) == 0) {
1882 bit_posn += 4;
1883 x >>= 4;
1884 }
1885 while ((x & 1) == 0) {
1886 bit_posn++;
1887 x >>= 1;
1888 }
1889 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001890}
1891
1892// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1893// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001894bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001895 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001896{
buzbeef3aac972012-04-11 16:33:36 -07001897#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001898 // No divide instruction for Arm, so check for more special cases
1899 if (lit < 2) {
1900 return false;
1901 }
1902 if (!isPowerOfTwo(lit)) {
1903 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1904 }
buzbeef3aac972012-04-11 16:33:36 -07001905#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001906 if (lit < 2 || !isPowerOfTwo(lit)) {
1907 return false;
1908 }
buzbeef3aac972012-04-11 16:33:36 -07001909#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001910 int k = lowestSetBit(lit);
1911 if (k >= 30) {
1912 // Avoid special cases.
1913 return false;
1914 }
1915 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1916 dalvikOpcode == Instruction::DIV_INT_LIT16);
1917 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1918 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1919 if (div) {
1920 int tReg = oatAllocTemp(cUnit);
1921 if (lit == 2) {
1922 // Division by 2 is by far the most common division by constant.
1923 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1924 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1925 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001926 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001927 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1928 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1929 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1930 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001931 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001932 } else {
1933 int tReg1 = oatAllocTemp(cUnit);
1934 int tReg2 = oatAllocTemp(cUnit);
1935 if (lit == 2) {
1936 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1937 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1938 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1939 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1940 } else {
1941 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1942 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1943 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1944 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1945 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1946 }
1947 }
1948 storeValue(cUnit, rlDest, rlResult);
1949 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001950}
1951
1952void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1953 RegLocation rlResult, int lit,
1954 int firstBit, int secondBit)
1955{
buzbee0398c422012-03-02 15:22:47 -08001956#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001957 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1958 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001959#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001960 int tReg = oatAllocTemp(cUnit);
1961 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1962 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1963 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001964#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001965 if (firstBit != 0) {
1966 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1967 }
buzbee31a4a6f2012-02-28 15:36:15 -08001968}
1969
1970// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1971// and store the result in 'rlDest'.
1972bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1973 RegLocation rlDest, int lit)
1974{
Bill Buzbeea114add2012-05-03 15:00:40 -07001975 // Can we simplify this multiplication?
1976 bool powerOfTwo = false;
1977 bool popCountLE2 = false;
1978 bool powerOfTwoMinusOne = false;
1979 if (lit < 2) {
1980 // Avoid special cases.
1981 return false;
1982 } else if (isPowerOfTwo(lit)) {
1983 powerOfTwo = true;
1984 } else if (isPopCountLE2(lit)) {
1985 popCountLE2 = true;
1986 } else if (isPowerOfTwo(lit + 1)) {
1987 powerOfTwoMinusOne = true;
1988 } else {
1989 return false;
1990 }
1991 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1992 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1993 if (powerOfTwo) {
1994 // Shift.
1995 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1996 lowestSetBit(lit));
1997 } else if (popCountLE2) {
1998 // Shift and add and shift.
1999 int firstBit = lowestSetBit(lit);
2000 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2001 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2002 firstBit, secondBit);
2003 } else {
2004 // Reverse subtract: (src << (shift + 1)) - src.
2005 DCHECK(powerOfTwoMinusOne);
2006 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2007 int tReg = oatAllocTemp(cUnit);
2008 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2009 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2010 }
2011 storeValue(cUnit, rlDest, rlResult);
2012 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002013}
2014
buzbee408ad162012-06-06 16:45:18 -07002015bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2016 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002017{
Bill Buzbeea114add2012-05-03 15:00:40 -07002018 RegLocation rlResult;
2019 OpKind op = (OpKind)0; /* Make gcc happy */
2020 int shiftOp = false;
2021 bool isDiv = false;
2022 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002023
buzbee408ad162012-06-06 16:45:18 -07002024 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002025 case Instruction::RSUB_INT_LIT8:
2026 case Instruction::RSUB_INT: {
2027 int tReg;
2028 //TUNING: add support for use of Arm rsub op
2029 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2030 tReg = oatAllocTemp(cUnit);
2031 loadConstant(cUnit, tReg, lit);
2032 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2033 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2034 storeValue(cUnit, rlDest, rlResult);
2035 return false;
2036 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002037 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002038
2039 case Instruction::ADD_INT_LIT8:
2040 case Instruction::ADD_INT_LIT16:
2041 op = kOpAdd;
2042 break;
2043 case Instruction::MUL_INT_LIT8:
2044 case Instruction::MUL_INT_LIT16: {
2045 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2046 return false;
2047 }
2048 op = kOpMul;
2049 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002050 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002051 case Instruction::AND_INT_LIT8:
2052 case Instruction::AND_INT_LIT16:
2053 op = kOpAnd;
2054 break;
2055 case Instruction::OR_INT_LIT8:
2056 case Instruction::OR_INT_LIT16:
2057 op = kOpOr;
2058 break;
2059 case Instruction::XOR_INT_LIT8:
2060 case Instruction::XOR_INT_LIT16:
2061 op = kOpXor;
2062 break;
2063 case Instruction::SHL_INT_LIT8:
2064 lit &= 31;
2065 shiftOp = true;
2066 op = kOpLsl;
2067 break;
2068 case Instruction::SHR_INT_LIT8:
2069 lit &= 31;
2070 shiftOp = true;
2071 op = kOpAsr;
2072 break;
2073 case Instruction::USHR_INT_LIT8:
2074 lit &= 31;
2075 shiftOp = true;
2076 op = kOpLsr;
2077 break;
2078
2079 case Instruction::DIV_INT_LIT8:
2080 case Instruction::DIV_INT_LIT16:
2081 case Instruction::REM_INT_LIT8:
2082 case Instruction::REM_INT_LIT16:
2083 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002084 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002085 return false;
2086 }
buzbee408ad162012-06-06 16:45:18 -07002087 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002088 return false;
2089 }
2090 oatFlushAllRegs(cUnit); /* Everything to home location */
2091 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2092 oatClobber(cUnit, rARG0);
2093 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee408ad162012-06-06 16:45:18 -07002094 if ((opcode == Instruction::DIV_INT_LIT8) ||
2095 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002096 isDiv = true;
2097 } else {
2098 isDiv = false;
2099 }
2100 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
2101 if (isDiv)
2102 rlResult = oatGetReturn(cUnit, false);
2103 else
2104 rlResult = oatGetReturnAlt(cUnit);
2105 storeValue(cUnit, rlDest, rlResult);
2106 return false;
2107 break;
2108 default:
2109 return true;
2110 }
2111 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2112 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2113 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2114 if (shiftOp && (lit == 0)) {
2115 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2116 } else {
2117 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2118 }
2119 storeValue(cUnit, rlDest, rlResult);
2120 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002121}
2122
buzbee408ad162012-06-06 16:45:18 -07002123bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002124 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002125{
Bill Buzbeea114add2012-05-03 15:00:40 -07002126 RegLocation rlResult;
2127 OpKind firstOp = kOpBkpt;
2128 OpKind secondOp = kOpBkpt;
2129 bool callOut = false;
2130 bool checkZero = false;
2131 int funcOffset;
2132 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002133
buzbee408ad162012-06-06 16:45:18 -07002134 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002135 case Instruction::NOT_LONG:
2136 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2137 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2138 // Check for destructive overlap
2139 if (rlResult.lowReg == rlSrc2.highReg) {
2140 int tReg = oatAllocTemp(cUnit);
2141 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2142 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2143 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2144 oatFreeTemp(cUnit, tReg);
2145 } else {
2146 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2147 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2148 }
2149 storeValueWide(cUnit, rlDest, rlResult);
2150 return false;
2151 break;
2152 case Instruction::ADD_LONG:
2153 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002154#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002155 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002156#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002157 firstOp = kOpAdd;
2158 secondOp = kOpAdc;
2159 break;
buzbeec5159d52012-03-03 11:48:39 -08002160#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002161 case Instruction::SUB_LONG:
2162 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002163#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002164 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002165#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002166 firstOp = kOpSub;
2167 secondOp = kOpSbc;
2168 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002169#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002170 case Instruction::MUL_LONG:
2171 case Instruction::MUL_LONG_2ADDR:
2172 callOut = true;
2173 retReg = rRET0;
2174 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2175 break;
2176 case Instruction::DIV_LONG:
2177 case Instruction::DIV_LONG_2ADDR:
2178 callOut = true;
2179 checkZero = true;
2180 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002181 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002182 break;
2183 case Instruction::REM_LONG:
2184 case Instruction::REM_LONG_2ADDR:
2185 callOut = true;
2186 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002187 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002188#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002189 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2190 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002191#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002192 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002193#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002194 break;
2195 case Instruction::AND_LONG_2ADDR:
2196 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002197#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002198 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002199#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002200 firstOp = kOpAnd;
2201 secondOp = kOpAnd;
2202 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002203#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002204 case Instruction::OR_LONG:
2205 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002206#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002207 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002208#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002209 firstOp = kOpOr;
2210 secondOp = kOpOr;
2211 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002212#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002213 case Instruction::XOR_LONG:
2214 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002215#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002216 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002217#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002218 firstOp = kOpXor;
2219 secondOp = kOpXor;
2220 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002221#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002222 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002223 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002224 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002225 default:
2226 LOG(FATAL) << "Invalid long arith op";
2227 }
2228 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002229 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002230 } else {
2231 oatFlushAllRegs(cUnit); /* Send everything to home location */
2232 if (checkZero) {
2233 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2234#if !defined(TARGET_X86)
2235 int rTgt = loadHelper(cUnit, funcOffset);
2236#endif
2237 int tReg = oatAllocTemp(cUnit);
2238#if defined(TARGET_ARM)
2239 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2240 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002241 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002242#else
2243 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2244#endif
buzbee408ad162012-06-06 16:45:18 -07002245 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002246 oatFreeTemp(cUnit, tReg);
2247 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2248#if !defined(TARGET_X86)
2249 opReg(cUnit, kOpBlx, rTgt);
2250 oatFreeTemp(cUnit, rTgt);
2251#else
2252 opThreadMem(cUnit, kOpBlx, funcOffset);
2253#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002254 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002255 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2256 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002257 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002258 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2259 if (retReg == rRET0)
2260 rlResult = oatGetReturnWide(cUnit, false);
2261 else
2262 rlResult = oatGetReturnWideAlt(cUnit);
2263 storeValueWide(cUnit, rlDest, rlResult);
2264 }
2265 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002266}
2267
buzbee408ad162012-06-06 16:45:18 -07002268bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2269 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002270{
Bill Buzbeea114add2012-05-03 15:00:40 -07002271 /*
2272 * Don't optimize the register usage since it calls out to support
2273 * functions
2274 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002275 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002276 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002277 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002278 } else {
2279 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002280 }
2281 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002282 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002283 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002284 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2285 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002286 } else {
2287 RegLocation rlResult;
2288 rlResult = oatGetReturn(cUnit, rlDest.fp);
2289 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002290 }
2291 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002292}
2293
2294void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002295bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002296 RegLocation rlDest, RegLocation rlSrc1,
2297 RegLocation rlSrc2)
2298{
Bill Buzbeea114add2012-05-03 15:00:40 -07002299 RegLocation rlResult;
2300 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002301
buzbee408ad162012-06-06 16:45:18 -07002302 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002303 case Instruction::ADD_FLOAT_2ADDR:
2304 case Instruction::ADD_FLOAT:
2305 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2306 break;
2307 case Instruction::SUB_FLOAT_2ADDR:
2308 case Instruction::SUB_FLOAT:
2309 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2310 break;
2311 case Instruction::DIV_FLOAT_2ADDR:
2312 case Instruction::DIV_FLOAT:
2313 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2314 break;
2315 case Instruction::MUL_FLOAT_2ADDR:
2316 case Instruction::MUL_FLOAT:
2317 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2318 break;
2319 case Instruction::REM_FLOAT_2ADDR:
2320 case Instruction::REM_FLOAT:
2321 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2322 break;
2323 case Instruction::NEG_FLOAT: {
2324 genNegFloat(cUnit, rlDest, rlSrc1);
2325 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002326 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002327 default:
2328 return true;
2329 }
2330 oatFlushAllRegs(cUnit); /* Send everything to home location */
2331 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2332 rlResult = oatGetReturn(cUnit, true);
2333 storeValue(cUnit, rlDest, rlResult);
2334 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002335}
2336
2337void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002338bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002339 RegLocation rlDest, RegLocation rlSrc1,
2340 RegLocation rlSrc2)
2341{
Bill Buzbeea114add2012-05-03 15:00:40 -07002342 RegLocation rlResult;
2343 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002344
buzbee408ad162012-06-06 16:45:18 -07002345 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002346 case Instruction::ADD_DOUBLE_2ADDR:
2347 case Instruction::ADD_DOUBLE:
2348 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2349 break;
2350 case Instruction::SUB_DOUBLE_2ADDR:
2351 case Instruction::SUB_DOUBLE:
2352 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2353 break;
2354 case Instruction::DIV_DOUBLE_2ADDR:
2355 case Instruction::DIV_DOUBLE:
2356 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2357 break;
2358 case Instruction::MUL_DOUBLE_2ADDR:
2359 case Instruction::MUL_DOUBLE:
2360 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2361 break;
2362 case Instruction::REM_DOUBLE_2ADDR:
2363 case Instruction::REM_DOUBLE:
2364 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2365 break;
2366 case Instruction::NEG_DOUBLE: {
2367 genNegDouble(cUnit, rlDest, rlSrc1);
2368 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002369 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002370 default:
2371 return true;
2372 }
2373 oatFlushAllRegs(cUnit); /* Send everything to home location */
2374 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2375 rlResult = oatGetReturnWide(cUnit, true);
2376 storeValueWide(cUnit, rlDest, rlResult);
2377 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002378}
2379
buzbee408ad162012-06-06 16:45:18 -07002380bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2381 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002382{
buzbee31a4a6f2012-02-28 15:36:15 -08002383
Bill Buzbeea114add2012-05-03 15:00:40 -07002384 switch (opcode) {
2385 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002386 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2387 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002388 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002389 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2390 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002391 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002392 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2393 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002394 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002395 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2396 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002397 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002398 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2399 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002400 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002401 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2402 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002403 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002404 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2405 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002406 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002407 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2408 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002409 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002410 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2411 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002412 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002413 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2414 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002415 default:
2416 return true;
2417 }
2418 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002419}
2420
2421/*
2422 * Generate callout to updateDebugger. Note that we're overloading
2423 * the use of rSUSPEND here. When the debugger is active, this
2424 * register holds the address of the update function. So, if it's
2425 * non-null, we call out to it.
2426 *
2427 * Note also that rRET0 and rRET1 must be preserved across this
2428 * code. This must be handled by the stub.
2429 */
2430void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2431{
Bill Buzbeea114add2012-05-03 15:00:40 -07002432 // Following DCHECK verifies that dPC is in range of single load immediate
2433 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2434 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2435 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002436#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002437 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2438 opIT(cUnit, kArmCondNe, "T");
2439 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2440 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002441#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002442 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002443#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002444 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2445 loadConstant(cUnit, rARG2, offset);
2446 opReg(cUnit, kOpBlx, rSUSPEND);
2447 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2448 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002449#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002450 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002451}
2452
2453/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002454void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002455{
buzbee408ad162012-06-06 16:45:18 -07002456 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002457 return;
2458 }
2459 oatFlushAllRegs(cUnit);
2460 if (cUnit->genDebugger) {
2461 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002462#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002463 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002464#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002465 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
2466 opReg(cUnit, kOpBlx, rTgt);
2467 // Refresh rSUSPEND
2468 loadWordDisp(cUnit, rSELF,
2469 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2470 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002471#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002472 } else {
2473 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002474#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002475 // In non-debug case, only check periodically
2476 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2477 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002478#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002479 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2480 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002481#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002482 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2483 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002484#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002485 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2486 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002487 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002488 branch->target = (LIR*)target;
2489 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2490 }
buzbee31a4a6f2012-02-28 15:36:15 -08002491}
2492
buzbeefead2932012-03-30 14:02:01 -07002493/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002494void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002495{
buzbee408ad162012-06-06 16:45:18 -07002496 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002497 opUnconditionalBranch(cUnit, target);
2498 return;
2499 }
2500 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002501 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002502 opUnconditionalBranch(cUnit, target);
2503 } else {
buzbeefead2932012-03-30 14:02:01 -07002504#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002505 // In non-debug case, only check periodically
2506 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2507 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002508#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002509 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2510 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002511#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002512 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2513 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002514#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002515 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002516 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002517 oatFlushAllRegs(cUnit);
2518 opUnconditionalBranch(cUnit, launchPad);
2519 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2520 (intptr_t)launchPad);
2521 }
buzbeefead2932012-03-30 14:02:01 -07002522}
2523
buzbee31a4a6f2012-02-28 15:36:15 -08002524} // namespace art