blob: c2023ff113cc0ddfc525caae92c97e03f156fc4d [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -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
buzbee31a4a6f2012-02-28 15:36:15 -080026typedef int (*NextCallInsn)(CompilationUnit*, MIR*, int, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -070027 uint32_t methodIdx, uintptr_t directCode,
28 uintptr_t directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -080029/*
30 * If there are any ins passed in registers that have not been promoted
31 * to a callee-save register, flush them to the frame. Perform intial
32 * assignment of promoted arguments.
33 */
34void flushIns(CompilationUnit* cUnit)
35{
buzbee9c044ce2012-03-18 13:24:07 -070036 /*
37 * Dummy up a RegLocation for the incoming Method*
38 * It will attempt to keep rARG0 live (or copy it to home location
39 * if promoted).
40 */
41 RegLocation rlSrc = cUnit->regLocation[cUnit->methodSReg];
42 RegLocation rlMethod = cUnit->regLocation[cUnit->methodSReg];
43 rlSrc.location = kLocPhysReg;
44 rlSrc.lowReg = rARG0;
45 rlSrc.home = false;
46 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
47 storeValue(cUnit, rlMethod, rlSrc);
48 // If Method* has been promoted, explicitly flush
49 if (rlMethod.location == kLocPhysReg) {
50 storeWordDisp(cUnit, rSP, 0, rARG0);
51 }
52
buzbee31a4a6f2012-02-28 15:36:15 -080053 if (cUnit->numIns == 0)
54 return;
55 int firstArgReg = rARG1;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070056#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -080057 int lastArgReg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070058#else
59 int lastArgReg = rARG2;
60#endif
buzbee31a4a6f2012-02-28 15:36:15 -080061 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
62 /*
buzbee86a4bce2012-03-06 18:15:00 -080063 * Copy incoming arguments to their proper home locations.
64 * NOTE: an older version of dx had an issue in which
65 * it would reuse static method argument registers.
buzbee31a4a6f2012-02-28 15:36:15 -080066 * This could result in the same Dalvik virtual register
buzbee86a4bce2012-03-06 18:15:00 -080067 * being promoted to both core and fp regs. To account for this,
68 * we only copy to the corresponding promoted physical register
69 * if it matches the type of the SSA name for the incoming
70 * argument. It is also possible that long and double arguments
71 * end up half-promoted. In those cases, we must flush the promoted
72 * half to memory as well.
buzbee31a4a6f2012-02-28 15:36:15 -080073 */
74 for (int i = 0; i < cUnit->numIns; i++) {
buzbee86a4bce2012-03-06 18:15:00 -080075 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
buzbee31a4a6f2012-02-28 15:36:15 -080076 if (i <= (lastArgReg - firstArgReg)) {
77 // If arriving in register
buzbee86a4bce2012-03-06 18:15:00 -080078 bool needFlush = true;
79 RegLocation* tLoc = &cUnit->regLocation[startVReg + i];
80 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
81 opRegCopy(cUnit, vMap->coreReg, firstArgReg + i);
82 needFlush = false;
83 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
84 opRegCopy(cUnit, vMap->fpReg, firstArgReg + i);
85 needFlush = false;
86 } else {
87 needFlush = true;
buzbee31a4a6f2012-02-28 15:36:15 -080088 }
buzbee86a4bce2012-03-06 18:15:00 -080089
90 // For wide args, force flush if only half is promoted
91 if (tLoc->wide) {
92 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
93 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
94 (pMap->fpLocation != vMap->fpLocation);
buzbee31a4a6f2012-02-28 15:36:15 -080095 }
buzbee86a4bce2012-03-06 18:15:00 -080096 if (needFlush) {
97 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
98 firstArgReg + i, kWord);
99 }
buzbee31a4a6f2012-02-28 15:36:15 -0800100 } else {
101 // If arriving in frame & promoted
buzbee86a4bce2012-03-06 18:15:00 -0800102 if (vMap->coreLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800103 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800104 vMap->coreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800105 }
buzbee86a4bce2012-03-06 18:15:00 -0800106 if (vMap->fpLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800107 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800108 vMap->fpReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800109 }
110 }
111 }
112}
113
Ian Rogers3fa13792012-03-18 15:53:45 -0700114void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget, const DexFile* dexFile, uint32_t dexMethodIdx)
115{
116 LIR* curTarget = cUnit->methodLiteralList;
117 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
118 while (curTarget != NULL && nextTarget != NULL) {
119 if (curTarget->operands[0] == (int)dexFile &&
120 nextTarget->operands[0] == (int)dexMethodIdx) {
121 *codeTarget = curTarget;
122 *methodTarget = nextTarget;
123 DCHECK((*codeTarget)->next == *methodTarget);
124 DCHECK_EQ((*codeTarget)->operands[0], (int)dexFile);
125 DCHECK_EQ((*methodTarget)->operands[0], (int)dexMethodIdx);
126 break;
127 }
128 curTarget = nextTarget->next;
129 nextTarget = curTarget != NULL ? curTarget->next : NULL;
130 }
131}
132
buzbee31a4a6f2012-02-28 15:36:15 -0800133/*
134 * Bit of a hack here - in leiu of a real scheduling pass,
135 * emit the next instruction in static & direct invoke sequences.
136 */
137int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700138 int state, uint32_t dexIdx, uint32_t unused,
139 uintptr_t directCode, uintptr_t directMethod)
buzbee31a4a6f2012-02-28 15:36:15 -0800140{
Ian Rogers2ed3b952012-03-17 11:49:39 -0700141 if (directCode != 0 && directMethod != 0) {
142 switch(state) {
143 case 0: // Get the current Method* [sets rARG0]
Ian Rogers3fa13792012-03-18 15:53:45 -0700144 if (directCode != (uintptr_t)-1) {
145 loadConstant(cUnit, rINVOKE_TGT, directCode);
146 } else {
147 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
148 if (dataTarget == NULL) {
149 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
150 }
151#if defined(TARGET_ARM)
152 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
153 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
154 oatAppendLIR(cUnit, loadPcRel);
155#else
156 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
157#endif
158 }
159 if (directMethod != (uintptr_t)-1) {
160 loadConstant(cUnit, rARG0, directMethod);
161 } else {
162 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
163 if (dataTarget == NULL) {
164 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
165 }
166#if defined(TARGET_ARM)
167 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
168 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0, dataTarget);
169 oatAppendLIR(cUnit, loadPcRel);
170#else
171 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
172#endif
173 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700174 break;
175 default:
176 return -1;
177 }
178 } else {
179 switch(state) {
buzbee31a4a6f2012-02-28 15:36:15 -0800180 case 0: // Get the current Method* [sets rARG0]
buzbee9c044ce2012-03-18 13:24:07 -0700181 // TUNING: we can save a reg copy if Method* has been promoted
buzbee31a4a6f2012-02-28 15:36:15 -0800182 loadCurrMethodDirect(cUnit, rARG0);
183 break;
184 case 1: // Get method->dex_cache_resolved_methods_
185 loadWordDisp(cUnit, rARG0,
186 Method::DexCacheResolvedMethodsOffset().Int32Value(),
187 rARG0);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700188 // Set up direct code if known.
189 if (directCode != 0) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700190 if (directCode != (uintptr_t)-1) {
191 loadConstant(cUnit, rINVOKE_TGT, directCode);
192 } else {
193 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
194 if (dataTarget == NULL) {
195 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
196 }
197#if defined(TARGET_ARM)
198 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
199 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
200 oatAppendLIR(cUnit, loadPcRel);
201#else
202 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
203#endif
204 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700205 }
buzbee31a4a6f2012-02-28 15:36:15 -0800206 break;
207 case 2: // Grab target method*
208 loadWordDisp(cUnit, rARG0,
209 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
210 rARG0);
211 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700212#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800213 case 3: // Grab the code from the method*
Ian Rogers2ed3b952012-03-17 11:49:39 -0700214 if (directCode == 0) {
215 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
216 rINVOKE_TGT);
217 }
buzbee31a4a6f2012-02-28 15:36:15 -0800218 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700219#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800220 default:
221 return -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700222 }
buzbee31a4a6f2012-02-28 15:36:15 -0800223 }
224 return state + 1;
225}
226
227/*
228 * Bit of a hack here - in leiu of a real scheduling pass,
229 * emit the next instruction in a virtual invoke sequence.
230 * We can use rLR as a temp prior to target address loading
231 * Note also that we'll load the first argument ("this") into
232 * rARG1 here rather than the standard loadArgRegs.
233 */
234int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700235 int state, uint32_t dexIdx, uint32_t methodIdx,
236 uintptr_t unused, uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800237{
238 RegLocation rlArg;
239 /*
240 * This is the fast path in which the target virtual method is
241 * fully resolved at compile time.
242 */
243 switch(state) {
244 case 0: // Get "this" [set rARG1]
245 rlArg = oatGetSrc(cUnit, mir, 0);
246 loadValueDirectFixed(cUnit, rlArg, rARG1);
247 break;
248 case 1: // Is "this" null? [use rARG1]
249 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
buzbee0398c422012-03-02 15:22:47 -0800250 // get this->klass_ [use rARG1, set rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800251 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800252 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800253 break;
buzbee0398c422012-03-02 15:22:47 -0800254 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
255 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
256 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800257 break;
buzbee0398c422012-03-02 15:22:47 -0800258 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
259 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
buzbee31a4a6f2012-02-28 15:36:15 -0800260 Array::DataOffset(sizeof(Object*)).Int32Value(),
261 rARG0);
262 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700263#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -0800264 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800265 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800266 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800267 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700268#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800269 default:
270 return -1;
271 }
272 return state + 1;
273}
274
buzbee31a4a6f2012-02-28 15:36:15 -0800275int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
276 int state, uint32_t dexIdx, uint32_t methodIdx)
277{
278 /*
279 * This handles the case in which the base method is not fully
280 * resolved at compile time, we bail to a runtime helper.
281 */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700282#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800283 if (state == 0) {
284 // Load trampoline target
buzbee0398c422012-03-02 15:22:47 -0800285 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800286 // Load rARG0 with method index
287 loadConstant(cUnit, rARG0, dexIdx);
288 return 1;
289 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700290#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800291 return -1;
292}
293
294int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700295 int state, uint32_t dexIdx, uint32_t methodIdx,
296 uintptr_t unused, uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800297{
298 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeStaticTrampolineWithAccessCheck);
299 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
300}
301
302int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700303 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
304 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800305{
306 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeDirectTrampolineWithAccessCheck);
307 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
308}
309
310int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700311 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
312 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800313{
314 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
315 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
316}
317
318int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700319 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
320 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800321{
322 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
323 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
324}
325
326/*
327 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
328 * which will locate the target and continue on via a tail call.
329 */
330int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700331 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
332 uintptr_t unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800333{
334 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
335 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
336}
337
338int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit, MIR* mir,
339 int state, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700340 uint32_t unused, uintptr_t unused2,
341 uintptr_t unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800342{
343 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
344 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
345}
346
347int loadArgRegs(CompilationUnit* cUnit, MIR* mir, DecodedInstruction* dInsn,
348 int callState, NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700349 uint32_t methodIdx, uintptr_t directCode,
350 uintptr_t directMethod, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800351{
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700352#if !defined(TARGET_X86)
353 int lastArgReg = rARG3;
354#else
355 int lastArgReg = rARG2;
356#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800357 int nextReg = rARG1;
358 int nextArg = 0;
359 if (skipThis) {
360 nextReg++;
361 nextArg++;
362 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700363 for (; (nextReg <= lastArgReg) && (nextArg < mir->ssaRep->numUses); nextReg++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800364 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
365 rlArg = oatUpdateRawLoc(cUnit, rlArg);
366 if (rlArg.wide && (nextReg <= rARG2)) {
367 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
368 nextReg++;
369 nextArg++;
370 } else {
371 rlArg.wide = false;
372 loadValueDirectFixed(cUnit, rlArg, nextReg);
373 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700374 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
375 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800376 }
377 return callState;
378}
379
380/*
381 * Load up to 5 arguments, the first three of which will be in
382 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
383 * and as part of the load sequence, it must be replaced with
384 * the target method pointer. Note, this may also be called
385 * for "range" variants if the number of arguments is 5 or fewer.
386 */
387int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
388 DecodedInstruction* dInsn, int callState,
389 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700390 uint32_t dexIdx, uint32_t methodIdx,
391 uintptr_t directCode, uintptr_t directMethod,
392 bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800393{
394 RegLocation rlArg;
395
396 /* If no arguments, just return */
397 if (dInsn->vA == 0)
398 return callState;
399
Ian Rogers2ed3b952012-03-17 11:49:39 -0700400 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
401 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800402
403 DCHECK_LE(dInsn->vA, 5U);
404 if (dInsn->vA > 3) {
405 uint32_t nextUse = 3;
406 //Detect special case of wide arg spanning arg3/arg4
407 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
408 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
409 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
410 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
411 rlUse2.wide) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700412 int reg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800413 // Wide spans, we need the 2nd half of uses[2].
414 rlArg = oatUpdateLocWide(cUnit, rlUse2);
415 if (rlArg.location == kLocPhysReg) {
416 reg = rlArg.highReg;
417 } else {
418 // rARG2 & rARG3 can safely be used here
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700419#if defined(TARGET_X86)
420 UNIMPLEMENTED(FATAL);
421#else
buzbee31a4a6f2012-02-28 15:36:15 -0800422 reg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700423#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800424 loadWordDisp(cUnit, rSP,
425 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
426 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700427 methodIdx, directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800428 }
429 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
430 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700431 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
432 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800433 nextUse++;
434 }
435 // Loop through the rest
436 while (nextUse < dInsn->vA) {
437 int lowReg;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700438 int highReg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800439 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
440 rlArg = oatUpdateRawLoc(cUnit, rlArg);
441 if (rlArg.location == kLocPhysReg) {
442 lowReg = rlArg.lowReg;
443 highReg = rlArg.highReg;
444 } else {
445 lowReg = rARG2;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700446#if defined(TARGET_X86)
447 UNIMPLEMENTED(FATAL);
448#else
buzbee31a4a6f2012-02-28 15:36:15 -0800449 highReg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700450#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800451 if (rlArg.wide) {
452 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
453 } else {
454 loadValueDirectFixed(cUnit, rlArg, lowReg);
455 }
456 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700457 methodIdx, directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800458 }
459 int outsOffset = (nextUse + 1) * 4;
460 if (rlArg.wide) {
461 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
462 nextUse += 2;
463 } else {
464 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
465 nextUse++;
466 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700467 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
468 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800469 }
470 }
471
472 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700473 dexIdx, methodIdx, directCode, directMethod,
474 skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800475
476 if (pcrLabel) {
477 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
478 }
479 return callState;
480}
481
482/*
483 * May have 0+ arguments (also used for jumbo). Note that
484 * source virtual registers may be in physical registers, so may
485 * need to be flushed to home location before copying. This
486 * applies to arg3 and above (see below).
487 *
488 * Two general strategies:
489 * If < 20 arguments
490 * Pass args 3-18 using vldm/vstm block copy
491 * Pass arg0, arg1 & arg2 in rARG1-rARG3
492 * If 20+ arguments
493 * Pass args arg19+ using memcpy block copy
494 * Pass arg0, arg1 & arg2 in rARG1-rARG3
495 *
496 */
497int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
498 DecodedInstruction* dInsn, int callState,
499 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700500 uint32_t dexIdx, uint32_t methodIdx,
501 uintptr_t directCode, uintptr_t directMethod,
502 bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800503{
504 int firstArg = dInsn->vC;
505 int numArgs = dInsn->vA;
506
507 // If we can treat it as non-range (Jumbo ops will use range form)
508 if (numArgs <= 5)
509 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
510 nextCallInsn, dexIdx, methodIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700511 directCode, directMethod, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800512 /*
513 * Make sure range list doesn't span the break between in normal
514 * Dalvik vRegs and the ins.
515 */
516 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
517 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
518 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
519 LOG(FATAL) << "Argument list spanned locals & args";
520 }
521
522 /*
523 * First load the non-register arguments. Both forms expect all
524 * of the source arguments to be in their home frame location, so
525 * scan the sReg names and flush any that have been promoted to
526 * frame backing storage.
527 */
528 // Scan the rest of the args - if in physReg flush to memory
529 for (int nextArg = 0; nextArg < numArgs;) {
530 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
531 if (loc.wide) {
532 loc = oatUpdateLocWide(cUnit, loc);
533 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
534 storeBaseDispWide(cUnit, rSP,
535 oatSRegOffset(cUnit, loc.sRegLow),
536 loc.lowReg, loc.highReg);
537 }
538 nextArg += 2;
539 } else {
540 loc = oatUpdateLoc(cUnit, loc);
541 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
542 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
543 loc.lowReg, kWord);
544 }
545 nextArg++;
546 }
547 }
548
549 int startOffset = oatSRegOffset(cUnit,
550 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
551 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700552#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800553 // Generate memcpy
554 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
555 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700556 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
557 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800558#else
559 if (numArgs >= 20) {
560 // Generate memcpy
561 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
562 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700563 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
564 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800565 } else {
566 // Use vldm/vstm pair using rARG3 as a temp
567 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700568 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
569 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800570 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
571 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
572 //TUNING: loosen barrier
573 ld->defMask = ENCODE_ALL;
574 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700575 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
576 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800577 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogers2ed3b952012-03-17 11:49:39 -0700578 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
579 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800580 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
581 setMemRefType(st, false /* isLoad */, kDalvikReg);
582 st->defMask = ENCODE_ALL;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700583 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
584 directCode, directMethod);
585
buzbee31a4a6f2012-02-28 15:36:15 -0800586 }
587#endif
588
589 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700590 dexIdx, methodIdx, directCode, directMethod,
591 skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800592
Ian Rogers2ed3b952012-03-17 11:49:39 -0700593 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
594 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800595 if (pcrLabel) {
596 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
597 }
598 return callState;
599}
600
buzbee31a4a6f2012-02-28 15:36:15 -0800601} // namespace art