blob: bc55800d9ac455bc298c8a16bd9ae2c91fb57864 [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
113/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700114 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800115 * emit the next instruction in static & direct invoke sequences.
116 */
buzbeeaad94382012-11-21 07:40:50 -0800117static int NextSDCallInsn(CompilationUnit* cUnit, CallInfo* info,
118 int state, uint32_t dexIdx, uint32_t unused,
119 uintptr_t directCode, uintptr_t directMethod,
120 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800121{
buzbeeb046e162012-10-30 15:48:42 -0700122 if (cUnit->instructionSet != kThumb2) {
123 // Disable sharpening
124 directCode = 0;
125 directMethod = 0;
126 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 if (directCode != 0 && directMethod != 0) {
128 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800129 case 0: // Get the current Method* [sets kArg0]
buzbeecbd6d442012-11-17 14:11:25 -0800130 if (directCode != static_cast<unsigned int>(-1)) {
buzbee52a77fc2012-11-20 19:50:46 -0800131 LoadConstant(cUnit, TargetReg(kInvokeTgt), directCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800133 LIR* dataTarget = ScanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800135 dataTarget = AddWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 dataTarget->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700137 }
buzbee52a77fc2012-11-20 19:50:46 -0800138 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kInvokeTgt), dataTarget);
139 AppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800140 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 }
buzbeecbd6d442012-11-17 14:11:25 -0800142 if (directMethod != static_cast<unsigned int>(-1)) {
buzbee52a77fc2012-11-20 19:50:46 -0800143 LoadConstant(cUnit, TargetReg(kArg0), directMethod);
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800145 LIR* dataTarget = ScanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700146 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800147 dataTarget = AddWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 dataTarget->operands[1] = type;
149 }
buzbee52a77fc2012-11-20 19:50:46 -0800150 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kArg0), dataTarget);
151 AppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800152 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 }
154 break;
155 default:
156 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800157 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 } else {
159 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800160 case 0: // Get the current Method* [sets kArg0]
Ian Rogers137e88f2012-10-08 17:46:47 -0700161 // TUNING: we can save a reg copy if Method* has been promoted.
buzbee52a77fc2012-11-20 19:50:46 -0800162 LoadCurrMethodDirect(cUnit, TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 break;
164 case 1: // Get method->dex_cache_resolved_methods_
buzbee52a77fc2012-11-20 19:50:46 -0800165 LoadWordDisp(cUnit, TargetReg(kArg0),
166 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(), TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 // Set up direct code if known.
168 if (directCode != 0) {
buzbeecbd6d442012-11-17 14:11:25 -0800169 if (directCode != static_cast<unsigned int>(-1)) {
buzbee52a77fc2012-11-20 19:50:46 -0800170 LoadConstant(cUnit, TargetReg(kInvokeTgt), directCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800172 LIR* dataTarget = ScanLiteralPool(cUnit->codeLiteralList, dexIdx, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800174 dataTarget = AddWordData(cUnit, &cUnit->codeLiteralList, dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 dataTarget->operands[1] = type;
176 }
buzbee52a77fc2012-11-20 19:50:46 -0800177 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kInvokeTgt), dataTarget);
178 AppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800179 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 }
181 }
182 break;
183 case 2: // Grab target method*
buzbee52a77fc2012-11-20 19:50:46 -0800184 LoadWordDisp(cUnit, TargetReg(kArg0),
185 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4, TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 case 3: // Grab the code from the method*
buzbeeb046e162012-10-30 15:48:42 -0700188 if (cUnit->instructionSet != kX86) {
189 if (directCode == 0) {
buzbee52a77fc2012-11-20 19:50:46 -0800190 LoadWordDisp(cUnit, TargetReg(kArg0), AbstractMethod::GetCodeOffset().Int32Value(),
191 TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700192 }
193 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 }
buzbeeb046e162012-10-30 15:48:42 -0700195 // Intentional fallthrough for x86
Bill Buzbeea114add2012-05-03 15:00:40 -0700196 default:
197 return -1;
198 }
199 }
200 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800201}
202
203/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700204 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800205 * emit the next instruction in a virtual invoke sequence.
buzbeef0504cd2012-11-13 16:31:10 -0800206 * We can use kLr as a temp prior to target address loading
buzbee31a4a6f2012-02-28 15:36:15 -0800207 * Note also that we'll load the first argument ("this") into
buzbee52a77fc2012-11-20 19:50:46 -0800208 * kArg1 here rather than the standard LoadArgRegs.
buzbee31a4a6f2012-02-28 15:36:15 -0800209 */
buzbeeaad94382012-11-21 07:40:50 -0800210static int NextVCallInsn(CompilationUnit* cUnit, CallInfo* info,
211 int state, uint32_t dexIdx, uint32_t methodIdx,
212 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800213{
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 /*
215 * This is the fast path in which the target virtual method is
216 * fully resolved at compile time.
217 */
218 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800219 case 0: { // Get "this" [set kArg1]
Ian Rogers137e88f2012-10-08 17:46:47 -0700220 RegLocation rlArg = info->args[0];
buzbee52a77fc2012-11-20 19:50:46 -0800221 LoadValueDirectFixed(cUnit, rlArg, TargetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 break;
Ian Rogers137e88f2012-10-08 17:46:47 -0700223 }
buzbeef0504cd2012-11-13 16:31:10 -0800224 case 1: // Is "this" null? [use kArg1]
buzbee52a77fc2012-11-20 19:50:46 -0800225 GenNullCheck(cUnit, info->args[0].sRegLow, TargetReg(kArg1), info->optFlags);
buzbeef0504cd2012-11-13 16:31:10 -0800226 // get this->klass_ [use kArg1, set kInvokeTgt]
buzbee52a77fc2012-11-20 19:50:46 -0800227 LoadWordDisp(cUnit, TargetReg(kArg1), Object::ClassOffset().Int32Value(),
228 TargetReg(kInvokeTgt));
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 break;
buzbeef0504cd2012-11-13 16:31:10 -0800230 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
buzbee52a77fc2012-11-20 19:50:46 -0800231 LoadWordDisp(cUnit, TargetReg(kInvokeTgt), Class::VTableOffset().Int32Value(),
232 TargetReg(kInvokeTgt));
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 break;
buzbeef0504cd2012-11-13 16:31:10 -0800234 case 3: // Get target method [use kInvokeTgt, set kArg0]
buzbee52a77fc2012-11-20 19:50:46 -0800235 LoadWordDisp(cUnit, TargetReg(kInvokeTgt), (methodIdx * 4) +
236 Array::DataOffset(sizeof(Object*)).Int32Value(), TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 break;
buzbeef0504cd2012-11-13 16:31:10 -0800238 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
buzbeeb046e162012-10-30 15:48:42 -0700239 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -0800240 LoadWordDisp(cUnit, TargetReg(kArg0), AbstractMethod::GetCodeOffset().Int32Value(),
241 TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700242 break;
243 }
244 // Intentional fallthrough for X86
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 default:
246 return -1;
247 }
248 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800249}
250
Ian Rogers137e88f2012-10-08 17:46:47 -0700251/*
252 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
253 * which will locate the target and continue on via a tail call.
254 */
buzbeeaad94382012-11-21 07:40:50 -0800255static int NextInterfaceCallInsn(CompilationUnit* cUnit, CallInfo* info, int state,
256 uint32_t dexIdx, uint32_t unused, uintptr_t unused2,
257 uintptr_t directMethod, InvokeType unused4)
Ian Rogers137e88f2012-10-08 17:46:47 -0700258{
buzbeeb046e162012-10-30 15:48:42 -0700259 if (cUnit->instructionSet != kThumb2) {
260 // Disable sharpening
261 directMethod = 0;
262 }
263 int trampoline = (cUnit->instructionSet == kX86) ? 0
264 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
Ian Rogers137e88f2012-10-08 17:46:47 -0700265
266 if (directMethod != 0) {
267 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800268 case 0: // Load the trampoline target [sets kInvokeTgt].
buzbeeb046e162012-10-30 15:48:42 -0700269 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -0800270 LoadWordDisp(cUnit, TargetReg(kSelf), trampoline, TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700271 }
buzbeef0504cd2012-11-13 16:31:10 -0800272 // Get the interface Method* [sets kArg0]
buzbeecbd6d442012-11-17 14:11:25 -0800273 if (directMethod != static_cast<unsigned int>(-1)) {
buzbee52a77fc2012-11-20 19:50:46 -0800274 LoadConstant(cUnit, TargetReg(kArg0), directMethod);
Ian Rogers137e88f2012-10-08 17:46:47 -0700275 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800276 LIR* dataTarget = ScanLiteralPool(cUnit->methodLiteralList, dexIdx, 0);
Ian Rogers137e88f2012-10-08 17:46:47 -0700277 if (dataTarget == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800278 dataTarget = AddWordData(cUnit, &cUnit->methodLiteralList, dexIdx);
Ian Rogers137e88f2012-10-08 17:46:47 -0700279 dataTarget->operands[1] = kInterface;
280 }
buzbee52a77fc2012-11-20 19:50:46 -0800281 LIR* loadPcRel = OpPcRelLoad(cUnit, TargetReg(kArg0), dataTarget);
282 AppendLIR(cUnit, loadPcRel);
buzbeecbd6d442012-11-17 14:11:25 -0800283 DCHECK_EQ(cUnit->instructionSet, kThumb2) << reinterpret_cast<void*>(dataTarget);
Ian Rogers137e88f2012-10-08 17:46:47 -0700284 }
285 break;
286 default:
287 return -1;
288 }
289 } else {
290 switch (state) {
291 case 0:
buzbeef0504cd2012-11-13 16:31:10 -0800292 // Get the current Method* [sets kArg0] - TUNING: remove copy of method if it is promoted.
buzbee52a77fc2012-11-20 19:50:46 -0800293 LoadCurrMethodDirect(cUnit, TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800294 // Load the trampoline target [sets kInvokeTgt].
buzbeeb046e162012-10-30 15:48:42 -0700295 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -0800296 LoadWordDisp(cUnit, TargetReg(kSelf), trampoline, TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700297 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700298 break;
buzbeef0504cd2012-11-13 16:31:10 -0800299 case 1: // Get method->dex_cache_resolved_methods_ [set/use kArg0]
buzbee52a77fc2012-11-20 19:50:46 -0800300 LoadWordDisp(cUnit, TargetReg(kArg0),
Ian Rogers137e88f2012-10-08 17:46:47 -0700301 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(),
buzbee52a77fc2012-11-20 19:50:46 -0800302 TargetReg(kArg0));
Ian Rogers137e88f2012-10-08 17:46:47 -0700303 break;
buzbeef0504cd2012-11-13 16:31:10 -0800304 case 2: // Grab target method* [set/use kArg0]
buzbee52a77fc2012-11-20 19:50:46 -0800305 LoadWordDisp(cUnit, TargetReg(kArg0),
Ian Rogers137e88f2012-10-08 17:46:47 -0700306 Array::DataOffset(sizeof(Object*)).Int32Value() + dexIdx * 4,
buzbee52a77fc2012-11-20 19:50:46 -0800307 TargetReg(kArg0));
Ian Rogers137e88f2012-10-08 17:46:47 -0700308 break;
309 default:
310 return -1;
311 }
312 }
313 return state + 1;
314}
315
buzbeeaad94382012-11-21 07:40:50 -0800316static int NextInvokeInsnSP(CompilationUnit* cUnit, CallInfo* info, int trampoline,
317 int state, uint32_t dexIdx, uint32_t methodIdx)
buzbee31a4a6f2012-02-28 15:36:15 -0800318{
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 /*
320 * This handles the case in which the base method is not fully
321 * resolved at compile time, we bail to a runtime helper.
322 */
323 if (state == 0) {
buzbeeb046e162012-10-30 15:48:42 -0700324 if (cUnit->instructionSet != kX86) {
325 // Load trampoline target
buzbee52a77fc2012-11-20 19:50:46 -0800326 LoadWordDisp(cUnit, TargetReg(kSelf), trampoline, TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700327 }
buzbeef0504cd2012-11-13 16:31:10 -0800328 // Load kArg0 with method index
buzbee52a77fc2012-11-20 19:50:46 -0800329 LoadConstant(cUnit, TargetReg(kArg0), dexIdx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 return 1;
331 }
332 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800333}
334
buzbeeaad94382012-11-21 07:40:50 -0800335static int NextStaticCallInsnSP(CompilationUnit* cUnit, CallInfo* info,
336 int state, uint32_t dexIdx, uint32_t methodIdx,
337 uintptr_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700338 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800339{
Ian Rogers57b86d42012-03-27 16:05:41 -0700340 int trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
buzbee52a77fc2012-11-20 19:50:46 -0800341 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800342}
343
buzbeeaad94382012-11-21 07:40:50 -0800344static int NextDirectCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
345 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
346 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800347{
Ian Rogers57b86d42012-03-27 16:05:41 -0700348 int trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
buzbee52a77fc2012-11-20 19:50:46 -0800349 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800350}
351
buzbeeaad94382012-11-21 07:40:50 -0800352static int NextSuperCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
353 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700354 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800355{
Ian Rogers57b86d42012-03-27 16:05:41 -0700356 int trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
buzbee52a77fc2012-11-20 19:50:46 -0800357 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800358}
359
buzbeeaad94382012-11-21 07:40:50 -0800360static int NextVCallInsnSP(CompilationUnit* cUnit, CallInfo* info, int state,
361 uint32_t dexIdx, uint32_t methodIdx, uintptr_t unused,
362 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800363{
Ian Rogers57b86d42012-03-27 16:05:41 -0700364 int trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
buzbee52a77fc2012-11-20 19:50:46 -0800365 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800366}
367
buzbeeaad94382012-11-21 07:40:50 -0800368static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cUnit,
369 CallInfo* info, int state,
buzbee15bf9802012-06-12 17:49:27 -0700370 uint32_t dexIdx, uint32_t unused,
371 uintptr_t unused2, uintptr_t unused3,
372 InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800373{
Ian Rogers57b86d42012-03-27 16:05:41 -0700374 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
buzbee52a77fc2012-11-20 19:50:46 -0800375 return NextInvokeInsnSP(cUnit, info, trampoline, state, dexIdx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800376}
377
buzbeeaad94382012-11-21 07:40:50 -0800378static int LoadArgRegs(CompilationUnit* cUnit, CallInfo* info, int callState,
379 NextCallInsn nextCallInsn, uint32_t dexIdx,
380 uint32_t methodIdx, uintptr_t directCode,
381 uintptr_t directMethod, InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800382{
buzbee52a77fc2012-11-20 19:50:46 -0800383 int lastArgReg = TargetReg(kArg3);
384 int nextReg = TargetReg(kArg1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 int nextArg = 0;
386 if (skipThis) {
387 nextReg++;
388 nextArg++;
389 }
buzbee15bf9802012-06-12 17:49:27 -0700390 for (; (nextReg <= lastArgReg) && (nextArg < info->numArgWords); nextReg++) {
391 RegLocation rlArg = info->args[nextArg++];
buzbee52a77fc2012-11-20 19:50:46 -0800392 rlArg = UpdateRawLoc(cUnit, rlArg);
393 if (rlArg.wide && (nextReg <= TargetReg(kArg2))) {
394 LoadValueDirectWideFixed(cUnit, rlArg, nextReg, nextReg + 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700395 nextReg++;
396 nextArg++;
397 } else {
398 rlArg.wide = false;
buzbee52a77fc2012-11-20 19:50:46 -0800399 LoadValueDirectFixed(cUnit, rlArg, nextReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800400 }
buzbee15bf9802012-06-12 17:49:27 -0700401 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 directCode, directMethod, type);
403 }
404 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800405}
406
407/*
408 * Load up to 5 arguments, the first three of which will be in
buzbeef0504cd2012-11-13 16:31:10 -0800409 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
buzbee31a4a6f2012-02-28 15:36:15 -0800410 * and as part of the load sequence, it must be replaced with
411 * the target method pointer. Note, this may also be called
412 * for "range" variants if the number of arguments is 5 or fewer.
413 */
buzbee52a77fc2012-11-20 19:50:46 -0800414int GenDalvikArgsNoRange(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700415 int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800416 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700417 uint32_t dexIdx, uint32_t methodIdx,
418 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700419 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800420{
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 RegLocation rlArg;
buzbee31a4a6f2012-02-28 15:36:15 -0800422
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 /* If no arguments, just return */
buzbee15bf9802012-06-12 17:49:27 -0700424 if (info->numArgWords == 0)
buzbee31a4a6f2012-02-28 15:36:15 -0800425 return callState;
Bill Buzbeea114add2012-05-03 15:00:40 -0700426
buzbee15bf9802012-06-12 17:49:27 -0700427 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 directCode, directMethod, type);
429
buzbee15bf9802012-06-12 17:49:27 -0700430 DCHECK_LE(info->numArgWords, 5);
431 if (info->numArgWords > 3) {
432 int32_t nextUse = 3;
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 //Detect special case of wide arg spanning arg3/arg4
buzbee15bf9802012-06-12 17:49:27 -0700434 RegLocation rlUse0 = info->args[0];
435 RegLocation rlUse1 = info->args[1];
436 RegLocation rlUse2 = info->args[2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 if (((!rlUse0.wide && !rlUse1.wide) || rlUse0.wide) &&
438 rlUse2.wide) {
439 int reg = -1;
440 // Wide spans, we need the 2nd half of uses[2].
buzbee52a77fc2012-11-20 19:50:46 -0800441 rlArg = UpdateLocWide(cUnit, rlUse2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700442 if (rlArg.location == kLocPhysReg) {
443 reg = rlArg.highReg;
444 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800445 // kArg2 & rArg3 can safely be used here
buzbee52a77fc2012-11-20 19:50:46 -0800446 reg = TargetReg(kArg3);
447 LoadWordDisp(cUnit, TargetReg(kSp), SRegOffset(cUnit, rlArg.sRegLow) + 4, reg);
buzbee15bf9802012-06-12 17:49:27 -0700448 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 methodIdx, directCode, directMethod, type);
450 }
buzbee52a77fc2012-11-20 19:50:46 -0800451 StoreBaseDisp(cUnit, TargetReg(kSp), (nextUse + 1) * 4, reg, kWord);
452 StoreBaseDisp(cUnit, TargetReg(kSp), 16 /* (3+1)*4 */, reg, kWord);
buzbee15bf9802012-06-12 17:49:27 -0700453 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 directCode, directMethod, type);
455 nextUse++;
456 }
457 // Loop through the rest
buzbee15bf9802012-06-12 17:49:27 -0700458 while (nextUse < info->numArgWords) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 int lowReg;
460 int highReg = -1;
buzbee15bf9802012-06-12 17:49:27 -0700461 rlArg = info->args[nextUse];
buzbee52a77fc2012-11-20 19:50:46 -0800462 rlArg = UpdateRawLoc(cUnit, rlArg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 if (rlArg.location == kLocPhysReg) {
464 lowReg = rlArg.lowReg;
465 highReg = rlArg.highReg;
466 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800467 lowReg = TargetReg(kArg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700468 if (rlArg.wide) {
buzbee52a77fc2012-11-20 19:50:46 -0800469 highReg = TargetReg(kArg3);
470 LoadValueDirectWideFixed(cUnit, rlArg, lowReg, highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800472 LoadValueDirectFixed(cUnit, rlArg, lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700473 }
buzbee15bf9802012-06-12 17:49:27 -0700474 callState = nextCallInsn(cUnit, info, callState, dexIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 methodIdx, directCode, directMethod, type);
476 }
477 int outsOffset = (nextUse + 1) * 4;
478 if (rlArg.wide) {
buzbee52a77fc2012-11-20 19:50:46 -0800479 StoreBaseDispWide(cUnit, TargetReg(kSp), outsOffset, lowReg, highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 nextUse += 2;
481 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800482 StoreWordDisp(cUnit, TargetReg(kSp), outsOffset, lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 nextUse++;
484 }
buzbee15bf9802012-06-12 17:49:27 -0700485 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 directCode, directMethod, type);
487 }
488 }
489
buzbee52a77fc2012-11-20 19:50:46 -0800490 callState = LoadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 dexIdx, methodIdx, directCode, directMethod,
492 type, skipThis);
493
494 if (pcrLabel) {
buzbee52a77fc2012-11-20 19:50:46 -0800495 *pcrLabel = GenNullCheck(cUnit, info->args[0].sRegLow, TargetReg(kArg1), info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700496 }
497 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800498}
499
500/*
501 * May have 0+ arguments (also used for jumbo). Note that
502 * source virtual registers may be in physical registers, so may
503 * need to be flushed to home location before copying. This
504 * applies to arg3 and above (see below).
505 *
506 * Two general strategies:
507 * If < 20 arguments
508 * Pass args 3-18 using vldm/vstm block copy
buzbeef0504cd2012-11-13 16:31:10 -0800509 * Pass arg0, arg1 & arg2 in kArg1-kArg3
buzbee31a4a6f2012-02-28 15:36:15 -0800510 * If 20+ arguments
511 * Pass args arg19+ using memcpy block copy
buzbeef0504cd2012-11-13 16:31:10 -0800512 * Pass arg0, arg1 & arg2 in kArg1-kArg3
buzbee31a4a6f2012-02-28 15:36:15 -0800513 *
514 */
buzbee52a77fc2012-11-20 19:50:46 -0800515int GenDalvikArgsRange(CompilationUnit* cUnit, CallInfo* info, int callState,
buzbee31a4a6f2012-02-28 15:36:15 -0800516 LIR** pcrLabel, NextCallInsn nextCallInsn,
Ian Rogers2ed3b952012-03-17 11:49:39 -0700517 uint32_t dexIdx, uint32_t methodIdx,
518 uintptr_t directCode, uintptr_t directMethod,
Brian Carlstromf5822582012-03-19 22:34:31 -0700519 InvokeType type, bool skipThis)
buzbee31a4a6f2012-02-28 15:36:15 -0800520{
buzbee31a4a6f2012-02-28 15:36:15 -0800521
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 // If we can treat it as non-range (Jumbo ops will use range form)
buzbee15bf9802012-06-12 17:49:27 -0700523 if (info->numArgWords <= 5)
buzbee52a77fc2012-11-20 19:50:46 -0800524 return GenDalvikArgsNoRange(cUnit, info, callState, pcrLabel,
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 nextCallInsn, dexIdx, methodIdx,
526 directCode, directMethod, type, skipThis);
527 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 * First load the non-register arguments. Both forms expect all
529 * of the source arguments to be in their home frame location, so
530 * scan the sReg names and flush any that have been promoted to
531 * frame backing storage.
532 */
533 // Scan the rest of the args - if in physReg flush to memory
buzbee15bf9802012-06-12 17:49:27 -0700534 for (int nextArg = 0; nextArg < info->numArgWords;) {
535 RegLocation loc = info->args[nextArg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700536 if (loc.wide) {
buzbee52a77fc2012-11-20 19:50:46 -0800537 loc = UpdateLocWide(cUnit, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 if ((nextArg >= 2) && (loc.location == kLocPhysReg)) {
buzbee52a77fc2012-11-20 19:50:46 -0800539 StoreBaseDispWide(cUnit, TargetReg(kSp), SRegOffset(cUnit, loc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700540 loc.lowReg, loc.highReg);
541 }
542 nextArg += 2;
543 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800544 loc = UpdateLoc(cUnit, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 if ((nextArg >= 3) && (loc.location == kLocPhysReg)) {
buzbee52a77fc2012-11-20 19:50:46 -0800546 StoreBaseDisp(cUnit, TargetReg(kSp), SRegOffset(cUnit, loc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700547 loc.lowReg, kWord);
548 }
549 nextArg++;
buzbee31a4a6f2012-02-28 15:36:15 -0800550 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 }
buzbee31a4a6f2012-02-28 15:36:15 -0800552
buzbee52a77fc2012-11-20 19:50:46 -0800553 int startOffset = SRegOffset(cUnit, info->args[3].sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700554 int outsOffset = 4 /* Method* */ + (3 * 4);
buzbeeb046e162012-10-30 15:48:42 -0700555 if (cUnit->instructionSet != kThumb2) {
buzbee31a4a6f2012-02-28 15:36:15 -0800556 // Generate memcpy
buzbee52a77fc2012-11-20 19:50:46 -0800557 OpRegRegImm(cUnit, kOpAdd, TargetReg(kArg0), TargetReg(kSp), outsOffset);
558 OpRegRegImm(cUnit, kOpAdd, TargetReg(kArg1), TargetReg(kSp), startOffset);
559 CallRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
560 TargetReg(kArg1), (info->numArgWords - 3) * 4, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 } else {
buzbeeb046e162012-10-30 15:48:42 -0700562 if (info->numArgWords >= 20) {
563 // Generate memcpy
buzbee52a77fc2012-11-20 19:50:46 -0800564 OpRegRegImm(cUnit, kOpAdd, TargetReg(kArg0), TargetReg(kSp), outsOffset);
565 OpRegRegImm(cUnit, kOpAdd, TargetReg(kArg1), TargetReg(kSp), startOffset);
566 CallRuntimeHelperRegRegImm(cUnit, ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
567 TargetReg(kArg1), (info->numArgWords - 3) * 4, false);
buzbeeb046e162012-10-30 15:48:42 -0700568 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800569 // Use vldm/vstm pair using kArg3 as a temp
buzbeeb046e162012-10-30 15:48:42 -0700570 int regsLeft = std::min(info->numArgWords - 3, 16);
571 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
572 directCode, directMethod, type);
buzbee52a77fc2012-11-20 19:50:46 -0800573 OpRegRegImm(cUnit, kOpAdd, TargetReg(kArg3), TargetReg(kSp), startOffset);
574 LIR* ld = OpVldm(cUnit, TargetReg(kArg3), regsLeft);
buzbeeb046e162012-10-30 15:48:42 -0700575 //TUNING: loosen barrier
576 ld->defMask = ENCODE_ALL;
buzbee52a77fc2012-11-20 19:50:46 -0800577 SetMemRefType(ld, true /* isLoad */, kDalvikReg);
buzbeeb046e162012-10-30 15:48:42 -0700578 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
579 directCode, directMethod, type);
buzbee52a77fc2012-11-20 19:50:46 -0800580 OpRegRegImm(cUnit, kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
buzbeeb046e162012-10-30 15:48:42 -0700581 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
582 directCode, directMethod, type);
buzbee52a77fc2012-11-20 19:50:46 -0800583 LIR* st = OpVstm(cUnit, TargetReg(kArg3), regsLeft);
584 SetMemRefType(st, false /* isLoad */, kDalvikReg);
buzbeeb046e162012-10-30 15:48:42 -0700585 st->defMask = ENCODE_ALL;
586 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
587 directCode, directMethod, type);
588 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700589 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700590
buzbee52a77fc2012-11-20 19:50:46 -0800591 callState = LoadArgRegs(cUnit, info, callState, nextCallInsn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 dexIdx, methodIdx, directCode, directMethod,
593 type, skipThis);
594
buzbee15bf9802012-06-12 17:49:27 -0700595 callState = nextCallInsn(cUnit, info, callState, dexIdx, methodIdx,
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 directCode, directMethod, type);
597 if (pcrLabel) {
buzbee52a77fc2012-11-20 19:50:46 -0800598 *pcrLabel = GenNullCheck(cUnit, info->args[0].sRegLow, TargetReg(kArg1),
buzbee15bf9802012-06-12 17:49:27 -0700599 info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 }
601 return callState;
buzbee31a4a6f2012-02-28 15:36:15 -0800602}
603
buzbee52a77fc2012-11-20 19:50:46 -0800604RegLocation InlineTarget(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700605{
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700607 if (info->result.location == kLocInvalid) {
buzbee52a77fc2012-11-20 19:50:46 -0800608 res = GetReturn(cUnit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 } else {
buzbee15bf9802012-06-12 17:49:27 -0700610 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700611 }
612 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700613}
614
buzbee52a77fc2012-11-20 19:50:46 -0800615RegLocation InlineTargetWide(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700616{
Bill Buzbeea114add2012-05-03 15:00:40 -0700617 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700618 if (info->result.location == kLocInvalid) {
buzbee52a77fc2012-11-20 19:50:46 -0800619 res = GetReturnWide(cUnit, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700620 } else {
buzbee15bf9802012-06-12 17:49:27 -0700621 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 }
623 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700624}
625
buzbee52a77fc2012-11-20 19:50:46 -0800626bool GenInlinedCharAt(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700627{
buzbeeb046e162012-10-30 15:48:42 -0700628 if (cUnit->instructionSet == kMips) {
629 // TODO - add Mips implementation
630 return false;
631 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700632 // Location of reference to data array
633 int valueOffset = String::ValueOffset().Int32Value();
634 // Location of count
635 int countOffset = String::CountOffset().Int32Value();
636 // Starting offset within data array
637 int offsetOffset = String::OffsetOffset().Int32Value();
638 // Start of char data with array_
639 int dataOffset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
buzbeefc9e6fa2012-03-23 15:14:29 -0700640
buzbee15bf9802012-06-12 17:49:27 -0700641 RegLocation rlObj = info->args[0];
642 RegLocation rlIdx = info->args[1];
buzbee52a77fc2012-11-20 19:50:46 -0800643 rlObj = LoadValue(cUnit, rlObj, kCoreReg);
644 rlIdx = LoadValue(cUnit, rlIdx, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 int regMax;
buzbee52a77fc2012-11-20 19:50:46 -0800646 GenNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
buzbee15bf9802012-06-12 17:49:27 -0700647 bool rangeCheck = (!(info->optFlags & MIR_IGNORE_RANGE_CHECK));
jeffhao634ea282012-08-10 13:04:01 -0700648 LIR* launchPad = NULL;
buzbeeb046e162012-10-30 15:48:42 -0700649 int regOff = INVALID_REG;
650 int regPtr = INVALID_REG;
651 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -0800652 regOff = AllocTemp(cUnit);
653 regPtr = AllocTemp(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700654 if (rangeCheck) {
buzbee52a77fc2012-11-20 19:50:46 -0800655 regMax = AllocTemp(cUnit);
656 LoadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
buzbeeb046e162012-10-30 15:48:42 -0700657 }
buzbee52a77fc2012-11-20 19:50:46 -0800658 LoadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
659 LoadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
buzbeeb046e162012-10-30 15:48:42 -0700660 if (rangeCheck) {
661 // Set up a launch pad to allow retry in case of bounds violation */
buzbee52a77fc2012-11-20 19:50:46 -0800662 launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
663 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
buzbeecbd6d442012-11-17 14:11:25 -0800664 reinterpret_cast<uintptr_t>(launchPad));
buzbee52a77fc2012-11-20 19:50:46 -0800665 OpRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
666 FreeTemp(cUnit, regMax);
667 OpCondBranch(cUnit, kCondCs, launchPad);
buzbeeb046e162012-10-30 15:48:42 -0700668 }
669 } else {
670 if (rangeCheck) {
buzbee52a77fc2012-11-20 19:50:46 -0800671 regMax = AllocTemp(cUnit);
672 LoadWordDisp(cUnit, rlObj.lowReg, countOffset, regMax);
buzbeeb046e162012-10-30 15:48:42 -0700673 // Set up a launch pad to allow retry in case of bounds violation */
buzbee52a77fc2012-11-20 19:50:46 -0800674 launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
675 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads,
buzbeecbd6d442012-11-17 14:11:25 -0800676 reinterpret_cast<uintptr_t>(launchPad));
buzbee52a77fc2012-11-20 19:50:46 -0800677 OpRegReg(cUnit, kOpCmp, rlIdx.lowReg, regMax);
678 FreeTemp(cUnit, regMax);
679 OpCondBranch(cUnit, kCondCc, launchPad);
buzbeeb046e162012-10-30 15:48:42 -0700680 }
buzbee52a77fc2012-11-20 19:50:46 -0800681 regOff = AllocTemp(cUnit);
682 regPtr = AllocTemp(cUnit);
683 LoadWordDisp(cUnit, rlObj.lowReg, offsetOffset, regOff);
684 LoadWordDisp(cUnit, rlObj.lowReg, valueOffset, regPtr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 }
buzbee52a77fc2012-11-20 19:50:46 -0800686 OpRegImm(cUnit, kOpAdd, regPtr, dataOffset);
687 OpRegReg(cUnit, kOpAdd, regOff, rlIdx.lowReg);
688 FreeTemp(cUnit, rlObj.lowReg);
689 FreeTemp(cUnit, rlIdx.lowReg);
690 RegLocation rlDest = InlineTarget(cUnit, info);
691 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
692 LoadBaseIndexed(cUnit, regPtr, regOff, rlResult.lowReg, 1, kUnsignedHalf);
693 FreeTemp(cUnit, regOff);
694 FreeTemp(cUnit, regPtr);
695 StoreValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -0700696 if (rangeCheck) {
Elliott Hughes60234562012-06-01 12:25:59 -0700697 launchPad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 }
699 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700700 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700702}
703
704// Generates an inlined String.isEmpty or String.length.
buzbee52a77fc2012-11-20 19:50:46 -0800705bool GenInlinedStringIsEmptyOrLength(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700706 bool isEmpty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700707{
buzbeeb046e162012-10-30 15:48:42 -0700708 if (cUnit->instructionSet == kMips) {
709 // TODO - add Mips implementation
710 return false;
711 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700712 // dst = src.length();
buzbee15bf9802012-06-12 17:49:27 -0700713 RegLocation rlObj = info->args[0];
buzbee52a77fc2012-11-20 19:50:46 -0800714 rlObj = LoadValue(cUnit, rlObj, kCoreReg);
715 RegLocation rlDest = InlineTarget(cUnit, info);
716 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
717 GenNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, info->optFlags);
718 LoadWordDisp(cUnit, rlObj.lowReg, String::CountOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700719 rlResult.lowReg);
720 if (isEmpty) {
721 // dst = (dst == 0);
buzbeeb046e162012-10-30 15:48:42 -0700722 if (cUnit->instructionSet == kThumb2) {
buzbee52a77fc2012-11-20 19:50:46 -0800723 int tReg = AllocTemp(cUnit);
724 OpRegReg(cUnit, kOpNeg, tReg, rlResult.lowReg);
725 OpRegRegReg(cUnit, kOpAdc, rlResult.lowReg, rlResult.lowReg, tReg);
buzbeeb046e162012-10-30 15:48:42 -0700726 } else {
727 DCHECK_EQ(cUnit->instructionSet, kX86);
buzbee52a77fc2012-11-20 19:50:46 -0800728 OpRegImm(cUnit, kOpSub, rlResult.lowReg, 1);
729 OpRegImm(cUnit, kOpLsr, rlResult.lowReg, 31);
buzbeeb046e162012-10-30 15:48:42 -0700730 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700731 }
buzbee52a77fc2012-11-20 19:50:46 -0800732 StoreValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -0700733 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700734}
735
buzbee52a77fc2012-11-20 19:50:46 -0800736bool GenInlinedAbsInt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700737{
buzbeeb046e162012-10-30 15:48:42 -0700738 if (cUnit->instructionSet == kMips) {
739 // TODO - add Mips implementation
740 return false;
741 }
buzbee15bf9802012-06-12 17:49:27 -0700742 RegLocation rlSrc = info->args[0];
buzbee52a77fc2012-11-20 19:50:46 -0800743 rlSrc = LoadValue(cUnit, rlSrc, kCoreReg);
744 RegLocation rlDest = InlineTarget(cUnit, info);
745 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
746 int signReg = AllocTemp(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700747 // abs(x) = y<=x>>31, (x+y)^y.
buzbee52a77fc2012-11-20 19:50:46 -0800748 OpRegRegImm(cUnit, kOpAsr, signReg, rlSrc.lowReg, 31);
749 OpRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
750 OpRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
751 StoreValue(cUnit, rlDest, rlResult);
Bill Buzbeea114add2012-05-03 15:00:40 -0700752 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700753}
754
buzbee52a77fc2012-11-20 19:50:46 -0800755bool GenInlinedAbsLong(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700756{
buzbeeb046e162012-10-30 15:48:42 -0700757 if (cUnit->instructionSet == kMips) {
758 // TODO - add Mips implementation
759 return false;
760 }
761 if (cUnit->instructionSet == kThumb2) {
762 RegLocation rlSrc = info->args[0];
buzbee52a77fc2012-11-20 19:50:46 -0800763 rlSrc = LoadValueWide(cUnit, rlSrc, kCoreReg);
764 RegLocation rlDest = InlineTargetWide(cUnit, info);
765 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
766 int signReg = AllocTemp(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700767 // abs(x) = y<=x>>31, (x+y)^y.
buzbee52a77fc2012-11-20 19:50:46 -0800768 OpRegRegImm(cUnit, kOpAsr, signReg, rlSrc.highReg, 31);
769 OpRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, signReg);
770 OpRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc.highReg, signReg);
771 OpRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
772 OpRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
773 StoreValueWide(cUnit, rlDest, rlResult);
buzbeeb046e162012-10-30 15:48:42 -0700774 return true;
775 } else {
776 DCHECK_EQ(cUnit->instructionSet, kX86);
777 // Reuse source registers to avoid running out of temps
778 RegLocation rlSrc = info->args[0];
buzbee52a77fc2012-11-20 19:50:46 -0800779 rlSrc = LoadValueWide(cUnit, rlSrc, kCoreReg);
780 RegLocation rlDest = InlineTargetWide(cUnit, info);
781 RegLocation rlResult = EvalLoc(cUnit, rlDest, kCoreReg, true);
782 OpRegCopyWide(cUnit, rlResult.lowReg, rlResult.highReg, rlSrc.lowReg, rlSrc.highReg);
783 FreeTemp(cUnit, rlSrc.lowReg);
784 FreeTemp(cUnit, rlSrc.highReg);
785 int signReg = AllocTemp(cUnit);
buzbeeb046e162012-10-30 15:48:42 -0700786 // abs(x) = y<=x>>31, (x+y)^y.
buzbee52a77fc2012-11-20 19:50:46 -0800787 OpRegRegImm(cUnit, kOpAsr, signReg, rlResult.highReg, 31);
788 OpRegReg(cUnit, kOpAdd, rlResult.lowReg, signReg);
789 OpRegReg(cUnit, kOpAdc, rlResult.highReg, signReg);
790 OpRegReg(cUnit, kOpXor, rlResult.lowReg, signReg);
791 OpRegReg(cUnit, kOpXor, rlResult.highReg, signReg);
792 StoreValueWide(cUnit, rlDest, rlResult);
buzbeeb046e162012-10-30 15:48:42 -0700793 return true;
794 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700795}
796
buzbee52a77fc2012-11-20 19:50:46 -0800797bool GenInlinedFloatCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700798{
buzbeeb046e162012-10-30 15:48:42 -0700799 if (cUnit->instructionSet == kMips) {
800 // TODO - add Mips implementation
801 return false;
802 }
buzbee15bf9802012-06-12 17:49:27 -0700803 RegLocation rlSrc = info->args[0];
buzbee52a77fc2012-11-20 19:50:46 -0800804 RegLocation rlDest = InlineTarget(cUnit, info);
805 StoreValue(cUnit, rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700806 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700807}
808
buzbee52a77fc2012-11-20 19:50:46 -0800809bool GenInlinedDoubleCvt(CompilationUnit *cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700810{
buzbeeb046e162012-10-30 15:48:42 -0700811 if (cUnit->instructionSet == kMips) {
812 // TODO - add Mips implementation
813 return false;
814 }
buzbee15bf9802012-06-12 17:49:27 -0700815 RegLocation rlSrc = info->args[0];
buzbee52a77fc2012-11-20 19:50:46 -0800816 RegLocation rlDest = InlineTargetWide(cUnit, info);
817 StoreValueWide(cUnit, rlDest, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700818 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700819}
820
821/*
822 * Fast string.indexOf(I) & (II). Tests for simple case of char <= 0xffff,
823 * otherwise bails to standard library code.
824 */
buzbee52a77fc2012-11-20 19:50:46 -0800825bool GenInlinedIndexOf(CompilationUnit* cUnit, CallInfo* info,
buzbee15bf9802012-06-12 17:49:27 -0700826 bool zeroBased)
buzbeefc9e6fa2012-03-23 15:14:29 -0700827{
buzbeeb046e162012-10-30 15:48:42 -0700828 if (cUnit->instructionSet == kMips) {
829 // TODO - add Mips implementation
830 return false;
831 }
buzbee52a77fc2012-11-20 19:50:46 -0800832 ClobberCalleeSave(cUnit);
833 LockCallTemps(cUnit); // Using fixed registers
834 int regPtr = TargetReg(kArg0);
835 int regChar = TargetReg(kArg1);
836 int regStart = TargetReg(kArg2);
buzbeefc9e6fa2012-03-23 15:14:29 -0700837
buzbee15bf9802012-06-12 17:49:27 -0700838 RegLocation rlObj = info->args[0];
839 RegLocation rlChar = info->args[1];
840 RegLocation rlStart = info->args[2];
buzbee52a77fc2012-11-20 19:50:46 -0800841 LoadValueDirectFixed(cUnit, rlObj, regPtr);
842 LoadValueDirectFixed(cUnit, rlChar, regChar);
Bill Buzbeea114add2012-05-03 15:00:40 -0700843 if (zeroBased) {
buzbee52a77fc2012-11-20 19:50:46 -0800844 LoadConstant(cUnit, regStart, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800846 LoadValueDirectFixed(cUnit, rlStart, regStart);
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 }
buzbee52a77fc2012-11-20 19:50:46 -0800848 int rTgt = (cUnit->instructionSet != kX86) ? LoadHelper(cUnit, ENTRYPOINT_OFFSET(pIndexOf)) : 0;
849 GenNullCheck(cUnit, rlObj.sRegLow, regPtr, info->optFlags);
850 LIR* launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
851 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads, reinterpret_cast<uintptr_t>(launchPad));
852 OpCmpImmBranch(cUnit, kCondGt, regChar, 0xFFFF, launchPad);
buzbee8320f382012-09-11 16:29:42 -0700853 // NOTE: not a safepoint
buzbeeb046e162012-10-30 15:48:42 -0700854 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -0800855 OpReg(cUnit, kOpBlx, rTgt);
buzbeeb046e162012-10-30 15:48:42 -0700856 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800857 OpThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
buzbeeb046e162012-10-30 15:48:42 -0700858 }
buzbee52a77fc2012-11-20 19:50:46 -0800859 LIR* resumeTgt = NewLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800860 launchPad->operands[2] = reinterpret_cast<uintptr_t>(resumeTgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700861 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700862 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
buzbee52a77fc2012-11-20 19:50:46 -0800863 RegLocation rlReturn = GetReturn(cUnit, false);
864 RegLocation rlDest = InlineTarget(cUnit, info);
865 StoreValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700866 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700867}
868
869/* Fast string.compareTo(Ljava/lang/string;)I. */
buzbee52a77fc2012-11-20 19:50:46 -0800870bool GenInlinedStringCompareTo(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700871{
buzbeeb046e162012-10-30 15:48:42 -0700872 if (cUnit->instructionSet == kMips) {
873 // TODO - add Mips implementation
874 return false;
875 }
buzbee52a77fc2012-11-20 19:50:46 -0800876 ClobberCalleeSave(cUnit);
877 LockCallTemps(cUnit); // Using fixed registers
878 int regThis = TargetReg(kArg0);
879 int regCmp = TargetReg(kArg1);
buzbeefc9e6fa2012-03-23 15:14:29 -0700880
buzbee15bf9802012-06-12 17:49:27 -0700881 RegLocation rlThis = info->args[0];
882 RegLocation rlCmp = info->args[1];
buzbee52a77fc2012-11-20 19:50:46 -0800883 LoadValueDirectFixed(cUnit, rlThis, regThis);
884 LoadValueDirectFixed(cUnit, rlCmp, regCmp);
buzbeeb046e162012-10-30 15:48:42 -0700885 int rTgt = (cUnit->instructionSet != kX86) ?
buzbee52a77fc2012-11-20 19:50:46 -0800886 LoadHelper(cUnit, ENTRYPOINT_OFFSET(pStringCompareTo)) : 0;
887 GenNullCheck(cUnit, rlThis.sRegLow, regThis, info->optFlags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700888 //TUNING: check if rlCmp.sRegLow is already null checked
buzbee52a77fc2012-11-20 19:50:46 -0800889 LIR* launchPad = RawLIR(cUnit, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
890 InsertGrowableList(cUnit, &cUnit->intrinsicLaunchpads, reinterpret_cast<uintptr_t>(launchPad));
891 OpCmpImmBranch(cUnit, kCondEq, regCmp, 0, launchPad);
buzbee8320f382012-09-11 16:29:42 -0700892 // NOTE: not a safepoint
buzbeeb046e162012-10-30 15:48:42 -0700893 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -0800894 OpReg(cUnit, kOpBlx, rTgt);
buzbeeb046e162012-10-30 15:48:42 -0700895 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800896 OpThreadMem(cUnit, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
buzbeeb046e162012-10-30 15:48:42 -0700897 }
Elliott Hughes60234562012-06-01 12:25:59 -0700898 launchPad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -0700899 // Record that we've already inlined & null checked
buzbee15bf9802012-06-12 17:49:27 -0700900 info->optFlags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
buzbee52a77fc2012-11-20 19:50:46 -0800901 RegLocation rlReturn = GetReturn(cUnit, false);
902 RegLocation rlDest = InlineTarget(cUnit, info);
903 StoreValue(cUnit, rlDest, rlReturn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700904 return true;
Ian Rogers0183dd72012-09-17 23:06:51 -0700905}
906
buzbee52a77fc2012-11-20 19:50:46 -0800907bool GenIntrinsic(CompilationUnit* cUnit, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700908{
Ian Rogerse13eafa2012-09-07 11:24:27 -0700909 if (info->optFlags & MIR_INLINED) {
buzbeefc9e6fa2012-03-23 15:14:29 -0700910 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 }
912 /*
913 * TODO: move these to a target-specific structured constant array
914 * and use a generic match function. The list of intrinsics may be
915 * slightly different depending on target.
916 * TODO: Fold this into a matching function that runs during
917 * basic block building. This should be part of the action for
918 * small method inlining and recognition of the special object init
919 * method. By doing this during basic block construction, we can also
920 * take advantage of/generate new useful dataflow info.
921 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700922 std::string tgtMethod(PrettyMethod(info->index, *cUnit->dex_file));
Ian Rogers0183dd72012-09-17 23:06:51 -0700923 if (tgtMethod.find(" java.lang") != std::string::npos) {
924 if (tgtMethod == "long java.lang.Double.doubleToRawLongBits(double)") {
buzbee52a77fc2012-11-20 19:50:46 -0800925 return GenInlinedDoubleCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700926 }
927 if (tgtMethod == "double java.lang.Double.longBitsToDouble(long)") {
buzbee52a77fc2012-11-20 19:50:46 -0800928 return GenInlinedDoubleCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700929 }
930 if (tgtMethod == "int java.lang.Float.floatToRawIntBits(float)") {
buzbee52a77fc2012-11-20 19:50:46 -0800931 return GenInlinedFloatCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700932 }
933 if (tgtMethod == "float java.lang.Float.intBitsToFloat(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800934 return GenInlinedFloatCvt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700935 }
936 if (tgtMethod == "int java.lang.Math.abs(int)" ||
937 tgtMethod == "int java.lang.StrictMath.abs(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800938 return GenInlinedAbsInt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700939 }
940 if (tgtMethod == "long java.lang.Math.abs(long)" ||
941 tgtMethod == "long java.lang.StrictMath.abs(long)") {
buzbee52a77fc2012-11-20 19:50:46 -0800942 return GenInlinedAbsLong(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700943 }
944 if (tgtMethod == "int java.lang.Math.max(int, int)" ||
945 tgtMethod == "int java.lang.StrictMath.max(int, int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800946 return GenInlinedMinMaxInt(cUnit, info, false /* isMin */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700947 }
948 if (tgtMethod == "int java.lang.Math.min(int, int)" ||
949 tgtMethod == "int java.lang.StrictMath.min(int, int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800950 return GenInlinedMinMaxInt(cUnit, info, true /* isMin */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700951 }
952 if (tgtMethod == "double java.lang.Math.sqrt(double)" ||
953 tgtMethod == "double java.lang.StrictMath.sqrt(double)") {
buzbee52a77fc2012-11-20 19:50:46 -0800954 return GenInlinedSqrt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700955 }
956 if (tgtMethod == "char java.lang.String.charAt(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800957 return GenInlinedCharAt(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700958 }
959 if (tgtMethod == "int java.lang.String.compareTo(java.lang.String)") {
buzbee52a77fc2012-11-20 19:50:46 -0800960 return GenInlinedStringCompareTo(cUnit, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700961 }
962 if (tgtMethod == "boolean java.lang.String.isEmpty()") {
buzbee52a77fc2012-11-20 19:50:46 -0800963 return GenInlinedStringIsEmptyOrLength(cUnit, info, true /* isEmpty */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700964 }
965 if (tgtMethod == "int java.lang.String.indexOf(int, int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800966 return GenInlinedIndexOf(cUnit, info, false /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700967 }
968 if (tgtMethod == "int java.lang.String.indexOf(int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800969 return GenInlinedIndexOf(cUnit, info, true /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700970 }
971 if (tgtMethod == "int java.lang.String.length()") {
buzbee52a77fc2012-11-20 19:50:46 -0800972 return GenInlinedStringIsEmptyOrLength(cUnit, info, false /* isEmpty */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700973 }
974 } else if (tgtMethod.find("boolean sun.misc.Unsafe.compareAndSwap") != std::string::npos) {
975 if (tgtMethod == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
buzbee52a77fc2012-11-20 19:50:46 -0800976 return GenInlinedCas32(cUnit, info, false);
Ian Rogers0183dd72012-09-17 23:06:51 -0700977 }
978 if (tgtMethod == "boolean sun.misc.Unsafe.compareAndSwapObject(java.lang.Object, long, java.lang.Object, java.lang.Object)") {
buzbee52a77fc2012-11-20 19:50:46 -0800979 return GenInlinedCas32(cUnit, info, true);
Ian Rogers0183dd72012-09-17 23:06:51 -0700980 }
Ian Rogerse13eafa2012-09-07 11:24:27 -0700981 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700982 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -0700983}
984
buzbee52a77fc2012-11-20 19:50:46 -0800985void GenInvoke(CompilationUnit* cUnit, CallInfo* info)
buzbee1bc37c62012-11-20 13:35:41 -0800986{
buzbee52a77fc2012-11-20 19:50:46 -0800987 if (GenIntrinsic(cUnit, info)) {
buzbee1bc37c62012-11-20 13:35:41 -0800988 return;
989 }
990 InvokeType originalType = info->type; // avoiding mutation by ComputeInvokeInfo
991 int callState = 0;
992 LIR* nullCk;
993 LIR** pNullCk = NULL;
994 NextCallInsn nextCallInsn;
buzbee52a77fc2012-11-20 19:50:46 -0800995 FlushAllRegs(cUnit); /* Everything to home location */
buzbee1bc37c62012-11-20 13:35:41 -0800996 // Explicit register usage
buzbee52a77fc2012-11-20 19:50:46 -0800997 LockCallTemps(cUnit);
buzbee1bc37c62012-11-20 13:35:41 -0800998
999 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
1000 *cUnit->dex_file,
1001 cUnit->code_item, cUnit->method_idx,
1002 cUnit->access_flags);
1003
1004 uint32_t dexMethodIdx = info->index;
1005 int vtableIdx;
1006 uintptr_t directCode;
1007 uintptr_t directMethod;
1008 bool skipThis;
1009 bool fastPath =
1010 cUnit->compiler->ComputeInvokeInfo(dexMethodIdx, &mUnit, info->type,
1011 vtableIdx, directCode,
1012 directMethod)
1013 && !SLOW_INVOKE_PATH;
1014 if (info->type == kInterface) {
1015 if (fastPath) {
1016 pNullCk = &nullCk;
1017 }
buzbee52a77fc2012-11-20 19:50:46 -08001018 nextCallInsn = fastPath ? NextInterfaceCallInsn
1019 : NextInterfaceCallInsnWithAccessCheck;
buzbee1bc37c62012-11-20 13:35:41 -08001020 skipThis = false;
1021 } else if (info->type == kDirect) {
1022 if (fastPath) {
1023 pNullCk = &nullCk;
1024 }
buzbee52a77fc2012-11-20 19:50:46 -08001025 nextCallInsn = fastPath ? NextSDCallInsn : NextDirectCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001026 skipThis = false;
1027 } else if (info->type == kStatic) {
buzbee52a77fc2012-11-20 19:50:46 -08001028 nextCallInsn = fastPath ? NextSDCallInsn : NextStaticCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001029 skipThis = false;
1030 } else if (info->type == kSuper) {
1031 DCHECK(!fastPath); // Fast path is a direct call.
buzbee52a77fc2012-11-20 19:50:46 -08001032 nextCallInsn = NextSuperCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001033 skipThis = false;
1034 } else {
1035 DCHECK_EQ(info->type, kVirtual);
buzbee52a77fc2012-11-20 19:50:46 -08001036 nextCallInsn = fastPath ? NextVCallInsn : NextVCallInsnSP;
buzbee1bc37c62012-11-20 13:35:41 -08001037 skipThis = fastPath;
1038 }
1039 if (!info->isRange) {
buzbee52a77fc2012-11-20 19:50:46 -08001040 callState = GenDalvikArgsNoRange(cUnit, info, callState, pNullCk,
buzbee1bc37c62012-11-20 13:35:41 -08001041 nextCallInsn, dexMethodIdx,
1042 vtableIdx, directCode, directMethod,
1043 originalType, skipThis);
1044 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001045 callState = GenDalvikArgsRange(cUnit, info, callState, pNullCk,
buzbee1bc37c62012-11-20 13:35:41 -08001046 nextCallInsn, dexMethodIdx, vtableIdx,
1047 directCode, directMethod, originalType,
1048 skipThis);
1049 }
1050 // Finish up any of the call sequence not interleaved in arg loading
1051 while (callState >= 0) {
1052 callState = nextCallInsn(cUnit, info, callState, dexMethodIdx,
1053 vtableIdx, directCode, directMethod,
1054 originalType);
1055 }
1056 if (cUnit->enableDebug & (1 << kDebugDisplayMissingTargets)) {
buzbee52a77fc2012-11-20 19:50:46 -08001057 GenShowTarget(cUnit);
buzbee1bc37c62012-11-20 13:35:41 -08001058 }
1059 LIR* callInst;
1060 if (cUnit->instructionSet != kX86) {
buzbee52a77fc2012-11-20 19:50:46 -08001061 callInst = OpReg(cUnit, kOpBlx, TargetReg(kInvokeTgt));
buzbee1bc37c62012-11-20 13:35:41 -08001062 } else {
1063 if (fastPath && info->type != kInterface) {
buzbee52a77fc2012-11-20 19:50:46 -08001064 callInst = OpMem(cUnit, kOpBlx, TargetReg(kArg0),
buzbee1bc37c62012-11-20 13:35:41 -08001065 AbstractMethod::GetCodeOffset().Int32Value());
1066 } else {
1067 int trampoline = 0;
1068 switch (info->type) {
1069 case kInterface:
1070 trampoline = fastPath ? ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline)
1071 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
1072 break;
1073 case kDirect:
1074 trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
1075 break;
1076 case kStatic:
1077 trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
1078 break;
1079 case kSuper:
1080 trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
1081 break;
1082 case kVirtual:
1083 trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
1084 break;
1085 default:
1086 LOG(FATAL) << "Unexpected invoke type";
1087 }
buzbee52a77fc2012-11-20 19:50:46 -08001088 callInst = OpThreadMem(cUnit, kOpBlx, trampoline);
buzbee1bc37c62012-11-20 13:35:41 -08001089 }
1090 }
buzbee52a77fc2012-11-20 19:50:46 -08001091 MarkSafepointPC(cUnit, callInst);
buzbee1bc37c62012-11-20 13:35:41 -08001092
buzbee52a77fc2012-11-20 19:50:46 -08001093 ClobberCalleeSave(cUnit);
buzbee1bc37c62012-11-20 13:35:41 -08001094 if (info->result.location != kLocInvalid) {
1095 // We have a following MOVE_RESULT - do it now.
1096 if (info->result.wide) {
buzbee52a77fc2012-11-20 19:50:46 -08001097 RegLocation retLoc = GetReturnWide(cUnit, info->result.fp);
1098 StoreValueWide(cUnit, info->result, retLoc);
buzbee1bc37c62012-11-20 13:35:41 -08001099 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001100 RegLocation retLoc = GetReturn(cUnit, info->result.fp);
1101 StoreValue(cUnit, info->result, retLoc);
buzbee1bc37c62012-11-20 13:35:41 -08001102 }
1103 }
1104}
1105
1106/*
1107 * Build an array of location records for the incoming arguments.
1108 * Note: one location record per word of arguments, with dummy
1109 * high-word loc for wide arguments. Also pull up any following
1110 * MOVE_RESULT and incorporate it into the invoke.
1111 */
buzbee52a77fc2012-11-20 19:50:46 -08001112CallInfo* NewMemCallInfo(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
buzbee1bc37c62012-11-20 13:35:41 -08001113 InvokeType type, bool isRange)
1114{
buzbee52a77fc2012-11-20 19:50:46 -08001115 CallInfo* info = static_cast<CallInfo*>(NewMem(cUnit, sizeof(CallInfo), true, kAllocMisc));
1116 MIR* moveResultMIR = FindMoveResult(cUnit, bb, mir);
buzbee1bc37c62012-11-20 13:35:41 -08001117 if (moveResultMIR == NULL) {
1118 info->result.location = kLocInvalid;
1119 } else {
buzbee52a77fc2012-11-20 19:50:46 -08001120 info->result = GetRawDest(cUnit, moveResultMIR);
buzbee1bc37c62012-11-20 13:35:41 -08001121 moveResultMIR->dalvikInsn.opcode = Instruction::NOP;
1122 }
1123 info->numArgWords = mir->ssaRep->numUses;
1124 info->args = (info->numArgWords == 0) ? NULL : static_cast<RegLocation*>
buzbee52a77fc2012-11-20 19:50:46 -08001125 (NewMem(cUnit, sizeof(RegLocation) * info->numArgWords, false, kAllocMisc));
buzbee1bc37c62012-11-20 13:35:41 -08001126 for (int i = 0; i < info->numArgWords; i++) {
buzbee52a77fc2012-11-20 19:50:46 -08001127 info->args[i] = GetRawSrc(cUnit, mir, i);
buzbee1bc37c62012-11-20 13:35:41 -08001128 }
1129 info->optFlags = mir->optimizationFlags;
1130 info->type = type;
1131 info->isRange = isRange;
1132 info->index = mir->dalvikInsn.vB;
1133 info->offset = mir->offset;
1134 return info;
1135}
1136
buzbeefc9e6fa2012-03-23 15:14:29 -07001137
buzbee31a4a6f2012-02-28 15:36:15 -08001138} // namespace art