blob: 13e5497798248dec5a78bc28d9b9b7bb890e432c [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
buzbeee9a72f62011-09-04 17:59:07 -070017#define FORCE_SLOW 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
148 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pSetObjStatic)
149 : OFFSETOF_MEMBER(Thread, pSet32Static);
buzbeee1931742011-08-28 21:15:53 -0700150 oatFlushAllRegs(cUnit);
151 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
152 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
153 loadCurrMethodDirect(cUnit, r1);
154 loadValueDirect(cUnit, rlSrc, r2);
155 opReg(cUnit, kOpBlx, rLR);
156 oatClobberCallRegs(cUnit);
157 } else {
buzbee1da522d2011-09-04 11:22:20 -0700158 // fast path
159 int fieldOffset = field->GetOffset().Int32Value();
160 art::ClassLinker* class_linker = art::Runtime::Current()->
161 GetClassLinker();
162 const art::DexFile& dex_file = class_linker->
163 FindDexFile(field->GetDeclaringClass()->GetDexCache());
164 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
165 int typeIdx = field_id.class_idx_;
166 // Using fixed register to sync with slow path
167 int rMethod = r1;
168 oatLockTemp(cUnit, rMethod);
169 loadCurrMethodDirect(cUnit, rMethod);
170 int rBase = r0;
171 oatLockTemp(cUnit, rBase);
172 loadWordDisp(cUnit, rMethod,
173 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
174 rBase);
175 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
176 sizeof(int32_t*)* typeIdx, rBase);
177 // TUNING: fast path should fall through
178 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
179 loadWordDisp(cUnit, rSELF,
180 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
181 loadConstant(cUnit, r0, typeIdx);
182 opReg(cUnit, kOpBlx, rLR);
183 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
184 skipTarget->defMask = ENCODE_ALL;
185 branchOver->generic.target = (LIR*)skipTarget;
186 rlSrc = oatGetSrc(cUnit, mir, 0);
187 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
188 storeWordDisp(cUnit, rBase, fieldOffset, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700189#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700190 if (field->IsVolatile()) {
191 oatGenMemBarrier(cUnit, kSY);
192 }
buzbee67bf8852011-08-17 17:51:35 -0700193#endif
buzbee1da522d2011-09-04 11:22:20 -0700194 if (isObject) {
195 markGCCard(cUnit, rlSrc.lowReg, rBase);
196 }
197 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700198 }
buzbee67bf8852011-08-17 17:51:35 -0700199}
200
201static void genSputWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
202{
buzbee1da522d2011-09-04 11:22:20 -0700203 int fieldIdx = mir->dalvikInsn.vB;
204 Field* field = cUnit->method->GetDexCacheResolvedFields()->Get(fieldIdx);
buzbeee9a72f62011-09-04 17:59:07 -0700205 if (FORCE_SLOW || field == NULL) {
buzbeee1931742011-08-28 21:15:53 -0700206 oatFlushAllRegs(cUnit);
buzbee1da522d2011-09-04 11:22:20 -0700207 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pSet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700208 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
209 loadCurrMethodDirect(cUnit, r1);
210 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
211 opReg(cUnit, kOpBlx, rLR);
212 oatClobberCallRegs(cUnit);
213 } else {
buzbee1da522d2011-09-04 11:22:20 -0700214 // fast path
215 int fieldOffset = field->GetOffset().Int32Value();
216 art::ClassLinker* class_linker = art::Runtime::Current()->
217 GetClassLinker();
218 const art::DexFile& dex_file = class_linker->
219 FindDexFile(field->GetDeclaringClass()->GetDexCache());
220 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
221 int typeIdx = field_id.class_idx_;
222 // Using fixed register to sync with slow path
223 int rMethod = r1;
224 oatLockTemp(cUnit, rMethod);
225 loadCurrMethodDirect(cUnit, r1);
226 int rBase = r0;
227 oatLockTemp(cUnit, rBase);
228 loadWordDisp(cUnit, rMethod,
229 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
230 rBase);
231 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
232 sizeof(int32_t*)* typeIdx, rBase);
233 // TUNING: fast path should fall through
234 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
235 loadWordDisp(cUnit, rSELF,
236 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
237 loadConstant(cUnit, r0, typeIdx);
238 opReg(cUnit, kOpBlx, rLR);
239 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
240 skipTarget->defMask = ENCODE_ALL;
241 branchOver->generic.target = (LIR*)skipTarget;
242 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
243 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
244 storeBaseDispWide(cUnit, rBase, fieldOffset, rlSrc.lowReg,
245 rlSrc.highReg);
246#if ANDROID_SMP != 0
247 if (field->IsVolatile()) {
248 oatGenMemBarrier(cUnit, kSY);
249 }
buzbeec143c552011-08-20 17:38:58 -0700250#endif
buzbee1da522d2011-09-04 11:22:20 -0700251 oatFreeTemp(cUnit, rBase);
buzbeee1931742011-08-28 21:15:53 -0700252 }
buzbee67bf8852011-08-17 17:51:35 -0700253}
254
255
buzbee67bf8852011-08-17 17:51:35 -0700256static void genSgetWide(CompilationUnit* cUnit, MIR* mir,
257 RegLocation rlResult, RegLocation rlDest)
258{
buzbee1da522d2011-09-04 11:22:20 -0700259 int fieldIdx = mir->dalvikInsn.vB;
260 Field* field = cUnit->method->GetDexCacheResolvedFields()->Get(fieldIdx);
buzbeee9a72f62011-09-04 17:59:07 -0700261 if (FORCE_SLOW || field == NULL) {
buzbeee1931742011-08-28 21:15:53 -0700262 oatFlushAllRegs(cUnit);
buzbee1da522d2011-09-04 11:22:20 -0700263 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pGet64Static), rLR);
buzbeee1931742011-08-28 21:15:53 -0700264 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
265 loadCurrMethodDirect(cUnit, r1);
266 opReg(cUnit, kOpBlx, rLR);
267 RegLocation rlResult = oatGetReturnWide(cUnit);
268 storeValueWide(cUnit, rlDest, rlResult);
269 } else {
buzbee1da522d2011-09-04 11:22:20 -0700270 // Fast path
271 int fieldOffset = field->GetOffset().Int32Value();
272 art::ClassLinker* class_linker = art::Runtime::Current()->
273 GetClassLinker();
274 const art::DexFile& dex_file = class_linker->
275 FindDexFile(field->GetDeclaringClass()->GetDexCache());
276 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
277 int typeIdx = field_id.class_idx_;
278 // Using fixed register to sync with slow path
279 int rMethod = r1;
280 oatLockTemp(cUnit, rMethod);
281 loadCurrMethodDirect(cUnit, rMethod);
282 int rBase = r0;
283 oatLockTemp(cUnit, rBase);
284 loadWordDisp(cUnit, rMethod,
285 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
286 rBase);
287 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
288 sizeof(int32_t*)* typeIdx, rBase);
289 // TUNING: fast path should fall through
290 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
291 loadWordDisp(cUnit, rSELF,
292 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
293 loadConstant(cUnit, r0, typeIdx);
294 opReg(cUnit, kOpBlx, rLR);
295 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
296 skipTarget->defMask = ENCODE_ALL;
297 branchOver->generic.target = (LIR*)skipTarget;
298 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
299 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
300#if ANDROID_SMP != 0
301 if (isVolatile) {
302 oatGenMemBarrier(cUnit, kSY);
303 }
buzbeec143c552011-08-20 17:38:58 -0700304#endif
buzbee1da522d2011-09-04 11:22:20 -0700305 loadBaseDispWide(cUnit, NULL, rBase, fieldOffset, rlResult.lowReg,
306 rlResult.highReg, INVALID_SREG);
307 oatFreeTemp(cUnit, rBase);
308 storeValueWide(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700309 }
buzbee67bf8852011-08-17 17:51:35 -0700310}
311
312static void genSget(CompilationUnit* cUnit, MIR* mir,
313 RegLocation rlResult, RegLocation rlDest)
314{
buzbee1da522d2011-09-04 11:22:20 -0700315 int fieldIdx = mir->dalvikInsn.vB;
316 Field* field = cUnit->method->GetDexCacheResolvedFields()->Get(fieldIdx);
buzbeee1931742011-08-28 21:15:53 -0700317 bool isObject = ((mir->dalvikInsn.opcode == OP_SGET_OBJECT) ||
318 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE));
buzbeee9a72f62011-09-04 17:59:07 -0700319 if (FORCE_SLOW || field == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700320 // Slow path
321 int funcOffset = isObject ? OFFSETOF_MEMBER(Thread, pGetObjStatic)
322 : OFFSETOF_MEMBER(Thread, pGet32Static);
buzbeee1931742011-08-28 21:15:53 -0700323 oatFlushAllRegs(cUnit);
324 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
325 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
326 loadCurrMethodDirect(cUnit, r1);
327 opReg(cUnit, kOpBlx, rLR);
328 RegLocation rlResult = oatGetReturn(cUnit);
329 storeValue(cUnit, rlDest, rlResult);
330 } else {
buzbee1da522d2011-09-04 11:22:20 -0700331 // Fast path
332 int fieldOffset = field->GetOffset().Int32Value();
333 art::ClassLinker* class_linker = art::Runtime::Current()->
334 GetClassLinker();
335 const art::DexFile& dex_file = class_linker->
336 FindDexFile(field->GetDeclaringClass()->GetDexCache());
337 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
338 int typeIdx = field_id.class_idx_;
339 // Using fixed register to sync with slow path
340 int rMethod = r1;
341 oatLockTemp(cUnit, rMethod);
342 loadCurrMethodDirect(cUnit, rMethod);
343 int rBase = r0;
344 oatLockTemp(cUnit, rBase);
345 loadWordDisp(cUnit, rMethod,
346 Method::DexCacheInitializedStaticStorageOffset().Int32Value(),
347 rBase);
348 loadWordDisp(cUnit, rBase, art::Array::DataOffset().Int32Value() +
349 sizeof(int32_t*)* typeIdx, rBase);
350 // TUNING: fast path should fall through
351 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, rBase, 0);
352 loadWordDisp(cUnit, rSELF,
353 OFFSETOF_MEMBER(Thread, pInitializeStaticStorage), rLR);
354 loadConstant(cUnit, r0, typeIdx);
355 opReg(cUnit, kOpBlx, rLR);
356 ArmLIR* skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
357 skipTarget->defMask = ENCODE_ALL;
358 branchOver->generic.target = (LIR*)skipTarget;
359 rlDest = oatGetDest(cUnit, mir, 0);
360 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700361#if ANDROID_SMP != 0
buzbee1da522d2011-09-04 11:22:20 -0700362 if (isVolatile) {
363 oatGenMemBarrier(cUnit, kSY);
364 }
buzbee67bf8852011-08-17 17:51:35 -0700365#endif
buzbee1da522d2011-09-04 11:22:20 -0700366 loadWordDisp(cUnit, rBase, fieldOffset, rlResult.lowReg);
367 oatFreeTemp(cUnit, rBase);
368 storeValue(cUnit, rlDest, rlResult);
buzbeee1931742011-08-28 21:15:53 -0700369 }
buzbee67bf8852011-08-17 17:51:35 -0700370}
371
buzbee561227c2011-09-02 15:28:19 -0700372typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int,
373 ArmLIR*);
buzbee67bf8852011-08-17 17:51:35 -0700374
375/*
376 * Bit of a hack here - in leiu of a real scheduling pass,
377 * emit the next instruction in static & direct invoke sequences.
378 */
379static int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700380 DecodedInstruction* dInsn, int state,
381 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700382{
buzbee561227c2011-09-02 15:28:19 -0700383 DCHECK(rollback == NULL);
384 uint32_t idx = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -0700385 switch(state) {
386 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700387 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700388 break;
buzbee561227c2011-09-02 15:28:19 -0700389 case 1: // Get method->code_and_direct_methods_
390 loadWordDisp(cUnit, r0,
391 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
392 r0);
buzbee67bf8852011-08-17 17:51:35 -0700393 break;
buzbee561227c2011-09-02 15:28:19 -0700394 case 2: // Grab target method* and target code_
395 loadWordDisp(cUnit, r0,
396 art::CodeAndDirectMethods::CodeOffsetInBytes(idx), rLR);
397 loadWordDisp(cUnit, r0,
398 art::CodeAndDirectMethods::MethodOffsetInBytes(idx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700399 break;
400 default:
401 return -1;
402 }
403 return state + 1;
404}
405
buzbee67bf8852011-08-17 17:51:35 -0700406/*
407 * Bit of a hack here - in leiu of a real scheduling pass,
408 * emit the next instruction in a virtual invoke sequence.
409 * We can use rLR as a temp prior to target address loading
410 * Note also that we'll load the first argument ("this") into
411 * r1 here rather than the standard loadArgRegs.
412 */
413static int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700414 DecodedInstruction* dInsn, int state,
415 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700416{
buzbee561227c2011-09-02 15:28:19 -0700417 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700418 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700419 /*
420 * This is the fast path in which the target virtual method is
421 * fully resolved at compile time.
422 */
423 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
424 Get(dInsn->vB);
425 CHECK(baseMethod != NULL);
426 uint32_t target_idx = baseMethod->GetMethodIndex();
buzbee67bf8852011-08-17 17:51:35 -0700427 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700428 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700429 rlArg = oatGetSrc(cUnit, mir, 0);
430 loadValueDirectFixed(cUnit, rlArg, r1);
431 break;
buzbee561227c2011-09-02 15:28:19 -0700432 case 1: // Is "this" null? [use r1]
433 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir->offset, NULL);
434 // get this->klass_ [use r1, set rLR]
435 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700436 break;
buzbee561227c2011-09-02 15:28:19 -0700437 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
438 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700439 break;
buzbee561227c2011-09-02 15:28:19 -0700440 case 3: // Get target method [use rLR, set r0]
441 loadWordDisp(cUnit, rLR, (target_idx * 4) +
442 art::Array::DataOffset().Int32Value(), r0);
443 break;
444 case 4: // Get the target compiled code address [uses r0, sets rLR]
445 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700446 break;
447 default:
448 return -1;
449 }
450 return state + 1;
451}
452
buzbee7b1b86d2011-08-26 18:59:10 -0700453static int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700454 DecodedInstruction* dInsn, int state,
455 ArmLIR* rollback)
buzbee7b1b86d2011-08-26 18:59:10 -0700456{
buzbee561227c2011-09-02 15:28:19 -0700457 DCHECK(rollback != NULL);
buzbee7b1b86d2011-08-26 18:59:10 -0700458 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700459 ArmLIR* skipBranch;
460 ArmLIR* skipTarget;
461 /*
462 * This handles the case in which the base method is not fully
463 * resolved at compile time. We must generate code to test
464 * for resolution a run time, bail to the slow path if not to
465 * fill in all the tables. In the latter case, we'll restart at
466 * at the beginning of the sequence.
467 */
buzbee7b1b86d2011-08-26 18:59:10 -0700468 switch(state) {
469 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700470 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700471 break;
buzbee561227c2011-09-02 15:28:19 -0700472 case 1: // Get method->dex_cache_resolved_methods_
473 loadWordDisp(cUnit, r0,
474 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700475 break;
buzbee561227c2011-09-02 15:28:19 -0700476 case 2: // method->dex_cache_resolved_methods_->Get(method_idx)
477 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
478 art::Array::DataOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700479 break;
buzbee561227c2011-09-02 15:28:19 -0700480 case 3: // Resolved?
481 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
482 // Slowest path, bail to helper, rollback and retry
483 loadWordDisp(cUnit, rSELF,
484 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
485 loadConstant(cUnit, r1, dInsn->vB);
486 newLIR1(cUnit, kThumbBlxR, rLR);
487 genUnconditionalBranch(cUnit, rollback);
488 // Resume normal slow path
489 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
490 skipTarget->defMask = ENCODE_ALL;
491 skipBranch->generic.target = (LIR*)skipTarget;
buzbee4a3164f2011-09-03 11:25:10 -0700492 // Get base_method->method_index [usr rLR, set r0]
buzbee561227c2011-09-02 15:28:19 -0700493 loadBaseDisp(cUnit, mir, rLR,
494 Method::GetMethodIndexOffset().Int32Value(), r0,
495 kUnsignedHalf, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700496 // Load "this" [set r1]
497 rlArg = oatGetSrc(cUnit, mir, 0);
498 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee7b1b86d2011-08-26 18:59:10 -0700499 break;
500 case 4:
501 // Is "this" null? [use r1]
502 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir->offset, NULL);
503 // get this->clazz [use r1, set rLR]
buzbee561227c2011-09-02 15:28:19 -0700504 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700505 break;
buzbee561227c2011-09-02 15:28:19 -0700506 case 5:
507 // get this->klass_->vtable_ [usr rLR, set rLR]
508 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
509 DCHECK((art::Array::DataOffset().Int32Value() & 0x3) == 0);
510 // In load shadow fold vtable_ object header size into method_index_
511 opRegImm(cUnit, kOpAdd, r0,
512 art::Array::DataOffset().Int32Value() / 4);
513 // Get target Method*
514 loadBaseIndexed(cUnit, rLR, r0, r0, 2, kWord);
515 break;
516 case 6: // Get the target compiled code address [uses r0, sets rLR]
517 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700518 break;
519 default:
520 return -1;
521 }
522 return state + 1;
523}
524
buzbee67bf8852011-08-17 17:51:35 -0700525/* Load up to 3 arguments in r1..r3 */
526static int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
527 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700528 int *args, NextCallInsn nextCallInsn, ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700529{
530 for (int i = 0; i < 3; i++) {
531 if (args[i] != INVALID_REG) {
buzbee1b4c8592011-08-31 10:43:51 -0700532 // Arguments are treated as a series of untyped 32-bit values.
buzbeee9a72f62011-09-04 17:59:07 -0700533 RegLocation rlArg = oatGetRawSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700534 rlArg.wide = false;
buzbee67bf8852011-08-17 17:51:35 -0700535 loadValueDirectFixed(cUnit, rlArg, r1 + i);
buzbee561227c2011-09-02 15:28:19 -0700536 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700537 }
538 }
539 return callState;
540}
541
buzbee4a3164f2011-09-03 11:25:10 -0700542// Interleave launch code for INVOKE_INTERFACE.
buzbee67bf8852011-08-17 17:51:35 -0700543static int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700544 DecodedInstruction* dInsn, int state,
545 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700546{
buzbee67bf8852011-08-17 17:51:35 -0700547 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700548 case 0: // Load trampoline target
549 loadWordDisp(cUnit, rSELF,
550 OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline),
551 rLR);
552 // Load r0 with method index
553 loadConstant(cUnit, r0, dInsn->vB);
buzbee67bf8852011-08-17 17:51:35 -0700554 break;
buzbee67bf8852011-08-17 17:51:35 -0700555 default:
556 return -1;
557 }
558 return state + 1;
559}
560
buzbee67bf8852011-08-17 17:51:35 -0700561/*
562 * Interleave launch code for INVOKE_SUPER. See comments
563 * for nextVCallIns.
564 */
565static int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700566 DecodedInstruction* dInsn, int state,
567 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700568{
buzbee4a3164f2011-09-03 11:25:10 -0700569 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700570 RegLocation rlArg;
buzbee4a3164f2011-09-03 11:25:10 -0700571 /*
572 * This is the fast path in which the target virtual method is
573 * fully resolved at compile time. Note also that this path assumes
574 * that the check to verify that the target method index falls
575 * within the size of the super's vtable has been done at compile-time.
576 */
577 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
578 Get(dInsn->vB);
579 CHECK(baseMethod != NULL);
580 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
581 CHECK(superClass != NULL);
582 int32_t target_idx = baseMethod->GetMethodIndex();
583 CHECK(superClass->GetVTable()->GetLength() > target_idx);
584 Method* targetMethod = superClass->GetVTable()->Get(target_idx);
585 CHECK(targetMethod != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700586 switch(state) {
buzbee4a3164f2011-09-03 11:25:10 -0700587 case 0: // Get current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700588 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700589 // Load "this" [set r1]
590 rlArg = oatGetSrc(cUnit, mir, 0);
591 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee4a3164f2011-09-03 11:25:10 -0700592 // Get method->declaring_class_ [use r0, set rLR]
593 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
594 rLR);
buzbee67bf8852011-08-17 17:51:35 -0700595 // Is "this" null? [use r1]
596 genNullCheck(cUnit, oatSSASrc(mir,0), r1,
597 mir->offset, NULL);
buzbee4a3164f2011-09-03 11:25:10 -0700598 break;
599 case 1: // Get method->declaring_class_->super_class [usr rLR, set rLR]
600 loadWordDisp(cUnit, rLR, Class::SuperClassOffset().Int32Value(),
601 rLR);
602 break;
603 case 2: // Get ...->super_class_->vtable [u/s rLR]
604 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
605 break;
606 case 3: // Get target method [use rLR, set r0]
607 loadWordDisp(cUnit, rLR, (target_idx * 4) +
608 art::Array::DataOffset().Int32Value(), r0);
609 break;
610 case 4: // Get the target compiled code address [uses r0, sets rLR]
611 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
612 break;
buzbee67bf8852011-08-17 17:51:35 -0700613 default:
614 return -1;
615 }
buzbee4a3164f2011-09-03 11:25:10 -0700616 return state + 1;
617}
618
619/* Slow-path version of nextSuperCallInsn */
620static int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir,
621 DecodedInstruction* dInsn, int state,
622 ArmLIR* rollback)
623{
624 DCHECK(rollback != NULL);
625 RegLocation rlArg;
626 ArmLIR* skipBranch;
627 ArmLIR* skipTarget;
628 int tReg;
629 /*
630 * This handles the case in which the base method is not fully
631 * resolved at compile time. We must generate code to test
632 * for resolution a run time, bail to the slow path if not to
633 * fill in all the tables. In the latter case, we'll restart at
634 * at the beginning of the sequence.
635 */
636 switch(state) {
637 case 0: // Get the current Method* [sets r0]
638 loadCurrMethodDirect(cUnit, r0);
639 break;
640 case 1: // Get method->dex_cache_resolved_methods_ [usr r0, set rLR]
641 loadWordDisp(cUnit, r0,
642 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
643 break;
644 case 2: // method->dex_cache_resolved_methods_->Get(meth_idx) [u/s rLR]
645 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
646 art::Array::DataOffset().Int32Value(), rLR);
647 break;
648 case 3: // Resolved?
649 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
650 // Slowest path, bail to helper, rollback and retry
651 loadWordDisp(cUnit, rSELF,
652 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
653 loadConstant(cUnit, r1, dInsn->vB);
654 newLIR1(cUnit, kThumbBlxR, rLR);
655 genUnconditionalBranch(cUnit, rollback);
656 // Resume normal slow path
657 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
658 skipTarget->defMask = ENCODE_ALL;
659 skipBranch->generic.target = (LIR*)skipTarget;
660 // Get base_method->method_index [usr rLR, set rLR]
661 loadBaseDisp(cUnit, mir, rLR,
662 Method::GetMethodIndexOffset().Int32Value(), rLR,
663 kUnsignedHalf, INVALID_SREG);
664 // Load "this" [set r1]
665 rlArg = oatGetSrc(cUnit, mir, 0);
666 loadValueDirectFixed(cUnit, rlArg, r1);
667 // Load curMethod->declaring_class_ [uses r0, sets r0]
668 loadWordDisp(cUnit, r0, Method::DeclaringClassOffset().Int32Value(),
669 r0);
670 case 4: // Get method->declaring_class_->super_class [usr r0, set r0]
671 loadWordDisp(cUnit, r0, Class::SuperClassOffset().Int32Value(), r0);
672 break;
673 case 5: // Get ...->super_class_->vtable [u/s r0]
674 loadWordDisp(cUnit, r0, Class::VTableOffset().Int32Value(), r0);
675 // In load shadow fold vtable_ object header size into method_index_
676 opRegImm(cUnit, kOpAdd, rLR,
677 art::Array::DataOffset().Int32Value() / 4);
678 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
679 // Range check, throw NSM on failure
680 tReg = oatAllocTemp(cUnit);
681 loadWordDisp(cUnit, r0, art::Array::LengthOffset().Int32Value(),
682 tReg);
683 genBoundsCheck(cUnit, tReg, rLR, mir->offset, NULL);
684 oatFreeTemp(cUnit, tReg);
685 }
686 // Get target Method*
687 loadBaseIndexed(cUnit, r0, r0, rLR, 2, kWord);
688 break;
689 case 6: // Get the target compiled code address [uses r0, sets rLR]
690 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
691 break;
692 default:
693 return -1;
694 }
buzbee67bf8852011-08-17 17:51:35 -0700695 return state + 1;
696}
697
698/*
699 * Load up to 5 arguments, the first three of which will be in
700 * r1 .. r3. On entry r0 contains the current method pointer,
701 * and as part of the load sequence, it must be replaced with
702 * the target method pointer. Note, this may also be called
703 * for "range" variants if the number of arguments is 5 or fewer.
704 */
705static int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
706 DecodedInstruction* dInsn, int callState,
707 ArmLIR** pcrLabel, bool isRange,
buzbee1da522d2011-09-04 11:22:20 -0700708 NextCallInsn nextCallInsn, ArmLIR* rollback,
709 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700710{
711 RegLocation rlArg;
712 int registerArgs[3];
713
buzbeee9a72f62011-09-04 17:59:07 -0700714skipThis = false;
715
buzbee67bf8852011-08-17 17:51:35 -0700716 /* If no arguments, just return */
717 if (dInsn->vA == 0)
718 return callState;
719
buzbee2e748f32011-08-29 21:02:19 -0700720 oatLockCallTemps(cUnit);
buzbee561227c2011-09-02 15:28:19 -0700721 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700722
723 /*
724 * Load frame arguments arg4 & arg5 first. Coded a little odd to
725 * pre-schedule the method pointer target.
726 */
727 for (unsigned int i=3; i < dInsn->vA; i++) {
728 int reg;
buzbeee9a72f62011-09-04 17:59:07 -0700729 rlArg = oatUpdateLoc(cUnit, oatGetSrc(cUnit, mir, i));
buzbee67bf8852011-08-17 17:51:35 -0700730 if (rlArg.location == kLocPhysReg) {
731 reg = rlArg.lowReg;
732 } else {
733 reg = r1;
734 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee561227c2011-09-02 15:28:19 -0700735 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700736 }
737 storeBaseDisp(cUnit, rSP, (i + 1) * 4, reg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700738 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700739 }
740
741 /* Load register arguments r1..r3 */
buzbeee9a72f62011-09-04 17:59:07 -0700742 for (unsigned int i = 0; i < 3; i++) {
buzbee67bf8852011-08-17 17:51:35 -0700743 if (i < dInsn->vA)
744 registerArgs[i] = (isRange) ? dInsn->vC + i : i;
745 else
746 registerArgs[i] = INVALID_REG;
747 }
buzbeee9a72f62011-09-04 17:59:07 -0700748 if (skipThis) {
749 registerArgs[0] = INVALID_REG;
750 }
buzbee67bf8852011-08-17 17:51:35 -0700751 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
buzbee561227c2011-09-02 15:28:19 -0700752 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700753
754 // Load direct & need a "this" null check?
755 if (pcrLabel) {
756 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1,
757 mir->offset, NULL);
758 }
759 return callState;
760}
761
762/*
763 * May have 0+ arguments (also used for jumbo). Note that
764 * source virtual registers may be in physical registers, so may
765 * need to be flushed to home location before copying. This
766 * applies to arg3 and above (see below).
767 *
768 * Two general strategies:
769 * If < 20 arguments
770 * Pass args 3-18 using vldm/vstm block copy
771 * Pass arg0, arg1 & arg2 in r1-r3
772 * If 20+ arguments
773 * Pass args arg19+ using memcpy block copy
774 * Pass arg0, arg1 & arg2 in r1-r3
775 *
776 */
777static int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
778 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700779 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
buzbee1da522d2011-09-04 11:22:20 -0700780 ArmLIR* rollback, bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700781{
782 int firstArg = dInsn->vC;
783 int numArgs = dInsn->vA;
buzbeee9a72f62011-09-04 17:59:07 -0700784 int registerArgs[3];
785
786skipThis = false;
buzbee67bf8852011-08-17 17:51:35 -0700787
788 // If we can treat it as non-range (Jumbo ops will use range form)
789 if (numArgs <= 5)
790 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700791 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700792 /*
793 * Make sure range list doesn't span the break between in normal
794 * Dalvik vRegs and the ins.
795 */
buzbee1b4c8592011-08-31 10:43:51 -0700796 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700797 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700798 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
799 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700800 }
801
802 /*
803 * First load the non-register arguments. Both forms expect all
804 * of the source arguments to be in their home frame location, so
805 * scan the sReg names and flush any that have been promoted to
806 * frame backing storage.
807 */
808 // Scan the rest of the args - if in physReg flush to memory
809 for (int i = 4; i < numArgs; i++) {
buzbeee9a72f62011-09-04 17:59:07 -0700810 RegLocation loc = oatGetRawSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700811 if (loc.wide) {
812 loc = oatUpdateLocWide(cUnit, loc);
813 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
814 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
815 loc.highReg);
buzbee561227c2011-09-02 15:28:19 -0700816 callState = nextCallInsn(cUnit, mir, dInsn, callState,
817 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700818 }
819 } else {
820 loc = oatUpdateLoc(cUnit, loc);
821 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
822 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700823 callState = nextCallInsn(cUnit, mir, dInsn, callState,
824 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700825 }
buzbee67bf8852011-08-17 17:51:35 -0700826 }
827 }
828
829 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
830 int outsOffset = 4 /* Method* */ + (3 * 4);
831 if (numArgs >= 20) {
832 // Generate memcpy, but first make sure all of
833 opRegRegImm(cUnit, kOpAdd, r0, rSP, startOffset);
834 opRegRegImm(cUnit, kOpAdd, r1, rSP, outsOffset);
835 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
836 loadConstant(cUnit, r2, (numArgs - 3) * 4);
837 newLIR1(cUnit, kThumbBlxR, rLR);
838 } else {
839 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700840 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700841 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700842 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbee1b4c8592011-08-31 10:43:51 -0700843 newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700844 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700845 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700846 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700847 newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700848 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700849 }
850
851 // Handle the 1st 3 in r1, r2 & r3
buzbeee9a72f62011-09-04 17:59:07 -0700852 for (unsigned int i = 0; i < 3; i++) {
853 if (i < dInsn->vA)
854 registerArgs[i] = dInsn->vC + i;
855 else
856 registerArgs[i] = INVALID_REG;
buzbee67bf8852011-08-17 17:51:35 -0700857 }
buzbeee9a72f62011-09-04 17:59:07 -0700858 if (skipThis) {
859 registerArgs[0] = INVALID_REG;
860 }
861 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
862 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700863
864 // Finally, deal with the register arguments
865 // We'll be using fixed registers here
buzbee2e748f32011-08-29 21:02:19 -0700866 oatLockCallTemps(cUnit);
buzbee561227c2011-09-02 15:28:19 -0700867 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700868 return callState;
869}
870
buzbee561227c2011-09-02 15:28:19 -0700871static void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
872 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700873{
874 DecodedInstruction* dInsn = &mir->dalvikInsn;
875 int callState = 0;
876 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700877 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee7b1b86d2011-08-26 18:59:10 -0700878
buzbee561227c2011-09-02 15:28:19 -0700879 NextCallInsn nextCallInsn = nextSDCallInsn;
880
881 if (range) {
882 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700883 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700884 } else {
885 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700886 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700887 }
buzbee67bf8852011-08-17 17:51:35 -0700888 // Finish up any of the call sequence not interleaved in arg loading
889 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700890 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700891 }
892 newLIR1(cUnit, kThumbBlxR, rLR);
893}
894
buzbee4a3164f2011-09-03 11:25:10 -0700895/*
896 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
897 * which will locate the target and continue on via a tail call.
898 */
buzbee67bf8852011-08-17 17:51:35 -0700899static void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
900{
901 DecodedInstruction* dInsn = &mir->dalvikInsn;
902 int callState = 0;
903 ArmLIR* nullCk;
904 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -0700905 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700906 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
907 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700908 false, nextInterfaceCallInsn, NULL,
909 true);
buzbee67bf8852011-08-17 17:51:35 -0700910 else
911 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700912 nextInterfaceCallInsn, NULL, true);
buzbee67bf8852011-08-17 17:51:35 -0700913 // Finish up any of the call sequence not interleaved in arg loading
914 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700915 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700916 }
917 newLIR1(cUnit, kThumbBlxR, rLR);
918}
919
920static void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
921{
922 DecodedInstruction* dInsn = &mir->dalvikInsn;
923 int callState = 0;
924 ArmLIR* nullCk;
buzbee4a3164f2011-09-03 11:25:10 -0700925 ArmLIR* rollback;
926 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
927 Get(dInsn->vB);
928 NextCallInsn nextCallInsn;
929 bool fastPath = true;
930 if (baseMethod == NULL) {
931 fastPath = false;
932 } else {
933 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
934 if (superClass == NULL) {
935 fastPath = false;
936 } else {
937 int32_t target_idx = baseMethod->GetMethodIndex();
938 if (superClass->GetVTable()->GetLength() <= target_idx) {
939 fastPath = false;
940 } else {
941 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
942 }
943 }
944 }
945 if (fastPath) {
946 nextCallInsn = nextSuperCallInsn;
947 rollback = NULL;
948 } else {
949 nextCallInsn = nextSuperCallInsnSP;
950 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
951 rollback->defMask = -1;
952 }
buzbee67bf8852011-08-17 17:51:35 -0700953 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
954 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700955 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700956 else
957 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700958 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700959 // Finish up any of the call sequence not interleaved in arg loading
960 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700961 callState = nextSuperCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700962 }
963 newLIR1(cUnit, kThumbBlxR, rLR);
964}
965
966static void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
967{
968 DecodedInstruction* dInsn = &mir->dalvikInsn;
969 int callState = 0;
970 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700971 ArmLIR* rollback;
972 Method* method = cUnit->method->GetDexCacheResolvedMethods()->
973 Get(dInsn->vB);
974 NextCallInsn nextCallInsn;
buzbee7b1b86d2011-08-26 18:59:10 -0700975
buzbeee9a72f62011-09-04 17:59:07 -0700976 if (FORCE_SLOW || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -0700977 // Slow path
978 nextCallInsn = nextVCallInsnSP;
979 // If we need a slow-path callout, we'll restart here
980 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
981 rollback->defMask = -1;
982 } else {
983 // Fast path
984 nextCallInsn = nextVCallInsn;
985 rollback = NULL;
986 }
buzbee67bf8852011-08-17 17:51:35 -0700987 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
988 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700989 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700990 else
991 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700992 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700993 // Finish up any of the call sequence not interleaved in arg loading
994 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700995 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700996 }
997 newLIR1(cUnit, kThumbBlxR, rLR);
998}
999
buzbee67bf8852011-08-17 17:51:35 -07001000static bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
1001 BasicBlock* bb, ArmLIR* labelList)
1002{
1003 bool res = false; // Assume success
1004 RegLocation rlSrc[3];
1005 RegLocation rlDest = badLoc;
1006 RegLocation rlResult = badLoc;
1007 Opcode opcode = mir->dalvikInsn.opcode;
1008
1009 /* Prep Src and Dest locations */
1010 int nextSreg = 0;
1011 int nextLoc = 0;
1012 int attrs = oatDataFlowAttributes[opcode];
1013 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1014 if (attrs & DF_UA) {
1015 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1016 nextSreg++;
1017 } else if (attrs & DF_UA_WIDE) {
1018 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1019 nextSreg + 1);
1020 nextSreg+= 2;
1021 }
1022 if (attrs & DF_UB) {
1023 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1024 nextSreg++;
1025 } else if (attrs & DF_UB_WIDE) {
1026 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1027 nextSreg + 1);
1028 nextSreg+= 2;
1029 }
1030 if (attrs & DF_UC) {
1031 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1032 } else if (attrs & DF_UC_WIDE) {
1033 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1034 nextSreg + 1);
1035 }
1036 if (attrs & DF_DA) {
1037 rlDest = oatGetDest(cUnit, mir, 0);
1038 } else if (attrs & DF_DA_WIDE) {
1039 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1040 }
1041
1042 switch(opcode) {
1043 case OP_NOP:
1044 break;
1045
1046 case OP_MOVE_EXCEPTION:
1047 int exOffset;
1048 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001049 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001050 resetReg = oatAllocTemp(cUnit);
1051 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1052 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1053 loadConstant(cUnit, resetReg, 0);
1054 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1055 storeValue(cUnit, rlDest, rlResult);
1056 break;
1057
1058 case OP_RETURN_VOID:
1059 break;
1060
1061 case OP_RETURN:
1062 case OP_RETURN_OBJECT:
1063 storeValue(cUnit, retLoc, rlSrc[0]);
1064 break;
1065
1066 case OP_RETURN_WIDE:
1067 rlDest = retLocWide;
1068 rlDest.fp = rlSrc[0].fp;
1069 storeValueWide(cUnit, rlDest, rlSrc[0]);
1070 break;
1071
1072 case OP_MOVE_RESULT_WIDE:
1073 if (mir->OptimizationFlags & MIR_INLINED)
1074 break; // Nop - combined w/ previous invoke
1075 /*
1076 * Somewhat hacky here. Because we're now passing
1077 * return values in registers, we have to let the
1078 * register allocation utilities know that the return
1079 * registers are live and may not be used for address
1080 * formation in storeValueWide.
1081 */
1082 assert(retLocWide.lowReg == r0);
buzbee1da522d2011-09-04 11:22:20 -07001083 assert(retLocWide.highReg == r1);
buzbee67bf8852011-08-17 17:51:35 -07001084 oatLockTemp(cUnit, retLocWide.lowReg);
1085 oatLockTemp(cUnit, retLocWide.highReg);
1086 storeValueWide(cUnit, rlDest, retLocWide);
1087 oatFreeTemp(cUnit, retLocWide.lowReg);
1088 oatFreeTemp(cUnit, retLocWide.highReg);
1089 break;
1090
1091 case OP_MOVE_RESULT:
1092 case OP_MOVE_RESULT_OBJECT:
1093 if (mir->OptimizationFlags & MIR_INLINED)
1094 break; // Nop - combined w/ previous invoke
1095 /* See comment for OP_MOVE_RESULT_WIDE */
1096 assert(retLoc.lowReg == r0);
1097 oatLockTemp(cUnit, retLoc.lowReg);
1098 storeValue(cUnit, rlDest, retLoc);
1099 oatFreeTemp(cUnit, retLoc.lowReg);
1100 break;
1101
1102 case OP_MOVE:
1103 case OP_MOVE_OBJECT:
1104 case OP_MOVE_16:
1105 case OP_MOVE_OBJECT_16:
1106 case OP_MOVE_FROM16:
1107 case OP_MOVE_OBJECT_FROM16:
1108 storeValue(cUnit, rlDest, rlSrc[0]);
1109 break;
1110
1111 case OP_MOVE_WIDE:
1112 case OP_MOVE_WIDE_16:
1113 case OP_MOVE_WIDE_FROM16:
1114 storeValueWide(cUnit, rlDest, rlSrc[0]);
1115 break;
1116
1117 case OP_CONST:
1118 case OP_CONST_4:
1119 case OP_CONST_16:
1120 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1121 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1122 storeValue(cUnit, rlDest, rlResult);
1123 break;
1124
1125 case OP_CONST_HIGH16:
1126 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1127 loadConstantNoClobber(cUnit, rlResult.lowReg,
1128 mir->dalvikInsn.vB << 16);
1129 storeValue(cUnit, rlDest, rlResult);
1130 break;
1131
1132 case OP_CONST_WIDE_16:
1133 case OP_CONST_WIDE_32:
1134 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1135 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1136 //TUNING: do high separately to avoid load dependency
1137 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1138 storeValueWide(cUnit, rlDest, rlResult);
1139 break;
1140
1141 case OP_CONST_WIDE:
1142 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1143 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001144 mir->dalvikInsn.vB_wide & 0xffffffff,
1145 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001146 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001147 break;
1148
1149 case OP_CONST_WIDE_HIGH16:
1150 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1151 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1152 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001153 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001154 break;
1155
1156 case OP_MONITOR_ENTER:
1157 genMonitorEnter(cUnit, mir, rlSrc[0]);
1158 break;
1159
1160 case OP_MONITOR_EXIT:
1161 genMonitorExit(cUnit, mir, rlSrc[0]);
1162 break;
1163
1164 case OP_CHECK_CAST:
1165 genCheckCast(cUnit, mir, rlSrc[0]);
1166 break;
1167
1168 case OP_INSTANCE_OF:
1169 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1170 break;
1171
1172 case OP_NEW_INSTANCE:
1173 genNewInstance(cUnit, mir, rlDest);
1174 break;
1175
1176 case OP_THROW:
1177 genThrow(cUnit, mir, rlSrc[0]);
1178 break;
1179
1180 case OP_ARRAY_LENGTH:
1181 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001182 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001183 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee67bf8852011-08-17 17:51:35 -07001184 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg,
1185 mir->offset, NULL);
1186 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1187 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1188 rlResult.lowReg);
1189 storeValue(cUnit, rlDest, rlResult);
1190 break;
1191
1192 case OP_CONST_STRING:
1193 case OP_CONST_STRING_JUMBO:
1194 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1195 break;
1196
1197 case OP_CONST_CLASS:
1198 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1199 break;
1200
1201 case OP_FILL_ARRAY_DATA:
1202 genFillArrayData(cUnit, mir, rlSrc[0]);
1203 break;
1204
1205 case OP_FILLED_NEW_ARRAY:
1206 genFilledNewArray(cUnit, mir, false /* not range */);
1207 break;
1208
1209 case OP_FILLED_NEW_ARRAY_RANGE:
1210 genFilledNewArray(cUnit, mir, true /* range */);
1211 break;
1212
1213 case OP_NEW_ARRAY:
1214 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1215 break;
1216
1217 case OP_GOTO:
1218 case OP_GOTO_16:
1219 case OP_GOTO_32:
1220 // TUNING: add MIR flag to disable when unnecessary
1221 bool backwardBranch;
1222 backwardBranch = (bb->taken->startOffset <= mir->offset);
1223 if (backwardBranch) {
1224 genSuspendPoll(cUnit, mir);
1225 }
1226 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1227 break;
1228
1229 case OP_PACKED_SWITCH:
1230 genPackedSwitch(cUnit, mir, rlSrc[0]);
1231 break;
1232
1233 case OP_SPARSE_SWITCH:
1234 genSparseSwitch(cUnit, mir, rlSrc[0]);
1235 break;
1236
1237 case OP_CMPL_FLOAT:
1238 case OP_CMPG_FLOAT:
1239 case OP_CMPL_DOUBLE:
1240 case OP_CMPG_DOUBLE:
1241 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1242 break;
1243
1244 case OP_CMP_LONG:
1245 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1246 break;
1247
1248 case OP_IF_EQ:
1249 case OP_IF_NE:
1250 case OP_IF_LT:
1251 case OP_IF_GE:
1252 case OP_IF_GT:
1253 case OP_IF_LE: {
1254 bool backwardBranch;
1255 ArmConditionCode cond;
1256 backwardBranch = (bb->taken->startOffset <= mir->offset);
1257 if (backwardBranch) {
1258 genSuspendPoll(cUnit, mir);
1259 }
1260 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1261 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1262 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1263 switch(opcode) {
1264 case OP_IF_EQ:
1265 cond = kArmCondEq;
1266 break;
1267 case OP_IF_NE:
1268 cond = kArmCondNe;
1269 break;
1270 case OP_IF_LT:
1271 cond = kArmCondLt;
1272 break;
1273 case OP_IF_GE:
1274 cond = kArmCondGe;
1275 break;
1276 case OP_IF_GT:
1277 cond = kArmCondGt;
1278 break;
1279 case OP_IF_LE:
1280 cond = kArmCondLe;
1281 break;
1282 default:
1283 cond = (ArmConditionCode)0;
1284 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1285 }
1286 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1287 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1288 break;
1289 }
1290
1291 case OP_IF_EQZ:
1292 case OP_IF_NEZ:
1293 case OP_IF_LTZ:
1294 case OP_IF_GEZ:
1295 case OP_IF_GTZ:
1296 case OP_IF_LEZ: {
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 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1305 switch(opcode) {
1306 case OP_IF_EQZ:
1307 cond = kArmCondEq;
1308 break;
1309 case OP_IF_NEZ:
1310 cond = kArmCondNe;
1311 break;
1312 case OP_IF_LTZ:
1313 cond = kArmCondLt;
1314 break;
1315 case OP_IF_GEZ:
1316 cond = kArmCondGe;
1317 break;
1318 case OP_IF_GTZ:
1319 cond = kArmCondGt;
1320 break;
1321 case OP_IF_LEZ:
1322 cond = kArmCondLe;
1323 break;
1324 default:
1325 cond = (ArmConditionCode)0;
1326 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1327 }
1328 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1329 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1330 break;
1331 }
1332
1333 case OP_AGET_WIDE:
1334 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1335 break;
1336 case OP_AGET:
1337 case OP_AGET_OBJECT:
1338 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1339 break;
1340 case OP_AGET_BOOLEAN:
1341 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1342 rlDest, 0);
1343 break;
1344 case OP_AGET_BYTE:
1345 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1346 break;
1347 case OP_AGET_CHAR:
1348 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1349 rlDest, 1);
1350 break;
1351 case OP_AGET_SHORT:
1352 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1353 break;
1354 case OP_APUT_WIDE:
1355 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1356 break;
1357 case OP_APUT:
1358 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1359 break;
1360 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001361 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001362 break;
1363 case OP_APUT_SHORT:
1364 case OP_APUT_CHAR:
1365 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1366 rlSrc[0], 1);
1367 break;
1368 case OP_APUT_BYTE:
1369 case OP_APUT_BOOLEAN:
1370 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1371 rlSrc[0], 0);
1372 break;
1373
1374 case OP_IGET_WIDE:
1375 case OP_IGET_WIDE_VOLATILE:
1376 genIGetWideX(cUnit, mir, rlDest, rlSrc[0]);
1377 break;
1378
1379 case OP_IGET:
1380 case OP_IGET_VOLATILE:
1381 case OP_IGET_OBJECT:
1382 case OP_IGET_OBJECT_VOLATILE:
1383 genIGetX(cUnit, mir, kWord, rlDest, rlSrc[0]);
1384 break;
1385
1386 case OP_IGET_BOOLEAN:
1387 case OP_IGET_BYTE:
1388 genIGetX(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
1389 break;
1390
1391 case OP_IGET_CHAR:
1392 genIGetX(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
1393 break;
1394
1395 case OP_IGET_SHORT:
1396 genIGetX(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
1397 break;
1398
1399 case OP_IPUT_WIDE:
1400 case OP_IPUT_WIDE_VOLATILE:
1401 genIPutWideX(cUnit, mir, rlSrc[0], rlSrc[1]);
1402 break;
1403
1404 case OP_IPUT_OBJECT:
1405 case OP_IPUT_OBJECT_VOLATILE:
1406 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
1407 break;
1408
1409 case OP_IPUT:
1410 case OP_IPUT_VOLATILE:
1411 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
1412 break;
1413
1414 case OP_IPUT_BOOLEAN:
1415 case OP_IPUT_BYTE:
1416 genIPutX(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
1417 break;
1418
1419 case OP_IPUT_CHAR:
1420 genIPutX(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
1421 break;
1422
1423 case OP_IPUT_SHORT:
1424 genIPutX(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
1425 break;
1426
1427 case OP_SGET:
1428 case OP_SGET_OBJECT:
1429 case OP_SGET_BOOLEAN:
1430 case OP_SGET_BYTE:
1431 case OP_SGET_CHAR:
1432 case OP_SGET_SHORT:
1433 genSget(cUnit, mir, rlResult, rlDest);
1434 break;
1435
1436 case OP_SGET_WIDE:
1437 genSgetWide(cUnit, mir, rlResult, rlDest);
1438 break;
1439
1440 case OP_SPUT:
1441 case OP_SPUT_OBJECT:
1442 case OP_SPUT_BOOLEAN:
1443 case OP_SPUT_BYTE:
1444 case OP_SPUT_CHAR:
1445 case OP_SPUT_SHORT:
1446 genSput(cUnit, mir, rlSrc[0]);
1447 break;
1448
1449 case OP_SPUT_WIDE:
1450 genSputWide(cUnit, mir, rlSrc[0]);
1451 break;
1452
1453 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001454 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1455 true /*range*/);
1456 break;
buzbee67bf8852011-08-17 17:51:35 -07001457 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001458 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1459 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001460 break;
1461
1462 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001463 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1464 false /*range*/);
1465 break;
buzbee67bf8852011-08-17 17:51:35 -07001466 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001467 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1468 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001469 break;
1470
1471 case OP_INVOKE_VIRTUAL:
1472 case OP_INVOKE_VIRTUAL_RANGE:
1473 genInvokeVirtual(cUnit, mir);
1474 break;
1475
1476 case OP_INVOKE_SUPER:
1477 case OP_INVOKE_SUPER_RANGE:
1478 genInvokeSuper(cUnit, mir);
1479 break;
1480
1481 case OP_INVOKE_INTERFACE:
1482 case OP_INVOKE_INTERFACE_RANGE:
1483 genInvokeInterface(cUnit, mir);
1484 break;
1485
1486 case OP_NEG_INT:
1487 case OP_NOT_INT:
1488 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1489 break;
1490
1491 case OP_NEG_LONG:
1492 case OP_NOT_LONG:
1493 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1494 break;
1495
1496 case OP_NEG_FLOAT:
1497 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1498 break;
1499
1500 case OP_NEG_DOUBLE:
1501 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1502 break;
1503
1504 case OP_INT_TO_LONG:
1505 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1506 if (rlSrc[0].location == kLocPhysReg) {
1507 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1508 } else {
1509 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1510 }
1511 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1512 rlResult.lowReg, 31);
1513 storeValueWide(cUnit, rlDest, rlResult);
1514 break;
1515
1516 case OP_LONG_TO_INT:
1517 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1518 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1519 storeValue(cUnit, rlDest, rlSrc[0]);
1520 break;
1521
1522 case OP_INT_TO_BYTE:
1523 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1524 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1525 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1526 storeValue(cUnit, rlDest, rlResult);
1527 break;
1528
1529 case OP_INT_TO_SHORT:
1530 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1531 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1532 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1533 storeValue(cUnit, rlDest, rlResult);
1534 break;
1535
1536 case OP_INT_TO_CHAR:
1537 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1538 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1539 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1540 storeValue(cUnit, rlDest, rlResult);
1541 break;
1542
1543 case OP_INT_TO_FLOAT:
1544 case OP_INT_TO_DOUBLE:
1545 case OP_LONG_TO_FLOAT:
1546 case OP_LONG_TO_DOUBLE:
1547 case OP_FLOAT_TO_INT:
1548 case OP_FLOAT_TO_LONG:
1549 case OP_FLOAT_TO_DOUBLE:
1550 case OP_DOUBLE_TO_INT:
1551 case OP_DOUBLE_TO_LONG:
1552 case OP_DOUBLE_TO_FLOAT:
1553 genConversion(cUnit, mir);
1554 break;
1555
1556 case OP_ADD_INT:
1557 case OP_SUB_INT:
1558 case OP_MUL_INT:
1559 case OP_DIV_INT:
1560 case OP_REM_INT:
1561 case OP_AND_INT:
1562 case OP_OR_INT:
1563 case OP_XOR_INT:
1564 case OP_SHL_INT:
1565 case OP_SHR_INT:
1566 case OP_USHR_INT:
1567 case OP_ADD_INT_2ADDR:
1568 case OP_SUB_INT_2ADDR:
1569 case OP_MUL_INT_2ADDR:
1570 case OP_DIV_INT_2ADDR:
1571 case OP_REM_INT_2ADDR:
1572 case OP_AND_INT_2ADDR:
1573 case OP_OR_INT_2ADDR:
1574 case OP_XOR_INT_2ADDR:
1575 case OP_SHL_INT_2ADDR:
1576 case OP_SHR_INT_2ADDR:
1577 case OP_USHR_INT_2ADDR:
1578 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1579 break;
1580
1581 case OP_ADD_LONG:
1582 case OP_SUB_LONG:
1583 case OP_MUL_LONG:
1584 case OP_DIV_LONG:
1585 case OP_REM_LONG:
1586 case OP_AND_LONG:
1587 case OP_OR_LONG:
1588 case OP_XOR_LONG:
1589 case OP_ADD_LONG_2ADDR:
1590 case OP_SUB_LONG_2ADDR:
1591 case OP_MUL_LONG_2ADDR:
1592 case OP_DIV_LONG_2ADDR:
1593 case OP_REM_LONG_2ADDR:
1594 case OP_AND_LONG_2ADDR:
1595 case OP_OR_LONG_2ADDR:
1596 case OP_XOR_LONG_2ADDR:
1597 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1598 break;
1599
buzbee67bf8852011-08-17 17:51:35 -07001600 case OP_SHL_LONG:
1601 case OP_SHR_LONG:
1602 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001603 case OP_SHL_LONG_2ADDR:
1604 case OP_SHR_LONG_2ADDR:
1605 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001606 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1607 break;
1608
1609 case OP_ADD_FLOAT:
1610 case OP_SUB_FLOAT:
1611 case OP_MUL_FLOAT:
1612 case OP_DIV_FLOAT:
1613 case OP_REM_FLOAT:
1614 case OP_ADD_FLOAT_2ADDR:
1615 case OP_SUB_FLOAT_2ADDR:
1616 case OP_MUL_FLOAT_2ADDR:
1617 case OP_DIV_FLOAT_2ADDR:
1618 case OP_REM_FLOAT_2ADDR:
1619 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1620 break;
1621
1622 case OP_ADD_DOUBLE:
1623 case OP_SUB_DOUBLE:
1624 case OP_MUL_DOUBLE:
1625 case OP_DIV_DOUBLE:
1626 case OP_REM_DOUBLE:
1627 case OP_ADD_DOUBLE_2ADDR:
1628 case OP_SUB_DOUBLE_2ADDR:
1629 case OP_MUL_DOUBLE_2ADDR:
1630 case OP_DIV_DOUBLE_2ADDR:
1631 case OP_REM_DOUBLE_2ADDR:
1632 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1633 break;
1634
1635 case OP_RSUB_INT:
1636 case OP_ADD_INT_LIT16:
1637 case OP_MUL_INT_LIT16:
1638 case OP_DIV_INT_LIT16:
1639 case OP_REM_INT_LIT16:
1640 case OP_AND_INT_LIT16:
1641 case OP_OR_INT_LIT16:
1642 case OP_XOR_INT_LIT16:
1643 case OP_ADD_INT_LIT8:
1644 case OP_RSUB_INT_LIT8:
1645 case OP_MUL_INT_LIT8:
1646 case OP_DIV_INT_LIT8:
1647 case OP_REM_INT_LIT8:
1648 case OP_AND_INT_LIT8:
1649 case OP_OR_INT_LIT8:
1650 case OP_XOR_INT_LIT8:
1651 case OP_SHL_INT_LIT8:
1652 case OP_SHR_INT_LIT8:
1653 case OP_USHR_INT_LIT8:
1654 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1655 break;
1656
1657 default:
1658 res = true;
1659 }
1660 return res;
1661}
1662
1663static const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
1664 "kMirOpPhi",
1665 "kMirOpNullNRangeUpCheck",
1666 "kMirOpNullNRangeDownCheck",
1667 "kMirOpLowerBound",
1668 "kMirOpPunt",
1669 "kMirOpCheckInlinePrediction",
1670};
1671
1672/* Extended MIR instructions like PHI */
1673static void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
1674{
1675 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1676 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1677 strcpy(msg, extendedMIROpNames[opOffset]);
1678 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1679
1680 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1681 case kMirOpPhi: {
1682 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1683 op->flags.isNop = true;
1684 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1685 break;
1686 }
1687 default:
1688 break;
1689 }
1690}
1691
1692/* If there are any ins passed in registers that have not been promoted
1693 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001694 * Note: at this pointCopy any ins that are passed in register to their
1695 * home location */
buzbee67bf8852011-08-17 17:51:35 -07001696static void flushIns(CompilationUnit* cUnit)
1697{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001698 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001699 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001700 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1701 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001702 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001703 int startLoc = cUnit->method->NumRegisters() -
1704 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001705 for (int i = 0; i < inRegs; i++) {
1706 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001707 //TUNING: be smarter about flushing ins to frame
1708 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001709 if (loc.location == kLocPhysReg) {
1710 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001711 }
1712 }
1713
1714 // Handle special case of wide argument half in regs, half in frame
1715 if (inRegs == 3) {
1716 RegLocation loc = cUnit->regLocation[startLoc + 2];
1717 if (loc.wide && loc.location == kLocPhysReg) {
1718 // Load the other half of the arg into the promoted pair
buzbee561227c2011-09-02 15:28:19 -07001719 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -07001720 inRegs++;
1721 }
1722 }
1723
1724 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001725 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001726 RegLocation loc = cUnit->regLocation[startLoc + i];
1727 if (loc.fpLocation == kLocPhysReg) {
1728 loc.location = kLocPhysReg;
1729 loc.fp = true;
1730 loc.lowReg = loc.fpLowReg;
1731 loc.highReg = loc.fpHighReg;
1732 }
1733 if (loc.location == kLocPhysReg) {
1734 if (loc.wide) {
1735 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1736 loc.lowReg, loc.highReg, INVALID_SREG);
1737 i++;
1738 } else {
buzbee561227c2011-09-02 15:28:19 -07001739 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001740 }
1741 }
1742 i++;
1743 }
1744}
1745
1746/* Handle the content in each basic block */
1747static bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
1748{
1749 MIR* mir;
1750 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1751 int blockId = bb->id;
1752
1753 cUnit->curBlock = bb;
1754 labelList[blockId].operands[0] = bb->startOffset;
1755
1756 /* Insert the block label */
1757 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1758 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1759
1760 oatClobberAllRegs(cUnit);
1761 oatResetNullCheck(cUnit);
1762
1763 ArmLIR* headLIR = NULL;
1764
1765 if (bb->blockType == kEntryBlock) {
1766 /*
1767 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1768 * mechanism know so it doesn't try to use any of them when
1769 * expanding the frame or flushing. This leaves the utility
1770 * code with a single temp: r12. This should be enough.
1771 */
1772 oatLockTemp(cUnit, r0);
1773 oatLockTemp(cUnit, r1);
1774 oatLockTemp(cUnit, r2);
1775 oatLockTemp(cUnit, r3);
1776 newLIR0(cUnit, kArmPseudoMethodEntry);
1777 /* Spill core callee saves */
1778 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1779 /* Need to spill any FP regs? */
1780 if (cUnit->numFPSpills) {
1781 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1782 }
1783 opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1784 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1785 flushIns(cUnit);
1786 oatFreeTemp(cUnit, r0);
1787 oatFreeTemp(cUnit, r1);
1788 oatFreeTemp(cUnit, r2);
1789 oatFreeTemp(cUnit, r3);
1790 } else if (bb->blockType == kExitBlock) {
1791 newLIR0(cUnit, kArmPseudoMethodExit);
1792 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1793 /* Need to restore any FP callee saves? */
1794 if (cUnit->numFPSpills) {
1795 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1796 }
1797 if (cUnit->coreSpillMask & (1 << rLR)) {
1798 /* Unspill rLR to rPC */
1799 cUnit->coreSpillMask &= ~(1 << rLR);
1800 cUnit->coreSpillMask |= (1 << rPC);
1801 }
1802 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1803 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1804 /* We didn't pop to rPC, so must do a bv rLR */
1805 newLIR1(cUnit, kThumbBx, rLR);
1806 }
1807 }
1808
1809 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1810
1811 oatResetRegPool(cUnit);
1812 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1813 oatClobberAllRegs(cUnit);
1814 }
1815
1816 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1817 oatResetDefTracking(cUnit);
1818 }
1819
1820 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1821 handleExtendedMethodMIR(cUnit, mir);
1822 continue;
1823 }
1824
1825 cUnit->currentDalvikOffset = mir->offset;
1826
1827 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1828 InstructionFormat dalvikFormat =
1829 dexGetFormatFromOpcode(dalvikOpcode);
1830
1831 ArmLIR* boundaryLIR;
1832
1833 /* Mark the beginning of a Dalvik instruction for line tracking */
1834 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1835 (int) oatGetDalvikDisassembly(
1836 &mir->dalvikInsn, ""));
1837 /* Remember the first LIR for this block */
1838 if (headLIR == NULL) {
1839 headLIR = boundaryLIR;
1840 /* Set the first boundaryLIR as a scheduling barrier */
1841 headLIR->defMask = ENCODE_ALL;
1842 }
1843
1844 /* Don't generate the SSA annotation unless verbose mode is on */
1845 if (cUnit->printMe && mir->ssaRep) {
1846 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1847 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1848 }
1849
1850 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1851
1852 if (notHandled) {
1853 char buf[100];
1854 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1855 mir->offset,
1856 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1857 dalvikFormat);
1858 LOG(FATAL) << buf;
1859 }
1860 }
1861
1862 if (headLIR) {
1863 /*
1864 * Eliminate redundant loads/stores and delay stores into later
1865 * slots
1866 */
1867 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1868 cUnit->lastLIRInsn);
1869
1870 /*
1871 * Generate an unconditional branch to the fallthrough block.
1872 */
1873 if (bb->fallThrough) {
1874 genUnconditionalBranch(cUnit,
1875 &labelList[bb->fallThrough->id]);
1876 }
1877 }
1878 return false;
1879}
1880
1881/*
1882 * Nop any unconditional branches that go to the next instruction.
1883 * Note: new redundant branches may be inserted later, and we'll
1884 * use a check in final instruction assembly to nop those out.
1885 */
1886void removeRedundantBranches(CompilationUnit* cUnit)
1887{
1888 ArmLIR* thisLIR;
1889
1890 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1891 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1892 thisLIR = NEXT_LIR(thisLIR)) {
1893
1894 /* Branch to the next instruction */
1895 if ((thisLIR->opcode == kThumbBUncond) ||
1896 (thisLIR->opcode == kThumb2BUncond)) {
1897 ArmLIR* nextLIR = thisLIR;
1898
1899 while (true) {
1900 nextLIR = NEXT_LIR(nextLIR);
1901
1902 /*
1903 * Is the branch target the next instruction?
1904 */
1905 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1906 thisLIR->flags.isNop = true;
1907 break;
1908 }
1909
1910 /*
1911 * Found real useful stuff between the branch and the target.
1912 * Need to explicitly check the lastLIRInsn here because it
1913 * might be the last real instruction.
1914 */
1915 if (!isPseudoOpcode(nextLIR->opcode) ||
1916 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1917 break;
1918 }
1919 }
1920 }
1921}
1922
1923void oatMethodMIR2LIR(CompilationUnit* cUnit)
1924{
1925 /* Used to hold the labels of each block */
1926 cUnit->blockLabelList =
1927 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
1928
1929 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
1930 kPreOrderDFSTraversal, false /* Iterative */);
1931 removeRedundantBranches(cUnit);
1932}
1933
1934/* Common initialization routine for an architecture family */
1935bool oatArchInit()
1936{
1937 int i;
1938
1939 for (i = 0; i < kArmLast; i++) {
1940 if (EncodingMap[i].opcode != i) {
1941 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
1942 " is wrong: expecting " << i << ", seeing " <<
1943 (int)EncodingMap[i].opcode;
1944 }
1945 }
1946
1947 return oatArchVariantInit();
1948}
1949
1950/* Needed by the Assembler */
1951void oatSetupResourceMasks(ArmLIR* lir)
1952{
1953 setupResourceMasks(lir);
1954}
1955
1956/* Needed by the ld/st optmizatons */
1957ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
1958{
1959 return genRegCopyNoInsert(cUnit, rDest, rSrc);
1960}
1961
1962/* Needed by the register allocator */
1963ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
1964{
1965 return genRegCopy(cUnit, rDest, rSrc);
1966}
1967
1968/* Needed by the register allocator */
1969void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
1970 int srcLo, int srcHi)
1971{
1972 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
1973}
1974
1975void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
1976 int displacement, int rSrc, OpSize size)
1977{
1978 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
1979}
1980
1981void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
1982 int displacement, int rSrcLo, int rSrcHi)
1983{
1984 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
1985}