blob: 6cb701f8297188235a8f1c10dce530bddd06f85c [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,
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 *cUnit->dex_file,
506 type_idx)) {
507 funcOffset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
508 } else {
509 funcOffset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
510 }
511 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
512 RegLocation rlResult = oatGetReturn(cUnit, false);
513 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800514}
515
516/*
517 * Similar to genNewArray, but with post-allocation initialization.
518 * Verifier guarantees we're dealing with an array class. Current
519 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
520 * Current code also throws internal unimp if not 'L', '[' or 'I'.
521 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700522void genFilledNewArray(CompilationUnit* cUnit, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800523{
buzbee3b3dbdd2012-06-13 13:39:34 -0700524 int elems = info->numArgWords;
525 int typeIdx = info->index;
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 oatFlushAllRegs(cUnit); /* Everything to home location */
527 int funcOffset;
528 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 *cUnit->dex_file,
530 typeIdx)) {
531 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
532 } else {
533 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
534 }
535 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
536 oatFreeTemp(cUnit, rARG2);
537 oatFreeTemp(cUnit, rARG1);
538 /*
539 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
540 * return region. Because AllocFromCode placed the new array
541 * in rRET0, we'll just lock it into place. When debugger support is
542 * added, it may be necessary to additionally copy all return
543 * values to a home location in thread-local storage
544 */
545 oatLockTemp(cUnit, rRET0);
546
547 // TODO: use the correct component size, currently all supported types
548 // share array alignment with ints (see comment at head of function)
549 size_t component_size = sizeof(int32_t);
550
551 // Having a range of 0 is legal
buzbee3b3dbdd2012-06-13 13:39:34 -0700552 if (info->isRange && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800553 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700554 * Bit of ugliness here. We're going generate a mem copy loop
555 * on the register range, but it is possible that some regs
556 * in the range have been promoted. This is unlikely, but
557 * before generating the copy, we'll just force a flush
558 * of any regs in the source range that have been promoted to
559 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800560 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700561 for (int i = 0; i < elems; i++) {
562 RegLocation loc = oatUpdateLoc(cUnit, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700563 if (loc.location == kLocPhysReg) {
564 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
565 loc.lowReg, kWord);
566 }
buzbee31a4a6f2012-02-28 15:36:15 -0800567 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700568 /*
569 * TUNING note: generated code here could be much improved, but
570 * this is an uncommon operation and isn't especially performance
571 * critical.
572 */
573 int rSrc = oatAllocTemp(cUnit);
574 int rDst = oatAllocTemp(cUnit);
575 int rIdx = oatAllocTemp(cUnit);
576#if defined(TARGET_ARM)
577 int rVal = rLR; // Using a lot of temps, rLR is known free here
578#elif defined(TARGET_X86)
jeffhao5772bab2012-05-18 11:51:26 -0700579 oatFreeTemp(cUnit, rRET0);
580 int rVal = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700581#else
582 int rVal = oatAllocTemp(cUnit);
583#endif
584 // Set up source pointer
buzbee3b3dbdd2012-06-13 13:39:34 -0700585 RegLocation rlFirst = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700586 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
587 oatSRegOffset(cUnit, rlFirst.sRegLow));
588 // Set up the target pointer
589 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
590 Array::DataOffset(component_size).Int32Value());
591 // Set up the loop counter (known to be > 0)
buzbee3b3dbdd2012-06-13 13:39:34 -0700592 loadConstant(cUnit, rIdx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700593 // Generate the copy loop. Going backwards for convenience
594 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
595 // Copy next element
596 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
597 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
598#if defined(TARGET_ARM)
599 // Combine sub & test using sub setflags encoding here
600 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
601 opCondBranch(cUnit, kCondGe, target);
602#else
603 oatFreeTemp(cUnit, rVal);
604 opRegImm(cUnit, kOpSub, rIdx, 1);
605 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
606#endif
jeffhao5772bab2012-05-18 11:51:26 -0700607#if defined(TARGET_X86)
608 // Restore the target pointer
609 opRegRegImm(cUnit, kOpAdd, rRET0, rDst,
610 -Array::DataOffset(component_size).Int32Value());
611#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700612 } else if (!info->isRange) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700613 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700614 for (int i = 0; i < elems; i++) {
615 RegLocation rlArg = loadValue(cUnit, info->args[i], kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 storeBaseDisp(cUnit, rRET0,
617 Array::DataOffset(component_size).Int32Value() +
618 i * 4, rlArg.lowReg, kWord);
619 // If the loadValue caused a temp to be allocated, free it
620 if (oatIsTemp(cUnit, rlArg.lowReg)) {
621 oatFreeTemp(cUnit, rlArg.lowReg);
622 }
623 }
624 }
buzbeee5f01222012-06-14 15:19:35 -0700625 if (info->result.location != kLocInvalid) {
626 storeValue(cUnit, info->result, oatGetReturn(cUnit, false /* not fp */));
627 }
buzbee31a4a6f2012-02-28 15:36:15 -0800628}
629
buzbee408ad162012-06-06 16:45:18 -0700630void genSput(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800632{
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 int fieldOffset;
634 int ssbIndex;
635 bool isVolatile;
636 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800637
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker, *cUnit->dex_file,
639 cUnit->code_item, cUnit->method_idx, cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800640
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 bool fastPath =
642 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
643 fieldOffset, ssbIndex,
644 isReferrersClass, isVolatile,
645 true);
646 if (fastPath && !SLOW_FIELD_PATH) {
647 DCHECK_GE(fieldOffset, 0);
648 int rBase;
649 if (isReferrersClass) {
650 // Fast path, static storage base is this method's class
651 RegLocation rlMethod = loadCurrMethod(cUnit);
652 rBase = oatAllocTemp(cUnit);
653 loadWordDisp(cUnit, rlMethod.lowReg,
654 Method::DeclaringClassOffset().Int32Value(), rBase);
655 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
656 oatFreeTemp(cUnit, rlMethod.lowReg);
657 }
buzbee31a4a6f2012-02-28 15:36:15 -0800658 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700659 // Medium path, static storage base in a different class which
660 // requires checks that the other class is initialized.
661 DCHECK_GE(ssbIndex, 0);
662 // May do runtime call so everything to home locations.
663 oatFlushAllRegs(cUnit);
664 // Using fixed register to sync with possible call to runtime
665 // support.
666 int rMethod = rARG1;
667 oatLockTemp(cUnit, rMethod);
668 loadCurrMethodDirect(cUnit, rMethod);
669 rBase = rARG0;
670 oatLockTemp(cUnit, rBase);
671 loadWordDisp(cUnit, rMethod,
672 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
673 rBase);
674 loadWordDisp(cUnit, rBase,
675 Array::DataOffset(sizeof(Object*)).Int32Value() +
676 sizeof(int32_t*) * ssbIndex, rBase);
677 // rBase now points at appropriate static storage base (Class*)
678 // or NULL if not initialized. Check for NULL and call helper if NULL.
679 // TUNING: fast path should fall through
680 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
681 loadConstant(cUnit, rARG0, ssbIndex);
682 callRuntimeHelperImm(cUnit,
683 ENTRYPOINT_OFFSET(pInitializeStaticStorage),
684 ssbIndex);
685#if defined(TARGET_MIPS)
686 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
687 opRegCopy(cUnit, rBase, rRET0);
688#endif
689 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
690 branchOver->target = (LIR*)skipTarget;
691 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800692 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700693 // rBase now holds static storage base
694 if (isLongOrDouble) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
696 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700697 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
698 }
699//FIXME: need to generalize the barrier call
700 if (isVolatile) {
701 oatGenMemBarrier(cUnit, kST);
702 }
703 if (isLongOrDouble) {
704 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
705 rlSrc.highReg);
706 } else {
707 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
708 }
709 if (isVolatile) {
710 oatGenMemBarrier(cUnit, kSY);
711 }
712 if (isObject) {
713 markGCCard(cUnit, rlSrc.lowReg, rBase);
714 }
715 oatFreeTemp(cUnit, rBase);
716 } else {
717 oatFlushAllRegs(cUnit); // Everything to home locations
718 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Static) :
719 (isObject ? ENTRYPOINT_OFFSET(pSetObjStatic)
720 : ENTRYPOINT_OFFSET(pSet32Static));
721 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
722 }
buzbee31a4a6f2012-02-28 15:36:15 -0800723}
724
buzbee408ad162012-06-06 16:45:18 -0700725void genSget(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700726 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800727{
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 int fieldOffset;
729 int ssbIndex;
730 bool isVolatile;
731 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800732
Bill Buzbeea114add2012-05-03 15:00:40 -0700733 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 *cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700735 cUnit->code_item, cUnit->method_idx,
736 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800737
Bill Buzbeea114add2012-05-03 15:00:40 -0700738 bool fastPath =
739 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
740 fieldOffset, ssbIndex,
741 isReferrersClass, isVolatile,
742 false);
743 if (fastPath && !SLOW_FIELD_PATH) {
744 DCHECK_GE(fieldOffset, 0);
745 int rBase;
746 if (isReferrersClass) {
747 // Fast path, static storage base is this method's class
748 RegLocation rlMethod = loadCurrMethod(cUnit);
749 rBase = oatAllocTemp(cUnit);
750 loadWordDisp(cUnit, rlMethod.lowReg,
751 Method::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800752 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 // Medium path, static storage base in a different class which
754 // requires checks that the other class is initialized
755 DCHECK_GE(ssbIndex, 0);
756 // May do runtime call so everything to home locations.
757 oatFlushAllRegs(cUnit);
758 // Using fixed register to sync with possible call to runtime
759 // support
760 int rMethod = rARG1;
761 oatLockTemp(cUnit, rMethod);
762 loadCurrMethodDirect(cUnit, rMethod);
763 rBase = rARG0;
764 oatLockTemp(cUnit, rBase);
765 loadWordDisp(cUnit, rMethod,
766 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
767 rBase);
768 loadWordDisp(cUnit, rBase,
769 Array::DataOffset(sizeof(Object*)).Int32Value() +
770 sizeof(int32_t*) * ssbIndex, rBase);
771 // rBase now points at appropriate static storage base (Class*)
772 // or NULL if not initialized. Check for NULL and call helper if NULL.
773 // TUNING: fast path should fall through
774 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
775 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage),
776 ssbIndex);
777#if defined(TARGET_MIPS)
778 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
779 opRegCopy(cUnit, rBase, rRET0);
780#endif
781 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
782 branchOver->target = (LIR*)skipTarget;
783 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800784 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 // rBase now holds static storage base
Bill Buzbeea114add2012-05-03 15:00:40 -0700786 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
787 if (isVolatile) {
788 oatGenMemBarrier(cUnit, kSY);
789 }
790 if (isLongOrDouble) {
buzbee408ad162012-06-06 16:45:18 -0700791 loadBaseDispWide(cUnit, rBase, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700792 rlResult.highReg, INVALID_SREG);
793 } else {
794 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
795 }
796 oatFreeTemp(cUnit, rBase);
797 if (isLongOrDouble) {
798 storeValueWide(cUnit, rlDest, rlResult);
799 } else {
800 storeValue(cUnit, rlDest, rlResult);
801 }
802 } else {
803 oatFlushAllRegs(cUnit); // Everything to home locations
804 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Static) :
805 (isObject ? ENTRYPOINT_OFFSET(pGetObjStatic)
806 : ENTRYPOINT_OFFSET(pGet32Static));
807 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
808 if (isLongOrDouble) {
809 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
810 storeValueWide(cUnit, rlDest, rlResult);
811 } else {
812 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
813 storeValue(cUnit, rlDest, rlResult);
814 }
815 }
buzbee31a4a6f2012-02-28 15:36:15 -0800816}
817
818
819// Debugging routine - if null target, branch to DebugMe
820void genShowTarget(CompilationUnit* cUnit)
821{
buzbeea7678db2012-03-05 15:35:46 -0800822#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700823 UNIMPLEMENTED(WARNING) << "genShowTarget";
buzbeea7678db2012-03-05 15:35:46 -0800824#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
826 loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pDebugMe), rINVOKE_TGT);
827 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
828 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800829#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800830}
831
buzbee408ad162012-06-06 16:45:18 -0700832void genThrowVerificationError(CompilationUnit* cUnit, int info1, int info2)
buzbee31a4a6f2012-02-28 15:36:15 -0800833{
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 callRuntimeHelperImmImm(cUnit,
835 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode),
buzbee408ad162012-06-06 16:45:18 -0700836 info1, info2);
buzbee31a4a6f2012-02-28 15:36:15 -0800837}
838
839void handleSuspendLaunchpads(CompilationUnit *cUnit)
840{
Bill Buzbeea114add2012-05-03 15:00:40 -0700841 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
842 int numElems = cUnit->suspendLaunchpads.numUsed;
843 for (int i = 0; i < numElems; i++) {
844 oatResetRegPool(cUnit);
845 oatResetDefTracking(cUnit);
846 LIR* lab = suspendLabel[i];
847 LIR* resumeLab = (LIR*)lab->operands[0];
848 cUnit->currentDalvikOffset = lab->operands[1];
849 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700850#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700851 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700852#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700853 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
854 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700855#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700856 opUnconditionalBranch(cUnit, resumeLab);
857 }
buzbee31a4a6f2012-02-28 15:36:15 -0800858}
859
buzbeefc9e6fa2012-03-23 15:14:29 -0700860void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
861{
Bill Buzbeea114add2012-05-03 15:00:40 -0700862 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
863 int numElems = cUnit->intrinsicLaunchpads.numUsed;
864 for (int i = 0; i < numElems; i++) {
865 oatResetRegPool(cUnit);
866 oatResetDefTracking(cUnit);
867 LIR* lab = intrinsicLabel[i];
buzbee3b3dbdd2012-06-13 13:39:34 -0700868 CallInfo* info = (CallInfo*)lab->operands[0];
buzbee15bf9802012-06-12 17:49:27 -0700869 cUnit->currentDalvikOffset = info->offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700870 oatAppendLIR(cUnit, lab);
buzbee15bf9802012-06-12 17:49:27 -0700871 genInvoke(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700872 LIR* resumeLab = (LIR*)lab->operands[2];
873 if (resumeLab != NULL) {
874 opUnconditionalBranch(cUnit, resumeLab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700875 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700877}
878
buzbee31a4a6f2012-02-28 15:36:15 -0800879void handleThrowLaunchpads(CompilationUnit *cUnit)
880{
Bill Buzbeea114add2012-05-03 15:00:40 -0700881 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
882 int numElems = cUnit->throwLaunchpads.numUsed;
883 for (int i = 0; i < numElems; i++) {
884 oatResetRegPool(cUnit);
885 oatResetDefTracking(cUnit);
886 LIR* lab = throwLabel[i];
887 cUnit->currentDalvikOffset = lab->operands[1];
888 oatAppendLIR(cUnit, lab);
889 int funcOffset = 0;
890 int v1 = lab->operands[2];
891 int v2 = lab->operands[3];
892 switch (lab->operands[0]) {
893 case kThrowNullPointer:
894 funcOffset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
895 break;
896 case kThrowArrayBounds:
jeffhao44335e12012-06-18 13:48:25 -0700897 // Move v1 (array index) to rARG0 and v2 (array length) to rARG1
Bill Buzbeea114add2012-05-03 15:00:40 -0700898 if (v2 != rARG0) {
899 opRegCopy(cUnit, rARG0, v1);
jeffhao703f2cd2012-07-13 17:25:52 -0700900#if defined (TARGET_X86)
901 // x86 leaves the array pointer in v2, so load the array length that the handler expects
902 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
903#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700904 opRegCopy(cUnit, rARG1, v2);
jeffhao703f2cd2012-07-13 17:25:52 -0700905#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 } else {
907 if (v1 == rARG1) {
jeffhao44335e12012-06-18 13:48:25 -0700908 // Swap v1 and v2, using rARG2 as a temp
909 opRegCopy(cUnit, rARG2, v1);
jeffhao703f2cd2012-07-13 17:25:52 -0700910#if defined (TARGET_X86)
911 // x86 leaves the array pointer in v2, so load the array length that the handler expects
912 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
913#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700914 opRegCopy(cUnit, rARG1, v2);
jeffhao703f2cd2012-07-13 17:25:52 -0700915#endif
jeffhao44335e12012-06-18 13:48:25 -0700916 opRegCopy(cUnit, rARG0, rARG2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700917 } else {
jeffhao703f2cd2012-07-13 17:25:52 -0700918#if defined (TARGET_X86)
919 // x86 leaves the array pointer in v2, so load the array length that the handler expects
920 opRegMem(cUnit, kOpMov, rARG1, v2, Array::LengthOffset().Int32Value());
921#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 opRegCopy(cUnit, rARG1, v2);
jeffhao703f2cd2012-07-13 17:25:52 -0700923#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700924 opRegCopy(cUnit, rARG0, v1);
925 }
buzbee31a4a6f2012-02-28 15:36:15 -0800926 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700927 funcOffset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
928 break;
929 case kThrowDivZero:
930 funcOffset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
931 break;
932 case kThrowVerificationError:
933 loadConstant(cUnit, rARG0, v1);
934 loadConstant(cUnit, rARG1, v2);
935 funcOffset =
936 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode);
937 break;
938 case kThrowNoSuchMethod:
939 opRegCopy(cUnit, rARG0, v1);
940 funcOffset =
941 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
942 break;
943 case kThrowStackOverflow:
944 funcOffset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
945 // Restore stack alignment
Ian Rogersab2b55d2012-03-18 00:06:11 -0700946#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 opRegImm(cUnit, kOpAdd, rSP,
948 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700949#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700951#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700952 break;
953 default:
954 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800955 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700956 oatClobberCalleeSave(cUnit);
957#if !defined(TARGET_X86)
958 int rTgt = loadHelper(cUnit, funcOffset);
959 opReg(cUnit, kOpBlx, rTgt);
960 oatFreeTemp(cUnit, rTgt);
961#else
962 opThreadMem(cUnit, kOpBlx, funcOffset);
963#endif
964 }
buzbee31a4a6f2012-02-28 15:36:15 -0800965}
966
967/* Needed by the Assembler */
968void oatSetupResourceMasks(LIR* lir)
969{
Bill Buzbeea114add2012-05-03 15:00:40 -0700970 setupResourceMasks(lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800971}
972
buzbee16da88c2012-03-20 10:38:17 -0700973bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
974 int& fieldOffset, bool& isVolatile, bool isPut)
975{
Bill Buzbeea114add2012-05-03 15:00:40 -0700976 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 *cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700978 cUnit->code_item, cUnit->method_idx,
979 cUnit->access_flags);
980 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
981 fieldOffset, isVolatile, isPut);
buzbee16da88c2012-03-20 10:38:17 -0700982}
983
buzbee408ad162012-06-06 16:45:18 -0700984void genIGet(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -0800985 RegLocation rlDest, RegLocation rlObj,
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800987{
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 int fieldOffset;
989 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800990
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800992
Bill Buzbeea114add2012-05-03 15:00:40 -0700993 if (fastPath && !SLOW_FIELD_PATH) {
994 RegLocation rlResult;
995 RegisterClass regClass = oatRegClassBySize(size);
996 DCHECK_GE(fieldOffset, 0);
997 rlObj = loadValue(cUnit, rlObj, kCoreReg);
998 if (isLongOrDouble) {
999 DCHECK(rlDest.wide);
buzbee408ad162012-06-06 16:45:18 -07001000 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001001#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001002 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001003 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
1004 loadBaseDispWide(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -07001005 rlResult.highReg, rlObj.sRegLow);
1006 if (isVolatile) {
1007 oatGenMemBarrier(cUnit, kSY);
1008 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001009#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001010 int regPtr = oatAllocTemp(cUnit);
1011 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1012 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1013 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1014 if (isVolatile) {
1015 oatGenMemBarrier(cUnit, kSY);
1016 }
1017 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001018#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001019 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001020 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001021 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001022 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
1023 loadBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 kWord, rlObj.sRegLow);
1025 if (isVolatile) {
1026 oatGenMemBarrier(cUnit, kSY);
1027 }
1028 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001029 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001030 } else {
1031 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Instance) :
1032 (isObject ? ENTRYPOINT_OFFSET(pGetObjInstance)
1033 : ENTRYPOINT_OFFSET(pGet32Instance));
1034 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
1035 if (isLongOrDouble) {
1036 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
1037 storeValueWide(cUnit, rlDest, rlResult);
1038 } else {
1039 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
1040 storeValue(cUnit, rlDest, rlResult);
1041 }
1042 }
buzbee31a4a6f2012-02-28 15:36:15 -08001043}
1044
buzbee408ad162012-06-06 16:45:18 -07001045void genIPut(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
1046 RegLocation rlSrc, RegLocation rlObj, bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -08001047{
Bill Buzbeea114add2012-05-03 15:00:40 -07001048 int fieldOffset;
1049 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -08001050
Bill Buzbeea114add2012-05-03 15:00:40 -07001051 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1052 true);
1053 if (fastPath && !SLOW_FIELD_PATH) {
1054 RegisterClass regClass = oatRegClassBySize(size);
1055 DCHECK_GE(fieldOffset, 0);
1056 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1057 if (isLongOrDouble) {
1058 int regPtr;
1059 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee408ad162012-06-06 16:45:18 -07001060 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001061 regPtr = oatAllocTemp(cUnit);
1062 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1063 if (isVolatile) {
1064 oatGenMemBarrier(cUnit, kST);
1065 }
jeffhao41005dd2012-05-09 17:58:52 -07001066 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001067 if (isVolatile) {
1068 oatGenMemBarrier(cUnit, kSY);
1069 }
1070 oatFreeTemp(cUnit, regPtr);
buzbee31a4a6f2012-02-28 15:36:15 -08001071 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001072 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee408ad162012-06-06 16:45:18 -07001073 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 if (isVolatile) {
1075 oatGenMemBarrier(cUnit, kST);
1076 }
1077 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1078 if (isVolatile) {
1079 oatGenMemBarrier(cUnit, kSY);
1080 }
1081 if (isObject) {
1082 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1083 }
buzbee31a4a6f2012-02-28 15:36:15 -08001084 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001085 } else {
1086 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Instance) :
1087 (isObject ? ENTRYPOINT_OFFSET(pSetObjInstance)
1088 : ENTRYPOINT_OFFSET(pSet32Instance));
1089 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1090 fieldIdx, rlObj, rlSrc);
1091 }
buzbee31a4a6f2012-02-28 15:36:15 -08001092}
1093
buzbee6969d502012-06-15 16:40:31 -07001094void genConstClass(CompilationUnit* cUnit, uint32_t type_idx,
1095 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001096{
Bill Buzbeea114add2012-05-03 15:00:40 -07001097 RegLocation rlMethod = loadCurrMethod(cUnit);
1098 int resReg = oatAllocTemp(cUnit);
1099 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1100 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001101 *cUnit->dex_file,
1102 type_idx)) {
1103 // Call out to helper which resolves type and verifies access.
1104 // Resolved type returned in rRET0.
1105 callRuntimeHelperImmReg(cUnit,
1106 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1107 type_idx, rlMethod.lowReg);
1108 RegLocation rlResult = oatGetReturn(cUnit, false);
1109 storeValue(cUnit, rlDest, rlResult);
1110 } else {
1111 // We're don't need access checks, load type from dex cache
1112 int32_t dex_cache_offset =
1113 Method::DexCacheResolvedTypesOffset().Int32Value();
1114 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
1115 int32_t offset_of_type =
1116 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1117 * type_idx);
1118 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(*cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001120 type_idx) || SLOW_TYPE_PATH) {
1121 // Slow path, at runtime test if type is null and if so initialize
1122 oatFlushAllRegs(cUnit);
1123 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, NULL);
1124 // Resolved, store and hop over following code
1125 storeValue(cUnit, rlDest, rlResult);
1126 /*
1127 * Because we have stores of the target value on two paths,
1128 * clobber temp tracking for the destination using the ssa name
1129 */
1130 oatClobberSReg(cUnit, rlDest.sRegLow);
1131 LIR* branch2 = opUnconditionalBranch(cUnit,0);
1132 // TUNING: move slow path to end & remove unconditional branch
1133 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
1134 // Call out to helper, which will return resolved type in rARG0
1135 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1136 type_idx, rlMethod.lowReg);
1137 RegLocation rlResult = oatGetReturn(cUnit, false);
1138 storeValue(cUnit, rlDest, rlResult);
1139 /*
1140 * Because we have stores of the target value on two paths,
1141 * clobber temp tracking for the destination using the ssa name
1142 */
1143 oatClobberSReg(cUnit, rlDest.sRegLow);
1144 // Rejoin code paths
1145 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
1146 branch1->target = (LIR*)target1;
1147 branch2->target = (LIR*)target2;
buzbee31a4a6f2012-02-28 15:36:15 -08001148 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001149 // Fast path, we're done - just store result
1150 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001151 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001152 }
buzbee31a4a6f2012-02-28 15:36:15 -08001153}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001154
buzbee6969d502012-06-15 16:40:31 -07001155void genConstString(CompilationUnit* cUnit, uint32_t string_idx,
1156 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001157{
Bill Buzbeea114add2012-05-03 15:00:40 -07001158 /* NOTE: Most strings should be available at compile time */
Bill Buzbeea114add2012-05-03 15:00:40 -07001159 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1160 (sizeof(String*) * string_idx);
1161 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 *cUnit->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 // slow path, resolve string if not in dex cache
1164 oatFlushAllRegs(cUnit);
1165 oatLockCallTemps(cUnit); // Using explicit registers
1166 loadCurrMethodDirect(cUnit, rARG2);
1167 loadWordDisp(cUnit, rARG2,
1168 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1169 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001170#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001171 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001172#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001173 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1174 loadConstant(cUnit, rARG1, string_idx);
buzbee31a4a6f2012-02-28 15:36:15 -08001175#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001176 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1177 genBarrier(cUnit);
1178 // For testing, always force through helper
1179 if (!EXERCISE_SLOWEST_STRING_PATH) {
1180 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001181 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001182 opRegCopy(cUnit, rARG0, rARG2); // .eq
1183 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
1184 oatFreeTemp(cUnit, rTgt);
1185#elif defined(TARGET_MIPS)
1186 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1187 opRegCopy(cUnit, rARG0, rARG2); // .eq
1188 opReg(cUnit, kOpBlx, rTgt);
1189 oatFreeTemp(cUnit, rTgt);
1190 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1191 branch->target = target;
1192#else
1193 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode),
1194 rARG2, rARG1);
1195#endif
1196 genBarrier(cUnit);
1197 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
1198 } else {
1199 RegLocation rlMethod = loadCurrMethod(cUnit);
1200 int resReg = oatAllocTemp(cUnit);
1201 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1202 loadWordDisp(cUnit, rlMethod.lowReg,
1203 Method::DexCacheStringsOffset().Int32Value(), resReg);
1204 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1205 storeValue(cUnit, rlDest, rlResult);
1206 }
buzbee31a4a6f2012-02-28 15:36:15 -08001207}
1208
1209/*
1210 * Let helper function take care of everything. Will
1211 * call Class::NewInstanceFromCode(type_idx, method);
1212 */
buzbee408ad162012-06-06 16:45:18 -07001213void genNewInstance(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001214{
Bill Buzbeea114add2012-05-03 15:00:40 -07001215 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -07001216 // alloc will always check for resolution, do we also need to verify
1217 // access because the verifier was unable to?
1218 int funcOffset;
1219 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 cUnit->method_idx, *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001221 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
1222 } else {
1223 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
1224 }
1225 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
1226 RegLocation rlResult = oatGetReturn(cUnit, false);
1227 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001228}
1229
buzbee408ad162012-06-06 16:45:18 -07001230void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001231{
Bill Buzbeea114add2012-05-03 15:00:40 -07001232 oatFlushAllRegs(cUnit);
1233 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException),
1234 rlSrc);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001235}
1236
buzbee408ad162012-06-06 16:45:18 -07001237void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001238 RegLocation rlSrc)
1239{
Bill Buzbeea114add2012-05-03 15:00:40 -07001240 oatFlushAllRegs(cUnit);
1241 // May generate a call - use explicit registers
1242 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001243 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1244 int classReg = rARG2; // rARG2 will hold the Class*
1245 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001246 *cUnit->dex_file,
1247 type_idx)) {
1248 // Check we have access to type_idx and if not throw IllegalAccessError,
1249 // returns Class* in rARG0
1250 callRuntimeHelperImm(cUnit,
1251 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1252 type_idx);
1253 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1254 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1255 } else {
1256 // Load dex cache entry into classReg (rARG2)
1257 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1258 loadWordDisp(cUnit, rARG1,
1259 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1260 int32_t offset_of_type =
1261 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1262 * type_idx);
1263 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1264 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001266 // Need to test presence of type in dex cache at runtime
1267 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1268 // Not resolved
1269 // Call out to helper, which will return resolved type in rRET0
1270 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1271 type_idx);
1272 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1273 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1274 // Rejoin code paths
1275 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1276 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001277 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001278 }
1279 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
1280 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1281 /* load object->klass_ */
1282 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1283 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1284 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001285#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001286 /* Uses conditional nullification */
1287 int rTgt = loadHelper(cUnit,
1288 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1289 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1290 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1291 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1292 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1293 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1294 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001295#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001296 /* Uses branchovers */
1297 loadConstant(cUnit, rARG0, 1); // assume true
1298 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001299#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001300 int rTgt = loadHelper(cUnit,
1301 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1302 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1303 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1304 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001305#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001306 opRegCopy(cUnit, rARG0, rARG2);
1307 opThreadMem(cUnit, kOpBlx,
1308 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001309#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001310#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001311 oatClobberCalleeSave(cUnit);
1312 /* branch targets here */
1313 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1314 RegLocation rlResult = oatGetReturn(cUnit, false);
1315 storeValue(cUnit, rlDest, rlResult);
1316 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001317#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001318 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001319#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001320}
1321
buzbee408ad162012-06-06 16:45:18 -07001322void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001323{
Bill Buzbeea114add2012-05-03 15:00:40 -07001324 oatFlushAllRegs(cUnit);
1325 // May generate a call - use explicit registers
1326 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001327 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1328 int classReg = rARG2; // rARG2 will hold the Class*
1329 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
Bill Buzbeea114add2012-05-03 15:00:40 -07001330 *cUnit->dex_file,
1331 type_idx)) {
1332 // Check we have access to type_idx and if not throw IllegalAccessError,
1333 // returns Class* in rRET0
1334 // InitializeTypeAndVerifyAccess(idx, method)
1335 callRuntimeHelperImmReg(cUnit,
1336 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1337 type_idx, rARG1);
1338 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1339 } else {
1340 // Load dex cache entry into classReg (rARG2)
1341 loadWordDisp(cUnit, rARG1,
1342 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1343 int32_t offset_of_type =
1344 Array::DataOffset(sizeof(Class*)).Int32Value() +
1345 (sizeof(Class*) * type_idx);
1346 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1347 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001348 *cUnit->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001349 // Need to test presence of type in dex cache at runtime
1350 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1351 // Not resolved
1352 // Call out to helper, which will return resolved type in rARG0
1353 // InitializeTypeFromCode(idx, method)
1354 callRuntimeHelperImmReg(cUnit,
1355 ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1356 type_idx, rARG1);
1357 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1358 // Rejoin code paths
1359 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1360 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001361 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001362 }
1363 // At this point, classReg (rARG2) has class
1364 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1365 /* Null is OK - continue */
1366 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1367 /* load object->klass_ */
1368 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1369 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1370 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001371#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001372 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
1373 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode),
1374 rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001375#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001376 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1377 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1378 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1379 opRegCopy(cUnit, rARG0, rARG1);
1380 opRegCopy(cUnit, rARG1, rARG2);
1381 oatClobberCalleeSave(cUnit);
1382 opReg(cUnit, kOpBlx, rTgt);
1383 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001384#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001385 /* branch target here */
1386 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1387 branch1->target = target;
1388 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001389}
1390
buzbee31a4a6f2012-02-28 15:36:15 -08001391/*
1392 * Generate array store
1393 *
1394 */
buzbee408ad162012-06-06 16:45:18 -07001395void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001396 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001397{
Bill Buzbeea114add2012-05-03 15:00:40 -07001398 int lenOffset = Array::LengthOffset().Int32Value();
1399 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001400
Bill Buzbeea114add2012-05-03 15:00:40 -07001401 oatFlushAllRegs(cUnit); // Use explicit registers
1402 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001403
Bill Buzbeea114add2012-05-03 15:00:40 -07001404 int rValue = rARG0; // Register holding value
1405 int rArrayClass = rARG1; // Register holding array's Class
1406 int rArray = rARG2; // Register holding array
1407 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001408
Bill Buzbeea114add2012-05-03 15:00:40 -07001409 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1410 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1411 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001412
buzbee408ad162012-06-06 16:45:18 -07001413 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001414
Bill Buzbeea114add2012-05-03 15:00:40 -07001415 // Store of null?
1416 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001417
Bill Buzbeea114add2012-05-03 15:00:40 -07001418 // Get the array's class.
1419 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
1420 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode),
1421 rValue, rArrayClass);
1422 // Redo loadValues in case they didn't survive the call.
1423 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1424 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1425 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1426 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001427
Bill Buzbeea114add2012-05-03 15:00:40 -07001428 // Branch here if value to be stored == null
1429 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1430 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001431
Ian Rogersb41b33b2012-03-20 14:22:54 -07001432#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001433 // make an extra temp available for card mark below
1434 oatFreeTemp(cUnit, rARG1);
buzbee408ad162012-06-06 16:45:18 -07001435 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001436 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
buzbee408ad162012-06-06 16:45:18 -07001437 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001438 }
buzbee408ad162012-06-06 16:45:18 -07001439 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001440 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001441#else
buzbee408ad162012-06-06 16:45:18 -07001442 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001443 int regLen = INVALID_REG;
1444 if (needsRangeCheck) {
1445 regLen = rARG1;
buzbeef1f86362012-07-10 15:18:31 -07001446 loadWordDisp(cUnit, rArray, lenOffset, regLen); // Get len
Bill Buzbeea114add2012-05-03 15:00:40 -07001447 }
1448 /* rPtr -> array data */
1449 int rPtr = oatAllocTemp(cUnit);
1450 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1451 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001452 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001453 }
1454 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1455 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001456#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001457 oatFreeTemp(cUnit, rIndex);
1458 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001459}
1460
1461/*
1462 * Generate array load
1463 */
buzbee408ad162012-06-06 16:45:18 -07001464void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001465 RegLocation rlArray, RegLocation rlIndex,
1466 RegLocation rlDest, int scale)
1467{
Bill Buzbeea114add2012-05-03 15:00:40 -07001468 RegisterClass regClass = oatRegClassBySize(size);
1469 int lenOffset = Array::LengthOffset().Int32Value();
1470 int dataOffset;
1471 RegLocation rlResult;
1472 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1473 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001474
Bill Buzbeea114add2012-05-03 15:00:40 -07001475 if (size == kLong || size == kDouble) {
1476 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1477 } else {
1478 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1479 }
buzbee31a4a6f2012-02-28 15:36:15 -08001480
Bill Buzbeea114add2012-05-03 15:00:40 -07001481 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001482 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001483
Ian Rogersb5d09b22012-03-06 22:14:17 -08001484#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001485 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001486 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1487 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001488 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001489 }
1490 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001491 int regAddr = oatAllocTemp(cUnit);
1492 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1493 oatFreeTemp(cUnit, rlArray.lowReg);
1494 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001495 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001496 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
jeffhao21e12712012-05-25 19:06:18 -07001497 rlResult.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001498 storeValueWide(cUnit, rlDest, rlResult);
1499 } else {
1500 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001501
buzbee408ad162012-06-06 16:45:18 -07001502 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001503 dataOffset, rlResult.lowReg, INVALID_REG, size,
1504 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001505
Bill Buzbeea114add2012-05-03 15:00:40 -07001506 storeValue(cUnit, rlDest, rlResult);
1507 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001508#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001509 int regPtr = oatAllocTemp(cUnit);
buzbee408ad162012-06-06 16:45:18 -07001510 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001511 int regLen = INVALID_REG;
1512 if (needsRangeCheck) {
1513 regLen = oatAllocTemp(cUnit);
1514 /* Get len */
1515 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1516 }
1517 /* regPtr -> array data */
1518 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1519 oatFreeTemp(cUnit, rlArray.lowReg);
1520 if ((size == kLong) || (size == kDouble)) {
1521 if (scale) {
1522 int rNewIndex = oatAllocTemp(cUnit);
1523 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1524 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1525 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001526 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001527 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001528 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001529 oatFreeTemp(cUnit, rlIndex.lowReg);
1530 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1531
1532 if (needsRangeCheck) {
1533 // TODO: change kCondCS to a more meaningful name, is the sense of
1534 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001535 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001536 oatFreeTemp(cUnit, regLen);
1537 }
1538 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1539
1540 oatFreeTemp(cUnit, regPtr);
1541 storeValueWide(cUnit, rlDest, rlResult);
1542 } else {
1543 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1544
1545 if (needsRangeCheck) {
1546 // TODO: change kCondCS to a more meaningful name, is the sense of
1547 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001548 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001549 oatFreeTemp(cUnit, regLen);
1550 }
1551 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1552 scale, size);
1553
1554 oatFreeTemp(cUnit, regPtr);
1555 storeValue(cUnit, rlDest, rlResult);
1556 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001557#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001558}
1559
1560/*
1561 * Generate array store
1562 *
1563 */
buzbee408ad162012-06-06 16:45:18 -07001564void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001565 RegLocation rlArray, RegLocation rlIndex,
1566 RegLocation rlSrc, int scale)
1567{
Bill Buzbeea114add2012-05-03 15:00:40 -07001568 RegisterClass regClass = oatRegClassBySize(size);
1569 int lenOffset = Array::LengthOffset().Int32Value();
1570 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001571
Bill Buzbeea114add2012-05-03 15:00:40 -07001572 if (size == kLong || size == kDouble) {
1573 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1574 } else {
1575 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1576 }
buzbee31a4a6f2012-02-28 15:36:15 -08001577
Bill Buzbeea114add2012-05-03 15:00:40 -07001578 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1579 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001580#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001581 int regPtr;
1582 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1583 oatClobber(cUnit, rlArray.lowReg);
1584 regPtr = rlArray.lowReg;
1585 } else {
1586 regPtr = oatAllocTemp(cUnit);
1587 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1588 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001589#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001590
Bill Buzbeea114add2012-05-03 15:00:40 -07001591 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001592 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001593
Ian Rogersb41b33b2012-03-20 14:22:54 -07001594#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001595 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001596 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1597 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001598 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001599 }
1600 if ((size == kLong) || (size == kDouble)) {
1601 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1602 } else {
1603 rlSrc = loadValue(cUnit, rlSrc, regClass);
1604 }
jeffhao703f2cd2012-07-13 17:25:52 -07001605 // If the src reg can't be byte accessed, move it to a temp first.
1606 if ((size == kSignedByte || size == kUnsignedByte) && rlSrc.lowReg >= 4) {
1607 int temp = oatAllocTemp(cUnit);
1608 opRegCopy(cUnit, temp, rlSrc.lowReg);
1609 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1610 dataOffset, temp, INVALID_REG, size,
1611 INVALID_SREG);
1612 } else {
1613 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
1614 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1615 INVALID_SREG);
1616 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001617#else
buzbee408ad162012-06-06 16:45:18 -07001618 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001619 int regLen = INVALID_REG;
1620 if (needsRangeCheck) {
1621 regLen = oatAllocTemp(cUnit);
1622 //NOTE: max live temps(4) here.
1623 /* Get len */
1624 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1625 }
1626 /* regPtr -> array data */
1627 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1628 /* at this point, regPtr points to array, 2 live temps */
1629 if ((size == kLong) || (size == kDouble)) {
1630 //TUNING: specific wide routine that can handle fp regs
1631 if (scale) {
1632 int rNewIndex = oatAllocTemp(cUnit);
1633 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1634 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1635 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001636 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001637 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001638 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001639 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1640
1641 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001642 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001643 oatFreeTemp(cUnit, regLen);
1644 }
1645
jeffhao41005dd2012-05-09 17:58:52 -07001646 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001647
1648 oatFreeTemp(cUnit, regPtr);
1649 } else {
1650 rlSrc = loadValue(cUnit, rlSrc, regClass);
1651 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001652 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001653 oatFreeTemp(cUnit, regLen);
1654 }
1655 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1656 scale, size);
1657 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001658#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001659}
1660
buzbee408ad162012-06-06 16:45:18 -07001661void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001662 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001663 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001664{
Bill Buzbeea114add2012-05-03 15:00:40 -07001665 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001666#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001667 /*
1668 * NOTE: This is the one place in the code in which we might have
1669 * as many as six live temporary registers. There are 5 in the normal
1670 * set for Arm. Until we have spill capabilities, temporarily add
1671 * lr to the temp set. It is safe to do this locally, but note that
1672 * lr is used explicitly elsewhere in the code generator and cannot
1673 * normally be used as a general temp register.
1674 */
1675 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1676 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001677#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001678 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1679 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1680 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1681 // The longs may overlap - use intermediate temp if so
1682 if (rlResult.lowReg == rlSrc1.highReg) {
1683 int tReg = oatAllocTemp(cUnit);
1684 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1685 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1686 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1687 oatFreeTemp(cUnit, tReg);
1688 } else {
1689 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1690 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1691 rlSrc2.highReg);
1692 }
1693 /*
1694 * NOTE: If rlDest refers to a frame variable in a large frame, the
1695 * following storeValueWide might need to allocate a temp register.
1696 * To further work around the lack of a spill capability, explicitly
1697 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1698 * Remove when spill is functional.
1699 */
1700 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1701 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1702 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001703#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001704 oatClobber(cUnit, rLR);
1705 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001706#endif
1707}
1708
1709
buzbee408ad162012-06-06 16:45:18 -07001710bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001711 RegLocation rlSrc1, RegLocation rlShift)
1712{
Bill Buzbeea114add2012-05-03 15:00:40 -07001713 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001714
buzbee408ad162012-06-06 16:45:18 -07001715 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001716 case Instruction::SHL_LONG:
1717 case Instruction::SHL_LONG_2ADDR:
1718 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1719 break;
1720 case Instruction::SHR_LONG:
1721 case Instruction::SHR_LONG_2ADDR:
1722 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1723 break;
1724 case Instruction::USHR_LONG:
1725 case Instruction::USHR_LONG_2ADDR:
1726 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1727 break;
1728 default:
1729 LOG(FATAL) << "Unexpected case";
1730 return true;
1731 }
1732 oatFlushAllRegs(cUnit); /* Send everything to home location */
1733 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
1734 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1735 storeValueWide(cUnit, rlDest, rlResult);
1736 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001737}
1738
1739
buzbee408ad162012-06-06 16:45:18 -07001740bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001741 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001742{
Bill Buzbeea114add2012-05-03 15:00:40 -07001743 OpKind op = kOpBkpt;
1744 bool callOut = false;
1745 bool checkZero = false;
1746 bool unary = false;
1747 RegLocation rlResult;
1748 bool shiftOp = false;
1749 int funcOffset;
1750 int retReg = rRET0;
buzbee408ad162012-06-06 16:45:18 -07001751 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001752 case Instruction::NEG_INT:
1753 op = kOpNeg;
1754 unary = true;
1755 break;
1756 case Instruction::NOT_INT:
1757 op = kOpMvn;
1758 unary = true;
1759 break;
1760 case Instruction::ADD_INT:
1761 case Instruction::ADD_INT_2ADDR:
1762 op = kOpAdd;
1763 break;
1764 case Instruction::SUB_INT:
1765 case Instruction::SUB_INT_2ADDR:
1766 op = kOpSub;
1767 break;
1768 case Instruction::MUL_INT:
1769 case Instruction::MUL_INT_2ADDR:
1770 op = kOpMul;
1771 break;
1772 case Instruction::DIV_INT:
1773 case Instruction::DIV_INT_2ADDR:
1774 checkZero = true;
1775 op = kOpDiv;
1776 callOut = true;
1777 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1778 retReg = rRET0;
1779 break;
1780 /* NOTE: returns in rARG1 */
1781 case Instruction::REM_INT:
1782 case Instruction::REM_INT_2ADDR:
1783 checkZero = true;
1784 op = kOpRem;
1785 callOut = true;
1786 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1787 retReg = rRET1;
1788 break;
1789 case Instruction::AND_INT:
1790 case Instruction::AND_INT_2ADDR:
1791 op = kOpAnd;
1792 break;
1793 case Instruction::OR_INT:
1794 case Instruction::OR_INT_2ADDR:
1795 op = kOpOr;
1796 break;
1797 case Instruction::XOR_INT:
1798 case Instruction::XOR_INT_2ADDR:
1799 op = kOpXor;
1800 break;
1801 case Instruction::SHL_INT:
1802 case Instruction::SHL_INT_2ADDR:
1803 shiftOp = true;
1804 op = kOpLsl;
1805 break;
1806 case Instruction::SHR_INT:
1807 case Instruction::SHR_INT_2ADDR:
1808 shiftOp = true;
1809 op = kOpAsr;
1810 break;
1811 case Instruction::USHR_INT:
1812 case Instruction::USHR_INT_2ADDR:
1813 shiftOp = true;
1814 op = kOpLsr;
1815 break;
1816 default:
1817 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001818 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001819 }
1820 if (!callOut) {
1821 if (unary) {
1822 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1823 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1824 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001825 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001826 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001827#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001828 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1829 int tReg = oatAllocTemp(cUnit);
1830 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001831#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001832 // X86 doesn't require masking and must use ECX
1833 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1834 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001835#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001836 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1837 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1838 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1839 oatFreeTemp(cUnit, tReg);
1840 } else {
1841 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1842 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1843 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1844 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1845 }
buzbee31a4a6f2012-02-28 15:36:15 -08001846 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001847 storeValue(cUnit, rlDest, rlResult);
1848 } else {
1849 RegLocation rlResult;
1850 oatFlushAllRegs(cUnit); /* Send everything to home location */
1851 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1852#if !defined(TARGET_X86)
1853 int rTgt = loadHelper(cUnit, funcOffset);
1854#endif
1855 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1856 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001857 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001858 }
1859#if !defined(TARGET_X86)
1860 opReg(cUnit, kOpBlx, rTgt);
1861 oatFreeTemp(cUnit, rTgt);
1862#else
1863 opThreadMem(cUnit, kOpBlx, funcOffset);
1864#endif
1865 if (retReg == rRET0)
1866 rlResult = oatGetReturn(cUnit, false);
1867 else
1868 rlResult = oatGetReturnAlt(cUnit);
1869 storeValue(cUnit, rlDest, rlResult);
1870 }
1871 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001872}
1873
1874/*
1875 * The following are the first-level codegen routines that analyze the format
1876 * of each bytecode then either dispatch special purpose codegen routines
1877 * or produce corresponding Thumb instructions directly.
1878 */
1879
1880bool isPowerOfTwo(int x)
1881{
Bill Buzbeea114add2012-05-03 15:00:40 -07001882 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001883}
1884
1885// Returns true if no more than two bits are set in 'x'.
1886bool isPopCountLE2(unsigned int x)
1887{
Bill Buzbeea114add2012-05-03 15:00:40 -07001888 x &= x - 1;
1889 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001890}
1891
1892// Returns the index of the lowest set bit in 'x'.
1893int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001894 int bit_posn = 0;
1895 while ((x & 0xf) == 0) {
1896 bit_posn += 4;
1897 x >>= 4;
1898 }
1899 while ((x & 1) == 0) {
1900 bit_posn++;
1901 x >>= 1;
1902 }
1903 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001904}
1905
1906// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1907// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001908bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001909 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001910{
buzbeef3aac972012-04-11 16:33:36 -07001911#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001912 // No divide instruction for Arm, so check for more special cases
1913 if (lit < 2) {
1914 return false;
1915 }
1916 if (!isPowerOfTwo(lit)) {
1917 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1918 }
buzbeef3aac972012-04-11 16:33:36 -07001919#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001920 if (lit < 2 || !isPowerOfTwo(lit)) {
1921 return false;
1922 }
buzbeef3aac972012-04-11 16:33:36 -07001923#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001924 int k = lowestSetBit(lit);
1925 if (k >= 30) {
1926 // Avoid special cases.
1927 return false;
1928 }
1929 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1930 dalvikOpcode == Instruction::DIV_INT_LIT16);
1931 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1932 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1933 if (div) {
1934 int tReg = oatAllocTemp(cUnit);
1935 if (lit == 2) {
1936 // Division by 2 is by far the most common division by constant.
1937 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1938 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1939 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001940 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001941 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1942 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1943 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1944 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001945 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001946 } else {
1947 int tReg1 = oatAllocTemp(cUnit);
1948 int tReg2 = oatAllocTemp(cUnit);
1949 if (lit == 2) {
1950 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1951 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1952 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1953 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1954 } else {
1955 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1956 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1957 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1958 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1959 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1960 }
1961 }
1962 storeValue(cUnit, rlDest, rlResult);
1963 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001964}
1965
1966void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1967 RegLocation rlResult, int lit,
1968 int firstBit, int secondBit)
1969{
buzbee0398c422012-03-02 15:22:47 -08001970#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001971 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1972 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001973#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001974 int tReg = oatAllocTemp(cUnit);
1975 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1976 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1977 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001978#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001979 if (firstBit != 0) {
1980 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1981 }
buzbee31a4a6f2012-02-28 15:36:15 -08001982}
1983
1984// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1985// and store the result in 'rlDest'.
1986bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1987 RegLocation rlDest, int lit)
1988{
Bill Buzbeea114add2012-05-03 15:00:40 -07001989 // Can we simplify this multiplication?
1990 bool powerOfTwo = false;
1991 bool popCountLE2 = false;
1992 bool powerOfTwoMinusOne = false;
1993 if (lit < 2) {
1994 // Avoid special cases.
1995 return false;
1996 } else if (isPowerOfTwo(lit)) {
1997 powerOfTwo = true;
1998 } else if (isPopCountLE2(lit)) {
1999 popCountLE2 = true;
2000 } else if (isPowerOfTwo(lit + 1)) {
2001 powerOfTwoMinusOne = true;
2002 } else {
2003 return false;
2004 }
2005 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2006 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2007 if (powerOfTwo) {
2008 // Shift.
2009 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
2010 lowestSetBit(lit));
2011 } else if (popCountLE2) {
2012 // Shift and add and shift.
2013 int firstBit = lowestSetBit(lit);
2014 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2015 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2016 firstBit, secondBit);
2017 } else {
2018 // Reverse subtract: (src << (shift + 1)) - src.
2019 DCHECK(powerOfTwoMinusOne);
2020 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2021 int tReg = oatAllocTemp(cUnit);
2022 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2023 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2024 }
2025 storeValue(cUnit, rlDest, rlResult);
2026 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002027}
2028
buzbee408ad162012-06-06 16:45:18 -07002029bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2030 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002031{
Bill Buzbeea114add2012-05-03 15:00:40 -07002032 RegLocation rlResult;
2033 OpKind op = (OpKind)0; /* Make gcc happy */
2034 int shiftOp = false;
2035 bool isDiv = false;
2036 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002037
buzbee408ad162012-06-06 16:45:18 -07002038 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002039 case Instruction::RSUB_INT_LIT8:
2040 case Instruction::RSUB_INT: {
2041 int tReg;
2042 //TUNING: add support for use of Arm rsub op
2043 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2044 tReg = oatAllocTemp(cUnit);
2045 loadConstant(cUnit, tReg, lit);
2046 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2047 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2048 storeValue(cUnit, rlDest, rlResult);
2049 return false;
2050 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002051 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002052
2053 case Instruction::ADD_INT_LIT8:
2054 case Instruction::ADD_INT_LIT16:
2055 op = kOpAdd;
2056 break;
2057 case Instruction::MUL_INT_LIT8:
2058 case Instruction::MUL_INT_LIT16: {
2059 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2060 return false;
2061 }
2062 op = kOpMul;
2063 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002064 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002065 case Instruction::AND_INT_LIT8:
2066 case Instruction::AND_INT_LIT16:
2067 op = kOpAnd;
2068 break;
2069 case Instruction::OR_INT_LIT8:
2070 case Instruction::OR_INT_LIT16:
2071 op = kOpOr;
2072 break;
2073 case Instruction::XOR_INT_LIT8:
2074 case Instruction::XOR_INT_LIT16:
2075 op = kOpXor;
2076 break;
2077 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002078 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002079 lit &= 31;
2080 shiftOp = true;
2081 op = kOpLsl;
2082 break;
2083 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002084 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002085 lit &= 31;
2086 shiftOp = true;
2087 op = kOpAsr;
2088 break;
2089 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002090 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002091 lit &= 31;
2092 shiftOp = true;
2093 op = kOpLsr;
2094 break;
2095
2096 case Instruction::DIV_INT_LIT8:
2097 case Instruction::DIV_INT_LIT16:
2098 case Instruction::REM_INT_LIT8:
2099 case Instruction::REM_INT_LIT16:
2100 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002101 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002102 return false;
2103 }
buzbee408ad162012-06-06 16:45:18 -07002104 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002105 return false;
2106 }
2107 oatFlushAllRegs(cUnit); /* Everything to home location */
2108 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2109 oatClobber(cUnit, rARG0);
2110 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee408ad162012-06-06 16:45:18 -07002111 if ((opcode == Instruction::DIV_INT_LIT8) ||
2112 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002113 isDiv = true;
2114 } else {
2115 isDiv = false;
2116 }
2117 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
2118 if (isDiv)
2119 rlResult = oatGetReturn(cUnit, false);
2120 else
2121 rlResult = oatGetReturnAlt(cUnit);
2122 storeValue(cUnit, rlDest, rlResult);
2123 return false;
2124 break;
2125 default:
2126 return true;
2127 }
2128 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2129 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2130 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2131 if (shiftOp && (lit == 0)) {
2132 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2133 } else {
2134 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2135 }
2136 storeValue(cUnit, rlDest, rlResult);
2137 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002138}
2139
buzbee408ad162012-06-06 16:45:18 -07002140bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002141 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002142{
Bill Buzbeea114add2012-05-03 15:00:40 -07002143 RegLocation rlResult;
2144 OpKind firstOp = kOpBkpt;
2145 OpKind secondOp = kOpBkpt;
2146 bool callOut = false;
2147 bool checkZero = false;
2148 int funcOffset;
2149 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002150
buzbee408ad162012-06-06 16:45:18 -07002151 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002152 case Instruction::NOT_LONG:
2153 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2154 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2155 // Check for destructive overlap
2156 if (rlResult.lowReg == rlSrc2.highReg) {
2157 int tReg = oatAllocTemp(cUnit);
2158 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2159 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2160 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2161 oatFreeTemp(cUnit, tReg);
2162 } else {
2163 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2164 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2165 }
2166 storeValueWide(cUnit, rlDest, rlResult);
2167 return false;
2168 break;
2169 case Instruction::ADD_LONG:
2170 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002171#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002172 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002173#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002174 firstOp = kOpAdd;
2175 secondOp = kOpAdc;
2176 break;
buzbeec5159d52012-03-03 11:48:39 -08002177#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002178 case Instruction::SUB_LONG:
2179 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002180#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002181 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002182#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002183 firstOp = kOpSub;
2184 secondOp = kOpSbc;
2185 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002186#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002187 case Instruction::MUL_LONG:
2188 case Instruction::MUL_LONG_2ADDR:
2189 callOut = true;
2190 retReg = rRET0;
2191 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2192 break;
2193 case Instruction::DIV_LONG:
2194 case Instruction::DIV_LONG_2ADDR:
2195 callOut = true;
2196 checkZero = true;
2197 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002198 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002199 break;
2200 case Instruction::REM_LONG:
2201 case Instruction::REM_LONG_2ADDR:
2202 callOut = true;
2203 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002204 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002205#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002206 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2207 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002208#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002209 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002210#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002211 break;
2212 case Instruction::AND_LONG_2ADDR:
2213 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002214#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002215 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002216#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002217 firstOp = kOpAnd;
2218 secondOp = kOpAnd;
2219 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002220#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002221 case Instruction::OR_LONG:
2222 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002223#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002224 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002225#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002226 firstOp = kOpOr;
2227 secondOp = kOpOr;
2228 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002229#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002230 case Instruction::XOR_LONG:
2231 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002232#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002233 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002234#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002235 firstOp = kOpXor;
2236 secondOp = kOpXor;
2237 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002238#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002239 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002240 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002241 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002242 default:
2243 LOG(FATAL) << "Invalid long arith op";
2244 }
2245 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002246 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002247 } else {
2248 oatFlushAllRegs(cUnit); /* Send everything to home location */
2249 if (checkZero) {
2250 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2251#if !defined(TARGET_X86)
2252 int rTgt = loadHelper(cUnit, funcOffset);
2253#endif
2254 int tReg = oatAllocTemp(cUnit);
2255#if defined(TARGET_ARM)
2256 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2257 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002258 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002259#else
2260 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2261#endif
buzbee408ad162012-06-06 16:45:18 -07002262 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002263 oatFreeTemp(cUnit, tReg);
2264 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2265#if !defined(TARGET_X86)
2266 opReg(cUnit, kOpBlx, rTgt);
2267 oatFreeTemp(cUnit, rTgt);
2268#else
2269 opThreadMem(cUnit, kOpBlx, funcOffset);
2270#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002271 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002272 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2273 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002274 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002275 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2276 if (retReg == rRET0)
2277 rlResult = oatGetReturnWide(cUnit, false);
2278 else
2279 rlResult = oatGetReturnWideAlt(cUnit);
2280 storeValueWide(cUnit, rlDest, rlResult);
2281 }
2282 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002283}
2284
buzbee408ad162012-06-06 16:45:18 -07002285bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2286 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002287{
Bill Buzbeea114add2012-05-03 15:00:40 -07002288 /*
2289 * Don't optimize the register usage since it calls out to support
2290 * functions
2291 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002292 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002293 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002294 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002295 } else {
2296 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002297 }
2298 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002299 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002300 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002301 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2302 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002303 } else {
2304 RegLocation rlResult;
2305 rlResult = oatGetReturn(cUnit, rlDest.fp);
2306 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002307 }
2308 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002309}
2310
2311void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002312bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002313 RegLocation rlDest, RegLocation rlSrc1,
2314 RegLocation rlSrc2)
2315{
Bill Buzbeea114add2012-05-03 15:00:40 -07002316 RegLocation rlResult;
2317 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002318
buzbee408ad162012-06-06 16:45:18 -07002319 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002320 case Instruction::ADD_FLOAT_2ADDR:
2321 case Instruction::ADD_FLOAT:
2322 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2323 break;
2324 case Instruction::SUB_FLOAT_2ADDR:
2325 case Instruction::SUB_FLOAT:
2326 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2327 break;
2328 case Instruction::DIV_FLOAT_2ADDR:
2329 case Instruction::DIV_FLOAT:
2330 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2331 break;
2332 case Instruction::MUL_FLOAT_2ADDR:
2333 case Instruction::MUL_FLOAT:
2334 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2335 break;
2336 case Instruction::REM_FLOAT_2ADDR:
2337 case Instruction::REM_FLOAT:
2338 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2339 break;
2340 case Instruction::NEG_FLOAT: {
2341 genNegFloat(cUnit, rlDest, rlSrc1);
2342 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002343 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002344 default:
2345 return true;
2346 }
2347 oatFlushAllRegs(cUnit); /* Send everything to home location */
2348 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2349 rlResult = oatGetReturn(cUnit, true);
2350 storeValue(cUnit, rlDest, rlResult);
2351 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002352}
2353
2354void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002355bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002356 RegLocation rlDest, RegLocation rlSrc1,
2357 RegLocation rlSrc2)
2358{
Bill Buzbeea114add2012-05-03 15:00:40 -07002359 RegLocation rlResult;
2360 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002361
buzbee408ad162012-06-06 16:45:18 -07002362 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002363 case Instruction::ADD_DOUBLE_2ADDR:
2364 case Instruction::ADD_DOUBLE:
2365 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2366 break;
2367 case Instruction::SUB_DOUBLE_2ADDR:
2368 case Instruction::SUB_DOUBLE:
2369 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2370 break;
2371 case Instruction::DIV_DOUBLE_2ADDR:
2372 case Instruction::DIV_DOUBLE:
2373 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2374 break;
2375 case Instruction::MUL_DOUBLE_2ADDR:
2376 case Instruction::MUL_DOUBLE:
2377 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2378 break;
2379 case Instruction::REM_DOUBLE_2ADDR:
2380 case Instruction::REM_DOUBLE:
2381 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2382 break;
2383 case Instruction::NEG_DOUBLE: {
2384 genNegDouble(cUnit, rlDest, rlSrc1);
2385 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002386 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002387 default:
2388 return true;
2389 }
2390 oatFlushAllRegs(cUnit); /* Send everything to home location */
2391 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2392 rlResult = oatGetReturnWide(cUnit, true);
2393 storeValueWide(cUnit, rlDest, rlResult);
2394 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002395}
2396
buzbee408ad162012-06-06 16:45:18 -07002397bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2398 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002399{
buzbee31a4a6f2012-02-28 15:36:15 -08002400
Bill Buzbeea114add2012-05-03 15:00:40 -07002401 switch (opcode) {
2402 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002403 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2404 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002405 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002406 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2407 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002408 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002409 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2410 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002411 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002412 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2413 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002414 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002415 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2416 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002417 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002418 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2419 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002420 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002421 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2422 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002423 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002424 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2425 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002426 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002427 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2428 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002429 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002430 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2431 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002432 default:
2433 return true;
2434 }
2435 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002436}
2437
2438/*
2439 * Generate callout to updateDebugger. Note that we're overloading
2440 * the use of rSUSPEND here. When the debugger is active, this
2441 * register holds the address of the update function. So, if it's
2442 * non-null, we call out to it.
2443 *
2444 * Note also that rRET0 and rRET1 must be preserved across this
2445 * code. This must be handled by the stub.
2446 */
2447void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2448{
Bill Buzbeea114add2012-05-03 15:00:40 -07002449 // Following DCHECK verifies that dPC is in range of single load immediate
2450 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2451 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2452 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002453#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002454 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2455 opIT(cUnit, kArmCondNe, "T");
2456 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2457 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002458#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002459 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002460#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002461 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2462 loadConstant(cUnit, rARG2, offset);
2463 opReg(cUnit, kOpBlx, rSUSPEND);
2464 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2465 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002466#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002467 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002468}
2469
2470/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002471void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002472{
buzbee408ad162012-06-06 16:45:18 -07002473 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002474 return;
2475 }
2476 oatFlushAllRegs(cUnit);
2477 if (cUnit->genDebugger) {
2478 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002479#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002480 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002481#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002482 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
2483 opReg(cUnit, kOpBlx, rTgt);
2484 // Refresh rSUSPEND
2485 loadWordDisp(cUnit, rSELF,
2486 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2487 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002488#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002489 } else {
2490 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002491#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002492 // In non-debug case, only check periodically
2493 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2494 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002495#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002496 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2497 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002498#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002499 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2500 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002501#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002502 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2503 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002504 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002505 branch->target = (LIR*)target;
2506 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2507 }
buzbee31a4a6f2012-02-28 15:36:15 -08002508}
2509
buzbeefead2932012-03-30 14:02:01 -07002510/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002511void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002512{
buzbee408ad162012-06-06 16:45:18 -07002513 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002514 opUnconditionalBranch(cUnit, target);
2515 return;
2516 }
2517 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002518 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002519 opUnconditionalBranch(cUnit, target);
2520 } else {
buzbeefead2932012-03-30 14:02:01 -07002521#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002522 // In non-debug case, only check periodically
2523 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2524 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002525#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002526 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2527 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002528#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002529 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2530 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002531#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002532 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002533 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002534 oatFlushAllRegs(cUnit);
2535 opUnconditionalBranch(cUnit, launchPad);
2536 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2537 (intptr_t)launchPad);
2538 }
buzbeefead2932012-03-30 14:02:01 -07002539}
2540
buzbee31a4a6f2012-02-28 15:36:15 -08002541} // namespace art