blob: 037a9bb5df566608232d1f8b5fe0bbef330b0194 [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
buzbee31a4a6f2012-02-28 15:36:15 -080025typedef int (*NextCallInsn)(CompilationUnit*, MIR*, int, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -070026 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -070027 uintptr_t directMethod, InvokeType type);
buzbee31a4a6f2012-02-28 15:36:15 -080028/*
29 * If there are any ins passed in registers that have not been promoted
30 * to a callee-save register, flush them to the frame. Perform intial
31 * assignment of promoted arguments.
32 */
33void flushIns(CompilationUnit* cUnit)
34{
buzbee9c044ce2012-03-18 13:24:07 -070035 /*
36 * Dummy up a RegLocation for the incoming Method*
37 * It will attempt to keep rARG0 live (or copy it to home location
38 * if promoted).
39 */
40 RegLocation rlSrc = cUnit->regLocation[cUnit->methodSReg];
41 RegLocation rlMethod = cUnit->regLocation[cUnit->methodSReg];
42 rlSrc.location = kLocPhysReg;
43 rlSrc.lowReg = rARG0;
44 rlSrc.home = false;
45 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
46 storeValue(cUnit, rlMethod, rlSrc);
47 // If Method* has been promoted, explicitly flush
48 if (rlMethod.location == kLocPhysReg) {
49 storeWordDisp(cUnit, rSP, 0, rARG0);
50 }
51
buzbee31a4a6f2012-02-28 15:36:15 -080052 if (cUnit->numIns == 0)
53 return;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070054#if !defined(TARGET_X86)
Ian Rogersb3ab25b2012-03-19 01:12:01 -070055 const int numArgRegs = 3;
56 static int argRegs[] = {rARG1, rARG2, rARG3};
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070057#else
Ian Rogersb3ab25b2012-03-19 01:12:01 -070058 const int numArgRegs = 2;
59 static int argRegs[] = {rARG1, rARG2};
Ian Rogers6cbb2bd2012-03-16 13:45:30 -070060#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];
Ian Rogersb3ab25b2012-03-19 01:12:01 -070076 if (i < numArgRegs) {
buzbee31a4a6f2012-02-28 15:36:15 -080077 // 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) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070081 opRegCopy(cUnit, vMap->coreReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080082 needFlush = false;
83 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070084 opRegCopy(cUnit, vMap->fpReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080085 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),
Ian Rogersb3ab25b2012-03-19 01:12:01 -070098 argRegs[i], kWord);
buzbee86a4bce2012-03-06 18:15:00 -080099 }
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/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700134 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800135 * 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,
Brian Carlstromf5822582012-03-19 22:34:31 -0700139 uintptr_t directCode, uintptr_t directMethod,
140 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800141{
Brian Carlstromf5822582012-03-19 22:34:31 -0700142#if !defined(TARGET_ARM)
143 directCode = 0;
144 directMethod = 0;
145#endif
Ian Rogers2ed3b952012-03-17 11:49:39 -0700146 if (directCode != 0 && directMethod != 0) {
147 switch(state) {
148 case 0: // Get the current Method* [sets rARG0]
Ian Rogers3fa13792012-03-18 15:53:45 -0700149 if (directCode != (uintptr_t)-1) {
150 loadConstant(cUnit, rINVOKE_TGT, directCode);
151 } else {
152 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
153 if (dataTarget == NULL) {
154 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700155 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700156 }
157#if defined(TARGET_ARM)
158 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
159 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
160 oatAppendLIR(cUnit, loadPcRel);
161#else
162 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
163#endif
164 }
165 if (directMethod != (uintptr_t)-1) {
166 loadConstant(cUnit, rARG0, directMethod);
167 } else {
168 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
169 if (dataTarget == NULL) {
170 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700171 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700172 }
173#if defined(TARGET_ARM)
174 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
175 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0, dataTarget);
176 oatAppendLIR(cUnit, loadPcRel);
177#else
178 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
179#endif
180 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700181 break;
182 default:
183 return -1;
184 }
185 } else {
186 switch(state) {
buzbee31a4a6f2012-02-28 15:36:15 -0800187 case 0: // Get the current Method* [sets rARG0]
buzbee9c044ce2012-03-18 13:24:07 -0700188 // TUNING: we can save a reg copy if Method* has been promoted
buzbee31a4a6f2012-02-28 15:36:15 -0800189 loadCurrMethodDirect(cUnit, rARG0);
190 break;
191 case 1: // Get method->dex_cache_resolved_methods_
192 loadWordDisp(cUnit, rARG0,
193 Method::DexCacheResolvedMethodsOffset().Int32Value(),
194 rARG0);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700195 // Set up direct code if known.
196 if (directCode != 0) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700197 if (directCode != (uintptr_t)-1) {
198 loadConstant(cUnit, rINVOKE_TGT, directCode);
199 } else {
200 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
201 if (dataTarget == NULL) {
202 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700203 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700204 }
205#if defined(TARGET_ARM)
206 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
207 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
208 oatAppendLIR(cUnit, loadPcRel);
209#else
210 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
211#endif
212 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700213 }
buzbee31a4a6f2012-02-28 15:36:15 -0800214 break;
215 case 2: // Grab target method*
216 loadWordDisp(cUnit, rARG0,
217 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
218 rARG0);
219 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700220#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800221 case 3: // Grab the code from the method*
Ian Rogers2ed3b952012-03-17 11:49:39 -0700222 if (directCode == 0) {
223 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
224 rINVOKE_TGT);
225 }
buzbee31a4a6f2012-02-28 15:36:15 -0800226 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700227#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800228 default:
229 return -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700230 }
buzbee31a4a6f2012-02-28 15:36:15 -0800231 }
232 return state + 1;
233}
234
235/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700236 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800237 * emit the next instruction in a virtual invoke sequence.
238 * We can use rLR as a temp prior to target address loading
239 * Note also that we'll load the first argument ("this") into
240 * rARG1 here rather than the standard loadArgRegs.
241 */
242int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700243 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700244 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800245{
246 RegLocation rlArg;
247 /*
248 * This is the fast path in which the target virtual method is
249 * fully resolved at compile time.
250 */
251 switch(state) {
252 case 0: // Get "this" [set rARG1]
253 rlArg = oatGetSrc(cUnit, mir, 0);
254 loadValueDirectFixed(cUnit, rlArg, rARG1);
255 break;
256 case 1: // Is "this" null? [use rARG1]
257 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
buzbee0398c422012-03-02 15:22:47 -0800258 // get this->klass_ [use rARG1, set rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800259 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800260 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800261 break;
buzbee0398c422012-03-02 15:22:47 -0800262 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
263 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
264 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800265 break;
buzbee0398c422012-03-02 15:22:47 -0800266 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
267 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
buzbee31a4a6f2012-02-28 15:36:15 -0800268 Array::DataOffset(sizeof(Object*)).Int32Value(),
269 rARG0);
270 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700271#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -0800272 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800273 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800274 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800275 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700276#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800277 default:
278 return -1;
279 }
280 return state + 1;
281}
282
buzbee31a4a6f2012-02-28 15:36:15 -0800283int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
284 int state, uint32_t dexIdx, uint32_t methodIdx)
285{
286 /*
287 * This handles the case in which the base method is not fully
288 * resolved at compile time, we bail to a runtime helper.
289 */
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700290#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800291 if (state == 0) {
292 // Load trampoline target
buzbee0398c422012-03-02 15:22:47 -0800293 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800294 // Load rARG0 with method index
295 loadConstant(cUnit, rARG0, dexIdx);
296 return 1;
297 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700298#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800299 return -1;
300}
301
302int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700303 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700304 uintptr_t unused, uintptr_t unused2,
305 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800306{
307 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeStaticTrampolineWithAccessCheck);
308 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
309}
310
311int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700312 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700313 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800314{
315 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeDirectTrampolineWithAccessCheck);
316 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
317}
318
319int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700320 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700321 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800322{
323 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeSuperTrampolineWithAccessCheck);
324 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
325}
326
327int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700328 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700329 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800330{
331 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeVirtualTrampolineWithAccessCheck);
332 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
333}
334
335/*
336 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
337 * which will locate the target and continue on via a tail call.
338 */
339int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700340 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700341 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800342{
343 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampoline);
344 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
345}
346
347int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit, MIR* mir,
348 int state, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700349 uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700350 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800351{
352 int trampoline = OFFSETOF_MEMBER(Thread, pInvokeInterfaceTrampolineWithAccessCheck);
353 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
354}
355
356int loadArgRegs(CompilationUnit* cUnit, MIR* mir, DecodedInstruction* dInsn,
357 int callState, NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700358 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -0700359 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800360{
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700361#if !defined(TARGET_X86)
362 int lastArgReg = rARG3;
363#else
364 int lastArgReg = rARG2;
365#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800366 int nextReg = rARG1;
367 int nextArg = 0;
368 if (skipThis) {
369 nextReg++;
370 nextArg++;
371 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700372 for (; (nextReg <= lastArgReg) && (nextArg < mir->ssaRep->numUses); nextReg++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800373 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
374 rlArg = oatUpdateRawLoc(cUnit, rlArg);
375 if (rlArg.wide && (nextReg <= rARG2)) {
376 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
377 nextReg++;
378 nextArg++;
379 } else {
380 rlArg.wide = false;
381 loadValueDirectFixed(cUnit, rlArg, nextReg);
382 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700383 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700384 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800385 }
386 return callState;
387}
388
389/*
390 * Load up to 5 arguments, the first three of which will be in
391 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
392 * and as part of the load sequence, it must be replaced with
393 * the target method pointer. Note, this may also be called
394 * for "range" variants if the number of arguments is 5 or fewer.
395 */
396int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
397 DecodedInstruction* dInsn, int callState,
398 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700399 uint32_t dexIdx, uint32_t methodIdx,
400 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700401 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800402{
403 RegLocation rlArg;
404
405 /* If no arguments, just return */
406 if (dInsn->vA == 0)
407 return callState;
408
Ian Rogers2ed3b952012-03-17 11:49:39 -0700409 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700410 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800411
412 DCHECK_LE(dInsn->vA, 5U);
413 if (dInsn->vA > 3) {
414 uint32_t nextUse = 3;
415 //Detect special case of wide arg spanning arg3/arg4
416 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
417 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
418 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
419 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
420 rlUse2.wide) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700421 int reg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800422 // Wide spans, we need the 2nd half of uses[2].
423 rlArg = oatUpdateLocWide(cUnit, rlUse2);
424 if (rlArg.location == kLocPhysReg) {
425 reg = rlArg.highReg;
426 } else {
427 // rARG2 & rARG3 can safely be used here
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700428#if defined(TARGET_X86)
429 UNIMPLEMENTED(FATAL);
430#else
buzbee31a4a6f2012-02-28 15:36:15 -0800431 reg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700432#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800433 loadWordDisp(cUnit, rSP,
434 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
435 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700436 methodIdx, directCode, directMethod,
437 type);
buzbee31a4a6f2012-02-28 15:36:15 -0800438 }
439 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
440 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700441 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700442 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800443 nextUse++;
444 }
445 // Loop through the rest
446 while (nextUse < dInsn->vA) {
447 int lowReg;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700448 int highReg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800449 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
450 rlArg = oatUpdateRawLoc(cUnit, rlArg);
451 if (rlArg.location == kLocPhysReg) {
452 lowReg = rlArg.lowReg;
453 highReg = rlArg.highReg;
454 } else {
455 lowReg = rARG2;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700456#if defined(TARGET_X86)
457 UNIMPLEMENTED(FATAL);
458#else
buzbee31a4a6f2012-02-28 15:36:15 -0800459 highReg = rARG3;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700460#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800461 if (rlArg.wide) {
462 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
463 } else {
464 loadValueDirectFixed(cUnit, rlArg, lowReg);
465 }
466 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700467 methodIdx, directCode, directMethod,
468 type);
buzbee31a4a6f2012-02-28 15:36:15 -0800469 }
470 int outsOffset = (nextUse + 1) * 4;
471 if (rlArg.wide) {
472 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
473 nextUse += 2;
474 } else {
475 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
476 nextUse++;
477 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700478 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700479 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800480 }
481 }
482
483 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700484 dexIdx, methodIdx, directCode, directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700485 type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800486
487 if (pcrLabel) {
488 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
489 }
490 return callState;
491}
492
493/*
494 * May have 0+ arguments (also used for jumbo). Note that
495 * source virtual registers may be in physical registers, so may
496 * need to be flushed to home location before copying. This
497 * applies to arg3 and above (see below).
498 *
499 * Two general strategies:
500 * If < 20 arguments
501 * Pass args 3-18 using vldm/vstm block copy
502 * Pass arg0, arg1 & arg2 in rARG1-rARG3
503 * If 20+ arguments
504 * Pass args arg19+ using memcpy block copy
505 * Pass arg0, arg1 & arg2 in rARG1-rARG3
506 *
507 */
508int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
509 DecodedInstruction* dInsn, int callState,
510 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700511 uint32_t dexIdx, uint32_t methodIdx,
512 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700513 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800514{
515 int firstArg = dInsn->vC;
516 int numArgs = dInsn->vA;
517
518 // If we can treat it as non-range (Jumbo ops will use range form)
519 if (numArgs <= 5)
520 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
521 nextCallInsn, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700522 directCode, directMethod, type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800523 /*
524 * Make sure range list doesn't span the break between in normal
525 * Dalvik vRegs and the ins.
526 */
527 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
528 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
529 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
530 LOG(FATAL) << "Argument list spanned locals & args";
531 }
532
533 /*
534 * First load the non-register arguments. Both forms expect all
535 * of the source arguments to be in their home frame location, so
536 * scan the sReg names and flush any that have been promoted to
537 * frame backing storage.
538 */
539 // Scan the rest of the args - if in physReg flush to memory
540 for (int nextArg = 0; nextArg < numArgs;) {
541 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
542 if (loc.wide) {
543 loc = oatUpdateLocWide(cUnit, loc);
544 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
545 storeBaseDispWide(cUnit, rSP,
546 oatSRegOffset(cUnit, loc.sRegLow),
547 loc.lowReg, loc.highReg);
548 }
549 nextArg += 2;
550 } else {
551 loc = oatUpdateLoc(cUnit, loc);
552 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
553 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
554 loc.lowReg, kWord);
555 }
556 nextArg++;
557 }
558 }
559
560 int startOffset = oatSRegOffset(cUnit,
561 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
562 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700563#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800564 // Generate memcpy
565 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
566 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700567 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
568 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800569#else
570 if (numArgs >= 20) {
571 // Generate memcpy
572 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
573 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700574 callRuntimeHelperRegRegImm(cUnit, OFFSETOF_MEMBER(Thread, pMemcpy),
575 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800576 } else {
577 // Use vldm/vstm pair using rARG3 as a temp
578 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700579 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700580 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800581 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
582 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
583 //TUNING: loosen barrier
584 ld->defMask = ENCODE_ALL;
585 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700586 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700587 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800588 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogers2ed3b952012-03-17 11:49:39 -0700589 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700590 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800591 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
592 setMemRefType(st, false /* isLoad */, kDalvikReg);
593 st->defMask = ENCODE_ALL;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700594 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700595 directCode, directMethod, type);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700596
buzbee31a4a6f2012-02-28 15:36:15 -0800597 }
598#endif
599
600 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700601 dexIdx, methodIdx, directCode, directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700602 type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800603
Ian Rogers2ed3b952012-03-17 11:49:39 -0700604 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700605 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800606 if (pcrLabel) {
607 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
608 }
609 return callState;
610}
611
buzbee31a4a6f2012-02-28 15:36:15 -0800612} // namespace art