blob: 3f8d9005a338fb46bba4349e840ba6acae078ef5 [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
buzbeece302932011-10-04 14:32:18 -070019#define DISPLAY_MISSING_TARGETS (cUnit->enableDebug & \
20 (1 << kDebugDisplayMissingTargets))
Elliott Hughes1240dad2011-09-09 16:24:50 -070021
buzbee67bc2362011-10-11 18:08:40 -070022STATIC const RegLocation badLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, INVALID_REG,
23 INVALID_REG, INVALID_SREG};
buzbee6181f792011-09-29 11:14:04 -070024
25/* Mark register usage state and return long retloc */
26STATIC RegLocation getRetLocWide(CompilationUnit* cUnit)
27{
28 RegLocation res = LOC_DALVIK_RETURN_VAL_WIDE;
29 oatLockTemp(cUnit, res.lowReg);
30 oatLockTemp(cUnit, res.highReg);
31 oatMarkPair(cUnit, res.lowReg, res.highReg);
32 return res;
33}
34
35STATIC RegLocation getRetLoc(CompilationUnit* cUnit)
36{
37 RegLocation res = LOC_DALVIK_RETURN_VAL;
38 oatLockTemp(cUnit, res.lowReg);
39 return res;
40}
buzbee67bf8852011-08-17 17:51:35 -070041
buzbeedfd3d702011-08-28 12:56:51 -070042/*
43 * Let helper function take care of everything. Will call
44 * Array::AllocFromCode(type_idx, method, count);
45 * Note: AllocFromCode will handle checks for errNegativeArraySize.
46 */
buzbeeed3e9302011-09-23 17:34:19 -070047STATIC void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -070048 RegLocation rlSrc)
49{
buzbeedfd3d702011-08-28 12:56:51 -070050 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers28ad40d2011-10-27 15:19:26 -070051 uint32_t type_idx = mir->dalvikInsn.vC;
Ian Rogersa3760aa2011-11-14 14:32:37 -080052 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
53 cUnit->dex_cache,
54 *cUnit->dex_file,
55 type_idx)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -070056 loadWordDisp(cUnit, rSELF,
57 OFFSETOF_MEMBER(Thread, pAllocArrayFromCode), rLR);
58 } else {
59 UNIMPLEMENTED(WARNING) << "Need to check access of '"
Ian Rogersa3760aa2011-11-14 14:32:37 -080060 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
Ian Rogers28ad40d2011-10-27 15:19:26 -070061 << "' to unresolved type " << type_idx;
62 loadWordDisp(cUnit, rSELF,
63 OFFSETOF_MEMBER(Thread, pAllocArrayFromCode), rLR);
64 }
buzbeedfd3d702011-08-28 12:56:51 -070065 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
Ian Rogers28ad40d2011-10-27 15:19:26 -070066 loadConstant(cUnit, r0, type_idx); // arg0 <- type_id
buzbeedfd3d702011-08-28 12:56:51 -070067 loadValueDirectFixed(cUnit, rlSrc, r2); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070068 callRuntimeHelper(cUnit, rLR);
buzbeedfd3d702011-08-28 12:56:51 -070069 RegLocation rlResult = oatGetReturn(cUnit);
70 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -070071}
72
73/*
74 * Similar to genNewArray, but with post-allocation initialization.
75 * Verifier guarantees we're dealing with an array class. Current
76 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
77 * Current code also throws internal unimp if not 'L', '[' or 'I'.
78 */
buzbeeed3e9302011-09-23 17:34:19 -070079STATIC void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
buzbee67bf8852011-08-17 17:51:35 -070080{
81 DecodedInstruction* dInsn = &mir->dalvikInsn;
buzbee81eccc02011-09-17 13:42:21 -070082 int elems = dInsn->vA;
83 int typeId = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070084 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbeedfd3d702011-08-28 12:56:51 -070085 loadWordDisp(cUnit, rSELF,
Elliott Hughesb408de72011-10-04 14:35:05 -070086 OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode), rLR);
Ian Rogersa3760aa2011-11-14 14:32:37 -080087 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
88 cUnit->dex_cache,
89 *cUnit->dex_file,
90 typeId)) {
91 UNIMPLEMENTED(WARNING) << "Need to check access of '"
92 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
93 << "' to unresolved type " << typeId;
Ian Rogers28ad40d2011-10-27 15:19:26 -070094 }
buzbeedfd3d702011-08-28 12:56:51 -070095 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
96 loadConstant(cUnit, r0, typeId); // arg0 <- type_id
97 loadConstant(cUnit, r2, elems); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070098 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -070099 /*
buzbeedfd3d702011-08-28 12:56:51 -0700100 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
101 * return region. Because AllocFromCode placed the new array
102 * in r0, we'll just lock it into place. When debugger support is
103 * added, it may be necessary to additionally copy all return
104 * values to a home location in thread-local storage
buzbee67bf8852011-08-17 17:51:35 -0700105 */
buzbee67bf8852011-08-17 17:51:35 -0700106 oatLockTemp(cUnit, r0);
buzbeedfd3d702011-08-28 12:56:51 -0700107
buzbee67bf8852011-08-17 17:51:35 -0700108 // Having a range of 0 is legal
109 if (isRange && (dInsn->vA > 0)) {
110 /*
111 * Bit of ugliness here. We're going generate a mem copy loop
112 * on the register range, but it is possible that some regs
113 * in the range have been promoted. This is unlikely, but
114 * before generating the copy, we'll just force a flush
115 * of any regs in the source range that have been promoted to
116 * home location.
117 */
118 for (unsigned int i = 0; i < dInsn->vA; i++) {
119 RegLocation loc = oatUpdateLoc(cUnit,
120 oatGetSrc(cUnit, mir, i));
121 if (loc.location == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -0700122 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
123 loc.lowReg, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700124 }
125 }
126 /*
127 * TUNING note: generated code here could be much improved, but
128 * this is an uncommon operation and isn't especially performance
129 * critical.
130 */
131 int rSrc = oatAllocTemp(cUnit);
132 int rDst = oatAllocTemp(cUnit);
133 int rIdx = oatAllocTemp(cUnit);
134 int rVal = rLR; // Using a lot of temps, rLR is known free here
135 // Set up source pointer
136 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
buzbee67bc2362011-10-11 18:08:40 -0700137 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
138 oatSRegOffset(cUnit, rlFirst.sRegLow));
buzbee67bf8852011-08-17 17:51:35 -0700139 // Set up the target pointer
140 opRegRegImm(cUnit, kOpAdd, rDst, r0,
buzbeec143c552011-08-20 17:38:58 -0700141 Array::DataOffset().Int32Value());
buzbee67bf8852011-08-17 17:51:35 -0700142 // Set up the loop counter (known to be > 0)
buzbee31813452011-10-16 14:33:08 -0700143 loadConstant(cUnit, rIdx, dInsn->vA - 1);
buzbee67bf8852011-08-17 17:51:35 -0700144 // Generate the copy loop. Going backwards for convenience
145 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
146 target->defMask = ENCODE_ALL;
147 // Copy next element
148 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
149 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
150 // Use setflags encoding here
151 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee31813452011-10-16 14:33:08 -0700152 ArmLIR* branch = opCondBranch(cUnit, kArmCondGe);
buzbee67bf8852011-08-17 17:51:35 -0700153 branch->generic.target = (LIR*)target;
154 } else if (!isRange) {
155 // TUNING: interleave
156 for (unsigned int i = 0; i < dInsn->vA; i++) {
157 RegLocation rlArg = loadValue(cUnit,
158 oatGetSrc(cUnit, mir, i), kCoreReg);
buzbeec143c552011-08-20 17:38:58 -0700159 storeBaseDisp(cUnit, r0,
160 Array::DataOffset().Int32Value() +
buzbee67bf8852011-08-17 17:51:35 -0700161 i * 4, rlArg.lowReg, kWord);
162 // If the loadValue caused a temp to be allocated, free it
163 if (oatIsTemp(cUnit, rlArg.lowReg)) {
164 oatFreeTemp(cUnit, rlArg.lowReg);
165 }
166 }
167 }
168}
169
Ian Rogersa3760aa2011-11-14 14:32:37 -0800170Field* FindFieldWithResolvedStaticStorage(CompilationUnit* cUnit,
Brian Carlstrom845490b2011-09-19 15:56:53 -0700171 const uint32_t fieldIdx,
172 uint32_t& resolvedTypeIdx) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800173 Field* field = cUnit->class_linker->ResolveField(*cUnit->dex_file,
174 fieldIdx,
175 cUnit->dex_cache,
176 cUnit->class_loader,
177 true);
Brian Carlstrom845490b2011-09-19 15:56:53 -0700178 if (field == NULL) {
Ian Rogersb093c6b2011-10-31 16:19:55 -0700179 Thread* thread = Thread::Current();
180 if (thread->IsExceptionPending()) { // clear any exception left by resolve field
181 thread->ClearException();
182 }
Brian Carlstrom845490b2011-09-19 15:56:53 -0700183 return NULL;
184 }
Ian Rogersa3760aa2011-11-14 14:32:37 -0800185 const art::DexFile::FieldId& field_id = cUnit->dex_file->GetFieldId(fieldIdx);
Brian Carlstrom845490b2011-09-19 15:56:53 -0700186 int type_idx = field_id.class_idx_;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800187 Class* klass = cUnit->dex_cache->GetResolvedTypes()->Get(type_idx);
Brian Carlstrom845490b2011-09-19 15:56:53 -0700188 // Check if storage class is the same as class referred to by type idx.
189 // They may not be if the FieldId refers a subclass, but storage is in super
190 if (field->GetDeclaringClass() == klass) {
191 resolvedTypeIdx = type_idx;
192 return field;
193 }
194 // See if we can find a dex reference for the storage class.
195 // we may not if the dex file never references the super class,
196 // but usually it will.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800197 std::string descriptor(art::FieldHelper(field).GetDeclaringClassDescriptor());
Ian Rogersa3760aa2011-11-14 14:32:37 -0800198 const art::DexFile::StringId* string_id =
199 cUnit->dex_file->FindStringId(descriptor);
200 if (string_id == NULL) {
201 return NULL; // descriptor not found, resort to slow path
Brian Carlstrom845490b2011-09-19 15:56:53 -0700202 }
Ian Rogersa3760aa2011-11-14 14:32:37 -0800203 const art::DexFile::TypeId* type_id =
204 cUnit->dex_file->FindTypeId(cUnit->dex_file->GetIndexForStringId(*string_id));
205 if (type_id == NULL) {
206 return NULL; // type id not found, resort to slow path
207 }
208 resolvedTypeIdx = cUnit->dex_file->GetIndexForTypeId(*type_id);
209 return field;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700210}
211
buzbeeed3e9302011-09-23 17:34:19 -0700212STATIC void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700213{
buzbeee1931742011-08-28 21:15:53 -0700214 bool isObject = ((mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
215 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE));
buzbee1da522d2011-09-04 11:22:20 -0700216 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700217 uint32_t typeIdx;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800218 Field* field = FindFieldWithResolvedStaticStorage(cUnit, fieldIdx, typeIdx);
buzbee6181f792011-09-29 11:14:04 -0700219 oatFlushAllRegs(cUnit);
Brian Carlstrom845490b2011-09-19 15:56:53 -0700220 if (SLOW_FIELD_PATH || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700221 // Slow path
Elliott Hughes81bc5092011-09-30 17:25:59 -0700222 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700223 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
224 : OFFSETOF_MEMBER(Thread, pSet32Static);
buzbeee1931742011-08-28 21:15:53 -0700225 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
226 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
227 loadCurrMethodDirect(cUnit, r1);
228 loadValueDirect(cUnit, rlSrc, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700229 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700230 } else {
buzbee1da522d2011-09-04 11:22:20 -0700231 // fast path
232 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700233 // Using fixed register to sync with slow path
234 int rMethod = r1;
235 oatLockTemp(cUnit, rMethod);
236 loadCurrMethodDirect(cUnit, rMethod);
237 int rBase = r0;
238 oatLockTemp(cUnit, rBase);
239 loadWordDisp(cUnit, rMethod,
240 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
241 rBase);
242 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
243 sizeof(int32_t*)* typeIdx, rBase);
244 // TUNING: fast path should fall through
buzbeec0ecd652011-09-25 18:11:54 -0700245 // TUNING: Try a conditional skip here, might be faster
buzbee1da522d2011-09-04 11:22:20 -0700246 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
247 loadWordDisp(cUnit, rSELF,
248 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
249 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700250 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700251 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
252 skipTarget->defMask = ENCODE_ALL;
253 branchOver->generic.target = (LIR*)skipTarget;
254 rlSrc = oatGetSrc(cUnit, mir, 0);
255 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbee12246b82011-09-29 14:15:05 -0700256#if ANDROID_SMP != 0
257 if (field->IsVolatile()) {
258 oatGenMemBarrier(cUnit, kST);
259 }
260#endif
buzbee1da522d2011-09-04 11:22:20 -0700261 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700262#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700263 if (field->IsVolatile()) {
264 oatGenMemBarrier(cUnit, kSY);
265 }
buzbee67bf8852011-08-17 17:51:35 -0700266#endif
buzbee1da522d2011-09-04 11:22:20 -0700267 if (isObject) {
268 markGCCard(cUnit, rlSrc.lowReg, rBase);
269 }
270 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700271 }
buzbee67bf8852011-08-17 17:51:35 -0700272}
273
buzbeeed3e9302011-09-23 17:34:19 -0700274STATIC void genSputWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700275{
buzbee1da522d2011-09-04 11:22:20 -0700276 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700277 uint32_t typeIdx;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800278 Field* field = FindFieldWithResolvedStaticStorage(cUnit, fieldIdx, typeIdx);
buzbee6181f792011-09-29 11:14:04 -0700279 oatFlushAllRegs(cUnit);
buzbee12246b82011-09-29 14:15:05 -0700280#if ANDROID_SMP != 0
281 bool isVolatile = (field == NULL) || field->IsVolatile();
282#else
283 bool isVolatile = false;
284#endif
285 if (SLOW_FIELD_PATH || field == NULL || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700286 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700287 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pSet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700288 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
289 loadCurrMethodDirect(cUnit, r1);
290 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -0700291 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700292 } else {
buzbee1da522d2011-09-04 11:22:20 -0700293 // fast path
294 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700295 // Using fixed register to sync with slow path
296 int rMethod = r1;
297 oatLockTemp(cUnit, rMethod);
298 loadCurrMethodDirect(cUnit, r1);
299 int rBase = r0;
300 oatLockTemp(cUnit, rBase);
301 loadWordDisp(cUnit, rMethod,
302 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
303 rBase);
304 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
305 sizeof(int32_t*)* typeIdx, rBase);
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, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700311 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700312 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
313 skipTarget->defMask = ENCODE_ALL;
314 branchOver->generic.target = (LIR*)skipTarget;
315 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
316 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
317 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
318 rlSrc.highReg);
buzbee1da522d2011-09-04 11:22:20 -0700319 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700320 }
buzbee67bf8852011-08-17 17:51:35 -0700321}
322
323
buzbeeed3e9302011-09-23 17:34:19 -0700324STATIC void genSgetWide(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700325 RegLocation rlResult, RegLocation rlDest)
326{
buzbee1da522d2011-09-04 11:22:20 -0700327 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700328 uint32_t typeIdx;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800329 Field* field = FindFieldWithResolvedStaticStorage(cUnit, fieldIdx, typeIdx);
buzbee12246b82011-09-29 14:15:05 -0700330#if ANDROID_SMP != 0
331 bool isVolatile = (field == NULL) || field->IsVolatile();
332#else
333 bool isVolatile = false;
334#endif
buzbee6181f792011-09-29 11:14:04 -0700335 oatFlushAllRegs(cUnit);
buzbee12246b82011-09-29 14:15:05 -0700336 if (SLOW_FIELD_PATH || field == NULL || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700337 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700338 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pGet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700339 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
340 loadCurrMethodDirect(cUnit, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -0700341 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700342 RegLocation rlResult = oatGetReturnWide(cUnit);
343 storeValueWide(cUnit, rlDest, rlResult);
344 } else {
buzbee1da522d2011-09-04 11:22:20 -0700345 // Fast path
346 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700347 // Using fixed register to sync with slow path
348 int rMethod = r1;
349 oatLockTemp(cUnit, rMethod);
350 loadCurrMethodDirect(cUnit, rMethod);
351 int rBase = r0;
352 oatLockTemp(cUnit, rBase);
353 loadWordDisp(cUnit, rMethod,
354 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
355 rBase);
356 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
357 sizeof(int32_t*)* typeIdx, rBase);
358 // TUNING: fast path should fall through
359 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
360 loadWordDisp(cUnit, rSELF,
361 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
362 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700363 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700364 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
365 skipTarget->defMask = ENCODE_ALL;
366 branchOver->generic.target = (LIR*)skipTarget;
367 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
368 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee1da522d2011-09-04 11:22:20 -0700369 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
370 rlResult.highReg, INVALID_SREG);
371 oatFreeTemp(cUnit, rBase);
372 storeValueWide(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700373 }
buzbee67bf8852011-08-17 17:51:35 -0700374}
375
buzbeeed3e9302011-09-23 17:34:19 -0700376STATIC void genSget(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700377 RegLocation rlResult, RegLocation rlDest)
378{
buzbee1da522d2011-09-04 11:22:20 -0700379 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700380 uint32_t typeIdx;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800381 Field* field = FindFieldWithResolvedStaticStorage(cUnit, fieldIdx, typeIdx);
buzbeee1931742011-08-28 21:15:53 -0700382 bool isObject = ((mir->dalvikInsn.opcode == OP_SGET_OBJECT) ||
383 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE));
buzbee6181f792011-09-29 11:14:04 -0700384 oatFlushAllRegs(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -0700385 if (SLOW_FIELD_PATH || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700386 // Slow path
Elliott Hughes81bc5092011-09-30 17:25:59 -0700387 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700388 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
389 : OFFSETOF_MEMBER(Thread, pGet32Static);
buzbeee1931742011-08-28 21:15:53 -0700390 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
391 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
392 loadCurrMethodDirect(cUnit, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -0700393 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700394 RegLocation rlResult = oatGetReturn(cUnit);
395 storeValue(cUnit, rlDest, rlResult);
396 } else {
buzbee1da522d2011-09-04 11:22:20 -0700397 // Fast path
398 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700399 // Using fixed register to sync with slow path
400 int rMethod = r1;
401 oatLockTemp(cUnit, rMethod);
402 loadCurrMethodDirect(cUnit, rMethod);
403 int rBase = r0;
404 oatLockTemp(cUnit, rBase);
405 loadWordDisp(cUnit, rMethod,
406 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
407 rBase);
408 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
409 sizeof(int32_t*)* typeIdx, rBase);
410 // TUNING: fast path should fall through
411 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
412 loadWordDisp(cUnit, rSELF,
413 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
414 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700415 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700416 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
417 skipTarget->defMask = ENCODE_ALL;
418 branchOver->generic.target = (LIR*)skipTarget;
419 rlDest = oatGetDest(cUnit, mir, 0);
420 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700421#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700422 if (field->IsVolatile()) {
buzbee1da522d2011-09-04 11:22:20 -0700423 oatGenMemBarrier(cUnit, kSY);
424 }
buzbee67bf8852011-08-17 17:51:35 -0700425#endif
buzbee1da522d2011-09-04 11:22:20 -0700426 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
427 oatFreeTemp(cUnit, rBase);
428 storeValue(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700429 }
buzbee67bf8852011-08-17 17:51:35 -0700430}
431
buzbee561227c2011-09-02 15:28:19 -0700432typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int,
433 ArmLIR*);
buzbee67bf8852011-08-17 17:51:35 -0700434
435/*
436 * Bit of a hack here - in leiu of a real scheduling pass,
437 * emit the next instruction in static & direct invoke sequences.
438 */
buzbeeed3e9302011-09-23 17:34:19 -0700439STATIC int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700440 DecodedInstruction* dInsn, int state,
441 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700442{
buzbee561227c2011-09-02 15:28:19 -0700443 DCHECK(rollback == NULL);
444 uint32_t idx = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -0700445 switch(state) {
446 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700447 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700448 break;
buzbee561227c2011-09-02 15:28:19 -0700449 case 1: // Get method->code_and_direct_methods_
450 loadWordDisp(cUnit, r0,
451 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
452 r0);
buzbee67bf8852011-08-17 17:51:35 -0700453 break;
buzbee561227c2011-09-02 15:28:19 -0700454 case 2: // Grab target method* and target code_
455 loadWordDisp(cUnit, r0,
456 art::CodeAndDirectMethods::CodeOffsetInBytes(idx), rLR);
457 loadWordDisp(cUnit, r0,
458 art::CodeAndDirectMethods::MethodOffsetInBytes(idx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700459 break;
460 default:
461 return -1;
462 }
463 return state + 1;
464}
465
buzbee67bf8852011-08-17 17:51:35 -0700466/*
467 * Bit of a hack here - in leiu of a real scheduling pass,
468 * emit the next instruction in a virtual invoke sequence.
469 * We can use rLR as a temp prior to target address loading
470 * Note also that we'll load the first argument ("this") into
471 * r1 here rather than the standard loadArgRegs.
472 */
buzbeeed3e9302011-09-23 17:34:19 -0700473STATIC int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700474 DecodedInstruction* dInsn, int state,
475 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700476{
buzbee561227c2011-09-02 15:28:19 -0700477 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700478 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700479 /*
480 * This is the fast path in which the target virtual method is
481 * fully resolved at compile time.
482 */
Ian Rogersa3760aa2011-11-14 14:32:37 -0800483 Method* baseMethod = cUnit->class_linker->ResolveMethod(*cUnit->dex_file,
484 dInsn->vB,
485 cUnit->dex_cache,
486 cUnit->class_loader,
487 false);
buzbee561227c2011-09-02 15:28:19 -0700488 CHECK(baseMethod != NULL);
489 uint32_t target_idx = baseMethod->GetMethodIndex();
buzbee67bf8852011-08-17 17:51:35 -0700490 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700491 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700492 rlArg = oatGetSrc(cUnit, mir, 0);
493 loadValueDirectFixed(cUnit, rlArg, r1);
494 break;
buzbee561227c2011-09-02 15:28:19 -0700495 case 1: // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700496 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee561227c2011-09-02 15:28:19 -0700497 // get this->klass_ [use r1, set rLR]
498 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700499 break;
buzbee561227c2011-09-02 15:28:19 -0700500 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
501 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700502 break;
buzbee561227c2011-09-02 15:28:19 -0700503 case 3: // Get target method [use rLR, set r0]
504 loadWordDisp(cUnit, rLR, (target_idx * 4) +
505 art::Array::DataOffset().Int32Value(), r0);
506 break;
507 case 4: // Get the target compiled code address [uses r0, sets rLR]
508 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700509 break;
510 default:
511 return -1;
512 }
513 return state + 1;
514}
515
buzbeeed3e9302011-09-23 17:34:19 -0700516STATIC int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700517 DecodedInstruction* dInsn, int state,
518 ArmLIR* rollback)
buzbee7b1b86d2011-08-26 18:59:10 -0700519{
520 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700521 ArmLIR* skipBranch;
522 ArmLIR* skipTarget;
523 /*
524 * This handles the case in which the base method is not fully
525 * resolved at compile time. We must generate code to test
526 * for resolution a run time, bail to the slow path if not to
527 * fill in all the tables. In the latter case, we'll restart at
528 * at the beginning of the sequence.
529 */
buzbee7b1b86d2011-08-26 18:59:10 -0700530 switch(state) {
531 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700532 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700533 break;
buzbee561227c2011-09-02 15:28:19 -0700534 case 1: // Get method->dex_cache_resolved_methods_
535 loadWordDisp(cUnit, r0,
536 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700537 break;
buzbee561227c2011-09-02 15:28:19 -0700538 case 2: // method->dex_cache_resolved_methods_->Get(method_idx)
539 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
540 art::Array::DataOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700541 break;
buzbee561227c2011-09-02 15:28:19 -0700542 case 3: // Resolved?
543 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
544 // Slowest path, bail to helper, rollback and retry
545 loadWordDisp(cUnit, rSELF,
546 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
547 loadConstant(cUnit, r1, dInsn->vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700548 callRuntimeHelper(cUnit, rLR);
buzbee561227c2011-09-02 15:28:19 -0700549 genUnconditionalBranch(cUnit, rollback);
550 // Resume normal slow path
551 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
552 skipTarget->defMask = ENCODE_ALL;
553 skipBranch->generic.target = (LIR*)skipTarget;
buzbee4a3164f2011-09-03 11:25:10 -0700554 // Get base_method->method_index [usr rLR, set r0]
buzbee561227c2011-09-02 15:28:19 -0700555 loadBaseDisp(cUnit, mir, rLR,
556 Method::GetMethodIndexOffset().Int32Value(), r0,
557 kUnsignedHalf, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700558 // Load "this" [set r1]
559 rlArg = oatGetSrc(cUnit, mir, 0);
560 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee7b1b86d2011-08-26 18:59:10 -0700561 break;
562 case 4:
563 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700564 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee7b1b86d2011-08-26 18:59:10 -0700565 // get this->clazz [use r1, set rLR]
buzbee561227c2011-09-02 15:28:19 -0700566 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700567 break;
buzbee561227c2011-09-02 15:28:19 -0700568 case 5:
569 // get this->klass_->vtable_ [usr rLR, set rLR]
570 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbeeed3e9302011-09-23 17:34:19 -0700571 DCHECK_EQ((art::Array::DataOffset().Int32Value() & 0x3), 0);
buzbee561227c2011-09-02 15:28:19 -0700572 // In load shadow fold vtable_ object header size into method_index_
573 opRegImm(cUnit, kOpAdd, r0,
574 art::Array::DataOffset().Int32Value() / 4);
575 // Get target Method*
576 loadBaseIndexed(cUnit, rLR, r0, r0, 2, kWord);
577 break;
578 case 6: // Get the target compiled code address [uses r0, sets rLR]
579 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700580 break;
581 default:
582 return -1;
583 }
584 return state + 1;
585}
586
buzbeeed3e9302011-09-23 17:34:19 -0700587STATIC int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
buzbeec0ecd652011-09-25 18:11:54 -0700588 DecodedInstruction* dInsn, int callState,
589 NextCallInsn nextCallInsn, ArmLIR* rollback,
590 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700591{
buzbeec0ecd652011-09-25 18:11:54 -0700592 int nextReg = r1;
593 int nextArg = 0;
594 if (skipThis) {
595 nextReg++;
596 nextArg++;
597 }
598 for (; (nextReg <= r3) && (nextArg < mir->ssaRep->numUses); nextReg++) {
599 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
600 rlArg = oatUpdateRawLoc(cUnit, rlArg);
601 if (rlArg.wide && (nextReg <= r2)) {
602 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
603 nextReg++;
604 nextArg++;
605 } else {
buzbee1b4c8592011-08-31 10:43:51 -0700606 rlArg.wide = false;
buzbeec0ecd652011-09-25 18:11:54 -0700607 loadValueDirectFixed(cUnit, rlArg, nextReg);
buzbee67bf8852011-08-17 17:51:35 -0700608 }
buzbeec0ecd652011-09-25 18:11:54 -0700609 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700610 }
611 return callState;
612}
613
buzbee4a3164f2011-09-03 11:25:10 -0700614// Interleave launch code for INVOKE_INTERFACE.
buzbeeed3e9302011-09-23 17:34:19 -0700615STATIC int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700616 DecodedInstruction* dInsn, int state,
617 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700618{
buzbee510c6052011-10-27 10:47:20 -0700619 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700620 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700621 case 0: // Load trampoline target
622 loadWordDisp(cUnit, rSELF,
623 OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline),
624 rLR);
625 // Load r0 with method index
626 loadConstant(cUnit, r0, dInsn->vB);
buzbee67bf8852011-08-17 17:51:35 -0700627 break;
buzbee67bf8852011-08-17 17:51:35 -0700628 default:
629 return -1;
630 }
631 return state + 1;
632}
633
buzbee67bf8852011-08-17 17:51:35 -0700634/*
635 * Interleave launch code for INVOKE_SUPER. See comments
636 * for nextVCallIns.
637 */
buzbeeed3e9302011-09-23 17:34:19 -0700638STATIC int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700639 DecodedInstruction* dInsn, int state,
640 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700641{
buzbee4a3164f2011-09-03 11:25:10 -0700642 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700643 RegLocation rlArg;
buzbee4a3164f2011-09-03 11:25:10 -0700644 /*
645 * This is the fast path in which the target virtual method is
646 * fully resolved at compile time. Note also that this path assumes
647 * that the check to verify that the target method index falls
648 * within the size of the super's vtable has been done at compile-time.
649 */
Brian Carlstrom845490b2011-09-19 15:56:53 -0700650 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
Ian Rogersa3760aa2011-11-14 14:32:37 -0800651 Method* baseMethod = class_linker->ResolveMethod(*cUnit->dex_file,
652 dInsn->vB,
653 cUnit->dex_cache,
654 cUnit->class_loader,
655 false);
buzbee4a3164f2011-09-03 11:25:10 -0700656 CHECK(baseMethod != NULL);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800657 Class* declaring_class = cUnit->dex_cache->GetResolvedTypes()
658 ->Get(cUnit->dex_file->GetMethodId(cUnit->method_idx).class_idx_);
659 Class* superClass = (declaring_class != NULL)
660 ? declaring_class->GetSuperClass() : NULL;
buzbee4a3164f2011-09-03 11:25:10 -0700661 CHECK(superClass != NULL);
662 int32_t target_idx = baseMethod->GetMethodIndex();
663 CHECK(superClass->GetVTable()->GetLength() > target_idx);
664 Method* targetMethod = superClass->GetVTable()->Get(target_idx);
665 CHECK(targetMethod != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700666 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700667 case 0: // Get current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700668 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700669 // Load "this" [set r1]
670 rlArg = oatGetSrc(cUnit, mir, 0);
671 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee4a3164f2011-09-03 11:25:10 -0700672 // Get method->declaring_class_ [use r0, set rLR]
673 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
674 rLR);
buzbee67bf8852011-08-17 17:51:35 -0700675 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700676 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee4a3164f2011-09-03 11:25:10 -0700677 break;
678 case 1: // Get method->declaring_class_->super_class [usr rLR, set rLR]
679 loadWordDisp(cUnit, rLR, Class::SuperClassOffset().Int32Value(),
680 rLR);
681 break;
682 case 2: // Get ...->super_class_->vtable [u/s rLR]
683 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
684 break;
685 case 3: // Get target method [use rLR, set r0]
686 loadWordDisp(cUnit, rLR, (target_idx * 4) +
687 art::Array::DataOffset().Int32Value(), r0);
688 break;
689 case 4: // Get the target compiled code address [uses r0, sets rLR]
690 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
691 break;
buzbee67bf8852011-08-17 17:51:35 -0700692 default:
693 return -1;
694 }
buzbee4a3164f2011-09-03 11:25:10 -0700695 return state + 1;
696}
697
698/* Slow-path version of nextSuperCallInsn */
buzbeeed3e9302011-09-23 17:34:19 -0700699STATIC int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee4a3164f2011-09-03 11:25:10 -0700700 DecodedInstruction* dInsn, int state,
701 ArmLIR* rollback)
702{
buzbee4a3164f2011-09-03 11:25:10 -0700703 RegLocation rlArg;
704 ArmLIR* skipBranch;
705 ArmLIR* skipTarget;
706 int tReg;
707 /*
708 * This handles the case in which the base method is not fully
709 * resolved at compile time. We must generate code to test
710 * for resolution a run time, bail to the slow path if not to
711 * fill in all the tables. In the latter case, we'll restart at
712 * at the beginning of the sequence.
713 */
714 switch(state) {
715 case 0: // Get the current Method* [sets r0]
716 loadCurrMethodDirect(cUnit, r0);
717 break;
buzbee34c77ad2012-01-11 13:01:32 -0800718 case 1: // Get method->dex_cache_resolved_methods_ [uses r0, set rLR]
buzbee4a3164f2011-09-03 11:25:10 -0700719 loadWordDisp(cUnit, r0,
720 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
721 break;
722 case 2: // method->dex_cache_resolved_methods_->Get(meth_idx) [u/s rLR]
723 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
724 art::Array::DataOffset().Int32Value(), rLR);
725 break;
726 case 3: // Resolved?
727 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
728 // Slowest path, bail to helper, rollback and retry
729 loadWordDisp(cUnit, rSELF,
730 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
731 loadConstant(cUnit, r1, dInsn->vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700732 callRuntimeHelper(cUnit, rLR);
buzbee4a3164f2011-09-03 11:25:10 -0700733 genUnconditionalBranch(cUnit, rollback);
734 // Resume normal slow path
735 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
736 skipTarget->defMask = ENCODE_ALL;
737 skipBranch->generic.target = (LIR*)skipTarget;
738 // Get base_method->method_index [usr rLR, set rLR]
739 loadBaseDisp(cUnit, mir, rLR,
740 Method::GetMethodIndexOffset().Int32Value(), rLR,
741 kUnsignedHalf, INVALID_SREG);
742 // Load "this" [set r1]
743 rlArg = oatGetSrc(cUnit, mir, 0);
744 loadValueDirectFixed(cUnit, rlArg, r1);
745 // Load curMethod->declaring_class_ [uses r0, sets r0]
746 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
747 r0);
buzbee6a0f7f52011-09-05 16:14:20 -0700748 // Null this?
buzbee5ade1d22011-09-09 14:44:52 -0700749 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee6a0f7f52011-09-05 16:14:20 -0700750 // Get method->declaring_class_->super_class [usr r0, set r0]
buzbee4a3164f2011-09-03 11:25:10 -0700751 loadWordDisp(cUnit, r0, Class::SuperClassOffset().Int32Value(), r0);
752 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700753 case 4: // Get ...->super_class_->vtable [u/s r0]
buzbee4a3164f2011-09-03 11:25:10 -0700754 loadWordDisp(cUnit, r0, Class::VTableOffset().Int32Value(), r0);
buzbee43a36422011-09-14 14:00:13 -0700755 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee4a3164f2011-09-03 11:25:10 -0700756 // Range check, throw NSM on failure
757 tReg = oatAllocTemp(cUnit);
758 loadWordDisp(cUnit, r0, art::Array::LengthOffset().Int32Value(),
759 tReg);
buzbeeec5adf32011-09-11 15:25:43 -0700760 genRegRegCheck(cUnit, kArmCondCs, tReg, rLR, mir,
761 kArmThrowNoSuchMethod);
buzbee4a3164f2011-09-03 11:25:10 -0700762 oatFreeTemp(cUnit, tReg);
763 }
buzbee6a0f7f52011-09-05 16:14:20 -0700764 // Adjust vtable_ base past object header
765 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
buzbee4a3164f2011-09-03 11:25:10 -0700766 // Get target Method*
buzbee6a0f7f52011-09-05 16:14:20 -0700767 loadBaseIndexed(cUnit, r0, rLR, r0, 2, kWord);
buzbee4a3164f2011-09-03 11:25:10 -0700768 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700769 case 5: // Get the target compiled code address [uses r0, sets rLR]
buzbee4a3164f2011-09-03 11:25:10 -0700770 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
771 break;
772 default:
773 return -1;
774 }
buzbee67bf8852011-08-17 17:51:35 -0700775 return state + 1;
776}
777
778/*
779 * Load up to 5 arguments, the first three of which will be in
780 * r1 .. r3. On entry r0 contains the current method pointer,
781 * and as part of the load sequence, it must be replaced with
782 * the target method pointer. Note, this may also be called
783 * for "range" variants if the number of arguments is 5 or fewer.
784 */
buzbeeed3e9302011-09-23 17:34:19 -0700785STATIC int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700786 DecodedInstruction* dInsn, int callState,
787 ArmLIR** pcrLabel, bool isRange,
buzbee1da522d2011-09-04 11:22:20 -0700788 NextCallInsn nextCallInsn, ArmLIR* rollback,
789 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700790{
791 RegLocation rlArg;
buzbee67bf8852011-08-17 17:51:35 -0700792
793 /* If no arguments, just return */
794 if (dInsn->vA == 0)
795 return callState;
796
buzbee561227c2011-09-02 15:28:19 -0700797 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700798
buzbeec0ecd652011-09-25 18:11:54 -0700799 DCHECK_LE(dInsn->vA, 5U);
800 if (dInsn->vA > 3) {
801 uint32_t nextUse = 3;
802 //Detect special case of wide arg spanning arg3/arg4
803 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
804 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
805 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
806 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
807 rlUse2.wide) {
808 int reg;
809 // Wide spans, we need the 2nd half of uses[2].
810 rlArg = oatUpdateLocWide(cUnit, rlUse2);
811 if (rlArg.location == kLocPhysReg) {
812 reg = rlArg.highReg;
813 } else {
814 // r2 & r3 can safely be used here
815 reg = r3;
buzbee67bc2362011-10-11 18:08:40 -0700816 loadWordDisp(cUnit, rSP,
817 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbeec0ecd652011-09-25 18:11:54 -0700818 callState = nextCallInsn(cUnit, mir, dInsn, callState,
819 rollback);
820 }
821 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
822 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
823 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
824 nextUse++;
825 }
826 // Loop through the rest
827 while (nextUse < dInsn->vA) {
828 int lowReg;
829 int highReg;
830 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
831 rlArg = oatUpdateRawLoc(cUnit, rlArg);
832 if (rlArg.location == kLocPhysReg) {
833 lowReg = rlArg.lowReg;
834 highReg = rlArg.highReg;
835 } else {
836 lowReg = r2;
837 highReg = r3;
838 if (rlArg.wide) {
839 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
840 } else {
841 loadValueDirectFixed(cUnit, rlArg, lowReg);
842 }
843 callState = nextCallInsn(cUnit, mir, dInsn, callState,
844 rollback);
845 }
846 int outsOffset = (nextUse + 1) * 4;
847 if (rlArg.wide) {
848 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
849 nextUse += 2;
850 } else {
851 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
852 nextUse++;
853 }
buzbee561227c2011-09-02 15:28:19 -0700854 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700855 }
buzbee67bf8852011-08-17 17:51:35 -0700856 }
857
buzbeec0ecd652011-09-25 18:11:54 -0700858 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
859 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700860
buzbee67bf8852011-08-17 17:51:35 -0700861 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700862 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700863 }
864 return callState;
865}
866
867/*
868 * May have 0+ arguments (also used for jumbo). Note that
869 * source virtual registers may be in physical registers, so may
870 * need to be flushed to home location before copying. This
871 * applies to arg3 and above (see below).
872 *
873 * Two general strategies:
874 * If < 20 arguments
875 * Pass args 3-18 using vldm/vstm block copy
876 * Pass arg0, arg1 & arg2 in r1-r3
877 * If 20+ arguments
878 * Pass args arg19+ using memcpy block copy
879 * Pass arg0, arg1 & arg2 in r1-r3
880 *
881 */
buzbeeed3e9302011-09-23 17:34:19 -0700882STATIC int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700883 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700884 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700885 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700886{
887 int firstArg = dInsn->vC;
888 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700889
buzbee67bf8852011-08-17 17:51:35 -0700890 // If we can treat it as non-range (Jumbo ops will use range form)
891 if (numArgs <= 5)
892 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700893 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700894 /*
895 * Make sure range list doesn't span the break between in normal
896 * Dalvik vRegs and the ins.
897 */
buzbee1b4c8592011-08-31 10:43:51 -0700898 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800899 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
buzbee1b4c8592011-08-31 10:43:51 -0700900 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
901 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700902 }
903
904 /*
905 * First load the non-register arguments. Both forms expect all
906 * of the source arguments to be in their home frame location, so
907 * scan the sReg names and flush any that have been promoted to
908 * frame backing storage.
909 */
910 // Scan the rest of the args - if in physReg flush to memory
buzbeec0ecd652011-09-25 18:11:54 -0700911 for (int nextArg = 0; nextArg < numArgs;) {
912 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
buzbee1b4c8592011-08-31 10:43:51 -0700913 if (loc.wide) {
914 loc = oatUpdateLocWide(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700915 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700916 storeBaseDispWide(cUnit, rSP,
917 oatSRegOffset(cUnit, loc.sRegLow),
918 loc.lowReg, loc.highReg);
buzbee1b4c8592011-08-31 10:43:51 -0700919 }
buzbeec0ecd652011-09-25 18:11:54 -0700920 nextArg += 2;
buzbee1b4c8592011-08-31 10:43:51 -0700921 } else {
922 loc = oatUpdateLoc(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700923 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700924 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
925 loc.lowReg, kWord);
buzbee1b4c8592011-08-31 10:43:51 -0700926 }
buzbeec0ecd652011-09-25 18:11:54 -0700927 nextArg++;
buzbee67bf8852011-08-17 17:51:35 -0700928 }
929 }
930
buzbee67bc2362011-10-11 18:08:40 -0700931 int startOffset = oatSRegOffset(cUnit,
932 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
buzbee67bf8852011-08-17 17:51:35 -0700933 int outsOffset = 4 /* Method* */ + (3 * 4);
934 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700935 // Generate memcpy
936 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
937 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700938 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
939 loadConstant(cUnit, r2, (numArgs - 3) * 4);
Ian Rogersff1ed472011-09-20 13:46:24 -0700940 callRuntimeHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700941 // Restore Method*
942 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700943 } else {
944 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700945 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700946 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700947 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700948 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
949 //TUNING: loosen barrier
950 ld->defMask = ENCODE_ALL;
951 setMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbee561227c2011-09-02 15:28:19 -0700952 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700953 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700954 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbeef48e9712011-09-15 17:54:28 -0700955 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
956 setMemRefType(st, false /* isLoad */, kDalvikReg);
957 st->defMask = ENCODE_ALL;
buzbee561227c2011-09-02 15:28:19 -0700958 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700959 }
960
buzbeec0ecd652011-09-25 18:11:54 -0700961 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
962 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700963
buzbee561227c2011-09-02 15:28:19 -0700964 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee99f27232011-10-05 12:56:36 -0700965 if (pcrLabel) {
966 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
967 }
buzbee67bf8852011-08-17 17:51:35 -0700968 return callState;
969}
970
buzbee2a475e72011-09-07 17:19:17 -0700971// Debugging routine - if null target, branch to DebugMe
buzbeeed3e9302011-09-23 17:34:19 -0700972STATIC void genShowTarget(CompilationUnit* cUnit)
buzbee2a475e72011-09-07 17:19:17 -0700973{
974 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
975 loadWordDisp(cUnit, rSELF,
976 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
977 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
978 target->defMask = -1;
979 branchOver->generic.target = (LIR*)target;
980}
buzbee2a475e72011-09-07 17:19:17 -0700981
buzbeeed3e9302011-09-23 17:34:19 -0700982STATIC void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700983 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700984{
985 DecodedInstruction* dInsn = &mir->dalvikInsn;
986 int callState = 0;
987 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700988 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700989 NextCallInsn nextCallInsn = nextSDCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700990 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee561227c2011-09-02 15:28:19 -0700991
buzbee109bd6a2011-09-06 13:58:41 -0700992 // Explicit register usage
993 oatLockCallTemps(cUnit);
994
buzbee99f27232011-10-05 12:56:36 -0700995 // Is this the special "Ljava/lang/Object;.<init>:()V" case?
996 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT) {
997 int idx = mir->dalvikInsn.vB;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800998 Method* target = cUnit->dex_cache->GetResolvedMethods()->Get(idx);
buzbee99f27232011-10-05 12:56:36 -0700999 if (target) {
1000 if (PrettyMethod(target) == "java.lang.Object.<init>()V") {
1001 RegLocation rlArg = oatGetSrc(cUnit, mir, 0);
1002 loadValueDirectFixed(cUnit, rlArg, r0);
1003 loadWordDisp(cUnit, rSELF,
1004 OFFSETOF_MEMBER(Thread, pObjectInit), rLR);
1005 genNullCheck(cUnit, oatSSASrc(mir,0), r0, mir);
1006 opReg(cUnit, kOpBlx, rLR);
1007 oatClobberCalleeSave(cUnit);
1008 return;
1009 }
1010 }
1011 }
1012
buzbee561227c2011-09-02 15:28:19 -07001013 if (range) {
1014 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -07001015 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -07001016 } else {
1017 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -07001018 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -07001019 }
buzbee67bf8852011-08-17 17:51:35 -07001020 // Finish up any of the call sequence not interleaved in arg loading
1021 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001022 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001023 }
buzbeece302932011-10-04 14:32:18 -07001024 if (DISPLAY_MISSING_TARGETS) {
1025 genShowTarget(cUnit);
1026 }
buzbeeec5adf32011-09-11 15:25:43 -07001027 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001028 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001029}
1030
buzbee4a3164f2011-09-03 11:25:10 -07001031/*
1032 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
1033 * which will locate the target and continue on via a tail call.
1034 */
buzbeeed3e9302011-09-23 17:34:19 -07001035STATIC void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001036{
1037 DecodedInstruction* dInsn = &mir->dalvikInsn;
1038 int callState = 0;
1039 ArmLIR* nullCk;
buzbeec0ecd652011-09-25 18:11:54 -07001040 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001041
1042 // Explicit register usage
1043 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001044 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -07001045 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001046 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
1047 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001048 false, nextInterfaceCallInsn, NULL,
buzbee367ce0b2011-09-14 23:19:50 -07001049 false);
buzbee67bf8852011-08-17 17:51:35 -07001050 else
1051 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee367ce0b2011-09-14 23:19:50 -07001052 nextInterfaceCallInsn, NULL, false);
buzbee67bf8852011-08-17 17:51:35 -07001053 // Finish up any of the call sequence not interleaved in arg loading
1054 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001055 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001056 }
buzbeece302932011-10-04 14:32:18 -07001057 if (DISPLAY_MISSING_TARGETS) {
1058 genShowTarget(cUnit);
1059 }
buzbeeec5adf32011-09-11 15:25:43 -07001060 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001061 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001062}
1063
buzbeeed3e9302011-09-23 17:34:19 -07001064STATIC void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001065{
1066 DecodedInstruction* dInsn = &mir->dalvikInsn;
1067 int callState = 0;
buzbee4a3164f2011-09-03 11:25:10 -07001068 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001069 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
Ian Rogersa3760aa2011-11-14 14:32:37 -08001070 Method* baseMethod = class_linker->ResolveMethod(*cUnit->dex_file,
1071 dInsn->vB,
1072 cUnit->dex_cache,
1073 cUnit->class_loader,
1074 false);
buzbee4a3164f2011-09-03 11:25:10 -07001075 NextCallInsn nextCallInsn;
1076 bool fastPath = true;
buzbeec0ecd652011-09-25 18:11:54 -07001077 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001078
1079 // Explicit register usage
1080 oatLockCallTemps(cUnit);
buzbee34c77ad2012-01-11 13:01:32 -08001081
1082 // For testing, force call to artResolveMethodFromCode & ignore result
1083 if (EXERCISE_RESOLVE_METHOD) {
1084 loadCurrMethodDirect(cUnit, r0);
1085 loadWordDisp(cUnit, rSELF,
1086 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
1087 loadConstant(cUnit, r1, dInsn->vB);
1088 callRuntimeHelper(cUnit, rLR);
1089 }
1090
buzbee34cd9e52011-09-08 14:31:52 -07001091 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
Ian Rogersbd441352011-10-31 10:49:47 -07001092 Thread* thread = Thread::Current();
1093 if (thread->IsExceptionPending()) { // clear any exception left by resolve method
1094 thread->ClearException();
1095 }
buzbee4a3164f2011-09-03 11:25:10 -07001096 fastPath = false;
1097 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -08001098 Class* declaring_class = cUnit->dex_cache->GetResolvedTypes()
1099 ->Get(cUnit->dex_file->GetMethodId(cUnit->method_idx).class_idx_);
1100 Class* superClass = (declaring_class != NULL)
1101 ? declaring_class->GetSuperClass() : NULL;
buzbee4a3164f2011-09-03 11:25:10 -07001102 if (superClass == NULL) {
1103 fastPath = false;
1104 } else {
1105 int32_t target_idx = baseMethod->GetMethodIndex();
1106 if (superClass->GetVTable()->GetLength() <= target_idx) {
1107 fastPath = false;
1108 } else {
1109 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
1110 }
1111 }
1112 }
1113 if (fastPath) {
1114 nextCallInsn = nextSuperCallInsn;
1115 rollback = NULL;
1116 } else {
1117 nextCallInsn = nextSuperCallInsnSP;
1118 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1119 rollback->defMask = -1;
1120 }
buzbee67bf8852011-08-17 17:51:35 -07001121 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
buzbeec0ecd652011-09-25 18:11:54 -07001122 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001123 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001124 else
buzbeec0ecd652011-09-25 18:11:54 -07001125 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001126 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001127 // Finish up any of the call sequence not interleaved in arg loading
1128 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -07001129 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001130 }
buzbeece302932011-10-04 14:32:18 -07001131 if (DISPLAY_MISSING_TARGETS) {
1132 genShowTarget(cUnit);
1133 }
buzbeeec5adf32011-09-11 15:25:43 -07001134 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001135 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001136}
1137
buzbeeed3e9302011-09-23 17:34:19 -07001138STATIC void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001139{
1140 DecodedInstruction* dInsn = &mir->dalvikInsn;
1141 int callState = 0;
buzbee561227c2011-09-02 15:28:19 -07001142 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001143 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
Ian Rogersa3760aa2011-11-14 14:32:37 -08001144 Method* method = class_linker->ResolveMethod(*cUnit->dex_file,
1145 dInsn->vB,
1146 cUnit->dex_cache,
1147 cUnit->class_loader,
1148 false);
buzbee561227c2011-09-02 15:28:19 -07001149 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -07001150 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001151 // Explicit register usage
1152 oatLockCallTemps(cUnit);
buzbee34c77ad2012-01-11 13:01:32 -08001153
1154 // For testing, force call to artResolveMethodFromCode & ignore result
1155 if (EXERCISE_RESOLVE_METHOD) {
1156 loadCurrMethodDirect(cUnit, r0);
1157 loadWordDisp(cUnit, rSELF,
1158 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
1159 loadConstant(cUnit, r1, dInsn->vB);
1160 callRuntimeHelper(cUnit, rLR);
1161 }
1162
buzbee34cd9e52011-09-08 14:31:52 -07001163 if (SLOW_INVOKE_PATH || method == NULL) {
Ian Rogersbd441352011-10-31 10:49:47 -07001164 Thread* thread = Thread::Current();
1165 if (thread->IsExceptionPending()) { // clear any exception left by resolve method
1166 thread->ClearException();
1167 }
buzbee561227c2011-09-02 15:28:19 -07001168 // Slow path
1169 nextCallInsn = nextVCallInsnSP;
1170 // If we need a slow-path callout, we'll restart here
1171 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1172 rollback->defMask = -1;
1173 } else {
1174 // Fast path
1175 nextCallInsn = nextVCallInsn;
1176 rollback = NULL;
1177 }
buzbee67bf8852011-08-17 17:51:35 -07001178 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
buzbeec0ecd652011-09-25 18:11:54 -07001179 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001180 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001181 else
buzbeec0ecd652011-09-25 18:11:54 -07001182 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001183 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001184 // Finish up any of the call sequence not interleaved in arg loading
1185 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001186 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001187 }
buzbeece302932011-10-04 14:32:18 -07001188 if (DISPLAY_MISSING_TARGETS) {
1189 genShowTarget(cUnit);
1190 }
buzbeeec5adf32011-09-11 15:25:43 -07001191 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001192 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001193}
1194
buzbeeed3e9302011-09-23 17:34:19 -07001195STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001196 BasicBlock* bb, ArmLIR* labelList)
1197{
1198 bool res = false; // Assume success
1199 RegLocation rlSrc[3];
1200 RegLocation rlDest = badLoc;
1201 RegLocation rlResult = badLoc;
1202 Opcode opcode = mir->dalvikInsn.opcode;
1203
1204 /* Prep Src and Dest locations */
1205 int nextSreg = 0;
1206 int nextLoc = 0;
1207 int attrs = oatDataFlowAttributes[opcode];
1208 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1209 if (attrs & DF_UA) {
1210 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1211 nextSreg++;
1212 } else if (attrs & DF_UA_WIDE) {
1213 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1214 nextSreg + 1);
1215 nextSreg+= 2;
1216 }
1217 if (attrs & DF_UB) {
1218 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1219 nextSreg++;
1220 } else if (attrs & DF_UB_WIDE) {
1221 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1222 nextSreg + 1);
1223 nextSreg+= 2;
1224 }
1225 if (attrs & DF_UC) {
1226 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1227 } else if (attrs & DF_UC_WIDE) {
1228 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1229 nextSreg + 1);
1230 }
1231 if (attrs & DF_DA) {
1232 rlDest = oatGetDest(cUnit, mir, 0);
1233 } else if (attrs & DF_DA_WIDE) {
1234 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1235 }
1236
1237 switch(opcode) {
1238 case OP_NOP:
1239 break;
1240
1241 case OP_MOVE_EXCEPTION:
1242 int exOffset;
1243 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001244 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001245 resetReg = oatAllocTemp(cUnit);
1246 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1247 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1248 loadConstant(cUnit, resetReg, 0);
1249 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1250 storeValue(cUnit, rlDest, rlResult);
1251 break;
1252
1253 case OP_RETURN_VOID:
buzbeefe2e17f2011-10-10 09:35:02 -07001254 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001255 break;
1256
1257 case OP_RETURN:
1258 case OP_RETURN_OBJECT:
buzbeefe2e17f2011-10-10 09:35:02 -07001259 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001260 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001261 break;
1262
1263 case OP_RETURN_WIDE:
buzbeefe2e17f2011-10-10 09:35:02 -07001264 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001265 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001266 break;
1267
1268 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -07001269 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001270 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001271 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001272 break;
1273
1274 case OP_MOVE_RESULT:
1275 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -07001276 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001277 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001278 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001279 break;
1280
1281 case OP_MOVE:
1282 case OP_MOVE_OBJECT:
1283 case OP_MOVE_16:
1284 case OP_MOVE_OBJECT_16:
1285 case OP_MOVE_FROM16:
1286 case OP_MOVE_OBJECT_FROM16:
1287 storeValue(cUnit, rlDest, rlSrc[0]);
1288 break;
1289
1290 case OP_MOVE_WIDE:
1291 case OP_MOVE_WIDE_16:
1292 case OP_MOVE_WIDE_FROM16:
1293 storeValueWide(cUnit, rlDest, rlSrc[0]);
1294 break;
1295
1296 case OP_CONST:
1297 case OP_CONST_4:
1298 case OP_CONST_16:
1299 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1300 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1301 storeValue(cUnit, rlDest, rlResult);
1302 break;
1303
1304 case OP_CONST_HIGH16:
1305 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1306 loadConstantNoClobber(cUnit, rlResult.lowReg,
1307 mir->dalvikInsn.vB << 16);
1308 storeValue(cUnit, rlDest, rlResult);
1309 break;
1310
1311 case OP_CONST_WIDE_16:
1312 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -07001313 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1314 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1315 mir->dalvikInsn.vB,
1316 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -07001317 storeValueWide(cUnit, rlDest, rlResult);
1318 break;
1319
1320 case OP_CONST_WIDE:
1321 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1322 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001323 mir->dalvikInsn.vB_wide & 0xffffffff,
1324 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001325 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001326 break;
1327
1328 case OP_CONST_WIDE_HIGH16:
1329 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1330 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1331 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001332 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001333 break;
1334
1335 case OP_MONITOR_ENTER:
1336 genMonitorEnter(cUnit, mir, rlSrc[0]);
1337 break;
1338
1339 case OP_MONITOR_EXIT:
1340 genMonitorExit(cUnit, mir, rlSrc[0]);
1341 break;
1342
1343 case OP_CHECK_CAST:
1344 genCheckCast(cUnit, mir, rlSrc[0]);
1345 break;
1346
1347 case OP_INSTANCE_OF:
1348 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1349 break;
1350
1351 case OP_NEW_INSTANCE:
1352 genNewInstance(cUnit, mir, rlDest);
1353 break;
1354
1355 case OP_THROW:
1356 genThrow(cUnit, mir, rlSrc[0]);
1357 break;
1358
buzbee5ade1d22011-09-09 14:44:52 -07001359 case OP_THROW_VERIFICATION_ERROR:
1360 loadWordDisp(cUnit, rSELF,
1361 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1362 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1363 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001364 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001365 break;
1366
buzbee67bf8852011-08-17 17:51:35 -07001367 case OP_ARRAY_LENGTH:
1368 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001369 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001370 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001371 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001372 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1373 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1374 rlResult.lowReg);
1375 storeValue(cUnit, rlDest, rlResult);
1376 break;
1377
1378 case OP_CONST_STRING:
1379 case OP_CONST_STRING_JUMBO:
1380 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1381 break;
1382
1383 case OP_CONST_CLASS:
1384 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1385 break;
1386
1387 case OP_FILL_ARRAY_DATA:
1388 genFillArrayData(cUnit, mir, rlSrc[0]);
1389 break;
1390
1391 case OP_FILLED_NEW_ARRAY:
1392 genFilledNewArray(cUnit, mir, false /* not range */);
1393 break;
1394
1395 case OP_FILLED_NEW_ARRAY_RANGE:
1396 genFilledNewArray(cUnit, mir, true /* range */);
1397 break;
1398
1399 case OP_NEW_ARRAY:
1400 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1401 break;
1402
1403 case OP_GOTO:
1404 case OP_GOTO_16:
1405 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001406 if (bb->taken->startOffset <= mir->offset) {
1407 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001408 }
1409 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1410 break;
1411
1412 case OP_PACKED_SWITCH:
1413 genPackedSwitch(cUnit, mir, rlSrc[0]);
1414 break;
1415
1416 case OP_SPARSE_SWITCH:
1417 genSparseSwitch(cUnit, mir, rlSrc[0]);
1418 break;
1419
1420 case OP_CMPL_FLOAT:
1421 case OP_CMPG_FLOAT:
1422 case OP_CMPL_DOUBLE:
1423 case OP_CMPG_DOUBLE:
1424 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1425 break;
1426
1427 case OP_CMP_LONG:
1428 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1429 break;
1430
1431 case OP_IF_EQ:
1432 case OP_IF_NE:
1433 case OP_IF_LT:
1434 case OP_IF_GE:
1435 case OP_IF_GT:
1436 case OP_IF_LE: {
1437 bool backwardBranch;
1438 ArmConditionCode cond;
1439 backwardBranch = (bb->taken->startOffset <= mir->offset);
1440 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001441 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001442 }
1443 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1444 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1445 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1446 switch(opcode) {
1447 case OP_IF_EQ:
1448 cond = kArmCondEq;
1449 break;
1450 case OP_IF_NE:
1451 cond = kArmCondNe;
1452 break;
1453 case OP_IF_LT:
1454 cond = kArmCondLt;
1455 break;
1456 case OP_IF_GE:
1457 cond = kArmCondGe;
1458 break;
1459 case OP_IF_GT:
1460 cond = kArmCondGt;
1461 break;
1462 case OP_IF_LE:
1463 cond = kArmCondLe;
1464 break;
1465 default:
1466 cond = (ArmConditionCode)0;
1467 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1468 }
1469 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1470 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1471 break;
1472 }
1473
1474 case OP_IF_EQZ:
1475 case OP_IF_NEZ:
1476 case OP_IF_LTZ:
1477 case OP_IF_GEZ:
1478 case OP_IF_GTZ:
1479 case OP_IF_LEZ: {
1480 bool backwardBranch;
1481 ArmConditionCode cond;
1482 backwardBranch = (bb->taken->startOffset <= mir->offset);
1483 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001484 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001485 }
1486 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1487 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1488 switch(opcode) {
1489 case OP_IF_EQZ:
1490 cond = kArmCondEq;
1491 break;
1492 case OP_IF_NEZ:
1493 cond = kArmCondNe;
1494 break;
1495 case OP_IF_LTZ:
1496 cond = kArmCondLt;
1497 break;
1498 case OP_IF_GEZ:
1499 cond = kArmCondGe;
1500 break;
1501 case OP_IF_GTZ:
1502 cond = kArmCondGt;
1503 break;
1504 case OP_IF_LEZ:
1505 cond = kArmCondLe;
1506 break;
1507 default:
1508 cond = (ArmConditionCode)0;
1509 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1510 }
1511 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1512 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1513 break;
1514 }
1515
1516 case OP_AGET_WIDE:
1517 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1518 break;
1519 case OP_AGET:
1520 case OP_AGET_OBJECT:
1521 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1522 break;
1523 case OP_AGET_BOOLEAN:
1524 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1525 rlDest, 0);
1526 break;
1527 case OP_AGET_BYTE:
1528 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1529 break;
1530 case OP_AGET_CHAR:
1531 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1532 rlDest, 1);
1533 break;
1534 case OP_AGET_SHORT:
1535 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1536 break;
1537 case OP_APUT_WIDE:
1538 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1539 break;
1540 case OP_APUT:
1541 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1542 break;
1543 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001544 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001545 break;
1546 case OP_APUT_SHORT:
1547 case OP_APUT_CHAR:
1548 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1549 rlSrc[0], 1);
1550 break;
1551 case OP_APUT_BYTE:
1552 case OP_APUT_BOOLEAN:
1553 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1554 rlSrc[0], 0);
1555 break;
1556
1557 case OP_IGET_WIDE:
1558 case OP_IGET_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001559 genIGetWide(cUnit, mir, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001560 break;
1561
1562 case OP_IGET:
1563 case OP_IGET_VOLATILE:
1564 case OP_IGET_OBJECT:
1565 case OP_IGET_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001566 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001567 break;
1568
1569 case OP_IGET_BOOLEAN:
1570 case OP_IGET_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001571 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001572 break;
1573
1574 case OP_IGET_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001575 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001576 break;
1577
1578 case OP_IGET_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001579 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001580 break;
1581
1582 case OP_IPUT_WIDE:
1583 case OP_IPUT_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001584 genIPutWide(cUnit, mir, rlSrc[0], rlSrc[1]);
buzbee67bf8852011-08-17 17:51:35 -07001585 break;
1586
1587 case OP_IPUT_OBJECT:
1588 case OP_IPUT_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001589 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
buzbee67bf8852011-08-17 17:51:35 -07001590 break;
1591
1592 case OP_IPUT:
1593 case OP_IPUT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001594 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001595 break;
1596
1597 case OP_IPUT_BOOLEAN:
1598 case OP_IPUT_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001599 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001600 break;
1601
1602 case OP_IPUT_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001603 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001604 break;
1605
1606 case OP_IPUT_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001607 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001608 break;
1609
1610 case OP_SGET:
1611 case OP_SGET_OBJECT:
1612 case OP_SGET_BOOLEAN:
1613 case OP_SGET_BYTE:
1614 case OP_SGET_CHAR:
1615 case OP_SGET_SHORT:
1616 genSget(cUnit, mir, rlResult, rlDest);
1617 break;
1618
1619 case OP_SGET_WIDE:
1620 genSgetWide(cUnit, mir, rlResult, rlDest);
1621 break;
1622
1623 case OP_SPUT:
1624 case OP_SPUT_OBJECT:
1625 case OP_SPUT_BOOLEAN:
1626 case OP_SPUT_BYTE:
1627 case OP_SPUT_CHAR:
1628 case OP_SPUT_SHORT:
1629 genSput(cUnit, mir, rlSrc[0]);
1630 break;
1631
1632 case OP_SPUT_WIDE:
1633 genSputWide(cUnit, mir, rlSrc[0]);
1634 break;
1635
1636 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001637 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1638 true /*range*/);
1639 break;
buzbee67bf8852011-08-17 17:51:35 -07001640 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001641 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1642 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001643 break;
1644
1645 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001646 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1647 false /*range*/);
1648 break;
buzbee67bf8852011-08-17 17:51:35 -07001649 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001650 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1651 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001652 break;
1653
1654 case OP_INVOKE_VIRTUAL:
1655 case OP_INVOKE_VIRTUAL_RANGE:
1656 genInvokeVirtual(cUnit, mir);
1657 break;
1658
1659 case OP_INVOKE_SUPER:
1660 case OP_INVOKE_SUPER_RANGE:
1661 genInvokeSuper(cUnit, mir);
1662 break;
1663
1664 case OP_INVOKE_INTERFACE:
1665 case OP_INVOKE_INTERFACE_RANGE:
1666 genInvokeInterface(cUnit, mir);
1667 break;
1668
1669 case OP_NEG_INT:
1670 case OP_NOT_INT:
1671 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1672 break;
1673
1674 case OP_NEG_LONG:
1675 case OP_NOT_LONG:
1676 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1677 break;
1678
1679 case OP_NEG_FLOAT:
1680 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1681 break;
1682
1683 case OP_NEG_DOUBLE:
1684 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1685 break;
1686
1687 case OP_INT_TO_LONG:
1688 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1689 if (rlSrc[0].location == kLocPhysReg) {
1690 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1691 } else {
1692 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1693 }
1694 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1695 rlResult.lowReg, 31);
1696 storeValueWide(cUnit, rlDest, rlResult);
1697 break;
1698
1699 case OP_LONG_TO_INT:
1700 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1701 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1702 storeValue(cUnit, rlDest, rlSrc[0]);
1703 break;
1704
1705 case OP_INT_TO_BYTE:
1706 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1707 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1708 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1709 storeValue(cUnit, rlDest, rlResult);
1710 break;
1711
1712 case OP_INT_TO_SHORT:
1713 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1714 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1715 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1716 storeValue(cUnit, rlDest, rlResult);
1717 break;
1718
1719 case OP_INT_TO_CHAR:
1720 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1721 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1722 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1723 storeValue(cUnit, rlDest, rlResult);
1724 break;
1725
1726 case OP_INT_TO_FLOAT:
1727 case OP_INT_TO_DOUBLE:
1728 case OP_LONG_TO_FLOAT:
1729 case OP_LONG_TO_DOUBLE:
1730 case OP_FLOAT_TO_INT:
1731 case OP_FLOAT_TO_LONG:
1732 case OP_FLOAT_TO_DOUBLE:
1733 case OP_DOUBLE_TO_INT:
1734 case OP_DOUBLE_TO_LONG:
1735 case OP_DOUBLE_TO_FLOAT:
1736 genConversion(cUnit, mir);
1737 break;
1738
1739 case OP_ADD_INT:
1740 case OP_SUB_INT:
1741 case OP_MUL_INT:
1742 case OP_DIV_INT:
1743 case OP_REM_INT:
1744 case OP_AND_INT:
1745 case OP_OR_INT:
1746 case OP_XOR_INT:
1747 case OP_SHL_INT:
1748 case OP_SHR_INT:
1749 case OP_USHR_INT:
1750 case OP_ADD_INT_2ADDR:
1751 case OP_SUB_INT_2ADDR:
1752 case OP_MUL_INT_2ADDR:
1753 case OP_DIV_INT_2ADDR:
1754 case OP_REM_INT_2ADDR:
1755 case OP_AND_INT_2ADDR:
1756 case OP_OR_INT_2ADDR:
1757 case OP_XOR_INT_2ADDR:
1758 case OP_SHL_INT_2ADDR:
1759 case OP_SHR_INT_2ADDR:
1760 case OP_USHR_INT_2ADDR:
1761 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1762 break;
1763
1764 case OP_ADD_LONG:
1765 case OP_SUB_LONG:
1766 case OP_MUL_LONG:
1767 case OP_DIV_LONG:
1768 case OP_REM_LONG:
1769 case OP_AND_LONG:
1770 case OP_OR_LONG:
1771 case OP_XOR_LONG:
1772 case OP_ADD_LONG_2ADDR:
1773 case OP_SUB_LONG_2ADDR:
1774 case OP_MUL_LONG_2ADDR:
1775 case OP_DIV_LONG_2ADDR:
1776 case OP_REM_LONG_2ADDR:
1777 case OP_AND_LONG_2ADDR:
1778 case OP_OR_LONG_2ADDR:
1779 case OP_XOR_LONG_2ADDR:
1780 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1781 break;
1782
buzbee67bf8852011-08-17 17:51:35 -07001783 case OP_SHL_LONG:
1784 case OP_SHR_LONG:
1785 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001786 case OP_SHL_LONG_2ADDR:
1787 case OP_SHR_LONG_2ADDR:
1788 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001789 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1790 break;
1791
1792 case OP_ADD_FLOAT:
1793 case OP_SUB_FLOAT:
1794 case OP_MUL_FLOAT:
1795 case OP_DIV_FLOAT:
1796 case OP_REM_FLOAT:
1797 case OP_ADD_FLOAT_2ADDR:
1798 case OP_SUB_FLOAT_2ADDR:
1799 case OP_MUL_FLOAT_2ADDR:
1800 case OP_DIV_FLOAT_2ADDR:
1801 case OP_REM_FLOAT_2ADDR:
1802 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1803 break;
1804
1805 case OP_ADD_DOUBLE:
1806 case OP_SUB_DOUBLE:
1807 case OP_MUL_DOUBLE:
1808 case OP_DIV_DOUBLE:
1809 case OP_REM_DOUBLE:
1810 case OP_ADD_DOUBLE_2ADDR:
1811 case OP_SUB_DOUBLE_2ADDR:
1812 case OP_MUL_DOUBLE_2ADDR:
1813 case OP_DIV_DOUBLE_2ADDR:
1814 case OP_REM_DOUBLE_2ADDR:
1815 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1816 break;
1817
1818 case OP_RSUB_INT:
1819 case OP_ADD_INT_LIT16:
1820 case OP_MUL_INT_LIT16:
1821 case OP_DIV_INT_LIT16:
1822 case OP_REM_INT_LIT16:
1823 case OP_AND_INT_LIT16:
1824 case OP_OR_INT_LIT16:
1825 case OP_XOR_INT_LIT16:
1826 case OP_ADD_INT_LIT8:
1827 case OP_RSUB_INT_LIT8:
1828 case OP_MUL_INT_LIT8:
1829 case OP_DIV_INT_LIT8:
1830 case OP_REM_INT_LIT8:
1831 case OP_AND_INT_LIT8:
1832 case OP_OR_INT_LIT8:
1833 case OP_XOR_INT_LIT8:
1834 case OP_SHL_INT_LIT8:
1835 case OP_SHR_INT_LIT8:
1836 case OP_USHR_INT_LIT8:
1837 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1838 break;
1839
1840 default:
1841 res = true;
1842 }
1843 return res;
1844}
1845
Elliott Hughesc1f143d2011-12-01 17:31:10 -08001846STATIC const char* extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001847 "kMirOpPhi",
1848 "kMirOpNullNRangeUpCheck",
1849 "kMirOpNullNRangeDownCheck",
1850 "kMirOpLowerBound",
1851 "kMirOpPunt",
1852 "kMirOpCheckInlinePrediction",
1853};
1854
1855/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001856STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001857{
1858 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1859 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1860 strcpy(msg, extendedMIROpNames[opOffset]);
1861 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1862
1863 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1864 case kMirOpPhi: {
1865 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1866 op->flags.isNop = true;
1867 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1868 break;
1869 }
1870 default:
1871 break;
1872 }
1873}
1874
buzbee67bc2362011-10-11 18:08:40 -07001875/*
1876 * If there are any ins passed in registers that have not been promoted
1877 * to a callee-save register, flush them to the frame. Perform intial
1878 * assignment of promoted arguments.
1879 */
buzbeeed3e9302011-09-23 17:34:19 -07001880STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001881{
Ian Rogersa3760aa2011-11-14 14:32:37 -08001882 if (cUnit->numIns == 0)
buzbee67bf8852011-08-17 17:51:35 -07001883 return;
buzbee67bc2362011-10-11 18:08:40 -07001884 int firstArgReg = r1;
1885 int lastArgReg = r3;
Ian Rogersa3760aa2011-11-14 14:32:37 -08001886 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
buzbee8febc582011-10-25 12:39:20 -07001887 /*
1888 * Arguments passed in registers should be flushed
1889 * to their backing locations in the frame for now.
1890 * Also, we need to do initial assignment for promoted
1891 * arguments. NOTE: an older version of dx had an issue
1892 * in which it would reuse static method argument registers.
1893 * This could result in the same Dalvik virtual register
1894 * being promoted to both core and fp regs. In those
1895 * cases, copy argument to both. This will be uncommon
1896 * enough that it isn't worth attempting to optimize.
1897 */
Ian Rogersa3760aa2011-11-14 14:32:37 -08001898 for (int i = 0; i < cUnit->numIns; i++) {
buzbee67bc2362011-10-11 18:08:40 -07001899 PromotionMap vMap = cUnit->promotionMap[startVReg + i];
buzbee67bc2362011-10-11 18:08:40 -07001900 if (i <= (lastArgReg - firstArgReg)) {
buzbee8febc582011-10-25 12:39:20 -07001901 // If arriving in register
buzbee67bc2362011-10-11 18:08:40 -07001902 if (vMap.coreLocation == kLocPhysReg) {
1903 genRegCopy(cUnit, vMap.coreReg, firstArgReg + i);
buzbee8febc582011-10-25 12:39:20 -07001904 }
1905 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001906 genRegCopy(cUnit, vMap.fpReg, firstArgReg + i);
1907 }
1908 // Also put a copy in memory in case we're partially promoted
1909 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1910 firstArgReg + i, kWord);
1911 } else {
buzbee8febc582011-10-25 12:39:20 -07001912 // If arriving in frame & promoted
buzbee67bc2362011-10-11 18:08:40 -07001913 if (vMap.coreLocation == kLocPhysReg) {
1914 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1915 vMap.coreReg);
buzbee8febc582011-10-25 12:39:20 -07001916 }
1917 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001918 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1919 vMap.fpReg);
buzbee67bf8852011-08-17 17:51:35 -07001920 }
1921 }
buzbee67bf8852011-08-17 17:51:35 -07001922 }
1923}
1924
1925/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001926STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001927{
1928 MIR* mir;
1929 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1930 int blockId = bb->id;
1931
1932 cUnit->curBlock = bb;
1933 labelList[blockId].operands[0] = bb->startOffset;
1934
1935 /* Insert the block label */
1936 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1937 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1938
buzbee6181f792011-09-29 11:14:04 -07001939 /* Reset local optimization data on block boundaries */
1940 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001941 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001942 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001943
1944 ArmLIR* headLIR = NULL;
1945
buzbeebbaf8942011-10-02 13:08:29 -07001946 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001947 if (bb->blockType == kEntryBlock) {
1948 /*
1949 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1950 * mechanism know so it doesn't try to use any of them when
1951 * expanding the frame or flushing. This leaves the utility
1952 * code with a single temp: r12. This should be enough.
1953 */
1954 oatLockTemp(cUnit, r0);
1955 oatLockTemp(cUnit, r1);
1956 oatLockTemp(cUnit, r2);
1957 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001958
1959 /*
1960 * We can safely skip the stack overflow check if we're
1961 * a leaf *and* our frame size < fudge factor.
1962 */
1963 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1964 ((size_t)cUnit->frameSize <
1965 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001966 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001967 if (!skipOverflowCheck) {
1968 /* Load stack limit */
1969 loadWordDisp(cUnit, rSELF,
1970 art::Thread::StackEndOffset().Int32Value(), r12);
1971 }
buzbee67bf8852011-08-17 17:51:35 -07001972 /* Spill core callee saves */
1973 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1974 /* Need to spill any FP regs? */
1975 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001976 /*
1977 * NOTE: fp spills are a little different from core spills in that
1978 * they are pushed as a contiguous block. When promoting from
1979 * the fp set, we must allocate all singles from s16..highest-promoted
1980 */
buzbee67bf8852011-08-17 17:51:35 -07001981 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1982 }
buzbeecefd1872011-09-09 09:59:52 -07001983 if (!skipOverflowCheck) {
1984 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001985 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001986 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1987 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001988 genRegCopy(cUnit, rSP, rLR); // Establish stack
1989 } else {
1990 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001991 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001992 }
buzbee67bf8852011-08-17 17:51:35 -07001993 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1994 flushIns(cUnit);
1995 oatFreeTemp(cUnit, r0);
1996 oatFreeTemp(cUnit, r1);
1997 oatFreeTemp(cUnit, r2);
1998 oatFreeTemp(cUnit, r3);
1999 } else if (bb->blockType == kExitBlock) {
2000 newLIR0(cUnit, kArmPseudoMethodExit);
buzbeebbaf8942011-10-02 13:08:29 -07002001 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07002002 /* Need to restore any FP callee saves? */
2003 if (cUnit->numFPSpills) {
2004 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
2005 }
2006 if (cUnit->coreSpillMask & (1 << rLR)) {
2007 /* Unspill rLR to rPC */
2008 cUnit->coreSpillMask &= ~(1 << rLR);
2009 cUnit->coreSpillMask |= (1 << rPC);
2010 }
2011 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
2012 if (!(cUnit->coreSpillMask & (1 << rPC))) {
2013 /* We didn't pop to rPC, so must do a bv rLR */
2014 newLIR1(cUnit, kThumbBx, rLR);
2015 }
2016 }
2017
2018 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
2019
2020 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07002021 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
2022 oatClobberAllRegs(cUnit);
2023 }
buzbee67bf8852011-08-17 17:51:35 -07002024
2025 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
2026 oatResetDefTracking(cUnit);
2027 }
2028
2029 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
2030 handleExtendedMethodMIR(cUnit, mir);
2031 continue;
2032 }
2033
2034 cUnit->currentDalvikOffset = mir->offset;
2035
2036 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2037 InstructionFormat dalvikFormat =
2038 dexGetFormatFromOpcode(dalvikOpcode);
2039
2040 ArmLIR* boundaryLIR;
2041
2042 /* Mark the beginning of a Dalvik instruction for line tracking */
2043 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
2044 (int) oatGetDalvikDisassembly(
2045 &mir->dalvikInsn, ""));
2046 /* Remember the first LIR for this block */
2047 if (headLIR == NULL) {
2048 headLIR = boundaryLIR;
2049 /* Set the first boundaryLIR as a scheduling barrier */
2050 headLIR->defMask = ENCODE_ALL;
2051 }
2052
2053 /* Don't generate the SSA annotation unless verbose mode is on */
2054 if (cUnit->printMe && mir->ssaRep) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -08002055 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
buzbee67bf8852011-08-17 17:51:35 -07002056 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
2057 }
2058
2059 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
2060
2061 if (notHandled) {
2062 char buf[100];
2063 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
2064 mir->offset,
2065 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
2066 dalvikFormat);
2067 LOG(FATAL) << buf;
2068 }
2069 }
2070
2071 if (headLIR) {
2072 /*
2073 * Eliminate redundant loads/stores and delay stores into later
2074 * slots
2075 */
2076 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
2077 cUnit->lastLIRInsn);
2078
2079 /*
2080 * Generate an unconditional branch to the fallthrough block.
2081 */
2082 if (bb->fallThrough) {
2083 genUnconditionalBranch(cUnit,
2084 &labelList[bb->fallThrough->id]);
2085 }
2086 }
2087 return false;
2088}
2089
2090/*
2091 * Nop any unconditional branches that go to the next instruction.
2092 * Note: new redundant branches may be inserted later, and we'll
2093 * use a check in final instruction assembly to nop those out.
2094 */
2095void removeRedundantBranches(CompilationUnit* cUnit)
2096{
2097 ArmLIR* thisLIR;
2098
2099 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
2100 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
2101 thisLIR = NEXT_LIR(thisLIR)) {
2102
2103 /* Branch to the next instruction */
2104 if ((thisLIR->opcode == kThumbBUncond) ||
2105 (thisLIR->opcode == kThumb2BUncond)) {
2106 ArmLIR* nextLIR = thisLIR;
2107
2108 while (true) {
2109 nextLIR = NEXT_LIR(nextLIR);
2110
2111 /*
2112 * Is the branch target the next instruction?
2113 */
2114 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
2115 thisLIR->flags.isNop = true;
2116 break;
2117 }
2118
2119 /*
2120 * Found real useful stuff between the branch and the target.
2121 * Need to explicitly check the lastLIRInsn here because it
2122 * might be the last real instruction.
2123 */
2124 if (!isPseudoOpcode(nextLIR->opcode) ||
2125 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
2126 break;
2127 }
2128 }
2129 }
2130}
2131
buzbeeed3e9302011-09-23 17:34:19 -07002132STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07002133{
2134 ArmLIR** suspendLabel =
2135 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
2136 int numElems = cUnit->suspendLaunchpads.numUsed;
2137
2138 for (int i = 0; i < numElems; i++) {
2139 /* TUNING: move suspend count load into helper */
2140 ArmLIR* lab = suspendLabel[i];
2141 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
2142 cUnit->currentDalvikOffset = lab->operands[1];
2143 oatAppendLIR(cUnit, (LIR *)lab);
2144 loadWordDisp(cUnit, rSELF,
2145 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
2146 loadWordDisp(cUnit, rSELF,
2147 art::Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
2148 opReg(cUnit, kOpBlx, rLR);
2149 genUnconditionalBranch(cUnit, resumeLab);
2150 }
2151}
2152
buzbeeed3e9302011-09-23 17:34:19 -07002153STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07002154{
2155 ArmLIR** throwLabel =
2156 (ArmLIR **) cUnit->throwLaunchpads.elemList;
2157 int numElems = cUnit->throwLaunchpads.numUsed;
2158 int i;
2159
2160 for (i = 0; i < numElems; i++) {
2161 ArmLIR* lab = throwLabel[i];
2162 cUnit->currentDalvikOffset = lab->operands[1];
2163 oatAppendLIR(cUnit, (LIR *)lab);
2164 int funcOffset = 0;
2165 int v1 = lab->operands[2];
2166 int v2 = lab->operands[3];
2167 switch(lab->operands[0]) {
2168 case kArmThrowNullPointer:
2169 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2170 break;
2171 case kArmThrowArrayBounds:
2172 if (v2 != r0) {
2173 genRegCopy(cUnit, r0, v1);
2174 genRegCopy(cUnit, r1, v2);
2175 } else {
2176 if (v1 == r1) {
2177 genRegCopy(cUnit, r12, v1);
2178 genRegCopy(cUnit, r1, v2);
2179 genRegCopy(cUnit, r0, r12);
2180 } else {
2181 genRegCopy(cUnit, r1, v2);
2182 genRegCopy(cUnit, r0, v1);
2183 }
2184 }
2185 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2186 break;
2187 case kArmThrowDivZero:
2188 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2189 break;
2190 case kArmThrowVerificationError:
2191 loadConstant(cUnit, r0, v1);
2192 loadConstant(cUnit, r1, v2);
2193 funcOffset =
2194 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2195 break;
2196 case kArmThrowNegArraySize:
2197 genRegCopy(cUnit, r0, v1);
2198 funcOffset =
2199 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2200 break;
buzbee5ade1d22011-09-09 14:44:52 -07002201 case kArmThrowNoSuchMethod:
buzbee95f08792012-01-11 10:51:30 -08002202 genRegCopy(cUnit, r0, v2);
buzbee5ade1d22011-09-09 14:44:52 -07002203 funcOffset =
2204 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2205 break;
buzbeeec5adf32011-09-11 15:25:43 -07002206 case kArmThrowStackOverflow:
2207 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07002208 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07002209 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07002210 opRegImm(cUnit, kOpAdd, rSP,
2211 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07002212 break;
buzbee5ade1d22011-09-09 14:44:52 -07002213 default:
2214 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2215 }
2216 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07002217 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07002218 }
2219}
2220
buzbee67bf8852011-08-17 17:51:35 -07002221void oatMethodMIR2LIR(CompilationUnit* cUnit)
2222{
2223 /* Used to hold the labels of each block */
2224 cUnit->blockLabelList =
2225 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2226
2227 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2228 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07002229 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002230
2231 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07002232
2233 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002234}
2235
2236/* Common initialization routine for an architecture family */
2237bool oatArchInit()
2238{
2239 int i;
2240
2241 for (i = 0; i < kArmLast; i++) {
2242 if (EncodingMap[i].opcode != i) {
2243 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2244 " is wrong: expecting " << i << ", seeing " <<
2245 (int)EncodingMap[i].opcode;
2246 }
2247 }
2248
2249 return oatArchVariantInit();
2250}
2251
2252/* Needed by the Assembler */
2253void oatSetupResourceMasks(ArmLIR* lir)
2254{
2255 setupResourceMasks(lir);
2256}
2257
2258/* Needed by the ld/st optmizatons */
2259ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2260{
2261 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2262}
2263
2264/* Needed by the register allocator */
2265ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2266{
2267 return genRegCopy(cUnit, rDest, rSrc);
2268}
2269
2270/* Needed by the register allocator */
2271void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2272 int srcLo, int srcHi)
2273{
2274 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2275}
2276
2277void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2278 int displacement, int rSrc, OpSize size)
2279{
2280 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2281}
2282
2283void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2284 int displacement, int rSrcLo, int rSrcHi)
2285{
2286 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2287}