blob: f114b45987ed0f6a82724aeec6eefbb961dae12f [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers57b86d42012-03-27 16:05:41 -070017#include "oat/runtime/oat_support_entrypoints.h"
18
buzbee31a4a6f2012-02-28 15:36:15 -080019namespace art {
20
21/*
22 * This source files contains "gen" codegen routines that should
23 * be applicable to most targets. Only mid-level support utilities
24 * and "op" calls may be used here.
25 */
buzbee3b3dbdd2012-06-13 13:39:34 -070026void genInvoke(CompilationUnit* cUnit, CallInfo* info);
buzbee31a4a6f2012-02-28 15:36:15 -080027#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080028LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbeef3aac972012-04-11 16:33:36 -070029bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
30 RegLocation rlSrc, RegLocation rlDest, int lit);
buzbee31a4a6f2012-02-28 15:36:15 -080031#endif
32
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
buzbee3b3dbdd2012-06-13 13:39:34 -0700382void genCompareAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
383 RegLocation rlSrc1, RegLocation rlSrc2, LIR* taken,
384 LIR* fallThrough)
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)
buzbee3b3dbdd2012-06-13 13:39:34 -0700413 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg, taken);
buzbee5de34942012-03-01 14:51:57 -0800414#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee3b3dbdd2012-06-13 13:39:34 -0700416 opCondBranch(cUnit, cond, taken);
buzbee5de34942012-03-01 14:51:57 -0800417#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700418 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800419}
420
buzbee3b3dbdd2012-06-13 13:39:34 -0700421void genCompareZeroAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
422 RegLocation rlSrc, LIR* taken, LIR* fallThrough)
buzbee31a4a6f2012-02-28 15:36:15 -0800423{
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 ConditionCode cond;
425 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 switch (opcode) {
427 case Instruction::IF_EQZ:
428 cond = kCondEq;
429 break;
430 case Instruction::IF_NEZ:
431 cond = kCondNe;
432 break;
433 case Instruction::IF_LTZ:
434 cond = kCondLt;
435 break;
436 case Instruction::IF_GEZ:
437 cond = kCondGe;
438 break;
439 case Instruction::IF_GTZ:
440 cond = kCondGt;
441 break;
442 case Instruction::IF_LEZ:
443 cond = kCondLe;
444 break;
445 default:
446 cond = (ConditionCode)0;
447 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
448 }
Ian Rogers7caad772012-03-30 01:07:54 -0700449#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee3b3dbdd2012-06-13 13:39:34 -0700450 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, taken);
buzbee5de34942012-03-01 14:51:57 -0800451#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee3b3dbdd2012-06-13 13:39:34 -0700453 opCondBranch(cUnit, cond, taken);
buzbee5de34942012-03-01 14:51:57 -0800454#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700455 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800456}
457
buzbee408ad162012-06-06 16:45:18 -0700458void genIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800459 RegLocation rlSrc)
460{
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
462 if (rlSrc.location == kLocPhysReg) {
463 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
464 } else {
465 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
466 }
467 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
468 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800469}
470
buzbee408ad162012-06-06 16:45:18 -0700471void genIntNarrowing(CompilationUnit* cUnit, Instruction::Code opcode,
472 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800473{
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
475 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
476 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700477 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 case Instruction::INT_TO_BYTE:
479 op = kOp2Byte;
480 break;
481 case Instruction::INT_TO_SHORT:
482 op = kOp2Short;
483 break;
484 case Instruction::INT_TO_CHAR:
485 op = kOp2Char;
486 break;
487 default:
488 LOG(ERROR) << "Bad int conversion type";
489 }
490 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
491 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800492}
493
494/*
495 * Let helper function take care of everything. Will call
496 * Array::AllocFromCode(type_idx, method, count);
497 * Note: AllocFromCode will handle checks for errNegativeArraySize.
498 */
buzbee408ad162012-06-06 16:45:18 -0700499void genNewArray(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800500 RegLocation rlSrc)
501{
Bill Buzbeea114add2012-05-03 15:00:40 -0700502 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 int funcOffset;
504 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
505 cUnit->dex_cache,
506 *cUnit->dex_file,
507 type_idx)) {
508 funcOffset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
509 } else {
510 funcOffset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
511 }
512 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
513 RegLocation rlResult = oatGetReturn(cUnit, false);
514 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800515}
516
517/*
518 * Similar to genNewArray, but with post-allocation initialization.
519 * Verifier guarantees we're dealing with an array class. Current
520 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
521 * Current code also throws internal unimp if not 'L', '[' or 'I'.
522 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700523void genFilledNewArray(CompilationUnit* cUnit, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800524{
buzbee3b3dbdd2012-06-13 13:39:34 -0700525 int elems = info->numArgWords;
526 int typeIdx = info->index;
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 oatFlushAllRegs(cUnit); /* Everything to home location */
528 int funcOffset;
529 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
530 cUnit->dex_cache,
531 *cUnit->dex_file,
532 typeIdx)) {
533 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
534 } else {
535 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
536 }
537 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
538 oatFreeTemp(cUnit, rARG2);
539 oatFreeTemp(cUnit, rARG1);
540 /*
541 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
542 * return region. Because AllocFromCode placed the new array
543 * in rRET0, we'll just lock it into place. When debugger support is
544 * added, it may be necessary to additionally copy all return
545 * values to a home location in thread-local storage
546 */
547 oatLockTemp(cUnit, rRET0);
548
549 // TODO: use the correct component size, currently all supported types
550 // share array alignment with ints (see comment at head of function)
551 size_t component_size = sizeof(int32_t);
552
553 // Having a range of 0 is legal
buzbee3b3dbdd2012-06-13 13:39:34 -0700554 if (info->isRange && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800555 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 * Bit of ugliness here. We're going generate a mem copy loop
557 * on the register range, but it is possible that some regs
558 * in the range have been promoted. This is unlikely, but
559 * before generating the copy, we'll just force a flush
560 * of any regs in the source range that have been promoted to
561 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800562 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700563 for (int i = 0; i < elems; i++) {
564 RegLocation loc = oatUpdateLoc(cUnit, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 if (loc.location == kLocPhysReg) {
566 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
567 loc.lowReg, kWord);
568 }
buzbee31a4a6f2012-02-28 15:36:15 -0800569 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 /*
571 * TUNING note: generated code here could be much improved, but
572 * this is an uncommon operation and isn't especially performance
573 * critical.
574 */
575 int rSrc = oatAllocTemp(cUnit);
576 int rDst = oatAllocTemp(cUnit);
577 int rIdx = oatAllocTemp(cUnit);
578#if defined(TARGET_ARM)
579 int rVal = rLR; // Using a lot of temps, rLR is known free here
580#elif defined(TARGET_X86)
jeffhao5772bab2012-05-18 11:51:26 -0700581 oatFreeTemp(cUnit, rRET0);
582 int rVal = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700583#else
584 int rVal = oatAllocTemp(cUnit);
585#endif
586 // Set up source pointer
buzbee3b3dbdd2012-06-13 13:39:34 -0700587 RegLocation rlFirst = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
589 oatSRegOffset(cUnit, rlFirst.sRegLow));
590 // Set up the target pointer
591 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
592 Array::DataOffset(component_size).Int32Value());
593 // Set up the loop counter (known to be > 0)
buzbee3b3dbdd2012-06-13 13:39:34 -0700594 loadConstant(cUnit, rIdx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 // Generate the copy loop. Going backwards for convenience
596 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
597 // Copy next element
598 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
599 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
600#if defined(TARGET_ARM)
601 // Combine sub & test using sub setflags encoding here
602 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
603 opCondBranch(cUnit, kCondGe, target);
604#else
605 oatFreeTemp(cUnit, rVal);
606 opRegImm(cUnit, kOpSub, rIdx, 1);
607 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
608#endif
jeffhao5772bab2012-05-18 11:51:26 -0700609#if defined(TARGET_X86)
610 // Restore the target pointer
611 opRegRegImm(cUnit, kOpAdd, rRET0, rDst,
612 -Array::DataOffset(component_size).Int32Value());
613#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700614 } else if (!info->isRange) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700615 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700616 for (int i = 0; i < elems; i++) {
617 RegLocation rlArg = loadValue(cUnit, info->args[i], kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 storeBaseDisp(cUnit, rRET0,
619 Array::DataOffset(component_size).Int32Value() +
620 i * 4, rlArg.lowReg, kWord);
621 // If the loadValue caused a temp to be allocated, free it
622 if (oatIsTemp(cUnit, rlArg.lowReg)) {
623 oatFreeTemp(cUnit, rlArg.lowReg);
624 }
625 }
626 }
buzbee31a4a6f2012-02-28 15:36:15 -0800627}
628
buzbee408ad162012-06-06 16:45:18 -0700629void genSput(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800631{
Bill Buzbeea114add2012-05-03 15:00:40 -0700632 int fieldOffset;
633 int ssbIndex;
634 bool isVolatile;
635 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800636
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
638 *cUnit->dex_file, *cUnit->dex_cache,
639 cUnit->code_item, cUnit->method_idx,
640 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800641
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 bool fastPath =
643 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
644 fieldOffset, ssbIndex,
645 isReferrersClass, isVolatile,
646 true);
647 if (fastPath && !SLOW_FIELD_PATH) {
648 DCHECK_GE(fieldOffset, 0);
649 int rBase;
650 if (isReferrersClass) {
651 // Fast path, static storage base is this method's class
652 RegLocation rlMethod = loadCurrMethod(cUnit);
653 rBase = oatAllocTemp(cUnit);
654 loadWordDisp(cUnit, rlMethod.lowReg,
655 Method::DeclaringClassOffset().Int32Value(), rBase);
656 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
657 oatFreeTemp(cUnit, rlMethod.lowReg);
658 }
buzbee31a4a6f2012-02-28 15:36:15 -0800659 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 // Medium path, static storage base in a different class which
661 // requires checks that the other class is initialized.
662 DCHECK_GE(ssbIndex, 0);
663 // May do runtime call so everything to home locations.
664 oatFlushAllRegs(cUnit);
665 // Using fixed register to sync with possible call to runtime
666 // support.
667 int rMethod = rARG1;
668 oatLockTemp(cUnit, rMethod);
669 loadCurrMethodDirect(cUnit, rMethod);
670 rBase = rARG0;
671 oatLockTemp(cUnit, rBase);
672 loadWordDisp(cUnit, rMethod,
673 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
674 rBase);
675 loadWordDisp(cUnit, rBase,
676 Array::DataOffset(sizeof(Object*)).Int32Value() +
677 sizeof(int32_t*) * ssbIndex, rBase);
678 // rBase now points at appropriate static storage base (Class*)
679 // or NULL if not initialized. Check for NULL and call helper if NULL.
680 // TUNING: fast path should fall through
681 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
682 loadConstant(cUnit, rARG0, ssbIndex);
683 callRuntimeHelperImm(cUnit,
684 ENTRYPOINT_OFFSET(pInitializeStaticStorage),
685 ssbIndex);
686#if defined(TARGET_MIPS)
687 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
688 opRegCopy(cUnit, rBase, rRET0);
689#endif
690 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
691 branchOver->target = (LIR*)skipTarget;
692 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800693 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700694 // rBase now holds static storage base
695 if (isLongOrDouble) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700696 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
697 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
699 }
700//FIXME: need to generalize the barrier call
701 if (isVolatile) {
702 oatGenMemBarrier(cUnit, kST);
703 }
704 if (isLongOrDouble) {
705 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
706 rlSrc.highReg);
707 } else {
708 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
709 }
710 if (isVolatile) {
711 oatGenMemBarrier(cUnit, kSY);
712 }
713 if (isObject) {
714 markGCCard(cUnit, rlSrc.lowReg, rBase);
715 }
716 oatFreeTemp(cUnit, rBase);
717 } else {
718 oatFlushAllRegs(cUnit); // Everything to home locations
719 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Static) :
720 (isObject ? ENTRYPOINT_OFFSET(pSetObjStatic)
721 : ENTRYPOINT_OFFSET(pSet32Static));
722 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
723 }
buzbee31a4a6f2012-02-28 15:36:15 -0800724}
725
buzbee408ad162012-06-06 16:45:18 -0700726void genSget(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700727 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800728{
Bill Buzbeea114add2012-05-03 15:00:40 -0700729 int fieldOffset;
730 int ssbIndex;
731 bool isVolatile;
732 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800733
Bill Buzbeea114add2012-05-03 15:00:40 -0700734 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
735 *cUnit->dex_file, *cUnit->dex_cache,
736 cUnit->code_item, cUnit->method_idx,
737 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800738
Bill Buzbeea114add2012-05-03 15:00:40 -0700739 bool fastPath =
740 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
741 fieldOffset, ssbIndex,
742 isReferrersClass, isVolatile,
743 false);
744 if (fastPath && !SLOW_FIELD_PATH) {
745 DCHECK_GE(fieldOffset, 0);
746 int rBase;
747 if (isReferrersClass) {
748 // Fast path, static storage base is this method's class
749 RegLocation rlMethod = loadCurrMethod(cUnit);
750 rBase = oatAllocTemp(cUnit);
751 loadWordDisp(cUnit, rlMethod.lowReg,
752 Method::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800753 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700754 // Medium path, static storage base in a different class which
755 // requires checks that the other class is initialized
756 DCHECK_GE(ssbIndex, 0);
757 // May do runtime call so everything to home locations.
758 oatFlushAllRegs(cUnit);
759 // Using fixed register to sync with possible call to runtime
760 // support
761 int rMethod = rARG1;
762 oatLockTemp(cUnit, rMethod);
763 loadCurrMethodDirect(cUnit, rMethod);
764 rBase = rARG0;
765 oatLockTemp(cUnit, rBase);
766 loadWordDisp(cUnit, rMethod,
767 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
768 rBase);
769 loadWordDisp(cUnit, rBase,
770 Array::DataOffset(sizeof(Object*)).Int32Value() +
771 sizeof(int32_t*) * ssbIndex, rBase);
772 // rBase now points at appropriate static storage base (Class*)
773 // or NULL if not initialized. Check for NULL and call helper if NULL.
774 // TUNING: fast path should fall through
775 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
776 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage),
777 ssbIndex);
778#if defined(TARGET_MIPS)
779 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
780 opRegCopy(cUnit, rBase, rRET0);
781#endif
782 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
783 branchOver->target = (LIR*)skipTarget;
784 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800785 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700786 // rBase now holds static storage base
Bill Buzbeea114add2012-05-03 15:00:40 -0700787 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
788 if (isVolatile) {
789 oatGenMemBarrier(cUnit, kSY);
790 }
791 if (isLongOrDouble) {
buzbee408ad162012-06-06 16:45:18 -0700792 loadBaseDispWide(cUnit, rBase, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700793 rlResult.highReg, INVALID_SREG);
794 } else {
795 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
796 }
797 oatFreeTemp(cUnit, rBase);
798 if (isLongOrDouble) {
799 storeValueWide(cUnit, rlDest, rlResult);
800 } else {
801 storeValue(cUnit, rlDest, rlResult);
802 }
803 } else {
804 oatFlushAllRegs(cUnit); // Everything to home locations
805 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Static) :
806 (isObject ? ENTRYPOINT_OFFSET(pGetObjStatic)
807 : ENTRYPOINT_OFFSET(pGet32Static));
808 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
809 if (isLongOrDouble) {
810 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
811 storeValueWide(cUnit, rlDest, rlResult);
812 } else {
813 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
814 storeValue(cUnit, rlDest, rlResult);
815 }
816 }
buzbee31a4a6f2012-02-28 15:36:15 -0800817}
818
819
820// Debugging routine - if null target, branch to DebugMe
821void genShowTarget(CompilationUnit* cUnit)
822{
buzbeea7678db2012-03-05 15:35:46 -0800823#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700824 UNIMPLEMENTED(WARNING) << "genShowTarget";
buzbeea7678db2012-03-05 15:35:46 -0800825#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700826 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
827 loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pDebugMe), rINVOKE_TGT);
828 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
829 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800830#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800831}
832
buzbee408ad162012-06-06 16:45:18 -0700833void genThrowVerificationError(CompilationUnit* cUnit, int info1, int info2)
buzbee31a4a6f2012-02-28 15:36:15 -0800834{
Bill Buzbeea114add2012-05-03 15:00:40 -0700835 callRuntimeHelperImmImm(cUnit,
836 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode),
buzbee408ad162012-06-06 16:45:18 -0700837 info1, info2);
buzbee31a4a6f2012-02-28 15:36:15 -0800838}
839
840void handleSuspendLaunchpads(CompilationUnit *cUnit)
841{
Bill Buzbeea114add2012-05-03 15:00:40 -0700842 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
843 int numElems = cUnit->suspendLaunchpads.numUsed;
844 for (int i = 0; i < numElems; i++) {
845 oatResetRegPool(cUnit);
846 oatResetDefTracking(cUnit);
847 LIR* lab = suspendLabel[i];
848 LIR* resumeLab = (LIR*)lab->operands[0];
849 cUnit->currentDalvikOffset = lab->operands[1];
850 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700851#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700853#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700854 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
855 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700856#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700857 opUnconditionalBranch(cUnit, resumeLab);
858 }
buzbee31a4a6f2012-02-28 15:36:15 -0800859}
860
buzbeefc9e6fa2012-03-23 15:14:29 -0700861void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
862{
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
864 int numElems = cUnit->intrinsicLaunchpads.numUsed;
865 for (int i = 0; i < numElems; i++) {
866 oatResetRegPool(cUnit);
867 oatResetDefTracking(cUnit);
868 LIR* lab = intrinsicLabel[i];
buzbee3b3dbdd2012-06-13 13:39:34 -0700869 CallInfo* info = (CallInfo*)lab->operands[0];
buzbee15bf9802012-06-12 17:49:27 -0700870 cUnit->currentDalvikOffset = info->offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700871 oatAppendLIR(cUnit, lab);
buzbee15bf9802012-06-12 17:49:27 -0700872 genInvoke(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 LIR* resumeLab = (LIR*)lab->operands[2];
874 if (resumeLab != NULL) {
875 opUnconditionalBranch(cUnit, resumeLab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700876 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700877 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700878}
879
buzbee31a4a6f2012-02-28 15:36:15 -0800880void handleThrowLaunchpads(CompilationUnit *cUnit)
881{
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
883 int numElems = cUnit->throwLaunchpads.numUsed;
884 for (int i = 0; i < numElems; i++) {
885 oatResetRegPool(cUnit);
886 oatResetDefTracking(cUnit);
887 LIR* lab = throwLabel[i];
888 cUnit->currentDalvikOffset = lab->operands[1];
889 oatAppendLIR(cUnit, lab);
890 int funcOffset = 0;
891 int v1 = lab->operands[2];
892 int v2 = lab->operands[3];
893 switch (lab->operands[0]) {
894 case kThrowNullPointer:
895 funcOffset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
896 break;
897 case kThrowArrayBounds:
898 if (v2 != rARG0) {
899 opRegCopy(cUnit, rARG0, v1);
900 opRegCopy(cUnit, rARG1, v2);
901 } else {
902 if (v1 == rARG1) {
buzbee31a4a6f2012-02-28 15:36:15 -0800903#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700904 int rTmp = r12;
buzbee31a4a6f2012-02-28 15:36:15 -0800905#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 int rTmp = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800907#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 opRegCopy(cUnit, rTmp, v1);
909 opRegCopy(cUnit, rARG1, v2);
910 opRegCopy(cUnit, rARG0, rTmp);
911 } else {
912 opRegCopy(cUnit, rARG1, v2);
913 opRegCopy(cUnit, rARG0, v1);
914 }
buzbee31a4a6f2012-02-28 15:36:15 -0800915 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700916 funcOffset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
917 break;
918 case kThrowDivZero:
919 funcOffset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
920 break;
921 case kThrowVerificationError:
922 loadConstant(cUnit, rARG0, v1);
923 loadConstant(cUnit, rARG1, v2);
924 funcOffset =
925 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode);
926 break;
927 case kThrowNoSuchMethod:
928 opRegCopy(cUnit, rARG0, v1);
929 funcOffset =
930 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
931 break;
932 case kThrowStackOverflow:
933 funcOffset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
934 // Restore stack alignment
Ian Rogersab2b55d2012-03-18 00:06:11 -0700935#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700936 opRegImm(cUnit, kOpAdd, rSP,
937 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700938#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700939 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700940#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700941 break;
942 default:
943 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800944 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 oatClobberCalleeSave(cUnit);
946#if !defined(TARGET_X86)
947 int rTgt = loadHelper(cUnit, funcOffset);
948 opReg(cUnit, kOpBlx, rTgt);
949 oatFreeTemp(cUnit, rTgt);
950#else
951 opThreadMem(cUnit, kOpBlx, funcOffset);
952#endif
953 }
buzbee31a4a6f2012-02-28 15:36:15 -0800954}
955
956/* Needed by the Assembler */
957void oatSetupResourceMasks(LIR* lir)
958{
Bill Buzbeea114add2012-05-03 15:00:40 -0700959 setupResourceMasks(lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800960}
961
buzbee16da88c2012-03-20 10:38:17 -0700962bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
963 int& fieldOffset, bool& isVolatile, bool isPut)
964{
Bill Buzbeea114add2012-05-03 15:00:40 -0700965 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
966 *cUnit->dex_file, *cUnit->dex_cache,
967 cUnit->code_item, cUnit->method_idx,
968 cUnit->access_flags);
969 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
970 fieldOffset, isVolatile, isPut);
buzbee16da88c2012-03-20 10:38:17 -0700971}
972
buzbee408ad162012-06-06 16:45:18 -0700973void genIGet(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -0800974 RegLocation rlDest, RegLocation rlObj,
Bill Buzbeea114add2012-05-03 15:00:40 -0700975 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800976{
Bill Buzbeea114add2012-05-03 15:00:40 -0700977 int fieldOffset;
978 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800979
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800981
Bill Buzbeea114add2012-05-03 15:00:40 -0700982 if (fastPath && !SLOW_FIELD_PATH) {
983 RegLocation rlResult;
984 RegisterClass regClass = oatRegClassBySize(size);
985 DCHECK_GE(fieldOffset, 0);
986 rlObj = loadValue(cUnit, rlObj, kCoreReg);
987 if (isLongOrDouble) {
988 DCHECK(rlDest.wide);
buzbee408ad162012-06-06 16:45:18 -0700989 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800990#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -0700992 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
993 loadBaseDispWide(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700994 rlResult.highReg, rlObj.sRegLow);
995 if (isVolatile) {
996 oatGenMemBarrier(cUnit, kSY);
997 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800998#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700999 int regPtr = oatAllocTemp(cUnit);
1000 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1001 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1002 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1003 if (isVolatile) {
1004 oatGenMemBarrier(cUnit, kSY);
1005 }
1006 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001007#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001009 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001010 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001011 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
1012 loadBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -07001013 kWord, rlObj.sRegLow);
1014 if (isVolatile) {
1015 oatGenMemBarrier(cUnit, kSY);
1016 }
1017 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001018 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001019 } else {
1020 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Instance) :
1021 (isObject ? ENTRYPOINT_OFFSET(pGetObjInstance)
1022 : ENTRYPOINT_OFFSET(pGet32Instance));
1023 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
1024 if (isLongOrDouble) {
1025 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
1026 storeValueWide(cUnit, rlDest, rlResult);
1027 } else {
1028 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
1029 storeValue(cUnit, rlDest, rlResult);
1030 }
1031 }
buzbee31a4a6f2012-02-28 15:36:15 -08001032}
1033
buzbee408ad162012-06-06 16:45:18 -07001034void genIPut(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
1035 RegLocation rlSrc, RegLocation rlObj, bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -08001036{
Bill Buzbeea114add2012-05-03 15:00:40 -07001037 int fieldOffset;
1038 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -08001039
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1041 true);
1042 if (fastPath && !SLOW_FIELD_PATH) {
1043 RegisterClass regClass = oatRegClassBySize(size);
1044 DCHECK_GE(fieldOffset, 0);
1045 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1046 if (isLongOrDouble) {
1047 int regPtr;
1048 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee408ad162012-06-06 16:45:18 -07001049 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001050 regPtr = oatAllocTemp(cUnit);
1051 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1052 if (isVolatile) {
1053 oatGenMemBarrier(cUnit, kST);
1054 }
jeffhao41005dd2012-05-09 17:58:52 -07001055 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001056 if (isVolatile) {
1057 oatGenMemBarrier(cUnit, kSY);
1058 }
1059 oatFreeTemp(cUnit, regPtr);
buzbee31a4a6f2012-02-28 15:36:15 -08001060 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001061 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee408ad162012-06-06 16:45:18 -07001062 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001063 if (isVolatile) {
1064 oatGenMemBarrier(cUnit, kST);
1065 }
1066 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1067 if (isVolatile) {
1068 oatGenMemBarrier(cUnit, kSY);
1069 }
1070 if (isObject) {
1071 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1072 }
buzbee31a4a6f2012-02-28 15:36:15 -08001073 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 } else {
1075 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Instance) :
1076 (isObject ? ENTRYPOINT_OFFSET(pSetObjInstance)
1077 : ENTRYPOINT_OFFSET(pSet32Instance));
1078 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1079 fieldIdx, rlObj, rlSrc);
1080 }
buzbee31a4a6f2012-02-28 15:36:15 -08001081}
1082
buzbee408ad162012-06-06 16:45:18 -07001083void genConstClass(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001084 RegLocation rlSrc)
1085{
Bill Buzbeea114add2012-05-03 15:00:40 -07001086 RegLocation rlMethod = loadCurrMethod(cUnit);
1087 int resReg = oatAllocTemp(cUnit);
1088 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1089 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1090 cUnit->dex_cache,
1091 *cUnit->dex_file,
1092 type_idx)) {
1093 // Call out to helper which resolves type and verifies access.
1094 // Resolved type returned in rRET0.
1095 callRuntimeHelperImmReg(cUnit,
1096 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1097 type_idx, rlMethod.lowReg);
1098 RegLocation rlResult = oatGetReturn(cUnit, false);
1099 storeValue(cUnit, rlDest, rlResult);
1100 } else {
1101 // We're don't need access checks, load type from dex cache
1102 int32_t dex_cache_offset =
1103 Method::DexCacheResolvedTypesOffset().Int32Value();
1104 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
1105 int32_t offset_of_type =
1106 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1107 * type_idx);
1108 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1109 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1110 type_idx) || SLOW_TYPE_PATH) {
1111 // Slow path, at runtime test if type is null and if so initialize
1112 oatFlushAllRegs(cUnit);
1113 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, NULL);
1114 // Resolved, store and hop over following code
1115 storeValue(cUnit, rlDest, rlResult);
1116 /*
1117 * Because we have stores of the target value on two paths,
1118 * clobber temp tracking for the destination using the ssa name
1119 */
1120 oatClobberSReg(cUnit, rlDest.sRegLow);
1121 LIR* branch2 = opUnconditionalBranch(cUnit,0);
1122 // TUNING: move slow path to end & remove unconditional branch
1123 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
1124 // Call out to helper, which will return resolved type in rARG0
1125 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1126 type_idx, rlMethod.lowReg);
1127 RegLocation rlResult = oatGetReturn(cUnit, false);
1128 storeValue(cUnit, rlDest, rlResult);
1129 /*
1130 * Because we have stores of the target value on two paths,
1131 * clobber temp tracking for the destination using the ssa name
1132 */
1133 oatClobberSReg(cUnit, rlDest.sRegLow);
1134 // Rejoin code paths
1135 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
1136 branch1->target = (LIR*)target1;
1137 branch2->target = (LIR*)target2;
buzbee31a4a6f2012-02-28 15:36:15 -08001138 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001139 // Fast path, we're done - just store result
1140 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001141 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001142 }
buzbee31a4a6f2012-02-28 15:36:15 -08001143}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001144
buzbee408ad162012-06-06 16:45:18 -07001145void genConstString(CompilationUnit* cUnit, uint32_t string_idx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001146 RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001147{
Bill Buzbeea114add2012-05-03 15:00:40 -07001148 /* NOTE: Most strings should be available at compile time */
Bill Buzbeea114add2012-05-03 15:00:40 -07001149 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1150 (sizeof(String*) * string_idx);
1151 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1152 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1153 // slow path, resolve string if not in dex cache
1154 oatFlushAllRegs(cUnit);
1155 oatLockCallTemps(cUnit); // Using explicit registers
1156 loadCurrMethodDirect(cUnit, rARG2);
1157 loadWordDisp(cUnit, rARG2,
1158 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1159 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001160#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001161 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001162#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1164 loadConstant(cUnit, rARG1, string_idx);
buzbee31a4a6f2012-02-28 15:36:15 -08001165#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1167 genBarrier(cUnit);
1168 // For testing, always force through helper
1169 if (!EXERCISE_SLOWEST_STRING_PATH) {
1170 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001171 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001172 opRegCopy(cUnit, rARG0, rARG2); // .eq
1173 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
1174 oatFreeTemp(cUnit, rTgt);
1175#elif defined(TARGET_MIPS)
1176 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1177 opRegCopy(cUnit, rARG0, rARG2); // .eq
1178 opReg(cUnit, kOpBlx, rTgt);
1179 oatFreeTemp(cUnit, rTgt);
1180 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1181 branch->target = target;
1182#else
1183 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode),
1184 rARG2, rARG1);
1185#endif
1186 genBarrier(cUnit);
1187 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
1188 } else {
1189 RegLocation rlMethod = loadCurrMethod(cUnit);
1190 int resReg = oatAllocTemp(cUnit);
1191 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1192 loadWordDisp(cUnit, rlMethod.lowReg,
1193 Method::DexCacheStringsOffset().Int32Value(), resReg);
1194 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1195 storeValue(cUnit, rlDest, rlResult);
1196 }
buzbee31a4a6f2012-02-28 15:36:15 -08001197}
1198
1199/*
1200 * Let helper function take care of everything. Will
1201 * call Class::NewInstanceFromCode(type_idx, method);
1202 */
buzbee408ad162012-06-06 16:45:18 -07001203void genNewInstance(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001204{
Bill Buzbeea114add2012-05-03 15:00:40 -07001205 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -07001206 // alloc will always check for resolution, do we also need to verify
1207 // access because the verifier was unable to?
1208 int funcOffset;
1209 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1210 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
1211 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
1212 } else {
1213 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
1214 }
1215 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
1216 RegLocation rlResult = oatGetReturn(cUnit, false);
1217 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001218}
1219
buzbee408ad162012-06-06 16:45:18 -07001220void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001221{
Bill Buzbeea114add2012-05-03 15:00:40 -07001222 oatFlushAllRegs(cUnit);
1223 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException),
1224 rlSrc);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001225}
1226
buzbee408ad162012-06-06 16:45:18 -07001227void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001228 RegLocation rlSrc)
1229{
Bill Buzbeea114add2012-05-03 15:00:40 -07001230 oatFlushAllRegs(cUnit);
1231 // May generate a call - use explicit registers
1232 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001233 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1234 int classReg = rARG2; // rARG2 will hold the Class*
1235 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1236 cUnit->dex_cache,
1237 *cUnit->dex_file,
1238 type_idx)) {
1239 // Check we have access to type_idx and if not throw IllegalAccessError,
1240 // returns Class* in rARG0
1241 callRuntimeHelperImm(cUnit,
1242 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1243 type_idx);
1244 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1245 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1246 } else {
1247 // Load dex cache entry into classReg (rARG2)
1248 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1249 loadWordDisp(cUnit, rARG1,
1250 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1251 int32_t offset_of_type =
1252 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1253 * type_idx);
1254 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1255 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1256 cUnit->dex_cache, type_idx)) {
1257 // Need to test presence of type in dex cache at runtime
1258 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1259 // Not resolved
1260 // Call out to helper, which will return resolved type in rRET0
1261 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1262 type_idx);
1263 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1264 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1265 // Rejoin code paths
1266 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1267 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001268 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001269 }
1270 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
1271 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1272 /* load object->klass_ */
1273 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1274 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1275 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001276#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001277 /* Uses conditional nullification */
1278 int rTgt = loadHelper(cUnit,
1279 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1280 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1281 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1282 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1283 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1284 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1285 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001286#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001287 /* Uses branchovers */
1288 loadConstant(cUnit, rARG0, 1); // assume true
1289 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001290#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001291 int rTgt = loadHelper(cUnit,
1292 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1293 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1294 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1295 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001296#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001297 opRegCopy(cUnit, rARG0, rARG2);
1298 opThreadMem(cUnit, kOpBlx,
1299 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001300#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001301#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001302 oatClobberCalleeSave(cUnit);
1303 /* branch targets here */
1304 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1305 RegLocation rlResult = oatGetReturn(cUnit, false);
1306 storeValue(cUnit, rlDest, rlResult);
1307 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001308#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001309 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001310#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001311}
1312
buzbee408ad162012-06-06 16:45:18 -07001313void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001314{
Bill Buzbeea114add2012-05-03 15:00:40 -07001315 oatFlushAllRegs(cUnit);
1316 // May generate a call - use explicit registers
1317 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001318 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1319 int classReg = rARG2; // rARG2 will hold the Class*
1320 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1321 cUnit->dex_cache,
1322 *cUnit->dex_file,
1323 type_idx)) {
1324 // Check we have access to type_idx and if not throw IllegalAccessError,
1325 // returns Class* in rRET0
1326 // InitializeTypeAndVerifyAccess(idx, method)
1327 callRuntimeHelperImmReg(cUnit,
1328 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1329 type_idx, rARG1);
1330 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1331 } else {
1332 // Load dex cache entry into classReg (rARG2)
1333 loadWordDisp(cUnit, rARG1,
1334 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1335 int32_t offset_of_type =
1336 Array::DataOffset(sizeof(Class*)).Int32Value() +
1337 (sizeof(Class*) * type_idx);
1338 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1339 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1340 cUnit->dex_cache, type_idx)) {
1341 // Need to test presence of type in dex cache at runtime
1342 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1343 // Not resolved
1344 // Call out to helper, which will return resolved type in rARG0
1345 // InitializeTypeFromCode(idx, method)
1346 callRuntimeHelperImmReg(cUnit,
1347 ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1348 type_idx, rARG1);
1349 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1350 // Rejoin code paths
1351 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1352 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001353 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001354 }
1355 // At this point, classReg (rARG2) has class
1356 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1357 /* Null is OK - continue */
1358 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1359 /* load object->klass_ */
1360 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1361 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1362 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001363#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001364 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
1365 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode),
1366 rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001367#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001368 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1369 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1370 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1371 opRegCopy(cUnit, rARG0, rARG1);
1372 opRegCopy(cUnit, rARG1, rARG2);
1373 oatClobberCalleeSave(cUnit);
1374 opReg(cUnit, kOpBlx, rTgt);
1375 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001376#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001377 /* branch target here */
1378 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1379 branch1->target = target;
1380 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001381}
1382
buzbee31a4a6f2012-02-28 15:36:15 -08001383/*
1384 * Generate array store
1385 *
1386 */
buzbee408ad162012-06-06 16:45:18 -07001387void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001388 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001389{
Bill Buzbeea114add2012-05-03 15:00:40 -07001390 int lenOffset = Array::LengthOffset().Int32Value();
1391 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001392
Bill Buzbeea114add2012-05-03 15:00:40 -07001393 oatFlushAllRegs(cUnit); // Use explicit registers
1394 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001395
Bill Buzbeea114add2012-05-03 15:00:40 -07001396 int rValue = rARG0; // Register holding value
1397 int rArrayClass = rARG1; // Register holding array's Class
1398 int rArray = rARG2; // Register holding array
1399 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001400
Bill Buzbeea114add2012-05-03 15:00:40 -07001401 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1402 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1403 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001404
buzbee408ad162012-06-06 16:45:18 -07001405 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001406
Bill Buzbeea114add2012-05-03 15:00:40 -07001407 // Store of null?
1408 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001409
Bill Buzbeea114add2012-05-03 15:00:40 -07001410 // Get the array's class.
1411 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
1412 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode),
1413 rValue, rArrayClass);
1414 // Redo loadValues in case they didn't survive the call.
1415 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1416 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1417 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1418 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001419
Bill Buzbeea114add2012-05-03 15:00:40 -07001420 // Branch here if value to be stored == null
1421 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1422 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001423
Ian Rogersb41b33b2012-03-20 14:22:54 -07001424#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001425 // make an extra temp available for card mark below
1426 oatFreeTemp(cUnit, rARG1);
buzbee408ad162012-06-06 16:45:18 -07001427 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001428 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
buzbee408ad162012-06-06 16:45:18 -07001429 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001430 }
buzbee408ad162012-06-06 16:45:18 -07001431 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001432 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001433#else
buzbee408ad162012-06-06 16:45:18 -07001434 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001435 int regLen = INVALID_REG;
1436 if (needsRangeCheck) {
1437 regLen = rARG1;
1438 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen); // Get len
1439 }
1440 /* rPtr -> array data */
1441 int rPtr = oatAllocTemp(cUnit);
1442 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1443 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001444 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001445 }
1446 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1447 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001448#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001449 oatFreeTemp(cUnit, rIndex);
1450 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001451}
1452
1453/*
1454 * Generate array load
1455 */
buzbee408ad162012-06-06 16:45:18 -07001456void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001457 RegLocation rlArray, RegLocation rlIndex,
1458 RegLocation rlDest, int scale)
1459{
Bill Buzbeea114add2012-05-03 15:00:40 -07001460 RegisterClass regClass = oatRegClassBySize(size);
1461 int lenOffset = Array::LengthOffset().Int32Value();
1462 int dataOffset;
1463 RegLocation rlResult;
1464 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1465 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001466
Bill Buzbeea114add2012-05-03 15:00:40 -07001467 if (size == kLong || size == kDouble) {
1468 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1469 } else {
1470 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1471 }
buzbee31a4a6f2012-02-28 15:36:15 -08001472
Bill Buzbeea114add2012-05-03 15:00:40 -07001473 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001474 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001475
Ian Rogersb5d09b22012-03-06 22:14:17 -08001476#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001477 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001478 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1479 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001480 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001481 }
1482 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001483 int regAddr = oatAllocTemp(cUnit);
1484 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1485 oatFreeTemp(cUnit, rlArray.lowReg);
1486 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001487 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001488 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
jeffhao21e12712012-05-25 19:06:18 -07001489 rlResult.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001490 storeValueWide(cUnit, rlDest, rlResult);
1491 } else {
1492 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001493
buzbee408ad162012-06-06 16:45:18 -07001494 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001495 dataOffset, rlResult.lowReg, INVALID_REG, size,
1496 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001497
Bill Buzbeea114add2012-05-03 15:00:40 -07001498 storeValue(cUnit, rlDest, rlResult);
1499 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001500#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001501 int regPtr = oatAllocTemp(cUnit);
buzbee408ad162012-06-06 16:45:18 -07001502 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001503 int regLen = INVALID_REG;
1504 if (needsRangeCheck) {
1505 regLen = oatAllocTemp(cUnit);
1506 /* Get len */
1507 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1508 }
1509 /* regPtr -> array data */
1510 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1511 oatFreeTemp(cUnit, rlArray.lowReg);
1512 if ((size == kLong) || (size == kDouble)) {
1513 if (scale) {
1514 int rNewIndex = oatAllocTemp(cUnit);
1515 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1516 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1517 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001518 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001519 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001520 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001521 oatFreeTemp(cUnit, rlIndex.lowReg);
1522 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1523
1524 if (needsRangeCheck) {
1525 // TODO: change kCondCS to a more meaningful name, is the sense of
1526 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001527 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001528 oatFreeTemp(cUnit, regLen);
1529 }
1530 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1531
1532 oatFreeTemp(cUnit, regPtr);
1533 storeValueWide(cUnit, rlDest, rlResult);
1534 } else {
1535 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1536
1537 if (needsRangeCheck) {
1538 // TODO: change kCondCS to a more meaningful name, is the sense of
1539 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001540 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001541 oatFreeTemp(cUnit, regLen);
1542 }
1543 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1544 scale, size);
1545
1546 oatFreeTemp(cUnit, regPtr);
1547 storeValue(cUnit, rlDest, rlResult);
1548 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001549#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001550}
1551
1552/*
1553 * Generate array store
1554 *
1555 */
buzbee408ad162012-06-06 16:45:18 -07001556void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001557 RegLocation rlArray, RegLocation rlIndex,
1558 RegLocation rlSrc, int scale)
1559{
Bill Buzbeea114add2012-05-03 15:00:40 -07001560 RegisterClass regClass = oatRegClassBySize(size);
1561 int lenOffset = Array::LengthOffset().Int32Value();
1562 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001563
Bill Buzbeea114add2012-05-03 15:00:40 -07001564 if (size == kLong || size == kDouble) {
1565 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1566 } else {
1567 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1568 }
buzbee31a4a6f2012-02-28 15:36:15 -08001569
Bill Buzbeea114add2012-05-03 15:00:40 -07001570 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1571 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001572#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001573 int regPtr;
1574 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1575 oatClobber(cUnit, rlArray.lowReg);
1576 regPtr = rlArray.lowReg;
1577 } else {
1578 regPtr = oatAllocTemp(cUnit);
1579 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1580 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001581#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001582
Bill Buzbeea114add2012-05-03 15:00:40 -07001583 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001584 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001585
Ian Rogersb41b33b2012-03-20 14:22:54 -07001586#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001587 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001588 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1589 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001590 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001591 }
1592 if ((size == kLong) || (size == kDouble)) {
1593 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1594 } else {
1595 rlSrc = loadValue(cUnit, rlSrc, regClass);
1596 }
buzbee408ad162012-06-06 16:45:18 -07001597 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001598 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1599 INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001600#else
buzbee408ad162012-06-06 16:45:18 -07001601 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001602 int regLen = INVALID_REG;
1603 if (needsRangeCheck) {
1604 regLen = oatAllocTemp(cUnit);
1605 //NOTE: max live temps(4) here.
1606 /* Get len */
1607 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1608 }
1609 /* regPtr -> array data */
1610 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1611 /* at this point, regPtr points to array, 2 live temps */
1612 if ((size == kLong) || (size == kDouble)) {
1613 //TUNING: specific wide routine that can handle fp regs
1614 if (scale) {
1615 int rNewIndex = oatAllocTemp(cUnit);
1616 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1617 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1618 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001619 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001620 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001621 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001622 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1623
1624 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001625 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001626 oatFreeTemp(cUnit, regLen);
1627 }
1628
jeffhao41005dd2012-05-09 17:58:52 -07001629 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001630
1631 oatFreeTemp(cUnit, regPtr);
1632 } else {
1633 rlSrc = loadValue(cUnit, rlSrc, regClass);
1634 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001635 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001636 oatFreeTemp(cUnit, regLen);
1637 }
1638 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1639 scale, size);
1640 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001641#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001642}
1643
buzbee408ad162012-06-06 16:45:18 -07001644void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001645 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001646 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001647{
Bill Buzbeea114add2012-05-03 15:00:40 -07001648 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001649#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001650 /*
1651 * NOTE: This is the one place in the code in which we might have
1652 * as many as six live temporary registers. There are 5 in the normal
1653 * set for Arm. Until we have spill capabilities, temporarily add
1654 * lr to the temp set. It is safe to do this locally, but note that
1655 * lr is used explicitly elsewhere in the code generator and cannot
1656 * normally be used as a general temp register.
1657 */
1658 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1659 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001660#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001661 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1662 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1663 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1664 // The longs may overlap - use intermediate temp if so
1665 if (rlResult.lowReg == rlSrc1.highReg) {
1666 int tReg = oatAllocTemp(cUnit);
1667 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1668 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1669 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1670 oatFreeTemp(cUnit, tReg);
1671 } else {
1672 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1673 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1674 rlSrc2.highReg);
1675 }
1676 /*
1677 * NOTE: If rlDest refers to a frame variable in a large frame, the
1678 * following storeValueWide might need to allocate a temp register.
1679 * To further work around the lack of a spill capability, explicitly
1680 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1681 * Remove when spill is functional.
1682 */
1683 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1684 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1685 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001686#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001687 oatClobber(cUnit, rLR);
1688 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001689#endif
1690}
1691
1692
buzbee408ad162012-06-06 16:45:18 -07001693bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001694 RegLocation rlSrc1, RegLocation rlShift)
1695{
Bill Buzbeea114add2012-05-03 15:00:40 -07001696 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001697
buzbee408ad162012-06-06 16:45:18 -07001698 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001699 case Instruction::SHL_LONG:
1700 case Instruction::SHL_LONG_2ADDR:
1701 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1702 break;
1703 case Instruction::SHR_LONG:
1704 case Instruction::SHR_LONG_2ADDR:
1705 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1706 break;
1707 case Instruction::USHR_LONG:
1708 case Instruction::USHR_LONG_2ADDR:
1709 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1710 break;
1711 default:
1712 LOG(FATAL) << "Unexpected case";
1713 return true;
1714 }
1715 oatFlushAllRegs(cUnit); /* Send everything to home location */
1716 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
1717 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1718 storeValueWide(cUnit, rlDest, rlResult);
1719 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001720}
1721
1722
buzbee408ad162012-06-06 16:45:18 -07001723bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001724 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001725{
Bill Buzbeea114add2012-05-03 15:00:40 -07001726 OpKind op = kOpBkpt;
1727 bool callOut = false;
1728 bool checkZero = false;
1729 bool unary = false;
1730 RegLocation rlResult;
1731 bool shiftOp = false;
1732 int funcOffset;
1733 int retReg = rRET0;
buzbee408ad162012-06-06 16:45:18 -07001734 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001735 case Instruction::NEG_INT:
1736 op = kOpNeg;
1737 unary = true;
1738 break;
1739 case Instruction::NOT_INT:
1740 op = kOpMvn;
1741 unary = true;
1742 break;
1743 case Instruction::ADD_INT:
1744 case Instruction::ADD_INT_2ADDR:
1745 op = kOpAdd;
1746 break;
1747 case Instruction::SUB_INT:
1748 case Instruction::SUB_INT_2ADDR:
1749 op = kOpSub;
1750 break;
1751 case Instruction::MUL_INT:
1752 case Instruction::MUL_INT_2ADDR:
1753 op = kOpMul;
1754 break;
1755 case Instruction::DIV_INT:
1756 case Instruction::DIV_INT_2ADDR:
1757 checkZero = true;
1758 op = kOpDiv;
1759 callOut = true;
1760 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1761 retReg = rRET0;
1762 break;
1763 /* NOTE: returns in rARG1 */
1764 case Instruction::REM_INT:
1765 case Instruction::REM_INT_2ADDR:
1766 checkZero = true;
1767 op = kOpRem;
1768 callOut = true;
1769 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1770 retReg = rRET1;
1771 break;
1772 case Instruction::AND_INT:
1773 case Instruction::AND_INT_2ADDR:
1774 op = kOpAnd;
1775 break;
1776 case Instruction::OR_INT:
1777 case Instruction::OR_INT_2ADDR:
1778 op = kOpOr;
1779 break;
1780 case Instruction::XOR_INT:
1781 case Instruction::XOR_INT_2ADDR:
1782 op = kOpXor;
1783 break;
1784 case Instruction::SHL_INT:
1785 case Instruction::SHL_INT_2ADDR:
1786 shiftOp = true;
1787 op = kOpLsl;
1788 break;
1789 case Instruction::SHR_INT:
1790 case Instruction::SHR_INT_2ADDR:
1791 shiftOp = true;
1792 op = kOpAsr;
1793 break;
1794 case Instruction::USHR_INT:
1795 case Instruction::USHR_INT_2ADDR:
1796 shiftOp = true;
1797 op = kOpLsr;
1798 break;
1799 default:
1800 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001801 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001802 }
1803 if (!callOut) {
1804 if (unary) {
1805 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1806 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1807 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001808 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001809 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001810#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001811 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1812 int tReg = oatAllocTemp(cUnit);
1813 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001814#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001815 // X86 doesn't require masking and must use ECX
1816 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1817 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001818#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001819 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1820 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1821 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1822 oatFreeTemp(cUnit, tReg);
1823 } else {
1824 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1825 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1826 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1827 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1828 }
buzbee31a4a6f2012-02-28 15:36:15 -08001829 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001830 storeValue(cUnit, rlDest, rlResult);
1831 } else {
1832 RegLocation rlResult;
1833 oatFlushAllRegs(cUnit); /* Send everything to home location */
1834 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1835#if !defined(TARGET_X86)
1836 int rTgt = loadHelper(cUnit, funcOffset);
1837#endif
1838 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1839 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001840 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001841 }
1842#if !defined(TARGET_X86)
1843 opReg(cUnit, kOpBlx, rTgt);
1844 oatFreeTemp(cUnit, rTgt);
1845#else
1846 opThreadMem(cUnit, kOpBlx, funcOffset);
1847#endif
1848 if (retReg == rRET0)
1849 rlResult = oatGetReturn(cUnit, false);
1850 else
1851 rlResult = oatGetReturnAlt(cUnit);
1852 storeValue(cUnit, rlDest, rlResult);
1853 }
1854 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001855}
1856
1857/*
1858 * The following are the first-level codegen routines that analyze the format
1859 * of each bytecode then either dispatch special purpose codegen routines
1860 * or produce corresponding Thumb instructions directly.
1861 */
1862
1863bool isPowerOfTwo(int x)
1864{
Bill Buzbeea114add2012-05-03 15:00:40 -07001865 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001866}
1867
1868// Returns true if no more than two bits are set in 'x'.
1869bool isPopCountLE2(unsigned int x)
1870{
Bill Buzbeea114add2012-05-03 15:00:40 -07001871 x &= x - 1;
1872 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001873}
1874
1875// Returns the index of the lowest set bit in 'x'.
1876int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001877 int bit_posn = 0;
1878 while ((x & 0xf) == 0) {
1879 bit_posn += 4;
1880 x >>= 4;
1881 }
1882 while ((x & 1) == 0) {
1883 bit_posn++;
1884 x >>= 1;
1885 }
1886 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001887}
1888
1889// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1890// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001891bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001892 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001893{
buzbeef3aac972012-04-11 16:33:36 -07001894#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001895 // No divide instruction for Arm, so check for more special cases
1896 if (lit < 2) {
1897 return false;
1898 }
1899 if (!isPowerOfTwo(lit)) {
1900 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1901 }
buzbeef3aac972012-04-11 16:33:36 -07001902#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001903 if (lit < 2 || !isPowerOfTwo(lit)) {
1904 return false;
1905 }
buzbeef3aac972012-04-11 16:33:36 -07001906#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001907 int k = lowestSetBit(lit);
1908 if (k >= 30) {
1909 // Avoid special cases.
1910 return false;
1911 }
1912 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1913 dalvikOpcode == Instruction::DIV_INT_LIT16);
1914 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1915 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1916 if (div) {
1917 int tReg = oatAllocTemp(cUnit);
1918 if (lit == 2) {
1919 // Division by 2 is by far the most common division by constant.
1920 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1921 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1922 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001923 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001924 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1925 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1926 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1927 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001928 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001929 } else {
1930 int tReg1 = oatAllocTemp(cUnit);
1931 int tReg2 = oatAllocTemp(cUnit);
1932 if (lit == 2) {
1933 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1934 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1935 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1936 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1937 } else {
1938 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1939 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1940 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1941 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1942 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1943 }
1944 }
1945 storeValue(cUnit, rlDest, rlResult);
1946 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001947}
1948
1949void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1950 RegLocation rlResult, int lit,
1951 int firstBit, int secondBit)
1952{
buzbee0398c422012-03-02 15:22:47 -08001953#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001954 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1955 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001956#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001957 int tReg = oatAllocTemp(cUnit);
1958 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1959 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1960 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001961#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001962 if (firstBit != 0) {
1963 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1964 }
buzbee31a4a6f2012-02-28 15:36:15 -08001965}
1966
1967// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1968// and store the result in 'rlDest'.
1969bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1970 RegLocation rlDest, int lit)
1971{
Bill Buzbeea114add2012-05-03 15:00:40 -07001972 // Can we simplify this multiplication?
1973 bool powerOfTwo = false;
1974 bool popCountLE2 = false;
1975 bool powerOfTwoMinusOne = false;
1976 if (lit < 2) {
1977 // Avoid special cases.
1978 return false;
1979 } else if (isPowerOfTwo(lit)) {
1980 powerOfTwo = true;
1981 } else if (isPopCountLE2(lit)) {
1982 popCountLE2 = true;
1983 } else if (isPowerOfTwo(lit + 1)) {
1984 powerOfTwoMinusOne = true;
1985 } else {
1986 return false;
1987 }
1988 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1989 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1990 if (powerOfTwo) {
1991 // Shift.
1992 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1993 lowestSetBit(lit));
1994 } else if (popCountLE2) {
1995 // Shift and add and shift.
1996 int firstBit = lowestSetBit(lit);
1997 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1998 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1999 firstBit, secondBit);
2000 } else {
2001 // Reverse subtract: (src << (shift + 1)) - src.
2002 DCHECK(powerOfTwoMinusOne);
2003 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2004 int tReg = oatAllocTemp(cUnit);
2005 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2006 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2007 }
2008 storeValue(cUnit, rlDest, rlResult);
2009 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002010}
2011
buzbee408ad162012-06-06 16:45:18 -07002012bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2013 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002014{
Bill Buzbeea114add2012-05-03 15:00:40 -07002015 RegLocation rlResult;
2016 OpKind op = (OpKind)0; /* Make gcc happy */
2017 int shiftOp = false;
2018 bool isDiv = false;
2019 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002020
buzbee408ad162012-06-06 16:45:18 -07002021 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002022 case Instruction::RSUB_INT_LIT8:
2023 case Instruction::RSUB_INT: {
2024 int tReg;
2025 //TUNING: add support for use of Arm rsub op
2026 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2027 tReg = oatAllocTemp(cUnit);
2028 loadConstant(cUnit, tReg, lit);
2029 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2030 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2031 storeValue(cUnit, rlDest, rlResult);
2032 return false;
2033 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002034 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002035
2036 case Instruction::ADD_INT_LIT8:
2037 case Instruction::ADD_INT_LIT16:
2038 op = kOpAdd;
2039 break;
2040 case Instruction::MUL_INT_LIT8:
2041 case Instruction::MUL_INT_LIT16: {
2042 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2043 return false;
2044 }
2045 op = kOpMul;
2046 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002047 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002048 case Instruction::AND_INT_LIT8:
2049 case Instruction::AND_INT_LIT16:
2050 op = kOpAnd;
2051 break;
2052 case Instruction::OR_INT_LIT8:
2053 case Instruction::OR_INT_LIT16:
2054 op = kOpOr;
2055 break;
2056 case Instruction::XOR_INT_LIT8:
2057 case Instruction::XOR_INT_LIT16:
2058 op = kOpXor;
2059 break;
2060 case Instruction::SHL_INT_LIT8:
2061 lit &= 31;
2062 shiftOp = true;
2063 op = kOpLsl;
2064 break;
2065 case Instruction::SHR_INT_LIT8:
2066 lit &= 31;
2067 shiftOp = true;
2068 op = kOpAsr;
2069 break;
2070 case Instruction::USHR_INT_LIT8:
2071 lit &= 31;
2072 shiftOp = true;
2073 op = kOpLsr;
2074 break;
2075
2076 case Instruction::DIV_INT_LIT8:
2077 case Instruction::DIV_INT_LIT16:
2078 case Instruction::REM_INT_LIT8:
2079 case Instruction::REM_INT_LIT16:
2080 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002081 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002082 return false;
2083 }
buzbee408ad162012-06-06 16:45:18 -07002084 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002085 return false;
2086 }
2087 oatFlushAllRegs(cUnit); /* Everything to home location */
2088 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2089 oatClobber(cUnit, rARG0);
2090 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee408ad162012-06-06 16:45:18 -07002091 if ((opcode == Instruction::DIV_INT_LIT8) ||
2092 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002093 isDiv = true;
2094 } else {
2095 isDiv = false;
2096 }
2097 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
2098 if (isDiv)
2099 rlResult = oatGetReturn(cUnit, false);
2100 else
2101 rlResult = oatGetReturnAlt(cUnit);
2102 storeValue(cUnit, rlDest, rlResult);
2103 return false;
2104 break;
2105 default:
2106 return true;
2107 }
2108 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2109 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2110 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2111 if (shiftOp && (lit == 0)) {
2112 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2113 } else {
2114 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2115 }
2116 storeValue(cUnit, rlDest, rlResult);
2117 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002118}
2119
buzbee408ad162012-06-06 16:45:18 -07002120bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002121 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002122{
Bill Buzbeea114add2012-05-03 15:00:40 -07002123 RegLocation rlResult;
2124 OpKind firstOp = kOpBkpt;
2125 OpKind secondOp = kOpBkpt;
2126 bool callOut = false;
2127 bool checkZero = false;
2128 int funcOffset;
2129 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002130
buzbee408ad162012-06-06 16:45:18 -07002131 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002132 case Instruction::NOT_LONG:
2133 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2134 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2135 // Check for destructive overlap
2136 if (rlResult.lowReg == rlSrc2.highReg) {
2137 int tReg = oatAllocTemp(cUnit);
2138 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2139 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2140 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2141 oatFreeTemp(cUnit, tReg);
2142 } else {
2143 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2144 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2145 }
2146 storeValueWide(cUnit, rlDest, rlResult);
2147 return false;
2148 break;
2149 case Instruction::ADD_LONG:
2150 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002151#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002152 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002153#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002154 firstOp = kOpAdd;
2155 secondOp = kOpAdc;
2156 break;
buzbeec5159d52012-03-03 11:48:39 -08002157#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002158 case Instruction::SUB_LONG:
2159 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002160#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002161 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002162#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002163 firstOp = kOpSub;
2164 secondOp = kOpSbc;
2165 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002166#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002167 case Instruction::MUL_LONG:
2168 case Instruction::MUL_LONG_2ADDR:
2169 callOut = true;
2170 retReg = rRET0;
2171 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2172 break;
2173 case Instruction::DIV_LONG:
2174 case Instruction::DIV_LONG_2ADDR:
2175 callOut = true;
2176 checkZero = true;
2177 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002178 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002179 break;
2180 case Instruction::REM_LONG:
2181 case Instruction::REM_LONG_2ADDR:
2182 callOut = true;
2183 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002184 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002185#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002186 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2187 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002188#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002189 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002190#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002191 break;
2192 case Instruction::AND_LONG_2ADDR:
2193 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002194#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002195 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002196#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002197 firstOp = kOpAnd;
2198 secondOp = kOpAnd;
2199 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002200#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002201 case Instruction::OR_LONG:
2202 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002203#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002204 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002205#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002206 firstOp = kOpOr;
2207 secondOp = kOpOr;
2208 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002209#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002210 case Instruction::XOR_LONG:
2211 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002212#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002213 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002214#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002215 firstOp = kOpXor;
2216 secondOp = kOpXor;
2217 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002218#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002219 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002220 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002221 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002222 default:
2223 LOG(FATAL) << "Invalid long arith op";
2224 }
2225 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002226 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002227 } else {
2228 oatFlushAllRegs(cUnit); /* Send everything to home location */
2229 if (checkZero) {
2230 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2231#if !defined(TARGET_X86)
2232 int rTgt = loadHelper(cUnit, funcOffset);
2233#endif
2234 int tReg = oatAllocTemp(cUnit);
2235#if defined(TARGET_ARM)
2236 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2237 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002238 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002239#else
2240 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2241#endif
buzbee408ad162012-06-06 16:45:18 -07002242 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002243 oatFreeTemp(cUnit, tReg);
2244 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2245#if !defined(TARGET_X86)
2246 opReg(cUnit, kOpBlx, rTgt);
2247 oatFreeTemp(cUnit, rTgt);
2248#else
2249 opThreadMem(cUnit, kOpBlx, funcOffset);
2250#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002251 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002252 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2253 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002254 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002255 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2256 if (retReg == rRET0)
2257 rlResult = oatGetReturnWide(cUnit, false);
2258 else
2259 rlResult = oatGetReturnWideAlt(cUnit);
2260 storeValueWide(cUnit, rlDest, rlResult);
2261 }
2262 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002263}
2264
buzbee408ad162012-06-06 16:45:18 -07002265bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2266 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002267{
Bill Buzbeea114add2012-05-03 15:00:40 -07002268 /*
2269 * Don't optimize the register usage since it calls out to support
2270 * functions
2271 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002272 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002273 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002274 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002275 } else {
2276 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002277 }
2278 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002279 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002280 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002281 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2282 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002283 } else {
2284 RegLocation rlResult;
2285 rlResult = oatGetReturn(cUnit, rlDest.fp);
2286 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002287 }
2288 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002289}
2290
2291void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002292bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002293 RegLocation rlDest, RegLocation rlSrc1,
2294 RegLocation rlSrc2)
2295{
Bill Buzbeea114add2012-05-03 15:00:40 -07002296 RegLocation rlResult;
2297 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002298
buzbee408ad162012-06-06 16:45:18 -07002299 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002300 case Instruction::ADD_FLOAT_2ADDR:
2301 case Instruction::ADD_FLOAT:
2302 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2303 break;
2304 case Instruction::SUB_FLOAT_2ADDR:
2305 case Instruction::SUB_FLOAT:
2306 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2307 break;
2308 case Instruction::DIV_FLOAT_2ADDR:
2309 case Instruction::DIV_FLOAT:
2310 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2311 break;
2312 case Instruction::MUL_FLOAT_2ADDR:
2313 case Instruction::MUL_FLOAT:
2314 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2315 break;
2316 case Instruction::REM_FLOAT_2ADDR:
2317 case Instruction::REM_FLOAT:
2318 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2319 break;
2320 case Instruction::NEG_FLOAT: {
2321 genNegFloat(cUnit, rlDest, rlSrc1);
2322 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002323 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002324 default:
2325 return true;
2326 }
2327 oatFlushAllRegs(cUnit); /* Send everything to home location */
2328 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2329 rlResult = oatGetReturn(cUnit, true);
2330 storeValue(cUnit, rlDest, rlResult);
2331 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002332}
2333
2334void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002335bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002336 RegLocation rlDest, RegLocation rlSrc1,
2337 RegLocation rlSrc2)
2338{
Bill Buzbeea114add2012-05-03 15:00:40 -07002339 RegLocation rlResult;
2340 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002341
buzbee408ad162012-06-06 16:45:18 -07002342 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002343 case Instruction::ADD_DOUBLE_2ADDR:
2344 case Instruction::ADD_DOUBLE:
2345 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2346 break;
2347 case Instruction::SUB_DOUBLE_2ADDR:
2348 case Instruction::SUB_DOUBLE:
2349 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2350 break;
2351 case Instruction::DIV_DOUBLE_2ADDR:
2352 case Instruction::DIV_DOUBLE:
2353 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2354 break;
2355 case Instruction::MUL_DOUBLE_2ADDR:
2356 case Instruction::MUL_DOUBLE:
2357 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2358 break;
2359 case Instruction::REM_DOUBLE_2ADDR:
2360 case Instruction::REM_DOUBLE:
2361 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2362 break;
2363 case Instruction::NEG_DOUBLE: {
2364 genNegDouble(cUnit, rlDest, rlSrc1);
2365 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002366 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002367 default:
2368 return true;
2369 }
2370 oatFlushAllRegs(cUnit); /* Send everything to home location */
2371 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2372 rlResult = oatGetReturnWide(cUnit, true);
2373 storeValueWide(cUnit, rlDest, rlResult);
2374 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002375}
2376
buzbee408ad162012-06-06 16:45:18 -07002377bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2378 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002379{
buzbee31a4a6f2012-02-28 15:36:15 -08002380
Bill Buzbeea114add2012-05-03 15:00:40 -07002381 switch (opcode) {
2382 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002383 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2384 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002385 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002386 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2387 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002388 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002389 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2390 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002391 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002392 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2393 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002394 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002395 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2396 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002397 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002398 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2399 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002400 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002401 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2402 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002403 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002404 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2405 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002406 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002407 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2408 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002409 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002410 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2411 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002412 default:
2413 return true;
2414 }
2415 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002416}
2417
2418/*
2419 * Generate callout to updateDebugger. Note that we're overloading
2420 * the use of rSUSPEND here. When the debugger is active, this
2421 * register holds the address of the update function. So, if it's
2422 * non-null, we call out to it.
2423 *
2424 * Note also that rRET0 and rRET1 must be preserved across this
2425 * code. This must be handled by the stub.
2426 */
2427void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2428{
Bill Buzbeea114add2012-05-03 15:00:40 -07002429 // Following DCHECK verifies that dPC is in range of single load immediate
2430 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2431 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2432 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002433#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002434 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2435 opIT(cUnit, kArmCondNe, "T");
2436 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2437 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002438#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002439 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002440#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002441 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2442 loadConstant(cUnit, rARG2, offset);
2443 opReg(cUnit, kOpBlx, rSUSPEND);
2444 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2445 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002446#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002447 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002448}
2449
2450/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002451void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002452{
buzbee408ad162012-06-06 16:45:18 -07002453 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002454 return;
2455 }
2456 oatFlushAllRegs(cUnit);
2457 if (cUnit->genDebugger) {
2458 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002459#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002460 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002461#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002462 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
2463 opReg(cUnit, kOpBlx, rTgt);
2464 // Refresh rSUSPEND
2465 loadWordDisp(cUnit, rSELF,
2466 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2467 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002468#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002469 } else {
2470 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002471#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002472 // In non-debug case, only check periodically
2473 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2474 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002475#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002476 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2477 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002478#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002479 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2480 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002481#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002482 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2483 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002484 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002485 branch->target = (LIR*)target;
2486 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2487 }
buzbee31a4a6f2012-02-28 15:36:15 -08002488}
2489
buzbeefead2932012-03-30 14:02:01 -07002490/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002491void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002492{
buzbee408ad162012-06-06 16:45:18 -07002493 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002494 opUnconditionalBranch(cUnit, target);
2495 return;
2496 }
2497 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002498 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002499 opUnconditionalBranch(cUnit, target);
2500 } else {
buzbeefead2932012-03-30 14:02:01 -07002501#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002502 // In non-debug case, only check periodically
2503 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2504 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002505#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002506 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2507 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002508#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002509 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2510 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002511#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002512 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002513 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002514 oatFlushAllRegs(cUnit);
2515 opUnconditionalBranch(cUnit, launchPad);
2516 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2517 (intptr_t)launchPad);
2518 }
buzbeefead2932012-03-30 14:02:01 -07002519}
2520
buzbee31a4a6f2012-02-28 15:36:15 -08002521} // namespace art