blob: a9d77ec4e13192f81be6755fdad636c102877182 [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
40 opReg(cUnit, kOpBlx, rLR);
41 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;
55 int elems;
buzbeedfd3d702011-08-28 12:56:51 -070056 int typeId;
buzbee67bf8852011-08-17 17:51:35 -070057 if (isRange) {
58 elems = dInsn->vA;
buzbeedfd3d702011-08-28 12:56:51 -070059 typeId = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -070060 } else {
61 elems = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070062 typeId = dInsn->vC;
buzbee67bf8852011-08-17 17:51:35 -070063 }
buzbeedfd3d702011-08-28 12:56:51 -070064 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbeedfd3d702011-08-28 12:56:51 -070065 loadWordDisp(cUnit, rSELF,
buzbee1da522d2011-09-04 11:22:20 -070066 OFFSETOF_MEMBER(Thread, pCheckAndAllocFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -070067 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
68 loadConstant(cUnit, r0, typeId); // arg0 <- type_id
69 loadConstant(cUnit, r2, elems); // arg2 <- count
70 opReg(cUnit, kOpBlx, rLR);
buzbee67bf8852011-08-17 17:51:35 -070071 /*
buzbeedfd3d702011-08-28 12:56:51 -070072 * NOTE: the implicit target for OP_FILLED_NEW_ARRAY is the
73 * return region. Because AllocFromCode placed the new array
74 * in r0, we'll just lock it into place. When debugger support is
75 * added, it may be necessary to additionally copy all return
76 * values to a home location in thread-local storage
buzbee67bf8852011-08-17 17:51:35 -070077 */
buzbee67bf8852011-08-17 17:51:35 -070078 oatLockTemp(cUnit, r0);
buzbeedfd3d702011-08-28 12:56:51 -070079
buzbee67bf8852011-08-17 17:51:35 -070080 // Having a range of 0 is legal
81 if (isRange && (dInsn->vA > 0)) {
82 /*
83 * Bit of ugliness here. We're going generate a mem copy loop
84 * on the register range, but it is possible that some regs
85 * in the range have been promoted. This is unlikely, but
86 * before generating the copy, we'll just force a flush
87 * of any regs in the source range that have been promoted to
88 * home location.
89 */
90 for (unsigned int i = 0; i < dInsn->vA; i++) {
91 RegLocation loc = oatUpdateLoc(cUnit,
92 oatGetSrc(cUnit, mir, i));
93 if (loc.location == kLocPhysReg) {
94 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
95 }
96 }
97 /*
98 * TUNING note: generated code here could be much improved, but
99 * this is an uncommon operation and isn't especially performance
100 * critical.
101 */
102 int rSrc = oatAllocTemp(cUnit);
103 int rDst = oatAllocTemp(cUnit);
104 int rIdx = oatAllocTemp(cUnit);
105 int rVal = rLR; // Using a lot of temps, rLR is known free here
106 // Set up source pointer
107 RegLocation rlFirst = oatGetSrc(cUnit, mir, 0);
108 opRegRegImm(cUnit, kOpAdd, rSrc, rSP, rlFirst.spOffset);
109 // Set up the target pointer
110 opRegRegImm(cUnit, kOpAdd, rDst, r0,
buzbeec143c552011-08-20 17:38:58 -0700111 Array::DataOffset().Int32Value());
buzbee67bf8852011-08-17 17:51:35 -0700112 // Set up the loop counter (known to be > 0)
113 loadConstant(cUnit, rIdx, dInsn->vA);
114 // Generate the copy loop. Going backwards for convenience
115 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
116 target->defMask = ENCODE_ALL;
117 // Copy next element
118 loadBaseIndexed(cUnit, rSrc, rIdx, rVal, 2, kWord);
119 storeBaseIndexed(cUnit, rDst, rIdx, rVal, 2, kWord);
120 // Use setflags encoding here
121 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
122 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
123 branch->generic.target = (LIR*)target;
124 } else if (!isRange) {
125 // TUNING: interleave
126 for (unsigned int i = 0; i < dInsn->vA; i++) {
127 RegLocation rlArg = loadValue(cUnit,
128 oatGetSrc(cUnit, mir, i), kCoreReg);
buzbeec143c552011-08-20 17:38:58 -0700129 storeBaseDisp(cUnit, r0,
130 Array::DataOffset().Int32Value() +
buzbee67bf8852011-08-17 17:51:35 -0700131 i * 4, rlArg.lowReg, kWord);
132 // If the loadValue caused a temp to be allocated, free it
133 if (oatIsTemp(cUnit, rlArg.lowReg)) {
134 oatFreeTemp(cUnit, rlArg.lowReg);
135 }
136 }
137 }
138}
139
140static void genSput(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
141{
buzbeee1931742011-08-28 21:15:53 -0700142 bool isObject = ((mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
143 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE));
buzbee1da522d2011-09-04 11:22:20 -0700144 int fieldIdx = mir->dalvikInsn.vB;
145 Field* field = cUnit->method->GetDexCacheResolvedFields()->Get(fieldIdx);
146 if (field == NULL) {
147 // Slow path
buzbee34cd9e52011-09-08 14:31:52 -0700148 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
149 << " unresolved at compile time";
buzbee1da522d2011-09-04 11:22:20 -0700150 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
151 : OFFSETOF_MEMBER(Thread, pSet32Static);
buzbeee1931742011-08-28 21:15:53 -0700152 oatFlushAllRegs(cUnit);
153 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
154 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
155 loadCurrMethodDirect(cUnit, r1);
156 loadValueDirect(cUnit, rlSrc, r2);
157 opReg(cUnit, kOpBlx, rLR);
158 oatClobberCallRegs(cUnit);
159 } else {
buzbee1da522d2011-09-04 11:22:20 -0700160 // fast path
161 int fieldOffset = field->GetOffset().Int32Value();
162 art::ClassLinker* class_linker = art::Runtime::Current()->
163 GetClassLinker();
164 const art::DexFile& dex_file = class_linker->
165 FindDexFile(field->GetDeclaringClass()->GetDexCache());
166 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
167 int typeIdx = field_id.class_idx_;
168 // Using fixed register to sync with slow path
169 int rMethod = r1;
170 oatLockTemp(cUnit, rMethod);
171 loadCurrMethodDirect(cUnit, rMethod);
172 int rBase = r0;
173 oatLockTemp(cUnit, rBase);
174 loadWordDisp(cUnit, rMethod,
175 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
176 rBase);
177 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
178 sizeof(int32_t*)* typeIdx, rBase);
179 // TUNING: fast path should fall through
180 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
181 loadWordDisp(cUnit, rSELF,
182 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
183 loadConstant(cUnit, r0, typeIdx);
184 opReg(cUnit, kOpBlx, rLR);
185 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
186 skipTarget->defMask = ENCODE_ALL;
187 branchOver->generic.target = (LIR*)skipTarget;
188 rlSrc = oatGetSrc(cUnit, mir, 0);
189 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
190 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700191#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700192 if (field->IsVolatile()) {
193 oatGenMemBarrier(cUnit, kSY);
194 }
buzbee67bf8852011-08-17 17:51:35 -0700195#endif
buzbee1da522d2011-09-04 11:22:20 -0700196 if (isObject) {
197 markGCCard(cUnit, rlSrc.lowReg, rBase);
198 }
199 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700200 }
buzbee67bf8852011-08-17 17:51:35 -0700201}
202
203static void genSputWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
204{
buzbee1da522d2011-09-04 11:22:20 -0700205 int fieldIdx = mir->dalvikInsn.vB;
206 Field* field = cUnit->method->GetDexCacheResolvedFields()->Get(fieldIdx);
buzbee34cd9e52011-09-08 14:31:52 -0700207 if (SLOW_FIELD_PATH || field == NULL) {
208 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
209 << " unresolved at compile time";
buzbeee1931742011-08-28 21:15:53 -0700210 oatFlushAllRegs(cUnit);
buzbee1da522d2011-09-04 11:22:20 -0700211 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pSet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700212 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
213 loadCurrMethodDirect(cUnit, r1);
214 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
215 opReg(cUnit, kOpBlx, rLR);
216 oatClobberCallRegs(cUnit);
217 } else {
buzbee1da522d2011-09-04 11:22:20 -0700218 // fast path
219 int fieldOffset = field->GetOffset().Int32Value();
220 art::ClassLinker* class_linker = art::Runtime::Current()->
221 GetClassLinker();
222 const art::DexFile& dex_file = class_linker->
223 FindDexFile(field->GetDeclaringClass()->GetDexCache());
224 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
225 int typeIdx = field_id.class_idx_;
226 // Using fixed register to sync with slow path
227 int rMethod = r1;
228 oatLockTemp(cUnit, rMethod);
229 loadCurrMethodDirect(cUnit, r1);
230 int rBase = r0;
231 oatLockTemp(cUnit, rBase);
232 loadWordDisp(cUnit, rMethod,
233 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
234 rBase);
235 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
236 sizeof(int32_t*)* typeIdx, rBase);
237 // TUNING: fast path should fall through
238 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
239 loadWordDisp(cUnit, rSELF,
240 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
241 loadConstant(cUnit, r0, typeIdx);
242 opReg(cUnit, kOpBlx, rLR);
243 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
244 skipTarget->defMask = ENCODE_ALL;
245 branchOver->generic.target = (LIR*)skipTarget;
246 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
247 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
248 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
249 rlSrc.highReg);
250#if ANDROID_SMP != 0
251 if (field->IsVolatile()) {
252 oatGenMemBarrier(cUnit, kSY);
253 }
buzbeec143c552011-08-20 17:38:58 -0700254#endif
buzbee1da522d2011-09-04 11:22:20 -0700255 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700256 }
buzbee67bf8852011-08-17 17:51:35 -0700257}
258
259
buzbee67bf8852011-08-17 17:51:35 -0700260static void genSgetWide(CompilationUnit* cUnit, MIR* mir,
261 RegLocation rlResult, RegLocation rlDest)
262{
buzbee1da522d2011-09-04 11:22:20 -0700263 int fieldIdx = mir->dalvikInsn.vB;
264 Field* field = cUnit->method->GetDexCacheResolvedFields()->Get(fieldIdx);
buzbee34cd9e52011-09-08 14:31:52 -0700265 if (SLOW_FIELD_PATH || field == NULL) {
266 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
267 << " unresolved at compile time";
buzbeee1931742011-08-28 21:15:53 -0700268 oatFlushAllRegs(cUnit);
buzbee1da522d2011-09-04 11:22:20 -0700269 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pGet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700270 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
271 loadCurrMethodDirect(cUnit, r1);
272 opReg(cUnit, kOpBlx, rLR);
273 RegLocation rlResult = oatGetReturnWide(cUnit);
274 storeValueWide(cUnit, rlDest, rlResult);
275 } else {
buzbee1da522d2011-09-04 11:22:20 -0700276 // Fast path
277 int fieldOffset = field->GetOffset().Int32Value();
278 art::ClassLinker* class_linker = art::Runtime::Current()->
279 GetClassLinker();
280 const art::DexFile& dex_file = class_linker->
281 FindDexFile(field->GetDeclaringClass()->GetDexCache());
282 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
283 int typeIdx = field_id.class_idx_;
284 // Using fixed register to sync with slow path
285 int rMethod = r1;
286 oatLockTemp(cUnit, rMethod);
287 loadCurrMethodDirect(cUnit, rMethod);
288 int rBase = r0;
289 oatLockTemp(cUnit, rBase);
290 loadWordDisp(cUnit, rMethod,
291 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
292 rBase);
293 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
294 sizeof(int32_t*)* typeIdx, rBase);
295 // TUNING: fast path should fall through
296 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
297 loadWordDisp(cUnit, rSELF,
298 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
299 loadConstant(cUnit, r0, typeIdx);
300 opReg(cUnit, kOpBlx, rLR);
301 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
302 skipTarget->defMask = ENCODE_ALL;
303 branchOver->generic.target = (LIR*)skipTarget;
304 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
305 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
306#if ANDROID_SMP != 0
307 if (isVolatile) {
308 oatGenMemBarrier(cUnit, kSY);
309 }
buzbeec143c552011-08-20 17:38:58 -0700310#endif
buzbee1da522d2011-09-04 11:22:20 -0700311 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
312 rlResult.highReg, INVALID_SREG);
313 oatFreeTemp(cUnit, rBase);
314 storeValueWide(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700315 }
buzbee67bf8852011-08-17 17:51:35 -0700316}
317
318static void genSget(CompilationUnit* cUnit, MIR* mir,
319 RegLocation rlResult, RegLocation rlDest)
320{
buzbee1da522d2011-09-04 11:22:20 -0700321 int fieldIdx = mir->dalvikInsn.vB;
322 Field* field = cUnit->method->GetDexCacheResolvedFields()->Get(fieldIdx);
buzbeee1931742011-08-28 21:15:53 -0700323 bool isObject = ((mir->dalvikInsn.opcode == OP_SGET_OBJECT) ||
324 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE));
buzbee34cd9e52011-09-08 14:31:52 -0700325 if (SLOW_FIELD_PATH || field == NULL) {
326 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
327 << " unresolved at compile time";
buzbee1da522d2011-09-04 11:22:20 -0700328 // Slow path
329 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
330 : OFFSETOF_MEMBER(Thread, pGet32Static);
buzbeee1931742011-08-28 21:15:53 -0700331 oatFlushAllRegs(cUnit);
332 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
333 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
334 loadCurrMethodDirect(cUnit, r1);
335 opReg(cUnit, kOpBlx, rLR);
336 RegLocation rlResult = oatGetReturn(cUnit);
337 storeValue(cUnit, rlDest, rlResult);
338 } else {
buzbee1da522d2011-09-04 11:22:20 -0700339 // Fast path
340 int fieldOffset = field->GetOffset().Int32Value();
341 art::ClassLinker* class_linker = art::Runtime::Current()->
342 GetClassLinker();
343 const art::DexFile& dex_file = class_linker->
344 FindDexFile(field->GetDeclaringClass()->GetDexCache());
345 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
346 int typeIdx = field_id.class_idx_;
347 // Using fixed register to sync with slow path
348 int rMethod = r1;
349 oatLockTemp(cUnit, rMethod);
350 loadCurrMethodDirect(cUnit, rMethod);
351 int rBase = r0;
352 oatLockTemp(cUnit, rBase);
353 loadWordDisp(cUnit, rMethod,
354 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
355 rBase);
356 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
357 sizeof(int32_t*)* typeIdx, rBase);
358 // TUNING: fast path should fall through
359 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
360 loadWordDisp(cUnit, rSELF,
361 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
362 loadConstant(cUnit, r0, typeIdx);
363 opReg(cUnit, kOpBlx, rLR);
364 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
365 skipTarget->defMask = ENCODE_ALL;
366 branchOver->generic.target = (LIR*)skipTarget;
367 rlDest = oatGetDest(cUnit, mir, 0);
368 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700369#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700370 if (isVolatile) {
371 oatGenMemBarrier(cUnit, kSY);
372 }
buzbee67bf8852011-08-17 17:51:35 -0700373#endif
buzbee1da522d2011-09-04 11:22:20 -0700374 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
375 oatFreeTemp(cUnit, rBase);
376 storeValue(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700377 }
buzbee67bf8852011-08-17 17:51:35 -0700378}
379
buzbee561227c2011-09-02 15:28:19 -0700380typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int,
381 ArmLIR*);
buzbee67bf8852011-08-17 17:51:35 -0700382
383/*
384 * Bit of a hack here - in leiu of a real scheduling pass,
385 * emit the next instruction in static & direct invoke sequences.
386 */
387static int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700388 DecodedInstruction* dInsn, int state,
389 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700390{
buzbee561227c2011-09-02 15:28:19 -0700391 DCHECK(rollback == NULL);
392 uint32_t idx = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -0700393 switch(state) {
394 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700395 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700396 break;
buzbee561227c2011-09-02 15:28:19 -0700397 case 1: // Get method->code_and_direct_methods_
398 loadWordDisp(cUnit, r0,
399 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
400 r0);
buzbee67bf8852011-08-17 17:51:35 -0700401 break;
buzbee561227c2011-09-02 15:28:19 -0700402 case 2: // Grab target method* and target code_
403 loadWordDisp(cUnit, r0,
404 art::CodeAndDirectMethods::CodeOffsetInBytes(idx), rLR);
405 loadWordDisp(cUnit, r0,
406 art::CodeAndDirectMethods::MethodOffsetInBytes(idx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700407 break;
408 default:
409 return -1;
410 }
411 return state + 1;
412}
413
buzbee67bf8852011-08-17 17:51:35 -0700414/*
415 * Bit of a hack here - in leiu of a real scheduling pass,
416 * emit the next instruction in a virtual invoke sequence.
417 * We can use rLR as a temp prior to target address loading
418 * Note also that we'll load the first argument ("this") into
419 * r1 here rather than the standard loadArgRegs.
420 */
421static int nextVCallInsn(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);
buzbee67bf8852011-08-17 17:51:35 -0700426 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700427 /*
428 * This is the fast path in which the target virtual method is
429 * fully resolved at compile time.
430 */
431 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
432 Get(dInsn->vB);
433 CHECK(baseMethod != NULL);
434 uint32_t target_idx = baseMethod->GetMethodIndex();
buzbee67bf8852011-08-17 17:51:35 -0700435 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700436 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700437 rlArg = oatGetSrc(cUnit, mir, 0);
438 loadValueDirectFixed(cUnit, rlArg, r1);
439 break;
buzbee561227c2011-09-02 15:28:19 -0700440 case 1: // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700441 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee561227c2011-09-02 15:28:19 -0700442 // get this->klass_ [use r1, set rLR]
443 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700444 break;
buzbee561227c2011-09-02 15:28:19 -0700445 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
446 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700447 break;
buzbee561227c2011-09-02 15:28:19 -0700448 case 3: // Get target method [use rLR, set r0]
449 loadWordDisp(cUnit, rLR, (target_idx * 4) +
450 art::Array::DataOffset().Int32Value(), r0);
451 break;
452 case 4: // Get the target compiled code address [uses r0, sets rLR]
453 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700454 break;
455 default:
456 return -1;
457 }
458 return state + 1;
459}
460
buzbee7b1b86d2011-08-26 18:59:10 -0700461static int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700462 DecodedInstruction* dInsn, int state,
463 ArmLIR* rollback)
buzbee7b1b86d2011-08-26 18:59:10 -0700464{
buzbee561227c2011-09-02 15:28:19 -0700465 DCHECK(rollback != NULL);
buzbee7b1b86d2011-08-26 18:59:10 -0700466 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700467 ArmLIR* skipBranch;
468 ArmLIR* skipTarget;
469 /*
470 * This handles the case in which the base method is not fully
471 * resolved at compile time. We must generate code to test
472 * for resolution a run time, bail to the slow path if not to
473 * fill in all the tables. In the latter case, we'll restart at
474 * at the beginning of the sequence.
475 */
buzbee7b1b86d2011-08-26 18:59:10 -0700476 switch(state) {
477 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700478 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700479 break;
buzbee561227c2011-09-02 15:28:19 -0700480 case 1: // Get method->dex_cache_resolved_methods_
481 loadWordDisp(cUnit, r0,
482 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700483 break;
buzbee561227c2011-09-02 15:28:19 -0700484 case 2: // method->dex_cache_resolved_methods_->Get(method_idx)
485 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
486 art::Array::DataOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700487 break;
buzbee561227c2011-09-02 15:28:19 -0700488 case 3: // Resolved?
489 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
490 // Slowest path, bail to helper, rollback and retry
491 loadWordDisp(cUnit, rSELF,
492 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
493 loadConstant(cUnit, r1, dInsn->vB);
494 newLIR1(cUnit, kThumbBlxR, rLR);
495 genUnconditionalBranch(cUnit, rollback);
496 // Resume normal slow path
497 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
498 skipTarget->defMask = ENCODE_ALL;
499 skipBranch->generic.target = (LIR*)skipTarget;
buzbee4a3164f2011-09-03 11:25:10 -0700500 // Get base_method->method_index [usr rLR, set r0]
buzbee561227c2011-09-02 15:28:19 -0700501 loadBaseDisp(cUnit, mir, rLR,
502 Method::GetMethodIndexOffset().Int32Value(), r0,
503 kUnsignedHalf, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700504 // Load "this" [set r1]
505 rlArg = oatGetSrc(cUnit, mir, 0);
506 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee7b1b86d2011-08-26 18:59:10 -0700507 break;
508 case 4:
509 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700510 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee7b1b86d2011-08-26 18:59:10 -0700511 // get this->clazz [use r1, set rLR]
buzbee561227c2011-09-02 15:28:19 -0700512 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700513 break;
buzbee561227c2011-09-02 15:28:19 -0700514 case 5:
515 // get this->klass_->vtable_ [usr rLR, set rLR]
516 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
517 DCHECK((art::Array::DataOffset().Int32Value() & 0x3) == 0);
518 // In load shadow fold vtable_ object header size into method_index_
519 opRegImm(cUnit, kOpAdd, r0,
520 art::Array::DataOffset().Int32Value() / 4);
521 // Get target Method*
522 loadBaseIndexed(cUnit, rLR, r0, r0, 2, kWord);
523 break;
524 case 6: // Get the target compiled code address [uses r0, sets rLR]
525 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700526 break;
527 default:
528 return -1;
529 }
530 return state + 1;
531}
532
buzbee67bf8852011-08-17 17:51:35 -0700533/* Load up to 3 arguments in r1..r3 */
534static int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
535 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700536 int *args, NextCallInsn nextCallInsn, ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700537{
538 for (int i = 0; i < 3; i++) {
539 if (args[i] != INVALID_REG) {
buzbee1b4c8592011-08-31 10:43:51 -0700540 // Arguments are treated as a series of untyped 32-bit values.
buzbeee9a72f62011-09-04 17:59:07 -0700541 RegLocation rlArg = oatGetRawSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700542 rlArg.wide = false;
buzbee67bf8852011-08-17 17:51:35 -0700543 loadValueDirectFixed(cUnit, rlArg, r1 + i);
buzbee561227c2011-09-02 15:28:19 -0700544 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700545 }
546 }
547 return callState;
548}
549
buzbee4a3164f2011-09-03 11:25:10 -0700550// Interleave launch code for INVOKE_INTERFACE.
buzbee67bf8852011-08-17 17:51:35 -0700551static int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700552 DecodedInstruction* dInsn, int state,
553 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700554{
buzbee67bf8852011-08-17 17:51:35 -0700555 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700556 case 0: // Load trampoline target
557 loadWordDisp(cUnit, rSELF,
558 OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline),
559 rLR);
560 // Load r0 with method index
561 loadConstant(cUnit, r0, dInsn->vB);
buzbee67bf8852011-08-17 17:51:35 -0700562 break;
buzbee67bf8852011-08-17 17:51:35 -0700563 default:
564 return -1;
565 }
566 return state + 1;
567}
568
buzbee67bf8852011-08-17 17:51:35 -0700569/*
570 * Interleave launch code for INVOKE_SUPER. See comments
571 * for nextVCallIns.
572 */
573static int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700574 DecodedInstruction* dInsn, int state,
575 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700576{
buzbee4a3164f2011-09-03 11:25:10 -0700577 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700578 RegLocation rlArg;
buzbee4a3164f2011-09-03 11:25:10 -0700579 /*
580 * This is the fast path in which the target virtual method is
581 * fully resolved at compile time. Note also that this path assumes
582 * that the check to verify that the target method index falls
583 * within the size of the super's vtable has been done at compile-time.
584 */
585 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
586 Get(dInsn->vB);
587 CHECK(baseMethod != NULL);
588 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
589 CHECK(superClass != NULL);
590 int32_t target_idx = baseMethod->GetMethodIndex();
591 CHECK(superClass->GetVTable()->GetLength() > target_idx);
592 Method* targetMethod = superClass->GetVTable()->Get(target_idx);
593 CHECK(targetMethod != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700594 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700595 case 0: // Get current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700596 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700597 // Load "this" [set r1]
598 rlArg = oatGetSrc(cUnit, mir, 0);
599 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee4a3164f2011-09-03 11:25:10 -0700600 // Get method->declaring_class_ [use r0, set rLR]
601 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
602 rLR);
buzbee67bf8852011-08-17 17:51:35 -0700603 // Is "this" null? [use r1]
buzbee5ade1d22011-09-09 14:44:52 -0700604 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee4a3164f2011-09-03 11:25:10 -0700605 break;
606 case 1: // Get method->declaring_class_->super_class [usr rLR, set rLR]
607 loadWordDisp(cUnit, rLR, Class::SuperClassOffset().Int32Value(),
608 rLR);
609 break;
610 case 2: // Get ...->super_class_->vtable [u/s rLR]
611 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
612 break;
613 case 3: // Get target method [use rLR, set r0]
614 loadWordDisp(cUnit, rLR, (target_idx * 4) +
615 art::Array::DataOffset().Int32Value(), r0);
616 break;
617 case 4: // Get the target compiled code address [uses r0, sets rLR]
618 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
619 break;
buzbee67bf8852011-08-17 17:51:35 -0700620 default:
621 return -1;
622 }
buzbee4a3164f2011-09-03 11:25:10 -0700623 return state + 1;
624}
625
626/* Slow-path version of nextSuperCallInsn */
627static int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
628 DecodedInstruction* dInsn, int state,
629 ArmLIR* rollback)
630{
631 DCHECK(rollback != NULL);
632 RegLocation rlArg;
633 ArmLIR* skipBranch;
634 ArmLIR* skipTarget;
635 int tReg;
636 /*
637 * This handles the case in which the base method is not fully
638 * resolved at compile time. We must generate code to test
639 * for resolution a run time, bail to the slow path if not to
640 * fill in all the tables. In the latter case, we'll restart at
641 * at the beginning of the sequence.
642 */
643 switch(state) {
644 case 0: // Get the current Method* [sets r0]
645 loadCurrMethodDirect(cUnit, r0);
646 break;
647 case 1: // Get method->dex_cache_resolved_methods_ [usr r0, set rLR]
648 loadWordDisp(cUnit, r0,
649 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
650 break;
651 case 2: // method->dex_cache_resolved_methods_->Get(meth_idx) [u/s rLR]
652 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
653 art::Array::DataOffset().Int32Value(), rLR);
654 break;
655 case 3: // Resolved?
656 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
657 // Slowest path, bail to helper, rollback and retry
658 loadWordDisp(cUnit, rSELF,
659 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
660 loadConstant(cUnit, r1, dInsn->vB);
661 newLIR1(cUnit, kThumbBlxR, rLR);
662 genUnconditionalBranch(cUnit, rollback);
663 // Resume normal slow path
664 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
665 skipTarget->defMask = ENCODE_ALL;
666 skipBranch->generic.target = (LIR*)skipTarget;
667 // Get base_method->method_index [usr rLR, set rLR]
668 loadBaseDisp(cUnit, mir, rLR,
669 Method::GetMethodIndexOffset().Int32Value(), rLR,
670 kUnsignedHalf, INVALID_SREG);
671 // Load "this" [set r1]
672 rlArg = oatGetSrc(cUnit, mir, 0);
673 loadValueDirectFixed(cUnit, rlArg, r1);
674 // Load curMethod->declaring_class_ [uses r0, sets r0]
675 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
676 r0);
buzbee6a0f7f52011-09-05 16:14:20 -0700677 // Null this?
buzbee5ade1d22011-09-09 14:44:52 -0700678 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee6a0f7f52011-09-05 16:14:20 -0700679 // Get method->declaring_class_->super_class [usr r0, set r0]
buzbee4a3164f2011-09-03 11:25:10 -0700680 loadWordDisp(cUnit, r0, Class::SuperClassOffset().Int32Value(), r0);
681 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700682 case 4: // Get ...->super_class_->vtable [u/s r0]
buzbee4a3164f2011-09-03 11:25:10 -0700683 loadWordDisp(cUnit, r0, Class::VTableOffset().Int32Value(), r0);
buzbee4a3164f2011-09-03 11:25:10 -0700684 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
685 // Range check, throw NSM on failure
686 tReg = oatAllocTemp(cUnit);
687 loadWordDisp(cUnit, r0, art::Array::LengthOffset().Int32Value(),
688 tReg);
buzbee5ade1d22011-09-09 14:44:52 -0700689 genBoundsCheck(cUnit, tReg, rLR, mir, kArmThrowNoSuchMethod);
buzbee4a3164f2011-09-03 11:25:10 -0700690 oatFreeTemp(cUnit, tReg);
691 }
buzbee6a0f7f52011-09-05 16:14:20 -0700692 // Adjust vtable_ base past object header
693 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
buzbee4a3164f2011-09-03 11:25:10 -0700694 // Get target Method*
buzbee6a0f7f52011-09-05 16:14:20 -0700695 loadBaseIndexed(cUnit, r0, rLR, r0, 2, kWord);
buzbee4a3164f2011-09-03 11:25:10 -0700696 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700697 case 5: // Get the target compiled code address [uses r0, sets rLR]
buzbee4a3164f2011-09-03 11:25:10 -0700698 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
699 break;
700 default:
701 return -1;
702 }
buzbee67bf8852011-08-17 17:51:35 -0700703 return state + 1;
704}
705
706/*
707 * Load up to 5 arguments, the first three of which will be in
708 * r1 .. r3. On entry r0 contains the current method pointer,
709 * and as part of the load sequence, it must be replaced with
710 * the target method pointer. Note, this may also be called
711 * for "range" variants if the number of arguments is 5 or fewer.
712 */
713static int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
714 DecodedInstruction* dInsn, int callState,
715 ArmLIR** pcrLabel, bool isRange,
buzbee1da522d2011-09-04 11:22:20 -0700716 NextCallInsn nextCallInsn, ArmLIR* rollback,
717 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700718{
719 RegLocation rlArg;
720 int registerArgs[3];
721
722 /* If no arguments, just return */
723 if (dInsn->vA == 0)
724 return callState;
725
buzbee561227c2011-09-02 15:28:19 -0700726 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700727
728 /*
729 * Load frame arguments arg4 & arg5 first. Coded a little odd to
730 * pre-schedule the method pointer target.
731 */
732 for (unsigned int i=3; i < dInsn->vA; i++) {
733 int reg;
buzbeee9a72f62011-09-04 17:59:07 -0700734 rlArg = oatUpdateLoc(cUnit, oatGetSrc(cUnit, mir, i));
buzbee67bf8852011-08-17 17:51:35 -0700735 if (rlArg.location == kLocPhysReg) {
736 reg = rlArg.lowReg;
737 } else {
buzbee109bd6a2011-09-06 13:58:41 -0700738 // r3 is the last arg register loaded, so can safely be used here
739 reg = r3;
740 loadValueDirectFixed(cUnit, rlArg, reg);
buzbee561227c2011-09-02 15:28:19 -0700741 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700742 }
743 storeBaseDisp(cUnit, rSP, (i + 1) * 4, reg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700744 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700745 }
746
747 /* Load register arguments r1..r3 */
buzbeee9a72f62011-09-04 17:59:07 -0700748 for (unsigned int i = 0; i < 3; i++) {
buzbee67bf8852011-08-17 17:51:35 -0700749 if (i < dInsn->vA)
750 registerArgs[i] = (isRange) ? dInsn->vC + i : i;
751 else
752 registerArgs[i] = INVALID_REG;
753 }
buzbeee9a72f62011-09-04 17:59:07 -0700754 if (skipThis) {
755 registerArgs[0] = INVALID_REG;
756 }
buzbee67bf8852011-08-17 17:51:35 -0700757 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
buzbee561227c2011-09-02 15:28:19 -0700758 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700759
buzbee6a0f7f52011-09-05 16:14:20 -0700760 //TODO: better to move this into CallInsn lists
buzbee67bf8852011-08-17 17:51:35 -0700761 // Load direct & need a "this" null check?
762 if (pcrLabel) {
buzbee5ade1d22011-09-09 14:44:52 -0700763 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir);
buzbee67bf8852011-08-17 17:51:35 -0700764 }
765 return callState;
766}
767
768/*
769 * May have 0+ arguments (also used for jumbo). Note that
770 * source virtual registers may be in physical registers, so may
771 * need to be flushed to home location before copying. This
772 * applies to arg3 and above (see below).
773 *
774 * Two general strategies:
775 * If < 20 arguments
776 * Pass args 3-18 using vldm/vstm block copy
777 * Pass arg0, arg1 & arg2 in r1-r3
778 * If 20+ arguments
779 * Pass args arg19+ using memcpy block copy
780 * Pass arg0, arg1 & arg2 in r1-r3
781 *
782 */
783static int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
784 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700785 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700786 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700787{
788 int firstArg = dInsn->vC;
789 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700790 int registerArgs[3];
791
buzbee67bf8852011-08-17 17:51:35 -0700792 // If we can treat it as non-range (Jumbo ops will use range form)
793 if (numArgs <= 5)
794 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700795 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700796 /*
797 * Make sure range list doesn't span the break between in normal
798 * Dalvik vRegs and the ins.
799 */
buzbee1b4c8592011-08-31 10:43:51 -0700800 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700802 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
803 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700804 }
805
806 /*
807 * First load the non-register arguments. Both forms expect all
808 * of the source arguments to be in their home frame location, so
809 * scan the sReg names and flush any that have been promoted to
810 * frame backing storage.
811 */
812 // Scan the rest of the args - if in physReg flush to memory
buzbee0c7f26d2011-09-07 12:28:51 -0700813 for (int i = 3; i < numArgs; i++) {
buzbeee9a72f62011-09-04 17:59:07 -0700814 RegLocation loc = oatGetRawSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700815 if (loc.wide) {
816 loc = oatUpdateLocWide(cUnit, loc);
817 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
818 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
819 loc.highReg);
buzbee561227c2011-09-02 15:28:19 -0700820 callState = nextCallInsn(cUnit, mir, dInsn, callState,
821 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700822 }
823 } else {
824 loc = oatUpdateLoc(cUnit, loc);
825 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
826 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700827 callState = nextCallInsn(cUnit, mir, dInsn, callState,
828 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700829 }
buzbee67bf8852011-08-17 17:51:35 -0700830 }
831 }
832
833 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
834 int outsOffset = 4 /* Method* */ + (3 * 4);
835 if (numArgs >= 20) {
836 // Generate memcpy, but first make sure all of
837 opRegRegImm(cUnit, kOpAdd, r0, rSP, startOffset);
838 opRegRegImm(cUnit, kOpAdd, r1, rSP, outsOffset);
839 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
840 loadConstant(cUnit, r2, (numArgs - 3) * 4);
841 newLIR1(cUnit, kThumbBlxR, rLR);
842 } else {
843 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700844 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700845 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700846 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbee1b4c8592011-08-31 10:43:51 -0700847 newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700848 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700849 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700850 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700851 newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700852 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700853 }
854
855 // Handle the 1st 3 in r1, r2 & r3
buzbeee9a72f62011-09-04 17:59:07 -0700856 for (unsigned int i = 0; i < 3; i++) {
857 if (i < dInsn->vA)
858 registerArgs[i] = dInsn->vC + i;
859 else
860 registerArgs[i] = INVALID_REG;
buzbee67bf8852011-08-17 17:51:35 -0700861 }
buzbeee9a72f62011-09-04 17:59:07 -0700862 if (skipThis) {
863 registerArgs[0] = INVALID_REG;
864 }
865 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
866 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700867
buzbee561227c2011-09-02 15:28:19 -0700868 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700869 return callState;
870}
871
buzbee2a475e72011-09-07 17:19:17 -0700872#ifdef DISPLAY_MISSING_TARGETS
873// Debugging routine - if null target, branch to DebugMe
874static void genShowTarget(CompilationUnit* cUnit)
875{
876 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
877 loadWordDisp(cUnit, rSELF,
878 OFFSETOF_MEMBER(Thread, pDebugMe), rLR);
879 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
880 target->defMask = -1;
881 branchOver->generic.target = (LIR*)target;
882}
883#endif
884
buzbee561227c2011-09-02 15:28:19 -0700885static void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
886 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700887{
888 DecodedInstruction* dInsn = &mir->dalvikInsn;
889 int callState = 0;
890 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700891 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee561227c2011-09-02 15:28:19 -0700892 NextCallInsn nextCallInsn = nextSDCallInsn;
893
buzbee109bd6a2011-09-06 13:58:41 -0700894 // Explicit register usage
895 oatLockCallTemps(cUnit);
896
buzbee561227c2011-09-02 15:28:19 -0700897 if (range) {
898 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700899 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700900 } else {
901 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700902 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700903 }
buzbee67bf8852011-08-17 17:51:35 -0700904 // Finish up any of the call sequence not interleaved in arg loading
905 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700906 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700907 }
buzbee2a475e72011-09-07 17:19:17 -0700908#ifdef DISPLAY_MISSING_TARGETS
909 genShowTarget(cUnit);
910#endif
buzbee67bf8852011-08-17 17:51:35 -0700911 newLIR1(cUnit, kThumbBlxR, rLR);
912}
913
buzbee4a3164f2011-09-03 11:25:10 -0700914/*
915 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
916 * which will locate the target and continue on via a tail call.
917 */
buzbee67bf8852011-08-17 17:51:35 -0700918static void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
919{
920 DecodedInstruction* dInsn = &mir->dalvikInsn;
921 int callState = 0;
922 ArmLIR* nullCk;
buzbee109bd6a2011-09-06 13:58:41 -0700923
924 // Explicit register usage
925 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700926 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -0700927 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700928 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
929 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700930 false, nextInterfaceCallInsn, NULL,
931 true);
buzbee67bf8852011-08-17 17:51:35 -0700932 else
933 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700934 nextInterfaceCallInsn, NULL, true);
buzbee67bf8852011-08-17 17:51:35 -0700935 // Finish up any of the call sequence not interleaved in arg loading
936 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700937 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700938 }
buzbee2a475e72011-09-07 17:19:17 -0700939#ifdef DISPLAY_MISSING_TARGETS
940 genShowTarget(cUnit);
941#endif
buzbee67bf8852011-08-17 17:51:35 -0700942 newLIR1(cUnit, kThumbBlxR, rLR);
943}
944
945static void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
946{
947 DecodedInstruction* dInsn = &mir->dalvikInsn;
948 int callState = 0;
949 ArmLIR* nullCk;
buzbee4a3164f2011-09-03 11:25:10 -0700950 ArmLIR* rollback;
951 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
952 Get(dInsn->vB);
953 NextCallInsn nextCallInsn;
954 bool fastPath = true;
buzbee109bd6a2011-09-06 13:58:41 -0700955
956 // Explicit register usage
957 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -0700958 if (SLOW_INVOKE_PATH || baseMethod == NULL) {
buzbee4a3164f2011-09-03 11:25:10 -0700959 fastPath = false;
960 } else {
961 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
962 if (superClass == NULL) {
963 fastPath = false;
964 } else {
965 int32_t target_idx = baseMethod->GetMethodIndex();
966 if (superClass->GetVTable()->GetLength() <= target_idx) {
967 fastPath = false;
968 } else {
969 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
970 }
971 }
972 }
973 if (fastPath) {
974 nextCallInsn = nextSuperCallInsn;
975 rollback = NULL;
976 } else {
977 nextCallInsn = nextSuperCallInsnSP;
978 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
979 rollback->defMask = -1;
980 }
buzbee67bf8852011-08-17 17:51:35 -0700981 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
982 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700983 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700984 else
985 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700986 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700987 // Finish up any of the call sequence not interleaved in arg loading
988 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -0700989 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700990 }
buzbee2a475e72011-09-07 17:19:17 -0700991#ifdef DISPLAY_MISSING_TARGETS
992 genShowTarget(cUnit);
993#endif
buzbee67bf8852011-08-17 17:51:35 -0700994 newLIR1(cUnit, kThumbBlxR, rLR);
995}
996
997static void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
998{
999 DecodedInstruction* dInsn = &mir->dalvikInsn;
1000 int callState = 0;
1001 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -07001002 ArmLIR* rollback;
1003 Method* method = cUnit->method->GetDexCacheResolvedMethods()->
1004 Get(dInsn->vB);
1005 NextCallInsn nextCallInsn;
buzbee7b1b86d2011-08-26 18:59:10 -07001006
buzbee109bd6a2011-09-06 13:58:41 -07001007 // Explicit register usage
1008 oatLockCallTemps(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -07001009 if (SLOW_INVOKE_PATH || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -07001010 // Slow path
1011 nextCallInsn = nextVCallInsnSP;
1012 // If we need a slow-path callout, we'll restart here
1013 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
1014 rollback->defMask = -1;
1015 } else {
1016 // Fast path
1017 nextCallInsn = nextVCallInsn;
1018 rollback = NULL;
1019 }
buzbee67bf8852011-08-17 17:51:35 -07001020 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
1021 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001022 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001023 else
1024 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -07001025 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -07001026 // Finish up any of the call sequence not interleaved in arg loading
1027 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -07001028 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -07001029 }
buzbee2a475e72011-09-07 17:19:17 -07001030#ifdef DISPLAY_MISSING_TARGETS
1031 genShowTarget(cUnit);
1032#endif
buzbee67bf8852011-08-17 17:51:35 -07001033 newLIR1(cUnit, kThumbBlxR, rLR);
1034}
1035
buzbee67bf8852011-08-17 17:51:35 -07001036static bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
1037 BasicBlock* bb, ArmLIR* labelList)
1038{
1039 bool res = false; // Assume success
1040 RegLocation rlSrc[3];
1041 RegLocation rlDest = badLoc;
1042 RegLocation rlResult = badLoc;
1043 Opcode opcode = mir->dalvikInsn.opcode;
1044
1045 /* Prep Src and Dest locations */
1046 int nextSreg = 0;
1047 int nextLoc = 0;
1048 int attrs = oatDataFlowAttributes[opcode];
1049 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1050 if (attrs & DF_UA) {
1051 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1052 nextSreg++;
1053 } else if (attrs & DF_UA_WIDE) {
1054 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1055 nextSreg + 1);
1056 nextSreg+= 2;
1057 }
1058 if (attrs & DF_UB) {
1059 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1060 nextSreg++;
1061 } else if (attrs & DF_UB_WIDE) {
1062 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1063 nextSreg + 1);
1064 nextSreg+= 2;
1065 }
1066 if (attrs & DF_UC) {
1067 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1068 } else if (attrs & DF_UC_WIDE) {
1069 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1070 nextSreg + 1);
1071 }
1072 if (attrs & DF_DA) {
1073 rlDest = oatGetDest(cUnit, mir, 0);
1074 } else if (attrs & DF_DA_WIDE) {
1075 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1076 }
1077
1078 switch(opcode) {
1079 case OP_NOP:
1080 break;
1081
1082 case OP_MOVE_EXCEPTION:
1083 int exOffset;
1084 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001085 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001086 resetReg = oatAllocTemp(cUnit);
1087 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1088 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1089 loadConstant(cUnit, resetReg, 0);
1090 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1091 storeValue(cUnit, rlDest, rlResult);
1092 break;
1093
1094 case OP_RETURN_VOID:
1095 break;
1096
1097 case OP_RETURN:
1098 case OP_RETURN_OBJECT:
1099 storeValue(cUnit, retLoc, rlSrc[0]);
1100 break;
1101
1102 case OP_RETURN_WIDE:
1103 rlDest = retLocWide;
1104 rlDest.fp = rlSrc[0].fp;
1105 storeValueWide(cUnit, rlDest, rlSrc[0]);
1106 break;
1107
1108 case OP_MOVE_RESULT_WIDE:
1109 if (mir->OptimizationFlags & MIR_INLINED)
1110 break; // Nop - combined w/ previous invoke
1111 /*
1112 * Somewhat hacky here. Because we're now passing
1113 * return values in registers, we have to let the
1114 * register allocation utilities know that the return
1115 * registers are live and may not be used for address
1116 * formation in storeValueWide.
1117 */
1118 assert(retLocWide.lowReg == r0);
buzbee1da522d2011-09-04 11:22:20 -07001119 assert(retLocWide.highReg == r1);
buzbee67bf8852011-08-17 17:51:35 -07001120 oatLockTemp(cUnit, retLocWide.lowReg);
1121 oatLockTemp(cUnit, retLocWide.highReg);
1122 storeValueWide(cUnit, rlDest, retLocWide);
1123 oatFreeTemp(cUnit, retLocWide.lowReg);
1124 oatFreeTemp(cUnit, retLocWide.highReg);
1125 break;
1126
1127 case OP_MOVE_RESULT:
1128 case OP_MOVE_RESULT_OBJECT:
1129 if (mir->OptimizationFlags & MIR_INLINED)
1130 break; // Nop - combined w/ previous invoke
1131 /* See comment for OP_MOVE_RESULT_WIDE */
1132 assert(retLoc.lowReg == r0);
1133 oatLockTemp(cUnit, retLoc.lowReg);
1134 storeValue(cUnit, rlDest, retLoc);
1135 oatFreeTemp(cUnit, retLoc.lowReg);
1136 break;
1137
1138 case OP_MOVE:
1139 case OP_MOVE_OBJECT:
1140 case OP_MOVE_16:
1141 case OP_MOVE_OBJECT_16:
1142 case OP_MOVE_FROM16:
1143 case OP_MOVE_OBJECT_FROM16:
1144 storeValue(cUnit, rlDest, rlSrc[0]);
1145 break;
1146
1147 case OP_MOVE_WIDE:
1148 case OP_MOVE_WIDE_16:
1149 case OP_MOVE_WIDE_FROM16:
1150 storeValueWide(cUnit, rlDest, rlSrc[0]);
1151 break;
1152
1153 case OP_CONST:
1154 case OP_CONST_4:
1155 case OP_CONST_16:
1156 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1157 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1158 storeValue(cUnit, rlDest, rlResult);
1159 break;
1160
1161 case OP_CONST_HIGH16:
1162 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1163 loadConstantNoClobber(cUnit, rlResult.lowReg,
1164 mir->dalvikInsn.vB << 16);
1165 storeValue(cUnit, rlDest, rlResult);
1166 break;
1167
1168 case OP_CONST_WIDE_16:
1169 case OP_CONST_WIDE_32:
1170 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1171 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1172 //TUNING: do high separately to avoid load dependency
1173 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1174 storeValueWide(cUnit, rlDest, rlResult);
1175 break;
1176
1177 case OP_CONST_WIDE:
1178 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1179 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001180 mir->dalvikInsn.vB_wide & 0xffffffff,
1181 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001182 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001183 break;
1184
1185 case OP_CONST_WIDE_HIGH16:
1186 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1187 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1188 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001189 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001190 break;
1191
1192 case OP_MONITOR_ENTER:
1193 genMonitorEnter(cUnit, mir, rlSrc[0]);
1194 break;
1195
1196 case OP_MONITOR_EXIT:
1197 genMonitorExit(cUnit, mir, rlSrc[0]);
1198 break;
1199
1200 case OP_CHECK_CAST:
1201 genCheckCast(cUnit, mir, rlSrc[0]);
1202 break;
1203
1204 case OP_INSTANCE_OF:
1205 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1206 break;
1207
1208 case OP_NEW_INSTANCE:
1209 genNewInstance(cUnit, mir, rlDest);
1210 break;
1211
1212 case OP_THROW:
1213 genThrow(cUnit, mir, rlSrc[0]);
1214 break;
1215
buzbee5ade1d22011-09-09 14:44:52 -07001216 case OP_THROW_VERIFICATION_ERROR:
1217 loadWordDisp(cUnit, rSELF,
1218 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode), rLR);
1219 loadConstant(cUnit, r0, mir->dalvikInsn.vA);
1220 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
1221 opReg(cUnit, kOpBlx, rLR);
1222 break;
1223
buzbee67bf8852011-08-17 17:51:35 -07001224 case OP_ARRAY_LENGTH:
1225 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001226 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001227 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee5ade1d22011-09-09 14:44:52 -07001228 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001229 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1230 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1231 rlResult.lowReg);
1232 storeValue(cUnit, rlDest, rlResult);
1233 break;
1234
1235 case OP_CONST_STRING:
1236 case OP_CONST_STRING_JUMBO:
1237 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1238 break;
1239
1240 case OP_CONST_CLASS:
1241 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1242 break;
1243
1244 case OP_FILL_ARRAY_DATA:
1245 genFillArrayData(cUnit, mir, rlSrc[0]);
1246 break;
1247
1248 case OP_FILLED_NEW_ARRAY:
1249 genFilledNewArray(cUnit, mir, false /* not range */);
1250 break;
1251
1252 case OP_FILLED_NEW_ARRAY_RANGE:
1253 genFilledNewArray(cUnit, mir, true /* range */);
1254 break;
1255
1256 case OP_NEW_ARRAY:
1257 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1258 break;
1259
1260 case OP_GOTO:
1261 case OP_GOTO_16:
1262 case OP_GOTO_32:
1263 // TUNING: add MIR flag to disable when unnecessary
1264 bool backwardBranch;
1265 backwardBranch = (bb->taken->startOffset <= mir->offset);
1266 if (backwardBranch) {
1267 genSuspendPoll(cUnit, mir);
1268 }
1269 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1270 break;
1271
1272 case OP_PACKED_SWITCH:
1273 genPackedSwitch(cUnit, mir, rlSrc[0]);
1274 break;
1275
1276 case OP_SPARSE_SWITCH:
1277 genSparseSwitch(cUnit, mir, rlSrc[0]);
1278 break;
1279
1280 case OP_CMPL_FLOAT:
1281 case OP_CMPG_FLOAT:
1282 case OP_CMPL_DOUBLE:
1283 case OP_CMPG_DOUBLE:
1284 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1285 break;
1286
1287 case OP_CMP_LONG:
1288 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1289 break;
1290
1291 case OP_IF_EQ:
1292 case OP_IF_NE:
1293 case OP_IF_LT:
1294 case OP_IF_GE:
1295 case OP_IF_GT:
1296 case OP_IF_LE: {
1297 bool backwardBranch;
1298 ArmConditionCode cond;
1299 backwardBranch = (bb->taken->startOffset <= mir->offset);
1300 if (backwardBranch) {
1301 genSuspendPoll(cUnit, mir);
1302 }
1303 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1304 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1305 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1306 switch(opcode) {
1307 case OP_IF_EQ:
1308 cond = kArmCondEq;
1309 break;
1310 case OP_IF_NE:
1311 cond = kArmCondNe;
1312 break;
1313 case OP_IF_LT:
1314 cond = kArmCondLt;
1315 break;
1316 case OP_IF_GE:
1317 cond = kArmCondGe;
1318 break;
1319 case OP_IF_GT:
1320 cond = kArmCondGt;
1321 break;
1322 case OP_IF_LE:
1323 cond = kArmCondLe;
1324 break;
1325 default:
1326 cond = (ArmConditionCode)0;
1327 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1328 }
1329 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1330 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1331 break;
1332 }
1333
1334 case OP_IF_EQZ:
1335 case OP_IF_NEZ:
1336 case OP_IF_LTZ:
1337 case OP_IF_GEZ:
1338 case OP_IF_GTZ:
1339 case OP_IF_LEZ: {
1340 bool backwardBranch;
1341 ArmConditionCode cond;
1342 backwardBranch = (bb->taken->startOffset <= mir->offset);
1343 if (backwardBranch) {
1344 genSuspendPoll(cUnit, mir);
1345 }
1346 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1347 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1348 switch(opcode) {
1349 case OP_IF_EQZ:
1350 cond = kArmCondEq;
1351 break;
1352 case OP_IF_NEZ:
1353 cond = kArmCondNe;
1354 break;
1355 case OP_IF_LTZ:
1356 cond = kArmCondLt;
1357 break;
1358 case OP_IF_GEZ:
1359 cond = kArmCondGe;
1360 break;
1361 case OP_IF_GTZ:
1362 cond = kArmCondGt;
1363 break;
1364 case OP_IF_LEZ:
1365 cond = kArmCondLe;
1366 break;
1367 default:
1368 cond = (ArmConditionCode)0;
1369 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1370 }
1371 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1372 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1373 break;
1374 }
1375
1376 case OP_AGET_WIDE:
1377 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1378 break;
1379 case OP_AGET:
1380 case OP_AGET_OBJECT:
1381 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1382 break;
1383 case OP_AGET_BOOLEAN:
1384 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1385 rlDest, 0);
1386 break;
1387 case OP_AGET_BYTE:
1388 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1389 break;
1390 case OP_AGET_CHAR:
1391 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1392 rlDest, 1);
1393 break;
1394 case OP_AGET_SHORT:
1395 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1396 break;
1397 case OP_APUT_WIDE:
1398 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1399 break;
1400 case OP_APUT:
1401 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1402 break;
1403 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001404 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001405 break;
1406 case OP_APUT_SHORT:
1407 case OP_APUT_CHAR:
1408 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1409 rlSrc[0], 1);
1410 break;
1411 case OP_APUT_BYTE:
1412 case OP_APUT_BOOLEAN:
1413 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1414 rlSrc[0], 0);
1415 break;
1416
1417 case OP_IGET_WIDE:
1418 case OP_IGET_WIDE_VOLATILE:
1419 genIGetWideX(cUnit, mir, rlDest, rlSrc[0]);
1420 break;
1421
1422 case OP_IGET:
1423 case OP_IGET_VOLATILE:
1424 case OP_IGET_OBJECT:
1425 case OP_IGET_OBJECT_VOLATILE:
1426 genIGetX(cUnit, mir, kWord, rlDest, rlSrc[0]);
1427 break;
1428
1429 case OP_IGET_BOOLEAN:
1430 case OP_IGET_BYTE:
1431 genIGetX(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
1432 break;
1433
1434 case OP_IGET_CHAR:
1435 genIGetX(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
1436 break;
1437
1438 case OP_IGET_SHORT:
1439 genIGetX(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
1440 break;
1441
1442 case OP_IPUT_WIDE:
1443 case OP_IPUT_WIDE_VOLATILE:
1444 genIPutWideX(cUnit, mir, rlSrc[0], rlSrc[1]);
1445 break;
1446
1447 case OP_IPUT_OBJECT:
1448 case OP_IPUT_OBJECT_VOLATILE:
1449 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
1450 break;
1451
1452 case OP_IPUT:
1453 case OP_IPUT_VOLATILE:
1454 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
1455 break;
1456
1457 case OP_IPUT_BOOLEAN:
1458 case OP_IPUT_BYTE:
1459 genIPutX(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
1460 break;
1461
1462 case OP_IPUT_CHAR:
1463 genIPutX(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
1464 break;
1465
1466 case OP_IPUT_SHORT:
1467 genIPutX(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
1468 break;
1469
1470 case OP_SGET:
1471 case OP_SGET_OBJECT:
1472 case OP_SGET_BOOLEAN:
1473 case OP_SGET_BYTE:
1474 case OP_SGET_CHAR:
1475 case OP_SGET_SHORT:
1476 genSget(cUnit, mir, rlResult, rlDest);
1477 break;
1478
1479 case OP_SGET_WIDE:
1480 genSgetWide(cUnit, mir, rlResult, rlDest);
1481 break;
1482
1483 case OP_SPUT:
1484 case OP_SPUT_OBJECT:
1485 case OP_SPUT_BOOLEAN:
1486 case OP_SPUT_BYTE:
1487 case OP_SPUT_CHAR:
1488 case OP_SPUT_SHORT:
1489 genSput(cUnit, mir, rlSrc[0]);
1490 break;
1491
1492 case OP_SPUT_WIDE:
1493 genSputWide(cUnit, mir, rlSrc[0]);
1494 break;
1495
1496 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001497 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1498 true /*range*/);
1499 break;
buzbee67bf8852011-08-17 17:51:35 -07001500 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001501 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1502 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001503 break;
1504
1505 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001506 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1507 false /*range*/);
1508 break;
buzbee67bf8852011-08-17 17:51:35 -07001509 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001510 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1511 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001512 break;
1513
1514 case OP_INVOKE_VIRTUAL:
1515 case OP_INVOKE_VIRTUAL_RANGE:
1516 genInvokeVirtual(cUnit, mir);
1517 break;
1518
1519 case OP_INVOKE_SUPER:
1520 case OP_INVOKE_SUPER_RANGE:
1521 genInvokeSuper(cUnit, mir);
1522 break;
1523
1524 case OP_INVOKE_INTERFACE:
1525 case OP_INVOKE_INTERFACE_RANGE:
1526 genInvokeInterface(cUnit, mir);
1527 break;
1528
1529 case OP_NEG_INT:
1530 case OP_NOT_INT:
1531 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1532 break;
1533
1534 case OP_NEG_LONG:
1535 case OP_NOT_LONG:
1536 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1537 break;
1538
1539 case OP_NEG_FLOAT:
1540 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1541 break;
1542
1543 case OP_NEG_DOUBLE:
1544 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1545 break;
1546
1547 case OP_INT_TO_LONG:
1548 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1549 if (rlSrc[0].location == kLocPhysReg) {
1550 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1551 } else {
1552 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1553 }
1554 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1555 rlResult.lowReg, 31);
1556 storeValueWide(cUnit, rlDest, rlResult);
1557 break;
1558
1559 case OP_LONG_TO_INT:
1560 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1561 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1562 storeValue(cUnit, rlDest, rlSrc[0]);
1563 break;
1564
1565 case OP_INT_TO_BYTE:
1566 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1567 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1568 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1569 storeValue(cUnit, rlDest, rlResult);
1570 break;
1571
1572 case OP_INT_TO_SHORT:
1573 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1574 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1575 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1576 storeValue(cUnit, rlDest, rlResult);
1577 break;
1578
1579 case OP_INT_TO_CHAR:
1580 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1581 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1582 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1583 storeValue(cUnit, rlDest, rlResult);
1584 break;
1585
1586 case OP_INT_TO_FLOAT:
1587 case OP_INT_TO_DOUBLE:
1588 case OP_LONG_TO_FLOAT:
1589 case OP_LONG_TO_DOUBLE:
1590 case OP_FLOAT_TO_INT:
1591 case OP_FLOAT_TO_LONG:
1592 case OP_FLOAT_TO_DOUBLE:
1593 case OP_DOUBLE_TO_INT:
1594 case OP_DOUBLE_TO_LONG:
1595 case OP_DOUBLE_TO_FLOAT:
1596 genConversion(cUnit, mir);
1597 break;
1598
1599 case OP_ADD_INT:
1600 case OP_SUB_INT:
1601 case OP_MUL_INT:
1602 case OP_DIV_INT:
1603 case OP_REM_INT:
1604 case OP_AND_INT:
1605 case OP_OR_INT:
1606 case OP_XOR_INT:
1607 case OP_SHL_INT:
1608 case OP_SHR_INT:
1609 case OP_USHR_INT:
1610 case OP_ADD_INT_2ADDR:
1611 case OP_SUB_INT_2ADDR:
1612 case OP_MUL_INT_2ADDR:
1613 case OP_DIV_INT_2ADDR:
1614 case OP_REM_INT_2ADDR:
1615 case OP_AND_INT_2ADDR:
1616 case OP_OR_INT_2ADDR:
1617 case OP_XOR_INT_2ADDR:
1618 case OP_SHL_INT_2ADDR:
1619 case OP_SHR_INT_2ADDR:
1620 case OP_USHR_INT_2ADDR:
1621 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1622 break;
1623
1624 case OP_ADD_LONG:
1625 case OP_SUB_LONG:
1626 case OP_MUL_LONG:
1627 case OP_DIV_LONG:
1628 case OP_REM_LONG:
1629 case OP_AND_LONG:
1630 case OP_OR_LONG:
1631 case OP_XOR_LONG:
1632 case OP_ADD_LONG_2ADDR:
1633 case OP_SUB_LONG_2ADDR:
1634 case OP_MUL_LONG_2ADDR:
1635 case OP_DIV_LONG_2ADDR:
1636 case OP_REM_LONG_2ADDR:
1637 case OP_AND_LONG_2ADDR:
1638 case OP_OR_LONG_2ADDR:
1639 case OP_XOR_LONG_2ADDR:
1640 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1641 break;
1642
buzbee67bf8852011-08-17 17:51:35 -07001643 case OP_SHL_LONG:
1644 case OP_SHR_LONG:
1645 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001646 case OP_SHL_LONG_2ADDR:
1647 case OP_SHR_LONG_2ADDR:
1648 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001649 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1650 break;
1651
1652 case OP_ADD_FLOAT:
1653 case OP_SUB_FLOAT:
1654 case OP_MUL_FLOAT:
1655 case OP_DIV_FLOAT:
1656 case OP_REM_FLOAT:
1657 case OP_ADD_FLOAT_2ADDR:
1658 case OP_SUB_FLOAT_2ADDR:
1659 case OP_MUL_FLOAT_2ADDR:
1660 case OP_DIV_FLOAT_2ADDR:
1661 case OP_REM_FLOAT_2ADDR:
1662 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1663 break;
1664
1665 case OP_ADD_DOUBLE:
1666 case OP_SUB_DOUBLE:
1667 case OP_MUL_DOUBLE:
1668 case OP_DIV_DOUBLE:
1669 case OP_REM_DOUBLE:
1670 case OP_ADD_DOUBLE_2ADDR:
1671 case OP_SUB_DOUBLE_2ADDR:
1672 case OP_MUL_DOUBLE_2ADDR:
1673 case OP_DIV_DOUBLE_2ADDR:
1674 case OP_REM_DOUBLE_2ADDR:
1675 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1676 break;
1677
1678 case OP_RSUB_INT:
1679 case OP_ADD_INT_LIT16:
1680 case OP_MUL_INT_LIT16:
1681 case OP_DIV_INT_LIT16:
1682 case OP_REM_INT_LIT16:
1683 case OP_AND_INT_LIT16:
1684 case OP_OR_INT_LIT16:
1685 case OP_XOR_INT_LIT16:
1686 case OP_ADD_INT_LIT8:
1687 case OP_RSUB_INT_LIT8:
1688 case OP_MUL_INT_LIT8:
1689 case OP_DIV_INT_LIT8:
1690 case OP_REM_INT_LIT8:
1691 case OP_AND_INT_LIT8:
1692 case OP_OR_INT_LIT8:
1693 case OP_XOR_INT_LIT8:
1694 case OP_SHL_INT_LIT8:
1695 case OP_SHR_INT_LIT8:
1696 case OP_USHR_INT_LIT8:
1697 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1698 break;
1699
1700 default:
1701 res = true;
1702 }
1703 return res;
1704}
1705
1706static const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
1707 "kMirOpPhi",
1708 "kMirOpNullNRangeUpCheck",
1709 "kMirOpNullNRangeDownCheck",
1710 "kMirOpLowerBound",
1711 "kMirOpPunt",
1712 "kMirOpCheckInlinePrediction",
1713};
1714
1715/* Extended MIR instructions like PHI */
1716static void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
1717{
1718 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1719 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1720 strcpy(msg, extendedMIROpNames[opOffset]);
1721 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1722
1723 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1724 case kMirOpPhi: {
1725 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1726 op->flags.isNop = true;
1727 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1728 break;
1729 }
1730 default:
1731 break;
1732 }
1733}
1734
1735/* If there are any ins passed in registers that have not been promoted
1736 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001737 * Note: at this pointCopy any ins that are passed in register to their
1738 * home location */
buzbee67bf8852011-08-17 17:51:35 -07001739static void flushIns(CompilationUnit* cUnit)
1740{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001741 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001742 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001743 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1744 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001745 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001746 int startLoc = cUnit->method->NumRegisters() -
1747 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001748 for (int i = 0; i < inRegs; i++) {
1749 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001750 //TUNING: be smarter about flushing ins to frame
1751 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001752 if (loc.location == kLocPhysReg) {
1753 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001754 }
1755 }
1756
1757 // Handle special case of wide argument half in regs, half in frame
1758 if (inRegs == 3) {
1759 RegLocation loc = cUnit->regLocation[startLoc + 2];
1760 if (loc.wide && loc.location == kLocPhysReg) {
1761 // Load the other half of the arg into the promoted pair
buzbee561227c2011-09-02 15:28:19 -07001762 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -07001763 inRegs++;
1764 }
1765 }
1766
1767 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001768 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001769 RegLocation loc = cUnit->regLocation[startLoc + i];
1770 if (loc.fpLocation == kLocPhysReg) {
1771 loc.location = kLocPhysReg;
1772 loc.fp = true;
1773 loc.lowReg = loc.fpLowReg;
1774 loc.highReg = loc.fpHighReg;
1775 }
1776 if (loc.location == kLocPhysReg) {
1777 if (loc.wide) {
1778 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1779 loc.lowReg, loc.highReg, INVALID_SREG);
1780 i++;
1781 } else {
buzbee561227c2011-09-02 15:28:19 -07001782 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001783 }
1784 }
1785 i++;
1786 }
1787}
1788
1789/* Handle the content in each basic block */
1790static bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
1791{
1792 MIR* mir;
1793 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1794 int blockId = bb->id;
1795
1796 cUnit->curBlock = bb;
1797 labelList[blockId].operands[0] = bb->startOffset;
1798
1799 /* Insert the block label */
1800 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1801 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1802
1803 oatClobberAllRegs(cUnit);
1804 oatResetNullCheck(cUnit);
1805
1806 ArmLIR* headLIR = NULL;
1807
1808 if (bb->blockType == kEntryBlock) {
1809 /*
1810 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1811 * mechanism know so it doesn't try to use any of them when
1812 * expanding the frame or flushing. This leaves the utility
1813 * code with a single temp: r12. This should be enough.
1814 */
1815 oatLockTemp(cUnit, r0);
1816 oatLockTemp(cUnit, r1);
1817 oatLockTemp(cUnit, r2);
1818 oatLockTemp(cUnit, r3);
buzbeecefd1872011-09-09 09:59:52 -07001819
1820 /*
1821 * We can safely skip the stack overflow check if we're
1822 * a leaf *and* our frame size < fudge factor.
1823 */
1824 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
1825 ((size_t)cUnit->frameSize <
1826 art::Thread::kStackOverflowReservedBytes));
buzbee67bf8852011-08-17 17:51:35 -07001827 newLIR0(cUnit, kArmPseudoMethodEntry);
buzbeecefd1872011-09-09 09:59:52 -07001828 if (!skipOverflowCheck) {
1829 /* Load stack limit */
1830 loadWordDisp(cUnit, rSELF,
1831 art::Thread::StackEndOffset().Int32Value(), r12);
1832 }
buzbee67bf8852011-08-17 17:51:35 -07001833 /* Spill core callee saves */
1834 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1835 /* Need to spill any FP regs? */
1836 if (cUnit->numFPSpills) {
1837 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1838 }
buzbeecefd1872011-09-09 09:59:52 -07001839 if (!skipOverflowCheck) {
1840 opRegRegImm(cUnit, kOpSub, rLR, rSP,
1841 cUnit->frameSize - (cUnit->numSpills * 4));
1842 opRegReg(cUnit, kOpCmp, rLR, r12); // Stack overflow?
1843 /* Begin conditional skip */
1844 genIT(cUnit, kArmCondCc, "TT"); // Carry clear; unsigned <
1845 loadWordDisp(cUnit, rSELF,
1846 OFFSETOF_MEMBER(Thread, pStackOverflowFromCode), rLR);
1847 newLIR2(cUnit, kThumbAddRI8, rSP, cUnit->numSpills * 4);
1848 opReg(cUnit, kOpBlx, rLR);
1849 /* End conditional skip */
1850 genRegCopy(cUnit, rSP, rLR); // Establish stack
1851 } else {
1852 opRegImm(cUnit, kOpSub, rSP,
1853 cUnit->frameSize - (cUnit->numSpills * 4));
1854 }
buzbee67bf8852011-08-17 17:51:35 -07001855 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1856 flushIns(cUnit);
1857 oatFreeTemp(cUnit, r0);
1858 oatFreeTemp(cUnit, r1);
1859 oatFreeTemp(cUnit, r2);
1860 oatFreeTemp(cUnit, r3);
1861 } else if (bb->blockType == kExitBlock) {
1862 newLIR0(cUnit, kArmPseudoMethodExit);
1863 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1864 /* Need to restore any FP callee saves? */
1865 if (cUnit->numFPSpills) {
1866 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1867 }
1868 if (cUnit->coreSpillMask & (1 << rLR)) {
1869 /* Unspill rLR to rPC */
1870 cUnit->coreSpillMask &= ~(1 << rLR);
1871 cUnit->coreSpillMask |= (1 << rPC);
1872 }
1873 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1874 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1875 /* We didn't pop to rPC, so must do a bv rLR */
1876 newLIR1(cUnit, kThumbBx, rLR);
1877 }
1878 }
1879
1880 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1881
1882 oatResetRegPool(cUnit);
1883 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1884 oatClobberAllRegs(cUnit);
1885 }
1886
1887 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1888 oatResetDefTracking(cUnit);
1889 }
1890
1891 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1892 handleExtendedMethodMIR(cUnit, mir);
1893 continue;
1894 }
1895
1896 cUnit->currentDalvikOffset = mir->offset;
1897
1898 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1899 InstructionFormat dalvikFormat =
1900 dexGetFormatFromOpcode(dalvikOpcode);
1901
1902 ArmLIR* boundaryLIR;
1903
1904 /* Mark the beginning of a Dalvik instruction for line tracking */
1905 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1906 (int) oatGetDalvikDisassembly(
1907 &mir->dalvikInsn, ""));
1908 /* Remember the first LIR for this block */
1909 if (headLIR == NULL) {
1910 headLIR = boundaryLIR;
1911 /* Set the first boundaryLIR as a scheduling barrier */
1912 headLIR->defMask = ENCODE_ALL;
1913 }
1914
1915 /* Don't generate the SSA annotation unless verbose mode is on */
1916 if (cUnit->printMe && mir->ssaRep) {
1917 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1918 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1919 }
1920
1921 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1922
1923 if (notHandled) {
1924 char buf[100];
1925 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1926 mir->offset,
1927 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1928 dalvikFormat);
1929 LOG(FATAL) << buf;
1930 }
1931 }
1932
1933 if (headLIR) {
1934 /*
1935 * Eliminate redundant loads/stores and delay stores into later
1936 * slots
1937 */
1938 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1939 cUnit->lastLIRInsn);
1940
1941 /*
1942 * Generate an unconditional branch to the fallthrough block.
1943 */
1944 if (bb->fallThrough) {
1945 genUnconditionalBranch(cUnit,
1946 &labelList[bb->fallThrough->id]);
1947 }
1948 }
1949 return false;
1950}
1951
1952/*
1953 * Nop any unconditional branches that go to the next instruction.
1954 * Note: new redundant branches may be inserted later, and we'll
1955 * use a check in final instruction assembly to nop those out.
1956 */
1957void removeRedundantBranches(CompilationUnit* cUnit)
1958{
1959 ArmLIR* thisLIR;
1960
1961 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1962 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1963 thisLIR = NEXT_LIR(thisLIR)) {
1964
1965 /* Branch to the next instruction */
1966 if ((thisLIR->opcode == kThumbBUncond) ||
1967 (thisLIR->opcode == kThumb2BUncond)) {
1968 ArmLIR* nextLIR = thisLIR;
1969
1970 while (true) {
1971 nextLIR = NEXT_LIR(nextLIR);
1972
1973 /*
1974 * Is the branch target the next instruction?
1975 */
1976 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1977 thisLIR->flags.isNop = true;
1978 break;
1979 }
1980
1981 /*
1982 * Found real useful stuff between the branch and the target.
1983 * Need to explicitly check the lastLIRInsn here because it
1984 * might be the last real instruction.
1985 */
1986 if (!isPseudoOpcode(nextLIR->opcode) ||
1987 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1988 break;
1989 }
1990 }
1991 }
1992}
1993
buzbee5ade1d22011-09-09 14:44:52 -07001994static void handleThrowLaunchpads(CompilationUnit *cUnit)
1995{
1996 ArmLIR** throwLabel =
1997 (ArmLIR **) cUnit->throwLaunchpads.elemList;
1998 int numElems = cUnit->throwLaunchpads.numUsed;
1999 int i;
2000
2001 for (i = 0; i < numElems; i++) {
2002 ArmLIR* lab = throwLabel[i];
2003 cUnit->currentDalvikOffset = lab->operands[1];
2004 oatAppendLIR(cUnit, (LIR *)lab);
2005 int funcOffset = 0;
2006 int v1 = lab->operands[2];
2007 int v2 = lab->operands[3];
2008 switch(lab->operands[0]) {
2009 case kArmThrowNullPointer:
2010 funcOffset = OFFSETOF_MEMBER(Thread, pThrowNullPointerFromCode);
2011 break;
2012 case kArmThrowArrayBounds:
2013 if (v2 != r0) {
2014 genRegCopy(cUnit, r0, v1);
2015 genRegCopy(cUnit, r1, v2);
2016 } else {
2017 if (v1 == r1) {
2018 genRegCopy(cUnit, r12, v1);
2019 genRegCopy(cUnit, r1, v2);
2020 genRegCopy(cUnit, r0, r12);
2021 } else {
2022 genRegCopy(cUnit, r1, v2);
2023 genRegCopy(cUnit, r0, v1);
2024 }
2025 }
2026 funcOffset = OFFSETOF_MEMBER(Thread, pThrowArrayBoundsFromCode);
2027 break;
2028 case kArmThrowDivZero:
2029 funcOffset = OFFSETOF_MEMBER(Thread, pThrowDivZeroFromCode);
2030 break;
2031 case kArmThrowVerificationError:
2032 loadConstant(cUnit, r0, v1);
2033 loadConstant(cUnit, r1, v2);
2034 funcOffset =
2035 OFFSETOF_MEMBER(Thread, pThrowVerificationErrorFromCode);
2036 break;
2037 case kArmThrowNegArraySize:
2038 genRegCopy(cUnit, r0, v1);
2039 funcOffset =
2040 OFFSETOF_MEMBER(Thread, pThrowNegArraySizeFromCode);
2041 break;
2042 case kArmThrowInternalError:
2043 genRegCopy(cUnit, r0, v1);
2044 funcOffset =
2045 OFFSETOF_MEMBER(Thread, pThrowInternalErrorFromCode);
2046 break;
2047 case kArmThrowRuntimeException:
2048 genRegCopy(cUnit, r0, v1);
2049 funcOffset =
2050 OFFSETOF_MEMBER(Thread, pThrowRuntimeExceptionFromCode);
2051 break;
2052 case kArmThrowNoSuchMethod:
2053 genRegCopy(cUnit, r0, v1);
2054 funcOffset =
2055 OFFSETOF_MEMBER(Thread, pThrowNoSuchMethodFromCode);
2056 break;
2057 default:
2058 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
2059 }
2060 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
2061 opReg(cUnit, kOpBlx, rLR);
2062 }
2063}
2064
buzbee67bf8852011-08-17 17:51:35 -07002065void oatMethodMIR2LIR(CompilationUnit* cUnit)
2066{
2067 /* Used to hold the labels of each block */
2068 cUnit->blockLabelList =
2069 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
2070
2071 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
2072 kPreOrderDFSTraversal, false /* Iterative */);
2073 removeRedundantBranches(cUnit);
buzbee5ade1d22011-09-09 14:44:52 -07002074
2075 handleThrowLaunchpads(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07002076}
2077
2078/* Common initialization routine for an architecture family */
2079bool oatArchInit()
2080{
2081 int i;
2082
2083 for (i = 0; i < kArmLast; i++) {
2084 if (EncodingMap[i].opcode != i) {
2085 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
2086 " is wrong: expecting " << i << ", seeing " <<
2087 (int)EncodingMap[i].opcode;
2088 }
2089 }
2090
2091 return oatArchVariantInit();
2092}
2093
2094/* Needed by the Assembler */
2095void oatSetupResourceMasks(ArmLIR* lir)
2096{
2097 setupResourceMasks(lir);
2098}
2099
2100/* Needed by the ld/st optmizatons */
2101ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
2102{
2103 return genRegCopyNoInsert(cUnit, rDest, rSrc);
2104}
2105
2106/* Needed by the register allocator */
2107ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
2108{
2109 return genRegCopy(cUnit, rDest, rSrc);
2110}
2111
2112/* Needed by the register allocator */
2113void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
2114 int srcLo, int srcHi)
2115{
2116 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
2117}
2118
2119void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
2120 int displacement, int rSrc, OpSize size)
2121{
2122 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
2123}
2124
2125void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
2126 int displacement, int rSrcLo, int rSrcHi)
2127{
2128 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
2129}