blob: fb665e68fa68d12a1557fe6c476c992332c78cce [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
buzbeeed3e9302011-09-23 17:34:19 -070020STATIC const RegLocation badLoc = {kLocDalvikFrame, 0, 0, INVALID_REG,
buzbee67bf8852011-08-17 17:51:35 -070021 INVALID_REG, INVALID_SREG, 0,
22 kLocDalvikFrame, INVALID_REG, INVALID_REG,
23 INVALID_OFFSET};
buzbee6181f792011-09-29 11:14:04 -070024
25/* Mark register usage state and return long retloc */
26STATIC RegLocation getRetLocWide(CompilationUnit* cUnit)
27{
28 RegLocation res = LOC_DALVIK_RETURN_VAL_WIDE;
29 oatLockTemp(cUnit, res.lowReg);
30 oatLockTemp(cUnit, res.highReg);
31 oatMarkPair(cUnit, res.lowReg, res.highReg);
32 return res;
33}
34
35STATIC RegLocation getRetLoc(CompilationUnit* cUnit)
36{
37 RegLocation res = LOC_DALVIK_RETURN_VAL;
38 oatLockTemp(cUnit, res.lowReg);
39 return res;
40}
buzbee67bf8852011-08-17 17:51:35 -070041
buzbeedfd3d702011-08-28 12:56:51 -070042/*
43 * Let helper function take care of everything. Will call
44 * Array::AllocFromCode(type_idx, method, count);
45 * Note: AllocFromCode will handle checks for errNegativeArraySize.
46 */
buzbeeed3e9302011-09-23 17:34:19 -070047STATIC void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -070048 RegLocation rlSrc)
49{
buzbeedfd3d702011-08-28 12:56:51 -070050 oatFlushAllRegs(cUnit); /* Everything to home location */
51 loadWordDisp(cUnit, rSELF,
Elliott Hughesb408de72011-10-04 14:35:05 -070052 OFFSETOF_MEMBER(Thread, pAllocArrayFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -070053 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
54 loadConstant(cUnit, r0, mir->dalvikInsn.vC); // arg0 <- type_id
55 loadValueDirectFixed(cUnit, rlSrc, r2); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070056 callRuntimeHelper(cUnit, rLR);
buzbeedfd3d702011-08-28 12:56:51 -070057 RegLocation rlResult = oatGetReturn(cUnit);
58 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -070059}
60
61/*
62 * Similar to genNewArray, but with post-allocation initialization.
63 * Verifier guarantees we're dealing with an array class. Current
64 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
65 * Current code also throws internal unimp if not 'L', '[' or 'I'.
66 */
buzbeeed3e9302011-09-23 17:34:19 -070067STATIC void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
buzbee67bf8852011-08-17 17:51:35 -070068{
69 DecodedInstruction* dInsn = &mir->dalvikInsn;
buzbee81eccc02011-09-17 13:42:21 -070070 int elems = dInsn->vA;
71 int typeId = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070072 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbeedfd3d702011-08-28 12:56:51 -070073 loadWordDisp(cUnit, rSELF,
Elliott Hughesb408de72011-10-04 14:35:05 -070074 OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -070075 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
76 loadConstant(cUnit, r0, typeId); // arg0 <- type_id
77 loadConstant(cUnit, r2, elems); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070078 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -070079 /*
buzbeedfd3d702011-08-28 12:56:51 -070080 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
81 * return region. Because AllocFromCode placed the new array
82 * in r0, we'll just lock it into place. When debugger support is
83 * added, it may be necessary to additionally copy all return
84 * values to a home location in thread-local storage
buzbee67bf8852011-08-17 17:51:35 -070085 */
buzbee67bf8852011-08-17 17:51:35 -070086 oatLockTemp(cUnit, r0);
buzbeedfd3d702011-08-28 12:56:51 -070087
buzbee67bf8852011-08-17 17:51:35 -070088 // Having a range of 0 is legal
89 if (isRange && (dInsn->vA > 0)) {
90 /*
91 * Bit of ugliness here. We're going generate a mem copy loop
92 * on the register range, but it is possible that some regs
93 * in the range have been promoted. This is unlikely, but
94 * before generating the copy, we'll just force a flush
95 * of any regs in the source range that have been promoted to
96 * home location.
97 */
98 for (unsigned int i = 0; i < dInsn->vA; i++) {
99 RegLocation loc = oatUpdateLoc(cUnit,
100 oatGetSrc(cUnit, mir, i));
101 if (loc.location == kLocPhysReg) {
102 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
103 }
104 }
105 /*
106 * TUNING note: generated code here could be much improved, but
107 * this is an uncommon operation and isn't especially performance
108 * critical.
109 */
110 int rSrc = oatAllocTemp(cUnit);
111 int rDst = oatAllocTemp(cUnit);
112 int rIdx = oatAllocTemp(cUnit);
113 int rVal = rLR; // Using a lot of temps, rLR is known free here
114 // Set up source pointer
115 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
116 opRegRegImm(cUnit, kOpAdd, rSrc, rSP, rlFirst.spOffset);
117 // 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)
121 loadConstant(cUnit, rIdx, dInsn->vA);
122 // 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);
130 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
131 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;
776 loadWordDisp(cUnit, rSP, rlArg.spOffset + 4, reg);
777 callState = nextCallInsn(cUnit, mir, dInsn, callState,
778 rollback);
779 }
780 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
781 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
782 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
783 nextUse++;
784 }
785 // Loop through the rest
786 while (nextUse < dInsn->vA) {
787 int lowReg;
788 int highReg;
789 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
790 rlArg = oatUpdateRawLoc(cUnit, rlArg);
791 if (rlArg.location == kLocPhysReg) {
792 lowReg = rlArg.lowReg;
793 highReg = rlArg.highReg;
794 } else {
795 lowReg = r2;
796 highReg = r3;
797 if (rlArg.wide) {
798 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
799 } else {
800 loadValueDirectFixed(cUnit, rlArg, lowReg);
801 }
802 callState = nextCallInsn(cUnit, mir, dInsn, callState,
803 rollback);
804 }
805 int outsOffset = (nextUse + 1) * 4;
806 if (rlArg.wide) {
807 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
808 nextUse += 2;
809 } else {
810 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
811 nextUse++;
812 }
buzbee561227c2011-09-02 15:28:19 -0700813 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700814 }
buzbee67bf8852011-08-17 17:51:35 -0700815 }
816
buzbeec0ecd652011-09-25 18:11:54 -0700817 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
818 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700819
buzbee67bf8852011-08-17 17:51:35 -0700820 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700821 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700822 }
823 return callState;
824}
825
826/*
827 * May have 0+ arguments (also used for jumbo). Note that
828 * source virtual registers may be in physical registers, so may
829 * need to be flushed to home location before copying. This
830 * applies to arg3 and above (see below).
831 *
832 * Two general strategies:
833 * If < 20 arguments
834 * Pass args 3-18 using vldm/vstm block copy
835 * Pass arg0, arg1 & arg2 in r1-r3
836 * If 20+ arguments
837 * Pass args arg19+ using memcpy block copy
838 * Pass arg0, arg1 & arg2 in r1-r3
839 *
840 */
buzbeeed3e9302011-09-23 17:34:19 -0700841STATIC int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700842 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700843 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700844 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700845{
846 int firstArg = dInsn->vC;
847 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700848
buzbee67bf8852011-08-17 17:51:35 -0700849 // If we can treat it as non-range (Jumbo ops will use range form)
850 if (numArgs <= 5)
851 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700852 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700853 /*
854 * Make sure range list doesn't span the break between in normal
855 * Dalvik vRegs and the ins.
856 */
buzbee1b4c8592011-08-31 10:43:51 -0700857 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700858 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700859 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
860 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700861 }
862
863 /*
864 * First load the non-register arguments. Both forms expect all
865 * of the source arguments to be in their home frame location, so
866 * scan the sReg names and flush any that have been promoted to
867 * frame backing storage.
868 */
869 // Scan the rest of the args - if in physReg flush to memory
buzbeec0ecd652011-09-25 18:11:54 -0700870 for (int nextArg = 0; nextArg < numArgs;) {
871 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
buzbee1b4c8592011-08-31 10:43:51 -0700872 if (loc.wide) {
873 loc = oatUpdateLocWide(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700874 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee1b4c8592011-08-31 10:43:51 -0700875 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
876 loc.highReg);
buzbee1b4c8592011-08-31 10:43:51 -0700877 }
buzbeec0ecd652011-09-25 18:11:54 -0700878 nextArg += 2;
buzbee1b4c8592011-08-31 10:43:51 -0700879 } else {
880 loc = oatUpdateLoc(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700881 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee1b4c8592011-08-31 10:43:51 -0700882 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
buzbee1b4c8592011-08-31 10:43:51 -0700883 }
buzbeec0ecd652011-09-25 18:11:54 -0700884 nextArg++;
buzbee67bf8852011-08-17 17:51:35 -0700885 }
886 }
887
888 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
889 int outsOffset = 4 /* Method* */ + (3 * 4);
890 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700891 // Generate memcpy
892 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
893 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700894 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
895 loadConstant(cUnit, r2, (numArgs - 3) * 4);
Ian Rogersff1ed472011-09-20 13:46:24 -0700896 callRuntimeHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700897 // Restore Method*
898 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700899 } else {
900 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700901 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700902 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700903 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700904 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
905 //TUNING: loosen barrier
906 ld->defMask = ENCODE_ALL;
907 setMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbee561227c2011-09-02 15:28:19 -0700908 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700909 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700910 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbeef48e9712011-09-15 17:54:28 -0700911 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
912 setMemRefType(st, false /* isLoad */, kDalvikReg);
913 st->defMask = ENCODE_ALL;
buzbee561227c2011-09-02 15:28:19 -0700914 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700915 }
916
buzbeec0ecd652011-09-25 18:11:54 -0700917 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
918 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700919
buzbee561227c2011-09-02 15:28:19 -0700920 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee99f27232011-10-05 12:56:36 -0700921 if (pcrLabel) {
922 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
923 }
buzbee67bf8852011-08-17 17:51:35 -0700924 return callState;
925}
926
buzbee2a475e72011-09-07 17:19:17 -0700927// Debugging routine - if null target, branch to DebugMe
buzbeeed3e9302011-09-23 17:34:19 -0700928STATIC void genShowTarget(CompilationUnit* cUnit)
buzbee2a475e72011-09-07 17:19:17 -0700929{
930 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
931 loadWordDisp(cUnit, rSELF,
932 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
933 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
934 target->defMask = -1;
935 branchOver->generic.target = (LIR*)target;
936}
buzbee2a475e72011-09-07 17:19:17 -0700937
buzbeeed3e9302011-09-23 17:34:19 -0700938STATIC void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700939 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700940{
941 DecodedInstruction* dInsn = &mir->dalvikInsn;
942 int callState = 0;
943 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700944 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700945 NextCallInsn nextCallInsn = nextSDCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700946 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee561227c2011-09-02 15:28:19 -0700947
buzbee109bd6a2011-09-06 13:58:41 -0700948 // Explicit register usage
949 oatLockCallTemps(cUnit);
950
buzbee99f27232011-10-05 12:56:36 -0700951 // Is this the special "Ljava/lang/Object;.<init>:()V" case?
952 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT) {
953 int idx = mir->dalvikInsn.vB;
954 Method* target = cUnit->method->GetDexCacheResolvedMethods()->Get(idx);
955 if (target) {
956 if (PrettyMethod(target) == "java.lang.Object.<init>()V") {
957 RegLocation rlArg = oatGetSrc(cUnit, mir, 0);
958 loadValueDirectFixed(cUnit, rlArg, r0);
959 loadWordDisp(cUnit, rSELF,
960 OFFSETOF_MEMBER(Thread, pObjectInit), rLR);
961 genNullCheck(cUnit, oatSSASrc(mir,0), r0, mir);
962 opReg(cUnit, kOpBlx, rLR);
963 oatClobberCalleeSave(cUnit);
964 return;
965 }
966 }
967 }
968
buzbee561227c2011-09-02 15:28:19 -0700969 if (range) {
970 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700971 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700972 } else {
973 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700974 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700975 }
buzbee67bf8852011-08-17 17:51:35 -0700976 // Finish up any of the call sequence not interleaved in arg loading
977 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700978 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700979 }
buzbeece302932011-10-04 14:32:18 -0700980 if (DISPLAY_MISSING_TARGETS) {
981 genShowTarget(cUnit);
982 }
buzbeeec5adf32011-09-11 15:25:43 -0700983 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -0700984 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700985}
986
buzbee4a3164f2011-09-03 11:25:10 -0700987/*
988 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
989 * which will locate the target and continue on via a tail call.
990 */
buzbeeed3e9302011-09-23 17:34:19 -0700991STATIC void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -0700992{
993 DecodedInstruction* dInsn = &mir->dalvikInsn;
994 int callState = 0;
995 ArmLIR* nullCk;
buzbeec0ecd652011-09-25 18:11:54 -0700996 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -0700997
998 // Explicit register usage
999 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001000 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -07001001 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001002 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
1003 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001004 false, nextInterfaceCallInsn, NULL,
buzbee367ce0b2011-09-14 23:19:50 -07001005 false);
buzbee67bf8852011-08-17 17:51:35 -07001006 else
1007 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee367ce0b2011-09-14 23:19:50 -07001008 nextInterfaceCallInsn, NULL, false);
buzbee67bf8852011-08-17 17:51:35 -07001009 // Finish up any of the call sequence not interleaved in arg loading
1010 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001011 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001012 }
buzbeece302932011-10-04 14:32:18 -07001013 if (DISPLAY_MISSING_TARGETS) {
1014 genShowTarget(cUnit);
1015 }
buzbeeec5adf32011-09-11 15:25:43 -07001016 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001017 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001018}
1019
buzbeeed3e9302011-09-23 17:34:19 -07001020STATIC void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001021{
1022 DecodedInstruction* dInsn = &mir->dalvikInsn;
1023 int callState = 0;
buzbee4a3164f2011-09-03 11:25:10 -07001024 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001025 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1026 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -07001027 NextCallInsn nextCallInsn;
1028 bool fastPath = true;
buzbeec0ecd652011-09-25 18:11:54 -07001029 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001030
1031 // Explicit register usage
1032 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001033 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
buzbee4a3164f2011-09-03 11:25:10 -07001034 fastPath = false;
1035 } else {
1036 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
1037 if (superClass == NULL) {
1038 fastPath = false;
1039 } else {
1040 int32_t target_idx = baseMethod->GetMethodIndex();
1041 if (superClass->GetVTable()->GetLength() <= target_idx) {
1042 fastPath = false;
1043 } else {
1044 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
1045 }
1046 }
1047 }
1048 if (fastPath) {
1049 nextCallInsn = nextSuperCallInsn;
1050 rollback = NULL;
1051 } else {
1052 nextCallInsn = nextSuperCallInsnSP;
1053 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1054 rollback->defMask = -1;
1055 }
buzbee67bf8852011-08-17 17:51:35 -07001056 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
buzbeec0ecd652011-09-25 18:11:54 -07001057 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001058 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001059 else
buzbeec0ecd652011-09-25 18:11:54 -07001060 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001061 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001062 // Finish up any of the call sequence not interleaved in arg loading
1063 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -07001064 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001065 }
buzbeece302932011-10-04 14:32:18 -07001066 if (DISPLAY_MISSING_TARGETS) {
1067 genShowTarget(cUnit);
1068 }
buzbeeec5adf32011-09-11 15:25:43 -07001069 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001070 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001071}
1072
buzbeeed3e9302011-09-23 17:34:19 -07001073STATIC void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001074{
1075 DecodedInstruction* dInsn = &mir->dalvikInsn;
1076 int callState = 0;
buzbee561227c2011-09-02 15:28:19 -07001077 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001078 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1079 Method* method = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -07001080 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -07001081 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee7b1b86d2011-08-26 18:59:10 -07001082
buzbee109bd6a2011-09-06 13:58:41 -07001083 // Explicit register usage
1084 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001085 if (SLOW_INVOKE_PATH || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -07001086 // Slow path
1087 nextCallInsn = nextVCallInsnSP;
1088 // If we need a slow-path callout, we'll restart here
1089 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1090 rollback->defMask = -1;
1091 } else {
1092 // Fast path
1093 nextCallInsn = nextVCallInsn;
1094 rollback = NULL;
1095 }
buzbee67bf8852011-08-17 17:51:35 -07001096 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
buzbeec0ecd652011-09-25 18:11:54 -07001097 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001098 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001099 else
buzbeec0ecd652011-09-25 18:11:54 -07001100 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001101 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001102 // Finish up any of the call sequence not interleaved in arg loading
1103 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001104 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001105 }
buzbeece302932011-10-04 14:32:18 -07001106 if (DISPLAY_MISSING_TARGETS) {
1107 genShowTarget(cUnit);
1108 }
buzbeeec5adf32011-09-11 15:25:43 -07001109 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001110 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001111}
1112
buzbeeed3e9302011-09-23 17:34:19 -07001113STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001114 BasicBlock* bb, ArmLIR* labelList)
1115{
1116 bool res = false; // Assume success
1117 RegLocation rlSrc[3];
1118 RegLocation rlDest = badLoc;
1119 RegLocation rlResult = badLoc;
1120 Opcode opcode = mir->dalvikInsn.opcode;
1121
1122 /* Prep Src and Dest locations */
1123 int nextSreg = 0;
1124 int nextLoc = 0;
1125 int attrs = oatDataFlowAttributes[opcode];
1126 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1127 if (attrs & DF_UA) {
1128 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1129 nextSreg++;
1130 } else if (attrs & DF_UA_WIDE) {
1131 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1132 nextSreg + 1);
1133 nextSreg+= 2;
1134 }
1135 if (attrs & DF_UB) {
1136 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1137 nextSreg++;
1138 } else if (attrs & DF_UB_WIDE) {
1139 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1140 nextSreg + 1);
1141 nextSreg+= 2;
1142 }
1143 if (attrs & DF_UC) {
1144 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1145 } else if (attrs & DF_UC_WIDE) {
1146 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1147 nextSreg + 1);
1148 }
1149 if (attrs & DF_DA) {
1150 rlDest = oatGetDest(cUnit, mir, 0);
1151 } else if (attrs & DF_DA_WIDE) {
1152 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1153 }
1154
1155 switch(opcode) {
1156 case OP_NOP:
1157 break;
1158
1159 case OP_MOVE_EXCEPTION:
1160 int exOffset;
1161 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001162 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001163 resetReg = oatAllocTemp(cUnit);
1164 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1165 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1166 loadConstant(cUnit, resetReg, 0);
1167 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1168 storeValue(cUnit, rlDest, rlResult);
1169 break;
1170
1171 case OP_RETURN_VOID:
buzbeec0ecd652011-09-25 18:11:54 -07001172 genSuspendPoll(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001173 break;
1174
1175 case OP_RETURN:
1176 case OP_RETURN_OBJECT:
buzbeec1f45042011-09-21 16:03:19 -07001177 genSuspendPoll(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001178 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001179 break;
1180
1181 case OP_RETURN_WIDE:
buzbeec1f45042011-09-21 16:03:19 -07001182 genSuspendPoll(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001183 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001184 break;
1185
1186 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -07001187 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001188 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001189 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001190 break;
1191
1192 case OP_MOVE_RESULT:
1193 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -07001194 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001195 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001196 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001197 break;
1198
1199 case OP_MOVE:
1200 case OP_MOVE_OBJECT:
1201 case OP_MOVE_16:
1202 case OP_MOVE_OBJECT_16:
1203 case OP_MOVE_FROM16:
1204 case OP_MOVE_OBJECT_FROM16:
1205 storeValue(cUnit, rlDest, rlSrc[0]);
1206 break;
1207
1208 case OP_MOVE_WIDE:
1209 case OP_MOVE_WIDE_16:
1210 case OP_MOVE_WIDE_FROM16:
1211 storeValueWide(cUnit, rlDest, rlSrc[0]);
1212 break;
1213
1214 case OP_CONST:
1215 case OP_CONST_4:
1216 case OP_CONST_16:
1217 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1218 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1219 storeValue(cUnit, rlDest, rlResult);
1220 break;
1221
1222 case OP_CONST_HIGH16:
1223 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1224 loadConstantNoClobber(cUnit, rlResult.lowReg,
1225 mir->dalvikInsn.vB << 16);
1226 storeValue(cUnit, rlDest, rlResult);
1227 break;
1228
1229 case OP_CONST_WIDE_16:
1230 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -07001231 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1232 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1233 mir->dalvikInsn.vB,
1234 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -07001235 storeValueWide(cUnit, rlDest, rlResult);
1236 break;
1237
1238 case OP_CONST_WIDE:
1239 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1240 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001241 mir->dalvikInsn.vB_wide & 0xffffffff,
1242 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001243 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001244 break;
1245
1246 case OP_CONST_WIDE_HIGH16:
1247 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1248 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1249 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001250 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001251 break;
1252
1253 case OP_MONITOR_ENTER:
1254 genMonitorEnter(cUnit, mir, rlSrc[0]);
1255 break;
1256
1257 case OP_MONITOR_EXIT:
1258 genMonitorExit(cUnit, mir, rlSrc[0]);
1259 break;
1260
1261 case OP_CHECK_CAST:
1262 genCheckCast(cUnit, mir, rlSrc[0]);
1263 break;
1264
1265 case OP_INSTANCE_OF:
1266 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1267 break;
1268
1269 case OP_NEW_INSTANCE:
1270 genNewInstance(cUnit, mir, rlDest);
1271 break;
1272
1273 case OP_THROW:
1274 genThrow(cUnit, mir, rlSrc[0]);
1275 break;
1276
buzbee5ade1d22011-09-09 14:44:52 -07001277 case OP_THROW_VERIFICATION_ERROR:
1278 loadWordDisp(cUnit, rSELF,
1279 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1280 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1281 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001282 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001283 break;
1284
buzbee67bf8852011-08-17 17:51:35 -07001285 case OP_ARRAY_LENGTH:
1286 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001287 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001288 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001289 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001290 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1291 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1292 rlResult.lowReg);
1293 storeValue(cUnit, rlDest, rlResult);
1294 break;
1295
1296 case OP_CONST_STRING:
1297 case OP_CONST_STRING_JUMBO:
1298 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1299 break;
1300
1301 case OP_CONST_CLASS:
1302 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1303 break;
1304
1305 case OP_FILL_ARRAY_DATA:
1306 genFillArrayData(cUnit, mir, rlSrc[0]);
1307 break;
1308
1309 case OP_FILLED_NEW_ARRAY:
1310 genFilledNewArray(cUnit, mir, false /* not range */);
1311 break;
1312
1313 case OP_FILLED_NEW_ARRAY_RANGE:
1314 genFilledNewArray(cUnit, mir, true /* range */);
1315 break;
1316
1317 case OP_NEW_ARRAY:
1318 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1319 break;
1320
1321 case OP_GOTO:
1322 case OP_GOTO_16:
1323 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001324 if (bb->taken->startOffset <= mir->offset) {
1325 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001326 }
1327 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1328 break;
1329
1330 case OP_PACKED_SWITCH:
1331 genPackedSwitch(cUnit, mir, rlSrc[0]);
1332 break;
1333
1334 case OP_SPARSE_SWITCH:
1335 genSparseSwitch(cUnit, mir, rlSrc[0]);
1336 break;
1337
1338 case OP_CMPL_FLOAT:
1339 case OP_CMPG_FLOAT:
1340 case OP_CMPL_DOUBLE:
1341 case OP_CMPG_DOUBLE:
1342 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1343 break;
1344
1345 case OP_CMP_LONG:
1346 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1347 break;
1348
1349 case OP_IF_EQ:
1350 case OP_IF_NE:
1351 case OP_IF_LT:
1352 case OP_IF_GE:
1353 case OP_IF_GT:
1354 case OP_IF_LE: {
1355 bool backwardBranch;
1356 ArmConditionCode cond;
1357 backwardBranch = (bb->taken->startOffset <= mir->offset);
1358 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001359 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001360 }
1361 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1362 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1363 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1364 switch(opcode) {
1365 case OP_IF_EQ:
1366 cond = kArmCondEq;
1367 break;
1368 case OP_IF_NE:
1369 cond = kArmCondNe;
1370 break;
1371 case OP_IF_LT:
1372 cond = kArmCondLt;
1373 break;
1374 case OP_IF_GE:
1375 cond = kArmCondGe;
1376 break;
1377 case OP_IF_GT:
1378 cond = kArmCondGt;
1379 break;
1380 case OP_IF_LE:
1381 cond = kArmCondLe;
1382 break;
1383 default:
1384 cond = (ArmConditionCode)0;
1385 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1386 }
1387 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1388 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1389 break;
1390 }
1391
1392 case OP_IF_EQZ:
1393 case OP_IF_NEZ:
1394 case OP_IF_LTZ:
1395 case OP_IF_GEZ:
1396 case OP_IF_GTZ:
1397 case OP_IF_LEZ: {
1398 bool backwardBranch;
1399 ArmConditionCode cond;
1400 backwardBranch = (bb->taken->startOffset <= mir->offset);
1401 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001402 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001403 }
1404 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1405 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1406 switch(opcode) {
1407 case OP_IF_EQZ:
1408 cond = kArmCondEq;
1409 break;
1410 case OP_IF_NEZ:
1411 cond = kArmCondNe;
1412 break;
1413 case OP_IF_LTZ:
1414 cond = kArmCondLt;
1415 break;
1416 case OP_IF_GEZ:
1417 cond = kArmCondGe;
1418 break;
1419 case OP_IF_GTZ:
1420 cond = kArmCondGt;
1421 break;
1422 case OP_IF_LEZ:
1423 cond = kArmCondLe;
1424 break;
1425 default:
1426 cond = (ArmConditionCode)0;
1427 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1428 }
1429 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1430 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1431 break;
1432 }
1433
1434 case OP_AGET_WIDE:
1435 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1436 break;
1437 case OP_AGET:
1438 case OP_AGET_OBJECT:
1439 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1440 break;
1441 case OP_AGET_BOOLEAN:
1442 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1443 rlDest, 0);
1444 break;
1445 case OP_AGET_BYTE:
1446 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1447 break;
1448 case OP_AGET_CHAR:
1449 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1450 rlDest, 1);
1451 break;
1452 case OP_AGET_SHORT:
1453 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1454 break;
1455 case OP_APUT_WIDE:
1456 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1457 break;
1458 case OP_APUT:
1459 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1460 break;
1461 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001462 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001463 break;
1464 case OP_APUT_SHORT:
1465 case OP_APUT_CHAR:
1466 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1467 rlSrc[0], 1);
1468 break;
1469 case OP_APUT_BYTE:
1470 case OP_APUT_BOOLEAN:
1471 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1472 rlSrc[0], 0);
1473 break;
1474
1475 case OP_IGET_WIDE:
1476 case OP_IGET_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001477 genIGetWide(cUnit, mir, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001478 break;
1479
1480 case OP_IGET:
1481 case OP_IGET_VOLATILE:
1482 case OP_IGET_OBJECT:
1483 case OP_IGET_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001484 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001485 break;
1486
1487 case OP_IGET_BOOLEAN:
1488 case OP_IGET_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001489 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001490 break;
1491
1492 case OP_IGET_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001493 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001494 break;
1495
1496 case OP_IGET_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001497 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001498 break;
1499
1500 case OP_IPUT_WIDE:
1501 case OP_IPUT_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001502 genIPutWide(cUnit, mir, rlSrc[0], rlSrc[1]);
buzbee67bf8852011-08-17 17:51:35 -07001503 break;
1504
1505 case OP_IPUT_OBJECT:
1506 case OP_IPUT_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001507 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
buzbee67bf8852011-08-17 17:51:35 -07001508 break;
1509
1510 case OP_IPUT:
1511 case OP_IPUT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001512 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001513 break;
1514
1515 case OP_IPUT_BOOLEAN:
1516 case OP_IPUT_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001517 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001518 break;
1519
1520 case OP_IPUT_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001521 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001522 break;
1523
1524 case OP_IPUT_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001525 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001526 break;
1527
1528 case OP_SGET:
1529 case OP_SGET_OBJECT:
1530 case OP_SGET_BOOLEAN:
1531 case OP_SGET_BYTE:
1532 case OP_SGET_CHAR:
1533 case OP_SGET_SHORT:
1534 genSget(cUnit, mir, rlResult, rlDest);
1535 break;
1536
1537 case OP_SGET_WIDE:
1538 genSgetWide(cUnit, mir, rlResult, rlDest);
1539 break;
1540
1541 case OP_SPUT:
1542 case OP_SPUT_OBJECT:
1543 case OP_SPUT_BOOLEAN:
1544 case OP_SPUT_BYTE:
1545 case OP_SPUT_CHAR:
1546 case OP_SPUT_SHORT:
1547 genSput(cUnit, mir, rlSrc[0]);
1548 break;
1549
1550 case OP_SPUT_WIDE:
1551 genSputWide(cUnit, mir, rlSrc[0]);
1552 break;
1553
1554 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001555 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1556 true /*range*/);
1557 break;
buzbee67bf8852011-08-17 17:51:35 -07001558 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001559 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1560 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001561 break;
1562
1563 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001564 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1565 false /*range*/);
1566 break;
buzbee67bf8852011-08-17 17:51:35 -07001567 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001568 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1569 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001570 break;
1571
1572 case OP_INVOKE_VIRTUAL:
1573 case OP_INVOKE_VIRTUAL_RANGE:
1574 genInvokeVirtual(cUnit, mir);
1575 break;
1576
1577 case OP_INVOKE_SUPER:
1578 case OP_INVOKE_SUPER_RANGE:
1579 genInvokeSuper(cUnit, mir);
1580 break;
1581
1582 case OP_INVOKE_INTERFACE:
1583 case OP_INVOKE_INTERFACE_RANGE:
1584 genInvokeInterface(cUnit, mir);
1585 break;
1586
1587 case OP_NEG_INT:
1588 case OP_NOT_INT:
1589 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1590 break;
1591
1592 case OP_NEG_LONG:
1593 case OP_NOT_LONG:
1594 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1595 break;
1596
1597 case OP_NEG_FLOAT:
1598 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1599 break;
1600
1601 case OP_NEG_DOUBLE:
1602 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1603 break;
1604
1605 case OP_INT_TO_LONG:
1606 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1607 if (rlSrc[0].location == kLocPhysReg) {
1608 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1609 } else {
1610 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1611 }
1612 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1613 rlResult.lowReg, 31);
1614 storeValueWide(cUnit, rlDest, rlResult);
1615 break;
1616
1617 case OP_LONG_TO_INT:
1618 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1619 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1620 storeValue(cUnit, rlDest, rlSrc[0]);
1621 break;
1622
1623 case OP_INT_TO_BYTE:
1624 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1625 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1626 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1627 storeValue(cUnit, rlDest, rlResult);
1628 break;
1629
1630 case OP_INT_TO_SHORT:
1631 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1632 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1633 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1634 storeValue(cUnit, rlDest, rlResult);
1635 break;
1636
1637 case OP_INT_TO_CHAR:
1638 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1639 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1640 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1641 storeValue(cUnit, rlDest, rlResult);
1642 break;
1643
1644 case OP_INT_TO_FLOAT:
1645 case OP_INT_TO_DOUBLE:
1646 case OP_LONG_TO_FLOAT:
1647 case OP_LONG_TO_DOUBLE:
1648 case OP_FLOAT_TO_INT:
1649 case OP_FLOAT_TO_LONG:
1650 case OP_FLOAT_TO_DOUBLE:
1651 case OP_DOUBLE_TO_INT:
1652 case OP_DOUBLE_TO_LONG:
1653 case OP_DOUBLE_TO_FLOAT:
1654 genConversion(cUnit, mir);
1655 break;
1656
1657 case OP_ADD_INT:
1658 case OP_SUB_INT:
1659 case OP_MUL_INT:
1660 case OP_DIV_INT:
1661 case OP_REM_INT:
1662 case OP_AND_INT:
1663 case OP_OR_INT:
1664 case OP_XOR_INT:
1665 case OP_SHL_INT:
1666 case OP_SHR_INT:
1667 case OP_USHR_INT:
1668 case OP_ADD_INT_2ADDR:
1669 case OP_SUB_INT_2ADDR:
1670 case OP_MUL_INT_2ADDR:
1671 case OP_DIV_INT_2ADDR:
1672 case OP_REM_INT_2ADDR:
1673 case OP_AND_INT_2ADDR:
1674 case OP_OR_INT_2ADDR:
1675 case OP_XOR_INT_2ADDR:
1676 case OP_SHL_INT_2ADDR:
1677 case OP_SHR_INT_2ADDR:
1678 case OP_USHR_INT_2ADDR:
1679 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1680 break;
1681
1682 case OP_ADD_LONG:
1683 case OP_SUB_LONG:
1684 case OP_MUL_LONG:
1685 case OP_DIV_LONG:
1686 case OP_REM_LONG:
1687 case OP_AND_LONG:
1688 case OP_OR_LONG:
1689 case OP_XOR_LONG:
1690 case OP_ADD_LONG_2ADDR:
1691 case OP_SUB_LONG_2ADDR:
1692 case OP_MUL_LONG_2ADDR:
1693 case OP_DIV_LONG_2ADDR:
1694 case OP_REM_LONG_2ADDR:
1695 case OP_AND_LONG_2ADDR:
1696 case OP_OR_LONG_2ADDR:
1697 case OP_XOR_LONG_2ADDR:
1698 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1699 break;
1700
buzbee67bf8852011-08-17 17:51:35 -07001701 case OP_SHL_LONG:
1702 case OP_SHR_LONG:
1703 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001704 case OP_SHL_LONG_2ADDR:
1705 case OP_SHR_LONG_2ADDR:
1706 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001707 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1708 break;
1709
1710 case OP_ADD_FLOAT:
1711 case OP_SUB_FLOAT:
1712 case OP_MUL_FLOAT:
1713 case OP_DIV_FLOAT:
1714 case OP_REM_FLOAT:
1715 case OP_ADD_FLOAT_2ADDR:
1716 case OP_SUB_FLOAT_2ADDR:
1717 case OP_MUL_FLOAT_2ADDR:
1718 case OP_DIV_FLOAT_2ADDR:
1719 case OP_REM_FLOAT_2ADDR:
1720 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1721 break;
1722
1723 case OP_ADD_DOUBLE:
1724 case OP_SUB_DOUBLE:
1725 case OP_MUL_DOUBLE:
1726 case OP_DIV_DOUBLE:
1727 case OP_REM_DOUBLE:
1728 case OP_ADD_DOUBLE_2ADDR:
1729 case OP_SUB_DOUBLE_2ADDR:
1730 case OP_MUL_DOUBLE_2ADDR:
1731 case OP_DIV_DOUBLE_2ADDR:
1732 case OP_REM_DOUBLE_2ADDR:
1733 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1734 break;
1735
1736 case OP_RSUB_INT:
1737 case OP_ADD_INT_LIT16:
1738 case OP_MUL_INT_LIT16:
1739 case OP_DIV_INT_LIT16:
1740 case OP_REM_INT_LIT16:
1741 case OP_AND_INT_LIT16:
1742 case OP_OR_INT_LIT16:
1743 case OP_XOR_INT_LIT16:
1744 case OP_ADD_INT_LIT8:
1745 case OP_RSUB_INT_LIT8:
1746 case OP_MUL_INT_LIT8:
1747 case OP_DIV_INT_LIT8:
1748 case OP_REM_INT_LIT8:
1749 case OP_AND_INT_LIT8:
1750 case OP_OR_INT_LIT8:
1751 case OP_XOR_INT_LIT8:
1752 case OP_SHL_INT_LIT8:
1753 case OP_SHR_INT_LIT8:
1754 case OP_USHR_INT_LIT8:
1755 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1756 break;
1757
1758 default:
1759 res = true;
1760 }
1761 return res;
1762}
1763
buzbeeed3e9302011-09-23 17:34:19 -07001764STATIC const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001765 "kMirOpPhi",
1766 "kMirOpNullNRangeUpCheck",
1767 "kMirOpNullNRangeDownCheck",
1768 "kMirOpLowerBound",
1769 "kMirOpPunt",
1770 "kMirOpCheckInlinePrediction",
1771};
1772
1773/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001774STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001775{
1776 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1777 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1778 strcpy(msg, extendedMIROpNames[opOffset]);
1779 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1780
1781 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1782 case kMirOpPhi: {
1783 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1784 op->flags.isNop = true;
1785 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1786 break;
1787 }
1788 default:
1789 break;
1790 }
1791}
1792
1793/* If there are any ins passed in registers that have not been promoted
1794 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001795 * Note: at this pointCopy any ins that are passed in register to their
1796 * home location */
buzbeeed3e9302011-09-23 17:34:19 -07001797STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001798{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001799 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001800 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001801 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1802 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001803 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804 int startLoc = cUnit->method->NumRegisters() -
1805 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001806 for (int i = 0; i < inRegs; i++) {
1807 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001808 //TUNING: be smarter about flushing ins to frame
1809 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001810 if (loc.location == kLocPhysReg) {
1811 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001812 }
1813 }
1814
1815 // Handle special case of wide argument half in regs, half in frame
1816 if (inRegs == 3) {
1817 RegLocation loc = cUnit->regLocation[startLoc + 2];
1818 if (loc.wide && loc.location == kLocPhysReg) {
1819 // Load the other half of the arg into the promoted pair
buzbee561227c2011-09-02 15:28:19 -07001820 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -07001821 inRegs++;
1822 }
1823 }
1824
1825 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001826 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001827 RegLocation loc = cUnit->regLocation[startLoc + i];
1828 if (loc.fpLocation == kLocPhysReg) {
1829 loc.location = kLocPhysReg;
1830 loc.fp = true;
1831 loc.lowReg = loc.fpLowReg;
1832 loc.highReg = loc.fpHighReg;
1833 }
1834 if (loc.location == kLocPhysReg) {
1835 if (loc.wide) {
buzbeeb29e4d12011-09-26 15:05:48 -07001836 if (loc.fp && (loc.lowReg & 1) != 0) {
1837 // Misaligned - need to load as a pair of singles
1838 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
1839 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
1840 } else {
1841 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1842 loc.lowReg, loc.highReg, INVALID_SREG);
1843 }
buzbee67bf8852011-08-17 17:51:35 -07001844 i++;
1845 } else {
buzbee561227c2011-09-02 15:28:19 -07001846 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001847 }
1848 }
1849 i++;
1850 }
1851}
1852
1853/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001854STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001855{
1856 MIR* mir;
1857 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1858 int blockId = bb->id;
1859
1860 cUnit->curBlock = bb;
1861 labelList[blockId].operands[0] = bb->startOffset;
1862
1863 /* Insert the block label */
1864 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1865 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1866
buzbee6181f792011-09-29 11:14:04 -07001867 /* Reset local optimization data on block boundaries */
1868 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001869 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001870 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001871
1872 ArmLIR* headLIR = NULL;
1873
buzbeebbaf8942011-10-02 13:08:29 -07001874 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001875 if (bb->blockType == kEntryBlock) {
1876 /*
1877 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1878 * mechanism know so it doesn't try to use any of them when
1879 * expanding the frame or flushing. This leaves the utility
1880 * code with a single temp: r12. This should be enough.
1881 */
1882 oatLockTemp(cUnit, r0);
1883 oatLockTemp(cUnit, r1);
1884 oatLockTemp(cUnit, r2);
1885 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001886
1887 /*
1888 * We can safely skip the stack overflow check if we're
1889 * a leaf *and* our frame size < fudge factor.
1890 */
1891 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1892 ((size_t)cUnit->frameSize <
1893 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001894 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001895 if (!skipOverflowCheck) {
1896 /* Load stack limit */
1897 loadWordDisp(cUnit, rSELF,
1898 art::Thread::StackEndOffset().Int32Value(), r12);
1899 }
buzbee67bf8852011-08-17 17:51:35 -07001900 /* Spill core callee saves */
1901 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1902 /* Need to spill any FP regs? */
1903 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001904 /*
1905 * NOTE: fp spills are a little different from core spills in that
1906 * they are pushed as a contiguous block. When promoting from
1907 * the fp set, we must allocate all singles from s16..highest-promoted
1908 */
buzbee67bf8852011-08-17 17:51:35 -07001909 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1910 }
buzbeecefd1872011-09-09 09:59:52 -07001911 if (!skipOverflowCheck) {
1912 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001913 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001914 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1915 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001916 genRegCopy(cUnit, rSP, rLR); // Establish stack
1917 } else {
1918 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001919 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001920 }
buzbee67bf8852011-08-17 17:51:35 -07001921 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1922 flushIns(cUnit);
1923 oatFreeTemp(cUnit, r0);
1924 oatFreeTemp(cUnit, r1);
1925 oatFreeTemp(cUnit, r2);
1926 oatFreeTemp(cUnit, r3);
1927 } else if (bb->blockType == kExitBlock) {
1928 newLIR0(cUnit, kArmPseudoMethodExit);
buzbeebbaf8942011-10-02 13:08:29 -07001929 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07001930 /* Need to restore any FP callee saves? */
1931 if (cUnit->numFPSpills) {
1932 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1933 }
1934 if (cUnit->coreSpillMask & (1 << rLR)) {
1935 /* Unspill rLR to rPC */
1936 cUnit->coreSpillMask &= ~(1 << rLR);
1937 cUnit->coreSpillMask |= (1 << rPC);
1938 }
1939 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1940 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1941 /* We didn't pop to rPC, so must do a bv rLR */
1942 newLIR1(cUnit, kThumbBx, rLR);
1943 }
1944 }
1945
1946 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1947
1948 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07001949 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1950 oatClobberAllRegs(cUnit);
1951 }
buzbee67bf8852011-08-17 17:51:35 -07001952
1953 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1954 oatResetDefTracking(cUnit);
1955 }
1956
1957 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1958 handleExtendedMethodMIR(cUnit, mir);
1959 continue;
1960 }
1961
1962 cUnit->currentDalvikOffset = mir->offset;
1963
1964 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1965 InstructionFormat dalvikFormat =
1966 dexGetFormatFromOpcode(dalvikOpcode);
1967
1968 ArmLIR* boundaryLIR;
1969
1970 /* Mark the beginning of a Dalvik instruction for line tracking */
1971 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1972 (int) oatGetDalvikDisassembly(
1973 &mir->dalvikInsn, ""));
1974 /* Remember the first LIR for this block */
1975 if (headLIR == NULL) {
1976 headLIR = boundaryLIR;
1977 /* Set the first boundaryLIR as a scheduling barrier */
1978 headLIR->defMask = ENCODE_ALL;
1979 }
1980
1981 /* Don't generate the SSA annotation unless verbose mode is on */
1982 if (cUnit->printMe && mir->ssaRep) {
1983 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1984 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1985 }
1986
1987 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1988
1989 if (notHandled) {
1990 char buf[100];
1991 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1992 mir->offset,
1993 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1994 dalvikFormat);
1995 LOG(FATAL) << buf;
1996 }
1997 }
1998
1999 if (headLIR) {
2000 /*
2001 * Eliminate redundant loads/stores and delay stores into later
2002 * slots
2003 */
2004 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
2005 cUnit->lastLIRInsn);
2006
2007 /*
2008 * Generate an unconditional branch to the fallthrough block.
2009 */
2010 if (bb->fallThrough) {
2011 genUnconditionalBranch(cUnit,
2012 &labelList[bb->fallThrough->id]);
2013 }
2014 }
2015 return false;
2016}
2017
2018/*
2019 * Nop any unconditional branches that go to the next instruction.
2020 * Note: new redundant branches may be inserted later, and we'll
2021 * use a check in final instruction assembly to nop those out.
2022 */
2023void removeRedundantBranches(CompilationUnit* cUnit)
2024{
2025 ArmLIR* thisLIR;
2026
2027 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
2028 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
2029 thisLIR = NEXT_LIR(thisLIR)) {
2030
2031 /* Branch to the next instruction */
2032 if ((thisLIR->opcode == kThumbBUncond) ||
2033 (thisLIR->opcode == kThumb2BUncond)) {
2034 ArmLIR* nextLIR = thisLIR;
2035
2036 while (true) {
2037 nextLIR = NEXT_LIR(nextLIR);
2038
2039 /*
2040 * Is the branch target the next instruction?
2041 */
2042 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
2043 thisLIR->flags.isNop = true;
2044 break;
2045 }
2046
2047 /*
2048 * Found real useful stuff between the branch and the target.
2049 * Need to explicitly check the lastLIRInsn here because it
2050 * might be the last real instruction.
2051 */
2052 if (!isPseudoOpcode(nextLIR->opcode) ||
2053 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
2054 break;
2055 }
2056 }
2057 }
2058}
2059
buzbeeed3e9302011-09-23 17:34:19 -07002060STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07002061{
2062 ArmLIR** suspendLabel =
2063 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
2064 int numElems = cUnit->suspendLaunchpads.numUsed;
2065
2066 for (int i = 0; i < numElems; i++) {
2067 /* TUNING: move suspend count load into helper */
2068 ArmLIR* lab = suspendLabel[i];
2069 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
2070 cUnit->currentDalvikOffset = lab->operands[1];
2071 oatAppendLIR(cUnit, (LIR *)lab);
2072 loadWordDisp(cUnit, rSELF,
2073 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
2074 loadWordDisp(cUnit, rSELF,
2075 art::Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
2076 opReg(cUnit, kOpBlx, rLR);
2077 genUnconditionalBranch(cUnit, resumeLab);
2078 }
2079}
2080
buzbeeed3e9302011-09-23 17:34:19 -07002081STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07002082{
2083 ArmLIR** throwLabel =
2084 (ArmLIR **) cUnit->throwLaunchpads.elemList;
2085 int numElems = cUnit->throwLaunchpads.numUsed;
2086 int i;
2087
2088 for (i = 0; i < numElems; i++) {
2089 ArmLIR* lab = throwLabel[i];
2090 cUnit->currentDalvikOffset = lab->operands[1];
2091 oatAppendLIR(cUnit, (LIR *)lab);
2092 int funcOffset = 0;
2093 int v1 = lab->operands[2];
2094 int v2 = lab->operands[3];
2095 switch(lab->operands[0]) {
2096 case kArmThrowNullPointer:
2097 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2098 break;
2099 case kArmThrowArrayBounds:
2100 if (v2 != r0) {
2101 genRegCopy(cUnit, r0, v1);
2102 genRegCopy(cUnit, r1, v2);
2103 } else {
2104 if (v1 == r1) {
2105 genRegCopy(cUnit, r12, v1);
2106 genRegCopy(cUnit, r1, v2);
2107 genRegCopy(cUnit, r0, r12);
2108 } else {
2109 genRegCopy(cUnit, r1, v2);
2110 genRegCopy(cUnit, r0, v1);
2111 }
2112 }
2113 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2114 break;
2115 case kArmThrowDivZero:
2116 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2117 break;
2118 case kArmThrowVerificationError:
2119 loadConstant(cUnit, r0, v1);
2120 loadConstant(cUnit, r1, v2);
2121 funcOffset =
2122 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2123 break;
2124 case kArmThrowNegArraySize:
2125 genRegCopy(cUnit, r0, v1);
2126 funcOffset =
2127 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2128 break;
buzbee5ade1d22011-09-09 14:44:52 -07002129 case kArmThrowNoSuchMethod:
2130 genRegCopy(cUnit, r0, v1);
2131 funcOffset =
2132 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2133 break;
buzbeeec5adf32011-09-11 15:25:43 -07002134 case kArmThrowStackOverflow:
2135 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07002136 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07002137 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07002138 opRegImm(cUnit, kOpAdd, rSP,
2139 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07002140 break;
buzbee5ade1d22011-09-09 14:44:52 -07002141 default:
2142 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2143 }
2144 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07002145 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07002146 }
2147}
2148
buzbee67bf8852011-08-17 17:51:35 -07002149void oatMethodMIR2LIR(CompilationUnit* cUnit)
2150{
2151 /* Used to hold the labels of each block */
2152 cUnit->blockLabelList =
2153 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2154
2155 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2156 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07002157 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002158
2159 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07002160
2161 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002162}
2163
2164/* Common initialization routine for an architecture family */
2165bool oatArchInit()
2166{
2167 int i;
2168
2169 for (i = 0; i < kArmLast; i++) {
2170 if (EncodingMap[i].opcode != i) {
2171 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2172 " is wrong: expecting " << i << ", seeing " <<
2173 (int)EncodingMap[i].opcode;
2174 }
2175 }
2176
2177 return oatArchVariantInit();
2178}
2179
2180/* Needed by the Assembler */
2181void oatSetupResourceMasks(ArmLIR* lir)
2182{
2183 setupResourceMasks(lir);
2184}
2185
2186/* Needed by the ld/st optmizatons */
2187ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2188{
2189 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2190}
2191
2192/* Needed by the register allocator */
2193ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2194{
2195 return genRegCopy(cUnit, rDest, rSrc);
2196}
2197
2198/* Needed by the register allocator */
2199void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2200 int srcLo, int srcHi)
2201{
2202 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2203}
2204
2205void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2206 int displacement, int rSrc, OpSize size)
2207{
2208 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2209}
2210
2211void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2212 int displacement, int rSrcLo, int rSrcHi)
2213{
2214 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2215}