blob: 90fc14b053697a8291cb9e6e8fe4a805b19f54b3 [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"
buzbee1bc37c62012-11-20 13:35:41 -080018#include "../compiler_ir.h"
19#include "ralloc_util.h"
20#include "codegen_util.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070021
buzbee31a4a6f2012-02-28 15:36:15 -080022namespace art {
23
24/*
25 * This source files contains "gen" codegen routines that should
26 * be applicable to most targets. Only mid-level support utilities
27 * and "op" calls may be used here.
28 */
29
buzbee31a4a6f2012-02-28 15:36:15 -080030/*
31 * If there are any ins passed in registers that have not been promoted
32 * to a callee-save register, flush them to the frame. Perform intial
33 * assignment of promoted arguments.
buzbeead8f15e2012-06-18 14:49:45 -070034 *
35 * argLocs is an array of location records describing the incoming arguments
36 * with one location record per word of argument.
buzbee31a4a6f2012-02-28 15:36:15 -080037 */
buzbeead8f15e2012-06-18 14:49:45 -070038void flushIns(CompilationUnit* cUnit, RegLocation* argLocs, RegLocation rlMethod)
buzbee31a4a6f2012-02-28 15:36:15 -080039{
Bill Buzbeea114add2012-05-03 15:00:40 -070040 /*
41 * Dummy up a RegLocation for the incoming Method*
buzbeef0504cd2012-11-13 16:31:10 -080042 * It will attempt to keep kArg0 live (or copy it to home location
Bill Buzbeea114add2012-05-03 15:00:40 -070043 * if promoted).
44 */
buzbeead8f15e2012-06-18 14:49:45 -070045 RegLocation rlSrc = rlMethod;
Bill Buzbeea114add2012-05-03 15:00:40 -070046 rlSrc.location = kLocPhysReg;
buzbeef0504cd2012-11-13 16:31:10 -080047 rlSrc.lowReg = targetReg(kArg0);
Bill Buzbeea114add2012-05-03 15:00:40 -070048 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) {
buzbeef0504cd2012-11-13 16:31:10 -080053 storeWordDisp(cUnit, targetReg(kSp), 0, targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -070054 }
buzbee9c044ce2012-03-18 13:24:07 -070055
Bill Buzbeea114add2012-05-03 15:00:40 -070056 if (cUnit->numIns == 0)
57 return;
58 const int numArgRegs = 3;
buzbeef0504cd2012-11-13 16:31:10 -080059 static SpecialTargetRegister argRegs[] = {kArg1, kArg2, kArg3};
Bill Buzbeea114add2012-05-03 15:00:40 -070060 int startVReg = cUnit->numDalvikRegisters - cUnit->numIns;
61 /*
62 * Copy incoming arguments to their proper home locations.
63 * NOTE: an older version of dx had an issue in which
64 * it would reuse static method argument registers.
65 * This could result in the same Dalvik virtual register
66 * being promoted to both core and fp regs. To account for this,
67 * we only copy to the corresponding promoted physical register
68 * if it matches the type of the SSA name for the incoming
69 * argument. It is also possible that long and double arguments
70 * end up half-promoted. In those cases, we must flush the promoted
71 * half to memory as well.
72 */
73 for (int i = 0; i < cUnit->numIns; i++) {
74 PromotionMap* vMap = &cUnit->promotionMap[startVReg + i];
75 if (i < numArgRegs) {
76 // If arriving in register
77 bool needFlush = true;
buzbeead8f15e2012-06-18 14:49:45 -070078 RegLocation* tLoc = &argLocs[i];
Bill Buzbeea114add2012-05-03 15:00:40 -070079 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
buzbeef0504cd2012-11-13 16:31:10 -080080 opRegCopy(cUnit, vMap->coreReg, targetReg(argRegs[i]));
Bill Buzbeea114add2012-05-03 15:00:40 -070081 needFlush = false;
82 } else if ((vMap->fpLocation == kLocPhysReg) && tLoc->fp) {
buzbeef0504cd2012-11-13 16:31:10 -080083 opRegCopy(cUnit, vMap->fpReg, targetReg(argRegs[i]));
Bill Buzbeea114add2012-05-03 15:00:40 -070084 needFlush = false;
85 } else {
86 needFlush = true;
87 }
buzbee86a4bce2012-03-06 18:15:00 -080088
Bill Buzbeea114add2012-05-03 15:00:40 -070089 // For wide args, force flush if only half is promoted
90 if (tLoc->wide) {
91 PromotionMap* pMap = vMap + (tLoc->highWord ? -1 : +1);
92 needFlush |= (pMap->coreLocation != vMap->coreLocation) ||
93 (pMap->fpLocation != vMap->fpLocation);
94 }
95 if (needFlush) {
buzbeef0504cd2012-11-13 16:31:10 -080096 storeBaseDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, startVReg + i),
97 targetReg(argRegs[i]), kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -070098 }
99 } else {
100 // If arriving in frame & promoted
101 if (vMap->coreLocation == kLocPhysReg) {
buzbeef0504cd2012-11-13 16:31:10 -0800102 loadWordDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, startVReg + i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 vMap->coreReg);
104 }
105 if (vMap->fpLocation == kLocPhysReg) {
buzbeef0504cd2012-11-13 16:31:10 -0800106 loadWordDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, startVReg + i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 vMap->fpReg);
108 }
buzbee31a4a6f2012-02-28 15:36:15 -0800109 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 }
buzbee31a4a6f2012-02-28 15:36:15 -0800111}
112
buzbeeeaf09bc2012-11-15 14:51:41 -0800113void scanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget,
114 const DexFile* dexFile, uint32_t dexMethodIdx)
Ian Rogers3fa13792012-03-18 15:53:45 -0700115{
Bill Buzbeea114add2012-05-03 15:00:40 -0700116 LIR* curTarget = cUnit->methodLiteralList;
117 LIR* nextTarget = curTarget != NULL ? curTarget->next : NULL;
118 while (curTarget != NULL && nextTarget != NULL) {
buzbeecbd6d442012-11-17 14:11:25 -0800119 if (curTarget->operands[0] == reinterpret_cast<intptr_t>(dexFile) &&
120 nextTarget->operands[0] == static_cast<int>(dexMethodIdx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700121 *codeTarget = curTarget;
122 *methodTarget = nextTarget;
123 DCHECK((*codeTarget)->next == *methodTarget);
buzbeecbd6d442012-11-17 14:11:25 -0800124 DCHECK_EQ((*codeTarget)->operands[0], reinterpret_cast<intptr_t>(dexFile));
125 DCHECK_EQ((*methodTarget)->operands[0], static_cast<int>(dexMethodIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 break;
Ian Rogers3fa13792012-03-18 15:53:45 -0700127 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 curTarget = nextTarget->next;
129 nextTarget = curTarget != NULL ? curTarget->next : NULL;
130 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700131}
132
buzbee31a4a6f2012-02-28 15:36:15 -0800133/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700134 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800135 * emit the next instruction in static & direct invoke sequences.
136 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700137int nextSDCallInsn(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700138 int state, uint32_t dexIdx, uint32_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700139 uintptr_t directCode, uintptr_t directMethod,
140 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800141{
buzbeeb046e162012-10-30 15:48:42 -0700142 if (cUnit->instructionSet != kThumb2) {
143 // Disable sharpening
144 directCode = 0;
145 directMethod = 0;
146 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 if (directCode != 0 && directMethod != 0) {
148 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800149 case 0: // Get the current Method* [sets kArg0]
buzbeecbd6d442012-11-17 14:11:25 -0800150 if (directCode != static_cast<unsigned int>(-1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800151 loadConstant(cUnit, targetReg(kInvokeTgt), directCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 } else {
153 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
154 if (dataTarget == NULL) {
155 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
156 dataTarget->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700157 }
buzbeef0504cd2012-11-13 16:31:10 -0800158 LIR* loadPcRel = opPcRelLoad(cUnit, targetReg(kInvokeTgt), dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 oatAppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800160 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 }
buzbeecbd6d442012-11-17 14:11:25 -0800162 if (directMethod != static_cast<unsigned int>(-1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800163 loadConstant(cUnit, targetReg(kArg0), directMethod);
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 } else {
165 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
166 if (dataTarget == NULL) {
167 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
168 dataTarget->operands[1] = type;
169 }
buzbeef0504cd2012-11-13 16:31:10 -0800170 LIR* loadPcRel = opPcRelLoad(cUnit, targetReg(kArg0), dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 oatAppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800172 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 }
174 break;
175 default:
176 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800177 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 } else {
179 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800180 case 0: // Get the current Method* [sets kArg0]
Ian Rogers137e88f2012-10-08 17:46:47 -0700181 // TUNING: we can save a reg copy if Method* has been promoted.
buzbeef0504cd2012-11-13 16:31:10 -0800182 loadCurrMethodDirect(cUnit, targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 break;
184 case 1: // Get method->dex_cache_resolved_methods_
buzbeef0504cd2012-11-13 16:31:10 -0800185 loadWordDisp(cUnit, targetReg(kArg0),
186 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(), targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 // Set up direct code if known.
188 if (directCode != 0) {
buzbeecbd6d442012-11-17 14:11:25 -0800189 if (directCode != static_cast<unsigned int>(-1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800190 loadConstant(cUnit, targetReg(kInvokeTgt), directCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 } else {
192 LIR* dataTarget = scanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
193 if (dataTarget == NULL) {
194 dataTarget = addWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
195 dataTarget->operands[1] = type;
196 }
buzbeef0504cd2012-11-13 16:31:10 -0800197 LIR* loadPcRel = opPcRelLoad(cUnit, targetReg(kInvokeTgt), dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 oatAppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800199 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 }
201 }
202 break;
203 case 2: // Grab target method*
buzbeef0504cd2012-11-13 16:31:10 -0800204 loadWordDisp(cUnit, targetReg(kArg0),
205 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4, targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 case 3: // Grab the code from the method*
buzbeeb046e162012-10-30 15:48:42 -0700208 if (cUnit->instructionSet != kX86) {
209 if (directCode == 0) {
buzbeef0504cd2012-11-13 16:31:10 -0800210 loadWordDisp(cUnit, targetReg(kArg0), AbstractMethod::GetCodeOffset().Int32Value(),
211 targetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700212 }
213 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 }
buzbeeb046e162012-10-30 15:48:42 -0700215 // Intentional fallthrough for x86
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 default:
217 return -1;
218 }
219 }
220 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800221}
222
223/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700224 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800225 * emit the next instruction in a virtual invoke sequence.
buzbeef0504cd2012-11-13 16:31:10 -0800226 * We can use kLr as a temp prior to target address loading
buzbee31a4a6f2012-02-28 15:36:15 -0800227 * Note also that we'll load the first argument ("this") into
buzbeef0504cd2012-11-13 16:31:10 -0800228 * kArg1 here rather than the standard loadArgRegs.
buzbee31a4a6f2012-02-28 15:36:15 -0800229 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700230int nextVCallInsn(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700231 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700232 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800233{
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 /*
235 * This is the fast path in which the target virtual method is
236 * fully resolved at compile time.
237 */
238 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800239 case 0: { // Get "this" [set kArg1]
Ian Rogers137e88f2012-10-08 17:46:47 -0700240 RegLocation rlArg = info->args[0];
buzbeef0504cd2012-11-13 16:31:10 -0800241 loadValueDirectFixed(cUnit, rlArg, targetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 break;
Ian Rogers137e88f2012-10-08 17:46:47 -0700243 }
buzbeef0504cd2012-11-13 16:31:10 -0800244 case 1: // Is "this" null? [use kArg1]
245 genNullCheck(cUnit, info->args[0].sRegLow, targetReg(kArg1), info->optFlags);
246 // get this->klass_ [use kArg1, set kInvokeTgt]
247 loadWordDisp(cUnit, targetReg(kArg1), Object::ClassOffset().Int32Value(),
248 targetReg(kInvokeTgt));
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 break;
buzbeef0504cd2012-11-13 16:31:10 -0800250 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
251 loadWordDisp(cUnit, targetReg(kInvokeTgt), Class::VTableOffset().Int32Value(),
252 targetReg(kInvokeTgt));
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 break;
buzbeef0504cd2012-11-13 16:31:10 -0800254 case 3: // Get target method [use kInvokeTgt, set kArg0]
255 loadWordDisp(cUnit, targetReg(kInvokeTgt), (methodIdx * 4) +
256 Array::DataOffset(sizeof(Object*)).Int32Value(), targetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 break;
buzbeef0504cd2012-11-13 16:31:10 -0800258 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
buzbeeb046e162012-10-30 15:48:42 -0700259 if (cUnit->instructionSet != kX86) {
buzbeef0504cd2012-11-13 16:31:10 -0800260 loadWordDisp(cUnit, targetReg(kArg0), AbstractMethod::GetCodeOffset().Int32Value(),
261 targetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700262 break;
263 }
264 // Intentional fallthrough for X86
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 default:
266 return -1;
267 }
268 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800269}
270
Ian Rogers137e88f2012-10-08 17:46:47 -0700271/*
272 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
273 * which will locate the target and continue on via a tail call.
274 */
275int nextInterfaceCallInsn(CompilationUnit* cUnit, CallInfo* info, int state,
276 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
277 uintptr_t directMethod, InvokeType unused4)
278{
buzbeeb046e162012-10-30 15:48:42 -0700279 if (cUnit->instructionSet != kThumb2) {
280 // Disable sharpening
281 directMethod = 0;
282 }
283 int trampoline = (cUnit->instructionSet == kX86) ? 0
284 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
Ian Rogers137e88f2012-10-08 17:46:47 -0700285
286 if (directMethod != 0) {
287 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800288 case 0: // Load the trampoline target [sets kInvokeTgt].
buzbeeb046e162012-10-30 15:48:42 -0700289 if (cUnit->instructionSet != kX86) {
buzbeef0504cd2012-11-13 16:31:10 -0800290 loadWordDisp(cUnit, targetReg(kSelf), trampoline, targetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700291 }
buzbeef0504cd2012-11-13 16:31:10 -0800292 // Get the interface Method* [sets kArg0]
buzbeecbd6d442012-11-17 14:11:25 -0800293 if (directMethod != static_cast<unsigned int>(-1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800294 loadConstant(cUnit, targetReg(kArg0), directMethod);
Ian Rogers137e88f2012-10-08 17:46:47 -0700295 } else {
296 LIR* dataTarget = scanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
297 if (dataTarget == NULL) {
298 dataTarget = addWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
299 dataTarget->operands[1] = kInterface;
300 }
buzbeef0504cd2012-11-13 16:31:10 -0800301 LIR* loadPcRel = opPcRelLoad(cUnit, targetReg(kArg0), dataTarget);
Ian Rogers137e88f2012-10-08 17:46:47 -0700302 oatAppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800303 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Ian Rogers137e88f2012-10-08 17:46:47 -0700304 }
305 break;
306 default:
307 return -1;
308 }
309 } else {
310 switch (state) {
311 case 0:
buzbeef0504cd2012-11-13 16:31:10 -0800312 // Get the current Method* [sets kArg0] - TUNING: remove copy of method if it is promoted.
313 loadCurrMethodDirect(cUnit, targetReg(kArg0));
314 // Load the trampoline target [sets kInvokeTgt].
buzbeeb046e162012-10-30 15:48:42 -0700315 if (cUnit->instructionSet != kX86) {
buzbeef0504cd2012-11-13 16:31:10 -0800316 loadWordDisp(cUnit, targetReg(kSelf), trampoline, targetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700317 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700318 break;
buzbeef0504cd2012-11-13 16:31:10 -0800319 case 1: // Get method->dex_cache_resolved_methods_ [set/use kArg0]
320 loadWordDisp(cUnit, targetReg(kArg0),
Ian Rogers137e88f2012-10-08 17:46:47 -0700321 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(),
buzbeef0504cd2012-11-13 16:31:10 -0800322 targetReg(kArg0));
Ian Rogers137e88f2012-10-08 17:46:47 -0700323 break;
buzbeef0504cd2012-11-13 16:31:10 -0800324 case 2: // Grab target method* [set/use kArg0]
325 loadWordDisp(cUnit, targetReg(kArg0),
Ian Rogers137e88f2012-10-08 17:46:47 -0700326 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
buzbeef0504cd2012-11-13 16:31:10 -0800327 targetReg(kArg0));
Ian Rogers137e88f2012-10-08 17:46:47 -0700328 break;
329 default:
330 return -1;
331 }
332 }
333 return state + 1;
334}
335
buzbee3b3dbdd2012-06-13 13:39:34 -0700336int nextInvokeInsnSP(CompilationUnit* cUnit, CallInfo* info, int trampoline,
buzbee31a4a6f2012-02-28 15:36:15 -0800337 int state, uint32_t dexIdx, uint32_t methodIdx)
338{
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 /*
340 * This handles the case in which the base method is not fully
341 * resolved at compile time, we bail to a runtime helper.
342 */
343 if (state == 0) {
buzbeeb046e162012-10-30 15:48:42 -0700344 if (cUnit->instructionSet != kX86) {
345 // Load trampoline target
buzbeef0504cd2012-11-13 16:31:10 -0800346 loadWordDisp(cUnit, targetReg(kSelf), trampoline, targetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700347 }
buzbeef0504cd2012-11-13 16:31:10 -0800348 // Load kArg0 with method index
349 loadConstant(cUnit, targetReg(kArg0), dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 return 1;
351 }
352 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800353}
354
buzbee3b3dbdd2012-06-13 13:39:34 -0700355int nextStaticCallInsnSP(CompilationUnit* cUnit, CallInfo* info,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700356 int state, uint32_t dexIdx, uint32_t methodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700357 uintptr_t unused, uintptr_t unused2,
358 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800359{
Ian Rogers57b86d42012-03-27 16:05:41 -0700360 int trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700361 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800362}
363
buzbee3b3dbdd2012-06-13 13:39:34 -0700364int nextDirectCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700365 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700366 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800367{
Ian Rogers57b86d42012-03-27 16:05:41 -0700368 int trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700369 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800370}
371
buzbee3b3dbdd2012-06-13 13:39:34 -0700372int nextSuperCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700373 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700374 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800375{
Ian Rogers57b86d42012-03-27 16:05:41 -0700376 int trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700377 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800378}
379
buzbee3b3dbdd2012-06-13 13:39:34 -0700380int nextVCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700381 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700382 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800383{
Ian Rogers57b86d42012-03-27 16:05:41 -0700384 int trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700385 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800386}
387
buzbee15bf9802012-06-12 17:49:27 -0700388int nextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit,
buzbee3b3dbdd2012-06-13 13:39:34 -0700389 CallInfo* info, int state,
buzbee15bf9802012-06-12 17:49:27 -0700390 uint32_t dexIdx, uint32_t unused,
391 uintptr_t unused2, uintptr_t unused3,
392 InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800393{
Ian Rogers57b86d42012-03-27 16:05:41 -0700394 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
buzbee15bf9802012-06-12 17:49:27 -0700395 return nextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800396}
397
buzbee3b3dbdd2012-06-13 13:39:34 -0700398int loadArgRegs(CompilationUnit* cUnit, CallInfo* info, int callState,
buzbee15bf9802012-06-12 17:49:27 -0700399 NextCallInsn nextCallInsn, uint32_t dexIdx,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700400 uint32_t methodIdx, uintptr_t directCode,
Brian Carlstromf5822582012-03-19 22:34:31 -0700401 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800402{
buzbeef0504cd2012-11-13 16:31:10 -0800403 int lastArgReg = targetReg(kArg3);
404 int nextReg = targetReg(kArg1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 int nextArg = 0;
406 if (skipThis) {
407 nextReg++;
408 nextArg++;
409 }
buzbee15bf9802012-06-12 17:49:27 -0700410 for (; (nextReg <= lastArgReg) && (nextArg < info->numArgWords); nextReg++) {
411 RegLocation rlArg = info->args[nextArg++];
Bill Buzbeea114add2012-05-03 15:00:40 -0700412 rlArg = oatUpdateRawLoc(cUnit, rlArg);
buzbeef0504cd2012-11-13 16:31:10 -0800413 if (rlArg.wide && (nextReg <= targetReg(kArg2))) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700414 loadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
415 nextReg++;
416 nextArg++;
417 } else {
418 rlArg.wide = false;
419 loadValueDirectFixed(cUnit, rlArg, nextReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800420 }
buzbee15bf9802012-06-12 17:49:27 -0700421 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 directCode, directMethod, type);
423 }
424 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800425}
426
427/*
428 * Load up to 5 arguments, the first three of which will be in
buzbeef0504cd2012-11-13 16:31:10 -0800429 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
buzbee31a4a6f2012-02-28 15:36:15 -0800430 * and as part of the load sequence, it must be replaced with
431 * the target method pointer. Note, this may also be called
432 * for "range" variants if the number of arguments is 5 or fewer.
433 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700434int genDalvikArgsNoRange(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700435 int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800436 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700437 uint32_t dexIdx, uint32_t methodIdx,
438 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700439 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800440{
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 RegLocation rlArg;
buzbee31a4a6f2012-02-28 15:36:15 -0800442
Bill Buzbeea114add2012-05-03 15:00:40 -0700443 /* If no arguments, just return */
buzbee15bf9802012-06-12 17:49:27 -0700444 if (info->numArgWords == 0)
buzbee31a4a6f2012-02-28 15:36:15 -0800445 return callState;
Bill Buzbeea114add2012-05-03 15:00:40 -0700446
buzbee15bf9802012-06-12 17:49:27 -0700447 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 directCode, directMethod, type);
449
buzbee15bf9802012-06-12 17:49:27 -0700450 DCHECK_LE(info->numArgWords, 5);
451 if (info->numArgWords > 3) {
452 int32_t nextUse = 3;
Bill Buzbeea114add2012-05-03 15:00:40 -0700453 //Detect special case of wide arg spanning arg3/arg4
buzbee15bf9802012-06-12 17:49:27 -0700454 RegLocation rlUse0 = info->args[0];
455 RegLocation rlUse1 = info->args[1];
456 RegLocation rlUse2 = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
458 rlUse2.wide) {
459 int reg = -1;
460 // Wide spans, we need the 2nd half of uses[2].
461 rlArg = oatUpdateLocWide(cUnit, rlUse2);
462 if (rlArg.location == kLocPhysReg) {
463 reg = rlArg.highReg;
464 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800465 // kArg2 & rArg3 can safely be used here
466 reg = targetReg(kArg3);
467 loadWordDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbee15bf9802012-06-12 17:49:27 -0700468 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700469 methodIdx, directCode, directMethod, type);
470 }
buzbeef0504cd2012-11-13 16:31:10 -0800471 storeBaseDisp(cUnit, targetReg(kSp), (nextUse + 1) * 4, reg, kWord);
472 storeBaseDisp(cUnit, targetReg(kSp), 16 /* (3+1)*4 */, reg, kWord);
buzbee15bf9802012-06-12 17:49:27 -0700473 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 directCode, directMethod, type);
475 nextUse++;
476 }
477 // Loop through the rest
buzbee15bf9802012-06-12 17:49:27 -0700478 while (nextUse < info->numArgWords) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 int lowReg;
480 int highReg = -1;
buzbee15bf9802012-06-12 17:49:27 -0700481 rlArg = info->args[nextUse];
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 rlArg = oatUpdateRawLoc(cUnit, rlArg);
483 if (rlArg.location == kLocPhysReg) {
484 lowReg = rlArg.lowReg;
485 highReg = rlArg.highReg;
486 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800487 lowReg = targetReg(kArg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 if (rlArg.wide) {
buzbeef0504cd2012-11-13 16:31:10 -0800489 highReg = targetReg(kArg3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700490 loadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
491 } else {
492 loadValueDirectFixed(cUnit, rlArg, lowReg);
493 }
buzbee15bf9802012-06-12 17:49:27 -0700494 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 methodIdx, directCode, directMethod, type);
496 }
497 int outsOffset = (nextUse + 1) * 4;
498 if (rlArg.wide) {
buzbeef0504cd2012-11-13 16:31:10 -0800499 storeBaseDispWide(cUnit, targetReg(kSp), outsOffset, lowReg, highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 nextUse += 2;
501 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800502 storeWordDisp(cUnit, targetReg(kSp), outsOffset, lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 nextUse++;
504 }
buzbee15bf9802012-06-12 17:49:27 -0700505 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 directCode, directMethod, type);
507 }
508 }
509
buzbee15bf9802012-06-12 17:49:27 -0700510 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700511 dexIdx, methodIdx, directCode, directMethod,
512 type, skipThis);
513
514 if (pcrLabel) {
buzbeecbd6d442012-11-17 14:11:25 -0800515 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, targetReg(kArg1), info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700516 }
517 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800518}
519
520/*
521 * May have 0+ arguments (also used for jumbo). Note that
522 * source virtual registers may be in physical registers, so may
523 * need to be flushed to home location before copying. This
524 * applies to arg3 and above (see below).
525 *
526 * Two general strategies:
527 * If < 20 arguments
528 * Pass args 3-18 using vldm/vstm block copy
buzbeef0504cd2012-11-13 16:31:10 -0800529 * Pass arg0, arg1 & arg2 in kArg1-kArg3
buzbee31a4a6f2012-02-28 15:36:15 -0800530 * If 20+ arguments
531 * Pass args arg19+ using memcpy block copy
buzbeef0504cd2012-11-13 16:31:10 -0800532 * Pass arg0, arg1 & arg2 in kArg1-kArg3
buzbee31a4a6f2012-02-28 15:36:15 -0800533 *
534 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700535int genDalvikArgsRange(CompilationUnit* cUnit, CallInfo* info, int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800536 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700537 uint32_t dexIdx, uint32_t methodIdx,
538 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700539 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800540{
buzbee31a4a6f2012-02-28 15:36:15 -0800541
Bill Buzbeea114add2012-05-03 15:00:40 -0700542 // If we can treat it as non-range (Jumbo ops will use range form)
buzbee15bf9802012-06-12 17:49:27 -0700543 if (info->numArgWords <= 5)
544 return genDalvikArgsNoRange(cUnit, info, callState, pcrLabel,
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 nextCallInsn, dexIdx, methodIdx,
546 directCode, directMethod, type, skipThis);
547 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700548 * First load the non-register arguments. Both forms expect all
549 * of the source arguments to be in their home frame location, so
550 * scan the sReg names and flush any that have been promoted to
551 * frame backing storage.
552 */
553 // Scan the rest of the args - if in physReg flush to memory
buzbee15bf9802012-06-12 17:49:27 -0700554 for (int nextArg = 0; nextArg < info->numArgWords;) {
555 RegLocation loc = info->args[nextArg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 if (loc.wide) {
557 loc = oatUpdateLocWide(cUnit, loc);
558 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbeef0504cd2012-11-13 16:31:10 -0800559 storeBaseDispWide(cUnit, targetReg(kSp), oatSRegOffset(cUnit, loc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 loc.lowReg, loc.highReg);
561 }
562 nextArg += 2;
563 } else {
564 loc = oatUpdateLoc(cUnit, loc);
565 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbeef0504cd2012-11-13 16:31:10 -0800566 storeBaseDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, loc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700567 loc.lowReg, kWord);
568 }
569 nextArg++;
buzbee31a4a6f2012-02-28 15:36:15 -0800570 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 }
buzbee31a4a6f2012-02-28 15:36:15 -0800572
buzbee15bf9802012-06-12 17:49:27 -0700573 int startOffset = oatSRegOffset(cUnit, info->args[3].sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 int outsOffset = 4 /* Method* */ + (3 * 4);
buzbeeb046e162012-10-30 15:48:42 -0700575 if (cUnit->instructionSet != kThumb2) {
buzbee31a4a6f2012-02-28 15:36:15 -0800576 // Generate memcpy
buzbeef0504cd2012-11-13 16:31:10 -0800577 opRegRegImm(cUnit, kOpAdd, targetReg(kArg0), targetReg(kSp), outsOffset);
578 opRegRegImm(cUnit, kOpAdd, targetReg(kArg1), targetReg(kSp), startOffset);
579 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy), targetReg(kArg0),
580 targetReg(kArg1), (info->numArgWords - 3) * 4, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700581 } else {
buzbeeb046e162012-10-30 15:48:42 -0700582 if (info->numArgWords >= 20) {
583 // Generate memcpy
buzbeef0504cd2012-11-13 16:31:10 -0800584 opRegRegImm(cUnit, kOpAdd, targetReg(kArg0), targetReg(kSp), outsOffset);
585 opRegRegImm(cUnit, kOpAdd, targetReg(kArg1), targetReg(kSp), startOffset);
586 callRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy), targetReg(kArg0),
587 targetReg(kArg1), (info->numArgWords - 3) * 4, false);
buzbeeb046e162012-10-30 15:48:42 -0700588 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800589 // Use vldm/vstm pair using kArg3 as a temp
buzbeeb046e162012-10-30 15:48:42 -0700590 int regsLeft = std::min(info->numArgWords - 3, 16);
591 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
592 directCode, directMethod, type);
buzbeef0504cd2012-11-13 16:31:10 -0800593 opRegRegImm(cUnit, kOpAdd, targetReg(kArg3), targetReg(kSp), startOffset);
594 LIR* ld = opVldm(cUnit, targetReg(kArg3), regsLeft);
buzbeeb046e162012-10-30 15:48:42 -0700595 //TUNING: loosen barrier
596 ld->defMask = ENCODE_ALL;
597 setMemRefType(ld, true /* isLoad */, kDalvikReg);
598 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
599 directCode, directMethod, type);
buzbeef0504cd2012-11-13 16:31:10 -0800600 opRegRegImm(cUnit, kOpAdd, targetReg(kArg3), targetReg(kSp), 4 /* Method* */ + (3 * 4));
buzbeeb046e162012-10-30 15:48:42 -0700601 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
602 directCode, directMethod, type);
buzbeef0504cd2012-11-13 16:31:10 -0800603 LIR* st = opVstm(cUnit, targetReg(kArg3), regsLeft);
buzbeeb046e162012-10-30 15:48:42 -0700604 setMemRefType(st, false /* isLoad */, kDalvikReg);
605 st->defMask = ENCODE_ALL;
606 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
607 directCode, directMethod, type);
608 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700610
buzbee15bf9802012-06-12 17:49:27 -0700611 callState = loadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 dexIdx, methodIdx, directCode, directMethod,
613 type, skipThis);
614
buzbee15bf9802012-06-12 17:49:27 -0700615 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 directCode, directMethod, type);
617 if (pcrLabel) {
buzbeef0504cd2012-11-13 16:31:10 -0800618 *pcrLabel = genNullCheck(cUnit, info->args[0].sRegLow, targetReg(kArg1),
buzbee15bf9802012-06-12 17:49:27 -0700619 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700620 }
621 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800622}
623
buzbee3b3dbdd2012-06-13 13:39:34 -0700624RegLocation inlineTarget(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700625{
Bill Buzbeea114add2012-05-03 15:00:40 -0700626 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700627 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 res = oatGetReturn(cUnit, false);
629 } else {
buzbee15bf9802012-06-12 17:49:27 -0700630 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 }
632 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700633}
634
buzbee3b3dbdd2012-06-13 13:39:34 -0700635RegLocation inlineTargetWide(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700636{
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700638 if (info->result.location == kLocInvalid) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 res = oatGetReturnWide(cUnit, false);
640 } else {
buzbee15bf9802012-06-12 17:49:27 -0700641 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 }
643 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700644}
645
buzbee3b3dbdd2012-06-13 13:39:34 -0700646bool genInlinedCharAt(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700647{
buzbeeb046e162012-10-30 15:48:42 -0700648 if (cUnit->instructionSet == kMips) {
649 // TODO - add Mips implementation
650 return false;
651 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 // Location of reference to data array
653 int valueOffset = String::ValueOffset().Int32Value();
654 // Location of count
655 int countOffset = String::CountOffset().Int32Value();
656 // Starting offset within data array
657 int offsetOffset = String::OffsetOffset().Int32Value();
658 // Start of char data with array_
659 int dataOffset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
buzbeefc9e6fa2012-03-23 15:14:29 -0700660
buzbee15bf9802012-06-12 17:49:27 -0700661 RegLocation rlObj = info->args[0];
662 RegLocation rlIdx = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 rlObj = loadValue(cUnit, rlObj, kCoreReg);
664 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
665 int regMax;
buzbee15bf9802012-06-12 17:49:27 -0700666 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
667 bool rangeCheck = (!(info->optFlags & MIR_IGNORE_RANGE_CHECK));
jeffhao634ea282012-08-10 13:04:01 -0700668 LIR* launchPad = NULL;
buzbeeb046e162012-10-30 15:48:42 -0700669 int regOff = INVALID_REG;
670 int regPtr = INVALID_REG;
671 if (cUnit->instructionSet != kX86) {
672 regOff = oatAllocTemp(cUnit);
673 regPtr = oatAllocTemp(cUnit);
674 if (rangeCheck) {
675 regMax = oatAllocTemp(cUnit);
676 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
677 }
678 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
679 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
680 if (rangeCheck) {
681 // Set up a launch pad to allow retry in case of bounds violation */
buzbeecbd6d442012-11-17 14:11:25 -0800682 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
buzbeeb046e162012-10-30 15:48:42 -0700683 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
buzbeecbd6d442012-11-17 14:11:25 -0800684 reinterpret_cast<uintptr_t>(launchPad));
buzbeeb046e162012-10-30 15:48:42 -0700685 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
686 oatFreeTemp(cUnit, regMax);
687 opCondBranch(cUnit, kCondCs, launchPad);
688 }
689 } else {
690 if (rangeCheck) {
691 regMax = oatAllocTemp(cUnit);
692 loadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
693 // Set up a launch pad to allow retry in case of bounds violation */
buzbeecbd6d442012-11-17 14:11:25 -0800694 launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
buzbeeb046e162012-10-30 15:48:42 -0700695 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
buzbeecbd6d442012-11-17 14:11:25 -0800696 reinterpret_cast<uintptr_t>(launchPad));
buzbeeb046e162012-10-30 15:48:42 -0700697 opRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
698 oatFreeTemp(cUnit, regMax);
699 opCondBranch(cUnit, kCondCc, launchPad);
700 }
701 regOff = oatAllocTemp(cUnit);
702 regPtr = oatAllocTemp(cUnit);
703 loadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
704 loadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700706 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
707 opRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
jeffhao634ea282012-08-10 13:04:01 -0700708 oatFreeTemp(cUnit, rlObj.lowReg);
709 oatFreeTemp(cUnit, rlIdx.lowReg);
buzbee15bf9802012-06-12 17:49:27 -0700710 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700711 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
712 loadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
713 oatFreeTemp(cUnit, regOff);
714 oatFreeTemp(cUnit, regPtr);
715 storeValue(cUnit, rlDest, rlResult);
716 if (rangeCheck) {
Elliott Hughes60234562012-06-01 12:25:59 -0700717 launchPad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700718 }
719 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700720 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700722}
723
724// Generates an inlined String.isEmpty or String.length.
buzbee3b3dbdd2012-06-13 13:39:34 -0700725bool genInlinedStringIsEmptyOrLength(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700726 bool isEmpty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700727{
buzbeeb046e162012-10-30 15:48:42 -0700728 if (cUnit->instructionSet == kMips) {
729 // TODO - add Mips implementation
730 return false;
731 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 // dst = src.length();
buzbee15bf9802012-06-12 17:49:27 -0700733 RegLocation rlObj = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700734 rlObj = loadValue(cUnit, rlObj, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700735 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700736 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee15bf9802012-06-12 17:49:27 -0700737 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700738 loadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
739 rlResult.lowReg);
740 if (isEmpty) {
741 // dst = (dst == 0);
buzbeeb046e162012-10-30 15:48:42 -0700742 if (cUnit->instructionSet == kThumb2) {
743 int tReg = oatAllocTemp(cUnit);
744 opRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
745 opRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
746 } else {
747 DCHECK_EQ(cUnit->instructionSet, kX86);
748 opRegImm(cUnit, kOpSub, rlResult.lowReg, 1);
749 opRegImm(cUnit, kOpLsr, rlResult.lowReg, 31);
750 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700751 }
752 storeValue(cUnit, rlDest, rlResult);
753 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700754}
755
buzbee3b3dbdd2012-06-13 13:39:34 -0700756bool genInlinedAbsInt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700757{
buzbeeb046e162012-10-30 15:48:42 -0700758 if (cUnit->instructionSet == kMips) {
759 // TODO - add Mips implementation
760 return false;
761 }
buzbee15bf9802012-06-12 17:49:27 -0700762 RegLocation rlSrc = info->args[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700763 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
buzbee15bf9802012-06-12 17:49:27 -0700764 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700765 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
766 int signReg = oatAllocTemp(cUnit);
767 // abs(x) = y<=x>>31, (x+y)^y.
768 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
769 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
770 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
771 storeValue(cUnit, rlDest, rlResult);
772 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700773}
774
buzbee3b3dbdd2012-06-13 13:39:34 -0700775bool genInlinedAbsLong(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700776{
buzbeeb046e162012-10-30 15:48:42 -0700777 if (cUnit->instructionSet == kMips) {
778 // TODO - add Mips implementation
779 return false;
780 }
781 if (cUnit->instructionSet == kThumb2) {
782 RegLocation rlSrc = info->args[0];
783 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
784 RegLocation rlDest = inlineTargetWide(cUnit, info);
785 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
786 int signReg = oatAllocTemp(cUnit);
787 // abs(x) = y<=x>>31, (x+y)^y.
788 opRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
789 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
790 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
791 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
792 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
793 storeValueWide(cUnit, rlDest, rlResult);
794 return true;
795 } else {
796 DCHECK_EQ(cUnit->instructionSet, kX86);
797 // Reuse source registers to avoid running out of temps
798 RegLocation rlSrc = info->args[0];
799 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
800 RegLocation rlDest = inlineTargetWide(cUnit, info);
801 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
802 opRegCopyWide(cUnit, rlResult.lowReg, rlResult.highReg, rlSrc.lowReg, rlSrc.highReg);
803 oatFreeTemp(cUnit, rlSrc.lowReg);
804 oatFreeTemp(cUnit, rlSrc.highReg);
805 int signReg = oatAllocTemp(cUnit);
806 // abs(x) = y<=x>>31, (x+y)^y.
807 opRegRegImm(cUnit, kOpAsr, signReg, rlResult.highReg, 31);
808 opRegReg(cUnit, kOpAdd, rlResult.lowReg, signReg);
809 opRegReg(cUnit, kOpAdc, rlResult.highReg, signReg);
810 opRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
811 opRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
812 storeValueWide(cUnit, rlDest, rlResult);
813 return true;
814 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700815}
816
buzbee3b3dbdd2012-06-13 13:39:34 -0700817bool genInlinedFloatCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700818{
buzbeeb046e162012-10-30 15:48:42 -0700819 if (cUnit->instructionSet == kMips) {
820 // TODO - add Mips implementation
821 return false;
822 }
buzbee15bf9802012-06-12 17:49:27 -0700823 RegLocation rlSrc = info->args[0];
824 RegLocation rlDest = inlineTarget(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 storeValue(cUnit, rlDest, rlSrc);
826 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700827}
828
buzbee3b3dbdd2012-06-13 13:39:34 -0700829bool genInlinedDoubleCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700830{
buzbeeb046e162012-10-30 15:48:42 -0700831 if (cUnit->instructionSet == kMips) {
832 // TODO - add Mips implementation
833 return false;
834 }
buzbee15bf9802012-06-12 17:49:27 -0700835 RegLocation rlSrc = info->args[0];
836 RegLocation rlDest = inlineTargetWide(cUnit, info);
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 storeValueWide(cUnit, rlDest, rlSrc);
838 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700839}
840
841/*
842 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
843 * otherwise bails to standard library code.
844 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700845bool genInlinedIndexOf(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700846 bool zeroBased)
buzbeefc9e6fa2012-03-23 15:14:29 -0700847{
buzbeeb046e162012-10-30 15:48:42 -0700848 if (cUnit->instructionSet == kMips) {
849 // TODO - add Mips implementation
850 return false;
851 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 oatClobberCalleeSave(cUnit);
853 oatLockCallTemps(cUnit); // Using fixed registers
buzbeef0504cd2012-11-13 16:31:10 -0800854 int regPtr = targetReg(kArg0);
855 int regChar = targetReg(kArg1);
856 int regStart = targetReg(kArg2);
buzbeefc9e6fa2012-03-23 15:14:29 -0700857
buzbee15bf9802012-06-12 17:49:27 -0700858 RegLocation rlObj = info->args[0];
859 RegLocation rlChar = info->args[1];
860 RegLocation rlStart = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700861 loadValueDirectFixed(cUnit, rlObj, regPtr);
862 loadValueDirectFixed(cUnit, rlChar, regChar);
863 if (zeroBased) {
864 loadConstant(cUnit, regStart, 0);
865 } else {
866 loadValueDirectFixed(cUnit, rlStart, regStart);
867 }
buzbeeb046e162012-10-30 15:48:42 -0700868 int rTgt = (cUnit->instructionSet != kX86) ? loadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf)) : 0;
buzbee15bf9802012-06-12 17:49:27 -0700869 genNullCheck(cUnit, rlObj.sRegLow, regPtr, info->optFlags);
buzbeecbd6d442012-11-17 14:11:25 -0800870 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
871 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads, reinterpret_cast<uintptr_t>(launchPad));
Bill Buzbeea114add2012-05-03 15:00:40 -0700872 opCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
buzbee8320f382012-09-11 16:29:42 -0700873 // NOTE: not a safepoint
buzbeeb046e162012-10-30 15:48:42 -0700874 if (cUnit->instructionSet != kX86) {
875 opReg(cUnit, kOpBlx, rTgt);
876 } else {
877 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
878 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700879 LIR* resumeTgt = newLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800880 launchPad->operands[2] = reinterpret_cast<uintptr_t>(resumeTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700881 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700882 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
883 RegLocation rlReturn = oatGetReturn(cUnit, false);
884 RegLocation rlDest = inlineTarget(cUnit, info);
885 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700886 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700887}
888
889/* Fast string.compareTo(Ljava/lang/string;)I. */
buzbee3b3dbdd2012-06-13 13:39:34 -0700890bool genInlinedStringCompareTo(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700891{
buzbeeb046e162012-10-30 15:48:42 -0700892 if (cUnit->instructionSet == kMips) {
893 // TODO - add Mips implementation
894 return false;
895 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 oatClobberCalleeSave(cUnit);
897 oatLockCallTemps(cUnit); // Using fixed registers
buzbeef0504cd2012-11-13 16:31:10 -0800898 int regThis = targetReg(kArg0);
899 int regCmp = targetReg(kArg1);
buzbeefc9e6fa2012-03-23 15:14:29 -0700900
buzbee15bf9802012-06-12 17:49:27 -0700901 RegLocation rlThis = info->args[0];
902 RegLocation rlCmp = info->args[1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700903 loadValueDirectFixed(cUnit, rlThis, regThis);
904 loadValueDirectFixed(cUnit, rlCmp, regCmp);
buzbeeb046e162012-10-30 15:48:42 -0700905 int rTgt = (cUnit->instructionSet != kX86) ?
906 loadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo)) : 0;
buzbee15bf9802012-06-12 17:49:27 -0700907 genNullCheck(cUnit, rlThis.sRegLow, regThis, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 //TUNING: check if rlCmp.sRegLow is already null checked
buzbeecbd6d442012-11-17 14:11:25 -0800909 LIR* launchPad = rawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
910 oatInsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads, reinterpret_cast<uintptr_t>(launchPad));
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 opCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
buzbee8320f382012-09-11 16:29:42 -0700912 // NOTE: not a safepoint
buzbeeb046e162012-10-30 15:48:42 -0700913 if (cUnit->instructionSet != kX86) {
914 opReg(cUnit, kOpBlx, rTgt);
915 } else {
916 opThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
917 }
Elliott Hughes60234562012-06-01 12:25:59 -0700918 launchPad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700920 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
921 RegLocation rlReturn = oatGetReturn(cUnit, false);
922 RegLocation rlDest = inlineTarget(cUnit, info);
923 storeValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700924 return true;
Ian Rogers0183dd72012-09-17 23:06:51 -0700925}
926
buzbee3b3dbdd2012-06-13 13:39:34 -0700927bool genIntrinsic(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700928{
Ian Rogerse13eafa2012-09-07 11:24:27 -0700929 if (info->optFlags & MIR_INLINED) {
buzbeefc9e6fa2012-03-23 15:14:29 -0700930 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700931 }
932 /*
933 * TODO: move these to a target-specific structured constant array
934 * and use a generic match function. The list of intrinsics may be
935 * slightly different depending on target.
936 * TODO: Fold this into a matching function that runs during
937 * basic block building. This should be part of the action for
938 * small method inlining and recognition of the special object init
939 * method. By doing this during basic block construction, we can also
940 * take advantage of/generate new useful dataflow info.
941 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700942 std::string tgtMethod(PrettyMethod(info->index, *cUnit->dex_file));
Ian Rogers0183dd72012-09-17 23:06:51 -0700943 if (tgtMethod.find(" java.lang") != std::string::npos) {
944 if (tgtMethod == "long java.lang.Double.doubleToRawLongBits(double)") {
945 return genInlinedDoubleCvt(cUnit, info);
946 }
947 if (tgtMethod == "double java.lang.Double.longBitsToDouble(long)") {
948 return genInlinedDoubleCvt(cUnit, info);
949 }
950 if (tgtMethod == "int java.lang.Float.floatToRawIntBits(float)") {
951 return genInlinedFloatCvt(cUnit, info);
952 }
953 if (tgtMethod == "float java.lang.Float.intBitsToFloat(int)") {
954 return genInlinedFloatCvt(cUnit, info);
955 }
956 if (tgtMethod == "int java.lang.Math.abs(int)" ||
957 tgtMethod == "int java.lang.StrictMath.abs(int)") {
958 return genInlinedAbsInt(cUnit, info);
959 }
960 if (tgtMethod == "long java.lang.Math.abs(long)" ||
961 tgtMethod == "long java.lang.StrictMath.abs(long)") {
962 return genInlinedAbsLong(cUnit, info);
963 }
964 if (tgtMethod == "int java.lang.Math.max(int, int)" ||
965 tgtMethod == "int java.lang.StrictMath.max(int, int)") {
966 return genInlinedMinMaxInt(cUnit, info, false /* isMin */);
967 }
968 if (tgtMethod == "int java.lang.Math.min(int, int)" ||
969 tgtMethod == "int java.lang.StrictMath.min(int, int)") {
970 return genInlinedMinMaxInt(cUnit, info, true /* isMin */);
971 }
972 if (tgtMethod == "double java.lang.Math.sqrt(double)" ||
973 tgtMethod == "double java.lang.StrictMath.sqrt(double)") {
974 return genInlinedSqrt(cUnit, info);
975 }
976 if (tgtMethod == "char java.lang.String.charAt(int)") {
977 return genInlinedCharAt(cUnit, info);
978 }
979 if (tgtMethod == "int java.lang.String.compareTo(java.lang.String)") {
980 return genInlinedStringCompareTo(cUnit, info);
981 }
982 if (tgtMethod == "boolean java.lang.String.isEmpty()") {
983 return genInlinedStringIsEmptyOrLength(cUnit, info, true /* isEmpty */);
984 }
985 if (tgtMethod == "int java.lang.String.indexOf(int, int)") {
986 return genInlinedIndexOf(cUnit, info, false /* base 0 */);
987 }
988 if (tgtMethod == "int java.lang.String.indexOf(int)") {
989 return genInlinedIndexOf(cUnit, info, true /* base 0 */);
990 }
991 if (tgtMethod == "int java.lang.String.length()") {
992 return genInlinedStringIsEmptyOrLength(cUnit, info, false /* isEmpty */);
993 }
994 } else if (tgtMethod.find("boolean sun.misc.Unsafe.compareAndSwap") != std::string::npos) {
995 if (tgtMethod == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
996 return genInlinedCas32(cUnit, info, false);
997 }
998 if (tgtMethod == "boolean sun.misc.Unsafe.compareAndSwapObject(java.lang.Object, long, java.lang.Object, java.lang.Object)") {
999 return genInlinedCas32(cUnit, info, true);
1000 }
Ian Rogerse13eafa2012-09-07 11:24:27 -07001001 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001002 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -07001003}
1004
buzbee1bc37c62012-11-20 13:35:41 -08001005void genInvoke(CompilationUnit* cUnit, CallInfo* info)
1006{
1007 if (genIntrinsic(cUnit, info)) {
1008 return;
1009 }
1010 InvokeType originalType = info->type; // avoiding mutation by ComputeInvokeInfo
1011 int callState = 0;
1012 LIR* nullCk;
1013 LIR** pNullCk = NULL;
1014 NextCallInsn nextCallInsn;
1015 oatFlushAllRegs(cUnit); /* Everything to home location */
1016 // Explicit register usage
1017 oatLockCallTemps(cUnit);
1018
1019 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
1020 *cUnit->dex_file,
1021 cUnit->code_item, cUnit->method_idx,
1022 cUnit->access_flags);
1023
1024 uint32_t dexMethodIdx = info->index;
1025 int vtableIdx;
1026 uintptr_t directCode;
1027 uintptr_t directMethod;
1028 bool skipThis;
1029 bool fastPath =
1030 cUnit->compiler->ComputeInvokeInfo(dexMethodIdx, &mUnit, info->type,
1031 vtableIdx, directCode,
1032 directMethod)
1033 && !SLOW_INVOKE_PATH;
1034 if (info->type == kInterface) {
1035 if (fastPath) {
1036 pNullCk = &nullCk;
1037 }
1038 nextCallInsn = fastPath ? nextInterfaceCallInsn
1039 : nextInterfaceCallInsnWithAccessCheck;
1040 skipThis = false;
1041 } else if (info->type == kDirect) {
1042 if (fastPath) {
1043 pNullCk = &nullCk;
1044 }
1045 nextCallInsn = fastPath ? nextSDCallInsn : nextDirectCallInsnSP;
1046 skipThis = false;
1047 } else if (info->type == kStatic) {
1048 nextCallInsn = fastPath ? nextSDCallInsn : nextStaticCallInsnSP;
1049 skipThis = false;
1050 } else if (info->type == kSuper) {
1051 DCHECK(!fastPath); // Fast path is a direct call.
1052 nextCallInsn = nextSuperCallInsnSP;
1053 skipThis = false;
1054 } else {
1055 DCHECK_EQ(info->type, kVirtual);
1056 nextCallInsn = fastPath ? nextVCallInsn : nextVCallInsnSP;
1057 skipThis = fastPath;
1058 }
1059 if (!info->isRange) {
1060 callState = genDalvikArgsNoRange(cUnit, info, callState, pNullCk,
1061 nextCallInsn, dexMethodIdx,
1062 vtableIdx, directCode, directMethod,
1063 originalType, skipThis);
1064 } else {
1065 callState = genDalvikArgsRange(cUnit, info, callState, pNullCk,
1066 nextCallInsn, dexMethodIdx, vtableIdx,
1067 directCode, directMethod, originalType,
1068 skipThis);
1069 }
1070 // Finish up any of the call sequence not interleaved in arg loading
1071 while (callState >= 0) {
1072 callState = nextCallInsn(cUnit, info, callState, dexMethodIdx,
1073 vtableIdx, directCode, directMethod,
1074 originalType);
1075 }
1076 if (cUnit->enableDebug & (1 << kDebugDisplayMissingTargets)) {
1077 genShowTarget(cUnit);
1078 }
1079 LIR* callInst;
1080 if (cUnit->instructionSet != kX86) {
1081 callInst = opReg(cUnit, kOpBlx, targetReg(kInvokeTgt));
1082 } else {
1083 if (fastPath && info->type != kInterface) {
1084 callInst = opMem(cUnit, kOpBlx, targetReg(kArg0),
1085 AbstractMethod::GetCodeOffset().Int32Value());
1086 } else {
1087 int trampoline = 0;
1088 switch (info->type) {
1089 case kInterface:
1090 trampoline = fastPath ? ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline)
1091 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
1092 break;
1093 case kDirect:
1094 trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
1095 break;
1096 case kStatic:
1097 trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
1098 break;
1099 case kSuper:
1100 trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
1101 break;
1102 case kVirtual:
1103 trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
1104 break;
1105 default:
1106 LOG(FATAL) << "Unexpected invoke type";
1107 }
1108 callInst = opThreadMem(cUnit, kOpBlx, trampoline);
1109 }
1110 }
1111 markSafepointPC(cUnit, callInst);
1112
1113 oatClobberCalleeSave(cUnit);
1114 if (info->result.location != kLocInvalid) {
1115 // We have a following MOVE_RESULT - do it now.
1116 if (info->result.wide) {
1117 RegLocation retLoc = oatGetReturnWide(cUnit, info->result.fp);
1118 storeValueWide(cUnit, info->result, retLoc);
1119 } else {
1120 RegLocation retLoc = oatGetReturn(cUnit, info->result.fp);
1121 storeValue(cUnit, info->result, retLoc);
1122 }
1123 }
1124}
1125
1126/*
1127 * Build an array of location records for the incoming arguments.
1128 * Note: one location record per word of arguments, with dummy
1129 * high-word loc for wide arguments. Also pull up any following
1130 * MOVE_RESULT and incorporate it into the invoke.
1131 */
1132CallInfo* oatNewCallInfo(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
1133 InvokeType type, bool isRange)
1134{
1135 CallInfo* info = static_cast<CallInfo*>(oatNew(cUnit, sizeof(CallInfo), true, kAllocMisc));
1136 MIR* moveResultMIR = oatFindMoveResult(cUnit, bb, mir);
1137 if (moveResultMIR == NULL) {
1138 info->result.location = kLocInvalid;
1139 } else {
1140 info->result = oatGetRawDest(cUnit, moveResultMIR);
1141 moveResultMIR->dalvikInsn.opcode = Instruction::NOP;
1142 }
1143 info->numArgWords = mir->ssaRep->numUses;
1144 info->args = (info->numArgWords == 0) ? NULL : static_cast<RegLocation*>
1145 (oatNew(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc));
1146 for (int i = 0; i < info->numArgWords; i++) {
1147 info->args[i] = oatGetRawSrc(cUnit, mir, i);
1148 }
1149 info->optFlags = mir->optimizationFlags;
1150 info->type = type;
1151 info->isRange = isRange;
1152 info->index = mir->dalvikInsn.vB;
1153 info->offset = mir->offset;
1154 return info;
1155}
1156
buzbeefc9e6fa2012-03-23 15:14:29 -07001157
buzbee31a4a6f2012-02-28 15:36:15 -08001158} // namespace art