blob: d75234e38e323e47980f4a18fdb1b1cd1f099413 [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
buzbee3b3dbdd2012-06-13 13:39:34 -070027typedef int (*NextCallInsn)(CompilationUnit*, CallInfo*, 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.
buzbeead8f15e2012-06-18 14:49:45 -070036 *
37 * argLocs is an array of location records describing the incoming arguments
38 * with one location record per word of argument.
buzbee31a4a6f2012-02-28 15:36:15 -080039 */
buzbeead8f15e2012-06-18 14:49:45 -070040void flushIns(CompilationUnit* cUnit, RegLocation* argLocs, RegLocation rlMethod)
buzbee31a4a6f2012-02-28 15:36:15 -080041{
Bill Buzbeea114add2012-05-03 15:00:40 -070042 /*
43 * Dummy up a RegLocation for the incoming Method*
44 * It will attempt to keep rARG0 live (or copy it to home location
45 * if promoted).
46 */
buzbeead8f15e2012-06-18 14:49:45 -070047 RegLocation rlSrc = rlMethod;
Bill Buzbeea114add2012-05-03 15:00:40 -070048 rlSrc.location = kLocPhysReg;
49 rlSrc.lowReg = rARG0;
50 rlSrc.home = false;
51 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
52 storeValue(cUnit, rlMethod, rlSrc);
53 // If Method* has been promoted, explicitly flush
54 if (rlMethod.location == kLocPhysReg) {
55 storeWordDisp(cUnit, rSP, 0, rARG0);
56 }
buzbee9c044ce2012-03-18 13:24:07 -070057
Bill Buzbeea114add2012-05-03 15:00:40 -070058 if (cUnit->numIns == 0)
59 return;
60 const int numArgRegs = 3;
61 static int argRegs[] = {rARG1, rARG2, rARG3};
62 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
63 /*
64 * Copy incoming arguments to their proper home locations.
65 * NOTE: an older version of dx had an issue in which
66 * it would reuse static method argument registers.
67 * This could result in the same Dalvik virtual register
68 * being promoted to both core and fp regs. To account for this,
69 * we only copy to the corresponding promoted physical register
70 * if it matches the type of the SSA name for the incoming
71 * argument. It is also possible that long and double arguments
72 * end up half-promoted. In those cases, we must flush the promoted
73 * half to memory as well.
74 */
75 for (int i = 0; i < cUnit->numIns; i++) {
76 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
77 if (i < numArgRegs) {
78 // If arriving in register
79 bool needFlush = true;
buzbeead8f15e2012-06-18 14:49:45 -070080 RegLocation* tLoc = &argLocs[i];
Bill Buzbeea114add2012-05-03 15:00:40 -070081 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
82 opRegCopy(cUnit, vMap->coreReg, argRegs[i]);
83 needFlush = false;
84 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
85 opRegCopy(cUnit, vMap->fpReg, argRegs[i]);
86 needFlush = false;
87 } else {
88 needFlush = true;
89 }
buzbee86a4bce2012-03-06 18:15:00 -080090
Bill Buzbeea114add2012-05-03 15:00:40 -070091 // For wide args, force flush if only half is promoted
92 if (tLoc->wide) {
93 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
94 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
95 (pMap->fpLocation != vMap->fpLocation);
96 }
97 if (needFlush) {
98 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
99 argRegs[i], kWord);
100 }
101 } else {
102 // If arriving in frame & promoted
103 if (vMap->coreLocation == kLocPhysReg) {
104 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
105 vMap->coreReg);
106 }
107 if (vMap->fpLocation == kLocPhysReg) {
108 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
109 vMap->fpReg);
110 }
buzbee31a4a6f2012-02-28 15:36:15 -0800111 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 }
buzbee31a4a6f2012-02-28 15:36:15 -0800113}
114
Ian Rogers3fa13792012-03-18 15:53:45 -0700115void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget, const DexFile* dexFile, uint32_t dexMethodIdx)
116{
Bill Buzbeea114add2012-05-03 15:00:40 -0700117 LIR* curTarget = cUnit->methodLiteralList;
118 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
119 while (curTarget != NULL && nextTarget != NULL) {
120 if (curTarget->operands[0] == (int)dexFile &&
121 nextTarget->operands[0] == (int)dexMethodIdx) {
122 *codeTarget = curTarget;
123 *methodTarget = nextTarget;
124 DCHECK((*codeTarget)->next == *methodTarget);
125 DCHECK_EQ((*codeTarget)->operands[0], (int)dexFile);
126 DCHECK_EQ((*methodTarget)->operands[0], (int)dexMethodIdx);
127 break;
Ian Rogers3fa13792012-03-18 15:53:45 -0700128 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 curTarget = nextTarget->next;
130 nextTarget = curTarget != NULL ? curTarget->next : NULL;
131 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700132}
133
buzbee31a4a6f2012-02-28 15:36:15 -0800134/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700135 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800136 * emit the next instruction in static & direct invoke sequences.
137 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700138int nextSDCallInsn(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700139 int state, uint32_t dexIdx, uint32_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700140 uintptr_t directCode, uintptr_t directMethod,
141 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800142{
Brian Carlstromf5822582012-03-19 22:34:31 -0700143#if !defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 directCode = 0;
145 directMethod = 0;
Brian Carlstromf5822582012-03-19 22:34:31 -0700146#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 if (directCode != 0 && directMethod != 0) {
148 switch (state) {
149 case 0: // Get the current Method* [sets rARG0]
150 if (directCode != (uintptr_t)-1) {
151 loadConstant(cUnit, rINVOKE_TGT, directCode);
152 } else {
153 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
154 if (dataTarget == NULL) {
155 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
156 dataTarget->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700157 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700158#if defined(TARGET_ARM)
159 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
160 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0,
161 dataTarget);
162 oatAppendLIR(cUnit, loadPcRel);
163#else
164 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
165#endif
166 }
167 if (directMethod != (uintptr_t)-1) {
168 loadConstant(cUnit, rARG0, directMethod);
169 } else {
170 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
171 if (dataTarget == NULL) {
172 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
173 dataTarget->operands[1] = type;
174 }
175#if defined(TARGET_ARM)
176 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
177 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0,
178 dataTarget);
179 oatAppendLIR(cUnit, loadPcRel);
180#else
181 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
182#endif
183 }
184 break;
185 default:
186 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800187 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 } else {
189 switch (state) {
190 case 0: // Get the current Method* [sets rARG0]
191 // TUNING: we can save a reg copy if Method* has been promoted
192 loadCurrMethodDirect(cUnit, rARG0);
193 break;
194 case 1: // Get method->dex_cache_resolved_methods_
195 loadWordDisp(cUnit, rARG0,
196 Method::DexCacheResolvedMethodsOffset().Int32Value(),
197 rARG0);
198 // Set up direct code if known.
199 if (directCode != 0) {
200 if (directCode != (uintptr_t)-1) {
201 loadConstant(cUnit, rINVOKE_TGT, directCode);
202 } else {
203 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
204 if (dataTarget == NULL) {
205 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
206 dataTarget->operands[1] = type;
207 }
208#if defined(TARGET_ARM)
209 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
210 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0,
211 dataTarget);
212 oatAppendLIR(cUnit, loadPcRel);
213#else
214 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
215#endif
216 }
217 }
218 break;
219 case 2: // Grab target method*
220 loadWordDisp(cUnit, rARG0,
221 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
222 rARG0);
223 break;
224#if !defined(TARGET_X86)
225 case 3: // Grab the code from the method*
226 if (directCode == 0) {
227 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
228 rINVOKE_TGT);
229 }
230 break;
231#endif
232 default:
233 return -1;
234 }
235 }
236 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800237}
238
239/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700240 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800241 * emit the next instruction in a virtual invoke sequence.
242 * We can use rLR as a temp prior to target address loading
243 * Note also that we'll load the first argument ("this") into
244 * rARG1 here rather than the standard loadArgRegs.
245 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700246int nextVCallInsn(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700247 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700248 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800249{
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 RegLocation rlArg;
251 /*
252 * This is the fast path in which the target virtual method is
253 * fully resolved at compile time.
254 */
255 switch (state) {
256 case 0: // Get "this" [set rARG1]
buzbee15bf9802012-06-12 17:49:27 -0700257 rlArg = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 loadValueDirectFixed(cUnit, rlArg, rARG1);
259 break;
260 case 1: // Is "this" null? [use rARG1]
buzbee15bf9802012-06-12 17:49:27 -0700261 genNullCheck(cUnit, info->args[0].sRegLow, rARG1, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 // get this->klass_ [use rARG1, set rINVOKE_TGT]
263 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
264 rINVOKE_TGT);
265 break;
266 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
267 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
268 rINVOKE_TGT);
269 break;
270 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
271 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
272 Array::DataOffset(sizeof(Object*)).Int32Value(), rARG0);
273 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700274#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
276 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
277 rINVOKE_TGT);
278 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700279#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 default:
281 return -1;
282 }
283 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800284}
285
buzbee3b3dbdd2012-06-13 13:39:34 -0700286int nextInvokeInsnSP(CompilationUnit* cUnit, CallInfo* info, int trampoline,
buzbee31a4a6f2012-02-28 15:36:15 -0800287 int state, uint32_t dexIdx, uint32_t methodIdx)
288{
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 /*
290 * This handles the case in which the base method is not fully
291 * resolved at compile time, we bail to a runtime helper.
292 */
293 if (state == 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700294#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700295 // Load trampoline target
296 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
Ian Rogers7caad772012-03-30 01:07:54 -0700297#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 // Load rARG0 with method index
299 loadConstant(cUnit, rARG0, dexIdx);
300 return 1;
301 }
302 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800303}
304
buzbee3b3dbdd2012-06-13 13:39:34 -0700305int nextStaticCallInsnSP(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700306 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700307 uintptr_t unused, uintptr_t unused2,
308 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800309{
Ian Rogers57b86d42012-03-27 16:05:41 -0700310 int trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700311 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800312}
313
buzbee3b3dbdd2012-06-13 13:39:34 -0700314int nextDirectCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700315 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700316 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800317{
Ian Rogers57b86d42012-03-27 16:05:41 -0700318 int trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700319 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800320}
321
buzbee3b3dbdd2012-06-13 13:39:34 -0700322int nextSuperCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700323 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700324 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800325{
Ian Rogers57b86d42012-03-27 16:05:41 -0700326 int trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700327 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800328}
329
buzbee3b3dbdd2012-06-13 13:39:34 -0700330int nextVCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700331 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700332 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800333{
Ian Rogers57b86d42012-03-27 16:05:41 -0700334 int trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700335 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800336}
337
338/*
339 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
340 * which will locate the target and continue on via a tail call.
341 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700342int nextInterfaceCallInsn(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700343 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700344 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800345{
Ian Rogers57b86d42012-03-27 16:05:41 -0700346 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
buzbee15bf9802012-06-12 17:49:27 -0700347 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800348}
349
buzbee15bf9802012-06-12 17:49:27 -0700350int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit,
buzbee3b3dbdd2012-06-13 13:39:34 -0700351 CallInfo* info, int state,
buzbee15bf9802012-06-12 17:49:27 -0700352 uint32_t dexIdx, uint32_t unused,
353 uintptr_t unused2, uintptr_t unused3,
354 InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800355{
Ian Rogers57b86d42012-03-27 16:05:41 -0700356 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700357 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800358}
359
buzbee3b3dbdd2012-06-13 13:39:34 -0700360int loadArgRegs(CompilationUnit* cUnit, CallInfo* info, int callState,
buzbee15bf9802012-06-12 17:49:27 -0700361 NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700362 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -0700363 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800364{
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 int lastArgReg = rARG3;
366 int nextReg = rARG1;
367 int nextArg = 0;
368 if (skipThis) {
369 nextReg++;
370 nextArg++;
371 }
buzbee15bf9802012-06-12 17:49:27 -0700372 for (; (nextReg <= lastArgReg) && (nextArg < info->numArgWords); nextReg++) {
373 RegLocation rlArg = info->args[nextArg++];
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 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);
buzbee31a4a6f2012-02-28 15:36:15 -0800382 }
buzbee15bf9802012-06-12 17:49:27 -0700383 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 directCode, directMethod, type);
385 }
386 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800387}
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 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700396int genDalvikArgsNoRange(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700397 int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800398 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{
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 RegLocation rlArg;
buzbee31a4a6f2012-02-28 15:36:15 -0800404
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 /* If no arguments, just return */
buzbee15bf9802012-06-12 17:49:27 -0700406 if (info->numArgWords == 0)
buzbee31a4a6f2012-02-28 15:36:15 -0800407 return callState;
Bill Buzbeea114add2012-05-03 15:00:40 -0700408
buzbee15bf9802012-06-12 17:49:27 -0700409 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 directCode, directMethod, type);
411
buzbee15bf9802012-06-12 17:49:27 -0700412 DCHECK_LE(info->numArgWords, 5);
413 if (info->numArgWords > 3) {
414 int32_t nextUse = 3;
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 //Detect special case of wide arg spanning arg3/arg4
buzbee15bf9802012-06-12 17:49:27 -0700416 RegLocation rlUse0 = info->args[0];
417 RegLocation rlUse1 = info->args[1];
418 RegLocation rlUse2 = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
420 rlUse2.wide) {
421 int reg = -1;
422 // 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
428 reg = rARG3;
429 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbee15bf9802012-06-12 17:49:27 -0700430 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 methodIdx, directCode, directMethod, type);
432 }
433 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
434 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
buzbee15bf9802012-06-12 17:49:27 -0700435 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 directCode, directMethod, type);
437 nextUse++;
438 }
439 // Loop through the rest
buzbee15bf9802012-06-12 17:49:27 -0700440 while (nextUse < info->numArgWords) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 int lowReg;
442 int highReg = -1;
buzbee15bf9802012-06-12 17:49:27 -0700443 rlArg = info->args[nextUse];
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 rlArg = oatUpdateRawLoc(cUnit, rlArg);
445 if (rlArg.location == kLocPhysReg) {
446 lowReg = rlArg.lowReg;
447 highReg = rlArg.highReg;
448 } else {
449 lowReg = rARG2;
450 if (rlArg.wide) {
451 highReg = rARG3;
452 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
453 } else {
454 loadValueDirectFixed(cUnit, rlArg, lowReg);
455 }
buzbee15bf9802012-06-12 17:49:27 -0700456 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 methodIdx, directCode, directMethod, type);
458 }
459 int outsOffset = (nextUse + 1) * 4;
460 if (rlArg.wide) {
461 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
462 nextUse += 2;
463 } else {
464 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
465 nextUse++;
466 }
buzbee15bf9802012-06-12 17:49:27 -0700467 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700468 directCode, directMethod, type);
469 }
470 }
471
buzbee15bf9802012-06-12 17:49:27 -0700472 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700473 dexIdx, methodIdx, directCode, directMethod,
474 type, skipThis);
475
476 if (pcrLabel) {
buzbee15bf9802012-06-12 17:49:27 -0700477 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, rARG1,
478 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 }
480 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800481}
482
483/*
484 * May have 0+ arguments (also used for jumbo). Note that
485 * source virtual registers may be in physical registers, so may
486 * need to be flushed to home location before copying. This
487 * applies to arg3 and above (see below).
488 *
489 * Two general strategies:
490 * If < 20 arguments
491 * Pass args 3-18 using vldm/vstm block copy
492 * Pass arg0, arg1 & arg2 in rARG1-rARG3
493 * If 20+ arguments
494 * Pass args arg19+ using memcpy block copy
495 * Pass arg0, arg1 & arg2 in rARG1-rARG3
496 *
497 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700498int genDalvikArgsRange(CompilationUnit* cUnit, CallInfo* info, int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800499 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700500 uint32_t dexIdx, uint32_t methodIdx,
501 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700502 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800503{
buzbee31a4a6f2012-02-28 15:36:15 -0800504
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 // If we can treat it as non-range (Jumbo ops will use range form)
buzbee15bf9802012-06-12 17:49:27 -0700506 if (info->numArgWords <= 5)
507 return genDalvikArgsNoRange(cUnit, info, callState, pcrLabel,
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 nextCallInsn, dexIdx, methodIdx,
509 directCode, directMethod, type, skipThis);
510 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700511 * First load the non-register arguments. Both forms expect all
512 * of the source arguments to be in their home frame location, so
513 * scan the sReg names and flush any that have been promoted to
514 * frame backing storage.
515 */
516 // Scan the rest of the args - if in physReg flush to memory
buzbee15bf9802012-06-12 17:49:27 -0700517 for (int nextArg = 0; nextArg < info->numArgWords;) {
518 RegLocation loc = info->args[nextArg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700519 if (loc.wide) {
520 loc = oatUpdateLocWide(cUnit, loc);
521 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
522 storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
523 loc.lowReg, loc.highReg);
524 }
525 nextArg += 2;
526 } else {
527 loc = oatUpdateLoc(cUnit, loc);
528 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
529 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
530 loc.lowReg, kWord);
531 }
532 nextArg++;
buzbee31a4a6f2012-02-28 15:36:15 -0800533 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700534 }
buzbee31a4a6f2012-02-28 15:36:15 -0800535
buzbee15bf9802012-06-12 17:49:27 -0700536 int startOffset = oatSRegOffset(cUnit, info->args[3].sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700538#if defined(TARGET_MIPS) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700539 // Generate memcpy
540 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
541 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
542 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
buzbee15bf9802012-06-12 17:49:27 -0700543 rARG0, rARG1, (info->numArgWords - 3) * 4);
Bill Buzbeea114add2012-05-03 15:00:40 -0700544#else
buzbee15bf9802012-06-12 17:49:27 -0700545 if (info->numArgWords >= 20) {
buzbee31a4a6f2012-02-28 15:36:15 -0800546 // Generate memcpy
547 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
548 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogers57b86d42012-03-27 16:05:41 -0700549 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
buzbee15bf9802012-06-12 17:49:27 -0700550 rARG0, rARG1, (info->numArgWords - 3) * 4);
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 } else {
552 // Use vldm/vstm pair using rARG3 as a temp
buzbee15bf9802012-06-12 17:49:27 -0700553 int regsLeft = std::min(info->numArgWords - 3, 16);
554 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700555 directCode, directMethod, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 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);
buzbee15bf9802012-06-12 17:49:27 -0700561 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 directCode, directMethod, type);
563 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
buzbee15bf9802012-06-12 17:49:27 -0700564 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 directCode, directMethod, type);
566 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
567 setMemRefType(st, false /* isLoad */, kDalvikReg);
568 st->defMask = ENCODE_ALL;
buzbee15bf9802012-06-12 17:49:27 -0700569 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 directCode, directMethod, type);
571
572 }
573#endif
574
buzbee15bf9802012-06-12 17:49:27 -0700575 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 dexIdx, methodIdx, directCode, directMethod,
577 type, skipThis);
578
buzbee15bf9802012-06-12 17:49:27 -0700579 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 directCode, directMethod, type);
581 if (pcrLabel) {
buzbee15bf9802012-06-12 17:49:27 -0700582 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, rARG1,
583 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 }
585 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800586}
587
buzbee3b3dbdd2012-06-13 13:39:34 -0700588RegLocation inlineTarget(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700589{
Bill Buzbeea114add2012-05-03 15:00:40 -0700590 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700591 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 res = oatGetReturn(cUnit, false);
593 } else {
buzbee15bf9802012-06-12 17:49:27 -0700594 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 }
596 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700597}
598
buzbee3b3dbdd2012-06-13 13:39:34 -0700599RegLocation inlineTargetWide(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700600{
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700602 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700603 res = oatGetReturnWide(cUnit, false);
604 } else {
buzbee15bf9802012-06-12 17:49:27 -0700605 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 }
607 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700608}
609
buzbee3b3dbdd2012-06-13 13:39:34 -0700610bool genInlinedCharAt(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700611{
612#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700613 // Location of reference to data array
614 int valueOffset = String::ValueOffset().Int32Value();
615 // Location of count
616 int countOffset = String::CountOffset().Int32Value();
617 // Starting offset within data array
618 int offsetOffset = String::OffsetOffset().Int32Value();
619 // Start of char data with array_
620 int dataOffset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
buzbeefc9e6fa2012-03-23 15:14:29 -0700621
buzbee15bf9802012-06-12 17:49:27 -0700622 RegLocation rlObj = info->args[0];
623 RegLocation rlIdx = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700624 rlObj = loadValue(cUnit, rlObj, kCoreReg);
625 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
626 int regMax;
627 int regOff = oatAllocTemp(cUnit);
628 int regPtr = oatAllocTemp(cUnit);
buzbee15bf9802012-06-12 17:49:27 -0700629 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
630 bool rangeCheck = (!(info->optFlags & MIR_IGNORE_RANGE_CHECK));
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 if (rangeCheck) {
632 regMax = oatAllocTemp(cUnit);
633 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
634 }
635 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
636 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
637 LIR* launchPad = NULL;
638 if (rangeCheck) {
639 // Set up a launch pad to allow retry in case of bounds violation */
buzbee15bf9802012-06-12 17:49:27 -0700640 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
642 (intptr_t)launchPad);
643 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
644 oatFreeTemp(cUnit, regMax);
645 opCondBranch(cUnit, kCondCs, launchPad);
646 }
647 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
648 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
buzbee15bf9802012-06-12 17:49:27 -0700649 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700650 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
651 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
652 oatFreeTemp(cUnit, regOff);
653 oatFreeTemp(cUnit, regPtr);
654 storeValue(cUnit, rlDest, rlResult);
655 if (rangeCheck) {
Elliott Hughes60234562012-06-01 12:25:59 -0700656 launchPad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700657 }
658 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700659 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700661#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700662 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700663#endif
664}
665
buzbee3b3dbdd2012-06-13 13:39:34 -0700666bool genInlinedMinMaxInt(CompilationUnit *cUnit, CallInfo* info, bool isMin)
buzbeefc9e6fa2012-03-23 15:14:29 -0700667{
jeffhao77ae36b2012-08-07 14:18:16 -0700668#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700669 RegLocation rlSrc1 = info->args[0];
670 RegLocation rlSrc2 = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700671 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
672 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700673 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
675 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
jeffhao77ae36b2012-08-07 14:18:16 -0700676#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700677 opIT(cUnit, (isMin) ? kArmCondGt : kArmCondLt, "E");
678 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc2.lowReg);
679 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc1.lowReg);
680 genBarrier(cUnit);
jeffhao77ae36b2012-08-07 14:18:16 -0700681#elif defined(TARGET_X86)
682 LIR* branch = newLIR2(cUnit, kX86Jcc8, 0, isMin ? kX86CondG : kX86CondL);
683 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc1.lowReg);
684 LIR* branch2 = newLIR1(cUnit, kX86Jmp8, 0);
685 branch->target = newLIR0(cUnit, kPseudoTargetLabel);
686 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc2.lowReg);
687 branch2->target = newLIR0(cUnit, kPseudoTargetLabel);
688#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700689 storeValue(cUnit, rlDest, rlResult);
690 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700691#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700692 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700693#endif
694}
695
696// Generates an inlined String.isEmpty or String.length.
buzbee3b3dbdd2012-06-13 13:39:34 -0700697bool genInlinedStringIsEmptyOrLength(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700698 bool isEmpty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700699{
jeffhao77ae36b2012-08-07 14:18:16 -0700700#if defined(TARGET_ARM) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 // dst = src.length();
buzbee15bf9802012-06-12 17:49:27 -0700702 RegLocation rlObj = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700703 rlObj = loadValue(cUnit, rlObj, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700704 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee15bf9802012-06-12 17:49:27 -0700706 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700707 loadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
708 rlResult.lowReg);
709 if (isEmpty) {
710 // dst = (dst == 0);
jeffhao77ae36b2012-08-07 14:18:16 -0700711#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700712 int tReg = oatAllocTemp(cUnit);
713 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
714 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
jeffhao77ae36b2012-08-07 14:18:16 -0700715#elif defined(TARGET_X86)
716 opRegImm(cUnit, kOpSub, rlResult.lowReg, 1);
717 opRegImm(cUnit, kOpLsr, rlResult.lowReg, 31);
718#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700719 }
720 storeValue(cUnit, rlDest, rlResult);
721 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700722#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700723 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700724#endif
725}
726
buzbee3b3dbdd2012-06-13 13:39:34 -0700727bool genInlinedAbsInt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700728{
jeffhao77ae36b2012-08-07 14:18:16 -0700729#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700730 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700731 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700732 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700733 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
734 int signReg = oatAllocTemp(cUnit);
735 // abs(x) = y<=x>>31, (x+y)^y.
736 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
737 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
738 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
739 storeValue(cUnit, rlDest, rlResult);
740 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700741#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700742 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700743#endif
744}
745
buzbee3b3dbdd2012-06-13 13:39:34 -0700746bool genInlinedAbsLong(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700747{
748#if defined(TARGET_ARM)
buzbee15bf9802012-06-12 17:49:27 -0700749 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700750 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700751 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700752 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
753 int signReg = oatAllocTemp(cUnit);
754 // abs(x) = y<=x>>31, (x+y)^y.
755 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
756 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
757 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
758 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
759 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
760 storeValueWide(cUnit, rlDest, rlResult);
761 return true;
jeffhao77ae36b2012-08-07 14:18:16 -0700762#elif defined(TARGET_X86)
763 // Reuse source registers to avoid running out of temps
764 RegLocation rlSrc = info->args[0];
765 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
766 RegLocation rlDest = inlineTargetWide(cUnit, info);
767 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
768 opRegCopyWide(cUnit, rlResult.lowReg, rlResult.highReg, rlSrc.lowReg, rlSrc.highReg);
769 oatFreeTemp(cUnit, rlSrc.lowReg);
770 oatFreeTemp(cUnit, rlSrc.highReg);
771 int signReg = oatAllocTemp(cUnit);
772 // abs(x) = y<=x>>31, (x+y)^y.
773 opRegRegImm(cUnit, kOpAsr, signReg, rlResult.highReg, 31);
774 opRegReg(cUnit, kOpAdd, rlResult.lowReg, signReg);
775 opRegReg(cUnit, kOpAdc, rlResult.highReg, signReg);
776 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
777 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
778 storeValueWide(cUnit, rlDest, rlResult);
779 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700780#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700781 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700782#endif
783}
784
buzbee3b3dbdd2012-06-13 13:39:34 -0700785bool genInlinedFloatCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700786{
jeffhaobabda952012-08-02 15:55:30 -0700787#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700788 RegLocation rlSrc = info->args[0];
789 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700790 storeValue(cUnit, rlDest, rlSrc);
791 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700792#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700793 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700794#endif
795}
796
buzbee3b3dbdd2012-06-13 13:39:34 -0700797bool genInlinedDoubleCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700798{
jeffhaobabda952012-08-02 15:55:30 -0700799#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700800 RegLocation rlSrc = info->args[0];
801 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700802 storeValueWide(cUnit, rlDest, rlSrc);
803 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700804#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700806#endif
807}
808
809/*
810 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
811 * otherwise bails to standard library code.
812 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700813bool genInlinedIndexOf(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700814 bool zeroBased)
buzbeefc9e6fa2012-03-23 15:14:29 -0700815{
jeffhao86e46712012-08-08 17:30:59 -0700816#if defined(TARGET_ARM) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700817 oatClobberCalleeSave(cUnit);
818 oatLockCallTemps(cUnit); // Using fixed registers
819 int regPtr = rARG0;
820 int regChar = rARG1;
821 int regStart = rARG2;
buzbeefc9e6fa2012-03-23 15:14:29 -0700822
buzbee15bf9802012-06-12 17:49:27 -0700823 RegLocation rlObj = info->args[0];
824 RegLocation rlChar = info->args[1];
825 RegLocation rlStart = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700826 loadValueDirectFixed(cUnit, rlObj, regPtr);
827 loadValueDirectFixed(cUnit, rlChar, regChar);
828 if (zeroBased) {
829 loadConstant(cUnit, regStart, 0);
830 } else {
831 loadValueDirectFixed(cUnit, rlStart, regStart);
832 }
jeffhao86e46712012-08-08 17:30:59 -0700833#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf));
jeffhao86e46712012-08-08 17:30:59 -0700835#endif
buzbee15bf9802012-06-12 17:49:27 -0700836 genNullCheck(cUnit, rlObj.sRegLow, regPtr, info->optFlags);
837 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700838 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
839 (intptr_t)launchPad);
840 opCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
jeffhao86e46712012-08-08 17:30:59 -0700841#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700842 opReg(cUnit, kOpBlx, rTgt);
jeffhao86e46712012-08-08 17:30:59 -0700843#else
844 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
845#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700846 LIR* resumeTgt = newLIR0(cUnit, kPseudoTargetLabel);
847 launchPad->operands[2] = (uintptr_t)resumeTgt;
Bill Buzbeea114add2012-05-03 15:00:40 -0700848 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700849 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
850 RegLocation rlReturn = oatGetReturn(cUnit, false);
851 RegLocation rlDest = inlineTarget(cUnit, info);
852 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700853 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700854#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700855 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700856#endif
857}
858
859/* Fast string.compareTo(Ljava/lang/string;)I. */
buzbee3b3dbdd2012-06-13 13:39:34 -0700860bool genInlinedStringCompareTo(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700861{
jeffhao86e46712012-08-08 17:30:59 -0700862#if defined(TARGET_ARM) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 oatClobberCalleeSave(cUnit);
864 oatLockCallTemps(cUnit); // Using fixed registers
865 int regThis = rARG0;
866 int regCmp = rARG1;
buzbeefc9e6fa2012-03-23 15:14:29 -0700867
buzbee15bf9802012-06-12 17:49:27 -0700868 RegLocation rlThis = info->args[0];
869 RegLocation rlCmp = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700870 loadValueDirectFixed(cUnit, rlThis, regThis);
871 loadValueDirectFixed(cUnit, rlCmp, regCmp);
jeffhao86e46712012-08-08 17:30:59 -0700872#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo));
jeffhao86e46712012-08-08 17:30:59 -0700874#endif
buzbee15bf9802012-06-12 17:49:27 -0700875 genNullCheck(cUnit, rlThis.sRegLow, regThis, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 //TUNING: check if rlCmp.sRegLow is already null checked
buzbee15bf9802012-06-12 17:49:27 -0700877 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700878 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
Elliott Hughes60234562012-06-01 12:25:59 -0700879 (intptr_t)launchPad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 opCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
jeffhao86e46712012-08-08 17:30:59 -0700881#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 opReg(cUnit, kOpBlx, rTgt);
jeffhao86e46712012-08-08 17:30:59 -0700883#else
884 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
885#endif
Elliott Hughes60234562012-06-01 12:25:59 -0700886 launchPad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -0700887 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700888 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
889 RegLocation rlReturn = oatGetReturn(cUnit, false);
890 RegLocation rlDest = inlineTarget(cUnit, info);
891 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700892 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700893#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700894 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700895#endif
896}
897
buzbee3b3dbdd2012-06-13 13:39:34 -0700898bool genIntrinsic(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700899{
buzbeee33c92b2012-06-20 17:45:00 -0700900 if ((info->optFlags & MIR_INLINED) || info->isRange) {
buzbeefc9e6fa2012-03-23 15:14:29 -0700901 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 }
903 /*
904 * TODO: move these to a target-specific structured constant array
905 * and use a generic match function. The list of intrinsics may be
906 * slightly different depending on target.
907 * TODO: Fold this into a matching function that runs during
908 * basic block building. This should be part of the action for
909 * small method inlining and recognition of the special object init
910 * method. By doing this during basic block construction, we can also
911 * take advantage of/generate new useful dataflow info.
912 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700913 std::string tgtMethod(PrettyMethod(info->index, *cUnit->dex_file));
Bill Buzbeea114add2012-05-03 15:00:40 -0700914 if (tgtMethod.compare("char java.lang.String.charAt(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700915 return genInlinedCharAt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700916 }
917 if (tgtMethod.compare("int java.lang.Math.min(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700918 return genInlinedMinMaxInt(cUnit, info, true /* isMin */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 }
920 if (tgtMethod.compare("int java.lang.Math.max(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700921 return genInlinedMinMaxInt(cUnit, info, false /* isMin */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 }
923 if (tgtMethod.compare("int java.lang.String.length()") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700924 return genInlinedStringIsEmptyOrLength(cUnit, info, false /* isEmpty */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700925 }
926 if (tgtMethod.compare("boolean java.lang.String.isEmpty()") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700927 return genInlinedStringIsEmptyOrLength(cUnit, info, true /* isEmpty */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700928 }
929 if (tgtMethod.compare("int java.lang.Math.abs(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700930 return genInlinedAbsInt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700931 }
932 if (tgtMethod.compare("long java.lang.Math.abs(long)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700933 return genInlinedAbsLong(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700934 }
935 if (tgtMethod.compare("int java.lang.Float.floatToRawIntBits(float)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700936 return genInlinedFloatCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700937 }
938 if (tgtMethod.compare("float java.lang.Float.intBitsToFloat(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700939 return genInlinedFloatCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700940 }
941 if (tgtMethod.compare("long java.lang.Double.doubleToRawLongBits(double)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700942 return genInlinedDoubleCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700943 }
944 if (tgtMethod.compare("double java.lang.Double.longBitsToDouble(long)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700945 return genInlinedDoubleCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700946 }
947 if (tgtMethod.compare("int java.lang.String.indexOf(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700948 return genInlinedIndexOf(cUnit, info, false /* base 0 */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700949 }
950 if (tgtMethod.compare("int java.lang.String.indexOf(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700951 return genInlinedIndexOf(cUnit, info, true /* base 0 */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700952 }
953 if (tgtMethod.compare("int java.lang.String.compareTo(java.lang.String)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700954 return genInlinedStringCompareTo(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700955 }
956 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700957}
958
959
buzbee31a4a6f2012-02-28 15:36:15 -0800960} // namespace art