blob: cbf58677eaa67d74c4b943111ac4aa7b7c8b6dfb [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 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 Rogers6d4d9fc2011-11-30 16:24:48 -080017#include "object_utils.h"
18
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080019namespace art {
20
buzbeece302932011-10-04 14:32:18 -070021#define DISPLAY_MISSING_TARGETS (cUnit->enableDebug & \
22 (1 << kDebugDisplayMissingTargets))
Elliott Hughes1240dad2011-09-09 16:24:50 -070023
buzbee67bc2362011-10-11 18:08:40 -070024STATIC const RegLocation badLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, INVALID_REG,
25 INVALID_REG, INVALID_SREG};
buzbee6181f792011-09-29 11:14:04 -070026
27/* Mark register usage state and return long retloc */
28STATIC RegLocation getRetLocWide(CompilationUnit* cUnit)
29{
30 RegLocation res = LOC_DALVIK_RETURN_VAL_WIDE;
31 oatLockTemp(cUnit, res.lowReg);
32 oatLockTemp(cUnit, res.highReg);
33 oatMarkPair(cUnit, res.lowReg, res.highReg);
34 return res;
35}
36
37STATIC RegLocation getRetLoc(CompilationUnit* cUnit)
38{
39 RegLocation res = LOC_DALVIK_RETURN_VAL;
40 oatLockTemp(cUnit, res.lowReg);
41 return res;
42}
buzbee67bf8852011-08-17 17:51:35 -070043
buzbeedfd3d702011-08-28 12:56:51 -070044/*
45 * Let helper function take care of everything. Will call
46 * Array::AllocFromCode(type_idx, method, count);
47 * Note: AllocFromCode will handle checks for errNegativeArraySize.
48 */
buzbeeed3e9302011-09-23 17:34:19 -070049STATIC void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -070050 RegLocation rlSrc)
51{
buzbeedfd3d702011-08-28 12:56:51 -070052 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers28ad40d2011-10-27 15:19:26 -070053 uint32_t type_idx = mir->dalvikInsn.vC;
Ian Rogersa3760aa2011-11-14 14:32:37 -080054 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
55 cUnit->dex_cache,
56 *cUnit->dex_file,
57 type_idx)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -070058 loadWordDisp(cUnit, rSELF,
59 OFFSETOF_MEMBER(Thread, pAllocArrayFromCode), rLR);
60 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -070061 loadWordDisp(cUnit, rSELF,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -080062 OFFSETOF_MEMBER(Thread, pAllocArrayFromCodeWithAccessCheck), rLR);
Ian Rogers28ad40d2011-10-27 15:19:26 -070063 }
buzbeedfd3d702011-08-28 12:56:51 -070064 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
Ian Rogers28ad40d2011-10-27 15:19:26 -070065 loadConstant(cUnit, r0, type_idx); // arg0 <- type_id
buzbeedfd3d702011-08-28 12:56:51 -070066 loadValueDirectFixed(cUnit, rlSrc, r2); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070067 callRuntimeHelper(cUnit, rLR);
buzbeedfd3d702011-08-28 12:56:51 -070068 RegLocation rlResult = oatGetReturn(cUnit);
69 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -070070}
71
72/*
73 * Similar to genNewArray, but with post-allocation initialization.
74 * Verifier guarantees we're dealing with an array class. Current
75 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
76 * Current code also throws internal unimp if not 'L', '[' or 'I'.
77 */
buzbeeed3e9302011-09-23 17:34:19 -070078STATIC void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
buzbee67bf8852011-08-17 17:51:35 -070079{
80 DecodedInstruction* dInsn = &mir->dalvikInsn;
buzbee81eccc02011-09-17 13:42:21 -070081 int elems = dInsn->vA;
82 int typeId = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070083 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers0eb7d7e2012-01-31 21:12:32 -080084 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
85 cUnit->dex_cache,
86 *cUnit->dex_file,
87 typeId)) {
88 loadWordDisp(cUnit, rSELF,
89 OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode), rLR);
90 } else {
91 loadWordDisp(cUnit, rSELF,
92 OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCodeWithAccessCheck), rLR);
Ian Rogers28ad40d2011-10-27 15:19:26 -070093 }
buzbeedfd3d702011-08-28 12:56:51 -070094 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
95 loadConstant(cUnit, r0, typeId); // arg0 <- type_id
96 loadConstant(cUnit, r2, elems); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070097 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -070098 /*
buzbeedfd3d702011-08-28 12:56:51 -070099 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
100 * return region. Because AllocFromCode placed the new array
101 * in r0, we'll just lock it into place. When debugger support is
102 * added, it may be necessary to additionally copy all return
103 * values to a home location in thread-local storage
buzbee67bf8852011-08-17 17:51:35 -0700104 */
buzbee67bf8852011-08-17 17:51:35 -0700105 oatLockTemp(cUnit, r0);
buzbeedfd3d702011-08-28 12:56:51 -0700106
buzbee67bf8852011-08-17 17:51:35 -0700107 // Having a range of 0 is legal
108 if (isRange && (dInsn->vA > 0)) {
109 /*
110 * Bit of ugliness here. We're going generate a mem copy loop
111 * on the register range, but it is possible that some regs
112 * in the range have been promoted. This is unlikely, but
113 * before generating the copy, we'll just force a flush
114 * of any regs in the source range that have been promoted to
115 * home location.
116 */
117 for (unsigned int i = 0; i < dInsn->vA; i++) {
118 RegLocation loc = oatUpdateLoc(cUnit,
119 oatGetSrc(cUnit, mir, i));
120 if (loc.location == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -0700121 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
122 loc.lowReg, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700123 }
124 }
125 /*
126 * TUNING note: generated code here could be much improved, but
127 * this is an uncommon operation and isn't especially performance
128 * critical.
129 */
130 int rSrc = oatAllocTemp(cUnit);
131 int rDst = oatAllocTemp(cUnit);
132 int rIdx = oatAllocTemp(cUnit);
133 int rVal = rLR; // Using a lot of temps, rLR is known free here
134 // Set up source pointer
135 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
buzbee67bc2362011-10-11 18:08:40 -0700136 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
137 oatSRegOffset(cUnit, rlFirst.sRegLow));
buzbee67bf8852011-08-17 17:51:35 -0700138 // Set up the target pointer
139 opRegRegImm(cUnit, kOpAdd, rDst, r0,
buzbeec143c552011-08-20 17:38:58 -0700140 Array::DataOffset().Int32Value());
buzbee67bf8852011-08-17 17:51:35 -0700141 // Set up the loop counter (known to be > 0)
buzbee31813452011-10-16 14:33:08 -0700142 loadConstant(cUnit, rIdx, dInsn->vA - 1);
buzbee67bf8852011-08-17 17:51:35 -0700143 // Generate the copy loop. Going backwards for convenience
144 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
145 target->defMask = ENCODE_ALL;
146 // Copy next element
147 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
148 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
149 // Use setflags encoding here
150 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee31813452011-10-16 14:33:08 -0700151 ArmLIR* branch = opCondBranch(cUnit, kArmCondGe);
buzbee67bf8852011-08-17 17:51:35 -0700152 branch->generic.target = (LIR*)target;
153 } else if (!isRange) {
154 // TUNING: interleave
155 for (unsigned int i = 0; i < dInsn->vA; i++) {
156 RegLocation rlArg = loadValue(cUnit,
157 oatGetSrc(cUnit, mir, i), kCoreReg);
buzbeec143c552011-08-20 17:38:58 -0700158 storeBaseDisp(cUnit, r0,
159 Array::DataOffset().Int32Value() +
buzbee67bf8852011-08-17 17:51:35 -0700160 i * 4, rlArg.lowReg, kWord);
161 // If the loadValue caused a temp to be allocated, free it
162 if (oatIsTemp(cUnit, rlArg.lowReg)) {
163 oatFreeTemp(cUnit, rlArg.lowReg);
164 }
165 }
166 }
167}
168
Ian Rogers1bddec32012-02-04 12:27:34 -0800169STATIC void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
170 bool isLongOrDouble, bool isObject)
buzbee67bf8852011-08-17 17:51:35 -0700171{
Ian Rogers1bddec32012-02-04 12:27:34 -0800172 int fieldOffset;
173 int ssbIndex;
174 bool isVolatile;
175 bool isReferrersClass;
176 uint32_t fieldIdx = mir->dalvikInsn.vB;
177 bool fastPath =
178 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, cUnit,
179 fieldOffset, ssbIndex,
180 isReferrersClass, isVolatile);
181 if (fastPath && !SLOW_FIELD_PATH) {
182 DCHECK_GE(fieldOffset, 0);
183 int rBase;
184 int rMethod;
185 if (isReferrersClass) {
186 // Fast path, static storage base is this method's class
187 rMethod = loadCurrMethod(cUnit);
188 rBase = oatAllocTemp(cUnit);
189 loadWordDisp(cUnit, rMethod,
190 Method::DeclaringClassOffset().Int32Value(), rBase);
191 } else {
192 // Medium path, static storage base in a different class which
193 // requires checks that the other class is initialized.
194 DCHECK_GE(ssbIndex, 0);
195 // May do runtime call so everything to home locations.
196 oatFlushAllRegs(cUnit);
197 // Using fixed register to sync with possible call to runtime
198 // support.
199 rMethod = r1;
200 oatLockTemp(cUnit, rMethod);
201 loadCurrMethodDirect(cUnit, rMethod);
202 rBase = r0;
203 oatLockTemp(cUnit, rBase);
204 loadWordDisp(cUnit, rMethod,
205 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
206 rBase);
207 loadWordDisp(cUnit, rBase,
208 Array::DataOffset().Int32Value() + sizeof(int32_t*) * ssbIndex,
209 rBase);
210 // rBase now points at appropriate static storage base (Class*)
211 // or NULL if not initialized. Check for NULL and call helper if NULL.
212 // TUNING: fast path should fall through
213 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
214 loadWordDisp(cUnit, rSELF,
215 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
216 loadConstant(cUnit, r0, ssbIndex);
217 callRuntimeHelper(cUnit, rLR);
218 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
219 skipTarget->defMask = ENCODE_ALL;
220 branchOver->generic.target = (LIR*)skipTarget;
221 }
222 // rBase now holds static storage base
223 oatFreeTemp(cUnit, rMethod);
224 if (isLongOrDouble) {
225 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
226 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
227 } else {
228 rlSrc = oatGetSrc(cUnit, mir, 0);
229 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
230 }
231 if (isVolatile) {
buzbee12246b82011-09-29 14:15:05 -0700232 oatGenMemBarrier(cUnit, kST);
233 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800234 if (isLongOrDouble) {
235 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
236 rlSrc.highReg);
237 } else {
238 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
239 }
240 if (isVolatile) {
buzbee1da522d2011-09-04 11:22:20 -0700241 oatGenMemBarrier(cUnit, kSY);
242 }
buzbee1da522d2011-09-04 11:22:20 -0700243 if (isObject) {
244 markGCCard(cUnit, rlSrc.lowReg, rBase);
245 }
246 oatFreeTemp(cUnit, rBase);
Ian Rogers1bddec32012-02-04 12:27:34 -0800247 } else {
248 oatFlushAllRegs(cUnit); // Everything to home locations
249 int setterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pSet64Static) :
250 (isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
251 : OFFSETOF_MEMBER(Thread, pSet32Static));
252 loadWordDisp(cUnit, rSELF, setterOffset, rLR);
253 loadConstant(cUnit, r0, fieldIdx);
254 if (isLongOrDouble) {
255 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
256 } else {
257 loadValueDirect(cUnit, rlSrc, r1);
258 }
259 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700260 }
buzbee67bf8852011-08-17 17:51:35 -0700261}
262
Ian Rogers1bddec32012-02-04 12:27:34 -0800263STATIC void genSget(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
264 bool isLongOrDouble, bool isObject)
buzbee67bf8852011-08-17 17:51:35 -0700265{
Ian Rogers1bddec32012-02-04 12:27:34 -0800266 int fieldOffset;
267 int ssbIndex;
268 bool isVolatile;
269 bool isReferrersClass;
270 uint32_t fieldIdx = mir->dalvikInsn.vB;
271 bool fastPath =
272 cUnit->compiler->ComputeStaticFieldInfo(fieldIdx, cUnit,
273 fieldOffset, ssbIndex,
274 isReferrersClass, isVolatile);
275 if (fastPath && !SLOW_FIELD_PATH) {
276 DCHECK_GE(fieldOffset, 0);
277 int rBase;
278 int rMethod;
279 if (isReferrersClass) {
280 // Fast path, static storage base is this method's class
281 rMethod = loadCurrMethod(cUnit);
282 rBase = oatAllocTemp(cUnit);
283 loadWordDisp(cUnit, rMethod,
284 Method::DeclaringClassOffset().Int32Value(), rBase);
285 } else {
286 // Medium path, static storage base in a different class which
287 // requires checks that the other class is initialized
288 DCHECK_GE(ssbIndex, 0);
289 // May do runtime call so everything to home locations.
290 oatFlushAllRegs(cUnit);
291 // Using fixed register to sync with possible call to runtime
292 // support
293 rMethod = r1;
294 oatLockTemp(cUnit, rMethod);
295 loadCurrMethodDirect(cUnit, rMethod);
296 rBase = r0;
297 oatLockTemp(cUnit, rBase);
298 loadWordDisp(cUnit, rMethod,
299 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
300 rBase);
301 loadWordDisp(cUnit, rBase,
302 Array::DataOffset().Int32Value() + sizeof(int32_t*) * ssbIndex,
303 rBase);
304 // rBase now points at appropriate static storage base (Class*)
305 // or NULL if not initialized. Check for NULL and call helper if NULL.
306 // TUNING: fast path should fall through
307 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
308 loadWordDisp(cUnit, rSELF,
309 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
310 loadConstant(cUnit, r0, ssbIndex);
311 callRuntimeHelper(cUnit, rLR);
312 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
313 skipTarget->defMask = ENCODE_ALL;
314 branchOver->generic.target = (LIR*)skipTarget;
315 }
316 // rBase now holds static storage base
317 oatFreeTemp(cUnit, rMethod);
318 rlDest = isLongOrDouble ? oatGetDestWide(cUnit, mir, 0, 1)
319 : oatGetDest(cUnit, mir, 0);
buzbee1da522d2011-09-04 11:22:20 -0700320 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
Ian Rogers1bddec32012-02-04 12:27:34 -0800321 if (isVolatile) {
buzbee1da522d2011-09-04 11:22:20 -0700322 oatGenMemBarrier(cUnit, kSY);
323 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800324 if (isLongOrDouble) {
325 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
326 rlResult.highReg, INVALID_SREG);
327 } else {
328 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
329 }
buzbee1da522d2011-09-04 11:22:20 -0700330 oatFreeTemp(cUnit, rBase);
Ian Rogers1bddec32012-02-04 12:27:34 -0800331 if (isLongOrDouble) {
332 storeValueWide(cUnit, rlDest, rlResult);
333 } else {
334 storeValue(cUnit, rlDest, rlResult);
335 }
336 } else {
337 oatFlushAllRegs(cUnit); // Everything to home locations
338 int getterOffset = isLongOrDouble ? OFFSETOF_MEMBER(Thread, pGet64Static) :
339 (isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
340 : OFFSETOF_MEMBER(Thread, pGet32Static));
341 loadWordDisp(cUnit, rSELF, getterOffset, rLR);
342 loadConstant(cUnit, r0, fieldIdx);
343 callRuntimeHelper(cUnit, rLR);
344 if (isLongOrDouble) {
345 RegLocation rlResult = oatGetReturnWide(cUnit);
346 storeValueWide(cUnit, rlDest, rlResult);
347 } else {
348 RegLocation rlResult = oatGetReturn(cUnit);
349 storeValue(cUnit, rlDest, rlResult);
350 }
buzbeee1931742011-08-28 21:15:53 -0700351 }
buzbee67bf8852011-08-17 17:51:35 -0700352}
353
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800354typedef int (*NextCallInsn)(CompilationUnit*, MIR*, int, uint32_t dexIdx,
355 uint32_t methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700356
357/*
358 * Bit of a hack here - in leiu of a real scheduling pass,
359 * emit the next instruction in static & direct invoke sequences.
360 */
buzbeeed3e9302011-09-23 17:34:19 -0700361STATIC int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800362 int state, uint32_t dexIdx, uint32_t unused)
buzbee67bf8852011-08-17 17:51:35 -0700363{
364 switch(state) {
365 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700366 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700367 break;
buzbee561227c2011-09-02 15:28:19 -0700368 case 1: // Get method->code_and_direct_methods_
369 loadWordDisp(cUnit, r0,
370 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
371 r0);
buzbee67bf8852011-08-17 17:51:35 -0700372 break;
buzbee561227c2011-09-02 15:28:19 -0700373 case 2: // Grab target method* and target code_
374 loadWordDisp(cUnit, r0,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800375 CodeAndDirectMethods::CodeOffsetInBytes(dexIdx), rLR);
buzbee561227c2011-09-02 15:28:19 -0700376 loadWordDisp(cUnit, r0,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800377 CodeAndDirectMethods::MethodOffsetInBytes(dexIdx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700378 break;
379 default:
380 return -1;
381 }
382 return state + 1;
383}
384
buzbee67bf8852011-08-17 17:51:35 -0700385/*
386 * Bit of a hack here - in leiu of a real scheduling pass,
387 * emit the next instruction in a virtual invoke sequence.
388 * We can use rLR as a temp prior to target address loading
389 * Note also that we'll load the first argument ("this") into
390 * r1 here rather than the standard loadArgRegs.
391 */
buzbeeed3e9302011-09-23 17:34:19 -0700392STATIC int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800393 int state, uint32_t dexIdx, uint32_t methodIdx)
buzbee67bf8852011-08-17 17:51:35 -0700394{
395 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700396 /*
397 * This is the fast path in which the target virtual method is
398 * fully resolved at compile time.
399 */
buzbee67bf8852011-08-17 17:51:35 -0700400 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700401 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700402 rlArg = oatGetSrc(cUnit, mir, 0);
403 loadValueDirectFixed(cUnit, rlArg, r1);
404 break;
buzbee561227c2011-09-02 15:28:19 -0700405 case 1: // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700406 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee561227c2011-09-02 15:28:19 -0700407 // get this->klass_ [use r1, set rLR]
408 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700409 break;
buzbee561227c2011-09-02 15:28:19 -0700410 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
411 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700412 break;
buzbee561227c2011-09-02 15:28:19 -0700413 case 3: // Get target method [use rLR, set r0]
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800414 loadWordDisp(cUnit, rLR, (methodIdx * 4) +
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800415 Array::DataOffset().Int32Value(), r0);
buzbee561227c2011-09-02 15:28:19 -0700416 break;
417 case 4: // Get the target compiled code address [uses r0, sets rLR]
418 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700419 break;
420 default:
421 return -1;
422 }
423 return state + 1;
424}
425
buzbee67bf8852011-08-17 17:51:35 -0700426/*
427 * Interleave launch code for INVOKE_SUPER. See comments
428 * for nextVCallIns.
429 */
buzbeeed3e9302011-09-23 17:34:19 -0700430STATIC int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800431 int state, uint32_t dexIdx, uint32_t methodIdx)
buzbee67bf8852011-08-17 17:51:35 -0700432{
buzbee4a3164f2011-09-03 11:25:10 -0700433 /*
434 * This is the fast path in which the target virtual method is
435 * fully resolved at compile time. Note also that this path assumes
436 * that the check to verify that the target method index falls
437 * within the size of the super's vtable has been done at compile-time.
438 */
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800439 RegLocation rlArg;
buzbee67bf8852011-08-17 17:51:35 -0700440 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700441 case 0: // Get current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700442 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700443 // Load "this" [set r1]
444 rlArg = oatGetSrc(cUnit, mir, 0);
445 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee4a3164f2011-09-03 11:25:10 -0700446 // Get method->declaring_class_ [use r0, set rLR]
447 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
448 rLR);
buzbee67bf8852011-08-17 17:51:35 -0700449 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700450 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee4a3164f2011-09-03 11:25:10 -0700451 break;
452 case 1: // Get method->declaring_class_->super_class [usr rLR, set rLR]
453 loadWordDisp(cUnit, rLR, Class::SuperClassOffset().Int32Value(),
454 rLR);
455 break;
456 case 2: // Get ...->super_class_->vtable [u/s rLR]
457 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
458 break;
459 case 3: // Get target method [use rLR, set r0]
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800460 loadWordDisp(cUnit, rLR, (methodIdx * 4) +
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800461 Array::DataOffset().Int32Value(), r0);
buzbee4a3164f2011-09-03 11:25:10 -0700462 break;
463 case 4: // Get the target compiled code address [uses r0, sets rLR]
464 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
465 break;
buzbee67bf8852011-08-17 17:51:35 -0700466 default:
467 return -1;
468 }
buzbee4a3164f2011-09-03 11:25:10 -0700469 return state + 1;
470}
471
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800472STATIC int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir,
473 bool accessCheck, bool isInterface,
474 bool isSuper, int state, uint32_t dexIdx,
475 uint32_t methodIdx)
buzbee4a3164f2011-09-03 11:25:10 -0700476{
buzbee4a3164f2011-09-03 11:25:10 -0700477 /*
478 * This handles the case in which the base method is not fully
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800479 * resolved at compile time, we bail to a runtime helper.
buzbee4a3164f2011-09-03 11:25:10 -0700480 */
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800481 if (state == 0) {
482 int trampoline;
483 if (!accessCheck) {
484 DCHECK(isInterface);
485 trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
486 } else {
487 if (isInterface) {
488 trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
489 } else if (isSuper) {
490 trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
491 } else {
492 trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
493 }
494 }
495 // Load trampoline target
496 loadWordDisp(cUnit, rSELF, trampoline, rLR);
497 // Load r0 with method index
498 loadConstant(cUnit, r0, dexIdx);
499 return 1;
buzbee4a3164f2011-09-03 11:25:10 -0700500 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800501 return -1;
502}
503
504STATIC int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
505 int state, uint32_t dexIdx, uint32_t methodIdx)
506{
507 return nextInvokeInsnSP(cUnit, mir, true, false, true, state, dexIdx,
508 methodIdx);
509}
510
511STATIC int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
512 int state, uint32_t dexIdx, uint32_t methodIdx)
513{
514 return nextInvokeInsnSP(cUnit, mir, true, false, false, state, dexIdx,
515 methodIdx);
516}
517
518/*
519 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
520 * which will locate the target and continue on via a tail call.
521 */
522STATIC int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
523 int state, uint32_t dexIdx, uint32_t unused)
524{
525 return nextInvokeInsnSP(cUnit, mir, false, true, false, state, dexIdx, 0);
526}
527
528STATIC int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit,
529 MIR* mir, int state,
530 uint32_t dexIdx,
531 uint32_t unused)
532{
533 return nextInvokeInsnSP(cUnit, mir, true, true, false, state, dexIdx, 0);
534}
535
536STATIC int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
537 DecodedInstruction* dInsn, int callState,
538 NextCallInsn nextCallInsn, uint32_t dexIdx,
539 uint32_t methodIdx, bool skipThis)
540{
541 int nextReg = r1;
542 int nextArg = 0;
543 if (skipThis) {
544 nextReg++;
545 nextArg++;
546 }
547 for (; (nextReg <= r3) && (nextArg < mir->ssaRep->numUses); nextReg++) {
548 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
549 rlArg = oatUpdateRawLoc(cUnit, rlArg);
550 if (rlArg.wide && (nextReg <= r2)) {
551 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
552 nextReg++;
553 nextArg++;
554 } else {
555 rlArg.wide = false;
556 loadValueDirectFixed(cUnit, rlArg, nextReg);
557 }
558 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
559 }
560 return callState;
buzbee67bf8852011-08-17 17:51:35 -0700561}
562
563/*
564 * Load up to 5 arguments, the first three of which will be in
565 * r1 .. r3. On entry r0 contains the current method pointer,
566 * and as part of the load sequence, it must be replaced with
567 * the target method pointer. Note, this may also be called
568 * for "range" variants if the number of arguments is 5 or fewer.
569 */
buzbeeed3e9302011-09-23 17:34:19 -0700570STATIC int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700571 DecodedInstruction* dInsn, int callState,
572 ArmLIR** pcrLabel, bool isRange,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800573 NextCallInsn nextCallInsn, uint32_t dexIdx,
574 uint32_t methodIdx, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700575{
576 RegLocation rlArg;
buzbee67bf8852011-08-17 17:51:35 -0700577
578 /* If no arguments, just return */
579 if (dInsn->vA == 0)
580 return callState;
581
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800582 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700583
buzbeec0ecd652011-09-25 18:11:54 -0700584 DCHECK_LE(dInsn->vA, 5U);
585 if (dInsn->vA > 3) {
586 uint32_t nextUse = 3;
587 //Detect special case of wide arg spanning arg3/arg4
588 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
589 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
590 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
591 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
592 rlUse2.wide) {
593 int reg;
594 // Wide spans, we need the 2nd half of uses[2].
595 rlArg = oatUpdateLocWide(cUnit, rlUse2);
596 if (rlArg.location == kLocPhysReg) {
597 reg = rlArg.highReg;
598 } else {
599 // r2 & r3 can safely be used here
600 reg = r3;
buzbee67bc2362011-10-11 18:08:40 -0700601 loadWordDisp(cUnit, rSP,
602 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800603 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
604 methodIdx);
buzbeec0ecd652011-09-25 18:11:54 -0700605 }
606 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
607 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800608 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbeec0ecd652011-09-25 18:11:54 -0700609 nextUse++;
610 }
611 // Loop through the rest
612 while (nextUse < dInsn->vA) {
613 int lowReg;
614 int highReg;
615 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
616 rlArg = oatUpdateRawLoc(cUnit, rlArg);
617 if (rlArg.location == kLocPhysReg) {
618 lowReg = rlArg.lowReg;
619 highReg = rlArg.highReg;
620 } else {
621 lowReg = r2;
622 highReg = r3;
623 if (rlArg.wide) {
624 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
625 } else {
626 loadValueDirectFixed(cUnit, rlArg, lowReg);
627 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800628 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
629 methodIdx);
buzbeec0ecd652011-09-25 18:11:54 -0700630 }
631 int outsOffset = (nextUse + 1) * 4;
632 if (rlArg.wide) {
633 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
634 nextUse += 2;
635 } else {
636 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
637 nextUse++;
638 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800639 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700640 }
buzbee67bf8852011-08-17 17:51:35 -0700641 }
642
buzbeec0ecd652011-09-25 18:11:54 -0700643 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800644 dexIdx, methodIdx, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700645
buzbee67bf8852011-08-17 17:51:35 -0700646 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700647 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700648 }
649 return callState;
650}
651
652/*
653 * May have 0+ arguments (also used for jumbo). Note that
654 * source virtual registers may be in physical registers, so may
655 * need to be flushed to home location before copying. This
656 * applies to arg3 and above (see below).
657 *
658 * Two general strategies:
659 * If < 20 arguments
660 * Pass args 3-18 using vldm/vstm block copy
661 * Pass arg0, arg1 & arg2 in r1-r3
662 * If 20+ arguments
663 * Pass args arg19+ using memcpy block copy
664 * Pass arg0, arg1 & arg2 in r1-r3
665 *
666 */
buzbeeed3e9302011-09-23 17:34:19 -0700667STATIC int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700668 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700669 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800670 uint32_t dexIdx, uint32_t methodIdx,
671 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700672{
673 int firstArg = dInsn->vC;
674 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700675
buzbee67bf8852011-08-17 17:51:35 -0700676 // If we can treat it as non-range (Jumbo ops will use range form)
677 if (numArgs <= 5)
678 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800679 true, nextCallInsn, dexIdx, methodIdx,
680 skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700681 /*
682 * Make sure range list doesn't span the break between in normal
683 * Dalvik vRegs and the ins.
684 */
buzbee1b4c8592011-08-31 10:43:51 -0700685 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800686 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
buzbee1b4c8592011-08-31 10:43:51 -0700687 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
688 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700689 }
690
691 /*
692 * First load the non-register arguments. Both forms expect all
693 * of the source arguments to be in their home frame location, so
694 * scan the sReg names and flush any that have been promoted to
695 * frame backing storage.
696 */
697 // Scan the rest of the args - if in physReg flush to memory
buzbeec0ecd652011-09-25 18:11:54 -0700698 for (int nextArg = 0; nextArg < numArgs;) {
699 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
buzbee1b4c8592011-08-31 10:43:51 -0700700 if (loc.wide) {
701 loc = oatUpdateLocWide(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700702 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700703 storeBaseDispWide(cUnit, rSP,
704 oatSRegOffset(cUnit, loc.sRegLow),
705 loc.lowReg, loc.highReg);
buzbee1b4c8592011-08-31 10:43:51 -0700706 }
buzbeec0ecd652011-09-25 18:11:54 -0700707 nextArg += 2;
buzbee1b4c8592011-08-31 10:43:51 -0700708 } else {
709 loc = oatUpdateLoc(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700710 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700711 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
712 loc.lowReg, kWord);
buzbee1b4c8592011-08-31 10:43:51 -0700713 }
buzbeec0ecd652011-09-25 18:11:54 -0700714 nextArg++;
buzbee67bf8852011-08-17 17:51:35 -0700715 }
716 }
717
buzbee67bc2362011-10-11 18:08:40 -0700718 int startOffset = oatSRegOffset(cUnit,
719 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
buzbee67bf8852011-08-17 17:51:35 -0700720 int outsOffset = 4 /* Method* */ + (3 * 4);
721 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700722 // Generate memcpy
723 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
724 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700725 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
726 loadConstant(cUnit, r2, (numArgs - 3) * 4);
Ian Rogersff1ed472011-09-20 13:46:24 -0700727 callRuntimeHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700728 // Restore Method*
729 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700730 } else {
731 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700732 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800733 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700734 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700735 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
736 //TUNING: loosen barrier
737 ld->defMask = ENCODE_ALL;
738 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800739 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700740 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800741 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbeef48e9712011-09-15 17:54:28 -0700742 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
743 setMemRefType(st, false /* isLoad */, kDalvikReg);
744 st->defMask = ENCODE_ALL;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800745 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700746 }
747
buzbeec0ecd652011-09-25 18:11:54 -0700748 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800749 dexIdx, methodIdx, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700750
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800751 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee99f27232011-10-05 12:56:36 -0700752 if (pcrLabel) {
753 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
754 }
buzbee67bf8852011-08-17 17:51:35 -0700755 return callState;
756}
757
buzbee2a475e72011-09-07 17:19:17 -0700758// Debugging routine - if null target, branch to DebugMe
buzbeeed3e9302011-09-23 17:34:19 -0700759STATIC void genShowTarget(CompilationUnit* cUnit)
buzbee2a475e72011-09-07 17:19:17 -0700760{
761 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
762 loadWordDisp(cUnit, rSELF,
763 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
764 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
765 target->defMask = -1;
766 branchOver->generic.target = (LIR*)target;
767}
buzbee2a475e72011-09-07 17:19:17 -0700768
buzbeeed3e9302011-09-23 17:34:19 -0700769STATIC void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700770 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700771{
772 DecodedInstruction* dInsn = &mir->dalvikInsn;
773 int callState = 0;
774 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700775 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700776 NextCallInsn nextCallInsn = nextSDCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700777 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee561227c2011-09-02 15:28:19 -0700778
buzbee109bd6a2011-09-06 13:58:41 -0700779 // Explicit register usage
780 oatLockCallTemps(cUnit);
781
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800782 uint32_t dexMethodIdx = mir->dalvikInsn.vB;
buzbee99f27232011-10-05 12:56:36 -0700783
buzbee561227c2011-09-02 15:28:19 -0700784 if (range) {
785 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800786 nextCallInsn, dexMethodIdx, 0, false);
buzbee561227c2011-09-02 15:28:19 -0700787 } else {
788 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800789 false, nextCallInsn, dexMethodIdx,
790 0, false);
buzbee561227c2011-09-02 15:28:19 -0700791 }
buzbee67bf8852011-08-17 17:51:35 -0700792 // Finish up any of the call sequence not interleaved in arg loading
793 while (callState >= 0) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800794 callState = nextCallInsn(cUnit, mir, callState, dexMethodIdx, 0);
buzbee67bf8852011-08-17 17:51:35 -0700795 }
buzbeece302932011-10-04 14:32:18 -0700796 if (DISPLAY_MISSING_TARGETS) {
797 genShowTarget(cUnit);
798 }
buzbeeec5adf32011-09-11 15:25:43 -0700799 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -0700800 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700801}
802
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800803STATIC void genInvoke(CompilationUnit* cUnit, MIR* mir, bool isInterface,
804 bool isSuper, bool isRange)
buzbee67bf8852011-08-17 17:51:35 -0700805{
806 DecodedInstruction* dInsn = &mir->dalvikInsn;
807 int callState = 0;
buzbee561227c2011-09-02 15:28:19 -0700808 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700809 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -0700810 // Explicit register usage
811 oatLockCallTemps(cUnit);
buzbee34c77ad2012-01-11 13:01:32 -0800812
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800813 uint32_t dexMethodIdx = dInsn->vB;
814 int vtableIdx;
815 bool fastPath =
816 cUnit->compiler->ComputeInvokeInfo(dexMethodIdx, cUnit,
817 isInterface, isSuper, vtableIdx)
818 && !SLOW_INVOKE_PATH;
819 if (isInterface) {
820 nextCallInsn = fastPath ? nextInterfaceCallInsn
821 : nextInterfaceCallInsnWithAccessCheck;
822 } else if (isSuper) {
823 nextCallInsn = fastPath ? nextSuperCallInsn : nextSuperCallInsnSP;
buzbee561227c2011-09-02 15:28:19 -0700824 } else {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800825 nextCallInsn = fastPath ? nextVCallInsn : nextVCallInsnSP;
buzbee561227c2011-09-02 15:28:19 -0700826 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800827 bool skipThis = fastPath && !isInterface;
828 if (!isRange) {
buzbeec0ecd652011-09-25 18:11:54 -0700829 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800830 false, nextCallInsn, dexMethodIdx,
831 vtableIdx, skipThis);
832 } else {
buzbeec0ecd652011-09-25 18:11:54 -0700833 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800834 nextCallInsn, dexMethodIdx, vtableIdx,
835 skipThis);
836 }
buzbee67bf8852011-08-17 17:51:35 -0700837 // Finish up any of the call sequence not interleaved in arg loading
838 while (callState >= 0) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800839 callState = nextCallInsn(cUnit, mir, callState, dexMethodIdx,
840 vtableIdx);
buzbee67bf8852011-08-17 17:51:35 -0700841 }
buzbeece302932011-10-04 14:32:18 -0700842 if (DISPLAY_MISSING_TARGETS) {
843 genShowTarget(cUnit);
844 }
buzbeeec5adf32011-09-11 15:25:43 -0700845 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -0700846 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700847}
848
buzbeeed3e9302011-09-23 17:34:19 -0700849STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700850 BasicBlock* bb, ArmLIR* labelList)
851{
852 bool res = false; // Assume success
853 RegLocation rlSrc[3];
854 RegLocation rlDest = badLoc;
855 RegLocation rlResult = badLoc;
856 Opcode opcode = mir->dalvikInsn.opcode;
857
858 /* Prep Src and Dest locations */
859 int nextSreg = 0;
860 int nextLoc = 0;
861 int attrs = oatDataFlowAttributes[opcode];
862 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
863 if (attrs & DF_UA) {
864 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
865 nextSreg++;
866 } else if (attrs & DF_UA_WIDE) {
867 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
868 nextSreg + 1);
869 nextSreg+= 2;
870 }
871 if (attrs & DF_UB) {
872 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
873 nextSreg++;
874 } else if (attrs & DF_UB_WIDE) {
875 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
876 nextSreg + 1);
877 nextSreg+= 2;
878 }
879 if (attrs & DF_UC) {
880 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
881 } else if (attrs & DF_UC_WIDE) {
882 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
883 nextSreg + 1);
884 }
885 if (attrs & DF_DA) {
886 rlDest = oatGetDest(cUnit, mir, 0);
887 } else if (attrs & DF_DA_WIDE) {
888 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
889 }
890
891 switch(opcode) {
892 case OP_NOP:
893 break;
894
895 case OP_MOVE_EXCEPTION:
896 int exOffset;
897 int resetReg;
buzbeec143c552011-08-20 17:38:58 -0700898 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700899 resetReg = oatAllocTemp(cUnit);
900 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
901 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
902 loadConstant(cUnit, resetReg, 0);
903 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
904 storeValue(cUnit, rlDest, rlResult);
905 break;
906
907 case OP_RETURN_VOID:
buzbeefe2e17f2011-10-10 09:35:02 -0700908 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -0700909 break;
910
911 case OP_RETURN:
912 case OP_RETURN_OBJECT:
buzbeefe2e17f2011-10-10 09:35:02 -0700913 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -0700914 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -0700915 break;
916
917 case OP_RETURN_WIDE:
buzbeefe2e17f2011-10-10 09:35:02 -0700918 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -0700919 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -0700920 break;
921
922 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -0700923 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -0700924 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -0700925 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -0700926 break;
927
928 case OP_MOVE_RESULT:
929 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -0700930 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -0700931 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -0700932 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -0700933 break;
934
935 case OP_MOVE:
936 case OP_MOVE_OBJECT:
937 case OP_MOVE_16:
938 case OP_MOVE_OBJECT_16:
939 case OP_MOVE_FROM16:
940 case OP_MOVE_OBJECT_FROM16:
941 storeValue(cUnit, rlDest, rlSrc[0]);
942 break;
943
944 case OP_MOVE_WIDE:
945 case OP_MOVE_WIDE_16:
946 case OP_MOVE_WIDE_FROM16:
947 storeValueWide(cUnit, rlDest, rlSrc[0]);
948 break;
949
950 case OP_CONST:
951 case OP_CONST_4:
952 case OP_CONST_16:
953 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
954 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
955 storeValue(cUnit, rlDest, rlResult);
956 break;
957
958 case OP_CONST_HIGH16:
959 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
960 loadConstantNoClobber(cUnit, rlResult.lowReg,
961 mir->dalvikInsn.vB << 16);
962 storeValue(cUnit, rlDest, rlResult);
963 break;
964
965 case OP_CONST_WIDE_16:
966 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -0700967 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
968 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
969 mir->dalvikInsn.vB,
970 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -0700971 storeValueWide(cUnit, rlDest, rlResult);
972 break;
973
974 case OP_CONST_WIDE:
975 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
976 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -0700977 mir->dalvikInsn.vB_wide & 0xffffffff,
978 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -0700979 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700980 break;
981
982 case OP_CONST_WIDE_HIGH16:
983 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
984 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
985 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -0700986 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700987 break;
988
989 case OP_MONITOR_ENTER:
990 genMonitorEnter(cUnit, mir, rlSrc[0]);
991 break;
992
993 case OP_MONITOR_EXIT:
994 genMonitorExit(cUnit, mir, rlSrc[0]);
995 break;
996
997 case OP_CHECK_CAST:
998 genCheckCast(cUnit, mir, rlSrc[0]);
999 break;
1000
1001 case OP_INSTANCE_OF:
1002 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1003 break;
1004
1005 case OP_NEW_INSTANCE:
1006 genNewInstance(cUnit, mir, rlDest);
1007 break;
1008
1009 case OP_THROW:
1010 genThrow(cUnit, mir, rlSrc[0]);
1011 break;
1012
buzbee5ade1d22011-09-09 14:44:52 -07001013 case OP_THROW_VERIFICATION_ERROR:
1014 loadWordDisp(cUnit, rSELF,
1015 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1016 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1017 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001018 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001019 break;
1020
buzbee67bf8852011-08-17 17:51:35 -07001021 case OP_ARRAY_LENGTH:
1022 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001023 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001024 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001025 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001026 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1027 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1028 rlResult.lowReg);
1029 storeValue(cUnit, rlDest, rlResult);
1030 break;
1031
1032 case OP_CONST_STRING:
1033 case OP_CONST_STRING_JUMBO:
1034 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1035 break;
1036
1037 case OP_CONST_CLASS:
1038 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1039 break;
1040
1041 case OP_FILL_ARRAY_DATA:
1042 genFillArrayData(cUnit, mir, rlSrc[0]);
1043 break;
1044
1045 case OP_FILLED_NEW_ARRAY:
1046 genFilledNewArray(cUnit, mir, false /* not range */);
1047 break;
1048
1049 case OP_FILLED_NEW_ARRAY_RANGE:
1050 genFilledNewArray(cUnit, mir, true /* range */);
1051 break;
1052
1053 case OP_NEW_ARRAY:
1054 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1055 break;
1056
1057 case OP_GOTO:
1058 case OP_GOTO_16:
1059 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001060 if (bb->taken->startOffset <= mir->offset) {
1061 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001062 }
1063 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1064 break;
1065
1066 case OP_PACKED_SWITCH:
1067 genPackedSwitch(cUnit, mir, rlSrc[0]);
1068 break;
1069
1070 case OP_SPARSE_SWITCH:
1071 genSparseSwitch(cUnit, mir, rlSrc[0]);
1072 break;
1073
1074 case OP_CMPL_FLOAT:
1075 case OP_CMPG_FLOAT:
1076 case OP_CMPL_DOUBLE:
1077 case OP_CMPG_DOUBLE:
1078 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1079 break;
1080
1081 case OP_CMP_LONG:
1082 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1083 break;
1084
1085 case OP_IF_EQ:
1086 case OP_IF_NE:
1087 case OP_IF_LT:
1088 case OP_IF_GE:
1089 case OP_IF_GT:
1090 case OP_IF_LE: {
1091 bool backwardBranch;
1092 ArmConditionCode cond;
1093 backwardBranch = (bb->taken->startOffset <= mir->offset);
1094 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001095 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001096 }
1097 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1098 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1099 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1100 switch(opcode) {
1101 case OP_IF_EQ:
1102 cond = kArmCondEq;
1103 break;
1104 case OP_IF_NE:
1105 cond = kArmCondNe;
1106 break;
1107 case OP_IF_LT:
1108 cond = kArmCondLt;
1109 break;
1110 case OP_IF_GE:
1111 cond = kArmCondGe;
1112 break;
1113 case OP_IF_GT:
1114 cond = kArmCondGt;
1115 break;
1116 case OP_IF_LE:
1117 cond = kArmCondLe;
1118 break;
1119 default:
1120 cond = (ArmConditionCode)0;
1121 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1122 }
1123 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1124 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1125 break;
1126 }
1127
1128 case OP_IF_EQZ:
1129 case OP_IF_NEZ:
1130 case OP_IF_LTZ:
1131 case OP_IF_GEZ:
1132 case OP_IF_GTZ:
1133 case OP_IF_LEZ: {
1134 bool backwardBranch;
1135 ArmConditionCode cond;
1136 backwardBranch = (bb->taken->startOffset <= mir->offset);
1137 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001138 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001139 }
1140 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1141 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1142 switch(opcode) {
1143 case OP_IF_EQZ:
1144 cond = kArmCondEq;
1145 break;
1146 case OP_IF_NEZ:
1147 cond = kArmCondNe;
1148 break;
1149 case OP_IF_LTZ:
1150 cond = kArmCondLt;
1151 break;
1152 case OP_IF_GEZ:
1153 cond = kArmCondGe;
1154 break;
1155 case OP_IF_GTZ:
1156 cond = kArmCondGt;
1157 break;
1158 case OP_IF_LEZ:
1159 cond = kArmCondLe;
1160 break;
1161 default:
1162 cond = (ArmConditionCode)0;
1163 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1164 }
1165 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1166 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1167 break;
1168 }
1169
1170 case OP_AGET_WIDE:
1171 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1172 break;
1173 case OP_AGET:
1174 case OP_AGET_OBJECT:
1175 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1176 break;
1177 case OP_AGET_BOOLEAN:
1178 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1179 rlDest, 0);
1180 break;
1181 case OP_AGET_BYTE:
1182 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1183 break;
1184 case OP_AGET_CHAR:
1185 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1186 rlDest, 1);
1187 break;
1188 case OP_AGET_SHORT:
1189 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1190 break;
1191 case OP_APUT_WIDE:
1192 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1193 break;
1194 case OP_APUT:
1195 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1196 break;
1197 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001198 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001199 break;
1200 case OP_APUT_SHORT:
1201 case OP_APUT_CHAR:
1202 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1203 rlSrc[0], 1);
1204 break;
1205 case OP_APUT_BYTE:
1206 case OP_APUT_BOOLEAN:
1207 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1208 rlSrc[0], 0);
1209 break;
1210
Ian Rogers1bddec32012-02-04 12:27:34 -08001211 case OP_IGET_OBJECT:
1212 case OP_IGET_OBJECT_VOLATILE:
1213 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, true);
1214 break;
1215
buzbee67bf8852011-08-17 17:51:35 -07001216 case OP_IGET_WIDE:
1217 case OP_IGET_WIDE_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001218 genIGet(cUnit, mir, kLong, rlDest, rlSrc[0], true, false);
buzbee67bf8852011-08-17 17:51:35 -07001219 break;
1220
1221 case OP_IGET:
1222 case OP_IGET_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001223 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, false);
1224 break;
1225
1226 case OP_IGET_CHAR:
1227 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0], false, false);
1228 break;
1229
1230 case OP_IGET_SHORT:
1231 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001232 break;
1233
1234 case OP_IGET_BOOLEAN:
1235 case OP_IGET_BYTE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001236 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001237 break;
1238
1239 case OP_IPUT_WIDE:
1240 case OP_IPUT_WIDE_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001241 genIPut(cUnit, mir, kLong, rlSrc[0], rlSrc[1], true, false);
buzbee67bf8852011-08-17 17:51:35 -07001242 break;
1243
1244 case OP_IPUT_OBJECT:
1245 case OP_IPUT_OBJECT_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001246 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, true);
buzbee67bf8852011-08-17 17:51:35 -07001247 break;
1248
1249 case OP_IPUT:
1250 case OP_IPUT_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001251 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001252 break;
1253
1254 case OP_IPUT_BOOLEAN:
1255 case OP_IPUT_BYTE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001256 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001257 break;
1258
1259 case OP_IPUT_CHAR:
Ian Rogers1bddec32012-02-04 12:27:34 -08001260 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001261 break;
1262
1263 case OP_IPUT_SHORT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001264 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001265 break;
1266
buzbee67bf8852011-08-17 17:51:35 -07001267 case OP_SGET_OBJECT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001268 genSget(cUnit, mir, rlDest, false, true);
1269 break;
1270 case OP_SGET:
buzbee67bf8852011-08-17 17:51:35 -07001271 case OP_SGET_BOOLEAN:
1272 case OP_SGET_BYTE:
1273 case OP_SGET_CHAR:
1274 case OP_SGET_SHORT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001275 genSget(cUnit, mir, rlDest, false, false);
buzbee67bf8852011-08-17 17:51:35 -07001276 break;
1277
1278 case OP_SGET_WIDE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001279 genSget(cUnit, mir, rlDest, true, false);
buzbee67bf8852011-08-17 17:51:35 -07001280 break;
1281
buzbee67bf8852011-08-17 17:51:35 -07001282 case OP_SPUT_OBJECT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001283 genSput(cUnit, mir, rlSrc[0], false, true);
1284 break;
1285
1286 case OP_SPUT:
buzbee67bf8852011-08-17 17:51:35 -07001287 case OP_SPUT_BOOLEAN:
1288 case OP_SPUT_BYTE:
1289 case OP_SPUT_CHAR:
1290 case OP_SPUT_SHORT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001291 genSput(cUnit, mir, rlSrc[0], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001292 break;
1293
1294 case OP_SPUT_WIDE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001295 genSput(cUnit, mir, rlSrc[0], true, false);
buzbee67bf8852011-08-17 17:51:35 -07001296 break;
1297
1298 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001299 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1300 true /*range*/);
1301 break;
buzbee67bf8852011-08-17 17:51:35 -07001302 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001303 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1304 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001305 break;
1306
1307 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001308 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1309 false /*range*/);
1310 break;
buzbee67bf8852011-08-17 17:51:35 -07001311 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001312 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1313 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001314 break;
1315
1316 case OP_INVOKE_VIRTUAL:
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001317 genInvoke(cUnit, mir, false /*interface*/, false /*super*/,
1318 false /*range*/);
1319 break;
buzbee67bf8852011-08-17 17:51:35 -07001320 case OP_INVOKE_VIRTUAL_RANGE:
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001321 genInvoke(cUnit, mir, false /*interface*/, false /*super*/,
1322 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001323 break;
1324
1325 case OP_INVOKE_SUPER:
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001326 genInvoke(cUnit, mir, false /*interface*/, true /*super*/,
1327 false /*range*/);
1328 break;
buzbee67bf8852011-08-17 17:51:35 -07001329 case OP_INVOKE_SUPER_RANGE:
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001330 genInvoke(cUnit, mir, false /*interface*/, true /*super*/,
1331 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001332 break;
1333
1334 case OP_INVOKE_INTERFACE:
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001335 genInvoke(cUnit, mir, true /*interface*/, false /*super*/,
1336 false /*range*/);
1337 break;
buzbee67bf8852011-08-17 17:51:35 -07001338 case OP_INVOKE_INTERFACE_RANGE:
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001339 genInvoke(cUnit, mir, true /*interface*/, false /*super*/,
1340 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001341 break;
1342
1343 case OP_NEG_INT:
1344 case OP_NOT_INT:
1345 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1346 break;
1347
1348 case OP_NEG_LONG:
1349 case OP_NOT_LONG:
1350 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1351 break;
1352
1353 case OP_NEG_FLOAT:
1354 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1355 break;
1356
1357 case OP_NEG_DOUBLE:
1358 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1359 break;
1360
1361 case OP_INT_TO_LONG:
1362 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1363 if (rlSrc[0].location == kLocPhysReg) {
1364 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1365 } else {
1366 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1367 }
1368 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1369 rlResult.lowReg, 31);
1370 storeValueWide(cUnit, rlDest, rlResult);
1371 break;
1372
1373 case OP_LONG_TO_INT:
1374 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1375 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1376 storeValue(cUnit, rlDest, rlSrc[0]);
1377 break;
1378
1379 case OP_INT_TO_BYTE:
1380 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1381 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1382 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1383 storeValue(cUnit, rlDest, rlResult);
1384 break;
1385
1386 case OP_INT_TO_SHORT:
1387 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1388 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1389 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1390 storeValue(cUnit, rlDest, rlResult);
1391 break;
1392
1393 case OP_INT_TO_CHAR:
1394 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1395 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1396 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1397 storeValue(cUnit, rlDest, rlResult);
1398 break;
1399
1400 case OP_INT_TO_FLOAT:
1401 case OP_INT_TO_DOUBLE:
1402 case OP_LONG_TO_FLOAT:
1403 case OP_LONG_TO_DOUBLE:
1404 case OP_FLOAT_TO_INT:
1405 case OP_FLOAT_TO_LONG:
1406 case OP_FLOAT_TO_DOUBLE:
1407 case OP_DOUBLE_TO_INT:
1408 case OP_DOUBLE_TO_LONG:
1409 case OP_DOUBLE_TO_FLOAT:
1410 genConversion(cUnit, mir);
1411 break;
1412
1413 case OP_ADD_INT:
1414 case OP_SUB_INT:
1415 case OP_MUL_INT:
1416 case OP_DIV_INT:
1417 case OP_REM_INT:
1418 case OP_AND_INT:
1419 case OP_OR_INT:
1420 case OP_XOR_INT:
1421 case OP_SHL_INT:
1422 case OP_SHR_INT:
1423 case OP_USHR_INT:
1424 case OP_ADD_INT_2ADDR:
1425 case OP_SUB_INT_2ADDR:
1426 case OP_MUL_INT_2ADDR:
1427 case OP_DIV_INT_2ADDR:
1428 case OP_REM_INT_2ADDR:
1429 case OP_AND_INT_2ADDR:
1430 case OP_OR_INT_2ADDR:
1431 case OP_XOR_INT_2ADDR:
1432 case OP_SHL_INT_2ADDR:
1433 case OP_SHR_INT_2ADDR:
1434 case OP_USHR_INT_2ADDR:
1435 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1436 break;
1437
1438 case OP_ADD_LONG:
1439 case OP_SUB_LONG:
1440 case OP_MUL_LONG:
1441 case OP_DIV_LONG:
1442 case OP_REM_LONG:
1443 case OP_AND_LONG:
1444 case OP_OR_LONG:
1445 case OP_XOR_LONG:
1446 case OP_ADD_LONG_2ADDR:
1447 case OP_SUB_LONG_2ADDR:
1448 case OP_MUL_LONG_2ADDR:
1449 case OP_DIV_LONG_2ADDR:
1450 case OP_REM_LONG_2ADDR:
1451 case OP_AND_LONG_2ADDR:
1452 case OP_OR_LONG_2ADDR:
1453 case OP_XOR_LONG_2ADDR:
1454 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1455 break;
1456
buzbee67bf8852011-08-17 17:51:35 -07001457 case OP_SHL_LONG:
1458 case OP_SHR_LONG:
1459 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001460 case OP_SHL_LONG_2ADDR:
1461 case OP_SHR_LONG_2ADDR:
1462 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001463 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1464 break;
1465
1466 case OP_ADD_FLOAT:
1467 case OP_SUB_FLOAT:
1468 case OP_MUL_FLOAT:
1469 case OP_DIV_FLOAT:
1470 case OP_REM_FLOAT:
1471 case OP_ADD_FLOAT_2ADDR:
1472 case OP_SUB_FLOAT_2ADDR:
1473 case OP_MUL_FLOAT_2ADDR:
1474 case OP_DIV_FLOAT_2ADDR:
1475 case OP_REM_FLOAT_2ADDR:
1476 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1477 break;
1478
1479 case OP_ADD_DOUBLE:
1480 case OP_SUB_DOUBLE:
1481 case OP_MUL_DOUBLE:
1482 case OP_DIV_DOUBLE:
1483 case OP_REM_DOUBLE:
1484 case OP_ADD_DOUBLE_2ADDR:
1485 case OP_SUB_DOUBLE_2ADDR:
1486 case OP_MUL_DOUBLE_2ADDR:
1487 case OP_DIV_DOUBLE_2ADDR:
1488 case OP_REM_DOUBLE_2ADDR:
1489 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1490 break;
1491
1492 case OP_RSUB_INT:
1493 case OP_ADD_INT_LIT16:
1494 case OP_MUL_INT_LIT16:
1495 case OP_DIV_INT_LIT16:
1496 case OP_REM_INT_LIT16:
1497 case OP_AND_INT_LIT16:
1498 case OP_OR_INT_LIT16:
1499 case OP_XOR_INT_LIT16:
1500 case OP_ADD_INT_LIT8:
1501 case OP_RSUB_INT_LIT8:
1502 case OP_MUL_INT_LIT8:
1503 case OP_DIV_INT_LIT8:
1504 case OP_REM_INT_LIT8:
1505 case OP_AND_INT_LIT8:
1506 case OP_OR_INT_LIT8:
1507 case OP_XOR_INT_LIT8:
1508 case OP_SHL_INT_LIT8:
1509 case OP_SHR_INT_LIT8:
1510 case OP_USHR_INT_LIT8:
1511 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1512 break;
1513
1514 default:
1515 res = true;
1516 }
1517 return res;
1518}
1519
Elliott Hughesc1f143d2011-12-01 17:31:10 -08001520STATIC const char* extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001521 "kMirOpPhi",
1522 "kMirOpNullNRangeUpCheck",
1523 "kMirOpNullNRangeDownCheck",
1524 "kMirOpLowerBound",
1525 "kMirOpPunt",
1526 "kMirOpCheckInlinePrediction",
1527};
1528
1529/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001530STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001531{
1532 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
buzbee5abfa3e2012-01-31 17:01:43 -08001533 char* msg = NULL;
1534 if (cUnit->printMe) {
buzbeeba938cb2012-02-03 14:47:55 -08001535 msg = (char*)oatNew(cUnit, strlen(extendedMIROpNames[opOffset]) + 1,
1536 false, kAllocDebugInfo);
buzbee5abfa3e2012-01-31 17:01:43 -08001537 strcpy(msg, extendedMIROpNames[opOffset]);
1538 }
buzbee67bf8852011-08-17 17:51:35 -07001539 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1540
1541 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1542 case kMirOpPhi: {
buzbee5abfa3e2012-01-31 17:01:43 -08001543 char* ssaString = NULL;
1544 if (cUnit->printMe) {
1545 ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1546 }
buzbee67bf8852011-08-17 17:51:35 -07001547 op->flags.isNop = true;
1548 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1549 break;
1550 }
1551 default:
1552 break;
1553 }
1554}
1555
buzbee67bc2362011-10-11 18:08:40 -07001556/*
1557 * If there are any ins passed in registers that have not been promoted
1558 * to a callee-save register, flush them to the frame. Perform intial
1559 * assignment of promoted arguments.
1560 */
buzbeeed3e9302011-09-23 17:34:19 -07001561STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001562{
Ian Rogersa3760aa2011-11-14 14:32:37 -08001563 if (cUnit->numIns == 0)
buzbee67bf8852011-08-17 17:51:35 -07001564 return;
buzbee67bc2362011-10-11 18:08:40 -07001565 int firstArgReg = r1;
1566 int lastArgReg = r3;
Ian Rogersa3760aa2011-11-14 14:32:37 -08001567 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
buzbee8febc582011-10-25 12:39:20 -07001568 /*
1569 * Arguments passed in registers should be flushed
1570 * to their backing locations in the frame for now.
1571 * Also, we need to do initial assignment for promoted
1572 * arguments. NOTE: an older version of dx had an issue
1573 * in which it would reuse static method argument registers.
1574 * This could result in the same Dalvik virtual register
1575 * being promoted to both core and fp regs. In those
1576 * cases, copy argument to both. This will be uncommon
1577 * enough that it isn't worth attempting to optimize.
1578 */
Ian Rogersa3760aa2011-11-14 14:32:37 -08001579 for (int i = 0; i < cUnit->numIns; i++) {
buzbee67bc2362011-10-11 18:08:40 -07001580 PromotionMap vMap = cUnit->promotionMap[startVReg + i];
buzbee67bc2362011-10-11 18:08:40 -07001581 if (i <= (lastArgReg - firstArgReg)) {
buzbee8febc582011-10-25 12:39:20 -07001582 // If arriving in register
buzbee67bc2362011-10-11 18:08:40 -07001583 if (vMap.coreLocation == kLocPhysReg) {
1584 genRegCopy(cUnit, vMap.coreReg, firstArgReg + i);
buzbee8febc582011-10-25 12:39:20 -07001585 }
1586 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001587 genRegCopy(cUnit, vMap.fpReg, firstArgReg + i);
1588 }
1589 // Also put a copy in memory in case we're partially promoted
1590 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1591 firstArgReg + i, kWord);
1592 } else {
buzbee8febc582011-10-25 12:39:20 -07001593 // If arriving in frame & promoted
buzbee67bc2362011-10-11 18:08:40 -07001594 if (vMap.coreLocation == kLocPhysReg) {
1595 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1596 vMap.coreReg);
buzbee8febc582011-10-25 12:39:20 -07001597 }
1598 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001599 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1600 vMap.fpReg);
buzbee67bf8852011-08-17 17:51:35 -07001601 }
1602 }
buzbee67bf8852011-08-17 17:51:35 -07001603 }
1604}
1605
1606/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001607STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001608{
1609 MIR* mir;
1610 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1611 int blockId = bb->id;
1612
1613 cUnit->curBlock = bb;
1614 labelList[blockId].operands[0] = bb->startOffset;
1615
1616 /* Insert the block label */
1617 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1618 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1619
buzbee6181f792011-09-29 11:14:04 -07001620 /* Reset local optimization data on block boundaries */
1621 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001622 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001623 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001624
1625 ArmLIR* headLIR = NULL;
1626
buzbeebbaf8942011-10-02 13:08:29 -07001627 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001628 if (bb->blockType == kEntryBlock) {
1629 /*
1630 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1631 * mechanism know so it doesn't try to use any of them when
1632 * expanding the frame or flushing. This leaves the utility
1633 * code with a single temp: r12. This should be enough.
1634 */
1635 oatLockTemp(cUnit, r0);
1636 oatLockTemp(cUnit, r1);
1637 oatLockTemp(cUnit, r2);
1638 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001639
1640 /*
1641 * We can safely skip the stack overflow check if we're
1642 * a leaf *and* our frame size < fudge factor.
1643 */
1644 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1645 ((size_t)cUnit->frameSize <
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001646 Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001647 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001648 if (!skipOverflowCheck) {
1649 /* Load stack limit */
1650 loadWordDisp(cUnit, rSELF,
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001651 Thread::StackEndOffset().Int32Value(), r12);
buzbeecefd1872011-09-09 09:59:52 -07001652 }
buzbee67bf8852011-08-17 17:51:35 -07001653 /* Spill core callee saves */
1654 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1655 /* Need to spill any FP regs? */
1656 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001657 /*
1658 * NOTE: fp spills are a little different from core spills in that
1659 * they are pushed as a contiguous block. When promoting from
1660 * the fp set, we must allocate all singles from s16..highest-promoted
1661 */
buzbee67bf8852011-08-17 17:51:35 -07001662 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1663 }
buzbeecefd1872011-09-09 09:59:52 -07001664 if (!skipOverflowCheck) {
1665 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001666 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001667 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1668 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001669 genRegCopy(cUnit, rSP, rLR); // Establish stack
1670 } else {
1671 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001672 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001673 }
buzbee67bf8852011-08-17 17:51:35 -07001674 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1675 flushIns(cUnit);
buzbee44b412b2012-02-04 08:50:53 -08001676
1677 if (cUnit->genDebugger) {
1678 // Refresh update debugger callout
1679 loadWordDisp(cUnit, rSELF,
1680 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), rSUSPEND);
1681 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY);
1682 }
1683
buzbee67bf8852011-08-17 17:51:35 -07001684 oatFreeTemp(cUnit, r0);
1685 oatFreeTemp(cUnit, r1);
1686 oatFreeTemp(cUnit, r2);
1687 oatFreeTemp(cUnit, r3);
1688 } else if (bb->blockType == kExitBlock) {
1689 newLIR0(cUnit, kArmPseudoMethodExit);
buzbee44b412b2012-02-04 08:50:53 -08001690 /* If we're compiling for the debugger, generate an update callout */
1691 if (cUnit->genDebugger) {
1692 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT);
1693 }
buzbeebbaf8942011-10-02 13:08:29 -07001694 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07001695 /* Need to restore any FP callee saves? */
1696 if (cUnit->numFPSpills) {
1697 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1698 }
1699 if (cUnit->coreSpillMask & (1 << rLR)) {
1700 /* Unspill rLR to rPC */
1701 cUnit->coreSpillMask &= ~(1 << rLR);
1702 cUnit->coreSpillMask |= (1 << rPC);
1703 }
1704 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1705 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1706 /* We didn't pop to rPC, so must do a bv rLR */
1707 newLIR1(cUnit, kThumbBx, rLR);
1708 }
1709 }
1710
1711 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1712
1713 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07001714 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1715 oatClobberAllRegs(cUnit);
1716 }
buzbee67bf8852011-08-17 17:51:35 -07001717
1718 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1719 oatResetDefTracking(cUnit);
1720 }
1721
1722 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1723 handleExtendedMethodMIR(cUnit, mir);
1724 continue;
1725 }
1726
1727 cUnit->currentDalvikOffset = mir->offset;
1728
1729 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1730 InstructionFormat dalvikFormat =
1731 dexGetFormatFromOpcode(dalvikOpcode);
1732
1733 ArmLIR* boundaryLIR;
1734
1735 /* Mark the beginning of a Dalvik instruction for line tracking */
buzbee5abfa3e2012-01-31 17:01:43 -08001736 char* instStr = cUnit->printMe ?
buzbeeba938cb2012-02-03 14:47:55 -08001737 oatGetDalvikDisassembly(cUnit, &mir->dalvikInsn, "") : NULL;
buzbee67bf8852011-08-17 17:51:35 -07001738 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
buzbee5abfa3e2012-01-31 17:01:43 -08001739 (intptr_t) instStr);
buzbee85d8c1e2012-01-27 15:52:35 -08001740 cUnit->boundaryMap.insert(std::make_pair(mir->offset,
1741 (LIR*)boundaryLIR));
buzbee67bf8852011-08-17 17:51:35 -07001742 /* Remember the first LIR for this block */
1743 if (headLIR == NULL) {
1744 headLIR = boundaryLIR;
1745 /* Set the first boundaryLIR as a scheduling barrier */
1746 headLIR->defMask = ENCODE_ALL;
1747 }
1748
buzbee44b412b2012-02-04 08:50:53 -08001749 /* If we're compiling for the debugger, generate an update callout */
1750 if (cUnit->genDebugger) {
1751 genDebuggerUpdate(cUnit, mir->offset);
1752 }
1753
buzbee67bf8852011-08-17 17:51:35 -07001754 /* Don't generate the SSA annotation unless verbose mode is on */
1755 if (cUnit->printMe && mir->ssaRep) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -08001756 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
buzbee67bf8852011-08-17 17:51:35 -07001757 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1758 }
1759
1760 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1761
1762 if (notHandled) {
1763 char buf[100];
1764 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1765 mir->offset,
1766 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1767 dalvikFormat);
1768 LOG(FATAL) << buf;
1769 }
1770 }
1771
1772 if (headLIR) {
1773 /*
1774 * Eliminate redundant loads/stores and delay stores into later
1775 * slots
1776 */
1777 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1778 cUnit->lastLIRInsn);
1779
1780 /*
1781 * Generate an unconditional branch to the fallthrough block.
1782 */
1783 if (bb->fallThrough) {
1784 genUnconditionalBranch(cUnit,
1785 &labelList[bb->fallThrough->id]);
1786 }
1787 }
1788 return false;
1789}
1790
1791/*
1792 * Nop any unconditional branches that go to the next instruction.
1793 * Note: new redundant branches may be inserted later, and we'll
1794 * use a check in final instruction assembly to nop those out.
1795 */
1796void removeRedundantBranches(CompilationUnit* cUnit)
1797{
1798 ArmLIR* thisLIR;
1799
1800 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1801 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1802 thisLIR = NEXT_LIR(thisLIR)) {
1803
1804 /* Branch to the next instruction */
1805 if ((thisLIR->opcode == kThumbBUncond) ||
1806 (thisLIR->opcode == kThumb2BUncond)) {
1807 ArmLIR* nextLIR = thisLIR;
1808
1809 while (true) {
1810 nextLIR = NEXT_LIR(nextLIR);
1811
1812 /*
1813 * Is the branch target the next instruction?
1814 */
1815 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1816 thisLIR->flags.isNop = true;
1817 break;
1818 }
1819
1820 /*
1821 * Found real useful stuff between the branch and the target.
1822 * Need to explicitly check the lastLIRInsn here because it
1823 * might be the last real instruction.
1824 */
1825 if (!isPseudoOpcode(nextLIR->opcode) ||
1826 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1827 break;
1828 }
1829 }
1830 }
1831}
1832
buzbeeed3e9302011-09-23 17:34:19 -07001833STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07001834{
1835 ArmLIR** suspendLabel =
1836 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
1837 int numElems = cUnit->suspendLaunchpads.numUsed;
1838
1839 for (int i = 0; i < numElems; i++) {
1840 /* TUNING: move suspend count load into helper */
1841 ArmLIR* lab = suspendLabel[i];
1842 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
1843 cUnit->currentDalvikOffset = lab->operands[1];
1844 oatAppendLIR(cUnit, (LIR *)lab);
1845 loadWordDisp(cUnit, rSELF,
1846 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
buzbee44b412b2012-02-04 08:50:53 -08001847 if (!cUnit->genDebugger) {
1848 // use rSUSPEND for suspend count
1849 loadWordDisp(cUnit, rSELF,
1850 Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
1851 }
buzbeec1f45042011-09-21 16:03:19 -07001852 opReg(cUnit, kOpBlx, rLR);
buzbee44b412b2012-02-04 08:50:53 -08001853 if ( cUnit->genDebugger) {
1854 // use rSUSPEND for update debugger
1855 loadWordDisp(cUnit, rSELF,
1856 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), rSUSPEND);
1857 }
buzbeec1f45042011-09-21 16:03:19 -07001858 genUnconditionalBranch(cUnit, resumeLab);
1859 }
1860}
1861
buzbeeed3e9302011-09-23 17:34:19 -07001862STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07001863{
1864 ArmLIR** throwLabel =
1865 (ArmLIR **) cUnit->throwLaunchpads.elemList;
1866 int numElems = cUnit->throwLaunchpads.numUsed;
1867 int i;
1868
1869 for (i = 0; i < numElems; i++) {
1870 ArmLIR* lab = throwLabel[i];
1871 cUnit->currentDalvikOffset = lab->operands[1];
1872 oatAppendLIR(cUnit, (LIR *)lab);
1873 int funcOffset = 0;
1874 int v1 = lab->operands[2];
1875 int v2 = lab->operands[3];
1876 switch(lab->operands[0]) {
1877 case kArmThrowNullPointer:
1878 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
1879 break;
1880 case kArmThrowArrayBounds:
1881 if (v2 != r0) {
1882 genRegCopy(cUnit, r0, v1);
1883 genRegCopy(cUnit, r1, v2);
1884 } else {
1885 if (v1 == r1) {
1886 genRegCopy(cUnit, r12, v1);
1887 genRegCopy(cUnit, r1, v2);
1888 genRegCopy(cUnit, r0, r12);
1889 } else {
1890 genRegCopy(cUnit, r1, v2);
1891 genRegCopy(cUnit, r0, v1);
1892 }
1893 }
1894 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
1895 break;
1896 case kArmThrowDivZero:
1897 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
1898 break;
1899 case kArmThrowVerificationError:
1900 loadConstant(cUnit, r0, v1);
1901 loadConstant(cUnit, r1, v2);
1902 funcOffset =
1903 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
1904 break;
1905 case kArmThrowNegArraySize:
1906 genRegCopy(cUnit, r0, v1);
1907 funcOffset =
1908 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
1909 break;
buzbee5ade1d22011-09-09 14:44:52 -07001910 case kArmThrowNoSuchMethod:
buzbee13ac45a2012-01-12 12:30:16 -08001911 genRegCopy(cUnit, r0, v1);
buzbee5ade1d22011-09-09 14:44:52 -07001912 funcOffset =
1913 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
1914 break;
buzbeeec5adf32011-09-11 15:25:43 -07001915 case kArmThrowStackOverflow:
1916 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07001917 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07001918 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07001919 opRegImm(cUnit, kOpAdd, rSP,
1920 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07001921 break;
buzbee5ade1d22011-09-09 14:44:52 -07001922 default:
1923 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
1924 }
1925 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07001926 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001927 }
1928}
1929
buzbee67bf8852011-08-17 17:51:35 -07001930void oatMethodMIR2LIR(CompilationUnit* cUnit)
1931{
1932 /* Used to hold the labels of each block */
1933 cUnit->blockLabelList =
buzbeeba938cb2012-02-03 14:47:55 -08001934 (void *) oatNew(cUnit, sizeof(ArmLIR) * cUnit->numBlocks, true,
1935 kAllocLIR);
buzbee67bf8852011-08-17 17:51:35 -07001936
1937 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
1938 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07001939 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07001940
1941 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07001942
1943 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001944}
1945
1946/* Common initialization routine for an architecture family */
1947bool oatArchInit()
1948{
1949 int i;
1950
1951 for (i = 0; i < kArmLast; i++) {
1952 if (EncodingMap[i].opcode != i) {
1953 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
1954 " is wrong: expecting " << i << ", seeing " <<
1955 (int)EncodingMap[i].opcode;
1956 }
1957 }
1958
1959 return oatArchVariantInit();
1960}
1961
1962/* Needed by the Assembler */
1963void oatSetupResourceMasks(ArmLIR* lir)
1964{
1965 setupResourceMasks(lir);
1966}
1967
1968/* Needed by the ld/st optmizatons */
1969ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
1970{
1971 return genRegCopyNoInsert(cUnit, rDest, rSrc);
1972}
1973
1974/* Needed by the register allocator */
1975ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
1976{
1977 return genRegCopy(cUnit, rDest, rSrc);
1978}
1979
1980/* Needed by the register allocator */
1981void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
1982 int srcLo, int srcHi)
1983{
1984 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
1985}
1986
1987void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
1988 int displacement, int rSrc, OpSize size)
1989{
1990 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
1991}
1992
1993void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
1994 int displacement, int rSrcLo, int rSrcHi)
1995{
1996 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
1997}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001998
1999} // namespace art