blob: 2810936caac737bd5bc63ee86f654b224f16b36d [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;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070055#if !defined(TARGET_X86)
Ian Rogersb3ab25b2012-03-19 01:12:01 -070056 const int numArgRegs = 3;
57 static int argRegs[] = {rARG1, rARG2, rARG3};
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070058#else
Ian Rogersb3ab25b2012-03-19 01:12:01 -070059 const int numArgRegs = 2;
60 static int argRegs[] = {rARG1, rARG2};
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070061#endif
buzbee31a4a6f2012-02-28 15:36:15 -080062 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
63 /*
buzbee86a4bce2012-03-06 18:15:00 -080064 * Copy incoming arguments to their proper home locations.
65 * NOTE: an older version of dx had an issue in which
66 * it would reuse static method argument registers.
buzbee31a4a6f2012-02-28 15:36:15 -080067 * This could result in the same Dalvik virtual register
buzbee86a4bce2012-03-06 18:15:00 -080068 * being promoted to both core and fp regs. To account for this,
69 * we only copy to the corresponding promoted physical register
70 * if it matches the type of the SSA name for the incoming
71 * argument. It is also possible that long and double arguments
72 * end up half-promoted. In those cases, we must flush the promoted
73 * half to memory as well.
buzbee31a4a6f2012-02-28 15:36:15 -080074 */
75 for (int i = 0; i < cUnit->numIns; i++) {
buzbee86a4bce2012-03-06 18:15:00 -080076 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
Ian Rogersb3ab25b2012-03-19 01:12:01 -070077 if (i < numArgRegs) {
buzbee31a4a6f2012-02-28 15:36:15 -080078 // If arriving in register
buzbee86a4bce2012-03-06 18:15:00 -080079 bool needFlush = true;
80 RegLocation* tLoc = &cUnit->regLocation[startVReg + i];
81 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070082 opRegCopy(cUnit, vMap->coreReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080083 needFlush = false;
84 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070085 opRegCopy(cUnit, vMap->fpReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080086 needFlush = false;
87 } else {
88 needFlush = true;
buzbee31a4a6f2012-02-28 15:36:15 -080089 }
buzbee86a4bce2012-03-06 18:15:00 -080090
91 // For wide args, force flush if only half is promoted
92 if (tLoc->wide) {
93 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
94 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
95 (pMap->fpLocation != vMap->fpLocation);
buzbee31a4a6f2012-02-28 15:36:15 -080096 }
buzbee86a4bce2012-03-06 18:15:00 -080097 if (needFlush) {
98 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
Ian Rogersb3ab25b2012-03-19 01:12:01 -070099 argRegs[i], kWord);
buzbee86a4bce2012-03-06 18:15:00 -0800100 }
buzbee31a4a6f2012-02-28 15:36:15 -0800101 } else {
102 // If arriving in frame & promoted
buzbee86a4bce2012-03-06 18:15:00 -0800103 if (vMap->coreLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800104 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800105 vMap->coreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800106 }
buzbee86a4bce2012-03-06 18:15:00 -0800107 if (vMap->fpLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800108 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800109 vMap->fpReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800110 }
111 }
112 }
113}
114
Ian Rogers3fa13792012-03-18 15:53:45 -0700115void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget, const DexFile* dexFile, uint32_t dexMethodIdx)
116{
117 LIR* curTarget = cUnit->methodLiteralList;
118 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
119 while (curTarget != NULL && nextTarget != NULL) {
120 if (curTarget->operands[0] == (int)dexFile &&
121 nextTarget->operands[0] == (int)dexMethodIdx) {
122 *codeTarget = curTarget;
123 *methodTarget = nextTarget;
124 DCHECK((*codeTarget)->next == *methodTarget);
125 DCHECK_EQ((*codeTarget)->operands[0], (int)dexFile);
126 DCHECK_EQ((*methodTarget)->operands[0], (int)dexMethodIdx);
127 break;
128 }
129 curTarget = nextTarget->next;
130 nextTarget = curTarget != NULL ? curTarget->next : NULL;
131 }
132}
133
buzbee31a4a6f2012-02-28 15:36:15 -0800134/*
135 * Bit of a hack here - in leiu of a real scheduling pass,
136 * emit the next instruction in static & direct invoke sequences.
137 */
138int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700139 int state, uint32_t dexIdx, uint32_t unused,
140 uintptr_t directCode, uintptr_t directMethod)
buzbee31a4a6f2012-02-28 15:36:15 -0800141{
Ian Rogers2ed3b952012-03-17 11:49:39 -0700142 if (directCode != 0 && directMethod != 0) {
143 switch(state) {
144 case 0: // Get the current Method* [sets rARG0]
Ian Rogers3fa13792012-03-18 15:53:45 -0700145 if (directCode != (uintptr_t)-1) {
146 loadConstant(cUnit, rINVOKE_TGT, directCode);
147 } else {
148 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
149 if (dataTarget == NULL) {
150 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
151 }
152#if defined(TARGET_ARM)
153 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
154 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
155 oatAppendLIR(cUnit, loadPcRel);
156#else
157 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
158#endif
159 }
160 if (directMethod != (uintptr_t)-1) {
161 loadConstant(cUnit, rARG0, directMethod);
162 } else {
163 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
164 if (dataTarget == NULL) {
165 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
166 }
167#if defined(TARGET_ARM)
168 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
169 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0, dataTarget);
170 oatAppendLIR(cUnit, loadPcRel);
171#else
172 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
173#endif
174 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700175 break;
176 default:
177 return -1;
178 }
179 } else {
180 switch(state) {
buzbee31a4a6f2012-02-28 15:36:15 -0800181 case 0: // Get the current Method* [sets rARG0]
buzbee9c044ce2012-03-18 13:24:07 -0700182 // TUNING: we can save a reg copy if Method* has been promoted
buzbee31a4a6f2012-02-28 15:36:15 -0800183 loadCurrMethodDirect(cUnit, rARG0);
184 break;
185 case 1: // Get method->dex_cache_resolved_methods_
186 loadWordDisp(cUnit, rARG0,
187 Method::DexCacheResolvedMethodsOffset().Int32Value(),
188 rARG0);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700189 // Set up direct code if known.
190 if (directCode != 0) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700191 if (directCode != (uintptr_t)-1) {
192 loadConstant(cUnit, rINVOKE_TGT, directCode);
193 } else {
194 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
195 if (dataTarget == NULL) {
196 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
197 }
198#if defined(TARGET_ARM)
199 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
200 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
201 oatAppendLIR(cUnit, loadPcRel);
202#else
203 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
204#endif
205 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700206 }
buzbee31a4a6f2012-02-28 15:36:15 -0800207 break;
208 case 2: // Grab target method*
209 loadWordDisp(cUnit, rARG0,
210 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
211 rARG0);
212 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700213#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800214 case 3: // Grab the code from the method*
Ian Rogers2ed3b952012-03-17 11:49:39 -0700215 if (directCode == 0) {
216 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
217 rINVOKE_TGT);
218 }
buzbee31a4a6f2012-02-28 15:36:15 -0800219 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700220#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800221 default:
222 return -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700223 }
buzbee31a4a6f2012-02-28 15:36:15 -0800224 }
225 return state + 1;
226}
227
228/*
229 * Bit of a hack here - in leiu of a real scheduling pass,
230 * emit the next instruction in a virtual invoke sequence.
231 * We can use rLR as a temp prior to target address loading
232 * Note also that we'll load the first argument ("this") into
233 * rARG1 here rather than the standard loadArgRegs.
234 */
235int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700236 int state, uint32_t dexIdx, uint32_t methodIdx,
237 uintptr_t unused, uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800238{
239 RegLocation rlArg;
240 /*
241 * This is the fast path in which the target virtual method is
242 * fully resolved at compile time.
243 */
244 switch(state) {
245 case 0: // Get "this" [set rARG1]
246 rlArg = oatGetSrc(cUnit, mir, 0);
247 loadValueDirectFixed(cUnit, rlArg, rARG1);
248 break;
249 case 1: // Is "this" null? [use rARG1]
250 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
buzbee0398c422012-03-02 15:22:47 -0800251 // get this->klass_ [use rARG1, set rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800252 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800253 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800254 break;
buzbee0398c422012-03-02 15:22:47 -0800255 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
256 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
257 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800258 break;
buzbee0398c422012-03-02 15:22:47 -0800259 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
260 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
buzbee31a4a6f2012-02-28 15:36:15 -0800261 Array::DataOffset(sizeof(Object*)).Int32Value(),
262 rARG0);
263 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700264#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -0800265 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800266 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800267 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800268 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700269#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800270 default:
271 return -1;
272 }
273 return state + 1;
274}
275
buzbee31a4a6f2012-02-28 15:36:15 -0800276int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
277 int state, uint32_t dexIdx, uint32_t methodIdx)
278{
279 /*
280 * This handles the case in which the base method is not fully
281 * resolved at compile time, we bail to a runtime helper.
282 */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700283#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800284 if (state == 0) {
285 // Load trampoline target
buzbee0398c422012-03-02 15:22:47 -0800286 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800287 // Load rARG0 with method index
288 loadConstant(cUnit, rARG0, dexIdx);
289 return 1;
290 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700291#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800292 return -1;
293}
294
295int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700296 int state, uint32_t dexIdx, uint32_t methodIdx,
297 uintptr_t unused, uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800298{
299 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeStaticTrampolineWithAccessCheck);
300 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
301}
302
303int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700304 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
305 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800306{
307 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeDirectTrampolineWithAccessCheck);
308 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
309}
310
311int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700312 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
313 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800314{
315 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
316 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
317}
318
319int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700320 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
321 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800322{
323 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
324 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
325}
326
327/*
328 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
329 * which will locate the target and continue on via a tail call.
330 */
331int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700332 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
333 uintptr_t unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800334{
335 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
336 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
337}
338
339int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit, MIR* mir,
340 int state, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700341 uint32_t unused, uintptr_t unused2,
342 uintptr_t unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800343{
344 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
345 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
346}
347
348int loadArgRegs(CompilationUnit* cUnit, MIR* mir, DecodedInstruction* dInsn,
349 int callState, NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700350 uint32_t methodIdx, uintptr_t directCode,
351 uintptr_t directMethod, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800352{
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700353#if !defined(TARGET_X86)
354 int lastArgReg = rARG3;
355#else
356 int lastArgReg = rARG2;
357#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800358 int nextReg = rARG1;
359 int nextArg = 0;
360 if (skipThis) {
361 nextReg++;
362 nextArg++;
363 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700364 for (; (nextReg <= lastArgReg) && (nextArg < mir->ssaRep->numUses); nextReg++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800365 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
366 rlArg = oatUpdateRawLoc(cUnit, rlArg);
367 if (rlArg.wide && (nextReg <= rARG2)) {
368 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
369 nextReg++;
370 nextArg++;
371 } else {
372 rlArg.wide = false;
373 loadValueDirectFixed(cUnit, rlArg, nextReg);
374 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700375 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
376 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800377 }
378 return callState;
379}
380
381/*
382 * Load up to 5 arguments, the first three of which will be in
383 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
384 * and as part of the load sequence, it must be replaced with
385 * the target method pointer. Note, this may also be called
386 * for "range" variants if the number of arguments is 5 or fewer.
387 */
388int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
389 DecodedInstruction* dInsn, int callState,
390 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700391 uint32_t dexIdx, uint32_t methodIdx,
392 uintptr_t directCode, uintptr_t directMethod,
393 bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800394{
395 RegLocation rlArg;
396
397 /* If no arguments, just return */
398 if (dInsn->vA == 0)
399 return callState;
400
Ian Rogers2ed3b952012-03-17 11:49:39 -0700401 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
402 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800403
404 DCHECK_LE(dInsn->vA, 5U);
405 if (dInsn->vA > 3) {
406 uint32_t nextUse = 3;
407 //Detect special case of wide arg spanning arg3/arg4
408 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
409 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
410 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
411 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
412 rlUse2.wide) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700413 int reg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800414 // Wide spans, we need the 2nd half of uses[2].
415 rlArg = oatUpdateLocWide(cUnit, rlUse2);
416 if (rlArg.location == kLocPhysReg) {
417 reg = rlArg.highReg;
418 } else {
419 // rARG2 & rARG3 can safely be used here
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700420#if defined(TARGET_X86)
421 UNIMPLEMENTED(FATAL);
422#else
buzbee31a4a6f2012-02-28 15:36:15 -0800423 reg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700424#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800425 loadWordDisp(cUnit, rSP,
426 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
427 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700428 methodIdx, directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800429 }
430 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
431 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700432 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
433 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800434 nextUse++;
435 }
436 // Loop through the rest
437 while (nextUse < dInsn->vA) {
438 int lowReg;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700439 int highReg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800440 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
441 rlArg = oatUpdateRawLoc(cUnit, rlArg);
442 if (rlArg.location == kLocPhysReg) {
443 lowReg = rlArg.lowReg;
444 highReg = rlArg.highReg;
445 } else {
446 lowReg = rARG2;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700447#if defined(TARGET_X86)
448 UNIMPLEMENTED(FATAL);
449#else
buzbee31a4a6f2012-02-28 15:36:15 -0800450 highReg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700451#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800452 if (rlArg.wide) {
453 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
454 } else {
455 loadValueDirectFixed(cUnit, rlArg, lowReg);
456 }
457 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700458 methodIdx, directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800459 }
460 int outsOffset = (nextUse + 1) * 4;
461 if (rlArg.wide) {
462 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
463 nextUse += 2;
464 } else {
465 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
466 nextUse++;
467 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700468 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
469 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800470 }
471 }
472
473 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700474 dexIdx, methodIdx, directCode, directMethod,
475 skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800476
477 if (pcrLabel) {
478 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
479 }
480 return callState;
481}
482
483/*
484 * May have 0+ arguments (also used for jumbo). Note that
485 * source virtual registers may be in physical registers, so may
486 * need to be flushed to home location before copying. This
487 * applies to arg3 and above (see below).
488 *
489 * Two general strategies:
490 * If < 20 arguments
491 * Pass args 3-18 using vldm/vstm block copy
492 * Pass arg0, arg1 & arg2 in rARG1-rARG3
493 * If 20+ arguments
494 * Pass args arg19+ using memcpy block copy
495 * Pass arg0, arg1 & arg2 in rARG1-rARG3
496 *
497 */
498int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
499 DecodedInstruction* dInsn, int callState,
500 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700501 uint32_t dexIdx, uint32_t methodIdx,
502 uintptr_t directCode, uintptr_t directMethod,
503 bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800504{
505 int firstArg = dInsn->vC;
506 int numArgs = dInsn->vA;
507
508 // If we can treat it as non-range (Jumbo ops will use range form)
509 if (numArgs <= 5)
510 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
511 nextCallInsn, dexIdx, methodIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700512 directCode, directMethod, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800513 /*
514 * Make sure range list doesn't span the break between in normal
515 * Dalvik vRegs and the ins.
516 */
517 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
518 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
519 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
520 LOG(FATAL) << "Argument list spanned locals & args";
521 }
522
523 /*
524 * First load the non-register arguments. Both forms expect all
525 * of the source arguments to be in their home frame location, so
526 * scan the sReg names and flush any that have been promoted to
527 * frame backing storage.
528 */
529 // Scan the rest of the args - if in physReg flush to memory
530 for (int nextArg = 0; nextArg < numArgs;) {
531 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
532 if (loc.wide) {
533 loc = oatUpdateLocWide(cUnit, loc);
534 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
535 storeBaseDispWide(cUnit, rSP,
536 oatSRegOffset(cUnit, loc.sRegLow),
537 loc.lowReg, loc.highReg);
538 }
539 nextArg += 2;
540 } else {
541 loc = oatUpdateLoc(cUnit, loc);
542 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
543 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
544 loc.lowReg, kWord);
545 }
546 nextArg++;
547 }
548 }
549
550 int startOffset = oatSRegOffset(cUnit,
551 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
552 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700553#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800554 // Generate memcpy
555 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
556 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700557 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
558 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800559#else
560 if (numArgs >= 20) {
561 // Generate memcpy
562 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
563 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700564 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
565 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800566 } else {
567 // Use vldm/vstm pair using rARG3 as a temp
568 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700569 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
570 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800571 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
572 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
573 //TUNING: loosen barrier
574 ld->defMask = ENCODE_ALL;
575 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700576 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
577 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800578 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogers2ed3b952012-03-17 11:49:39 -0700579 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
580 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800581 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
582 setMemRefType(st, false /* isLoad */, kDalvikReg);
583 st->defMask = ENCODE_ALL;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700584 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
585 directCode, directMethod);
586
buzbee31a4a6f2012-02-28 15:36:15 -0800587 }
588#endif
589
590 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700591 dexIdx, methodIdx, directCode, directMethod,
592 skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800593
Ian Rogers2ed3b952012-03-17 11:49:39 -0700594 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
595 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800596 if (pcrLabel) {
597 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
598 }
599 return callState;
600}
601
buzbee31a4a6f2012-02-28 15:36:15 -0800602} // namespace art