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