blob: 4326a9e2b6e8022171308b2ed4d73fa37d726233 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbeece302932011-10-04 14:32:18 -070017#define DISPLAY_MISSING_TARGETS (cUnit->enableDebug & \
18 (1 << kDebugDisplayMissingTargets))
Elliott Hughes1240dad2011-09-09 16:24:50 -070019
buzbee67bc2362011-10-11 18:08:40 -070020STATIC const RegLocation badLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, INVALID_REG,
21 INVALID_REG, INVALID_SREG};
buzbee6181f792011-09-29 11:14:04 -070022
23/* Mark register usage state and return long retloc */
24STATIC RegLocation getRetLocWide(CompilationUnit* cUnit)
25{
26 RegLocation res = LOC_DALVIK_RETURN_VAL_WIDE;
27 oatLockTemp(cUnit, res.lowReg);
28 oatLockTemp(cUnit, res.highReg);
29 oatMarkPair(cUnit, res.lowReg, res.highReg);
30 return res;
31}
32
33STATIC RegLocation getRetLoc(CompilationUnit* cUnit)
34{
35 RegLocation res = LOC_DALVIK_RETURN_VAL;
36 oatLockTemp(cUnit, res.lowReg);
37 return res;
38}
buzbee67bf8852011-08-17 17:51:35 -070039
buzbeedfd3d702011-08-28 12:56:51 -070040/*
41 * Let helper function take care of everything. Will call
42 * Array::AllocFromCode(type_idx, method, count);
43 * Note: AllocFromCode will handle checks for errNegativeArraySize.
44 */
buzbeeed3e9302011-09-23 17:34:19 -070045STATIC void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -070046 RegLocation rlSrc)
47{
buzbeedfd3d702011-08-28 12:56:51 -070048 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers28ad40d2011-10-27 15:19:26 -070049 uint32_t type_idx = mir->dalvikInsn.vC;
50 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method, type_idx)) {
51 loadWordDisp(cUnit, rSELF,
52 OFFSETOF_MEMBER(Thread, pAllocArrayFromCode), rLR);
53 } else {
54 UNIMPLEMENTED(WARNING) << "Need to check access of '"
55 << PrettyMethod(cUnit->method)
56 << "' to unresolved type " << type_idx;
57 loadWordDisp(cUnit, rSELF,
58 OFFSETOF_MEMBER(Thread, pAllocArrayFromCode), rLR);
59 }
buzbeedfd3d702011-08-28 12:56:51 -070060 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
Ian Rogers28ad40d2011-10-27 15:19:26 -070061 loadConstant(cUnit, r0, type_idx); // arg0 <- type_id
buzbeedfd3d702011-08-28 12:56:51 -070062 loadValueDirectFixed(cUnit, rlSrc, r2); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070063 callRuntimeHelper(cUnit, rLR);
buzbeedfd3d702011-08-28 12:56:51 -070064 RegLocation rlResult = oatGetReturn(cUnit);
65 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -070066}
67
68/*
69 * Similar to genNewArray, but with post-allocation initialization.
70 * Verifier guarantees we're dealing with an array class. Current
71 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
72 * Current code also throws internal unimp if not 'L', '[' or 'I'.
73 */
buzbeeed3e9302011-09-23 17:34:19 -070074STATIC void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
buzbee67bf8852011-08-17 17:51:35 -070075{
76 DecodedInstruction* dInsn = &mir->dalvikInsn;
buzbee81eccc02011-09-17 13:42:21 -070077 int elems = dInsn->vA;
78 int typeId = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070079 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbeedfd3d702011-08-28 12:56:51 -070080 loadWordDisp(cUnit, rSELF,
Elliott Hughesb408de72011-10-04 14:35:05 -070081 OFFSETOF_MEMBER(Thread, pCheckAndAllocArrayFromCode), rLR);
Ian Rogers28ad40d2011-10-27 15:19:26 -070082 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method, typeId)) {
83 UNIMPLEMENTED(WARNING) << "Need to check access of '" << PrettyMethod(cUnit->method)
84 << "' to unresolved type " << typeId;
85 }
buzbeedfd3d702011-08-28 12:56:51 -070086 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
87 loadConstant(cUnit, r0, typeId); // arg0 <- type_id
88 loadConstant(cUnit, r2, elems); // arg2 <- count
Ian Rogersff1ed472011-09-20 13:46:24 -070089 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -070090 /*
buzbeedfd3d702011-08-28 12:56:51 -070091 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
92 * return region. Because AllocFromCode placed the new array
93 * in r0, we'll just lock it into place. When debugger support is
94 * added, it may be necessary to additionally copy all return
95 * values to a home location in thread-local storage
buzbee67bf8852011-08-17 17:51:35 -070096 */
buzbee67bf8852011-08-17 17:51:35 -070097 oatLockTemp(cUnit, r0);
buzbeedfd3d702011-08-28 12:56:51 -070098
buzbee67bf8852011-08-17 17:51:35 -070099 // Having a range of 0 is legal
100 if (isRange && (dInsn->vA > 0)) {
101 /*
102 * Bit of ugliness here. We're going generate a mem copy loop
103 * on the register range, but it is possible that some regs
104 * in the range have been promoted. This is unlikely, but
105 * before generating the copy, we'll just force a flush
106 * of any regs in the source range that have been promoted to
107 * home location.
108 */
109 for (unsigned int i = 0; i < dInsn->vA; i++) {
110 RegLocation loc = oatUpdateLoc(cUnit,
111 oatGetSrc(cUnit, mir, i));
112 if (loc.location == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -0700113 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
114 loc.lowReg, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700115 }
116 }
117 /*
118 * TUNING note: generated code here could be much improved, but
119 * this is an uncommon operation and isn't especially performance
120 * critical.
121 */
122 int rSrc = oatAllocTemp(cUnit);
123 int rDst = oatAllocTemp(cUnit);
124 int rIdx = oatAllocTemp(cUnit);
125 int rVal = rLR; // Using a lot of temps, rLR is known free here
126 // Set up source pointer
127 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
buzbee67bc2362011-10-11 18:08:40 -0700128 opRegRegImm(cUnit, kOpAdd, rSrc, rSP,
129 oatSRegOffset(cUnit, rlFirst.sRegLow));
buzbee67bf8852011-08-17 17:51:35 -0700130 // Set up the target pointer
131 opRegRegImm(cUnit, kOpAdd, rDst, r0,
buzbeec143c552011-08-20 17:38:58 -0700132 Array::DataOffset().Int32Value());
buzbee67bf8852011-08-17 17:51:35 -0700133 // Set up the loop counter (known to be > 0)
buzbee31813452011-10-16 14:33:08 -0700134 loadConstant(cUnit, rIdx, dInsn->vA - 1);
buzbee67bf8852011-08-17 17:51:35 -0700135 // Generate the copy loop. Going backwards for convenience
136 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
137 target->defMask = ENCODE_ALL;
138 // Copy next element
139 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
140 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
141 // Use setflags encoding here
142 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee31813452011-10-16 14:33:08 -0700143 ArmLIR* branch = opCondBranch(cUnit, kArmCondGe);
buzbee67bf8852011-08-17 17:51:35 -0700144 branch->generic.target = (LIR*)target;
145 } else if (!isRange) {
146 // TUNING: interleave
147 for (unsigned int i = 0; i < dInsn->vA; i++) {
148 RegLocation rlArg = loadValue(cUnit,
149 oatGetSrc(cUnit, mir, i), kCoreReg);
buzbeec143c552011-08-20 17:38:58 -0700150 storeBaseDisp(cUnit, r0,
151 Array::DataOffset().Int32Value() +
buzbee67bf8852011-08-17 17:51:35 -0700152 i * 4, rlArg.lowReg, kWord);
153 // If the loadValue caused a temp to be allocated, free it
154 if (oatIsTemp(cUnit, rlArg.lowReg)) {
155 oatFreeTemp(cUnit, rlArg.lowReg);
156 }
157 }
158 }
159}
160
Brian Carlstrom845490b2011-09-19 15:56:53 -0700161Field* FindFieldWithResolvedStaticStorage(const Method* method,
162 const uint32_t fieldIdx,
163 uint32_t& resolvedTypeIdx) {
164 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
165 Field* field = class_linker->ResolveField(fieldIdx, method, true);
166 if (field == NULL) {
167 return NULL;
168 }
169 const art::DexFile& dex_file = class_linker->
170 FindDexFile(method->GetDeclaringClass()->GetDexCache());
171 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
172 int type_idx = field_id.class_idx_;
173 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
174 // Check if storage class is the same as class referred to by type idx.
175 // They may not be if the FieldId refers a subclass, but storage is in super
176 if (field->GetDeclaringClass() == klass) {
177 resolvedTypeIdx = type_idx;
178 return field;
179 }
180 // See if we can find a dex reference for the storage class.
181 // we may not if the dex file never references the super class,
182 // but usually it will.
183 std::string descriptor = field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8();
184 for (size_t type_idx = 0; type_idx < dex_file.NumTypeIds(); type_idx++) {
185 const art::DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx);
186 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
187 resolvedTypeIdx = type_idx;
188 return field;
189 }
190 }
191 return NULL; // resort to slow path
192}
193
buzbeeed3e9302011-09-23 17:34:19 -0700194STATIC void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700195{
buzbeee1931742011-08-28 21:15:53 -0700196 bool isObject = ((mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
197 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE));
buzbee1da522d2011-09-04 11:22:20 -0700198 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700199 uint32_t typeIdx;
200 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee6181f792011-09-29 11:14:04 -0700201 oatFlushAllRegs(cUnit);
Brian Carlstrom845490b2011-09-19 15:56:53 -0700202 if (SLOW_FIELD_PATH || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700203 // Slow path
Elliott Hughes81bc5092011-09-30 17:25:59 -0700204 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700205 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
206 : OFFSETOF_MEMBER(Thread, pSet32Static);
buzbeee1931742011-08-28 21:15:53 -0700207 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
208 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
209 loadCurrMethodDirect(cUnit, r1);
210 loadValueDirect(cUnit, rlSrc, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700211 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700212 } else {
buzbee1da522d2011-09-04 11:22:20 -0700213 // fast path
214 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700215 // Using fixed register to sync with slow path
216 int rMethod = r1;
217 oatLockTemp(cUnit, rMethod);
218 loadCurrMethodDirect(cUnit, rMethod);
219 int rBase = r0;
220 oatLockTemp(cUnit, rBase);
221 loadWordDisp(cUnit, rMethod,
222 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
223 rBase);
224 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
225 sizeof(int32_t*)* typeIdx, rBase);
226 // TUNING: fast path should fall through
buzbeec0ecd652011-09-25 18:11:54 -0700227 // TUNING: Try a conditional skip here, might be faster
buzbee1da522d2011-09-04 11:22:20 -0700228 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
229 loadWordDisp(cUnit, rSELF,
230 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
231 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700232 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700233 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
234 skipTarget->defMask = ENCODE_ALL;
235 branchOver->generic.target = (LIR*)skipTarget;
236 rlSrc = oatGetSrc(cUnit, mir, 0);
237 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
buzbee12246b82011-09-29 14:15:05 -0700238#if ANDROID_SMP != 0
239 if (field->IsVolatile()) {
240 oatGenMemBarrier(cUnit, kST);
241 }
242#endif
buzbee1da522d2011-09-04 11:22:20 -0700243 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700244#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700245 if (field->IsVolatile()) {
246 oatGenMemBarrier(cUnit, kSY);
247 }
buzbee67bf8852011-08-17 17:51:35 -0700248#endif
buzbee1da522d2011-09-04 11:22:20 -0700249 if (isObject) {
250 markGCCard(cUnit, rlSrc.lowReg, rBase);
251 }
252 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700253 }
buzbee67bf8852011-08-17 17:51:35 -0700254}
255
buzbeeed3e9302011-09-23 17:34:19 -0700256STATIC void genSputWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700257{
buzbee1da522d2011-09-04 11:22:20 -0700258 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700259 uint32_t typeIdx;
260 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee6181f792011-09-29 11:14:04 -0700261 oatFlushAllRegs(cUnit);
buzbee12246b82011-09-29 14:15:05 -0700262#if ANDROID_SMP != 0
263 bool isVolatile = (field == NULL) || field->IsVolatile();
264#else
265 bool isVolatile = false;
266#endif
267 if (SLOW_FIELD_PATH || field == NULL || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700268 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700269 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pSet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700270 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
271 loadCurrMethodDirect(cUnit, r1);
272 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -0700273 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700274 } else {
buzbee1da522d2011-09-04 11:22:20 -0700275 // fast path
276 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700277 // Using fixed register to sync with slow path
278 int rMethod = r1;
279 oatLockTemp(cUnit, rMethod);
280 loadCurrMethodDirect(cUnit, r1);
281 int rBase = r0;
282 oatLockTemp(cUnit, rBase);
283 loadWordDisp(cUnit, rMethod,
284 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
285 rBase);
286 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
287 sizeof(int32_t*)* typeIdx, rBase);
288 // TUNING: fast path should fall through
289 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
290 loadWordDisp(cUnit, rSELF,
291 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
292 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700293 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700294 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
295 skipTarget->defMask = ENCODE_ALL;
296 branchOver->generic.target = (LIR*)skipTarget;
297 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
298 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
299 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
300 rlSrc.highReg);
buzbee1da522d2011-09-04 11:22:20 -0700301 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700302 }
buzbee67bf8852011-08-17 17:51:35 -0700303}
304
305
buzbeeed3e9302011-09-23 17:34:19 -0700306STATIC void genSgetWide(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700307 RegLocation rlResult, RegLocation rlDest)
308{
buzbee1da522d2011-09-04 11:22:20 -0700309 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700310 uint32_t typeIdx;
311 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee12246b82011-09-29 14:15:05 -0700312#if ANDROID_SMP != 0
313 bool isVolatile = (field == NULL) || field->IsVolatile();
314#else
315 bool isVolatile = false;
316#endif
buzbee6181f792011-09-29 11:14:04 -0700317 oatFlushAllRegs(cUnit);
buzbee12246b82011-09-29 14:15:05 -0700318 if (SLOW_FIELD_PATH || field == NULL || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700319 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700320 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pGet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700321 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
322 loadCurrMethodDirect(cUnit, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -0700323 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700324 RegLocation rlResult = oatGetReturnWide(cUnit);
325 storeValueWide(cUnit, rlDest, rlResult);
326 } else {
buzbee1da522d2011-09-04 11:22:20 -0700327 // Fast path
328 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700329 // Using fixed register to sync with slow path
330 int rMethod = r1;
331 oatLockTemp(cUnit, rMethod);
332 loadCurrMethodDirect(cUnit, rMethod);
333 int rBase = r0;
334 oatLockTemp(cUnit, rBase);
335 loadWordDisp(cUnit, rMethod,
336 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
337 rBase);
338 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
339 sizeof(int32_t*)* typeIdx, rBase);
340 // TUNING: fast path should fall through
341 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
342 loadWordDisp(cUnit, rSELF,
343 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
344 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700345 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700346 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
347 skipTarget->defMask = ENCODE_ALL;
348 branchOver->generic.target = (LIR*)skipTarget;
349 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
350 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee1da522d2011-09-04 11:22:20 -0700351 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
352 rlResult.highReg, INVALID_SREG);
353 oatFreeTemp(cUnit, rBase);
354 storeValueWide(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700355 }
buzbee67bf8852011-08-17 17:51:35 -0700356}
357
buzbeeed3e9302011-09-23 17:34:19 -0700358STATIC void genSget(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700359 RegLocation rlResult, RegLocation rlDest)
360{
buzbee1da522d2011-09-04 11:22:20 -0700361 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700362 uint32_t typeIdx;
363 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbeee1931742011-08-28 21:15:53 -0700364 bool isObject = ((mir->dalvikInsn.opcode == OP_SGET_OBJECT) ||
365 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE));
buzbee6181f792011-09-29 11:14:04 -0700366 oatFlushAllRegs(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -0700367 if (SLOW_FIELD_PATH || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700368 // Slow path
Elliott Hughes81bc5092011-09-30 17:25:59 -0700369 warnIfUnresolved(cUnit, fieldIdx, field);
buzbee1da522d2011-09-04 11:22:20 -0700370 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
371 : OFFSETOF_MEMBER(Thread, pGet32Static);
buzbeee1931742011-08-28 21:15:53 -0700372 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
373 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
374 loadCurrMethodDirect(cUnit, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -0700375 callRuntimeHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700376 RegLocation rlResult = oatGetReturn(cUnit);
377 storeValue(cUnit, rlDest, rlResult);
378 } else {
buzbee1da522d2011-09-04 11:22:20 -0700379 // Fast path
380 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700381 // Using fixed register to sync with slow path
382 int rMethod = r1;
383 oatLockTemp(cUnit, rMethod);
384 loadCurrMethodDirect(cUnit, rMethod);
385 int rBase = r0;
386 oatLockTemp(cUnit, rBase);
387 loadWordDisp(cUnit, rMethod,
388 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
389 rBase);
390 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
391 sizeof(int32_t*)* typeIdx, rBase);
392 // TUNING: fast path should fall through
393 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
394 loadWordDisp(cUnit, rSELF,
395 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
396 loadConstant(cUnit, r0, typeIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700397 callRuntimeHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700398 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
399 skipTarget->defMask = ENCODE_ALL;
400 branchOver->generic.target = (LIR*)skipTarget;
401 rlDest = oatGetDest(cUnit, mir, 0);
402 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700403#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700404 if (field->IsVolatile()) {
buzbee1da522d2011-09-04 11:22:20 -0700405 oatGenMemBarrier(cUnit, kSY);
406 }
buzbee67bf8852011-08-17 17:51:35 -0700407#endif
buzbee1da522d2011-09-04 11:22:20 -0700408 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
409 oatFreeTemp(cUnit, rBase);
410 storeValue(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700411 }
buzbee67bf8852011-08-17 17:51:35 -0700412}
413
buzbee561227c2011-09-02 15:28:19 -0700414typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int,
415 ArmLIR*);
buzbee67bf8852011-08-17 17:51:35 -0700416
417/*
418 * Bit of a hack here - in leiu of a real scheduling pass,
419 * emit the next instruction in static & direct invoke sequences.
420 */
buzbeeed3e9302011-09-23 17:34:19 -0700421STATIC int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700422 DecodedInstruction* dInsn, int state,
423 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700424{
buzbee561227c2011-09-02 15:28:19 -0700425 DCHECK(rollback == NULL);
426 uint32_t idx = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -0700427 switch(state) {
428 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700429 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700430 break;
buzbee561227c2011-09-02 15:28:19 -0700431 case 1: // Get method->code_and_direct_methods_
432 loadWordDisp(cUnit, r0,
433 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
434 r0);
buzbee67bf8852011-08-17 17:51:35 -0700435 break;
buzbee561227c2011-09-02 15:28:19 -0700436 case 2: // Grab target method* and target code_
437 loadWordDisp(cUnit, r0,
438 art::CodeAndDirectMethods::CodeOffsetInBytes(idx), rLR);
439 loadWordDisp(cUnit, r0,
440 art::CodeAndDirectMethods::MethodOffsetInBytes(idx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700441 break;
442 default:
443 return -1;
444 }
445 return state + 1;
446}
447
buzbee67bf8852011-08-17 17:51:35 -0700448/*
449 * Bit of a hack here - in leiu of a real scheduling pass,
450 * emit the next instruction in a virtual invoke sequence.
451 * We can use rLR as a temp prior to target address loading
452 * Note also that we'll load the first argument ("this") into
453 * r1 here rather than the standard loadArgRegs.
454 */
buzbeeed3e9302011-09-23 17:34:19 -0700455STATIC int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700456 DecodedInstruction* dInsn, int state,
457 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700458{
buzbee561227c2011-09-02 15:28:19 -0700459 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700460 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700461 /*
462 * This is the fast path in which the target virtual method is
463 * fully resolved at compile time.
464 */
Brian Carlstrom845490b2011-09-19 15:56:53 -0700465 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
466 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -0700467 CHECK(baseMethod != NULL);
468 uint32_t target_idx = baseMethod->GetMethodIndex();
buzbee67bf8852011-08-17 17:51:35 -0700469 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700470 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700471 rlArg = oatGetSrc(cUnit, mir, 0);
472 loadValueDirectFixed(cUnit, rlArg, r1);
473 break;
buzbee561227c2011-09-02 15:28:19 -0700474 case 1: // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700475 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee561227c2011-09-02 15:28:19 -0700476 // get this->klass_ [use r1, set rLR]
477 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700478 break;
buzbee561227c2011-09-02 15:28:19 -0700479 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
480 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700481 break;
buzbee561227c2011-09-02 15:28:19 -0700482 case 3: // Get target method [use rLR, set r0]
483 loadWordDisp(cUnit, rLR, (target_idx * 4) +
484 art::Array::DataOffset().Int32Value(), r0);
485 break;
486 case 4: // Get the target compiled code address [uses r0, sets rLR]
487 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700488 break;
489 default:
490 return -1;
491 }
492 return state + 1;
493}
494
buzbeeed3e9302011-09-23 17:34:19 -0700495STATIC int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700496 DecodedInstruction* dInsn, int state,
497 ArmLIR* rollback)
buzbee7b1b86d2011-08-26 18:59:10 -0700498{
499 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700500 ArmLIR* skipBranch;
501 ArmLIR* skipTarget;
502 /*
503 * This handles the case in which the base method is not fully
504 * resolved at compile time. We must generate code to test
505 * for resolution a run time, bail to the slow path if not to
506 * fill in all the tables. In the latter case, we'll restart at
507 * at the beginning of the sequence.
508 */
buzbee7b1b86d2011-08-26 18:59:10 -0700509 switch(state) {
510 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700511 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700512 break;
buzbee561227c2011-09-02 15:28:19 -0700513 case 1: // Get method->dex_cache_resolved_methods_
514 loadWordDisp(cUnit, r0,
515 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700516 break;
buzbee561227c2011-09-02 15:28:19 -0700517 case 2: // method->dex_cache_resolved_methods_->Get(method_idx)
518 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
519 art::Array::DataOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700520 break;
buzbee561227c2011-09-02 15:28:19 -0700521 case 3: // Resolved?
522 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
523 // Slowest path, bail to helper, rollback and retry
524 loadWordDisp(cUnit, rSELF,
525 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
526 loadConstant(cUnit, r1, dInsn->vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700527 callRuntimeHelper(cUnit, rLR);
buzbee561227c2011-09-02 15:28:19 -0700528 genUnconditionalBranch(cUnit, rollback);
529 // Resume normal slow path
530 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
531 skipTarget->defMask = ENCODE_ALL;
532 skipBranch->generic.target = (LIR*)skipTarget;
buzbee4a3164f2011-09-03 11:25:10 -0700533 // Get base_method->method_index [usr rLR, set r0]
buzbee561227c2011-09-02 15:28:19 -0700534 loadBaseDisp(cUnit, mir, rLR,
535 Method::GetMethodIndexOffset().Int32Value(), r0,
536 kUnsignedHalf, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700537 // Load "this" [set r1]
538 rlArg = oatGetSrc(cUnit, mir, 0);
539 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee7b1b86d2011-08-26 18:59:10 -0700540 break;
541 case 4:
542 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700543 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee7b1b86d2011-08-26 18:59:10 -0700544 // get this->clazz [use r1, set rLR]
buzbee561227c2011-09-02 15:28:19 -0700545 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700546 break;
buzbee561227c2011-09-02 15:28:19 -0700547 case 5:
548 // get this->klass_->vtable_ [usr rLR, set rLR]
549 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbeeed3e9302011-09-23 17:34:19 -0700550 DCHECK_EQ((art::Array::DataOffset().Int32Value() & 0x3), 0);
buzbee561227c2011-09-02 15:28:19 -0700551 // In load shadow fold vtable_ object header size into method_index_
552 opRegImm(cUnit, kOpAdd, r0,
553 art::Array::DataOffset().Int32Value() / 4);
554 // Get target Method*
555 loadBaseIndexed(cUnit, rLR, r0, r0, 2, kWord);
556 break;
557 case 6: // Get the target compiled code address [uses r0, sets rLR]
558 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700559 break;
560 default:
561 return -1;
562 }
563 return state + 1;
564}
565
buzbeeed3e9302011-09-23 17:34:19 -0700566STATIC int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
buzbeec0ecd652011-09-25 18:11:54 -0700567 DecodedInstruction* dInsn, int callState,
568 NextCallInsn nextCallInsn, ArmLIR* rollback,
569 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700570{
buzbeec0ecd652011-09-25 18:11:54 -0700571 int nextReg = r1;
572 int nextArg = 0;
573 if (skipThis) {
574 nextReg++;
575 nextArg++;
576 }
577 for (; (nextReg <= r3) && (nextArg < mir->ssaRep->numUses); nextReg++) {
578 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
579 rlArg = oatUpdateRawLoc(cUnit, rlArg);
580 if (rlArg.wide && (nextReg <= r2)) {
581 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
582 nextReg++;
583 nextArg++;
584 } else {
buzbee1b4c8592011-08-31 10:43:51 -0700585 rlArg.wide = false;
buzbeec0ecd652011-09-25 18:11:54 -0700586 loadValueDirectFixed(cUnit, rlArg, nextReg);
buzbee67bf8852011-08-17 17:51:35 -0700587 }
buzbeec0ecd652011-09-25 18:11:54 -0700588 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700589 }
590 return callState;
591}
592
buzbee4a3164f2011-09-03 11:25:10 -0700593// Interleave launch code for INVOKE_INTERFACE.
buzbeeed3e9302011-09-23 17:34:19 -0700594STATIC int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700595 DecodedInstruction* dInsn, int state,
596 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700597{
buzbee510c6052011-10-27 10:47:20 -0700598 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700599 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700600 case 0: // Load trampoline target
601 loadWordDisp(cUnit, rSELF,
602 OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline),
603 rLR);
604 // Load r0 with method index
605 loadConstant(cUnit, r0, dInsn->vB);
buzbee67bf8852011-08-17 17:51:35 -0700606 break;
buzbee67bf8852011-08-17 17:51:35 -0700607 default:
608 return -1;
609 }
610 return state + 1;
611}
612
buzbee67bf8852011-08-17 17:51:35 -0700613/*
614 * Interleave launch code for INVOKE_SUPER. See comments
615 * for nextVCallIns.
616 */
buzbeeed3e9302011-09-23 17:34:19 -0700617STATIC int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700618 DecodedInstruction* dInsn, int state,
619 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700620{
buzbee4a3164f2011-09-03 11:25:10 -0700621 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700622 RegLocation rlArg;
buzbee4a3164f2011-09-03 11:25:10 -0700623 /*
624 * This is the fast path in which the target virtual method is
625 * fully resolved at compile time. Note also that this path assumes
626 * that the check to verify that the target method index falls
627 * within the size of the super's vtable has been done at compile-time.
628 */
Brian Carlstrom845490b2011-09-19 15:56:53 -0700629 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
630 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -0700631 CHECK(baseMethod != NULL);
632 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
633 CHECK(superClass != NULL);
634 int32_t target_idx = baseMethod->GetMethodIndex();
635 CHECK(superClass->GetVTable()->GetLength() > target_idx);
636 Method* targetMethod = superClass->GetVTable()->Get(target_idx);
637 CHECK(targetMethod != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700638 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700639 case 0: // Get current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700640 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700641 // Load "this" [set r1]
642 rlArg = oatGetSrc(cUnit, mir, 0);
643 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee4a3164f2011-09-03 11:25:10 -0700644 // Get method->declaring_class_ [use r0, set rLR]
645 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
646 rLR);
buzbee67bf8852011-08-17 17:51:35 -0700647 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700648 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee4a3164f2011-09-03 11:25:10 -0700649 break;
650 case 1: // Get method->declaring_class_->super_class [usr rLR, set rLR]
651 loadWordDisp(cUnit, rLR, Class::SuperClassOffset().Int32Value(),
652 rLR);
653 break;
654 case 2: // Get ...->super_class_->vtable [u/s rLR]
655 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
656 break;
657 case 3: // Get target method [use rLR, set r0]
658 loadWordDisp(cUnit, rLR, (target_idx * 4) +
659 art::Array::DataOffset().Int32Value(), r0);
660 break;
661 case 4: // Get the target compiled code address [uses r0, sets rLR]
662 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
663 break;
buzbee67bf8852011-08-17 17:51:35 -0700664 default:
665 return -1;
666 }
buzbee4a3164f2011-09-03 11:25:10 -0700667 return state + 1;
668}
669
670/* Slow-path version of nextSuperCallInsn */
buzbeeed3e9302011-09-23 17:34:19 -0700671STATIC int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee4a3164f2011-09-03 11:25:10 -0700672 DecodedInstruction* dInsn, int state,
673 ArmLIR* rollback)
674{
buzbee4a3164f2011-09-03 11:25:10 -0700675 RegLocation rlArg;
676 ArmLIR* skipBranch;
677 ArmLIR* skipTarget;
678 int tReg;
679 /*
680 * This handles the case in which the base method is not fully
681 * resolved at compile time. We must generate code to test
682 * for resolution a run time, bail to the slow path if not to
683 * fill in all the tables. In the latter case, we'll restart at
684 * at the beginning of the sequence.
685 */
686 switch(state) {
687 case 0: // Get the current Method* [sets r0]
688 loadCurrMethodDirect(cUnit, r0);
689 break;
690 case 1: // Get method->dex_cache_resolved_methods_ [usr r0, set rLR]
691 loadWordDisp(cUnit, r0,
692 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
693 break;
694 case 2: // method->dex_cache_resolved_methods_->Get(meth_idx) [u/s rLR]
695 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
696 art::Array::DataOffset().Int32Value(), rLR);
697 break;
698 case 3: // Resolved?
699 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
700 // Slowest path, bail to helper, rollback and retry
701 loadWordDisp(cUnit, rSELF,
702 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
703 loadConstant(cUnit, r1, dInsn->vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700704 callRuntimeHelper(cUnit, rLR);
buzbee4a3164f2011-09-03 11:25:10 -0700705 genUnconditionalBranch(cUnit, rollback);
706 // Resume normal slow path
707 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
708 skipTarget->defMask = ENCODE_ALL;
709 skipBranch->generic.target = (LIR*)skipTarget;
710 // Get base_method->method_index [usr rLR, set rLR]
711 loadBaseDisp(cUnit, mir, rLR,
712 Method::GetMethodIndexOffset().Int32Value(), rLR,
713 kUnsignedHalf, INVALID_SREG);
714 // Load "this" [set r1]
715 rlArg = oatGetSrc(cUnit, mir, 0);
716 loadValueDirectFixed(cUnit, rlArg, r1);
717 // Load curMethod->declaring_class_ [uses r0, sets r0]
718 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
719 r0);
buzbee6a0f7f52011-09-05 16:14:20 -0700720 // Null this?
buzbee5ade1d22011-09-09 14:44:52 -0700721 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee6a0f7f52011-09-05 16:14:20 -0700722 // Get method->declaring_class_->super_class [usr r0, set r0]
buzbee4a3164f2011-09-03 11:25:10 -0700723 loadWordDisp(cUnit, r0, Class::SuperClassOffset().Int32Value(), r0);
724 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700725 case 4: // Get ...->super_class_->vtable [u/s r0]
buzbee4a3164f2011-09-03 11:25:10 -0700726 loadWordDisp(cUnit, r0, Class::VTableOffset().Int32Value(), r0);
buzbee43a36422011-09-14 14:00:13 -0700727 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee4a3164f2011-09-03 11:25:10 -0700728 // Range check, throw NSM on failure
729 tReg = oatAllocTemp(cUnit);
730 loadWordDisp(cUnit, r0, art::Array::LengthOffset().Int32Value(),
731 tReg);
buzbeeec5adf32011-09-11 15:25:43 -0700732 genRegRegCheck(cUnit, kArmCondCs, tReg, rLR, mir,
733 kArmThrowNoSuchMethod);
buzbee4a3164f2011-09-03 11:25:10 -0700734 oatFreeTemp(cUnit, tReg);
735 }
buzbee6a0f7f52011-09-05 16:14:20 -0700736 // Adjust vtable_ base past object header
737 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
buzbee4a3164f2011-09-03 11:25:10 -0700738 // Get target Method*
buzbee6a0f7f52011-09-05 16:14:20 -0700739 loadBaseIndexed(cUnit, r0, rLR, r0, 2, kWord);
buzbee4a3164f2011-09-03 11:25:10 -0700740 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700741 case 5: // Get the target compiled code address [uses r0, sets rLR]
buzbee4a3164f2011-09-03 11:25:10 -0700742 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
743 break;
744 default:
745 return -1;
746 }
buzbee67bf8852011-08-17 17:51:35 -0700747 return state + 1;
748}
749
750/*
751 * Load up to 5 arguments, the first three of which will be in
752 * r1 .. r3. On entry r0 contains the current method pointer,
753 * and as part of the load sequence, it must be replaced with
754 * the target method pointer. Note, this may also be called
755 * for "range" variants if the number of arguments is 5 or fewer.
756 */
buzbeeed3e9302011-09-23 17:34:19 -0700757STATIC int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700758 DecodedInstruction* dInsn, int callState,
759 ArmLIR** pcrLabel, bool isRange,
buzbee1da522d2011-09-04 11:22:20 -0700760 NextCallInsn nextCallInsn, ArmLIR* rollback,
761 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700762{
763 RegLocation rlArg;
buzbee67bf8852011-08-17 17:51:35 -0700764
765 /* If no arguments, just return */
766 if (dInsn->vA == 0)
767 return callState;
768
buzbee561227c2011-09-02 15:28:19 -0700769 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700770
buzbeec0ecd652011-09-25 18:11:54 -0700771 DCHECK_LE(dInsn->vA, 5U);
772 if (dInsn->vA > 3) {
773 uint32_t nextUse = 3;
774 //Detect special case of wide arg spanning arg3/arg4
775 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
776 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
777 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
778 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
779 rlUse2.wide) {
780 int reg;
781 // Wide spans, we need the 2nd half of uses[2].
782 rlArg = oatUpdateLocWide(cUnit, rlUse2);
783 if (rlArg.location == kLocPhysReg) {
784 reg = rlArg.highReg;
785 } else {
786 // r2 & r3 can safely be used here
787 reg = r3;
buzbee67bc2362011-10-11 18:08:40 -0700788 loadWordDisp(cUnit, rSP,
789 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbeec0ecd652011-09-25 18:11:54 -0700790 callState = nextCallInsn(cUnit, mir, dInsn, callState,
791 rollback);
792 }
793 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
794 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
795 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
796 nextUse++;
797 }
798 // Loop through the rest
799 while (nextUse < dInsn->vA) {
800 int lowReg;
801 int highReg;
802 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
803 rlArg = oatUpdateRawLoc(cUnit, rlArg);
804 if (rlArg.location == kLocPhysReg) {
805 lowReg = rlArg.lowReg;
806 highReg = rlArg.highReg;
807 } else {
808 lowReg = r2;
809 highReg = r3;
810 if (rlArg.wide) {
811 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
812 } else {
813 loadValueDirectFixed(cUnit, rlArg, lowReg);
814 }
815 callState = nextCallInsn(cUnit, mir, dInsn, callState,
816 rollback);
817 }
818 int outsOffset = (nextUse + 1) * 4;
819 if (rlArg.wide) {
820 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
821 nextUse += 2;
822 } else {
823 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
824 nextUse++;
825 }
buzbee561227c2011-09-02 15:28:19 -0700826 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700827 }
buzbee67bf8852011-08-17 17:51:35 -0700828 }
829
buzbeec0ecd652011-09-25 18:11:54 -0700830 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
831 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700832
buzbee67bf8852011-08-17 17:51:35 -0700833 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700834 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700835 }
836 return callState;
837}
838
839/*
840 * May have 0+ arguments (also used for jumbo). Note that
841 * source virtual registers may be in physical registers, so may
842 * need to be flushed to home location before copying. This
843 * applies to arg3 and above (see below).
844 *
845 * Two general strategies:
846 * If < 20 arguments
847 * Pass args 3-18 using vldm/vstm block copy
848 * Pass arg0, arg1 & arg2 in r1-r3
849 * If 20+ arguments
850 * Pass args arg19+ using memcpy block copy
851 * Pass arg0, arg1 & arg2 in r1-r3
852 *
853 */
buzbeeed3e9302011-09-23 17:34:19 -0700854STATIC int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700855 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700856 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700857 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700858{
859 int firstArg = dInsn->vC;
860 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700861
buzbee67bf8852011-08-17 17:51:35 -0700862 // If we can treat it as non-range (Jumbo ops will use range form)
863 if (numArgs <= 5)
864 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700865 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700866 /*
867 * Make sure range list doesn't span the break between in normal
868 * Dalvik vRegs and the ins.
869 */
buzbee1b4c8592011-08-31 10:43:51 -0700870 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700871 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700872 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
873 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700874 }
875
876 /*
877 * First load the non-register arguments. Both forms expect all
878 * of the source arguments to be in their home frame location, so
879 * scan the sReg names and flush any that have been promoted to
880 * frame backing storage.
881 */
882 // Scan the rest of the args - if in physReg flush to memory
buzbeec0ecd652011-09-25 18:11:54 -0700883 for (int nextArg = 0; nextArg < numArgs;) {
884 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
buzbee1b4c8592011-08-31 10:43:51 -0700885 if (loc.wide) {
886 loc = oatUpdateLocWide(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700887 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700888 storeBaseDispWide(cUnit, rSP,
889 oatSRegOffset(cUnit, loc.sRegLow),
890 loc.lowReg, loc.highReg);
buzbee1b4c8592011-08-31 10:43:51 -0700891 }
buzbeec0ecd652011-09-25 18:11:54 -0700892 nextArg += 2;
buzbee1b4c8592011-08-31 10:43:51 -0700893 } else {
894 loc = oatUpdateLoc(cUnit, loc);
buzbeec0ecd652011-09-25 18:11:54 -0700895 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee67bc2362011-10-11 18:08:40 -0700896 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
897 loc.lowReg, kWord);
buzbee1b4c8592011-08-31 10:43:51 -0700898 }
buzbeec0ecd652011-09-25 18:11:54 -0700899 nextArg++;
buzbee67bf8852011-08-17 17:51:35 -0700900 }
901 }
902
buzbee67bc2362011-10-11 18:08:40 -0700903 int startOffset = oatSRegOffset(cUnit,
904 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
buzbee67bf8852011-08-17 17:51:35 -0700905 int outsOffset = 4 /* Method* */ + (3 * 4);
906 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700907 // Generate memcpy
908 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
909 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700910 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
911 loadConstant(cUnit, r2, (numArgs - 3) * 4);
Ian Rogersff1ed472011-09-20 13:46:24 -0700912 callRuntimeHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700913 // Restore Method*
914 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700915 } else {
916 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700917 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700918 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700919 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700920 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
921 //TUNING: loosen barrier
922 ld->defMask = ENCODE_ALL;
923 setMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbee561227c2011-09-02 15:28:19 -0700924 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700925 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700926 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbeef48e9712011-09-15 17:54:28 -0700927 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
928 setMemRefType(st, false /* isLoad */, kDalvikReg);
929 st->defMask = ENCODE_ALL;
buzbee561227c2011-09-02 15:28:19 -0700930 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700931 }
932
buzbeec0ecd652011-09-25 18:11:54 -0700933 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
934 rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700935
buzbee561227c2011-09-02 15:28:19 -0700936 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee99f27232011-10-05 12:56:36 -0700937 if (pcrLabel) {
938 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
939 }
buzbee67bf8852011-08-17 17:51:35 -0700940 return callState;
941}
942
buzbee2a475e72011-09-07 17:19:17 -0700943// Debugging routine - if null target, branch to DebugMe
buzbeeed3e9302011-09-23 17:34:19 -0700944STATIC void genShowTarget(CompilationUnit* cUnit)
buzbee2a475e72011-09-07 17:19:17 -0700945{
946 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
947 loadWordDisp(cUnit, rSELF,
948 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
949 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
950 target->defMask = -1;
951 branchOver->generic.target = (LIR*)target;
952}
buzbee2a475e72011-09-07 17:19:17 -0700953
buzbeeed3e9302011-09-23 17:34:19 -0700954STATIC void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700955 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700956{
957 DecodedInstruction* dInsn = &mir->dalvikInsn;
958 int callState = 0;
959 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700960 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700961 NextCallInsn nextCallInsn = nextSDCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -0700962 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee561227c2011-09-02 15:28:19 -0700963
buzbee109bd6a2011-09-06 13:58:41 -0700964 // Explicit register usage
965 oatLockCallTemps(cUnit);
966
buzbee99f27232011-10-05 12:56:36 -0700967 // Is this the special "Ljava/lang/Object;.<init>:()V" case?
968 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT) {
969 int idx = mir->dalvikInsn.vB;
970 Method* target = cUnit->method->GetDexCacheResolvedMethods()->Get(idx);
971 if (target) {
972 if (PrettyMethod(target) == "java.lang.Object.<init>()V") {
973 RegLocation rlArg = oatGetSrc(cUnit, mir, 0);
974 loadValueDirectFixed(cUnit, rlArg, r0);
975 loadWordDisp(cUnit, rSELF,
976 OFFSETOF_MEMBER(Thread, pObjectInit), rLR);
977 genNullCheck(cUnit, oatSSASrc(mir,0), r0, mir);
978 opReg(cUnit, kOpBlx, rLR);
979 oatClobberCalleeSave(cUnit);
980 return;
981 }
982 }
983 }
984
buzbee561227c2011-09-02 15:28:19 -0700985 if (range) {
986 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700987 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700988 } else {
989 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700990 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700991 }
buzbee67bf8852011-08-17 17:51:35 -0700992 // Finish up any of the call sequence not interleaved in arg loading
993 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700994 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700995 }
buzbeece302932011-10-04 14:32:18 -0700996 if (DISPLAY_MISSING_TARGETS) {
997 genShowTarget(cUnit);
998 }
buzbeeec5adf32011-09-11 15:25:43 -0700999 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001000 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001001}
1002
buzbee4a3164f2011-09-03 11:25:10 -07001003/*
1004 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
1005 * which will locate the target and continue on via a tail call.
1006 */
buzbeeed3e9302011-09-23 17:34:19 -07001007STATIC void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001008{
1009 DecodedInstruction* dInsn = &mir->dalvikInsn;
1010 int callState = 0;
1011 ArmLIR* nullCk;
buzbeec0ecd652011-09-25 18:11:54 -07001012 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001013
1014 // Explicit register usage
1015 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001016 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -07001017 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001018 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
1019 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001020 false, nextInterfaceCallInsn, NULL,
buzbee367ce0b2011-09-14 23:19:50 -07001021 false);
buzbee67bf8852011-08-17 17:51:35 -07001022 else
1023 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee367ce0b2011-09-14 23:19:50 -07001024 nextInterfaceCallInsn, NULL, false);
buzbee67bf8852011-08-17 17:51:35 -07001025 // Finish up any of the call sequence not interleaved in arg loading
1026 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001027 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -07001028 }
buzbeece302932011-10-04 14:32:18 -07001029 if (DISPLAY_MISSING_TARGETS) {
1030 genShowTarget(cUnit);
1031 }
buzbeeec5adf32011-09-11 15:25:43 -07001032 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001033 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001034}
1035
buzbeeed3e9302011-09-23 17:34:19 -07001036STATIC void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001037{
1038 DecodedInstruction* dInsn = &mir->dalvikInsn;
1039 int callState = 0;
buzbee4a3164f2011-09-03 11:25:10 -07001040 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001041 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1042 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -07001043 NextCallInsn nextCallInsn;
1044 bool fastPath = true;
buzbeec0ecd652011-09-25 18:11:54 -07001045 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee109bd6a2011-09-06 13:58:41 -07001046
1047 // Explicit register usage
1048 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001049 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
buzbee4a3164f2011-09-03 11:25:10 -07001050 fastPath = false;
1051 } else {
1052 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
1053 if (superClass == NULL) {
1054 fastPath = false;
1055 } else {
1056 int32_t target_idx = baseMethod->GetMethodIndex();
1057 if (superClass->GetVTable()->GetLength() <= target_idx) {
1058 fastPath = false;
1059 } else {
1060 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
1061 }
1062 }
1063 }
1064 if (fastPath) {
1065 nextCallInsn = nextSuperCallInsn;
1066 rollback = NULL;
1067 } else {
1068 nextCallInsn = nextSuperCallInsnSP;
1069 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1070 rollback->defMask = -1;
1071 }
buzbee67bf8852011-08-17 17:51:35 -07001072 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
buzbeec0ecd652011-09-25 18:11:54 -07001073 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001074 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001075 else
buzbeec0ecd652011-09-25 18:11:54 -07001076 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001077 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001078 // Finish up any of the call sequence not interleaved in arg loading
1079 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -07001080 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001081 }
buzbeece302932011-10-04 14:32:18 -07001082 if (DISPLAY_MISSING_TARGETS) {
1083 genShowTarget(cUnit);
1084 }
buzbeeec5adf32011-09-11 15:25:43 -07001085 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001086 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001087}
1088
buzbeeed3e9302011-09-23 17:34:19 -07001089STATIC void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001090{
1091 DecodedInstruction* dInsn = &mir->dalvikInsn;
1092 int callState = 0;
buzbee561227c2011-09-02 15:28:19 -07001093 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001094 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1095 Method* method = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -07001096 NextCallInsn nextCallInsn;
buzbeec0ecd652011-09-25 18:11:54 -07001097 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee7b1b86d2011-08-26 18:59:10 -07001098
buzbee109bd6a2011-09-06 13:58:41 -07001099 // Explicit register usage
1100 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001101 if (SLOW_INVOKE_PATH || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -07001102 // Slow path
1103 nextCallInsn = nextVCallInsnSP;
1104 // If we need a slow-path callout, we'll restart here
1105 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1106 rollback->defMask = -1;
1107 } else {
1108 // Fast path
1109 nextCallInsn = nextVCallInsn;
1110 rollback = NULL;
1111 }
buzbee67bf8852011-08-17 17:51:35 -07001112 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
buzbeec0ecd652011-09-25 18:11:54 -07001113 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001114 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001115 else
buzbeec0ecd652011-09-25 18:11:54 -07001116 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbee1da522d2011-09-04 11:22:20 -07001117 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001118 // Finish up any of the call sequence not interleaved in arg loading
1119 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001120 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001121 }
buzbeece302932011-10-04 14:32:18 -07001122 if (DISPLAY_MISSING_TARGETS) {
1123 genShowTarget(cUnit);
1124 }
buzbeeec5adf32011-09-11 15:25:43 -07001125 opReg(cUnit, kOpBlx, rLR);
buzbee6181f792011-09-29 11:14:04 -07001126 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001127}
1128
buzbeeed3e9302011-09-23 17:34:19 -07001129STATIC bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001130 BasicBlock* bb, ArmLIR* labelList)
1131{
1132 bool res = false; // Assume success
1133 RegLocation rlSrc[3];
1134 RegLocation rlDest = badLoc;
1135 RegLocation rlResult = badLoc;
1136 Opcode opcode = mir->dalvikInsn.opcode;
1137
1138 /* Prep Src and Dest locations */
1139 int nextSreg = 0;
1140 int nextLoc = 0;
1141 int attrs = oatDataFlowAttributes[opcode];
1142 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1143 if (attrs & DF_UA) {
1144 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1145 nextSreg++;
1146 } else if (attrs & DF_UA_WIDE) {
1147 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1148 nextSreg + 1);
1149 nextSreg+= 2;
1150 }
1151 if (attrs & DF_UB) {
1152 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1153 nextSreg++;
1154 } else if (attrs & DF_UB_WIDE) {
1155 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1156 nextSreg + 1);
1157 nextSreg+= 2;
1158 }
1159 if (attrs & DF_UC) {
1160 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1161 } else if (attrs & DF_UC_WIDE) {
1162 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1163 nextSreg + 1);
1164 }
1165 if (attrs & DF_DA) {
1166 rlDest = oatGetDest(cUnit, mir, 0);
1167 } else if (attrs & DF_DA_WIDE) {
1168 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1169 }
1170
1171 switch(opcode) {
1172 case OP_NOP:
1173 break;
1174
1175 case OP_MOVE_EXCEPTION:
1176 int exOffset;
1177 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001178 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001179 resetReg = oatAllocTemp(cUnit);
1180 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1181 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1182 loadConstant(cUnit, resetReg, 0);
1183 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1184 storeValue(cUnit, rlDest, rlResult);
1185 break;
1186
1187 case OP_RETURN_VOID:
buzbeefe2e17f2011-10-10 09:35:02 -07001188 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001189 break;
1190
1191 case OP_RETURN:
1192 case OP_RETURN_OBJECT:
buzbeefe2e17f2011-10-10 09:35:02 -07001193 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001194 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001195 break;
1196
1197 case OP_RETURN_WIDE:
buzbeefe2e17f2011-10-10 09:35:02 -07001198 genSuspendTest(cUnit, mir);
buzbee6181f792011-09-29 11:14:04 -07001199 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001200 break;
1201
1202 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -07001203 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001204 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001205 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001206 break;
1207
1208 case OP_MOVE_RESULT:
1209 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -07001210 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001211 break; // Nop - combined w/ previous invoke
buzbee6181f792011-09-29 11:14:04 -07001212 storeValue(cUnit, rlDest, getRetLoc(cUnit));
buzbee67bf8852011-08-17 17:51:35 -07001213 break;
1214
1215 case OP_MOVE:
1216 case OP_MOVE_OBJECT:
1217 case OP_MOVE_16:
1218 case OP_MOVE_OBJECT_16:
1219 case OP_MOVE_FROM16:
1220 case OP_MOVE_OBJECT_FROM16:
1221 storeValue(cUnit, rlDest, rlSrc[0]);
1222 break;
1223
1224 case OP_MOVE_WIDE:
1225 case OP_MOVE_WIDE_16:
1226 case OP_MOVE_WIDE_FROM16:
1227 storeValueWide(cUnit, rlDest, rlSrc[0]);
1228 break;
1229
1230 case OP_CONST:
1231 case OP_CONST_4:
1232 case OP_CONST_16:
1233 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1234 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1235 storeValue(cUnit, rlDest, rlResult);
1236 break;
1237
1238 case OP_CONST_HIGH16:
1239 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1240 loadConstantNoClobber(cUnit, rlResult.lowReg,
1241 mir->dalvikInsn.vB << 16);
1242 storeValue(cUnit, rlDest, rlResult);
1243 break;
1244
1245 case OP_CONST_WIDE_16:
1246 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -07001247 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1248 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1249 mir->dalvikInsn.vB,
1250 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -07001251 storeValueWide(cUnit, rlDest, rlResult);
1252 break;
1253
1254 case OP_CONST_WIDE:
1255 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1256 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001257 mir->dalvikInsn.vB_wide & 0xffffffff,
1258 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001259 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001260 break;
1261
1262 case OP_CONST_WIDE_HIGH16:
1263 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1264 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1265 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001266 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001267 break;
1268
1269 case OP_MONITOR_ENTER:
1270 genMonitorEnter(cUnit, mir, rlSrc[0]);
1271 break;
1272
1273 case OP_MONITOR_EXIT:
1274 genMonitorExit(cUnit, mir, rlSrc[0]);
1275 break;
1276
1277 case OP_CHECK_CAST:
1278 genCheckCast(cUnit, mir, rlSrc[0]);
1279 break;
1280
1281 case OP_INSTANCE_OF:
1282 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1283 break;
1284
1285 case OP_NEW_INSTANCE:
1286 genNewInstance(cUnit, mir, rlDest);
1287 break;
1288
1289 case OP_THROW:
1290 genThrow(cUnit, mir, rlSrc[0]);
1291 break;
1292
buzbee5ade1d22011-09-09 14:44:52 -07001293 case OP_THROW_VERIFICATION_ERROR:
1294 loadWordDisp(cUnit, rSELF,
1295 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1296 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1297 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -07001298 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001299 break;
1300
buzbee67bf8852011-08-17 17:51:35 -07001301 case OP_ARRAY_LENGTH:
1302 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001303 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001304 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001305 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001306 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1307 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1308 rlResult.lowReg);
1309 storeValue(cUnit, rlDest, rlResult);
1310 break;
1311
1312 case OP_CONST_STRING:
1313 case OP_CONST_STRING_JUMBO:
1314 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1315 break;
1316
1317 case OP_CONST_CLASS:
1318 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1319 break;
1320
1321 case OP_FILL_ARRAY_DATA:
1322 genFillArrayData(cUnit, mir, rlSrc[0]);
1323 break;
1324
1325 case OP_FILLED_NEW_ARRAY:
1326 genFilledNewArray(cUnit, mir, false /* not range */);
1327 break;
1328
1329 case OP_FILLED_NEW_ARRAY_RANGE:
1330 genFilledNewArray(cUnit, mir, true /* range */);
1331 break;
1332
1333 case OP_NEW_ARRAY:
1334 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1335 break;
1336
1337 case OP_GOTO:
1338 case OP_GOTO_16:
1339 case OP_GOTO_32:
buzbeec1f45042011-09-21 16:03:19 -07001340 if (bb->taken->startOffset <= mir->offset) {
1341 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001342 }
1343 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1344 break;
1345
1346 case OP_PACKED_SWITCH:
1347 genPackedSwitch(cUnit, mir, rlSrc[0]);
1348 break;
1349
1350 case OP_SPARSE_SWITCH:
1351 genSparseSwitch(cUnit, mir, rlSrc[0]);
1352 break;
1353
1354 case OP_CMPL_FLOAT:
1355 case OP_CMPG_FLOAT:
1356 case OP_CMPL_DOUBLE:
1357 case OP_CMPG_DOUBLE:
1358 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1359 break;
1360
1361 case OP_CMP_LONG:
1362 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1363 break;
1364
1365 case OP_IF_EQ:
1366 case OP_IF_NE:
1367 case OP_IF_LT:
1368 case OP_IF_GE:
1369 case OP_IF_GT:
1370 case OP_IF_LE: {
1371 bool backwardBranch;
1372 ArmConditionCode cond;
1373 backwardBranch = (bb->taken->startOffset <= mir->offset);
1374 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001375 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001376 }
1377 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1378 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1379 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1380 switch(opcode) {
1381 case OP_IF_EQ:
1382 cond = kArmCondEq;
1383 break;
1384 case OP_IF_NE:
1385 cond = kArmCondNe;
1386 break;
1387 case OP_IF_LT:
1388 cond = kArmCondLt;
1389 break;
1390 case OP_IF_GE:
1391 cond = kArmCondGe;
1392 break;
1393 case OP_IF_GT:
1394 cond = kArmCondGt;
1395 break;
1396 case OP_IF_LE:
1397 cond = kArmCondLe;
1398 break;
1399 default:
1400 cond = (ArmConditionCode)0;
1401 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1402 }
1403 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1404 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1405 break;
1406 }
1407
1408 case OP_IF_EQZ:
1409 case OP_IF_NEZ:
1410 case OP_IF_LTZ:
1411 case OP_IF_GEZ:
1412 case OP_IF_GTZ:
1413 case OP_IF_LEZ: {
1414 bool backwardBranch;
1415 ArmConditionCode cond;
1416 backwardBranch = (bb->taken->startOffset <= mir->offset);
1417 if (backwardBranch) {
buzbeec1f45042011-09-21 16:03:19 -07001418 genSuspendTest(cUnit, mir);
buzbee67bf8852011-08-17 17:51:35 -07001419 }
1420 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1421 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1422 switch(opcode) {
1423 case OP_IF_EQZ:
1424 cond = kArmCondEq;
1425 break;
1426 case OP_IF_NEZ:
1427 cond = kArmCondNe;
1428 break;
1429 case OP_IF_LTZ:
1430 cond = kArmCondLt;
1431 break;
1432 case OP_IF_GEZ:
1433 cond = kArmCondGe;
1434 break;
1435 case OP_IF_GTZ:
1436 cond = kArmCondGt;
1437 break;
1438 case OP_IF_LEZ:
1439 cond = kArmCondLe;
1440 break;
1441 default:
1442 cond = (ArmConditionCode)0;
1443 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1444 }
1445 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1446 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1447 break;
1448 }
1449
1450 case OP_AGET_WIDE:
1451 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1452 break;
1453 case OP_AGET:
1454 case OP_AGET_OBJECT:
1455 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1456 break;
1457 case OP_AGET_BOOLEAN:
1458 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1459 rlDest, 0);
1460 break;
1461 case OP_AGET_BYTE:
1462 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1463 break;
1464 case OP_AGET_CHAR:
1465 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1466 rlDest, 1);
1467 break;
1468 case OP_AGET_SHORT:
1469 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1470 break;
1471 case OP_APUT_WIDE:
1472 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1473 break;
1474 case OP_APUT:
1475 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1476 break;
1477 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001478 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001479 break;
1480 case OP_APUT_SHORT:
1481 case OP_APUT_CHAR:
1482 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1483 rlSrc[0], 1);
1484 break;
1485 case OP_APUT_BYTE:
1486 case OP_APUT_BOOLEAN:
1487 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1488 rlSrc[0], 0);
1489 break;
1490
1491 case OP_IGET_WIDE:
1492 case OP_IGET_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001493 genIGetWide(cUnit, mir, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001494 break;
1495
1496 case OP_IGET:
1497 case OP_IGET_VOLATILE:
1498 case OP_IGET_OBJECT:
1499 case OP_IGET_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001500 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001501 break;
1502
1503 case OP_IGET_BOOLEAN:
1504 case OP_IGET_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001505 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001506 break;
1507
1508 case OP_IGET_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001509 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001510 break;
1511
1512 case OP_IGET_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001513 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001514 break;
1515
1516 case OP_IPUT_WIDE:
1517 case OP_IPUT_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001518 genIPutWide(cUnit, mir, rlSrc[0], rlSrc[1]);
buzbee67bf8852011-08-17 17:51:35 -07001519 break;
1520
1521 case OP_IPUT_OBJECT:
1522 case OP_IPUT_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001523 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
buzbee67bf8852011-08-17 17:51:35 -07001524 break;
1525
1526 case OP_IPUT:
1527 case OP_IPUT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001528 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001529 break;
1530
1531 case OP_IPUT_BOOLEAN:
1532 case OP_IPUT_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001533 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001534 break;
1535
1536 case OP_IPUT_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001537 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001538 break;
1539
1540 case OP_IPUT_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001541 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001542 break;
1543
1544 case OP_SGET:
1545 case OP_SGET_OBJECT:
1546 case OP_SGET_BOOLEAN:
1547 case OP_SGET_BYTE:
1548 case OP_SGET_CHAR:
1549 case OP_SGET_SHORT:
1550 genSget(cUnit, mir, rlResult, rlDest);
1551 break;
1552
1553 case OP_SGET_WIDE:
1554 genSgetWide(cUnit, mir, rlResult, rlDest);
1555 break;
1556
1557 case OP_SPUT:
1558 case OP_SPUT_OBJECT:
1559 case OP_SPUT_BOOLEAN:
1560 case OP_SPUT_BYTE:
1561 case OP_SPUT_CHAR:
1562 case OP_SPUT_SHORT:
1563 genSput(cUnit, mir, rlSrc[0]);
1564 break;
1565
1566 case OP_SPUT_WIDE:
1567 genSputWide(cUnit, mir, rlSrc[0]);
1568 break;
1569
1570 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001571 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1572 true /*range*/);
1573 break;
buzbee67bf8852011-08-17 17:51:35 -07001574 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001575 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1576 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001577 break;
1578
1579 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001580 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1581 false /*range*/);
1582 break;
buzbee67bf8852011-08-17 17:51:35 -07001583 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001584 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1585 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001586 break;
1587
1588 case OP_INVOKE_VIRTUAL:
1589 case OP_INVOKE_VIRTUAL_RANGE:
1590 genInvokeVirtual(cUnit, mir);
1591 break;
1592
1593 case OP_INVOKE_SUPER:
1594 case OP_INVOKE_SUPER_RANGE:
1595 genInvokeSuper(cUnit, mir);
1596 break;
1597
1598 case OP_INVOKE_INTERFACE:
1599 case OP_INVOKE_INTERFACE_RANGE:
1600 genInvokeInterface(cUnit, mir);
1601 break;
1602
1603 case OP_NEG_INT:
1604 case OP_NOT_INT:
1605 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1606 break;
1607
1608 case OP_NEG_LONG:
1609 case OP_NOT_LONG:
1610 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1611 break;
1612
1613 case OP_NEG_FLOAT:
1614 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1615 break;
1616
1617 case OP_NEG_DOUBLE:
1618 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1619 break;
1620
1621 case OP_INT_TO_LONG:
1622 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1623 if (rlSrc[0].location == kLocPhysReg) {
1624 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1625 } else {
1626 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1627 }
1628 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1629 rlResult.lowReg, 31);
1630 storeValueWide(cUnit, rlDest, rlResult);
1631 break;
1632
1633 case OP_LONG_TO_INT:
1634 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1635 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1636 storeValue(cUnit, rlDest, rlSrc[0]);
1637 break;
1638
1639 case OP_INT_TO_BYTE:
1640 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1641 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1642 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1643 storeValue(cUnit, rlDest, rlResult);
1644 break;
1645
1646 case OP_INT_TO_SHORT:
1647 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1648 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1649 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1650 storeValue(cUnit, rlDest, rlResult);
1651 break;
1652
1653 case OP_INT_TO_CHAR:
1654 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1655 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1656 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1657 storeValue(cUnit, rlDest, rlResult);
1658 break;
1659
1660 case OP_INT_TO_FLOAT:
1661 case OP_INT_TO_DOUBLE:
1662 case OP_LONG_TO_FLOAT:
1663 case OP_LONG_TO_DOUBLE:
1664 case OP_FLOAT_TO_INT:
1665 case OP_FLOAT_TO_LONG:
1666 case OP_FLOAT_TO_DOUBLE:
1667 case OP_DOUBLE_TO_INT:
1668 case OP_DOUBLE_TO_LONG:
1669 case OP_DOUBLE_TO_FLOAT:
1670 genConversion(cUnit, mir);
1671 break;
1672
1673 case OP_ADD_INT:
1674 case OP_SUB_INT:
1675 case OP_MUL_INT:
1676 case OP_DIV_INT:
1677 case OP_REM_INT:
1678 case OP_AND_INT:
1679 case OP_OR_INT:
1680 case OP_XOR_INT:
1681 case OP_SHL_INT:
1682 case OP_SHR_INT:
1683 case OP_USHR_INT:
1684 case OP_ADD_INT_2ADDR:
1685 case OP_SUB_INT_2ADDR:
1686 case OP_MUL_INT_2ADDR:
1687 case OP_DIV_INT_2ADDR:
1688 case OP_REM_INT_2ADDR:
1689 case OP_AND_INT_2ADDR:
1690 case OP_OR_INT_2ADDR:
1691 case OP_XOR_INT_2ADDR:
1692 case OP_SHL_INT_2ADDR:
1693 case OP_SHR_INT_2ADDR:
1694 case OP_USHR_INT_2ADDR:
1695 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1696 break;
1697
1698 case OP_ADD_LONG:
1699 case OP_SUB_LONG:
1700 case OP_MUL_LONG:
1701 case OP_DIV_LONG:
1702 case OP_REM_LONG:
1703 case OP_AND_LONG:
1704 case OP_OR_LONG:
1705 case OP_XOR_LONG:
1706 case OP_ADD_LONG_2ADDR:
1707 case OP_SUB_LONG_2ADDR:
1708 case OP_MUL_LONG_2ADDR:
1709 case OP_DIV_LONG_2ADDR:
1710 case OP_REM_LONG_2ADDR:
1711 case OP_AND_LONG_2ADDR:
1712 case OP_OR_LONG_2ADDR:
1713 case OP_XOR_LONG_2ADDR:
1714 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1715 break;
1716
buzbee67bf8852011-08-17 17:51:35 -07001717 case OP_SHL_LONG:
1718 case OP_SHR_LONG:
1719 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001720 case OP_SHL_LONG_2ADDR:
1721 case OP_SHR_LONG_2ADDR:
1722 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001723 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1724 break;
1725
1726 case OP_ADD_FLOAT:
1727 case OP_SUB_FLOAT:
1728 case OP_MUL_FLOAT:
1729 case OP_DIV_FLOAT:
1730 case OP_REM_FLOAT:
1731 case OP_ADD_FLOAT_2ADDR:
1732 case OP_SUB_FLOAT_2ADDR:
1733 case OP_MUL_FLOAT_2ADDR:
1734 case OP_DIV_FLOAT_2ADDR:
1735 case OP_REM_FLOAT_2ADDR:
1736 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1737 break;
1738
1739 case OP_ADD_DOUBLE:
1740 case OP_SUB_DOUBLE:
1741 case OP_MUL_DOUBLE:
1742 case OP_DIV_DOUBLE:
1743 case OP_REM_DOUBLE:
1744 case OP_ADD_DOUBLE_2ADDR:
1745 case OP_SUB_DOUBLE_2ADDR:
1746 case OP_MUL_DOUBLE_2ADDR:
1747 case OP_DIV_DOUBLE_2ADDR:
1748 case OP_REM_DOUBLE_2ADDR:
1749 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1750 break;
1751
1752 case OP_RSUB_INT:
1753 case OP_ADD_INT_LIT16:
1754 case OP_MUL_INT_LIT16:
1755 case OP_DIV_INT_LIT16:
1756 case OP_REM_INT_LIT16:
1757 case OP_AND_INT_LIT16:
1758 case OP_OR_INT_LIT16:
1759 case OP_XOR_INT_LIT16:
1760 case OP_ADD_INT_LIT8:
1761 case OP_RSUB_INT_LIT8:
1762 case OP_MUL_INT_LIT8:
1763 case OP_DIV_INT_LIT8:
1764 case OP_REM_INT_LIT8:
1765 case OP_AND_INT_LIT8:
1766 case OP_OR_INT_LIT8:
1767 case OP_XOR_INT_LIT8:
1768 case OP_SHL_INT_LIT8:
1769 case OP_SHR_INT_LIT8:
1770 case OP_USHR_INT_LIT8:
1771 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1772 break;
1773
1774 default:
1775 res = true;
1776 }
1777 return res;
1778}
1779
buzbeeed3e9302011-09-23 17:34:19 -07001780STATIC const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbee67bf8852011-08-17 17:51:35 -07001781 "kMirOpPhi",
1782 "kMirOpNullNRangeUpCheck",
1783 "kMirOpNullNRangeDownCheck",
1784 "kMirOpLowerBound",
1785 "kMirOpPunt",
1786 "kMirOpCheckInlinePrediction",
1787};
1788
1789/* Extended MIR instructions like PHI */
buzbeeed3e9302011-09-23 17:34:19 -07001790STATIC void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001791{
1792 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1793 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1794 strcpy(msg, extendedMIROpNames[opOffset]);
1795 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1796
1797 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1798 case kMirOpPhi: {
1799 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1800 op->flags.isNop = true;
1801 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1802 break;
1803 }
1804 default:
1805 break;
1806 }
1807}
1808
buzbee67bc2362011-10-11 18:08:40 -07001809/*
1810 * If there are any ins passed in registers that have not been promoted
1811 * to a callee-save register, flush them to the frame. Perform intial
1812 * assignment of promoted arguments.
1813 */
buzbeeed3e9302011-09-23 17:34:19 -07001814STATIC void flushIns(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -07001815{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001816 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001817 return;
buzbee67bc2362011-10-11 18:08:40 -07001818 int firstArgReg = r1;
1819 int lastArgReg = r3;
1820 int startVReg = cUnit->method->NumRegisters() -
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001821 cUnit->method->NumIns();
buzbee8febc582011-10-25 12:39:20 -07001822 /*
1823 * Arguments passed in registers should be flushed
1824 * to their backing locations in the frame for now.
1825 * Also, we need to do initial assignment for promoted
1826 * arguments. NOTE: an older version of dx had an issue
1827 * in which it would reuse static method argument registers.
1828 * This could result in the same Dalvik virtual register
1829 * being promoted to both core and fp regs. In those
1830 * cases, copy argument to both. This will be uncommon
1831 * enough that it isn't worth attempting to optimize.
1832 */
buzbee67bc2362011-10-11 18:08:40 -07001833 for (int i = 0; i < cUnit->method->NumIns(); i++) {
1834 PromotionMap vMap = cUnit->promotionMap[startVReg + i];
buzbee67bc2362011-10-11 18:08:40 -07001835 if (i <= (lastArgReg - firstArgReg)) {
buzbee8febc582011-10-25 12:39:20 -07001836 // If arriving in register
buzbee67bc2362011-10-11 18:08:40 -07001837 if (vMap.coreLocation == kLocPhysReg) {
1838 genRegCopy(cUnit, vMap.coreReg, firstArgReg + i);
buzbee8febc582011-10-25 12:39:20 -07001839 }
1840 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001841 genRegCopy(cUnit, vMap.fpReg, firstArgReg + i);
1842 }
1843 // Also put a copy in memory in case we're partially promoted
1844 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1845 firstArgReg + i, kWord);
1846 } else {
buzbee8febc582011-10-25 12:39:20 -07001847 // If arriving in frame & promoted
buzbee67bc2362011-10-11 18:08:40 -07001848 if (vMap.coreLocation == kLocPhysReg) {
1849 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1850 vMap.coreReg);
buzbee8febc582011-10-25 12:39:20 -07001851 }
1852 if (vMap.fpLocation == kLocPhysReg) {
buzbee67bc2362011-10-11 18:08:40 -07001853 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
1854 vMap.fpReg);
buzbee67bf8852011-08-17 17:51:35 -07001855 }
1856 }
buzbee67bf8852011-08-17 17:51:35 -07001857 }
1858}
1859
1860/* Handle the content in each basic block */
buzbeeed3e9302011-09-23 17:34:19 -07001861STATIC bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -07001862{
1863 MIR* mir;
1864 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1865 int blockId = bb->id;
1866
1867 cUnit->curBlock = bb;
1868 labelList[blockId].operands[0] = bb->startOffset;
1869
1870 /* Insert the block label */
1871 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1872 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1873
buzbee6181f792011-09-29 11:14:04 -07001874 /* Reset local optimization data on block boundaries */
1875 oatResetRegPool(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001876 oatClobberAllRegs(cUnit);
buzbee6181f792011-09-29 11:14:04 -07001877 oatResetDefTracking(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001878
1879 ArmLIR* headLIR = NULL;
1880
buzbeebbaf8942011-10-02 13:08:29 -07001881 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -07001882 if (bb->blockType == kEntryBlock) {
1883 /*
1884 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1885 * mechanism know so it doesn't try to use any of them when
1886 * expanding the frame or flushing. This leaves the utility
1887 * code with a single temp: r12. This should be enough.
1888 */
1889 oatLockTemp(cUnit, r0);
1890 oatLockTemp(cUnit, r1);
1891 oatLockTemp(cUnit, r2);
1892 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001893
1894 /*
1895 * We can safely skip the stack overflow check if we're
1896 * a leaf *and* our frame size < fudge factor.
1897 */
1898 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1899 ((size_t)cUnit->frameSize <
1900 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001901 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001902 if (!skipOverflowCheck) {
1903 /* Load stack limit */
1904 loadWordDisp(cUnit, rSELF,
1905 art::Thread::StackEndOffset().Int32Value(), r12);
1906 }
buzbee67bf8852011-08-17 17:51:35 -07001907 /* Spill core callee saves */
1908 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1909 /* Need to spill any FP regs? */
1910 if (cUnit->numFPSpills) {
buzbeebbaf8942011-10-02 13:08:29 -07001911 /*
1912 * NOTE: fp spills are a little different from core spills in that
1913 * they are pushed as a contiguous block. When promoting from
1914 * the fp set, we must allocate all singles from s16..highest-promoted
1915 */
buzbee67bf8852011-08-17 17:51:35 -07001916 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1917 }
buzbeecefd1872011-09-09 09:59:52 -07001918 if (!skipOverflowCheck) {
1919 opRegRegImm(cUnit, kOpSub, rLR, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001920 cUnit->frameSize - (spillCount * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001921 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1922 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001923 genRegCopy(cUnit, rSP, rLR); // Establish stack
1924 } else {
1925 opRegImm(cUnit, kOpSub, rSP,
buzbeebbaf8942011-10-02 13:08:29 -07001926 cUnit->frameSize - (spillCount * 4));
buzbeecefd1872011-09-09 09:59:52 -07001927 }
buzbee67bf8852011-08-17 17:51:35 -07001928 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1929 flushIns(cUnit);
1930 oatFreeTemp(cUnit, r0);
1931 oatFreeTemp(cUnit, r1);
1932 oatFreeTemp(cUnit, r2);
1933 oatFreeTemp(cUnit, r3);
1934 } else if (bb->blockType == kExitBlock) {
1935 newLIR0(cUnit, kArmPseudoMethodExit);
buzbeebbaf8942011-10-02 13:08:29 -07001936 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4));
buzbee67bf8852011-08-17 17:51:35 -07001937 /* Need to restore any FP callee saves? */
1938 if (cUnit->numFPSpills) {
1939 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1940 }
1941 if (cUnit->coreSpillMask & (1 << rLR)) {
1942 /* Unspill rLR to rPC */
1943 cUnit->coreSpillMask &= ~(1 << rLR);
1944 cUnit->coreSpillMask |= (1 << rPC);
1945 }
1946 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1947 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1948 /* We didn't pop to rPC, so must do a bv rLR */
1949 newLIR1(cUnit, kThumbBx, rLR);
1950 }
1951 }
1952
1953 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1954
1955 oatResetRegPool(cUnit);
buzbeec0ecd652011-09-25 18:11:54 -07001956 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1957 oatClobberAllRegs(cUnit);
1958 }
buzbee67bf8852011-08-17 17:51:35 -07001959
1960 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1961 oatResetDefTracking(cUnit);
1962 }
1963
1964 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1965 handleExtendedMethodMIR(cUnit, mir);
1966 continue;
1967 }
1968
1969 cUnit->currentDalvikOffset = mir->offset;
1970
1971 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1972 InstructionFormat dalvikFormat =
1973 dexGetFormatFromOpcode(dalvikOpcode);
1974
1975 ArmLIR* boundaryLIR;
1976
1977 /* Mark the beginning of a Dalvik instruction for line tracking */
1978 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1979 (int) oatGetDalvikDisassembly(
1980 &mir->dalvikInsn, ""));
1981 /* Remember the first LIR for this block */
1982 if (headLIR == NULL) {
1983 headLIR = boundaryLIR;
1984 /* Set the first boundaryLIR as a scheduling barrier */
1985 headLIR->defMask = ENCODE_ALL;
1986 }
1987
1988 /* Don't generate the SSA annotation unless verbose mode is on */
1989 if (cUnit->printMe && mir->ssaRep) {
1990 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1991 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1992 }
1993
1994 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1995
1996 if (notHandled) {
1997 char buf[100];
1998 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1999 mir->offset,
2000 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
2001 dalvikFormat);
2002 LOG(FATAL) << buf;
2003 }
2004 }
2005
2006 if (headLIR) {
2007 /*
2008 * Eliminate redundant loads/stores and delay stores into later
2009 * slots
2010 */
2011 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
2012 cUnit->lastLIRInsn);
2013
2014 /*
2015 * Generate an unconditional branch to the fallthrough block.
2016 */
2017 if (bb->fallThrough) {
2018 genUnconditionalBranch(cUnit,
2019 &labelList[bb->fallThrough->id]);
2020 }
2021 }
2022 return false;
2023}
2024
2025/*
2026 * Nop any unconditional branches that go to the next instruction.
2027 * Note: new redundant branches may be inserted later, and we'll
2028 * use a check in final instruction assembly to nop those out.
2029 */
2030void removeRedundantBranches(CompilationUnit* cUnit)
2031{
2032 ArmLIR* thisLIR;
2033
2034 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
2035 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
2036 thisLIR = NEXT_LIR(thisLIR)) {
2037
2038 /* Branch to the next instruction */
2039 if ((thisLIR->opcode == kThumbBUncond) ||
2040 (thisLIR->opcode == kThumb2BUncond)) {
2041 ArmLIR* nextLIR = thisLIR;
2042
2043 while (true) {
2044 nextLIR = NEXT_LIR(nextLIR);
2045
2046 /*
2047 * Is the branch target the next instruction?
2048 */
2049 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
2050 thisLIR->flags.isNop = true;
2051 break;
2052 }
2053
2054 /*
2055 * Found real useful stuff between the branch and the target.
2056 * Need to explicitly check the lastLIRInsn here because it
2057 * might be the last real instruction.
2058 */
2059 if (!isPseudoOpcode(nextLIR->opcode) ||
2060 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
2061 break;
2062 }
2063 }
2064 }
2065}
2066
buzbeeed3e9302011-09-23 17:34:19 -07002067STATIC void handleSuspendLaunchpads(CompilationUnit *cUnit)
buzbeec1f45042011-09-21 16:03:19 -07002068{
2069 ArmLIR** suspendLabel =
2070 (ArmLIR **) cUnit->suspendLaunchpads.elemList;
2071 int numElems = cUnit->suspendLaunchpads.numUsed;
2072
2073 for (int i = 0; i < numElems; i++) {
2074 /* TUNING: move suspend count load into helper */
2075 ArmLIR* lab = suspendLabel[i];
2076 ArmLIR* resumeLab = (ArmLIR*)lab->operands[0];
2077 cUnit->currentDalvikOffset = lab->operands[1];
2078 oatAppendLIR(cUnit, (LIR *)lab);
2079 loadWordDisp(cUnit, rSELF,
2080 OFFSETOF_MEMBER(Thread, pTestSuspendFromCode), rLR);
2081 loadWordDisp(cUnit, rSELF,
2082 art::Thread::SuspendCountOffset().Int32Value(), rSUSPEND);
2083 opReg(cUnit, kOpBlx, rLR);
2084 genUnconditionalBranch(cUnit, resumeLab);
2085 }
2086}
2087
buzbeeed3e9302011-09-23 17:34:19 -07002088STATIC void handleThrowLaunchpads(CompilationUnit *cUnit)
buzbee5ade1d22011-09-09 14:44:52 -07002089{
2090 ArmLIR** throwLabel =
2091 (ArmLIR **) cUnit->throwLaunchpads.elemList;
2092 int numElems = cUnit->throwLaunchpads.numUsed;
2093 int i;
2094
2095 for (i = 0; i < numElems; i++) {
2096 ArmLIR* lab = throwLabel[i];
2097 cUnit->currentDalvikOffset = lab->operands[1];
2098 oatAppendLIR(cUnit, (LIR *)lab);
2099 int funcOffset = 0;
2100 int v1 = lab->operands[2];
2101 int v2 = lab->operands[3];
2102 switch(lab->operands[0]) {
2103 case kArmThrowNullPointer:
2104 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2105 break;
2106 case kArmThrowArrayBounds:
2107 if (v2 != r0) {
2108 genRegCopy(cUnit, r0, v1);
2109 genRegCopy(cUnit, r1, v2);
2110 } else {
2111 if (v1 == r1) {
2112 genRegCopy(cUnit, r12, v1);
2113 genRegCopy(cUnit, r1, v2);
2114 genRegCopy(cUnit, r0, r12);
2115 } else {
2116 genRegCopy(cUnit, r1, v2);
2117 genRegCopy(cUnit, r0, v1);
2118 }
2119 }
2120 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2121 break;
2122 case kArmThrowDivZero:
2123 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2124 break;
2125 case kArmThrowVerificationError:
2126 loadConstant(cUnit, r0, v1);
2127 loadConstant(cUnit, r1, v2);
2128 funcOffset =
2129 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2130 break;
2131 case kArmThrowNegArraySize:
2132 genRegCopy(cUnit, r0, v1);
2133 funcOffset =
2134 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2135 break;
buzbee5ade1d22011-09-09 14:44:52 -07002136 case kArmThrowNoSuchMethod:
2137 genRegCopy(cUnit, r0, v1);
2138 funcOffset =
2139 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2140 break;
buzbeeec5adf32011-09-11 15:25:43 -07002141 case kArmThrowStackOverflow:
2142 funcOffset =
Ian Rogers932746a2011-09-22 18:57:50 -07002143 OFFSETOF_MEMBER(Thread, pThrowStackOverflowFromCode);
buzbeeec5adf32011-09-11 15:25:43 -07002144 // Restore stack alignment
buzbeebbaf8942011-10-02 13:08:29 -07002145 opRegImm(cUnit, kOpAdd, rSP,
2146 (cUnit->numCoreSpills + cUnit->numFPSpills) * 4);
buzbeeec5adf32011-09-11 15:25:43 -07002147 break;
buzbee5ade1d22011-09-09 14:44:52 -07002148 default:
2149 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2150 }
2151 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07002152 callRuntimeHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07002153 }
2154}
2155
buzbee67bf8852011-08-17 17:51:35 -07002156void oatMethodMIR2LIR(CompilationUnit* cUnit)
2157{
2158 /* Used to hold the labels of each block */
2159 cUnit->blockLabelList =
2160 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2161
2162 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2163 kPreOrderDFSTraversal, false /* Iterative */);
buzbeec1f45042011-09-21 16:03:19 -07002164 handleSuspendLaunchpads(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002165
2166 handleThrowLaunchpads(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07002167
2168 removeRedundantBranches(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002169}
2170
2171/* Common initialization routine for an architecture family */
2172bool oatArchInit()
2173{
2174 int i;
2175
2176 for (i = 0; i < kArmLast; i++) {
2177 if (EncodingMap[i].opcode != i) {
2178 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2179 " is wrong: expecting " << i << ", seeing " <<
2180 (int)EncodingMap[i].opcode;
2181 }
2182 }
2183
2184 return oatArchVariantInit();
2185}
2186
2187/* Needed by the Assembler */
2188void oatSetupResourceMasks(ArmLIR* lir)
2189{
2190 setupResourceMasks(lir);
2191}
2192
2193/* Needed by the ld/st optmizatons */
2194ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2195{
2196 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2197}
2198
2199/* Needed by the register allocator */
2200ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2201{
2202 return genRegCopy(cUnit, rDest, rSrc);
2203}
2204
2205/* Needed by the register allocator */
2206void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2207 int srcLo, int srcHi)
2208{
2209 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2210}
2211
2212void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2213 int displacement, int rSrc, OpSize size)
2214{
2215 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2216}
2217
2218void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2219 int displacement, int rSrcLo, int rSrcHi)
2220{
2221 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2222}