blob: 4efa27ab3bd74a3468385fb9ec5de17333938321 [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,
jeffhao8cd6dda2012-02-22 10:15:34 -0800180 isReferrersClass, isVolatile, true);
Ian Rogers1bddec32012-02-04 12:27:34 -0800181 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,
jeffhao8cd6dda2012-02-22 10:15:34 -0800274 isReferrersClass, isVolatile, false);
Ian Rogers1bddec32012-02-04 12:27:34 -0800275 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 Rogersc8b306f2012-02-17 21:34:44 -0800472STATIC int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
473 int state, uint32_t dexIdx, uint32_t methodIdx)
buzbee4a3164f2011-09-03 11:25:10 -0700474{
buzbee4a3164f2011-09-03 11:25:10 -0700475 /*
476 * This handles the case in which the base method is not fully
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800477 * resolved at compile time, we bail to a runtime helper.
buzbee4a3164f2011-09-03 11:25:10 -0700478 */
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800479 if (state == 0) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800480 // Load trampoline target
481 loadWordDisp(cUnit, rSELF, trampoline, rLR);
482 // Load r0 with method index
483 loadConstant(cUnit, r0, dexIdx);
484 return 1;
buzbee4a3164f2011-09-03 11:25:10 -0700485 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800486 return -1;
487}
488
Ian Rogersc8b306f2012-02-17 21:34:44 -0800489STATIC int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
490 int state, uint32_t dexIdx, uint32_t methodIdx)
491{
492 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeStaticTrampolineWithAccessCheck);
493 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
494}
495
496STATIC int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir,
497 int state, uint32_t dexIdx, uint32_t methodIdx)
498{
499 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeDirectTrampolineWithAccessCheck);
500 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
501}
502
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800503STATIC int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
504 int state, uint32_t dexIdx, uint32_t methodIdx)
505{
Ian Rogersc8b306f2012-02-17 21:34:44 -0800506 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
507 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800508}
509
510STATIC int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
511 int state, uint32_t dexIdx, uint32_t methodIdx)
512{
Ian Rogersc8b306f2012-02-17 21:34:44 -0800513 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
514 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800515}
516
517/*
518 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
519 * which will locate the target and continue on via a tail call.
520 */
521STATIC int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
522 int state, uint32_t dexIdx, uint32_t unused)
523{
Ian Rogersc8b306f2012-02-17 21:34:44 -0800524 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
525 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800526}
527
528STATIC int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit,
529 MIR* mir, int state,
530 uint32_t dexIdx,
531 uint32_t unused)
532{
Ian Rogersc8b306f2012-02-17 21:34:44 -0800533 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
534 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800535}
536
537STATIC int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
538 DecodedInstruction* dInsn, int callState,
539 NextCallInsn nextCallInsn, uint32_t dexIdx,
540 uint32_t methodIdx, bool skipThis)
541{
542 int nextReg = r1;
543 int nextArg = 0;
544 if (skipThis) {
545 nextReg++;
546 nextArg++;
547 }
548 for (; (nextReg <= r3) && (nextArg < mir->ssaRep->numUses); nextReg++) {
549 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
550 rlArg = oatUpdateRawLoc(cUnit, rlArg);
551 if (rlArg.wide && (nextReg <= r2)) {
552 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
553 nextReg++;
554 nextArg++;
555 } else {
556 rlArg.wide = false;
557 loadValueDirectFixed(cUnit, rlArg, nextReg);
558 }
559 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
560 }
561 return callState;
buzbee67bf8852011-08-17 17:51:35 -0700562}
563
564/*
565 * Load up to 5 arguments, the first three of which will be in
566 * r1 .. r3. On entry r0 contains the current method pointer,
567 * and as part of the load sequence, it must be replaced with
568 * the target method pointer. Note, this may also be called
569 * for "range" variants if the number of arguments is 5 or fewer.
570 */
buzbeeed3e9302011-09-23 17:34:19 -0700571STATIC int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700572 DecodedInstruction* dInsn, int callState,
Ian Rogersc8b306f2012-02-17 21:34:44 -0800573 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
574 uint32_t dexIdx, uint32_t methodIdx,
575 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700576{
577 RegLocation rlArg;
buzbee67bf8852011-08-17 17:51:35 -0700578
579 /* If no arguments, just return */
580 if (dInsn->vA == 0)
581 return callState;
582
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800583 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700584
buzbeec0ecd652011-09-25 18:11:54 -0700585 DCHECK_LE(dInsn->vA, 5U);
586 if (dInsn->vA > 3) {
587 uint32_t nextUse = 3;
588 //Detect special case of wide arg spanning arg3/arg4
589 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
590 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
591 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
592 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
593 rlUse2.wide) {
594 int reg;
595 // Wide spans, we need the 2nd half of uses[2].
596 rlArg = oatUpdateLocWide(cUnit, rlUse2);
597 if (rlArg.location == kLocPhysReg) {
598 reg = rlArg.highReg;
599 } else {
600 // r2 & r3 can safely be used here
601 reg = r3;
buzbee67bc2362011-10-11 18:08:40 -0700602 loadWordDisp(cUnit, rSP,
603 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800604 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
605 methodIdx);
buzbeec0ecd652011-09-25 18:11:54 -0700606 }
607 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
608 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800609 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbeec0ecd652011-09-25 18:11:54 -0700610 nextUse++;
611 }
612 // Loop through the rest
613 while (nextUse < dInsn->vA) {
614 int lowReg;
615 int highReg;
616 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
617 rlArg = oatUpdateRawLoc(cUnit, rlArg);
618 if (rlArg.location == kLocPhysReg) {
619 lowReg = rlArg.lowReg;
620 highReg = rlArg.highReg;
621 } else {
622 lowReg = r2;
623 highReg = r3;
624 if (rlArg.wide) {
625 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
626 } else {
627 loadValueDirectFixed(cUnit, rlArg, lowReg);
628 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800629 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
630 methodIdx);
buzbeec0ecd652011-09-25 18:11:54 -0700631 }
632 int outsOffset = (nextUse + 1) * 4;
633 if (rlArg.wide) {
634 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
635 nextUse += 2;
636 } else {
637 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
638 nextUse++;
639 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800640 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700641 }
buzbee67bf8852011-08-17 17:51:35 -0700642 }
643
buzbeec0ecd652011-09-25 18:11:54 -0700644 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800645 dexIdx, methodIdx, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700646
buzbee67bf8852011-08-17 17:51:35 -0700647 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700648 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700649 }
650 return callState;
651}
652
653/*
654 * May have 0+ arguments (also used for jumbo). Note that
655 * source virtual registers may be in physical registers, so may
656 * need to be flushed to home location before copying. This
657 * applies to arg3 and above (see below).
658 *
659 * Two general strategies:
660 * If < 20 arguments
661 * Pass args 3-18 using vldm/vstm block copy
662 * Pass arg0, arg1 & arg2 in r1-r3
663 * If 20+ arguments
664 * Pass args arg19+ using memcpy block copy
665 * Pass arg0, arg1 & arg2 in r1-r3
666 *
667 */
buzbeeed3e9302011-09-23 17:34:19 -0700668STATIC int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700669 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700670 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800671 uint32_t dexIdx, uint32_t methodIdx,
672 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700673{
674 int firstArg = dInsn->vC;
675 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700676
buzbee67bf8852011-08-17 17:51:35 -0700677 // If we can treat it as non-range (Jumbo ops will use range form)
678 if (numArgs <= 5)
679 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
Ian Rogersc8b306f2012-02-17 21:34:44 -0800680 nextCallInsn, dexIdx, methodIdx,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800681 skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700682 /*
683 * Make sure range list doesn't span the break between in normal
684 * Dalvik vRegs and the ins.
685 */
buzbee1b4c8592011-08-31 10:43:51 -0700686 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800687 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
buzbee1b4c8592011-08-31 10:43:51 -0700688 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
689 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700690 }
691
692 /*
693 * First load the non-register arguments. Both forms expect all
694 * of the source arguments to be in their home frame location, so
695 * scan the sReg names and flush any that have been promoted to
696 * frame backing storage.
697 */
698 // Scan the rest of the args - if in physReg flush to memory
buzbeec0ecd652011-09-25 18:11:54 -0700699 for (int nextArg = 0; nextArg < numArgs;) {
700 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
buzbee1b4c8592011-08-31 10:43:51 -0700701 if (loc.wide) {
702 loc = oatUpdateLocWide(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700703 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700704 storeBaseDispWide(cUnit, rSP,
705 oatSRegOffset(cUnit, loc.sRegLow),
706 loc.lowReg, loc.highReg);
buzbee1b4c8592011-08-31 10:43:51 -0700707 }
buzbeec0ecd652011-09-25 18:11:54 -0700708 nextArg += 2;
buzbee1b4c8592011-08-31 10:43:51 -0700709 } else {
710 loc = oatUpdateLoc(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700711 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700712 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
713 loc.lowReg, kWord);
buzbee1b4c8592011-08-31 10:43:51 -0700714 }
buzbeec0ecd652011-09-25 18:11:54 -0700715 nextArg++;
buzbee67bf8852011-08-17 17:51:35 -0700716 }
717 }
718
buzbee67bc2362011-10-11 18:08:40 -0700719 int startOffset = oatSRegOffset(cUnit,
720 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
buzbee67bf8852011-08-17 17:51:35 -0700721 int outsOffset = 4 /* Method* */ + (3 * 4);
722 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700723 // Generate memcpy
724 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
725 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700726 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
727 loadConstant(cUnit, r2, (numArgs - 3) * 4);
Ian Rogersff1ed472011-09-20 13:46:24 -0700728 callRuntimeHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700729 // Restore Method*
730 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700731 } else {
732 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700733 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800734 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700735 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700736 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
737 //TUNING: loosen barrier
738 ld->defMask = ENCODE_ALL;
739 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800740 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700741 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800742 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbeef48e9712011-09-15 17:54:28 -0700743 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
744 setMemRefType(st, false /* isLoad */, kDalvikReg);
745 st->defMask = ENCODE_ALL;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800746 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee67bf8852011-08-17 17:51:35 -0700747 }
748
buzbeec0ecd652011-09-25 18:11:54 -0700749 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800750 dexIdx, methodIdx, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700751
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800752 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
buzbee99f27232011-10-05 12:56:36 -0700753 if (pcrLabel) {
754 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
755 }
buzbee67bf8852011-08-17 17:51:35 -0700756 return callState;
757}
758
buzbee2a475e72011-09-07 17:19:17 -0700759// Debugging routine - if null target, branch to DebugMe
buzbeeed3e9302011-09-23 17:34:19 -0700760STATIC void genShowTarget(CompilationUnit* cUnit)
buzbee2a475e72011-09-07 17:19:17 -0700761{
762 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
763 loadWordDisp(cUnit, rSELF,
764 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
765 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
766 target->defMask = -1;
767 branchOver->generic.target = (LIR*)target;
768}
buzbee2a475e72011-09-07 17:19:17 -0700769
Ian Rogersc8b306f2012-02-17 21:34:44 -0800770STATIC void genInvoke(CompilationUnit* cUnit, MIR* mir,
771 InvokeType type, bool isRange)
buzbee67bf8852011-08-17 17:51:35 -0700772{
773 DecodedInstruction* dInsn = &mir->dalvikInsn;
774 int callState = 0;
775 ArmLIR* nullCk;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800776 ArmLIR** pNullCk = NULL;
buzbee561227c2011-09-02 15:28:19 -0700777 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700778 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -0700779 // Explicit register usage
780 oatLockCallTemps(cUnit);
buzbee34c77ad2012-01-11 13:01:32 -0800781
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800782 uint32_t dexMethodIdx = dInsn->vB;
783 int vtableIdx;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800784 bool skipThis;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800785 bool fastPath =
Ian Rogersc8b306f2012-02-17 21:34:44 -0800786 cUnit->compiler->ComputeInvokeInfo(dexMethodIdx, cUnit, type,
787 vtableIdx)
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800788 && !SLOW_INVOKE_PATH;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800789 if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800790 nextCallInsn = fastPath ? nextInterfaceCallInsn
791 : nextInterfaceCallInsnWithAccessCheck;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800792 skipThis = false;
793 } else if (type == kDirect) {
794 if (fastPath) {
795 pNullCk = &nullCk;
796 }
797 nextCallInsn = fastPath ? nextSDCallInsn : nextDirectCallInsnSP;
798 skipThis = false;
799 } else if (type == kStatic) {
800 nextCallInsn = fastPath ? nextSDCallInsn : nextStaticCallInsnSP;
801 skipThis = false;
802 } else if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800803 nextCallInsn = fastPath ? nextSuperCallInsn : nextSuperCallInsnSP;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800804 skipThis = fastPath;
buzbee561227c2011-09-02 15:28:19 -0700805 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800806 DCHECK_EQ(type, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800807 nextCallInsn = fastPath ? nextVCallInsn : nextVCallInsnSP;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800808 skipThis = fastPath;
buzbee561227c2011-09-02 15:28:19 -0700809 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800810 if (!isRange) {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800811 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
812 nextCallInsn, dexMethodIdx,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800813 vtableIdx, skipThis);
814 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800815 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800816 nextCallInsn, dexMethodIdx, vtableIdx,
817 skipThis);
818 }
buzbee67bf8852011-08-17 17:51:35 -0700819 // Finish up any of the call sequence not interleaved in arg loading
820 while (callState >= 0) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800821 callState = nextCallInsn(cUnit, mir, callState, dexMethodIdx,
822 vtableIdx);
buzbee67bf8852011-08-17 17:51:35 -0700823 }
buzbeece302932011-10-04 14:32:18 -0700824 if (DISPLAY_MISSING_TARGETS) {
825 genShowTarget(cUnit);
826 }
buzbeeec5adf32011-09-11 15:25:43 -0700827 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -0700828 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700829}
830
buzbeeed3e9302011-09-23 17:34:19 -0700831STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700832 BasicBlock* bb, ArmLIR* labelList)
833{
834 bool res = false; // Assume success
835 RegLocation rlSrc[3];
836 RegLocation rlDest = badLoc;
837 RegLocation rlResult = badLoc;
838 Opcode opcode = mir->dalvikInsn.opcode;
839
840 /* Prep Src and Dest locations */
841 int nextSreg = 0;
842 int nextLoc = 0;
843 int attrs = oatDataFlowAttributes[opcode];
844 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
845 if (attrs & DF_UA) {
846 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
847 nextSreg++;
848 } else if (attrs & DF_UA_WIDE) {
849 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
850 nextSreg + 1);
851 nextSreg+= 2;
852 }
853 if (attrs & DF_UB) {
854 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
855 nextSreg++;
856 } else if (attrs & DF_UB_WIDE) {
857 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
858 nextSreg + 1);
859 nextSreg+= 2;
860 }
861 if (attrs & DF_UC) {
862 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
863 } else if (attrs & DF_UC_WIDE) {
864 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
865 nextSreg + 1);
866 }
867 if (attrs & DF_DA) {
868 rlDest = oatGetDest(cUnit, mir, 0);
869 } else if (attrs & DF_DA_WIDE) {
870 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
871 }
872
873 switch(opcode) {
874 case OP_NOP:
875 break;
876
877 case OP_MOVE_EXCEPTION:
878 int exOffset;
879 int resetReg;
buzbeec143c552011-08-20 17:38:58 -0700880 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700881 resetReg = oatAllocTemp(cUnit);
882 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
883 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
884 loadConstant(cUnit, resetReg, 0);
885 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
886 storeValue(cUnit, rlDest, rlResult);
887 break;
888
889 case OP_RETURN_VOID:
buzbeefe2e17f2011-10-10 09:35:02 -0700890 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -0700891 break;
892
893 case OP_RETURN:
894 case OP_RETURN_OBJECT:
buzbeefe2e17f2011-10-10 09:35:02 -0700895 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -0700896 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -0700897 break;
898
899 case OP_RETURN_WIDE:
buzbeefe2e17f2011-10-10 09:35:02 -0700900 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -0700901 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -0700902 break;
903
904 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -0700905 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -0700906 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -0700907 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -0700908 break;
909
910 case OP_MOVE_RESULT:
911 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -0700912 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -0700913 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -0700914 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -0700915 break;
916
917 case OP_MOVE:
918 case OP_MOVE_OBJECT:
919 case OP_MOVE_16:
920 case OP_MOVE_OBJECT_16:
921 case OP_MOVE_FROM16:
922 case OP_MOVE_OBJECT_FROM16:
923 storeValue(cUnit, rlDest, rlSrc[0]);
924 break;
925
926 case OP_MOVE_WIDE:
927 case OP_MOVE_WIDE_16:
928 case OP_MOVE_WIDE_FROM16:
929 storeValueWide(cUnit, rlDest, rlSrc[0]);
930 break;
931
932 case OP_CONST:
933 case OP_CONST_4:
934 case OP_CONST_16:
935 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
936 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
937 storeValue(cUnit, rlDest, rlResult);
938 break;
939
940 case OP_CONST_HIGH16:
941 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
942 loadConstantNoClobber(cUnit, rlResult.lowReg,
943 mir->dalvikInsn.vB << 16);
944 storeValue(cUnit, rlDest, rlResult);
945 break;
946
947 case OP_CONST_WIDE_16:
948 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -0700949 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
950 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
951 mir->dalvikInsn.vB,
952 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -0700953 storeValueWide(cUnit, rlDest, rlResult);
954 break;
955
956 case OP_CONST_WIDE:
957 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
958 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -0700959 mir->dalvikInsn.vB_wide & 0xffffffff,
960 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -0700961 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700962 break;
963
964 case OP_CONST_WIDE_HIGH16:
965 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
966 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
967 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -0700968 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700969 break;
970
971 case OP_MONITOR_ENTER:
972 genMonitorEnter(cUnit, mir, rlSrc[0]);
973 break;
974
975 case OP_MONITOR_EXIT:
976 genMonitorExit(cUnit, mir, rlSrc[0]);
977 break;
978
979 case OP_CHECK_CAST:
980 genCheckCast(cUnit, mir, rlSrc[0]);
981 break;
982
983 case OP_INSTANCE_OF:
984 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
985 break;
986
987 case OP_NEW_INSTANCE:
988 genNewInstance(cUnit, mir, rlDest);
989 break;
990
991 case OP_THROW:
992 genThrow(cUnit, mir, rlSrc[0]);
993 break;
994
buzbee5ade1d22011-09-09 14:44:52 -0700995 case OP_THROW_VERIFICATION_ERROR:
996 loadWordDisp(cUnit, rSELF,
997 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
998 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
999 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001000 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001001 break;
1002
buzbee67bf8852011-08-17 17:51:35 -07001003 case OP_ARRAY_LENGTH:
1004 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001005 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001006 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001007 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001008 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1009 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1010 rlResult.lowReg);
1011 storeValue(cUnit, rlDest, rlResult);
1012 break;
1013
1014 case OP_CONST_STRING:
1015 case OP_CONST_STRING_JUMBO:
1016 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1017 break;
1018
1019 case OP_CONST_CLASS:
1020 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1021 break;
1022
1023 case OP_FILL_ARRAY_DATA:
1024 genFillArrayData(cUnit, mir, rlSrc[0]);
1025 break;
1026
1027 case OP_FILLED_NEW_ARRAY:
1028 genFilledNewArray(cUnit, mir, false /* not range */);
1029 break;
1030
1031 case OP_FILLED_NEW_ARRAY_RANGE:
1032 genFilledNewArray(cUnit, mir, true /* range */);
1033 break;
1034
1035 case OP_NEW_ARRAY:
1036 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1037 break;
1038
1039 case OP_GOTO:
1040 case OP_GOTO_16:
1041 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001042 if (bb->taken->startOffset <= mir->offset) {
1043 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001044 }
1045 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1046 break;
1047
1048 case OP_PACKED_SWITCH:
1049 genPackedSwitch(cUnit, mir, rlSrc[0]);
1050 break;
1051
1052 case OP_SPARSE_SWITCH:
1053 genSparseSwitch(cUnit, mir, rlSrc[0]);
1054 break;
1055
1056 case OP_CMPL_FLOAT:
1057 case OP_CMPG_FLOAT:
1058 case OP_CMPL_DOUBLE:
1059 case OP_CMPG_DOUBLE:
1060 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1061 break;
1062
1063 case OP_CMP_LONG:
1064 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1065 break;
1066
1067 case OP_IF_EQ:
1068 case OP_IF_NE:
1069 case OP_IF_LT:
1070 case OP_IF_GE:
1071 case OP_IF_GT:
1072 case OP_IF_LE: {
1073 bool backwardBranch;
1074 ArmConditionCode cond;
1075 backwardBranch = (bb->taken->startOffset <= mir->offset);
1076 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001077 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001078 }
1079 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1080 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1081 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1082 switch(opcode) {
1083 case OP_IF_EQ:
1084 cond = kArmCondEq;
1085 break;
1086 case OP_IF_NE:
1087 cond = kArmCondNe;
1088 break;
1089 case OP_IF_LT:
1090 cond = kArmCondLt;
1091 break;
1092 case OP_IF_GE:
1093 cond = kArmCondGe;
1094 break;
1095 case OP_IF_GT:
1096 cond = kArmCondGt;
1097 break;
1098 case OP_IF_LE:
1099 cond = kArmCondLe;
1100 break;
1101 default:
1102 cond = (ArmConditionCode)0;
1103 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1104 }
1105 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1106 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1107 break;
1108 }
1109
1110 case OP_IF_EQZ:
1111 case OP_IF_NEZ:
1112 case OP_IF_LTZ:
1113 case OP_IF_GEZ:
1114 case OP_IF_GTZ:
1115 case OP_IF_LEZ: {
1116 bool backwardBranch;
1117 ArmConditionCode cond;
1118 backwardBranch = (bb->taken->startOffset <= mir->offset);
1119 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001120 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001121 }
1122 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1123 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1124 switch(opcode) {
1125 case OP_IF_EQZ:
1126 cond = kArmCondEq;
1127 break;
1128 case OP_IF_NEZ:
1129 cond = kArmCondNe;
1130 break;
1131 case OP_IF_LTZ:
1132 cond = kArmCondLt;
1133 break;
1134 case OP_IF_GEZ:
1135 cond = kArmCondGe;
1136 break;
1137 case OP_IF_GTZ:
1138 cond = kArmCondGt;
1139 break;
1140 case OP_IF_LEZ:
1141 cond = kArmCondLe;
1142 break;
1143 default:
1144 cond = (ArmConditionCode)0;
1145 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1146 }
1147 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1148 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1149 break;
1150 }
1151
1152 case OP_AGET_WIDE:
1153 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1154 break;
1155 case OP_AGET:
1156 case OP_AGET_OBJECT:
1157 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1158 break;
1159 case OP_AGET_BOOLEAN:
1160 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1161 rlDest, 0);
1162 break;
1163 case OP_AGET_BYTE:
1164 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1165 break;
1166 case OP_AGET_CHAR:
1167 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1168 rlDest, 1);
1169 break;
1170 case OP_AGET_SHORT:
1171 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1172 break;
1173 case OP_APUT_WIDE:
1174 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1175 break;
1176 case OP_APUT:
1177 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1178 break;
1179 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001180 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001181 break;
1182 case OP_APUT_SHORT:
1183 case OP_APUT_CHAR:
1184 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1185 rlSrc[0], 1);
1186 break;
1187 case OP_APUT_BYTE:
1188 case OP_APUT_BOOLEAN:
1189 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1190 rlSrc[0], 0);
1191 break;
1192
Ian Rogers1bddec32012-02-04 12:27:34 -08001193 case OP_IGET_OBJECT:
1194 case OP_IGET_OBJECT_VOLATILE:
1195 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, true);
1196 break;
1197
buzbee67bf8852011-08-17 17:51:35 -07001198 case OP_IGET_WIDE:
1199 case OP_IGET_WIDE_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001200 genIGet(cUnit, mir, kLong, rlDest, rlSrc[0], true, false);
buzbee67bf8852011-08-17 17:51:35 -07001201 break;
1202
1203 case OP_IGET:
1204 case OP_IGET_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001205 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, false);
1206 break;
1207
1208 case OP_IGET_CHAR:
1209 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0], false, false);
1210 break;
1211
1212 case OP_IGET_SHORT:
1213 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001214 break;
1215
1216 case OP_IGET_BOOLEAN:
1217 case OP_IGET_BYTE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001218 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001219 break;
1220
1221 case OP_IPUT_WIDE:
1222 case OP_IPUT_WIDE_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001223 genIPut(cUnit, mir, kLong, rlSrc[0], rlSrc[1], true, false);
buzbee67bf8852011-08-17 17:51:35 -07001224 break;
1225
1226 case OP_IPUT_OBJECT:
1227 case OP_IPUT_OBJECT_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001228 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, true);
buzbee67bf8852011-08-17 17:51:35 -07001229 break;
1230
1231 case OP_IPUT:
1232 case OP_IPUT_VOLATILE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001233 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001234 break;
1235
1236 case OP_IPUT_BOOLEAN:
1237 case OP_IPUT_BYTE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001238 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001239 break;
1240
1241 case OP_IPUT_CHAR:
Ian Rogers1bddec32012-02-04 12:27:34 -08001242 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001243 break;
1244
1245 case OP_IPUT_SHORT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001246 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001247 break;
1248
buzbee67bf8852011-08-17 17:51:35 -07001249 case OP_SGET_OBJECT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001250 genSget(cUnit, mir, rlDest, false, true);
1251 break;
1252 case OP_SGET:
buzbee67bf8852011-08-17 17:51:35 -07001253 case OP_SGET_BOOLEAN:
1254 case OP_SGET_BYTE:
1255 case OP_SGET_CHAR:
1256 case OP_SGET_SHORT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001257 genSget(cUnit, mir, rlDest, false, false);
buzbee67bf8852011-08-17 17:51:35 -07001258 break;
1259
1260 case OP_SGET_WIDE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001261 genSget(cUnit, mir, rlDest, true, false);
buzbee67bf8852011-08-17 17:51:35 -07001262 break;
1263
buzbee67bf8852011-08-17 17:51:35 -07001264 case OP_SPUT_OBJECT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001265 genSput(cUnit, mir, rlSrc[0], false, true);
1266 break;
1267
1268 case OP_SPUT:
buzbee67bf8852011-08-17 17:51:35 -07001269 case OP_SPUT_BOOLEAN:
1270 case OP_SPUT_BYTE:
1271 case OP_SPUT_CHAR:
1272 case OP_SPUT_SHORT:
Ian Rogers1bddec32012-02-04 12:27:34 -08001273 genSput(cUnit, mir, rlSrc[0], false, false);
buzbee67bf8852011-08-17 17:51:35 -07001274 break;
1275
1276 case OP_SPUT_WIDE:
Ian Rogers1bddec32012-02-04 12:27:34 -08001277 genSput(cUnit, mir, rlSrc[0], true, false);
buzbee67bf8852011-08-17 17:51:35 -07001278 break;
1279
1280 case OP_INVOKE_STATIC_RANGE:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001281 genInvoke(cUnit, mir, kStatic, true /*range*/);
buzbee561227c2011-09-02 15:28:19 -07001282 break;
buzbee67bf8852011-08-17 17:51:35 -07001283 case OP_INVOKE_STATIC:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001284 genInvoke(cUnit, mir, kStatic, false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001285 break;
1286
1287 case OP_INVOKE_DIRECT:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001288 genInvoke(cUnit, mir, kDirect, false /*range*/);
buzbee561227c2011-09-02 15:28:19 -07001289 break;
buzbee67bf8852011-08-17 17:51:35 -07001290 case OP_INVOKE_DIRECT_RANGE:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001291 genInvoke(cUnit, mir, kDirect, true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001292 break;
1293
1294 case OP_INVOKE_VIRTUAL:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001295 genInvoke(cUnit, mir, kVirtual, false /*range*/);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001296 break;
buzbee67bf8852011-08-17 17:51:35 -07001297 case OP_INVOKE_VIRTUAL_RANGE:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001298 genInvoke(cUnit, mir, kVirtual, true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001299 break;
1300
1301 case OP_INVOKE_SUPER:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001302 genInvoke(cUnit, mir, kSuper, false /*range*/);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001303 break;
buzbee67bf8852011-08-17 17:51:35 -07001304 case OP_INVOKE_SUPER_RANGE:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001305 genInvoke(cUnit, mir, kSuper, true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001306 break;
1307
1308 case OP_INVOKE_INTERFACE:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001309 genInvoke(cUnit, mir, kInterface, false /*range*/);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001310 break;
buzbee67bf8852011-08-17 17:51:35 -07001311 case OP_INVOKE_INTERFACE_RANGE:
Ian Rogersc8b306f2012-02-17 21:34:44 -08001312 genInvoke(cUnit, mir, kInterface, true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001313 break;
1314
1315 case OP_NEG_INT:
1316 case OP_NOT_INT:
1317 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1318 break;
1319
1320 case OP_NEG_LONG:
1321 case OP_NOT_LONG:
1322 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1323 break;
1324
1325 case OP_NEG_FLOAT:
1326 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1327 break;
1328
1329 case OP_NEG_DOUBLE:
1330 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1331 break;
1332
1333 case OP_INT_TO_LONG:
1334 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1335 if (rlSrc[0].location == kLocPhysReg) {
1336 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1337 } else {
1338 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1339 }
1340 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1341 rlResult.lowReg, 31);
1342 storeValueWide(cUnit, rlDest, rlResult);
1343 break;
1344
1345 case OP_LONG_TO_INT:
1346 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1347 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1348 storeValue(cUnit, rlDest, rlSrc[0]);
1349 break;
1350
1351 case OP_INT_TO_BYTE:
1352 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1353 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1354 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1355 storeValue(cUnit, rlDest, rlResult);
1356 break;
1357
1358 case OP_INT_TO_SHORT:
1359 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1360 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1361 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1362 storeValue(cUnit, rlDest, rlResult);
1363 break;
1364
1365 case OP_INT_TO_CHAR:
1366 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1367 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1368 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1369 storeValue(cUnit, rlDest, rlResult);
1370 break;
1371
1372 case OP_INT_TO_FLOAT:
1373 case OP_INT_TO_DOUBLE:
1374 case OP_LONG_TO_FLOAT:
1375 case OP_LONG_TO_DOUBLE:
1376 case OP_FLOAT_TO_INT:
1377 case OP_FLOAT_TO_LONG:
1378 case OP_FLOAT_TO_DOUBLE:
1379 case OP_DOUBLE_TO_INT:
1380 case OP_DOUBLE_TO_LONG:
1381 case OP_DOUBLE_TO_FLOAT:
1382 genConversion(cUnit, mir);
1383 break;
1384
1385 case OP_ADD_INT:
1386 case OP_SUB_INT:
1387 case OP_MUL_INT:
1388 case OP_DIV_INT:
1389 case OP_REM_INT:
1390 case OP_AND_INT:
1391 case OP_OR_INT:
1392 case OP_XOR_INT:
1393 case OP_SHL_INT:
1394 case OP_SHR_INT:
1395 case OP_USHR_INT:
1396 case OP_ADD_INT_2ADDR:
1397 case OP_SUB_INT_2ADDR:
1398 case OP_MUL_INT_2ADDR:
1399 case OP_DIV_INT_2ADDR:
1400 case OP_REM_INT_2ADDR:
1401 case OP_AND_INT_2ADDR:
1402 case OP_OR_INT_2ADDR:
1403 case OP_XOR_INT_2ADDR:
1404 case OP_SHL_INT_2ADDR:
1405 case OP_SHR_INT_2ADDR:
1406 case OP_USHR_INT_2ADDR:
1407 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1408 break;
1409
1410 case OP_ADD_LONG:
1411 case OP_SUB_LONG:
1412 case OP_MUL_LONG:
1413 case OP_DIV_LONG:
1414 case OP_REM_LONG:
1415 case OP_AND_LONG:
1416 case OP_OR_LONG:
1417 case OP_XOR_LONG:
1418 case OP_ADD_LONG_2ADDR:
1419 case OP_SUB_LONG_2ADDR:
1420 case OP_MUL_LONG_2ADDR:
1421 case OP_DIV_LONG_2ADDR:
1422 case OP_REM_LONG_2ADDR:
1423 case OP_AND_LONG_2ADDR:
1424 case OP_OR_LONG_2ADDR:
1425 case OP_XOR_LONG_2ADDR:
1426 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1427 break;
1428
buzbee67bf8852011-08-17 17:51:35 -07001429 case OP_SHL_LONG:
1430 case OP_SHR_LONG:
1431 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001432 case OP_SHL_LONG_2ADDR:
1433 case OP_SHR_LONG_2ADDR:
1434 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001435 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1436 break;
1437
1438 case OP_ADD_FLOAT:
1439 case OP_SUB_FLOAT:
1440 case OP_MUL_FLOAT:
1441 case OP_DIV_FLOAT:
1442 case OP_REM_FLOAT:
1443 case OP_ADD_FLOAT_2ADDR:
1444 case OP_SUB_FLOAT_2ADDR:
1445 case OP_MUL_FLOAT_2ADDR:
1446 case OP_DIV_FLOAT_2ADDR:
1447 case OP_REM_FLOAT_2ADDR:
1448 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1449 break;
1450
1451 case OP_ADD_DOUBLE:
1452 case OP_SUB_DOUBLE:
1453 case OP_MUL_DOUBLE:
1454 case OP_DIV_DOUBLE:
1455 case OP_REM_DOUBLE:
1456 case OP_ADD_DOUBLE_2ADDR:
1457 case OP_SUB_DOUBLE_2ADDR:
1458 case OP_MUL_DOUBLE_2ADDR:
1459 case OP_DIV_DOUBLE_2ADDR:
1460 case OP_REM_DOUBLE_2ADDR:
1461 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1462 break;
1463
1464 case OP_RSUB_INT:
1465 case OP_ADD_INT_LIT16:
1466 case OP_MUL_INT_LIT16:
1467 case OP_DIV_INT_LIT16:
1468 case OP_REM_INT_LIT16:
1469 case OP_AND_INT_LIT16:
1470 case OP_OR_INT_LIT16:
1471 case OP_XOR_INT_LIT16:
1472 case OP_ADD_INT_LIT8:
1473 case OP_RSUB_INT_LIT8:
1474 case OP_MUL_INT_LIT8:
1475 case OP_DIV_INT_LIT8:
1476 case OP_REM_INT_LIT8:
1477 case OP_AND_INT_LIT8:
1478 case OP_OR_INT_LIT8:
1479 case OP_XOR_INT_LIT8:
1480 case OP_SHL_INT_LIT8:
1481 case OP_SHR_INT_LIT8:
1482 case OP_USHR_INT_LIT8:
1483 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1484 break;
1485
1486 default:
1487 res = true;
1488 }
1489 return res;
1490}
1491
Elliott Hughesc1f143d2011-12-01 17:31:10 -08001492STATIC const char* extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001493 "kMirOpPhi",
1494 "kMirOpNullNRangeUpCheck",
1495 "kMirOpNullNRangeDownCheck",
1496 "kMirOpLowerBound",
1497 "kMirOpPunt",
1498 "kMirOpCheckInlinePrediction",
1499};
1500
1501/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001502STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001503{
1504 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
buzbee5abfa3e2012-01-31 17:01:43 -08001505 char* msg = NULL;
1506 if (cUnit->printMe) {
buzbeeba938cb2012-02-03 14:47:55 -08001507 msg = (char*)oatNew(cUnit, strlen(extendedMIROpNames[opOffset]) + 1,
1508 false, kAllocDebugInfo);
buzbee5abfa3e2012-01-31 17:01:43 -08001509 strcpy(msg, extendedMIROpNames[opOffset]);
1510 }
buzbee67bf8852011-08-17 17:51:35 -07001511 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1512
1513 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1514 case kMirOpPhi: {
buzbee5abfa3e2012-01-31 17:01:43 -08001515 char* ssaString = NULL;
1516 if (cUnit->printMe) {
1517 ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1518 }
buzbee67bf8852011-08-17 17:51:35 -07001519 op->flags.isNop = true;
1520 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1521 break;
1522 }
1523 default:
1524 break;
1525 }
1526}
1527
buzbee67bc2362011-10-11 18:08:40 -07001528/*
1529 * If there are any ins passed in registers that have not been promoted
1530 * to a callee-save register, flush them to the frame. Perform intial
1531 * assignment of promoted arguments.
1532 */
buzbeeed3e9302011-09-23 17:34:19 -07001533STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001534{
Ian Rogersa3760aa2011-11-14 14:32:37 -08001535 if (cUnit->numIns == 0)
buzbee67bf8852011-08-17 17:51:35 -07001536 return;
buzbee67bc2362011-10-11 18:08:40 -07001537 int firstArgReg = r1;
1538 int lastArgReg = r3;
Ian Rogersa3760aa2011-11-14 14:32:37 -08001539 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
buzbee8febc582011-10-25 12:39:20 -07001540 /*
1541 * Arguments passed in registers should be flushed
1542 * to their backing locations in the frame for now.
1543 * Also, we need to do initial assignment for promoted
1544 * arguments. NOTE: an older version of dx had an issue
1545 * in which it would reuse static method argument registers.
1546 * This could result in the same Dalvik virtual register
1547 * being promoted to both core and fp regs. In those
1548 * cases, copy argument to both. This will be uncommon
1549 * enough that it isn't worth attempting to optimize.
1550 */
Ian Rogersa3760aa2011-11-14 14:32:37 -08001551 for (int i = 0; i < cUnit->numIns; i++) {
buzbee67bc2362011-10-11 18:08:40 -07001552 PromotionMap vMap = cUnit->promotionMap[startVReg + i];
buzbee67bc2362011-10-11 18:08:40 -07001553 if (i <= (lastArgReg - firstArgReg)) {
buzbee8febc582011-10-25 12:39:20 -07001554 // If arriving in register
buzbee67bc2362011-10-11 18:08:40 -07001555 if (vMap.coreLocation == kLocPhysReg) {
1556 genRegCopy(cUnit, vMap.coreReg, firstArgReg + i);
buzbee8febc582011-10-25 12:39:20 -07001557 }
1558 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001559 genRegCopy(cUnit, vMap.fpReg, firstArgReg + i);
1560 }
1561 // Also put a copy in memory in case we're partially promoted
1562 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1563 firstArgReg + i, kWord);
1564 } else {
buzbee8febc582011-10-25 12:39:20 -07001565 // If arriving in frame & promoted
buzbee67bc2362011-10-11 18:08:40 -07001566 if (vMap.coreLocation == kLocPhysReg) {
1567 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1568 vMap.coreReg);
buzbee8febc582011-10-25 12:39:20 -07001569 }
1570 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001571 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1572 vMap.fpReg);
buzbee67bf8852011-08-17 17:51:35 -07001573 }
1574 }
buzbee67bf8852011-08-17 17:51:35 -07001575 }
1576}
1577
1578/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001579STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001580{
1581 MIR* mir;
1582 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1583 int blockId = bb->id;
1584
1585 cUnit->curBlock = bb;
1586 labelList[blockId].operands[0] = bb->startOffset;
1587
1588 /* Insert the block label */
1589 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1590 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1591
buzbee6181f792011-09-29 11:14:04 -07001592 /* Reset local optimization data on block boundaries */
1593 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001594 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001595 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001596
1597 ArmLIR* headLIR = NULL;
1598
buzbeebbaf8942011-10-02 13:08:29 -07001599 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001600 if (bb->blockType == kEntryBlock) {
1601 /*
1602 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1603 * mechanism know so it doesn't try to use any of them when
1604 * expanding the frame or flushing. This leaves the utility
1605 * code with a single temp: r12. This should be enough.
1606 */
1607 oatLockTemp(cUnit, r0);
1608 oatLockTemp(cUnit, r1);
1609 oatLockTemp(cUnit, r2);
1610 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001611
1612 /*
1613 * We can safely skip the stack overflow check if we're
1614 * a leaf *and* our frame size < fudge factor.
1615 */
1616 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1617 ((size_t)cUnit->frameSize <
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001618 Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001619 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001620 if (!skipOverflowCheck) {
1621 /* Load stack limit */
1622 loadWordDisp(cUnit, rSELF,
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001623 Thread::StackEndOffset().Int32Value(), r12);
buzbeecefd1872011-09-09 09:59:52 -07001624 }
buzbee67bf8852011-08-17 17:51:35 -07001625 /* Spill core callee saves */
1626 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1627 /* Need to spill any FP regs? */
1628 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001629 /*
1630 * NOTE: fp spills are a little different from core spills in that
1631 * they are pushed as a contiguous block. When promoting from
1632 * the fp set, we must allocate all singles from s16..highest-promoted
1633 */
buzbee67bf8852011-08-17 17:51:35 -07001634 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1635 }
buzbeecefd1872011-09-09 09:59:52 -07001636 if (!skipOverflowCheck) {
1637 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001638 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001639 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1640 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001641 genRegCopy(cUnit, rSP, rLR); // Establish stack
1642 } else {
1643 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001644 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001645 }
buzbee67bf8852011-08-17 17:51:35 -07001646 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1647 flushIns(cUnit);
buzbee44b412b2012-02-04 08:50:53 -08001648
1649 if (cUnit->genDebugger) {
1650 // Refresh update debugger callout
1651 loadWordDisp(cUnit, rSELF,
1652 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), rSUSPEND);
1653 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY);
1654 }
1655
buzbee67bf8852011-08-17 17:51:35 -07001656 oatFreeTemp(cUnit, r0);
1657 oatFreeTemp(cUnit, r1);
1658 oatFreeTemp(cUnit, r2);
1659 oatFreeTemp(cUnit, r3);
1660 } else if (bb->blockType == kExitBlock) {
buzbee80c4d572012-02-24 13:35:44 -08001661 /*
1662 * In the exit path, r0/r1 are live - make sure they aren't
1663 * allocated by the register utilities as temps.
1664 */
1665 oatLockTemp(cUnit, r0);
1666 oatLockTemp(cUnit, r1);
1667
buzbee67bf8852011-08-17 17:51:35 -07001668 newLIR0(cUnit, kArmPseudoMethodExit);
buzbee44b412b2012-02-04 08:50:53 -08001669 /* If we're compiling for the debugger, generate an update callout */
1670 if (cUnit->genDebugger) {
1671 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT);
1672 }
buzbeebbaf8942011-10-02 13:08:29 -07001673 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07001674 /* Need to restore any FP callee saves? */
1675 if (cUnit->numFPSpills) {
1676 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1677 }
1678 if (cUnit->coreSpillMask & (1 << rLR)) {
1679 /* Unspill rLR to rPC */
1680 cUnit->coreSpillMask &= ~(1 << rLR);
1681 cUnit->coreSpillMask |= (1 << rPC);
1682 }
1683 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1684 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1685 /* We didn't pop to rPC, so must do a bv rLR */
1686 newLIR1(cUnit, kThumbBx, rLR);
1687 }
1688 }
1689
1690 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1691
1692 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07001693 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1694 oatClobberAllRegs(cUnit);
1695 }
buzbee67bf8852011-08-17 17:51:35 -07001696
1697 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1698 oatResetDefTracking(cUnit);
1699 }
1700
1701 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1702 handleExtendedMethodMIR(cUnit, mir);
1703 continue;
1704 }
1705
1706 cUnit->currentDalvikOffset = mir->offset;
1707
1708 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1709 InstructionFormat dalvikFormat =
1710 dexGetFormatFromOpcode(dalvikOpcode);
1711
1712 ArmLIR* boundaryLIR;
1713
1714 /* Mark the beginning of a Dalvik instruction for line tracking */
buzbee5abfa3e2012-01-31 17:01:43 -08001715 char* instStr = cUnit->printMe ?
buzbeeba938cb2012-02-03 14:47:55 -08001716 oatGetDalvikDisassembly(cUnit, &mir->dalvikInsn, "") : NULL;
buzbee67bf8852011-08-17 17:51:35 -07001717 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
buzbee5abfa3e2012-01-31 17:01:43 -08001718 (intptr_t) instStr);
buzbee85d8c1e2012-01-27 15:52:35 -08001719 cUnit->boundaryMap.insert(std::make_pair(mir->offset,
1720 (LIR*)boundaryLIR));
buzbee67bf8852011-08-17 17:51:35 -07001721 /* Remember the first LIR for this block */
1722 if (headLIR == NULL) {
1723 headLIR = boundaryLIR;
1724 /* Set the first boundaryLIR as a scheduling barrier */
1725 headLIR->defMask = ENCODE_ALL;
1726 }
1727
buzbee44b412b2012-02-04 08:50:53 -08001728 /* If we're compiling for the debugger, generate an update callout */
1729 if (cUnit->genDebugger) {
1730 genDebuggerUpdate(cUnit, mir->offset);
1731 }
1732
buzbee67bf8852011-08-17 17:51:35 -07001733 /* Don't generate the SSA annotation unless verbose mode is on */
1734 if (cUnit->printMe && mir->ssaRep) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -08001735 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
buzbee67bf8852011-08-17 17:51:35 -07001736 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1737 }
1738
1739 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1740
1741 if (notHandled) {
1742 char buf[100];
1743 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1744 mir->offset,
1745 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1746 dalvikFormat);
1747 LOG(FATAL) << buf;
1748 }
1749 }
1750
1751 if (headLIR) {
1752 /*
1753 * Eliminate redundant loads/stores and delay stores into later
1754 * slots
1755 */
1756 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1757 cUnit->lastLIRInsn);
1758
1759 /*
1760 * Generate an unconditional branch to the fallthrough block.
1761 */
1762 if (bb->fallThrough) {
1763 genUnconditionalBranch(cUnit,
1764 &labelList[bb->fallThrough->id]);
1765 }
1766 }
1767 return false;
1768}
1769
1770/*
1771 * Nop any unconditional branches that go to the next instruction.
1772 * Note: new redundant branches may be inserted later, and we'll
1773 * use a check in final instruction assembly to nop those out.
1774 */
1775void removeRedundantBranches(CompilationUnit* cUnit)
1776{
1777 ArmLIR* thisLIR;
1778
1779 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1780 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1781 thisLIR = NEXT_LIR(thisLIR)) {
1782
1783 /* Branch to the next instruction */
1784 if ((thisLIR->opcode == kThumbBUncond) ||
1785 (thisLIR->opcode == kThumb2BUncond)) {
1786 ArmLIR* nextLIR = thisLIR;
1787
1788 while (true) {
1789 nextLIR = NEXT_LIR(nextLIR);
1790
1791 /*
1792 * Is the branch target the next instruction?
1793 */
1794 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1795 thisLIR->flags.isNop = true;
1796 break;
1797 }
1798
1799 /*
1800 * Found real useful stuff between the branch and the target.
1801 * Need to explicitly check the lastLIRInsn here because it
1802 * might be the last real instruction.
1803 */
1804 if (!isPseudoOpcode(nextLIR->opcode) ||
1805 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1806 break;
1807 }
1808 }
1809 }
1810}
1811
buzbeeed3e9302011-09-23 17:34:19 -07001812STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07001813{
1814 ArmLIR** suspendLabel =
1815 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
1816 int numElems = cUnit->suspendLaunchpads.numUsed;
1817
1818 for (int i = 0; i < numElems; i++) {
1819 /* TUNING: move suspend count load into helper */
1820 ArmLIR* lab = suspendLabel[i];
1821 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
1822 cUnit->currentDalvikOffset = lab->operands[1];
1823 oatAppendLIR(cUnit, (LIR *)lab);
1824 loadWordDisp(cUnit, rSELF,
1825 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
buzbee44b412b2012-02-04 08:50:53 -08001826 if (!cUnit->genDebugger) {
1827 // use rSUSPEND for suspend count
1828 loadWordDisp(cUnit, rSELF,
1829 Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
1830 }
buzbeec1f45042011-09-21 16:03:19 -07001831 opReg(cUnit, kOpBlx, rLR);
buzbee44b412b2012-02-04 08:50:53 -08001832 if ( cUnit->genDebugger) {
1833 // use rSUSPEND for update debugger
1834 loadWordDisp(cUnit, rSELF,
1835 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), rSUSPEND);
1836 }
buzbeec1f45042011-09-21 16:03:19 -07001837 genUnconditionalBranch(cUnit, resumeLab);
1838 }
1839}
1840
buzbeeed3e9302011-09-23 17:34:19 -07001841STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07001842{
1843 ArmLIR** throwLabel =
1844 (ArmLIR **) cUnit->throwLaunchpads.elemList;
1845 int numElems = cUnit->throwLaunchpads.numUsed;
1846 int i;
1847
1848 for (i = 0; i < numElems; i++) {
1849 ArmLIR* lab = throwLabel[i];
1850 cUnit->currentDalvikOffset = lab->operands[1];
1851 oatAppendLIR(cUnit, (LIR *)lab);
1852 int funcOffset = 0;
1853 int v1 = lab->operands[2];
1854 int v2 = lab->operands[3];
1855 switch(lab->operands[0]) {
1856 case kArmThrowNullPointer:
1857 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
1858 break;
1859 case kArmThrowArrayBounds:
1860 if (v2 != r0) {
1861 genRegCopy(cUnit, r0, v1);
1862 genRegCopy(cUnit, r1, v2);
1863 } else {
1864 if (v1 == r1) {
1865 genRegCopy(cUnit, r12, v1);
1866 genRegCopy(cUnit, r1, v2);
1867 genRegCopy(cUnit, r0, r12);
1868 } else {
1869 genRegCopy(cUnit, r1, v2);
1870 genRegCopy(cUnit, r0, v1);
1871 }
1872 }
1873 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
1874 break;
1875 case kArmThrowDivZero:
1876 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
1877 break;
1878 case kArmThrowVerificationError:
1879 loadConstant(cUnit, r0, v1);
1880 loadConstant(cUnit, r1, v2);
1881 funcOffset =
1882 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
1883 break;
1884 case kArmThrowNegArraySize:
1885 genRegCopy(cUnit, r0, v1);
1886 funcOffset =
1887 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
1888 break;
buzbee5ade1d22011-09-09 14:44:52 -07001889 case kArmThrowNoSuchMethod:
buzbee13ac45a2012-01-12 12:30:16 -08001890 genRegCopy(cUnit, r0, v1);
buzbee5ade1d22011-09-09 14:44:52 -07001891 funcOffset =
1892 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
1893 break;
buzbeeec5adf32011-09-11 15:25:43 -07001894 case kArmThrowStackOverflow:
1895 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07001896 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07001897 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07001898 opRegImm(cUnit, kOpAdd, rSP,
1899 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07001900 break;
buzbee5ade1d22011-09-09 14:44:52 -07001901 default:
1902 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
1903 }
1904 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07001905 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001906 }
1907}
1908
buzbee67bf8852011-08-17 17:51:35 -07001909void oatMethodMIR2LIR(CompilationUnit* cUnit)
1910{
1911 /* Used to hold the labels of each block */
1912 cUnit->blockLabelList =
buzbeeba938cb2012-02-03 14:47:55 -08001913 (void *) oatNew(cUnit, sizeof(ArmLIR) * cUnit->numBlocks, true,
1914 kAllocLIR);
buzbee67bf8852011-08-17 17:51:35 -07001915
1916 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
1917 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07001918 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07001919
1920 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07001921
1922 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001923}
1924
1925/* Common initialization routine for an architecture family */
1926bool oatArchInit()
1927{
1928 int i;
1929
1930 for (i = 0; i < kArmLast; i++) {
1931 if (EncodingMap[i].opcode != i) {
1932 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
1933 " is wrong: expecting " << i << ", seeing " <<
1934 (int)EncodingMap[i].opcode;
1935 }
1936 }
1937
1938 return oatArchVariantInit();
1939}
1940
1941/* Needed by the Assembler */
1942void oatSetupResourceMasks(ArmLIR* lir)
1943{
1944 setupResourceMasks(lir);
1945}
1946
1947/* Needed by the ld/st optmizatons */
1948ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
1949{
1950 return genRegCopyNoInsert(cUnit, rDest, rSrc);
1951}
1952
1953/* Needed by the register allocator */
1954ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
1955{
1956 return genRegCopy(cUnit, rDest, rSrc);
1957}
1958
1959/* Needed by the register allocator */
1960void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
1961 int srcLo, int srcHi)
1962{
1963 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
1964}
1965
1966void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
1967 int displacement, int rSrc, OpSize size)
1968{
1969 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
1970}
1971
1972void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
1973 int displacement, int rSrcLo, int rSrcHi)
1974{
1975 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
1976}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001977
1978} // namespace art