blob: 21ccae7940ed8ab34281a9e534bab10231dc64bb [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
Ian Rogers57b86d42012-03-27 16:05:41 -070017#include "oat/runtime/oat_support_entrypoints.h"
18
buzbee31a4a6f2012-02-28 15:36:15 -080019namespace art {
20
21/*
22 * This source files contains "gen" codegen routines that should
23 * be applicable to most targets. Only mid-level support utilities
24 * and "op" calls may be used here.
25 */
26
buzbee15bf9802012-06-12 17:49:27 -070027typedef int (*NextCallInsn)(CompilationUnit*, InvokeInfo*, int, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -070028 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -070029 uintptr_t directMethod, InvokeType type);
buzbeefc9e6fa2012-03-23 15:14:29 -070030LIR* opCondBranch(CompilationUnit* cUnit, ConditionCode cc, LIR* target);
31
buzbee31a4a6f2012-02-28 15:36:15 -080032/*
33 * If there are any ins passed in registers that have not been promoted
34 * to a callee-save register, flush them to the frame. Perform intial
35 * assignment of promoted arguments.
36 */
37void flushIns(CompilationUnit* cUnit)
38{
Bill Buzbeea114add2012-05-03 15:00:40 -070039 /*
40 * Dummy up a RegLocation for the incoming Method*
41 * It will attempt to keep rARG0 live (or copy it to home location
42 * if promoted).
43 */
44 RegLocation rlSrc = cUnit->regLocation[cUnit->methodSReg];
45 RegLocation rlMethod = cUnit->regLocation[cUnit->methodSReg];
46 rlSrc.location = kLocPhysReg;
47 rlSrc.lowReg = rARG0;
48 rlSrc.home = false;
49 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
50 storeValue(cUnit, rlMethod, rlSrc);
51 // If Method* has been promoted, explicitly flush
52 if (rlMethod.location == kLocPhysReg) {
53 storeWordDisp(cUnit, rSP, 0, rARG0);
54 }
buzbee9c044ce2012-03-18 13:24:07 -070055
Bill Buzbeea114add2012-05-03 15:00:40 -070056 if (cUnit->numIns == 0)
57 return;
58 const int numArgRegs = 3;
59 static int argRegs[] = {rARG1, rARG2, rARG3};
60 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
61 /*
62 * Copy incoming arguments to their proper home locations.
63 * NOTE: an older version of dx had an issue in which
64 * it would reuse static method argument registers.
65 * This could result in the same Dalvik virtual register
66 * being promoted to both core and fp regs. To account for this,
67 * we only copy to the corresponding promoted physical register
68 * if it matches the type of the SSA name for the incoming
69 * argument. It is also possible that long and double arguments
70 * end up half-promoted. In those cases, we must flush the promoted
71 * half to memory as well.
72 */
73 for (int i = 0; i < cUnit->numIns; i++) {
74 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
75 if (i < numArgRegs) {
76 // If arriving in register
77 bool needFlush = true;
78 RegLocation* tLoc = &cUnit->regLocation[startVReg + i];
79 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
80 opRegCopy(cUnit, vMap->coreReg, argRegs[i]);
81 needFlush = false;
82 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
83 opRegCopy(cUnit, vMap->fpReg, argRegs[i]);
84 needFlush = false;
85 } else {
86 needFlush = true;
87 }
buzbee86a4bce2012-03-06 18:15:00 -080088
Bill Buzbeea114add2012-05-03 15:00:40 -070089 // For wide args, force flush if only half is promoted
90 if (tLoc->wide) {
91 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
92 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
93 (pMap->fpLocation != vMap->fpLocation);
94 }
95 if (needFlush) {
96 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
97 argRegs[i], kWord);
98 }
99 } else {
100 // If arriving in frame & promoted
101 if (vMap->coreLocation == kLocPhysReg) {
102 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
103 vMap->coreReg);
104 }
105 if (vMap->fpLocation == kLocPhysReg) {
106 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
107 vMap->fpReg);
108 }
buzbee31a4a6f2012-02-28 15:36:15 -0800109 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 }
buzbee31a4a6f2012-02-28 15:36:15 -0800111}
112
Ian Rogers3fa13792012-03-18 15:53:45 -0700113void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget, const DexFile* dexFile, uint32_t dexMethodIdx)
114{
Bill Buzbeea114add2012-05-03 15:00:40 -0700115 LIR* curTarget = cUnit->methodLiteralList;
116 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
117 while (curTarget != NULL && nextTarget != NULL) {
118 if (curTarget->operands[0] == (int)dexFile &&
119 nextTarget->operands[0] == (int)dexMethodIdx) {
120 *codeTarget = curTarget;
121 *methodTarget = nextTarget;
122 DCHECK((*codeTarget)->next == *methodTarget);
123 DCHECK_EQ((*codeTarget)->operands[0], (int)dexFile);
124 DCHECK_EQ((*methodTarget)->operands[0], (int)dexMethodIdx);
125 break;
Ian Rogers3fa13792012-03-18 15:53:45 -0700126 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 curTarget = nextTarget->next;
128 nextTarget = curTarget != NULL ? curTarget->next : NULL;
129 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700130}
131
buzbee31a4a6f2012-02-28 15:36:15 -0800132/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700133 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800134 * emit the next instruction in static & direct invoke sequences.
135 */
buzbee15bf9802012-06-12 17:49:27 -0700136int nextSDCallInsn(CompilationUnit* cUnit, InvokeInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700137 int state, uint32_t dexIdx, uint32_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700138 uintptr_t directCode, uintptr_t directMethod,
139 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800140{
Brian Carlstromf5822582012-03-19 22:34:31 -0700141#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 directCode = 0;
143 directMethod = 0;
Brian Carlstromf5822582012-03-19 22:34:31 -0700144#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 if (directCode != 0 && directMethod != 0) {
146 switch (state) {
147 case 0: // Get the current Method* [sets rARG0]
148 if (directCode != (uintptr_t)-1) {
149 loadConstant(cUnit, rINVOKE_TGT, directCode);
150 } else {
151 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
152 if (dataTarget == NULL) {
153 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
154 dataTarget->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700155 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700156#if defined(TARGET_ARM)
157 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
158 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0,
159 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);
171 dataTarget->operands[1] = type;
172 }
173#if defined(TARGET_ARM)
174 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
175 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0,
176 dataTarget);
177 oatAppendLIR(cUnit, loadPcRel);
178#else
179 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
180#endif
181 }
182 break;
183 default:
184 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800185 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 } else {
187 switch (state) {
188 case 0: // Get the current Method* [sets rARG0]
189 // TUNING: we can save a reg copy if Method* has been promoted
190 loadCurrMethodDirect(cUnit, rARG0);
191 break;
192 case 1: // Get method->dex_cache_resolved_methods_
193 loadWordDisp(cUnit, rARG0,
194 Method::DexCacheResolvedMethodsOffset().Int32Value(),
195 rARG0);
196 // Set up direct code if known.
197 if (directCode != 0) {
198 if (directCode != (uintptr_t)-1) {
199 loadConstant(cUnit, rINVOKE_TGT, directCode);
200 } else {
201 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
202 if (dataTarget == NULL) {
203 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
204 dataTarget->operands[1] = type;
205 }
206#if defined(TARGET_ARM)
207 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
208 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0,
209 dataTarget);
210 oatAppendLIR(cUnit, loadPcRel);
211#else
212 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
213#endif
214 }
215 }
216 break;
217 case 2: // Grab target method*
218 loadWordDisp(cUnit, rARG0,
219 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
220 rARG0);
221 break;
222#if !defined(TARGET_X86)
223 case 3: // Grab the code from the method*
224 if (directCode == 0) {
225 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
226 rINVOKE_TGT);
227 }
228 break;
229#endif
230 default:
231 return -1;
232 }
233 }
234 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800235}
236
237/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700238 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800239 * emit the next instruction in a virtual invoke sequence.
240 * We can use rLR as a temp prior to target address loading
241 * Note also that we'll load the first argument ("this") into
242 * rARG1 here rather than the standard loadArgRegs.
243 */
buzbee15bf9802012-06-12 17:49:27 -0700244int nextVCallInsn(CompilationUnit* cUnit, InvokeInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700245 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700246 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800247{
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 RegLocation rlArg;
249 /*
250 * This is the fast path in which the target virtual method is
251 * fully resolved at compile time.
252 */
253 switch (state) {
254 case 0: // Get "this" [set rARG1]
buzbee15bf9802012-06-12 17:49:27 -0700255 rlArg = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 loadValueDirectFixed(cUnit, rlArg, rARG1);
257 break;
258 case 1: // Is "this" null? [use rARG1]
buzbee15bf9802012-06-12 17:49:27 -0700259 genNullCheck(cUnit, info->args[0].sRegLow, rARG1, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 // get this->klass_ [use rARG1, set rINVOKE_TGT]
261 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
262 rINVOKE_TGT);
263 break;
264 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
265 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
266 rINVOKE_TGT);
267 break;
268 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
269 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
270 Array::DataOffset(sizeof(Object*)).Int32Value(), rARG0);
271 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700272#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
274 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
275 rINVOKE_TGT);
276 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700277#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 default:
279 return -1;
280 }
281 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800282}
283
buzbee15bf9802012-06-12 17:49:27 -0700284int nextInvokeInsnSP(CompilationUnit* cUnit, InvokeInfo* info, int trampoline,
buzbee31a4a6f2012-02-28 15:36:15 -0800285 int state, uint32_t dexIdx, uint32_t methodIdx)
286{
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 /*
288 * This handles the case in which the base method is not fully
289 * resolved at compile time, we bail to a runtime helper.
290 */
291 if (state == 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700292#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 // Load trampoline target
294 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
Ian Rogers7caad772012-03-30 01:07:54 -0700295#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 // Load rARG0 with method index
297 loadConstant(cUnit, rARG0, dexIdx);
298 return 1;
299 }
300 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800301}
302
buzbee15bf9802012-06-12 17:49:27 -0700303int nextStaticCallInsnSP(CompilationUnit* cUnit, InvokeInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700304 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700305 uintptr_t unused, uintptr_t unused2,
306 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800307{
Ian Rogers57b86d42012-03-27 16:05:41 -0700308 int trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700309 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800310}
311
buzbee15bf9802012-06-12 17:49:27 -0700312int nextDirectCallInsnSP(CompilationUnit* cUnit, InvokeInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700313 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700314 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800315{
Ian Rogers57b86d42012-03-27 16:05:41 -0700316 int trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700317 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800318}
319
buzbee15bf9802012-06-12 17:49:27 -0700320int nextSuperCallInsnSP(CompilationUnit* cUnit, InvokeInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700321 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700322 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800323{
Ian Rogers57b86d42012-03-27 16:05:41 -0700324 int trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700325 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800326}
327
buzbee15bf9802012-06-12 17:49:27 -0700328int nextVCallInsnSP(CompilationUnit* cUnit, InvokeInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700329 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700330 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800331{
Ian Rogers57b86d42012-03-27 16:05:41 -0700332 int trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700333 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800334}
335
336/*
337 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
338 * which will locate the target and continue on via a tail call.
339 */
buzbee15bf9802012-06-12 17:49:27 -0700340int nextInterfaceCallInsn(CompilationUnit* cUnit, InvokeInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700341 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700342 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800343{
Ian Rogers57b86d42012-03-27 16:05:41 -0700344 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
buzbee15bf9802012-06-12 17:49:27 -0700345 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800346}
347
buzbee15bf9802012-06-12 17:49:27 -0700348int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit,
349 InvokeInfo* info, int state,
350 uint32_t dexIdx, uint32_t unused,
351 uintptr_t unused2, uintptr_t unused3,
352 InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800353{
Ian Rogers57b86d42012-03-27 16:05:41 -0700354 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700355 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800356}
357
buzbee15bf9802012-06-12 17:49:27 -0700358int loadArgRegs(CompilationUnit* cUnit, InvokeInfo* info, int callState,
359 NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700360 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -0700361 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800362{
Bill Buzbeea114add2012-05-03 15:00:40 -0700363 int lastArgReg = rARG3;
364 int nextReg = rARG1;
365 int nextArg = 0;
366 if (skipThis) {
367 nextReg++;
368 nextArg++;
369 }
buzbee15bf9802012-06-12 17:49:27 -0700370 for (; (nextReg <= lastArgReg) && (nextArg < info->numArgWords); nextReg++) {
371 RegLocation rlArg = info->args[nextArg++];
Bill Buzbeea114add2012-05-03 15:00:40 -0700372 rlArg = oatUpdateRawLoc(cUnit, rlArg);
373 if (rlArg.wide && (nextReg <= rARG2)) {
374 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
375 nextReg++;
376 nextArg++;
377 } else {
378 rlArg.wide = false;
379 loadValueDirectFixed(cUnit, rlArg, nextReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800380 }
buzbee15bf9802012-06-12 17:49:27 -0700381 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 directCode, directMethod, type);
383 }
384 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800385}
386
387/*
388 * Load up to 5 arguments, the first three of which will be in
389 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
390 * and as part of the load sequence, it must be replaced with
391 * the target method pointer. Note, this may also be called
392 * for "range" variants if the number of arguments is 5 or fewer.
393 */
buzbee15bf9802012-06-12 17:49:27 -0700394int genDalvikArgsNoRange(CompilationUnit* cUnit, InvokeInfo* info,
395 int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800396 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700397 uint32_t dexIdx, uint32_t methodIdx,
398 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700399 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800400{
Bill Buzbeea114add2012-05-03 15:00:40 -0700401 RegLocation rlArg;
buzbee31a4a6f2012-02-28 15:36:15 -0800402
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 /* If no arguments, just return */
buzbee15bf9802012-06-12 17:49:27 -0700404 if (info->numArgWords == 0)
buzbee31a4a6f2012-02-28 15:36:15 -0800405 return callState;
Bill Buzbeea114add2012-05-03 15:00:40 -0700406
buzbee15bf9802012-06-12 17:49:27 -0700407 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 directCode, directMethod, type);
409
buzbee15bf9802012-06-12 17:49:27 -0700410 DCHECK_LE(info->numArgWords, 5);
411 if (info->numArgWords > 3) {
412 int32_t nextUse = 3;
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 //Detect special case of wide arg spanning arg3/arg4
buzbee15bf9802012-06-12 17:49:27 -0700414 RegLocation rlUse0 = info->args[0];
415 RegLocation rlUse1 = info->args[1];
416 RegLocation rlUse2 = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
418 rlUse2.wide) {
419 int reg = -1;
420 // Wide spans, we need the 2nd half of uses[2].
421 rlArg = oatUpdateLocWide(cUnit, rlUse2);
422 if (rlArg.location == kLocPhysReg) {
423 reg = rlArg.highReg;
424 } else {
425 // rARG2 & rARG3 can safely be used here
426 reg = rARG3;
427 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbee15bf9802012-06-12 17:49:27 -0700428 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 methodIdx, directCode, directMethod, type);
430 }
431 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
432 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
buzbee15bf9802012-06-12 17:49:27 -0700433 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 directCode, directMethod, type);
435 nextUse++;
436 }
437 // Loop through the rest
buzbee15bf9802012-06-12 17:49:27 -0700438 while (nextUse < info->numArgWords) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 int lowReg;
440 int highReg = -1;
buzbee15bf9802012-06-12 17:49:27 -0700441 rlArg = info->args[nextUse];
Bill Buzbeea114add2012-05-03 15:00:40 -0700442 rlArg = oatUpdateRawLoc(cUnit, rlArg);
443 if (rlArg.location == kLocPhysReg) {
444 lowReg = rlArg.lowReg;
445 highReg = rlArg.highReg;
446 } else {
447 lowReg = rARG2;
448 if (rlArg.wide) {
449 highReg = rARG3;
450 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
451 } else {
452 loadValueDirectFixed(cUnit, rlArg, lowReg);
453 }
buzbee15bf9802012-06-12 17:49:27 -0700454 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 methodIdx, directCode, directMethod, type);
456 }
457 int outsOffset = (nextUse + 1) * 4;
458 if (rlArg.wide) {
459 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
460 nextUse += 2;
461 } else {
462 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
463 nextUse++;
464 }
buzbee15bf9802012-06-12 17:49:27 -0700465 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700466 directCode, directMethod, type);
467 }
468 }
469
buzbee15bf9802012-06-12 17:49:27 -0700470 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 dexIdx, methodIdx, directCode, directMethod,
472 type, skipThis);
473
474 if (pcrLabel) {
buzbee15bf9802012-06-12 17:49:27 -0700475 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, rARG1,
476 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700477 }
478 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800479}
480
481/*
482 * May have 0+ arguments (also used for jumbo). Note that
483 * source virtual registers may be in physical registers, so may
484 * need to be flushed to home location before copying. This
485 * applies to arg3 and above (see below).
486 *
487 * Two general strategies:
488 * If < 20 arguments
489 * Pass args 3-18 using vldm/vstm block copy
490 * Pass arg0, arg1 & arg2 in rARG1-rARG3
491 * If 20+ arguments
492 * Pass args arg19+ using memcpy block copy
493 * Pass arg0, arg1 & arg2 in rARG1-rARG3
494 *
495 */
buzbee15bf9802012-06-12 17:49:27 -0700496int genDalvikArgsRange(CompilationUnit* cUnit, InvokeInfo* info, int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800497 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700498 uint32_t dexIdx, uint32_t methodIdx,
499 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700500 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800501{
buzbee31a4a6f2012-02-28 15:36:15 -0800502
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 // If we can treat it as non-range (Jumbo ops will use range form)
buzbee15bf9802012-06-12 17:49:27 -0700504 if (info->numArgWords <= 5)
505 return genDalvikArgsNoRange(cUnit, info, callState, pcrLabel,
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 nextCallInsn, dexIdx, methodIdx,
507 directCode, directMethod, type, skipThis);
508 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700509 * First load the non-register arguments. Both forms expect all
510 * of the source arguments to be in their home frame location, so
511 * scan the sReg names and flush any that have been promoted to
512 * frame backing storage.
513 */
514 // Scan the rest of the args - if in physReg flush to memory
buzbee15bf9802012-06-12 17:49:27 -0700515 for (int nextArg = 0; nextArg < info->numArgWords;) {
516 RegLocation loc = info->args[nextArg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 if (loc.wide) {
518 loc = oatUpdateLocWide(cUnit, loc);
519 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
520 storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
521 loc.lowReg, loc.highReg);
522 }
523 nextArg += 2;
524 } else {
525 loc = oatUpdateLoc(cUnit, loc);
526 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
527 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
528 loc.lowReg, kWord);
529 }
530 nextArg++;
buzbee31a4a6f2012-02-28 15:36:15 -0800531 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700532 }
buzbee31a4a6f2012-02-28 15:36:15 -0800533
buzbee15bf9802012-06-12 17:49:27 -0700534 int startOffset = oatSRegOffset(cUnit, info->args[3].sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700535 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700536#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 // Generate memcpy
538 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
539 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
540 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
buzbee15bf9802012-06-12 17:49:27 -0700541 rARG0, rARG1, (info->numArgWords - 3) * 4);
Bill Buzbeea114add2012-05-03 15:00:40 -0700542#else
buzbee15bf9802012-06-12 17:49:27 -0700543 if (info->numArgWords >= 20) {
buzbee31a4a6f2012-02-28 15:36:15 -0800544 // Generate memcpy
545 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
546 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogers57b86d42012-03-27 16:05:41 -0700547 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
buzbee15bf9802012-06-12 17:49:27 -0700548 rARG0, rARG1, (info->numArgWords - 3) * 4);
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 } else {
550 // Use vldm/vstm pair using rARG3 as a temp
buzbee15bf9802012-06-12 17:49:27 -0700551 int regsLeft = std::min(info->numArgWords - 3, 16);
552 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700553 directCode, directMethod, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700554 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
555 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
556 //TUNING: loosen barrier
557 ld->defMask = ENCODE_ALL;
558 setMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbee15bf9802012-06-12 17:49:27 -0700559 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 directCode, directMethod, type);
561 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
buzbee15bf9802012-06-12 17:49:27 -0700562 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700563 directCode, directMethod, type);
564 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
565 setMemRefType(st, false /* isLoad */, kDalvikReg);
566 st->defMask = ENCODE_ALL;
buzbee15bf9802012-06-12 17:49:27 -0700567 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700568 directCode, directMethod, type);
569
570 }
571#endif
572
buzbee15bf9802012-06-12 17:49:27 -0700573 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 dexIdx, methodIdx, directCode, directMethod,
575 type, skipThis);
576
buzbee15bf9802012-06-12 17:49:27 -0700577 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700578 directCode, directMethod, type);
579 if (pcrLabel) {
buzbee15bf9802012-06-12 17:49:27 -0700580 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, rARG1,
581 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700582 }
583 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800584}
585
buzbee15bf9802012-06-12 17:49:27 -0700586RegLocation inlineTarget(CompilationUnit* cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700587{
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700589 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700590 res = oatGetReturn(cUnit, false);
591 } else {
buzbee15bf9802012-06-12 17:49:27 -0700592 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700593 }
594 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700595}
596
buzbee15bf9802012-06-12 17:49:27 -0700597RegLocation inlineTargetWide(CompilationUnit* cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700598{
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700600 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 res = oatGetReturnWide(cUnit, false);
602 } else {
buzbee15bf9802012-06-12 17:49:27 -0700603 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700604 }
605 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700606}
607
buzbee15bf9802012-06-12 17:49:27 -0700608bool genInlinedCharAt(CompilationUnit* cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700609{
610#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700611 // Location of reference to data array
612 int valueOffset = String::ValueOffset().Int32Value();
613 // Location of count
614 int countOffset = String::CountOffset().Int32Value();
615 // Starting offset within data array
616 int offsetOffset = String::OffsetOffset().Int32Value();
617 // Start of char data with array_
618 int dataOffset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
buzbeefc9e6fa2012-03-23 15:14:29 -0700619
buzbee15bf9802012-06-12 17:49:27 -0700620 RegLocation rlObj = info->args[0];
621 RegLocation rlIdx = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 rlObj = loadValue(cUnit, rlObj, kCoreReg);
623 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
624 int regMax;
625 int regOff = oatAllocTemp(cUnit);
626 int regPtr = oatAllocTemp(cUnit);
buzbee15bf9802012-06-12 17:49:27 -0700627 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
628 bool rangeCheck = (!(info->optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 if (rangeCheck) {
630 regMax = oatAllocTemp(cUnit);
631 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
632 }
633 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
634 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
635 LIR* launchPad = NULL;
636 if (rangeCheck) {
637 // Set up a launch pad to allow retry in case of bounds violation */
buzbee15bf9802012-06-12 17:49:27 -0700638 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
640 (intptr_t)launchPad);
641 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
642 oatFreeTemp(cUnit, regMax);
643 opCondBranch(cUnit, kCondCs, launchPad);
644 }
645 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
646 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
buzbee15bf9802012-06-12 17:49:27 -0700647 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
649 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
650 oatFreeTemp(cUnit, regOff);
651 oatFreeTemp(cUnit, regPtr);
652 storeValue(cUnit, rlDest, rlResult);
653 if (rangeCheck) {
Elliott Hughes60234562012-06-01 12:25:59 -0700654 launchPad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 }
656 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700657 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700659#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700661#endif
662}
663
buzbee15bf9802012-06-12 17:49:27 -0700664bool genInlinedMinMaxInt(CompilationUnit *cUnit, InvokeInfo* info, bool isMin)
buzbeefc9e6fa2012-03-23 15:14:29 -0700665{
666#if defined(TARGET_ARM)
buzbee15bf9802012-06-12 17:49:27 -0700667 RegLocation rlSrc1 = info->args[0];
668 RegLocation rlSrc2 = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700669 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
670 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700671 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
673 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
674 opIT(cUnit, (isMin) ? kArmCondGt : kArmCondLt, "E");
675 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc2.lowReg);
676 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc1.lowReg);
677 genBarrier(cUnit);
678 storeValue(cUnit, rlDest, rlResult);
679 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700680#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700682#endif
683}
684
685// Generates an inlined String.isEmpty or String.length.
buzbee15bf9802012-06-12 17:49:27 -0700686bool genInlinedStringIsEmptyOrLength(CompilationUnit* cUnit, InvokeInfo* info,
687 bool isEmpty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700688{
689#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700690 // dst = src.length();
buzbee15bf9802012-06-12 17:49:27 -0700691 RegLocation rlObj = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700692 rlObj = loadValue(cUnit, rlObj, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700693 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700694 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee15bf9802012-06-12 17:49:27 -0700695 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700696 loadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
697 rlResult.lowReg);
698 if (isEmpty) {
699 // dst = (dst == 0);
700 int tReg = oatAllocTemp(cUnit);
701 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
702 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
703 }
704 storeValue(cUnit, rlDest, rlResult);
705 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700706#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700707 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700708#endif
709}
710
buzbee15bf9802012-06-12 17:49:27 -0700711bool genInlinedAbsInt(CompilationUnit *cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700712{
713#if defined(TARGET_ARM)
buzbee15bf9802012-06-12 17:49:27 -0700714 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700715 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700716 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700717 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
718 int signReg = oatAllocTemp(cUnit);
719 // abs(x) = y<=x>>31, (x+y)^y.
720 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
721 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
722 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
723 storeValue(cUnit, rlDest, rlResult);
724 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700725#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700726 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700727#endif
728}
729
buzbee15bf9802012-06-12 17:49:27 -0700730bool genInlinedAbsLong(CompilationUnit *cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700731{
732#if defined(TARGET_ARM)
buzbee15bf9802012-06-12 17:49:27 -0700733 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700734 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700735 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700736 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
737 int signReg = oatAllocTemp(cUnit);
738 // abs(x) = y<=x>>31, (x+y)^y.
739 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
740 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
741 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
742 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
743 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
744 storeValueWide(cUnit, rlDest, rlResult);
745 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700746#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700747 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700748#endif
749}
750
buzbee15bf9802012-06-12 17:49:27 -0700751bool genInlinedFloatCvt(CompilationUnit *cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700752{
753#if defined(TARGET_ARM)
buzbee15bf9802012-06-12 17:49:27 -0700754 RegLocation rlSrc = info->args[0];
755 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700756 storeValue(cUnit, rlDest, rlSrc);
757 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700758#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700759 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700760#endif
761}
762
buzbee15bf9802012-06-12 17:49:27 -0700763bool genInlinedDoubleCvt(CompilationUnit *cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700764{
765#if defined(TARGET_ARM)
buzbee15bf9802012-06-12 17:49:27 -0700766 RegLocation rlSrc = info->args[0];
767 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 storeValueWide(cUnit, rlDest, rlSrc);
769 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700770#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700771 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700772#endif
773}
774
775/*
776 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
777 * otherwise bails to standard library code.
778 */
buzbee15bf9802012-06-12 17:49:27 -0700779bool genInlinedIndexOf(CompilationUnit* cUnit, InvokeInfo* info,
780 bool zeroBased)
buzbeefc9e6fa2012-03-23 15:14:29 -0700781{
782#if defined(TARGET_ARM)
783
Bill Buzbeea114add2012-05-03 15:00:40 -0700784 oatClobberCalleeSave(cUnit);
785 oatLockCallTemps(cUnit); // Using fixed registers
786 int regPtr = rARG0;
787 int regChar = rARG1;
788 int regStart = rARG2;
buzbeefc9e6fa2012-03-23 15:14:29 -0700789
buzbee15bf9802012-06-12 17:49:27 -0700790 RegLocation rlObj = info->args[0];
791 RegLocation rlChar = info->args[1];
792 RegLocation rlStart = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700793 loadValueDirectFixed(cUnit, rlObj, regPtr);
794 loadValueDirectFixed(cUnit, rlChar, regChar);
795 if (zeroBased) {
796 loadConstant(cUnit, regStart, 0);
797 } else {
798 loadValueDirectFixed(cUnit, rlStart, regStart);
799 }
800 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf));
buzbee15bf9802012-06-12 17:49:27 -0700801 genNullCheck(cUnit, rlObj.sRegLow, regPtr, info->optFlags);
802 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700803 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
804 (intptr_t)launchPad);
805 opCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
806 opReg(cUnit, kOpBlx, rTgt);
807 LIR* resumeTgt = newLIR0(cUnit, kPseudoTargetLabel);
808 launchPad->operands[2] = (uintptr_t)resumeTgt;
Bill Buzbeea114add2012-05-03 15:00:40 -0700809 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700810 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
811 RegLocation rlReturn = oatGetReturn(cUnit, false);
812 RegLocation rlDest = inlineTarget(cUnit, info);
813 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700814 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700815#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700816 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700817#endif
818}
819
820/* Fast string.compareTo(Ljava/lang/string;)I. */
buzbee15bf9802012-06-12 17:49:27 -0700821bool genInlinedStringCompareTo(CompilationUnit* cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700822{
823#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700824 oatClobberCalleeSave(cUnit);
825 oatLockCallTemps(cUnit); // Using fixed registers
826 int regThis = rARG0;
827 int regCmp = rARG1;
buzbeefc9e6fa2012-03-23 15:14:29 -0700828
buzbee15bf9802012-06-12 17:49:27 -0700829 RegLocation rlThis = info->args[0];
830 RegLocation rlCmp = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700831 loadValueDirectFixed(cUnit, rlThis, regThis);
832 loadValueDirectFixed(cUnit, rlCmp, regCmp);
833 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo));
buzbee15bf9802012-06-12 17:49:27 -0700834 genNullCheck(cUnit, rlThis.sRegLow, regThis, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700835 //TUNING: check if rlCmp.sRegLow is already null checked
buzbee15bf9802012-06-12 17:49:27 -0700836 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
Elliott Hughes60234562012-06-01 12:25:59 -0700838 (intptr_t)launchPad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 opCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
840 opReg(cUnit, kOpBlx, rTgt);
Elliott Hughes60234562012-06-01 12:25:59 -0700841 launchPad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -0700842 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700843 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
844 RegLocation rlReturn = oatGetReturn(cUnit, false);
845 RegLocation rlDest = inlineTarget(cUnit, info);
846 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700848#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700849 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700850#endif
851}
852
buzbee15bf9802012-06-12 17:49:27 -0700853bool genIntrinsic(CompilationUnit* cUnit, InvokeInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700854{
buzbee15bf9802012-06-12 17:49:27 -0700855 if ((info->optFlags & MIR_INLINED) || info->isRange ||
856 (info->result.location == kLocInvalid)) {
buzbeefc9e6fa2012-03-23 15:14:29 -0700857 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700858 }
859 /*
860 * TODO: move these to a target-specific structured constant array
861 * and use a generic match function. The list of intrinsics may be
862 * slightly different depending on target.
863 * TODO: Fold this into a matching function that runs during
864 * basic block building. This should be part of the action for
865 * small method inlining and recognition of the special object init
866 * method. By doing this during basic block construction, we can also
867 * take advantage of/generate new useful dataflow info.
868 */
buzbee15bf9802012-06-12 17:49:27 -0700869 std::string tgtMethod(PrettyMethod(info->methodIdx, *cUnit->dex_file));
Bill Buzbeea114add2012-05-03 15:00:40 -0700870 if (tgtMethod.compare("char java.lang.String.charAt(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700871 return genInlinedCharAt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700872 }
873 if (tgtMethod.compare("int java.lang.Math.min(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700874 return genInlinedMinMaxInt(cUnit, info, true /* isMin */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700875 }
876 if (tgtMethod.compare("int java.lang.Math.max(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700877 return genInlinedMinMaxInt(cUnit, info, false /* isMin */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700878 }
879 if (tgtMethod.compare("int java.lang.String.length()") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700880 return genInlinedStringIsEmptyOrLength(cUnit, info, false /* isEmpty */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700881 }
882 if (tgtMethod.compare("boolean java.lang.String.isEmpty()") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700883 return genInlinedStringIsEmptyOrLength(cUnit, info, true /* isEmpty */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700884 }
885 if (tgtMethod.compare("int java.lang.Math.abs(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700886 return genInlinedAbsInt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700887 }
888 if (tgtMethod.compare("long java.lang.Math.abs(long)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700889 return genInlinedAbsLong(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700890 }
891 if (tgtMethod.compare("int java.lang.Float.floatToRawIntBits(float)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700892 return genInlinedFloatCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700893 }
894 if (tgtMethod.compare("float java.lang.Float.intBitsToFloat(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700895 return genInlinedFloatCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 }
897 if (tgtMethod.compare("long java.lang.Double.doubleToRawLongBits(double)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700898 return genInlinedDoubleCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700899 }
900 if (tgtMethod.compare("double java.lang.Double.longBitsToDouble(long)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700901 return genInlinedDoubleCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 }
903 if (tgtMethod.compare("int java.lang.String.indexOf(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700904 return genInlinedIndexOf(cUnit, info, false /* base 0 */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700905 }
906 if (tgtMethod.compare("int java.lang.String.indexOf(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700907 return genInlinedIndexOf(cUnit, info, true /* base 0 */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 }
909 if (tgtMethod.compare("int java.lang.String.compareTo(java.lang.String)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700910 return genInlinedStringCompareTo(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 }
912 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700913}
914
915
buzbee31a4a6f2012-02-28 15:36:15 -0800916} // namespace art