blob: 90e2267a5ffbcc0358c7715a4429aef8b9169dd0 [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{
36 if (cUnit->numIns == 0)
37 return;
38 int firstArgReg = rARG1;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070039#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -080040 int lastArgReg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070041#else
42 int lastArgReg = rARG2;
43#endif
buzbee31a4a6f2012-02-28 15:36:15 -080044 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
45 /*
buzbee86a4bce2012-03-06 18:15:00 -080046 * Copy incoming arguments to their proper home locations.
47 * NOTE: an older version of dx had an issue in which
48 * it would reuse static method argument registers.
buzbee31a4a6f2012-02-28 15:36:15 -080049 * This could result in the same Dalvik virtual register
buzbee86a4bce2012-03-06 18:15:00 -080050 * being promoted to both core and fp regs. To account for this,
51 * we only copy to the corresponding promoted physical register
52 * if it matches the type of the SSA name for the incoming
53 * argument. It is also possible that long and double arguments
54 * end up half-promoted. In those cases, we must flush the promoted
55 * half to memory as well.
buzbee31a4a6f2012-02-28 15:36:15 -080056 */
57 for (int i = 0; i < cUnit->numIns; i++) {
buzbee86a4bce2012-03-06 18:15:00 -080058 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
buzbee31a4a6f2012-02-28 15:36:15 -080059 if (i <= (lastArgReg - firstArgReg)) {
60 // If arriving in register
buzbee86a4bce2012-03-06 18:15:00 -080061 bool needFlush = true;
62 RegLocation* tLoc = &cUnit->regLocation[startVReg + i];
63 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
64 opRegCopy(cUnit, vMap->coreReg, firstArgReg + i);
65 needFlush = false;
66 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
67 opRegCopy(cUnit, vMap->fpReg, firstArgReg + i);
68 needFlush = false;
69 } else {
70 needFlush = true;
buzbee31a4a6f2012-02-28 15:36:15 -080071 }
buzbee86a4bce2012-03-06 18:15:00 -080072
73 // For wide args, force flush if only half is promoted
74 if (tLoc->wide) {
75 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
76 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
77 (pMap->fpLocation != vMap->fpLocation);
buzbee31a4a6f2012-02-28 15:36:15 -080078 }
buzbee86a4bce2012-03-06 18:15:00 -080079 if (needFlush) {
80 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
81 firstArgReg + i, kWord);
82 }
buzbee31a4a6f2012-02-28 15:36:15 -080083 } else {
84 // If arriving in frame & promoted
buzbee86a4bce2012-03-06 18:15:00 -080085 if (vMap->coreLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -080086 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -080087 vMap->coreReg);
buzbee31a4a6f2012-02-28 15:36:15 -080088 }
buzbee86a4bce2012-03-06 18:15:00 -080089 if (vMap->fpLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -080090 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -080091 vMap->fpReg);
buzbee31a4a6f2012-02-28 15:36:15 -080092 }
93 }
94 }
95}
96
Ian Rogers3fa13792012-03-18 15:53:45 -070097void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget, const DexFile* dexFile, uint32_t dexMethodIdx)
98{
99 LIR* curTarget = cUnit->methodLiteralList;
100 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
101 while (curTarget != NULL && nextTarget != NULL) {
102 if (curTarget->operands[0] == (int)dexFile &&
103 nextTarget->operands[0] == (int)dexMethodIdx) {
104 *codeTarget = curTarget;
105 *methodTarget = nextTarget;
106 DCHECK((*codeTarget)->next == *methodTarget);
107 DCHECK_EQ((*codeTarget)->operands[0], (int)dexFile);
108 DCHECK_EQ((*methodTarget)->operands[0], (int)dexMethodIdx);
109 break;
110 }
111 curTarget = nextTarget->next;
112 nextTarget = curTarget != NULL ? curTarget->next : NULL;
113 }
114}
115
buzbee31a4a6f2012-02-28 15:36:15 -0800116/*
117 * Bit of a hack here - in leiu of a real scheduling pass,
118 * emit the next instruction in static & direct invoke sequences.
119 */
120int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700121 int state, uint32_t dexIdx, uint32_t unused,
122 uintptr_t directCode, uintptr_t directMethod)
buzbee31a4a6f2012-02-28 15:36:15 -0800123{
Ian Rogers2ed3b952012-03-17 11:49:39 -0700124 if (directCode != 0 && directMethod != 0) {
125 switch(state) {
126 case 0: // Get the current Method* [sets rARG0]
Ian Rogers3fa13792012-03-18 15:53:45 -0700127 if (directCode != (uintptr_t)-1) {
128 loadConstant(cUnit, rINVOKE_TGT, directCode);
129 } else {
130 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
131 if (dataTarget == NULL) {
132 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
133 }
134#if defined(TARGET_ARM)
135 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
136 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
137 oatAppendLIR(cUnit, loadPcRel);
138#else
139 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
140#endif
141 }
142 if (directMethod != (uintptr_t)-1) {
143 loadConstant(cUnit, rARG0, directMethod);
144 } else {
145 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
146 if (dataTarget == NULL) {
147 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
148 }
149#if defined(TARGET_ARM)
150 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
151 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0, dataTarget);
152 oatAppendLIR(cUnit, loadPcRel);
153#else
154 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
155#endif
156 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700157 break;
158 default:
159 return -1;
160 }
161 } else {
162 switch(state) {
buzbee31a4a6f2012-02-28 15:36:15 -0800163 case 0: // Get the current Method* [sets rARG0]
164 loadCurrMethodDirect(cUnit, rARG0);
165 break;
166 case 1: // Get method->dex_cache_resolved_methods_
167 loadWordDisp(cUnit, rARG0,
168 Method::DexCacheResolvedMethodsOffset().Int32Value(),
169 rARG0);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700170 // Set up direct code if known.
171 if (directCode != 0) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700172 if (directCode != (uintptr_t)-1) {
173 loadConstant(cUnit, rINVOKE_TGT, directCode);
174 } else {
175 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
176 if (dataTarget == NULL) {
177 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
178 }
179#if defined(TARGET_ARM)
180 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
181 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
182 oatAppendLIR(cUnit, loadPcRel);
183#else
184 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
185#endif
186 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700187 }
buzbee31a4a6f2012-02-28 15:36:15 -0800188 break;
189 case 2: // Grab target method*
190 loadWordDisp(cUnit, rARG0,
191 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
192 rARG0);
193 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700194#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800195 case 3: // Grab the code from the method*
Ian Rogers2ed3b952012-03-17 11:49:39 -0700196 if (directCode == 0) {
197 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
198 rINVOKE_TGT);
199 }
buzbee31a4a6f2012-02-28 15:36:15 -0800200 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700201#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800202 default:
203 return -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700204 }
buzbee31a4a6f2012-02-28 15:36:15 -0800205 }
206 return state + 1;
207}
208
209/*
210 * Bit of a hack here - in leiu of a real scheduling pass,
211 * emit the next instruction in a virtual invoke sequence.
212 * We can use rLR as a temp prior to target address loading
213 * Note also that we'll load the first argument ("this") into
214 * rARG1 here rather than the standard loadArgRegs.
215 */
216int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700217 int state, uint32_t dexIdx, uint32_t methodIdx,
218 uintptr_t unused, uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800219{
220 RegLocation rlArg;
221 /*
222 * This is the fast path in which the target virtual method is
223 * fully resolved at compile time.
224 */
225 switch(state) {
226 case 0: // Get "this" [set rARG1]
227 rlArg = oatGetSrc(cUnit, mir, 0);
228 loadValueDirectFixed(cUnit, rlArg, rARG1);
229 break;
230 case 1: // Is "this" null? [use rARG1]
231 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
buzbee0398c422012-03-02 15:22:47 -0800232 // get this->klass_ [use rARG1, set rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800233 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800234 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800235 break;
buzbee0398c422012-03-02 15:22:47 -0800236 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
237 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
238 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800239 break;
buzbee0398c422012-03-02 15:22:47 -0800240 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
241 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
buzbee31a4a6f2012-02-28 15:36:15 -0800242 Array::DataOffset(sizeof(Object*)).Int32Value(),
243 rARG0);
244 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700245#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -0800246 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800247 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800248 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800249 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700250#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800251 default:
252 return -1;
253 }
254 return state + 1;
255}
256
buzbee31a4a6f2012-02-28 15:36:15 -0800257int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
258 int state, uint32_t dexIdx, uint32_t methodIdx)
259{
260 /*
261 * This handles the case in which the base method is not fully
262 * resolved at compile time, we bail to a runtime helper.
263 */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700264#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800265 if (state == 0) {
266 // Load trampoline target
buzbee0398c422012-03-02 15:22:47 -0800267 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800268 // Load rARG0 with method index
269 loadConstant(cUnit, rARG0, dexIdx);
270 return 1;
271 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700272#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800273 return -1;
274}
275
276int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700277 int state, uint32_t dexIdx, uint32_t methodIdx,
278 uintptr_t unused, uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800279{
280 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeStaticTrampolineWithAccessCheck);
281 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
282}
283
284int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700285 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
286 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800287{
288 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeDirectTrampolineWithAccessCheck);
289 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
290}
291
292int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700293 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
294 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800295{
296 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
297 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
298}
299
300int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700301 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
302 uintptr_t unused2)
buzbee31a4a6f2012-02-28 15:36:15 -0800303{
304 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
305 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
306}
307
308/*
309 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
310 * which will locate the target and continue on via a tail call.
311 */
312int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700313 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
314 uintptr_t unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800315{
316 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
317 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
318}
319
320int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit, MIR* mir,
321 int state, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700322 uint32_t unused, uintptr_t unused2,
323 uintptr_t unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800324{
325 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
326 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
327}
328
329int loadArgRegs(CompilationUnit* cUnit, MIR* mir, DecodedInstruction* dInsn,
330 int callState, NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700331 uint32_t methodIdx, uintptr_t directCode,
332 uintptr_t directMethod, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800333{
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700334#if !defined(TARGET_X86)
335 int lastArgReg = rARG3;
336#else
337 int lastArgReg = rARG2;
338#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800339 int nextReg = rARG1;
340 int nextArg = 0;
341 if (skipThis) {
342 nextReg++;
343 nextArg++;
344 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700345 for (; (nextReg <= lastArgReg) && (nextArg < mir->ssaRep->numUses); nextReg++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800346 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
347 rlArg = oatUpdateRawLoc(cUnit, rlArg);
348 if (rlArg.wide && (nextReg <= rARG2)) {
349 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
350 nextReg++;
351 nextArg++;
352 } else {
353 rlArg.wide = false;
354 loadValueDirectFixed(cUnit, rlArg, nextReg);
355 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700356 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
357 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800358 }
359 return callState;
360}
361
362/*
363 * Load up to 5 arguments, the first three of which will be in
364 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
365 * and as part of the load sequence, it must be replaced with
366 * the target method pointer. Note, this may also be called
367 * for "range" variants if the number of arguments is 5 or fewer.
368 */
369int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
370 DecodedInstruction* dInsn, int callState,
371 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700372 uint32_t dexIdx, uint32_t methodIdx,
373 uintptr_t directCode, uintptr_t directMethod,
374 bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800375{
376 RegLocation rlArg;
377
378 /* If no arguments, just return */
379 if (dInsn->vA == 0)
380 return callState;
381
Ian Rogers2ed3b952012-03-17 11:49:39 -0700382 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
383 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800384
385 DCHECK_LE(dInsn->vA, 5U);
386 if (dInsn->vA > 3) {
387 uint32_t nextUse = 3;
388 //Detect special case of wide arg spanning arg3/arg4
389 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
390 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
391 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
392 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
393 rlUse2.wide) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700394 int reg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800395 // Wide spans, we need the 2nd half of uses[2].
396 rlArg = oatUpdateLocWide(cUnit, rlUse2);
397 if (rlArg.location == kLocPhysReg) {
398 reg = rlArg.highReg;
399 } else {
400 // rARG2 & rARG3 can safely be used here
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700401#if defined(TARGET_X86)
402 UNIMPLEMENTED(FATAL);
403#else
buzbee31a4a6f2012-02-28 15:36:15 -0800404 reg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700405#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800406 loadWordDisp(cUnit, rSP,
407 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
408 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700409 methodIdx, directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800410 }
411 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
412 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700413 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
414 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800415 nextUse++;
416 }
417 // Loop through the rest
418 while (nextUse < dInsn->vA) {
419 int lowReg;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700420 int highReg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800421 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
422 rlArg = oatUpdateRawLoc(cUnit, rlArg);
423 if (rlArg.location == kLocPhysReg) {
424 lowReg = rlArg.lowReg;
425 highReg = rlArg.highReg;
426 } else {
427 lowReg = rARG2;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700428#if defined(TARGET_X86)
429 UNIMPLEMENTED(FATAL);
430#else
buzbee31a4a6f2012-02-28 15:36:15 -0800431 highReg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700432#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800433 if (rlArg.wide) {
434 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
435 } else {
436 loadValueDirectFixed(cUnit, rlArg, lowReg);
437 }
438 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700439 methodIdx, directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800440 }
441 int outsOffset = (nextUse + 1) * 4;
442 if (rlArg.wide) {
443 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
444 nextUse += 2;
445 } else {
446 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
447 nextUse++;
448 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700449 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
450 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800451 }
452 }
453
454 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700455 dexIdx, methodIdx, directCode, directMethod,
456 skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800457
458 if (pcrLabel) {
459 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
460 }
461 return callState;
462}
463
464/*
465 * May have 0+ arguments (also used for jumbo). Note that
466 * source virtual registers may be in physical registers, so may
467 * need to be flushed to home location before copying. This
468 * applies to arg3 and above (see below).
469 *
470 * Two general strategies:
471 * If < 20 arguments
472 * Pass args 3-18 using vldm/vstm block copy
473 * Pass arg0, arg1 & arg2 in rARG1-rARG3
474 * If 20+ arguments
475 * Pass args arg19+ using memcpy block copy
476 * Pass arg0, arg1 & arg2 in rARG1-rARG3
477 *
478 */
479int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
480 DecodedInstruction* dInsn, int callState,
481 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700482 uint32_t dexIdx, uint32_t methodIdx,
483 uintptr_t directCode, uintptr_t directMethod,
484 bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800485{
486 int firstArg = dInsn->vC;
487 int numArgs = dInsn->vA;
488
489 // If we can treat it as non-range (Jumbo ops will use range form)
490 if (numArgs <= 5)
491 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
492 nextCallInsn, dexIdx, methodIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700493 directCode, directMethod, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800494 /*
495 * Make sure range list doesn't span the break between in normal
496 * Dalvik vRegs and the ins.
497 */
498 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
499 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
500 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
501 LOG(FATAL) << "Argument list spanned locals & args";
502 }
503
504 /*
505 * First load the non-register arguments. Both forms expect all
506 * of the source arguments to be in their home frame location, so
507 * scan the sReg names and flush any that have been promoted to
508 * frame backing storage.
509 */
510 // Scan the rest of the args - if in physReg flush to memory
511 for (int nextArg = 0; nextArg < numArgs;) {
512 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
513 if (loc.wide) {
514 loc = oatUpdateLocWide(cUnit, loc);
515 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
516 storeBaseDispWide(cUnit, rSP,
517 oatSRegOffset(cUnit, loc.sRegLow),
518 loc.lowReg, loc.highReg);
519 }
520 nextArg += 2;
521 } else {
522 loc = oatUpdateLoc(cUnit, loc);
523 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
524 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
525 loc.lowReg, kWord);
526 }
527 nextArg++;
528 }
529 }
530
531 int startOffset = oatSRegOffset(cUnit,
532 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
533 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700534#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800535 // Generate memcpy
536 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
537 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700538 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
539 rARG0, rARG1, (numArgs - 3) * 4);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700540 // Restore Method*
541 loadCurrMethodDirect(cUnit, rARG0);
buzbee31a4a6f2012-02-28 15:36:15 -0800542#else
543 if (numArgs >= 20) {
544 // Generate memcpy
545 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
546 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700547 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
548 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800549 // Restore Method*
550 loadCurrMethodDirect(cUnit, rARG0);
551 } else {
552 // Use vldm/vstm pair using rARG3 as a temp
553 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700554 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
555 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800556 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
557 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
558 //TUNING: loosen barrier
559 ld->defMask = ENCODE_ALL;
560 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700561 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
562 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800563 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogers2ed3b952012-03-17 11:49:39 -0700564 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
565 directCode, directMethod);
buzbee31a4a6f2012-02-28 15:36:15 -0800566 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
567 setMemRefType(st, false /* isLoad */, kDalvikReg);
568 st->defMask = ENCODE_ALL;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700569 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
570 directCode, directMethod);
571
buzbee31a4a6f2012-02-28 15:36:15 -0800572 }
573#endif
574
575 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700576 dexIdx, methodIdx, directCode, directMethod,
577 skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800578
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 if (pcrLabel) {
582 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
583 }
584 return callState;
585}
586
buzbee31a4a6f2012-02-28 15:36:15 -0800587} // namespace art