blob: f57094fd07f3dd3675fbd47525e875b8c826b958 [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
buzbeece302932011-10-04 14:32:18 -070017#define DISPLAY_MISSING_TARGETS (cUnit->enableDebug & \
18 (1 << kDebugDisplayMissingTargets))
Elliott Hughes1240dad2011-09-09 16:24:50 -070019
buzbee67bc2362011-10-11 18:08:40 -070020STATIC const RegLocation badLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, INVALID_REG,
21 INVALID_REG, INVALID_SREG};
buzbee6181f792011-09-29 11:14:04 -070022
23/* Mark register usage state and return long retloc */
24STATIC RegLocation getRetLocWide(CompilationUnit* cUnit)
25{
26 RegLocation res = LOC_DALVIK_RETURN_VAL_WIDE;
27 oatLockTemp(cUnit, res.lowReg);
28 oatLockTemp(cUnit, res.highReg);
29 oatMarkPair(cUnit, res.lowReg, res.highReg);
30 return res;
31}
32
33STATIC RegLocation getRetLoc(CompilationUnit* cUnit)
34{
35 RegLocation res = LOC_DALVIK_RETURN_VAL;
36 oatLockTemp(cUnit, res.lowReg);
37 return res;
38}
buzbee67bf8852011-08-17 17:51:35 -070039
buzbeedfd3d702011-08-28 12:56:51 -070040/*
41 * Let helper function take care of everything. Will call
42 * Array::AllocFromCode(type_idx, method, count);
43 * Note: AllocFromCode will handle checks for errNegativeArraySize.
44 */
buzbeeed3e9302011-09-23 17:34:19 -070045STATIC void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -070046 RegLocation rlSrc)
47{
buzbeedfd3d702011-08-28 12:56:51 -070048 oatFlushAllRegs(cUnit); /* Everything to home location */
49 loadWordDisp(cUnit, rSELF,
Elliott Hughesb408de72011-10-04 14:35:05 -070050 OFFSETOF_MEMBER(Thread, pAllocArrayFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -070051 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
52 loadConstant(cUnit, r0, mir->dalvikInsn.vC); // arg0 <- type_id
53 loadValueDirectFixed(cUnit, rlSrc, r2); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070054 callRuntimeHelper(cUnit, rLR);
buzbeedfd3d702011-08-28 12:56:51 -070055 RegLocation rlResult = oatGetReturn(cUnit);
56 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -070057}
58
59/*
60 * Similar to genNewArray, but with post-allocation initialization.
61 * Verifier guarantees we're dealing with an array class. Current
62 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
63 * Current code also throws internal unimp if not 'L', '[' or 'I'.
64 */
buzbeeed3e9302011-09-23 17:34:19 -070065STATIC void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
buzbee67bf8852011-08-17 17:51:35 -070066{
67 DecodedInstruction* dInsn = &mir->dalvikInsn;
buzbee81eccc02011-09-17 13:42:21 -070068 int elems = dInsn->vA;
69 int typeId = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070070 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbeedfd3d702011-08-28 12:56:51 -070071 loadWordDisp(cUnit, rSELF,
Elliott Hughesb408de72011-10-04 14:35:05 -070072 OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -070073 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
74 loadConstant(cUnit, r0, typeId); // arg0 <- type_id
75 loadConstant(cUnit, r2, elems); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070076 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -070077 /*
buzbeedfd3d702011-08-28 12:56:51 -070078 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
79 * return region. Because AllocFromCode placed the new array
80 * in r0, we'll just lock it into place. When debugger support is
81 * added, it may be necessary to additionally copy all return
82 * values to a home location in thread-local storage
buzbee67bf8852011-08-17 17:51:35 -070083 */
buzbee67bf8852011-08-17 17:51:35 -070084 oatLockTemp(cUnit, r0);
buzbeedfd3d702011-08-28 12:56:51 -070085
buzbee67bf8852011-08-17 17:51:35 -070086 // Having a range of 0 is legal
87 if (isRange && (dInsn->vA > 0)) {
88 /*
89 * Bit of ugliness here. We're going generate a mem copy loop
90 * on the register range, but it is possible that some regs
91 * in the range have been promoted. This is unlikely, but
92 * before generating the copy, we'll just force a flush
93 * of any regs in the source range that have been promoted to
94 * home location.
95 */
96 for (unsigned int i = 0; i < dInsn->vA; i++) {
97 RegLocation loc = oatUpdateLoc(cUnit,
98 oatGetSrc(cUnit, mir, i));
99 if (loc.location == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -0700100 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
101 loc.lowReg, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700102 }
103 }
104 /*
105 * TUNING note: generated code here could be much improved, but
106 * this is an uncommon operation and isn't especially performance
107 * critical.
108 */
109 int rSrc = oatAllocTemp(cUnit);
110 int rDst = oatAllocTemp(cUnit);
111 int rIdx = oatAllocTemp(cUnit);
112 int rVal = rLR; // Using a lot of temps, rLR is known free here
113 // Set up source pointer
114 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
buzbee67bc2362011-10-11 18:08:40 -0700115 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
116 oatSRegOffset(cUnit, rlFirst.sRegLow));
buzbee67bf8852011-08-17 17:51:35 -0700117 // Set up the target pointer
118 opRegRegImm(cUnit, kOpAdd, rDst, r0,
buzbeec143c552011-08-20 17:38:58 -0700119 Array::DataOffset().Int32Value());
buzbee67bf8852011-08-17 17:51:35 -0700120 // Set up the loop counter (known to be > 0)
buzbee31813452011-10-16 14:33:08 -0700121 loadConstant(cUnit, rIdx, dInsn->vA - 1);
buzbee67bf8852011-08-17 17:51:35 -0700122 // Generate the copy loop. Going backwards for convenience
123 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
124 target->defMask = ENCODE_ALL;
125 // Copy next element
126 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
127 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
128 // Use setflags encoding here
129 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee31813452011-10-16 14:33:08 -0700130 ArmLIR* branch = opCondBranch(cUnit, kArmCondGe);
buzbee67bf8852011-08-17 17:51:35 -0700131 branch->generic.target = (LIR*)target;
132 } else if (!isRange) {
133 // TUNING: interleave
134 for (unsigned int i = 0; i < dInsn->vA; i++) {
135 RegLocation rlArg = loadValue(cUnit,
136 oatGetSrc(cUnit, mir, i), kCoreReg);
buzbeec143c552011-08-20 17:38:58 -0700137 storeBaseDisp(cUnit, r0,
138 Array::DataOffset().Int32Value() +
buzbee67bf8852011-08-17 17:51:35 -0700139 i * 4, rlArg.lowReg, kWord);
140 // If the loadValue caused a temp to be allocated, free it
141 if (oatIsTemp(cUnit, rlArg.lowReg)) {
142 oatFreeTemp(cUnit, rlArg.lowReg);
143 }
144 }
145 }
146}
147
Brian Carlstrom845490b2011-09-19 15:56:53 -0700148Field* FindFieldWithResolvedStaticStorage(const Method* method,
149 const uint32_t fieldIdx,
150 uint32_t& resolvedTypeIdx) {
151 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
152 Field* field = class_linker->ResolveField(fieldIdx, method, true);
153 if (field == NULL) {
154 return NULL;
155 }
156 const art::DexFile& dex_file = class_linker->
157 FindDexFile(method->GetDeclaringClass()->GetDexCache());
158 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
159 int type_idx = field_id.class_idx_;
160 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
161 // Check if storage class is the same as class referred to by type idx.
162 // They may not be if the FieldId refers a subclass, but storage is in super
163 if (field->GetDeclaringClass() == klass) {
164 resolvedTypeIdx = type_idx;
165 return field;
166 }
167 // See if we can find a dex reference for the storage class.
168 // we may not if the dex file never references the super class,
169 // but usually it will.
170 std::string descriptor = field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8();
171 for (size_t type_idx = 0; type_idx < dex_file.NumTypeIds(); type_idx++) {
172 const art::DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx);
173 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
174 resolvedTypeIdx = type_idx;
175 return field;
176 }
177 }
178 return NULL; // resort to slow path
179}
180
buzbeeed3e9302011-09-23 17:34:19 -0700181STATIC void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700182{
buzbeee1931742011-08-28 21:15:53 -0700183 bool isObject = ((mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
184 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE));
buzbee1da522d2011-09-04 11:22:20 -0700185 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700186 uint32_t typeIdx;
187 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee6181f792011-09-29 11:14:04 -0700188 oatFlushAllRegs(cUnit);
Brian Carlstrom845490b2011-09-19 15:56:53 -0700189 if (SLOW_FIELD_PATH || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700190 // Slow path
Elliott Hughes81bc5092011-09-30 17:25:59 -0700191 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700192 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
193 : OFFSETOF_MEMBER(Thread, pSet32Static);
buzbeee1931742011-08-28 21:15:53 -0700194 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
195 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
196 loadCurrMethodDirect(cUnit, r1);
197 loadValueDirect(cUnit, rlSrc, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700198 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700199 } else {
buzbee1da522d2011-09-04 11:22:20 -0700200 // fast path
201 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700202 // Using fixed register to sync with slow path
203 int rMethod = r1;
204 oatLockTemp(cUnit, rMethod);
205 loadCurrMethodDirect(cUnit, rMethod);
206 int rBase = r0;
207 oatLockTemp(cUnit, rBase);
208 loadWordDisp(cUnit, rMethod,
209 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
210 rBase);
211 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
212 sizeof(int32_t*)* typeIdx, rBase);
213 // TUNING: fast path should fall through
buzbeec0ecd652011-09-25 18:11:54 -0700214 // TUNING: Try a conditional skip here, might be faster
buzbee1da522d2011-09-04 11:22:20 -0700215 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
216 loadWordDisp(cUnit, rSELF,
217 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
218 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700219 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700220 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
221 skipTarget->defMask = ENCODE_ALL;
222 branchOver->generic.target = (LIR*)skipTarget;
223 rlSrc = oatGetSrc(cUnit, mir, 0);
224 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbee12246b82011-09-29 14:15:05 -0700225#if ANDROID_SMP != 0
226 if (field->IsVolatile()) {
227 oatGenMemBarrier(cUnit, kST);
228 }
229#endif
buzbee1da522d2011-09-04 11:22:20 -0700230 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700231#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700232 if (field->IsVolatile()) {
233 oatGenMemBarrier(cUnit, kSY);
234 }
buzbee67bf8852011-08-17 17:51:35 -0700235#endif
buzbee1da522d2011-09-04 11:22:20 -0700236 if (isObject) {
237 markGCCard(cUnit, rlSrc.lowReg, rBase);
238 }
239 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700240 }
buzbee67bf8852011-08-17 17:51:35 -0700241}
242
buzbeeed3e9302011-09-23 17:34:19 -0700243STATIC void genSputWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700244{
buzbee1da522d2011-09-04 11:22:20 -0700245 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700246 uint32_t typeIdx;
247 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee6181f792011-09-29 11:14:04 -0700248 oatFlushAllRegs(cUnit);
buzbee12246b82011-09-29 14:15:05 -0700249#if ANDROID_SMP != 0
250 bool isVolatile = (field == NULL) || field->IsVolatile();
251#else
252 bool isVolatile = false;
253#endif
254 if (SLOW_FIELD_PATH || field == NULL || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700255 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700256 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pSet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700257 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
258 loadCurrMethodDirect(cUnit, r1);
259 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -0700260 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700261 } else {
buzbee1da522d2011-09-04 11:22:20 -0700262 // fast path
263 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700264 // Using fixed register to sync with slow path
265 int rMethod = r1;
266 oatLockTemp(cUnit, rMethod);
267 loadCurrMethodDirect(cUnit, r1);
268 int rBase = r0;
269 oatLockTemp(cUnit, rBase);
270 loadWordDisp(cUnit, rMethod,
271 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
272 rBase);
273 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
274 sizeof(int32_t*)* typeIdx, rBase);
275 // TUNING: fast path should fall through
276 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
277 loadWordDisp(cUnit, rSELF,
278 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
279 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700280 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700281 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
282 skipTarget->defMask = ENCODE_ALL;
283 branchOver->generic.target = (LIR*)skipTarget;
284 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
285 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
286 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
287 rlSrc.highReg);
buzbee1da522d2011-09-04 11:22:20 -0700288 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700289 }
buzbee67bf8852011-08-17 17:51:35 -0700290}
291
292
buzbeeed3e9302011-09-23 17:34:19 -0700293STATIC void genSgetWide(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700294 RegLocation rlResult, RegLocation rlDest)
295{
buzbee1da522d2011-09-04 11:22:20 -0700296 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700297 uint32_t typeIdx;
298 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee12246b82011-09-29 14:15:05 -0700299#if ANDROID_SMP != 0
300 bool isVolatile = (field == NULL) || field->IsVolatile();
301#else
302 bool isVolatile = false;
303#endif
buzbee6181f792011-09-29 11:14:04 -0700304 oatFlushAllRegs(cUnit);
buzbee12246b82011-09-29 14:15:05 -0700305 if (SLOW_FIELD_PATH || field == NULL || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700306 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700307 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pGet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700308 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
309 loadCurrMethodDirect(cUnit, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -0700310 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700311 RegLocation rlResult = oatGetReturnWide(cUnit);
312 storeValueWide(cUnit, rlDest, rlResult);
313 } else {
buzbee1da522d2011-09-04 11:22:20 -0700314 // Fast path
315 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700316 // Using fixed register to sync with slow path
317 int rMethod = r1;
318 oatLockTemp(cUnit, rMethod);
319 loadCurrMethodDirect(cUnit, rMethod);
320 int rBase = r0;
321 oatLockTemp(cUnit, rBase);
322 loadWordDisp(cUnit, rMethod,
323 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
324 rBase);
325 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
326 sizeof(int32_t*)* typeIdx, rBase);
327 // TUNING: fast path should fall through
328 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
329 loadWordDisp(cUnit, rSELF,
330 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
331 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700332 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700333 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
334 skipTarget->defMask = ENCODE_ALL;
335 branchOver->generic.target = (LIR*)skipTarget;
336 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
337 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee1da522d2011-09-04 11:22:20 -0700338 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
339 rlResult.highReg, INVALID_SREG);
340 oatFreeTemp(cUnit, rBase);
341 storeValueWide(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700342 }
buzbee67bf8852011-08-17 17:51:35 -0700343}
344
buzbeeed3e9302011-09-23 17:34:19 -0700345STATIC void genSget(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700346 RegLocation rlResult, RegLocation rlDest)
347{
buzbee1da522d2011-09-04 11:22:20 -0700348 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700349 uint32_t typeIdx;
350 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbeee1931742011-08-28 21:15:53 -0700351 bool isObject = ((mir->dalvikInsn.opcode == OP_SGET_OBJECT) ||
352 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE));
buzbee6181f792011-09-29 11:14:04 -0700353 oatFlushAllRegs(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -0700354 if (SLOW_FIELD_PATH || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700355 // Slow path
Elliott Hughes81bc5092011-09-30 17:25:59 -0700356 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700357 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
358 : OFFSETOF_MEMBER(Thread, pGet32Static);
buzbeee1931742011-08-28 21:15:53 -0700359 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
360 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
361 loadCurrMethodDirect(cUnit, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -0700362 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700363 RegLocation rlResult = oatGetReturn(cUnit);
364 storeValue(cUnit, rlDest, rlResult);
365 } else {
buzbee1da522d2011-09-04 11:22:20 -0700366 // Fast path
367 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700368 // Using fixed register to sync with slow path
369 int rMethod = r1;
370 oatLockTemp(cUnit, rMethod);
371 loadCurrMethodDirect(cUnit, rMethod);
372 int rBase = r0;
373 oatLockTemp(cUnit, rBase);
374 loadWordDisp(cUnit, rMethod,
375 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
376 rBase);
377 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
378 sizeof(int32_t*)* typeIdx, rBase);
379 // TUNING: fast path should fall through
380 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
381 loadWordDisp(cUnit, rSELF,
382 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
383 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700384 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700385 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
386 skipTarget->defMask = ENCODE_ALL;
387 branchOver->generic.target = (LIR*)skipTarget;
388 rlDest = oatGetDest(cUnit, mir, 0);
389 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700390#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700391 if (field->IsVolatile()) {
buzbee1da522d2011-09-04 11:22:20 -0700392 oatGenMemBarrier(cUnit, kSY);
393 }
buzbee67bf8852011-08-17 17:51:35 -0700394#endif
buzbee1da522d2011-09-04 11:22:20 -0700395 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
396 oatFreeTemp(cUnit, rBase);
397 storeValue(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700398 }
buzbee67bf8852011-08-17 17:51:35 -0700399}
400
buzbee561227c2011-09-02 15:28:19 -0700401typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int,
402 ArmLIR*);
buzbee67bf8852011-08-17 17:51:35 -0700403
404/*
405 * Bit of a hack here - in leiu of a real scheduling pass,
406 * emit the next instruction in static & direct invoke sequences.
407 */
buzbeeed3e9302011-09-23 17:34:19 -0700408STATIC int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700409 DecodedInstruction* dInsn, int state,
410 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700411{
buzbee561227c2011-09-02 15:28:19 -0700412 DCHECK(rollback == NULL);
413 uint32_t idx = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -0700414 switch(state) {
415 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700416 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700417 break;
buzbee561227c2011-09-02 15:28:19 -0700418 case 1: // Get method->code_and_direct_methods_
419 loadWordDisp(cUnit, r0,
420 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
421 r0);
buzbee67bf8852011-08-17 17:51:35 -0700422 break;
buzbee561227c2011-09-02 15:28:19 -0700423 case 2: // Grab target method* and target code_
424 loadWordDisp(cUnit, r0,
425 art::CodeAndDirectMethods::CodeOffsetInBytes(idx), rLR);
426 loadWordDisp(cUnit, r0,
427 art::CodeAndDirectMethods::MethodOffsetInBytes(idx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700428 break;
429 default:
430 return -1;
431 }
432 return state + 1;
433}
434
buzbee67bf8852011-08-17 17:51:35 -0700435/*
436 * Bit of a hack here - in leiu of a real scheduling pass,
437 * emit the next instruction in a virtual invoke sequence.
438 * We can use rLR as a temp prior to target address loading
439 * Note also that we'll load the first argument ("this") into
440 * r1 here rather than the standard loadArgRegs.
441 */
buzbeeed3e9302011-09-23 17:34:19 -0700442STATIC int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700443 DecodedInstruction* dInsn, int state,
444 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700445{
buzbee561227c2011-09-02 15:28:19 -0700446 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700447 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700448 /*
449 * This is the fast path in which the target virtual method is
450 * fully resolved at compile time.
451 */
Brian Carlstrom845490b2011-09-19 15:56:53 -0700452 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
453 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -0700454 CHECK(baseMethod != NULL);
455 uint32_t target_idx = baseMethod->GetMethodIndex();
buzbee67bf8852011-08-17 17:51:35 -0700456 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700457 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700458 rlArg = oatGetSrc(cUnit, mir, 0);
459 loadValueDirectFixed(cUnit, rlArg, r1);
460 break;
buzbee561227c2011-09-02 15:28:19 -0700461 case 1: // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700462 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee561227c2011-09-02 15:28:19 -0700463 // get this->klass_ [use r1, set rLR]
464 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700465 break;
buzbee561227c2011-09-02 15:28:19 -0700466 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
467 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700468 break;
buzbee561227c2011-09-02 15:28:19 -0700469 case 3: // Get target method [use rLR, set r0]
470 loadWordDisp(cUnit, rLR, (target_idx * 4) +
471 art::Array::DataOffset().Int32Value(), r0);
472 break;
473 case 4: // Get the target compiled code address [uses r0, sets rLR]
474 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700475 break;
476 default:
477 return -1;
478 }
479 return state + 1;
480}
481
buzbeeed3e9302011-09-23 17:34:19 -0700482STATIC int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700483 DecodedInstruction* dInsn, int state,
484 ArmLIR* rollback)
buzbee7b1b86d2011-08-26 18:59:10 -0700485{
buzbeeed3e9302011-09-23 17:34:19 -0700486 DCHECK(rollback == NULL);
buzbee7b1b86d2011-08-26 18:59:10 -0700487 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700488 ArmLIR* skipBranch;
489 ArmLIR* skipTarget;
490 /*
491 * This handles the case in which the base method is not fully
492 * resolved at compile time. We must generate code to test
493 * for resolution a run time, bail to the slow path if not to
494 * fill in all the tables. In the latter case, we'll restart at
495 * at the beginning of the sequence.
496 */
buzbee7b1b86d2011-08-26 18:59:10 -0700497 switch(state) {
498 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700499 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700500 break;
buzbee561227c2011-09-02 15:28:19 -0700501 case 1: // Get method->dex_cache_resolved_methods_
502 loadWordDisp(cUnit, r0,
503 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700504 break;
buzbee561227c2011-09-02 15:28:19 -0700505 case 2: // method->dex_cache_resolved_methods_->Get(method_idx)
506 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
507 art::Array::DataOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700508 break;
buzbee561227c2011-09-02 15:28:19 -0700509 case 3: // Resolved?
510 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
511 // Slowest path, bail to helper, rollback and retry
512 loadWordDisp(cUnit, rSELF,
513 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
514 loadConstant(cUnit, r1, dInsn->vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700515 callRuntimeHelper(cUnit, rLR);
buzbee561227c2011-09-02 15:28:19 -0700516 genUnconditionalBranch(cUnit, rollback);
517 // Resume normal slow path
518 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
519 skipTarget->defMask = ENCODE_ALL;
520 skipBranch->generic.target = (LIR*)skipTarget;
buzbee4a3164f2011-09-03 11:25:10 -0700521 // Get base_method->method_index [usr rLR, set r0]
buzbee561227c2011-09-02 15:28:19 -0700522 loadBaseDisp(cUnit, mir, rLR,
523 Method::GetMethodIndexOffset().Int32Value(), r0,
524 kUnsignedHalf, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700525 // Load "this" [set r1]
526 rlArg = oatGetSrc(cUnit, mir, 0);
527 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee7b1b86d2011-08-26 18:59:10 -0700528 break;
529 case 4:
530 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700531 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee7b1b86d2011-08-26 18:59:10 -0700532 // get this->clazz [use r1, set rLR]
buzbee561227c2011-09-02 15:28:19 -0700533 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700534 break;
buzbee561227c2011-09-02 15:28:19 -0700535 case 5:
536 // get this->klass_->vtable_ [usr rLR, set rLR]
537 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbeeed3e9302011-09-23 17:34:19 -0700538 DCHECK_EQ((art::Array::DataOffset().Int32Value() & 0x3), 0);
buzbee561227c2011-09-02 15:28:19 -0700539 // In load shadow fold vtable_ object header size into method_index_
540 opRegImm(cUnit, kOpAdd, r0,
541 art::Array::DataOffset().Int32Value() / 4);
542 // Get target Method*
543 loadBaseIndexed(cUnit, rLR, r0, r0, 2, kWord);
544 break;
545 case 6: // Get the target compiled code address [uses r0, sets rLR]
546 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700547 break;
548 default:
549 return -1;
550 }
551 return state + 1;
552}
553
buzbeeed3e9302011-09-23 17:34:19 -0700554STATIC int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
buzbeec0ecd652011-09-25 18:11:54 -0700555 DecodedInstruction* dInsn, int callState,
556 NextCallInsn nextCallInsn, ArmLIR* rollback,
557 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700558{
buzbeec0ecd652011-09-25 18:11:54 -0700559 int nextReg = r1;
560 int nextArg = 0;
561 if (skipThis) {
562 nextReg++;
563 nextArg++;
564 }
565 for (; (nextReg <= r3) && (nextArg < mir->ssaRep->numUses); nextReg++) {
566 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
567 rlArg = oatUpdateRawLoc(cUnit, rlArg);
568 if (rlArg.wide && (nextReg <= r2)) {
569 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
570 nextReg++;
571 nextArg++;
572 } else {
buzbee1b4c8592011-08-31 10:43:51 -0700573 rlArg.wide = false;
buzbeec0ecd652011-09-25 18:11:54 -0700574 loadValueDirectFixed(cUnit, rlArg, nextReg);
buzbee67bf8852011-08-17 17:51:35 -0700575 }
buzbeec0ecd652011-09-25 18:11:54 -0700576 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700577 }
578 return callState;
579}
580
buzbee4a3164f2011-09-03 11:25:10 -0700581// Interleave launch code for INVOKE_INTERFACE.
buzbeeed3e9302011-09-23 17:34:19 -0700582STATIC int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700583 DecodedInstruction* dInsn, int state,
584 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700585{
buzbee67bf8852011-08-17 17:51:35 -0700586 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700587 case 0: // Load trampoline target
588 loadWordDisp(cUnit, rSELF,
589 OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline),
590 rLR);
591 // Load r0 with method index
592 loadConstant(cUnit, r0, dInsn->vB);
buzbee67bf8852011-08-17 17:51:35 -0700593 break;
buzbee67bf8852011-08-17 17:51:35 -0700594 default:
595 return -1;
596 }
597 return state + 1;
598}
599
buzbee67bf8852011-08-17 17:51:35 -0700600/*
601 * Interleave launch code for INVOKE_SUPER. See comments
602 * for nextVCallIns.
603 */
buzbeeed3e9302011-09-23 17:34:19 -0700604STATIC int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700605 DecodedInstruction* dInsn, int state,
606 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700607{
buzbee4a3164f2011-09-03 11:25:10 -0700608 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700609 RegLocation rlArg;
buzbee4a3164f2011-09-03 11:25:10 -0700610 /*
611 * This is the fast path in which the target virtual method is
612 * fully resolved at compile time. Note also that this path assumes
613 * that the check to verify that the target method index falls
614 * within the size of the super's vtable has been done at compile-time.
615 */
Brian Carlstrom845490b2011-09-19 15:56:53 -0700616 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
617 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -0700618 CHECK(baseMethod != NULL);
619 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
620 CHECK(superClass != NULL);
621 int32_t target_idx = baseMethod->GetMethodIndex();
622 CHECK(superClass->GetVTable()->GetLength() > target_idx);
623 Method* targetMethod = superClass->GetVTable()->Get(target_idx);
624 CHECK(targetMethod != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700625 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700626 case 0: // Get current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700627 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700628 // Load "this" [set r1]
629 rlArg = oatGetSrc(cUnit, mir, 0);
630 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee4a3164f2011-09-03 11:25:10 -0700631 // Get method->declaring_class_ [use r0, set rLR]
632 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
633 rLR);
buzbee67bf8852011-08-17 17:51:35 -0700634 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700635 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee4a3164f2011-09-03 11:25:10 -0700636 break;
637 case 1: // Get method->declaring_class_->super_class [usr rLR, set rLR]
638 loadWordDisp(cUnit, rLR, Class::SuperClassOffset().Int32Value(),
639 rLR);
640 break;
641 case 2: // Get ...->super_class_->vtable [u/s rLR]
642 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
643 break;
644 case 3: // Get target method [use rLR, set r0]
645 loadWordDisp(cUnit, rLR, (target_idx * 4) +
646 art::Array::DataOffset().Int32Value(), r0);
647 break;
648 case 4: // Get the target compiled code address [uses r0, sets rLR]
649 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
650 break;
buzbee67bf8852011-08-17 17:51:35 -0700651 default:
652 return -1;
653 }
buzbee4a3164f2011-09-03 11:25:10 -0700654 return state + 1;
655}
656
657/* Slow-path version of nextSuperCallInsn */
buzbeeed3e9302011-09-23 17:34:19 -0700658STATIC int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee4a3164f2011-09-03 11:25:10 -0700659 DecodedInstruction* dInsn, int state,
660 ArmLIR* rollback)
661{
buzbeeed3e9302011-09-23 17:34:19 -0700662 DCHECK(rollback == NULL);
buzbee4a3164f2011-09-03 11:25:10 -0700663 RegLocation rlArg;
664 ArmLIR* skipBranch;
665 ArmLIR* skipTarget;
666 int tReg;
667 /*
668 * This handles the case in which the base method is not fully
669 * resolved at compile time. We must generate code to test
670 * for resolution a run time, bail to the slow path if not to
671 * fill in all the tables. In the latter case, we'll restart at
672 * at the beginning of the sequence.
673 */
674 switch(state) {
675 case 0: // Get the current Method* [sets r0]
676 loadCurrMethodDirect(cUnit, r0);
677 break;
678 case 1: // Get method->dex_cache_resolved_methods_ [usr r0, set rLR]
679 loadWordDisp(cUnit, r0,
680 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
681 break;
682 case 2: // method->dex_cache_resolved_methods_->Get(meth_idx) [u/s rLR]
683 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
684 art::Array::DataOffset().Int32Value(), rLR);
685 break;
686 case 3: // Resolved?
687 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
688 // Slowest path, bail to helper, rollback and retry
689 loadWordDisp(cUnit, rSELF,
690 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
691 loadConstant(cUnit, r1, dInsn->vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700692 callRuntimeHelper(cUnit, rLR);
buzbee4a3164f2011-09-03 11:25:10 -0700693 genUnconditionalBranch(cUnit, rollback);
694 // Resume normal slow path
695 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
696 skipTarget->defMask = ENCODE_ALL;
697 skipBranch->generic.target = (LIR*)skipTarget;
698 // Get base_method->method_index [usr rLR, set rLR]
699 loadBaseDisp(cUnit, mir, rLR,
700 Method::GetMethodIndexOffset().Int32Value(), rLR,
701 kUnsignedHalf, INVALID_SREG);
702 // Load "this" [set r1]
703 rlArg = oatGetSrc(cUnit, mir, 0);
704 loadValueDirectFixed(cUnit, rlArg, r1);
705 // Load curMethod->declaring_class_ [uses r0, sets r0]
706 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
707 r0);
buzbee6a0f7f52011-09-05 16:14:20 -0700708 // Null this?
buzbee5ade1d22011-09-09 14:44:52 -0700709 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee6a0f7f52011-09-05 16:14:20 -0700710 // Get method->declaring_class_->super_class [usr r0, set r0]
buzbee4a3164f2011-09-03 11:25:10 -0700711 loadWordDisp(cUnit, r0, Class::SuperClassOffset().Int32Value(), r0);
712 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700713 case 4: // Get ...->super_class_->vtable [u/s r0]
buzbee4a3164f2011-09-03 11:25:10 -0700714 loadWordDisp(cUnit, r0, Class::VTableOffset().Int32Value(), r0);
buzbee43a36422011-09-14 14:00:13 -0700715 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee4a3164f2011-09-03 11:25:10 -0700716 // Range check, throw NSM on failure
717 tReg = oatAllocTemp(cUnit);
718 loadWordDisp(cUnit, r0, art::Array::LengthOffset().Int32Value(),
719 tReg);
buzbeeec5adf32011-09-11 15:25:43 -0700720 genRegRegCheck(cUnit, kArmCondCs, tReg, rLR, mir,
721 kArmThrowNoSuchMethod);
buzbee4a3164f2011-09-03 11:25:10 -0700722 oatFreeTemp(cUnit, tReg);
723 }
buzbee6a0f7f52011-09-05 16:14:20 -0700724 // Adjust vtable_ base past object header
725 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
buzbee4a3164f2011-09-03 11:25:10 -0700726 // Get target Method*
buzbee6a0f7f52011-09-05 16:14:20 -0700727 loadBaseIndexed(cUnit, r0, rLR, r0, 2, kWord);
buzbee4a3164f2011-09-03 11:25:10 -0700728 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700729 case 5: // Get the target compiled code address [uses r0, sets rLR]
buzbee4a3164f2011-09-03 11:25:10 -0700730 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
731 break;
732 default:
733 return -1;
734 }
buzbee67bf8852011-08-17 17:51:35 -0700735 return state + 1;
736}
737
738/*
739 * Load up to 5 arguments, the first three of which will be in
740 * r1 .. r3. On entry r0 contains the current method pointer,
741 * and as part of the load sequence, it must be replaced with
742 * the target method pointer. Note, this may also be called
743 * for "range" variants if the number of arguments is 5 or fewer.
744 */
buzbeeed3e9302011-09-23 17:34:19 -0700745STATIC int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700746 DecodedInstruction* dInsn, int callState,
747 ArmLIR** pcrLabel, bool isRange,
buzbee1da522d2011-09-04 11:22:20 -0700748 NextCallInsn nextCallInsn, ArmLIR* rollback,
749 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700750{
751 RegLocation rlArg;
buzbee67bf8852011-08-17 17:51:35 -0700752
753 /* If no arguments, just return */
754 if (dInsn->vA == 0)
755 return callState;
756
buzbee561227c2011-09-02 15:28:19 -0700757 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700758
buzbeec0ecd652011-09-25 18:11:54 -0700759 DCHECK_LE(dInsn->vA, 5U);
760 if (dInsn->vA > 3) {
761 uint32_t nextUse = 3;
762 //Detect special case of wide arg spanning arg3/arg4
763 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
764 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
765 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
766 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
767 rlUse2.wide) {
768 int reg;
769 // Wide spans, we need the 2nd half of uses[2].
770 rlArg = oatUpdateLocWide(cUnit, rlUse2);
771 if (rlArg.location == kLocPhysReg) {
772 reg = rlArg.highReg;
773 } else {
774 // r2 & r3 can safely be used here
775 reg = r3;
buzbee67bc2362011-10-11 18:08:40 -0700776 loadWordDisp(cUnit, rSP,
777 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbeec0ecd652011-09-25 18:11:54 -0700778 callState = nextCallInsn(cUnit, mir, dInsn, callState,
779 rollback);
780 }
781 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
782 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
783 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
784 nextUse++;
785 }
786 // Loop through the rest
787 while (nextUse < dInsn->vA) {
788 int lowReg;
789 int highReg;
790 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
791 rlArg = oatUpdateRawLoc(cUnit, rlArg);
792 if (rlArg.location == kLocPhysReg) {
793 lowReg = rlArg.lowReg;
794 highReg = rlArg.highReg;
795 } else {
796 lowReg = r2;
797 highReg = r3;
798 if (rlArg.wide) {
799 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
800 } else {
801 loadValueDirectFixed(cUnit, rlArg, lowReg);
802 }
803 callState = nextCallInsn(cUnit, mir, dInsn, callState,
804 rollback);
805 }
806 int outsOffset = (nextUse + 1) * 4;
807 if (rlArg.wide) {
808 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
809 nextUse += 2;
810 } else {
811 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
812 nextUse++;
813 }
buzbee561227c2011-09-02 15:28:19 -0700814 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700815 }
buzbee67bf8852011-08-17 17:51:35 -0700816 }
817
buzbeec0ecd652011-09-25 18:11:54 -0700818 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
819 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700820
buzbee67bf8852011-08-17 17:51:35 -0700821 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700822 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700823 }
824 return callState;
825}
826
827/*
828 * May have 0+ arguments (also used for jumbo). Note that
829 * source virtual registers may be in physical registers, so may
830 * need to be flushed to home location before copying. This
831 * applies to arg3 and above (see below).
832 *
833 * Two general strategies:
834 * If < 20 arguments
835 * Pass args 3-18 using vldm/vstm block copy
836 * Pass arg0, arg1 & arg2 in r1-r3
837 * If 20+ arguments
838 * Pass args arg19+ using memcpy block copy
839 * Pass arg0, arg1 & arg2 in r1-r3
840 *
841 */
buzbeeed3e9302011-09-23 17:34:19 -0700842STATIC int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700843 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700844 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700845 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700846{
847 int firstArg = dInsn->vC;
848 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700849
buzbee67bf8852011-08-17 17:51:35 -0700850 // If we can treat it as non-range (Jumbo ops will use range form)
851 if (numArgs <= 5)
852 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700853 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700854 /*
855 * Make sure range list doesn't span the break between in normal
856 * Dalvik vRegs and the ins.
857 */
buzbee1b4c8592011-08-31 10:43:51 -0700858 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700859 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700860 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
861 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700862 }
863
864 /*
865 * First load the non-register arguments. Both forms expect all
866 * of the source arguments to be in their home frame location, so
867 * scan the sReg names and flush any that have been promoted to
868 * frame backing storage.
869 */
870 // Scan the rest of the args - if in physReg flush to memory
buzbeec0ecd652011-09-25 18:11:54 -0700871 for (int nextArg = 0; nextArg < numArgs;) {
872 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
buzbee1b4c8592011-08-31 10:43:51 -0700873 if (loc.wide) {
874 loc = oatUpdateLocWide(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700875 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700876 storeBaseDispWide(cUnit, rSP,
877 oatSRegOffset(cUnit, loc.sRegLow),
878 loc.lowReg, loc.highReg);
buzbee1b4c8592011-08-31 10:43:51 -0700879 }
buzbeec0ecd652011-09-25 18:11:54 -0700880 nextArg += 2;
buzbee1b4c8592011-08-31 10:43:51 -0700881 } else {
882 loc = oatUpdateLoc(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700883 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700884 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
885 loc.lowReg, kWord);
buzbee1b4c8592011-08-31 10:43:51 -0700886 }
buzbeec0ecd652011-09-25 18:11:54 -0700887 nextArg++;
buzbee67bf8852011-08-17 17:51:35 -0700888 }
889 }
890
buzbee67bc2362011-10-11 18:08:40 -0700891 int startOffset = oatSRegOffset(cUnit,
892 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
buzbee67bf8852011-08-17 17:51:35 -0700893 int outsOffset = 4 /* Method* */ + (3 * 4);
894 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700895 // Generate memcpy
896 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
897 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700898 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
899 loadConstant(cUnit, r2, (numArgs - 3) * 4);
Ian Rogersff1ed472011-09-20 13:46:24 -0700900 callRuntimeHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700901 // Restore Method*
902 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700903 } else {
904 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700905 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700906 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700907 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700908 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
909 //TUNING: loosen barrier
910 ld->defMask = ENCODE_ALL;
911 setMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbee561227c2011-09-02 15:28:19 -0700912 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700913 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700914 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbeef48e9712011-09-15 17:54:28 -0700915 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
916 setMemRefType(st, false /* isLoad */, kDalvikReg);
917 st->defMask = ENCODE_ALL;
buzbee561227c2011-09-02 15:28:19 -0700918 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700919 }
920
buzbeec0ecd652011-09-25 18:11:54 -0700921 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
922 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700923
buzbee561227c2011-09-02 15:28:19 -0700924 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee99f27232011-10-05 12:56:36 -0700925 if (pcrLabel) {
926 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
927 }
buzbee67bf8852011-08-17 17:51:35 -0700928 return callState;
929}
930
buzbee2a475e72011-09-07 17:19:17 -0700931// Debugging routine - if null target, branch to DebugMe
buzbeeed3e9302011-09-23 17:34:19 -0700932STATIC void genShowTarget(CompilationUnit* cUnit)
buzbee2a475e72011-09-07 17:19:17 -0700933{
934 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
935 loadWordDisp(cUnit, rSELF,
936 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
937 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
938 target->defMask = -1;
939 branchOver->generic.target = (LIR*)target;
940}
buzbee2a475e72011-09-07 17:19:17 -0700941
buzbeeed3e9302011-09-23 17:34:19 -0700942STATIC void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700943 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700944{
945 DecodedInstruction* dInsn = &mir->dalvikInsn;
946 int callState = 0;
947 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700948 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700949 NextCallInsn nextCallInsn = nextSDCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700950 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee561227c2011-09-02 15:28:19 -0700951
buzbee109bd6a2011-09-06 13:58:41 -0700952 // Explicit register usage
953 oatLockCallTemps(cUnit);
954
buzbee99f27232011-10-05 12:56:36 -0700955 // Is this the special "Ljava/lang/Object;.<init>:()V" case?
956 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT) {
957 int idx = mir->dalvikInsn.vB;
958 Method* target = cUnit->method->GetDexCacheResolvedMethods()->Get(idx);
959 if (target) {
960 if (PrettyMethod(target) == "java.lang.Object.<init>()V") {
961 RegLocation rlArg = oatGetSrc(cUnit, mir, 0);
962 loadValueDirectFixed(cUnit, rlArg, r0);
963 loadWordDisp(cUnit, rSELF,
964 OFFSETOF_MEMBER(Thread, pObjectInit), rLR);
965 genNullCheck(cUnit, oatSSASrc(mir,0), r0, mir);
966 opReg(cUnit, kOpBlx, rLR);
967 oatClobberCalleeSave(cUnit);
968 return;
969 }
970 }
971 }
972
buzbee561227c2011-09-02 15:28:19 -0700973 if (range) {
974 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700975 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700976 } else {
977 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700978 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700979 }
buzbee67bf8852011-08-17 17:51:35 -0700980 // Finish up any of the call sequence not interleaved in arg loading
981 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700982 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700983 }
buzbeece302932011-10-04 14:32:18 -0700984 if (DISPLAY_MISSING_TARGETS) {
985 genShowTarget(cUnit);
986 }
buzbeeec5adf32011-09-11 15:25:43 -0700987 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -0700988 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700989}
990
buzbee4a3164f2011-09-03 11:25:10 -0700991/*
992 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
993 * which will locate the target and continue on via a tail call.
994 */
buzbeeed3e9302011-09-23 17:34:19 -0700995STATIC void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -0700996{
997 DecodedInstruction* dInsn = &mir->dalvikInsn;
998 int callState = 0;
999 ArmLIR* nullCk;
buzbeec0ecd652011-09-25 18:11:54 -07001000 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001001
1002 // Explicit register usage
1003 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001004 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -07001005 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001006 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
1007 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001008 false, nextInterfaceCallInsn, NULL,
buzbee367ce0b2011-09-14 23:19:50 -07001009 false);
buzbee67bf8852011-08-17 17:51:35 -07001010 else
1011 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee367ce0b2011-09-14 23:19:50 -07001012 nextInterfaceCallInsn, NULL, false);
buzbee67bf8852011-08-17 17:51:35 -07001013 // Finish up any of the call sequence not interleaved in arg loading
1014 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001015 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001016 }
buzbeece302932011-10-04 14:32:18 -07001017 if (DISPLAY_MISSING_TARGETS) {
1018 genShowTarget(cUnit);
1019 }
buzbeeec5adf32011-09-11 15:25:43 -07001020 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001021 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001022}
1023
buzbeeed3e9302011-09-23 17:34:19 -07001024STATIC void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001025{
1026 DecodedInstruction* dInsn = &mir->dalvikInsn;
1027 int callState = 0;
buzbee4a3164f2011-09-03 11:25:10 -07001028 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001029 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1030 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -07001031 NextCallInsn nextCallInsn;
1032 bool fastPath = true;
buzbeec0ecd652011-09-25 18:11:54 -07001033 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001034
1035 // Explicit register usage
1036 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001037 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
buzbee4a3164f2011-09-03 11:25:10 -07001038 fastPath = false;
1039 } else {
1040 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
1041 if (superClass == NULL) {
1042 fastPath = false;
1043 } else {
1044 int32_t target_idx = baseMethod->GetMethodIndex();
1045 if (superClass->GetVTable()->GetLength() <= target_idx) {
1046 fastPath = false;
1047 } else {
1048 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
1049 }
1050 }
1051 }
1052 if (fastPath) {
1053 nextCallInsn = nextSuperCallInsn;
1054 rollback = NULL;
1055 } else {
1056 nextCallInsn = nextSuperCallInsnSP;
1057 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1058 rollback->defMask = -1;
1059 }
buzbee67bf8852011-08-17 17:51:35 -07001060 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
buzbeec0ecd652011-09-25 18:11:54 -07001061 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001062 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001063 else
buzbeec0ecd652011-09-25 18:11:54 -07001064 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001065 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001066 // Finish up any of the call sequence not interleaved in arg loading
1067 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -07001068 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001069 }
buzbeece302932011-10-04 14:32:18 -07001070 if (DISPLAY_MISSING_TARGETS) {
1071 genShowTarget(cUnit);
1072 }
buzbeeec5adf32011-09-11 15:25:43 -07001073 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001074 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001075}
1076
buzbeeed3e9302011-09-23 17:34:19 -07001077STATIC void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001078{
1079 DecodedInstruction* dInsn = &mir->dalvikInsn;
1080 int callState = 0;
buzbee561227c2011-09-02 15:28:19 -07001081 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001082 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1083 Method* method = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -07001084 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -07001085 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee7b1b86d2011-08-26 18:59:10 -07001086
buzbee109bd6a2011-09-06 13:58:41 -07001087 // Explicit register usage
1088 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001089 if (SLOW_INVOKE_PATH || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -07001090 // Slow path
1091 nextCallInsn = nextVCallInsnSP;
1092 // If we need a slow-path callout, we'll restart here
1093 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1094 rollback->defMask = -1;
1095 } else {
1096 // Fast path
1097 nextCallInsn = nextVCallInsn;
1098 rollback = NULL;
1099 }
buzbee67bf8852011-08-17 17:51:35 -07001100 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
buzbeec0ecd652011-09-25 18:11:54 -07001101 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001102 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001103 else
buzbeec0ecd652011-09-25 18:11:54 -07001104 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001105 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001106 // Finish up any of the call sequence not interleaved in arg loading
1107 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001108 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001109 }
buzbeece302932011-10-04 14:32:18 -07001110 if (DISPLAY_MISSING_TARGETS) {
1111 genShowTarget(cUnit);
1112 }
buzbeeec5adf32011-09-11 15:25:43 -07001113 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001114 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001115}
1116
buzbeeed3e9302011-09-23 17:34:19 -07001117STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001118 BasicBlock* bb, ArmLIR* labelList)
1119{
1120 bool res = false; // Assume success
1121 RegLocation rlSrc[3];
1122 RegLocation rlDest = badLoc;
1123 RegLocation rlResult = badLoc;
1124 Opcode opcode = mir->dalvikInsn.opcode;
1125
1126 /* Prep Src and Dest locations */
1127 int nextSreg = 0;
1128 int nextLoc = 0;
1129 int attrs = oatDataFlowAttributes[opcode];
1130 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1131 if (attrs & DF_UA) {
1132 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1133 nextSreg++;
1134 } else if (attrs & DF_UA_WIDE) {
1135 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1136 nextSreg + 1);
1137 nextSreg+= 2;
1138 }
1139 if (attrs & DF_UB) {
1140 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1141 nextSreg++;
1142 } else if (attrs & DF_UB_WIDE) {
1143 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1144 nextSreg + 1);
1145 nextSreg+= 2;
1146 }
1147 if (attrs & DF_UC) {
1148 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1149 } else if (attrs & DF_UC_WIDE) {
1150 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1151 nextSreg + 1);
1152 }
1153 if (attrs & DF_DA) {
1154 rlDest = oatGetDest(cUnit, mir, 0);
1155 } else if (attrs & DF_DA_WIDE) {
1156 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1157 }
1158
1159 switch(opcode) {
1160 case OP_NOP:
1161 break;
1162
1163 case OP_MOVE_EXCEPTION:
1164 int exOffset;
1165 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001166 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001167 resetReg = oatAllocTemp(cUnit);
1168 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1169 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1170 loadConstant(cUnit, resetReg, 0);
1171 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1172 storeValue(cUnit, rlDest, rlResult);
1173 break;
1174
1175 case OP_RETURN_VOID:
buzbeefe2e17f2011-10-10 09:35:02 -07001176 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001177 break;
1178
1179 case OP_RETURN:
1180 case OP_RETURN_OBJECT:
buzbeefe2e17f2011-10-10 09:35:02 -07001181 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001182 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001183 break;
1184
1185 case OP_RETURN_WIDE:
buzbeefe2e17f2011-10-10 09:35:02 -07001186 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001187 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001188 break;
1189
1190 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -07001191 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001192 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001193 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001194 break;
1195
1196 case OP_MOVE_RESULT:
1197 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -07001198 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001199 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001200 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001201 break;
1202
1203 case OP_MOVE:
1204 case OP_MOVE_OBJECT:
1205 case OP_MOVE_16:
1206 case OP_MOVE_OBJECT_16:
1207 case OP_MOVE_FROM16:
1208 case OP_MOVE_OBJECT_FROM16:
1209 storeValue(cUnit, rlDest, rlSrc[0]);
1210 break;
1211
1212 case OP_MOVE_WIDE:
1213 case OP_MOVE_WIDE_16:
1214 case OP_MOVE_WIDE_FROM16:
1215 storeValueWide(cUnit, rlDest, rlSrc[0]);
1216 break;
1217
1218 case OP_CONST:
1219 case OP_CONST_4:
1220 case OP_CONST_16:
1221 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1222 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1223 storeValue(cUnit, rlDest, rlResult);
1224 break;
1225
1226 case OP_CONST_HIGH16:
1227 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1228 loadConstantNoClobber(cUnit, rlResult.lowReg,
1229 mir->dalvikInsn.vB << 16);
1230 storeValue(cUnit, rlDest, rlResult);
1231 break;
1232
1233 case OP_CONST_WIDE_16:
1234 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -07001235 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1236 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1237 mir->dalvikInsn.vB,
1238 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -07001239 storeValueWide(cUnit, rlDest, rlResult);
1240 break;
1241
1242 case OP_CONST_WIDE:
1243 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1244 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001245 mir->dalvikInsn.vB_wide & 0xffffffff,
1246 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001247 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001248 break;
1249
1250 case OP_CONST_WIDE_HIGH16:
1251 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1252 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1253 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001254 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001255 break;
1256
1257 case OP_MONITOR_ENTER:
1258 genMonitorEnter(cUnit, mir, rlSrc[0]);
1259 break;
1260
1261 case OP_MONITOR_EXIT:
1262 genMonitorExit(cUnit, mir, rlSrc[0]);
1263 break;
1264
1265 case OP_CHECK_CAST:
1266 genCheckCast(cUnit, mir, rlSrc[0]);
1267 break;
1268
1269 case OP_INSTANCE_OF:
1270 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1271 break;
1272
1273 case OP_NEW_INSTANCE:
1274 genNewInstance(cUnit, mir, rlDest);
1275 break;
1276
1277 case OP_THROW:
1278 genThrow(cUnit, mir, rlSrc[0]);
1279 break;
1280
buzbee5ade1d22011-09-09 14:44:52 -07001281 case OP_THROW_VERIFICATION_ERROR:
1282 loadWordDisp(cUnit, rSELF,
1283 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1284 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1285 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001286 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001287 break;
1288
buzbee67bf8852011-08-17 17:51:35 -07001289 case OP_ARRAY_LENGTH:
1290 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001291 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001292 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001293 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001294 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1295 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1296 rlResult.lowReg);
1297 storeValue(cUnit, rlDest, rlResult);
1298 break;
1299
1300 case OP_CONST_STRING:
1301 case OP_CONST_STRING_JUMBO:
1302 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1303 break;
1304
1305 case OP_CONST_CLASS:
1306 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1307 break;
1308
1309 case OP_FILL_ARRAY_DATA:
1310 genFillArrayData(cUnit, mir, rlSrc[0]);
1311 break;
1312
1313 case OP_FILLED_NEW_ARRAY:
1314 genFilledNewArray(cUnit, mir, false /* not range */);
1315 break;
1316
1317 case OP_FILLED_NEW_ARRAY_RANGE:
1318 genFilledNewArray(cUnit, mir, true /* range */);
1319 break;
1320
1321 case OP_NEW_ARRAY:
1322 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1323 break;
1324
1325 case OP_GOTO:
1326 case OP_GOTO_16:
1327 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001328 if (bb->taken->startOffset <= mir->offset) {
1329 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001330 }
1331 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1332 break;
1333
1334 case OP_PACKED_SWITCH:
1335 genPackedSwitch(cUnit, mir, rlSrc[0]);
1336 break;
1337
1338 case OP_SPARSE_SWITCH:
1339 genSparseSwitch(cUnit, mir, rlSrc[0]);
1340 break;
1341
1342 case OP_CMPL_FLOAT:
1343 case OP_CMPG_FLOAT:
1344 case OP_CMPL_DOUBLE:
1345 case OP_CMPG_DOUBLE:
1346 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1347 break;
1348
1349 case OP_CMP_LONG:
1350 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1351 break;
1352
1353 case OP_IF_EQ:
1354 case OP_IF_NE:
1355 case OP_IF_LT:
1356 case OP_IF_GE:
1357 case OP_IF_GT:
1358 case OP_IF_LE: {
1359 bool backwardBranch;
1360 ArmConditionCode cond;
1361 backwardBranch = (bb->taken->startOffset <= mir->offset);
1362 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001363 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001364 }
1365 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1366 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1367 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1368 switch(opcode) {
1369 case OP_IF_EQ:
1370 cond = kArmCondEq;
1371 break;
1372 case OP_IF_NE:
1373 cond = kArmCondNe;
1374 break;
1375 case OP_IF_LT:
1376 cond = kArmCondLt;
1377 break;
1378 case OP_IF_GE:
1379 cond = kArmCondGe;
1380 break;
1381 case OP_IF_GT:
1382 cond = kArmCondGt;
1383 break;
1384 case OP_IF_LE:
1385 cond = kArmCondLe;
1386 break;
1387 default:
1388 cond = (ArmConditionCode)0;
1389 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1390 }
1391 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1392 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1393 break;
1394 }
1395
1396 case OP_IF_EQZ:
1397 case OP_IF_NEZ:
1398 case OP_IF_LTZ:
1399 case OP_IF_GEZ:
1400 case OP_IF_GTZ:
1401 case OP_IF_LEZ: {
1402 bool backwardBranch;
1403 ArmConditionCode cond;
1404 backwardBranch = (bb->taken->startOffset <= mir->offset);
1405 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001406 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001407 }
1408 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1409 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1410 switch(opcode) {
1411 case OP_IF_EQZ:
1412 cond = kArmCondEq;
1413 break;
1414 case OP_IF_NEZ:
1415 cond = kArmCondNe;
1416 break;
1417 case OP_IF_LTZ:
1418 cond = kArmCondLt;
1419 break;
1420 case OP_IF_GEZ:
1421 cond = kArmCondGe;
1422 break;
1423 case OP_IF_GTZ:
1424 cond = kArmCondGt;
1425 break;
1426 case OP_IF_LEZ:
1427 cond = kArmCondLe;
1428 break;
1429 default:
1430 cond = (ArmConditionCode)0;
1431 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1432 }
1433 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1434 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1435 break;
1436 }
1437
1438 case OP_AGET_WIDE:
1439 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1440 break;
1441 case OP_AGET:
1442 case OP_AGET_OBJECT:
1443 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1444 break;
1445 case OP_AGET_BOOLEAN:
1446 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1447 rlDest, 0);
1448 break;
1449 case OP_AGET_BYTE:
1450 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1451 break;
1452 case OP_AGET_CHAR:
1453 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1454 rlDest, 1);
1455 break;
1456 case OP_AGET_SHORT:
1457 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1458 break;
1459 case OP_APUT_WIDE:
1460 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1461 break;
1462 case OP_APUT:
1463 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1464 break;
1465 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001466 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001467 break;
1468 case OP_APUT_SHORT:
1469 case OP_APUT_CHAR:
1470 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1471 rlSrc[0], 1);
1472 break;
1473 case OP_APUT_BYTE:
1474 case OP_APUT_BOOLEAN:
1475 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1476 rlSrc[0], 0);
1477 break;
1478
1479 case OP_IGET_WIDE:
1480 case OP_IGET_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001481 genIGetWide(cUnit, mir, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001482 break;
1483
1484 case OP_IGET:
1485 case OP_IGET_VOLATILE:
1486 case OP_IGET_OBJECT:
1487 case OP_IGET_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001488 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001489 break;
1490
1491 case OP_IGET_BOOLEAN:
1492 case OP_IGET_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001493 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001494 break;
1495
1496 case OP_IGET_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001497 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001498 break;
1499
1500 case OP_IGET_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001501 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001502 break;
1503
1504 case OP_IPUT_WIDE:
1505 case OP_IPUT_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001506 genIPutWide(cUnit, mir, rlSrc[0], rlSrc[1]);
buzbee67bf8852011-08-17 17:51:35 -07001507 break;
1508
1509 case OP_IPUT_OBJECT:
1510 case OP_IPUT_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001511 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
buzbee67bf8852011-08-17 17:51:35 -07001512 break;
1513
1514 case OP_IPUT:
1515 case OP_IPUT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001516 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001517 break;
1518
1519 case OP_IPUT_BOOLEAN:
1520 case OP_IPUT_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001521 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001522 break;
1523
1524 case OP_IPUT_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001525 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001526 break;
1527
1528 case OP_IPUT_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001529 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001530 break;
1531
1532 case OP_SGET:
1533 case OP_SGET_OBJECT:
1534 case OP_SGET_BOOLEAN:
1535 case OP_SGET_BYTE:
1536 case OP_SGET_CHAR:
1537 case OP_SGET_SHORT:
1538 genSget(cUnit, mir, rlResult, rlDest);
1539 break;
1540
1541 case OP_SGET_WIDE:
1542 genSgetWide(cUnit, mir, rlResult, rlDest);
1543 break;
1544
1545 case OP_SPUT:
1546 case OP_SPUT_OBJECT:
1547 case OP_SPUT_BOOLEAN:
1548 case OP_SPUT_BYTE:
1549 case OP_SPUT_CHAR:
1550 case OP_SPUT_SHORT:
1551 genSput(cUnit, mir, rlSrc[0]);
1552 break;
1553
1554 case OP_SPUT_WIDE:
1555 genSputWide(cUnit, mir, rlSrc[0]);
1556 break;
1557
1558 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001559 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1560 true /*range*/);
1561 break;
buzbee67bf8852011-08-17 17:51:35 -07001562 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001563 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1564 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001565 break;
1566
1567 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001568 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1569 false /*range*/);
1570 break;
buzbee67bf8852011-08-17 17:51:35 -07001571 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001572 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1573 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001574 break;
1575
1576 case OP_INVOKE_VIRTUAL:
1577 case OP_INVOKE_VIRTUAL_RANGE:
1578 genInvokeVirtual(cUnit, mir);
1579 break;
1580
1581 case OP_INVOKE_SUPER:
1582 case OP_INVOKE_SUPER_RANGE:
1583 genInvokeSuper(cUnit, mir);
1584 break;
1585
1586 case OP_INVOKE_INTERFACE:
1587 case OP_INVOKE_INTERFACE_RANGE:
1588 genInvokeInterface(cUnit, mir);
1589 break;
1590
1591 case OP_NEG_INT:
1592 case OP_NOT_INT:
1593 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1594 break;
1595
1596 case OP_NEG_LONG:
1597 case OP_NOT_LONG:
1598 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1599 break;
1600
1601 case OP_NEG_FLOAT:
1602 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1603 break;
1604
1605 case OP_NEG_DOUBLE:
1606 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1607 break;
1608
1609 case OP_INT_TO_LONG:
1610 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1611 if (rlSrc[0].location == kLocPhysReg) {
1612 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1613 } else {
1614 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1615 }
1616 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1617 rlResult.lowReg, 31);
1618 storeValueWide(cUnit, rlDest, rlResult);
1619 break;
1620
1621 case OP_LONG_TO_INT:
1622 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1623 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1624 storeValue(cUnit, rlDest, rlSrc[0]);
1625 break;
1626
1627 case OP_INT_TO_BYTE:
1628 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1629 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1630 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1631 storeValue(cUnit, rlDest, rlResult);
1632 break;
1633
1634 case OP_INT_TO_SHORT:
1635 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1636 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1637 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1638 storeValue(cUnit, rlDest, rlResult);
1639 break;
1640
1641 case OP_INT_TO_CHAR:
1642 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1643 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1644 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1645 storeValue(cUnit, rlDest, rlResult);
1646 break;
1647
1648 case OP_INT_TO_FLOAT:
1649 case OP_INT_TO_DOUBLE:
1650 case OP_LONG_TO_FLOAT:
1651 case OP_LONG_TO_DOUBLE:
1652 case OP_FLOAT_TO_INT:
1653 case OP_FLOAT_TO_LONG:
1654 case OP_FLOAT_TO_DOUBLE:
1655 case OP_DOUBLE_TO_INT:
1656 case OP_DOUBLE_TO_LONG:
1657 case OP_DOUBLE_TO_FLOAT:
1658 genConversion(cUnit, mir);
1659 break;
1660
1661 case OP_ADD_INT:
1662 case OP_SUB_INT:
1663 case OP_MUL_INT:
1664 case OP_DIV_INT:
1665 case OP_REM_INT:
1666 case OP_AND_INT:
1667 case OP_OR_INT:
1668 case OP_XOR_INT:
1669 case OP_SHL_INT:
1670 case OP_SHR_INT:
1671 case OP_USHR_INT:
1672 case OP_ADD_INT_2ADDR:
1673 case OP_SUB_INT_2ADDR:
1674 case OP_MUL_INT_2ADDR:
1675 case OP_DIV_INT_2ADDR:
1676 case OP_REM_INT_2ADDR:
1677 case OP_AND_INT_2ADDR:
1678 case OP_OR_INT_2ADDR:
1679 case OP_XOR_INT_2ADDR:
1680 case OP_SHL_INT_2ADDR:
1681 case OP_SHR_INT_2ADDR:
1682 case OP_USHR_INT_2ADDR:
1683 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1684 break;
1685
1686 case OP_ADD_LONG:
1687 case OP_SUB_LONG:
1688 case OP_MUL_LONG:
1689 case OP_DIV_LONG:
1690 case OP_REM_LONG:
1691 case OP_AND_LONG:
1692 case OP_OR_LONG:
1693 case OP_XOR_LONG:
1694 case OP_ADD_LONG_2ADDR:
1695 case OP_SUB_LONG_2ADDR:
1696 case OP_MUL_LONG_2ADDR:
1697 case OP_DIV_LONG_2ADDR:
1698 case OP_REM_LONG_2ADDR:
1699 case OP_AND_LONG_2ADDR:
1700 case OP_OR_LONG_2ADDR:
1701 case OP_XOR_LONG_2ADDR:
1702 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1703 break;
1704
buzbee67bf8852011-08-17 17:51:35 -07001705 case OP_SHL_LONG:
1706 case OP_SHR_LONG:
1707 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001708 case OP_SHL_LONG_2ADDR:
1709 case OP_SHR_LONG_2ADDR:
1710 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001711 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1712 break;
1713
1714 case OP_ADD_FLOAT:
1715 case OP_SUB_FLOAT:
1716 case OP_MUL_FLOAT:
1717 case OP_DIV_FLOAT:
1718 case OP_REM_FLOAT:
1719 case OP_ADD_FLOAT_2ADDR:
1720 case OP_SUB_FLOAT_2ADDR:
1721 case OP_MUL_FLOAT_2ADDR:
1722 case OP_DIV_FLOAT_2ADDR:
1723 case OP_REM_FLOAT_2ADDR:
1724 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1725 break;
1726
1727 case OP_ADD_DOUBLE:
1728 case OP_SUB_DOUBLE:
1729 case OP_MUL_DOUBLE:
1730 case OP_DIV_DOUBLE:
1731 case OP_REM_DOUBLE:
1732 case OP_ADD_DOUBLE_2ADDR:
1733 case OP_SUB_DOUBLE_2ADDR:
1734 case OP_MUL_DOUBLE_2ADDR:
1735 case OP_DIV_DOUBLE_2ADDR:
1736 case OP_REM_DOUBLE_2ADDR:
1737 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1738 break;
1739
1740 case OP_RSUB_INT:
1741 case OP_ADD_INT_LIT16:
1742 case OP_MUL_INT_LIT16:
1743 case OP_DIV_INT_LIT16:
1744 case OP_REM_INT_LIT16:
1745 case OP_AND_INT_LIT16:
1746 case OP_OR_INT_LIT16:
1747 case OP_XOR_INT_LIT16:
1748 case OP_ADD_INT_LIT8:
1749 case OP_RSUB_INT_LIT8:
1750 case OP_MUL_INT_LIT8:
1751 case OP_DIV_INT_LIT8:
1752 case OP_REM_INT_LIT8:
1753 case OP_AND_INT_LIT8:
1754 case OP_OR_INT_LIT8:
1755 case OP_XOR_INT_LIT8:
1756 case OP_SHL_INT_LIT8:
1757 case OP_SHR_INT_LIT8:
1758 case OP_USHR_INT_LIT8:
1759 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1760 break;
1761
1762 default:
1763 res = true;
1764 }
1765 return res;
1766}
1767
buzbeeed3e9302011-09-23 17:34:19 -07001768STATIC const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001769 "kMirOpPhi",
1770 "kMirOpNullNRangeUpCheck",
1771 "kMirOpNullNRangeDownCheck",
1772 "kMirOpLowerBound",
1773 "kMirOpPunt",
1774 "kMirOpCheckInlinePrediction",
1775};
1776
1777/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001778STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001779{
1780 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1781 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1782 strcpy(msg, extendedMIROpNames[opOffset]);
1783 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1784
1785 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1786 case kMirOpPhi: {
1787 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1788 op->flags.isNop = true;
1789 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1790 break;
1791 }
1792 default:
1793 break;
1794 }
1795}
1796
buzbee67bc2362011-10-11 18:08:40 -07001797/*
1798 * If there are any ins passed in registers that have not been promoted
1799 * to a callee-save register, flush them to the frame. Perform intial
1800 * assignment of promoted arguments.
1801 */
buzbeeed3e9302011-09-23 17:34:19 -07001802STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001803{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001805 return;
buzbee67bc2362011-10-11 18:08:40 -07001806 int firstArgReg = r1;
1807 int lastArgReg = r3;
1808 int startVReg = cUnit->method->NumRegisters() -
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001809 cUnit->method->NumIns();
buzbee67bc2362011-10-11 18:08:40 -07001810 for (int i = 0; i < cUnit->method->NumIns(); i++) {
1811 PromotionMap vMap = cUnit->promotionMap[startVReg + i];
1812 // For arguments only, should have at most one promotion kind
1813 DCHECK(!((vMap.coreLocation == kLocPhysReg) &&
1814 (vMap.fpLocation == kLocPhysReg)));
1815 if (i <= (lastArgReg - firstArgReg)) {
1816 // If arriving in register, copy or flush
1817 if (vMap.coreLocation == kLocPhysReg) {
1818 genRegCopy(cUnit, vMap.coreReg, firstArgReg + i);
1819 } else if (vMap.fpLocation == kLocPhysReg) {
1820 genRegCopy(cUnit, vMap.fpReg, firstArgReg + i);
1821 }
1822 // Also put a copy in memory in case we're partially promoted
1823 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1824 firstArgReg + i, kWord);
1825 } else {
1826 // If arriving in frame, initialize promoted target regs
1827 if (vMap.coreLocation == kLocPhysReg) {
1828 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1829 vMap.coreReg);
1830 } else if (vMap.fpLocation == kLocPhysReg) {
1831 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1832 vMap.fpReg);
buzbee67bf8852011-08-17 17:51:35 -07001833 }
1834 }
buzbee67bf8852011-08-17 17:51:35 -07001835 }
1836}
1837
1838/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001839STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001840{
1841 MIR* mir;
1842 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1843 int blockId = bb->id;
1844
1845 cUnit->curBlock = bb;
1846 labelList[blockId].operands[0] = bb->startOffset;
1847
1848 /* Insert the block label */
1849 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1850 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1851
buzbee6181f792011-09-29 11:14:04 -07001852 /* Reset local optimization data on block boundaries */
1853 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001854 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001855 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001856
1857 ArmLIR* headLIR = NULL;
1858
buzbeebbaf8942011-10-02 13:08:29 -07001859 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001860 if (bb->blockType == kEntryBlock) {
1861 /*
1862 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1863 * mechanism know so it doesn't try to use any of them when
1864 * expanding the frame or flushing. This leaves the utility
1865 * code with a single temp: r12. This should be enough.
1866 */
1867 oatLockTemp(cUnit, r0);
1868 oatLockTemp(cUnit, r1);
1869 oatLockTemp(cUnit, r2);
1870 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001871
1872 /*
1873 * We can safely skip the stack overflow check if we're
1874 * a leaf *and* our frame size < fudge factor.
1875 */
1876 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1877 ((size_t)cUnit->frameSize <
1878 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001879 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001880 if (!skipOverflowCheck) {
1881 /* Load stack limit */
1882 loadWordDisp(cUnit, rSELF,
1883 art::Thread::StackEndOffset().Int32Value(), r12);
1884 }
buzbee67bf8852011-08-17 17:51:35 -07001885 /* Spill core callee saves */
1886 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1887 /* Need to spill any FP regs? */
1888 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001889 /*
1890 * NOTE: fp spills are a little different from core spills in that
1891 * they are pushed as a contiguous block. When promoting from
1892 * the fp set, we must allocate all singles from s16..highest-promoted
1893 */
buzbee67bf8852011-08-17 17:51:35 -07001894 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1895 }
buzbeecefd1872011-09-09 09:59:52 -07001896 if (!skipOverflowCheck) {
1897 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001898 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001899 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1900 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001901 genRegCopy(cUnit, rSP, rLR); // Establish stack
1902 } else {
1903 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001904 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001905 }
buzbee67bf8852011-08-17 17:51:35 -07001906 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1907 flushIns(cUnit);
1908 oatFreeTemp(cUnit, r0);
1909 oatFreeTemp(cUnit, r1);
1910 oatFreeTemp(cUnit, r2);
1911 oatFreeTemp(cUnit, r3);
1912 } else if (bb->blockType == kExitBlock) {
1913 newLIR0(cUnit, kArmPseudoMethodExit);
buzbeebbaf8942011-10-02 13:08:29 -07001914 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07001915 /* Need to restore any FP callee saves? */
1916 if (cUnit->numFPSpills) {
1917 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1918 }
1919 if (cUnit->coreSpillMask & (1 << rLR)) {
1920 /* Unspill rLR to rPC */
1921 cUnit->coreSpillMask &= ~(1 << rLR);
1922 cUnit->coreSpillMask |= (1 << rPC);
1923 }
1924 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1925 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1926 /* We didn't pop to rPC, so must do a bv rLR */
1927 newLIR1(cUnit, kThumbBx, rLR);
1928 }
1929 }
1930
1931 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1932
1933 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07001934 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1935 oatClobberAllRegs(cUnit);
1936 }
buzbee67bf8852011-08-17 17:51:35 -07001937
1938 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1939 oatResetDefTracking(cUnit);
1940 }
1941
1942 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1943 handleExtendedMethodMIR(cUnit, mir);
1944 continue;
1945 }
1946
1947 cUnit->currentDalvikOffset = mir->offset;
1948
1949 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1950 InstructionFormat dalvikFormat =
1951 dexGetFormatFromOpcode(dalvikOpcode);
1952
1953 ArmLIR* boundaryLIR;
1954
1955 /* Mark the beginning of a Dalvik instruction for line tracking */
1956 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1957 (int) oatGetDalvikDisassembly(
1958 &mir->dalvikInsn, ""));
1959 /* Remember the first LIR for this block */
1960 if (headLIR == NULL) {
1961 headLIR = boundaryLIR;
1962 /* Set the first boundaryLIR as a scheduling barrier */
1963 headLIR->defMask = ENCODE_ALL;
1964 }
1965
1966 /* Don't generate the SSA annotation unless verbose mode is on */
1967 if (cUnit->printMe && mir->ssaRep) {
1968 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1969 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1970 }
1971
1972 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1973
1974 if (notHandled) {
1975 char buf[100];
1976 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1977 mir->offset,
1978 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1979 dalvikFormat);
1980 LOG(FATAL) << buf;
1981 }
1982 }
1983
1984 if (headLIR) {
1985 /*
1986 * Eliminate redundant loads/stores and delay stores into later
1987 * slots
1988 */
1989 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1990 cUnit->lastLIRInsn);
1991
1992 /*
1993 * Generate an unconditional branch to the fallthrough block.
1994 */
1995 if (bb->fallThrough) {
1996 genUnconditionalBranch(cUnit,
1997 &labelList[bb->fallThrough->id]);
1998 }
1999 }
2000 return false;
2001}
2002
2003/*
2004 * Nop any unconditional branches that go to the next instruction.
2005 * Note: new redundant branches may be inserted later, and we'll
2006 * use a check in final instruction assembly to nop those out.
2007 */
2008void removeRedundantBranches(CompilationUnit* cUnit)
2009{
2010 ArmLIR* thisLIR;
2011
2012 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
2013 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
2014 thisLIR = NEXT_LIR(thisLIR)) {
2015
2016 /* Branch to the next instruction */
2017 if ((thisLIR->opcode == kThumbBUncond) ||
2018 (thisLIR->opcode == kThumb2BUncond)) {
2019 ArmLIR* nextLIR = thisLIR;
2020
2021 while (true) {
2022 nextLIR = NEXT_LIR(nextLIR);
2023
2024 /*
2025 * Is the branch target the next instruction?
2026 */
2027 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
2028 thisLIR->flags.isNop = true;
2029 break;
2030 }
2031
2032 /*
2033 * Found real useful stuff between the branch and the target.
2034 * Need to explicitly check the lastLIRInsn here because it
2035 * might be the last real instruction.
2036 */
2037 if (!isPseudoOpcode(nextLIR->opcode) ||
2038 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
2039 break;
2040 }
2041 }
2042 }
2043}
2044
buzbeeed3e9302011-09-23 17:34:19 -07002045STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07002046{
2047 ArmLIR** suspendLabel =
2048 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
2049 int numElems = cUnit->suspendLaunchpads.numUsed;
2050
2051 for (int i = 0; i < numElems; i++) {
2052 /* TUNING: move suspend count load into helper */
2053 ArmLIR* lab = suspendLabel[i];
2054 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
2055 cUnit->currentDalvikOffset = lab->operands[1];
2056 oatAppendLIR(cUnit, (LIR *)lab);
2057 loadWordDisp(cUnit, rSELF,
2058 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
2059 loadWordDisp(cUnit, rSELF,
2060 art::Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
2061 opReg(cUnit, kOpBlx, rLR);
2062 genUnconditionalBranch(cUnit, resumeLab);
2063 }
2064}
2065
buzbeeed3e9302011-09-23 17:34:19 -07002066STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07002067{
2068 ArmLIR** throwLabel =
2069 (ArmLIR **) cUnit->throwLaunchpads.elemList;
2070 int numElems = cUnit->throwLaunchpads.numUsed;
2071 int i;
2072
2073 for (i = 0; i < numElems; i++) {
2074 ArmLIR* lab = throwLabel[i];
2075 cUnit->currentDalvikOffset = lab->operands[1];
2076 oatAppendLIR(cUnit, (LIR *)lab);
2077 int funcOffset = 0;
2078 int v1 = lab->operands[2];
2079 int v2 = lab->operands[3];
2080 switch(lab->operands[0]) {
2081 case kArmThrowNullPointer:
2082 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2083 break;
2084 case kArmThrowArrayBounds:
2085 if (v2 != r0) {
2086 genRegCopy(cUnit, r0, v1);
2087 genRegCopy(cUnit, r1, v2);
2088 } else {
2089 if (v1 == r1) {
2090 genRegCopy(cUnit, r12, v1);
2091 genRegCopy(cUnit, r1, v2);
2092 genRegCopy(cUnit, r0, r12);
2093 } else {
2094 genRegCopy(cUnit, r1, v2);
2095 genRegCopy(cUnit, r0, v1);
2096 }
2097 }
2098 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2099 break;
2100 case kArmThrowDivZero:
2101 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2102 break;
2103 case kArmThrowVerificationError:
2104 loadConstant(cUnit, r0, v1);
2105 loadConstant(cUnit, r1, v2);
2106 funcOffset =
2107 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2108 break;
2109 case kArmThrowNegArraySize:
2110 genRegCopy(cUnit, r0, v1);
2111 funcOffset =
2112 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2113 break;
buzbee5ade1d22011-09-09 14:44:52 -07002114 case kArmThrowNoSuchMethod:
2115 genRegCopy(cUnit, r0, v1);
2116 funcOffset =
2117 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2118 break;
buzbeeec5adf32011-09-11 15:25:43 -07002119 case kArmThrowStackOverflow:
2120 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07002121 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07002122 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07002123 opRegImm(cUnit, kOpAdd, rSP,
2124 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07002125 break;
buzbee5ade1d22011-09-09 14:44:52 -07002126 default:
2127 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2128 }
2129 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07002130 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07002131 }
2132}
2133
buzbee67bf8852011-08-17 17:51:35 -07002134void oatMethodMIR2LIR(CompilationUnit* cUnit)
2135{
2136 /* Used to hold the labels of each block */
2137 cUnit->blockLabelList =
2138 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2139
2140 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2141 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07002142 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002143
2144 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07002145
2146 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002147}
2148
2149/* Common initialization routine for an architecture family */
2150bool oatArchInit()
2151{
2152 int i;
2153
2154 for (i = 0; i < kArmLast; i++) {
2155 if (EncodingMap[i].opcode != i) {
2156 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2157 " is wrong: expecting " << i << ", seeing " <<
2158 (int)EncodingMap[i].opcode;
2159 }
2160 }
2161
2162 return oatArchVariantInit();
2163}
2164
2165/* Needed by the Assembler */
2166void oatSetupResourceMasks(ArmLIR* lir)
2167{
2168 setupResourceMasks(lir);
2169}
2170
2171/* Needed by the ld/st optmizatons */
2172ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2173{
2174 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2175}
2176
2177/* Needed by the register allocator */
2178ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2179{
2180 return genRegCopy(cUnit, rDest, rSrc);
2181}
2182
2183/* Needed by the register allocator */
2184void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2185 int srcLo, int srcHi)
2186{
2187 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2188}
2189
2190void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2191 int displacement, int rSrc, OpSize size)
2192{
2193 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2194}
2195
2196void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2197 int displacement, int rSrcLo, int rSrcHi)
2198{
2199 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2200}