blob: b4b0f6a9e0dca3bd4598fd1bdcdbe05234b33245 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers57b86d42012-03-27 16:05:41 -070017#include "oat/runtime/oat_support_entrypoints.h"
18
buzbee31a4a6f2012-02-28 15:36:15 -080019namespace art {
20
21/*
22 * This source files contains "gen" codegen routines that should
23 * be applicable to most targets. Only mid-level support utilities
24 * and "op" calls may be used here.
25 */
buzbee3b3dbdd2012-06-13 13:39:34 -070026void genInvoke(CompilationUnit* cUnit, CallInfo* info);
buzbee31a4a6f2012-02-28 15:36:15 -080027#if defined(TARGET_ARM)
buzbee82488f52012-03-02 08:20:26 -080028LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide);
buzbeef3aac972012-04-11 16:33:36 -070029bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
30 RegLocation rlSrc, RegLocation rlDest, int lit);
buzbee31a4a6f2012-02-28 15:36:15 -080031#endif
32
Ian Rogersab2b55d2012-03-18 00:06:11 -070033void callRuntimeHelperImm(CompilationUnit* cUnit, int helperOffset, int arg0) {
34#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070035 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070036#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070037 loadConstant(cUnit, rARG0, arg0);
38 oatClobberCalleeSave(cUnit);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070039#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070040 opReg(cUnit, kOpBlx, rTgt);
41 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070042#else
Bill Buzbeea114add2012-05-03 15:00:40 -070043 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070044#endif
45}
46
Ian Rogers7caad772012-03-30 01:07:54 -070047void callRuntimeHelperReg(CompilationUnit* cUnit, int helperOffset, int arg0) {
48#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070049 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070050#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070051 opRegCopy(cUnit, rARG0, arg0);
52 oatClobberCalleeSave(cUnit);
Ian Rogers7caad772012-03-30 01:07:54 -070053#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070054 opReg(cUnit, kOpBlx, rTgt);
55 oatFreeTemp(cUnit, rTgt);
Ian Rogers7caad772012-03-30 01:07:54 -070056#else
Bill Buzbeea114add2012-05-03 15:00:40 -070057 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers7caad772012-03-30 01:07:54 -070058#endif
59}
60
Ian Rogersab2b55d2012-03-18 00:06:11 -070061void callRuntimeHelperRegLocation(CompilationUnit* cUnit, int helperOffset,
62 RegLocation arg0) {
63#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070064 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070065#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070066 if (arg0.wide == 0) {
67 loadValueDirectFixed(cUnit, arg0, rARG0);
68 } else {
69 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
70 }
71 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -070072#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070073 opReg(cUnit, kOpBlx, rTgt);
74 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -070075#else
Bill Buzbeea114add2012-05-03 15:00:40 -070076 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070077#endif
78}
79
80void callRuntimeHelperImmImm(CompilationUnit* cUnit, int helperOffset,
81 int arg0, int arg1) {
82#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070083 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070084#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070085 loadConstant(cUnit, rARG0, arg0);
86 loadConstant(cUnit, rARG1, arg1);
87 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -070088#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070089 opReg(cUnit, kOpBlx, rTgt);
90 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -070091#else
Bill Buzbeea114add2012-05-03 15:00:40 -070092 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -070093#endif
94}
95
96void callRuntimeHelperImmRegLocation(CompilationUnit* cUnit, int helperOffset,
97 int arg0, RegLocation arg1) {
98#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -070099 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700100#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700101 if (arg1.wide == 0) {
102 loadValueDirectFixed(cUnit, arg1, rARG1);
103 } else {
104 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
105 }
106 loadConstant(cUnit, rARG0, arg0);
107 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700108#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 opReg(cUnit, kOpBlx, rTgt);
110 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700111#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700113#endif
114}
115
116void callRuntimeHelperRegLocationImm(CompilationUnit* cUnit, int helperOffset,
117 RegLocation arg0, int arg1) {
118#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700120#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700121 loadValueDirectFixed(cUnit, arg0, rARG0);
122 loadConstant(cUnit, rARG1, arg1);
123 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700124#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 opReg(cUnit, kOpBlx, rTgt);
126 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700127#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700129#endif
130}
131
132void callRuntimeHelperImmReg(CompilationUnit* cUnit, int helperOffset,
133 int arg0, int arg1) {
134#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700135 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700136#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 opRegCopy(cUnit, rARG1, arg1);
138 loadConstant(cUnit, rARG0, arg0);
139 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700140#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 opReg(cUnit, kOpBlx, rTgt);
142 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700143#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700145#endif
146}
147
148void callRuntimeHelperRegImm(CompilationUnit* cUnit, int helperOffset,
149 int arg0, int arg1) {
150#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700152#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 opRegCopy(cUnit, rARG0, arg0);
154 loadConstant(cUnit, rARG1, arg1);
155 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700156#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 opReg(cUnit, kOpBlx, rTgt);
158 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700159#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700161#endif
162}
163
164void callRuntimeHelperImmMethod(CompilationUnit* cUnit, int helperOffset,
165 int arg0) {
166#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700168#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 loadCurrMethodDirect(cUnit, rARG1);
170 loadConstant(cUnit, rARG0, arg0);
171 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700172#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 opReg(cUnit, kOpBlx, rTgt);
174 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700175#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700177#endif
178}
179
180void callRuntimeHelperRegLocationRegLocation(CompilationUnit* cUnit,
181 int helperOffset,
182 RegLocation arg0,
183 RegLocation arg1) {
184#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700186#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 if (arg0.wide == 0) {
188 loadValueDirectFixed(cUnit, arg0, rARG0);
189 if (arg1.wide == 0) {
190 loadValueDirectFixed(cUnit, arg1, rARG1);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700191 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 loadValueDirectWideFixed(cUnit, arg1, rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700193 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 } else {
195 loadValueDirectWideFixed(cUnit, arg0, rARG0, rARG1);
196 if (arg1.wide == 0) {
197 loadValueDirectFixed(cUnit, arg1, rARG2);
198 } else {
199 loadValueDirectWideFixed(cUnit, arg1, rARG2, rARG3);
200 }
201 }
202 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700203#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 opReg(cUnit, kOpBlx, rTgt);
205 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700206#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700208#endif
209}
210
211void callRuntimeHelperRegReg(CompilationUnit* cUnit, int helperOffset,
212 int arg0, int arg1) {
213#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700215#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
217 opRegCopy(cUnit, rARG0, arg0);
218 opRegCopy(cUnit, rARG1, arg1);
219 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700220#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700221 opReg(cUnit, kOpBlx, rTgt);
222 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700223#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700225#endif
226}
227
228void callRuntimeHelperRegRegImm(CompilationUnit* cUnit, int helperOffset,
229 int arg0, int arg1, int arg2) {
230#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700232#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 DCHECK_NE((int)rARG0, arg1); // check copy into arg0 won't clobber arg1
234 opRegCopy(cUnit, rARG0, arg0);
235 opRegCopy(cUnit, rARG1, arg1);
236 loadConstant(cUnit, rARG2, arg2);
237 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700238#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 opReg(cUnit, kOpBlx, rTgt);
240 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700241#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700243#endif
244}
245
Bill Buzbeea114add2012-05-03 15:00:40 -0700246void callRuntimeHelperImmMethodRegLocation(CompilationUnit* cUnit,
247 int helperOffset,
248 int arg0, RegLocation arg2) {
Ian Rogersab2b55d2012-03-18 00:06:11 -0700249#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700251#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 loadValueDirectFixed(cUnit, arg2, rARG2);
253 loadCurrMethodDirect(cUnit, rARG1);
254 loadConstant(cUnit, rARG0, arg0);
255 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700256#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 opReg(cUnit, kOpBlx, rTgt);
258 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700259#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700261#endif
262}
263
264void callRuntimeHelperImmMethodImm(CompilationUnit* cUnit, int helperOffset,
265 int arg0, int arg2) {
266#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700268#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 loadCurrMethodDirect(cUnit, rARG1);
270 loadConstant(cUnit, rARG2, arg2);
271 loadConstant(cUnit, rARG0, arg0);
272 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700273#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 opReg(cUnit, kOpBlx, rTgt);
275 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700276#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700278#endif
279}
280
281void callRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cUnit,
282 int helperOffset,
283 int arg0, RegLocation arg1,
284 RegLocation arg2) {
285#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 int rTgt = loadHelper(cUnit, helperOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700287#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 loadValueDirectFixed(cUnit, arg1, rARG1);
289 if (arg2.wide == 0) {
290 loadValueDirectFixed(cUnit, arg2, rARG2);
291 } else {
292 loadValueDirectWideFixed(cUnit, arg2, rARG2, rARG3);
293 }
294 loadConstant(cUnit, rARG0, arg0);
295 oatClobberCalleeSave(cUnit);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700296#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 opReg(cUnit, kOpBlx, rTgt);
298 oatFreeTemp(cUnit, rTgt);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700299#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 opThreadMem(cUnit, kOpBlx, helperOffset);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700301#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800302}
303
304/*
305 * Generate an kPseudoBarrier marker to indicate the boundary of special
306 * blocks.
307 */
308void genBarrier(CompilationUnit* cUnit)
309{
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 LIR* barrier = newLIR0(cUnit, kPseudoBarrier);
311 /* Mark all resources as being clobbered */
312 barrier->defMask = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800313}
314
buzbee31a4a6f2012-02-28 15:36:15 -0800315
316/* Generate unconditional branch instructions */
buzbee82488f52012-03-02 08:20:26 -0800317LIR* opUnconditionalBranch(CompilationUnit* cUnit, LIR* target)
buzbee31a4a6f2012-02-28 15:36:15 -0800318{
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 LIR* branch = opBranchUnconditional(cUnit, kOpUncondBr);
320 branch->target = (LIR*) target;
321 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800322}
323
buzbee5de34942012-03-01 14:51:57 -0800324// FIXME: need to do some work to split out targets with
325// condition codes and those without
326#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -0700327LIR* genCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee31a4a6f2012-02-28 15:36:15 -0800328 ThrowKind kind)
329{
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700331 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 LIR* branch = opCondBranch(cUnit, cCode, tgt);
333 // Remember branch target - will process later
334 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
335 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800336}
buzbee5de34942012-03-01 14:51:57 -0800337#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800338
339LIR* genImmedCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700340 int reg, int immVal, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800341{
buzbee408ad162012-06-06 16:45:18 -0700342 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
343 cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 LIR* branch;
345 if (cCode == kCondAl) {
346 branch = opUnconditionalBranch(cUnit, tgt);
347 } else {
348 branch = opCmpImmBranch(cUnit, cCode, reg, immVal, tgt);
349 }
350 // Remember branch target - will process later
351 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
352 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800353}
354
355/* Perform null-check on a register. */
buzbee408ad162012-06-06 16:45:18 -0700356LIR* genNullCheck(CompilationUnit* cUnit, int sReg, int mReg, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -0800357{
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 if (!(cUnit->disableOpt & (1 << kNullCheckElimination)) &&
buzbee408ad162012-06-06 16:45:18 -0700359 optFlags & MIR_IGNORE_NULL_CHECK) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 return NULL;
361 }
buzbee408ad162012-06-06 16:45:18 -0700362 return genImmedCheck(cUnit, kCondEq, mReg, 0, kThrowNullPointer);
buzbee31a4a6f2012-02-28 15:36:15 -0800363}
364
365/* Perform check on two registers */
366LIR* genRegRegCheck(CompilationUnit* cUnit, ConditionCode cCode,
buzbee408ad162012-06-06 16:45:18 -0700367 int reg1, int reg2, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -0800368{
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kind,
buzbee408ad162012-06-06 16:45:18 -0700370 cUnit->currentDalvikOffset, reg1, reg2);
buzbee5de34942012-03-01 14:51:57 -0800371#if defined(TARGET_MIPS)
Bill Buzbeea114add2012-05-03 15:00:40 -0700372 LIR* branch = opCmpBranch(cUnit, cCode, reg1, reg2, tgt);
buzbee5de34942012-03-01 14:51:57 -0800373#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 opRegReg(cUnit, kOpCmp, reg1, reg2);
375 LIR* branch = opCondBranch(cUnit, cCode, tgt);
buzbee5de34942012-03-01 14:51:57 -0800376#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 // Remember branch target - will process later
378 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
379 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -0800380}
381
buzbee3b3dbdd2012-06-13 13:39:34 -0700382void genCompareAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
383 RegLocation rlSrc1, RegLocation rlSrc2, LIR* taken,
384 LIR* fallThrough)
buzbee31a4a6f2012-02-28 15:36:15 -0800385{
Bill Buzbeea114add2012-05-03 15:00:40 -0700386 ConditionCode cond;
387 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
388 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 switch (opcode) {
390 case Instruction::IF_EQ:
391 cond = kCondEq;
392 break;
393 case Instruction::IF_NE:
394 cond = kCondNe;
395 break;
396 case Instruction::IF_LT:
397 cond = kCondLt;
398 break;
399 case Instruction::IF_GE:
400 cond = kCondGe;
401 break;
402 case Instruction::IF_GT:
403 cond = kCondGt;
404 break;
405 case Instruction::IF_LE:
406 cond = kCondLe;
407 break;
408 default:
409 cond = (ConditionCode)0;
410 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
411 }
buzbee5de34942012-03-01 14:51:57 -0800412#if defined(TARGET_MIPS)
buzbee3b3dbdd2012-06-13 13:39:34 -0700413 opCmpBranch(cUnit, cond, rlSrc1.lowReg, rlSrc2.lowReg, taken);
buzbee5de34942012-03-01 14:51:57 -0800414#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee3b3dbdd2012-06-13 13:39:34 -0700416 opCondBranch(cUnit, cond, taken);
buzbee5de34942012-03-01 14:51:57 -0800417#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700418 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800419}
420
buzbee3b3dbdd2012-06-13 13:39:34 -0700421void genCompareZeroAndBranch(CompilationUnit* cUnit, Instruction::Code opcode,
422 RegLocation rlSrc, LIR* taken, LIR* fallThrough)
buzbee31a4a6f2012-02-28 15:36:15 -0800423{
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 ConditionCode cond;
425 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 switch (opcode) {
427 case Instruction::IF_EQZ:
428 cond = kCondEq;
429 break;
430 case Instruction::IF_NEZ:
431 cond = kCondNe;
432 break;
433 case Instruction::IF_LTZ:
434 cond = kCondLt;
435 break;
436 case Instruction::IF_GEZ:
437 cond = kCondGe;
438 break;
439 case Instruction::IF_GTZ:
440 cond = kCondGt;
441 break;
442 case Instruction::IF_LEZ:
443 cond = kCondLe;
444 break;
445 default:
446 cond = (ConditionCode)0;
447 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
448 }
Ian Rogers7caad772012-03-30 01:07:54 -0700449#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee3b3dbdd2012-06-13 13:39:34 -0700450 opCmpImmBranch(cUnit, cond, rlSrc.lowReg, 0, taken);
buzbee5de34942012-03-01 14:51:57 -0800451#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
buzbee3b3dbdd2012-06-13 13:39:34 -0700453 opCondBranch(cUnit, cond, taken);
buzbee5de34942012-03-01 14:51:57 -0800454#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700455 opUnconditionalBranch(cUnit, fallThrough);
buzbee31a4a6f2012-02-28 15:36:15 -0800456}
457
buzbee408ad162012-06-06 16:45:18 -0700458void genIntToLong(CompilationUnit* cUnit, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800459 RegLocation rlSrc)
460{
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
462 if (rlSrc.location == kLocPhysReg) {
463 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
464 } else {
465 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
466 }
467 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
468 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800469}
470
buzbee408ad162012-06-06 16:45:18 -0700471void genIntNarrowing(CompilationUnit* cUnit, Instruction::Code opcode,
472 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800473{
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
475 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
476 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700477 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 case Instruction::INT_TO_BYTE:
479 op = kOp2Byte;
480 break;
481 case Instruction::INT_TO_SHORT:
482 op = kOp2Short;
483 break;
484 case Instruction::INT_TO_CHAR:
485 op = kOp2Char;
486 break;
487 default:
488 LOG(ERROR) << "Bad int conversion type";
489 }
490 opRegReg(cUnit, op, rlResult.lowReg, rlSrc.lowReg);
491 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800492}
493
494/*
495 * Let helper function take care of everything. Will call
496 * Array::AllocFromCode(type_idx, method, count);
497 * Note: AllocFromCode will handle checks for errNegativeArraySize.
498 */
buzbee408ad162012-06-06 16:45:18 -0700499void genNewArray(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -0800500 RegLocation rlSrc)
501{
Bill Buzbeea114add2012-05-03 15:00:40 -0700502 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 int funcOffset;
504 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
505 cUnit->dex_cache,
506 *cUnit->dex_file,
507 type_idx)) {
508 funcOffset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
509 } else {
510 funcOffset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
511 }
512 callRuntimeHelperImmMethodRegLocation(cUnit, funcOffset, type_idx, rlSrc);
513 RegLocation rlResult = oatGetReturn(cUnit, false);
514 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -0800515}
516
517/*
518 * Similar to genNewArray, but with post-allocation initialization.
519 * Verifier guarantees we're dealing with an array class. Current
520 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
521 * Current code also throws internal unimp if not 'L', '[' or 'I'.
522 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700523void genFilledNewArray(CompilationUnit* cUnit, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800524{
buzbee3b3dbdd2012-06-13 13:39:34 -0700525 int elems = info->numArgWords;
526 int typeIdx = info->index;
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 oatFlushAllRegs(cUnit); /* Everything to home location */
528 int funcOffset;
529 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
530 cUnit->dex_cache,
531 *cUnit->dex_file,
532 typeIdx)) {
533 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
534 } else {
535 funcOffset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
536 }
537 callRuntimeHelperImmMethodImm(cUnit, funcOffset, typeIdx, elems);
538 oatFreeTemp(cUnit, rARG2);
539 oatFreeTemp(cUnit, rARG1);
540 /*
541 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
542 * return region. Because AllocFromCode placed the new array
543 * in rRET0, we'll just lock it into place. When debugger support is
544 * added, it may be necessary to additionally copy all return
545 * values to a home location in thread-local storage
546 */
547 oatLockTemp(cUnit, rRET0);
548
549 // TODO: use the correct component size, currently all supported types
550 // share array alignment with ints (see comment at head of function)
551 size_t component_size = sizeof(int32_t);
552
553 // Having a range of 0 is legal
buzbee3b3dbdd2012-06-13 13:39:34 -0700554 if (info->isRange && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800555 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 * Bit of ugliness here. We're going generate a mem copy loop
557 * on the register range, but it is possible that some regs
558 * in the range have been promoted. This is unlikely, but
559 * before generating the copy, we'll just force a flush
560 * of any regs in the source range that have been promoted to
561 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800562 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700563 for (int i = 0; i < elems; i++) {
564 RegLocation loc = oatUpdateLoc(cUnit, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 if (loc.location == kLocPhysReg) {
566 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
567 loc.lowReg, kWord);
568 }
buzbee31a4a6f2012-02-28 15:36:15 -0800569 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 /*
571 * TUNING note: generated code here could be much improved, but
572 * this is an uncommon operation and isn't especially performance
573 * critical.
574 */
575 int rSrc = oatAllocTemp(cUnit);
576 int rDst = oatAllocTemp(cUnit);
577 int rIdx = oatAllocTemp(cUnit);
578#if defined(TARGET_ARM)
579 int rVal = rLR; // Using a lot of temps, rLR is known free here
580#elif defined(TARGET_X86)
jeffhao5772bab2012-05-18 11:51:26 -0700581 oatFreeTemp(cUnit, rRET0);
582 int rVal = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700583#else
584 int rVal = oatAllocTemp(cUnit);
585#endif
586 // Set up source pointer
buzbee3b3dbdd2012-06-13 13:39:34 -0700587 RegLocation rlFirst = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
589 oatSRegOffset(cUnit, rlFirst.sRegLow));
590 // Set up the target pointer
591 opRegRegImm(cUnit, kOpAdd, rDst, rRET0,
592 Array::DataOffset(component_size).Int32Value());
593 // Set up the loop counter (known to be > 0)
buzbee3b3dbdd2012-06-13 13:39:34 -0700594 loadConstant(cUnit, rIdx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 // Generate the copy loop. Going backwards for convenience
596 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
597 // Copy next element
598 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
599 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
600#if defined(TARGET_ARM)
601 // Combine sub & test using sub setflags encoding here
602 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
603 opCondBranch(cUnit, kCondGe, target);
604#else
605 oatFreeTemp(cUnit, rVal);
606 opRegImm(cUnit, kOpSub, rIdx, 1);
607 opCmpImmBranch(cUnit, kCondGe, rIdx, 0, target);
608#endif
jeffhao5772bab2012-05-18 11:51:26 -0700609#if defined(TARGET_X86)
610 // Restore the target pointer
611 opRegRegImm(cUnit, kOpAdd, rRET0, rDst,
612 -Array::DataOffset(component_size).Int32Value());
613#endif
buzbee3b3dbdd2012-06-13 13:39:34 -0700614 } else if (!info->isRange) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700615 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700616 for (int i = 0; i < elems; i++) {
617 RegLocation rlArg = loadValue(cUnit, info->args[i], kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 storeBaseDisp(cUnit, rRET0,
619 Array::DataOffset(component_size).Int32Value() +
620 i * 4, rlArg.lowReg, kWord);
621 // If the loadValue caused a temp to be allocated, free it
622 if (oatIsTemp(cUnit, rlArg.lowReg)) {
623 oatFreeTemp(cUnit, rlArg.lowReg);
624 }
625 }
626 }
buzbeee5f01222012-06-14 15:19:35 -0700627 if (info->result.location != kLocInvalid) {
628 storeValue(cUnit, info->result, oatGetReturn(cUnit, false /* not fp */));
629 }
buzbee31a4a6f2012-02-28 15:36:15 -0800630}
631
buzbee408ad162012-06-06 16:45:18 -0700632void genSput(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800634{
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 int fieldOffset;
636 int ssbIndex;
637 bool isVolatile;
638 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800639
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
641 *cUnit->dex_file, *cUnit->dex_cache,
642 cUnit->code_item, cUnit->method_idx,
643 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800644
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 bool fastPath =
646 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
647 fieldOffset, ssbIndex,
648 isReferrersClass, isVolatile,
649 true);
650 if (fastPath && !SLOW_FIELD_PATH) {
651 DCHECK_GE(fieldOffset, 0);
652 int rBase;
653 if (isReferrersClass) {
654 // Fast path, static storage base is this method's class
655 RegLocation rlMethod = loadCurrMethod(cUnit);
656 rBase = oatAllocTemp(cUnit);
657 loadWordDisp(cUnit, rlMethod.lowReg,
658 Method::DeclaringClassOffset().Int32Value(), rBase);
659 if (oatIsTemp(cUnit, rlMethod.lowReg)) {
660 oatFreeTemp(cUnit, rlMethod.lowReg);
661 }
buzbee31a4a6f2012-02-28 15:36:15 -0800662 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 // Medium path, static storage base in a different class which
664 // requires checks that the other class is initialized.
665 DCHECK_GE(ssbIndex, 0);
666 // May do runtime call so everything to home locations.
667 oatFlushAllRegs(cUnit);
668 // Using fixed register to sync with possible call to runtime
669 // support.
670 int rMethod = rARG1;
671 oatLockTemp(cUnit, rMethod);
672 loadCurrMethodDirect(cUnit, rMethod);
673 rBase = rARG0;
674 oatLockTemp(cUnit, rBase);
675 loadWordDisp(cUnit, rMethod,
676 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
677 rBase);
678 loadWordDisp(cUnit, rBase,
679 Array::DataOffset(sizeof(Object*)).Int32Value() +
680 sizeof(int32_t*) * ssbIndex, rBase);
681 // rBase now points at appropriate static storage base (Class*)
682 // or NULL if not initialized. Check for NULL and call helper if NULL.
683 // TUNING: fast path should fall through
684 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
685 loadConstant(cUnit, rARG0, ssbIndex);
686 callRuntimeHelperImm(cUnit,
687 ENTRYPOINT_OFFSET(pInitializeStaticStorage),
688 ssbIndex);
689#if defined(TARGET_MIPS)
690 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
691 opRegCopy(cUnit, rBase, rRET0);
692#endif
693 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
694 branchOver->target = (LIR*)skipTarget;
695 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800696 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700697 // rBase now holds static storage base
698 if (isLongOrDouble) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
700 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
702 }
703//FIXME: need to generalize the barrier call
704 if (isVolatile) {
705 oatGenMemBarrier(cUnit, kST);
706 }
707 if (isLongOrDouble) {
708 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
709 rlSrc.highReg);
710 } else {
711 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
712 }
713 if (isVolatile) {
714 oatGenMemBarrier(cUnit, kSY);
715 }
716 if (isObject) {
717 markGCCard(cUnit, rlSrc.lowReg, rBase);
718 }
719 oatFreeTemp(cUnit, rBase);
720 } else {
721 oatFlushAllRegs(cUnit); // Everything to home locations
722 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Static) :
723 (isObject ? ENTRYPOINT_OFFSET(pSetObjStatic)
724 : ENTRYPOINT_OFFSET(pSet32Static));
725 callRuntimeHelperImmRegLocation(cUnit, setterOffset, fieldIdx, rlSrc);
726 }
buzbee31a4a6f2012-02-28 15:36:15 -0800727}
728
buzbee408ad162012-06-06 16:45:18 -0700729void genSget(CompilationUnit* cUnit, uint32_t fieldIdx, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700730 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800731{
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 int fieldOffset;
733 int ssbIndex;
734 bool isVolatile;
735 bool isReferrersClass;
buzbee31a4a6f2012-02-28 15:36:15 -0800736
Bill Buzbeea114add2012-05-03 15:00:40 -0700737 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
738 *cUnit->dex_file, *cUnit->dex_cache,
739 cUnit->code_item, cUnit->method_idx,
740 cUnit->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800741
Bill Buzbeea114add2012-05-03 15:00:40 -0700742 bool fastPath =
743 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, &mUnit,
744 fieldOffset, ssbIndex,
745 isReferrersClass, isVolatile,
746 false);
747 if (fastPath && !SLOW_FIELD_PATH) {
748 DCHECK_GE(fieldOffset, 0);
749 int rBase;
750 if (isReferrersClass) {
751 // Fast path, static storage base is this method's class
752 RegLocation rlMethod = loadCurrMethod(cUnit);
753 rBase = oatAllocTemp(cUnit);
754 loadWordDisp(cUnit, rlMethod.lowReg,
755 Method::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800756 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700757 // Medium path, static storage base in a different class which
758 // requires checks that the other class is initialized
759 DCHECK_GE(ssbIndex, 0);
760 // May do runtime call so everything to home locations.
761 oatFlushAllRegs(cUnit);
762 // Using fixed register to sync with possible call to runtime
763 // support
764 int rMethod = rARG1;
765 oatLockTemp(cUnit, rMethod);
766 loadCurrMethodDirect(cUnit, rMethod);
767 rBase = rARG0;
768 oatLockTemp(cUnit, rBase);
769 loadWordDisp(cUnit, rMethod,
770 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
771 rBase);
772 loadWordDisp(cUnit, rBase,
773 Array::DataOffset(sizeof(Object*)).Int32Value() +
774 sizeof(int32_t*) * ssbIndex, rBase);
775 // rBase now points at appropriate static storage base (Class*)
776 // or NULL if not initialized. Check for NULL and call helper if NULL.
777 // TUNING: fast path should fall through
778 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rBase, 0, NULL);
779 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeStaticStorage),
780 ssbIndex);
781#if defined(TARGET_MIPS)
782 // For Arm, rRET0 = rARG0 = rBASE, for Mips, we need to copy
783 opRegCopy(cUnit, rBase, rRET0);
784#endif
785 LIR* skipTarget = newLIR0(cUnit, kPseudoTargetLabel);
786 branchOver->target = (LIR*)skipTarget;
787 oatFreeTemp(cUnit, rMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800788 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700789 // rBase now holds static storage base
Bill Buzbeea114add2012-05-03 15:00:40 -0700790 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
791 if (isVolatile) {
792 oatGenMemBarrier(cUnit, kSY);
793 }
794 if (isLongOrDouble) {
buzbee408ad162012-06-06 16:45:18 -0700795 loadBaseDispWide(cUnit, rBase, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700796 rlResult.highReg, INVALID_SREG);
797 } else {
798 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
799 }
800 oatFreeTemp(cUnit, rBase);
801 if (isLongOrDouble) {
802 storeValueWide(cUnit, rlDest, rlResult);
803 } else {
804 storeValue(cUnit, rlDest, rlResult);
805 }
806 } else {
807 oatFlushAllRegs(cUnit); // Everything to home locations
808 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Static) :
809 (isObject ? ENTRYPOINT_OFFSET(pGetObjStatic)
810 : ENTRYPOINT_OFFSET(pGet32Static));
811 callRuntimeHelperImm(cUnit, getterOffset, fieldIdx);
812 if (isLongOrDouble) {
813 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
814 storeValueWide(cUnit, rlDest, rlResult);
815 } else {
816 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
817 storeValue(cUnit, rlDest, rlResult);
818 }
819 }
buzbee31a4a6f2012-02-28 15:36:15 -0800820}
821
822
823// Debugging routine - if null target, branch to DebugMe
824void genShowTarget(CompilationUnit* cUnit)
825{
buzbeea7678db2012-03-05 15:35:46 -0800826#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 UNIMPLEMENTED(WARNING) << "genShowTarget";
buzbeea7678db2012-03-05 15:35:46 -0800828#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 LIR* branchOver = opCmpImmBranch(cUnit, kCondNe, rINVOKE_TGT, 0, NULL);
830 loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pDebugMe), rINVOKE_TGT);
831 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
832 branchOver->target = (LIR*)target;
buzbeea7678db2012-03-05 15:35:46 -0800833#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800834}
835
buzbee408ad162012-06-06 16:45:18 -0700836void genThrowVerificationError(CompilationUnit* cUnit, int info1, int info2)
buzbee31a4a6f2012-02-28 15:36:15 -0800837{
Bill Buzbeea114add2012-05-03 15:00:40 -0700838 callRuntimeHelperImmImm(cUnit,
839 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode),
buzbee408ad162012-06-06 16:45:18 -0700840 info1, info2);
buzbee31a4a6f2012-02-28 15:36:15 -0800841}
842
843void handleSuspendLaunchpads(CompilationUnit *cUnit)
844{
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 LIR** suspendLabel = (LIR **)cUnit->suspendLaunchpads.elemList;
846 int numElems = cUnit->suspendLaunchpads.numUsed;
847 for (int i = 0; i < numElems; i++) {
848 oatResetRegPool(cUnit);
849 oatResetDefTracking(cUnit);
850 LIR* lab = suspendLabel[i];
851 LIR* resumeLab = (LIR*)lab->operands[0];
852 cUnit->currentDalvikOffset = lab->operands[1];
853 oatAppendLIR(cUnit, lab);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700854#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700855 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700856#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700857 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
858 opReg(cUnit, kOpBlx, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700859#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700860 opUnconditionalBranch(cUnit, resumeLab);
861 }
buzbee31a4a6f2012-02-28 15:36:15 -0800862}
863
buzbeefc9e6fa2012-03-23 15:14:29 -0700864void handleIntrinsicLaunchpads(CompilationUnit *cUnit)
865{
Bill Buzbeea114add2012-05-03 15:00:40 -0700866 LIR** intrinsicLabel = (LIR **)cUnit->intrinsicLaunchpads.elemList;
867 int numElems = cUnit->intrinsicLaunchpads.numUsed;
868 for (int i = 0; i < numElems; i++) {
869 oatResetRegPool(cUnit);
870 oatResetDefTracking(cUnit);
871 LIR* lab = intrinsicLabel[i];
buzbee3b3dbdd2012-06-13 13:39:34 -0700872 CallInfo* info = (CallInfo*)lab->operands[0];
buzbee15bf9802012-06-12 17:49:27 -0700873 cUnit->currentDalvikOffset = info->offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700874 oatAppendLIR(cUnit, lab);
buzbee15bf9802012-06-12 17:49:27 -0700875 genInvoke(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 LIR* resumeLab = (LIR*)lab->operands[2];
877 if (resumeLab != NULL) {
878 opUnconditionalBranch(cUnit, resumeLab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700879 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700881}
882
buzbee31a4a6f2012-02-28 15:36:15 -0800883void handleThrowLaunchpads(CompilationUnit *cUnit)
884{
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 LIR** throwLabel = (LIR **)cUnit->throwLaunchpads.elemList;
886 int numElems = cUnit->throwLaunchpads.numUsed;
887 for (int i = 0; i < numElems; i++) {
888 oatResetRegPool(cUnit);
889 oatResetDefTracking(cUnit);
890 LIR* lab = throwLabel[i];
891 cUnit->currentDalvikOffset = lab->operands[1];
892 oatAppendLIR(cUnit, lab);
893 int funcOffset = 0;
894 int v1 = lab->operands[2];
895 int v2 = lab->operands[3];
896 switch (lab->operands[0]) {
897 case kThrowNullPointer:
898 funcOffset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
899 break;
900 case kThrowArrayBounds:
jeffhao44335e12012-06-18 13:48:25 -0700901#if defined (TARGET_X86)
902 // x86 leaves the array pointer in v2, so load the array length that the handler expects
903 opRegMem(cUnit, kOpMov, v2, v2, Array::LengthOffset().Int32Value());
904#endif
905 // Move v1 (array index) to rARG0 and v2 (array length) to rARG1
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 if (v2 != rARG0) {
907 opRegCopy(cUnit, rARG0, v1);
908 opRegCopy(cUnit, rARG1, v2);
909 } else {
910 if (v1 == rARG1) {
jeffhao44335e12012-06-18 13:48:25 -0700911 // Swap v1 and v2, using rARG2 as a temp
912 opRegCopy(cUnit, rARG2, v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 opRegCopy(cUnit, rARG1, v2);
jeffhao44335e12012-06-18 13:48:25 -0700914 opRegCopy(cUnit, rARG0, rARG2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700915 } else {
916 opRegCopy(cUnit, rARG1, v2);
917 opRegCopy(cUnit, rARG0, v1);
918 }
buzbee31a4a6f2012-02-28 15:36:15 -0800919 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700920 funcOffset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
921 break;
922 case kThrowDivZero:
923 funcOffset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
924 break;
925 case kThrowVerificationError:
926 loadConstant(cUnit, rARG0, v1);
927 loadConstant(cUnit, rARG1, v2);
928 funcOffset =
929 ENTRYPOINT_OFFSET(pThrowVerificationErrorFromCode);
930 break;
931 case kThrowNoSuchMethod:
932 opRegCopy(cUnit, rARG0, v1);
933 funcOffset =
934 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
935 break;
936 case kThrowStackOverflow:
937 funcOffset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
938 // Restore stack alignment
Ian Rogersab2b55d2012-03-18 00:06:11 -0700939#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700940 opRegImm(cUnit, kOpAdd, rSP,
941 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700942#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700943 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700944#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 break;
946 default:
947 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800948 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700949 oatClobberCalleeSave(cUnit);
950#if !defined(TARGET_X86)
951 int rTgt = loadHelper(cUnit, funcOffset);
952 opReg(cUnit, kOpBlx, rTgt);
953 oatFreeTemp(cUnit, rTgt);
954#else
955 opThreadMem(cUnit, kOpBlx, funcOffset);
956#endif
957 }
buzbee31a4a6f2012-02-28 15:36:15 -0800958}
959
960/* Needed by the Assembler */
961void oatSetupResourceMasks(LIR* lir)
962{
Bill Buzbeea114add2012-05-03 15:00:40 -0700963 setupResourceMasks(lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800964}
965
buzbee16da88c2012-03-20 10:38:17 -0700966bool fastInstance(CompilationUnit* cUnit, uint32_t fieldIdx,
967 int& fieldOffset, bool& isVolatile, bool isPut)
968{
Bill Buzbeea114add2012-05-03 15:00:40 -0700969 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
970 *cUnit->dex_file, *cUnit->dex_cache,
971 cUnit->code_item, cUnit->method_idx,
972 cUnit->access_flags);
973 return cUnit->compiler->ComputeInstanceFieldInfo(fieldIdx, &mUnit,
974 fieldOffset, isVolatile, isPut);
buzbee16da88c2012-03-20 10:38:17 -0700975}
976
buzbee408ad162012-06-06 16:45:18 -0700977void genIGet(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -0800978 RegLocation rlDest, RegLocation rlObj,
Bill Buzbeea114add2012-05-03 15:00:40 -0700979 bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -0800980{
Bill Buzbeea114add2012-05-03 15:00:40 -0700981 int fieldOffset;
982 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800983
Bill Buzbeea114add2012-05-03 15:00:40 -0700984 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800985
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 if (fastPath && !SLOW_FIELD_PATH) {
987 RegLocation rlResult;
988 RegisterClass regClass = oatRegClassBySize(size);
989 DCHECK_GE(fieldOffset, 0);
990 rlObj = loadValue(cUnit, rlObj, kCoreReg);
991 if (isLongOrDouble) {
992 DCHECK(rlDest.wide);
buzbee408ad162012-06-06 16:45:18 -0700993 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800994#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700995 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -0700996 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
997 loadBaseDispWide(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 rlResult.highReg, rlObj.sRegLow);
999 if (isVolatile) {
1000 oatGenMemBarrier(cUnit, kSY);
1001 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001002#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001003 int regPtr = oatAllocTemp(cUnit);
1004 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1005 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1006 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1007 if (isVolatile) {
1008 oatGenMemBarrier(cUnit, kSY);
1009 }
1010 oatFreeTemp(cUnit, regPtr);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001011#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001012 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001013 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001015 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
1016 loadBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlResult.lowReg,
Bill Buzbeea114add2012-05-03 15:00:40 -07001017 kWord, rlObj.sRegLow);
1018 if (isVolatile) {
1019 oatGenMemBarrier(cUnit, kSY);
1020 }
1021 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001022 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001023 } else {
1024 int getterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pGet64Instance) :
1025 (isObject ? ENTRYPOINT_OFFSET(pGetObjInstance)
1026 : ENTRYPOINT_OFFSET(pGet32Instance));
1027 callRuntimeHelperImmRegLocation(cUnit, getterOffset, fieldIdx, rlObj);
1028 if (isLongOrDouble) {
1029 RegLocation rlResult = oatGetReturnWide(cUnit, rlDest.fp);
1030 storeValueWide(cUnit, rlDest, rlResult);
1031 } else {
1032 RegLocation rlResult = oatGetReturn(cUnit, rlDest.fp);
1033 storeValue(cUnit, rlDest, rlResult);
1034 }
1035 }
buzbee31a4a6f2012-02-28 15:36:15 -08001036}
1037
buzbee408ad162012-06-06 16:45:18 -07001038void genIPut(CompilationUnit* cUnit, uint32_t fieldIdx, int optFlags, OpSize size,
1039 RegLocation rlSrc, RegLocation rlObj, bool isLongOrDouble, bool isObject)
buzbee31a4a6f2012-02-28 15:36:15 -08001040{
Bill Buzbeea114add2012-05-03 15:00:40 -07001041 int fieldOffset;
1042 bool isVolatile;
buzbee31a4a6f2012-02-28 15:36:15 -08001043
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
1045 true);
1046 if (fastPath && !SLOW_FIELD_PATH) {
1047 RegisterClass regClass = oatRegClassBySize(size);
1048 DCHECK_GE(fieldOffset, 0);
1049 rlObj = loadValue(cUnit, rlObj, kCoreReg);
1050 if (isLongOrDouble) {
1051 int regPtr;
1052 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee408ad162012-06-06 16:45:18 -07001053 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001054 regPtr = oatAllocTemp(cUnit);
1055 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
1056 if (isVolatile) {
1057 oatGenMemBarrier(cUnit, kST);
1058 }
jeffhao41005dd2012-05-09 17:58:52 -07001059 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001060 if (isVolatile) {
1061 oatGenMemBarrier(cUnit, kSY);
1062 }
1063 oatFreeTemp(cUnit, regPtr);
buzbee31a4a6f2012-02-28 15:36:15 -08001064 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001065 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee408ad162012-06-06 16:45:18 -07001066 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07001067 if (isVolatile) {
1068 oatGenMemBarrier(cUnit, kST);
1069 }
1070 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
1071 if (isVolatile) {
1072 oatGenMemBarrier(cUnit, kSY);
1073 }
1074 if (isObject) {
1075 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
1076 }
buzbee31a4a6f2012-02-28 15:36:15 -08001077 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001078 } else {
1079 int setterOffset = isLongOrDouble ? ENTRYPOINT_OFFSET(pSet64Instance) :
1080 (isObject ? ENTRYPOINT_OFFSET(pSetObjInstance)
1081 : ENTRYPOINT_OFFSET(pSet32Instance));
1082 callRuntimeHelperImmRegLocationRegLocation(cUnit, setterOffset,
1083 fieldIdx, rlObj, rlSrc);
1084 }
buzbee31a4a6f2012-02-28 15:36:15 -08001085}
1086
buzbee6969d502012-06-15 16:40:31 -07001087void genConstClass(CompilationUnit* cUnit, uint32_t type_idx,
1088 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001089{
Bill Buzbeea114add2012-05-03 15:00:40 -07001090 RegLocation rlMethod = loadCurrMethod(cUnit);
1091 int resReg = oatAllocTemp(cUnit);
1092 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1093 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1094 cUnit->dex_cache,
1095 *cUnit->dex_file,
1096 type_idx)) {
1097 // Call out to helper which resolves type and verifies access.
1098 // Resolved type returned in rRET0.
1099 callRuntimeHelperImmReg(cUnit,
1100 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1101 type_idx, rlMethod.lowReg);
1102 RegLocation rlResult = oatGetReturn(cUnit, false);
1103 storeValue(cUnit, rlDest, rlResult);
1104 } else {
1105 // We're don't need access checks, load type from dex cache
1106 int32_t dex_cache_offset =
1107 Method::DexCacheResolvedTypesOffset().Int32Value();
1108 loadWordDisp(cUnit, rlMethod.lowReg, dex_cache_offset, resReg);
1109 int32_t offset_of_type =
1110 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1111 * type_idx);
1112 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
1113 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
1114 type_idx) || SLOW_TYPE_PATH) {
1115 // Slow path, at runtime test if type is null and if so initialize
1116 oatFlushAllRegs(cUnit);
1117 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rlResult.lowReg, 0, NULL);
1118 // Resolved, store and hop over following code
1119 storeValue(cUnit, rlDest, rlResult);
1120 /*
1121 * Because we have stores of the target value on two paths,
1122 * clobber temp tracking for the destination using the ssa name
1123 */
1124 oatClobberSReg(cUnit, rlDest.sRegLow);
1125 LIR* branch2 = opUnconditionalBranch(cUnit,0);
1126 // TUNING: move slow path to end & remove unconditional branch
1127 LIR* target1 = newLIR0(cUnit, kPseudoTargetLabel);
1128 // Call out to helper, which will return resolved type in rARG0
1129 callRuntimeHelperImmReg(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1130 type_idx, rlMethod.lowReg);
1131 RegLocation rlResult = oatGetReturn(cUnit, false);
1132 storeValue(cUnit, rlDest, rlResult);
1133 /*
1134 * Because we have stores of the target value on two paths,
1135 * clobber temp tracking for the destination using the ssa name
1136 */
1137 oatClobberSReg(cUnit, rlDest.sRegLow);
1138 // Rejoin code paths
1139 LIR* target2 = newLIR0(cUnit, kPseudoTargetLabel);
1140 branch1->target = (LIR*)target1;
1141 branch2->target = (LIR*)target2;
buzbee31a4a6f2012-02-28 15:36:15 -08001142 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001143 // Fast path, we're done - just store result
1144 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001145 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001146 }
buzbee31a4a6f2012-02-28 15:36:15 -08001147}
Ian Rogersab2b55d2012-03-18 00:06:11 -07001148
buzbee6969d502012-06-15 16:40:31 -07001149void genConstString(CompilationUnit* cUnit, uint32_t string_idx,
1150 RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001151{
Bill Buzbeea114add2012-05-03 15:00:40 -07001152 /* NOTE: Most strings should be available at compile time */
Bill Buzbeea114add2012-05-03 15:00:40 -07001153 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
1154 (sizeof(String*) * string_idx);
1155 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(
1156 cUnit->dex_cache, string_idx) || SLOW_STRING_PATH) {
1157 // slow path, resolve string if not in dex cache
1158 oatFlushAllRegs(cUnit);
1159 oatLockCallTemps(cUnit); // Using explicit registers
1160 loadCurrMethodDirect(cUnit, rARG2);
1161 loadWordDisp(cUnit, rARG2,
1162 Method::DexCacheStringsOffset().Int32Value(), rARG0);
1163 // Might call out to helper, which will return resolved string in rRET0
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001164#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001165 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001166#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001167 loadWordDisp(cUnit, rRET0, offset_of_string, rARG0);
1168 loadConstant(cUnit, rARG1, string_idx);
buzbee31a4a6f2012-02-28 15:36:15 -08001169#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001170 opRegImm(cUnit, kOpCmp, rRET0, 0); // Is resolved?
1171 genBarrier(cUnit);
1172 // For testing, always force through helper
1173 if (!EXERCISE_SLOWEST_STRING_PATH) {
1174 opIT(cUnit, kArmCondEq, "T");
buzbee31a4a6f2012-02-28 15:36:15 -08001175 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001176 opRegCopy(cUnit, rARG0, rARG2); // .eq
1177 opReg(cUnit, kOpBlx, rTgt); // .eq, helper(Method*, string_idx)
1178 oatFreeTemp(cUnit, rTgt);
1179#elif defined(TARGET_MIPS)
1180 LIR* branch = opCmpImmBranch(cUnit, kCondNe, rRET0, 0, NULL);
1181 opRegCopy(cUnit, rARG0, rARG2); // .eq
1182 opReg(cUnit, kOpBlx, rTgt);
1183 oatFreeTemp(cUnit, rTgt);
1184 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1185 branch->target = target;
1186#else
1187 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pResolveStringFromCode),
1188 rARG2, rARG1);
1189#endif
1190 genBarrier(cUnit);
1191 storeValue(cUnit, rlDest, oatGetReturn(cUnit, false));
1192 } else {
1193 RegLocation rlMethod = loadCurrMethod(cUnit);
1194 int resReg = oatAllocTemp(cUnit);
1195 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1196 loadWordDisp(cUnit, rlMethod.lowReg,
1197 Method::DexCacheStringsOffset().Int32Value(), resReg);
1198 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
1199 storeValue(cUnit, rlDest, rlResult);
1200 }
buzbee31a4a6f2012-02-28 15:36:15 -08001201}
1202
1203/*
1204 * Let helper function take care of everything. Will
1205 * call Class::NewInstanceFromCode(type_idx, method);
1206 */
buzbee408ad162012-06-06 16:45:18 -07001207void genNewInstance(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest)
buzbee31a4a6f2012-02-28 15:36:15 -08001208{
Bill Buzbeea114add2012-05-03 15:00:40 -07001209 oatFlushAllRegs(cUnit); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -07001210 // alloc will always check for resolution, do we also need to verify
1211 // access because the verifier was unable to?
1212 int funcOffset;
1213 if (cUnit->compiler->CanAccessInstantiableTypeWithoutChecks(
1214 cUnit->method_idx, cUnit->dex_cache, *cUnit->dex_file, type_idx)) {
1215 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
1216 } else {
1217 funcOffset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
1218 }
1219 callRuntimeHelperImmMethod(cUnit, funcOffset, type_idx);
1220 RegLocation rlResult = oatGetReturn(cUnit, false);
1221 storeValue(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001222}
1223
buzbee408ad162012-06-06 16:45:18 -07001224void genThrow(CompilationUnit* cUnit, RegLocation rlSrc)
Ian Rogersab2b55d2012-03-18 00:06:11 -07001225{
Bill Buzbeea114add2012-05-03 15:00:40 -07001226 oatFlushAllRegs(cUnit);
1227 callRuntimeHelperRegLocation(cUnit, ENTRYPOINT_OFFSET(pDeliverException),
1228 rlSrc);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001229}
1230
buzbee408ad162012-06-06 16:45:18 -07001231void genInstanceof(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001232 RegLocation rlSrc)
1233{
Bill Buzbeea114add2012-05-03 15:00:40 -07001234 oatFlushAllRegs(cUnit);
1235 // May generate a call - use explicit registers
1236 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001237 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1238 int classReg = rARG2; // rARG2 will hold the Class*
1239 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1240 cUnit->dex_cache,
1241 *cUnit->dex_file,
1242 type_idx)) {
1243 // Check we have access to type_idx and if not throw IllegalAccessError,
1244 // returns Class* in rARG0
1245 callRuntimeHelperImm(cUnit,
1246 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1247 type_idx);
1248 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1249 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1250 } else {
1251 // Load dex cache entry into classReg (rARG2)
1252 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1253 loadWordDisp(cUnit, rARG1,
1254 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1255 int32_t offset_of_type =
1256 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
1257 * type_idx);
1258 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1259 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1260 cUnit->dex_cache, type_idx)) {
1261 // Need to test presence of type in dex cache at runtime
1262 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1263 // Not resolved
1264 // Call out to helper, which will return resolved type in rRET0
1265 callRuntimeHelperImm(cUnit, ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1266 type_idx);
1267 opRegCopy(cUnit, rARG2, rRET0); // Align usage with fast path
1268 loadValueDirectFixed(cUnit, rlSrc, rARG0); /* reload Ref */
1269 // Rejoin code paths
1270 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1271 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001272 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001273 }
1274 /* rARG0 is ref, rARG2 is class. If ref==null, use directly as bool result */
1275 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1276 /* load object->klass_ */
1277 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1278 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1279 /* rARG0 is ref, rARG1 is ref->klass_, rARG2 is class */
buzbee0398c422012-03-02 15:22:47 -08001280#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001281 /* Uses conditional nullification */
1282 int rTgt = loadHelper(cUnit,
1283 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1284 opRegReg(cUnit, kOpCmp, rARG1, rARG2); // Same?
1285 opIT(cUnit, kArmCondEq, "EE"); // if-convert the test
1286 loadConstant(cUnit, rARG0, 1); // .eq case - load true
1287 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1288 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1289 oatFreeTemp(cUnit, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -08001290#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001291 /* Uses branchovers */
1292 loadConstant(cUnit, rARG0, 1); // assume true
1293 LIR* branchover = opCmpBranch(cUnit, kCondEq, rARG1, rARG2, NULL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001294#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001295 int rTgt = loadHelper(cUnit,
1296 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1297 opRegCopy(cUnit, rARG0, rARG2); // .ne case - arg0 <= class
1298 opReg(cUnit, kOpBlx, rTgt); // .ne case: helper(class, ref->class)
1299 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001300#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001301 opRegCopy(cUnit, rARG0, rARG2);
1302 opThreadMem(cUnit, kOpBlx,
1303 ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001304#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001305#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001306 oatClobberCalleeSave(cUnit);
1307 /* branch targets here */
1308 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1309 RegLocation rlResult = oatGetReturn(cUnit, false);
1310 storeValue(cUnit, rlDest, rlResult);
1311 branch1->target = target;
buzbee0398c422012-03-02 15:22:47 -08001312#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001313 branchover->target = target;
buzbee0398c422012-03-02 15:22:47 -08001314#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001315}
1316
buzbee408ad162012-06-06 16:45:18 -07001317void genCheckCast(CompilationUnit* cUnit, uint32_t type_idx, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08001318{
Bill Buzbeea114add2012-05-03 15:00:40 -07001319 oatFlushAllRegs(cUnit);
1320 // May generate a call - use explicit registers
1321 oatLockCallTemps(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001322 loadCurrMethodDirect(cUnit, rARG1); // rARG1 <= current Method*
1323 int classReg = rARG2; // rARG2 will hold the Class*
1324 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
1325 cUnit->dex_cache,
1326 *cUnit->dex_file,
1327 type_idx)) {
1328 // Check we have access to type_idx and if not throw IllegalAccessError,
1329 // returns Class* in rRET0
1330 // InitializeTypeAndVerifyAccess(idx, method)
1331 callRuntimeHelperImmReg(cUnit,
1332 ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
1333 type_idx, rARG1);
1334 opRegCopy(cUnit, classReg, rRET0); // Align usage with fast path
1335 } else {
1336 // Load dex cache entry into classReg (rARG2)
1337 loadWordDisp(cUnit, rARG1,
1338 Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
1339 int32_t offset_of_type =
1340 Array::DataOffset(sizeof(Class*)).Int32Value() +
1341 (sizeof(Class*) * type_idx);
1342 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
1343 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(
1344 cUnit->dex_cache, type_idx)) {
1345 // Need to test presence of type in dex cache at runtime
1346 LIR* hopBranch = opCmpImmBranch(cUnit, kCondNe, classReg, 0, NULL);
1347 // Not resolved
1348 // Call out to helper, which will return resolved type in rARG0
1349 // InitializeTypeFromCode(idx, method)
1350 callRuntimeHelperImmReg(cUnit,
1351 ENTRYPOINT_OFFSET(pInitializeTypeFromCode),
1352 type_idx, rARG1);
1353 opRegCopy(cUnit, classReg, rARG0); // Align usage with fast path
1354 // Rejoin code paths
1355 LIR* hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
1356 hopBranch->target = (LIR*)hopTarget;
buzbee31a4a6f2012-02-28 15:36:15 -08001357 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001358 }
1359 // At this point, classReg (rARG2) has class
1360 loadValueDirectFixed(cUnit, rlSrc, rARG0); // rARG0 <= ref
1361 /* Null is OK - continue */
1362 LIR* branch1 = opCmpImmBranch(cUnit, kCondEq, rARG0, 0, NULL);
1363 /* load object->klass_ */
1364 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1365 loadWordDisp(cUnit, rARG0, Object::ClassOffset().Int32Value(), rARG1);
1366 /* rARG1 now contains object->klass_ */
Ian Rogersab2b55d2012-03-18 00:06:11 -07001367#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001368 LIR* branch2 = opCmpBranch(cUnit, kCondEq, rARG1, classReg, NULL);
1369 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode),
1370 rARG1, rARG2);
Ian Rogersab2b55d2012-03-18 00:06:11 -07001371#else // defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001372 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1373 opRegReg(cUnit, kOpCmp, rARG1, classReg);
1374 LIR* branch2 = opCondBranch(cUnit, kCondEq, NULL); /* If eq, trivial yes */
1375 opRegCopy(cUnit, rARG0, rARG1);
1376 opRegCopy(cUnit, rARG1, rARG2);
1377 oatClobberCalleeSave(cUnit);
1378 opReg(cUnit, kOpBlx, rTgt);
1379 oatFreeTemp(cUnit, rTgt);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001380#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001381 /* branch target here */
1382 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1383 branch1->target = target;
1384 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001385}
1386
buzbee31a4a6f2012-02-28 15:36:15 -08001387/*
1388 * Generate array store
1389 *
1390 */
buzbee408ad162012-06-06 16:45:18 -07001391void genArrayObjPut(CompilationUnit* cUnit, int optFlags, RegLocation rlArray,
Bill Buzbeea114add2012-05-03 15:00:40 -07001392 RegLocation rlIndex, RegLocation rlSrc, int scale)
buzbee31a4a6f2012-02-28 15:36:15 -08001393{
Bill Buzbeea114add2012-05-03 15:00:40 -07001394 int lenOffset = Array::LengthOffset().Int32Value();
1395 int dataOffset = Array::DataOffset(sizeof(Object*)).Int32Value();
buzbee31a4a6f2012-02-28 15:36:15 -08001396
Bill Buzbeea114add2012-05-03 15:00:40 -07001397 oatFlushAllRegs(cUnit); // Use explicit registers
1398 oatLockCallTemps(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08001399
Bill Buzbeea114add2012-05-03 15:00:40 -07001400 int rValue = rARG0; // Register holding value
1401 int rArrayClass = rARG1; // Register holding array's Class
1402 int rArray = rARG2; // Register holding array
1403 int rIndex = rARG3; // Register holding index into array
Ian Rogersd36c52e2012-04-09 16:29:25 -07001404
Bill Buzbeea114add2012-05-03 15:00:40 -07001405 loadValueDirectFixed(cUnit, rlArray, rArray); // Grab array
1406 loadValueDirectFixed(cUnit, rlSrc, rValue); // Grab value
1407 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Grab index
Ian Rogersd36c52e2012-04-09 16:29:25 -07001408
buzbee408ad162012-06-06 16:45:18 -07001409 genNullCheck(cUnit, rlArray.sRegLow, rArray, optFlags); // NPE?
Ian Rogersd36c52e2012-04-09 16:29:25 -07001410
Bill Buzbeea114add2012-05-03 15:00:40 -07001411 // Store of null?
1412 LIR* null_value_check = opCmpImmBranch(cUnit, kCondEq, rValue, 0, NULL);
Ian Rogersd36c52e2012-04-09 16:29:25 -07001413
Bill Buzbeea114add2012-05-03 15:00:40 -07001414 // Get the array's class.
1415 loadWordDisp(cUnit, rArray, Object::ClassOffset().Int32Value(), rArrayClass);
1416 callRuntimeHelperRegReg(cUnit, ENTRYPOINT_OFFSET(pCanPutArrayElementFromCode),
1417 rValue, rArrayClass);
1418 // Redo loadValues in case they didn't survive the call.
1419 loadValueDirectFixed(cUnit, rlArray, rArray); // Reload array
1420 loadValueDirectFixed(cUnit, rlIndex, rIndex); // Reload index
1421 loadValueDirectFixed(cUnit, rlSrc, rValue); // Reload value
1422 rArrayClass = INVALID_REG;
buzbee31a4a6f2012-02-28 15:36:15 -08001423
Bill Buzbeea114add2012-05-03 15:00:40 -07001424 // Branch here if value to be stored == null
1425 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
1426 null_value_check->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001427
Ian Rogersb41b33b2012-03-20 14:22:54 -07001428#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001429 // make an extra temp available for card mark below
1430 oatFreeTemp(cUnit, rARG1);
buzbee408ad162012-06-06 16:45:18 -07001431 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001432 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
buzbee408ad162012-06-06 16:45:18 -07001433 genRegMemCheck(cUnit, kCondUge, rIndex, rArray, lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001434 }
buzbee408ad162012-06-06 16:45:18 -07001435 storeBaseIndexedDisp(cUnit, rArray, rIndex, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001436 dataOffset, rValue, INVALID_REG, kWord, INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001437#else
buzbee408ad162012-06-06 16:45:18 -07001438 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001439 int regLen = INVALID_REG;
1440 if (needsRangeCheck) {
1441 regLen = rARG1;
buzbeef1f86362012-07-10 15:18:31 -07001442 loadWordDisp(cUnit, rArray, lenOffset, regLen); // Get len
Bill Buzbeea114add2012-05-03 15:00:40 -07001443 }
1444 /* rPtr -> array data */
1445 int rPtr = oatAllocTemp(cUnit);
1446 opRegRegImm(cUnit, kOpAdd, rPtr, rArray, dataOffset);
1447 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001448 genRegRegCheck(cUnit, kCondCs, rIndex, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001449 }
1450 storeBaseIndexed(cUnit, rPtr, rIndex, rValue, scale, kWord);
1451 oatFreeTemp(cUnit, rPtr);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001452#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001453 oatFreeTemp(cUnit, rIndex);
1454 markGCCard(cUnit, rValue, rArray);
buzbee31a4a6f2012-02-28 15:36:15 -08001455}
1456
1457/*
1458 * Generate array load
1459 */
buzbee408ad162012-06-06 16:45:18 -07001460void genArrayGet(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001461 RegLocation rlArray, RegLocation rlIndex,
1462 RegLocation rlDest, int scale)
1463{
Bill Buzbeea114add2012-05-03 15:00:40 -07001464 RegisterClass regClass = oatRegClassBySize(size);
1465 int lenOffset = Array::LengthOffset().Int32Value();
1466 int dataOffset;
1467 RegLocation rlResult;
1468 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1469 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001470
Bill Buzbeea114add2012-05-03 15:00:40 -07001471 if (size == kLong || size == kDouble) {
1472 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1473 } else {
1474 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1475 }
buzbee31a4a6f2012-02-28 15:36:15 -08001476
Bill Buzbeea114add2012-05-03 15:00:40 -07001477 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001478 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001479
Ian Rogersb5d09b22012-03-06 22:14:17 -08001480#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001481 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001482 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1483 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001484 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001485 }
1486 if ((size == kLong) || (size == kDouble)) {
jeffhao3f9ace82012-05-25 11:25:36 -07001487 int regAddr = oatAllocTemp(cUnit);
1488 newLIR5(cUnit, kX86Lea32RA, regAddr, rlArray.lowReg, rlIndex.lowReg, scale, dataOffset);
1489 oatFreeTemp(cUnit, rlArray.lowReg);
1490 oatFreeTemp(cUnit, rlIndex.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001491 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee408ad162012-06-06 16:45:18 -07001492 loadBaseIndexedDisp(cUnit, regAddr, INVALID_REG, 0, 0, rlResult.lowReg,
jeffhao21e12712012-05-25 19:06:18 -07001493 rlResult.highReg, size, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001494 storeValueWide(cUnit, rlDest, rlResult);
1495 } else {
1496 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001497
buzbee408ad162012-06-06 16:45:18 -07001498 loadBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001499 dataOffset, rlResult.lowReg, INVALID_REG, size,
1500 INVALID_SREG);
Ian Rogersb5d09b22012-03-06 22:14:17 -08001501
Bill Buzbeea114add2012-05-03 15:00:40 -07001502 storeValue(cUnit, rlDest, rlResult);
1503 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001504#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001505 int regPtr = oatAllocTemp(cUnit);
buzbee408ad162012-06-06 16:45:18 -07001506 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001507 int regLen = INVALID_REG;
1508 if (needsRangeCheck) {
1509 regLen = oatAllocTemp(cUnit);
1510 /* Get len */
1511 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1512 }
1513 /* regPtr -> array data */
1514 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1515 oatFreeTemp(cUnit, rlArray.lowReg);
1516 if ((size == kLong) || (size == kDouble)) {
1517 if (scale) {
1518 int rNewIndex = oatAllocTemp(cUnit);
1519 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1520 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1521 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001522 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001523 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001524 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001525 oatFreeTemp(cUnit, rlIndex.lowReg);
1526 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1527
1528 if (needsRangeCheck) {
1529 // TODO: change kCondCS to a more meaningful name, is the sense of
1530 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001531 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001532 oatFreeTemp(cUnit, regLen);
1533 }
1534 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1535
1536 oatFreeTemp(cUnit, regPtr);
1537 storeValueWide(cUnit, rlDest, rlResult);
1538 } else {
1539 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1540
1541 if (needsRangeCheck) {
1542 // TODO: change kCondCS to a more meaningful name, is the sense of
1543 // carry-set/clear flipped?
buzbee408ad162012-06-06 16:45:18 -07001544 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001545 oatFreeTemp(cUnit, regLen);
1546 }
1547 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1548 scale, size);
1549
1550 oatFreeTemp(cUnit, regPtr);
1551 storeValue(cUnit, rlDest, rlResult);
1552 }
Ian Rogersb5d09b22012-03-06 22:14:17 -08001553#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001554}
1555
1556/*
1557 * Generate array store
1558 *
1559 */
buzbee408ad162012-06-06 16:45:18 -07001560void genArrayPut(CompilationUnit* cUnit, int optFlags, OpSize size,
buzbee31a4a6f2012-02-28 15:36:15 -08001561 RegLocation rlArray, RegLocation rlIndex,
1562 RegLocation rlSrc, int scale)
1563{
Bill Buzbeea114add2012-05-03 15:00:40 -07001564 RegisterClass regClass = oatRegClassBySize(size);
1565 int lenOffset = Array::LengthOffset().Int32Value();
1566 int dataOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001567
Bill Buzbeea114add2012-05-03 15:00:40 -07001568 if (size == kLong || size == kDouble) {
1569 dataOffset = Array::DataOffset(sizeof(int64_t)).Int32Value();
1570 } else {
1571 dataOffset = Array::DataOffset(sizeof(int32_t)).Int32Value();
1572 }
buzbee31a4a6f2012-02-28 15:36:15 -08001573
Bill Buzbeea114add2012-05-03 15:00:40 -07001574 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1575 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001576#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001577 int regPtr;
1578 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1579 oatClobber(cUnit, rlArray.lowReg);
1580 regPtr = rlArray.lowReg;
1581 } else {
1582 regPtr = oatAllocTemp(cUnit);
1583 opRegCopy(cUnit, regPtr, rlArray.lowReg);
1584 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001585#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001586
Bill Buzbeea114add2012-05-03 15:00:40 -07001587 /* null object? */
buzbee408ad162012-06-06 16:45:18 -07001588 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, optFlags);
buzbee31a4a6f2012-02-28 15:36:15 -08001589
Ian Rogersb41b33b2012-03-20 14:22:54 -07001590#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07001591 if (!(optFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001592 /* if (rlIndex >= [rlArray + lenOffset]) goto kThrowArrayBounds */
1593 genRegMemCheck(cUnit, kCondUge, rlIndex.lowReg, rlArray.lowReg,
buzbee408ad162012-06-06 16:45:18 -07001594 lenOffset, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001595 }
1596 if ((size == kLong) || (size == kDouble)) {
1597 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1598 } else {
1599 rlSrc = loadValue(cUnit, rlSrc, regClass);
1600 }
buzbee408ad162012-06-06 16:45:18 -07001601 storeBaseIndexedDisp(cUnit, rlArray.lowReg, rlIndex.lowReg, scale,
Bill Buzbeea114add2012-05-03 15:00:40 -07001602 dataOffset, rlSrc.lowReg, rlSrc.highReg, size,
1603 INVALID_SREG);
Ian Rogersb41b33b2012-03-20 14:22:54 -07001604#else
buzbee408ad162012-06-06 16:45:18 -07001605 bool needsRangeCheck = (!(optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -07001606 int regLen = INVALID_REG;
1607 if (needsRangeCheck) {
1608 regLen = oatAllocTemp(cUnit);
1609 //NOTE: max live temps(4) here.
1610 /* Get len */
1611 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1612 }
1613 /* regPtr -> array data */
1614 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1615 /* at this point, regPtr points to array, 2 live temps */
1616 if ((size == kLong) || (size == kDouble)) {
1617 //TUNING: specific wide routine that can handle fp regs
1618 if (scale) {
1619 int rNewIndex = oatAllocTemp(cUnit);
1620 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1621 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1622 oatFreeTemp(cUnit, rNewIndex);
buzbee31a4a6f2012-02-28 15:36:15 -08001623 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001624 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001625 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001626 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1627
1628 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001629 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001630 oatFreeTemp(cUnit, regLen);
1631 }
1632
jeffhao41005dd2012-05-09 17:58:52 -07001633 storeBaseDispWide(cUnit, regPtr, 0, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001634
1635 oatFreeTemp(cUnit, regPtr);
1636 } else {
1637 rlSrc = loadValue(cUnit, rlSrc, regClass);
1638 if (needsRangeCheck) {
buzbee408ad162012-06-06 16:45:18 -07001639 genRegRegCheck(cUnit, kCondCs, rlIndex.lowReg, regLen, kThrowArrayBounds);
Bill Buzbeea114add2012-05-03 15:00:40 -07001640 oatFreeTemp(cUnit, regLen);
1641 }
1642 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1643 scale, size);
1644 }
Ian Rogersb41b33b2012-03-20 14:22:54 -07001645#endif
buzbee31a4a6f2012-02-28 15:36:15 -08001646}
1647
buzbee408ad162012-06-06 16:45:18 -07001648void genLong3Addr(CompilationUnit* cUnit, OpKind firstOp,
buzbee31a4a6f2012-02-28 15:36:15 -08001649 OpKind secondOp, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001650 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001651{
Bill Buzbeea114add2012-05-03 15:00:40 -07001652 RegLocation rlResult;
buzbee31a4a6f2012-02-28 15:36:15 -08001653#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001654 /*
1655 * NOTE: This is the one place in the code in which we might have
1656 * as many as six live temporary registers. There are 5 in the normal
1657 * set for Arm. Until we have spill capabilities, temporarily add
1658 * lr to the temp set. It is safe to do this locally, but note that
1659 * lr is used explicitly elsewhere in the code generator and cannot
1660 * normally be used as a general temp register.
1661 */
1662 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
1663 oatFreeTemp(cUnit, rLR); // and make it available
buzbee31a4a6f2012-02-28 15:36:15 -08001664#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001665 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1666 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1667 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1668 // The longs may overlap - use intermediate temp if so
1669 if (rlResult.lowReg == rlSrc1.highReg) {
1670 int tReg = oatAllocTemp(cUnit);
1671 opRegCopy(cUnit, tReg, rlSrc1.highReg);
1672 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1673 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg, rlSrc2.highReg);
1674 oatFreeTemp(cUnit, tReg);
1675 } else {
1676 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1677 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
1678 rlSrc2.highReg);
1679 }
1680 /*
1681 * NOTE: If rlDest refers to a frame variable in a large frame, the
1682 * following storeValueWide might need to allocate a temp register.
1683 * To further work around the lack of a spill capability, explicitly
1684 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
1685 * Remove when spill is functional.
1686 */
1687 freeRegLocTemps(cUnit, rlResult, rlSrc1);
1688 freeRegLocTemps(cUnit, rlResult, rlSrc2);
1689 storeValueWide(cUnit, rlDest, rlResult);
buzbee31a4a6f2012-02-28 15:36:15 -08001690#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001691 oatClobber(cUnit, rLR);
1692 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee31a4a6f2012-02-28 15:36:15 -08001693#endif
1694}
1695
1696
buzbee408ad162012-06-06 16:45:18 -07001697bool genShiftOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
buzbee31a4a6f2012-02-28 15:36:15 -08001698 RegLocation rlSrc1, RegLocation rlShift)
1699{
Bill Buzbeea114add2012-05-03 15:00:40 -07001700 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08001701
buzbee408ad162012-06-06 16:45:18 -07001702 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001703 case Instruction::SHL_LONG:
1704 case Instruction::SHL_LONG_2ADDR:
1705 funcOffset = ENTRYPOINT_OFFSET(pShlLong);
1706 break;
1707 case Instruction::SHR_LONG:
1708 case Instruction::SHR_LONG_2ADDR:
1709 funcOffset = ENTRYPOINT_OFFSET(pShrLong);
1710 break;
1711 case Instruction::USHR_LONG:
1712 case Instruction::USHR_LONG_2ADDR:
1713 funcOffset = ENTRYPOINT_OFFSET(pUshrLong);
1714 break;
1715 default:
1716 LOG(FATAL) << "Unexpected case";
1717 return true;
1718 }
1719 oatFlushAllRegs(cUnit); /* Send everything to home location */
1720 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlShift);
1721 RegLocation rlResult = oatGetReturnWide(cUnit, false);
1722 storeValueWide(cUnit, rlDest, rlResult);
1723 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001724}
1725
1726
buzbee408ad162012-06-06 16:45:18 -07001727bool genArithOpInt(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07001728 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08001729{
Bill Buzbeea114add2012-05-03 15:00:40 -07001730 OpKind op = kOpBkpt;
1731 bool callOut = false;
1732 bool checkZero = false;
1733 bool unary = false;
1734 RegLocation rlResult;
1735 bool shiftOp = false;
1736 int funcOffset;
1737 int retReg = rRET0;
buzbee408ad162012-06-06 16:45:18 -07001738 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001739 case Instruction::NEG_INT:
1740 op = kOpNeg;
1741 unary = true;
1742 break;
1743 case Instruction::NOT_INT:
1744 op = kOpMvn;
1745 unary = true;
1746 break;
1747 case Instruction::ADD_INT:
1748 case Instruction::ADD_INT_2ADDR:
1749 op = kOpAdd;
1750 break;
1751 case Instruction::SUB_INT:
1752 case Instruction::SUB_INT_2ADDR:
1753 op = kOpSub;
1754 break;
1755 case Instruction::MUL_INT:
1756 case Instruction::MUL_INT_2ADDR:
1757 op = kOpMul;
1758 break;
1759 case Instruction::DIV_INT:
1760 case Instruction::DIV_INT_2ADDR:
1761 checkZero = true;
1762 op = kOpDiv;
1763 callOut = true;
1764 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1765 retReg = rRET0;
1766 break;
1767 /* NOTE: returns in rARG1 */
1768 case Instruction::REM_INT:
1769 case Instruction::REM_INT_2ADDR:
1770 checkZero = true;
1771 op = kOpRem;
1772 callOut = true;
1773 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
1774 retReg = rRET1;
1775 break;
1776 case Instruction::AND_INT:
1777 case Instruction::AND_INT_2ADDR:
1778 op = kOpAnd;
1779 break;
1780 case Instruction::OR_INT:
1781 case Instruction::OR_INT_2ADDR:
1782 op = kOpOr;
1783 break;
1784 case Instruction::XOR_INT:
1785 case Instruction::XOR_INT_2ADDR:
1786 op = kOpXor;
1787 break;
1788 case Instruction::SHL_INT:
1789 case Instruction::SHL_INT_2ADDR:
1790 shiftOp = true;
1791 op = kOpLsl;
1792 break;
1793 case Instruction::SHR_INT:
1794 case Instruction::SHR_INT_2ADDR:
1795 shiftOp = true;
1796 op = kOpAsr;
1797 break;
1798 case Instruction::USHR_INT:
1799 case Instruction::USHR_INT_2ADDR:
1800 shiftOp = true;
1801 op = kOpLsr;
1802 break;
1803 default:
1804 LOG(FATAL) << "Invalid word arith op: " <<
buzbee408ad162012-06-06 16:45:18 -07001805 (int)opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001806 }
1807 if (!callOut) {
1808 if (unary) {
1809 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1810 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1811 opRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg);
buzbee31a4a6f2012-02-28 15:36:15 -08001812 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001813 if (shiftOp) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001814#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07001815 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1816 int tReg = oatAllocTemp(cUnit);
1817 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001818#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001819 // X86 doesn't require masking and must use ECX
1820 loadValueDirectFixed(cUnit, rlSrc2, rCX);
1821 int tReg = rCX;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07001822#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001823 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1824 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1825 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, tReg);
1826 oatFreeTemp(cUnit, tReg);
1827 } else {
1828 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1829 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1830 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1831 opRegRegReg(cUnit, op, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1832 }
buzbee31a4a6f2012-02-28 15:36:15 -08001833 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001834 storeValue(cUnit, rlDest, rlResult);
1835 } else {
1836 RegLocation rlResult;
1837 oatFlushAllRegs(cUnit); /* Send everything to home location */
1838 loadValueDirectFixed(cUnit, rlSrc2, rARG1);
1839#if !defined(TARGET_X86)
1840 int rTgt = loadHelper(cUnit, funcOffset);
1841#endif
1842 loadValueDirectFixed(cUnit, rlSrc1, rARG0);
1843 if (checkZero) {
buzbee408ad162012-06-06 16:45:18 -07001844 genImmedCheck(cUnit, kCondEq, rARG1, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001845 }
1846#if !defined(TARGET_X86)
1847 opReg(cUnit, kOpBlx, rTgt);
1848 oatFreeTemp(cUnit, rTgt);
1849#else
1850 opThreadMem(cUnit, kOpBlx, funcOffset);
1851#endif
1852 if (retReg == rRET0)
1853 rlResult = oatGetReturn(cUnit, false);
1854 else
1855 rlResult = oatGetReturnAlt(cUnit);
1856 storeValue(cUnit, rlDest, rlResult);
1857 }
1858 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001859}
1860
1861/*
1862 * The following are the first-level codegen routines that analyze the format
1863 * of each bytecode then either dispatch special purpose codegen routines
1864 * or produce corresponding Thumb instructions directly.
1865 */
1866
1867bool isPowerOfTwo(int x)
1868{
Bill Buzbeea114add2012-05-03 15:00:40 -07001869 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001870}
1871
1872// Returns true if no more than two bits are set in 'x'.
1873bool isPopCountLE2(unsigned int x)
1874{
Bill Buzbeea114add2012-05-03 15:00:40 -07001875 x &= x - 1;
1876 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001877}
1878
1879// Returns the index of the lowest set bit in 'x'.
1880int lowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001881 int bit_posn = 0;
1882 while ((x & 0xf) == 0) {
1883 bit_posn += 4;
1884 x >>= 4;
1885 }
1886 while ((x & 1) == 0) {
1887 bit_posn++;
1888 x >>= 1;
1889 }
1890 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001891}
1892
1893// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1894// and store the result in 'rlDest'.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001895bool handleEasyDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode,
Bill Buzbeea114add2012-05-03 15:00:40 -07001896 RegLocation rlSrc, RegLocation rlDest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001897{
buzbeef3aac972012-04-11 16:33:36 -07001898#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001899 // No divide instruction for Arm, so check for more special cases
1900 if (lit < 2) {
1901 return false;
1902 }
1903 if (!isPowerOfTwo(lit)) {
1904 return smallLiteralDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit);
1905 }
buzbeef3aac972012-04-11 16:33:36 -07001906#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001907 if (lit < 2 || !isPowerOfTwo(lit)) {
1908 return false;
1909 }
buzbeef3aac972012-04-11 16:33:36 -07001910#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001911 int k = lowestSetBit(lit);
1912 if (k >= 30) {
1913 // Avoid special cases.
1914 return false;
1915 }
1916 bool div = (dalvikOpcode == Instruction::DIV_INT_LIT8 ||
1917 dalvikOpcode == Instruction::DIV_INT_LIT16);
1918 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1919 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1920 if (div) {
1921 int tReg = oatAllocTemp(cUnit);
1922 if (lit == 2) {
1923 // Division by 2 is by far the most common division by constant.
1924 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1925 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1926 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001927 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07001928 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1929 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1930 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1931 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001932 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001933 } else {
1934 int tReg1 = oatAllocTemp(cUnit);
1935 int tReg2 = oatAllocTemp(cUnit);
1936 if (lit == 2) {
1937 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1938 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1939 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit -1);
1940 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1941 } else {
1942 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1943 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1944 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1945 opRegRegImm(cUnit, kOpAnd, tReg2, tReg2, lit - 1);
1946 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1947 }
1948 }
1949 storeValue(cUnit, rlDest, rlResult);
1950 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001951}
1952
1953void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc,
1954 RegLocation rlResult, int lit,
1955 int firstBit, int secondBit)
1956{
buzbee0398c422012-03-02 15:22:47 -08001957#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07001958 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1959 encodeShift(kArmLsl, secondBit - firstBit));
buzbee0398c422012-03-02 15:22:47 -08001960#else
Bill Buzbeea114add2012-05-03 15:00:40 -07001961 int tReg = oatAllocTemp(cUnit);
1962 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit);
1963 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg);
1964 oatFreeTemp(cUnit, tReg);
buzbee5de34942012-03-01 14:51:57 -08001965#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07001966 if (firstBit != 0) {
1967 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1968 }
buzbee31a4a6f2012-02-28 15:36:15 -08001969}
1970
1971// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1972// and store the result in 'rlDest'.
1973bool handleEasyMultiply(CompilationUnit* cUnit, RegLocation rlSrc,
1974 RegLocation rlDest, int lit)
1975{
Bill Buzbeea114add2012-05-03 15:00:40 -07001976 // Can we simplify this multiplication?
1977 bool powerOfTwo = false;
1978 bool popCountLE2 = false;
1979 bool powerOfTwoMinusOne = false;
1980 if (lit < 2) {
1981 // Avoid special cases.
1982 return false;
1983 } else if (isPowerOfTwo(lit)) {
1984 powerOfTwo = true;
1985 } else if (isPopCountLE2(lit)) {
1986 popCountLE2 = true;
1987 } else if (isPowerOfTwo(lit + 1)) {
1988 powerOfTwoMinusOne = true;
1989 } else {
1990 return false;
1991 }
1992 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1993 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1994 if (powerOfTwo) {
1995 // Shift.
1996 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1997 lowestSetBit(lit));
1998 } else if (popCountLE2) {
1999 // Shift and add and shift.
2000 int firstBit = lowestSetBit(lit);
2001 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
2002 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
2003 firstBit, secondBit);
2004 } else {
2005 // Reverse subtract: (src << (shift + 1)) - src.
2006 DCHECK(powerOfTwoMinusOne);
2007 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
2008 int tReg = oatAllocTemp(cUnit);
2009 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
2010 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2011 }
2012 storeValue(cUnit, rlDest, rlResult);
2013 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08002014}
2015
buzbee408ad162012-06-06 16:45:18 -07002016bool genArithOpIntLit(CompilationUnit* cUnit, Instruction::Code opcode,
2017 RegLocation rlDest, RegLocation rlSrc, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08002018{
Bill Buzbeea114add2012-05-03 15:00:40 -07002019 RegLocation rlResult;
2020 OpKind op = (OpKind)0; /* Make gcc happy */
2021 int shiftOp = false;
2022 bool isDiv = false;
2023 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002024
buzbee408ad162012-06-06 16:45:18 -07002025 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002026 case Instruction::RSUB_INT_LIT8:
2027 case Instruction::RSUB_INT: {
2028 int tReg;
2029 //TUNING: add support for use of Arm rsub op
2030 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2031 tReg = oatAllocTemp(cUnit);
2032 loadConstant(cUnit, tReg, lit);
2033 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2034 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
2035 storeValue(cUnit, rlDest, rlResult);
2036 return false;
2037 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002038 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002039
2040 case Instruction::ADD_INT_LIT8:
2041 case Instruction::ADD_INT_LIT16:
2042 op = kOpAdd;
2043 break;
2044 case Instruction::MUL_INT_LIT8:
2045 case Instruction::MUL_INT_LIT16: {
2046 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
2047 return false;
2048 }
2049 op = kOpMul;
2050 break;
buzbee31a4a6f2012-02-28 15:36:15 -08002051 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002052 case Instruction::AND_INT_LIT8:
2053 case Instruction::AND_INT_LIT16:
2054 op = kOpAnd;
2055 break;
2056 case Instruction::OR_INT_LIT8:
2057 case Instruction::OR_INT_LIT16:
2058 op = kOpOr;
2059 break;
2060 case Instruction::XOR_INT_LIT8:
2061 case Instruction::XOR_INT_LIT16:
2062 op = kOpXor;
2063 break;
2064 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002065 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002066 lit &= 31;
2067 shiftOp = true;
2068 op = kOpLsl;
2069 break;
2070 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002071 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002072 lit &= 31;
2073 shiftOp = true;
2074 op = kOpAsr;
2075 break;
2076 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07002077 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -07002078 lit &= 31;
2079 shiftOp = true;
2080 op = kOpLsr;
2081 break;
2082
2083 case Instruction::DIV_INT_LIT8:
2084 case Instruction::DIV_INT_LIT16:
2085 case Instruction::REM_INT_LIT8:
2086 case Instruction::REM_INT_LIT16:
2087 if (lit == 0) {
buzbee408ad162012-06-06 16:45:18 -07002088 genImmedCheck(cUnit, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002089 return false;
2090 }
buzbee408ad162012-06-06 16:45:18 -07002091 if (handleEasyDivide(cUnit, opcode, rlSrc, rlDest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002092 return false;
2093 }
2094 oatFlushAllRegs(cUnit); /* Everything to home location */
2095 loadValueDirectFixed(cUnit, rlSrc, rARG0);
2096 oatClobber(cUnit, rARG0);
2097 funcOffset = ENTRYPOINT_OFFSET(pIdivmod);
buzbee408ad162012-06-06 16:45:18 -07002098 if ((opcode == Instruction::DIV_INT_LIT8) ||
2099 (opcode == Instruction::DIV_INT_LIT16)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002100 isDiv = true;
2101 } else {
2102 isDiv = false;
2103 }
2104 callRuntimeHelperRegImm(cUnit, funcOffset, rARG0, lit);
2105 if (isDiv)
2106 rlResult = oatGetReturn(cUnit, false);
2107 else
2108 rlResult = oatGetReturnAlt(cUnit);
2109 storeValue(cUnit, rlDest, rlResult);
2110 return false;
2111 break;
2112 default:
2113 return true;
2114 }
2115 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2116 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2117 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2118 if (shiftOp && (lit == 0)) {
2119 opRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2120 } else {
2121 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2122 }
2123 storeValue(cUnit, rlDest, rlResult);
2124 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002125}
2126
buzbee408ad162012-06-06 16:45:18 -07002127bool genArithOpLong(CompilationUnit* cUnit, Instruction::Code opcode, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -07002128 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee31a4a6f2012-02-28 15:36:15 -08002129{
Bill Buzbeea114add2012-05-03 15:00:40 -07002130 RegLocation rlResult;
2131 OpKind firstOp = kOpBkpt;
2132 OpKind secondOp = kOpBkpt;
2133 bool callOut = false;
2134 bool checkZero = false;
2135 int funcOffset;
2136 int retReg = rRET0;
buzbee31a4a6f2012-02-28 15:36:15 -08002137
buzbee408ad162012-06-06 16:45:18 -07002138 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002139 case Instruction::NOT_LONG:
2140 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
2141 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2142 // Check for destructive overlap
2143 if (rlResult.lowReg == rlSrc2.highReg) {
2144 int tReg = oatAllocTemp(cUnit);
2145 opRegCopy(cUnit, tReg, rlSrc2.highReg);
2146 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2147 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
2148 oatFreeTemp(cUnit, tReg);
2149 } else {
2150 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
2151 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
2152 }
2153 storeValueWide(cUnit, rlDest, rlResult);
2154 return false;
2155 break;
2156 case Instruction::ADD_LONG:
2157 case Instruction::ADD_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002158#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002159 return genAddLong(cUnit, rlDest, rlSrc1, rlSrc2);
buzbeec5159d52012-03-03 11:48:39 -08002160#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002161 firstOp = kOpAdd;
2162 secondOp = kOpAdc;
2163 break;
buzbeec5159d52012-03-03 11:48:39 -08002164#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002165 case Instruction::SUB_LONG:
2166 case Instruction::SUB_LONG_2ADDR:
Ian Rogers7caad772012-03-30 01:07:54 -07002167#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002168 return genSubLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002169#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002170 firstOp = kOpSub;
2171 secondOp = kOpSbc;
2172 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002173#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002174 case Instruction::MUL_LONG:
2175 case Instruction::MUL_LONG_2ADDR:
2176 callOut = true;
2177 retReg = rRET0;
2178 funcOffset = ENTRYPOINT_OFFSET(pLmul);
2179 break;
2180 case Instruction::DIV_LONG:
2181 case Instruction::DIV_LONG_2ADDR:
2182 callOut = true;
2183 checkZero = true;
2184 retReg = rRET0;
jeffhao644d5312012-05-03 19:04:49 -07002185 funcOffset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07002186 break;
2187 case Instruction::REM_LONG:
2188 case Instruction::REM_LONG_2ADDR:
2189 callOut = true;
2190 checkZero = true;
jeffhao644d5312012-05-03 19:04:49 -07002191 funcOffset = ENTRYPOINT_OFFSET(pLdivmod);
Ian Rogers55bd45f2012-04-04 17:31:20 -07002192#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002193 /* NOTE - result is in rARG2/rARG3 instead of rRET0/rRET1 */
2194 retReg = rARG2;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002195#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002196 retReg = rRET0;
Ian Rogers55bd45f2012-04-04 17:31:20 -07002197#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002198 break;
2199 case Instruction::AND_LONG_2ADDR:
2200 case Instruction::AND_LONG:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002201#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002202 return genAndLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002203#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002204 firstOp = kOpAnd;
2205 secondOp = kOpAnd;
2206 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002207#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002208 case Instruction::OR_LONG:
2209 case Instruction::OR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002210#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002211 return genOrLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002212#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002213 firstOp = kOpOr;
2214 secondOp = kOpOr;
2215 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002216#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002217 case Instruction::XOR_LONG:
2218 case Instruction::XOR_LONG_2ADDR:
Ian Rogersc6f3bb82012-03-21 20:40:33 -07002219#if defined(TARGET_X86)
buzbee408ad162012-06-06 16:45:18 -07002220 return genXorLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ian Rogers7caad772012-03-30 01:07:54 -07002221#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002222 firstOp = kOpXor;
2223 secondOp = kOpXor;
2224 break;
Ian Rogers7caad772012-03-30 01:07:54 -07002225#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002226 case Instruction::NEG_LONG: {
buzbee408ad162012-06-06 16:45:18 -07002227 return genNegLong(cUnit, rlDest, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002228 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002229 default:
2230 LOG(FATAL) << "Invalid long arith op";
2231 }
2232 if (!callOut) {
buzbee408ad162012-06-06 16:45:18 -07002233 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Bill Buzbeea114add2012-05-03 15:00:40 -07002234 } else {
2235 oatFlushAllRegs(cUnit); /* Send everything to home location */
2236 if (checkZero) {
2237 loadValueDirectWideFixed(cUnit, rlSrc2, rARG2, rARG3);
2238#if !defined(TARGET_X86)
2239 int rTgt = loadHelper(cUnit, funcOffset);
2240#endif
2241 int tReg = oatAllocTemp(cUnit);
2242#if defined(TARGET_ARM)
2243 newLIR4(cUnit, kThumb2OrrRRRs, tReg, rARG2, rARG3, 0);
2244 oatFreeTemp(cUnit, tReg);
buzbee408ad162012-06-06 16:45:18 -07002245 genCheck(cUnit, kCondEq, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002246#else
2247 opRegRegReg(cUnit, kOpOr, tReg, rARG2, rARG3);
2248#endif
buzbee408ad162012-06-06 16:45:18 -07002249 genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07002250 oatFreeTemp(cUnit, tReg);
2251 loadValueDirectWideFixed(cUnit, rlSrc1, rARG0, rARG1);
2252#if !defined(TARGET_X86)
2253 opReg(cUnit, kOpBlx, rTgt);
2254 oatFreeTemp(cUnit, rTgt);
2255#else
2256 opThreadMem(cUnit, kOpBlx, funcOffset);
2257#endif
buzbee31a4a6f2012-02-28 15:36:15 -08002258 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -07002259 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset,
2260 rlSrc1, rlSrc2);
buzbee31a4a6f2012-02-28 15:36:15 -08002261 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002262 // Adjust return regs in to handle case of rem returning rARG2/rARG3
2263 if (retReg == rRET0)
2264 rlResult = oatGetReturnWide(cUnit, false);
2265 else
2266 rlResult = oatGetReturnWideAlt(cUnit);
2267 storeValueWide(cUnit, rlDest, rlResult);
2268 }
2269 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002270}
2271
buzbee408ad162012-06-06 16:45:18 -07002272bool genConversionCall(CompilationUnit* cUnit, int funcOffset,
2273 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002274{
Bill Buzbeea114add2012-05-03 15:00:40 -07002275 /*
2276 * Don't optimize the register usage since it calls out to support
2277 * functions
2278 */
Bill Buzbeea114add2012-05-03 15:00:40 -07002279 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee408ad162012-06-06 16:45:18 -07002280 if (rlSrc.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002281 loadValueDirectWideFixed(cUnit, rlSrc, rARG0, rARG1);
buzbee408ad162012-06-06 16:45:18 -07002282 } else {
2283 loadValueDirectFixed(cUnit, rlSrc, rARG0);
Bill Buzbeea114add2012-05-03 15:00:40 -07002284 }
2285 callRuntimeHelperRegLocation(cUnit, funcOffset, rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002286 if (rlDest.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002287 RegLocation rlResult;
Bill Buzbeea114add2012-05-03 15:00:40 -07002288 rlResult = oatGetReturnWide(cUnit, rlDest.fp);
2289 storeValueWide(cUnit, rlDest, rlResult);
buzbee408ad162012-06-06 16:45:18 -07002290 } else {
2291 RegLocation rlResult;
2292 rlResult = oatGetReturn(cUnit, rlDest.fp);
2293 storeValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -07002294 }
2295 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002296}
2297
2298void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002299bool genArithOpFloatPortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002300 RegLocation rlDest, RegLocation rlSrc1,
2301 RegLocation rlSrc2)
2302{
Bill Buzbeea114add2012-05-03 15:00:40 -07002303 RegLocation rlResult;
2304 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002305
buzbee408ad162012-06-06 16:45:18 -07002306 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002307 case Instruction::ADD_FLOAT_2ADDR:
2308 case Instruction::ADD_FLOAT:
2309 funcOffset = ENTRYPOINT_OFFSET(pFadd);
2310 break;
2311 case Instruction::SUB_FLOAT_2ADDR:
2312 case Instruction::SUB_FLOAT:
2313 funcOffset = ENTRYPOINT_OFFSET(pFsub);
2314 break;
2315 case Instruction::DIV_FLOAT_2ADDR:
2316 case Instruction::DIV_FLOAT:
2317 funcOffset = ENTRYPOINT_OFFSET(pFdiv);
2318 break;
2319 case Instruction::MUL_FLOAT_2ADDR:
2320 case Instruction::MUL_FLOAT:
2321 funcOffset = ENTRYPOINT_OFFSET(pFmul);
2322 break;
2323 case Instruction::REM_FLOAT_2ADDR:
2324 case Instruction::REM_FLOAT:
2325 funcOffset = ENTRYPOINT_OFFSET(pFmodf);
2326 break;
2327 case Instruction::NEG_FLOAT: {
2328 genNegFloat(cUnit, rlDest, rlSrc1);
2329 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002330 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002331 default:
2332 return true;
2333 }
2334 oatFlushAllRegs(cUnit); /* Send everything to home location */
2335 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2336 rlResult = oatGetReturn(cUnit, true);
2337 storeValue(cUnit, rlDest, rlResult);
2338 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002339}
2340
2341void genNegDouble(CompilationUnit* cUnit, RegLocation rlDst, RegLocation rlSrc);
buzbee408ad162012-06-06 16:45:18 -07002342bool genArithOpDoublePortable(CompilationUnit* cUnit, Instruction::Code opcode,
buzbee31a4a6f2012-02-28 15:36:15 -08002343 RegLocation rlDest, RegLocation rlSrc1,
2344 RegLocation rlSrc2)
2345{
Bill Buzbeea114add2012-05-03 15:00:40 -07002346 RegLocation rlResult;
2347 int funcOffset;
buzbee31a4a6f2012-02-28 15:36:15 -08002348
buzbee408ad162012-06-06 16:45:18 -07002349 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002350 case Instruction::ADD_DOUBLE_2ADDR:
2351 case Instruction::ADD_DOUBLE:
2352 funcOffset = ENTRYPOINT_OFFSET(pDadd);
2353 break;
2354 case Instruction::SUB_DOUBLE_2ADDR:
2355 case Instruction::SUB_DOUBLE:
2356 funcOffset = ENTRYPOINT_OFFSET(pDsub);
2357 break;
2358 case Instruction::DIV_DOUBLE_2ADDR:
2359 case Instruction::DIV_DOUBLE:
2360 funcOffset = ENTRYPOINT_OFFSET(pDdiv);
2361 break;
2362 case Instruction::MUL_DOUBLE_2ADDR:
2363 case Instruction::MUL_DOUBLE:
2364 funcOffset = ENTRYPOINT_OFFSET(pDmul);
2365 break;
2366 case Instruction::REM_DOUBLE_2ADDR:
2367 case Instruction::REM_DOUBLE:
2368 funcOffset = ENTRYPOINT_OFFSET(pFmod);
2369 break;
2370 case Instruction::NEG_DOUBLE: {
2371 genNegDouble(cUnit, rlDest, rlSrc1);
2372 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002373 }
Bill Buzbeea114add2012-05-03 15:00:40 -07002374 default:
2375 return true;
2376 }
2377 oatFlushAllRegs(cUnit); /* Send everything to home location */
2378 callRuntimeHelperRegLocationRegLocation(cUnit, funcOffset, rlSrc1, rlSrc2);
2379 rlResult = oatGetReturnWide(cUnit, true);
2380 storeValueWide(cUnit, rlDest, rlResult);
2381 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002382}
2383
buzbee408ad162012-06-06 16:45:18 -07002384bool genConversionPortable(CompilationUnit* cUnit, Instruction::Code opcode,
2385 RegLocation rlDest, RegLocation rlSrc)
buzbee31a4a6f2012-02-28 15:36:15 -08002386{
buzbee31a4a6f2012-02-28 15:36:15 -08002387
Bill Buzbeea114add2012-05-03 15:00:40 -07002388 switch (opcode) {
2389 case Instruction::INT_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002390 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2f),
2391 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002392 case Instruction::FLOAT_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002393 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2iz),
2394 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002395 case Instruction::DOUBLE_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002396 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2f),
2397 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002398 case Instruction::FLOAT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002399 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2d),
2400 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002401 case Instruction::INT_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002402 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pI2d),
2403 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002404 case Instruction::DOUBLE_TO_INT:
buzbee408ad162012-06-06 16:45:18 -07002405 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2iz),
2406 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002407 case Instruction::FLOAT_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002408 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pF2l),
2409 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002410 case Instruction::LONG_TO_FLOAT:
buzbee408ad162012-06-06 16:45:18 -07002411 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2f),
2412 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002413 case Instruction::DOUBLE_TO_LONG:
buzbee408ad162012-06-06 16:45:18 -07002414 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pD2l),
2415 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002416 case Instruction::LONG_TO_DOUBLE:
buzbee408ad162012-06-06 16:45:18 -07002417 return genConversionCall(cUnit, ENTRYPOINT_OFFSET(pL2d),
2418 rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -07002419 default:
2420 return true;
2421 }
2422 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08002423}
2424
2425/*
2426 * Generate callout to updateDebugger. Note that we're overloading
2427 * the use of rSUSPEND here. When the debugger is active, this
2428 * register holds the address of the update function. So, if it's
2429 * non-null, we call out to it.
2430 *
2431 * Note also that rRET0 and rRET1 must be preserved across this
2432 * code. This must be handled by the stub.
2433 */
2434void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset)
2435{
Bill Buzbeea114add2012-05-03 15:00:40 -07002436 // Following DCHECK verifies that dPC is in range of single load immediate
2437 DCHECK((offset == DEBUGGER_METHOD_ENTRY) ||
2438 (offset == DEBUGGER_METHOD_EXIT) || ((offset & 0xffff) == offset));
2439 oatClobberCalleeSave(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -08002440#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002441 opRegImm(cUnit, kOpCmp, rSUSPEND, 0);
2442 opIT(cUnit, kArmCondNe, "T");
2443 loadConstant(cUnit, rARG2, offset); // arg2 <- Entry code
2444 opReg(cUnit, kOpBlx, rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002445#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002446 UNIMPLEMENTED(FATAL);
buzbee31a4a6f2012-02-28 15:36:15 -08002447#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002448 LIR* branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
2449 loadConstant(cUnit, rARG2, offset);
2450 opReg(cUnit, kOpBlx, rSUSPEND);
2451 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
2452 branch->target = (LIR*)target;
buzbee31a4a6f2012-02-28 15:36:15 -08002453#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002454 oatFreeTemp(cUnit, rARG2);
buzbee31a4a6f2012-02-28 15:36:15 -08002455}
2456
2457/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002458void genSuspendTest(CompilationUnit* cUnit, int optFlags)
buzbee31a4a6f2012-02-28 15:36:15 -08002459{
buzbee408ad162012-06-06 16:45:18 -07002460 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002461 return;
2462 }
2463 oatFlushAllRegs(cUnit);
2464 if (cUnit->genDebugger) {
2465 // If generating code for the debugger, always check for suspension
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002466#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002467 UNIMPLEMENTED(FATAL);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -07002468#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002469 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pTestSuspendFromCode));
2470 opReg(cUnit, kOpBlx, rTgt);
2471 // Refresh rSUSPEND
2472 loadWordDisp(cUnit, rSELF,
2473 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode),
2474 rSUSPEND);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002475#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002476 } else {
2477 LIR* branch = NULL;
buzbee31a4a6f2012-02-28 15:36:15 -08002478#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002479 // In non-debug case, only check periodically
2480 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2481 branch = opCondBranch(cUnit, kCondEq, NULL);
Ian Rogersb5d09b22012-03-06 22:14:17 -08002482#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002483 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2484 branch = opCondBranch(cUnit, kCondNe, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002485#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002486 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2487 branch = opCmpImmBranch(cUnit, kCondEq, rSUSPEND, 0, NULL);
buzbee31a4a6f2012-02-28 15:36:15 -08002488#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002489 LIR* retLab = newLIR0(cUnit, kPseudoTargetLabel);
2490 LIR* target = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002491 kPseudoSuspendTarget, (intptr_t)retLab, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002492 branch->target = (LIR*)target;
2493 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads, (intptr_t)target);
2494 }
buzbee31a4a6f2012-02-28 15:36:15 -08002495}
2496
buzbeefead2932012-03-30 14:02:01 -07002497/* Check if we need to check for pending suspend request */
buzbee408ad162012-06-06 16:45:18 -07002498void genSuspendTestAndBranch(CompilationUnit* cUnit, int optFlags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07002499{
buzbee408ad162012-06-06 16:45:18 -07002500 if (NO_SUSPEND || (optFlags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07002501 opUnconditionalBranch(cUnit, target);
2502 return;
2503 }
2504 if (cUnit->genDebugger) {
buzbee408ad162012-06-06 16:45:18 -07002505 genSuspendTest(cUnit, optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -07002506 opUnconditionalBranch(cUnit, target);
2507 } else {
buzbeefead2932012-03-30 14:02:01 -07002508#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -07002509 // In non-debug case, only check periodically
2510 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
2511 opCondBranch(cUnit, kCondNe, target);
buzbeefead2932012-03-30 14:02:01 -07002512#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -07002513 newLIR2(cUnit, kX86Cmp32TI8, Thread::SuspendCountOffset().Int32Value(), 0);
2514 opCondBranch(cUnit, kCondEq, target);
buzbeefead2932012-03-30 14:02:01 -07002515#else
Bill Buzbeea114add2012-05-03 15:00:40 -07002516 opRegImm(cUnit, kOpSub, rSUSPEND, 1);
2517 opCmpImmBranch(cUnit, kCondNe, rSUSPEND, 0, target);
buzbeefead2932012-03-30 14:02:01 -07002518#endif
Bill Buzbeea114add2012-05-03 15:00:40 -07002519 LIR* launchPad = rawLIR(cUnit, cUnit->currentDalvikOffset,
buzbee408ad162012-06-06 16:45:18 -07002520 kPseudoSuspendTarget, (intptr_t)target, cUnit->currentDalvikOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -07002521 oatFlushAllRegs(cUnit);
2522 opUnconditionalBranch(cUnit, launchPad);
2523 oatInsertGrowableList(cUnit, &cUnit->suspendLaunchpads,
2524 (intptr_t)launchPad);
2525 }
buzbeefead2932012-03-30 14:02:01 -07002526}
2527
buzbee31a4a6f2012-02-28 15:36:15 -08002528} // namespace art