blob: a904419138a1dfefb4706241c593b24154ffec90 [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
buzbee31a4a6f2012-02-28 15:36:15 -080027typedef int (*NextCallInsn)(CompilationUnit*, MIR*, int, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -070028 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -070029 uintptr_t directMethod, InvokeType type);
buzbeefc9e6fa2012-03-23 15:14:29 -070030LIR* opCondBranch(CompilationUnit* cUnit, ConditionCode cc, LIR* target);
31
buzbee31a4a6f2012-02-28 15:36:15 -080032/*
33 * If there are any ins passed in registers that have not been promoted
34 * to a callee-save register, flush them to the frame. Perform intial
35 * assignment of promoted arguments.
36 */
37void flushIns(CompilationUnit* cUnit)
38{
buzbee9c044ce2012-03-18 13:24:07 -070039 /*
40 * Dummy up a RegLocation for the incoming Method*
41 * It will attempt to keep rARG0 live (or copy it to home location
42 * if promoted).
43 */
44 RegLocation rlSrc = cUnit->regLocation[cUnit->methodSReg];
45 RegLocation rlMethod = cUnit->regLocation[cUnit->methodSReg];
46 rlSrc.location = kLocPhysReg;
47 rlSrc.lowReg = rARG0;
48 rlSrc.home = false;
49 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
50 storeValue(cUnit, rlMethod, rlSrc);
51 // If Method* has been promoted, explicitly flush
52 if (rlMethod.location == kLocPhysReg) {
53 storeWordDisp(cUnit, rSP, 0, rARG0);
54 }
55
buzbee31a4a6f2012-02-28 15:36:15 -080056 if (cUnit->numIns == 0)
57 return;
Ian Rogersb3ab25b2012-03-19 01:12:01 -070058 const int numArgRegs = 3;
59 static int argRegs[] = {rARG1, rARG2, rARG3};
buzbee31a4a6f2012-02-28 15:36:15 -080060 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
61 /*
buzbee86a4bce2012-03-06 18:15:00 -080062 * Copy incoming arguments to their proper home locations.
63 * NOTE: an older version of dx had an issue in which
64 * it would reuse static method argument registers.
buzbee31a4a6f2012-02-28 15:36:15 -080065 * This could result in the same Dalvik virtual register
buzbee86a4bce2012-03-06 18:15:00 -080066 * being promoted to both core and fp regs. To account for this,
67 * we only copy to the corresponding promoted physical register
68 * if it matches the type of the SSA name for the incoming
69 * argument. It is also possible that long and double arguments
70 * end up half-promoted. In those cases, we must flush the promoted
71 * half to memory as well.
buzbee31a4a6f2012-02-28 15:36:15 -080072 */
73 for (int i = 0; i < cUnit->numIns; i++) {
buzbee86a4bce2012-03-06 18:15:00 -080074 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
Ian Rogersb3ab25b2012-03-19 01:12:01 -070075 if (i < numArgRegs) {
buzbee31a4a6f2012-02-28 15:36:15 -080076 // If arriving in register
buzbee86a4bce2012-03-06 18:15:00 -080077 bool needFlush = true;
78 RegLocation* tLoc = &cUnit->regLocation[startVReg + i];
79 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070080 opRegCopy(cUnit, vMap->coreReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080081 needFlush = false;
82 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -070083 opRegCopy(cUnit, vMap->fpReg, argRegs[i]);
buzbee86a4bce2012-03-06 18:15:00 -080084 needFlush = false;
85 } else {
86 needFlush = true;
buzbee31a4a6f2012-02-28 15:36:15 -080087 }
buzbee86a4bce2012-03-06 18:15:00 -080088
89 // For wide args, force flush if only half is promoted
90 if (tLoc->wide) {
91 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
92 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
93 (pMap->fpLocation != vMap->fpLocation);
buzbee31a4a6f2012-02-28 15:36:15 -080094 }
buzbee86a4bce2012-03-06 18:15:00 -080095 if (needFlush) {
96 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
Ian Rogersb3ab25b2012-03-19 01:12:01 -070097 argRegs[i], kWord);
buzbee86a4bce2012-03-06 18:15:00 -080098 }
buzbee31a4a6f2012-02-28 15:36:15 -080099 } else {
100 // If arriving in frame & promoted
buzbee86a4bce2012-03-06 18:15:00 -0800101 if (vMap->coreLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800102 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800103 vMap->coreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800104 }
buzbee86a4bce2012-03-06 18:15:00 -0800105 if (vMap->fpLocation == kLocPhysReg) {
buzbee31a4a6f2012-02-28 15:36:15 -0800106 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, startVReg + i),
buzbee86a4bce2012-03-06 18:15:00 -0800107 vMap->fpReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800108 }
109 }
110 }
111}
112
Ian Rogers3fa13792012-03-18 15:53:45 -0700113void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget, const DexFile* dexFile, uint32_t dexMethodIdx)
114{
115 LIR* curTarget = cUnit->methodLiteralList;
116 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
117 while (curTarget != NULL && nextTarget != NULL) {
118 if (curTarget->operands[0] == (int)dexFile &&
119 nextTarget->operands[0] == (int)dexMethodIdx) {
120 *codeTarget = curTarget;
121 *methodTarget = nextTarget;
122 DCHECK((*codeTarget)->next == *methodTarget);
123 DCHECK_EQ((*codeTarget)->operands[0], (int)dexFile);
124 DCHECK_EQ((*methodTarget)->operands[0], (int)dexMethodIdx);
125 break;
126 }
127 curTarget = nextTarget->next;
128 nextTarget = curTarget != NULL ? curTarget->next : NULL;
129 }
130}
131
buzbee31a4a6f2012-02-28 15:36:15 -0800132/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700133 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800134 * emit the next instruction in static & direct invoke sequences.
135 */
136int nextSDCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700137 int state, uint32_t dexIdx, uint32_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700138 uintptr_t directCode, uintptr_t directMethod,
139 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800140{
Brian Carlstromf5822582012-03-19 22:34:31 -0700141#if !defined(TARGET_ARM)
142 directCode = 0;
143 directMethod = 0;
144#endif
Ian Rogers2ed3b952012-03-17 11:49:39 -0700145 if (directCode != 0 && directMethod != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700146 switch (state) {
Ian Rogers2ed3b952012-03-17 11:49:39 -0700147 case 0: // Get the current Method* [sets rARG0]
Ian Rogers3fa13792012-03-18 15:53:45 -0700148 if (directCode != (uintptr_t)-1) {
149 loadConstant(cUnit, rINVOKE_TGT, directCode);
150 } else {
151 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
152 if (dataTarget == NULL) {
153 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700154 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700155 }
156#if defined(TARGET_ARM)
157 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
158 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
159 oatAppendLIR(cUnit, loadPcRel);
160#else
161 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
162#endif
163 }
164 if (directMethod != (uintptr_t)-1) {
165 loadConstant(cUnit, rARG0, directMethod);
166 } else {
167 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
168 if (dataTarget == NULL) {
169 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700170 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700171 }
172#if defined(TARGET_ARM)
173 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
174 kThumb2LdrPcRel12, rARG0, 0, 0, 0, 0, dataTarget);
175 oatAppendLIR(cUnit, loadPcRel);
176#else
177 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
178#endif
179 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700180 break;
181 default:
182 return -1;
183 }
184 } else {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700185 switch (state) {
buzbee31a4a6f2012-02-28 15:36:15 -0800186 case 0: // Get the current Method* [sets rARG0]
buzbee9c044ce2012-03-18 13:24:07 -0700187 // TUNING: we can save a reg copy if Method* has been promoted
buzbee31a4a6f2012-02-28 15:36:15 -0800188 loadCurrMethodDirect(cUnit, rARG0);
189 break;
190 case 1: // Get method->dex_cache_resolved_methods_
191 loadWordDisp(cUnit, rARG0,
192 Method::DexCacheResolvedMethodsOffset().Int32Value(),
193 rARG0);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700194 // Set up direct code if known.
195 if (directCode != 0) {
Ian Rogers3fa13792012-03-18 15:53:45 -0700196 if (directCode != (uintptr_t)-1) {
197 loadConstant(cUnit, rINVOKE_TGT, directCode);
198 } else {
199 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
200 if (dataTarget == NULL) {
201 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Brian Carlstromf5822582012-03-19 22:34:31 -0700202 dataTarget->operands[1] = type;
Ian Rogers3fa13792012-03-18 15:53:45 -0700203 }
204#if defined(TARGET_ARM)
205 LIR* loadPcRel = rawLIR(cUnit, cUnit->currentDalvikOffset,
206 kThumb2LdrPcRel12, rINVOKE_TGT, 0, 0, 0, 0, dataTarget);
207 oatAppendLIR(cUnit, loadPcRel);
208#else
209 UNIMPLEMENTED(FATAL) << (void*)dataTarget;
210#endif
211 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700212 }
buzbee31a4a6f2012-02-28 15:36:15 -0800213 break;
214 case 2: // Grab target method*
215 loadWordDisp(cUnit, rARG0,
216 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
217 rARG0);
218 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700219#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800220 case 3: // Grab the code from the method*
Ian Rogers2ed3b952012-03-17 11:49:39 -0700221 if (directCode == 0) {
222 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
223 rINVOKE_TGT);
224 }
buzbee31a4a6f2012-02-28 15:36:15 -0800225 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700226#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800227 default:
228 return -1;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700229 }
buzbee31a4a6f2012-02-28 15:36:15 -0800230 }
231 return state + 1;
232}
233
234/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700235 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800236 * emit the next instruction in a virtual invoke sequence.
237 * We can use rLR as a temp prior to target address loading
238 * Note also that we'll load the first argument ("this") into
239 * rARG1 here rather than the standard loadArgRegs.
240 */
241int nextVCallInsn(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700242 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700243 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800244{
245 RegLocation rlArg;
246 /*
247 * This is the fast path in which the target virtual method is
248 * fully resolved at compile time.
249 */
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700250 switch (state) {
buzbee31a4a6f2012-02-28 15:36:15 -0800251 case 0: // Get "this" [set rARG1]
252 rlArg = oatGetSrc(cUnit, mir, 0);
253 loadValueDirectFixed(cUnit, rlArg, rARG1);
254 break;
255 case 1: // Is "this" null? [use rARG1]
256 genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
buzbee0398c422012-03-02 15:22:47 -0800257 // get this->klass_ [use rARG1, set rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800258 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800259 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800260 break;
buzbee0398c422012-03-02 15:22:47 -0800261 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
262 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
263 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800264 break;
buzbee0398c422012-03-02 15:22:47 -0800265 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
266 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
buzbee31a4a6f2012-02-28 15:36:15 -0800267 Array::DataOffset(sizeof(Object*)).Int32Value(),
268 rARG0);
269 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700270#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -0800271 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
buzbee31a4a6f2012-02-28 15:36:15 -0800272 loadWordDisp(cUnit, rARG0, Method::GetCodeOffset().Int32Value(),
buzbee0398c422012-03-02 15:22:47 -0800273 rINVOKE_TGT);
buzbee31a4a6f2012-02-28 15:36:15 -0800274 break;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700275#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800276 default:
277 return -1;
278 }
279 return state + 1;
280}
281
buzbee31a4a6f2012-02-28 15:36:15 -0800282int nextInvokeInsnSP(CompilationUnit* cUnit, MIR* mir, int trampoline,
283 int state, uint32_t dexIdx, uint32_t methodIdx)
284{
285 /*
286 * This handles the case in which the base method is not fully
287 * resolved at compile time, we bail to a runtime helper.
288 */
289 if (state == 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700290#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800291 // Load trampoline target
buzbee0398c422012-03-02 15:22:47 -0800292 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
Ian Rogers7caad772012-03-30 01:07:54 -0700293#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800294 // Load rARG0 with method index
295 loadConstant(cUnit, rARG0, dexIdx);
296 return 1;
297 }
298 return -1;
299}
300
301int nextStaticCallInsnSP(CompilationUnit* cUnit, MIR* mir,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700302 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700303 uintptr_t unused, uintptr_t unused2,
304 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800305{
Ian Rogers57b86d42012-03-27 16:05:41 -0700306 int trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800307 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
308}
309
310int nextDirectCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700311 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700312 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800313{
Ian Rogers57b86d42012-03-27 16:05:41 -0700314 int trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800315 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
316}
317
318int nextSuperCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700319 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700320 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800321{
Ian Rogers57b86d42012-03-27 16:05:41 -0700322 int trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800323 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
324}
325
326int nextVCallInsnSP(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700327 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700328 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800329{
Ian Rogers57b86d42012-03-27 16:05:41 -0700330 int trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800331 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
332}
333
334/*
335 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
336 * which will locate the target and continue on via a tail call.
337 */
338int nextInterfaceCallInsn(CompilationUnit* cUnit, MIR* mir, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700339 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700340 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800341{
Ian Rogers57b86d42012-03-27 16:05:41 -0700342 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
buzbee31a4a6f2012-02-28 15:36:15 -0800343 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
344}
345
346int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit, MIR* mir,
347 int state, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700348 uint32_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700349 uintptr_t unused3, InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800350{
Ian Rogers57b86d42012-03-27 16:05:41 -0700351 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
buzbee31a4a6f2012-02-28 15:36:15 -0800352 return nextInvokeInsnSP(cUnit, mir, trampoline, state, dexIdx, 0);
353}
354
355int loadArgRegs(CompilationUnit* cUnit, MIR* mir, DecodedInstruction* dInsn,
356 int callState, NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700357 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -0700358 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800359{
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700360 int lastArgReg = rARG3;
buzbee31a4a6f2012-02-28 15:36:15 -0800361 int nextReg = rARG1;
362 int nextArg = 0;
363 if (skipThis) {
364 nextReg++;
365 nextArg++;
366 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700367 for (; (nextReg <= lastArgReg) && (nextArg < mir->ssaRep->numUses); nextReg++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800368 RegLocation rlArg = oatGetRawSrc(cUnit, mir, nextArg++);
369 rlArg = oatUpdateRawLoc(cUnit, rlArg);
370 if (rlArg.wide && (nextReg <= rARG2)) {
371 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
372 nextReg++;
373 nextArg++;
374 } else {
375 rlArg.wide = false;
376 loadValueDirectFixed(cUnit, rlArg, nextReg);
377 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700378 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700379 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800380 }
381 return callState;
382}
383
384/*
385 * Load up to 5 arguments, the first three of which will be in
386 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
387 * and as part of the load sequence, it must be replaced with
388 * the target method pointer. Note, this may also be called
389 * for "range" variants if the number of arguments is 5 or fewer.
390 */
391int genDalvikArgsNoRange(CompilationUnit* cUnit, MIR* mir,
392 DecodedInstruction* dInsn, int callState,
393 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700394 uint32_t dexIdx, uint32_t methodIdx,
395 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700396 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800397{
398 RegLocation rlArg;
399
400 /* If no arguments, just return */
401 if (dInsn->vA == 0)
402 return callState;
403
Ian Rogers2ed3b952012-03-17 11:49:39 -0700404 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700405 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800406
407 DCHECK_LE(dInsn->vA, 5U);
408 if (dInsn->vA > 3) {
409 uint32_t nextUse = 3;
410 //Detect special case of wide arg spanning arg3/arg4
411 RegLocation rlUse0 = oatGetRawSrc(cUnit, mir, 0);
412 RegLocation rlUse1 = oatGetRawSrc(cUnit, mir, 1);
413 RegLocation rlUse2 = oatGetRawSrc(cUnit, mir, 2);
414 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
415 rlUse2.wide) {
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700416 int reg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800417 // Wide spans, we need the 2nd half of uses[2].
418 rlArg = oatUpdateLocWide(cUnit, rlUse2);
419 if (rlArg.location == kLocPhysReg) {
420 reg = rlArg.highReg;
421 } else {
422 // rARG2 & rARG3 can safely be used here
423 reg = rARG3;
424 loadWordDisp(cUnit, rSP,
425 oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
426 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700427 methodIdx, directCode, directMethod,
428 type);
buzbee31a4a6f2012-02-28 15:36:15 -0800429 }
430 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
431 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700432 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700433 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800434 nextUse++;
435 }
436 // Loop through the rest
437 while (nextUse < dInsn->vA) {
438 int lowReg;
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700439 int highReg = -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800440 rlArg = oatGetRawSrc(cUnit, mir, nextUse);
441 rlArg = oatUpdateRawLoc(cUnit, rlArg);
442 if (rlArg.location == kLocPhysReg) {
443 lowReg = rlArg.lowReg;
444 highReg = rlArg.highReg;
445 } else {
446 lowReg = rARG2;
buzbee31a4a6f2012-02-28 15:36:15 -0800447 if (rlArg.wide) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700448 highReg = rARG3;
buzbee31a4a6f2012-02-28 15:36:15 -0800449 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
450 } else {
451 loadValueDirectFixed(cUnit, rlArg, lowReg);
452 }
453 callState = nextCallInsn(cUnit, mir, callState, dexIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700454 methodIdx, directCode, directMethod,
455 type);
buzbee31a4a6f2012-02-28 15:36:15 -0800456 }
457 int outsOffset = (nextUse + 1) * 4;
458 if (rlArg.wide) {
459 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
460 nextUse += 2;
461 } else {
462 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
463 nextUse++;
464 }
Ian Rogers2ed3b952012-03-17 11:49:39 -0700465 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700466 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800467 }
468 }
469
470 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700471 dexIdx, methodIdx, directCode, directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700472 type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800473
474 if (pcrLabel) {
475 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
476 }
477 return callState;
478}
479
480/*
481 * May have 0+ arguments (also used for jumbo). Note that
482 * source virtual registers may be in physical registers, so may
483 * need to be flushed to home location before copying. This
484 * applies to arg3 and above (see below).
485 *
486 * Two general strategies:
487 * If < 20 arguments
488 * Pass args 3-18 using vldm/vstm block copy
489 * Pass arg0, arg1 & arg2 in rARG1-rARG3
490 * If 20+ arguments
491 * Pass args arg19+ using memcpy block copy
492 * Pass arg0, arg1 & arg2 in rARG1-rARG3
493 *
494 */
495int genDalvikArgsRange(CompilationUnit* cUnit, MIR* mir,
496 DecodedInstruction* dInsn, int callState,
497 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700498 uint32_t dexIdx, uint32_t methodIdx,
499 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700500 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800501{
502 int firstArg = dInsn->vC;
503 int numArgs = dInsn->vA;
504
505 // If we can treat it as non-range (Jumbo ops will use range form)
506 if (numArgs <= 5)
507 return genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pcrLabel,
508 nextCallInsn, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700509 directCode, directMethod, type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800510 /*
511 * Make sure range list doesn't span the break between in normal
512 * Dalvik vRegs and the ins.
513 */
514 int highestArg = oatGetSrc(cUnit, mir, numArgs-1).sRegLow;
515 int boundaryReg = cUnit->numDalvikRegisters - cUnit->numIns;
516 if ((firstArg < boundaryReg) && (highestArg >= boundaryReg)) {
517 LOG(FATAL) << "Argument list spanned locals & args";
518 }
519
520 /*
521 * First load the non-register arguments. Both forms expect all
522 * of the source arguments to be in their home frame location, so
523 * scan the sReg names and flush any that have been promoted to
524 * frame backing storage.
525 */
526 // Scan the rest of the args - if in physReg flush to memory
527 for (int nextArg = 0; nextArg < numArgs;) {
528 RegLocation loc = oatGetRawSrc(cUnit, mir, nextArg);
529 if (loc.wide) {
530 loc = oatUpdateLocWide(cUnit, loc);
531 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
532 storeBaseDispWide(cUnit, rSP,
533 oatSRegOffset(cUnit, loc.sRegLow),
534 loc.lowReg, loc.highReg);
535 }
536 nextArg += 2;
537 } else {
538 loc = oatUpdateLoc(cUnit, loc);
539 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
540 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
541 loc.lowReg, kWord);
542 }
543 nextArg++;
544 }
545 }
546
547 int startOffset = oatSRegOffset(cUnit,
548 cUnit->regLocation[mir->ssaRep->uses[3]].sRegLow);
549 int outsOffset = 4 /* Method* */ + (3 * 4);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700550#if defined(TARGET_MIPS) || defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800551 // Generate memcpy
552 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
553 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogers57b86d42012-03-27 16:05:41 -0700554 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
Ian Rogersab2b55d2012-03-18 00:06:11 -0700555 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800556#else
557 if (numArgs >= 20) {
558 // Generate memcpy
559 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
560 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogers57b86d42012-03-27 16:05:41 -0700561 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
Ian Rogersab2b55d2012-03-18 00:06:11 -0700562 rARG0, rARG1, (numArgs - 3) * 4);
buzbee31a4a6f2012-02-28 15:36:15 -0800563 } else {
564 // Use vldm/vstm pair using rARG3 as a temp
565 int regsLeft = std::min(numArgs - 3, 16);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700566 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700567 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800568 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
569 LIR* ld = newLIR3(cUnit, kThumb2Vldms, rARG3, fr0, regsLeft);
570 //TUNING: loosen barrier
571 ld->defMask = ENCODE_ALL;
572 setMemRefType(ld, true /* isLoad */, kDalvikReg);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700573 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700574 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800575 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
Ian Rogers2ed3b952012-03-17 11:49:39 -0700576 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700577 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800578 LIR* st = newLIR3(cUnit, kThumb2Vstms, rARG3, fr0, regsLeft);
579 setMemRefType(st, false /* isLoad */, kDalvikReg);
580 st->defMask = ENCODE_ALL;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700581 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700582 directCode, directMethod, type);
Ian Rogers2ed3b952012-03-17 11:49:39 -0700583
buzbee31a4a6f2012-02-28 15:36:15 -0800584 }
585#endif
586
587 callState = loadArgRegs(cUnit, mir, dInsn, callState, nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700588 dexIdx, methodIdx, directCode, directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700589 type, skipThis);
buzbee31a4a6f2012-02-28 15:36:15 -0800590
Ian Rogers2ed3b952012-03-17 11:49:39 -0700591 callState = nextCallInsn(cUnit, mir, callState, dexIdx, methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700592 directCode, directMethod, type);
buzbee31a4a6f2012-02-28 15:36:15 -0800593 if (pcrLabel) {
594 *pcrLabel = genNullCheck(cUnit, oatSSASrc(mir,0), rARG1, mir);
595 }
596 return callState;
597}
598
buzbeefc9e6fa2012-03-23 15:14:29 -0700599RegLocation inlineTarget(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir)
600{
601 RegLocation res;
602 mir = oatFindMoveResult(cUnit, bb, mir, false);
603 if (mir == NULL) {
604 res = oatGetReturn(cUnit, false);
605 } else {
606 res = oatGetDest(cUnit, mir, 0);
607 mir->dalvikInsn.opcode = Instruction::NOP;
608 }
609 return res;
610}
611
612RegLocation inlineTargetWide(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir)
613{
614 RegLocation res;
615 mir = oatFindMoveResult(cUnit, bb, mir, true);
616 if (mir == NULL) {
617 res = oatGetReturnWide(cUnit, false);
618 } else {
619 res = oatGetDestWide(cUnit, mir, 0, 1);
620 mir->dalvikInsn.opcode = Instruction::NOP;
621 }
622 return res;
623}
624
625bool genInlinedCharAt(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
626 InvokeType type, bool isRange)
627{
628#if defined(TARGET_ARM)
629 // Location of reference to data array
630 int valueOffset = String::ValueOffset().Int32Value();
631 // Location of count
632 int countOffset = String::CountOffset().Int32Value();
633 // Starting offset within data array
634 int offsetOffset = String::OffsetOffset().Int32Value();
635 // Start of char data with array_
636 int dataOffset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
637
638 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
639 RegLocation rlIdx = oatGetSrc(cUnit, mir, 1);
640 rlObj = loadValue(cUnit, rlObj, kCoreReg);
641 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
642 int regMax;
643 int regOff = oatAllocTemp(cUnit);
644 int regPtr = oatAllocTemp(cUnit);
645 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);
646 bool rangeCheck = (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK));
647 if (rangeCheck) {
648 regMax = oatAllocTemp(cUnit);
649 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
650 }
651 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
652 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
653 LIR* launchPad = NULL;
654 if (rangeCheck) {
655 // Set up a launch pad to allow retry in case of bounds violation */
656 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (int)mir, type);
657 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
658 (intptr_t)launchPad);
659 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
660 oatFreeTemp(cUnit, regMax);
661 opCondBranch(cUnit, kCondCs, launchPad);
662 }
663 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
664 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
665 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
666 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
667 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
668 oatFreeTemp(cUnit, regOff);
669 oatFreeTemp(cUnit, regPtr);
670 storeValue(cUnit, rlDest, rlResult);
671 if (rangeCheck) {
672 launchPad->operands[2] = NULL; // no resumption
673 launchPad->operands[3] = (uintptr_t)bb;
674 }
675 // Record that we've already inlined & null checked
676 mir->optimizationFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
677 return true;
678#else
679 return false;
680#endif
681}
682
683bool genInlinedMinMaxInt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir,
684 bool isMin)
685{
686#if defined(TARGET_ARM)
687 RegLocation rlSrc1 = oatGetSrc(cUnit, mir, 0);
688 RegLocation rlSrc2 = oatGetSrc(cUnit, mir, 1);
689 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
690 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
691 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
692 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
693 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
694 opIT(cUnit, (isMin) ? kArmCondGt : kArmCondLt, "E");
695 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc2.lowReg);
696 opRegReg(cUnit, kOpMov, rlResult.lowReg, rlSrc1.lowReg);
697 genBarrier(cUnit);
698 storeValue(cUnit, rlDest, rlResult);
699 return true;
700#else
701 return false;
702#endif
703}
704
705// Generates an inlined String.isEmpty or String.length.
706bool genInlinedStringIsEmptyOrLength(CompilationUnit* cUnit,
707 BasicBlock* bb, MIR* mir,
708 bool isEmpty)
709{
710#if defined(TARGET_ARM)
711 // dst = src.length();
712 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
713 rlObj = loadValue(cUnit, rlObj, kCoreReg);
714 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
715 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
716 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);
717 loadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
718 rlResult.lowReg);
719 if (isEmpty) {
720 // dst = (dst == 0);
721 int tReg = oatAllocTemp(cUnit);
722 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
723 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
724 }
725 storeValue(cUnit, rlDest, rlResult);
726 return true;
727#else
728 return false;
729#endif
730}
731
732bool genInlinedAbsInt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
733{
734#if defined(TARGET_ARM)
735 RegLocation rlSrc = oatGetSrc(cUnit, mir, 0);
736 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
737 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
738 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
739 int signReg = oatAllocTemp(cUnit);
740 // abs(x) = y<=x>>31, (x+y)^y.
741 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
742 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
743 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
744 storeValue(cUnit, rlDest, rlResult);
745 return true;
746#else
747 return false;
748#endif
749}
750
751bool genInlinedAbsLong(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
752{
753#if defined(TARGET_ARM)
754 RegLocation rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
755 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
756 RegLocation rlDest = inlineTargetWide(cUnit, bb, mir);
757 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
758 int signReg = oatAllocTemp(cUnit);
759 // abs(x) = y<=x>>31, (x+y)^y.
760 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
761 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
762 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
763 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
764 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
765 storeValueWide(cUnit, rlDest, rlResult);
766 return true;
767#else
768 return false;
769#endif
770}
771
772bool genInlinedFloatCvt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
773{
774#if defined(TARGET_ARM)
775 RegLocation rlSrc = oatGetSrc(cUnit, mir, 0);
776 RegLocation rlDest = inlineTarget(cUnit, bb, mir);
777 storeValue(cUnit, rlDest, rlSrc);
778 return true;
779#else
780 return false;
781#endif
782}
783
784bool genInlinedDoubleCvt(CompilationUnit *cUnit, BasicBlock* bb, MIR *mir)
785{
786#if defined(TARGET_ARM)
787 RegLocation rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
788 RegLocation rlDest = inlineTargetWide(cUnit, bb, mir);
789 storeValueWide(cUnit, rlDest, rlSrc);
790 return true;
791#else
792 return false;
793#endif
794}
795
796/*
797 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
798 * otherwise bails to standard library code.
799 */
800bool genInlinedIndexOf(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
801 InvokeType type, bool zeroBased)
802{
803#if defined(TARGET_ARM)
804
805 oatClobberCalleeSave(cUnit);
806 oatLockCallTemps(cUnit); // Using fixed registers
807 int regPtr = rARG0;
808 int regChar = rARG1;
809 int regStart = rARG2;
810
811 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
812 RegLocation rlChar = oatGetSrc(cUnit, mir, 1);
813 RegLocation rlStart = oatGetSrc(cUnit, mir, 2);
814 loadValueDirectFixed(cUnit, rlObj, regPtr);
815 loadValueDirectFixed(cUnit, rlChar, regChar);
816 if (zeroBased) {
817 loadConstant(cUnit, regStart, 0);
818 } else {
819 loadValueDirectFixed(cUnit, rlStart, regStart);
820 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700821 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf));
buzbeefc9e6fa2012-03-23 15:14:29 -0700822 genNullCheck(cUnit, rlObj.sRegLow, regPtr, mir);
823 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (int)mir, type);
824 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
825 (intptr_t)launchPad);
826 opCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
827 opReg(cUnit, kOpBlx, rTgt);
828 LIR* resumeTgt = newLIR0(cUnit, kPseudoTargetLabel);
829 launchPad->operands[2] = (uintptr_t)resumeTgt;
830 launchPad->operands[3] = (uintptr_t)bb;
831 // Record that we've already inlined & null checked
832 mir->optimizationFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
833 return true;
834#else
835 return false;
836#endif
837}
838
839/* Fast string.compareTo(Ljava/lang/string;)I. */
840bool genInlinedStringCompareTo(CompilationUnit* cUnit, BasicBlock* bb,
841 MIR* mir, InvokeType type)
842{
843#if defined(TARGET_ARM)
844 oatClobberCalleeSave(cUnit);
845 oatLockCallTemps(cUnit); // Using fixed registers
846 int regThis = rARG0;
847 int regCmp = rARG1;
848
849 RegLocation rlThis = oatGetSrc(cUnit, mir, 0);
850 RegLocation rlCmp = oatGetSrc(cUnit, mir, 1);
851 loadValueDirectFixed(cUnit, rlThis, regThis);
852 loadValueDirectFixed(cUnit, rlCmp, regCmp);
Ian Rogers57b86d42012-03-27 16:05:41 -0700853 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo));
buzbeefc9e6fa2012-03-23 15:14:29 -0700854 genNullCheck(cUnit, rlThis.sRegLow, regThis, mir);
855 //TUNING: check if rlCmp.sRegLow is already null checked
856 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (int)mir, type);
857 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
858 (intptr_t)launchPad);
859 opCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
860 opReg(cUnit, kOpBlx, rTgt);
861 launchPad->operands[2] = NULL; // No return possible
862 launchPad->operands[3] = (uintptr_t)bb;
863 // Record that we've already inlined & null checked
864 mir->optimizationFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
865 return true;
866#else
867 return false;
868#endif
869}
870
871bool genIntrinsic(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
872 InvokeType type, bool isRange)
873{
874 if ((mir->optimizationFlags & MIR_INLINED) || isRange) {
875 return false;
876 }
877 /*
878 * TODO: move these to a target-specific structured constant array
879 * and use a generic match function. The list of intrinsics may be
880 * slightly different depending on target.
881 * TODO: Fold this into a matching function that runs during
882 * basic block building. This should be part of the action for
883 * small method inlining and recognition of the special object init
884 * method. By doing this during basic block construction, we can also
885 * take advantage of/generate new useful dataflow info.
886 */
887 std::string tgtMethod = PrettyMethod(mir->dalvikInsn.vB, *cUnit->dex_file);
888 if (tgtMethod.compare("char java.lang.String.charAt(int)") == 0) {
889 return genInlinedCharAt(cUnit, bb, mir, type, isRange);
890 }
891 if (tgtMethod.compare("int java.lang.Math.min(int, int)") == 0) {
892 return genInlinedMinMaxInt(cUnit, bb, mir, true /* isMin */);
893 }
894 if (tgtMethod.compare("int java.lang.Math.max(int, int)") == 0) {
895 return genInlinedMinMaxInt(cUnit, bb, mir, false /* isMin */);
896 }
897 if (tgtMethod.compare("int java.lang.String.length()") == 0) {
898 return genInlinedStringIsEmptyOrLength(cUnit, bb, mir, false /* isEmpty */);
899 }
900 if (tgtMethod.compare("boolean java.lang.String.isEmpty()") == 0) {
901 return genInlinedStringIsEmptyOrLength(cUnit, bb, mir, true /* isEmpty */);
902 }
903 if (tgtMethod.compare("int java.lang.Math.abs(int)") == 0) {
904 return genInlinedAbsInt(cUnit, bb, mir);
905 }
906 if (tgtMethod.compare("long java.lang.Math.abs(long)") == 0) {
907 return genInlinedAbsLong(cUnit, bb, mir);
908 }
909 if (tgtMethod.compare("int java.lang.Float.floatToRawIntBits(float)") == 0) {
910 return genInlinedFloatCvt(cUnit, bb, mir);
911 }
912 if (tgtMethod.compare("float java.lang.Float.intBitsToFloat(int)") == 0) {
913 return genInlinedFloatCvt(cUnit, bb, mir);
914 }
915 if (tgtMethod.compare("long java.lang.Double.doubleToRawLongBits(double)") == 0) {
916 return genInlinedDoubleCvt(cUnit, bb, mir);
917 }
918 if (tgtMethod.compare("double java.lang.Double.longBitsToDouble(long)") == 0) {
919 return genInlinedDoubleCvt(cUnit, bb, mir);
920 }
921 if (tgtMethod.compare("int java.lang.String.indexOf(int, int)") == 0) {
922 return genInlinedIndexOf(cUnit, bb, mir, type, false /* base 0 */);
923 }
924 if (tgtMethod.compare("int java.lang.String.indexOf(int)") == 0) {
925 return genInlinedIndexOf(cUnit, bb, mir, type, true /* base 0 */);
926 }
927 if (tgtMethod.compare("int java.lang.String.compareTo(java.lang.String)") == 0) {
928 return genInlinedStringCompareTo(cUnit, bb, mir, type);
929 }
930 return false;
931}
932
933
buzbee31a4a6f2012-02-28 15:36:15 -0800934} // namespace art