blob: 2da6242ab6e5c3b3f31bad1a1f47a8e49bdb46e5 [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{
buzbeeb046e162012-10-30 15:48:42 -0700143 if (cUnit->instructionSet != kThumb2) {
144 // Disable sharpening
145 directCode = 0;
146 directMethod = 0;
147 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 if (directCode != 0 && directMethod != 0) {
149 switch (state) {
150 case 0: // Get the current Method* [sets rARG0]
151 if (directCode != (uintptr_t)-1) {
152 loadConstant(cUnit, rINVOKE_TGT, directCode);
153 } else {
154 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
155 if (dataTarget == NULL) {
156 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
157 dataTarget->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700158 }
buzbeeb046e162012-10-30 15:48:42 -0700159 LIR* loadPcRel = opPcRelLoad(cUnit, rINVOKE_TGT, dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 oatAppendLIR(cUnit, loadPcRel);
buzbeeb046e162012-10-30 15:48:42 -0700161 DCHECK_EQ(cUnit->instructionSet, kThumb2) << (void*)dataTarget;
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 }
163 if (directMethod != (uintptr_t)-1) {
164 loadConstant(cUnit, rARG0, directMethod);
165 } else {
166 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
167 if (dataTarget == NULL) {
168 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
169 dataTarget->operands[1] = type;
170 }
buzbeeb046e162012-10-30 15:48:42 -0700171 LIR* loadPcRel = opPcRelLoad(cUnit, rARG0, dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 oatAppendLIR(cUnit, loadPcRel);
buzbeeb046e162012-10-30 15:48:42 -0700173 DCHECK_EQ(cUnit->instructionSet, kThumb2) << (void*)dataTarget;
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 }
175 break;
176 default:
177 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800178 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 } else {
180 switch (state) {
181 case 0: // Get the current Method* [sets rARG0]
Ian Rogers137e88f2012-10-08 17:46:47 -0700182 // TUNING: we can save a reg copy if Method* has been promoted.
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 loadCurrMethodDirect(cUnit, rARG0);
184 break;
185 case 1: // Get method->dex_cache_resolved_methods_
186 loadWordDisp(cUnit, rARG0,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700187 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 rARG0);
189 // Set up direct code if known.
190 if (directCode != 0) {
191 if (directCode != (uintptr_t)-1) {
192 loadConstant(cUnit, rINVOKE_TGT, directCode);
193 } else {
194 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
195 if (dataTarget == NULL) {
196 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
197 dataTarget->operands[1] = type;
198 }
buzbeeb046e162012-10-30 15:48:42 -0700199 LIR* loadPcRel = opPcRelLoad(cUnit, rINVOKE_TGT, dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 oatAppendLIR(cUnit, loadPcRel);
buzbeeb046e162012-10-30 15:48:42 -0700201 DCHECK_EQ(cUnit->instructionSet, kThumb2) << (void*)dataTarget;
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 }
203 }
204 break;
205 case 2: // Grab target method*
206 loadWordDisp(cUnit, rARG0,
207 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
208 rARG0);
209 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 case 3: // Grab the code from the method*
buzbeeb046e162012-10-30 15:48:42 -0700211 if (cUnit->instructionSet != kX86) {
212 if (directCode == 0) {
213 loadWordDisp(cUnit, rARG0, AbstractMethod::GetCodeOffset().Int32Value(),
214 rINVOKE_TGT);
215 }
216 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700217 }
buzbeeb046e162012-10-30 15:48:42 -0700218 // Intentional fallthrough for x86
Bill Buzbeea114add2012-05-03 15:00:40 -0700219 default:
220 return -1;
221 }
222 }
223 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800224}
225
226/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700227 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800228 * emit the next instruction in a virtual invoke sequence.
229 * We can use rLR as a temp prior to target address loading
230 * Note also that we'll load the first argument ("this") into
231 * rARG1 here rather than the standard loadArgRegs.
232 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700233int nextVCallInsn(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700234 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700235 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800236{
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 /*
238 * This is the fast path in which the target virtual method is
239 * fully resolved at compile time.
240 */
241 switch (state) {
Ian Rogers137e88f2012-10-08 17:46:47 -0700242 case 0: { // Get "this" [set rARG1]
243 RegLocation rlArg = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 loadValueDirectFixed(cUnit, rlArg, rARG1);
245 break;
Ian Rogers137e88f2012-10-08 17:46:47 -0700246 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 case 1: // Is "this" null? [use rARG1]
buzbee15bf9802012-06-12 17:49:27 -0700248 genNullCheck(cUnit, info->args[0].sRegLow, rARG1, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 // get this->klass_ [use rARG1, set rINVOKE_TGT]
250 loadWordDisp(cUnit, rARG1, Object::ClassOffset().Int32Value(),
251 rINVOKE_TGT);
252 break;
253 case 2: // Get this->klass_->vtable [usr rINVOKE_TGT, set rINVOKE_TGT]
254 loadWordDisp(cUnit, rINVOKE_TGT, Class::VTableOffset().Int32Value(),
255 rINVOKE_TGT);
256 break;
257 case 3: // Get target method [use rINVOKE_TGT, set rARG0]
258 loadWordDisp(cUnit, rINVOKE_TGT, (methodIdx * 4) +
259 Array::DataOffset(sizeof(Object*)).Int32Value(), rARG0);
260 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 case 4: // Get the compiled code address [uses rARG0, sets rINVOKE_TGT]
buzbeeb046e162012-10-30 15:48:42 -0700262 if (cUnit->instructionSet != kX86) {
263 loadWordDisp(cUnit, rARG0, AbstractMethod::GetCodeOffset().Int32Value(), rINVOKE_TGT);
264 break;
265 }
266 // Intentional fallthrough for X86
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 default:
268 return -1;
269 }
270 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800271}
272
Ian Rogers137e88f2012-10-08 17:46:47 -0700273/*
274 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
275 * which will locate the target and continue on via a tail call.
276 */
277int nextInterfaceCallInsn(CompilationUnit* cUnit, CallInfo* info, int state,
278 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
279 uintptr_t directMethod, InvokeType unused4)
280{
buzbeeb046e162012-10-30 15:48:42 -0700281 if (cUnit->instructionSet != kThumb2) {
282 // Disable sharpening
283 directMethod = 0;
284 }
285 int trampoline = (cUnit->instructionSet == kX86) ? 0
286 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
Ian Rogers137e88f2012-10-08 17:46:47 -0700287
288 if (directMethod != 0) {
289 switch (state) {
290 case 0: // Load the trampoline target [sets rINVOKE_TGT].
buzbeeb046e162012-10-30 15:48:42 -0700291 if (cUnit->instructionSet != kX86) {
292 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
293 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700294 // Get the interface Method* [sets rARG0]
295 if (directMethod != (uintptr_t)-1) {
296 loadConstant(cUnit, rARG0, directMethod);
297 } else {
298 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
299 if (dataTarget == NULL) {
300 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
301 dataTarget->operands[1] = kInterface;
302 }
buzbeeb046e162012-10-30 15:48:42 -0700303 LIR* loadPcRel = opPcRelLoad(cUnit, rARG0, dataTarget);
Ian Rogers137e88f2012-10-08 17:46:47 -0700304 oatAppendLIR(cUnit, loadPcRel);
buzbeeb046e162012-10-30 15:48:42 -0700305 DCHECK_EQ(cUnit->instructionSet, kThumb2) << (void*)dataTarget;
Ian Rogers137e88f2012-10-08 17:46:47 -0700306 }
307 break;
308 default:
309 return -1;
310 }
311 } else {
312 switch (state) {
313 case 0:
314 // Get the current Method* [sets rARG0] - TUNING: remove copy of method if it is promoted.
315 loadCurrMethodDirect(cUnit, rARG0);
316 // Load the trampoline target [sets rINVOKE_TGT].
buzbeeb046e162012-10-30 15:48:42 -0700317 if (cUnit->instructionSet != kX86) {
318 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
319 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700320 break;
321 case 1: // Get method->dex_cache_resolved_methods_ [set/use rARG0]
322 loadWordDisp(cUnit, rARG0,
323 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(),
324 rARG0);
325 break;
326 case 2: // Grab target method* [set/use rARG0]
327 loadWordDisp(cUnit, rARG0,
328 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
329 rARG0);
330 break;
331 default:
332 return -1;
333 }
334 }
335 return state + 1;
336}
337
buzbee3b3dbdd2012-06-13 13:39:34 -0700338int nextInvokeInsnSP(CompilationUnit* cUnit, CallInfo* info, int trampoline,
buzbee31a4a6f2012-02-28 15:36:15 -0800339 int state, uint32_t dexIdx, uint32_t methodIdx)
340{
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 /*
342 * This handles the case in which the base method is not fully
343 * resolved at compile time, we bail to a runtime helper.
344 */
345 if (state == 0) {
buzbeeb046e162012-10-30 15:48:42 -0700346 if (cUnit->instructionSet != kX86) {
347 // Load trampoline target
348 loadWordDisp(cUnit, rSELF, trampoline, rINVOKE_TGT);
349 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 // Load rARG0 with method index
351 loadConstant(cUnit, rARG0, dexIdx);
352 return 1;
353 }
354 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800355}
356
buzbee3b3dbdd2012-06-13 13:39:34 -0700357int nextStaticCallInsnSP(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700358 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700359 uintptr_t unused, uintptr_t unused2,
360 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800361{
Ian Rogers57b86d42012-03-27 16:05:41 -0700362 int trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700363 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800364}
365
buzbee3b3dbdd2012-06-13 13:39:34 -0700366int nextDirectCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700367 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700368 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800369{
Ian Rogers57b86d42012-03-27 16:05:41 -0700370 int trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700371 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800372}
373
buzbee3b3dbdd2012-06-13 13:39:34 -0700374int nextSuperCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700375 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700376 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800377{
Ian Rogers57b86d42012-03-27 16:05:41 -0700378 int trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700379 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800380}
381
buzbee3b3dbdd2012-06-13 13:39:34 -0700382int nextVCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700383 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700384 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800385{
Ian Rogers57b86d42012-03-27 16:05:41 -0700386 int trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700387 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800388}
389
buzbee15bf9802012-06-12 17:49:27 -0700390int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit,
buzbee3b3dbdd2012-06-13 13:39:34 -0700391 CallInfo* info, int state,
buzbee15bf9802012-06-12 17:49:27 -0700392 uint32_t dexIdx, uint32_t unused,
393 uintptr_t unused2, uintptr_t unused3,
394 InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800395{
Ian Rogers57b86d42012-03-27 16:05:41 -0700396 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700397 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800398}
399
buzbee3b3dbdd2012-06-13 13:39:34 -0700400int loadArgRegs(CompilationUnit* cUnit, CallInfo* info, int callState,
buzbee15bf9802012-06-12 17:49:27 -0700401 NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700402 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -0700403 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800404{
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 int lastArgReg = rARG3;
406 int nextReg = rARG1;
407 int nextArg = 0;
408 if (skipThis) {
409 nextReg++;
410 nextArg++;
411 }
buzbee15bf9802012-06-12 17:49:27 -0700412 for (; (nextReg <= lastArgReg) && (nextArg < info->numArgWords); nextReg++) {
413 RegLocation rlArg = info->args[nextArg++];
Bill Buzbeea114add2012-05-03 15:00:40 -0700414 rlArg = oatUpdateRawLoc(cUnit, rlArg);
415 if (rlArg.wide && (nextReg <= rARG2)) {
416 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
417 nextReg++;
418 nextArg++;
419 } else {
420 rlArg.wide = false;
421 loadValueDirectFixed(cUnit, rlArg, nextReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800422 }
buzbee15bf9802012-06-12 17:49:27 -0700423 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 directCode, directMethod, type);
425 }
426 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800427}
428
429/*
430 * Load up to 5 arguments, the first three of which will be in
431 * rARG1 .. rARG3. On entry rARG0 contains the current method pointer,
432 * and as part of the load sequence, it must be replaced with
433 * the target method pointer. Note, this may also be called
434 * for "range" variants if the number of arguments is 5 or fewer.
435 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700436int genDalvikArgsNoRange(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700437 int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800438 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700439 uint32_t dexIdx, uint32_t methodIdx,
440 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700441 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800442{
Bill Buzbeea114add2012-05-03 15:00:40 -0700443 RegLocation rlArg;
buzbee31a4a6f2012-02-28 15:36:15 -0800444
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 /* If no arguments, just return */
buzbee15bf9802012-06-12 17:49:27 -0700446 if (info->numArgWords == 0)
buzbee31a4a6f2012-02-28 15:36:15 -0800447 return callState;
Bill Buzbeea114add2012-05-03 15:00:40 -0700448
buzbee15bf9802012-06-12 17:49:27 -0700449 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 directCode, directMethod, type);
451
buzbee15bf9802012-06-12 17:49:27 -0700452 DCHECK_LE(info->numArgWords, 5);
453 if (info->numArgWords > 3) {
454 int32_t nextUse = 3;
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 //Detect special case of wide arg spanning arg3/arg4
buzbee15bf9802012-06-12 17:49:27 -0700456 RegLocation rlUse0 = info->args[0];
457 RegLocation rlUse1 = info->args[1];
458 RegLocation rlUse2 = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
460 rlUse2.wide) {
461 int reg = -1;
462 // Wide spans, we need the 2nd half of uses[2].
463 rlArg = oatUpdateLocWide(cUnit, rlUse2);
464 if (rlArg.location == kLocPhysReg) {
465 reg = rlArg.highReg;
466 } else {
467 // rARG2 & rARG3 can safely be used here
468 reg = rARG3;
469 loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbee15bf9802012-06-12 17:49:27 -0700470 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 methodIdx, directCode, directMethod, type);
472 }
473 storeBaseDisp(cUnit, rSP, (nextUse + 1) * 4, reg, kWord);
474 storeBaseDisp(cUnit, rSP, 16 /* (3+1)*4 */, reg, kWord);
buzbee15bf9802012-06-12 17:49:27 -0700475 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 directCode, directMethod, type);
477 nextUse++;
478 }
479 // Loop through the rest
buzbee15bf9802012-06-12 17:49:27 -0700480 while (nextUse < info->numArgWords) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 int lowReg;
482 int highReg = -1;
buzbee15bf9802012-06-12 17:49:27 -0700483 rlArg = info->args[nextUse];
Bill Buzbeea114add2012-05-03 15:00:40 -0700484 rlArg = oatUpdateRawLoc(cUnit, rlArg);
485 if (rlArg.location == kLocPhysReg) {
486 lowReg = rlArg.lowReg;
487 highReg = rlArg.highReg;
488 } else {
489 lowReg = rARG2;
490 if (rlArg.wide) {
491 highReg = rARG3;
492 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
493 } else {
494 loadValueDirectFixed(cUnit, rlArg, lowReg);
495 }
buzbee15bf9802012-06-12 17:49:27 -0700496 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700497 methodIdx, directCode, directMethod, type);
498 }
499 int outsOffset = (nextUse + 1) * 4;
500 if (rlArg.wide) {
501 storeBaseDispWide(cUnit, rSP, outsOffset, lowReg, highReg);
502 nextUse += 2;
503 } else {
504 storeWordDisp(cUnit, rSP, outsOffset, lowReg);
505 nextUse++;
506 }
buzbee15bf9802012-06-12 17:49:27 -0700507 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 directCode, directMethod, type);
509 }
510 }
511
buzbee15bf9802012-06-12 17:49:27 -0700512 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700513 dexIdx, methodIdx, directCode, directMethod,
514 type, skipThis);
515
516 if (pcrLabel) {
buzbee15bf9802012-06-12 17:49:27 -0700517 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, rARG1,
518 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700519 }
520 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800521}
522
523/*
524 * May have 0+ arguments (also used for jumbo). Note that
525 * source virtual registers may be in physical registers, so may
526 * need to be flushed to home location before copying. This
527 * applies to arg3 and above (see below).
528 *
529 * Two general strategies:
530 * If < 20 arguments
531 * Pass args 3-18 using vldm/vstm block copy
532 * Pass arg0, arg1 & arg2 in rARG1-rARG3
533 * If 20+ arguments
534 * Pass args arg19+ using memcpy block copy
535 * Pass arg0, arg1 & arg2 in rARG1-rARG3
536 *
537 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700538int genDalvikArgsRange(CompilationUnit* cUnit, CallInfo* info, int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800539 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700540 uint32_t dexIdx, uint32_t methodIdx,
541 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700542 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800543{
buzbee31a4a6f2012-02-28 15:36:15 -0800544
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 // If we can treat it as non-range (Jumbo ops will use range form)
buzbee15bf9802012-06-12 17:49:27 -0700546 if (info->numArgWords <= 5)
547 return genDalvikArgsNoRange(cUnit, info, callState, pcrLabel,
Bill Buzbeea114add2012-05-03 15:00:40 -0700548 nextCallInsn, dexIdx, methodIdx,
549 directCode, directMethod, type, skipThis);
550 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 * First load the non-register arguments. Both forms expect all
552 * of the source arguments to be in their home frame location, so
553 * scan the sReg names and flush any that have been promoted to
554 * frame backing storage.
555 */
556 // Scan the rest of the args - if in physReg flush to memory
buzbee15bf9802012-06-12 17:49:27 -0700557 for (int nextArg = 0; nextArg < info->numArgWords;) {
558 RegLocation loc = info->args[nextArg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 if (loc.wide) {
560 loc = oatUpdateLocWide(cUnit, loc);
561 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
562 storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
563 loc.lowReg, loc.highReg);
564 }
565 nextArg += 2;
566 } else {
567 loc = oatUpdateLoc(cUnit, loc);
568 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
569 storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, loc.sRegLow),
570 loc.lowReg, kWord);
571 }
572 nextArg++;
buzbee31a4a6f2012-02-28 15:36:15 -0800573 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 }
buzbee31a4a6f2012-02-28 15:36:15 -0800575
buzbee15bf9802012-06-12 17:49:27 -0700576 int startOffset = oatSRegOffset(cUnit, info->args[3].sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 int outsOffset = 4 /* Method* */ + (3 * 4);
buzbeeb046e162012-10-30 15:48:42 -0700578 if (cUnit->instructionSet != kThumb2) {
buzbee31a4a6f2012-02-28 15:36:15 -0800579 // Generate memcpy
580 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
581 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
Ian Rogers57b86d42012-03-27 16:05:41 -0700582 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
buzbee8320f382012-09-11 16:29:42 -0700583 rARG0, rARG1, (info->numArgWords - 3) * 4, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 } else {
buzbeeb046e162012-10-30 15:48:42 -0700585 if (info->numArgWords >= 20) {
586 // Generate memcpy
587 opRegRegImm(cUnit, kOpAdd, rARG0, rSP, outsOffset);
588 opRegRegImm(cUnit, kOpAdd, rARG1, rSP, startOffset);
589 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy),
590 rARG0, rARG1, (info->numArgWords - 3) * 4, false);
591 } else {
592 // Use vldm/vstm pair using rARG3 as a temp
593 int regsLeft = std::min(info->numArgWords - 3, 16);
594 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
595 directCode, directMethod, type);
596 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, startOffset);
597 LIR* ld = opVldm(cUnit, rARG3, regsLeft);
598 //TUNING: loosen barrier
599 ld->defMask = ENCODE_ALL;
600 setMemRefType(ld, true /* isLoad */, kDalvikReg);
601 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
602 directCode, directMethod, type);
603 opRegRegImm(cUnit, kOpAdd, rARG3, rSP, 4 /* Method* */ + (3 * 4));
604 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
605 directCode, directMethod, type);
606 LIR* st = opVstm(cUnit, rARG3, regsLeft);
607 setMemRefType(st, false /* isLoad */, kDalvikReg);
608 st->defMask = ENCODE_ALL;
609 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
610 directCode, directMethod, type);
611 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700613
buzbee15bf9802012-06-12 17:49:27 -0700614 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700615 dexIdx, methodIdx, directCode, directMethod,
616 type, skipThis);
617
buzbee15bf9802012-06-12 17:49:27 -0700618 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 directCode, directMethod, type);
620 if (pcrLabel) {
buzbee15bf9802012-06-12 17:49:27 -0700621 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, rARG1,
622 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 }
624 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800625}
626
buzbee3b3dbdd2012-06-13 13:39:34 -0700627RegLocation inlineTarget(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700628{
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700630 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 res = oatGetReturn(cUnit, false);
632 } else {
buzbee15bf9802012-06-12 17:49:27 -0700633 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700634 }
635 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700636}
637
buzbee3b3dbdd2012-06-13 13:39:34 -0700638RegLocation inlineTargetWide(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700639{
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700641 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 res = oatGetReturnWide(cUnit, false);
643 } else {
buzbee15bf9802012-06-12 17:49:27 -0700644 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 }
646 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700647}
648
buzbee3b3dbdd2012-06-13 13:39:34 -0700649bool genInlinedCharAt(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700650{
buzbeeb046e162012-10-30 15:48:42 -0700651 if (cUnit->instructionSet == kMips) {
652 // TODO - add Mips implementation
653 return false;
654 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 // Location of reference to data array
656 int valueOffset = String::ValueOffset().Int32Value();
657 // Location of count
658 int countOffset = String::CountOffset().Int32Value();
659 // Starting offset within data array
660 int offsetOffset = String::OffsetOffset().Int32Value();
661 // Start of char data with array_
662 int dataOffset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
buzbeefc9e6fa2012-03-23 15:14:29 -0700663
buzbee15bf9802012-06-12 17:49:27 -0700664 RegLocation rlObj = info->args[0];
665 RegLocation rlIdx = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 rlObj = loadValue(cUnit, rlObj, kCoreReg);
667 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
668 int regMax;
buzbee15bf9802012-06-12 17:49:27 -0700669 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
670 bool rangeCheck = (!(info->optFlags & MIR_IGNORE_RANGE_CHECK));
jeffhao634ea282012-08-10 13:04:01 -0700671 LIR* launchPad = NULL;
buzbeeb046e162012-10-30 15:48:42 -0700672 int regOff = INVALID_REG;
673 int regPtr = INVALID_REG;
674 if (cUnit->instructionSet != kX86) {
675 regOff = oatAllocTemp(cUnit);
676 regPtr = oatAllocTemp(cUnit);
677 if (rangeCheck) {
678 regMax = oatAllocTemp(cUnit);
679 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
680 }
681 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
682 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
683 if (rangeCheck) {
684 // Set up a launch pad to allow retry in case of bounds violation */
685 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
686 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
687 (intptr_t)launchPad);
688 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
689 oatFreeTemp(cUnit, regMax);
690 opCondBranch(cUnit, kCondCs, launchPad);
691 }
692 } else {
693 if (rangeCheck) {
694 regMax = oatAllocTemp(cUnit);
695 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
696 // Set up a launch pad to allow retry in case of bounds violation */
697 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
698 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
699 (intptr_t)launchPad);
700 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
701 oatFreeTemp(cUnit, regMax);
702 opCondBranch(cUnit, kCondCc, launchPad);
703 }
704 regOff = oatAllocTemp(cUnit);
705 regPtr = oatAllocTemp(cUnit);
706 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
707 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700708 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
710 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
jeffhao634ea282012-08-10 13:04:01 -0700711 oatFreeTemp(cUnit, rlObj.lowReg);
712 oatFreeTemp(cUnit, rlIdx.lowReg);
buzbee15bf9802012-06-12 17:49:27 -0700713 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700714 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
715 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
716 oatFreeTemp(cUnit, regOff);
717 oatFreeTemp(cUnit, regPtr);
718 storeValue(cUnit, rlDest, rlResult);
719 if (rangeCheck) {
Elliott Hughes60234562012-06-01 12:25:59 -0700720 launchPad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 }
722 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700723 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700725}
726
727// Generates an inlined String.isEmpty or String.length.
buzbee3b3dbdd2012-06-13 13:39:34 -0700728bool genInlinedStringIsEmptyOrLength(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700729 bool isEmpty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700730{
buzbeeb046e162012-10-30 15:48:42 -0700731 if (cUnit->instructionSet == kMips) {
732 // TODO - add Mips implementation
733 return false;
734 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700735 // dst = src.length();
buzbee15bf9802012-06-12 17:49:27 -0700736 RegLocation rlObj = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700737 rlObj = loadValue(cUnit, rlObj, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700738 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700739 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee15bf9802012-06-12 17:49:27 -0700740 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700741 loadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
742 rlResult.lowReg);
743 if (isEmpty) {
744 // dst = (dst == 0);
buzbeeb046e162012-10-30 15:48:42 -0700745 if (cUnit->instructionSet == kThumb2) {
746 int tReg = oatAllocTemp(cUnit);
747 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
748 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
749 } else {
750 DCHECK_EQ(cUnit->instructionSet, kX86);
751 opRegImm(cUnit, kOpSub, rlResult.lowReg, 1);
752 opRegImm(cUnit, kOpLsr, rlResult.lowReg, 31);
753 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700754 }
755 storeValue(cUnit, rlDest, rlResult);
756 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700757}
758
buzbee3b3dbdd2012-06-13 13:39:34 -0700759bool genInlinedAbsInt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700760{
buzbeeb046e162012-10-30 15:48:42 -0700761 if (cUnit->instructionSet == kMips) {
762 // TODO - add Mips implementation
763 return false;
764 }
buzbee15bf9802012-06-12 17:49:27 -0700765 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700766 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700767 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
769 int signReg = oatAllocTemp(cUnit);
770 // abs(x) = y<=x>>31, (x+y)^y.
771 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
772 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
773 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
774 storeValue(cUnit, rlDest, rlResult);
775 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700776}
777
buzbee3b3dbdd2012-06-13 13:39:34 -0700778bool genInlinedAbsLong(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700779{
buzbeeb046e162012-10-30 15:48:42 -0700780 if (cUnit->instructionSet == kMips) {
781 // TODO - add Mips implementation
782 return false;
783 }
784 if (cUnit->instructionSet == kThumb2) {
785 RegLocation rlSrc = info->args[0];
786 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
787 RegLocation rlDest = inlineTargetWide(cUnit, info);
788 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
789 int signReg = oatAllocTemp(cUnit);
790 // abs(x) = y<=x>>31, (x+y)^y.
791 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
792 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
793 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
794 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
795 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
796 storeValueWide(cUnit, rlDest, rlResult);
797 return true;
798 } else {
799 DCHECK_EQ(cUnit->instructionSet, kX86);
800 // Reuse source registers to avoid running out of temps
801 RegLocation rlSrc = info->args[0];
802 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
803 RegLocation rlDest = inlineTargetWide(cUnit, info);
804 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
805 opRegCopyWide(cUnit, rlResult.lowReg, rlResult.highReg, rlSrc.lowReg, rlSrc.highReg);
806 oatFreeTemp(cUnit, rlSrc.lowReg);
807 oatFreeTemp(cUnit, rlSrc.highReg);
808 int signReg = oatAllocTemp(cUnit);
809 // abs(x) = y<=x>>31, (x+y)^y.
810 opRegRegImm(cUnit, kOpAsr, signReg, rlResult.highReg, 31);
811 opRegReg(cUnit, kOpAdd, rlResult.lowReg, signReg);
812 opRegReg(cUnit, kOpAdc, rlResult.highReg, signReg);
813 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
814 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
815 storeValueWide(cUnit, rlDest, rlResult);
816 return true;
817 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700818}
819
buzbee3b3dbdd2012-06-13 13:39:34 -0700820bool genInlinedFloatCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700821{
buzbeeb046e162012-10-30 15:48:42 -0700822 if (cUnit->instructionSet == kMips) {
823 // TODO - add Mips implementation
824 return false;
825 }
buzbee15bf9802012-06-12 17:49:27 -0700826 RegLocation rlSrc = info->args[0];
827 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700828 storeValue(cUnit, rlDest, rlSrc);
829 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700830}
831
buzbee3b3dbdd2012-06-13 13:39:34 -0700832bool genInlinedDoubleCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700833{
buzbeeb046e162012-10-30 15:48:42 -0700834 if (cUnit->instructionSet == kMips) {
835 // TODO - add Mips implementation
836 return false;
837 }
buzbee15bf9802012-06-12 17:49:27 -0700838 RegLocation rlSrc = info->args[0];
839 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700840 storeValueWide(cUnit, rlDest, rlSrc);
841 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700842}
843
844/*
845 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
846 * otherwise bails to standard library code.
847 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700848bool genInlinedIndexOf(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700849 bool zeroBased)
buzbeefc9e6fa2012-03-23 15:14:29 -0700850{
buzbeeb046e162012-10-30 15:48:42 -0700851 if (cUnit->instructionSet == kMips) {
852 // TODO - add Mips implementation
853 return false;
854 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700855 oatClobberCalleeSave(cUnit);
856 oatLockCallTemps(cUnit); // Using fixed registers
857 int regPtr = rARG0;
858 int regChar = rARG1;
859 int regStart = rARG2;
buzbeefc9e6fa2012-03-23 15:14:29 -0700860
buzbee15bf9802012-06-12 17:49:27 -0700861 RegLocation rlObj = info->args[0];
862 RegLocation rlChar = info->args[1];
863 RegLocation rlStart = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700864 loadValueDirectFixed(cUnit, rlObj, regPtr);
865 loadValueDirectFixed(cUnit, rlChar, regChar);
866 if (zeroBased) {
867 loadConstant(cUnit, regStart, 0);
868 } else {
869 loadValueDirectFixed(cUnit, rlStart, regStart);
870 }
buzbeeb046e162012-10-30 15:48:42 -0700871 int rTgt = (cUnit->instructionSet != kX86) ? loadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf)) : 0;
buzbee15bf9802012-06-12 17:49:27 -0700872 genNullCheck(cUnit, rlObj.sRegLow, regPtr, info->optFlags);
873 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700874 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
875 (intptr_t)launchPad);
876 opCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
buzbee8320f382012-09-11 16:29:42 -0700877 // NOTE: not a safepoint
buzbeeb046e162012-10-30 15:48:42 -0700878 if (cUnit->instructionSet != kX86) {
879 opReg(cUnit, kOpBlx, rTgt);
880 } else {
881 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
882 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700883 LIR* resumeTgt = newLIR0(cUnit, kPseudoTargetLabel);
884 launchPad->operands[2] = (uintptr_t)resumeTgt;
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700886 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
887 RegLocation rlReturn = oatGetReturn(cUnit, false);
888 RegLocation rlDest = inlineTarget(cUnit, info);
889 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700890 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700891}
892
893/* Fast string.compareTo(Ljava/lang/string;)I. */
buzbee3b3dbdd2012-06-13 13:39:34 -0700894bool genInlinedStringCompareTo(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700895{
buzbeeb046e162012-10-30 15:48:42 -0700896 if (cUnit->instructionSet == kMips) {
897 // TODO - add Mips implementation
898 return false;
899 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 oatClobberCalleeSave(cUnit);
901 oatLockCallTemps(cUnit); // Using fixed registers
902 int regThis = rARG0;
903 int regCmp = rARG1;
buzbeefc9e6fa2012-03-23 15:14:29 -0700904
buzbee15bf9802012-06-12 17:49:27 -0700905 RegLocation rlThis = info->args[0];
906 RegLocation rlCmp = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700907 loadValueDirectFixed(cUnit, rlThis, regThis);
908 loadValueDirectFixed(cUnit, rlCmp, regCmp);
buzbeeb046e162012-10-30 15:48:42 -0700909 int rTgt = (cUnit->instructionSet != kX86) ?
910 loadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo)) : 0;
buzbee15bf9802012-06-12 17:49:27 -0700911 genNullCheck(cUnit, rlThis.sRegLow, regThis, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700912 //TUNING: check if rlCmp.sRegLow is already null checked
buzbee15bf9802012-06-12 17:49:27 -0700913 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, (uintptr_t)info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700914 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
Elliott Hughes60234562012-06-01 12:25:59 -0700915 (intptr_t)launchPad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700916 opCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
buzbee8320f382012-09-11 16:29:42 -0700917 // NOTE: not a safepoint
buzbeeb046e162012-10-30 15:48:42 -0700918 if (cUnit->instructionSet != kX86) {
919 opReg(cUnit, kOpBlx, rTgt);
920 } else {
921 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
922 }
Elliott Hughes60234562012-06-01 12:25:59 -0700923 launchPad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -0700924 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700925 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
926 RegLocation rlReturn = oatGetReturn(cUnit, false);
927 RegLocation rlDest = inlineTarget(cUnit, info);
928 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700929 return true;
Ian Rogers0183dd72012-09-17 23:06:51 -0700930}
931
buzbee3b3dbdd2012-06-13 13:39:34 -0700932bool genIntrinsic(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700933{
Ian Rogerse13eafa2012-09-07 11:24:27 -0700934 if (info->optFlags & MIR_INLINED) {
buzbeefc9e6fa2012-03-23 15:14:29 -0700935 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700936 }
937 /*
938 * TODO: move these to a target-specific structured constant array
939 * and use a generic match function. The list of intrinsics may be
940 * slightly different depending on target.
941 * TODO: Fold this into a matching function that runs during
942 * basic block building. This should be part of the action for
943 * small method inlining and recognition of the special object init
944 * method. By doing this during basic block construction, we can also
945 * take advantage of/generate new useful dataflow info.
946 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700947 std::string tgtMethod(PrettyMethod(info->index, *cUnit->dex_file));
Ian Rogers0183dd72012-09-17 23:06:51 -0700948 if (tgtMethod.find(" java.lang") != std::string::npos) {
949 if (tgtMethod == "long java.lang.Double.doubleToRawLongBits(double)") {
950 return genInlinedDoubleCvt(cUnit, info);
951 }
952 if (tgtMethod == "double java.lang.Double.longBitsToDouble(long)") {
953 return genInlinedDoubleCvt(cUnit, info);
954 }
955 if (tgtMethod == "int java.lang.Float.floatToRawIntBits(float)") {
956 return genInlinedFloatCvt(cUnit, info);
957 }
958 if (tgtMethod == "float java.lang.Float.intBitsToFloat(int)") {
959 return genInlinedFloatCvt(cUnit, info);
960 }
961 if (tgtMethod == "int java.lang.Math.abs(int)" ||
962 tgtMethod == "int java.lang.StrictMath.abs(int)") {
963 return genInlinedAbsInt(cUnit, info);
964 }
965 if (tgtMethod == "long java.lang.Math.abs(long)" ||
966 tgtMethod == "long java.lang.StrictMath.abs(long)") {
967 return genInlinedAbsLong(cUnit, info);
968 }
969 if (tgtMethod == "int java.lang.Math.max(int, int)" ||
970 tgtMethod == "int java.lang.StrictMath.max(int, int)") {
971 return genInlinedMinMaxInt(cUnit, info, false /* isMin */);
972 }
973 if (tgtMethod == "int java.lang.Math.min(int, int)" ||
974 tgtMethod == "int java.lang.StrictMath.min(int, int)") {
975 return genInlinedMinMaxInt(cUnit, info, true /* isMin */);
976 }
977 if (tgtMethod == "double java.lang.Math.sqrt(double)" ||
978 tgtMethod == "double java.lang.StrictMath.sqrt(double)") {
979 return genInlinedSqrt(cUnit, info);
980 }
981 if (tgtMethod == "char java.lang.String.charAt(int)") {
982 return genInlinedCharAt(cUnit, info);
983 }
984 if (tgtMethod == "int java.lang.String.compareTo(java.lang.String)") {
985 return genInlinedStringCompareTo(cUnit, info);
986 }
987 if (tgtMethod == "boolean java.lang.String.isEmpty()") {
988 return genInlinedStringIsEmptyOrLength(cUnit, info, true /* isEmpty */);
989 }
990 if (tgtMethod == "int java.lang.String.indexOf(int, int)") {
991 return genInlinedIndexOf(cUnit, info, false /* base 0 */);
992 }
993 if (tgtMethod == "int java.lang.String.indexOf(int)") {
994 return genInlinedIndexOf(cUnit, info, true /* base 0 */);
995 }
996 if (tgtMethod == "int java.lang.String.length()") {
997 return genInlinedStringIsEmptyOrLength(cUnit, info, false /* isEmpty */);
998 }
999 } else if (tgtMethod.find("boolean sun.misc.Unsafe.compareAndSwap") != std::string::npos) {
1000 if (tgtMethod == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
1001 return genInlinedCas32(cUnit, info, false);
1002 }
1003 if (tgtMethod == "boolean sun.misc.Unsafe.compareAndSwapObject(java.lang.Object, long, java.lang.Object, java.lang.Object)") {
1004 return genInlinedCas32(cUnit, info, true);
1005 }
Ian Rogerse13eafa2012-09-07 11:24:27 -07001006 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001007 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -07001008}
1009
1010
buzbee31a4a6f2012-02-28 15:36:15 -08001011} // namespace art