blob: 6f8b092ce2259db9988a390b130b49c1eb8f5f09 [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 *
buzbee52a77fc2012-11-20 19:50:46 -080035 * ArgLocs is an array of location records describing the incoming arguments
buzbeead8f15e2012-06-18 14:49:45 -070036 * with one location record per word of argument.
buzbee31a4a6f2012-02-28 15:36:15 -080037 */
buzbee52a77fc2012-11-20 19:50:46 -080038void 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;
buzbee52a77fc2012-11-20 19:50:46 -080047 rlSrc.lowReg = TargetReg(kArg0);
Bill Buzbeea114add2012-05-03 15:00:40 -070048 rlSrc.home = false;
buzbee52a77fc2012-11-20 19:50:46 -080049 MarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
50 StoreValue(cUnit, rlMethod, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -070051 // If Method* has been promoted, explicitly flush
52 if (rlMethod.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -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;
buzbee52a77fc2012-11-20 19:50:46 -080078 RegLocation* tLoc = &ArgLocs[i];
Bill Buzbeea114add2012-05-03 15:00:40 -070079 if ((vMap->coreLocation == kLocPhysReg) && !tLoc->fp) {
buzbee52a77fc2012-11-20 19:50:46 -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) {
buzbee52a77fc2012-11-20 19:50:46 -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) {
buzbee52a77fc2012-11-20 19:50:46 -080096 StoreBaseDisp(cUnit, TargetReg(kSp), SRegOffset(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) {
buzbee52a77fc2012-11-20 19:50:46 -0800102 LoadWordDisp(cUnit, TargetReg(kSp), SRegOffset(cUnit, startVReg + i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 vMap->coreReg);
104 }
105 if (vMap->fpLocation == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800106 LoadWordDisp(cUnit, TargetReg(kSp), SRegOffset(cUnit, startVReg + i),
107 vMap->FpReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 }
buzbee31a4a6f2012-02-28 15:36:15 -0800109 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 }
buzbee31a4a6f2012-02-28 15:36:15 -0800111}
112
buzbee52a77fc2012-11-20 19:50:46 -0800113void ScanMethodLiteralPool(CompilationUnit* cUnit, LIR** methodTarget, LIR** codeTarget,
buzbeeeaf09bc2012-11-15 14:51:41 -0800114 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800137int 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)) {
buzbee52a77fc2012-11-20 19:50:46 -0800151 LoadConstant(cUnit, TargetReg(kInvokeTgt), directCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800153 LIR* dataTarget = ScanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800155 dataTarget = AddWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 dataTarget->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700157 }
buzbee52a77fc2012-11-20 19:50:46 -0800158 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kInvokeTgt), dataTarget);
159 AppendLIR(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)) {
buzbee52a77fc2012-11-20 19:50:46 -0800163 LoadConstant(cUnit, TargetReg(kArg0), directMethod);
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800165 LIR* dataTarget = ScanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800167 dataTarget = AddWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 dataTarget->operands[1] = type;
169 }
buzbee52a77fc2012-11-20 19:50:46 -0800170 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kArg0), dataTarget);
171 AppendLIR(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.
buzbee52a77fc2012-11-20 19:50:46 -0800182 LoadCurrMethodDirect(cUnit, TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 break;
184 case 1: // Get method->dex_cache_resolved_methods_
buzbee52a77fc2012-11-20 19:50:46 -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)) {
buzbee52a77fc2012-11-20 19:50:46 -0800190 LoadConstant(cUnit, TargetReg(kInvokeTgt), directCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800192 LIR* dataTarget = ScanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700193 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800194 dataTarget = AddWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 dataTarget->operands[1] = type;
196 }
buzbee52a77fc2012-11-20 19:50:46 -0800197 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kInvokeTgt), dataTarget);
198 AppendLIR(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*
buzbee52a77fc2012-11-20 19:50:46 -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) {
buzbee52a77fc2012-11-20 19:50:46 -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
buzbee52a77fc2012-11-20 19:50:46 -0800228 * kArg1 here rather than the standard LoadArgRegs.
buzbee31a4a6f2012-02-28 15:36:15 -0800229 */
buzbee52a77fc2012-11-20 19:50:46 -0800230int 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];
buzbee52a77fc2012-11-20 19:50:46 -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]
buzbee52a77fc2012-11-20 19:50:46 -0800245 GenNullCheck(cUnit, info->args[0].sRegLow, TargetReg(kArg1), info->optFlags);
buzbeef0504cd2012-11-13 16:31:10 -0800246 // get this->klass_ [use kArg1, set kInvokeTgt]
buzbee52a77fc2012-11-20 19:50:46 -0800247 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]
buzbee52a77fc2012-11-20 19:50:46 -0800251 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]
buzbee52a77fc2012-11-20 19:50:46 -0800255 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) {
buzbee52a77fc2012-11-20 19:50:46 -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 */
buzbee52a77fc2012-11-20 19:50:46 -0800275int NextInterfaceCallInsn(CompilationUnit* cUnit, CallInfo* info, int state,
Ian Rogers137e88f2012-10-08 17:46:47 -0700276 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) {
buzbee52a77fc2012-11-20 19:50:46 -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)) {
buzbee52a77fc2012-11-20 19:50:46 -0800294 LoadConstant(cUnit, TargetReg(kArg0), directMethod);
Ian Rogers137e88f2012-10-08 17:46:47 -0700295 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800296 LIR* dataTarget = ScanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
Ian Rogers137e88f2012-10-08 17:46:47 -0700297 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800298 dataTarget = AddWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
Ian Rogers137e88f2012-10-08 17:46:47 -0700299 dataTarget->operands[1] = kInterface;
300 }
buzbee52a77fc2012-11-20 19:50:46 -0800301 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kArg0), dataTarget);
302 AppendLIR(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.
buzbee52a77fc2012-11-20 19:50:46 -0800313 LoadCurrMethodDirect(cUnit, TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800314 // Load the trampoline target [sets kInvokeTgt].
buzbeeb046e162012-10-30 15:48:42 -0700315 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -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]
buzbee52a77fc2012-11-20 19:50:46 -0800320 LoadWordDisp(cUnit, TargetReg(kArg0),
Ian Rogers137e88f2012-10-08 17:46:47 -0700321 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(),
buzbee52a77fc2012-11-20 19:50:46 -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]
buzbee52a77fc2012-11-20 19:50:46 -0800325 LoadWordDisp(cUnit, TargetReg(kArg0),
Ian Rogers137e88f2012-10-08 17:46:47 -0700326 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
buzbee52a77fc2012-11-20 19:50:46 -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
buzbee52a77fc2012-11-20 19:50:46 -0800336int 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
buzbee52a77fc2012-11-20 19:50:46 -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
buzbee52a77fc2012-11-20 19:50:46 -0800349 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
buzbee52a77fc2012-11-20 19:50:46 -0800355int 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);
buzbee52a77fc2012-11-20 19:50:46 -0800361 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800362}
363
buzbee52a77fc2012-11-20 19:50:46 -0800364int 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);
buzbee52a77fc2012-11-20 19:50:46 -0800369 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800370}
371
buzbee52a77fc2012-11-20 19:50:46 -0800372int 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);
buzbee52a77fc2012-11-20 19:50:46 -0800377 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800378}
379
buzbee52a77fc2012-11-20 19:50:46 -0800380int 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);
buzbee52a77fc2012-11-20 19:50:46 -0800385 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800386}
387
buzbee52a77fc2012-11-20 19:50:46 -0800388int 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);
buzbee52a77fc2012-11-20 19:50:46 -0800395 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800396}
397
buzbee52a77fc2012-11-20 19:50:46 -0800398int 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{
buzbee52a77fc2012-11-20 19:50:46 -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++];
buzbee52a77fc2012-11-20 19:50:46 -0800412 rlArg = UpdateRawLoc(cUnit, rlArg);
413 if (rlArg.wide && (nextReg <= TargetReg(kArg2))) {
414 LoadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 nextReg++;
416 nextArg++;
417 } else {
418 rlArg.wide = false;
buzbee52a77fc2012-11-20 19:50:46 -0800419 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800434int 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].
buzbee52a77fc2012-11-20 19:50:46 -0800461 rlArg = UpdateLocWide(cUnit, rlUse2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 if (rlArg.location == kLocPhysReg) {
463 reg = rlArg.highReg;
464 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800465 // kArg2 & rArg3 can safely be used here
buzbee52a77fc2012-11-20 19:50:46 -0800466 reg = TargetReg(kArg3);
467 LoadWordDisp(cUnit, TargetReg(kSp), SRegOffset(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 }
buzbee52a77fc2012-11-20 19:50:46 -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];
buzbee52a77fc2012-11-20 19:50:46 -0800482 rlArg = UpdateRawLoc(cUnit, rlArg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 if (rlArg.location == kLocPhysReg) {
484 lowReg = rlArg.lowReg;
485 highReg = rlArg.highReg;
486 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800487 lowReg = TargetReg(kArg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 if (rlArg.wide) {
buzbee52a77fc2012-11-20 19:50:46 -0800489 highReg = TargetReg(kArg3);
490 LoadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800492 LoadValueDirectFixed(cUnit, rlArg, lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 }
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) {
buzbee52a77fc2012-11-20 19:50:46 -0800499 StoreBaseDispWide(cUnit, TargetReg(kSp), outsOffset, lowReg, highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 nextUse += 2;
501 } else {
buzbee52a77fc2012-11-20 19:50:46 -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
buzbee52a77fc2012-11-20 19:50:46 -0800510 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) {
buzbee52a77fc2012-11-20 19:50:46 -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 */
buzbee52a77fc2012-11-20 19:50:46 -0800535int 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)
buzbee52a77fc2012-11-20 19:50:46 -0800544 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800557 loc = UpdateLocWide(cUnit, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700558 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee52a77fc2012-11-20 19:50:46 -0800559 StoreBaseDispWide(cUnit, TargetReg(kSp), SRegOffset(cUnit, loc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 loc.lowReg, loc.highReg);
561 }
562 nextArg += 2;
563 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800564 loc = UpdateLoc(cUnit, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee52a77fc2012-11-20 19:50:46 -0800566 StoreBaseDisp(cUnit, TargetReg(kSp), SRegOffset(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
buzbee52a77fc2012-11-20 19:50:46 -0800573 int startOffset = SRegOffset(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
buzbee52a77fc2012-11-20 19:50:46 -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
buzbee52a77fc2012-11-20 19:50:46 -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);
buzbee52a77fc2012-11-20 19:50:46 -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;
buzbee52a77fc2012-11-20 19:50:46 -0800597 SetMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbeeb046e162012-10-30 15:48:42 -0700598 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
599 directCode, directMethod, type);
buzbee52a77fc2012-11-20 19:50:46 -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);
buzbee52a77fc2012-11-20 19:50:46 -0800603 LIR* st = OpVstm(cUnit, TargetReg(kArg3), regsLeft);
604 SetMemRefType(st, false /* isLoad */, kDalvikReg);
buzbeeb046e162012-10-30 15:48:42 -0700605 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
buzbee52a77fc2012-11-20 19:50:46 -0800611 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) {
buzbee52a77fc2012-11-20 19:50:46 -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
buzbee52a77fc2012-11-20 19:50:46 -0800624RegLocation 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800628 res = GetReturn(cUnit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 } 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
buzbee52a77fc2012-11-20 19:50:46 -0800635RegLocation 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800639 res = GetReturnWide(cUnit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 } 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
buzbee52a77fc2012-11-20 19:50:46 -0800646bool 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];
buzbee52a77fc2012-11-20 19:50:46 -0800663 rlObj = LoadValue(cUnit, rlObj, kCoreReg);
664 rlIdx = LoadValue(cUnit, rlIdx, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 int regMax;
buzbee52a77fc2012-11-20 19:50:46 -0800666 GenNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
buzbee15bf9802012-06-12 17:49:27 -0700667 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800672 regOff = AllocTemp(cUnit);
673 regPtr = AllocTemp(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700674 if (rangeCheck) {
buzbee52a77fc2012-11-20 19:50:46 -0800675 regMax = AllocTemp(cUnit);
676 LoadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
buzbeeb046e162012-10-30 15:48:42 -0700677 }
buzbee52a77fc2012-11-20 19:50:46 -0800678 LoadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
679 LoadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
buzbeeb046e162012-10-30 15:48:42 -0700680 if (rangeCheck) {
681 // Set up a launch pad to allow retry in case of bounds violation */
buzbee52a77fc2012-11-20 19:50:46 -0800682 launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
683 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
buzbeecbd6d442012-11-17 14:11:25 -0800684 reinterpret_cast<uintptr_t>(launchPad));
buzbee52a77fc2012-11-20 19:50:46 -0800685 OpRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
686 FreeTemp(cUnit, regMax);
687 OpCondBranch(cUnit, kCondCs, launchPad);
buzbeeb046e162012-10-30 15:48:42 -0700688 }
689 } else {
690 if (rangeCheck) {
buzbee52a77fc2012-11-20 19:50:46 -0800691 regMax = AllocTemp(cUnit);
692 LoadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
buzbeeb046e162012-10-30 15:48:42 -0700693 // Set up a launch pad to allow retry in case of bounds violation */
buzbee52a77fc2012-11-20 19:50:46 -0800694 launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
695 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
buzbeecbd6d442012-11-17 14:11:25 -0800696 reinterpret_cast<uintptr_t>(launchPad));
buzbee52a77fc2012-11-20 19:50:46 -0800697 OpRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
698 FreeTemp(cUnit, regMax);
699 OpCondBranch(cUnit, kCondCc, launchPad);
buzbeeb046e162012-10-30 15:48:42 -0700700 }
buzbee52a77fc2012-11-20 19:50:46 -0800701 regOff = AllocTemp(cUnit);
702 regPtr = AllocTemp(cUnit);
703 LoadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
704 LoadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 }
buzbee52a77fc2012-11-20 19:50:46 -0800706 OpRegImm(cUnit, kOpAdd, regPtr, dataOffset);
707 OpRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
708 FreeTemp(cUnit, rlObj.lowReg);
709 FreeTemp(cUnit, rlIdx.lowReg);
710 RegLocation rlDest = InlineTarget(cUnit, info);
711 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
712 LoadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
713 FreeTemp(cUnit, regOff);
714 FreeTemp(cUnit, regPtr);
715 StoreValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -0700716 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.
buzbee52a77fc2012-11-20 19:50:46 -0800725bool 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];
buzbee52a77fc2012-11-20 19:50:46 -0800734 rlObj = LoadValue(cUnit, rlObj, kCoreReg);
735 RegLocation rlDest = InlineTarget(cUnit, info);
736 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
737 GenNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
738 LoadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700739 rlResult.lowReg);
740 if (isEmpty) {
741 // dst = (dst == 0);
buzbeeb046e162012-10-30 15:48:42 -0700742 if (cUnit->instructionSet == kThumb2) {
buzbee52a77fc2012-11-20 19:50:46 -0800743 int tReg = AllocTemp(cUnit);
744 OpRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
745 OpRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
buzbeeb046e162012-10-30 15:48:42 -0700746 } else {
747 DCHECK_EQ(cUnit->instructionSet, kX86);
buzbee52a77fc2012-11-20 19:50:46 -0800748 OpRegImm(cUnit, kOpSub, rlResult.lowReg, 1);
749 OpRegImm(cUnit, kOpLsr, rlResult.lowReg, 31);
buzbeeb046e162012-10-30 15:48:42 -0700750 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700751 }
buzbee52a77fc2012-11-20 19:50:46 -0800752 StoreValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700754}
755
buzbee52a77fc2012-11-20 19:50:46 -0800756bool 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];
buzbee52a77fc2012-11-20 19:50:46 -0800763 rlSrc = LoadValue(cUnit, rlSrc, kCoreReg);
764 RegLocation rlDest = InlineTarget(cUnit, info);
765 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
766 int signReg = AllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700767 // abs(x) = y<=x>>31, (x+y)^y.
buzbee52a77fc2012-11-20 19:50:46 -0800768 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);
Bill Buzbeea114add2012-05-03 15:00:40 -0700772 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700773}
774
buzbee52a77fc2012-11-20 19:50:46 -0800775bool 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];
buzbee52a77fc2012-11-20 19:50:46 -0800783 rlSrc = LoadValueWide(cUnit, rlSrc, kCoreReg);
784 RegLocation rlDest = InlineTargetWide(cUnit, info);
785 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
786 int signReg = AllocTemp(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700787 // abs(x) = y<=x>>31, (x+y)^y.
buzbee52a77fc2012-11-20 19:50:46 -0800788 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);
buzbeeb046e162012-10-30 15:48:42 -0700794 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];
buzbee52a77fc2012-11-20 19:50:46 -0800799 rlSrc = LoadValueWide(cUnit, rlSrc, kCoreReg);
800 RegLocation rlDest = InlineTargetWide(cUnit, info);
801 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
802 OpRegCopyWide(cUnit, rlResult.lowReg, rlResult.highReg, rlSrc.lowReg, rlSrc.highReg);
803 FreeTemp(cUnit, rlSrc.lowReg);
804 FreeTemp(cUnit, rlSrc.highReg);
805 int signReg = AllocTemp(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700806 // abs(x) = y<=x>>31, (x+y)^y.
buzbee52a77fc2012-11-20 19:50:46 -0800807 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);
buzbeeb046e162012-10-30 15:48:42 -0700813 return true;
814 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700815}
816
buzbee52a77fc2012-11-20 19:50:46 -0800817bool 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];
buzbee52a77fc2012-11-20 19:50:46 -0800824 RegLocation rlDest = InlineTarget(cUnit, info);
825 StoreValue(cUnit, rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700826 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700827}
828
buzbee52a77fc2012-11-20 19:50:46 -0800829bool 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];
buzbee52a77fc2012-11-20 19:50:46 -0800836 RegLocation rlDest = InlineTargetWide(cUnit, info);
837 StoreValueWide(cUnit, rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700838 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800845bool 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 }
buzbee52a77fc2012-11-20 19:50:46 -0800852 ClobberCalleeSave(cUnit);
853 LockCallTemps(cUnit); // Using fixed registers
854 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];
buzbee52a77fc2012-11-20 19:50:46 -0800861 LoadValueDirectFixed(cUnit, rlObj, regPtr);
862 LoadValueDirectFixed(cUnit, rlChar, regChar);
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 if (zeroBased) {
buzbee52a77fc2012-11-20 19:50:46 -0800864 LoadConstant(cUnit, regStart, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700865 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800866 LoadValueDirectFixed(cUnit, rlStart, regStart);
Bill Buzbeea114add2012-05-03 15:00:40 -0700867 }
buzbee52a77fc2012-11-20 19:50:46 -0800868 int rTgt = (cUnit->instructionSet != kX86) ? LoadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf)) : 0;
869 GenNullCheck(cUnit, rlObj.sRegLow, regPtr, info->optFlags);
870 LIR* launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
871 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads, reinterpret_cast<uintptr_t>(launchPad));
872 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800875 OpReg(cUnit, kOpBlx, rTgt);
buzbeeb046e162012-10-30 15:48:42 -0700876 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800877 OpThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
buzbeeb046e162012-10-30 15:48:42 -0700878 }
buzbee52a77fc2012-11-20 19:50:46 -0800879 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);
buzbee52a77fc2012-11-20 19:50:46 -0800883 RegLocation rlReturn = GetReturn(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. */
buzbee52a77fc2012-11-20 19:50:46 -0800890bool 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 }
buzbee52a77fc2012-11-20 19:50:46 -0800896 ClobberCalleeSave(cUnit);
897 LockCallTemps(cUnit); // Using fixed registers
898 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];
buzbee52a77fc2012-11-20 19:50:46 -0800903 LoadValueDirectFixed(cUnit, rlThis, regThis);
904 LoadValueDirectFixed(cUnit, rlCmp, regCmp);
buzbeeb046e162012-10-30 15:48:42 -0700905 int rTgt = (cUnit->instructionSet != kX86) ?
buzbee52a77fc2012-11-20 19:50:46 -0800906 LoadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo)) : 0;
907 GenNullCheck(cUnit, rlThis.sRegLow, regThis, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 //TUNING: check if rlCmp.sRegLow is already null checked
buzbee52a77fc2012-11-20 19:50:46 -0800909 LIR* launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
910 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads, reinterpret_cast<uintptr_t>(launchPad));
911 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800914 OpReg(cUnit, kOpBlx, rTgt);
buzbeeb046e162012-10-30 15:48:42 -0700915 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800916 OpThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
buzbeeb046e162012-10-30 15:48:42 -0700917 }
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);
buzbee52a77fc2012-11-20 19:50:46 -0800921 RegLocation rlReturn = GetReturn(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
buzbee52a77fc2012-11-20 19:50:46 -0800927bool 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)") {
buzbee52a77fc2012-11-20 19:50:46 -0800945 return GenInlinedDoubleCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700946 }
947 if (tgtMethod == "double java.lang.Double.longBitsToDouble(long)") {
buzbee52a77fc2012-11-20 19:50:46 -0800948 return GenInlinedDoubleCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700949 }
950 if (tgtMethod == "int java.lang.Float.floatToRawIntBits(float)") {
buzbee52a77fc2012-11-20 19:50:46 -0800951 return GenInlinedFloatCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700952 }
953 if (tgtMethod == "float java.lang.Float.intBitsToFloat(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800954 return GenInlinedFloatCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700955 }
956 if (tgtMethod == "int java.lang.Math.abs(int)" ||
957 tgtMethod == "int java.lang.StrictMath.abs(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800958 return GenInlinedAbsInt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700959 }
960 if (tgtMethod == "long java.lang.Math.abs(long)" ||
961 tgtMethod == "long java.lang.StrictMath.abs(long)") {
buzbee52a77fc2012-11-20 19:50:46 -0800962 return GenInlinedAbsLong(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700963 }
964 if (tgtMethod == "int java.lang.Math.max(int, int)" ||
965 tgtMethod == "int java.lang.StrictMath.max(int, int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800966 return GenInlinedMinMaxInt(cUnit, info, false /* isMin */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700967 }
968 if (tgtMethod == "int java.lang.Math.min(int, int)" ||
969 tgtMethod == "int java.lang.StrictMath.min(int, int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800970 return GenInlinedMinMaxInt(cUnit, info, true /* isMin */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700971 }
972 if (tgtMethod == "double java.lang.Math.sqrt(double)" ||
973 tgtMethod == "double java.lang.StrictMath.sqrt(double)") {
buzbee52a77fc2012-11-20 19:50:46 -0800974 return GenInlinedSqrt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700975 }
976 if (tgtMethod == "char java.lang.String.charAt(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800977 return GenInlinedCharAt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700978 }
979 if (tgtMethod == "int java.lang.String.compareTo(java.lang.String)") {
buzbee52a77fc2012-11-20 19:50:46 -0800980 return GenInlinedStringCompareTo(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700981 }
982 if (tgtMethod == "boolean java.lang.String.isEmpty()") {
buzbee52a77fc2012-11-20 19:50:46 -0800983 return GenInlinedStringIsEmptyOrLength(cUnit, info, true /* isEmpty */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700984 }
985 if (tgtMethod == "int java.lang.String.indexOf(int, int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800986 return GenInlinedIndexOf(cUnit, info, false /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700987 }
988 if (tgtMethod == "int java.lang.String.indexOf(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800989 return GenInlinedIndexOf(cUnit, info, true /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700990 }
991 if (tgtMethod == "int java.lang.String.length()") {
buzbee52a77fc2012-11-20 19:50:46 -0800992 return GenInlinedStringIsEmptyOrLength(cUnit, info, false /* isEmpty */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700993 }
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)") {
buzbee52a77fc2012-11-20 19:50:46 -0800996 return GenInlinedCas32(cUnit, info, false);
Ian Rogers0183dd72012-09-17 23:06:51 -0700997 }
998 if (tgtMethod == "boolean sun.misc.Unsafe.compareAndSwapObject(java.lang.Object, long, java.lang.Object, java.lang.Object)") {
buzbee52a77fc2012-11-20 19:50:46 -0800999 return GenInlinedCas32(cUnit, info, true);
Ian Rogers0183dd72012-09-17 23:06:51 -07001000 }
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
buzbee52a77fc2012-11-20 19:50:46 -08001005void GenInvoke(CompilationUnit* cUnit, CallInfo* info)
buzbee1bc37c62012-11-20 13:35:41 -08001006{
buzbee52a77fc2012-11-20 19:50:46 -08001007 if (GenIntrinsic(cUnit, info)) {
buzbee1bc37c62012-11-20 13:35:41 -08001008 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;
buzbee52a77fc2012-11-20 19:50:46 -08001015 FlushAllRegs(cUnit); /* Everything to home location */
buzbee1bc37c62012-11-20 13:35:41 -08001016 // Explicit register usage
buzbee52a77fc2012-11-20 19:50:46 -08001017 LockCallTemps(cUnit);
buzbee1bc37c62012-11-20 13:35:41 -08001018
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 }
buzbee52a77fc2012-11-20 19:50:46 -08001038 nextCallInsn = fastPath ? NextInterfaceCallInsn
1039 : NextInterfaceCallInsnWithAccessCheck;
buzbee1bc37c62012-11-20 13:35:41 -08001040 skipThis = false;
1041 } else if (info->type == kDirect) {
1042 if (fastPath) {
1043 pNullCk = &nullCk;
1044 }
buzbee52a77fc2012-11-20 19:50:46 -08001045 nextCallInsn = fastPath ? NextSDCallInsn : NextDirectCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001046 skipThis = false;
1047 } else if (info->type == kStatic) {
buzbee52a77fc2012-11-20 19:50:46 -08001048 nextCallInsn = fastPath ? NextSDCallInsn : NextStaticCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001049 skipThis = false;
1050 } else if (info->type == kSuper) {
1051 DCHECK(!fastPath); // Fast path is a direct call.
buzbee52a77fc2012-11-20 19:50:46 -08001052 nextCallInsn = NextSuperCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001053 skipThis = false;
1054 } else {
1055 DCHECK_EQ(info->type, kVirtual);
buzbee52a77fc2012-11-20 19:50:46 -08001056 nextCallInsn = fastPath ? NextVCallInsn : NextVCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001057 skipThis = fastPath;
1058 }
1059 if (!info->isRange) {
buzbee52a77fc2012-11-20 19:50:46 -08001060 callState = GenDalvikArgsNoRange(cUnit, info, callState, pNullCk,
buzbee1bc37c62012-11-20 13:35:41 -08001061 nextCallInsn, dexMethodIdx,
1062 vtableIdx, directCode, directMethod,
1063 originalType, skipThis);
1064 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001065 callState = GenDalvikArgsRange(cUnit, info, callState, pNullCk,
buzbee1bc37c62012-11-20 13:35:41 -08001066 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)) {
buzbee52a77fc2012-11-20 19:50:46 -08001077 GenShowTarget(cUnit);
buzbee1bc37c62012-11-20 13:35:41 -08001078 }
1079 LIR* callInst;
1080 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -08001081 callInst = OpReg(cUnit, kOpBlx, TargetReg(kInvokeTgt));
buzbee1bc37c62012-11-20 13:35:41 -08001082 } else {
1083 if (fastPath && info->type != kInterface) {
buzbee52a77fc2012-11-20 19:50:46 -08001084 callInst = OpMem(cUnit, kOpBlx, TargetReg(kArg0),
buzbee1bc37c62012-11-20 13:35:41 -08001085 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 }
buzbee52a77fc2012-11-20 19:50:46 -08001108 callInst = OpThreadMem(cUnit, kOpBlx, trampoline);
buzbee1bc37c62012-11-20 13:35:41 -08001109 }
1110 }
buzbee52a77fc2012-11-20 19:50:46 -08001111 MarkSafepointPC(cUnit, callInst);
buzbee1bc37c62012-11-20 13:35:41 -08001112
buzbee52a77fc2012-11-20 19:50:46 -08001113 ClobberCalleeSave(cUnit);
buzbee1bc37c62012-11-20 13:35:41 -08001114 if (info->result.location != kLocInvalid) {
1115 // We have a following MOVE_RESULT - do it now.
1116 if (info->result.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08001117 RegLocation retLoc = GetReturnWide(cUnit, info->result.fp);
1118 StoreValueWide(cUnit, info->result, retLoc);
buzbee1bc37c62012-11-20 13:35:41 -08001119 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001120 RegLocation retLoc = GetReturn(cUnit, info->result.fp);
1121 StoreValue(cUnit, info->result, retLoc);
buzbee1bc37c62012-11-20 13:35:41 -08001122 }
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 */
buzbee52a77fc2012-11-20 19:50:46 -08001132CallInfo* NewMemCallInfo(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
buzbee1bc37c62012-11-20 13:35:41 -08001133 InvokeType type, bool isRange)
1134{
buzbee52a77fc2012-11-20 19:50:46 -08001135 CallInfo* info = static_cast<CallInfo*>(NewMem(cUnit, sizeof(CallInfo), true, kAllocMisc));
1136 MIR* moveResultMIR = FindMoveResult(cUnit, bb, mir);
buzbee1bc37c62012-11-20 13:35:41 -08001137 if (moveResultMIR == NULL) {
1138 info->result.location = kLocInvalid;
1139 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001140 info->result = GetRawDest(cUnit, moveResultMIR);
buzbee1bc37c62012-11-20 13:35:41 -08001141 moveResultMIR->dalvikInsn.opcode = Instruction::NOP;
1142 }
1143 info->numArgWords = mir->ssaRep->numUses;
1144 info->args = (info->numArgWords == 0) ? NULL : static_cast<RegLocation*>
buzbee52a77fc2012-11-20 19:50:46 -08001145 (NewMem(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc));
buzbee1bc37c62012-11-20 13:35:41 -08001146 for (int i = 0; i < info->numArgWords; i++) {
buzbee52a77fc2012-11-20 19:50:46 -08001147 info->args[i] = GetRawSrc(cUnit, mir, i);
buzbee1bc37c62012-11-20 13:35:41 -08001148 }
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