blob: ce658033ea987171646b150e30e8f9dfa4a84db6 [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
Elliott Hughes1240dad2011-09-09 16:24:50 -070017#define DISPLAY_MISSING_TARGETS 1
18
buzbee67bf8852011-08-17 17:51:35 -070019static const RegLocation badLoc = {kLocDalvikFrame, 0, 0, INVALID_REG,
20 INVALID_REG, INVALID_SREG, 0,
21 kLocDalvikFrame, INVALID_REG, INVALID_REG,
22 INVALID_OFFSET};
23static const RegLocation retLoc = LOC_DALVIK_RETURN_VAL;
24static const RegLocation retLocWide = LOC_DALVIK_RETURN_VAL_WIDE;
25
buzbeedfd3d702011-08-28 12:56:51 -070026/*
27 * Let helper function take care of everything. Will call
28 * Array::AllocFromCode(type_idx, method, count);
29 * Note: AllocFromCode will handle checks for errNegativeArraySize.
30 */
buzbee67bf8852011-08-17 17:51:35 -070031static void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
32 RegLocation rlSrc)
33{
buzbeedfd3d702011-08-28 12:56:51 -070034 oatFlushAllRegs(cUnit); /* Everything to home location */
35 loadWordDisp(cUnit, rSELF,
36 OFFSETOF_MEMBER(Thread, pAllocFromCode), rLR);
37 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
38 loadConstant(cUnit, r0, mir->dalvikInsn.vC); // arg0 <- type_id
39 loadValueDirectFixed(cUnit, rlSrc, r2); // arg2 <- count
buzbeeec5adf32011-09-11 15:25:43 -070040 callUnwindableHelper(cUnit, rLR);
buzbeedfd3d702011-08-28 12:56:51 -070041 oatClobberCallRegs(cUnit);
42 RegLocation rlResult = oatGetReturn(cUnit);
43 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -070044}
45
46/*
47 * Similar to genNewArray, but with post-allocation initialization.
48 * Verifier guarantees we're dealing with an array class. Current
49 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
50 * Current code also throws internal unimp if not 'L', '[' or 'I'.
51 */
52static void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
53{
54 DecodedInstruction* dInsn = &mir->dalvikInsn;
buzbee81eccc02011-09-17 13:42:21 -070055 int elems = dInsn->vA;
56 int typeId = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070057 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbeedfd3d702011-08-28 12:56:51 -070058 loadWordDisp(cUnit, rSELF,
buzbee1da522d2011-09-04 11:22:20 -070059 OFFSETOF_MEMBER(Thread, pCheckAndAllocFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -070060 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
61 loadConstant(cUnit, r0, typeId); // arg0 <- type_id
62 loadConstant(cUnit, r2, elems); // arg2 <- count
buzbeeec5adf32011-09-11 15:25:43 -070063 callUnwindableHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -070064 /*
buzbeedfd3d702011-08-28 12:56:51 -070065 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
66 * return region. Because AllocFromCode placed the new array
67 * in r0, we'll just lock it into place. When debugger support is
68 * added, it may be necessary to additionally copy all return
69 * values to a home location in thread-local storage
buzbee67bf8852011-08-17 17:51:35 -070070 */
buzbee67bf8852011-08-17 17:51:35 -070071 oatLockTemp(cUnit, r0);
buzbeedfd3d702011-08-28 12:56:51 -070072
buzbee67bf8852011-08-17 17:51:35 -070073 // Having a range of 0 is legal
74 if (isRange && (dInsn->vA > 0)) {
75 /*
76 * Bit of ugliness here. We're going generate a mem copy loop
77 * on the register range, but it is possible that some regs
78 * in the range have been promoted. This is unlikely, but
79 * before generating the copy, we'll just force a flush
80 * of any regs in the source range that have been promoted to
81 * home location.
82 */
83 for (unsigned int i = 0; i < dInsn->vA; i++) {
84 RegLocation loc = oatUpdateLoc(cUnit,
85 oatGetSrc(cUnit, mir, i));
86 if (loc.location == kLocPhysReg) {
87 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
88 }
89 }
90 /*
91 * TUNING note: generated code here could be much improved, but
92 * this is an uncommon operation and isn't especially performance
93 * critical.
94 */
95 int rSrc = oatAllocTemp(cUnit);
96 int rDst = oatAllocTemp(cUnit);
97 int rIdx = oatAllocTemp(cUnit);
98 int rVal = rLR; // Using a lot of temps, rLR is known free here
99 // Set up source pointer
100 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
101 opRegRegImm(cUnit, kOpAdd, rSrc, rSP, rlFirst.spOffset);
102 // Set up the target pointer
103 opRegRegImm(cUnit, kOpAdd, rDst, r0,
buzbeec143c552011-08-20 17:38:58 -0700104 Array::DataOffset().Int32Value());
buzbee67bf8852011-08-17 17:51:35 -0700105 // Set up the loop counter (known to be > 0)
106 loadConstant(cUnit, rIdx, dInsn->vA);
107 // Generate the copy loop. Going backwards for convenience
108 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
109 target->defMask = ENCODE_ALL;
110 // Copy next element
111 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
112 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
113 // Use setflags encoding here
114 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
115 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
116 branch->generic.target = (LIR*)target;
117 } else if (!isRange) {
118 // TUNING: interleave
119 for (unsigned int i = 0; i < dInsn->vA; i++) {
120 RegLocation rlArg = loadValue(cUnit,
121 oatGetSrc(cUnit, mir, i), kCoreReg);
buzbeec143c552011-08-20 17:38:58 -0700122 storeBaseDisp(cUnit, r0,
123 Array::DataOffset().Int32Value() +
buzbee67bf8852011-08-17 17:51:35 -0700124 i * 4, rlArg.lowReg, kWord);
125 // If the loadValue caused a temp to be allocated, free it
126 if (oatIsTemp(cUnit, rlArg.lowReg)) {
127 oatFreeTemp(cUnit, rlArg.lowReg);
128 }
129 }
130 }
131}
132
Brian Carlstrom845490b2011-09-19 15:56:53 -0700133Field* FindFieldWithResolvedStaticStorage(const Method* method,
134 const uint32_t fieldIdx,
135 uint32_t& resolvedTypeIdx) {
136 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
137 Field* field = class_linker->ResolveField(fieldIdx, method, true);
138 if (field == NULL) {
139 return NULL;
140 }
141 const art::DexFile& dex_file = class_linker->
142 FindDexFile(method->GetDeclaringClass()->GetDexCache());
143 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
144 int type_idx = field_id.class_idx_;
145 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
146 // Check if storage class is the same as class referred to by type idx.
147 // They may not be if the FieldId refers a subclass, but storage is in super
148 if (field->GetDeclaringClass() == klass) {
149 resolvedTypeIdx = type_idx;
150 return field;
151 }
152 // See if we can find a dex reference for the storage class.
153 // we may not if the dex file never references the super class,
154 // but usually it will.
155 std::string descriptor = field->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8();
156 for (size_t type_idx = 0; type_idx < dex_file.NumTypeIds(); type_idx++) {
157 const art::DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx);
158 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
159 resolvedTypeIdx = type_idx;
160 return field;
161 }
162 }
163 return NULL; // resort to slow path
164}
165
buzbee67bf8852011-08-17 17:51:35 -0700166static void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
167{
buzbeee1931742011-08-28 21:15:53 -0700168 bool isObject = ((mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
169 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE));
buzbee1da522d2011-09-04 11:22:20 -0700170 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700171 uint32_t typeIdx;
172 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
173 if (SLOW_FIELD_PATH || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700174 // Slow path
buzbee34cd9e52011-09-08 14:31:52 -0700175 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
176 << " unresolved at compile time";
buzbee1da522d2011-09-04 11:22:20 -0700177 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
178 : OFFSETOF_MEMBER(Thread, pSet32Static);
buzbeee1931742011-08-28 21:15:53 -0700179 oatFlushAllRegs(cUnit);
180 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
181 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
182 loadCurrMethodDirect(cUnit, r1);
183 loadValueDirect(cUnit, rlSrc, r2);
buzbeeec5adf32011-09-11 15:25:43 -0700184 callUnwindableHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700185 oatClobberCallRegs(cUnit);
186 } else {
buzbee1da522d2011-09-04 11:22:20 -0700187 // fast path
188 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700189 // Using fixed register to sync with slow path
190 int rMethod = r1;
191 oatLockTemp(cUnit, rMethod);
192 loadCurrMethodDirect(cUnit, rMethod);
193 int rBase = r0;
194 oatLockTemp(cUnit, rBase);
195 loadWordDisp(cUnit, rMethod,
196 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
197 rBase);
198 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
199 sizeof(int32_t*)* typeIdx, rBase);
200 // TUNING: fast path should fall through
201 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
202 loadWordDisp(cUnit, rSELF,
203 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
204 loadConstant(cUnit, r0, typeIdx);
buzbeeec5adf32011-09-11 15:25:43 -0700205 callUnwindableHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700206 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
207 skipTarget->defMask = ENCODE_ALL;
208 branchOver->generic.target = (LIR*)skipTarget;
209 rlSrc = oatGetSrc(cUnit, mir, 0);
210 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
211 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700212#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700213 if (field->IsVolatile()) {
214 oatGenMemBarrier(cUnit, kSY);
215 }
buzbee67bf8852011-08-17 17:51:35 -0700216#endif
buzbee1da522d2011-09-04 11:22:20 -0700217 if (isObject) {
218 markGCCard(cUnit, rlSrc.lowReg, rBase);
219 }
220 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700221 }
buzbee67bf8852011-08-17 17:51:35 -0700222}
223
224static void genSputWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
225{
buzbee1da522d2011-09-04 11:22:20 -0700226 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700227 uint32_t typeIdx;
228 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee34cd9e52011-09-08 14:31:52 -0700229 if (SLOW_FIELD_PATH || field == NULL) {
230 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
231 << " unresolved at compile time";
buzbeee1931742011-08-28 21:15:53 -0700232 oatFlushAllRegs(cUnit);
buzbee1da522d2011-09-04 11:22:20 -0700233 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pSet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700234 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
235 loadCurrMethodDirect(cUnit, r1);
236 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
buzbeeec5adf32011-09-11 15:25:43 -0700237 callUnwindableHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700238 oatClobberCallRegs(cUnit);
239 } else {
buzbee1da522d2011-09-04 11:22:20 -0700240 // fast path
241 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700242 // Using fixed register to sync with slow path
243 int rMethod = r1;
244 oatLockTemp(cUnit, rMethod);
245 loadCurrMethodDirect(cUnit, r1);
246 int rBase = r0;
247 oatLockTemp(cUnit, rBase);
248 loadWordDisp(cUnit, rMethod,
249 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
250 rBase);
251 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
252 sizeof(int32_t*)* typeIdx, rBase);
253 // TUNING: fast path should fall through
254 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
255 loadWordDisp(cUnit, rSELF,
256 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
257 loadConstant(cUnit, r0, typeIdx);
buzbeeec5adf32011-09-11 15:25:43 -0700258 callUnwindableHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700259 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
260 skipTarget->defMask = ENCODE_ALL;
261 branchOver->generic.target = (LIR*)skipTarget;
262 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
263 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
264 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
265 rlSrc.highReg);
266#if ANDROID_SMP != 0
267 if (field->IsVolatile()) {
268 oatGenMemBarrier(cUnit, kSY);
269 }
buzbeec143c552011-08-20 17:38:58 -0700270#endif
buzbee1da522d2011-09-04 11:22:20 -0700271 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700272 }
buzbee67bf8852011-08-17 17:51:35 -0700273}
274
275
buzbee67bf8852011-08-17 17:51:35 -0700276static void genSgetWide(CompilationUnit* cUnit, MIR* mir,
277 RegLocation rlResult, RegLocation rlDest)
278{
buzbee1da522d2011-09-04 11:22:20 -0700279 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700280 uint32_t typeIdx;
281 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbee34cd9e52011-09-08 14:31:52 -0700282 if (SLOW_FIELD_PATH || field == NULL) {
283 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
284 << " unresolved at compile time";
buzbeee1931742011-08-28 21:15:53 -0700285 oatFlushAllRegs(cUnit);
buzbee1da522d2011-09-04 11:22:20 -0700286 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pGet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700287 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
288 loadCurrMethodDirect(cUnit, r1);
buzbeeec5adf32011-09-11 15:25:43 -0700289 callUnwindableHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700290 RegLocation rlResult = oatGetReturnWide(cUnit);
291 storeValueWide(cUnit, rlDest, rlResult);
292 } else {
buzbee1da522d2011-09-04 11:22:20 -0700293 // Fast path
294 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700295 // Using fixed register to sync with slow path
296 int rMethod = r1;
297 oatLockTemp(cUnit, rMethod);
298 loadCurrMethodDirect(cUnit, rMethod);
299 int rBase = r0;
300 oatLockTemp(cUnit, rBase);
301 loadWordDisp(cUnit, rMethod,
302 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
303 rBase);
304 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
305 sizeof(int32_t*)* typeIdx, rBase);
306 // TUNING: fast path should fall through
307 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
308 loadWordDisp(cUnit, rSELF,
309 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
310 loadConstant(cUnit, r0, typeIdx);
buzbeeec5adf32011-09-11 15:25:43 -0700311 callUnwindableHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700312 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
313 skipTarget->defMask = ENCODE_ALL;
314 branchOver->generic.target = (LIR*)skipTarget;
315 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
316 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
317#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700318 if (field->IsVolatile()) {
buzbee1da522d2011-09-04 11:22:20 -0700319 oatGenMemBarrier(cUnit, kSY);
320 }
buzbeec143c552011-08-20 17:38:58 -0700321#endif
buzbee1da522d2011-09-04 11:22:20 -0700322 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
323 rlResult.highReg, INVALID_SREG);
324 oatFreeTemp(cUnit, rBase);
325 storeValueWide(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700326 }
buzbee67bf8852011-08-17 17:51:35 -0700327}
328
329static void genSget(CompilationUnit* cUnit, MIR* mir,
330 RegLocation rlResult, RegLocation rlDest)
331{
buzbee1da522d2011-09-04 11:22:20 -0700332 int fieldIdx = mir->dalvikInsn.vB;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700333 uint32_t typeIdx;
334 Field* field = FindFieldWithResolvedStaticStorage(cUnit->method, fieldIdx, typeIdx);
buzbeee1931742011-08-28 21:15:53 -0700335 bool isObject = ((mir->dalvikInsn.opcode == OP_SGET_OBJECT) ||
336 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE));
buzbee34cd9e52011-09-08 14:31:52 -0700337 if (SLOW_FIELD_PATH || field == NULL) {
338 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
339 << " unresolved at compile time";
buzbee1da522d2011-09-04 11:22:20 -0700340 // Slow path
341 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
342 : OFFSETOF_MEMBER(Thread, pGet32Static);
buzbeee1931742011-08-28 21:15:53 -0700343 oatFlushAllRegs(cUnit);
344 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
345 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
346 loadCurrMethodDirect(cUnit, r1);
buzbeeec5adf32011-09-11 15:25:43 -0700347 callUnwindableHelper(cUnit, rLR);
buzbeee1931742011-08-28 21:15:53 -0700348 RegLocation rlResult = oatGetReturn(cUnit);
349 storeValue(cUnit, rlDest, rlResult);
350 } else {
buzbee1da522d2011-09-04 11:22:20 -0700351 // Fast path
352 int fieldOffset = field->GetOffset().Int32Value();
buzbee1da522d2011-09-04 11:22:20 -0700353 // Using fixed register to sync with slow path
354 int rMethod = r1;
355 oatLockTemp(cUnit, rMethod);
356 loadCurrMethodDirect(cUnit, rMethod);
357 int rBase = r0;
358 oatLockTemp(cUnit, rBase);
359 loadWordDisp(cUnit, rMethod,
360 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
361 rBase);
362 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
363 sizeof(int32_t*)* typeIdx, rBase);
364 // TUNING: fast path should fall through
365 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
366 loadWordDisp(cUnit, rSELF,
367 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
368 loadConstant(cUnit, r0, typeIdx);
buzbeeec5adf32011-09-11 15:25:43 -0700369 callUnwindableHelper(cUnit, rLR);
buzbee1da522d2011-09-04 11:22:20 -0700370 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
371 skipTarget->defMask = ENCODE_ALL;
372 branchOver->generic.target = (LIR*)skipTarget;
373 rlDest = oatGetDest(cUnit, mir, 0);
374 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700375#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700376 if (field->IsVolatile()) {
buzbee1da522d2011-09-04 11:22:20 -0700377 oatGenMemBarrier(cUnit, kSY);
378 }
buzbee67bf8852011-08-17 17:51:35 -0700379#endif
buzbee1da522d2011-09-04 11:22:20 -0700380 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
381 oatFreeTemp(cUnit, rBase);
382 storeValue(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700383 }
buzbee67bf8852011-08-17 17:51:35 -0700384}
385
buzbee561227c2011-09-02 15:28:19 -0700386typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int,
387 ArmLIR*);
buzbee67bf8852011-08-17 17:51:35 -0700388
389/*
390 * Bit of a hack here - in leiu of a real scheduling pass,
391 * emit the next instruction in static & direct invoke sequences.
392 */
393static int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700394 DecodedInstruction* dInsn, int state,
395 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700396{
buzbee561227c2011-09-02 15:28:19 -0700397 DCHECK(rollback == NULL);
398 uint32_t idx = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -0700399 switch(state) {
400 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700401 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700402 break;
buzbee561227c2011-09-02 15:28:19 -0700403 case 1: // Get method->code_and_direct_methods_
404 loadWordDisp(cUnit, r0,
405 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
406 r0);
buzbee67bf8852011-08-17 17:51:35 -0700407 break;
buzbee561227c2011-09-02 15:28:19 -0700408 case 2: // Grab target method* and target code_
409 loadWordDisp(cUnit, r0,
410 art::CodeAndDirectMethods::CodeOffsetInBytes(idx), rLR);
411 loadWordDisp(cUnit, r0,
412 art::CodeAndDirectMethods::MethodOffsetInBytes(idx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700413 break;
414 default:
415 return -1;
416 }
417 return state + 1;
418}
419
buzbee67bf8852011-08-17 17:51:35 -0700420/*
421 * Bit of a hack here - in leiu of a real scheduling pass,
422 * emit the next instruction in a virtual invoke sequence.
423 * We can use rLR as a temp prior to target address loading
424 * Note also that we'll load the first argument ("this") into
425 * r1 here rather than the standard loadArgRegs.
426 */
427static int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700428 DecodedInstruction* dInsn, int state,
429 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700430{
buzbee561227c2011-09-02 15:28:19 -0700431 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700432 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700433 /*
434 * This is the fast path in which the target virtual method is
435 * fully resolved at compile time.
436 */
Brian Carlstrom845490b2011-09-19 15:56:53 -0700437 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
438 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -0700439 CHECK(baseMethod != NULL);
440 uint32_t target_idx = baseMethod->GetMethodIndex();
buzbee67bf8852011-08-17 17:51:35 -0700441 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700442 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700443 rlArg = oatGetSrc(cUnit, mir, 0);
444 loadValueDirectFixed(cUnit, rlArg, r1);
445 break;
buzbee561227c2011-09-02 15:28:19 -0700446 case 1: // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700447 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee561227c2011-09-02 15:28:19 -0700448 // get this->klass_ [use r1, set rLR]
449 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700450 break;
buzbee561227c2011-09-02 15:28:19 -0700451 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
452 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700453 break;
buzbee561227c2011-09-02 15:28:19 -0700454 case 3: // Get target method [use rLR, set r0]
455 loadWordDisp(cUnit, rLR, (target_idx * 4) +
456 art::Array::DataOffset().Int32Value(), r0);
457 break;
458 case 4: // Get the target compiled code address [uses r0, sets rLR]
459 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700460 break;
461 default:
462 return -1;
463 }
464 return state + 1;
465}
466
buzbee7b1b86d2011-08-26 18:59:10 -0700467static int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700468 DecodedInstruction* dInsn, int state,
469 ArmLIR* rollback)
buzbee7b1b86d2011-08-26 18:59:10 -0700470{
buzbee561227c2011-09-02 15:28:19 -0700471 DCHECK(rollback != NULL);
buzbee7b1b86d2011-08-26 18:59:10 -0700472 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700473 ArmLIR* skipBranch;
474 ArmLIR* skipTarget;
475 /*
476 * This handles the case in which the base method is not fully
477 * resolved at compile time. We must generate code to test
478 * for resolution a run time, bail to the slow path if not to
479 * fill in all the tables. In the latter case, we'll restart at
480 * at the beginning of the sequence.
481 */
buzbee7b1b86d2011-08-26 18:59:10 -0700482 switch(state) {
483 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700484 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700485 break;
buzbee561227c2011-09-02 15:28:19 -0700486 case 1: // Get method->dex_cache_resolved_methods_
487 loadWordDisp(cUnit, r0,
488 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700489 break;
buzbee561227c2011-09-02 15:28:19 -0700490 case 2: // method->dex_cache_resolved_methods_->Get(method_idx)
491 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
492 art::Array::DataOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700493 break;
buzbee561227c2011-09-02 15:28:19 -0700494 case 3: // Resolved?
495 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
496 // Slowest path, bail to helper, rollback and retry
497 loadWordDisp(cUnit, rSELF,
498 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
499 loadConstant(cUnit, r1, dInsn->vB);
buzbeeec5adf32011-09-11 15:25:43 -0700500 callUnwindableHelper(cUnit, rLR);
buzbee561227c2011-09-02 15:28:19 -0700501 genUnconditionalBranch(cUnit, rollback);
502 // Resume normal slow path
503 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
504 skipTarget->defMask = ENCODE_ALL;
505 skipBranch->generic.target = (LIR*)skipTarget;
buzbee4a3164f2011-09-03 11:25:10 -0700506 // Get base_method->method_index [usr rLR, set r0]
buzbee561227c2011-09-02 15:28:19 -0700507 loadBaseDisp(cUnit, mir, rLR,
508 Method::GetMethodIndexOffset().Int32Value(), r0,
509 kUnsignedHalf, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700510 // Load "this" [set r1]
511 rlArg = oatGetSrc(cUnit, mir, 0);
512 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee7b1b86d2011-08-26 18:59:10 -0700513 break;
514 case 4:
515 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700516 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee7b1b86d2011-08-26 18:59:10 -0700517 // get this->clazz [use r1, set rLR]
buzbee561227c2011-09-02 15:28:19 -0700518 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700519 break;
buzbee561227c2011-09-02 15:28:19 -0700520 case 5:
521 // get this->klass_->vtable_ [usr rLR, set rLR]
522 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
523 DCHECK((art::Array::DataOffset().Int32Value() & 0x3) == 0);
524 // In load shadow fold vtable_ object header size into method_index_
525 opRegImm(cUnit, kOpAdd, r0,
526 art::Array::DataOffset().Int32Value() / 4);
527 // Get target Method*
528 loadBaseIndexed(cUnit, rLR, r0, r0, 2, kWord);
529 break;
530 case 6: // Get the target compiled code address [uses r0, sets rLR]
531 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700532 break;
533 default:
534 return -1;
535 }
536 return state + 1;
537}
538
buzbee67bf8852011-08-17 17:51:35 -0700539/* Load up to 3 arguments in r1..r3 */
540static int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
541 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700542 int *args, NextCallInsn nextCallInsn, ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700543{
544 for (int i = 0; i < 3; i++) {
545 if (args[i] != INVALID_REG) {
buzbee1b4c8592011-08-31 10:43:51 -0700546 // Arguments are treated as a series of untyped 32-bit values.
buzbeee9a72f62011-09-04 17:59:07 -0700547 RegLocation rlArg = oatGetRawSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700548 rlArg.wide = false;
buzbee67bf8852011-08-17 17:51:35 -0700549 loadValueDirectFixed(cUnit, rlArg, r1 + i);
buzbee561227c2011-09-02 15:28:19 -0700550 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700551 }
552 }
553 return callState;
554}
555
buzbee4a3164f2011-09-03 11:25:10 -0700556// Interleave launch code for INVOKE_INTERFACE.
buzbee67bf8852011-08-17 17:51:35 -0700557static int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700558 DecodedInstruction* dInsn, int state,
559 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700560{
buzbee67bf8852011-08-17 17:51:35 -0700561 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700562 case 0: // Load trampoline target
563 loadWordDisp(cUnit, rSELF,
564 OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline),
565 rLR);
566 // Load r0 with method index
567 loadConstant(cUnit, r0, dInsn->vB);
buzbee67bf8852011-08-17 17:51:35 -0700568 break;
buzbee67bf8852011-08-17 17:51:35 -0700569 default:
570 return -1;
571 }
572 return state + 1;
573}
574
buzbee67bf8852011-08-17 17:51:35 -0700575/*
576 * Interleave launch code for INVOKE_SUPER. See comments
577 * for nextVCallIns.
578 */
579static int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700580 DecodedInstruction* dInsn, int state,
581 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700582{
buzbee4a3164f2011-09-03 11:25:10 -0700583 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700584 RegLocation rlArg;
buzbee4a3164f2011-09-03 11:25:10 -0700585 /*
586 * This is the fast path in which the target virtual method is
587 * fully resolved at compile time. Note also that this path assumes
588 * that the check to verify that the target method index falls
589 * within the size of the super's vtable has been done at compile-time.
590 */
Brian Carlstrom845490b2011-09-19 15:56:53 -0700591 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
592 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -0700593 CHECK(baseMethod != NULL);
594 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
595 CHECK(superClass != NULL);
596 int32_t target_idx = baseMethod->GetMethodIndex();
597 CHECK(superClass->GetVTable()->GetLength() > target_idx);
598 Method* targetMethod = superClass->GetVTable()->Get(target_idx);
599 CHECK(targetMethod != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700600 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700601 case 0: // Get current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700602 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700603 // Load "this" [set r1]
604 rlArg = oatGetSrc(cUnit, mir, 0);
605 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee4a3164f2011-09-03 11:25:10 -0700606 // Get method->declaring_class_ [use r0, set rLR]
607 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
608 rLR);
buzbee67bf8852011-08-17 17:51:35 -0700609 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700610 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee4a3164f2011-09-03 11:25:10 -0700611 break;
612 case 1: // Get method->declaring_class_->super_class [usr rLR, set rLR]
613 loadWordDisp(cUnit, rLR, Class::SuperClassOffset().Int32Value(),
614 rLR);
615 break;
616 case 2: // Get ...->super_class_->vtable [u/s rLR]
617 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
618 break;
619 case 3: // Get target method [use rLR, set r0]
620 loadWordDisp(cUnit, rLR, (target_idx * 4) +
621 art::Array::DataOffset().Int32Value(), r0);
622 break;
623 case 4: // Get the target compiled code address [uses r0, sets rLR]
624 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
625 break;
buzbee67bf8852011-08-17 17:51:35 -0700626 default:
627 return -1;
628 }
buzbee4a3164f2011-09-03 11:25:10 -0700629 return state + 1;
630}
631
632/* Slow-path version of nextSuperCallInsn */
633static int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
634 DecodedInstruction* dInsn, int state,
635 ArmLIR* rollback)
636{
637 DCHECK(rollback != NULL);
638 RegLocation rlArg;
639 ArmLIR* skipBranch;
640 ArmLIR* skipTarget;
641 int tReg;
642 /*
643 * This handles the case in which the base method is not fully
644 * resolved at compile time. We must generate code to test
645 * for resolution a run time, bail to the slow path if not to
646 * fill in all the tables. In the latter case, we'll restart at
647 * at the beginning of the sequence.
648 */
649 switch(state) {
650 case 0: // Get the current Method* [sets r0]
651 loadCurrMethodDirect(cUnit, r0);
652 break;
653 case 1: // Get method->dex_cache_resolved_methods_ [usr r0, set rLR]
654 loadWordDisp(cUnit, r0,
655 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
656 break;
657 case 2: // method->dex_cache_resolved_methods_->Get(meth_idx) [u/s rLR]
658 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
659 art::Array::DataOffset().Int32Value(), rLR);
660 break;
661 case 3: // Resolved?
662 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
663 // Slowest path, bail to helper, rollback and retry
664 loadWordDisp(cUnit, rSELF,
665 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
666 loadConstant(cUnit, r1, dInsn->vB);
buzbeeec5adf32011-09-11 15:25:43 -0700667 callUnwindableHelper(cUnit, rLR);
buzbee4a3164f2011-09-03 11:25:10 -0700668 genUnconditionalBranch(cUnit, rollback);
669 // Resume normal slow path
670 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
671 skipTarget->defMask = ENCODE_ALL;
672 skipBranch->generic.target = (LIR*)skipTarget;
673 // Get base_method->method_index [usr rLR, set rLR]
674 loadBaseDisp(cUnit, mir, rLR,
675 Method::GetMethodIndexOffset().Int32Value(), rLR,
676 kUnsignedHalf, INVALID_SREG);
677 // Load "this" [set r1]
678 rlArg = oatGetSrc(cUnit, mir, 0);
679 loadValueDirectFixed(cUnit, rlArg, r1);
680 // Load curMethod->declaring_class_ [uses r0, sets r0]
681 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
682 r0);
buzbee6a0f7f52011-09-05 16:14:20 -0700683 // Null this?
buzbee5ade1d22011-09-09 14:44:52 -0700684 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee6a0f7f52011-09-05 16:14:20 -0700685 // Get method->declaring_class_->super_class [usr r0, set r0]
buzbee4a3164f2011-09-03 11:25:10 -0700686 loadWordDisp(cUnit, r0, Class::SuperClassOffset().Int32Value(), r0);
687 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700688 case 4: // Get ...->super_class_->vtable [u/s r0]
buzbee4a3164f2011-09-03 11:25:10 -0700689 loadWordDisp(cUnit, r0, Class::VTableOffset().Int32Value(), r0);
buzbee43a36422011-09-14 14:00:13 -0700690 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee4a3164f2011-09-03 11:25:10 -0700691 // Range check, throw NSM on failure
692 tReg = oatAllocTemp(cUnit);
693 loadWordDisp(cUnit, r0, art::Array::LengthOffset().Int32Value(),
694 tReg);
buzbeeec5adf32011-09-11 15:25:43 -0700695 genRegRegCheck(cUnit, kArmCondCs, tReg, rLR, mir,
696 kArmThrowNoSuchMethod);
buzbee4a3164f2011-09-03 11:25:10 -0700697 oatFreeTemp(cUnit, tReg);
698 }
buzbee6a0f7f52011-09-05 16:14:20 -0700699 // Adjust vtable_ base past object header
700 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
buzbee4a3164f2011-09-03 11:25:10 -0700701 // Get target Method*
buzbee6a0f7f52011-09-05 16:14:20 -0700702 loadBaseIndexed(cUnit, r0, rLR, r0, 2, kWord);
buzbee4a3164f2011-09-03 11:25:10 -0700703 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700704 case 5: // Get the target compiled code address [uses r0, sets rLR]
buzbee4a3164f2011-09-03 11:25:10 -0700705 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
706 break;
707 default:
708 return -1;
709 }
buzbee67bf8852011-08-17 17:51:35 -0700710 return state + 1;
711}
712
713/*
714 * Load up to 5 arguments, the first three of which will be in
715 * r1 .. r3. On entry r0 contains the current method pointer,
716 * and as part of the load sequence, it must be replaced with
717 * the target method pointer. Note, this may also be called
718 * for "range" variants if the number of arguments is 5 or fewer.
719 */
720static int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
721 DecodedInstruction* dInsn, int callState,
722 ArmLIR** pcrLabel, bool isRange,
buzbee1da522d2011-09-04 11:22:20 -0700723 NextCallInsn nextCallInsn, ArmLIR* rollback,
724 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700725{
726 RegLocation rlArg;
727 int registerArgs[3];
728
729 /* If no arguments, just return */
730 if (dInsn->vA == 0)
731 return callState;
732
buzbee561227c2011-09-02 15:28:19 -0700733 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700734
735 /*
736 * Load frame arguments arg4 & arg5 first. Coded a little odd to
737 * pre-schedule the method pointer target.
738 */
739 for (unsigned int i=3; i < dInsn->vA; i++) {
740 int reg;
buzbeeec5adf32011-09-11 15:25:43 -0700741 // Treating args as untyped 32-bit chunks
742 rlArg = oatGetRawSrc(cUnit, mir, i);
743 rlArg.wide = false;
744 rlArg = oatUpdateLoc(cUnit, rlArg);
buzbee67bf8852011-08-17 17:51:35 -0700745 if (rlArg.location == kLocPhysReg) {
746 reg = rlArg.lowReg;
747 } else {
buzbee109bd6a2011-09-06 13:58:41 -0700748 // r3 is the last arg register loaded, so can safely be used here
749 reg = r3;
750 loadValueDirectFixed(cUnit, rlArg, reg);
buzbee561227c2011-09-02 15:28:19 -0700751 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700752 }
753 storeBaseDisp(cUnit, rSP, (i + 1) * 4, reg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700754 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700755 }
756
757 /* Load register arguments r1..r3 */
buzbeee9a72f62011-09-04 17:59:07 -0700758 for (unsigned int i = 0; i < 3; i++) {
buzbee67bf8852011-08-17 17:51:35 -0700759 if (i < dInsn->vA)
760 registerArgs[i] = (isRange) ? dInsn->vC + i : i;
761 else
762 registerArgs[i] = INVALID_REG;
763 }
buzbeee9a72f62011-09-04 17:59:07 -0700764 if (skipThis) {
765 registerArgs[0] = INVALID_REG;
766 }
buzbee67bf8852011-08-17 17:51:35 -0700767 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
buzbee561227c2011-09-02 15:28:19 -0700768 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700769
buzbee6a0f7f52011-09-05 16:14:20 -0700770 //TODO: better to move this into CallInsn lists
buzbee67bf8852011-08-17 17:51:35 -0700771 // Load direct & need a "this" null check?
772 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700773 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700774 }
775 return callState;
776}
777
778/*
779 * May have 0+ arguments (also used for jumbo). Note that
780 * source virtual registers may be in physical registers, so may
781 * need to be flushed to home location before copying. This
782 * applies to arg3 and above (see below).
783 *
784 * Two general strategies:
785 * If < 20 arguments
786 * Pass args 3-18 using vldm/vstm block copy
787 * Pass arg0, arg1 & arg2 in r1-r3
788 * If 20+ arguments
789 * Pass args arg19+ using memcpy block copy
790 * Pass arg0, arg1 & arg2 in r1-r3
791 *
792 */
793static int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
794 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700795 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700796 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700797{
798 int firstArg = dInsn->vC;
799 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700800 int registerArgs[3];
801
buzbee67bf8852011-08-17 17:51:35 -0700802 // If we can treat it as non-range (Jumbo ops will use range form)
803 if (numArgs <= 5)
804 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700805 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700806 /*
807 * Make sure range list doesn't span the break between in normal
808 * Dalvik vRegs and the ins.
809 */
buzbee1b4c8592011-08-31 10:43:51 -0700810 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700811 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700812 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
813 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700814 }
815
816 /*
817 * First load the non-register arguments. Both forms expect all
818 * of the source arguments to be in their home frame location, so
819 * scan the sReg names and flush any that have been promoted to
820 * frame backing storage.
821 */
822 // Scan the rest of the args - if in physReg flush to memory
buzbee0c7f26d2011-09-07 12:28:51 -0700823 for (int i = 3; i < numArgs; i++) {
buzbeee9a72f62011-09-04 17:59:07 -0700824 RegLocation loc = oatGetRawSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700825 if (loc.wide) {
826 loc = oatUpdateLocWide(cUnit, loc);
827 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
828 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
829 loc.highReg);
buzbee561227c2011-09-02 15:28:19 -0700830 callState = nextCallInsn(cUnit, mir, dInsn, callState,
831 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700832 }
833 } else {
834 loc = oatUpdateLoc(cUnit, loc);
835 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
836 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700837 callState = nextCallInsn(cUnit, mir, dInsn, callState,
838 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700839 }
buzbee67bf8852011-08-17 17:51:35 -0700840 }
841 }
842
843 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
844 int outsOffset = 4 /* Method* */ + (3 * 4);
845 if (numArgs >= 20) {
buzbeec0fe6c72011-09-18 20:19:14 -0700846 // Generate memcpy
847 opRegRegImm(cUnit, kOpAdd, r0, rSP, outsOffset);
848 opRegRegImm(cUnit, kOpAdd, r1, rSP, startOffset);
buzbee67bf8852011-08-17 17:51:35 -0700849 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
850 loadConstant(cUnit, r2, (numArgs - 3) * 4);
buzbeeec5adf32011-09-11 15:25:43 -0700851 callNoUnwindHelper(cUnit, rLR);
buzbee010cffc2011-09-21 18:28:43 -0700852 // Restore Method*
853 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700854 } else {
855 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700856 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700857 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700858 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbeef48e9712011-09-15 17:54:28 -0700859 ArmLIR* ld = newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
860 //TUNING: loosen barrier
861 ld->defMask = ENCODE_ALL;
862 setMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbee561227c2011-09-02 15:28:19 -0700863 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700864 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700865 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbeef48e9712011-09-15 17:54:28 -0700866 ArmLIR* st = newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
867 setMemRefType(st, false /* isLoad */, kDalvikReg);
868 st->defMask = ENCODE_ALL;
buzbee561227c2011-09-02 15:28:19 -0700869 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700870 }
871
872 // Handle the 1st 3 in r1, r2 & r3
buzbeee9a72f62011-09-04 17:59:07 -0700873 for (unsigned int i = 0; i < 3; i++) {
874 if (i < dInsn->vA)
875 registerArgs[i] = dInsn->vC + i;
876 else
877 registerArgs[i] = INVALID_REG;
buzbee67bf8852011-08-17 17:51:35 -0700878 }
buzbeee9a72f62011-09-04 17:59:07 -0700879 if (skipThis) {
880 registerArgs[0] = INVALID_REG;
881 }
882 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
883 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700884
buzbee561227c2011-09-02 15:28:19 -0700885 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700886 return callState;
887}
888
buzbee2a475e72011-09-07 17:19:17 -0700889#ifdef DISPLAY_MISSING_TARGETS
890// Debugging routine - if null target, branch to DebugMe
891static void genShowTarget(CompilationUnit* cUnit)
892{
893 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
894 loadWordDisp(cUnit, rSELF,
895 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
896 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
897 target->defMask = -1;
898 branchOver->generic.target = (LIR*)target;
899}
900#endif
901
buzbee561227c2011-09-02 15:28:19 -0700902static void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
903 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700904{
905 DecodedInstruction* dInsn = &mir->dalvikInsn;
906 int callState = 0;
907 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700908 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700909 NextCallInsn nextCallInsn = nextSDCallInsn;
910
buzbee109bd6a2011-09-06 13:58:41 -0700911 // Explicit register usage
912 oatLockCallTemps(cUnit);
913
buzbee561227c2011-09-02 15:28:19 -0700914 if (range) {
915 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700916 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700917 } else {
918 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700919 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700920 }
buzbee67bf8852011-08-17 17:51:35 -0700921 // Finish up any of the call sequence not interleaved in arg loading
922 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700923 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700924 }
buzbee2a475e72011-09-07 17:19:17 -0700925#ifdef DISPLAY_MISSING_TARGETS
926 genShowTarget(cUnit);
927#endif
buzbeeec5adf32011-09-11 15:25:43 -0700928 opReg(cUnit, kOpBlx, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700929}
930
buzbee4a3164f2011-09-03 11:25:10 -0700931/*
932 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
933 * which will locate the target and continue on via a tail call.
934 */
buzbee67bf8852011-08-17 17:51:35 -0700935static void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
936{
937 DecodedInstruction* dInsn = &mir->dalvikInsn;
938 int callState = 0;
939 ArmLIR* nullCk;
buzbee109bd6a2011-09-06 13:58:41 -0700940
941 // Explicit register usage
942 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700943 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -0700944 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700945 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
946 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700947 false, nextInterfaceCallInsn, NULL,
buzbee367ce0b2011-09-14 23:19:50 -0700948 false);
buzbee67bf8852011-08-17 17:51:35 -0700949 else
950 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee367ce0b2011-09-14 23:19:50 -0700951 nextInterfaceCallInsn, NULL, false);
buzbee67bf8852011-08-17 17:51:35 -0700952 // Finish up any of the call sequence not interleaved in arg loading
953 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700954 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700955 }
buzbee2a475e72011-09-07 17:19:17 -0700956#ifdef DISPLAY_MISSING_TARGETS
957 genShowTarget(cUnit);
958#endif
buzbeeec5adf32011-09-11 15:25:43 -0700959 opReg(cUnit, kOpBlx, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700960}
961
962static void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
963{
964 DecodedInstruction* dInsn = &mir->dalvikInsn;
965 int callState = 0;
966 ArmLIR* nullCk;
buzbee4a3164f2011-09-03 11:25:10 -0700967 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -0700968 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
969 Method* baseMethod = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee4a3164f2011-09-03 11:25:10 -0700970 NextCallInsn nextCallInsn;
971 bool fastPath = true;
buzbee109bd6a2011-09-06 13:58:41 -0700972
973 // Explicit register usage
974 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -0700975 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
buzbee4a3164f2011-09-03 11:25:10 -0700976 fastPath = false;
977 } else {
978 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
979 if (superClass == NULL) {
980 fastPath = false;
981 } else {
982 int32_t target_idx = baseMethod->GetMethodIndex();
983 if (superClass->GetVTable()->GetLength() <= target_idx) {
984 fastPath = false;
985 } else {
986 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
987 }
988 }
989 }
990 if (fastPath) {
991 nextCallInsn = nextSuperCallInsn;
992 rollback = NULL;
993 } else {
994 nextCallInsn = nextSuperCallInsnSP;
995 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
996 rollback->defMask = -1;
997 }
buzbee67bf8852011-08-17 17:51:35 -0700998 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
999 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001000 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001001 else
1002 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001003 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001004 // Finish up any of the call sequence not interleaved in arg loading
1005 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -07001006 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001007 }
buzbee2a475e72011-09-07 17:19:17 -07001008#ifdef DISPLAY_MISSING_TARGETS
1009 genShowTarget(cUnit);
1010#endif
buzbeeec5adf32011-09-11 15:25:43 -07001011 opReg(cUnit, kOpBlx, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001012}
1013
1014static void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
1015{
1016 DecodedInstruction* dInsn = &mir->dalvikInsn;
1017 int callState = 0;
1018 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -07001019 ArmLIR* rollback;
Brian Carlstrom845490b2011-09-19 15:56:53 -07001020 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
1021 Method* method = class_linker->ResolveMethod(dInsn->vB, cUnit->method, false);
buzbee561227c2011-09-02 15:28:19 -07001022 NextCallInsn nextCallInsn;
buzbee7b1b86d2011-08-26 18:59:10 -07001023
buzbee109bd6a2011-09-06 13:58:41 -07001024 // Explicit register usage
1025 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001026 if (SLOW_INVOKE_PATH || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -07001027 // Slow path
1028 nextCallInsn = nextVCallInsnSP;
1029 // If we need a slow-path callout, we'll restart here
1030 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1031 rollback->defMask = -1;
1032 } else {
1033 // Fast path
1034 nextCallInsn = nextVCallInsn;
1035 rollback = NULL;
1036 }
buzbee67bf8852011-08-17 17:51:35 -07001037 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
1038 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001039 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001040 else
1041 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001042 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001043 // Finish up any of the call sequence not interleaved in arg loading
1044 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001045 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001046 }
buzbee2a475e72011-09-07 17:19:17 -07001047#ifdef DISPLAY_MISSING_TARGETS
1048 genShowTarget(cUnit);
1049#endif
buzbeeec5adf32011-09-11 15:25:43 -07001050 opReg(cUnit, kOpBlx, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001051}
1052
buzbee67bf8852011-08-17 17:51:35 -07001053static bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
1054 BasicBlock* bb, ArmLIR* labelList)
1055{
1056 bool res = false; // Assume success
1057 RegLocation rlSrc[3];
1058 RegLocation rlDest = badLoc;
1059 RegLocation rlResult = badLoc;
1060 Opcode opcode = mir->dalvikInsn.opcode;
1061
1062 /* Prep Src and Dest locations */
1063 int nextSreg = 0;
1064 int nextLoc = 0;
1065 int attrs = oatDataFlowAttributes[opcode];
1066 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1067 if (attrs & DF_UA) {
1068 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1069 nextSreg++;
1070 } else if (attrs & DF_UA_WIDE) {
1071 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1072 nextSreg + 1);
1073 nextSreg+= 2;
1074 }
1075 if (attrs & DF_UB) {
1076 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1077 nextSreg++;
1078 } else if (attrs & DF_UB_WIDE) {
1079 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1080 nextSreg + 1);
1081 nextSreg+= 2;
1082 }
1083 if (attrs & DF_UC) {
1084 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1085 } else if (attrs & DF_UC_WIDE) {
1086 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1087 nextSreg + 1);
1088 }
1089 if (attrs & DF_DA) {
1090 rlDest = oatGetDest(cUnit, mir, 0);
1091 } else if (attrs & DF_DA_WIDE) {
1092 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1093 }
1094
1095 switch(opcode) {
1096 case OP_NOP:
1097 break;
1098
1099 case OP_MOVE_EXCEPTION:
1100 int exOffset;
1101 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001102 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001103 resetReg = oatAllocTemp(cUnit);
1104 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1105 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1106 loadConstant(cUnit, resetReg, 0);
1107 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1108 storeValue(cUnit, rlDest, rlResult);
1109 break;
1110
1111 case OP_RETURN_VOID:
1112 break;
1113
1114 case OP_RETURN:
1115 case OP_RETURN_OBJECT:
1116 storeValue(cUnit, retLoc, rlSrc[0]);
1117 break;
1118
1119 case OP_RETURN_WIDE:
1120 rlDest = retLocWide;
1121 rlDest.fp = rlSrc[0].fp;
1122 storeValueWide(cUnit, rlDest, rlSrc[0]);
1123 break;
1124
1125 case OP_MOVE_RESULT_WIDE:
buzbee43a36422011-09-14 14:00:13 -07001126 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001127 break; // Nop - combined w/ previous invoke
1128 /*
1129 * Somewhat hacky here. Because we're now passing
1130 * return values in registers, we have to let the
1131 * register allocation utilities know that the return
1132 * registers are live and may not be used for address
1133 * formation in storeValueWide.
1134 */
1135 assert(retLocWide.lowReg == r0);
buzbee1da522d2011-09-04 11:22:20 -07001136 assert(retLocWide.highReg == r1);
buzbee67bf8852011-08-17 17:51:35 -07001137 oatLockTemp(cUnit, retLocWide.lowReg);
1138 oatLockTemp(cUnit, retLocWide.highReg);
1139 storeValueWide(cUnit, rlDest, retLocWide);
1140 oatFreeTemp(cUnit, retLocWide.lowReg);
1141 oatFreeTemp(cUnit, retLocWide.highReg);
1142 break;
1143
1144 case OP_MOVE_RESULT:
1145 case OP_MOVE_RESULT_OBJECT:
buzbee43a36422011-09-14 14:00:13 -07001146 if (mir->optimizationFlags & MIR_INLINED)
buzbee67bf8852011-08-17 17:51:35 -07001147 break; // Nop - combined w/ previous invoke
1148 /* See comment for OP_MOVE_RESULT_WIDE */
1149 assert(retLoc.lowReg == r0);
1150 oatLockTemp(cUnit, retLoc.lowReg);
1151 storeValue(cUnit, rlDest, retLoc);
1152 oatFreeTemp(cUnit, retLoc.lowReg);
1153 break;
1154
1155 case OP_MOVE:
1156 case OP_MOVE_OBJECT:
1157 case OP_MOVE_16:
1158 case OP_MOVE_OBJECT_16:
1159 case OP_MOVE_FROM16:
1160 case OP_MOVE_OBJECT_FROM16:
1161 storeValue(cUnit, rlDest, rlSrc[0]);
1162 break;
1163
1164 case OP_MOVE_WIDE:
1165 case OP_MOVE_WIDE_16:
1166 case OP_MOVE_WIDE_FROM16:
1167 storeValueWide(cUnit, rlDest, rlSrc[0]);
1168 break;
1169
1170 case OP_CONST:
1171 case OP_CONST_4:
1172 case OP_CONST_16:
1173 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1174 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1175 storeValue(cUnit, rlDest, rlResult);
1176 break;
1177
1178 case OP_CONST_HIGH16:
1179 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1180 loadConstantNoClobber(cUnit, rlResult.lowReg,
1181 mir->dalvikInsn.vB << 16);
1182 storeValue(cUnit, rlDest, rlResult);
1183 break;
1184
1185 case OP_CONST_WIDE_16:
1186 case OP_CONST_WIDE_32:
buzbee03fa2632011-09-20 17:10:57 -07001187 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1188 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1189 mir->dalvikInsn.vB,
1190 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
buzbee67bf8852011-08-17 17:51:35 -07001191 storeValueWide(cUnit, rlDest, rlResult);
1192 break;
1193
1194 case OP_CONST_WIDE:
1195 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1196 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001197 mir->dalvikInsn.vB_wide & 0xffffffff,
1198 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001199 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001200 break;
1201
1202 case OP_CONST_WIDE_HIGH16:
1203 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1204 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1205 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001206 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001207 break;
1208
1209 case OP_MONITOR_ENTER:
1210 genMonitorEnter(cUnit, mir, rlSrc[0]);
1211 break;
1212
1213 case OP_MONITOR_EXIT:
1214 genMonitorExit(cUnit, mir, rlSrc[0]);
1215 break;
1216
1217 case OP_CHECK_CAST:
1218 genCheckCast(cUnit, mir, rlSrc[0]);
1219 break;
1220
1221 case OP_INSTANCE_OF:
1222 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1223 break;
1224
1225 case OP_NEW_INSTANCE:
1226 genNewInstance(cUnit, mir, rlDest);
1227 break;
1228
1229 case OP_THROW:
1230 genThrow(cUnit, mir, rlSrc[0]);
1231 break;
1232
buzbee5ade1d22011-09-09 14:44:52 -07001233 case OP_THROW_VERIFICATION_ERROR:
1234 loadWordDisp(cUnit, rSELF,
1235 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1236 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1237 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
buzbeeec5adf32011-09-11 15:25:43 -07001238 callUnwindableHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07001239 break;
1240
buzbee67bf8852011-08-17 17:51:35 -07001241 case OP_ARRAY_LENGTH:
1242 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001243 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001244 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001245 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001246 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1247 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1248 rlResult.lowReg);
1249 storeValue(cUnit, rlDest, rlResult);
1250 break;
1251
1252 case OP_CONST_STRING:
1253 case OP_CONST_STRING_JUMBO:
1254 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1255 break;
1256
1257 case OP_CONST_CLASS:
1258 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1259 break;
1260
1261 case OP_FILL_ARRAY_DATA:
1262 genFillArrayData(cUnit, mir, rlSrc[0]);
1263 break;
1264
1265 case OP_FILLED_NEW_ARRAY:
1266 genFilledNewArray(cUnit, mir, false /* not range */);
1267 break;
1268
1269 case OP_FILLED_NEW_ARRAY_RANGE:
1270 genFilledNewArray(cUnit, mir, true /* range */);
1271 break;
1272
1273 case OP_NEW_ARRAY:
1274 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1275 break;
1276
1277 case OP_GOTO:
1278 case OP_GOTO_16:
1279 case OP_GOTO_32:
1280 // TUNING: add MIR flag to disable when unnecessary
1281 bool backwardBranch;
1282 backwardBranch = (bb->taken->startOffset <= mir->offset);
1283 if (backwardBranch) {
1284 genSuspendPoll(cUnit, mir);
1285 }
1286 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1287 break;
1288
1289 case OP_PACKED_SWITCH:
1290 genPackedSwitch(cUnit, mir, rlSrc[0]);
1291 break;
1292
1293 case OP_SPARSE_SWITCH:
1294 genSparseSwitch(cUnit, mir, rlSrc[0]);
1295 break;
1296
1297 case OP_CMPL_FLOAT:
1298 case OP_CMPG_FLOAT:
1299 case OP_CMPL_DOUBLE:
1300 case OP_CMPG_DOUBLE:
1301 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1302 break;
1303
1304 case OP_CMP_LONG:
1305 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1306 break;
1307
1308 case OP_IF_EQ:
1309 case OP_IF_NE:
1310 case OP_IF_LT:
1311 case OP_IF_GE:
1312 case OP_IF_GT:
1313 case OP_IF_LE: {
1314 bool backwardBranch;
1315 ArmConditionCode cond;
1316 backwardBranch = (bb->taken->startOffset <= mir->offset);
1317 if (backwardBranch) {
1318 genSuspendPoll(cUnit, mir);
1319 }
1320 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1321 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1322 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1323 switch(opcode) {
1324 case OP_IF_EQ:
1325 cond = kArmCondEq;
1326 break;
1327 case OP_IF_NE:
1328 cond = kArmCondNe;
1329 break;
1330 case OP_IF_LT:
1331 cond = kArmCondLt;
1332 break;
1333 case OP_IF_GE:
1334 cond = kArmCondGe;
1335 break;
1336 case OP_IF_GT:
1337 cond = kArmCondGt;
1338 break;
1339 case OP_IF_LE:
1340 cond = kArmCondLe;
1341 break;
1342 default:
1343 cond = (ArmConditionCode)0;
1344 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1345 }
1346 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1347 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1348 break;
1349 }
1350
1351 case OP_IF_EQZ:
1352 case OP_IF_NEZ:
1353 case OP_IF_LTZ:
1354 case OP_IF_GEZ:
1355 case OP_IF_GTZ:
1356 case OP_IF_LEZ: {
1357 bool backwardBranch;
1358 ArmConditionCode cond;
1359 backwardBranch = (bb->taken->startOffset <= mir->offset);
1360 if (backwardBranch) {
1361 genSuspendPoll(cUnit, mir);
1362 }
1363 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1364 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1365 switch(opcode) {
1366 case OP_IF_EQZ:
1367 cond = kArmCondEq;
1368 break;
1369 case OP_IF_NEZ:
1370 cond = kArmCondNe;
1371 break;
1372 case OP_IF_LTZ:
1373 cond = kArmCondLt;
1374 break;
1375 case OP_IF_GEZ:
1376 cond = kArmCondGe;
1377 break;
1378 case OP_IF_GTZ:
1379 cond = kArmCondGt;
1380 break;
1381 case OP_IF_LEZ:
1382 cond = kArmCondLe;
1383 break;
1384 default:
1385 cond = (ArmConditionCode)0;
1386 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1387 }
1388 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1389 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1390 break;
1391 }
1392
1393 case OP_AGET_WIDE:
1394 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1395 break;
1396 case OP_AGET:
1397 case OP_AGET_OBJECT:
1398 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1399 break;
1400 case OP_AGET_BOOLEAN:
1401 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1402 rlDest, 0);
1403 break;
1404 case OP_AGET_BYTE:
1405 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1406 break;
1407 case OP_AGET_CHAR:
1408 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1409 rlDest, 1);
1410 break;
1411 case OP_AGET_SHORT:
1412 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1413 break;
1414 case OP_APUT_WIDE:
1415 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1416 break;
1417 case OP_APUT:
1418 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1419 break;
1420 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001421 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001422 break;
1423 case OP_APUT_SHORT:
1424 case OP_APUT_CHAR:
1425 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1426 rlSrc[0], 1);
1427 break;
1428 case OP_APUT_BYTE:
1429 case OP_APUT_BOOLEAN:
1430 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1431 rlSrc[0], 0);
1432 break;
1433
1434 case OP_IGET_WIDE:
1435 case OP_IGET_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001436 genIGetWide(cUnit, mir, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001437 break;
1438
1439 case OP_IGET:
1440 case OP_IGET_VOLATILE:
1441 case OP_IGET_OBJECT:
1442 case OP_IGET_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001443 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001444 break;
1445
1446 case OP_IGET_BOOLEAN:
1447 case OP_IGET_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001448 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001449 break;
1450
1451 case OP_IGET_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001452 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001453 break;
1454
1455 case OP_IGET_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001456 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
buzbee67bf8852011-08-17 17:51:35 -07001457 break;
1458
1459 case OP_IPUT_WIDE:
1460 case OP_IPUT_WIDE_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001461 genIPutWide(cUnit, mir, rlSrc[0], rlSrc[1]);
buzbee67bf8852011-08-17 17:51:35 -07001462 break;
1463
1464 case OP_IPUT_OBJECT:
1465 case OP_IPUT_OBJECT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001466 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
buzbee67bf8852011-08-17 17:51:35 -07001467 break;
1468
1469 case OP_IPUT:
1470 case OP_IPUT_VOLATILE:
buzbee43a36422011-09-14 14:00:13 -07001471 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001472 break;
1473
1474 case OP_IPUT_BOOLEAN:
1475 case OP_IPUT_BYTE:
buzbee43a36422011-09-14 14:00:13 -07001476 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001477 break;
1478
1479 case OP_IPUT_CHAR:
buzbee43a36422011-09-14 14:00:13 -07001480 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001481 break;
1482
1483 case OP_IPUT_SHORT:
buzbee43a36422011-09-14 14:00:13 -07001484 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
buzbee67bf8852011-08-17 17:51:35 -07001485 break;
1486
1487 case OP_SGET:
1488 case OP_SGET_OBJECT:
1489 case OP_SGET_BOOLEAN:
1490 case OP_SGET_BYTE:
1491 case OP_SGET_CHAR:
1492 case OP_SGET_SHORT:
1493 genSget(cUnit, mir, rlResult, rlDest);
1494 break;
1495
1496 case OP_SGET_WIDE:
1497 genSgetWide(cUnit, mir, rlResult, rlDest);
1498 break;
1499
1500 case OP_SPUT:
1501 case OP_SPUT_OBJECT:
1502 case OP_SPUT_BOOLEAN:
1503 case OP_SPUT_BYTE:
1504 case OP_SPUT_CHAR:
1505 case OP_SPUT_SHORT:
1506 genSput(cUnit, mir, rlSrc[0]);
1507 break;
1508
1509 case OP_SPUT_WIDE:
1510 genSputWide(cUnit, mir, rlSrc[0]);
1511 break;
1512
1513 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001514 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1515 true /*range*/);
1516 break;
buzbee67bf8852011-08-17 17:51:35 -07001517 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001518 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1519 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001520 break;
1521
1522 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001523 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1524 false /*range*/);
1525 break;
buzbee67bf8852011-08-17 17:51:35 -07001526 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001527 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1528 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001529 break;
1530
1531 case OP_INVOKE_VIRTUAL:
1532 case OP_INVOKE_VIRTUAL_RANGE:
1533 genInvokeVirtual(cUnit, mir);
1534 break;
1535
1536 case OP_INVOKE_SUPER:
1537 case OP_INVOKE_SUPER_RANGE:
1538 genInvokeSuper(cUnit, mir);
1539 break;
1540
1541 case OP_INVOKE_INTERFACE:
1542 case OP_INVOKE_INTERFACE_RANGE:
1543 genInvokeInterface(cUnit, mir);
1544 break;
1545
1546 case OP_NEG_INT:
1547 case OP_NOT_INT:
1548 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1549 break;
1550
1551 case OP_NEG_LONG:
1552 case OP_NOT_LONG:
1553 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1554 break;
1555
1556 case OP_NEG_FLOAT:
1557 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1558 break;
1559
1560 case OP_NEG_DOUBLE:
1561 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1562 break;
1563
1564 case OP_INT_TO_LONG:
1565 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1566 if (rlSrc[0].location == kLocPhysReg) {
1567 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1568 } else {
1569 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1570 }
1571 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1572 rlResult.lowReg, 31);
1573 storeValueWide(cUnit, rlDest, rlResult);
1574 break;
1575
1576 case OP_LONG_TO_INT:
1577 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1578 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1579 storeValue(cUnit, rlDest, rlSrc[0]);
1580 break;
1581
1582 case OP_INT_TO_BYTE:
1583 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1584 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1585 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1586 storeValue(cUnit, rlDest, rlResult);
1587 break;
1588
1589 case OP_INT_TO_SHORT:
1590 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1591 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1592 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1593 storeValue(cUnit, rlDest, rlResult);
1594 break;
1595
1596 case OP_INT_TO_CHAR:
1597 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1598 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1599 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1600 storeValue(cUnit, rlDest, rlResult);
1601 break;
1602
1603 case OP_INT_TO_FLOAT:
1604 case OP_INT_TO_DOUBLE:
1605 case OP_LONG_TO_FLOAT:
1606 case OP_LONG_TO_DOUBLE:
1607 case OP_FLOAT_TO_INT:
1608 case OP_FLOAT_TO_LONG:
1609 case OP_FLOAT_TO_DOUBLE:
1610 case OP_DOUBLE_TO_INT:
1611 case OP_DOUBLE_TO_LONG:
1612 case OP_DOUBLE_TO_FLOAT:
1613 genConversion(cUnit, mir);
1614 break;
1615
1616 case OP_ADD_INT:
1617 case OP_SUB_INT:
1618 case OP_MUL_INT:
1619 case OP_DIV_INT:
1620 case OP_REM_INT:
1621 case OP_AND_INT:
1622 case OP_OR_INT:
1623 case OP_XOR_INT:
1624 case OP_SHL_INT:
1625 case OP_SHR_INT:
1626 case OP_USHR_INT:
1627 case OP_ADD_INT_2ADDR:
1628 case OP_SUB_INT_2ADDR:
1629 case OP_MUL_INT_2ADDR:
1630 case OP_DIV_INT_2ADDR:
1631 case OP_REM_INT_2ADDR:
1632 case OP_AND_INT_2ADDR:
1633 case OP_OR_INT_2ADDR:
1634 case OP_XOR_INT_2ADDR:
1635 case OP_SHL_INT_2ADDR:
1636 case OP_SHR_INT_2ADDR:
1637 case OP_USHR_INT_2ADDR:
1638 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1639 break;
1640
1641 case OP_ADD_LONG:
1642 case OP_SUB_LONG:
1643 case OP_MUL_LONG:
1644 case OP_DIV_LONG:
1645 case OP_REM_LONG:
1646 case OP_AND_LONG:
1647 case OP_OR_LONG:
1648 case OP_XOR_LONG:
1649 case OP_ADD_LONG_2ADDR:
1650 case OP_SUB_LONG_2ADDR:
1651 case OP_MUL_LONG_2ADDR:
1652 case OP_DIV_LONG_2ADDR:
1653 case OP_REM_LONG_2ADDR:
1654 case OP_AND_LONG_2ADDR:
1655 case OP_OR_LONG_2ADDR:
1656 case OP_XOR_LONG_2ADDR:
1657 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1658 break;
1659
buzbee67bf8852011-08-17 17:51:35 -07001660 case OP_SHL_LONG:
1661 case OP_SHR_LONG:
1662 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001663 case OP_SHL_LONG_2ADDR:
1664 case OP_SHR_LONG_2ADDR:
1665 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001666 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1667 break;
1668
1669 case OP_ADD_FLOAT:
1670 case OP_SUB_FLOAT:
1671 case OP_MUL_FLOAT:
1672 case OP_DIV_FLOAT:
1673 case OP_REM_FLOAT:
1674 case OP_ADD_FLOAT_2ADDR:
1675 case OP_SUB_FLOAT_2ADDR:
1676 case OP_MUL_FLOAT_2ADDR:
1677 case OP_DIV_FLOAT_2ADDR:
1678 case OP_REM_FLOAT_2ADDR:
1679 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1680 break;
1681
1682 case OP_ADD_DOUBLE:
1683 case OP_SUB_DOUBLE:
1684 case OP_MUL_DOUBLE:
1685 case OP_DIV_DOUBLE:
1686 case OP_REM_DOUBLE:
1687 case OP_ADD_DOUBLE_2ADDR:
1688 case OP_SUB_DOUBLE_2ADDR:
1689 case OP_MUL_DOUBLE_2ADDR:
1690 case OP_DIV_DOUBLE_2ADDR:
1691 case OP_REM_DOUBLE_2ADDR:
1692 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1693 break;
1694
1695 case OP_RSUB_INT:
1696 case OP_ADD_INT_LIT16:
1697 case OP_MUL_INT_LIT16:
1698 case OP_DIV_INT_LIT16:
1699 case OP_REM_INT_LIT16:
1700 case OP_AND_INT_LIT16:
1701 case OP_OR_INT_LIT16:
1702 case OP_XOR_INT_LIT16:
1703 case OP_ADD_INT_LIT8:
1704 case OP_RSUB_INT_LIT8:
1705 case OP_MUL_INT_LIT8:
1706 case OP_DIV_INT_LIT8:
1707 case OP_REM_INT_LIT8:
1708 case OP_AND_INT_LIT8:
1709 case OP_OR_INT_LIT8:
1710 case OP_XOR_INT_LIT8:
1711 case OP_SHL_INT_LIT8:
1712 case OP_SHR_INT_LIT8:
1713 case OP_USHR_INT_LIT8:
1714 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1715 break;
1716
1717 default:
1718 res = true;
1719 }
1720 return res;
1721}
1722
1723static const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
1724 "kMirOpPhi",
1725 "kMirOpNullNRangeUpCheck",
1726 "kMirOpNullNRangeDownCheck",
1727 "kMirOpLowerBound",
1728 "kMirOpPunt",
1729 "kMirOpCheckInlinePrediction",
1730};
1731
1732/* Extended MIR instructions like PHI */
1733static void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
1734{
1735 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1736 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1737 strcpy(msg, extendedMIROpNames[opOffset]);
1738 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1739
1740 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1741 case kMirOpPhi: {
1742 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1743 op->flags.isNop = true;
1744 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1745 break;
1746 }
1747 default:
1748 break;
1749 }
1750}
1751
1752/* If there are any ins passed in registers that have not been promoted
1753 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001754 * Note: at this pointCopy any ins that are passed in register to their
1755 * home location */
buzbee67bf8852011-08-17 17:51:35 -07001756static void flushIns(CompilationUnit* cUnit)
1757{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001759 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001760 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1761 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001762 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001763 int startLoc = cUnit->method->NumRegisters() -
1764 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001765 for (int i = 0; i < inRegs; i++) {
1766 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001767 //TUNING: be smarter about flushing ins to frame
1768 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001769 if (loc.location == kLocPhysReg) {
1770 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001771 }
1772 }
1773
1774 // Handle special case of wide argument half in regs, half in frame
1775 if (inRegs == 3) {
1776 RegLocation loc = cUnit->regLocation[startLoc + 2];
1777 if (loc.wide && loc.location == kLocPhysReg) {
1778 // Load the other half of the arg into the promoted pair
buzbee561227c2011-09-02 15:28:19 -07001779 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -07001780 inRegs++;
1781 }
1782 }
1783
1784 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001785 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001786 RegLocation loc = cUnit->regLocation[startLoc + i];
1787 if (loc.fpLocation == kLocPhysReg) {
1788 loc.location = kLocPhysReg;
1789 loc.fp = true;
1790 loc.lowReg = loc.fpLowReg;
1791 loc.highReg = loc.fpHighReg;
1792 }
1793 if (loc.location == kLocPhysReg) {
1794 if (loc.wide) {
1795 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1796 loc.lowReg, loc.highReg, INVALID_SREG);
1797 i++;
1798 } else {
buzbee561227c2011-09-02 15:28:19 -07001799 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001800 }
1801 }
1802 i++;
1803 }
1804}
1805
1806/* Handle the content in each basic block */
1807static bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
1808{
1809 MIR* mir;
1810 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1811 int blockId = bb->id;
1812
1813 cUnit->curBlock = bb;
1814 labelList[blockId].operands[0] = bb->startOffset;
1815
1816 /* Insert the block label */
1817 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1818 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1819
1820 oatClobberAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001821
1822 ArmLIR* headLIR = NULL;
1823
1824 if (bb->blockType == kEntryBlock) {
1825 /*
1826 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1827 * mechanism know so it doesn't try to use any of them when
1828 * expanding the frame or flushing. This leaves the utility
1829 * code with a single temp: r12. This should be enough.
1830 */
1831 oatLockTemp(cUnit, r0);
1832 oatLockTemp(cUnit, r1);
1833 oatLockTemp(cUnit, r2);
1834 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001835
1836 /*
1837 * We can safely skip the stack overflow check if we're
1838 * a leaf *and* our frame size < fudge factor.
1839 */
1840 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1841 ((size_t)cUnit->frameSize <
1842 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001843 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001844 if (!skipOverflowCheck) {
1845 /* Load stack limit */
1846 loadWordDisp(cUnit, rSELF,
1847 art::Thread::StackEndOffset().Int32Value(), r12);
1848 }
buzbee67bf8852011-08-17 17:51:35 -07001849 /* Spill core callee saves */
1850 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1851 /* Need to spill any FP regs? */
1852 if (cUnit->numFPSpills) {
1853 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1854 }
buzbeecefd1872011-09-09 09:59:52 -07001855 if (!skipOverflowCheck) {
1856 opRegRegImm(cUnit, kOpSub, rLR, rSP,
1857 cUnit->frameSize - (cUnit->numSpills * 4));
buzbeeec5adf32011-09-11 15:25:43 -07001858 genRegRegCheck(cUnit, kArmCondCc, rLR, r12, NULL,
1859 kArmThrowStackOverflow);
buzbeecefd1872011-09-09 09:59:52 -07001860 genRegCopy(cUnit, rSP, rLR); // Establish stack
1861 } else {
1862 opRegImm(cUnit, kOpSub, rSP,
1863 cUnit->frameSize - (cUnit->numSpills * 4));
1864 }
buzbee67bf8852011-08-17 17:51:35 -07001865 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1866 flushIns(cUnit);
1867 oatFreeTemp(cUnit, r0);
1868 oatFreeTemp(cUnit, r1);
1869 oatFreeTemp(cUnit, r2);
1870 oatFreeTemp(cUnit, r3);
1871 } else if (bb->blockType == kExitBlock) {
1872 newLIR0(cUnit, kArmPseudoMethodExit);
1873 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1874 /* Need to restore any FP callee saves? */
1875 if (cUnit->numFPSpills) {
1876 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1877 }
1878 if (cUnit->coreSpillMask & (1 << rLR)) {
1879 /* Unspill rLR to rPC */
1880 cUnit->coreSpillMask &= ~(1 << rLR);
1881 cUnit->coreSpillMask |= (1 << rPC);
1882 }
1883 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1884 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1885 /* We didn't pop to rPC, so must do a bv rLR */
1886 newLIR1(cUnit, kThumbBx, rLR);
1887 }
1888 }
1889
1890 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1891
1892 oatResetRegPool(cUnit);
buzbeef0cde542011-09-13 14:55:02 -07001893 oatClobberAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001894
1895 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1896 oatResetDefTracking(cUnit);
1897 }
1898
1899 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1900 handleExtendedMethodMIR(cUnit, mir);
1901 continue;
1902 }
1903
1904 cUnit->currentDalvikOffset = mir->offset;
1905
1906 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1907 InstructionFormat dalvikFormat =
1908 dexGetFormatFromOpcode(dalvikOpcode);
1909
1910 ArmLIR* boundaryLIR;
1911
1912 /* Mark the beginning of a Dalvik instruction for line tracking */
1913 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1914 (int) oatGetDalvikDisassembly(
1915 &mir->dalvikInsn, ""));
1916 /* Remember the first LIR for this block */
1917 if (headLIR == NULL) {
1918 headLIR = boundaryLIR;
1919 /* Set the first boundaryLIR as a scheduling barrier */
1920 headLIR->defMask = ENCODE_ALL;
1921 }
1922
1923 /* Don't generate the SSA annotation unless verbose mode is on */
1924 if (cUnit->printMe && mir->ssaRep) {
1925 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1926 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1927 }
1928
1929 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1930
1931 if (notHandled) {
1932 char buf[100];
1933 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1934 mir->offset,
1935 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1936 dalvikFormat);
1937 LOG(FATAL) << buf;
1938 }
1939 }
1940
1941 if (headLIR) {
1942 /*
1943 * Eliminate redundant loads/stores and delay stores into later
1944 * slots
1945 */
1946 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1947 cUnit->lastLIRInsn);
1948
1949 /*
1950 * Generate an unconditional branch to the fallthrough block.
1951 */
1952 if (bb->fallThrough) {
1953 genUnconditionalBranch(cUnit,
1954 &labelList[bb->fallThrough->id]);
1955 }
1956 }
1957 return false;
1958}
1959
1960/*
1961 * Nop any unconditional branches that go to the next instruction.
1962 * Note: new redundant branches may be inserted later, and we'll
1963 * use a check in final instruction assembly to nop those out.
1964 */
1965void removeRedundantBranches(CompilationUnit* cUnit)
1966{
1967 ArmLIR* thisLIR;
1968
1969 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1970 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1971 thisLIR = NEXT_LIR(thisLIR)) {
1972
1973 /* Branch to the next instruction */
1974 if ((thisLIR->opcode == kThumbBUncond) ||
1975 (thisLIR->opcode == kThumb2BUncond)) {
1976 ArmLIR* nextLIR = thisLIR;
1977
1978 while (true) {
1979 nextLIR = NEXT_LIR(nextLIR);
1980
1981 /*
1982 * Is the branch target the next instruction?
1983 */
1984 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1985 thisLIR->flags.isNop = true;
1986 break;
1987 }
1988
1989 /*
1990 * Found real useful stuff between the branch and the target.
1991 * Need to explicitly check the lastLIRInsn here because it
1992 * might be the last real instruction.
1993 */
1994 if (!isPseudoOpcode(nextLIR->opcode) ||
1995 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1996 break;
1997 }
1998 }
1999 }
2000}
2001
buzbee5ade1d22011-09-09 14:44:52 -07002002static void handleThrowLaunchpads(CompilationUnit *cUnit)
2003{
2004 ArmLIR** throwLabel =
2005 (ArmLIR **) cUnit->throwLaunchpads.elemList;
2006 int numElems = cUnit->throwLaunchpads.numUsed;
2007 int i;
2008
2009 for (i = 0; i < numElems; i++) {
2010 ArmLIR* lab = throwLabel[i];
2011 cUnit->currentDalvikOffset = lab->operands[1];
2012 oatAppendLIR(cUnit, (LIR *)lab);
2013 int funcOffset = 0;
2014 int v1 = lab->operands[2];
2015 int v2 = lab->operands[3];
2016 switch(lab->operands[0]) {
2017 case kArmThrowNullPointer:
2018 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2019 break;
2020 case kArmThrowArrayBounds:
2021 if (v2 != r0) {
2022 genRegCopy(cUnit, r0, v1);
2023 genRegCopy(cUnit, r1, v2);
2024 } else {
2025 if (v1 == r1) {
2026 genRegCopy(cUnit, r12, v1);
2027 genRegCopy(cUnit, r1, v2);
2028 genRegCopy(cUnit, r0, r12);
2029 } else {
2030 genRegCopy(cUnit, r1, v2);
2031 genRegCopy(cUnit, r0, v1);
2032 }
2033 }
2034 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2035 break;
2036 case kArmThrowDivZero:
2037 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2038 break;
2039 case kArmThrowVerificationError:
2040 loadConstant(cUnit, r0, v1);
2041 loadConstant(cUnit, r1, v2);
2042 funcOffset =
2043 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2044 break;
2045 case kArmThrowNegArraySize:
2046 genRegCopy(cUnit, r0, v1);
2047 funcOffset =
2048 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2049 break;
2050 case kArmThrowInternalError:
2051 genRegCopy(cUnit, r0, v1);
2052 funcOffset =
2053 OFFSETOF_MEMBER(Thread, pThrowInternalErrorFromCode);
2054 break;
2055 case kArmThrowRuntimeException:
2056 genRegCopy(cUnit, r0, v1);
2057 funcOffset =
2058 OFFSETOF_MEMBER(Thread, pThrowRuntimeExceptionFromCode);
2059 break;
2060 case kArmThrowNoSuchMethod:
2061 genRegCopy(cUnit, r0, v1);
2062 funcOffset =
2063 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2064 break;
buzbeeec5adf32011-09-11 15:25:43 -07002065 case kArmThrowStackOverflow:
2066 funcOffset =
2067 OFFSETOF_MEMBER(Thread, pStackOverflowFromCode);
2068 // Restore stack alignment
2069 opRegImm(cUnit, kOpAdd, rSP, cUnit->numSpills * 4);
2070 break;
buzbee5ade1d22011-09-09 14:44:52 -07002071 default:
2072 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2073 }
2074 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
buzbeeec5adf32011-09-11 15:25:43 -07002075 callUnwindableHelper(cUnit, rLR);
buzbee5ade1d22011-09-09 14:44:52 -07002076 }
2077}
2078
buzbee67bf8852011-08-17 17:51:35 -07002079void oatMethodMIR2LIR(CompilationUnit* cUnit)
2080{
2081 /* Used to hold the labels of each block */
2082 cUnit->blockLabelList =
2083 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2084
2085 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2086 kPreOrderDFSTraversal, false /* Iterative */);
2087 removeRedundantBranches(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002088
2089 handleThrowLaunchpads(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002090}
2091
2092/* Common initialization routine for an architecture family */
2093bool oatArchInit()
2094{
2095 int i;
2096
2097 for (i = 0; i < kArmLast; i++) {
2098 if (EncodingMap[i].opcode != i) {
2099 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2100 " is wrong: expecting " << i << ", seeing " <<
2101 (int)EncodingMap[i].opcode;
2102 }
2103 }
2104
2105 return oatArchVariantInit();
2106}
2107
2108/* Needed by the Assembler */
2109void oatSetupResourceMasks(ArmLIR* lir)
2110{
2111 setupResourceMasks(lir);
2112}
2113
2114/* Needed by the ld/st optmizatons */
2115ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2116{
2117 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2118}
2119
2120/* Needed by the register allocator */
2121ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2122{
2123 return genRegCopy(cUnit, rDest, rSrc);
2124}
2125
2126/* Needed by the register allocator */
2127void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2128 int srcLo, int srcHi)
2129{
2130 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2131}
2132
2133void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2134 int displacement, int rSrc, OpSize size)
2135{
2136 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2137}
2138
2139void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2140 int displacement, int rSrcLo, int rSrcHi)
2141{
2142 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2143}