blob: 2c3ee0d3a26b8a23529ac3573b5b69e0b78b3bef [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;
718 case 1: // Get method->dex_cache_resolved_methods_ [usr r0, set rLR]
719 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);
buzbee34cd9e52011-09-08 14:31:52 -07001081 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
Ian Rogersbd441352011-10-31 10:49:47 -07001082 Thread* thread = Thread::Current();
1083 if (thread->IsExceptionPending()) { // clear any exception left by resolve method
1084 thread->ClearException();
1085 }
buzbee4a3164f2011-09-03 11:25:10 -07001086 fastPath = false;
1087 } else {
Ian Rogersa3760aa2011-11-14 14:32:37 -08001088 Class* declaring_class = cUnit->dex_cache->GetResolvedTypes()
1089 ->Get(cUnit->dex_file->GetMethodId(cUnit->method_idx).class_idx_);
1090 Class* superClass = (declaring_class != NULL)
1091 ? declaring_class->GetSuperClass() : NULL;
buzbee4a3164f2011-09-03 11:25:10 -07001092 if (superClass == NULL) {
1093 fastPath = false;
1094 } else {
1095 int32_t target_idx = baseMethod->GetMethodIndex();
1096 if (superClass->GetVTable()->GetLength() <= target_idx) {
1097 fastPath = false;
1098 } else {
1099 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
1100 }
1101 }
1102 }
1103 if (fastPath) {
1104 nextCallInsn = nextSuperCallInsn;
1105 rollback = NULL;
1106 } else {
1107 nextCallInsn = nextSuperCallInsnSP;
1108 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1109 rollback->defMask = -1;
1110 }
buzbee67bf8852011-08-17 17:51:35 -07001111 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
buzbeec0ecd652011-09-25 18:11:54 -07001112 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001113 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001114 else
buzbeec0ecd652011-09-25 18:11:54 -07001115 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001116 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001117 // Finish up any of the call sequence not interleaved in arg loading
1118 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -07001119 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001120 }
buzbeece302932011-10-04 14:32:18 -07001121 if (DISPLAY_MISSING_TARGETS) {
1122 genShowTarget(cUnit);
1123 }
buzbeeec5adf32011-09-11 15:25:43 -07001124 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001125 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001126}
1127
buzbeeed3e9302011-09-23 17:34:19 -07001128STATIC void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001129{
1130 DecodedInstruction* dInsn = &mir->dalvikInsn;
1131 int callState = 0;
buzbee561227c2011-09-02 15:28:19 -07001132 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001133 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
Ian Rogersa3760aa2011-11-14 14:32:37 -08001134 Method* method = class_linker->ResolveMethod(*cUnit->dex_file,
1135 dInsn->vB,
1136 cUnit->dex_cache,
1137 cUnit->class_loader,
1138 false);
buzbee561227c2011-09-02 15:28:19 -07001139 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -07001140 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee7b1b86d2011-08-26 18:59:10 -07001141
buzbee109bd6a2011-09-06 13:58:41 -07001142 // Explicit register usage
1143 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001144 if (SLOW_INVOKE_PATH || method == NULL) {
Ian Rogersbd441352011-10-31 10:49:47 -07001145 Thread* thread = Thread::Current();
1146 if (thread->IsExceptionPending()) { // clear any exception left by resolve method
1147 thread->ClearException();
1148 }
buzbee561227c2011-09-02 15:28:19 -07001149 // Slow path
1150 nextCallInsn = nextVCallInsnSP;
1151 // If we need a slow-path callout, we'll restart here
1152 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1153 rollback->defMask = -1;
1154 } else {
1155 // Fast path
1156 nextCallInsn = nextVCallInsn;
1157 rollback = NULL;
1158 }
buzbee67bf8852011-08-17 17:51:35 -07001159 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
buzbeec0ecd652011-09-25 18:11:54 -07001160 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001161 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001162 else
buzbeec0ecd652011-09-25 18:11:54 -07001163 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001164 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001165 // Finish up any of the call sequence not interleaved in arg loading
1166 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001167 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001168 }
buzbeece302932011-10-04 14:32:18 -07001169 if (DISPLAY_MISSING_TARGETS) {
1170 genShowTarget(cUnit);
1171 }
buzbeeec5adf32011-09-11 15:25:43 -07001172 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001173 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001174}
1175
buzbeeed3e9302011-09-23 17:34:19 -07001176STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001177 BasicBlock* bb, ArmLIR* labelList)
1178{
1179 bool res = false; // Assume success
1180 RegLocation rlSrc[3];
1181 RegLocation rlDest = badLoc;
1182 RegLocation rlResult = badLoc;
1183 Opcode opcode = mir->dalvikInsn.opcode;
1184
1185 /* Prep Src and Dest locations */
1186 int nextSreg = 0;
1187 int nextLoc = 0;
1188 int attrs = oatDataFlowAttributes[opcode];
1189 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1190 if (attrs & DF_UA) {
1191 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1192 nextSreg++;
1193 } else if (attrs & DF_UA_WIDE) {
1194 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1195 nextSreg + 1);
1196 nextSreg+= 2;
1197 }
1198 if (attrs & DF_UB) {
1199 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1200 nextSreg++;
1201 } else if (attrs & DF_UB_WIDE) {
1202 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1203 nextSreg + 1);
1204 nextSreg+= 2;
1205 }
1206 if (attrs & DF_UC) {
1207 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1208 } else if (attrs & DF_UC_WIDE) {
1209 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1210 nextSreg + 1);
1211 }
1212 if (attrs & DF_DA) {
1213 rlDest = oatGetDest(cUnit, mir, 0);
1214 } else if (attrs & DF_DA_WIDE) {
1215 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1216 }
1217
1218 switch(opcode) {
1219 case OP_NOP:
1220 break;
1221
1222 case OP_MOVE_EXCEPTION:
1223 int exOffset;
1224 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001225 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001226 resetReg = oatAllocTemp(cUnit);
1227 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1228 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1229 loadConstant(cUnit, resetReg, 0);
1230 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1231 storeValue(cUnit, rlDest, rlResult);
1232 break;
1233
1234 case OP_RETURN_VOID:
buzbeefe2e17f2011-10-10 09:35:02 -07001235 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001236 break;
1237
1238 case OP_RETURN:
1239 case OP_RETURN_OBJECT:
buzbeefe2e17f2011-10-10 09:35:02 -07001240 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001241 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001242 break;
1243
1244 case OP_RETURN_WIDE:
buzbeefe2e17f2011-10-10 09:35:02 -07001245 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001246 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001247 break;
1248
1249 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -07001250 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001251 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001252 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001253 break;
1254
1255 case OP_MOVE_RESULT:
1256 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -07001257 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001258 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001259 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001260 break;
1261
1262 case OP_MOVE:
1263 case OP_MOVE_OBJECT:
1264 case OP_MOVE_16:
1265 case OP_MOVE_OBJECT_16:
1266 case OP_MOVE_FROM16:
1267 case OP_MOVE_OBJECT_FROM16:
1268 storeValue(cUnit, rlDest, rlSrc[0]);
1269 break;
1270
1271 case OP_MOVE_WIDE:
1272 case OP_MOVE_WIDE_16:
1273 case OP_MOVE_WIDE_FROM16:
1274 storeValueWide(cUnit, rlDest, rlSrc[0]);
1275 break;
1276
1277 case OP_CONST:
1278 case OP_CONST_4:
1279 case OP_CONST_16:
1280 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1281 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1282 storeValue(cUnit, rlDest, rlResult);
1283 break;
1284
1285 case OP_CONST_HIGH16:
1286 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1287 loadConstantNoClobber(cUnit, rlResult.lowReg,
1288 mir->dalvikInsn.vB << 16);
1289 storeValue(cUnit, rlDest, rlResult);
1290 break;
1291
1292 case OP_CONST_WIDE_16:
1293 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -07001294 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1295 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1296 mir->dalvikInsn.vB,
1297 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -07001298 storeValueWide(cUnit, rlDest, rlResult);
1299 break;
1300
1301 case OP_CONST_WIDE:
1302 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1303 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001304 mir->dalvikInsn.vB_wide & 0xffffffff,
1305 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001306 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001307 break;
1308
1309 case OP_CONST_WIDE_HIGH16:
1310 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1311 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1312 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001313 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001314 break;
1315
1316 case OP_MONITOR_ENTER:
1317 genMonitorEnter(cUnit, mir, rlSrc[0]);
1318 break;
1319
1320 case OP_MONITOR_EXIT:
1321 genMonitorExit(cUnit, mir, rlSrc[0]);
1322 break;
1323
1324 case OP_CHECK_CAST:
1325 genCheckCast(cUnit, mir, rlSrc[0]);
1326 break;
1327
1328 case OP_INSTANCE_OF:
1329 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1330 break;
1331
1332 case OP_NEW_INSTANCE:
1333 genNewInstance(cUnit, mir, rlDest);
1334 break;
1335
1336 case OP_THROW:
1337 genThrow(cUnit, mir, rlSrc[0]);
1338 break;
1339
buzbee5ade1d22011-09-09 14:44:52 -07001340 case OP_THROW_VERIFICATION_ERROR:
1341 loadWordDisp(cUnit, rSELF,
1342 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1343 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1344 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001345 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001346 break;
1347
buzbee67bf8852011-08-17 17:51:35 -07001348 case OP_ARRAY_LENGTH:
1349 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001350 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001351 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001352 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001353 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1354 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1355 rlResult.lowReg);
1356 storeValue(cUnit, rlDest, rlResult);
1357 break;
1358
1359 case OP_CONST_STRING:
1360 case OP_CONST_STRING_JUMBO:
1361 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1362 break;
1363
1364 case OP_CONST_CLASS:
1365 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1366 break;
1367
1368 case OP_FILL_ARRAY_DATA:
1369 genFillArrayData(cUnit, mir, rlSrc[0]);
1370 break;
1371
1372 case OP_FILLED_NEW_ARRAY:
1373 genFilledNewArray(cUnit, mir, false /* not range */);
1374 break;
1375
1376 case OP_FILLED_NEW_ARRAY_RANGE:
1377 genFilledNewArray(cUnit, mir, true /* range */);
1378 break;
1379
1380 case OP_NEW_ARRAY:
1381 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1382 break;
1383
1384 case OP_GOTO:
1385 case OP_GOTO_16:
1386 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001387 if (bb->taken->startOffset <= mir->offset) {
1388 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001389 }
1390 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1391 break;
1392
1393 case OP_PACKED_SWITCH:
1394 genPackedSwitch(cUnit, mir, rlSrc[0]);
1395 break;
1396
1397 case OP_SPARSE_SWITCH:
1398 genSparseSwitch(cUnit, mir, rlSrc[0]);
1399 break;
1400
1401 case OP_CMPL_FLOAT:
1402 case OP_CMPG_FLOAT:
1403 case OP_CMPL_DOUBLE:
1404 case OP_CMPG_DOUBLE:
1405 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1406 break;
1407
1408 case OP_CMP_LONG:
1409 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1410 break;
1411
1412 case OP_IF_EQ:
1413 case OP_IF_NE:
1414 case OP_IF_LT:
1415 case OP_IF_GE:
1416 case OP_IF_GT:
1417 case OP_IF_LE: {
1418 bool backwardBranch;
1419 ArmConditionCode cond;
1420 backwardBranch = (bb->taken->startOffset <= mir->offset);
1421 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001422 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001423 }
1424 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1425 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1426 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1427 switch(opcode) {
1428 case OP_IF_EQ:
1429 cond = kArmCondEq;
1430 break;
1431 case OP_IF_NE:
1432 cond = kArmCondNe;
1433 break;
1434 case OP_IF_LT:
1435 cond = kArmCondLt;
1436 break;
1437 case OP_IF_GE:
1438 cond = kArmCondGe;
1439 break;
1440 case OP_IF_GT:
1441 cond = kArmCondGt;
1442 break;
1443 case OP_IF_LE:
1444 cond = kArmCondLe;
1445 break;
1446 default:
1447 cond = (ArmConditionCode)0;
1448 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1449 }
1450 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1451 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1452 break;
1453 }
1454
1455 case OP_IF_EQZ:
1456 case OP_IF_NEZ:
1457 case OP_IF_LTZ:
1458 case OP_IF_GEZ:
1459 case OP_IF_GTZ:
1460 case OP_IF_LEZ: {
1461 bool backwardBranch;
1462 ArmConditionCode cond;
1463 backwardBranch = (bb->taken->startOffset <= mir->offset);
1464 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001465 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001466 }
1467 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1468 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1469 switch(opcode) {
1470 case OP_IF_EQZ:
1471 cond = kArmCondEq;
1472 break;
1473 case OP_IF_NEZ:
1474 cond = kArmCondNe;
1475 break;
1476 case OP_IF_LTZ:
1477 cond = kArmCondLt;
1478 break;
1479 case OP_IF_GEZ:
1480 cond = kArmCondGe;
1481 break;
1482 case OP_IF_GTZ:
1483 cond = kArmCondGt;
1484 break;
1485 case OP_IF_LEZ:
1486 cond = kArmCondLe;
1487 break;
1488 default:
1489 cond = (ArmConditionCode)0;
1490 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1491 }
1492 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1493 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1494 break;
1495 }
1496
1497 case OP_AGET_WIDE:
1498 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1499 break;
1500 case OP_AGET:
1501 case OP_AGET_OBJECT:
1502 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1503 break;
1504 case OP_AGET_BOOLEAN:
1505 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1506 rlDest, 0);
1507 break;
1508 case OP_AGET_BYTE:
1509 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1510 break;
1511 case OP_AGET_CHAR:
1512 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1513 rlDest, 1);
1514 break;
1515 case OP_AGET_SHORT:
1516 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1517 break;
1518 case OP_APUT_WIDE:
1519 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1520 break;
1521 case OP_APUT:
1522 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1523 break;
1524 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001525 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001526 break;
1527 case OP_APUT_SHORT:
1528 case OP_APUT_CHAR:
1529 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1530 rlSrc[0], 1);
1531 break;
1532 case OP_APUT_BYTE:
1533 case OP_APUT_BOOLEAN:
1534 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1535 rlSrc[0], 0);
1536 break;
1537
1538 case OP_IGET_WIDE:
1539 case OP_IGET_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001540 genIGetWide(cUnit, mir, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001541 break;
1542
1543 case OP_IGET:
1544 case OP_IGET_VOLATILE:
1545 case OP_IGET_OBJECT:
1546 case OP_IGET_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001547 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001548 break;
1549
1550 case OP_IGET_BOOLEAN:
1551 case OP_IGET_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001552 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001553 break;
1554
1555 case OP_IGET_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001556 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001557 break;
1558
1559 case OP_IGET_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001560 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001561 break;
1562
1563 case OP_IPUT_WIDE:
1564 case OP_IPUT_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001565 genIPutWide(cUnit, mir, rlSrc[0], rlSrc[1]);
buzbee67bf8852011-08-17 17:51:35 -07001566 break;
1567
1568 case OP_IPUT_OBJECT:
1569 case OP_IPUT_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001570 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
buzbee67bf8852011-08-17 17:51:35 -07001571 break;
1572
1573 case OP_IPUT:
1574 case OP_IPUT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001575 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001576 break;
1577
1578 case OP_IPUT_BOOLEAN:
1579 case OP_IPUT_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001580 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001581 break;
1582
1583 case OP_IPUT_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001584 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001585 break;
1586
1587 case OP_IPUT_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001588 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001589 break;
1590
1591 case OP_SGET:
1592 case OP_SGET_OBJECT:
1593 case OP_SGET_BOOLEAN:
1594 case OP_SGET_BYTE:
1595 case OP_SGET_CHAR:
1596 case OP_SGET_SHORT:
1597 genSget(cUnit, mir, rlResult, rlDest);
1598 break;
1599
1600 case OP_SGET_WIDE:
1601 genSgetWide(cUnit, mir, rlResult, rlDest);
1602 break;
1603
1604 case OP_SPUT:
1605 case OP_SPUT_OBJECT:
1606 case OP_SPUT_BOOLEAN:
1607 case OP_SPUT_BYTE:
1608 case OP_SPUT_CHAR:
1609 case OP_SPUT_SHORT:
1610 genSput(cUnit, mir, rlSrc[0]);
1611 break;
1612
1613 case OP_SPUT_WIDE:
1614 genSputWide(cUnit, mir, rlSrc[0]);
1615 break;
1616
1617 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001618 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1619 true /*range*/);
1620 break;
buzbee67bf8852011-08-17 17:51:35 -07001621 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001622 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1623 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001624 break;
1625
1626 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001627 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1628 false /*range*/);
1629 break;
buzbee67bf8852011-08-17 17:51:35 -07001630 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001631 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1632 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001633 break;
1634
1635 case OP_INVOKE_VIRTUAL:
1636 case OP_INVOKE_VIRTUAL_RANGE:
1637 genInvokeVirtual(cUnit, mir);
1638 break;
1639
1640 case OP_INVOKE_SUPER:
1641 case OP_INVOKE_SUPER_RANGE:
1642 genInvokeSuper(cUnit, mir);
1643 break;
1644
1645 case OP_INVOKE_INTERFACE:
1646 case OP_INVOKE_INTERFACE_RANGE:
1647 genInvokeInterface(cUnit, mir);
1648 break;
1649
1650 case OP_NEG_INT:
1651 case OP_NOT_INT:
1652 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1653 break;
1654
1655 case OP_NEG_LONG:
1656 case OP_NOT_LONG:
1657 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1658 break;
1659
1660 case OP_NEG_FLOAT:
1661 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1662 break;
1663
1664 case OP_NEG_DOUBLE:
1665 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1666 break;
1667
1668 case OP_INT_TO_LONG:
1669 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1670 if (rlSrc[0].location == kLocPhysReg) {
1671 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1672 } else {
1673 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1674 }
1675 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1676 rlResult.lowReg, 31);
1677 storeValueWide(cUnit, rlDest, rlResult);
1678 break;
1679
1680 case OP_LONG_TO_INT:
1681 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1682 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1683 storeValue(cUnit, rlDest, rlSrc[0]);
1684 break;
1685
1686 case OP_INT_TO_BYTE:
1687 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1688 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1689 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1690 storeValue(cUnit, rlDest, rlResult);
1691 break;
1692
1693 case OP_INT_TO_SHORT:
1694 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1695 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1696 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1697 storeValue(cUnit, rlDest, rlResult);
1698 break;
1699
1700 case OP_INT_TO_CHAR:
1701 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1702 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1703 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1704 storeValue(cUnit, rlDest, rlResult);
1705 break;
1706
1707 case OP_INT_TO_FLOAT:
1708 case OP_INT_TO_DOUBLE:
1709 case OP_LONG_TO_FLOAT:
1710 case OP_LONG_TO_DOUBLE:
1711 case OP_FLOAT_TO_INT:
1712 case OP_FLOAT_TO_LONG:
1713 case OP_FLOAT_TO_DOUBLE:
1714 case OP_DOUBLE_TO_INT:
1715 case OP_DOUBLE_TO_LONG:
1716 case OP_DOUBLE_TO_FLOAT:
1717 genConversion(cUnit, mir);
1718 break;
1719
1720 case OP_ADD_INT:
1721 case OP_SUB_INT:
1722 case OP_MUL_INT:
1723 case OP_DIV_INT:
1724 case OP_REM_INT:
1725 case OP_AND_INT:
1726 case OP_OR_INT:
1727 case OP_XOR_INT:
1728 case OP_SHL_INT:
1729 case OP_SHR_INT:
1730 case OP_USHR_INT:
1731 case OP_ADD_INT_2ADDR:
1732 case OP_SUB_INT_2ADDR:
1733 case OP_MUL_INT_2ADDR:
1734 case OP_DIV_INT_2ADDR:
1735 case OP_REM_INT_2ADDR:
1736 case OP_AND_INT_2ADDR:
1737 case OP_OR_INT_2ADDR:
1738 case OP_XOR_INT_2ADDR:
1739 case OP_SHL_INT_2ADDR:
1740 case OP_SHR_INT_2ADDR:
1741 case OP_USHR_INT_2ADDR:
1742 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1743 break;
1744
1745 case OP_ADD_LONG:
1746 case OP_SUB_LONG:
1747 case OP_MUL_LONG:
1748 case OP_DIV_LONG:
1749 case OP_REM_LONG:
1750 case OP_AND_LONG:
1751 case OP_OR_LONG:
1752 case OP_XOR_LONG:
1753 case OP_ADD_LONG_2ADDR:
1754 case OP_SUB_LONG_2ADDR:
1755 case OP_MUL_LONG_2ADDR:
1756 case OP_DIV_LONG_2ADDR:
1757 case OP_REM_LONG_2ADDR:
1758 case OP_AND_LONG_2ADDR:
1759 case OP_OR_LONG_2ADDR:
1760 case OP_XOR_LONG_2ADDR:
1761 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1762 break;
1763
buzbee67bf8852011-08-17 17:51:35 -07001764 case OP_SHL_LONG:
1765 case OP_SHR_LONG:
1766 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001767 case OP_SHL_LONG_2ADDR:
1768 case OP_SHR_LONG_2ADDR:
1769 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001770 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1771 break;
1772
1773 case OP_ADD_FLOAT:
1774 case OP_SUB_FLOAT:
1775 case OP_MUL_FLOAT:
1776 case OP_DIV_FLOAT:
1777 case OP_REM_FLOAT:
1778 case OP_ADD_FLOAT_2ADDR:
1779 case OP_SUB_FLOAT_2ADDR:
1780 case OP_MUL_FLOAT_2ADDR:
1781 case OP_DIV_FLOAT_2ADDR:
1782 case OP_REM_FLOAT_2ADDR:
1783 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1784 break;
1785
1786 case OP_ADD_DOUBLE:
1787 case OP_SUB_DOUBLE:
1788 case OP_MUL_DOUBLE:
1789 case OP_DIV_DOUBLE:
1790 case OP_REM_DOUBLE:
1791 case OP_ADD_DOUBLE_2ADDR:
1792 case OP_SUB_DOUBLE_2ADDR:
1793 case OP_MUL_DOUBLE_2ADDR:
1794 case OP_DIV_DOUBLE_2ADDR:
1795 case OP_REM_DOUBLE_2ADDR:
1796 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1797 break;
1798
1799 case OP_RSUB_INT:
1800 case OP_ADD_INT_LIT16:
1801 case OP_MUL_INT_LIT16:
1802 case OP_DIV_INT_LIT16:
1803 case OP_REM_INT_LIT16:
1804 case OP_AND_INT_LIT16:
1805 case OP_OR_INT_LIT16:
1806 case OP_XOR_INT_LIT16:
1807 case OP_ADD_INT_LIT8:
1808 case OP_RSUB_INT_LIT8:
1809 case OP_MUL_INT_LIT8:
1810 case OP_DIV_INT_LIT8:
1811 case OP_REM_INT_LIT8:
1812 case OP_AND_INT_LIT8:
1813 case OP_OR_INT_LIT8:
1814 case OP_XOR_INT_LIT8:
1815 case OP_SHL_INT_LIT8:
1816 case OP_SHR_INT_LIT8:
1817 case OP_USHR_INT_LIT8:
1818 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1819 break;
1820
1821 default:
1822 res = true;
1823 }
1824 return res;
1825}
1826
Elliott Hughesc1f143d2011-12-01 17:31:10 -08001827STATIC const char* extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001828 "kMirOpPhi",
1829 "kMirOpNullNRangeUpCheck",
1830 "kMirOpNullNRangeDownCheck",
1831 "kMirOpLowerBound",
1832 "kMirOpPunt",
1833 "kMirOpCheckInlinePrediction",
1834};
1835
1836/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001837STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001838{
1839 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1840 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1841 strcpy(msg, extendedMIROpNames[opOffset]);
1842 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1843
1844 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1845 case kMirOpPhi: {
1846 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1847 op->flags.isNop = true;
1848 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1849 break;
1850 }
1851 default:
1852 break;
1853 }
1854}
1855
buzbee67bc2362011-10-11 18:08:40 -07001856/*
1857 * If there are any ins passed in registers that have not been promoted
1858 * to a callee-save register, flush them to the frame. Perform intial
1859 * assignment of promoted arguments.
1860 */
buzbeeed3e9302011-09-23 17:34:19 -07001861STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001862{
Ian Rogersa3760aa2011-11-14 14:32:37 -08001863 if (cUnit->numIns == 0)
buzbee67bf8852011-08-17 17:51:35 -07001864 return;
buzbee67bc2362011-10-11 18:08:40 -07001865 int firstArgReg = r1;
1866 int lastArgReg = r3;
Ian Rogersa3760aa2011-11-14 14:32:37 -08001867 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
buzbee8febc582011-10-25 12:39:20 -07001868 /*
1869 * Arguments passed in registers should be flushed
1870 * to their backing locations in the frame for now.
1871 * Also, we need to do initial assignment for promoted
1872 * arguments. NOTE: an older version of dx had an issue
1873 * in which it would reuse static method argument registers.
1874 * This could result in the same Dalvik virtual register
1875 * being promoted to both core and fp regs. In those
1876 * cases, copy argument to both. This will be uncommon
1877 * enough that it isn't worth attempting to optimize.
1878 */
Ian Rogersa3760aa2011-11-14 14:32:37 -08001879 for (int i = 0; i < cUnit->numIns; i++) {
buzbee67bc2362011-10-11 18:08:40 -07001880 PromotionMap vMap = cUnit->promotionMap[startVReg + i];
buzbee67bc2362011-10-11 18:08:40 -07001881 if (i <= (lastArgReg - firstArgReg)) {
buzbee8febc582011-10-25 12:39:20 -07001882 // If arriving in register
buzbee67bc2362011-10-11 18:08:40 -07001883 if (vMap.coreLocation == kLocPhysReg) {
1884 genRegCopy(cUnit, vMap.coreReg, firstArgReg + i);
buzbee8febc582011-10-25 12:39:20 -07001885 }
1886 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001887 genRegCopy(cUnit, vMap.fpReg, firstArgReg + i);
1888 }
1889 // Also put a copy in memory in case we're partially promoted
1890 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1891 firstArgReg + i, kWord);
1892 } else {
buzbee8febc582011-10-25 12:39:20 -07001893 // If arriving in frame & promoted
buzbee67bc2362011-10-11 18:08:40 -07001894 if (vMap.coreLocation == kLocPhysReg) {
1895 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1896 vMap.coreReg);
buzbee8febc582011-10-25 12:39:20 -07001897 }
1898 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001899 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1900 vMap.fpReg);
buzbee67bf8852011-08-17 17:51:35 -07001901 }
1902 }
buzbee67bf8852011-08-17 17:51:35 -07001903 }
1904}
1905
1906/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001907STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001908{
1909 MIR* mir;
1910 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1911 int blockId = bb->id;
1912
1913 cUnit->curBlock = bb;
1914 labelList[blockId].operands[0] = bb->startOffset;
1915
1916 /* Insert the block label */
1917 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1918 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1919
buzbee6181f792011-09-29 11:14:04 -07001920 /* Reset local optimization data on block boundaries */
1921 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001922 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001923 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001924
1925 ArmLIR* headLIR = NULL;
1926
buzbeebbaf8942011-10-02 13:08:29 -07001927 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001928 if (bb->blockType == kEntryBlock) {
1929 /*
1930 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1931 * mechanism know so it doesn't try to use any of them when
1932 * expanding the frame or flushing. This leaves the utility
1933 * code with a single temp: r12. This should be enough.
1934 */
1935 oatLockTemp(cUnit, r0);
1936 oatLockTemp(cUnit, r1);
1937 oatLockTemp(cUnit, r2);
1938 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001939
1940 /*
1941 * We can safely skip the stack overflow check if we're
1942 * a leaf *and* our frame size < fudge factor.
1943 */
1944 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1945 ((size_t)cUnit->frameSize <
1946 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001947 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001948 if (!skipOverflowCheck) {
1949 /* Load stack limit */
1950 loadWordDisp(cUnit, rSELF,
1951 art::Thread::StackEndOffset().Int32Value(), r12);
1952 }
buzbee67bf8852011-08-17 17:51:35 -07001953 /* Spill core callee saves */
1954 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1955 /* Need to spill any FP regs? */
1956 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001957 /*
1958 * NOTE: fp spills are a little different from core spills in that
1959 * they are pushed as a contiguous block. When promoting from
1960 * the fp set, we must allocate all singles from s16..highest-promoted
1961 */
buzbee67bf8852011-08-17 17:51:35 -07001962 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1963 }
buzbeecefd1872011-09-09 09:59:52 -07001964 if (!skipOverflowCheck) {
1965 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001966 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001967 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1968 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001969 genRegCopy(cUnit, rSP, rLR); // Establish stack
1970 } else {
1971 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001972 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001973 }
buzbee67bf8852011-08-17 17:51:35 -07001974 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1975 flushIns(cUnit);
1976 oatFreeTemp(cUnit, r0);
1977 oatFreeTemp(cUnit, r1);
1978 oatFreeTemp(cUnit, r2);
1979 oatFreeTemp(cUnit, r3);
1980 } else if (bb->blockType == kExitBlock) {
1981 newLIR0(cUnit, kArmPseudoMethodExit);
buzbeebbaf8942011-10-02 13:08:29 -07001982 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07001983 /* Need to restore any FP callee saves? */
1984 if (cUnit->numFPSpills) {
1985 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1986 }
1987 if (cUnit->coreSpillMask & (1 << rLR)) {
1988 /* Unspill rLR to rPC */
1989 cUnit->coreSpillMask &= ~(1 << rLR);
1990 cUnit->coreSpillMask |= (1 << rPC);
1991 }
1992 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1993 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1994 /* We didn't pop to rPC, so must do a bv rLR */
1995 newLIR1(cUnit, kThumbBx, rLR);
1996 }
1997 }
1998
1999 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
2000
2001 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07002002 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
2003 oatClobberAllRegs(cUnit);
2004 }
buzbee67bf8852011-08-17 17:51:35 -07002005
2006 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
2007 oatResetDefTracking(cUnit);
2008 }
2009
2010 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
2011 handleExtendedMethodMIR(cUnit, mir);
2012 continue;
2013 }
2014
2015 cUnit->currentDalvikOffset = mir->offset;
2016
2017 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
2018 InstructionFormat dalvikFormat =
2019 dexGetFormatFromOpcode(dalvikOpcode);
2020
2021 ArmLIR* boundaryLIR;
2022
2023 /* Mark the beginning of a Dalvik instruction for line tracking */
2024 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
2025 (int) oatGetDalvikDisassembly(
2026 &mir->dalvikInsn, ""));
2027 /* Remember the first LIR for this block */
2028 if (headLIR == NULL) {
2029 headLIR = boundaryLIR;
2030 /* Set the first boundaryLIR as a scheduling barrier */
2031 headLIR->defMask = ENCODE_ALL;
2032 }
2033
2034 /* Don't generate the SSA annotation unless verbose mode is on */
2035 if (cUnit->printMe && mir->ssaRep) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -08002036 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
buzbee67bf8852011-08-17 17:51:35 -07002037 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
2038 }
2039
2040 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
2041
2042 if (notHandled) {
2043 char buf[100];
2044 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
2045 mir->offset,
2046 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
2047 dalvikFormat);
2048 LOG(FATAL) << buf;
2049 }
2050 }
2051
2052 if (headLIR) {
2053 /*
2054 * Eliminate redundant loads/stores and delay stores into later
2055 * slots
2056 */
2057 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
2058 cUnit->lastLIRInsn);
2059
2060 /*
2061 * Generate an unconditional branch to the fallthrough block.
2062 */
2063 if (bb->fallThrough) {
2064 genUnconditionalBranch(cUnit,
2065 &labelList[bb->fallThrough->id]);
2066 }
2067 }
2068 return false;
2069}
2070
2071/*
2072 * Nop any unconditional branches that go to the next instruction.
2073 * Note: new redundant branches may be inserted later, and we'll
2074 * use a check in final instruction assembly to nop those out.
2075 */
2076void removeRedundantBranches(CompilationUnit* cUnit)
2077{
2078 ArmLIR* thisLIR;
2079
2080 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
2081 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
2082 thisLIR = NEXT_LIR(thisLIR)) {
2083
2084 /* Branch to the next instruction */
2085 if ((thisLIR->opcode == kThumbBUncond) ||
2086 (thisLIR->opcode == kThumb2BUncond)) {
2087 ArmLIR* nextLIR = thisLIR;
2088
2089 while (true) {
2090 nextLIR = NEXT_LIR(nextLIR);
2091
2092 /*
2093 * Is the branch target the next instruction?
2094 */
2095 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
2096 thisLIR->flags.isNop = true;
2097 break;
2098 }
2099
2100 /*
2101 * Found real useful stuff between the branch and the target.
2102 * Need to explicitly check the lastLIRInsn here because it
2103 * might be the last real instruction.
2104 */
2105 if (!isPseudoOpcode(nextLIR->opcode) ||
2106 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
2107 break;
2108 }
2109 }
2110 }
2111}
2112
buzbeeed3e9302011-09-23 17:34:19 -07002113STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07002114{
2115 ArmLIR** suspendLabel =
2116 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
2117 int numElems = cUnit->suspendLaunchpads.numUsed;
2118
2119 for (int i = 0; i < numElems; i++) {
2120 /* TUNING: move suspend count load into helper */
2121 ArmLIR* lab = suspendLabel[i];
2122 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
2123 cUnit->currentDalvikOffset = lab->operands[1];
2124 oatAppendLIR(cUnit, (LIR *)lab);
2125 loadWordDisp(cUnit, rSELF,
2126 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
2127 loadWordDisp(cUnit, rSELF,
2128 art::Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
2129 opReg(cUnit, kOpBlx, rLR);
2130 genUnconditionalBranch(cUnit, resumeLab);
2131 }
2132}
2133
buzbeeed3e9302011-09-23 17:34:19 -07002134STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07002135{
2136 ArmLIR** throwLabel =
2137 (ArmLIR **) cUnit->throwLaunchpads.elemList;
2138 int numElems = cUnit->throwLaunchpads.numUsed;
2139 int i;
2140
2141 for (i = 0; i < numElems; i++) {
2142 ArmLIR* lab = throwLabel[i];
2143 cUnit->currentDalvikOffset = lab->operands[1];
2144 oatAppendLIR(cUnit, (LIR *)lab);
2145 int funcOffset = 0;
2146 int v1 = lab->operands[2];
2147 int v2 = lab->operands[3];
2148 switch(lab->operands[0]) {
2149 case kArmThrowNullPointer:
2150 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2151 break;
2152 case kArmThrowArrayBounds:
2153 if (v2 != r0) {
2154 genRegCopy(cUnit, r0, v1);
2155 genRegCopy(cUnit, r1, v2);
2156 } else {
2157 if (v1 == r1) {
2158 genRegCopy(cUnit, r12, v1);
2159 genRegCopy(cUnit, r1, v2);
2160 genRegCopy(cUnit, r0, r12);
2161 } else {
2162 genRegCopy(cUnit, r1, v2);
2163 genRegCopy(cUnit, r0, v1);
2164 }
2165 }
2166 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2167 break;
2168 case kArmThrowDivZero:
2169 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2170 break;
2171 case kArmThrowVerificationError:
2172 loadConstant(cUnit, r0, v1);
2173 loadConstant(cUnit, r1, v2);
2174 funcOffset =
2175 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2176 break;
2177 case kArmThrowNegArraySize:
2178 genRegCopy(cUnit, r0, v1);
2179 funcOffset =
2180 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2181 break;
buzbee5ade1d22011-09-09 14:44:52 -07002182 case kArmThrowNoSuchMethod:
2183 genRegCopy(cUnit, r0, v1);
2184 funcOffset =
2185 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2186 break;
buzbeeec5adf32011-09-11 15:25:43 -07002187 case kArmThrowStackOverflow:
2188 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07002189 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07002190 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07002191 opRegImm(cUnit, kOpAdd, rSP,
2192 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07002193 break;
buzbee5ade1d22011-09-09 14:44:52 -07002194 default:
2195 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2196 }
2197 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07002198 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07002199 }
2200}
2201
buzbee67bf8852011-08-17 17:51:35 -07002202void oatMethodMIR2LIR(CompilationUnit* cUnit)
2203{
2204 /* Used to hold the labels of each block */
2205 cUnit->blockLabelList =
2206 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2207
2208 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2209 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07002210 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002211
2212 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07002213
2214 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002215}
2216
2217/* Common initialization routine for an architecture family */
2218bool oatArchInit()
2219{
2220 int i;
2221
2222 for (i = 0; i < kArmLast; i++) {
2223 if (EncodingMap[i].opcode != i) {
2224 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2225 " is wrong: expecting " << i << ", seeing " <<
2226 (int)EncodingMap[i].opcode;
2227 }
2228 }
2229
2230 return oatArchVariantInit();
2231}
2232
2233/* Needed by the Assembler */
2234void oatSetupResourceMasks(ArmLIR* lir)
2235{
2236 setupResourceMasks(lir);
2237}
2238
2239/* Needed by the ld/st optmizatons */
2240ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2241{
2242 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2243}
2244
2245/* Needed by the register allocator */
2246ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2247{
2248 return genRegCopy(cUnit, rDest, rSrc);
2249}
2250
2251/* Needed by the register allocator */
2252void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2253 int srcLo, int srcHi)
2254{
2255 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2256}
2257
2258void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2259 int displacement, int rSrc, OpSize size)
2260{
2261 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2262}
2263
2264void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2265 int displacement, int rSrcLo, int rSrcHi)
2266{
2267 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2268}