blob: 2f095f130bdf8cb2b7f608375d1429ab680dd332 [file] [log] [blame]
buzbeea7678db2012-03-05 15:35:46 -08001/*
2 * Copyright (C) 2012 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
17namespace art {
18
19/*
20 * This source files contains "gen" codegen routines that should
21 * be applicable to most targets. Only mid-level support utilities
22 * and "op" calls may be used here.
23 */
24
25
26/*
27 * x86 targets will likely be different enough to need their own
28 * invoke gen routies.
29 */
30typedef int (*NextCallInsn)(CompilationUnit*, MIR*, int, uint32_t dexIdx,
31 uint32_t methodIdx);
32/*
33 * If there are any ins passed in registers that have not been promoted
34 * to a callee-save register, flush them to the frame. Perform intial
35 * assignment of promoted arguments.
36 */
37void flushIns(CompilationUnit* cUnit)
38{
39 UNIMPLEMENTED(WARNING) << "flushIns";
40#if 0
41 if (cUnit->numIns == 0)
42 return;
43 int firstArgReg = rARG1;
44 int lastArgReg = rARG3;
45 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
46 /*
47 * Arguments passed in registers should be flushed
48 * to their backing locations in the frame for now.
49 * Also, we need to do initial assignment for promoted
50 * arguments. NOTE: an older version of dx had an issue
51 * in which it would reuse static method argument registers.
52 * This could result in the same Dalvik virtual register
53 * being promoted to both core and fp regs. In those
54 * cases, copy argument to both. This will be uncommon
55 * enough that it isn't worth attempting to optimize.
56 */
57 for (int i = 0; i < cUnit->numIns; i++) {
58 PromotionMap vMap = cUnit->promotionMap[startVReg + i];
59 if (i <= (lastArgReg - firstArgReg)) {
60 // If arriving in register
61 if (vMap.coreLocation == kLocPhysReg) {
62 opRegCopy(cUnit, vMap.coreReg, firstArgReg + i);
63 }
64 if (vMap.fpLocation == kLocPhysReg) {
65 opRegCopy(cUnit, vMap.fpReg, firstArgReg + i);
66 }
67 // Also put a copy in memory in case we're partially promoted
68 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
69 firstArgReg + i, kWord);
70 } else {
71 // If arriving in frame & promoted
72 if (vMap.coreLocation == kLocPhysReg) {
73 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
74 vMap.coreReg);
75 }
76 if (vMap.fpLocation == kLocPhysReg) {
77 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
78 vMap.fpReg);
79 }
80 }
81 }
82#endif
83}
84
85/*
86 * Bit of a hack here - in leiu of a real scheduling pass,
87 * emit the next instruction in static & direct invoke sequences.
88 */
89int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
90 int state, uint32_t dexIdx, uint32_t unused)
91{
92 UNIMPLEMENTED(WARNING) << "nextSDCallInsn";
93 return 0;
94#if 0
95 switch(state) {
96 case 0: // Get the current Method* [sets rARG0]
97 loadCurrMethodDirect(cUnit, rARG0);
98 break;
99 case 1: // Get method->dex_cache_resolved_methods_
100 loadWordDisp(cUnit, rARG0,
101 Method::DexCacheResolvedMethodsOffset().Int32Value(),
102 rARG0);
103 break;
104 case 2: // Grab target method*
105 loadWordDisp(cUnit, rARG0,
106 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
107 rARG0);
108 break;
109 case 3: // Grab the code from the method*
110 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
111 rINVOKE_TGT);
112 break;
113 default:
114 return -1;
115 }
116 return state + 1;
117#endif
118}
119
120/*
121 * Bit of a hack here - in leiu of a real scheduling pass,
122 * emit the next instruction in a virtual invoke sequence.
123 * We can use rLR as a temp prior to target address loading
124 * Note also that we'll load the first argument ("this") into
125 * rARG1 here rather than the standard loadArgRegs.
126 */
127int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
128 int state, uint32_t dexIdx, uint32_t methodIdx)
129{
130 UNIMPLEMENTED(WARNING) << "nextVCallInsn";
131 return 0;
132#if 0
133 RegLocation rlArg;
134 /*
135 * This is the fast path in which the target virtual method is
136 * fully resolved at compile time.
137 */
138 switch(state) {
139 case 0: // Get "this" [set rARG1]
140 rlArg = oatGetSrc(cUnit, mir, 0);
141 loadValueDirectFixed(cUnit, rlArg, rARG1);
142 break;
143 case 1: // Is "this" null? [use rARG1]
144 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
145 // get this->klass_ [use rARG1, set rINVOKE_TGT]
146 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
147 rINVOKE_TGT);
148 break;
149 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
150 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
151 rINVOKE_TGT);
152 break;
153 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
154 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
155 Array::DataOffset(sizeof(Object*)).Int32Value(),
156 rARG0);
157 break;
158 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
159 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
160 rINVOKE_TGT);
161 break;
162 default:
163 return -1;
164 }
165 return state + 1;
166#endif
167}
168
169/*
170 * Interleave launch code for INVOKE_SUPER. See comments
171 * for nextVCallIns.
172 */
173int nextSuperCallInsn(CompilationUnit* cUnit, MIR* mir,
174 int state, uint32_t dexIdx, uint32_t methodIdx)
175{
176 UNIMPLEMENTED(WARNING) << "nextSuperCallInsn";
177 return 0;
178#if 0
179 /*
180 * This is the fast path in which the target virtual method is
181 * fully resolved at compile time. Note also that this path assumes
182 * that the check to verify that the target method index falls
183 * within the size of the super's vtable has been done at compile-time.
184 */
185 RegLocation rlArg;
186 switch(state) {
187 case 0: // Get current Method* [set rARG0]
188 loadCurrMethodDirect(cUnit, rARG0);
189 // Load "this" [set rARG1]
190 rlArg = oatGetSrc(cUnit, mir, 0);
191 loadValueDirectFixed(cUnit, rlArg, rARG1);
192 // Get method->declaring_class_ [use rARG0, set rINVOKE_TGT]
193 loadWordDisp(cUnit, rARG0,
194 Method::DeclaringClassOffset().Int32Value(),
195 rINVOKE_TGT);
196 // Is "this" null? [use rARG1]
197 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
198 break;
199 case 1: // method->declaring_class_->super_class [use/set rINVOKE_TGT]
200 loadWordDisp(cUnit, rINVOKE_TGT,
201 Class::SuperClassOffset().Int32Value(), rINVOKE_TGT);
202 break;
203 case 2: // Get ...->super_class_->vtable [u/s rINVOKE_TGT]
204 loadWordDisp(cUnit, rINVOKE_TGT,
205 Class::VTableOffset().Int32Value(), rINVOKE_TGT);
206 break;
207 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
208 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
209 Array::DataOffset(sizeof(Object*)).Int32Value(),
210 rARG0);
211 break;
212 case 4: // target compiled code address [uses rARG0, sets rINVOKE_TGT]
213 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
214 rINVOKE_TGT);
215 break;
216 default:
217 return -1;
218 }
219 return state + 1;
220#endif
221}
222
223int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
224 int state, uint32_t dexIdx, uint32_t methodIdx)
225{
226 UNIMPLEMENTED(WARNING) << "nextInvokeInsnSP";
227 return 0;
228#if 0
229 /*
230 * This handles the case in which the base method is not fully
231 * resolved at compile time, we bail to a runtime helper.
232 */
233 if (state == 0) {
234 // Load trampoline target
235 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
236 // Load rARG0 with method index
237 loadConstant(cUnit, rARG0, dexIdx);
238 return 1;
239 }
240 return -1;
241#endif
242}
243
244int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
245 int state, uint32_t dexIdx, uint32_t methodIdx)
246{
247 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeStaticTrampolineWithAccessCheck);
248 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
249}
250
251int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
252 uint32_t dexIdx, uint32_t methodIdx)
253{
254 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeDirectTrampolineWithAccessCheck);
255 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
256}
257
258int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
259 uint32_t dexIdx, uint32_t methodIdx)
260{
261 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
262 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
263}
264
265int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
266 uint32_t dexIdx, uint32_t methodIdx)
267{
268 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
269 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
270}
271
272/*
273 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
274 * which will locate the target and continue on via a tail call.
275 */
276int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir, int state,
277 uint32_t dexIdx, uint32_t unused)
278{
279 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
280 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
281}
282
283int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit, MIR* mir,
284 int state, uint32_t dexIdx,
285 uint32_t unused)
286{
287 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
288 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
289}
290
291int loadArgRegs(CompilationUnit* cUnit, MIR* mir, DecodedInstruction* dInsn,
292 int callState, NextCallInsn nextCallInsn, uint32_t dexIdx,
293 uint32_t methodIdx, bool skipThis)
294{
295 UNIMPLEMENTED(WARNING) << "loadArgRegs";
296 return 0;
297#if 0
298 int nextReg = rARG1;
299 int nextArg = 0;
300 if (skipThis) {
301 nextReg++;
302 nextArg++;
303 }
304 for (; (nextReg <= rARG3) && (nextArg < mir->ssaRep->numUses); nextReg++) {
305 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
306 rlArg = oatUpdateRawLoc(cUnit, rlArg);
307 if (rlArg.wide && (nextReg <= rARG2)) {
308 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
309 nextReg++;
310 nextArg++;
311 } else {
312 rlArg.wide = false;
313 loadValueDirectFixed(cUnit, rlArg, nextReg);
314 }
315 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
316 }
317 return callState;
318#endif
319}
320
321/*
322 * Load up to 5 arguments, the first three of which will be in
323 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
324 * and as part of the load sequence, it must be replaced with
325 * the target method pointer. Note, this may also be called
326 * for "range" variants if the number of arguments is 5 or fewer.
327 */
328int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
329 DecodedInstruction* dInsn, int callState,
330 LIR** pcrLabel, NextCallInsn nextCallInsn,
331 uint32_t dexIdx, uint32_t methodIdx, bool skipThis)
332{
333 UNIMPLEMENTED(WARNING) << "genDalvikArgsNoRange";
334 return 0;
335#if 0
336 RegLocation rlArg;
337
338 /* If no arguments, just return */
339 if (dInsn->vA == 0)
340 return callState;
341
342 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
343
344 DCHECK_LE(dInsn->vA, 5U);
345 if (dInsn->vA > 3) {
346 uint32_t nextUse = 3;
347 //Detect special case of wide arg spanning arg3/arg4
348 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
349 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
350 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
351 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
352 rlUse2.wide) {
353 int reg;
354 // Wide spans, we need the 2nd half of uses[2].
355 rlArg = oatUpdateLocWide(cUnit, rlUse2);
356 if (rlArg.location == kLocPhysReg) {
357 reg = rlArg.highReg;
358 } else {
359 // rARG2 & rARG3 can safely be used here
360 reg = rARG3;
361 loadWordDisp(cUnit, rSP,
362 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
363 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
364 methodIdx);
365 }
366 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
367 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
368 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
369 nextUse++;
370 }
371 // Loop through the rest
372 while (nextUse < dInsn->vA) {
373 int lowReg;
374 int highReg;
375 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
376 rlArg = oatUpdateRawLoc(cUnit, rlArg);
377 if (rlArg.location == kLocPhysReg) {
378 lowReg = rlArg.lowReg;
379 highReg = rlArg.highReg;
380 } else {
381 lowReg = rARG2;
382 highReg = rARG3;
383 if (rlArg.wide) {
384 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
385 } else {
386 loadValueDirectFixed(cUnit, rlArg, lowReg);
387 }
388 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
389 methodIdx);
390 }
391 int outsOffset = (nextUse + 1) * 4;
392 if (rlArg.wide) {
393 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
394 nextUse += 2;
395 } else {
396 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
397 nextUse++;
398 }
399 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
400 }
401 }
402
403 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
404 dexIdx, methodIdx, skipThis);
405
406 if (pcrLabel) {
407 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
408 }
409 return callState;
410#endif
411}
412
413/*
414 * May have 0+ arguments (also used for jumbo). Note that
415 * source virtual registers may be in physical registers, so may
416 * need to be flushed to home location before copying. This
417 * applies to arg3 and above (see below).
418 *
419 * Two general strategies:
420 * If < 20 arguments
421 * Pass args 3-18 using vldm/vstm block copy
422 * Pass arg0, arg1 & arg2 in rARG1-rARG3
423 * If 20+ arguments
424 * Pass args arg19+ using memcpy block copy
425 * Pass arg0, arg1 & arg2 in rARG1-rARG3
426 *
427 */
428int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
429 DecodedInstruction* dInsn, int callState,
430 LIR** pcrLabel, NextCallInsn nextCallInsn,
431 uint32_t dexIdx, uint32_t methodIdx, bool skipThis)
432{
433 UNIMPLEMENTED(WARNING) << "genDalvikArgsRange";
434 return 0;
435#if 0
436 int firstArg = dInsn->vC;
437 int numArgs = dInsn->vA;
438
439 // If we can treat it as non-range (Jumbo ops will use range form)
440 if (numArgs <= 5)
441 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
442 nextCallInsn, dexIdx, methodIdx,
443 skipThis);
444 /*
445 * Make sure range list doesn't span the break between in normal
446 * Dalvik vRegs and the ins.
447 */
448 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
449 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
450 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
451 LOG(FATAL) << "Argument list spanned locals & args";
452 }
453
454 /*
455 * First load the non-register arguments. Both forms expect all
456 * of the source arguments to be in their home frame location, so
457 * scan the sReg names and flush any that have been promoted to
458 * frame backing storage.
459 */
460 // Scan the rest of the args - if in physReg flush to memory
461 for (int nextArg = 0; nextArg < numArgs;) {
462 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
463 if (loc.wide) {
464 loc = oatUpdateLocWide(cUnit, loc);
465 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
466 storeBaseDispWide(cUnit, rSP,
467 oatSRegOffset(cUnit, loc.sRegLow),
468 loc.lowReg, loc.highReg);
469 }
470 nextArg += 2;
471 } else {
472 loc = oatUpdateLoc(cUnit, loc);
473 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
474 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
475 loc.lowReg, kWord);
476 }
477 nextArg++;
478 }
479 }
480
481 int startOffset = oatSRegOffset(cUnit,
482 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
483 int outsOffset = 4 /* Method* */ + (3 * 4);
484#if defined(TARGET_MIPS)
485 // Generate memcpy
486 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
487 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
488 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy));
489 loadConstant(cUnit, rARG2, (numArgs - 3) * 4);
490 callRuntimeHelper(cUnit, rTgt);
491 // Restore Method*
492 loadCurrMethodDirect(cUnit, rARG0);
493#else
494 if (numArgs >= 20) {
495 // Generate memcpy
496 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
497 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
498 int rTgt = loadHelper(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy));
499 loadConstant(cUnit, rARG2, (numArgs - 3) * 4);
500 callRuntimeHelper(cUnit, rTgt);
501 // Restore Method*
502 loadCurrMethodDirect(cUnit, rARG0);
503 } else {
504 // Use vldm/vstm pair using rARG3 as a temp
505 int regsLeft = std::min(numArgs - 3, 16);
506 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
507 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
508 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
509 //TUNING: loosen barrier
510 ld->defMask = ENCODE_ALL;
511 setMemRefType(ld, true /* isLoad */, kDalvikReg);
512 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
513 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
514 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
515 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
516 setMemRefType(st, false /* isLoad */, kDalvikReg);
517 st->defMask = ENCODE_ALL;
518 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
519 }
520#endif
521
522 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
523 dexIdx, methodIdx, skipThis);
524
525 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx);
526 if (pcrLabel) {
527 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
528 }
529 return callState;
530#endif
531}
532
533} // namespace art