blob: 78a97675eb7a7723565112f535822d728a16c575 [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);
buzbee6a0f7f52011-09-05 16:14:20 -0700670 // Null this?
671 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir->offset, NULL);
672 // Get method->declaring_class_->super_class [usr r0, set r0]
buzbee4a3164f2011-09-03 11:25:10 -0700673 loadWordDisp(cUnit, r0, Class::SuperClassOffset().Int32Value(), r0);
674 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700675 case 4: // Get ...->super_class_->vtable [u/s r0]
buzbee4a3164f2011-09-03 11:25:10 -0700676 loadWordDisp(cUnit, r0, Class::VTableOffset().Int32Value(), r0);
buzbee4a3164f2011-09-03 11:25:10 -0700677 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
678 // Range check, throw NSM on failure
679 tReg = oatAllocTemp(cUnit);
680 loadWordDisp(cUnit, r0, art::Array::LengthOffset().Int32Value(),
681 tReg);
682 genBoundsCheck(cUnit, tReg, rLR, mir->offset, NULL);
683 oatFreeTemp(cUnit, tReg);
684 }
buzbee6a0f7f52011-09-05 16:14:20 -0700685 // Adjust vtable_ base past object header
686 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
buzbee4a3164f2011-09-03 11:25:10 -0700687 // Get target Method*
buzbee6a0f7f52011-09-05 16:14:20 -0700688 loadBaseIndexed(cUnit, r0, rLR, r0, 2, kWord);
buzbee4a3164f2011-09-03 11:25:10 -0700689 break;
buzbee6a0f7f52011-09-05 16:14:20 -0700690 case 5: // Get the target compiled code address [uses r0, sets rLR]
buzbee4a3164f2011-09-03 11:25:10 -0700691 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
692 break;
693 default:
694 return -1;
695 }
buzbee67bf8852011-08-17 17:51:35 -0700696 return state + 1;
697}
698
699/*
700 * Load up to 5 arguments, the first three of which will be in
701 * r1 .. r3. On entry r0 contains the current method pointer,
702 * and as part of the load sequence, it must be replaced with
703 * the target method pointer. Note, this may also be called
704 * for "range" variants if the number of arguments is 5 or fewer.
705 */
706static int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
707 DecodedInstruction* dInsn, int callState,
708 ArmLIR** pcrLabel, bool isRange,
buzbee1da522d2011-09-04 11:22:20 -0700709 NextCallInsn nextCallInsn, ArmLIR* rollback,
710 bool skipThis)
buzbee67bf8852011-08-17 17:51:35 -0700711{
712 RegLocation rlArg;
713 int registerArgs[3];
714
715 /* If no arguments, just return */
716 if (dInsn->vA == 0)
717 return callState;
718
buzbee2e748f32011-08-29 21:02:19 -0700719 oatLockCallTemps(cUnit);
buzbee561227c2011-09-02 15:28:19 -0700720 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700721
722 /*
723 * Load frame arguments arg4 & arg5 first. Coded a little odd to
724 * pre-schedule the method pointer target.
725 */
726 for (unsigned int i=3; i < dInsn->vA; i++) {
727 int reg;
buzbeee9a72f62011-09-04 17:59:07 -0700728 rlArg = oatUpdateLoc(cUnit, oatGetSrc(cUnit, mir, i));
buzbee67bf8852011-08-17 17:51:35 -0700729 if (rlArg.location == kLocPhysReg) {
730 reg = rlArg.lowReg;
731 } else {
732 reg = r1;
733 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee561227c2011-09-02 15:28:19 -0700734 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700735 }
736 storeBaseDisp(cUnit, rSP, (i + 1) * 4, reg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700737 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700738 }
739
740 /* Load register arguments r1..r3 */
buzbeee9a72f62011-09-04 17:59:07 -0700741 for (unsigned int i = 0; i < 3; i++) {
buzbee67bf8852011-08-17 17:51:35 -0700742 if (i < dInsn->vA)
743 registerArgs[i] = (isRange) ? dInsn->vC + i : i;
744 else
745 registerArgs[i] = INVALID_REG;
746 }
buzbeee9a72f62011-09-04 17:59:07 -0700747 if (skipThis) {
748 registerArgs[0] = INVALID_REG;
749 }
buzbee67bf8852011-08-17 17:51:35 -0700750 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
buzbee561227c2011-09-02 15:28:19 -0700751 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700752
buzbee6a0f7f52011-09-05 16:14:20 -0700753 //TODO: better to move this into CallInsn lists
buzbee67bf8852011-08-17 17:51:35 -0700754 // 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
buzbee67bf8852011-08-17 17:51:35 -0700786 // If we can treat it as non-range (Jumbo ops will use range form)
787 if (numArgs <= 5)
788 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee1da522d2011-09-04 11:22:20 -0700789 true, nextCallInsn, rollback, skipThis);
buzbee67bf8852011-08-17 17:51:35 -0700790 /*
791 * Make sure range list doesn't span the break between in normal
792 * Dalvik vRegs and the ins.
793 */
buzbee1b4c8592011-08-31 10:43:51 -0700794 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700795 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700796 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
797 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700798 }
799
800 /*
801 * First load the non-register arguments. Both forms expect all
802 * of the source arguments to be in their home frame location, so
803 * scan the sReg names and flush any that have been promoted to
804 * frame backing storage.
805 */
806 // Scan the rest of the args - if in physReg flush to memory
807 for (int i = 4; i < numArgs; i++) {
buzbeee9a72f62011-09-04 17:59:07 -0700808 RegLocation loc = oatGetRawSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700809 if (loc.wide) {
810 loc = oatUpdateLocWide(cUnit, loc);
811 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
812 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
813 loc.highReg);
buzbee561227c2011-09-02 15:28:19 -0700814 callState = nextCallInsn(cUnit, mir, dInsn, callState,
815 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700816 }
817 } else {
818 loc = oatUpdateLoc(cUnit, loc);
819 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
820 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700821 callState = nextCallInsn(cUnit, mir, dInsn, callState,
822 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700823 }
buzbee67bf8852011-08-17 17:51:35 -0700824 }
825 }
826
827 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
828 int outsOffset = 4 /* Method* */ + (3 * 4);
829 if (numArgs >= 20) {
830 // Generate memcpy, but first make sure all of
831 opRegRegImm(cUnit, kOpAdd, r0, rSP, startOffset);
832 opRegRegImm(cUnit, kOpAdd, r1, rSP, outsOffset);
833 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
834 loadConstant(cUnit, r2, (numArgs - 3) * 4);
835 newLIR1(cUnit, kThumbBlxR, rLR);
836 } else {
837 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700838 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700839 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700840 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbee1b4c8592011-08-31 10:43:51 -0700841 newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700842 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700843 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700844 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700845 newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700846 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700847 }
848
849 // Handle the 1st 3 in r1, r2 & r3
buzbeee9a72f62011-09-04 17:59:07 -0700850 for (unsigned int i = 0; i < 3; i++) {
851 if (i < dInsn->vA)
852 registerArgs[i] = dInsn->vC + i;
853 else
854 registerArgs[i] = INVALID_REG;
buzbee67bf8852011-08-17 17:51:35 -0700855 }
buzbeee9a72f62011-09-04 17:59:07 -0700856 if (skipThis) {
857 registerArgs[0] = INVALID_REG;
858 }
859 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
860 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700861
862 // Finally, deal with the register arguments
863 // We'll be using fixed registers here
buzbee2e748f32011-08-29 21:02:19 -0700864 oatLockCallTemps(cUnit);
buzbee561227c2011-09-02 15:28:19 -0700865 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700866 return callState;
867}
868
buzbee561227c2011-09-02 15:28:19 -0700869static void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
870 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700871{
872 DecodedInstruction* dInsn = &mir->dalvikInsn;
873 int callState = 0;
874 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700875 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee7b1b86d2011-08-26 18:59:10 -0700876
buzbee561227c2011-09-02 15:28:19 -0700877 NextCallInsn nextCallInsn = nextSDCallInsn;
878
879 if (range) {
880 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700881 nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700882 } else {
883 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
buzbee1da522d2011-09-04 11:22:20 -0700884 false, nextCallInsn, NULL, false);
buzbee561227c2011-09-02 15:28:19 -0700885 }
buzbee67bf8852011-08-17 17:51:35 -0700886 // Finish up any of the call sequence not interleaved in arg loading
887 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700888 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700889 }
890 newLIR1(cUnit, kThumbBlxR, rLR);
891}
892
buzbee4a3164f2011-09-03 11:25:10 -0700893/*
894 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
895 * which will locate the target and continue on via a tail call.
896 */
buzbee67bf8852011-08-17 17:51:35 -0700897static void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
898{
899 DecodedInstruction* dInsn = &mir->dalvikInsn;
900 int callState = 0;
901 ArmLIR* nullCk;
902 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -0700903 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700904 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
905 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700906 false, nextInterfaceCallInsn, NULL,
907 true);
buzbee67bf8852011-08-17 17:51:35 -0700908 else
909 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700910 nextInterfaceCallInsn, NULL, true);
buzbee67bf8852011-08-17 17:51:35 -0700911 // Finish up any of the call sequence not interleaved in arg loading
912 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700913 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700914 }
915 newLIR1(cUnit, kThumbBlxR, rLR);
916}
917
918static void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
919{
920 DecodedInstruction* dInsn = &mir->dalvikInsn;
921 int callState = 0;
922 ArmLIR* nullCk;
buzbee4a3164f2011-09-03 11:25:10 -0700923 ArmLIR* rollback;
924 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
925 Get(dInsn->vB);
926 NextCallInsn nextCallInsn;
927 bool fastPath = true;
buzbee6a0f7f52011-09-05 16:14:20 -0700928 if (FORCE_SLOW || baseMethod == NULL) {
buzbee4a3164f2011-09-03 11:25:10 -0700929 fastPath = false;
930 } else {
931 Class* superClass = cUnit->method->GetDeclaringClass()->GetSuperClass();
932 if (superClass == NULL) {
933 fastPath = false;
934 } else {
935 int32_t target_idx = baseMethod->GetMethodIndex();
936 if (superClass->GetVTable()->GetLength() <= target_idx) {
937 fastPath = false;
938 } else {
939 fastPath = (superClass->GetVTable()->Get(target_idx) != NULL);
940 }
941 }
942 }
943 if (fastPath) {
944 nextCallInsn = nextSuperCallInsn;
945 rollback = NULL;
946 } else {
947 nextCallInsn = nextSuperCallInsnSP;
948 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
949 rollback->defMask = -1;
950 }
buzbee67bf8852011-08-17 17:51:35 -0700951 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
952 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700953 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700954 else
955 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700956 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700957 // Finish up any of the call sequence not interleaved in arg loading
958 while (callState >= 0) {
buzbee6a0f7f52011-09-05 16:14:20 -0700959 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700960 }
961 newLIR1(cUnit, kThumbBlxR, rLR);
962}
963
964static void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
965{
966 DecodedInstruction* dInsn = &mir->dalvikInsn;
967 int callState = 0;
968 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700969 ArmLIR* rollback;
970 Method* method = cUnit->method->GetDexCacheResolvedMethods()->
971 Get(dInsn->vB);
972 NextCallInsn nextCallInsn;
buzbee7b1b86d2011-08-26 18:59:10 -0700973
buzbeee9a72f62011-09-04 17:59:07 -0700974 if (FORCE_SLOW || method == NULL) {
buzbee561227c2011-09-02 15:28:19 -0700975 // Slow path
976 nextCallInsn = nextVCallInsnSP;
977 // If we need a slow-path callout, we'll restart here
978 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
979 rollback->defMask = -1;
980 } else {
981 // Fast path
982 nextCallInsn = nextVCallInsn;
983 rollback = NULL;
984 }
buzbee67bf8852011-08-17 17:51:35 -0700985 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
986 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700987 false, nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700988 else
989 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee1da522d2011-09-04 11:22:20 -0700990 nextCallInsn, rollback, true);
buzbee67bf8852011-08-17 17:51:35 -0700991 // Finish up any of the call sequence not interleaved in arg loading
992 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700993 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700994 }
995 newLIR1(cUnit, kThumbBlxR, rLR);
996}
997
buzbee67bf8852011-08-17 17:51:35 -0700998static bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
999 BasicBlock* bb, ArmLIR* labelList)
1000{
1001 bool res = false; // Assume success
1002 RegLocation rlSrc[3];
1003 RegLocation rlDest = badLoc;
1004 RegLocation rlResult = badLoc;
1005 Opcode opcode = mir->dalvikInsn.opcode;
1006
1007 /* Prep Src and Dest locations */
1008 int nextSreg = 0;
1009 int nextLoc = 0;
1010 int attrs = oatDataFlowAttributes[opcode];
1011 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
1012 if (attrs & DF_UA) {
1013 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1014 nextSreg++;
1015 } else if (attrs & DF_UA_WIDE) {
1016 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1017 nextSreg + 1);
1018 nextSreg+= 2;
1019 }
1020 if (attrs & DF_UB) {
1021 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1022 nextSreg++;
1023 } else if (attrs & DF_UB_WIDE) {
1024 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1025 nextSreg + 1);
1026 nextSreg+= 2;
1027 }
1028 if (attrs & DF_UC) {
1029 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
1030 } else if (attrs & DF_UC_WIDE) {
1031 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
1032 nextSreg + 1);
1033 }
1034 if (attrs & DF_DA) {
1035 rlDest = oatGetDest(cUnit, mir, 0);
1036 } else if (attrs & DF_DA_WIDE) {
1037 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1038 }
1039
1040 switch(opcode) {
1041 case OP_NOP:
1042 break;
1043
1044 case OP_MOVE_EXCEPTION:
1045 int exOffset;
1046 int resetReg;
buzbeec143c552011-08-20 17:38:58 -07001047 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001048 resetReg = oatAllocTemp(cUnit);
1049 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1050 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
1051 loadConstant(cUnit, resetReg, 0);
1052 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1053 storeValue(cUnit, rlDest, rlResult);
1054 break;
1055
1056 case OP_RETURN_VOID:
1057 break;
1058
1059 case OP_RETURN:
1060 case OP_RETURN_OBJECT:
1061 storeValue(cUnit, retLoc, rlSrc[0]);
1062 break;
1063
1064 case OP_RETURN_WIDE:
1065 rlDest = retLocWide;
1066 rlDest.fp = rlSrc[0].fp;
1067 storeValueWide(cUnit, rlDest, rlSrc[0]);
1068 break;
1069
1070 case OP_MOVE_RESULT_WIDE:
1071 if (mir->OptimizationFlags & MIR_INLINED)
1072 break; // Nop - combined w/ previous invoke
1073 /*
1074 * Somewhat hacky here. Because we're now passing
1075 * return values in registers, we have to let the
1076 * register allocation utilities know that the return
1077 * registers are live and may not be used for address
1078 * formation in storeValueWide.
1079 */
1080 assert(retLocWide.lowReg == r0);
buzbee1da522d2011-09-04 11:22:20 -07001081 assert(retLocWide.highReg == r1);
buzbee67bf8852011-08-17 17:51:35 -07001082 oatLockTemp(cUnit, retLocWide.lowReg);
1083 oatLockTemp(cUnit, retLocWide.highReg);
1084 storeValueWide(cUnit, rlDest, retLocWide);
1085 oatFreeTemp(cUnit, retLocWide.lowReg);
1086 oatFreeTemp(cUnit, retLocWide.highReg);
1087 break;
1088
1089 case OP_MOVE_RESULT:
1090 case OP_MOVE_RESULT_OBJECT:
1091 if (mir->OptimizationFlags & MIR_INLINED)
1092 break; // Nop - combined w/ previous invoke
1093 /* See comment for OP_MOVE_RESULT_WIDE */
1094 assert(retLoc.lowReg == r0);
1095 oatLockTemp(cUnit, retLoc.lowReg);
1096 storeValue(cUnit, rlDest, retLoc);
1097 oatFreeTemp(cUnit, retLoc.lowReg);
1098 break;
1099
1100 case OP_MOVE:
1101 case OP_MOVE_OBJECT:
1102 case OP_MOVE_16:
1103 case OP_MOVE_OBJECT_16:
1104 case OP_MOVE_FROM16:
1105 case OP_MOVE_OBJECT_FROM16:
1106 storeValue(cUnit, rlDest, rlSrc[0]);
1107 break;
1108
1109 case OP_MOVE_WIDE:
1110 case OP_MOVE_WIDE_16:
1111 case OP_MOVE_WIDE_FROM16:
1112 storeValueWide(cUnit, rlDest, rlSrc[0]);
1113 break;
1114
1115 case OP_CONST:
1116 case OP_CONST_4:
1117 case OP_CONST_16:
1118 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1119 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1120 storeValue(cUnit, rlDest, rlResult);
1121 break;
1122
1123 case OP_CONST_HIGH16:
1124 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1125 loadConstantNoClobber(cUnit, rlResult.lowReg,
1126 mir->dalvikInsn.vB << 16);
1127 storeValue(cUnit, rlDest, rlResult);
1128 break;
1129
1130 case OP_CONST_WIDE_16:
1131 case OP_CONST_WIDE_32:
1132 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1133 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1134 //TUNING: do high separately to avoid load dependency
1135 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1136 storeValueWide(cUnit, rlDest, rlResult);
1137 break;
1138
1139 case OP_CONST_WIDE:
1140 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1141 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001142 mir->dalvikInsn.vB_wide & 0xffffffff,
1143 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001144 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001145 break;
1146
1147 case OP_CONST_WIDE_HIGH16:
1148 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1149 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1150 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001151 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001152 break;
1153
1154 case OP_MONITOR_ENTER:
1155 genMonitorEnter(cUnit, mir, rlSrc[0]);
1156 break;
1157
1158 case OP_MONITOR_EXIT:
1159 genMonitorExit(cUnit, mir, rlSrc[0]);
1160 break;
1161
1162 case OP_CHECK_CAST:
1163 genCheckCast(cUnit, mir, rlSrc[0]);
1164 break;
1165
1166 case OP_INSTANCE_OF:
1167 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1168 break;
1169
1170 case OP_NEW_INSTANCE:
1171 genNewInstance(cUnit, mir, rlDest);
1172 break;
1173
1174 case OP_THROW:
1175 genThrow(cUnit, mir, rlSrc[0]);
1176 break;
1177
1178 case OP_ARRAY_LENGTH:
1179 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001180 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001181 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee67bf8852011-08-17 17:51:35 -07001182 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg,
1183 mir->offset, NULL);
1184 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1185 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1186 rlResult.lowReg);
1187 storeValue(cUnit, rlDest, rlResult);
1188 break;
1189
1190 case OP_CONST_STRING:
1191 case OP_CONST_STRING_JUMBO:
1192 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1193 break;
1194
1195 case OP_CONST_CLASS:
1196 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1197 break;
1198
1199 case OP_FILL_ARRAY_DATA:
1200 genFillArrayData(cUnit, mir, rlSrc[0]);
1201 break;
1202
1203 case OP_FILLED_NEW_ARRAY:
1204 genFilledNewArray(cUnit, mir, false /* not range */);
1205 break;
1206
1207 case OP_FILLED_NEW_ARRAY_RANGE:
1208 genFilledNewArray(cUnit, mir, true /* range */);
1209 break;
1210
1211 case OP_NEW_ARRAY:
1212 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1213 break;
1214
1215 case OP_GOTO:
1216 case OP_GOTO_16:
1217 case OP_GOTO_32:
1218 // TUNING: add MIR flag to disable when unnecessary
1219 bool backwardBranch;
1220 backwardBranch = (bb->taken->startOffset <= mir->offset);
1221 if (backwardBranch) {
1222 genSuspendPoll(cUnit, mir);
1223 }
1224 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1225 break;
1226
1227 case OP_PACKED_SWITCH:
1228 genPackedSwitch(cUnit, mir, rlSrc[0]);
1229 break;
1230
1231 case OP_SPARSE_SWITCH:
1232 genSparseSwitch(cUnit, mir, rlSrc[0]);
1233 break;
1234
1235 case OP_CMPL_FLOAT:
1236 case OP_CMPG_FLOAT:
1237 case OP_CMPL_DOUBLE:
1238 case OP_CMPG_DOUBLE:
1239 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1240 break;
1241
1242 case OP_CMP_LONG:
1243 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1244 break;
1245
1246 case OP_IF_EQ:
1247 case OP_IF_NE:
1248 case OP_IF_LT:
1249 case OP_IF_GE:
1250 case OP_IF_GT:
1251 case OP_IF_LE: {
1252 bool backwardBranch;
1253 ArmConditionCode cond;
1254 backwardBranch = (bb->taken->startOffset <= mir->offset);
1255 if (backwardBranch) {
1256 genSuspendPoll(cUnit, mir);
1257 }
1258 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1259 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1260 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1261 switch(opcode) {
1262 case OP_IF_EQ:
1263 cond = kArmCondEq;
1264 break;
1265 case OP_IF_NE:
1266 cond = kArmCondNe;
1267 break;
1268 case OP_IF_LT:
1269 cond = kArmCondLt;
1270 break;
1271 case OP_IF_GE:
1272 cond = kArmCondGe;
1273 break;
1274 case OP_IF_GT:
1275 cond = kArmCondGt;
1276 break;
1277 case OP_IF_LE:
1278 cond = kArmCondLe;
1279 break;
1280 default:
1281 cond = (ArmConditionCode)0;
1282 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1283 }
1284 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1285 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1286 break;
1287 }
1288
1289 case OP_IF_EQZ:
1290 case OP_IF_NEZ:
1291 case OP_IF_LTZ:
1292 case OP_IF_GEZ:
1293 case OP_IF_GTZ:
1294 case OP_IF_LEZ: {
1295 bool backwardBranch;
1296 ArmConditionCode cond;
1297 backwardBranch = (bb->taken->startOffset <= mir->offset);
1298 if (backwardBranch) {
1299 genSuspendPoll(cUnit, mir);
1300 }
1301 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1302 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1303 switch(opcode) {
1304 case OP_IF_EQZ:
1305 cond = kArmCondEq;
1306 break;
1307 case OP_IF_NEZ:
1308 cond = kArmCondNe;
1309 break;
1310 case OP_IF_LTZ:
1311 cond = kArmCondLt;
1312 break;
1313 case OP_IF_GEZ:
1314 cond = kArmCondGe;
1315 break;
1316 case OP_IF_GTZ:
1317 cond = kArmCondGt;
1318 break;
1319 case OP_IF_LEZ:
1320 cond = kArmCondLe;
1321 break;
1322 default:
1323 cond = (ArmConditionCode)0;
1324 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1325 }
1326 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1327 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1328 break;
1329 }
1330
1331 case OP_AGET_WIDE:
1332 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1333 break;
1334 case OP_AGET:
1335 case OP_AGET_OBJECT:
1336 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1337 break;
1338 case OP_AGET_BOOLEAN:
1339 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1340 rlDest, 0);
1341 break;
1342 case OP_AGET_BYTE:
1343 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1344 break;
1345 case OP_AGET_CHAR:
1346 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1347 rlDest, 1);
1348 break;
1349 case OP_AGET_SHORT:
1350 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1351 break;
1352 case OP_APUT_WIDE:
1353 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1354 break;
1355 case OP_APUT:
1356 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1357 break;
1358 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001359 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001360 break;
1361 case OP_APUT_SHORT:
1362 case OP_APUT_CHAR:
1363 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1364 rlSrc[0], 1);
1365 break;
1366 case OP_APUT_BYTE:
1367 case OP_APUT_BOOLEAN:
1368 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1369 rlSrc[0], 0);
1370 break;
1371
1372 case OP_IGET_WIDE:
1373 case OP_IGET_WIDE_VOLATILE:
1374 genIGetWideX(cUnit, mir, rlDest, rlSrc[0]);
1375 break;
1376
1377 case OP_IGET:
1378 case OP_IGET_VOLATILE:
1379 case OP_IGET_OBJECT:
1380 case OP_IGET_OBJECT_VOLATILE:
1381 genIGetX(cUnit, mir, kWord, rlDest, rlSrc[0]);
1382 break;
1383
1384 case OP_IGET_BOOLEAN:
1385 case OP_IGET_BYTE:
1386 genIGetX(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
1387 break;
1388
1389 case OP_IGET_CHAR:
1390 genIGetX(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
1391 break;
1392
1393 case OP_IGET_SHORT:
1394 genIGetX(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
1395 break;
1396
1397 case OP_IPUT_WIDE:
1398 case OP_IPUT_WIDE_VOLATILE:
1399 genIPutWideX(cUnit, mir, rlSrc[0], rlSrc[1]);
1400 break;
1401
1402 case OP_IPUT_OBJECT:
1403 case OP_IPUT_OBJECT_VOLATILE:
1404 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
1405 break;
1406
1407 case OP_IPUT:
1408 case OP_IPUT_VOLATILE:
1409 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
1410 break;
1411
1412 case OP_IPUT_BOOLEAN:
1413 case OP_IPUT_BYTE:
1414 genIPutX(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
1415 break;
1416
1417 case OP_IPUT_CHAR:
1418 genIPutX(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
1419 break;
1420
1421 case OP_IPUT_SHORT:
1422 genIPutX(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
1423 break;
1424
1425 case OP_SGET:
1426 case OP_SGET_OBJECT:
1427 case OP_SGET_BOOLEAN:
1428 case OP_SGET_BYTE:
1429 case OP_SGET_CHAR:
1430 case OP_SGET_SHORT:
1431 genSget(cUnit, mir, rlResult, rlDest);
1432 break;
1433
1434 case OP_SGET_WIDE:
1435 genSgetWide(cUnit, mir, rlResult, rlDest);
1436 break;
1437
1438 case OP_SPUT:
1439 case OP_SPUT_OBJECT:
1440 case OP_SPUT_BOOLEAN:
1441 case OP_SPUT_BYTE:
1442 case OP_SPUT_CHAR:
1443 case OP_SPUT_SHORT:
1444 genSput(cUnit, mir, rlSrc[0]);
1445 break;
1446
1447 case OP_SPUT_WIDE:
1448 genSputWide(cUnit, mir, rlSrc[0]);
1449 break;
1450
1451 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001452 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1453 true /*range*/);
1454 break;
buzbee67bf8852011-08-17 17:51:35 -07001455 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001456 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1457 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001458 break;
1459
1460 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001461 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1462 false /*range*/);
1463 break;
buzbee67bf8852011-08-17 17:51:35 -07001464 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001465 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1466 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001467 break;
1468
1469 case OP_INVOKE_VIRTUAL:
1470 case OP_INVOKE_VIRTUAL_RANGE:
1471 genInvokeVirtual(cUnit, mir);
1472 break;
1473
1474 case OP_INVOKE_SUPER:
1475 case OP_INVOKE_SUPER_RANGE:
1476 genInvokeSuper(cUnit, mir);
1477 break;
1478
1479 case OP_INVOKE_INTERFACE:
1480 case OP_INVOKE_INTERFACE_RANGE:
1481 genInvokeInterface(cUnit, mir);
1482 break;
1483
1484 case OP_NEG_INT:
1485 case OP_NOT_INT:
1486 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1487 break;
1488
1489 case OP_NEG_LONG:
1490 case OP_NOT_LONG:
1491 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1492 break;
1493
1494 case OP_NEG_FLOAT:
1495 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1496 break;
1497
1498 case OP_NEG_DOUBLE:
1499 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1500 break;
1501
1502 case OP_INT_TO_LONG:
1503 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1504 if (rlSrc[0].location == kLocPhysReg) {
1505 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1506 } else {
1507 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1508 }
1509 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1510 rlResult.lowReg, 31);
1511 storeValueWide(cUnit, rlDest, rlResult);
1512 break;
1513
1514 case OP_LONG_TO_INT:
1515 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1516 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1517 storeValue(cUnit, rlDest, rlSrc[0]);
1518 break;
1519
1520 case OP_INT_TO_BYTE:
1521 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1522 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1523 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1524 storeValue(cUnit, rlDest, rlResult);
1525 break;
1526
1527 case OP_INT_TO_SHORT:
1528 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1529 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1530 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1531 storeValue(cUnit, rlDest, rlResult);
1532 break;
1533
1534 case OP_INT_TO_CHAR:
1535 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1536 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1537 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1538 storeValue(cUnit, rlDest, rlResult);
1539 break;
1540
1541 case OP_INT_TO_FLOAT:
1542 case OP_INT_TO_DOUBLE:
1543 case OP_LONG_TO_FLOAT:
1544 case OP_LONG_TO_DOUBLE:
1545 case OP_FLOAT_TO_INT:
1546 case OP_FLOAT_TO_LONG:
1547 case OP_FLOAT_TO_DOUBLE:
1548 case OP_DOUBLE_TO_INT:
1549 case OP_DOUBLE_TO_LONG:
1550 case OP_DOUBLE_TO_FLOAT:
1551 genConversion(cUnit, mir);
1552 break;
1553
1554 case OP_ADD_INT:
1555 case OP_SUB_INT:
1556 case OP_MUL_INT:
1557 case OP_DIV_INT:
1558 case OP_REM_INT:
1559 case OP_AND_INT:
1560 case OP_OR_INT:
1561 case OP_XOR_INT:
1562 case OP_SHL_INT:
1563 case OP_SHR_INT:
1564 case OP_USHR_INT:
1565 case OP_ADD_INT_2ADDR:
1566 case OP_SUB_INT_2ADDR:
1567 case OP_MUL_INT_2ADDR:
1568 case OP_DIV_INT_2ADDR:
1569 case OP_REM_INT_2ADDR:
1570 case OP_AND_INT_2ADDR:
1571 case OP_OR_INT_2ADDR:
1572 case OP_XOR_INT_2ADDR:
1573 case OP_SHL_INT_2ADDR:
1574 case OP_SHR_INT_2ADDR:
1575 case OP_USHR_INT_2ADDR:
1576 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1577 break;
1578
1579 case OP_ADD_LONG:
1580 case OP_SUB_LONG:
1581 case OP_MUL_LONG:
1582 case OP_DIV_LONG:
1583 case OP_REM_LONG:
1584 case OP_AND_LONG:
1585 case OP_OR_LONG:
1586 case OP_XOR_LONG:
1587 case OP_ADD_LONG_2ADDR:
1588 case OP_SUB_LONG_2ADDR:
1589 case OP_MUL_LONG_2ADDR:
1590 case OP_DIV_LONG_2ADDR:
1591 case OP_REM_LONG_2ADDR:
1592 case OP_AND_LONG_2ADDR:
1593 case OP_OR_LONG_2ADDR:
1594 case OP_XOR_LONG_2ADDR:
1595 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1596 break;
1597
buzbee67bf8852011-08-17 17:51:35 -07001598 case OP_SHL_LONG:
1599 case OP_SHR_LONG:
1600 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001601 case OP_SHL_LONG_2ADDR:
1602 case OP_SHR_LONG_2ADDR:
1603 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001604 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1605 break;
1606
1607 case OP_ADD_FLOAT:
1608 case OP_SUB_FLOAT:
1609 case OP_MUL_FLOAT:
1610 case OP_DIV_FLOAT:
1611 case OP_REM_FLOAT:
1612 case OP_ADD_FLOAT_2ADDR:
1613 case OP_SUB_FLOAT_2ADDR:
1614 case OP_MUL_FLOAT_2ADDR:
1615 case OP_DIV_FLOAT_2ADDR:
1616 case OP_REM_FLOAT_2ADDR:
1617 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1618 break;
1619
1620 case OP_ADD_DOUBLE:
1621 case OP_SUB_DOUBLE:
1622 case OP_MUL_DOUBLE:
1623 case OP_DIV_DOUBLE:
1624 case OP_REM_DOUBLE:
1625 case OP_ADD_DOUBLE_2ADDR:
1626 case OP_SUB_DOUBLE_2ADDR:
1627 case OP_MUL_DOUBLE_2ADDR:
1628 case OP_DIV_DOUBLE_2ADDR:
1629 case OP_REM_DOUBLE_2ADDR:
1630 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1631 break;
1632
1633 case OP_RSUB_INT:
1634 case OP_ADD_INT_LIT16:
1635 case OP_MUL_INT_LIT16:
1636 case OP_DIV_INT_LIT16:
1637 case OP_REM_INT_LIT16:
1638 case OP_AND_INT_LIT16:
1639 case OP_OR_INT_LIT16:
1640 case OP_XOR_INT_LIT16:
1641 case OP_ADD_INT_LIT8:
1642 case OP_RSUB_INT_LIT8:
1643 case OP_MUL_INT_LIT8:
1644 case OP_DIV_INT_LIT8:
1645 case OP_REM_INT_LIT8:
1646 case OP_AND_INT_LIT8:
1647 case OP_OR_INT_LIT8:
1648 case OP_XOR_INT_LIT8:
1649 case OP_SHL_INT_LIT8:
1650 case OP_SHR_INT_LIT8:
1651 case OP_USHR_INT_LIT8:
1652 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1653 break;
1654
1655 default:
1656 res = true;
1657 }
1658 return res;
1659}
1660
1661static const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
1662 "kMirOpPhi",
1663 "kMirOpNullNRangeUpCheck",
1664 "kMirOpNullNRangeDownCheck",
1665 "kMirOpLowerBound",
1666 "kMirOpPunt",
1667 "kMirOpCheckInlinePrediction",
1668};
1669
1670/* Extended MIR instructions like PHI */
1671static void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
1672{
1673 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1674 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1675 strcpy(msg, extendedMIROpNames[opOffset]);
1676 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1677
1678 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1679 case kMirOpPhi: {
1680 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1681 op->flags.isNop = true;
1682 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1683 break;
1684 }
1685 default:
1686 break;
1687 }
1688}
1689
1690/* If there are any ins passed in registers that have not been promoted
1691 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001692 * Note: at this pointCopy any ins that are passed in register to their
1693 * home location */
buzbee67bf8852011-08-17 17:51:35 -07001694static void flushIns(CompilationUnit* cUnit)
1695{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001696 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001697 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001698 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1699 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001700 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001701 int startLoc = cUnit->method->NumRegisters() -
1702 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001703 for (int i = 0; i < inRegs; i++) {
1704 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001705 //TUNING: be smarter about flushing ins to frame
1706 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001707 if (loc.location == kLocPhysReg) {
1708 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001709 }
1710 }
1711
1712 // Handle special case of wide argument half in regs, half in frame
1713 if (inRegs == 3) {
1714 RegLocation loc = cUnit->regLocation[startLoc + 2];
1715 if (loc.wide && loc.location == kLocPhysReg) {
1716 // Load the other half of the arg into the promoted pair
buzbee561227c2011-09-02 15:28:19 -07001717 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -07001718 inRegs++;
1719 }
1720 }
1721
1722 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001723 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001724 RegLocation loc = cUnit->regLocation[startLoc + i];
1725 if (loc.fpLocation == kLocPhysReg) {
1726 loc.location = kLocPhysReg;
1727 loc.fp = true;
1728 loc.lowReg = loc.fpLowReg;
1729 loc.highReg = loc.fpHighReg;
1730 }
1731 if (loc.location == kLocPhysReg) {
1732 if (loc.wide) {
1733 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1734 loc.lowReg, loc.highReg, INVALID_SREG);
1735 i++;
1736 } else {
buzbee561227c2011-09-02 15:28:19 -07001737 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001738 }
1739 }
1740 i++;
1741 }
1742}
1743
1744/* Handle the content in each basic block */
1745static bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
1746{
1747 MIR* mir;
1748 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1749 int blockId = bb->id;
1750
1751 cUnit->curBlock = bb;
1752 labelList[blockId].operands[0] = bb->startOffset;
1753
1754 /* Insert the block label */
1755 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1756 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1757
1758 oatClobberAllRegs(cUnit);
1759 oatResetNullCheck(cUnit);
1760
1761 ArmLIR* headLIR = NULL;
1762
1763 if (bb->blockType == kEntryBlock) {
1764 /*
1765 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1766 * mechanism know so it doesn't try to use any of them when
1767 * expanding the frame or flushing. This leaves the utility
1768 * code with a single temp: r12. This should be enough.
1769 */
1770 oatLockTemp(cUnit, r0);
1771 oatLockTemp(cUnit, r1);
1772 oatLockTemp(cUnit, r2);
1773 oatLockTemp(cUnit, r3);
1774 newLIR0(cUnit, kArmPseudoMethodEntry);
1775 /* Spill core callee saves */
1776 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1777 /* Need to spill any FP regs? */
1778 if (cUnit->numFPSpills) {
1779 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1780 }
1781 opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1782 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1783 flushIns(cUnit);
1784 oatFreeTemp(cUnit, r0);
1785 oatFreeTemp(cUnit, r1);
1786 oatFreeTemp(cUnit, r2);
1787 oatFreeTemp(cUnit, r3);
1788 } else if (bb->blockType == kExitBlock) {
1789 newLIR0(cUnit, kArmPseudoMethodExit);
1790 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1791 /* Need to restore any FP callee saves? */
1792 if (cUnit->numFPSpills) {
1793 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1794 }
1795 if (cUnit->coreSpillMask & (1 << rLR)) {
1796 /* Unspill rLR to rPC */
1797 cUnit->coreSpillMask &= ~(1 << rLR);
1798 cUnit->coreSpillMask |= (1 << rPC);
1799 }
1800 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1801 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1802 /* We didn't pop to rPC, so must do a bv rLR */
1803 newLIR1(cUnit, kThumbBx, rLR);
1804 }
1805 }
1806
1807 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1808
1809 oatResetRegPool(cUnit);
1810 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1811 oatClobberAllRegs(cUnit);
1812 }
1813
1814 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1815 oatResetDefTracking(cUnit);
1816 }
1817
1818 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1819 handleExtendedMethodMIR(cUnit, mir);
1820 continue;
1821 }
1822
1823 cUnit->currentDalvikOffset = mir->offset;
1824
1825 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1826 InstructionFormat dalvikFormat =
1827 dexGetFormatFromOpcode(dalvikOpcode);
1828
1829 ArmLIR* boundaryLIR;
1830
1831 /* Mark the beginning of a Dalvik instruction for line tracking */
1832 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1833 (int) oatGetDalvikDisassembly(
1834 &mir->dalvikInsn, ""));
1835 /* Remember the first LIR for this block */
1836 if (headLIR == NULL) {
1837 headLIR = boundaryLIR;
1838 /* Set the first boundaryLIR as a scheduling barrier */
1839 headLIR->defMask = ENCODE_ALL;
1840 }
1841
1842 /* Don't generate the SSA annotation unless verbose mode is on */
1843 if (cUnit->printMe && mir->ssaRep) {
1844 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1845 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1846 }
1847
1848 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1849
1850 if (notHandled) {
1851 char buf[100];
1852 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1853 mir->offset,
1854 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1855 dalvikFormat);
1856 LOG(FATAL) << buf;
1857 }
1858 }
1859
1860 if (headLIR) {
1861 /*
1862 * Eliminate redundant loads/stores and delay stores into later
1863 * slots
1864 */
1865 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1866 cUnit->lastLIRInsn);
1867
1868 /*
1869 * Generate an unconditional branch to the fallthrough block.
1870 */
1871 if (bb->fallThrough) {
1872 genUnconditionalBranch(cUnit,
1873 &labelList[bb->fallThrough->id]);
1874 }
1875 }
1876 return false;
1877}
1878
1879/*
1880 * Nop any unconditional branches that go to the next instruction.
1881 * Note: new redundant branches may be inserted later, and we'll
1882 * use a check in final instruction assembly to nop those out.
1883 */
1884void removeRedundantBranches(CompilationUnit* cUnit)
1885{
1886 ArmLIR* thisLIR;
1887
1888 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1889 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1890 thisLIR = NEXT_LIR(thisLIR)) {
1891
1892 /* Branch to the next instruction */
1893 if ((thisLIR->opcode == kThumbBUncond) ||
1894 (thisLIR->opcode == kThumb2BUncond)) {
1895 ArmLIR* nextLIR = thisLIR;
1896
1897 while (true) {
1898 nextLIR = NEXT_LIR(nextLIR);
1899
1900 /*
1901 * Is the branch target the next instruction?
1902 */
1903 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1904 thisLIR->flags.isNop = true;
1905 break;
1906 }
1907
1908 /*
1909 * Found real useful stuff between the branch and the target.
1910 * Need to explicitly check the lastLIRInsn here because it
1911 * might be the last real instruction.
1912 */
1913 if (!isPseudoOpcode(nextLIR->opcode) ||
1914 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1915 break;
1916 }
1917 }
1918 }
1919}
1920
1921void oatMethodMIR2LIR(CompilationUnit* cUnit)
1922{
1923 /* Used to hold the labels of each block */
1924 cUnit->blockLabelList =
1925 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
1926
1927 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
1928 kPreOrderDFSTraversal, false /* Iterative */);
1929 removeRedundantBranches(cUnit);
1930}
1931
1932/* Common initialization routine for an architecture family */
1933bool oatArchInit()
1934{
1935 int i;
1936
1937 for (i = 0; i < kArmLast; i++) {
1938 if (EncodingMap[i].opcode != i) {
1939 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
1940 " is wrong: expecting " << i << ", seeing " <<
1941 (int)EncodingMap[i].opcode;
1942 }
1943 }
1944
1945 return oatArchVariantInit();
1946}
1947
1948/* Needed by the Assembler */
1949void oatSetupResourceMasks(ArmLIR* lir)
1950{
1951 setupResourceMasks(lir);
1952}
1953
1954/* Needed by the ld/st optmizatons */
1955ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
1956{
1957 return genRegCopyNoInsert(cUnit, rDest, rSrc);
1958}
1959
1960/* Needed by the register allocator */
1961ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
1962{
1963 return genRegCopy(cUnit, rDest, rSrc);
1964}
1965
1966/* Needed by the register allocator */
1967void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
1968 int srcLo, int srcHi)
1969{
1970 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
1971}
1972
1973void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
1974 int displacement, int rSrc, OpSize size)
1975{
1976 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
1977}
1978
1979void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
1980 int displacement, int rSrcLo, int rSrcHi)
1981{
1982 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
1983}