blob: 29bd2a6c6dd63c3afdbea80e36fa33235aaa02c1 [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{
jeffhao634ea282012-08-10 13:04:01 -0700612#if defined(TARGET_ARM) || defined(TARGET_X86)
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;
buzbee15bf9802012-06-12 17:49:27 -0700627 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
628 bool rangeCheck = (!(info->optFlags & MIR_IGNORE_RANGE_CHECK));
jeffhao634ea282012-08-10 13:04:01 -0700629 LIR* launchPad = NULL;
630#if !defined(TARGET_X86)
631 int regOff = oatAllocTemp(cUnit);
632 int regPtr = oatAllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 if (rangeCheck) {
634 regMax = oatAllocTemp(cUnit);
635 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
636 }
637 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
638 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 if (rangeCheck) {
640 // Set up a launch pad to allow retry in case of bounds violation */
buzbee15bf9802012-06-12 17:49:27 -0700641 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
643 (intptr_t)launchPad);
644 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
645 oatFreeTemp(cUnit, regMax);
646 opCondBranch(cUnit, kCondCs, launchPad);
647 }
jeffhao634ea282012-08-10 13:04:01 -0700648#else
649 if (rangeCheck) {
650 regMax = oatAllocTemp(cUnit);
651 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
652 // Set up a launch pad to allow retry in case of bounds violation */
653 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
654 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
655 (intptr_t)launchPad);
656 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
657 oatFreeTemp(cUnit, regMax);
658 opCondBranch(cUnit, kCondCc, launchPad);
659 }
660 int regOff = oatAllocTemp(cUnit);
661 int regPtr = oatAllocTemp(cUnit);
662 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
663 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
664#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
666 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
jeffhao634ea282012-08-10 13:04:01 -0700667 oatFreeTemp(cUnit, rlObj.lowReg);
668 oatFreeTemp(cUnit, rlIdx.lowReg);
buzbee15bf9802012-06-12 17:49:27 -0700669 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700670 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
671 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
672 oatFreeTemp(cUnit, regOff);
673 oatFreeTemp(cUnit, regPtr);
674 storeValue(cUnit, rlDest, rlResult);
675 if (rangeCheck) {
Elliott Hughes60234562012-06-01 12:25:59 -0700676 launchPad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700677 }
678 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700679 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700681#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700682 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700683#endif
684}
685
buzbee3b3dbdd2012-06-13 13:39:34 -0700686bool genInlinedMinMaxInt(CompilationUnit *cUnit, CallInfo* info, bool isMin)
buzbeefc9e6fa2012-03-23 15:14:29 -0700687{
jeffhao77ae36b2012-08-07 14:18:16 -0700688#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700689 RegLocation rlSrc1 = info->args[0];
690 RegLocation rlSrc2 = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700691 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
692 rlSrc2 = loadValue(cUnit, rlSrc2, 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);
695 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
jeffhao77ae36b2012-08-07 14:18:16 -0700696#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700697 opIT(cUnit, (isMin) ? kArmCondGt : kArmCondLt, "E");
698 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc2.lowReg);
699 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc1.lowReg);
700 genBarrier(cUnit);
jeffhao77ae36b2012-08-07 14:18:16 -0700701#elif defined(TARGET_X86)
702 LIR* branch = newLIR2(cUnit, kX86Jcc8, 0, isMin ? kX86CondG : kX86CondL);
703 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc1.lowReg);
704 LIR* branch2 = newLIR1(cUnit, kX86Jmp8, 0);
705 branch->target = newLIR0(cUnit, kPseudoTargetLabel);
706 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc2.lowReg);
707 branch2->target = newLIR0(cUnit, kPseudoTargetLabel);
708#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 storeValue(cUnit, rlDest, rlResult);
710 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700711#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700712 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700713#endif
714}
715
716// Generates an inlined String.isEmpty or String.length.
buzbee3b3dbdd2012-06-13 13:39:34 -0700717bool genInlinedStringIsEmptyOrLength(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700718 bool isEmpty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700719{
jeffhao77ae36b2012-08-07 14:18:16 -0700720#if defined(TARGET_ARM) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 // dst = src.length();
buzbee15bf9802012-06-12 17:49:27 -0700722 RegLocation rlObj = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700723 rlObj = loadValue(cUnit, rlObj, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700724 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700725 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee15bf9802012-06-12 17:49:27 -0700726 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700727 loadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
728 rlResult.lowReg);
729 if (isEmpty) {
730 // dst = (dst == 0);
jeffhao77ae36b2012-08-07 14:18:16 -0700731#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 int tReg = oatAllocTemp(cUnit);
733 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
734 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
jeffhao77ae36b2012-08-07 14:18:16 -0700735#elif defined(TARGET_X86)
736 opRegImm(cUnit, kOpSub, rlResult.lowReg, 1);
737 opRegImm(cUnit, kOpLsr, rlResult.lowReg, 31);
738#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700739 }
740 storeValue(cUnit, rlDest, rlResult);
741 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700742#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700743 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700744#endif
745}
746
buzbee3b3dbdd2012-06-13 13:39:34 -0700747bool genInlinedAbsInt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700748{
jeffhao77ae36b2012-08-07 14:18:16 -0700749#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700750 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700751 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700752 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
754 int signReg = oatAllocTemp(cUnit);
755 // abs(x) = y<=x>>31, (x+y)^y.
756 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
757 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
758 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
759 storeValue(cUnit, rlDest, rlResult);
760 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700761#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700762 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700763#endif
764}
765
buzbee3b3dbdd2012-06-13 13:39:34 -0700766bool genInlinedAbsLong(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700767{
768#if defined(TARGET_ARM)
buzbee15bf9802012-06-12 17:49:27 -0700769 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700770 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700771 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700772 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
773 int signReg = oatAllocTemp(cUnit);
774 // abs(x) = y<=x>>31, (x+y)^y.
775 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
776 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
777 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
778 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
779 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
780 storeValueWide(cUnit, rlDest, rlResult);
781 return true;
jeffhao77ae36b2012-08-07 14:18:16 -0700782#elif defined(TARGET_X86)
783 // Reuse source registers to avoid running out of temps
784 RegLocation rlSrc = info->args[0];
785 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
786 RegLocation rlDest = inlineTargetWide(cUnit, info);
787 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
788 opRegCopyWide(cUnit, rlResult.lowReg, rlResult.highReg, rlSrc.lowReg, rlSrc.highReg);
789 oatFreeTemp(cUnit, rlSrc.lowReg);
790 oatFreeTemp(cUnit, rlSrc.highReg);
791 int signReg = oatAllocTemp(cUnit);
792 // abs(x) = y<=x>>31, (x+y)^y.
793 opRegRegImm(cUnit, kOpAsr, signReg, rlResult.highReg, 31);
794 opRegReg(cUnit, kOpAdd, rlResult.lowReg, signReg);
795 opRegReg(cUnit, kOpAdc, rlResult.highReg, signReg);
796 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
797 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
798 storeValueWide(cUnit, rlDest, rlResult);
799 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700800#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700802#endif
803}
804
buzbee3b3dbdd2012-06-13 13:39:34 -0700805bool genInlinedFloatCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700806{
jeffhaobabda952012-08-02 15:55:30 -0700807#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700808 RegLocation rlSrc = info->args[0];
809 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700810 storeValue(cUnit, rlDest, rlSrc);
811 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700812#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700813 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700814#endif
815}
816
buzbee3b3dbdd2012-06-13 13:39:34 -0700817bool genInlinedDoubleCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700818{
jeffhaobabda952012-08-02 15:55:30 -0700819#if defined(TARGET_ARM) || defined(TARGET_X86)
buzbee15bf9802012-06-12 17:49:27 -0700820 RegLocation rlSrc = info->args[0];
821 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700822 storeValueWide(cUnit, rlDest, rlSrc);
823 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700824#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700826#endif
827}
828
829/*
830 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
831 * otherwise bails to standard library code.
832 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700833bool genInlinedIndexOf(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700834 bool zeroBased)
buzbeefc9e6fa2012-03-23 15:14:29 -0700835{
jeffhao86e46712012-08-08 17:30:59 -0700836#if defined(TARGET_ARM) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 oatClobberCalleeSave(cUnit);
838 oatLockCallTemps(cUnit); // Using fixed registers
839 int regPtr = rARG0;
840 int regChar = rARG1;
841 int regStart = rARG2;
buzbeefc9e6fa2012-03-23 15:14:29 -0700842
buzbee15bf9802012-06-12 17:49:27 -0700843 RegLocation rlObj = info->args[0];
844 RegLocation rlChar = info->args[1];
845 RegLocation rlStart = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700846 loadValueDirectFixed(cUnit, rlObj, regPtr);
847 loadValueDirectFixed(cUnit, rlChar, regChar);
848 if (zeroBased) {
849 loadConstant(cUnit, regStart, 0);
850 } else {
851 loadValueDirectFixed(cUnit, rlStart, regStart);
852 }
jeffhao86e46712012-08-08 17:30:59 -0700853#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700854 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf));
jeffhao86e46712012-08-08 17:30:59 -0700855#endif
buzbee15bf9802012-06-12 17:49:27 -0700856 genNullCheck(cUnit, rlObj.sRegLow, regPtr, info->optFlags);
857 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700858 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
859 (intptr_t)launchPad);
860 opCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
jeffhao86e46712012-08-08 17:30:59 -0700861#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700862 opReg(cUnit, kOpBlx, rTgt);
jeffhao86e46712012-08-08 17:30:59 -0700863#else
864 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
865#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700866 LIR* resumeTgt = newLIR0(cUnit, kPseudoTargetLabel);
867 launchPad->operands[2] = (uintptr_t)resumeTgt;
Bill Buzbeea114add2012-05-03 15:00:40 -0700868 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700869 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
870 RegLocation rlReturn = oatGetReturn(cUnit, false);
871 RegLocation rlDest = inlineTarget(cUnit, info);
872 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700874#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700875 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700876#endif
877}
878
879/* Fast string.compareTo(Ljava/lang/string;)I. */
buzbee3b3dbdd2012-06-13 13:39:34 -0700880bool genInlinedStringCompareTo(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700881{
jeffhao86e46712012-08-08 17:30:59 -0700882#if defined(TARGET_ARM) || defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700883 oatClobberCalleeSave(cUnit);
884 oatLockCallTemps(cUnit); // Using fixed registers
885 int regThis = rARG0;
886 int regCmp = rARG1;
buzbeefc9e6fa2012-03-23 15:14:29 -0700887
buzbee15bf9802012-06-12 17:49:27 -0700888 RegLocation rlThis = info->args[0];
889 RegLocation rlCmp = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700890 loadValueDirectFixed(cUnit, rlThis, regThis);
891 loadValueDirectFixed(cUnit, rlCmp, regCmp);
jeffhao86e46712012-08-08 17:30:59 -0700892#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700893 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo));
jeffhao86e46712012-08-08 17:30:59 -0700894#endif
buzbee15bf9802012-06-12 17:49:27 -0700895 genNullCheck(cUnit, rlThis.sRegLow, regThis, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 //TUNING: check if rlCmp.sRegLow is already null checked
buzbee15bf9802012-06-12 17:49:27 -0700897 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700898 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
Elliott Hughes60234562012-06-01 12:25:59 -0700899 (intptr_t)launchPad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 opCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
jeffhao86e46712012-08-08 17:30:59 -0700901#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 opReg(cUnit, kOpBlx, rTgt);
jeffhao86e46712012-08-08 17:30:59 -0700903#else
904 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
905#endif
Elliott Hughes60234562012-06-01 12:25:59 -0700906 launchPad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -0700907 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700908 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
909 RegLocation rlReturn = oatGetReturn(cUnit, false);
910 RegLocation rlDest = inlineTarget(cUnit, info);
911 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700912 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700913#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700914 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700915#endif
916}
917
buzbee3b3dbdd2012-06-13 13:39:34 -0700918bool genIntrinsic(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700919{
buzbeee33c92b2012-06-20 17:45:00 -0700920 if ((info->optFlags & MIR_INLINED) || info->isRange) {
buzbeefc9e6fa2012-03-23 15:14:29 -0700921 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 }
923 /*
924 * TODO: move these to a target-specific structured constant array
925 * and use a generic match function. The list of intrinsics may be
926 * slightly different depending on target.
927 * TODO: Fold this into a matching function that runs during
928 * basic block building. This should be part of the action for
929 * small method inlining and recognition of the special object init
930 * method. By doing this during basic block construction, we can also
931 * take advantage of/generate new useful dataflow info.
932 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700933 std::string tgtMethod(PrettyMethod(info->index, *cUnit->dex_file));
Bill Buzbeea114add2012-05-03 15:00:40 -0700934 if (tgtMethod.compare("char java.lang.String.charAt(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700935 return genInlinedCharAt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700936 }
937 if (tgtMethod.compare("int java.lang.Math.min(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700938 return genInlinedMinMaxInt(cUnit, info, true /* isMin */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700939 }
940 if (tgtMethod.compare("int java.lang.Math.max(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700941 return genInlinedMinMaxInt(cUnit, info, false /* isMin */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 }
943 if (tgtMethod.compare("int java.lang.String.length()") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700944 return genInlinedStringIsEmptyOrLength(cUnit, info, false /* isEmpty */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 }
946 if (tgtMethod.compare("boolean java.lang.String.isEmpty()") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700947 return genInlinedStringIsEmptyOrLength(cUnit, info, true /* isEmpty */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 }
949 if (tgtMethod.compare("int java.lang.Math.abs(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700950 return genInlinedAbsInt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700951 }
952 if (tgtMethod.compare("long java.lang.Math.abs(long)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700953 return genInlinedAbsLong(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700954 }
955 if (tgtMethod.compare("int java.lang.Float.floatToRawIntBits(float)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700956 return genInlinedFloatCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 }
958 if (tgtMethod.compare("float java.lang.Float.intBitsToFloat(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700959 return genInlinedFloatCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700960 }
961 if (tgtMethod.compare("long java.lang.Double.doubleToRawLongBits(double)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700962 return genInlinedDoubleCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700963 }
964 if (tgtMethod.compare("double java.lang.Double.longBitsToDouble(long)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700965 return genInlinedDoubleCvt(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700966 }
967 if (tgtMethod.compare("int java.lang.String.indexOf(int, int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700968 return genInlinedIndexOf(cUnit, info, false /* base 0 */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700969 }
970 if (tgtMethod.compare("int java.lang.String.indexOf(int)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700971 return genInlinedIndexOf(cUnit, info, true /* base 0 */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700972 }
973 if (tgtMethod.compare("int java.lang.String.compareTo(java.lang.String)") == 0) {
buzbee15bf9802012-06-12 17:49:27 -0700974 return genInlinedStringCompareTo(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700975 }
976 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700977}
978
979
buzbee31a4a6f2012-02-28 15:36:15 -0800980} // namespace art