blob: 91d3db16fae18afef80fbebfd49c36f6c9b6df2a [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 */
buzbeefa57c472012-11-21 12:06:18 -080038void FlushIns(CompilationUnit* cu, RegLocation* ArgLocs, RegLocation rl_method)
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 */
buzbeefa57c472012-11-21 12:06:18 -080045 RegLocation rl_src = rl_method;
46 rl_src.location = kLocPhysReg;
47 rl_src.low_reg = TargetReg(kArg0);
48 rl_src.home = false;
49 MarkLive(cu, rl_src.low_reg, rl_src.s_reg_low);
50 StoreValue(cu, rl_method, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -070051 // If Method* has been promoted, explicitly flush
buzbeefa57c472012-11-21 12:06:18 -080052 if (rl_method.location == kLocPhysReg) {
53 StoreWordDisp(cu, TargetReg(kSp), 0, TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -070054 }
buzbee9c044ce2012-03-18 13:24:07 -070055
buzbeefa57c472012-11-21 12:06:18 -080056 if (cu->num_ins == 0)
Bill Buzbeea114add2012-05-03 15:00:40 -070057 return;
buzbeefa57c472012-11-21 12:06:18 -080058 const int num_arg_regs = 3;
59 static SpecialTargetRegister arg_regs[] = {kArg1, kArg2, kArg3};
60 int start_vreg = cu->num_dalvik_registers - cu->num_ins;
Bill Buzbeea114add2012-05-03 15:00:40 -070061 /*
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 */
buzbeefa57c472012-11-21 12:06:18 -080073 for (int i = 0; i < cu->num_ins; i++) {
74 PromotionMap* v_map = &cu->promotion_map[start_vreg + i];
75 if (i < num_arg_regs) {
Bill Buzbeea114add2012-05-03 15:00:40 -070076 // If arriving in register
buzbeefa57c472012-11-21 12:06:18 -080077 bool need_flush = true;
78 RegLocation* t_loc = &ArgLocs[i];
79 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
80 OpRegCopy(cu, v_map->core_reg, TargetReg(arg_regs[i]));
81 need_flush = false;
82 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
83 OpRegCopy(cu, v_map->FpReg, TargetReg(arg_regs[i]));
84 need_flush = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070085 } else {
buzbeefa57c472012-11-21 12:06:18 -080086 need_flush = true;
Bill Buzbeea114add2012-05-03 15:00:40 -070087 }
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
buzbeefa57c472012-11-21 12:06:18 -080090 if (t_loc->wide) {
91 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
92 need_flush |= (p_map->core_location != v_map->core_location) ||
93 (p_map->fp_location != v_map->fp_location);
Bill Buzbeea114add2012-05-03 15:00:40 -070094 }
buzbeefa57c472012-11-21 12:06:18 -080095 if (need_flush) {
96 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, start_vreg + i),
97 TargetReg(arg_regs[i]), kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -070098 }
99 } else {
100 // If arriving in frame & promoted
buzbeefa57c472012-11-21 12:06:18 -0800101 if (v_map->core_location == kLocPhysReg) {
102 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, start_vreg + i),
103 v_map->core_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 }
buzbeefa57c472012-11-21 12:06:18 -0800105 if (v_map->fp_location == kLocPhysReg) {
106 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, start_vreg + i),
107 v_map->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 */
buzbeefa57c472012-11-21 12:06:18 -0800117static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
118 int state, uint32_t dex_idx, uint32_t unused,
119 uintptr_t direct_code, uintptr_t direct_method,
buzbeeaad94382012-11-21 07:40:50 -0800120 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800121{
buzbeefa57c472012-11-21 12:06:18 -0800122 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700123 // Disable sharpening
buzbeefa57c472012-11-21 12:06:18 -0800124 direct_code = 0;
125 direct_method = 0;
buzbeeb046e162012-10-30 15:48:42 -0700126 }
buzbeefa57c472012-11-21 12:06:18 -0800127 if (direct_code != 0 && direct_method != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800129 case 0: // Get the current Method* [sets kArg0]
buzbeefa57c472012-11-21 12:06:18 -0800130 if (direct_code != static_cast<unsigned int>(-1)) {
131 LoadConstant(cu, TargetReg(kInvokeTgt), direct_code);
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 } else {
buzbeefa57c472012-11-21 12:06:18 -0800133 LIR* data_target = ScanLiteralPool(cu->code_literal_list, dex_idx, 0);
134 if (data_target == NULL) {
135 data_target = AddWordData(cu, &cu->code_literal_list, dex_idx);
136 data_target->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700137 }
buzbeefa57c472012-11-21 12:06:18 -0800138 LIR* load_pc_rel = OpPcRelLoad(cu, TargetReg(kInvokeTgt), data_target);
139 AppendLIR(cu, load_pc_rel);
140 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 }
buzbeefa57c472012-11-21 12:06:18 -0800142 if (direct_method != static_cast<unsigned int>(-1)) {
143 LoadConstant(cu, TargetReg(kArg0), direct_method);
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 } else {
buzbeefa57c472012-11-21 12:06:18 -0800145 LIR* data_target = ScanLiteralPool(cu->method_literal_list, dex_idx, 0);
146 if (data_target == NULL) {
147 data_target = AddWordData(cu, &cu->method_literal_list, dex_idx);
148 data_target->operands[1] = type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700149 }
buzbeefa57c472012-11-21 12:06:18 -0800150 LIR* load_pc_rel = OpPcRelLoad(cu, TargetReg(kArg0), data_target);
151 AppendLIR(cu, load_pc_rel);
152 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
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.
buzbeefa57c472012-11-21 12:06:18 -0800162 LoadCurrMethodDirect(cu, TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 break;
164 case 1: // Get method->dex_cache_resolved_methods_
buzbeefa57c472012-11-21 12:06:18 -0800165 LoadWordDisp(cu, TargetReg(kArg0),
buzbee52a77fc2012-11-20 19:50:46 -0800166 AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(), TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 // Set up direct code if known.
buzbeefa57c472012-11-21 12:06:18 -0800168 if (direct_code != 0) {
169 if (direct_code != static_cast<unsigned int>(-1)) {
170 LoadConstant(cu, TargetReg(kInvokeTgt), direct_code);
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 } else {
buzbeefa57c472012-11-21 12:06:18 -0800172 LIR* data_target = ScanLiteralPool(cu->code_literal_list, dex_idx, 0);
173 if (data_target == NULL) {
174 data_target = AddWordData(cu, &cu->code_literal_list, dex_idx);
175 data_target->operands[1] = type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 }
buzbeefa57c472012-11-21 12:06:18 -0800177 LIR* load_pc_rel = OpPcRelLoad(cu, TargetReg(kInvokeTgt), data_target);
178 AppendLIR(cu, load_pc_rel);
179 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 }
181 }
182 break;
183 case 2: // Grab target method*
buzbeefa57c472012-11-21 12:06:18 -0800184 LoadWordDisp(cu, TargetReg(kArg0),
185 Array::DataOffset(sizeof(Object*)).Int32Value() + dex_idx * 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*
buzbeefa57c472012-11-21 12:06:18 -0800188 if (cu->instruction_set != kX86) {
189 if (direct_code == 0) {
190 LoadWordDisp(cu, TargetReg(kArg0), AbstractMethod::GetCodeOffset().Int32Value(),
buzbee52a77fc2012-11-20 19:50:46 -0800191 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 */
buzbeefa57c472012-11-21 12:06:18 -0800210static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
211 int state, uint32_t dex_idx, uint32_t method_idx,
buzbeeaad94382012-11-21 07:40:50 -0800212 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]
buzbeefa57c472012-11-21 12:06:18 -0800220 RegLocation rl_arg = info->args[0];
221 LoadValueDirectFixed(cu, rl_arg, 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]
buzbeefa57c472012-11-21 12:06:18 -0800225 GenNullCheck(cu, info->args[0].s_reg_low, TargetReg(kArg1), info->opt_flags);
buzbeef0504cd2012-11-13 16:31:10 -0800226 // get this->klass_ [use kArg1, set kInvokeTgt]
buzbeefa57c472012-11-21 12:06:18 -0800227 LoadWordDisp(cu, TargetReg(kArg1), Object::ClassOffset().Int32Value(),
buzbee52a77fc2012-11-20 19:50:46 -0800228 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]
buzbeefa57c472012-11-21 12:06:18 -0800231 LoadWordDisp(cu, TargetReg(kInvokeTgt), Class::VTableOffset().Int32Value(),
buzbee52a77fc2012-11-20 19:50:46 -0800232 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]
buzbeefa57c472012-11-21 12:06:18 -0800235 LoadWordDisp(cu, TargetReg(kInvokeTgt), (method_idx * 4) +
buzbee52a77fc2012-11-20 19:50:46 -0800236 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]
buzbeefa57c472012-11-21 12:06:18 -0800239 if (cu->instruction_set != kX86) {
240 LoadWordDisp(cu, TargetReg(kArg0), AbstractMethod::GetCodeOffset().Int32Value(),
buzbee52a77fc2012-11-20 19:50:46 -0800241 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 */
buzbeefa57c472012-11-21 12:06:18 -0800255static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
256 uint32_t dex_idx, uint32_t unused, uintptr_t unused2,
257 uintptr_t direct_method, InvokeType unused4)
Ian Rogers137e88f2012-10-08 17:46:47 -0700258{
buzbeefa57c472012-11-21 12:06:18 -0800259 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700260 // Disable sharpening
buzbeefa57c472012-11-21 12:06:18 -0800261 direct_method = 0;
buzbeeb046e162012-10-30 15:48:42 -0700262 }
buzbeefa57c472012-11-21 12:06:18 -0800263 int trampoline = (cu->instruction_set == kX86) ? 0
buzbeeb046e162012-10-30 15:48:42 -0700264 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
Ian Rogers137e88f2012-10-08 17:46:47 -0700265
buzbeefa57c472012-11-21 12:06:18 -0800266 if (direct_method != 0) {
Ian Rogers137e88f2012-10-08 17:46:47 -0700267 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800268 case 0: // Load the trampoline target [sets kInvokeTgt].
buzbeefa57c472012-11-21 12:06:18 -0800269 if (cu->instruction_set != kX86) {
270 LoadWordDisp(cu, 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]
buzbeefa57c472012-11-21 12:06:18 -0800273 if (direct_method != static_cast<unsigned int>(-1)) {
274 LoadConstant(cu, TargetReg(kArg0), direct_method);
Ian Rogers137e88f2012-10-08 17:46:47 -0700275 } else {
buzbeefa57c472012-11-21 12:06:18 -0800276 LIR* data_target = ScanLiteralPool(cu->method_literal_list, dex_idx, 0);
277 if (data_target == NULL) {
278 data_target = AddWordData(cu, &cu->method_literal_list, dex_idx);
279 data_target->operands[1] = kInterface;
Ian Rogers137e88f2012-10-08 17:46:47 -0700280 }
buzbeefa57c472012-11-21 12:06:18 -0800281 LIR* load_pc_rel = OpPcRelLoad(cu, TargetReg(kArg0), data_target);
282 AppendLIR(cu, load_pc_rel);
283 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
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.
buzbeefa57c472012-11-21 12:06:18 -0800293 LoadCurrMethodDirect(cu, TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800294 // Load the trampoline target [sets kInvokeTgt].
buzbeefa57c472012-11-21 12:06:18 -0800295 if (cu->instruction_set != kX86) {
296 LoadWordDisp(cu, 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]
buzbeefa57c472012-11-21 12:06:18 -0800300 LoadWordDisp(cu, 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]
buzbeefa57c472012-11-21 12:06:18 -0800305 LoadWordDisp(cu, TargetReg(kArg0),
306 Array::DataOffset(sizeof(Object*)).Int32Value() + dex_idx * 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
buzbeefa57c472012-11-21 12:06:18 -0800316static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, int trampoline,
317 int state, uint32_t dex_idx, uint32_t method_idx)
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) {
buzbeefa57c472012-11-21 12:06:18 -0800324 if (cu->instruction_set != kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700325 // Load trampoline target
buzbeefa57c472012-11-21 12:06:18 -0800326 LoadWordDisp(cu, TargetReg(kSelf), trampoline, TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700327 }
buzbeef0504cd2012-11-13 16:31:10 -0800328 // Load kArg0 with method index
buzbeefa57c472012-11-21 12:06:18 -0800329 LoadConstant(cu, TargetReg(kArg0), dex_idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 return 1;
331 }
332 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800333}
334
buzbeefa57c472012-11-21 12:06:18 -0800335static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
336 int state, uint32_t dex_idx, uint32_t method_idx,
buzbeeaad94382012-11-21 07:40:50 -0800337 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);
buzbeefa57c472012-11-21 12:06:18 -0800341 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800342}
343
buzbeefa57c472012-11-21 12:06:18 -0800344static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
345 uint32_t dex_idx, uint32_t method_idx, uintptr_t unused,
buzbeeaad94382012-11-21 07:40:50 -0800346 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);
buzbeefa57c472012-11-21 12:06:18 -0800349 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800350}
351
buzbeefa57c472012-11-21 12:06:18 -0800352static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
353 uint32_t dex_idx, uint32_t method_idx, 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);
buzbeefa57c472012-11-21 12:06:18 -0800357 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800358}
359
buzbeefa57c472012-11-21 12:06:18 -0800360static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
361 uint32_t dex_idx, uint32_t method_idx, uintptr_t unused,
buzbeeaad94382012-11-21 07:40:50 -0800362 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);
buzbeefa57c472012-11-21 12:06:18 -0800365 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800366}
367
buzbeefa57c472012-11-21 12:06:18 -0800368static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
buzbeeaad94382012-11-21 07:40:50 -0800369 CallInfo* info, int state,
buzbeefa57c472012-11-21 12:06:18 -0800370 uint32_t dex_idx, uint32_t unused,
buzbee15bf9802012-06-12 17:49:27 -0700371 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);
buzbeefa57c472012-11-21 12:06:18 -0800375 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800376}
377
buzbeefa57c472012-11-21 12:06:18 -0800378static int LoadArgRegs(CompilationUnit* cu, CallInfo* info, int call_state,
379 NextCallInsn next_call_insn, uint32_t dex_idx,
380 uint32_t method_idx, uintptr_t direct_code,
381 uintptr_t direct_method, InvokeType type, bool skip_this)
buzbee31a4a6f2012-02-28 15:36:15 -0800382{
buzbeefa57c472012-11-21 12:06:18 -0800383 int last_arg_reg = TargetReg(kArg3);
384 int next_reg = TargetReg(kArg1);
385 int next_arg = 0;
386 if (skip_this) {
387 next_reg++;
388 next_arg++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 }
buzbeefa57c472012-11-21 12:06:18 -0800390 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
391 RegLocation rl_arg = info->args[next_arg++];
392 rl_arg = UpdateRawLoc(cu, rl_arg);
393 if (rl_arg.wide && (next_reg <= TargetReg(kArg2))) {
394 LoadValueDirectWideFixed(cu, rl_arg, next_reg, next_reg + 1);
395 next_reg++;
396 next_arg++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700397 } else {
buzbeefa57c472012-11-21 12:06:18 -0800398 rl_arg.wide = false;
399 LoadValueDirectFixed(cu, rl_arg, next_reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800400 }
buzbeefa57c472012-11-21 12:06:18 -0800401 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
402 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 }
buzbeefa57c472012-11-21 12:06:18 -0800404 return call_state;
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 */
buzbeefa57c472012-11-21 12:06:18 -0800414int GenDalvikArgsNoRange(CompilationUnit* cu, CallInfo* info,
415 int call_state,
416 LIR** pcrLabel, NextCallInsn next_call_insn,
417 uint32_t dex_idx, uint32_t method_idx,
418 uintptr_t direct_code, uintptr_t direct_method,
419 InvokeType type, bool skip_this)
buzbee31a4a6f2012-02-28 15:36:15 -0800420{
buzbeefa57c472012-11-21 12:06:18 -0800421 RegLocation rl_arg;
buzbee31a4a6f2012-02-28 15:36:15 -0800422
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 /* If no arguments, just return */
buzbeefa57c472012-11-21 12:06:18 -0800424 if (info->num_arg_words == 0)
425 return call_state;
Bill Buzbeea114add2012-05-03 15:00:40 -0700426
buzbeefa57c472012-11-21 12:06:18 -0800427 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
428 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700429
buzbeefa57c472012-11-21 12:06:18 -0800430 DCHECK_LE(info->num_arg_words, 5);
431 if (info->num_arg_words > 3) {
432 int32_t next_use = 3;
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 //Detect special case of wide arg spanning arg3/arg4
buzbeefa57c472012-11-21 12:06:18 -0800434 RegLocation rl_use0 = info->args[0];
435 RegLocation rl_use1 = info->args[1];
436 RegLocation rl_use2 = info->args[2];
437 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) &&
438 rl_use2.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 int reg = -1;
440 // Wide spans, we need the 2nd half of uses[2].
buzbeefa57c472012-11-21 12:06:18 -0800441 rl_arg = UpdateLocWide(cu, rl_use2);
442 if (rl_arg.location == kLocPhysReg) {
443 reg = rl_arg.high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 } 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);
buzbeefa57c472012-11-21 12:06:18 -0800447 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_arg.s_reg_low) + 4, reg);
448 call_state = next_call_insn(cu, info, call_state, dex_idx,
449 method_idx, direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 }
buzbeefa57c472012-11-21 12:06:18 -0800451 StoreBaseDisp(cu, TargetReg(kSp), (next_use + 1) * 4, reg, kWord);
452 StoreBaseDisp(cu, TargetReg(kSp), 16 /* (3+1)*4 */, reg, kWord);
453 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
454 direct_code, direct_method, type);
455 next_use++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 }
457 // Loop through the rest
buzbeefa57c472012-11-21 12:06:18 -0800458 while (next_use < info->num_arg_words) {
459 int low_reg;
460 int high_reg = -1;
461 rl_arg = info->args[next_use];
462 rl_arg = UpdateRawLoc(cu, rl_arg);
463 if (rl_arg.location == kLocPhysReg) {
464 low_reg = rl_arg.low_reg;
465 high_reg = rl_arg.high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700466 } else {
buzbeefa57c472012-11-21 12:06:18 -0800467 low_reg = TargetReg(kArg2);
468 if (rl_arg.wide) {
469 high_reg = TargetReg(kArg3);
470 LoadValueDirectWideFixed(cu, rl_arg, low_reg, high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 } else {
buzbeefa57c472012-11-21 12:06:18 -0800472 LoadValueDirectFixed(cu, rl_arg, low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700473 }
buzbeefa57c472012-11-21 12:06:18 -0800474 call_state = next_call_insn(cu, info, call_state, dex_idx,
475 method_idx, direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 }
buzbeefa57c472012-11-21 12:06:18 -0800477 int outs_offset = (next_use + 1) * 4;
478 if (rl_arg.wide) {
479 StoreBaseDispWide(cu, TargetReg(kSp), outs_offset, low_reg, high_reg);
480 next_use += 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 } else {
buzbeefa57c472012-11-21 12:06:18 -0800482 StoreWordDisp(cu, TargetReg(kSp), outs_offset, low_reg);
483 next_use++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700484 }
buzbeefa57c472012-11-21 12:06:18 -0800485 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
486 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 }
488 }
489
buzbeefa57c472012-11-21 12:06:18 -0800490 call_state = LoadArgRegs(cu, info, call_state, next_call_insn,
491 dex_idx, method_idx, direct_code, direct_method,
492 type, skip_this);
Bill Buzbeea114add2012-05-03 15:00:40 -0700493
494 if (pcrLabel) {
buzbeefa57c472012-11-21 12:06:18 -0800495 *pcrLabel = GenNullCheck(cu, info->args[0].s_reg_low, TargetReg(kArg1), info->opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700496 }
buzbeefa57c472012-11-21 12:06:18 -0800497 return call_state;
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 */
buzbeefa57c472012-11-21 12:06:18 -0800515int GenDalvikArgsRange(CompilationUnit* cu, CallInfo* info, int call_state,
516 LIR** pcrLabel, NextCallInsn next_call_insn,
517 uint32_t dex_idx, uint32_t method_idx,
518 uintptr_t direct_code, uintptr_t direct_method,
519 InvokeType type, bool skip_this)
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)
buzbeefa57c472012-11-21 12:06:18 -0800523 if (info->num_arg_words <= 5)
524 return GenDalvikArgsNoRange(cu, info, call_state, pcrLabel,
525 next_call_insn, dex_idx, method_idx,
526 direct_code, direct_method, type, skip_this);
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 /*
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
buzbeefa57c472012-11-21 12:06:18 -0800530 * scan the s_reg names and flush any that have been promoted to
Bill Buzbeea114add2012-05-03 15:00:40 -0700531 * frame backing storage.
532 */
buzbeefa57c472012-11-21 12:06:18 -0800533 // Scan the rest of the args - if in phys_reg flush to memory
534 for (int next_arg = 0; next_arg < info->num_arg_words;) {
535 RegLocation loc = info->args[next_arg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700536 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -0800537 loc = UpdateLocWide(cu, loc);
538 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
539 StoreBaseDispWide(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
540 loc.low_reg, loc.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 }
buzbeefa57c472012-11-21 12:06:18 -0800542 next_arg += 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700543 } else {
buzbeefa57c472012-11-21 12:06:18 -0800544 loc = UpdateLoc(cu, loc);
545 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
546 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
547 loc.low_reg, kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700548 }
buzbeefa57c472012-11-21 12:06:18 -0800549 next_arg++;
buzbee31a4a6f2012-02-28 15:36:15 -0800550 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 }
buzbee31a4a6f2012-02-28 15:36:15 -0800552
buzbeefa57c472012-11-21 12:06:18 -0800553 int start_offset = SRegOffset(cu, info->args[3].s_reg_low);
554 int outs_offset = 4 /* Method* */ + (3 * 4);
555 if (cu->instruction_set != kThumb2) {
buzbee31a4a6f2012-02-28 15:36:15 -0800556 // Generate memcpy
buzbeefa57c472012-11-21 12:06:18 -0800557 OpRegRegImm(cu, kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
558 OpRegRegImm(cu, kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
559 CallRuntimeHelperRegRegImm(cu, ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
560 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 } else {
buzbeefa57c472012-11-21 12:06:18 -0800562 if (info->num_arg_words >= 20) {
buzbeeb046e162012-10-30 15:48:42 -0700563 // Generate memcpy
buzbeefa57c472012-11-21 12:06:18 -0800564 OpRegRegImm(cu, kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
565 OpRegRegImm(cu, kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
566 CallRuntimeHelperRegRegImm(cu, ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
567 TargetReg(kArg1), (info->num_arg_words - 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
buzbeefa57c472012-11-21 12:06:18 -0800570 int regs_left = std::min(info->num_arg_words - 3, 16);
571 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
572 direct_code, direct_method, type);
573 OpRegRegImm(cu, kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
574 LIR* ld = OpVldm(cu, TargetReg(kArg3), regs_left);
buzbeeb046e162012-10-30 15:48:42 -0700575 //TUNING: loosen barrier
buzbeefa57c472012-11-21 12:06:18 -0800576 ld->def_mask = ENCODE_ALL;
577 SetMemRefType(ld, true /* is_load */, kDalvikReg);
578 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
579 direct_code, direct_method, type);
580 OpRegRegImm(cu, kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
581 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
582 direct_code, direct_method, type);
583 LIR* st = OpVstm(cu, TargetReg(kArg3), regs_left);
584 SetMemRefType(st, false /* is_load */, kDalvikReg);
585 st->def_mask = ENCODE_ALL;
586 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
587 direct_code, direct_method, type);
buzbeeb046e162012-10-30 15:48:42 -0700588 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700589 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700590
buzbeefa57c472012-11-21 12:06:18 -0800591 call_state = LoadArgRegs(cu, info, call_state, next_call_insn,
592 dex_idx, method_idx, direct_code, direct_method,
593 type, skip_this);
Bill Buzbeea114add2012-05-03 15:00:40 -0700594
buzbeefa57c472012-11-21 12:06:18 -0800595 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
596 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700597 if (pcrLabel) {
buzbeefa57c472012-11-21 12:06:18 -0800598 *pcrLabel = GenNullCheck(cu, info->args[0].s_reg_low, TargetReg(kArg1),
599 info->opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 }
buzbeefa57c472012-11-21 12:06:18 -0800601 return call_state;
buzbee31a4a6f2012-02-28 15:36:15 -0800602}
603
buzbeefa57c472012-11-21 12:06:18 -0800604RegLocation InlineTarget(CompilationUnit* cu, 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) {
buzbeefa57c472012-11-21 12:06:18 -0800608 res = GetReturn(cu, 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
buzbeefa57c472012-11-21 12:06:18 -0800615RegLocation InlineTargetWide(CompilationUnit* cu, 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) {
buzbeefa57c472012-11-21 12:06:18 -0800619 res = GetReturnWide(cu, 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
buzbeefa57c472012-11-21 12:06:18 -0800626bool GenInlinedCharAt(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700627{
buzbeefa57c472012-11-21 12:06:18 -0800628 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700629 // TODO - add Mips implementation
630 return false;
631 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700632 // Location of reference to data array
buzbeefa57c472012-11-21 12:06:18 -0800633 int value_offset = String::ValueOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -0700634 // Location of count
buzbeefa57c472012-11-21 12:06:18 -0800635 int count_offset = String::CountOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 // Starting offset within data array
buzbeefa57c472012-11-21 12:06:18 -0800637 int offset_offset = String::OffsetOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -0700638 // Start of char data with array_
buzbeefa57c472012-11-21 12:06:18 -0800639 int data_offset = Array::DataOffset(sizeof(uint16_t)).Int32Value();
buzbeefc9e6fa2012-03-23 15:14:29 -0700640
buzbeefa57c472012-11-21 12:06:18 -0800641 RegLocation rl_obj = info->args[0];
642 RegLocation rl_idx = info->args[1];
643 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
644 rl_idx = LoadValue(cu, rl_idx, kCoreReg);
645 int reg_max;
646 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, info->opt_flags);
647 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
648 LIR* launch_pad = NULL;
649 int reg_off = INVALID_REG;
650 int reg_ptr = INVALID_REG;
651 if (cu->instruction_set != kX86) {
652 reg_off = AllocTemp(cu);
653 reg_ptr = AllocTemp(cu);
654 if (range_check) {
655 reg_max = AllocTemp(cu);
656 LoadWordDisp(cu, rl_obj.low_reg, count_offset, reg_max);
buzbeeb046e162012-10-30 15:48:42 -0700657 }
buzbeefa57c472012-11-21 12:06:18 -0800658 LoadWordDisp(cu, rl_obj.low_reg, offset_offset, reg_off);
659 LoadWordDisp(cu, rl_obj.low_reg, value_offset, reg_ptr);
660 if (range_check) {
buzbeeb046e162012-10-30 15:48:42 -0700661 // Set up a launch pad to allow retry in case of bounds violation */
buzbeefa57c472012-11-21 12:06:18 -0800662 launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
663 InsertGrowableList(cu, &cu->intrinsic_launchpads,
664 reinterpret_cast<uintptr_t>(launch_pad));
665 OpRegReg(cu, kOpCmp, rl_idx.low_reg, reg_max);
666 FreeTemp(cu, reg_max);
667 OpCondBranch(cu, kCondCs, launch_pad);
buzbeeb046e162012-10-30 15:48:42 -0700668 }
669 } else {
buzbeefa57c472012-11-21 12:06:18 -0800670 if (range_check) {
671 reg_max = AllocTemp(cu);
672 LoadWordDisp(cu, rl_obj.low_reg, count_offset, reg_max);
buzbeeb046e162012-10-30 15:48:42 -0700673 // Set up a launch pad to allow retry in case of bounds violation */
buzbeefa57c472012-11-21 12:06:18 -0800674 launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
675 InsertGrowableList(cu, &cu->intrinsic_launchpads,
676 reinterpret_cast<uintptr_t>(launch_pad));
677 OpRegReg(cu, kOpCmp, rl_idx.low_reg, reg_max);
678 FreeTemp(cu, reg_max);
679 OpCondBranch(cu, kCondCc, launch_pad);
buzbeeb046e162012-10-30 15:48:42 -0700680 }
buzbeefa57c472012-11-21 12:06:18 -0800681 reg_off = AllocTemp(cu);
682 reg_ptr = AllocTemp(cu);
683 LoadWordDisp(cu, rl_obj.low_reg, offset_offset, reg_off);
684 LoadWordDisp(cu, rl_obj.low_reg, value_offset, reg_ptr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 }
buzbeefa57c472012-11-21 12:06:18 -0800686 OpRegImm(cu, kOpAdd, reg_ptr, data_offset);
687 OpRegReg(cu, kOpAdd, reg_off, rl_idx.low_reg);
688 FreeTemp(cu, rl_obj.low_reg);
689 FreeTemp(cu, rl_idx.low_reg);
690 RegLocation rl_dest = InlineTarget(cu, info);
691 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
692 LoadBaseIndexed(cu, reg_ptr, reg_off, rl_result.low_reg, 1, kUnsignedHalf);
693 FreeTemp(cu, reg_off);
694 FreeTemp(cu, reg_ptr);
695 StoreValue(cu, rl_dest, rl_result);
696 if (range_check) {
697 launch_pad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 }
699 // Record that we've already inlined & null checked
buzbeefa57c472012-11-21 12:06:18 -0800700 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700702}
703
buzbeefa57c472012-11-21 12:06:18 -0800704// Generates an inlined String.is_empty or String.length.
705bool GenInlinedStringIsEmptyOrLength(CompilationUnit* cu, CallInfo* info,
706 bool is_empty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700707{
buzbeefa57c472012-11-21 12:06:18 -0800708 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700709 // TODO - add Mips implementation
710 return false;
711 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700712 // dst = src.length();
buzbeefa57c472012-11-21 12:06:18 -0800713 RegLocation rl_obj = info->args[0];
714 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
715 RegLocation rl_dest = InlineTarget(cu, info);
716 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
717 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, info->opt_flags);
718 LoadWordDisp(cu, rl_obj.low_reg, String::CountOffset().Int32Value(),
719 rl_result.low_reg);
720 if (is_empty) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 // dst = (dst == 0);
buzbeefa57c472012-11-21 12:06:18 -0800722 if (cu->instruction_set == kThumb2) {
723 int t_reg = AllocTemp(cu);
724 OpRegReg(cu, kOpNeg, t_reg, rl_result.low_reg);
725 OpRegRegReg(cu, kOpAdc, rl_result.low_reg, rl_result.low_reg, t_reg);
buzbeeb046e162012-10-30 15:48:42 -0700726 } else {
buzbeefa57c472012-11-21 12:06:18 -0800727 DCHECK_EQ(cu->instruction_set, kX86);
728 OpRegImm(cu, kOpSub, rl_result.low_reg, 1);
729 OpRegImm(cu, kOpLsr, rl_result.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -0700730 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700731 }
buzbeefa57c472012-11-21 12:06:18 -0800732 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700733 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700734}
735
buzbeefa57c472012-11-21 12:06:18 -0800736bool GenInlinedAbsInt(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700737{
buzbeefa57c472012-11-21 12:06:18 -0800738 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700739 // TODO - add Mips implementation
740 return false;
741 }
buzbeefa57c472012-11-21 12:06:18 -0800742 RegLocation rl_src = info->args[0];
743 rl_src = LoadValue(cu, rl_src, kCoreReg);
744 RegLocation rl_dest = InlineTarget(cu, info);
745 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
746 int sign_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700747 // abs(x) = y<=x>>31, (x+y)^y.
buzbeefa57c472012-11-21 12:06:18 -0800748 OpRegRegImm(cu, kOpAsr, sign_reg, rl_src.low_reg, 31);
749 OpRegRegReg(cu, kOpAdd, rl_result.low_reg, rl_src.low_reg, sign_reg);
750 OpRegReg(cu, kOpXor, rl_result.low_reg, sign_reg);
751 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700752 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700753}
754
buzbeefa57c472012-11-21 12:06:18 -0800755bool GenInlinedAbsLong(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700756{
buzbeefa57c472012-11-21 12:06:18 -0800757 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700758 // TODO - add Mips implementation
759 return false;
760 }
buzbeefa57c472012-11-21 12:06:18 -0800761 if (cu->instruction_set == kThumb2) {
762 RegLocation rl_src = info->args[0];
763 rl_src = LoadValueWide(cu, rl_src, kCoreReg);
764 RegLocation rl_dest = InlineTargetWide(cu, info);
765 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
766 int sign_reg = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700767 // abs(x) = y<=x>>31, (x+y)^y.
buzbeefa57c472012-11-21 12:06:18 -0800768 OpRegRegImm(cu, kOpAsr, sign_reg, rl_src.high_reg, 31);
769 OpRegRegReg(cu, kOpAdd, rl_result.low_reg, rl_src.low_reg, sign_reg);
770 OpRegRegReg(cu, kOpAdc, rl_result.high_reg, rl_src.high_reg, sign_reg);
771 OpRegReg(cu, kOpXor, rl_result.low_reg, sign_reg);
772 OpRegReg(cu, kOpXor, rl_result.high_reg, sign_reg);
773 StoreValueWide(cu, rl_dest, rl_result);
buzbeeb046e162012-10-30 15:48:42 -0700774 return true;
775 } else {
buzbeefa57c472012-11-21 12:06:18 -0800776 DCHECK_EQ(cu->instruction_set, kX86);
buzbeeb046e162012-10-30 15:48:42 -0700777 // Reuse source registers to avoid running out of temps
buzbeefa57c472012-11-21 12:06:18 -0800778 RegLocation rl_src = info->args[0];
779 rl_src = LoadValueWide(cu, rl_src, kCoreReg);
780 RegLocation rl_dest = InlineTargetWide(cu, info);
781 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
782 OpRegCopyWide(cu, rl_result.low_reg, rl_result.high_reg, rl_src.low_reg, rl_src.high_reg);
783 FreeTemp(cu, rl_src.low_reg);
784 FreeTemp(cu, rl_src.high_reg);
785 int sign_reg = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700786 // abs(x) = y<=x>>31, (x+y)^y.
buzbeefa57c472012-11-21 12:06:18 -0800787 OpRegRegImm(cu, kOpAsr, sign_reg, rl_result.high_reg, 31);
788 OpRegReg(cu, kOpAdd, rl_result.low_reg, sign_reg);
789 OpRegReg(cu, kOpAdc, rl_result.high_reg, sign_reg);
790 OpRegReg(cu, kOpXor, rl_result.low_reg, sign_reg);
791 OpRegReg(cu, kOpXor, rl_result.high_reg, sign_reg);
792 StoreValueWide(cu, rl_dest, rl_result);
buzbeeb046e162012-10-30 15:48:42 -0700793 return true;
794 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700795}
796
buzbeefa57c472012-11-21 12:06:18 -0800797bool GenInlinedFloatCvt(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700798{
buzbeefa57c472012-11-21 12:06:18 -0800799 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700800 // TODO - add Mips implementation
801 return false;
802 }
buzbeefa57c472012-11-21 12:06:18 -0800803 RegLocation rl_src = info->args[0];
804 RegLocation rl_dest = InlineTarget(cu, info);
805 StoreValue(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -0700806 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700807}
808
buzbeefa57c472012-11-21 12:06:18 -0800809bool GenInlinedDoubleCvt(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700810{
buzbeefa57c472012-11-21 12:06:18 -0800811 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700812 // TODO - add Mips implementation
813 return false;
814 }
buzbeefa57c472012-11-21 12:06:18 -0800815 RegLocation rl_src = info->args[0];
816 RegLocation rl_dest = InlineTargetWide(cu, info);
817 StoreValueWide(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -0700818 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700819}
820
821/*
buzbeefa57c472012-11-21 12:06:18 -0800822 * Fast string.index_of(I) & (II). Tests for simple case of char <= 0xffff,
buzbeefc9e6fa2012-03-23 15:14:29 -0700823 * otherwise bails to standard library code.
824 */
buzbeefa57c472012-11-21 12:06:18 -0800825bool GenInlinedIndexOf(CompilationUnit* cu, CallInfo* info,
826 bool zero_based)
buzbeefc9e6fa2012-03-23 15:14:29 -0700827{
buzbeefa57c472012-11-21 12:06:18 -0800828 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700829 // TODO - add Mips implementation
830 return false;
831 }
buzbeefa57c472012-11-21 12:06:18 -0800832 ClobberCalleeSave(cu);
833 LockCallTemps(cu); // Using fixed registers
834 int reg_ptr = TargetReg(kArg0);
835 int reg_char = TargetReg(kArg1);
836 int reg_start = TargetReg(kArg2);
buzbeefc9e6fa2012-03-23 15:14:29 -0700837
buzbeefa57c472012-11-21 12:06:18 -0800838 RegLocation rl_obj = info->args[0];
839 RegLocation rl_char = info->args[1];
840 RegLocation rl_start = info->args[2];
841 LoadValueDirectFixed(cu, rl_obj, reg_ptr);
842 LoadValueDirectFixed(cu, rl_char, reg_char);
843 if (zero_based) {
844 LoadConstant(cu, reg_start, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 } else {
buzbeefa57c472012-11-21 12:06:18 -0800846 LoadValueDirectFixed(cu, rl_start, reg_start);
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 }
buzbeefa57c472012-11-21 12:06:18 -0800848 int r_tgt = (cu->instruction_set != kX86) ? LoadHelper(cu, ENTRYPOINT_OFFSET(pIndexOf)) : 0;
849 GenNullCheck(cu, rl_obj.s_reg_low, reg_ptr, info->opt_flags);
850 LIR* launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
851 InsertGrowableList(cu, &cu->intrinsic_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
852 OpCmpImmBranch(cu, kCondGt, reg_char, 0xFFFF, launch_pad);
buzbee8320f382012-09-11 16:29:42 -0700853 // NOTE: not a safepoint
buzbeefa57c472012-11-21 12:06:18 -0800854 if (cu->instruction_set != kX86) {
855 OpReg(cu, kOpBlx, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700856 } else {
buzbeefa57c472012-11-21 12:06:18 -0800857 OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
buzbeeb046e162012-10-30 15:48:42 -0700858 }
buzbeefa57c472012-11-21 12:06:18 -0800859 LIR* resume_tgt = NewLIR0(cu, kPseudoTargetLabel);
860 launch_pad->operands[2] = reinterpret_cast<uintptr_t>(resume_tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -0700861 // Record that we've already inlined & null checked
buzbeefa57c472012-11-21 12:06:18 -0800862 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
863 RegLocation rl_return = GetReturn(cu, false);
864 RegLocation rl_dest = InlineTarget(cu, info);
865 StoreValue(cu, rl_dest, rl_return);
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. */
buzbeefa57c472012-11-21 12:06:18 -0800870bool GenInlinedStringCompareTo(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700871{
buzbeefa57c472012-11-21 12:06:18 -0800872 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700873 // TODO - add Mips implementation
874 return false;
875 }
buzbeefa57c472012-11-21 12:06:18 -0800876 ClobberCalleeSave(cu);
877 LockCallTemps(cu); // Using fixed registers
878 int reg_this = TargetReg(kArg0);
879 int reg_cmp = TargetReg(kArg1);
buzbeefc9e6fa2012-03-23 15:14:29 -0700880
buzbeefa57c472012-11-21 12:06:18 -0800881 RegLocation rl_this = info->args[0];
882 RegLocation rl_cmp = info->args[1];
883 LoadValueDirectFixed(cu, rl_this, reg_this);
884 LoadValueDirectFixed(cu, rl_cmp, reg_cmp);
885 int r_tgt = (cu->instruction_set != kX86) ?
886 LoadHelper(cu, ENTRYPOINT_OFFSET(pStringCompareTo)) : 0;
887 GenNullCheck(cu, rl_this.s_reg_low, reg_this, info->opt_flags);
888 //TUNING: check if rl_cmp.s_reg_low is already null checked
889 LIR* launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
890 InsertGrowableList(cu, &cu->intrinsic_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
891 OpCmpImmBranch(cu, kCondEq, reg_cmp, 0, launch_pad);
buzbee8320f382012-09-11 16:29:42 -0700892 // NOTE: not a safepoint
buzbeefa57c472012-11-21 12:06:18 -0800893 if (cu->instruction_set != kX86) {
894 OpReg(cu, kOpBlx, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700895 } else {
buzbeefa57c472012-11-21 12:06:18 -0800896 OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
buzbeeb046e162012-10-30 15:48:42 -0700897 }
buzbeefa57c472012-11-21 12:06:18 -0800898 launch_pad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -0700899 // Record that we've already inlined & null checked
buzbeefa57c472012-11-21 12:06:18 -0800900 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
901 RegLocation rl_return = GetReturn(cu, false);
902 RegLocation rl_dest = InlineTarget(cu, info);
903 StoreValue(cu, rl_dest, rl_return);
Bill Buzbeea114add2012-05-03 15:00:40 -0700904 return true;
Ian Rogers0183dd72012-09-17 23:06:51 -0700905}
906
buzbeefa57c472012-11-21 12:06:18 -0800907bool GenIntrinsic(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700908{
buzbeefa57c472012-11-21 12:06:18 -0800909 if (info->opt_flags & 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 */
buzbeefa57c472012-11-21 12:06:18 -0800922 std::string tgt_method(PrettyMethod(info->index, *cu->dex_file));
923 if (tgt_method.find(" java.lang") != std::string::npos) {
924 if (tgt_method == "long java.lang.Double.doubleToRawLongBits(double)") {
925 return GenInlinedDoubleCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700926 }
buzbeefa57c472012-11-21 12:06:18 -0800927 if (tgt_method == "double java.lang.Double.longBitsToDouble(long)") {
928 return GenInlinedDoubleCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700929 }
buzbeefa57c472012-11-21 12:06:18 -0800930 if (tgt_method == "int java.lang.Float.float_to_raw_int_bits(float)") {
931 return GenInlinedFloatCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700932 }
buzbeefa57c472012-11-21 12:06:18 -0800933 if (tgt_method == "float java.lang.Float.intBitsToFloat(int)") {
934 return GenInlinedFloatCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700935 }
buzbeefa57c472012-11-21 12:06:18 -0800936 if (tgt_method == "int java.lang.Math.abs(int)" ||
937 tgt_method == "int java.lang.StrictMath.abs(int)") {
938 return GenInlinedAbsInt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700939 }
buzbeefa57c472012-11-21 12:06:18 -0800940 if (tgt_method == "long java.lang.Math.abs(long)" ||
941 tgt_method == "long java.lang.StrictMath.abs(long)") {
942 return GenInlinedAbsLong(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700943 }
buzbeefa57c472012-11-21 12:06:18 -0800944 if (tgt_method == "int java.lang.Math.max(int, int)" ||
945 tgt_method == "int java.lang.StrictMath.max(int, int)") {
946 return GenInlinedMinMaxInt(cu, info, false /* is_min */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700947 }
buzbeefa57c472012-11-21 12:06:18 -0800948 if (tgt_method == "int java.lang.Math.min(int, int)" ||
949 tgt_method == "int java.lang.StrictMath.min(int, int)") {
950 return GenInlinedMinMaxInt(cu, info, true /* is_min */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700951 }
buzbeefa57c472012-11-21 12:06:18 -0800952 if (tgt_method == "double java.lang.Math.sqrt(double)" ||
953 tgt_method == "double java.lang.StrictMath.sqrt(double)") {
954 return GenInlinedSqrt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700955 }
buzbeefa57c472012-11-21 12:06:18 -0800956 if (tgt_method == "char java.lang.String.charAt(int)") {
957 return GenInlinedCharAt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700958 }
buzbeefa57c472012-11-21 12:06:18 -0800959 if (tgt_method == "int java.lang.String.compareTo(java.lang.String)") {
960 return GenInlinedStringCompareTo(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -0700961 }
buzbeefa57c472012-11-21 12:06:18 -0800962 if (tgt_method == "boolean java.lang.String.is_empty()") {
963 return GenInlinedStringIsEmptyOrLength(cu, info, true /* is_empty */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700964 }
buzbeefa57c472012-11-21 12:06:18 -0800965 if (tgt_method == "int java.lang.String.index_of(int, int)") {
966 return GenInlinedIndexOf(cu, info, false /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700967 }
buzbeefa57c472012-11-21 12:06:18 -0800968 if (tgt_method == "int java.lang.String.index_of(int)") {
969 return GenInlinedIndexOf(cu, info, true /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700970 }
buzbeefa57c472012-11-21 12:06:18 -0800971 if (tgt_method == "int java.lang.String.length()") {
972 return GenInlinedStringIsEmptyOrLength(cu, info, false /* is_empty */);
Ian Rogers0183dd72012-09-17 23:06:51 -0700973 }
buzbeefa57c472012-11-21 12:06:18 -0800974 } else if (tgt_method.find("boolean sun.misc.Unsafe.compareAndSwap") != std::string::npos) {
975 if (tgt_method == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
976 return GenInlinedCas32(cu, info, false);
Ian Rogers0183dd72012-09-17 23:06:51 -0700977 }
buzbeefa57c472012-11-21 12:06:18 -0800978 if (tgt_method == "boolean sun.misc.Unsafe.compareAndSwapObject(java.lang.Object, long, java.lang.Object, java.lang.Object)") {
979 return GenInlinedCas32(cu, 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
buzbeefa57c472012-11-21 12:06:18 -0800985void GenInvoke(CompilationUnit* cu, CallInfo* info)
buzbee1bc37c62012-11-20 13:35:41 -0800986{
buzbeefa57c472012-11-21 12:06:18 -0800987 if (GenIntrinsic(cu, info)) {
buzbee1bc37c62012-11-20 13:35:41 -0800988 return;
989 }
buzbeefa57c472012-11-21 12:06:18 -0800990 InvokeType original_type = info->type; // avoiding mutation by ComputeInvokeInfo
991 int call_state = 0;
992 LIR* null_ck;
993 LIR** p_null_ck = NULL;
994 NextCallInsn next_call_insn;
995 FlushAllRegs(cu); /* Everything to home location */
buzbee1bc37c62012-11-20 13:35:41 -0800996 // Explicit register usage
buzbeefa57c472012-11-21 12:06:18 -0800997 LockCallTemps(cu);
buzbee1bc37c62012-11-20 13:35:41 -0800998
buzbeefa57c472012-11-21 12:06:18 -0800999 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
1000 *cu->dex_file,
1001 cu->code_item, cu->method_idx,
1002 cu->access_flags);
buzbee1bc37c62012-11-20 13:35:41 -08001003
buzbeefa57c472012-11-21 12:06:18 -08001004 uint32_t dex_method_idx = info->index;
1005 int vtable_idx;
1006 uintptr_t direct_code;
1007 uintptr_t direct_method;
1008 bool skip_this;
1009 bool fast_path =
1010 cu->compiler->ComputeInvokeInfo(dex_method_idx, &m_unit, info->type,
1011 vtable_idx, direct_code,
1012 direct_method)
buzbee1bc37c62012-11-20 13:35:41 -08001013 && !SLOW_INVOKE_PATH;
1014 if (info->type == kInterface) {
buzbeefa57c472012-11-21 12:06:18 -08001015 if (fast_path) {
1016 p_null_ck = &null_ck;
buzbee1bc37c62012-11-20 13:35:41 -08001017 }
buzbeefa57c472012-11-21 12:06:18 -08001018 next_call_insn = fast_path ? NextInterfaceCallInsn
buzbee52a77fc2012-11-20 19:50:46 -08001019 : NextInterfaceCallInsnWithAccessCheck;
buzbeefa57c472012-11-21 12:06:18 -08001020 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001021 } else if (info->type == kDirect) {
buzbeefa57c472012-11-21 12:06:18 -08001022 if (fast_path) {
1023 p_null_ck = &null_ck;
buzbee1bc37c62012-11-20 13:35:41 -08001024 }
buzbeefa57c472012-11-21 12:06:18 -08001025 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1026 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001027 } else if (info->type == kStatic) {
buzbeefa57c472012-11-21 12:06:18 -08001028 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1029 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001030 } else if (info->type == kSuper) {
buzbeefa57c472012-11-21 12:06:18 -08001031 DCHECK(!fast_path); // Fast path is a direct call.
1032 next_call_insn = NextSuperCallInsnSP;
1033 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001034 } else {
1035 DCHECK_EQ(info->type, kVirtual);
buzbeefa57c472012-11-21 12:06:18 -08001036 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1037 skip_this = fast_path;
buzbee1bc37c62012-11-20 13:35:41 -08001038 }
buzbeefa57c472012-11-21 12:06:18 -08001039 if (!info->is_range) {
1040 call_state = GenDalvikArgsNoRange(cu, info, call_state, p_null_ck,
1041 next_call_insn, dex_method_idx,
1042 vtable_idx, direct_code, direct_method,
1043 original_type, skip_this);
buzbee1bc37c62012-11-20 13:35:41 -08001044 } else {
buzbeefa57c472012-11-21 12:06:18 -08001045 call_state = GenDalvikArgsRange(cu, info, call_state, p_null_ck,
1046 next_call_insn, dex_method_idx, vtable_idx,
1047 direct_code, direct_method, original_type,
1048 skip_this);
buzbee1bc37c62012-11-20 13:35:41 -08001049 }
1050 // Finish up any of the call sequence not interleaved in arg loading
buzbeefa57c472012-11-21 12:06:18 -08001051 while (call_state >= 0) {
1052 call_state = next_call_insn(cu, info, call_state, dex_method_idx,
1053 vtable_idx, direct_code, direct_method,
1054 original_type);
buzbee1bc37c62012-11-20 13:35:41 -08001055 }
buzbeefa57c472012-11-21 12:06:18 -08001056 if (cu->enable_debug & (1 << kDebugDisplayMissingTargets)) {
1057 GenShowTarget(cu);
buzbee1bc37c62012-11-20 13:35:41 -08001058 }
buzbeefa57c472012-11-21 12:06:18 -08001059 LIR* call_inst;
1060 if (cu->instruction_set != kX86) {
1061 call_inst = OpReg(cu, kOpBlx, TargetReg(kInvokeTgt));
buzbee1bc37c62012-11-20 13:35:41 -08001062 } else {
buzbeefa57c472012-11-21 12:06:18 -08001063 if (fast_path && info->type != kInterface) {
1064 call_inst = OpMem(cu, 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:
buzbeefa57c472012-11-21 12:06:18 -08001070 trampoline = fast_path ? ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline)
buzbee1bc37c62012-11-20 13:35:41 -08001071 : 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 }
buzbeefa57c472012-11-21 12:06:18 -08001088 call_inst = OpThreadMem(cu, kOpBlx, trampoline);
buzbee1bc37c62012-11-20 13:35:41 -08001089 }
1090 }
buzbeefa57c472012-11-21 12:06:18 -08001091 MarkSafepointPC(cu, call_inst);
buzbee1bc37c62012-11-20 13:35:41 -08001092
buzbeefa57c472012-11-21 12:06:18 -08001093 ClobberCalleeSave(cu);
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) {
buzbeefa57c472012-11-21 12:06:18 -08001097 RegLocation ret_loc = GetReturnWide(cu, info->result.fp);
1098 StoreValueWide(cu, info->result, ret_loc);
buzbee1bc37c62012-11-20 13:35:41 -08001099 } else {
buzbeefa57c472012-11-21 12:06:18 -08001100 RegLocation ret_loc = GetReturn(cu, info->result.fp);
1101 StoreValue(cu, info->result, ret_loc);
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 */
buzbeefa57c472012-11-21 12:06:18 -08001112CallInfo* NewMemCallInfo(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
1113 InvokeType type, bool is_range)
buzbee1bc37c62012-11-20 13:35:41 -08001114{
buzbeefa57c472012-11-21 12:06:18 -08001115 CallInfo* info = static_cast<CallInfo*>(NewMem(cu, sizeof(CallInfo), true, kAllocMisc));
1116 MIR* move_result_mir = FindMoveResult(cu, bb, mir);
1117 if (move_result_mir == NULL) {
buzbee1bc37c62012-11-20 13:35:41 -08001118 info->result.location = kLocInvalid;
1119 } else {
buzbeefa57c472012-11-21 12:06:18 -08001120 info->result = GetRawDest(cu, move_result_mir);
1121 move_result_mir->dalvikInsn.opcode = Instruction::NOP;
buzbee1bc37c62012-11-20 13:35:41 -08001122 }
buzbeefa57c472012-11-21 12:06:18 -08001123 info->num_arg_words = mir->ssa_rep->num_uses;
1124 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
1125 (NewMem(cu, sizeof(RegLocation) * info->num_arg_words, false, kAllocMisc));
1126 for (int i = 0; i < info->num_arg_words; i++) {
1127 info->args[i] = GetRawSrc(cu, mir, i);
buzbee1bc37c62012-11-20 13:35:41 -08001128 }
buzbeefa57c472012-11-21 12:06:18 -08001129 info->opt_flags = mir->optimization_flags;
buzbee1bc37c62012-11-20 13:35:41 -08001130 info->type = type;
buzbeefa57c472012-11-21 12:06:18 -08001131 info->is_range = is_range;
buzbee1bc37c62012-11-20 13:35:41 -08001132 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