blob: d95b8700019d7936967634d95ad7d427ef3934d7 [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
buzbee67bf8852011-08-17 17:51:35 -070017static const RegLocation badLoc = {kLocDalvikFrame, 0, 0, INVALID_REG,
18 INVALID_REG, INVALID_SREG, 0,
19 kLocDalvikFrame, INVALID_REG, INVALID_REG,
20 INVALID_OFFSET};
21static const RegLocation retLoc = LOC_DALVIK_RETURN_VAL;
22static const RegLocation retLocWide = LOC_DALVIK_RETURN_VAL_WIDE;
23
buzbeedfd3d702011-08-28 12:56:51 -070024/*
25 * Let helper function take care of everything. Will call
26 * Array::AllocFromCode(type_idx, method, count);
27 * Note: AllocFromCode will handle checks for errNegativeArraySize.
28 */
buzbee67bf8852011-08-17 17:51:35 -070029static void genNewArray(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
30 RegLocation rlSrc)
31{
buzbeedfd3d702011-08-28 12:56:51 -070032 oatFlushAllRegs(cUnit); /* Everything to home location */
33 loadWordDisp(cUnit, rSELF,
34 OFFSETOF_MEMBER(Thread, pAllocFromCode), rLR);
35 loadCurrMethodDirect(cUnit, r1); // arg1 <- Method*
36 loadConstant(cUnit, r0, mir->dalvikInsn.vC); // arg0 <- type_id
37 loadValueDirectFixed(cUnit, rlSrc, r2); // arg2 <- count
38 opReg(cUnit, kOpBlx, rLR);
39 oatClobberCallRegs(cUnit);
40 RegLocation rlResult = oatGetReturn(cUnit);
41 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -070042}
43
44/*
45 * Similar to genNewArray, but with post-allocation initialization.
46 * Verifier guarantees we're dealing with an array class. Current
47 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
48 * Current code also throws internal unimp if not 'L', '[' or 'I'.
49 */
50static void genFilledNewArray(CompilationUnit* cUnit, MIR* mir, bool isRange)
51{
52 DecodedInstruction* dInsn = &mir->dalvikInsn;
53 int elems;
buzbeedfd3d702011-08-28 12:56:51 -070054 int typeId;
buzbee67bf8852011-08-17 17:51:35 -070055 if (isRange) {
56 elems = dInsn->vA;
buzbeedfd3d702011-08-28 12:56:51 -070057 typeId = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -070058 } else {
59 elems = dInsn->vB;
buzbeedfd3d702011-08-28 12:56:51 -070060 typeId = dInsn->vC;
buzbee67bf8852011-08-17 17:51:35 -070061 }
buzbeedfd3d702011-08-28 12:56:51 -070062 oatFlushAllRegs(cUnit); /* Everything to home location */
63 // TODO: Alloc variant that checks types (see header comment) */
64 UNIMPLEMENTED(WARNING) << "Need AllocFromCode variant w/ extra checks";
65 loadWordDisp(cUnit, rSELF,
66 OFFSETOF_MEMBER(Thread, pAllocFromCode), rLR);
67 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 slow_path = true;
143 bool isObject = ((mir->dalvikInsn.opcode == OP_SPUT_OBJECT) ||
144 (mir->dalvikInsn.opcode == OP_SPUT_OBJECT_VOLATILE));
145 UNIMPLEMENTED(WARNING) << "Implement sput fast path";
146 int funcOffset;
147 if (slow_path) {
148 if (isObject) {
149 funcOffset = OFFSETOF_MEMBER(Thread, pSetObjStatic);
150 } else {
151 funcOffset = OFFSETOF_MEMBER(Thread, pSet32Static);
152 }
153 oatFlushAllRegs(cUnit);
154 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
155 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
156 loadCurrMethodDirect(cUnit, r1);
157 loadValueDirect(cUnit, rlSrc, r2);
158 opReg(cUnit, kOpBlx, rLR);
159 oatClobberCallRegs(cUnit);
160 } else {
161 UNIMPLEMENTED(FATAL) << "Must update for new world";
buzbeec143c552011-08-20 17:38:58 -0700162#if 0
buzbee67bf8852011-08-17 17:51:35 -0700163 int valOffset = OFFSETOF_MEMBER(StaticField, value);
164 int tReg = oatAllocTemp(cUnit);
165 int objHead;
166 bool isVolatile;
167 bool isSputObject;
168 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
169 mir->meta.calleeMethod : cUnit->method;
170 void* fieldPtr = (void*)
171 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
172 Opcode opcode = mir->dalvikInsn.opcode;
173
174 if (fieldPtr == NULL) {
175 // FIXME: need to handle this case for oat();
176 UNIMPLEMENTED(FATAL);
177 }
178
179#if ANDROID_SMP != 0
180 isVolatile = (opcode == OP_SPUT_VOLATILE) ||
181 (opcode == OP_SPUT_VOLATILE_JUMBO) ||
182 (opcode == OP_SPUT_OBJECT_VOLATILE) ||
183 (opcode == OP_SPUT_OBJECT_VOLATILE_JUMBO);
buzbeec143c552011-08-20 17:38:58 -0700184 assert(isVolatile == artIsVolatileField((Field *) fieldPtr));
buzbee67bf8852011-08-17 17:51:35 -0700185#else
buzbeec143c552011-08-20 17:38:58 -0700186 isVolatile = artIsVolatileField((Field *) fieldPtr);
buzbee67bf8852011-08-17 17:51:35 -0700187#endif
188
189 isSputObject = (opcode == OP_SPUT_OBJECT) ||
190 (opcode == OP_SPUT_OBJECT_VOLATILE);
191
192 rlSrc = oatGetSrc(cUnit, mir, 0);
193 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
194 loadConstant(cUnit, tReg, (int) fieldPtr);
195 if (isSputObject) {
196 objHead = oatAllocTemp(cUnit);
197 loadWordDisp(cUnit, tReg, OFFSETOF_MEMBER(Field, clazz), objHead);
198 }
199 storeWordDisp(cUnit, tReg, valOffset ,rlSrc.lowReg);
200 oatFreeTemp(cUnit, tReg);
201 if (isVolatile) {
202 oatGenMemBarrier(cUnit, kSY);
203 }
204 if (isSputObject) {
205 /* NOTE: marking card based sfield->clazz */
206 markGCCard(cUnit, rlSrc.lowReg, objHead);
207 oatFreeTemp(cUnit, objHead);
208 }
buzbeec143c552011-08-20 17:38:58 -0700209#endif
buzbeee1931742011-08-28 21:15:53 -0700210 }
buzbee67bf8852011-08-17 17:51:35 -0700211}
212
213static void genSputWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
214{
buzbeee1931742011-08-28 21:15:53 -0700215 bool slow_path = true;
216 UNIMPLEMENTED(WARNING) << "Implement sput-wide fast path";
217 int funcOffset;
218 if (slow_path) {
219 funcOffset = OFFSETOF_MEMBER(Thread, pSet64Static);
220 oatFlushAllRegs(cUnit);
221 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
222 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
223 loadCurrMethodDirect(cUnit, r1);
224 loadValueDirectWideFixed(cUnit, rlSrc, r2, r3);
225 opReg(cUnit, kOpBlx, rLR);
226 oatClobberCallRegs(cUnit);
227 } else {
228 UNIMPLEMENTED(FATAL) << "Must update for new world";
buzbeec143c552011-08-20 17:38:58 -0700229#if 0
buzbee67bf8852011-08-17 17:51:35 -0700230 int tReg = oatAllocTemp(cUnit);
231 int valOffset = OFFSETOF_MEMBER(StaticField, value);
232 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
233 mir->meta.calleeMethod : cUnit->method;
234 void* fieldPtr = (void*)
235 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
236
237 if (fieldPtr == NULL) {
238 // FIXME: need to handle this case for oat();
239 UNIMPLEMENTED(FATAL);
240 }
241
242 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
243 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
244 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
245
246 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
buzbeec143c552011-08-20 17:38:58 -0700247#endif
buzbeee1931742011-08-28 21:15:53 -0700248 }
buzbee67bf8852011-08-17 17:51:35 -0700249}
250
251
252
253static void genSgetWide(CompilationUnit* cUnit, MIR* mir,
254 RegLocation rlResult, RegLocation rlDest)
255{
buzbeee1931742011-08-28 21:15:53 -0700256 bool slow_path = true;
257 UNIMPLEMENTED(WARNING) << "Implement sget-wide fast path";
258 int funcOffset;
259 if (slow_path) {
260 funcOffset = OFFSETOF_MEMBER(Thread, pGet64Static);
261 oatFlushAllRegs(cUnit);
262 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
263 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
264 loadCurrMethodDirect(cUnit, r1);
265 opReg(cUnit, kOpBlx, rLR);
266 RegLocation rlResult = oatGetReturnWide(cUnit);
267 storeValueWide(cUnit, rlDest, rlResult);
268 } else {
269 UNIMPLEMENTED(FATAL) << "Must update for new world";
buzbeec143c552011-08-20 17:38:58 -0700270#if 0
buzbee67bf8852011-08-17 17:51:35 -0700271 int valOffset = OFFSETOF_MEMBER(StaticField, value);
272 const Method *method = (mir->OptimizationFlags & MIR_CALLEE) ?
273 mir->meta.calleeMethod : cUnit->method;
274 void* fieldPtr = (void*)
275 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
276
277 if (fieldPtr == NULL) {
278 // FIXME: need to handle this case for oat();
279 UNIMPLEMENTED(FATAL);
280 }
281
282 int tReg = oatAllocTemp(cUnit);
283 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
284 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
285 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
286
287 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
288
289 storeValueWide(cUnit, rlDest, rlResult);
buzbeec143c552011-08-20 17:38:58 -0700290#endif
buzbeee1931742011-08-28 21:15:53 -0700291 }
buzbee67bf8852011-08-17 17:51:35 -0700292}
293
294static void genSget(CompilationUnit* cUnit, MIR* mir,
295 RegLocation rlResult, RegLocation rlDest)
296{
buzbeee1931742011-08-28 21:15:53 -0700297 bool slow_path = true;
298 bool isObject = ((mir->dalvikInsn.opcode == OP_SGET_OBJECT) ||
299 (mir->dalvikInsn.opcode == OP_SGET_OBJECT_VOLATILE));
300 UNIMPLEMENTED(WARNING) << "Implement sget fast path";
301 int funcOffset;
302 if (slow_path) {
303 if (isObject) {
304 funcOffset = OFFSETOF_MEMBER(Thread, pGetObjStatic);
305 } else {
306 funcOffset = OFFSETOF_MEMBER(Thread, pGet32Static);
307 }
308 oatFlushAllRegs(cUnit);
309 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
310 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
311 loadCurrMethodDirect(cUnit, r1);
312 opReg(cUnit, kOpBlx, rLR);
313 RegLocation rlResult = oatGetReturn(cUnit);
314 storeValue(cUnit, rlDest, rlResult);
315 } else {
316 UNIMPLEMENTED(FATAL) << "Must update for new world";
buzbeec143c552011-08-20 17:38:58 -0700317#if 0
buzbee67bf8852011-08-17 17:51:35 -0700318 int valOffset = OFFSETOF_MEMBER(StaticField, value);
319 int tReg = oatAllocTemp(cUnit);
320 bool isVolatile;
321 const Method *method = cUnit->method;
322 void* fieldPtr = (void*)
323 (method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
324
325 if (fieldPtr == NULL) {
326 // FIXME: need to handle this case for oat();
327 UNIMPLEMENTED(FATAL);
328 }
329
330 /*
331 * On SMP systems, Dalvik opcodes found to be referencing
332 * volatile fields are rewritten to their _VOLATILE variant.
333 * However, this does not happen on non-SMP systems. The compiler
334 * still needs to know about volatility to avoid unsafe
335 * optimizations so we determine volatility based on either
336 * the opcode or the field access flags.
337 */
338#if ANDROID_SMP != 0
339 Opcode opcode = mir->dalvikInsn.opcode;
340 isVolatile = (opcode == OP_SGET_VOLATILE) ||
341 (opcode == OP_SGET_OBJECT_VOLATILE);
buzbeec143c552011-08-20 17:38:58 -0700342 assert(isVolatile == artIsVolatileField((Field *) fieldPtr));
buzbee67bf8852011-08-17 17:51:35 -0700343#else
buzbeec143c552011-08-20 17:38:58 -0700344 isVolatile = artIsVolatileField((Field *) fieldPtr);
buzbee67bf8852011-08-17 17:51:35 -0700345#endif
346
347 rlDest = oatGetDest(cUnit, mir, 0);
348 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
349 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
350
351 if (isVolatile) {
352 oatGenMemBarrier(cUnit, kSY);
353 }
354 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
355
356 storeValue(cUnit, rlDest, rlResult);
buzbeec143c552011-08-20 17:38:58 -0700357#endif
buzbeee1931742011-08-28 21:15:53 -0700358 }
buzbee67bf8852011-08-17 17:51:35 -0700359}
360
buzbee561227c2011-09-02 15:28:19 -0700361typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int,
362 ArmLIR*);
buzbee67bf8852011-08-17 17:51:35 -0700363
364/*
365 * Bit of a hack here - in leiu of a real scheduling pass,
366 * emit the next instruction in static & direct invoke sequences.
367 */
368static int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700369 DecodedInstruction* dInsn, int state,
370 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700371{
buzbee561227c2011-09-02 15:28:19 -0700372 DCHECK(rollback == NULL);
373 uint32_t idx = dInsn->vB;
buzbee67bf8852011-08-17 17:51:35 -0700374 switch(state) {
375 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700376 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700377 break;
buzbee561227c2011-09-02 15:28:19 -0700378 case 1: // Get method->code_and_direct_methods_
379 loadWordDisp(cUnit, r0,
380 Method::GetDexCacheCodeAndDirectMethodsOffset().Int32Value(),
381 r0);
buzbee67bf8852011-08-17 17:51:35 -0700382 break;
buzbee561227c2011-09-02 15:28:19 -0700383 case 2: // Grab target method* and target code_
384 loadWordDisp(cUnit, r0,
385 art::CodeAndDirectMethods::CodeOffsetInBytes(idx), rLR);
386 loadWordDisp(cUnit, r0,
387 art::CodeAndDirectMethods::MethodOffsetInBytes(idx), r0);
buzbeec5ef0462011-08-25 18:44:49 -0700388 break;
389 default:
390 return -1;
391 }
392 return state + 1;
393}
394
buzbee67bf8852011-08-17 17:51:35 -0700395/*
396 * Bit of a hack here - in leiu of a real scheduling pass,
397 * emit the next instruction in a virtual invoke sequence.
398 * We can use rLR as a temp prior to target address loading
399 * Note also that we'll load the first argument ("this") into
400 * r1 here rather than the standard loadArgRegs.
401 */
402static int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700403 DecodedInstruction* dInsn, int state,
404 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700405{
buzbee561227c2011-09-02 15:28:19 -0700406 DCHECK(rollback == NULL);
buzbee67bf8852011-08-17 17:51:35 -0700407 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700408 /*
409 * This is the fast path in which the target virtual method is
410 * fully resolved at compile time.
411 */
412 Method* baseMethod = cUnit->method->GetDexCacheResolvedMethods()->
413 Get(dInsn->vB);
414 CHECK(baseMethod != NULL);
415 uint32_t target_idx = baseMethod->GetMethodIndex();
buzbee67bf8852011-08-17 17:51:35 -0700416 switch(state) {
buzbee561227c2011-09-02 15:28:19 -0700417 case 0: // Get "this" [set r1]
buzbee67bf8852011-08-17 17:51:35 -0700418 rlArg = oatGetSrc(cUnit, mir, 0);
419 loadValueDirectFixed(cUnit, rlArg, r1);
420 break;
buzbee561227c2011-09-02 15:28:19 -0700421 case 1: // Is "this" null? [use r1]
422 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir->offset, NULL);
423 // get this->klass_ [use r1, set rLR]
424 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700425 break;
buzbee561227c2011-09-02 15:28:19 -0700426 case 2: // Get this->klass_->vtable [usr rLR, set rLR]
427 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700428 break;
buzbee561227c2011-09-02 15:28:19 -0700429 case 3: // Get target method [use rLR, set r0]
430 loadWordDisp(cUnit, rLR, (target_idx * 4) +
431 art::Array::DataOffset().Int32Value(), r0);
432 break;
433 case 4: // Get the target compiled code address [uses r0, sets rLR]
434 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700435 break;
436 default:
437 return -1;
438 }
439 return state + 1;
440}
441
buzbee7b1b86d2011-08-26 18:59:10 -0700442static int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700443 DecodedInstruction* dInsn, int state,
444 ArmLIR* rollback)
buzbee7b1b86d2011-08-26 18:59:10 -0700445{
buzbee561227c2011-09-02 15:28:19 -0700446 DCHECK(rollback != NULL);
buzbee7b1b86d2011-08-26 18:59:10 -0700447 RegLocation rlArg;
buzbee561227c2011-09-02 15:28:19 -0700448 ArmLIR* skipBranch;
449 ArmLIR* skipTarget;
450 /*
451 * This handles the case in which the base method is not fully
452 * resolved at compile time. We must generate code to test
453 * for resolution a run time, bail to the slow path if not to
454 * fill in all the tables. In the latter case, we'll restart at
455 * at the beginning of the sequence.
456 */
buzbee7b1b86d2011-08-26 18:59:10 -0700457 switch(state) {
458 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700459 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700460 break;
buzbee561227c2011-09-02 15:28:19 -0700461 case 1: // Get method->dex_cache_resolved_methods_
462 loadWordDisp(cUnit, r0,
463 Method::GetDexCacheResolvedMethodsOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700464 break;
buzbee561227c2011-09-02 15:28:19 -0700465 case 2: // method->dex_cache_resolved_methods_->Get(method_idx)
466 loadWordDisp(cUnit, rLR, (dInsn->vB * 4) +
467 art::Array::DataOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700468 break;
buzbee561227c2011-09-02 15:28:19 -0700469 case 3: // Resolved?
470 skipBranch = genCmpImmBranch(cUnit, kArmCondNe, rLR, 0);
471 // Slowest path, bail to helper, rollback and retry
472 loadWordDisp(cUnit, rSELF,
473 OFFSETOF_MEMBER(Thread, pResolveMethodFromCode), rLR);
474 loadConstant(cUnit, r1, dInsn->vB);
475 newLIR1(cUnit, kThumbBlxR, rLR);
476 genUnconditionalBranch(cUnit, rollback);
477 // Resume normal slow path
478 skipTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
479 skipTarget->defMask = ENCODE_ALL;
480 skipBranch->generic.target = (LIR*)skipTarget;
481 // Get base_method->method_index [usr rLR, set r12]
482 loadBaseDisp(cUnit, mir, rLR,
483 Method::GetMethodIndexOffset().Int32Value(), r0,
484 kUnsignedHalf, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700485 // Load "this" [set r1]
486 rlArg = oatGetSrc(cUnit, mir, 0);
487 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee7b1b86d2011-08-26 18:59:10 -0700488 break;
489 case 4:
490 // Is "this" null? [use r1]
491 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir->offset, NULL);
492 // get this->clazz [use r1, set rLR]
buzbee561227c2011-09-02 15:28:19 -0700493 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700494 break;
buzbee561227c2011-09-02 15:28:19 -0700495 case 5:
496 // get this->klass_->vtable_ [usr rLR, set rLR]
497 loadWordDisp(cUnit, rLR, Class::VTableOffset().Int32Value(), rLR);
498 DCHECK((art::Array::DataOffset().Int32Value() & 0x3) == 0);
499 // In load shadow fold vtable_ object header size into method_index_
500 opRegImm(cUnit, kOpAdd, r0,
501 art::Array::DataOffset().Int32Value() / 4);
502 // Get target Method*
503 loadBaseIndexed(cUnit, rLR, r0, r0, 2, kWord);
504 break;
505 case 6: // Get the target compiled code address [uses r0, sets rLR]
506 loadWordDisp(cUnit, r0, Method::GetCodeOffset().Int32Value(), rLR);
buzbee7b1b86d2011-08-26 18:59:10 -0700507 break;
508 default:
509 return -1;
510 }
511 return state + 1;
512}
513
buzbee67bf8852011-08-17 17:51:35 -0700514/* Load up to 3 arguments in r1..r3 */
515static int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
516 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700517 int *args, NextCallInsn nextCallInsn, ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700518{
519 for (int i = 0; i < 3; i++) {
520 if (args[i] != INVALID_REG) {
521 RegLocation rlArg = oatGetSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700522 // Arguments are treated as a series of untyped 32-bit values.
523 rlArg.wide = false;
buzbee67bf8852011-08-17 17:51:35 -0700524 loadValueDirectFixed(cUnit, rlArg, r1 + i);
buzbee561227c2011-09-02 15:28:19 -0700525 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700526 }
527 }
528 return callState;
529}
530
531/*
532 * Interleave launch code for INVOKE_INTERFACE. The target is
533 * identified using artFindInterfaceMethodInCache(class, ref, method, dex)
534 * Note that we'll have to reload "this" following the helper call.
535 *
536 * FIXME: do we need to have artFindInterfaceMethodInCache return
537 * a NULL if not found so we can throw exception here? Otherwise,
538 * may need to pass some additional info to allow the helper function
539 * to throw on its own.
540 */
541static int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700542 DecodedInstruction* dInsn, int state,
543 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700544{
buzbee1b4c8592011-08-31 10:43:51 -0700545 UNIMPLEMENTED(FATAL) << "Need findInterfaceMethodInCache";
buzbeec143c552011-08-20 17:38:58 -0700546#if 0
buzbee67bf8852011-08-17 17:51:35 -0700547 RegLocation rlArg;
548 switch(state) {
549 case 0:
550 // Load "this" [set r12]
551 rlArg = oatGetSrc(cUnit, mir, 0);
552 loadValueDirectFixed(cUnit, rlArg, r12);
553 // Get the current Method* [set arg2]
buzbeedfd3d702011-08-28 12:56:51 -0700554 loadCurrMethodDirect(cUnit, r2);
buzbee67bf8852011-08-17 17:51:35 -0700555 // Is "this" null? [use r12]
556 genNullCheck(cUnit, oatSSASrc(mir,0), r12,
557 mir->offset, NULL);
558 // Get curMethod->clazz [set arg3]
559 loadBaseDisp(cUnit, mir, r2, OFFSETOF_MEMBER(Method, clazz),
560 r3, kWord, INVALID_SREG);
561 // Load this->class [usr r12, set arg0]
buzbeec143c552011-08-20 17:38:58 -0700562 loadBaseDisp(cUnit, mir, r12, OFFSETOF_MEMBER(Class, clazz),
buzbee67bf8852011-08-17 17:51:35 -0700563 r3, kWord, INVALID_SREG);
564 // Load address of helper function
565 loadBaseDisp(cUnit, mir, rSELF,
566 OFFSETOF_MEMBER(Thread, pArtFindInterfaceMethodInCache),
567 rLR, kWord, INVALID_SREG);
568 // Get dvmDex
buzbeec143c552011-08-20 17:38:58 -0700569 loadBaseDisp(cUnit, mir, r3, OFFSETOF_MEMBER(Class, pDvmDex),
buzbee67bf8852011-08-17 17:51:35 -0700570 r3, kWord, INVALID_SREG);
571 // Load ref [set arg1]
572 loadConstant(cUnit, r1, dInsn->vB);
573 // Call out to helper, target Method returned in ret0
574 newLIR1(cUnit, kThumbBlxR, rLR);
575 break;
576 case 1: // Get the target compiled code address [use r0, set rLR]
577 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, compiledInsns),
578 rLR, kWord, INVALID_SREG);
579 default:
580 return -1;
581 }
buzbeec143c552011-08-20 17:38:58 -0700582#endif
buzbee67bf8852011-08-17 17:51:35 -0700583 return state + 1;
584}
585
586
587/*
588 * Interleave launch code for INVOKE_SUPER. See comments
589 * for nextVCallIns.
590 */
591static int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
buzbee561227c2011-09-02 15:28:19 -0700592 DecodedInstruction* dInsn, int state,
593 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700594{
buzbee1b4c8592011-08-31 10:43:51 -0700595 UNIMPLEMENTED(FATAL) << "Need INVOKE_SUPER implementation";
buzbeec143c552011-08-20 17:38:58 -0700596#if 0
buzbee67bf8852011-08-17 17:51:35 -0700597 RegLocation rlArg;
598 switch(state) {
599 case 0:
600 // Get the current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700601 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700602 // Load "this" [set r1]
603 rlArg = oatGetSrc(cUnit, mir, 0);
604 loadValueDirectFixed(cUnit, rlArg, r1);
605 // Get method->clazz [use r0, set r12]
606 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, clazz),
607 r12, kWord, INVALID_SREG);
608 // Get pResmethods [use r0, set rLR]
609 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, pResMethods),
610 rLR, kWord, INVALID_SREG);
611 // Get clazz->super [use r12, set r12]
buzbeec143c552011-08-20 17:38:58 -0700612 loadBaseDisp(cUnit, mir, r12, OFFSETOF_MEMBER(Class, super),
buzbee67bf8852011-08-17 17:51:35 -0700613 r12, kWord, INVALID_SREG);
614 // Get base method [use rLR, set r0]
615 loadBaseDisp(cUnit, mir, rLR, dInsn->vB * 4, r0,
616 kWord, INVALID_SREG);
617 // Is "this" null? [use r1]
618 genNullCheck(cUnit, oatSSASrc(mir,0), r1,
619 mir->offset, NULL);
620 // Get methodIndex [use r0, set rLR]
621 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, methodIndex),
622 rLR, kUnsignedHalf, INVALID_SREG);
623 // Get vtableCount [use r12, set r0]
624 loadBaseDisp(cUnit, mir, r12,
buzbeec143c552011-08-20 17:38:58 -0700625 OFFSETOF_MEMBER(Class, vtableCount),
buzbee67bf8852011-08-17 17:51:35 -0700626 r0, kWord, INVALID_SREG);
627 // Compare method index w/ vtable count [use r12, use rLR]
628 genRegRegCheck(cUnit, kArmCondGe, rLR, r0, mir->offset, NULL);
629 // get target Method* [use rLR, use r12, set r0]
630 loadBaseIndexed(cUnit, r0, r12, rLR, 2, kWord);
631 case 1: // Get the target compiled code address [use r0, set rLR]
632 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, compiledInsns),
633 rLR, kWord, INVALID_SREG);
634 default:
635 return -1;
636 }
buzbeec143c552011-08-20 17:38:58 -0700637#endif
buzbee67bf8852011-08-17 17:51:35 -0700638 return state + 1;
639}
640
641/*
642 * Load up to 5 arguments, the first three of which will be in
643 * r1 .. r3. On entry r0 contains the current method pointer,
644 * and as part of the load sequence, it must be replaced with
645 * the target method pointer. Note, this may also be called
646 * for "range" variants if the number of arguments is 5 or fewer.
647 */
648static int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
649 DecodedInstruction* dInsn, int callState,
650 ArmLIR** pcrLabel, bool isRange,
buzbee561227c2011-09-02 15:28:19 -0700651 NextCallInsn nextCallInsn, ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700652{
653 RegLocation rlArg;
654 int registerArgs[3];
655
656 /* If no arguments, just return */
657 if (dInsn->vA == 0)
658 return callState;
659
buzbee2e748f32011-08-29 21:02:19 -0700660 oatLockCallTemps(cUnit);
buzbee561227c2011-09-02 15:28:19 -0700661 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700662
663 /*
664 * Load frame arguments arg4 & arg5 first. Coded a little odd to
665 * pre-schedule the method pointer target.
666 */
667 for (unsigned int i=3; i < dInsn->vA; i++) {
668 int reg;
669 int arg = (isRange) ? dInsn->vC + i : i;
670 rlArg = oatUpdateLoc(cUnit, oatGetSrc(cUnit, mir, arg));
671 if (rlArg.location == kLocPhysReg) {
672 reg = rlArg.lowReg;
673 } else {
674 reg = r1;
675 loadValueDirectFixed(cUnit, rlArg, r1);
buzbee561227c2011-09-02 15:28:19 -0700676 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700677 }
678 storeBaseDisp(cUnit, rSP, (i + 1) * 4, reg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700679 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700680 }
681
682 /* Load register arguments r1..r3 */
683 for (unsigned int i = 0; i < 3; i++) {
684 if (i < dInsn->vA)
685 registerArgs[i] = (isRange) ? dInsn->vC + i : i;
686 else
687 registerArgs[i] = INVALID_REG;
688 }
689 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
buzbee561227c2011-09-02 15:28:19 -0700690 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700691
692 // Load direct & need a "this" null check?
693 if (pcrLabel) {
694 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1,
695 mir->offset, NULL);
696 }
697 return callState;
698}
699
700/*
701 * May have 0+ arguments (also used for jumbo). Note that
702 * source virtual registers may be in physical registers, so may
703 * need to be flushed to home location before copying. This
704 * applies to arg3 and above (see below).
705 *
706 * Two general strategies:
707 * If < 20 arguments
708 * Pass args 3-18 using vldm/vstm block copy
709 * Pass arg0, arg1 & arg2 in r1-r3
710 * If 20+ arguments
711 * Pass args arg19+ using memcpy block copy
712 * Pass arg0, arg1 & arg2 in r1-r3
713 *
714 */
715static int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
716 DecodedInstruction* dInsn, int callState,
buzbee561227c2011-09-02 15:28:19 -0700717 ArmLIR** pcrLabel, NextCallInsn nextCallInsn,
718 ArmLIR* rollback)
buzbee67bf8852011-08-17 17:51:35 -0700719{
720 int firstArg = dInsn->vC;
721 int numArgs = dInsn->vA;
722
723 // If we can treat it as non-range (Jumbo ops will use range form)
724 if (numArgs <= 5)
725 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
buzbee561227c2011-09-02 15:28:19 -0700726 true, nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700727 /*
728 * Make sure range list doesn't span the break between in normal
729 * Dalvik vRegs and the ins.
730 */
buzbee1b4c8592011-08-31 10:43:51 -0700731 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700732 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700733 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
734 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700735 }
736
737 /*
738 * First load the non-register arguments. Both forms expect all
739 * of the source arguments to be in their home frame location, so
740 * scan the sReg names and flush any that have been promoted to
741 * frame backing storage.
742 */
743 // Scan the rest of the args - if in physReg flush to memory
744 for (int i = 4; i < numArgs; i++) {
buzbee1b4c8592011-08-31 10:43:51 -0700745 RegLocation loc = oatGetSrc(cUnit, mir, i);
746 //TODO: generic loc flushing routine
747 if (loc.wide) {
748 loc = oatUpdateLocWide(cUnit, loc);
749 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
750 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
751 loc.highReg);
buzbee561227c2011-09-02 15:28:19 -0700752 callState = nextCallInsn(cUnit, mir, dInsn, callState,
753 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700754 }
755 } else {
756 loc = oatUpdateLoc(cUnit, loc);
757 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
758 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
buzbee561227c2011-09-02 15:28:19 -0700759 callState = nextCallInsn(cUnit, mir, dInsn, callState,
760 rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700761 }
buzbee67bf8852011-08-17 17:51:35 -0700762 }
763 }
764
765 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
766 int outsOffset = 4 /* Method* */ + (3 * 4);
767 if (numArgs >= 20) {
768 // Generate memcpy, but first make sure all of
769 opRegRegImm(cUnit, kOpAdd, r0, rSP, startOffset);
770 opRegRegImm(cUnit, kOpAdd, r1, rSP, outsOffset);
771 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
772 loadConstant(cUnit, r2, (numArgs - 3) * 4);
773 newLIR1(cUnit, kThumbBlxR, rLR);
774 } else {
775 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700776 int regsLeft = std::min(numArgs - 3, 16);
buzbee561227c2011-09-02 15:28:19 -0700777 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700778 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbee1b4c8592011-08-31 10:43:51 -0700779 newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700780 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700781 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
buzbee561227c2011-09-02 15:28:19 -0700782 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee1b4c8592011-08-31 10:43:51 -0700783 newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
buzbee561227c2011-09-02 15:28:19 -0700784 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700785 }
786
787 // Handle the 1st 3 in r1, r2 & r3
788 for (unsigned int i = 0; i < dInsn->vA && i < 3; i++) {
789 RegLocation loc = oatGetSrc(cUnit, mir, firstArg + i);
790 loadValueDirectFixed(cUnit, loc, r1 + i);
buzbee561227c2011-09-02 15:28:19 -0700791 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700792 }
793
794 // Finally, deal with the register arguments
795 // We'll be using fixed registers here
buzbee2e748f32011-08-29 21:02:19 -0700796 oatLockCallTemps(cUnit);
buzbee561227c2011-09-02 15:28:19 -0700797 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700798 return callState;
799}
800
buzbee561227c2011-09-02 15:28:19 -0700801static void genInvokeStaticDirect(CompilationUnit* cUnit, MIR* mir,
802 bool direct, bool range)
buzbee67bf8852011-08-17 17:51:35 -0700803{
804 DecodedInstruction* dInsn = &mir->dalvikInsn;
805 int callState = 0;
806 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700807 ArmLIR** pNullCk = direct ? &nullCk : NULL;
buzbee7b1b86d2011-08-26 18:59:10 -0700808
buzbee561227c2011-09-02 15:28:19 -0700809 NextCallInsn nextCallInsn = nextSDCallInsn;
810
811 if (range) {
812 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
813 nextCallInsn, NULL);
814 } else {
815 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
816 false, nextCallInsn, NULL);
817 }
buzbee67bf8852011-08-17 17:51:35 -0700818 // Finish up any of the call sequence not interleaved in arg loading
819 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700820 callState = nextCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700821 }
822 newLIR1(cUnit, kThumbBlxR, rLR);
823}
824
825static void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
826{
827 DecodedInstruction* dInsn = &mir->dalvikInsn;
828 int callState = 0;
829 ArmLIR* nullCk;
830 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
buzbee561227c2011-09-02 15:28:19 -0700831 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700832 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
833 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee561227c2011-09-02 15:28:19 -0700834 false, nextInterfaceCallInsn, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700835 else
836 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee561227c2011-09-02 15:28:19 -0700837 nextInterfaceCallInsn, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700838 // Finish up any of the call sequence not interleaved in arg loading
839 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700840 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700841 }
842 newLIR1(cUnit, kThumbBlxR, rLR);
843}
844
845static void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
846{
847 DecodedInstruction* dInsn = &mir->dalvikInsn;
848 int callState = 0;
849 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700850// TODO - redundantly loading arg0/r1 ("this")
buzbee67bf8852011-08-17 17:51:35 -0700851 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
852 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee561227c2011-09-02 15:28:19 -0700853 false, nextSuperCallInsn, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700854 else
855 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee561227c2011-09-02 15:28:19 -0700856 nextSuperCallInsn, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700857 // Finish up any of the call sequence not interleaved in arg loading
858 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700859 callState = nextSuperCallInsn(cUnit, mir, dInsn, callState, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700860 }
861 newLIR1(cUnit, kThumbBlxR, rLR);
862}
863
864static void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
865{
866 DecodedInstruction* dInsn = &mir->dalvikInsn;
867 int callState = 0;
868 ArmLIR* nullCk;
buzbee561227c2011-09-02 15:28:19 -0700869 ArmLIR* rollback;
870 Method* method = cUnit->method->GetDexCacheResolvedMethods()->
871 Get(dInsn->vB);
872 NextCallInsn nextCallInsn;
buzbee7b1b86d2011-08-26 18:59:10 -0700873
buzbee561227c2011-09-02 15:28:19 -0700874 if (method == NULL) {
875 // Slow path
876 nextCallInsn = nextVCallInsnSP;
877 // If we need a slow-path callout, we'll restart here
878 rollback = newLIR0(cUnit, kArmPseudoTargetLabel);
879 rollback->defMask = -1;
880 } else {
881 // Fast path
882 nextCallInsn = nextVCallInsn;
883 rollback = NULL;
884 }
buzbee7b1b86d2011-08-26 18:59:10 -0700885 // TODO - redundantly loading arg0/r1 ("this")
buzbee67bf8852011-08-17 17:51:35 -0700886 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
887 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee561227c2011-09-02 15:28:19 -0700888 false, nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700889 else
890 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee561227c2011-09-02 15:28:19 -0700891 nextCallInsn, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700892 // Finish up any of the call sequence not interleaved in arg loading
893 while (callState >= 0) {
buzbee561227c2011-09-02 15:28:19 -0700894 callState = nextCallInsn(cUnit, mir, dInsn, callState, rollback);
buzbee67bf8852011-08-17 17:51:35 -0700895 }
896 newLIR1(cUnit, kThumbBlxR, rLR);
897}
898
899// TODO: break out the case handlers. Might make it easier to support x86
900static bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
901 BasicBlock* bb, ArmLIR* labelList)
902{
903 bool res = false; // Assume success
904 RegLocation rlSrc[3];
905 RegLocation rlDest = badLoc;
906 RegLocation rlResult = badLoc;
907 Opcode opcode = mir->dalvikInsn.opcode;
908
909 /* Prep Src and Dest locations */
910 int nextSreg = 0;
911 int nextLoc = 0;
912 int attrs = oatDataFlowAttributes[opcode];
913 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
914 if (attrs & DF_UA) {
915 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
916 nextSreg++;
917 } else if (attrs & DF_UA_WIDE) {
918 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
919 nextSreg + 1);
920 nextSreg+= 2;
921 }
922 if (attrs & DF_UB) {
923 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
924 nextSreg++;
925 } else if (attrs & DF_UB_WIDE) {
926 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
927 nextSreg + 1);
928 nextSreg+= 2;
929 }
930 if (attrs & DF_UC) {
931 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
932 } else if (attrs & DF_UC_WIDE) {
933 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
934 nextSreg + 1);
935 }
936 if (attrs & DF_DA) {
937 rlDest = oatGetDest(cUnit, mir, 0);
938 } else if (attrs & DF_DA_WIDE) {
939 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
940 }
941
942 switch(opcode) {
943 case OP_NOP:
944 break;
945
946 case OP_MOVE_EXCEPTION:
947 int exOffset;
948 int resetReg;
buzbeec143c552011-08-20 17:38:58 -0700949 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700950 resetReg = oatAllocTemp(cUnit);
951 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
952 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
953 loadConstant(cUnit, resetReg, 0);
954 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
955 storeValue(cUnit, rlDest, rlResult);
956 break;
957
958 case OP_RETURN_VOID:
959 break;
960
961 case OP_RETURN:
962 case OP_RETURN_OBJECT:
963 storeValue(cUnit, retLoc, rlSrc[0]);
964 break;
965
966 case OP_RETURN_WIDE:
967 rlDest = retLocWide;
968 rlDest.fp = rlSrc[0].fp;
969 storeValueWide(cUnit, rlDest, rlSrc[0]);
970 break;
971
972 case OP_MOVE_RESULT_WIDE:
973 if (mir->OptimizationFlags & MIR_INLINED)
974 break; // Nop - combined w/ previous invoke
975 /*
976 * Somewhat hacky here. Because we're now passing
977 * return values in registers, we have to let the
978 * register allocation utilities know that the return
979 * registers are live and may not be used for address
980 * formation in storeValueWide.
981 */
982 assert(retLocWide.lowReg == r0);
983 assert(retLocWide.lowReg == r1);
984 oatLockTemp(cUnit, retLocWide.lowReg);
985 oatLockTemp(cUnit, retLocWide.highReg);
986 storeValueWide(cUnit, rlDest, retLocWide);
987 oatFreeTemp(cUnit, retLocWide.lowReg);
988 oatFreeTemp(cUnit, retLocWide.highReg);
989 break;
990
991 case OP_MOVE_RESULT:
992 case OP_MOVE_RESULT_OBJECT:
993 if (mir->OptimizationFlags & MIR_INLINED)
994 break; // Nop - combined w/ previous invoke
995 /* See comment for OP_MOVE_RESULT_WIDE */
996 assert(retLoc.lowReg == r0);
997 oatLockTemp(cUnit, retLoc.lowReg);
998 storeValue(cUnit, rlDest, retLoc);
999 oatFreeTemp(cUnit, retLoc.lowReg);
1000 break;
1001
1002 case OP_MOVE:
1003 case OP_MOVE_OBJECT:
1004 case OP_MOVE_16:
1005 case OP_MOVE_OBJECT_16:
1006 case OP_MOVE_FROM16:
1007 case OP_MOVE_OBJECT_FROM16:
1008 storeValue(cUnit, rlDest, rlSrc[0]);
1009 break;
1010
1011 case OP_MOVE_WIDE:
1012 case OP_MOVE_WIDE_16:
1013 case OP_MOVE_WIDE_FROM16:
1014 storeValueWide(cUnit, rlDest, rlSrc[0]);
1015 break;
1016
1017 case OP_CONST:
1018 case OP_CONST_4:
1019 case OP_CONST_16:
1020 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1021 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1022 storeValue(cUnit, rlDest, rlResult);
1023 break;
1024
1025 case OP_CONST_HIGH16:
1026 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1027 loadConstantNoClobber(cUnit, rlResult.lowReg,
1028 mir->dalvikInsn.vB << 16);
1029 storeValue(cUnit, rlDest, rlResult);
1030 break;
1031
1032 case OP_CONST_WIDE_16:
1033 case OP_CONST_WIDE_32:
1034 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1035 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1036 //TUNING: do high separately to avoid load dependency
1037 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1038 storeValueWide(cUnit, rlDest, rlResult);
1039 break;
1040
1041 case OP_CONST_WIDE:
1042 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1043 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001044 mir->dalvikInsn.vB_wide & 0xffffffff,
1045 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001046 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001047 break;
1048
1049 case OP_CONST_WIDE_HIGH16:
1050 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1051 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1052 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001053 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001054 break;
1055
1056 case OP_MONITOR_ENTER:
1057 genMonitorEnter(cUnit, mir, rlSrc[0]);
1058 break;
1059
1060 case OP_MONITOR_EXIT:
1061 genMonitorExit(cUnit, mir, rlSrc[0]);
1062 break;
1063
1064 case OP_CHECK_CAST:
1065 genCheckCast(cUnit, mir, rlSrc[0]);
1066 break;
1067
1068 case OP_INSTANCE_OF:
1069 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1070 break;
1071
1072 case OP_NEW_INSTANCE:
1073 genNewInstance(cUnit, mir, rlDest);
1074 break;
1075
1076 case OP_THROW:
1077 genThrow(cUnit, mir, rlSrc[0]);
1078 break;
1079
1080 case OP_ARRAY_LENGTH:
1081 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001082 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001083 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee67bf8852011-08-17 17:51:35 -07001084 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg,
1085 mir->offset, NULL);
1086 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1087 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1088 rlResult.lowReg);
1089 storeValue(cUnit, rlDest, rlResult);
1090 break;
1091
1092 case OP_CONST_STRING:
1093 case OP_CONST_STRING_JUMBO:
1094 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1095 break;
1096
1097 case OP_CONST_CLASS:
1098 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1099 break;
1100
1101 case OP_FILL_ARRAY_DATA:
1102 genFillArrayData(cUnit, mir, rlSrc[0]);
1103 break;
1104
1105 case OP_FILLED_NEW_ARRAY:
1106 genFilledNewArray(cUnit, mir, false /* not range */);
1107 break;
1108
1109 case OP_FILLED_NEW_ARRAY_RANGE:
1110 genFilledNewArray(cUnit, mir, true /* range */);
1111 break;
1112
1113 case OP_NEW_ARRAY:
1114 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1115 break;
1116
1117 case OP_GOTO:
1118 case OP_GOTO_16:
1119 case OP_GOTO_32:
1120 // TUNING: add MIR flag to disable when unnecessary
1121 bool backwardBranch;
1122 backwardBranch = (bb->taken->startOffset <= mir->offset);
1123 if (backwardBranch) {
1124 genSuspendPoll(cUnit, mir);
1125 }
1126 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1127 break;
1128
1129 case OP_PACKED_SWITCH:
1130 genPackedSwitch(cUnit, mir, rlSrc[0]);
1131 break;
1132
1133 case OP_SPARSE_SWITCH:
1134 genSparseSwitch(cUnit, mir, rlSrc[0]);
1135 break;
1136
1137 case OP_CMPL_FLOAT:
1138 case OP_CMPG_FLOAT:
1139 case OP_CMPL_DOUBLE:
1140 case OP_CMPG_DOUBLE:
1141 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1142 break;
1143
1144 case OP_CMP_LONG:
1145 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1146 break;
1147
1148 case OP_IF_EQ:
1149 case OP_IF_NE:
1150 case OP_IF_LT:
1151 case OP_IF_GE:
1152 case OP_IF_GT:
1153 case OP_IF_LE: {
1154 bool backwardBranch;
1155 ArmConditionCode cond;
1156 backwardBranch = (bb->taken->startOffset <= mir->offset);
1157 if (backwardBranch) {
1158 genSuspendPoll(cUnit, mir);
1159 }
1160 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1161 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1162 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1163 switch(opcode) {
1164 case OP_IF_EQ:
1165 cond = kArmCondEq;
1166 break;
1167 case OP_IF_NE:
1168 cond = kArmCondNe;
1169 break;
1170 case OP_IF_LT:
1171 cond = kArmCondLt;
1172 break;
1173 case OP_IF_GE:
1174 cond = kArmCondGe;
1175 break;
1176 case OP_IF_GT:
1177 cond = kArmCondGt;
1178 break;
1179 case OP_IF_LE:
1180 cond = kArmCondLe;
1181 break;
1182 default:
1183 cond = (ArmConditionCode)0;
1184 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1185 }
1186 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1187 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1188 break;
1189 }
1190
1191 case OP_IF_EQZ:
1192 case OP_IF_NEZ:
1193 case OP_IF_LTZ:
1194 case OP_IF_GEZ:
1195 case OP_IF_GTZ:
1196 case OP_IF_LEZ: {
1197 bool backwardBranch;
1198 ArmConditionCode cond;
1199 backwardBranch = (bb->taken->startOffset <= mir->offset);
1200 if (backwardBranch) {
1201 genSuspendPoll(cUnit, mir);
1202 }
1203 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1204 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1205 switch(opcode) {
1206 case OP_IF_EQZ:
1207 cond = kArmCondEq;
1208 break;
1209 case OP_IF_NEZ:
1210 cond = kArmCondNe;
1211 break;
1212 case OP_IF_LTZ:
1213 cond = kArmCondLt;
1214 break;
1215 case OP_IF_GEZ:
1216 cond = kArmCondGe;
1217 break;
1218 case OP_IF_GTZ:
1219 cond = kArmCondGt;
1220 break;
1221 case OP_IF_LEZ:
1222 cond = kArmCondLe;
1223 break;
1224 default:
1225 cond = (ArmConditionCode)0;
1226 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1227 }
1228 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1229 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1230 break;
1231 }
1232
1233 case OP_AGET_WIDE:
1234 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1235 break;
1236 case OP_AGET:
1237 case OP_AGET_OBJECT:
1238 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1239 break;
1240 case OP_AGET_BOOLEAN:
1241 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1242 rlDest, 0);
1243 break;
1244 case OP_AGET_BYTE:
1245 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1246 break;
1247 case OP_AGET_CHAR:
1248 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1249 rlDest, 1);
1250 break;
1251 case OP_AGET_SHORT:
1252 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1253 break;
1254 case OP_APUT_WIDE:
1255 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1256 break;
1257 case OP_APUT:
1258 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1259 break;
1260 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001261 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001262 break;
1263 case OP_APUT_SHORT:
1264 case OP_APUT_CHAR:
1265 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1266 rlSrc[0], 1);
1267 break;
1268 case OP_APUT_BYTE:
1269 case OP_APUT_BOOLEAN:
1270 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1271 rlSrc[0], 0);
1272 break;
1273
1274 case OP_IGET_WIDE:
1275 case OP_IGET_WIDE_VOLATILE:
1276 genIGetWideX(cUnit, mir, rlDest, rlSrc[0]);
1277 break;
1278
1279 case OP_IGET:
1280 case OP_IGET_VOLATILE:
1281 case OP_IGET_OBJECT:
1282 case OP_IGET_OBJECT_VOLATILE:
1283 genIGetX(cUnit, mir, kWord, rlDest, rlSrc[0]);
1284 break;
1285
1286 case OP_IGET_BOOLEAN:
1287 case OP_IGET_BYTE:
1288 genIGetX(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
1289 break;
1290
1291 case OP_IGET_CHAR:
1292 genIGetX(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
1293 break;
1294
1295 case OP_IGET_SHORT:
1296 genIGetX(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
1297 break;
1298
1299 case OP_IPUT_WIDE:
1300 case OP_IPUT_WIDE_VOLATILE:
1301 genIPutWideX(cUnit, mir, rlSrc[0], rlSrc[1]);
1302 break;
1303
1304 case OP_IPUT_OBJECT:
1305 case OP_IPUT_OBJECT_VOLATILE:
1306 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
1307 break;
1308
1309 case OP_IPUT:
1310 case OP_IPUT_VOLATILE:
1311 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
1312 break;
1313
1314 case OP_IPUT_BOOLEAN:
1315 case OP_IPUT_BYTE:
1316 genIPutX(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
1317 break;
1318
1319 case OP_IPUT_CHAR:
1320 genIPutX(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
1321 break;
1322
1323 case OP_IPUT_SHORT:
1324 genIPutX(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
1325 break;
1326
1327 case OP_SGET:
1328 case OP_SGET_OBJECT:
1329 case OP_SGET_BOOLEAN:
1330 case OP_SGET_BYTE:
1331 case OP_SGET_CHAR:
1332 case OP_SGET_SHORT:
1333 genSget(cUnit, mir, rlResult, rlDest);
1334 break;
1335
1336 case OP_SGET_WIDE:
1337 genSgetWide(cUnit, mir, rlResult, rlDest);
1338 break;
1339
1340 case OP_SPUT:
1341 case OP_SPUT_OBJECT:
1342 case OP_SPUT_BOOLEAN:
1343 case OP_SPUT_BYTE:
1344 case OP_SPUT_CHAR:
1345 case OP_SPUT_SHORT:
1346 genSput(cUnit, mir, rlSrc[0]);
1347 break;
1348
1349 case OP_SPUT_WIDE:
1350 genSputWide(cUnit, mir, rlSrc[0]);
1351 break;
1352
1353 case OP_INVOKE_STATIC_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001354 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1355 true /*range*/);
1356 break;
buzbee67bf8852011-08-17 17:51:35 -07001357 case OP_INVOKE_STATIC:
buzbee561227c2011-09-02 15:28:19 -07001358 genInvokeStaticDirect(cUnit, mir, false /*direct*/,
1359 false /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001360 break;
1361
1362 case OP_INVOKE_DIRECT:
buzbee561227c2011-09-02 15:28:19 -07001363 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1364 false /*range*/);
1365 break;
buzbee67bf8852011-08-17 17:51:35 -07001366 case OP_INVOKE_DIRECT_RANGE:
buzbee561227c2011-09-02 15:28:19 -07001367 genInvokeStaticDirect(cUnit, mir, true /*direct*/,
1368 true /*range*/);
buzbee67bf8852011-08-17 17:51:35 -07001369 break;
1370
1371 case OP_INVOKE_VIRTUAL:
1372 case OP_INVOKE_VIRTUAL_RANGE:
1373 genInvokeVirtual(cUnit, mir);
1374 break;
1375
1376 case OP_INVOKE_SUPER:
1377 case OP_INVOKE_SUPER_RANGE:
1378 genInvokeSuper(cUnit, mir);
1379 break;
1380
1381 case OP_INVOKE_INTERFACE:
1382 case OP_INVOKE_INTERFACE_RANGE:
1383 genInvokeInterface(cUnit, mir);
1384 break;
1385
1386 case OP_NEG_INT:
1387 case OP_NOT_INT:
1388 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1389 break;
1390
1391 case OP_NEG_LONG:
1392 case OP_NOT_LONG:
1393 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1394 break;
1395
1396 case OP_NEG_FLOAT:
1397 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1398 break;
1399
1400 case OP_NEG_DOUBLE:
1401 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1402 break;
1403
1404 case OP_INT_TO_LONG:
1405 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1406 if (rlSrc[0].location == kLocPhysReg) {
1407 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1408 } else {
1409 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1410 }
1411 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1412 rlResult.lowReg, 31);
1413 storeValueWide(cUnit, rlDest, rlResult);
1414 break;
1415
1416 case OP_LONG_TO_INT:
1417 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1418 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1419 storeValue(cUnit, rlDest, rlSrc[0]);
1420 break;
1421
1422 case OP_INT_TO_BYTE:
1423 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1424 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1425 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1426 storeValue(cUnit, rlDest, rlResult);
1427 break;
1428
1429 case OP_INT_TO_SHORT:
1430 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1431 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1432 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1433 storeValue(cUnit, rlDest, rlResult);
1434 break;
1435
1436 case OP_INT_TO_CHAR:
1437 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1438 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1439 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1440 storeValue(cUnit, rlDest, rlResult);
1441 break;
1442
1443 case OP_INT_TO_FLOAT:
1444 case OP_INT_TO_DOUBLE:
1445 case OP_LONG_TO_FLOAT:
1446 case OP_LONG_TO_DOUBLE:
1447 case OP_FLOAT_TO_INT:
1448 case OP_FLOAT_TO_LONG:
1449 case OP_FLOAT_TO_DOUBLE:
1450 case OP_DOUBLE_TO_INT:
1451 case OP_DOUBLE_TO_LONG:
1452 case OP_DOUBLE_TO_FLOAT:
1453 genConversion(cUnit, mir);
1454 break;
1455
1456 case OP_ADD_INT:
1457 case OP_SUB_INT:
1458 case OP_MUL_INT:
1459 case OP_DIV_INT:
1460 case OP_REM_INT:
1461 case OP_AND_INT:
1462 case OP_OR_INT:
1463 case OP_XOR_INT:
1464 case OP_SHL_INT:
1465 case OP_SHR_INT:
1466 case OP_USHR_INT:
1467 case OP_ADD_INT_2ADDR:
1468 case OP_SUB_INT_2ADDR:
1469 case OP_MUL_INT_2ADDR:
1470 case OP_DIV_INT_2ADDR:
1471 case OP_REM_INT_2ADDR:
1472 case OP_AND_INT_2ADDR:
1473 case OP_OR_INT_2ADDR:
1474 case OP_XOR_INT_2ADDR:
1475 case OP_SHL_INT_2ADDR:
1476 case OP_SHR_INT_2ADDR:
1477 case OP_USHR_INT_2ADDR:
1478 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1479 break;
1480
1481 case OP_ADD_LONG:
1482 case OP_SUB_LONG:
1483 case OP_MUL_LONG:
1484 case OP_DIV_LONG:
1485 case OP_REM_LONG:
1486 case OP_AND_LONG:
1487 case OP_OR_LONG:
1488 case OP_XOR_LONG:
1489 case OP_ADD_LONG_2ADDR:
1490 case OP_SUB_LONG_2ADDR:
1491 case OP_MUL_LONG_2ADDR:
1492 case OP_DIV_LONG_2ADDR:
1493 case OP_REM_LONG_2ADDR:
1494 case OP_AND_LONG_2ADDR:
1495 case OP_OR_LONG_2ADDR:
1496 case OP_XOR_LONG_2ADDR:
1497 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1498 break;
1499
buzbee67bf8852011-08-17 17:51:35 -07001500 case OP_SHL_LONG:
1501 case OP_SHR_LONG:
1502 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001503 case OP_SHL_LONG_2ADDR:
1504 case OP_SHR_LONG_2ADDR:
1505 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001506 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1507 break;
1508
1509 case OP_ADD_FLOAT:
1510 case OP_SUB_FLOAT:
1511 case OP_MUL_FLOAT:
1512 case OP_DIV_FLOAT:
1513 case OP_REM_FLOAT:
1514 case OP_ADD_FLOAT_2ADDR:
1515 case OP_SUB_FLOAT_2ADDR:
1516 case OP_MUL_FLOAT_2ADDR:
1517 case OP_DIV_FLOAT_2ADDR:
1518 case OP_REM_FLOAT_2ADDR:
1519 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1520 break;
1521
1522 case OP_ADD_DOUBLE:
1523 case OP_SUB_DOUBLE:
1524 case OP_MUL_DOUBLE:
1525 case OP_DIV_DOUBLE:
1526 case OP_REM_DOUBLE:
1527 case OP_ADD_DOUBLE_2ADDR:
1528 case OP_SUB_DOUBLE_2ADDR:
1529 case OP_MUL_DOUBLE_2ADDR:
1530 case OP_DIV_DOUBLE_2ADDR:
1531 case OP_REM_DOUBLE_2ADDR:
1532 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1533 break;
1534
1535 case OP_RSUB_INT:
1536 case OP_ADD_INT_LIT16:
1537 case OP_MUL_INT_LIT16:
1538 case OP_DIV_INT_LIT16:
1539 case OP_REM_INT_LIT16:
1540 case OP_AND_INT_LIT16:
1541 case OP_OR_INT_LIT16:
1542 case OP_XOR_INT_LIT16:
1543 case OP_ADD_INT_LIT8:
1544 case OP_RSUB_INT_LIT8:
1545 case OP_MUL_INT_LIT8:
1546 case OP_DIV_INT_LIT8:
1547 case OP_REM_INT_LIT8:
1548 case OP_AND_INT_LIT8:
1549 case OP_OR_INT_LIT8:
1550 case OP_XOR_INT_LIT8:
1551 case OP_SHL_INT_LIT8:
1552 case OP_SHR_INT_LIT8:
1553 case OP_USHR_INT_LIT8:
1554 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1555 break;
1556
1557 default:
1558 res = true;
1559 }
1560 return res;
1561}
1562
1563static const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
1564 "kMirOpPhi",
1565 "kMirOpNullNRangeUpCheck",
1566 "kMirOpNullNRangeDownCheck",
1567 "kMirOpLowerBound",
1568 "kMirOpPunt",
1569 "kMirOpCheckInlinePrediction",
1570};
1571
1572/* Extended MIR instructions like PHI */
1573static void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
1574{
1575 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1576 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1577 strcpy(msg, extendedMIROpNames[opOffset]);
1578 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1579
1580 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1581 case kMirOpPhi: {
1582 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1583 op->flags.isNop = true;
1584 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1585 break;
1586 }
1587 default:
1588 break;
1589 }
1590}
1591
1592/* If there are any ins passed in registers that have not been promoted
1593 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001594 * Note: at this pointCopy any ins that are passed in register to their
1595 * home location */
buzbee67bf8852011-08-17 17:51:35 -07001596static void flushIns(CompilationUnit* cUnit)
1597{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001598 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001599 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001600 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1601 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001602 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001603 int startLoc = cUnit->method->NumRegisters() -
1604 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001605 for (int i = 0; i < inRegs; i++) {
1606 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001607 //TUNING: be smarter about flushing ins to frame
1608 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001609 if (loc.location == kLocPhysReg) {
1610 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001611 }
1612 }
1613
1614 // Handle special case of wide argument half in regs, half in frame
1615 if (inRegs == 3) {
1616 RegLocation loc = cUnit->regLocation[startLoc + 2];
1617 if (loc.wide && loc.location == kLocPhysReg) {
1618 // Load the other half of the arg into the promoted pair
buzbee561227c2011-09-02 15:28:19 -07001619 loadWordDisp(cUnit, rSP, loc.spOffset + 4, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -07001620 inRegs++;
1621 }
1622 }
1623
1624 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001625 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001626 RegLocation loc = cUnit->regLocation[startLoc + i];
1627 if (loc.fpLocation == kLocPhysReg) {
1628 loc.location = kLocPhysReg;
1629 loc.fp = true;
1630 loc.lowReg = loc.fpLowReg;
1631 loc.highReg = loc.fpHighReg;
1632 }
1633 if (loc.location == kLocPhysReg) {
1634 if (loc.wide) {
1635 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1636 loc.lowReg, loc.highReg, INVALID_SREG);
1637 i++;
1638 } else {
buzbee561227c2011-09-02 15:28:19 -07001639 loadWordDisp(cUnit, rSP, loc.spOffset, loc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001640 }
1641 }
1642 i++;
1643 }
1644}
1645
1646/* Handle the content in each basic block */
1647static bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
1648{
1649 MIR* mir;
1650 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1651 int blockId = bb->id;
1652
1653 cUnit->curBlock = bb;
1654 labelList[blockId].operands[0] = bb->startOffset;
1655
1656 /* Insert the block label */
1657 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1658 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1659
1660 oatClobberAllRegs(cUnit);
1661 oatResetNullCheck(cUnit);
1662
1663 ArmLIR* headLIR = NULL;
1664
1665 if (bb->blockType == kEntryBlock) {
1666 /*
1667 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1668 * mechanism know so it doesn't try to use any of them when
1669 * expanding the frame or flushing. This leaves the utility
1670 * code with a single temp: r12. This should be enough.
1671 */
1672 oatLockTemp(cUnit, r0);
1673 oatLockTemp(cUnit, r1);
1674 oatLockTemp(cUnit, r2);
1675 oatLockTemp(cUnit, r3);
1676 newLIR0(cUnit, kArmPseudoMethodEntry);
1677 /* Spill core callee saves */
1678 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1679 /* Need to spill any FP regs? */
1680 if (cUnit->numFPSpills) {
1681 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1682 }
1683 opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1684 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1685 flushIns(cUnit);
1686 oatFreeTemp(cUnit, r0);
1687 oatFreeTemp(cUnit, r1);
1688 oatFreeTemp(cUnit, r2);
1689 oatFreeTemp(cUnit, r3);
1690 } else if (bb->blockType == kExitBlock) {
1691 newLIR0(cUnit, kArmPseudoMethodExit);
1692 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1693 /* Need to restore any FP callee saves? */
1694 if (cUnit->numFPSpills) {
1695 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1696 }
1697 if (cUnit->coreSpillMask & (1 << rLR)) {
1698 /* Unspill rLR to rPC */
1699 cUnit->coreSpillMask &= ~(1 << rLR);
1700 cUnit->coreSpillMask |= (1 << rPC);
1701 }
1702 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1703 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1704 /* We didn't pop to rPC, so must do a bv rLR */
1705 newLIR1(cUnit, kThumbBx, rLR);
1706 }
1707 }
1708
1709 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1710
1711 oatResetRegPool(cUnit);
1712 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1713 oatClobberAllRegs(cUnit);
1714 }
1715
1716 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1717 oatResetDefTracking(cUnit);
1718 }
1719
1720 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1721 handleExtendedMethodMIR(cUnit, mir);
1722 continue;
1723 }
1724
1725 cUnit->currentDalvikOffset = mir->offset;
1726
1727 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1728 InstructionFormat dalvikFormat =
1729 dexGetFormatFromOpcode(dalvikOpcode);
1730
1731 ArmLIR* boundaryLIR;
1732
1733 /* Mark the beginning of a Dalvik instruction for line tracking */
1734 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1735 (int) oatGetDalvikDisassembly(
1736 &mir->dalvikInsn, ""));
1737 /* Remember the first LIR for this block */
1738 if (headLIR == NULL) {
1739 headLIR = boundaryLIR;
1740 /* Set the first boundaryLIR as a scheduling barrier */
1741 headLIR->defMask = ENCODE_ALL;
1742 }
1743
1744 /* Don't generate the SSA annotation unless verbose mode is on */
1745 if (cUnit->printMe && mir->ssaRep) {
1746 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1747 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1748 }
1749
1750 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1751
1752 if (notHandled) {
1753 char buf[100];
1754 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1755 mir->offset,
1756 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1757 dalvikFormat);
1758 LOG(FATAL) << buf;
1759 }
1760 }
1761
1762 if (headLIR) {
1763 /*
1764 * Eliminate redundant loads/stores and delay stores into later
1765 * slots
1766 */
1767 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1768 cUnit->lastLIRInsn);
1769
1770 /*
1771 * Generate an unconditional branch to the fallthrough block.
1772 */
1773 if (bb->fallThrough) {
1774 genUnconditionalBranch(cUnit,
1775 &labelList[bb->fallThrough->id]);
1776 }
1777 }
1778 return false;
1779}
1780
1781/*
1782 * Nop any unconditional branches that go to the next instruction.
1783 * Note: new redundant branches may be inserted later, and we'll
1784 * use a check in final instruction assembly to nop those out.
1785 */
1786void removeRedundantBranches(CompilationUnit* cUnit)
1787{
1788 ArmLIR* thisLIR;
1789
1790 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1791 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1792 thisLIR = NEXT_LIR(thisLIR)) {
1793
1794 /* Branch to the next instruction */
1795 if ((thisLIR->opcode == kThumbBUncond) ||
1796 (thisLIR->opcode == kThumb2BUncond)) {
1797 ArmLIR* nextLIR = thisLIR;
1798
1799 while (true) {
1800 nextLIR = NEXT_LIR(nextLIR);
1801
1802 /*
1803 * Is the branch target the next instruction?
1804 */
1805 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1806 thisLIR->flags.isNop = true;
1807 break;
1808 }
1809
1810 /*
1811 * Found real useful stuff between the branch and the target.
1812 * Need to explicitly check the lastLIRInsn here because it
1813 * might be the last real instruction.
1814 */
1815 if (!isPseudoOpcode(nextLIR->opcode) ||
1816 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1817 break;
1818 }
1819 }
1820 }
1821}
1822
1823void oatMethodMIR2LIR(CompilationUnit* cUnit)
1824{
1825 /* Used to hold the labels of each block */
1826 cUnit->blockLabelList =
1827 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
1828
1829 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
1830 kPreOrderDFSTraversal, false /* Iterative */);
1831 removeRedundantBranches(cUnit);
1832}
1833
1834/* Common initialization routine for an architecture family */
1835bool oatArchInit()
1836{
1837 int i;
1838
1839 for (i = 0; i < kArmLast; i++) {
1840 if (EncodingMap[i].opcode != i) {
1841 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
1842 " is wrong: expecting " << i << ", seeing " <<
1843 (int)EncodingMap[i].opcode;
1844 }
1845 }
1846
1847 return oatArchVariantInit();
1848}
1849
1850/* Needed by the Assembler */
1851void oatSetupResourceMasks(ArmLIR* lir)
1852{
1853 setupResourceMasks(lir);
1854}
1855
1856/* Needed by the ld/st optmizatons */
1857ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
1858{
1859 return genRegCopyNoInsert(cUnit, rDest, rSrc);
1860}
1861
1862/* Needed by the register allocator */
1863ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
1864{
1865 return genRegCopy(cUnit, rDest, rSrc);
1866}
1867
1868/* Needed by the register allocator */
1869void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
1870 int srcLo, int srcHi)
1871{
1872 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
1873}
1874
1875void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
1876 int displacement, int rSrc, OpSize size)
1877{
1878 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
1879}
1880
1881void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
1882 int displacement, int rSrcLo, int rSrcHi)
1883{
1884 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
1885}