blob: 922e25b4e6a442cb149110e6f0f10ad0400ede74 [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
buzbee6a0f7f52011-09-05 16:14:20 -0700820 //TODO: better to move this into CallInsn lists
buzbee67bf8852011-08-17 17:51:35 -0700821 // Load direct & need a "this" null check?
822 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700823 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700824 }
825 return callState;
826}
827
828/*
829 * May have 0+ arguments (also used for jumbo). Note that
830 * source virtual registers may be in physical registers, so may
831 * need to be flushed to home location before copying. This
832 * applies to arg3 and above (see below).
833 *
834 * Two general strategies:
835 * If < 20 arguments
836 * Pass args 3-18 using vldm/vstm block copy
837 * Pass arg0, arg1 & arg2 in r1-r3
838 * If 20+ arguments
839 * Pass args arg19+ using memcpy block copy
840 * Pass arg0, arg1 & arg2 in r1-r3
841 *
842 */
buzbeeed3e9302011-09-23 17:34:19 -0700843STATIC int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700844 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700845 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700846 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700847{
848 int firstArg = dInsn->vC;
849 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700850
buzbee67bf8852011-08-17 17:51:35 -0700851 // If we can treat it as non-range (Jumbo ops will use range form)
852 if (numArgs <= 5)
853 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700854 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700855 /*
856 * Make sure range list doesn't span the break between in normal
857 * Dalvik vRegs and the ins.
858 */
buzbee1b4c8592011-08-31 10:43:51 -0700859 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700861 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
862 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700863 }
864
865 /*
866 * First load the non-register arguments. Both forms expect all
867 * of the source arguments to be in their home frame location, so
868 * scan the sReg names and flush any that have been promoted to
869 * frame backing storage.
870 */
871 // Scan the rest of the args - if in physReg flush to memory
buzbeec0ecd652011-09-25 18:11:54 -0700872 for (int nextArg = 0; nextArg < numArgs;) {
873 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
buzbee1b4c8592011-08-31 10:43:51 -0700874 if (loc.wide) {
875 loc = oatUpdateLocWide(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700876 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee1b4c8592011-08-31 10:43:51 -0700877 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
878 loc.highReg);
buzbee1b4c8592011-08-31 10:43:51 -0700879 }
buzbeec0ecd652011-09-25 18:11:54 -0700880 nextArg += 2;
buzbee1b4c8592011-08-31 10:43:51 -0700881 } else {
882 loc = oatUpdateLoc(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700883 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee1b4c8592011-08-31 10:43:51 -0700884 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
buzbee1b4c8592011-08-31 10:43:51 -0700885 }
buzbeec0ecd652011-09-25 18:11:54 -0700886 nextArg++;
buzbee67bf8852011-08-17 17:51:35 -0700887 }
888 }
889
890 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
891 int outsOffset = 4 /* Method* */ + (3 * 4);
892 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700893 // Generate memcpy
894 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
895 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700896 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
897 loadConstant(cUnit, r2, (numArgs - 3) * 4);
Ian Rogersff1ed472011-09-20 13:46:24 -0700898 callRuntimeHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700899 // Restore Method*
900 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700901 } else {
902 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700903 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700904 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700905 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700906 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
907 //TUNING: loosen barrier
908 ld->defMask = ENCODE_ALL;
909 setMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbee561227c2011-09-02 15:28:19 -0700910 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700911 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700912 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbeef48e9712011-09-15 17:54:28 -0700913 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
914 setMemRefType(st, false /* isLoad */, kDalvikReg);
915 st->defMask = ENCODE_ALL;
buzbee561227c2011-09-02 15:28:19 -0700916 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700917 }
918
buzbeec0ecd652011-09-25 18:11:54 -0700919 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
920 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700921
buzbee561227c2011-09-02 15:28:19 -0700922 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700923 return callState;
924}
925
buzbee2a475e72011-09-07 17:19:17 -0700926// Debugging routine - if null target, branch to DebugMe
buzbeeed3e9302011-09-23 17:34:19 -0700927STATIC void genShowTarget(CompilationUnit* cUnit)
buzbee2a475e72011-09-07 17:19:17 -0700928{
929 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
930 loadWordDisp(cUnit, rSELF,
931 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
932 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
933 target->defMask = -1;
934 branchOver->generic.target = (LIR*)target;
935}
buzbee2a475e72011-09-07 17:19:17 -0700936
buzbeeed3e9302011-09-23 17:34:19 -0700937STATIC void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700938 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700939{
940 DecodedInstruction* dInsn = &mir->dalvikInsn;
941 int callState = 0;
942 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700943 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700944 NextCallInsn nextCallInsn = nextSDCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700945 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee561227c2011-09-02 15:28:19 -0700946
buzbee109bd6a2011-09-06 13:58:41 -0700947 // Explicit register usage
948 oatLockCallTemps(cUnit);
949
buzbee561227c2011-09-02 15:28:19 -0700950 if (range) {
951 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700952 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700953 } else {
954 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700955 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700956 }
buzbee67bf8852011-08-17 17:51:35 -0700957 // Finish up any of the call sequence not interleaved in arg loading
958 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700959 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700960 }
buzbeece302932011-10-04 14:32:18 -0700961 if (DISPLAY_MISSING_TARGETS) {
962 genShowTarget(cUnit);
963 }
buzbeeec5adf32011-09-11 15:25:43 -0700964 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -0700965 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700966}
967
buzbee4a3164f2011-09-03 11:25:10 -0700968/*
969 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
970 * which will locate the target and continue on via a tail call.
971 */
buzbeeed3e9302011-09-23 17:34:19 -0700972STATIC void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -0700973{
974 DecodedInstruction* dInsn = &mir->dalvikInsn;
975 int callState = 0;
976 ArmLIR* nullCk;
buzbeec0ecd652011-09-25 18:11:54 -0700977 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -0700978
979 // Explicit register usage
980 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700981 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -0700982 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700983 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
984 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700985 false, nextInterfaceCallInsn, NULL,
buzbee367ce0b2011-09-14 23:19:50 -0700986 false);
buzbee67bf8852011-08-17 17:51:35 -0700987 else
988 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee367ce0b2011-09-14 23:19:50 -0700989 nextInterfaceCallInsn, NULL, false);
buzbee67bf8852011-08-17 17:51:35 -0700990 // Finish up any of the call sequence not interleaved in arg loading
991 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700992 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700993 }
buzbeece302932011-10-04 14:32:18 -0700994 if (DISPLAY_MISSING_TARGETS) {
995 genShowTarget(cUnit);
996 }
buzbeeec5adf32011-09-11 15:25:43 -0700997 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -0700998 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700999}
1000
buzbeeed3e9302011-09-23 17:34:19 -07001001STATIC void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001002{
1003 DecodedInstruction* dInsn = &mir->dalvikInsn;
1004 int callState = 0;
buzbee4a3164f2011-09-03 11:25:10 -07001005 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001006 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1007 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -07001008 NextCallInsn nextCallInsn;
1009 bool fastPath = true;
buzbeec0ecd652011-09-25 18:11:54 -07001010 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001011
1012 // Explicit register usage
1013 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001014 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
buzbee4a3164f2011-09-03 11:25:10 -07001015 fastPath = false;
1016 } else {
1017 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
1018 if (superClass == NULL) {
1019 fastPath = false;
1020 } else {
1021 int32_t target_idx = baseMethod->GetMethodIndex();
1022 if (superClass->GetVTable()->GetLength() <= target_idx) {
1023 fastPath = false;
1024 } else {
1025 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
1026 }
1027 }
1028 }
1029 if (fastPath) {
1030 nextCallInsn = nextSuperCallInsn;
1031 rollback = NULL;
1032 } else {
1033 nextCallInsn = nextSuperCallInsnSP;
1034 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1035 rollback->defMask = -1;
1036 }
buzbee67bf8852011-08-17 17:51:35 -07001037 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
buzbeec0ecd652011-09-25 18:11:54 -07001038 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001039 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001040 else
buzbeec0ecd652011-09-25 18:11:54 -07001041 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001042 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001043 // Finish up any of the call sequence not interleaved in arg loading
1044 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -07001045 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001046 }
buzbeece302932011-10-04 14:32:18 -07001047 if (DISPLAY_MISSING_TARGETS) {
1048 genShowTarget(cUnit);
1049 }
buzbeeec5adf32011-09-11 15:25:43 -07001050 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001051 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001052}
1053
buzbeeed3e9302011-09-23 17:34:19 -07001054STATIC void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001055{
1056 DecodedInstruction* dInsn = &mir->dalvikInsn;
1057 int callState = 0;
buzbee561227c2011-09-02 15:28:19 -07001058 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001059 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1060 Method* method = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -07001061 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -07001062 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee7b1b86d2011-08-26 18:59:10 -07001063
buzbee109bd6a2011-09-06 13:58:41 -07001064 // Explicit register usage
1065 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001066 if (SLOW_INVOKE_PATH || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -07001067 // Slow path
1068 nextCallInsn = nextVCallInsnSP;
1069 // If we need a slow-path callout, we'll restart here
1070 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1071 rollback->defMask = -1;
1072 } else {
1073 // Fast path
1074 nextCallInsn = nextVCallInsn;
1075 rollback = NULL;
1076 }
buzbee67bf8852011-08-17 17:51:35 -07001077 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
buzbeec0ecd652011-09-25 18:11:54 -07001078 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001079 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001080 else
buzbeec0ecd652011-09-25 18:11:54 -07001081 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001082 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001083 // Finish up any of the call sequence not interleaved in arg loading
1084 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001085 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001086 }
buzbeece302932011-10-04 14:32:18 -07001087 if (DISPLAY_MISSING_TARGETS) {
1088 genShowTarget(cUnit);
1089 }
buzbeeec5adf32011-09-11 15:25:43 -07001090 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001091 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001092}
1093
buzbeeed3e9302011-09-23 17:34:19 -07001094STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001095 BasicBlock* bb, ArmLIR* labelList)
1096{
1097 bool res = false; // Assume success
1098 RegLocation rlSrc[3];
1099 RegLocation rlDest = badLoc;
1100 RegLocation rlResult = badLoc;
1101 Opcode opcode = mir->dalvikInsn.opcode;
1102
1103 /* Prep Src and Dest locations */
1104 int nextSreg = 0;
1105 int nextLoc = 0;
1106 int attrs = oatDataFlowAttributes[opcode];
1107 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1108 if (attrs & DF_UA) {
1109 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1110 nextSreg++;
1111 } else if (attrs & DF_UA_WIDE) {
1112 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1113 nextSreg + 1);
1114 nextSreg+= 2;
1115 }
1116 if (attrs & DF_UB) {
1117 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1118 nextSreg++;
1119 } else if (attrs & DF_UB_WIDE) {
1120 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1121 nextSreg + 1);
1122 nextSreg+= 2;
1123 }
1124 if (attrs & DF_UC) {
1125 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1126 } else if (attrs & DF_UC_WIDE) {
1127 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1128 nextSreg + 1);
1129 }
1130 if (attrs & DF_DA) {
1131 rlDest = oatGetDest(cUnit, mir, 0);
1132 } else if (attrs & DF_DA_WIDE) {
1133 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1134 }
1135
1136 switch(opcode) {
1137 case OP_NOP:
1138 break;
1139
1140 case OP_MOVE_EXCEPTION:
1141 int exOffset;
1142 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001143 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001144 resetReg = oatAllocTemp(cUnit);
1145 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1146 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1147 loadConstant(cUnit, resetReg, 0);
1148 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1149 storeValue(cUnit, rlDest, rlResult);
1150 break;
1151
1152 case OP_RETURN_VOID:
buzbeec0ecd652011-09-25 18:11:54 -07001153 genSuspendPoll(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001154 break;
1155
1156 case OP_RETURN:
1157 case OP_RETURN_OBJECT:
buzbeec1f45042011-09-21 16:03:19 -07001158 genSuspendPoll(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001159 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001160 break;
1161
1162 case OP_RETURN_WIDE:
buzbeec1f45042011-09-21 16:03:19 -07001163 genSuspendPoll(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001164 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001165 break;
1166
1167 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -07001168 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001169 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001170 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001171 break;
1172
1173 case OP_MOVE_RESULT:
1174 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -07001175 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001176 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001177 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001178 break;
1179
1180 case OP_MOVE:
1181 case OP_MOVE_OBJECT:
1182 case OP_MOVE_16:
1183 case OP_MOVE_OBJECT_16:
1184 case OP_MOVE_FROM16:
1185 case OP_MOVE_OBJECT_FROM16:
1186 storeValue(cUnit, rlDest, rlSrc[0]);
1187 break;
1188
1189 case OP_MOVE_WIDE:
1190 case OP_MOVE_WIDE_16:
1191 case OP_MOVE_WIDE_FROM16:
1192 storeValueWide(cUnit, rlDest, rlSrc[0]);
1193 break;
1194
1195 case OP_CONST:
1196 case OP_CONST_4:
1197 case OP_CONST_16:
1198 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1199 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1200 storeValue(cUnit, rlDest, rlResult);
1201 break;
1202
1203 case OP_CONST_HIGH16:
1204 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1205 loadConstantNoClobber(cUnit, rlResult.lowReg,
1206 mir->dalvikInsn.vB << 16);
1207 storeValue(cUnit, rlDest, rlResult);
1208 break;
1209
1210 case OP_CONST_WIDE_16:
1211 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -07001212 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1213 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1214 mir->dalvikInsn.vB,
1215 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -07001216 storeValueWide(cUnit, rlDest, rlResult);
1217 break;
1218
1219 case OP_CONST_WIDE:
1220 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1221 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001222 mir->dalvikInsn.vB_wide & 0xffffffff,
1223 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001224 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001225 break;
1226
1227 case OP_CONST_WIDE_HIGH16:
1228 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1229 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1230 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001231 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001232 break;
1233
1234 case OP_MONITOR_ENTER:
1235 genMonitorEnter(cUnit, mir, rlSrc[0]);
1236 break;
1237
1238 case OP_MONITOR_EXIT:
1239 genMonitorExit(cUnit, mir, rlSrc[0]);
1240 break;
1241
1242 case OP_CHECK_CAST:
1243 genCheckCast(cUnit, mir, rlSrc[0]);
1244 break;
1245
1246 case OP_INSTANCE_OF:
1247 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1248 break;
1249
1250 case OP_NEW_INSTANCE:
1251 genNewInstance(cUnit, mir, rlDest);
1252 break;
1253
1254 case OP_THROW:
1255 genThrow(cUnit, mir, rlSrc[0]);
1256 break;
1257
buzbee5ade1d22011-09-09 14:44:52 -07001258 case OP_THROW_VERIFICATION_ERROR:
1259 loadWordDisp(cUnit, rSELF,
1260 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1261 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1262 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001263 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001264 break;
1265
buzbee67bf8852011-08-17 17:51:35 -07001266 case OP_ARRAY_LENGTH:
1267 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001268 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001269 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001270 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001271 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1272 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1273 rlResult.lowReg);
1274 storeValue(cUnit, rlDest, rlResult);
1275 break;
1276
1277 case OP_CONST_STRING:
1278 case OP_CONST_STRING_JUMBO:
1279 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1280 break;
1281
1282 case OP_CONST_CLASS:
1283 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1284 break;
1285
1286 case OP_FILL_ARRAY_DATA:
1287 genFillArrayData(cUnit, mir, rlSrc[0]);
1288 break;
1289
1290 case OP_FILLED_NEW_ARRAY:
1291 genFilledNewArray(cUnit, mir, false /* not range */);
1292 break;
1293
1294 case OP_FILLED_NEW_ARRAY_RANGE:
1295 genFilledNewArray(cUnit, mir, true /* range */);
1296 break;
1297
1298 case OP_NEW_ARRAY:
1299 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1300 break;
1301
1302 case OP_GOTO:
1303 case OP_GOTO_16:
1304 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001305 if (bb->taken->startOffset <= mir->offset) {
1306 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001307 }
1308 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1309 break;
1310
1311 case OP_PACKED_SWITCH:
1312 genPackedSwitch(cUnit, mir, rlSrc[0]);
1313 break;
1314
1315 case OP_SPARSE_SWITCH:
1316 genSparseSwitch(cUnit, mir, rlSrc[0]);
1317 break;
1318
1319 case OP_CMPL_FLOAT:
1320 case OP_CMPG_FLOAT:
1321 case OP_CMPL_DOUBLE:
1322 case OP_CMPG_DOUBLE:
1323 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1324 break;
1325
1326 case OP_CMP_LONG:
1327 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1328 break;
1329
1330 case OP_IF_EQ:
1331 case OP_IF_NE:
1332 case OP_IF_LT:
1333 case OP_IF_GE:
1334 case OP_IF_GT:
1335 case OP_IF_LE: {
1336 bool backwardBranch;
1337 ArmConditionCode cond;
1338 backwardBranch = (bb->taken->startOffset <= mir->offset);
1339 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001340 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001341 }
1342 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1343 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1344 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1345 switch(opcode) {
1346 case OP_IF_EQ:
1347 cond = kArmCondEq;
1348 break;
1349 case OP_IF_NE:
1350 cond = kArmCondNe;
1351 break;
1352 case OP_IF_LT:
1353 cond = kArmCondLt;
1354 break;
1355 case OP_IF_GE:
1356 cond = kArmCondGe;
1357 break;
1358 case OP_IF_GT:
1359 cond = kArmCondGt;
1360 break;
1361 case OP_IF_LE:
1362 cond = kArmCondLe;
1363 break;
1364 default:
1365 cond = (ArmConditionCode)0;
1366 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1367 }
1368 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1369 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1370 break;
1371 }
1372
1373 case OP_IF_EQZ:
1374 case OP_IF_NEZ:
1375 case OP_IF_LTZ:
1376 case OP_IF_GEZ:
1377 case OP_IF_GTZ:
1378 case OP_IF_LEZ: {
1379 bool backwardBranch;
1380 ArmConditionCode cond;
1381 backwardBranch = (bb->taken->startOffset <= mir->offset);
1382 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001383 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001384 }
1385 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1386 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1387 switch(opcode) {
1388 case OP_IF_EQZ:
1389 cond = kArmCondEq;
1390 break;
1391 case OP_IF_NEZ:
1392 cond = kArmCondNe;
1393 break;
1394 case OP_IF_LTZ:
1395 cond = kArmCondLt;
1396 break;
1397 case OP_IF_GEZ:
1398 cond = kArmCondGe;
1399 break;
1400 case OP_IF_GTZ:
1401 cond = kArmCondGt;
1402 break;
1403 case OP_IF_LEZ:
1404 cond = kArmCondLe;
1405 break;
1406 default:
1407 cond = (ArmConditionCode)0;
1408 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1409 }
1410 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1411 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1412 break;
1413 }
1414
1415 case OP_AGET_WIDE:
1416 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1417 break;
1418 case OP_AGET:
1419 case OP_AGET_OBJECT:
1420 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1421 break;
1422 case OP_AGET_BOOLEAN:
1423 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1424 rlDest, 0);
1425 break;
1426 case OP_AGET_BYTE:
1427 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1428 break;
1429 case OP_AGET_CHAR:
1430 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1431 rlDest, 1);
1432 break;
1433 case OP_AGET_SHORT:
1434 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1435 break;
1436 case OP_APUT_WIDE:
1437 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1438 break;
1439 case OP_APUT:
1440 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1441 break;
1442 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001443 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001444 break;
1445 case OP_APUT_SHORT:
1446 case OP_APUT_CHAR:
1447 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1448 rlSrc[0], 1);
1449 break;
1450 case OP_APUT_BYTE:
1451 case OP_APUT_BOOLEAN:
1452 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1453 rlSrc[0], 0);
1454 break;
1455
1456 case OP_IGET_WIDE:
1457 case OP_IGET_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001458 genIGetWide(cUnit, mir, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001459 break;
1460
1461 case OP_IGET:
1462 case OP_IGET_VOLATILE:
1463 case OP_IGET_OBJECT:
1464 case OP_IGET_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001465 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001466 break;
1467
1468 case OP_IGET_BOOLEAN:
1469 case OP_IGET_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001470 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001471 break;
1472
1473 case OP_IGET_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001474 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001475 break;
1476
1477 case OP_IGET_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001478 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001479 break;
1480
1481 case OP_IPUT_WIDE:
1482 case OP_IPUT_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001483 genIPutWide(cUnit, mir, rlSrc[0], rlSrc[1]);
buzbee67bf8852011-08-17 17:51:35 -07001484 break;
1485
1486 case OP_IPUT_OBJECT:
1487 case OP_IPUT_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001488 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
buzbee67bf8852011-08-17 17:51:35 -07001489 break;
1490
1491 case OP_IPUT:
1492 case OP_IPUT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001493 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001494 break;
1495
1496 case OP_IPUT_BOOLEAN:
1497 case OP_IPUT_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001498 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001499 break;
1500
1501 case OP_IPUT_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001502 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001503 break;
1504
1505 case OP_IPUT_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001506 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001507 break;
1508
1509 case OP_SGET:
1510 case OP_SGET_OBJECT:
1511 case OP_SGET_BOOLEAN:
1512 case OP_SGET_BYTE:
1513 case OP_SGET_CHAR:
1514 case OP_SGET_SHORT:
1515 genSget(cUnit, mir, rlResult, rlDest);
1516 break;
1517
1518 case OP_SGET_WIDE:
1519 genSgetWide(cUnit, mir, rlResult, rlDest);
1520 break;
1521
1522 case OP_SPUT:
1523 case OP_SPUT_OBJECT:
1524 case OP_SPUT_BOOLEAN:
1525 case OP_SPUT_BYTE:
1526 case OP_SPUT_CHAR:
1527 case OP_SPUT_SHORT:
1528 genSput(cUnit, mir, rlSrc[0]);
1529 break;
1530
1531 case OP_SPUT_WIDE:
1532 genSputWide(cUnit, mir, rlSrc[0]);
1533 break;
1534
1535 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001536 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1537 true /*range*/);
1538 break;
buzbee67bf8852011-08-17 17:51:35 -07001539 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001540 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1541 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001542 break;
1543
1544 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001545 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1546 false /*range*/);
1547 break;
buzbee67bf8852011-08-17 17:51:35 -07001548 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001549 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1550 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001551 break;
1552
1553 case OP_INVOKE_VIRTUAL:
1554 case OP_INVOKE_VIRTUAL_RANGE:
1555 genInvokeVirtual(cUnit, mir);
1556 break;
1557
1558 case OP_INVOKE_SUPER:
1559 case OP_INVOKE_SUPER_RANGE:
1560 genInvokeSuper(cUnit, mir);
1561 break;
1562
1563 case OP_INVOKE_INTERFACE:
1564 case OP_INVOKE_INTERFACE_RANGE:
1565 genInvokeInterface(cUnit, mir);
1566 break;
1567
1568 case OP_NEG_INT:
1569 case OP_NOT_INT:
1570 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1571 break;
1572
1573 case OP_NEG_LONG:
1574 case OP_NOT_LONG:
1575 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1576 break;
1577
1578 case OP_NEG_FLOAT:
1579 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1580 break;
1581
1582 case OP_NEG_DOUBLE:
1583 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1584 break;
1585
1586 case OP_INT_TO_LONG:
1587 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1588 if (rlSrc[0].location == kLocPhysReg) {
1589 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1590 } else {
1591 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1592 }
1593 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1594 rlResult.lowReg, 31);
1595 storeValueWide(cUnit, rlDest, rlResult);
1596 break;
1597
1598 case OP_LONG_TO_INT:
1599 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1600 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1601 storeValue(cUnit, rlDest, rlSrc[0]);
1602 break;
1603
1604 case OP_INT_TO_BYTE:
1605 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1606 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1607 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1608 storeValue(cUnit, rlDest, rlResult);
1609 break;
1610
1611 case OP_INT_TO_SHORT:
1612 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1613 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1614 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1615 storeValue(cUnit, rlDest, rlResult);
1616 break;
1617
1618 case OP_INT_TO_CHAR:
1619 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1620 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1621 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1622 storeValue(cUnit, rlDest, rlResult);
1623 break;
1624
1625 case OP_INT_TO_FLOAT:
1626 case OP_INT_TO_DOUBLE:
1627 case OP_LONG_TO_FLOAT:
1628 case OP_LONG_TO_DOUBLE:
1629 case OP_FLOAT_TO_INT:
1630 case OP_FLOAT_TO_LONG:
1631 case OP_FLOAT_TO_DOUBLE:
1632 case OP_DOUBLE_TO_INT:
1633 case OP_DOUBLE_TO_LONG:
1634 case OP_DOUBLE_TO_FLOAT:
1635 genConversion(cUnit, mir);
1636 break;
1637
1638 case OP_ADD_INT:
1639 case OP_SUB_INT:
1640 case OP_MUL_INT:
1641 case OP_DIV_INT:
1642 case OP_REM_INT:
1643 case OP_AND_INT:
1644 case OP_OR_INT:
1645 case OP_XOR_INT:
1646 case OP_SHL_INT:
1647 case OP_SHR_INT:
1648 case OP_USHR_INT:
1649 case OP_ADD_INT_2ADDR:
1650 case OP_SUB_INT_2ADDR:
1651 case OP_MUL_INT_2ADDR:
1652 case OP_DIV_INT_2ADDR:
1653 case OP_REM_INT_2ADDR:
1654 case OP_AND_INT_2ADDR:
1655 case OP_OR_INT_2ADDR:
1656 case OP_XOR_INT_2ADDR:
1657 case OP_SHL_INT_2ADDR:
1658 case OP_SHR_INT_2ADDR:
1659 case OP_USHR_INT_2ADDR:
1660 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1661 break;
1662
1663 case OP_ADD_LONG:
1664 case OP_SUB_LONG:
1665 case OP_MUL_LONG:
1666 case OP_DIV_LONG:
1667 case OP_REM_LONG:
1668 case OP_AND_LONG:
1669 case OP_OR_LONG:
1670 case OP_XOR_LONG:
1671 case OP_ADD_LONG_2ADDR:
1672 case OP_SUB_LONG_2ADDR:
1673 case OP_MUL_LONG_2ADDR:
1674 case OP_DIV_LONG_2ADDR:
1675 case OP_REM_LONG_2ADDR:
1676 case OP_AND_LONG_2ADDR:
1677 case OP_OR_LONG_2ADDR:
1678 case OP_XOR_LONG_2ADDR:
1679 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1680 break;
1681
buzbee67bf8852011-08-17 17:51:35 -07001682 case OP_SHL_LONG:
1683 case OP_SHR_LONG:
1684 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001685 case OP_SHL_LONG_2ADDR:
1686 case OP_SHR_LONG_2ADDR:
1687 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001688 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1689 break;
1690
1691 case OP_ADD_FLOAT:
1692 case OP_SUB_FLOAT:
1693 case OP_MUL_FLOAT:
1694 case OP_DIV_FLOAT:
1695 case OP_REM_FLOAT:
1696 case OP_ADD_FLOAT_2ADDR:
1697 case OP_SUB_FLOAT_2ADDR:
1698 case OP_MUL_FLOAT_2ADDR:
1699 case OP_DIV_FLOAT_2ADDR:
1700 case OP_REM_FLOAT_2ADDR:
1701 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1702 break;
1703
1704 case OP_ADD_DOUBLE:
1705 case OP_SUB_DOUBLE:
1706 case OP_MUL_DOUBLE:
1707 case OP_DIV_DOUBLE:
1708 case OP_REM_DOUBLE:
1709 case OP_ADD_DOUBLE_2ADDR:
1710 case OP_SUB_DOUBLE_2ADDR:
1711 case OP_MUL_DOUBLE_2ADDR:
1712 case OP_DIV_DOUBLE_2ADDR:
1713 case OP_REM_DOUBLE_2ADDR:
1714 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1715 break;
1716
1717 case OP_RSUB_INT:
1718 case OP_ADD_INT_LIT16:
1719 case OP_MUL_INT_LIT16:
1720 case OP_DIV_INT_LIT16:
1721 case OP_REM_INT_LIT16:
1722 case OP_AND_INT_LIT16:
1723 case OP_OR_INT_LIT16:
1724 case OP_XOR_INT_LIT16:
1725 case OP_ADD_INT_LIT8:
1726 case OP_RSUB_INT_LIT8:
1727 case OP_MUL_INT_LIT8:
1728 case OP_DIV_INT_LIT8:
1729 case OP_REM_INT_LIT8:
1730 case OP_AND_INT_LIT8:
1731 case OP_OR_INT_LIT8:
1732 case OP_XOR_INT_LIT8:
1733 case OP_SHL_INT_LIT8:
1734 case OP_SHR_INT_LIT8:
1735 case OP_USHR_INT_LIT8:
1736 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1737 break;
1738
1739 default:
1740 res = true;
1741 }
1742 return res;
1743}
1744
buzbeeed3e9302011-09-23 17:34:19 -07001745STATIC const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001746 "kMirOpPhi",
1747 "kMirOpNullNRangeUpCheck",
1748 "kMirOpNullNRangeDownCheck",
1749 "kMirOpLowerBound",
1750 "kMirOpPunt",
1751 "kMirOpCheckInlinePrediction",
1752};
1753
1754/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001755STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001756{
1757 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1758 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1759 strcpy(msg, extendedMIROpNames[opOffset]);
1760 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1761
1762 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1763 case kMirOpPhi: {
1764 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1765 op->flags.isNop = true;
1766 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1767 break;
1768 }
1769 default:
1770 break;
1771 }
1772}
1773
1774/* If there are any ins passed in registers that have not been promoted
1775 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001776 * Note: at this pointCopy any ins that are passed in register to their
1777 * home location */
buzbeeed3e9302011-09-23 17:34:19 -07001778STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001779{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001780 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001781 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001782 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1783 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001784 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001785 int startLoc = cUnit->method->NumRegisters() -
1786 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001787 for (int i = 0; i < inRegs; i++) {
1788 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001789 //TUNING: be smarter about flushing ins to frame
1790 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001791 if (loc.location == kLocPhysReg) {
1792 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001793 }
1794 }
1795
1796 // Handle special case of wide argument half in regs, half in frame
1797 if (inRegs == 3) {
1798 RegLocation loc = cUnit->regLocation[startLoc + 2];
1799 if (loc.wide && loc.location == kLocPhysReg) {
1800 // Load the other half of the arg into the promoted pair
buzbee561227c2011-09-02 15:28:19 -07001801 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -07001802 inRegs++;
1803 }
1804 }
1805
1806 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001807 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001808 RegLocation loc = cUnit->regLocation[startLoc + i];
1809 if (loc.fpLocation == kLocPhysReg) {
1810 loc.location = kLocPhysReg;
1811 loc.fp = true;
1812 loc.lowReg = loc.fpLowReg;
1813 loc.highReg = loc.fpHighReg;
1814 }
1815 if (loc.location == kLocPhysReg) {
1816 if (loc.wide) {
buzbeeb29e4d12011-09-26 15:05:48 -07001817 if (loc.fp && (loc.lowReg & 1) != 0) {
1818 // Misaligned - need to load as a pair of singles
1819 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
1820 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
1821 } else {
1822 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1823 loc.lowReg, loc.highReg, INVALID_SREG);
1824 }
buzbee67bf8852011-08-17 17:51:35 -07001825 i++;
1826 } else {
buzbee561227c2011-09-02 15:28:19 -07001827 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001828 }
1829 }
1830 i++;
1831 }
1832}
1833
1834/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001835STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001836{
1837 MIR* mir;
1838 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1839 int blockId = bb->id;
1840
1841 cUnit->curBlock = bb;
1842 labelList[blockId].operands[0] = bb->startOffset;
1843
1844 /* Insert the block label */
1845 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1846 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1847
buzbee6181f792011-09-29 11:14:04 -07001848 /* Reset local optimization data on block boundaries */
1849 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001850 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001851 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001852
1853 ArmLIR* headLIR = NULL;
1854
buzbeebbaf8942011-10-02 13:08:29 -07001855 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001856 if (bb->blockType == kEntryBlock) {
1857 /*
1858 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1859 * mechanism know so it doesn't try to use any of them when
1860 * expanding the frame or flushing. This leaves the utility
1861 * code with a single temp: r12. This should be enough.
1862 */
1863 oatLockTemp(cUnit, r0);
1864 oatLockTemp(cUnit, r1);
1865 oatLockTemp(cUnit, r2);
1866 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001867
1868 /*
1869 * We can safely skip the stack overflow check if we're
1870 * a leaf *and* our frame size < fudge factor.
1871 */
1872 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1873 ((size_t)cUnit->frameSize <
1874 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001875 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001876 if (!skipOverflowCheck) {
1877 /* Load stack limit */
1878 loadWordDisp(cUnit, rSELF,
1879 art::Thread::StackEndOffset().Int32Value(), r12);
1880 }
buzbee67bf8852011-08-17 17:51:35 -07001881 /* Spill core callee saves */
1882 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1883 /* Need to spill any FP regs? */
1884 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001885 /*
1886 * NOTE: fp spills are a little different from core spills in that
1887 * they are pushed as a contiguous block. When promoting from
1888 * the fp set, we must allocate all singles from s16..highest-promoted
1889 */
buzbee67bf8852011-08-17 17:51:35 -07001890 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1891 }
buzbeecefd1872011-09-09 09:59:52 -07001892 if (!skipOverflowCheck) {
1893 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001894 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001895 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1896 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001897 genRegCopy(cUnit, rSP, rLR); // Establish stack
1898 } else {
1899 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001900 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001901 }
buzbee67bf8852011-08-17 17:51:35 -07001902 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1903 flushIns(cUnit);
1904 oatFreeTemp(cUnit, r0);
1905 oatFreeTemp(cUnit, r1);
1906 oatFreeTemp(cUnit, r2);
1907 oatFreeTemp(cUnit, r3);
1908 } else if (bb->blockType == kExitBlock) {
1909 newLIR0(cUnit, kArmPseudoMethodExit);
buzbeebbaf8942011-10-02 13:08:29 -07001910 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07001911 /* Need to restore any FP callee saves? */
1912 if (cUnit->numFPSpills) {
1913 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1914 }
1915 if (cUnit->coreSpillMask & (1 << rLR)) {
1916 /* Unspill rLR to rPC */
1917 cUnit->coreSpillMask &= ~(1 << rLR);
1918 cUnit->coreSpillMask |= (1 << rPC);
1919 }
1920 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1921 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1922 /* We didn't pop to rPC, so must do a bv rLR */
1923 newLIR1(cUnit, kThumbBx, rLR);
1924 }
1925 }
1926
1927 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1928
1929 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07001930 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1931 oatClobberAllRegs(cUnit);
1932 }
buzbee67bf8852011-08-17 17:51:35 -07001933
1934 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1935 oatResetDefTracking(cUnit);
1936 }
1937
1938 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1939 handleExtendedMethodMIR(cUnit, mir);
1940 continue;
1941 }
1942
1943 cUnit->currentDalvikOffset = mir->offset;
1944
1945 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1946 InstructionFormat dalvikFormat =
1947 dexGetFormatFromOpcode(dalvikOpcode);
1948
1949 ArmLIR* boundaryLIR;
1950
1951 /* Mark the beginning of a Dalvik instruction for line tracking */
1952 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1953 (int) oatGetDalvikDisassembly(
1954 &mir->dalvikInsn, ""));
1955 /* Remember the first LIR for this block */
1956 if (headLIR == NULL) {
1957 headLIR = boundaryLIR;
1958 /* Set the first boundaryLIR as a scheduling barrier */
1959 headLIR->defMask = ENCODE_ALL;
1960 }
1961
1962 /* Don't generate the SSA annotation unless verbose mode is on */
1963 if (cUnit->printMe && mir->ssaRep) {
1964 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1965 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1966 }
1967
1968 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1969
1970 if (notHandled) {
1971 char buf[100];
1972 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1973 mir->offset,
1974 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1975 dalvikFormat);
1976 LOG(FATAL) << buf;
1977 }
1978 }
1979
1980 if (headLIR) {
1981 /*
1982 * Eliminate redundant loads/stores and delay stores into later
1983 * slots
1984 */
1985 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1986 cUnit->lastLIRInsn);
1987
1988 /*
1989 * Generate an unconditional branch to the fallthrough block.
1990 */
1991 if (bb->fallThrough) {
1992 genUnconditionalBranch(cUnit,
1993 &labelList[bb->fallThrough->id]);
1994 }
1995 }
1996 return false;
1997}
1998
1999/*
2000 * Nop any unconditional branches that go to the next instruction.
2001 * Note: new redundant branches may be inserted later, and we'll
2002 * use a check in final instruction assembly to nop those out.
2003 */
2004void removeRedundantBranches(CompilationUnit* cUnit)
2005{
2006 ArmLIR* thisLIR;
2007
2008 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
2009 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
2010 thisLIR = NEXT_LIR(thisLIR)) {
2011
2012 /* Branch to the next instruction */
2013 if ((thisLIR->opcode == kThumbBUncond) ||
2014 (thisLIR->opcode == kThumb2BUncond)) {
2015 ArmLIR* nextLIR = thisLIR;
2016
2017 while (true) {
2018 nextLIR = NEXT_LIR(nextLIR);
2019
2020 /*
2021 * Is the branch target the next instruction?
2022 */
2023 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
2024 thisLIR->flags.isNop = true;
2025 break;
2026 }
2027
2028 /*
2029 * Found real useful stuff between the branch and the target.
2030 * Need to explicitly check the lastLIRInsn here because it
2031 * might be the last real instruction.
2032 */
2033 if (!isPseudoOpcode(nextLIR->opcode) ||
2034 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
2035 break;
2036 }
2037 }
2038 }
2039}
2040
buzbeeed3e9302011-09-23 17:34:19 -07002041STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07002042{
2043 ArmLIR** suspendLabel =
2044 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
2045 int numElems = cUnit->suspendLaunchpads.numUsed;
2046
2047 for (int i = 0; i < numElems; i++) {
2048 /* TUNING: move suspend count load into helper */
2049 ArmLIR* lab = suspendLabel[i];
2050 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
2051 cUnit->currentDalvikOffset = lab->operands[1];
2052 oatAppendLIR(cUnit, (LIR *)lab);
2053 loadWordDisp(cUnit, rSELF,
2054 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
2055 loadWordDisp(cUnit, rSELF,
2056 art::Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
2057 opReg(cUnit, kOpBlx, rLR);
2058 genUnconditionalBranch(cUnit, resumeLab);
2059 }
2060}
2061
buzbeeed3e9302011-09-23 17:34:19 -07002062STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07002063{
2064 ArmLIR** throwLabel =
2065 (ArmLIR **) cUnit->throwLaunchpads.elemList;
2066 int numElems = cUnit->throwLaunchpads.numUsed;
2067 int i;
2068
2069 for (i = 0; i < numElems; i++) {
2070 ArmLIR* lab = throwLabel[i];
2071 cUnit->currentDalvikOffset = lab->operands[1];
2072 oatAppendLIR(cUnit, (LIR *)lab);
2073 int funcOffset = 0;
2074 int v1 = lab->operands[2];
2075 int v2 = lab->operands[3];
2076 switch(lab->operands[0]) {
2077 case kArmThrowNullPointer:
2078 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2079 break;
2080 case kArmThrowArrayBounds:
2081 if (v2 != r0) {
2082 genRegCopy(cUnit, r0, v1);
2083 genRegCopy(cUnit, r1, v2);
2084 } else {
2085 if (v1 == r1) {
2086 genRegCopy(cUnit, r12, v1);
2087 genRegCopy(cUnit, r1, v2);
2088 genRegCopy(cUnit, r0, r12);
2089 } else {
2090 genRegCopy(cUnit, r1, v2);
2091 genRegCopy(cUnit, r0, v1);
2092 }
2093 }
2094 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2095 break;
2096 case kArmThrowDivZero:
2097 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2098 break;
2099 case kArmThrowVerificationError:
2100 loadConstant(cUnit, r0, v1);
2101 loadConstant(cUnit, r1, v2);
2102 funcOffset =
2103 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2104 break;
2105 case kArmThrowNegArraySize:
2106 genRegCopy(cUnit, r0, v1);
2107 funcOffset =
2108 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2109 break;
buzbee5ade1d22011-09-09 14:44:52 -07002110 case kArmThrowNoSuchMethod:
2111 genRegCopy(cUnit, r0, v1);
2112 funcOffset =
2113 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2114 break;
buzbeeec5adf32011-09-11 15:25:43 -07002115 case kArmThrowStackOverflow:
2116 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07002117 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07002118 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07002119 opRegImm(cUnit, kOpAdd, rSP,
2120 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07002121 break;
buzbee5ade1d22011-09-09 14:44:52 -07002122 default:
2123 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2124 }
2125 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07002126 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07002127 }
2128}
2129
buzbee67bf8852011-08-17 17:51:35 -07002130void oatMethodMIR2LIR(CompilationUnit* cUnit)
2131{
2132 /* Used to hold the labels of each block */
2133 cUnit->blockLabelList =
2134 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2135
2136 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2137 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07002138 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002139
2140 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07002141
2142 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002143}
2144
2145/* Common initialization routine for an architecture family */
2146bool oatArchInit()
2147{
2148 int i;
2149
2150 for (i = 0; i < kArmLast; i++) {
2151 if (EncodingMap[i].opcode != i) {
2152 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2153 " is wrong: expecting " << i << ", seeing " <<
2154 (int)EncodingMap[i].opcode;
2155 }
2156 }
2157
2158 return oatArchVariantInit();
2159}
2160
2161/* Needed by the Assembler */
2162void oatSetupResourceMasks(ArmLIR* lir)
2163{
2164 setupResourceMasks(lir);
2165}
2166
2167/* Needed by the ld/st optmizatons */
2168ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2169{
2170 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2171}
2172
2173/* Needed by the register allocator */
2174ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2175{
2176 return genRegCopy(cUnit, rDest, rSrc);
2177}
2178
2179/* Needed by the register allocator */
2180void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2181 int srcLo, int srcHi)
2182{
2183 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2184}
2185
2186void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2187 int displacement, int rSrc, OpSize size)
2188{
2189 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2190}
2191
2192void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2193 int displacement, int rSrcLo, int rSrcHi)
2194{
2195 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2196}