blob: 8caf0d145a1c36a71a445285d5b68afaf7196541 [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
361typedef int (*NextCallInsn)(CompilationUnit*, MIR*, DecodedInstruction*, int);
362
363/*
364 * Bit of a hack here - in leiu of a real scheduling pass,
365 * emit the next instruction in static & direct invoke sequences.
366 */
367static int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
368 DecodedInstruction* dInsn, int state)
369{
buzbeec143c552011-08-20 17:38:58 -0700370 UNIMPLEMENTED(FATAL) << "Update with new cache model";
371#if 0
buzbee67bf8852011-08-17 17:51:35 -0700372 switch(state) {
373 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700374 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700375 break;
376 case 1: // Get the pResMethods pointer [uses r0, sets r0]
buzbeec143c552011-08-20 17:38:58 -0700377 UNIMPLEMENTED(FATAL) << "Update with new cache";
buzbee67bf8852011-08-17 17:51:35 -0700378 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, pResMethods),
379 r0, kWord, INVALID_SREG);
380 break;
381 case 2: // Get the target Method* [uses r0, sets r0]
382 loadBaseDisp(cUnit, mir, r0, dInsn->vB * 4, r0,
383 kWord, INVALID_SREG);
384 break;
385 case 3: // Get the target compiled code address [uses r0, sets rLR]
386 loadBaseDisp(cUnit, mir, r0,
387 OFFSETOF_MEMBER(Method, compiledInsns), rLR,
388 kWord, INVALID_SREG);
389 break;
390 default:
391 return -1;
392 }
buzbeec143c552011-08-20 17:38:58 -0700393#endif
buzbee67bf8852011-08-17 17:51:35 -0700394 return state + 1;
395}
396
buzbeec5ef0462011-08-25 18:44:49 -0700397// Slow path static & direct invoke launch sequence
398static int nextSDCallInsnSP(CompilationUnit* cUnit, MIR* mir,
399 DecodedInstruction* dInsn, int state)
400{
401 switch(state) {
402 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700403 loadCurrMethodDirect(cUnit, r0);
buzbeec5ef0462011-08-25 18:44:49 -0700404 break;
405 case 1: // Get the current Method->DeclaringClass() [sets r0]
406 loadBaseDisp(cUnit, mir, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407 art::Method::DeclaringClassOffset().Int32Value(),
buzbeec5ef0462011-08-25 18:44:49 -0700408 r0, kWord, INVALID_SREG);
409 break;
410 case 2: // Method->DeclaringClass()->GetDexCache() [sets r0]
411 loadBaseDisp(cUnit, mir, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700412 art::Class::DexCacheOffset().Int32Value(), r0, kWord,
buzbeec5ef0462011-08-25 18:44:49 -0700413 INVALID_SREG);
414 break;
415 case 3: // Method->DeclaringClass()->GetDexCache()->methodsObjectArr
buzbee5cd21802011-08-26 10:40:14 -0700416 loadBaseDisp(cUnit, mir, r0,
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700417 art::DexCache::ResolvedMethodsOffset().Int32Value(), r0,
buzbee5cd21802011-08-26 10:40:14 -0700418 kWord, INVALID_SREG);
buzbeec5ef0462011-08-25 18:44:49 -0700419 break;
420 case 4: // Skip past the object header
421 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
422 break;
423 case 5: // Get the target Method* [uses r0, sets r0]
424 loadBaseDisp(cUnit, mir, r0, dInsn->vB * 4, r0,
425 kWord, INVALID_SREG);
426 break;
427 case 6: // Get the target compiled code address [uses r0, sets rLR]
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 loadBaseDisp(cUnit, mir, r0,
429 art::Method::GetCodeOffset().Int32Value(), rLR,
buzbeec5ef0462011-08-25 18:44:49 -0700430 kWord, INVALID_SREG);
431 break;
432 default:
433 return -1;
434 }
435 return state + 1;
436}
437
buzbee67bf8852011-08-17 17:51:35 -0700438/*
439 * Bit of a hack here - in leiu of a real scheduling pass,
440 * emit the next instruction in a virtual invoke sequence.
441 * We can use rLR as a temp prior to target address loading
442 * Note also that we'll load the first argument ("this") into
443 * r1 here rather than the standard loadArgRegs.
444 */
445static int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
446 DecodedInstruction* dInsn, int state)
447{
buzbeec143c552011-08-20 17:38:58 -0700448 UNIMPLEMENTED(FATAL) << "Update with new cache model";
449#if 0
buzbee67bf8852011-08-17 17:51:35 -0700450 RegLocation rlArg;
451 switch(state) {
452 case 0: // Get the current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700453 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700454 // Load "this" [set r1]
455 rlArg = oatGetSrc(cUnit, mir, 0);
456 loadValueDirectFixed(cUnit, rlArg, r1);
457 break;
458 case 1: // Get the pResMethods pointer [use r0, set r12]
459 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, pResMethods),
460 r12, kWord, INVALID_SREG);
461 // Is "this" null? [use r1]
462 genNullCheck(cUnit, oatSSASrc(mir,0), r1,
463 mir->offset, NULL);
464 break;
465 case 2: // Get the base Method* [use r12, set r0]
466 loadBaseDisp(cUnit, mir, r12, dInsn->vB * 4, r0,
467 kWord, INVALID_SREG);
468 // get this->clazz [use r1, set rLR]
469 loadBaseDisp(cUnit, mir, r1, OFFSETOF_MEMBER(Object, clazz), rLR,
470 kWord, INVALID_SREG);
471 break;
472 case 3: // Get the method index [use r0, set r12]
473 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, methodIndex),
474 r12, kUnsignedHalf, INVALID_SREG);
475 // get this->clazz->vtable [use rLR, set rLR]
476 loadBaseDisp(cUnit, mir, rLR,
buzbeec143c552011-08-20 17:38:58 -0700477 OFFSETOF_MEMBER(Class, vtable), rLR, kWord,
buzbee67bf8852011-08-17 17:51:35 -0700478 INVALID_SREG);
479 break;
480 case 4: // get target Method* [use rLR, use r12, set r0]
481 loadBaseIndexed(cUnit, rLR, r12, r0, 2, kWord);
482 break;
483 case 5: // Get the target compiled code address [use r0, set rLR]
buzbeec143c552011-08-20 17:38:58 -0700484 UNIMPLEMENTED(FATAL) << "Update with new cache";
buzbee67bf8852011-08-17 17:51:35 -0700485 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, compiledInsns),
486 rLR, kWord, INVALID_SREG);
487 break;
488 default:
489 return -1;
490 }
buzbeec143c552011-08-20 17:38:58 -0700491#endif
buzbee67bf8852011-08-17 17:51:35 -0700492 return state + 1;
493}
494
buzbee7b1b86d2011-08-26 18:59:10 -0700495// Slow path sequence for virtual calls
496static int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir,
497 DecodedInstruction* dInsn, int state)
498{
499 RegLocation rlArg;
500 switch(state) {
501 case 0: // Get the current Method* [sets r0]
buzbeedfd3d702011-08-28 12:56:51 -0700502 loadCurrMethodDirect(cUnit, r0);
buzbee7b1b86d2011-08-26 18:59:10 -0700503 break;
504 case 1: // Get the current Method->DeclaringClass() [uses/sets r0]
505 loadBaseDisp(cUnit, mir, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700506 art::Method::DeclaringClassOffset().Int32Value(),
buzbee7b1b86d2011-08-26 18:59:10 -0700507 r0, kWord, INVALID_SREG);
508 break;
509 case 2: // Method->DeclaringClass()->GetDexCache() [uses/sets r0]
510 loadBaseDisp(cUnit, mir, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700511 art::Class::DexCacheOffset().Int32Value(),
512 r0, kWord,
buzbee7b1b86d2011-08-26 18:59:10 -0700513 INVALID_SREG);
514 break;
515 case 3: // ...()->GetDexCache()->methodsObjectArr [uses/sets r0]
516 loadBaseDisp(cUnit, mir, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700517 art::DexCache::ResolvedMethodsOffset().Int32Value(),
518 r0, kWord, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700519 // Load "this" [set r1]
520 rlArg = oatGetSrc(cUnit, mir, 0);
521 loadValueDirectFixed(cUnit, rlArg, r1);
522 // Skip past the object header
523 opRegImm(cUnit, kOpAdd, r0, art::Array::DataOffset().Int32Value());
524 break;
525 case 4:
526 // Is "this" null? [use r1]
527 genNullCheck(cUnit, oatSSASrc(mir,0), r1, mir->offset, NULL);
528 // get this->clazz [use r1, set rLR]
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700529 loadBaseDisp(cUnit, mir, r1, Object::ClassOffset().Int32Value(),
530 rLR, kWord, INVALID_SREG);
buzbee7b1b86d2011-08-26 18:59:10 -0700531 // Get the base Method* [uses r0, sets r0]
532 loadBaseDisp(cUnit, mir, r0, dInsn->vB * 4, r0,
533 kWord, INVALID_SREG);
534 // get this->clazz->vtable [use rLR, set rLR]
535 loadBaseDisp(cUnit, mir, rLR,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700536 Class::VTableOffset().Int32Value(), rLR, kWord,
buzbee7b1b86d2011-08-26 18:59:10 -0700537 INVALID_SREG);
538 // Get the method index [use r0, set r12]
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700539 loadBaseDisp(cUnit, mir, r0,
540 Method::MethodIndexOffset().Int32Value(),
buzbee7b1b86d2011-08-26 18:59:10 -0700541 r12, kUnsignedHalf, INVALID_SREG);
542 // Skip past the object header
543 opRegImm(cUnit, kOpAdd, rLR, art::Array::DataOffset().Int32Value());
544 // Get target Method*
545 loadBaseIndexed(cUnit, rLR, r12, r0, 2, kWord);
546 break;
547 case 5: // Get the target compiled code address [uses r0, sets rLR]
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700548 loadBaseDisp(cUnit, mir, r0,
549 art::Method::GetCodeOffset().Int32Value(), rLR,
buzbee7b1b86d2011-08-26 18:59:10 -0700550 kWord, INVALID_SREG);
551 break;
552 default:
553 return -1;
554 }
555 return state + 1;
556}
557
buzbee67bf8852011-08-17 17:51:35 -0700558/* Load up to 3 arguments in r1..r3 */
559static int loadArgRegs(CompilationUnit* cUnit, MIR* mir,
560 DecodedInstruction* dInsn, int callState,
561 int *args, NextCallInsn nextCallInsn)
562{
563 for (int i = 0; i < 3; i++) {
564 if (args[i] != INVALID_REG) {
565 RegLocation rlArg = oatGetSrc(cUnit, mir, i);
buzbee1b4c8592011-08-31 10:43:51 -0700566 // Arguments are treated as a series of untyped 32-bit values.
567 rlArg.wide = false;
buzbee67bf8852011-08-17 17:51:35 -0700568 loadValueDirectFixed(cUnit, rlArg, r1 + i);
569 callState = nextCallInsn(cUnit, mir, dInsn, callState);
570 }
571 }
572 return callState;
573}
574
575/*
576 * Interleave launch code for INVOKE_INTERFACE. The target is
577 * identified using artFindInterfaceMethodInCache(class, ref, method, dex)
578 * Note that we'll have to reload "this" following the helper call.
579 *
580 * FIXME: do we need to have artFindInterfaceMethodInCache return
581 * a NULL if not found so we can throw exception here? Otherwise,
582 * may need to pass some additional info to allow the helper function
583 * to throw on its own.
584 */
585static int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir,
586 DecodedInstruction* dInsn, int state)
587{
buzbee1b4c8592011-08-31 10:43:51 -0700588 UNIMPLEMENTED(FATAL) << "Need findInterfaceMethodInCache";
buzbeec143c552011-08-20 17:38:58 -0700589#if 0
buzbee67bf8852011-08-17 17:51:35 -0700590 RegLocation rlArg;
591 switch(state) {
592 case 0:
593 // Load "this" [set r12]
594 rlArg = oatGetSrc(cUnit, mir, 0);
595 loadValueDirectFixed(cUnit, rlArg, r12);
596 // Get the current Method* [set arg2]
buzbeedfd3d702011-08-28 12:56:51 -0700597 loadCurrMethodDirect(cUnit, r2);
buzbee67bf8852011-08-17 17:51:35 -0700598 // Is "this" null? [use r12]
599 genNullCheck(cUnit, oatSSASrc(mir,0), r12,
600 mir->offset, NULL);
601 // Get curMethod->clazz [set arg3]
602 loadBaseDisp(cUnit, mir, r2, OFFSETOF_MEMBER(Method, clazz),
603 r3, kWord, INVALID_SREG);
604 // Load this->class [usr r12, set arg0]
buzbeec143c552011-08-20 17:38:58 -0700605 loadBaseDisp(cUnit, mir, r12, OFFSETOF_MEMBER(Class, clazz),
buzbee67bf8852011-08-17 17:51:35 -0700606 r3, kWord, INVALID_SREG);
607 // Load address of helper function
608 loadBaseDisp(cUnit, mir, rSELF,
609 OFFSETOF_MEMBER(Thread, pArtFindInterfaceMethodInCache),
610 rLR, kWord, INVALID_SREG);
611 // Get dvmDex
buzbeec143c552011-08-20 17:38:58 -0700612 loadBaseDisp(cUnit, mir, r3, OFFSETOF_MEMBER(Class, pDvmDex),
buzbee67bf8852011-08-17 17:51:35 -0700613 r3, kWord, INVALID_SREG);
614 // Load ref [set arg1]
615 loadConstant(cUnit, r1, dInsn->vB);
616 // Call out to helper, target Method returned in ret0
617 newLIR1(cUnit, kThumbBlxR, rLR);
618 break;
619 case 1: // Get the target compiled code address [use r0, set rLR]
620 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, compiledInsns),
621 rLR, kWord, INVALID_SREG);
622 default:
623 return -1;
624 }
buzbeec143c552011-08-20 17:38:58 -0700625#endif
buzbee67bf8852011-08-17 17:51:35 -0700626 return state + 1;
627}
628
629
630/*
631 * Interleave launch code for INVOKE_SUPER. See comments
632 * for nextVCallIns.
633 */
634static int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
635 DecodedInstruction* dInsn, int state)
636{
buzbee1b4c8592011-08-31 10:43:51 -0700637 UNIMPLEMENTED(FATAL) << "Need INVOKE_SUPER implementation";
buzbeec143c552011-08-20 17:38:58 -0700638#if 0
buzbee67bf8852011-08-17 17:51:35 -0700639 RegLocation rlArg;
640 switch(state) {
641 case 0:
642 // Get the current Method* [set r0]
buzbeedfd3d702011-08-28 12:56:51 -0700643 loadCurrMethodDirect(cUnit, r0);
buzbee67bf8852011-08-17 17:51:35 -0700644 // Load "this" [set r1]
645 rlArg = oatGetSrc(cUnit, mir, 0);
646 loadValueDirectFixed(cUnit, rlArg, r1);
647 // Get method->clazz [use r0, set r12]
648 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, clazz),
649 r12, kWord, INVALID_SREG);
650 // Get pResmethods [use r0, set rLR]
651 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, pResMethods),
652 rLR, kWord, INVALID_SREG);
653 // Get clazz->super [use r12, set r12]
buzbeec143c552011-08-20 17:38:58 -0700654 loadBaseDisp(cUnit, mir, r12, OFFSETOF_MEMBER(Class, super),
buzbee67bf8852011-08-17 17:51:35 -0700655 r12, kWord, INVALID_SREG);
656 // Get base method [use rLR, set r0]
657 loadBaseDisp(cUnit, mir, rLR, dInsn->vB * 4, r0,
658 kWord, INVALID_SREG);
659 // Is "this" null? [use r1]
660 genNullCheck(cUnit, oatSSASrc(mir,0), r1,
661 mir->offset, NULL);
662 // Get methodIndex [use r0, set rLR]
663 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, methodIndex),
664 rLR, kUnsignedHalf, INVALID_SREG);
665 // Get vtableCount [use r12, set r0]
666 loadBaseDisp(cUnit, mir, r12,
buzbeec143c552011-08-20 17:38:58 -0700667 OFFSETOF_MEMBER(Class, vtableCount),
buzbee67bf8852011-08-17 17:51:35 -0700668 r0, kWord, INVALID_SREG);
669 // Compare method index w/ vtable count [use r12, use rLR]
670 genRegRegCheck(cUnit, kArmCondGe, rLR, r0, mir->offset, NULL);
671 // get target Method* [use rLR, use r12, set r0]
672 loadBaseIndexed(cUnit, r0, r12, rLR, 2, kWord);
673 case 1: // Get the target compiled code address [use r0, set rLR]
674 loadBaseDisp(cUnit, mir, r0, OFFSETOF_MEMBER(Method, compiledInsns),
675 rLR, kWord, INVALID_SREG);
676 default:
677 return -1;
678 }
buzbeec143c552011-08-20 17:38:58 -0700679#endif
buzbee67bf8852011-08-17 17:51:35 -0700680 return state + 1;
681}
682
683/*
684 * Load up to 5 arguments, the first three of which will be in
685 * r1 .. r3. On entry r0 contains the current method pointer,
686 * and as part of the load sequence, it must be replaced with
687 * the target method pointer. Note, this may also be called
688 * for "range" variants if the number of arguments is 5 or fewer.
689 */
690static int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
691 DecodedInstruction* dInsn, int callState,
692 ArmLIR** pcrLabel, bool isRange,
693 NextCallInsn nextCallInsn)
694{
695 RegLocation rlArg;
696 int registerArgs[3];
697
698 /* If no arguments, just return */
699 if (dInsn->vA == 0)
700 return callState;
701
buzbee2e748f32011-08-29 21:02:19 -0700702 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700703 callState = nextCallInsn(cUnit, mir, dInsn, callState);
704
705 /*
706 * Load frame arguments arg4 & arg5 first. Coded a little odd to
707 * pre-schedule the method pointer target.
708 */
709 for (unsigned int i=3; i < dInsn->vA; i++) {
710 int reg;
711 int arg = (isRange) ? dInsn->vC + i : i;
712 rlArg = oatUpdateLoc(cUnit, oatGetSrc(cUnit, mir, arg));
713 if (rlArg.location == kLocPhysReg) {
714 reg = rlArg.lowReg;
715 } else {
716 reg = r1;
717 loadValueDirectFixed(cUnit, rlArg, r1);
718 callState = nextCallInsn(cUnit, mir, dInsn, callState);
719 }
720 storeBaseDisp(cUnit, rSP, (i + 1) * 4, reg, kWord);
721 callState = nextCallInsn(cUnit, mir, dInsn, callState);
722 }
723
724 /* Load register arguments r1..r3 */
725 for (unsigned int i = 0; i < 3; i++) {
726 if (i < dInsn->vA)
727 registerArgs[i] = (isRange) ? dInsn->vC + i : i;
728 else
729 registerArgs[i] = INVALID_REG;
730 }
731 callState = loadArgRegs(cUnit, mir, dInsn, callState, registerArgs,
732 nextCallInsn);
733
734 // Load direct & need a "this" null check?
735 if (pcrLabel) {
736 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), r1,
737 mir->offset, NULL);
738 }
739 return callState;
740}
741
742/*
743 * May have 0+ arguments (also used for jumbo). Note that
744 * source virtual registers may be in physical registers, so may
745 * need to be flushed to home location before copying. This
746 * applies to arg3 and above (see below).
747 *
748 * Two general strategies:
749 * If < 20 arguments
750 * Pass args 3-18 using vldm/vstm block copy
751 * Pass arg0, arg1 & arg2 in r1-r3
752 * If 20+ arguments
753 * Pass args arg19+ using memcpy block copy
754 * Pass arg0, arg1 & arg2 in r1-r3
755 *
756 */
757static int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
758 DecodedInstruction* dInsn, int callState,
759 ArmLIR** pcrLabel, NextCallInsn nextCallInsn)
760{
761 int firstArg = dInsn->vC;
762 int numArgs = dInsn->vA;
763
764 // If we can treat it as non-range (Jumbo ops will use range form)
765 if (numArgs <= 5)
766 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
767 true, nextCallInsn);
768 /*
769 * Make sure range list doesn't span the break between in normal
770 * Dalvik vRegs and the ins.
771 */
buzbee1b4c8592011-08-31 10:43:51 -0700772 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700773 int boundaryReg = cUnit->method->NumRegisters() - cUnit->method->NumIns();
buzbee1b4c8592011-08-31 10:43:51 -0700774 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
775 LOG(FATAL) << "Argument list spanned locals & args";
buzbee67bf8852011-08-17 17:51:35 -0700776 }
777
778 /*
779 * First load the non-register arguments. Both forms expect all
780 * of the source arguments to be in their home frame location, so
781 * scan the sReg names and flush any that have been promoted to
782 * frame backing storage.
783 */
784 // Scan the rest of the args - if in physReg flush to memory
785 for (int i = 4; i < numArgs; i++) {
buzbee1b4c8592011-08-31 10:43:51 -0700786 RegLocation loc = oatGetSrc(cUnit, mir, i);
787 //TODO: generic loc flushing routine
788 if (loc.wide) {
789 loc = oatUpdateLocWide(cUnit, loc);
790 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
791 storeBaseDispWide(cUnit, rSP, loc.spOffset, loc.lowReg,
792 loc.highReg);
793 callState = nextCallInsn(cUnit, mir, dInsn, callState);
794 }
795 } else {
796 loc = oatUpdateLoc(cUnit, loc);
797 if (loc.location == kLocPhysReg) { // TUNING: if dirty?
798 storeBaseDisp(cUnit, rSP, loc.spOffset, loc.lowReg, kWord);
799 callState = nextCallInsn(cUnit, mir, dInsn, callState);
800 }
buzbee67bf8852011-08-17 17:51:35 -0700801 }
802 }
803
804 int startOffset = cUnit->regLocation[mir->ssaRep->uses[3]].spOffset;
805 int outsOffset = 4 /* Method* */ + (3 * 4);
806 if (numArgs >= 20) {
807 // Generate memcpy, but first make sure all of
808 opRegRegImm(cUnit, kOpAdd, r0, rSP, startOffset);
809 opRegRegImm(cUnit, kOpAdd, r1, rSP, outsOffset);
810 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pMemcpy), rLR);
811 loadConstant(cUnit, r2, (numArgs - 3) * 4);
812 newLIR1(cUnit, kThumbBlxR, rLR);
813 } else {
814 // Use vldm/vstm pair using r3 as a temp
buzbeec143c552011-08-20 17:38:58 -0700815 int regsLeft = std::min(numArgs - 3, 16);
buzbee67bf8852011-08-17 17:51:35 -0700816 callState = nextCallInsn(cUnit, mir, dInsn, callState);
817 opRegRegImm(cUnit, kOpAdd, r3, rSP, startOffset);
buzbee1b4c8592011-08-31 10:43:51 -0700818 newLIR3(cUnit, kThumb2Vldms, r3, fr0, regsLeft);
buzbee67bf8852011-08-17 17:51:35 -0700819 callState = nextCallInsn(cUnit, mir, dInsn, callState);
820 opRegRegImm(cUnit, kOpAdd, r3, rSP, 4 /* Method* */ + (3 * 4));
821 callState = nextCallInsn(cUnit, mir, dInsn, callState);
buzbee1b4c8592011-08-31 10:43:51 -0700822 newLIR3(cUnit, kThumb2Vstms, r3, fr0, regsLeft);
buzbee67bf8852011-08-17 17:51:35 -0700823 callState = nextCallInsn(cUnit, mir, dInsn, callState);
824 }
825
826 // Handle the 1st 3 in r1, r2 & r3
827 for (unsigned int i = 0; i < dInsn->vA && i < 3; i++) {
828 RegLocation loc = oatGetSrc(cUnit, mir, firstArg + i);
829 loadValueDirectFixed(cUnit, loc, r1 + i);
830 callState = nextCallInsn(cUnit, mir, dInsn, callState);
831 }
832
833 // Finally, deal with the register arguments
834 // We'll be using fixed registers here
buzbee2e748f32011-08-29 21:02:19 -0700835 oatLockCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700836 callState = nextCallInsn(cUnit, mir, dInsn, callState);
837 return callState;
838}
839
840static void genInvokeStatic(CompilationUnit* cUnit, MIR* mir)
841{
842 DecodedInstruction* dInsn = &mir->dalvikInsn;
843 int callState = 0;
buzbeec5ef0462011-08-25 18:44:49 -0700844 int fastPath = false; // TODO: set based on resolution results
845
846 NextCallInsn nextCallInsn = fastPath ? nextSDCallInsn : nextSDCallInsnSP;
847
buzbee67bf8852011-08-17 17:51:35 -0700848 if (mir->dalvikInsn.opcode == OP_INVOKE_STATIC) {
849 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, NULL,
buzbeec5ef0462011-08-25 18:44:49 -0700850 false, nextCallInsn);
buzbee67bf8852011-08-17 17:51:35 -0700851 } else {
852 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, NULL,
buzbeec5ef0462011-08-25 18:44:49 -0700853 nextCallInsn);
buzbee67bf8852011-08-17 17:51:35 -0700854 }
855 // Finish up any of the call sequence not interleaved in arg loading
856 while (callState >= 0) {
buzbeec5ef0462011-08-25 18:44:49 -0700857 callState = nextCallInsn(cUnit, mir, dInsn, callState);
buzbee67bf8852011-08-17 17:51:35 -0700858 }
859 newLIR1(cUnit, kThumbBlxR, rLR);
860}
861
862static void genInvokeDirect(CompilationUnit* cUnit, MIR* mir)
863{
864 DecodedInstruction* dInsn = &mir->dalvikInsn;
865 int callState = 0;
866 ArmLIR* nullCk;
buzbee7b1b86d2011-08-26 18:59:10 -0700867 int fastPath = false; // TODO: set based on resolution results
868
869 NextCallInsn nextCallInsn = fastPath ? nextSDCallInsn : nextSDCallInsnSP;
buzbee67bf8852011-08-17 17:51:35 -0700870 if (mir->dalvikInsn.opcode == OP_INVOKE_DIRECT)
871 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee7b1b86d2011-08-26 18:59:10 -0700872 false, nextCallInsn);
buzbee67bf8852011-08-17 17:51:35 -0700873 else
874 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee7b1b86d2011-08-26 18:59:10 -0700875 nextCallInsn);
buzbee67bf8852011-08-17 17:51:35 -0700876 // Finish up any of the call sequence not interleaved in arg loading
877 while (callState >= 0) {
buzbee7b1b86d2011-08-26 18:59:10 -0700878 callState = nextCallInsn(cUnit, mir, dInsn, callState);
buzbee67bf8852011-08-17 17:51:35 -0700879 }
880 newLIR1(cUnit, kThumbBlxR, rLR);
881}
882
883static void genInvokeInterface(CompilationUnit* cUnit, MIR* mir)
884{
885 DecodedInstruction* dInsn = &mir->dalvikInsn;
886 int callState = 0;
887 ArmLIR* nullCk;
888 /* Note: must call nextInterfaceCallInsn() prior to 1st argument load */
889 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState);
890 if (mir->dalvikInsn.opcode == OP_INVOKE_INTERFACE)
891 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
892 false, nextInterfaceCallInsn);
893 else
894 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
895 nextInterfaceCallInsn);
896 // Finish up any of the call sequence not interleaved in arg loading
897 while (callState >= 0) {
898 callState = nextInterfaceCallInsn(cUnit, mir, dInsn, callState);
899 }
900 newLIR1(cUnit, kThumbBlxR, rLR);
901}
902
903static void genInvokeSuper(CompilationUnit* cUnit, MIR* mir)
904{
905 DecodedInstruction* dInsn = &mir->dalvikInsn;
906 int callState = 0;
907 ArmLIR* nullCk;
908// FIXME - redundantly loading arg0/r1 ("this")
909 if (mir->dalvikInsn.opcode == OP_INVOKE_SUPER)
910 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
911 false, nextSuperCallInsn);
912 else
913 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
914 nextSuperCallInsn);
915 // Finish up any of the call sequence not interleaved in arg loading
916 while (callState >= 0) {
917 callState = nextSuperCallInsn(cUnit, mir, dInsn, callState);
918 }
919 newLIR1(cUnit, kThumbBlxR, rLR);
920}
921
922static void genInvokeVirtual(CompilationUnit* cUnit, MIR* mir)
923{
924 DecodedInstruction* dInsn = &mir->dalvikInsn;
925 int callState = 0;
926 ArmLIR* nullCk;
buzbee7b1b86d2011-08-26 18:59:10 -0700927 int fastPath = false; // TODO: set based on resolution results
928
929 NextCallInsn nextCallInsn = fastPath ? nextVCallInsn : nextVCallInsnSP;
930 // TODO - redundantly loading arg0/r1 ("this")
buzbee67bf8852011-08-17 17:51:35 -0700931 if (mir->dalvikInsn.opcode == OP_INVOKE_VIRTUAL)
932 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee7b1b86d2011-08-26 18:59:10 -0700933 false, nextCallInsn);
buzbee67bf8852011-08-17 17:51:35 -0700934 else
935 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, &nullCk,
buzbee7b1b86d2011-08-26 18:59:10 -0700936 nextCallInsn);
buzbee67bf8852011-08-17 17:51:35 -0700937 // Finish up any of the call sequence not interleaved in arg loading
938 while (callState >= 0) {
buzbee7b1b86d2011-08-26 18:59:10 -0700939 callState = nextCallInsn(cUnit, mir, dInsn, callState);
buzbee67bf8852011-08-17 17:51:35 -0700940 }
941 newLIR1(cUnit, kThumbBlxR, rLR);
942}
943
944// TODO: break out the case handlers. Might make it easier to support x86
945static bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
946 BasicBlock* bb, ArmLIR* labelList)
947{
948 bool res = false; // Assume success
949 RegLocation rlSrc[3];
950 RegLocation rlDest = badLoc;
951 RegLocation rlResult = badLoc;
952 Opcode opcode = mir->dalvikInsn.opcode;
953
954 /* Prep Src and Dest locations */
955 int nextSreg = 0;
956 int nextLoc = 0;
957 int attrs = oatDataFlowAttributes[opcode];
958 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
959 if (attrs & DF_UA) {
960 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
961 nextSreg++;
962 } else if (attrs & DF_UA_WIDE) {
963 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
964 nextSreg + 1);
965 nextSreg+= 2;
966 }
967 if (attrs & DF_UB) {
968 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
969 nextSreg++;
970 } else if (attrs & DF_UB_WIDE) {
971 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
972 nextSreg + 1);
973 nextSreg+= 2;
974 }
975 if (attrs & DF_UC) {
976 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
977 } else if (attrs & DF_UC_WIDE) {
978 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
979 nextSreg + 1);
980 }
981 if (attrs & DF_DA) {
982 rlDest = oatGetDest(cUnit, mir, 0);
983 } else if (attrs & DF_DA_WIDE) {
984 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
985 }
986
987 switch(opcode) {
988 case OP_NOP:
989 break;
990
991 case OP_MOVE_EXCEPTION:
992 int exOffset;
993 int resetReg;
buzbeec143c552011-08-20 17:38:58 -0700994 exOffset = Thread::ExceptionOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700995 resetReg = oatAllocTemp(cUnit);
996 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
997 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
998 loadConstant(cUnit, resetReg, 0);
999 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
1000 storeValue(cUnit, rlDest, rlResult);
1001 break;
1002
1003 case OP_RETURN_VOID:
1004 break;
1005
1006 case OP_RETURN:
1007 case OP_RETURN_OBJECT:
1008 storeValue(cUnit, retLoc, rlSrc[0]);
1009 break;
1010
1011 case OP_RETURN_WIDE:
1012 rlDest = retLocWide;
1013 rlDest.fp = rlSrc[0].fp;
1014 storeValueWide(cUnit, rlDest, rlSrc[0]);
1015 break;
1016
1017 case OP_MOVE_RESULT_WIDE:
1018 if (mir->OptimizationFlags & MIR_INLINED)
1019 break; // Nop - combined w/ previous invoke
1020 /*
1021 * Somewhat hacky here. Because we're now passing
1022 * return values in registers, we have to let the
1023 * register allocation utilities know that the return
1024 * registers are live and may not be used for address
1025 * formation in storeValueWide.
1026 */
1027 assert(retLocWide.lowReg == r0);
1028 assert(retLocWide.lowReg == r1);
1029 oatLockTemp(cUnit, retLocWide.lowReg);
1030 oatLockTemp(cUnit, retLocWide.highReg);
1031 storeValueWide(cUnit, rlDest, retLocWide);
1032 oatFreeTemp(cUnit, retLocWide.lowReg);
1033 oatFreeTemp(cUnit, retLocWide.highReg);
1034 break;
1035
1036 case OP_MOVE_RESULT:
1037 case OP_MOVE_RESULT_OBJECT:
1038 if (mir->OptimizationFlags & MIR_INLINED)
1039 break; // Nop - combined w/ previous invoke
1040 /* See comment for OP_MOVE_RESULT_WIDE */
1041 assert(retLoc.lowReg == r0);
1042 oatLockTemp(cUnit, retLoc.lowReg);
1043 storeValue(cUnit, rlDest, retLoc);
1044 oatFreeTemp(cUnit, retLoc.lowReg);
1045 break;
1046
1047 case OP_MOVE:
1048 case OP_MOVE_OBJECT:
1049 case OP_MOVE_16:
1050 case OP_MOVE_OBJECT_16:
1051 case OP_MOVE_FROM16:
1052 case OP_MOVE_OBJECT_FROM16:
1053 storeValue(cUnit, rlDest, rlSrc[0]);
1054 break;
1055
1056 case OP_MOVE_WIDE:
1057 case OP_MOVE_WIDE_16:
1058 case OP_MOVE_WIDE_FROM16:
1059 storeValueWide(cUnit, rlDest, rlSrc[0]);
1060 break;
1061
1062 case OP_CONST:
1063 case OP_CONST_4:
1064 case OP_CONST_16:
1065 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1066 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1067 storeValue(cUnit, rlDest, rlResult);
1068 break;
1069
1070 case OP_CONST_HIGH16:
1071 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1072 loadConstantNoClobber(cUnit, rlResult.lowReg,
1073 mir->dalvikInsn.vB << 16);
1074 storeValue(cUnit, rlDest, rlResult);
1075 break;
1076
1077 case OP_CONST_WIDE_16:
1078 case OP_CONST_WIDE_32:
1079 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1080 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1081 //TUNING: do high separately to avoid load dependency
1082 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
1083 storeValueWide(cUnit, rlDest, rlResult);
1084 break;
1085
1086 case OP_CONST_WIDE:
1087 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1088 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
buzbee54330722011-08-23 16:46:55 -07001089 mir->dalvikInsn.vB_wide & 0xffffffff,
1090 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee3ea4ec52011-08-22 17:37:19 -07001091 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001092 break;
1093
1094 case OP_CONST_WIDE_HIGH16:
1095 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
1096 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1097 0, mir->dalvikInsn.vB << 16);
buzbee7b1b86d2011-08-26 18:59:10 -07001098 storeValueWide(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -07001099 break;
1100
1101 case OP_MONITOR_ENTER:
1102 genMonitorEnter(cUnit, mir, rlSrc[0]);
1103 break;
1104
1105 case OP_MONITOR_EXIT:
1106 genMonitorExit(cUnit, mir, rlSrc[0]);
1107 break;
1108
1109 case OP_CHECK_CAST:
1110 genCheckCast(cUnit, mir, rlSrc[0]);
1111 break;
1112
1113 case OP_INSTANCE_OF:
1114 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
1115 break;
1116
1117 case OP_NEW_INSTANCE:
1118 genNewInstance(cUnit, mir, rlDest);
1119 break;
1120
1121 case OP_THROW:
1122 genThrow(cUnit, mir, rlSrc[0]);
1123 break;
1124
1125 case OP_ARRAY_LENGTH:
1126 int lenOffset;
buzbeec143c552011-08-20 17:38:58 -07001127 lenOffset = Array::LengthOffset().Int32Value();
buzbee7b1b86d2011-08-26 18:59:10 -07001128 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
buzbee67bf8852011-08-17 17:51:35 -07001129 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg,
1130 mir->offset, NULL);
1131 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1132 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
1133 rlResult.lowReg);
1134 storeValue(cUnit, rlDest, rlResult);
1135 break;
1136
1137 case OP_CONST_STRING:
1138 case OP_CONST_STRING_JUMBO:
1139 genConstString(cUnit, mir, rlDest, rlSrc[0]);
1140 break;
1141
1142 case OP_CONST_CLASS:
1143 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
1144 break;
1145
1146 case OP_FILL_ARRAY_DATA:
1147 genFillArrayData(cUnit, mir, rlSrc[0]);
1148 break;
1149
1150 case OP_FILLED_NEW_ARRAY:
1151 genFilledNewArray(cUnit, mir, false /* not range */);
1152 break;
1153
1154 case OP_FILLED_NEW_ARRAY_RANGE:
1155 genFilledNewArray(cUnit, mir, true /* range */);
1156 break;
1157
1158 case OP_NEW_ARRAY:
1159 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
1160 break;
1161
1162 case OP_GOTO:
1163 case OP_GOTO_16:
1164 case OP_GOTO_32:
1165 // TUNING: add MIR flag to disable when unnecessary
1166 bool backwardBranch;
1167 backwardBranch = (bb->taken->startOffset <= mir->offset);
1168 if (backwardBranch) {
1169 genSuspendPoll(cUnit, mir);
1170 }
1171 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1172 break;
1173
1174 case OP_PACKED_SWITCH:
1175 genPackedSwitch(cUnit, mir, rlSrc[0]);
1176 break;
1177
1178 case OP_SPARSE_SWITCH:
1179 genSparseSwitch(cUnit, mir, rlSrc[0]);
1180 break;
1181
1182 case OP_CMPL_FLOAT:
1183 case OP_CMPG_FLOAT:
1184 case OP_CMPL_DOUBLE:
1185 case OP_CMPG_DOUBLE:
1186 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1187 break;
1188
1189 case OP_CMP_LONG:
1190 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1191 break;
1192
1193 case OP_IF_EQ:
1194 case OP_IF_NE:
1195 case OP_IF_LT:
1196 case OP_IF_GE:
1197 case OP_IF_GT:
1198 case OP_IF_LE: {
1199 bool backwardBranch;
1200 ArmConditionCode cond;
1201 backwardBranch = (bb->taken->startOffset <= mir->offset);
1202 if (backwardBranch) {
1203 genSuspendPoll(cUnit, mir);
1204 }
1205 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1206 rlSrc[1] = loadValue(cUnit, rlSrc[1], kCoreReg);
1207 opRegReg(cUnit, kOpCmp, rlSrc[0].lowReg, rlSrc[1].lowReg);
1208 switch(opcode) {
1209 case OP_IF_EQ:
1210 cond = kArmCondEq;
1211 break;
1212 case OP_IF_NE:
1213 cond = kArmCondNe;
1214 break;
1215 case OP_IF_LT:
1216 cond = kArmCondLt;
1217 break;
1218 case OP_IF_GE:
1219 cond = kArmCondGe;
1220 break;
1221 case OP_IF_GT:
1222 cond = kArmCondGt;
1223 break;
1224 case OP_IF_LE:
1225 cond = kArmCondLe;
1226 break;
1227 default:
1228 cond = (ArmConditionCode)0;
1229 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1230 }
1231 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1232 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1233 break;
1234 }
1235
1236 case OP_IF_EQZ:
1237 case OP_IF_NEZ:
1238 case OP_IF_LTZ:
1239 case OP_IF_GEZ:
1240 case OP_IF_GTZ:
1241 case OP_IF_LEZ: {
1242 bool backwardBranch;
1243 ArmConditionCode cond;
1244 backwardBranch = (bb->taken->startOffset <= mir->offset);
1245 if (backwardBranch) {
1246 genSuspendPoll(cUnit, mir);
1247 }
1248 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1249 opRegImm(cUnit, kOpCmp, rlSrc[0].lowReg, 0);
1250 switch(opcode) {
1251 case OP_IF_EQZ:
1252 cond = kArmCondEq;
1253 break;
1254 case OP_IF_NEZ:
1255 cond = kArmCondNe;
1256 break;
1257 case OP_IF_LTZ:
1258 cond = kArmCondLt;
1259 break;
1260 case OP_IF_GEZ:
1261 cond = kArmCondGe;
1262 break;
1263 case OP_IF_GTZ:
1264 cond = kArmCondGt;
1265 break;
1266 case OP_IF_LEZ:
1267 cond = kArmCondLe;
1268 break;
1269 default:
1270 cond = (ArmConditionCode)0;
1271 LOG(FATAL) << "Unexpected opcode " << (int)opcode;
1272 }
1273 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
1274 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
1275 break;
1276 }
1277
1278 case OP_AGET_WIDE:
1279 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
1280 break;
1281 case OP_AGET:
1282 case OP_AGET_OBJECT:
1283 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
1284 break;
1285 case OP_AGET_BOOLEAN:
1286 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
1287 rlDest, 0);
1288 break;
1289 case OP_AGET_BYTE:
1290 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
1291 break;
1292 case OP_AGET_CHAR:
1293 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
1294 rlDest, 1);
1295 break;
1296 case OP_AGET_SHORT:
1297 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
1298 break;
1299 case OP_APUT_WIDE:
1300 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
1301 break;
1302 case OP_APUT:
1303 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
1304 break;
1305 case OP_APUT_OBJECT:
buzbee1b4c8592011-08-31 10:43:51 -07001306 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
buzbee67bf8852011-08-17 17:51:35 -07001307 break;
1308 case OP_APUT_SHORT:
1309 case OP_APUT_CHAR:
1310 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
1311 rlSrc[0], 1);
1312 break;
1313 case OP_APUT_BYTE:
1314 case OP_APUT_BOOLEAN:
1315 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
1316 rlSrc[0], 0);
1317 break;
1318
1319 case OP_IGET_WIDE:
1320 case OP_IGET_WIDE_VOLATILE:
1321 genIGetWideX(cUnit, mir, rlDest, rlSrc[0]);
1322 break;
1323
1324 case OP_IGET:
1325 case OP_IGET_VOLATILE:
1326 case OP_IGET_OBJECT:
1327 case OP_IGET_OBJECT_VOLATILE:
1328 genIGetX(cUnit, mir, kWord, rlDest, rlSrc[0]);
1329 break;
1330
1331 case OP_IGET_BOOLEAN:
1332 case OP_IGET_BYTE:
1333 genIGetX(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0]);
1334 break;
1335
1336 case OP_IGET_CHAR:
1337 genIGetX(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0]);
1338 break;
1339
1340 case OP_IGET_SHORT:
1341 genIGetX(cUnit, mir, kSignedHalf, rlDest, rlSrc[0]);
1342 break;
1343
1344 case OP_IPUT_WIDE:
1345 case OP_IPUT_WIDE_VOLATILE:
1346 genIPutWideX(cUnit, mir, rlSrc[0], rlSrc[1]);
1347 break;
1348
1349 case OP_IPUT_OBJECT:
1350 case OP_IPUT_OBJECT_VOLATILE:
1351 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], true);
1352 break;
1353
1354 case OP_IPUT:
1355 case OP_IPUT_VOLATILE:
1356 genIPutX(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false);
1357 break;
1358
1359 case OP_IPUT_BOOLEAN:
1360 case OP_IPUT_BYTE:
1361 genIPutX(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false);
1362 break;
1363
1364 case OP_IPUT_CHAR:
1365 genIPutX(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false);
1366 break;
1367
1368 case OP_IPUT_SHORT:
1369 genIPutX(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false);
1370 break;
1371
1372 case OP_SGET:
1373 case OP_SGET_OBJECT:
1374 case OP_SGET_BOOLEAN:
1375 case OP_SGET_BYTE:
1376 case OP_SGET_CHAR:
1377 case OP_SGET_SHORT:
1378 genSget(cUnit, mir, rlResult, rlDest);
1379 break;
1380
1381 case OP_SGET_WIDE:
1382 genSgetWide(cUnit, mir, rlResult, rlDest);
1383 break;
1384
1385 case OP_SPUT:
1386 case OP_SPUT_OBJECT:
1387 case OP_SPUT_BOOLEAN:
1388 case OP_SPUT_BYTE:
1389 case OP_SPUT_CHAR:
1390 case OP_SPUT_SHORT:
1391 genSput(cUnit, mir, rlSrc[0]);
1392 break;
1393
1394 case OP_SPUT_WIDE:
1395 genSputWide(cUnit, mir, rlSrc[0]);
1396 break;
1397
1398 case OP_INVOKE_STATIC_RANGE:
1399 case OP_INVOKE_STATIC:
1400 genInvokeStatic(cUnit, mir);
1401 break;
1402
1403 case OP_INVOKE_DIRECT:
1404 case OP_INVOKE_DIRECT_RANGE:
1405 genInvokeDirect(cUnit, mir);
1406 break;
1407
1408 case OP_INVOKE_VIRTUAL:
1409 case OP_INVOKE_VIRTUAL_RANGE:
1410 genInvokeVirtual(cUnit, mir);
1411 break;
1412
1413 case OP_INVOKE_SUPER:
1414 case OP_INVOKE_SUPER_RANGE:
1415 genInvokeSuper(cUnit, mir);
1416 break;
1417
1418 case OP_INVOKE_INTERFACE:
1419 case OP_INVOKE_INTERFACE_RANGE:
1420 genInvokeInterface(cUnit, mir);
1421 break;
1422
1423 case OP_NEG_INT:
1424 case OP_NOT_INT:
1425 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1426 break;
1427
1428 case OP_NEG_LONG:
1429 case OP_NOT_LONG:
1430 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1431 break;
1432
1433 case OP_NEG_FLOAT:
1434 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1435 break;
1436
1437 case OP_NEG_DOUBLE:
1438 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
1439 break;
1440
1441 case OP_INT_TO_LONG:
1442 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1443 if (rlSrc[0].location == kLocPhysReg) {
1444 genRegCopy(cUnit, rlResult.lowReg, rlSrc[0].lowReg);
1445 } else {
1446 loadValueDirect(cUnit, rlSrc[0], rlResult.lowReg);
1447 }
1448 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1449 rlResult.lowReg, 31);
1450 storeValueWide(cUnit, rlDest, rlResult);
1451 break;
1452
1453 case OP_LONG_TO_INT:
1454 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
1455 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
1456 storeValue(cUnit, rlDest, rlSrc[0]);
1457 break;
1458
1459 case OP_INT_TO_BYTE:
1460 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1461 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1462 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc[0].lowReg);
1463 storeValue(cUnit, rlDest, rlResult);
1464 break;
1465
1466 case OP_INT_TO_SHORT:
1467 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1468 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1469 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc[0].lowReg);
1470 storeValue(cUnit, rlDest, rlResult);
1471 break;
1472
1473 case OP_INT_TO_CHAR:
1474 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
1475 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1476 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc[0].lowReg);
1477 storeValue(cUnit, rlDest, rlResult);
1478 break;
1479
1480 case OP_INT_TO_FLOAT:
1481 case OP_INT_TO_DOUBLE:
1482 case OP_LONG_TO_FLOAT:
1483 case OP_LONG_TO_DOUBLE:
1484 case OP_FLOAT_TO_INT:
1485 case OP_FLOAT_TO_LONG:
1486 case OP_FLOAT_TO_DOUBLE:
1487 case OP_DOUBLE_TO_INT:
1488 case OP_DOUBLE_TO_LONG:
1489 case OP_DOUBLE_TO_FLOAT:
1490 genConversion(cUnit, mir);
1491 break;
1492
1493 case OP_ADD_INT:
1494 case OP_SUB_INT:
1495 case OP_MUL_INT:
1496 case OP_DIV_INT:
1497 case OP_REM_INT:
1498 case OP_AND_INT:
1499 case OP_OR_INT:
1500 case OP_XOR_INT:
1501 case OP_SHL_INT:
1502 case OP_SHR_INT:
1503 case OP_USHR_INT:
1504 case OP_ADD_INT_2ADDR:
1505 case OP_SUB_INT_2ADDR:
1506 case OP_MUL_INT_2ADDR:
1507 case OP_DIV_INT_2ADDR:
1508 case OP_REM_INT_2ADDR:
1509 case OP_AND_INT_2ADDR:
1510 case OP_OR_INT_2ADDR:
1511 case OP_XOR_INT_2ADDR:
1512 case OP_SHL_INT_2ADDR:
1513 case OP_SHR_INT_2ADDR:
1514 case OP_USHR_INT_2ADDR:
1515 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1516 break;
1517
1518 case OP_ADD_LONG:
1519 case OP_SUB_LONG:
1520 case OP_MUL_LONG:
1521 case OP_DIV_LONG:
1522 case OP_REM_LONG:
1523 case OP_AND_LONG:
1524 case OP_OR_LONG:
1525 case OP_XOR_LONG:
1526 case OP_ADD_LONG_2ADDR:
1527 case OP_SUB_LONG_2ADDR:
1528 case OP_MUL_LONG_2ADDR:
1529 case OP_DIV_LONG_2ADDR:
1530 case OP_REM_LONG_2ADDR:
1531 case OP_AND_LONG_2ADDR:
1532 case OP_OR_LONG_2ADDR:
1533 case OP_XOR_LONG_2ADDR:
1534 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1535 break;
1536
buzbee67bf8852011-08-17 17:51:35 -07001537 case OP_SHL_LONG:
1538 case OP_SHR_LONG:
1539 case OP_USHR_LONG:
buzbeee6d61962011-08-27 11:58:19 -07001540 case OP_SHL_LONG_2ADDR:
1541 case OP_SHR_LONG_2ADDR:
1542 case OP_USHR_LONG_2ADDR:
buzbee67bf8852011-08-17 17:51:35 -07001543 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
1544 break;
1545
1546 case OP_ADD_FLOAT:
1547 case OP_SUB_FLOAT:
1548 case OP_MUL_FLOAT:
1549 case OP_DIV_FLOAT:
1550 case OP_REM_FLOAT:
1551 case OP_ADD_FLOAT_2ADDR:
1552 case OP_SUB_FLOAT_2ADDR:
1553 case OP_MUL_FLOAT_2ADDR:
1554 case OP_DIV_FLOAT_2ADDR:
1555 case OP_REM_FLOAT_2ADDR:
1556 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1557 break;
1558
1559 case OP_ADD_DOUBLE:
1560 case OP_SUB_DOUBLE:
1561 case OP_MUL_DOUBLE:
1562 case OP_DIV_DOUBLE:
1563 case OP_REM_DOUBLE:
1564 case OP_ADD_DOUBLE_2ADDR:
1565 case OP_SUB_DOUBLE_2ADDR:
1566 case OP_MUL_DOUBLE_2ADDR:
1567 case OP_DIV_DOUBLE_2ADDR:
1568 case OP_REM_DOUBLE_2ADDR:
1569 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
1570 break;
1571
1572 case OP_RSUB_INT:
1573 case OP_ADD_INT_LIT16:
1574 case OP_MUL_INT_LIT16:
1575 case OP_DIV_INT_LIT16:
1576 case OP_REM_INT_LIT16:
1577 case OP_AND_INT_LIT16:
1578 case OP_OR_INT_LIT16:
1579 case OP_XOR_INT_LIT16:
1580 case OP_ADD_INT_LIT8:
1581 case OP_RSUB_INT_LIT8:
1582 case OP_MUL_INT_LIT8:
1583 case OP_DIV_INT_LIT8:
1584 case OP_REM_INT_LIT8:
1585 case OP_AND_INT_LIT8:
1586 case OP_OR_INT_LIT8:
1587 case OP_XOR_INT_LIT8:
1588 case OP_SHL_INT_LIT8:
1589 case OP_SHR_INT_LIT8:
1590 case OP_USHR_INT_LIT8:
1591 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
1592 break;
1593
1594 default:
1595 res = true;
1596 }
1597 return res;
1598}
1599
1600static const char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
1601 "kMirOpPhi",
1602 "kMirOpNullNRangeUpCheck",
1603 "kMirOpNullNRangeDownCheck",
1604 "kMirOpLowerBound",
1605 "kMirOpPunt",
1606 "kMirOpCheckInlinePrediction",
1607};
1608
1609/* Extended MIR instructions like PHI */
1610static void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
1611{
1612 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
1613 char* msg = (char*)oatNew(strlen(extendedMIROpNames[opOffset]) + 1, false);
1614 strcpy(msg, extendedMIROpNames[opOffset]);
1615 ArmLIR* op = newLIR1(cUnit, kArmPseudoExtended, (int) msg);
1616
1617 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
1618 case kMirOpPhi: {
1619 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1620 op->flags.isNop = true;
1621 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1622 break;
1623 }
1624 default:
1625 break;
1626 }
1627}
1628
1629/* If there are any ins passed in registers that have not been promoted
1630 * to a callee-save register, flush them to the frame.
buzbeedfd3d702011-08-28 12:56:51 -07001631 * Note: at this pointCopy any ins that are passed in register to their
1632 * home location */
buzbee67bf8852011-08-17 17:51:35 -07001633static void flushIns(CompilationUnit* cUnit)
1634{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001635 if (cUnit->method->NumIns() == 0)
buzbee67bf8852011-08-17 17:51:35 -07001636 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001637 int inRegs = (cUnit->method->NumIns() > 2) ? 3
1638 : cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001639 int startReg = r1;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001640 int startLoc = cUnit->method->NumRegisters() -
1641 cUnit->method->NumIns();
buzbee67bf8852011-08-17 17:51:35 -07001642 for (int i = 0; i < inRegs; i++) {
1643 RegLocation loc = cUnit->regLocation[startLoc + i];
buzbeedfd3d702011-08-28 12:56:51 -07001644 //TUNING: be smarter about flushing ins to frame
1645 storeBaseDisp(cUnit, rSP, loc.spOffset, startReg + i, kWord);
buzbee67bf8852011-08-17 17:51:35 -07001646 if (loc.location == kLocPhysReg) {
1647 genRegCopy(cUnit, loc.lowReg, startReg + i);
buzbee67bf8852011-08-17 17:51:35 -07001648 }
1649 }
1650
1651 // Handle special case of wide argument half in regs, half in frame
1652 if (inRegs == 3) {
1653 RegLocation loc = cUnit->regLocation[startLoc + 2];
1654 if (loc.wide && loc.location == kLocPhysReg) {
1655 // Load the other half of the arg into the promoted pair
1656 loadBaseDisp(cUnit, NULL, rSP, loc.spOffset+4,
1657 loc.highReg, kWord, INVALID_SREG);
1658 inRegs++;
1659 }
1660 }
1661
1662 // Now, do initial assignment of all promoted arguments passed in frame
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001663 for (int i = inRegs; i < cUnit->method->NumIns();) {
buzbee67bf8852011-08-17 17:51:35 -07001664 RegLocation loc = cUnit->regLocation[startLoc + i];
1665 if (loc.fpLocation == kLocPhysReg) {
1666 loc.location = kLocPhysReg;
1667 loc.fp = true;
1668 loc.lowReg = loc.fpLowReg;
1669 loc.highReg = loc.fpHighReg;
1670 }
1671 if (loc.location == kLocPhysReg) {
1672 if (loc.wide) {
1673 loadBaseDispWide(cUnit, NULL, rSP, loc.spOffset,
1674 loc.lowReg, loc.highReg, INVALID_SREG);
1675 i++;
1676 } else {
1677 loadBaseDisp(cUnit, NULL, rSP, loc.spOffset,
1678 loc.lowReg, kWord, INVALID_SREG);
1679 }
1680 }
1681 i++;
1682 }
1683}
1684
1685/* Handle the content in each basic block */
1686static bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
1687{
1688 MIR* mir;
1689 ArmLIR* labelList = (ArmLIR*) cUnit->blockLabelList;
1690 int blockId = bb->id;
1691
1692 cUnit->curBlock = bb;
1693 labelList[blockId].operands[0] = bb->startOffset;
1694
1695 /* Insert the block label */
1696 labelList[blockId].opcode = kArmPseudoNormalBlockLabel;
1697 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
1698
1699 oatClobberAllRegs(cUnit);
1700 oatResetNullCheck(cUnit);
1701
1702 ArmLIR* headLIR = NULL;
1703
1704 if (bb->blockType == kEntryBlock) {
1705 /*
1706 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
1707 * mechanism know so it doesn't try to use any of them when
1708 * expanding the frame or flushing. This leaves the utility
1709 * code with a single temp: r12. This should be enough.
1710 */
1711 oatLockTemp(cUnit, r0);
1712 oatLockTemp(cUnit, r1);
1713 oatLockTemp(cUnit, r2);
1714 oatLockTemp(cUnit, r3);
1715 newLIR0(cUnit, kArmPseudoMethodEntry);
1716 /* Spill core callee saves */
1717 newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask);
1718 /* Need to spill any FP regs? */
1719 if (cUnit->numFPSpills) {
1720 newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills);
1721 }
1722 opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1723 storeBaseDisp(cUnit, rSP, 0, r0, kWord);
1724 flushIns(cUnit);
1725 oatFreeTemp(cUnit, r0);
1726 oatFreeTemp(cUnit, r1);
1727 oatFreeTemp(cUnit, r2);
1728 oatFreeTemp(cUnit, r3);
1729 } else if (bb->blockType == kExitBlock) {
1730 newLIR0(cUnit, kArmPseudoMethodExit);
1731 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (cUnit->numSpills * 4));
1732 /* Need to restore any FP callee saves? */
1733 if (cUnit->numFPSpills) {
1734 newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills);
1735 }
1736 if (cUnit->coreSpillMask & (1 << rLR)) {
1737 /* Unspill rLR to rPC */
1738 cUnit->coreSpillMask &= ~(1 << rLR);
1739 cUnit->coreSpillMask |= (1 << rPC);
1740 }
1741 newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask);
1742 if (!(cUnit->coreSpillMask & (1 << rPC))) {
1743 /* We didn't pop to rPC, so must do a bv rLR */
1744 newLIR1(cUnit, kThumbBx, rLR);
1745 }
1746 }
1747
1748 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
1749
1750 oatResetRegPool(cUnit);
1751 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
1752 oatClobberAllRegs(cUnit);
1753 }
1754
1755 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
1756 oatResetDefTracking(cUnit);
1757 }
1758
1759 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
1760 handleExtendedMethodMIR(cUnit, mir);
1761 continue;
1762 }
1763
1764 cUnit->currentDalvikOffset = mir->offset;
1765
1766 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1767 InstructionFormat dalvikFormat =
1768 dexGetFormatFromOpcode(dalvikOpcode);
1769
1770 ArmLIR* boundaryLIR;
1771
1772 /* Mark the beginning of a Dalvik instruction for line tracking */
1773 boundaryLIR = newLIR1(cUnit, kArmPseudoDalvikByteCodeBoundary,
1774 (int) oatGetDalvikDisassembly(
1775 &mir->dalvikInsn, ""));
1776 /* Remember the first LIR for this block */
1777 if (headLIR == NULL) {
1778 headLIR = boundaryLIR;
1779 /* Set the first boundaryLIR as a scheduling barrier */
1780 headLIR->defMask = ENCODE_ALL;
1781 }
1782
1783 /* Don't generate the SSA annotation unless verbose mode is on */
1784 if (cUnit->printMe && mir->ssaRep) {
1785 char *ssaString = oatGetSSAString(cUnit, mir->ssaRep);
1786 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
1787 }
1788
1789 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
1790
1791 if (notHandled) {
1792 char buf[100];
1793 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
1794 mir->offset,
1795 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
1796 dalvikFormat);
1797 LOG(FATAL) << buf;
1798 }
1799 }
1800
1801 if (headLIR) {
1802 /*
1803 * Eliminate redundant loads/stores and delay stores into later
1804 * slots
1805 */
1806 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
1807 cUnit->lastLIRInsn);
1808
1809 /*
1810 * Generate an unconditional branch to the fallthrough block.
1811 */
1812 if (bb->fallThrough) {
1813 genUnconditionalBranch(cUnit,
1814 &labelList[bb->fallThrough->id]);
1815 }
1816 }
1817 return false;
1818}
1819
1820/*
1821 * Nop any unconditional branches that go to the next instruction.
1822 * Note: new redundant branches may be inserted later, and we'll
1823 * use a check in final instruction assembly to nop those out.
1824 */
1825void removeRedundantBranches(CompilationUnit* cUnit)
1826{
1827 ArmLIR* thisLIR;
1828
1829 for (thisLIR = (ArmLIR*) cUnit->firstLIRInsn;
1830 thisLIR != (ArmLIR*) cUnit->lastLIRInsn;
1831 thisLIR = NEXT_LIR(thisLIR)) {
1832
1833 /* Branch to the next instruction */
1834 if ((thisLIR->opcode == kThumbBUncond) ||
1835 (thisLIR->opcode == kThumb2BUncond)) {
1836 ArmLIR* nextLIR = thisLIR;
1837
1838 while (true) {
1839 nextLIR = NEXT_LIR(nextLIR);
1840
1841 /*
1842 * Is the branch target the next instruction?
1843 */
1844 if (nextLIR == (ArmLIR*) thisLIR->generic.target) {
1845 thisLIR->flags.isNop = true;
1846 break;
1847 }
1848
1849 /*
1850 * Found real useful stuff between the branch and the target.
1851 * Need to explicitly check the lastLIRInsn here because it
1852 * might be the last real instruction.
1853 */
1854 if (!isPseudoOpcode(nextLIR->opcode) ||
1855 (nextLIR = (ArmLIR*) cUnit->lastLIRInsn))
1856 break;
1857 }
1858 }
1859 }
1860}
1861
1862void oatMethodMIR2LIR(CompilationUnit* cUnit)
1863{
1864 /* Used to hold the labels of each block */
1865 cUnit->blockLabelList =
1866 (void *) oatNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
1867
1868 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
1869 kPreOrderDFSTraversal, false /* Iterative */);
1870 removeRedundantBranches(cUnit);
1871}
1872
1873/* Common initialization routine for an architecture family */
1874bool oatArchInit()
1875{
1876 int i;
1877
1878 for (i = 0; i < kArmLast; i++) {
1879 if (EncodingMap[i].opcode != i) {
1880 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
1881 " is wrong: expecting " << i << ", seeing " <<
1882 (int)EncodingMap[i].opcode;
1883 }
1884 }
1885
1886 return oatArchVariantInit();
1887}
1888
1889/* Needed by the Assembler */
1890void oatSetupResourceMasks(ArmLIR* lir)
1891{
1892 setupResourceMasks(lir);
1893}
1894
1895/* Needed by the ld/st optmizatons */
1896ArmLIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
1897{
1898 return genRegCopyNoInsert(cUnit, rDest, rSrc);
1899}
1900
1901/* Needed by the register allocator */
1902ArmLIR* oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
1903{
1904 return genRegCopy(cUnit, rDest, rSrc);
1905}
1906
1907/* Needed by the register allocator */
1908void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
1909 int srcLo, int srcHi)
1910{
1911 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
1912}
1913
1914void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
1915 int displacement, int rSrc, OpSize size)
1916{
1917 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
1918}
1919
1920void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
1921 int displacement, int rSrcLo, int rSrcHi)
1922{
1923 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
1924}