blob: 78425c40bdd079b6aa0ceacb4dce401af9e94c4e [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 Rogers07ec8e12012-12-01 01:26:51 -080021#include "x86/codegen_x86.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022
buzbee31a4a6f2012-02-28 15:36:15 -080023namespace art {
24
25/*
26 * This source files contains "gen" codegen routines that should
27 * be applicable to most targets. Only mid-level support utilities
28 * and "op" calls may be used here.
29 */
30
buzbee31a4a6f2012-02-28 15:36:15 -080031/*
buzbee02031b12012-11-23 09:41:35 -080032 * To save scheduling time, helper calls are broken into two parts: generation of
33 * the helper target address, and the actuall call to the helper. Because x86
34 * has a memory call operation, part 1 is a NOP for x86. For other targets,
35 * load arguments between the two parts.
36 */
37int Codegen::CallHelperSetup(CompilationUnit* cu, int helper_offset)
38{
39 return (cu->instruction_set == kX86) ? 0 : LoadHelper(cu, helper_offset);
40}
41
42/* NOTE: if r_tgt is a temp, it will be freed following use */
43LIR* Codegen::CallHelper(CompilationUnit* cu, int r_tgt, int helper_offset, bool safepoint_pc)
44{
45 LIR* call_inst;
46 if (cu->instruction_set == kX86) {
47 call_inst = OpThreadMem(cu, kOpBlx, helper_offset);
48 } else {
49 call_inst = OpReg(cu, kOpBlx, r_tgt);
50 FreeTemp(cu, r_tgt);
51 }
52 if (safepoint_pc) {
53 MarkSafepointPC(cu, call_inst);
54 }
55 return call_inst;
56}
57
58void Codegen::CallRuntimeHelperImm(CompilationUnit* cu, int helper_offset, int arg0,
59 bool safepoint_pc) {
60 int r_tgt = CallHelperSetup(cu, helper_offset);
61 LoadConstant(cu, TargetReg(kArg0), arg0);
62 ClobberCalleeSave(cu);
63 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
64}
65
66void Codegen::CallRuntimeHelperReg(CompilationUnit* cu, int helper_offset, int arg0,
67 bool safepoint_pc) {
68 int r_tgt = CallHelperSetup(cu, helper_offset);
69 OpRegCopy(cu, TargetReg(kArg0), arg0);
70 ClobberCalleeSave(cu);
71 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
72}
73
74void Codegen::CallRuntimeHelperRegLocation(CompilationUnit* cu, int helper_offset, RegLocation arg0,
75 bool safepoint_pc) {
76 int r_tgt = CallHelperSetup(cu, helper_offset);
77 if (arg0.wide == 0) {
78 LoadValueDirectFixed(cu, arg0, TargetReg(kArg0));
79 } else {
80 LoadValueDirectWideFixed(cu, arg0, TargetReg(kArg0), TargetReg(kArg1));
81 }
82 ClobberCalleeSave(cu);
83 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
84}
85
86void Codegen::CallRuntimeHelperImmImm(CompilationUnit* cu, int helper_offset, int arg0, int arg1,
87 bool safepoint_pc) {
88 int r_tgt = CallHelperSetup(cu, helper_offset);
89 LoadConstant(cu, TargetReg(kArg0), arg0);
90 LoadConstant(cu, TargetReg(kArg1), arg1);
91 ClobberCalleeSave(cu);
92 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
93}
94
95void Codegen::CallRuntimeHelperImmRegLocation(CompilationUnit* cu, int helper_offset, int arg0,
96 RegLocation arg1, bool safepoint_pc) {
97 int r_tgt = CallHelperSetup(cu, helper_offset);
98 if (arg1.wide == 0) {
99 LoadValueDirectFixed(cu, arg1, TargetReg(kArg1));
100 } else {
101 LoadValueDirectWideFixed(cu, arg1, TargetReg(kArg1), TargetReg(kArg2));
102 }
103 LoadConstant(cu, TargetReg(kArg0), arg0);
104 ClobberCalleeSave(cu);
105 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
106}
107
108void Codegen::CallRuntimeHelperRegLocationImm(CompilationUnit* cu, int helper_offset,
109 RegLocation arg0, int arg1, bool safepoint_pc) {
110 int r_tgt = CallHelperSetup(cu, helper_offset);
111 LoadValueDirectFixed(cu, arg0, TargetReg(kArg0));
112 LoadConstant(cu, TargetReg(kArg1), arg1);
113 ClobberCalleeSave(cu);
114 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
115}
116
117void Codegen::CallRuntimeHelperImmReg(CompilationUnit* cu, int helper_offset, int arg0, int arg1,
118 bool safepoint_pc) {
119 int r_tgt = CallHelperSetup(cu, helper_offset);
120 OpRegCopy(cu, TargetReg(kArg1), arg1);
121 LoadConstant(cu, TargetReg(kArg0), arg0);
122 ClobberCalleeSave(cu);
123 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
124}
125
126void Codegen::CallRuntimeHelperRegImm(CompilationUnit* cu, int helper_offset, int arg0, int arg1,
127 bool safepoint_pc) {
128 int r_tgt = CallHelperSetup(cu, helper_offset);
129 OpRegCopy(cu, TargetReg(kArg0), arg0);
130 LoadConstant(cu, TargetReg(kArg1), arg1);
131 ClobberCalleeSave(cu);
132 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
133}
134
135void Codegen::CallRuntimeHelperImmMethod(CompilationUnit* cu, int helper_offset, int arg0,
136 bool safepoint_pc) {
137 int r_tgt = CallHelperSetup(cu, helper_offset);
138 LoadCurrMethodDirect(cu, TargetReg(kArg1));
139 LoadConstant(cu, TargetReg(kArg0), arg0);
140 ClobberCalleeSave(cu);
141 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
142}
143
144void Codegen::CallRuntimeHelperRegLocationRegLocation(CompilationUnit* cu, int helper_offset,
145 RegLocation arg0, RegLocation arg1,
146 bool safepoint_pc) {
147 int r_tgt = CallHelperSetup(cu, helper_offset);
148 if (arg0.wide == 0) {
149 LoadValueDirectFixed(cu, arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
150 if (arg1.wide == 0) {
151 if (cu->instruction_set == kMips) {
152 LoadValueDirectFixed(cu, arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1));
153 } else {
154 LoadValueDirectFixed(cu, arg1, TargetReg(kArg1));
155 }
156 } else {
157 if (cu->instruction_set == kMips) {
158 LoadValueDirectWideFixed(cu, arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg1), arg1.fp ? TargetReg(kFArg3) : TargetReg(kArg2));
159 } else {
160 LoadValueDirectWideFixed(cu, arg1, TargetReg(kArg1), TargetReg(kArg2));
161 }
162 }
163 } else {
164 LoadValueDirectWideFixed(cu, arg0, arg0.fp ? TargetReg(kFArg0) : TargetReg(kArg0), arg0.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
165 if (arg1.wide == 0) {
166 LoadValueDirectFixed(cu, arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2));
167 } else {
168 LoadValueDirectWideFixed(cu, arg1, arg1.fp ? TargetReg(kFArg2) : TargetReg(kArg2), arg1.fp ? TargetReg(kFArg3) : TargetReg(kArg3));
169 }
170 }
171 ClobberCalleeSave(cu);
172 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
173}
174
175void Codegen::CallRuntimeHelperRegReg(CompilationUnit* cu, int helper_offset, int arg0, int arg1,
176 bool safepoint_pc) {
177 int r_tgt = CallHelperSetup(cu, helper_offset);
178 DCHECK_NE(TargetReg(kArg0), arg1); // check copy into arg0 won't clobber arg1
179 OpRegCopy(cu, TargetReg(kArg0), arg0);
180 OpRegCopy(cu, TargetReg(kArg1), arg1);
181 ClobberCalleeSave(cu);
182 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
183}
184
185void Codegen::CallRuntimeHelperRegRegImm(CompilationUnit* cu, int helper_offset, int arg0, int arg1,
186 int arg2, bool safepoint_pc) {
187 int r_tgt = CallHelperSetup(cu, helper_offset);
188 DCHECK_NE(TargetReg(kArg0), arg1); // check copy into arg0 won't clobber arg1
189 OpRegCopy(cu, TargetReg(kArg0), arg0);
190 OpRegCopy(cu, TargetReg(kArg1), arg1);
191 LoadConstant(cu, TargetReg(kArg2), arg2);
192 ClobberCalleeSave(cu);
193 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
194}
195
196void Codegen::CallRuntimeHelperImmMethodRegLocation(CompilationUnit* cu, int helper_offset,
197 int arg0, RegLocation arg2, bool safepoint_pc) {
198 int r_tgt = CallHelperSetup(cu, helper_offset);
199 LoadValueDirectFixed(cu, arg2, TargetReg(kArg2));
200 LoadCurrMethodDirect(cu, TargetReg(kArg1));
201 LoadConstant(cu, TargetReg(kArg0), arg0);
202 ClobberCalleeSave(cu);
203 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
204}
205
206void Codegen::CallRuntimeHelperImmMethodImm(CompilationUnit* cu, int helper_offset, int arg0,
207 int arg2, bool safepoint_pc) {
208 int r_tgt = CallHelperSetup(cu, helper_offset);
209 LoadCurrMethodDirect(cu, TargetReg(kArg1));
210 LoadConstant(cu, TargetReg(kArg2), arg2);
211 LoadConstant(cu, TargetReg(kArg0), arg0);
212 ClobberCalleeSave(cu);
213 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
214}
215
216void Codegen::CallRuntimeHelperImmRegLocationRegLocation(CompilationUnit* cu, int helper_offset,
217 int arg0, RegLocation arg1,
218 RegLocation arg2, bool safepoint_pc) {
219 int r_tgt = CallHelperSetup(cu, helper_offset);
220 LoadValueDirectFixed(cu, arg1, TargetReg(kArg1));
221 if (arg2.wide == 0) {
222 LoadValueDirectFixed(cu, arg2, TargetReg(kArg2));
223 } else {
224 LoadValueDirectWideFixed(cu, arg2, TargetReg(kArg2), TargetReg(kArg3));
225 }
226 LoadConstant(cu, TargetReg(kArg0), arg0);
227 ClobberCalleeSave(cu);
228 CallHelper(cu, r_tgt, helper_offset, safepoint_pc);
229}
230
231/*
buzbee31a4a6f2012-02-28 15:36:15 -0800232 * If there are any ins passed in registers that have not been promoted
233 * to a callee-save register, flush them to the frame. Perform intial
234 * assignment of promoted arguments.
buzbeead8f15e2012-06-18 14:49:45 -0700235 *
buzbee52a77fc2012-11-20 19:50:46 -0800236 * ArgLocs is an array of location records describing the incoming arguments
buzbeead8f15e2012-06-18 14:49:45 -0700237 * with one location record per word of argument.
buzbee31a4a6f2012-02-28 15:36:15 -0800238 */
buzbee02031b12012-11-23 09:41:35 -0800239void Codegen::FlushIns(CompilationUnit* cu, RegLocation* ArgLocs, RegLocation rl_method)
buzbee31a4a6f2012-02-28 15:36:15 -0800240{
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 /*
242 * Dummy up a RegLocation for the incoming Method*
buzbeef0504cd2012-11-13 16:31:10 -0800243 * It will attempt to keep kArg0 live (or copy it to home location
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 * if promoted).
245 */
buzbeefa57c472012-11-21 12:06:18 -0800246 RegLocation rl_src = rl_method;
247 rl_src.location = kLocPhysReg;
248 rl_src.low_reg = TargetReg(kArg0);
249 rl_src.home = false;
250 MarkLive(cu, rl_src.low_reg, rl_src.s_reg_low);
251 StoreValue(cu, rl_method, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 // If Method* has been promoted, explicitly flush
buzbeefa57c472012-11-21 12:06:18 -0800253 if (rl_method.location == kLocPhysReg) {
254 StoreWordDisp(cu, TargetReg(kSp), 0, TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 }
buzbee9c044ce2012-03-18 13:24:07 -0700256
buzbeefa57c472012-11-21 12:06:18 -0800257 if (cu->num_ins == 0)
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 return;
buzbeefa57c472012-11-21 12:06:18 -0800259 const int num_arg_regs = 3;
260 static SpecialTargetRegister arg_regs[] = {kArg1, kArg2, kArg3};
261 int start_vreg = cu->num_dalvik_registers - cu->num_ins;
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 /*
263 * Copy incoming arguments to their proper home locations.
264 * NOTE: an older version of dx had an issue in which
265 * it would reuse static method argument registers.
266 * This could result in the same Dalvik virtual register
267 * being promoted to both core and fp regs. To account for this,
268 * we only copy to the corresponding promoted physical register
269 * if it matches the type of the SSA name for the incoming
270 * argument. It is also possible that long and double arguments
271 * end up half-promoted. In those cases, we must flush the promoted
272 * half to memory as well.
273 */
buzbeefa57c472012-11-21 12:06:18 -0800274 for (int i = 0; i < cu->num_ins; i++) {
275 PromotionMap* v_map = &cu->promotion_map[start_vreg + i];
276 if (i < num_arg_regs) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 // If arriving in register
buzbeefa57c472012-11-21 12:06:18 -0800278 bool need_flush = true;
279 RegLocation* t_loc = &ArgLocs[i];
280 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
281 OpRegCopy(cu, v_map->core_reg, TargetReg(arg_regs[i]));
282 need_flush = false;
283 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
284 OpRegCopy(cu, v_map->FpReg, TargetReg(arg_regs[i]));
285 need_flush = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 } else {
buzbeefa57c472012-11-21 12:06:18 -0800287 need_flush = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 }
buzbee86a4bce2012-03-06 18:15:00 -0800289
Bill Buzbeea114add2012-05-03 15:00:40 -0700290 // For wide args, force flush if only half is promoted
buzbeefa57c472012-11-21 12:06:18 -0800291 if (t_loc->wide) {
292 PromotionMap* p_map = v_map + (t_loc->high_word ? -1 : +1);
293 need_flush |= (p_map->core_location != v_map->core_location) ||
294 (p_map->fp_location != v_map->fp_location);
Bill Buzbeea114add2012-05-03 15:00:40 -0700295 }
buzbeefa57c472012-11-21 12:06:18 -0800296 if (need_flush) {
297 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, start_vreg + i),
298 TargetReg(arg_regs[i]), kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700299 }
300 } else {
301 // If arriving in frame & promoted
buzbeefa57c472012-11-21 12:06:18 -0800302 if (v_map->core_location == kLocPhysReg) {
303 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, start_vreg + i),
304 v_map->core_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 }
buzbeefa57c472012-11-21 12:06:18 -0800306 if (v_map->fp_location == kLocPhysReg) {
307 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, start_vreg + i),
308 v_map->FpReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 }
buzbee31a4a6f2012-02-28 15:36:15 -0800310 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 }
buzbee31a4a6f2012-02-28 15:36:15 -0800312}
313
314/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700315 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800316 * emit the next instruction in static & direct invoke sequences.
317 */
buzbeefa57c472012-11-21 12:06:18 -0800318static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
319 int state, uint32_t dex_idx, uint32_t unused,
320 uintptr_t direct_code, uintptr_t direct_method,
buzbeeaad94382012-11-21 07:40:50 -0800321 InvokeType type)
buzbee31a4a6f2012-02-28 15:36:15 -0800322{
buzbee02031b12012-11-23 09:41:35 -0800323 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800324 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700325 // Disable sharpening
buzbeefa57c472012-11-21 12:06:18 -0800326 direct_code = 0;
327 direct_method = 0;
buzbeeb046e162012-10-30 15:48:42 -0700328 }
buzbeefa57c472012-11-21 12:06:18 -0800329 if (direct_code != 0 && direct_method != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800331 case 0: // Get the current Method* [sets kArg0]
buzbeefa57c472012-11-21 12:06:18 -0800332 if (direct_code != static_cast<unsigned int>(-1)) {
buzbee02031b12012-11-23 09:41:35 -0800333 cg->LoadConstant(cu, cg->TargetReg(kInvokeTgt), direct_code);
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 } else {
buzbeefa57c472012-11-21 12:06:18 -0800335 LIR* data_target = ScanLiteralPool(cu->code_literal_list, dex_idx, 0);
336 if (data_target == NULL) {
337 data_target = AddWordData(cu, &cu->code_literal_list, dex_idx);
338 data_target->operands[1] = type;
Ian Rogers2ed3b952012-03-17 11:49:39 -0700339 }
buzbee02031b12012-11-23 09:41:35 -0800340 LIR* load_pc_rel = cg->OpPcRelLoad(cu, cg->TargetReg(kInvokeTgt), data_target);
buzbeefa57c472012-11-21 12:06:18 -0800341 AppendLIR(cu, load_pc_rel);
342 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 }
buzbeefa57c472012-11-21 12:06:18 -0800344 if (direct_method != static_cast<unsigned int>(-1)) {
buzbee02031b12012-11-23 09:41:35 -0800345 cg->LoadConstant(cu, cg->TargetReg(kArg0), direct_method);
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 } else {
buzbeefa57c472012-11-21 12:06:18 -0800347 LIR* data_target = ScanLiteralPool(cu->method_literal_list, dex_idx, 0);
348 if (data_target == NULL) {
349 data_target = AddWordData(cu, &cu->method_literal_list, dex_idx);
350 data_target->operands[1] = type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 }
buzbee02031b12012-11-23 09:41:35 -0800352 LIR* load_pc_rel = cg->OpPcRelLoad(cu, cg->TargetReg(kArg0), data_target);
buzbeefa57c472012-11-21 12:06:18 -0800353 AppendLIR(cu, load_pc_rel);
354 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 }
356 break;
357 default:
358 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800359 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 } else {
361 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800362 case 0: // Get the current Method* [sets kArg0]
Ian Rogers137e88f2012-10-08 17:46:47 -0700363 // TUNING: we can save a reg copy if Method* has been promoted.
buzbee02031b12012-11-23 09:41:35 -0800364 cg->LoadCurrMethodDirect(cu, cg->TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 break;
366 case 1: // Get method->dex_cache_resolved_methods_
buzbee02031b12012-11-23 09:41:35 -0800367 cg->LoadWordDisp(cu, cg->TargetReg(kArg0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 mirror::AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(), cg->TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 // Set up direct code if known.
buzbeefa57c472012-11-21 12:06:18 -0800370 if (direct_code != 0) {
371 if (direct_code != static_cast<unsigned int>(-1)) {
buzbee02031b12012-11-23 09:41:35 -0800372 cg->LoadConstant(cu, cg->TargetReg(kInvokeTgt), direct_code);
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 } else {
buzbeefa57c472012-11-21 12:06:18 -0800374 LIR* data_target = ScanLiteralPool(cu->code_literal_list, dex_idx, 0);
375 if (data_target == NULL) {
376 data_target = AddWordData(cu, &cu->code_literal_list, dex_idx);
377 data_target->operands[1] = type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 }
buzbee02031b12012-11-23 09:41:35 -0800379 LIR* load_pc_rel = cg->OpPcRelLoad(cu, cg->TargetReg(kInvokeTgt), data_target);
buzbeefa57c472012-11-21 12:06:18 -0800380 AppendLIR(cu, load_pc_rel);
381 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 }
383 }
384 break;
385 case 2: // Grab target method*
buzbee02031b12012-11-23 09:41:35 -0800386 cg->LoadWordDisp(cu, cg->TargetReg(kArg0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800387 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() + dex_idx * 4,
buzbee02031b12012-11-23 09:41:35 -0800388 cg-> TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 case 3: // Grab the code from the method*
buzbeefa57c472012-11-21 12:06:18 -0800391 if (cu->instruction_set != kX86) {
392 if (direct_code == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 cg->LoadWordDisp(cu, cg->TargetReg(kArg0),
394 mirror::AbstractMethod::GetCodeOffset().Int32Value(),
buzbee02031b12012-11-23 09:41:35 -0800395 cg->TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700396 }
397 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 }
buzbeeb046e162012-10-30 15:48:42 -0700399 // Intentional fallthrough for x86
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 default:
401 return -1;
402 }
403 }
404 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800405}
406
407/*
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700408 * Bit of a hack here - in the absence of a real scheduling pass,
buzbee31a4a6f2012-02-28 15:36:15 -0800409 * emit the next instruction in a virtual invoke sequence.
buzbeef0504cd2012-11-13 16:31:10 -0800410 * We can use kLr as a temp prior to target address loading
buzbee31a4a6f2012-02-28 15:36:15 -0800411 * Note also that we'll load the first argument ("this") into
buzbee52a77fc2012-11-20 19:50:46 -0800412 * kArg1 here rather than the standard LoadArgRegs.
buzbee31a4a6f2012-02-28 15:36:15 -0800413 */
buzbeefa57c472012-11-21 12:06:18 -0800414static int NextVCallInsn(CompilationUnit* cu, CallInfo* info,
415 int state, uint32_t dex_idx, uint32_t method_idx,
buzbeeaad94382012-11-21 07:40:50 -0800416 uintptr_t unused, uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800417{
buzbee02031b12012-11-23 09:41:35 -0800418 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 /*
420 * This is the fast path in which the target virtual method is
421 * fully resolved at compile time.
422 */
423 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800424 case 0: { // Get "this" [set kArg1]
buzbeefa57c472012-11-21 12:06:18 -0800425 RegLocation rl_arg = info->args[0];
buzbee02031b12012-11-23 09:41:35 -0800426 cg->LoadValueDirectFixed(cu, rl_arg, cg->TargetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 break;
Ian Rogers137e88f2012-10-08 17:46:47 -0700428 }
buzbeef0504cd2012-11-13 16:31:10 -0800429 case 1: // Is "this" null? [use kArg1]
buzbee02031b12012-11-23 09:41:35 -0800430 cg->GenNullCheck(cu, info->args[0].s_reg_low, cg->TargetReg(kArg1), info->opt_flags);
buzbeef0504cd2012-11-13 16:31:10 -0800431 // get this->klass_ [use kArg1, set kInvokeTgt]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 cg->LoadWordDisp(cu, cg->TargetReg(kArg1), mirror::Object::ClassOffset().Int32Value(),
buzbee02031b12012-11-23 09:41:35 -0800433 cg->TargetReg(kInvokeTgt));
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 break;
buzbeef0504cd2012-11-13 16:31:10 -0800435 case 2: // Get this->klass_->vtable [usr kInvokeTgt, set kInvokeTgt]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436 cg->LoadWordDisp(cu, cg->TargetReg(kInvokeTgt), mirror::Class::VTableOffset().Int32Value(),
buzbee02031b12012-11-23 09:41:35 -0800437 cg->TargetReg(kInvokeTgt));
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 break;
buzbeef0504cd2012-11-13 16:31:10 -0800439 case 3: // Get target method [use kInvokeTgt, set kArg0]
buzbee02031b12012-11-23 09:41:35 -0800440 cg->LoadWordDisp(cu, cg->TargetReg(kInvokeTgt), (method_idx * 4) +
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value(),
442 cg->TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700443 break;
buzbeef0504cd2012-11-13 16:31:10 -0800444 case 4: // Get the compiled code address [uses kArg0, sets kInvokeTgt]
buzbeefa57c472012-11-21 12:06:18 -0800445 if (cu->instruction_set != kX86) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446 cg->LoadWordDisp(cu, cg->TargetReg(kArg0),
447 mirror::AbstractMethod::GetCodeOffset().Int32Value(),
buzbee02031b12012-11-23 09:41:35 -0800448 cg->TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700449 break;
450 }
451 // Intentional fallthrough for X86
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 default:
453 return -1;
454 }
455 return state + 1;
buzbee31a4a6f2012-02-28 15:36:15 -0800456}
457
Ian Rogers137e88f2012-10-08 17:46:47 -0700458/*
459 * All invoke-interface calls bounce off of art_invoke_interface_trampoline,
460 * which will locate the target and continue on via a tail call.
461 */
buzbeefa57c472012-11-21 12:06:18 -0800462static int NextInterfaceCallInsn(CompilationUnit* cu, CallInfo* info, int state,
463 uint32_t dex_idx, uint32_t unused, uintptr_t unused2,
464 uintptr_t direct_method, InvokeType unused4)
Ian Rogers137e88f2012-10-08 17:46:47 -0700465{
buzbee02031b12012-11-23 09:41:35 -0800466 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800467 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700468 // Disable sharpening
buzbeefa57c472012-11-21 12:06:18 -0800469 direct_method = 0;
buzbeeb046e162012-10-30 15:48:42 -0700470 }
buzbeefa57c472012-11-21 12:06:18 -0800471 int trampoline = (cu->instruction_set == kX86) ? 0
buzbeeb046e162012-10-30 15:48:42 -0700472 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline);
Ian Rogers137e88f2012-10-08 17:46:47 -0700473
buzbeefa57c472012-11-21 12:06:18 -0800474 if (direct_method != 0) {
Ian Rogers137e88f2012-10-08 17:46:47 -0700475 switch (state) {
buzbeef0504cd2012-11-13 16:31:10 -0800476 case 0: // Load the trampoline target [sets kInvokeTgt].
buzbeefa57c472012-11-21 12:06:18 -0800477 if (cu->instruction_set != kX86) {
buzbee02031b12012-11-23 09:41:35 -0800478 cg->LoadWordDisp(cu, cg->TargetReg(kSelf), trampoline, cg->TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700479 }
buzbeef0504cd2012-11-13 16:31:10 -0800480 // Get the interface Method* [sets kArg0]
buzbeefa57c472012-11-21 12:06:18 -0800481 if (direct_method != static_cast<unsigned int>(-1)) {
buzbee02031b12012-11-23 09:41:35 -0800482 cg->LoadConstant(cu, cg->TargetReg(kArg0), direct_method);
Ian Rogers137e88f2012-10-08 17:46:47 -0700483 } else {
buzbeefa57c472012-11-21 12:06:18 -0800484 LIR* data_target = ScanLiteralPool(cu->method_literal_list, dex_idx, 0);
485 if (data_target == NULL) {
486 data_target = AddWordData(cu, &cu->method_literal_list, dex_idx);
487 data_target->operands[1] = kInterface;
Ian Rogers137e88f2012-10-08 17:46:47 -0700488 }
buzbee02031b12012-11-23 09:41:35 -0800489 LIR* load_pc_rel = cg->OpPcRelLoad(cu, cg->TargetReg(kArg0), data_target);
buzbeefa57c472012-11-21 12:06:18 -0800490 AppendLIR(cu, load_pc_rel);
491 DCHECK_EQ(cu->instruction_set, kThumb2) << reinterpret_cast<void*>(data_target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700492 }
493 break;
494 default:
495 return -1;
496 }
497 } else {
498 switch (state) {
499 case 0:
buzbeef0504cd2012-11-13 16:31:10 -0800500 // Get the current Method* [sets kArg0] - TUNING: remove copy of method if it is promoted.
buzbee02031b12012-11-23 09:41:35 -0800501 cg->LoadCurrMethodDirect(cu, cg->TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800502 // Load the trampoline target [sets kInvokeTgt].
buzbeefa57c472012-11-21 12:06:18 -0800503 if (cu->instruction_set != kX86) {
buzbee02031b12012-11-23 09:41:35 -0800504 cg->LoadWordDisp(cu, cg->TargetReg(kSelf), trampoline, cg->TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700505 }
Ian Rogers137e88f2012-10-08 17:46:47 -0700506 break;
buzbeef0504cd2012-11-13 16:31:10 -0800507 case 1: // Get method->dex_cache_resolved_methods_ [set/use kArg0]
buzbee02031b12012-11-23 09:41:35 -0800508 cg->LoadWordDisp(cu, cg->TargetReg(kArg0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800509 mirror::AbstractMethod::DexCacheResolvedMethodsOffset().Int32Value(),
buzbee02031b12012-11-23 09:41:35 -0800510 cg->TargetReg(kArg0));
Ian Rogers137e88f2012-10-08 17:46:47 -0700511 break;
buzbeef0504cd2012-11-13 16:31:10 -0800512 case 2: // Grab target method* [set/use kArg0]
buzbee02031b12012-11-23 09:41:35 -0800513 cg->LoadWordDisp(cu, cg->TargetReg(kArg0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() + dex_idx * 4,
buzbee02031b12012-11-23 09:41:35 -0800515 cg->TargetReg(kArg0));
Ian Rogers137e88f2012-10-08 17:46:47 -0700516 break;
517 default:
518 return -1;
519 }
520 }
521 return state + 1;
522}
523
buzbeefa57c472012-11-21 12:06:18 -0800524static int NextInvokeInsnSP(CompilationUnit* cu, CallInfo* info, int trampoline,
525 int state, uint32_t dex_idx, uint32_t method_idx)
buzbee31a4a6f2012-02-28 15:36:15 -0800526{
buzbee02031b12012-11-23 09:41:35 -0800527 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 /*
529 * This handles the case in which the base method is not fully
530 * resolved at compile time, we bail to a runtime helper.
531 */
532 if (state == 0) {
buzbeefa57c472012-11-21 12:06:18 -0800533 if (cu->instruction_set != kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700534 // Load trampoline target
buzbee02031b12012-11-23 09:41:35 -0800535 cg->LoadWordDisp(cu, cg->TargetReg(kSelf), trampoline, cg->TargetReg(kInvokeTgt));
buzbeeb046e162012-10-30 15:48:42 -0700536 }
buzbeef0504cd2012-11-13 16:31:10 -0800537 // Load kArg0 with method index
buzbee02031b12012-11-23 09:41:35 -0800538 cg->LoadConstant(cu, cg->TargetReg(kArg0), dex_idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700539 return 1;
540 }
541 return -1;
buzbee31a4a6f2012-02-28 15:36:15 -0800542}
543
buzbeefa57c472012-11-21 12:06:18 -0800544static int NextStaticCallInsnSP(CompilationUnit* cu, CallInfo* info,
545 int state, uint32_t dex_idx, uint32_t method_idx,
buzbeeaad94382012-11-21 07:40:50 -0800546 uintptr_t unused, uintptr_t unused2,
Brian Carlstromf5822582012-03-19 22:34:31 -0700547 InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800548{
Ian Rogers57b86d42012-03-27 16:05:41 -0700549 int trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
buzbeefa57c472012-11-21 12:06:18 -0800550 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800551}
552
buzbeefa57c472012-11-21 12:06:18 -0800553static int NextDirectCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
554 uint32_t dex_idx, uint32_t method_idx, uintptr_t unused,
buzbeeaad94382012-11-21 07:40:50 -0800555 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800556{
Ian Rogers57b86d42012-03-27 16:05:41 -0700557 int trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
buzbeefa57c472012-11-21 12:06:18 -0800558 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800559}
560
buzbeefa57c472012-11-21 12:06:18 -0800561static int NextSuperCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
562 uint32_t dex_idx, uint32_t method_idx, uintptr_t unused,
Brian Carlstromf5822582012-03-19 22:34:31 -0700563 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800564{
Ian Rogers57b86d42012-03-27 16:05:41 -0700565 int trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
buzbeefa57c472012-11-21 12:06:18 -0800566 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800567}
568
buzbeefa57c472012-11-21 12:06:18 -0800569static int NextVCallInsnSP(CompilationUnit* cu, CallInfo* info, int state,
570 uint32_t dex_idx, uint32_t method_idx, uintptr_t unused,
buzbeeaad94382012-11-21 07:40:50 -0800571 uintptr_t unused2, InvokeType unused3)
buzbee31a4a6f2012-02-28 15:36:15 -0800572{
Ian Rogers57b86d42012-03-27 16:05:41 -0700573 int trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
buzbeefa57c472012-11-21 12:06:18 -0800574 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800575}
576
buzbeefa57c472012-11-21 12:06:18 -0800577static int NextInterfaceCallInsnWithAccessCheck(CompilationUnit* cu,
buzbeeaad94382012-11-21 07:40:50 -0800578 CallInfo* info, int state,
buzbeefa57c472012-11-21 12:06:18 -0800579 uint32_t dex_idx, uint32_t unused,
buzbee15bf9802012-06-12 17:49:27 -0700580 uintptr_t unused2, uintptr_t unused3,
581 InvokeType unused4)
buzbee31a4a6f2012-02-28 15:36:15 -0800582{
Ian Rogers57b86d42012-03-27 16:05:41 -0700583 int trampoline = ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
buzbeefa57c472012-11-21 12:06:18 -0800584 return NextInvokeInsnSP(cu, info, trampoline, state, dex_idx, 0);
buzbee31a4a6f2012-02-28 15:36:15 -0800585}
586
buzbeefa57c472012-11-21 12:06:18 -0800587static int LoadArgRegs(CompilationUnit* cu, CallInfo* info, int call_state,
588 NextCallInsn next_call_insn, uint32_t dex_idx,
589 uint32_t method_idx, uintptr_t direct_code,
590 uintptr_t direct_method, InvokeType type, bool skip_this)
buzbee31a4a6f2012-02-28 15:36:15 -0800591{
buzbee02031b12012-11-23 09:41:35 -0800592 Codegen* cg = cu->cg.get();
593 int last_arg_reg = cg->TargetReg(kArg3);
594 int next_reg = cg->TargetReg(kArg1);
buzbeefa57c472012-11-21 12:06:18 -0800595 int next_arg = 0;
596 if (skip_this) {
597 next_reg++;
598 next_arg++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 }
buzbeefa57c472012-11-21 12:06:18 -0800600 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
601 RegLocation rl_arg = info->args[next_arg++];
602 rl_arg = UpdateRawLoc(cu, rl_arg);
buzbee02031b12012-11-23 09:41:35 -0800603 if (rl_arg.wide && (next_reg <= cg->TargetReg(kArg2))) {
604 cg->LoadValueDirectWideFixed(cu, rl_arg, next_reg, next_reg + 1);
buzbeefa57c472012-11-21 12:06:18 -0800605 next_reg++;
606 next_arg++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 } else {
buzbeee6285f92012-12-06 15:57:46 -0800608 if (rl_arg.wide) {
609 rl_arg.wide = false;
610 rl_arg.is_const = false;
611 }
buzbee02031b12012-11-23 09:41:35 -0800612 cg->LoadValueDirectFixed(cu, rl_arg, next_reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800613 }
buzbeefa57c472012-11-21 12:06:18 -0800614 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
615 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 }
buzbeefa57c472012-11-21 12:06:18 -0800617 return call_state;
buzbee31a4a6f2012-02-28 15:36:15 -0800618}
619
620/*
621 * Load up to 5 arguments, the first three of which will be in
buzbeef0504cd2012-11-13 16:31:10 -0800622 * kArg1 .. kArg3. On entry kArg0 contains the current method pointer,
buzbee31a4a6f2012-02-28 15:36:15 -0800623 * and as part of the load sequence, it must be replaced with
624 * the target method pointer. Note, this may also be called
625 * for "range" variants if the number of arguments is 5 or fewer.
626 */
buzbee02031b12012-11-23 09:41:35 -0800627int Codegen::GenDalvikArgsNoRange(CompilationUnit* cu, CallInfo* info,
628 int call_state, LIR** pcrLabel, NextCallInsn next_call_insn,
629 uint32_t dex_idx, uint32_t method_idx, uintptr_t direct_code,
630 uintptr_t direct_method, InvokeType type, bool skip_this)
buzbee31a4a6f2012-02-28 15:36:15 -0800631{
buzbeefa57c472012-11-21 12:06:18 -0800632 RegLocation rl_arg;
buzbee31a4a6f2012-02-28 15:36:15 -0800633
Bill Buzbeea114add2012-05-03 15:00:40 -0700634 /* If no arguments, just return */
buzbeefa57c472012-11-21 12:06:18 -0800635 if (info->num_arg_words == 0)
636 return call_state;
Bill Buzbeea114add2012-05-03 15:00:40 -0700637
buzbeefa57c472012-11-21 12:06:18 -0800638 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
639 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700640
buzbeefa57c472012-11-21 12:06:18 -0800641 DCHECK_LE(info->num_arg_words, 5);
642 if (info->num_arg_words > 3) {
643 int32_t next_use = 3;
Bill Buzbeea114add2012-05-03 15:00:40 -0700644 //Detect special case of wide arg spanning arg3/arg4
buzbeefa57c472012-11-21 12:06:18 -0800645 RegLocation rl_use0 = info->args[0];
646 RegLocation rl_use1 = info->args[1];
647 RegLocation rl_use2 = info->args[2];
648 if (((!rl_use0.wide && !rl_use1.wide) || rl_use0.wide) &&
649 rl_use2.wide) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700650 int reg = -1;
651 // Wide spans, we need the 2nd half of uses[2].
buzbeefa57c472012-11-21 12:06:18 -0800652 rl_arg = UpdateLocWide(cu, rl_use2);
653 if (rl_arg.location == kLocPhysReg) {
654 reg = rl_arg.high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800656 // kArg2 & rArg3 can safely be used here
buzbee52a77fc2012-11-20 19:50:46 -0800657 reg = TargetReg(kArg3);
buzbeefa57c472012-11-21 12:06:18 -0800658 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_arg.s_reg_low) + 4, reg);
659 call_state = next_call_insn(cu, info, call_state, dex_idx,
660 method_idx, direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700661 }
buzbeefa57c472012-11-21 12:06:18 -0800662 StoreBaseDisp(cu, TargetReg(kSp), (next_use + 1) * 4, reg, kWord);
663 StoreBaseDisp(cu, TargetReg(kSp), 16 /* (3+1)*4 */, reg, kWord);
664 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
665 direct_code, direct_method, type);
666 next_use++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700667 }
668 // Loop through the rest
buzbeefa57c472012-11-21 12:06:18 -0800669 while (next_use < info->num_arg_words) {
670 int low_reg;
671 int high_reg = -1;
672 rl_arg = info->args[next_use];
673 rl_arg = UpdateRawLoc(cu, rl_arg);
674 if (rl_arg.location == kLocPhysReg) {
675 low_reg = rl_arg.low_reg;
676 high_reg = rl_arg.high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700677 } else {
buzbeefa57c472012-11-21 12:06:18 -0800678 low_reg = TargetReg(kArg2);
679 if (rl_arg.wide) {
680 high_reg = TargetReg(kArg3);
681 LoadValueDirectWideFixed(cu, rl_arg, low_reg, high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700682 } else {
buzbeefa57c472012-11-21 12:06:18 -0800683 LoadValueDirectFixed(cu, rl_arg, low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700684 }
buzbeefa57c472012-11-21 12:06:18 -0800685 call_state = next_call_insn(cu, info, call_state, dex_idx,
686 method_idx, direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700687 }
buzbeefa57c472012-11-21 12:06:18 -0800688 int outs_offset = (next_use + 1) * 4;
689 if (rl_arg.wide) {
690 StoreBaseDispWide(cu, TargetReg(kSp), outs_offset, low_reg, high_reg);
691 next_use += 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700692 } else {
buzbeefa57c472012-11-21 12:06:18 -0800693 StoreWordDisp(cu, TargetReg(kSp), outs_offset, low_reg);
694 next_use++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 }
buzbeefa57c472012-11-21 12:06:18 -0800696 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
697 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 }
699 }
700
buzbeefa57c472012-11-21 12:06:18 -0800701 call_state = LoadArgRegs(cu, info, call_state, next_call_insn,
702 dex_idx, method_idx, direct_code, direct_method,
703 type, skip_this);
Bill Buzbeea114add2012-05-03 15:00:40 -0700704
705 if (pcrLabel) {
buzbeefa57c472012-11-21 12:06:18 -0800706 *pcrLabel = GenNullCheck(cu, info->args[0].s_reg_low, TargetReg(kArg1), info->opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700707 }
buzbeefa57c472012-11-21 12:06:18 -0800708 return call_state;
buzbee31a4a6f2012-02-28 15:36:15 -0800709}
710
711/*
712 * May have 0+ arguments (also used for jumbo). Note that
713 * source virtual registers may be in physical registers, so may
714 * need to be flushed to home location before copying. This
715 * applies to arg3 and above (see below).
716 *
717 * Two general strategies:
718 * If < 20 arguments
719 * Pass args 3-18 using vldm/vstm block copy
buzbeef0504cd2012-11-13 16:31:10 -0800720 * Pass arg0, arg1 & arg2 in kArg1-kArg3
buzbee31a4a6f2012-02-28 15:36:15 -0800721 * If 20+ arguments
722 * Pass args arg19+ using memcpy block copy
buzbeef0504cd2012-11-13 16:31:10 -0800723 * Pass arg0, arg1 & arg2 in kArg1-kArg3
buzbee31a4a6f2012-02-28 15:36:15 -0800724 *
725 */
buzbee02031b12012-11-23 09:41:35 -0800726int Codegen::GenDalvikArgsRange(CompilationUnit* cu, CallInfo* info, int call_state,
727 LIR** pcrLabel, NextCallInsn next_call_insn, uint32_t dex_idx,
728 uint32_t method_idx, uintptr_t direct_code, uintptr_t direct_method,
729 InvokeType type, bool skip_this)
buzbee31a4a6f2012-02-28 15:36:15 -0800730{
buzbee31a4a6f2012-02-28 15:36:15 -0800731
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 // If we can treat it as non-range (Jumbo ops will use range form)
buzbeefa57c472012-11-21 12:06:18 -0800733 if (info->num_arg_words <= 5)
734 return GenDalvikArgsNoRange(cu, info, call_state, pcrLabel,
735 next_call_insn, dex_idx, method_idx,
736 direct_code, direct_method, type, skip_this);
Bill Buzbeea114add2012-05-03 15:00:40 -0700737 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700738 * First load the non-register arguments. Both forms expect all
739 * of the source arguments to be in their home frame location, so
buzbeefa57c472012-11-21 12:06:18 -0800740 * scan the s_reg names and flush any that have been promoted to
Bill Buzbeea114add2012-05-03 15:00:40 -0700741 * frame backing storage.
742 */
buzbeefa57c472012-11-21 12:06:18 -0800743 // Scan the rest of the args - if in phys_reg flush to memory
744 for (int next_arg = 0; next_arg < info->num_arg_words;) {
745 RegLocation loc = info->args[next_arg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700746 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -0800747 loc = UpdateLocWide(cu, loc);
748 if ((next_arg >= 2) && (loc.location == kLocPhysReg)) {
749 StoreBaseDispWide(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
750 loc.low_reg, loc.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700751 }
buzbeefa57c472012-11-21 12:06:18 -0800752 next_arg += 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 } else {
buzbeefa57c472012-11-21 12:06:18 -0800754 loc = UpdateLoc(cu, loc);
755 if ((next_arg >= 3) && (loc.location == kLocPhysReg)) {
756 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
757 loc.low_reg, kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700758 }
buzbeefa57c472012-11-21 12:06:18 -0800759 next_arg++;
buzbee31a4a6f2012-02-28 15:36:15 -0800760 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700761 }
buzbee31a4a6f2012-02-28 15:36:15 -0800762
buzbeefa57c472012-11-21 12:06:18 -0800763 int start_offset = SRegOffset(cu, info->args[3].s_reg_low);
764 int outs_offset = 4 /* Method* */ + (3 * 4);
765 if (cu->instruction_set != kThumb2) {
buzbee31a4a6f2012-02-28 15:36:15 -0800766 // Generate memcpy
buzbeefa57c472012-11-21 12:06:18 -0800767 OpRegRegImm(cu, kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
768 OpRegRegImm(cu, kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
769 CallRuntimeHelperRegRegImm(cu, ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
770 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700771 } else {
buzbeefa57c472012-11-21 12:06:18 -0800772 if (info->num_arg_words >= 20) {
buzbeeb046e162012-10-30 15:48:42 -0700773 // Generate memcpy
buzbeefa57c472012-11-21 12:06:18 -0800774 OpRegRegImm(cu, kOpAdd, TargetReg(kArg0), TargetReg(kSp), outs_offset);
775 OpRegRegImm(cu, kOpAdd, TargetReg(kArg1), TargetReg(kSp), start_offset);
776 CallRuntimeHelperRegRegImm(cu, ENTRYPOINT_OFFSET(pMemcpy), TargetReg(kArg0),
777 TargetReg(kArg1), (info->num_arg_words - 3) * 4, false);
buzbeeb046e162012-10-30 15:48:42 -0700778 } else {
buzbeef0504cd2012-11-13 16:31:10 -0800779 // Use vldm/vstm pair using kArg3 as a temp
buzbeefa57c472012-11-21 12:06:18 -0800780 int regs_left = std::min(info->num_arg_words - 3, 16);
781 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
782 direct_code, direct_method, type);
783 OpRegRegImm(cu, kOpAdd, TargetReg(kArg3), TargetReg(kSp), start_offset);
784 LIR* ld = OpVldm(cu, TargetReg(kArg3), regs_left);
buzbeeb046e162012-10-30 15:48:42 -0700785 //TUNING: loosen barrier
buzbeefa57c472012-11-21 12:06:18 -0800786 ld->def_mask = ENCODE_ALL;
buzbee02031b12012-11-23 09:41:35 -0800787 SetMemRefType(cu, ld, true /* is_load */, kDalvikReg);
buzbeefa57c472012-11-21 12:06:18 -0800788 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
789 direct_code, direct_method, type);
790 OpRegRegImm(cu, kOpAdd, TargetReg(kArg3), TargetReg(kSp), 4 /* Method* */ + (3 * 4));
791 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
792 direct_code, direct_method, type);
793 LIR* st = OpVstm(cu, TargetReg(kArg3), regs_left);
buzbee02031b12012-11-23 09:41:35 -0800794 SetMemRefType(cu, st, false /* is_load */, kDalvikReg);
buzbeefa57c472012-11-21 12:06:18 -0800795 st->def_mask = ENCODE_ALL;
796 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
797 direct_code, direct_method, type);
buzbeeb046e162012-10-30 15:48:42 -0700798 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700799 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700800
buzbeefa57c472012-11-21 12:06:18 -0800801 call_state = LoadArgRegs(cu, info, call_state, next_call_insn,
802 dex_idx, method_idx, direct_code, direct_method,
803 type, skip_this);
Bill Buzbeea114add2012-05-03 15:00:40 -0700804
buzbeefa57c472012-11-21 12:06:18 -0800805 call_state = next_call_insn(cu, info, call_state, dex_idx, method_idx,
806 direct_code, direct_method, type);
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 if (pcrLabel) {
buzbeefa57c472012-11-21 12:06:18 -0800808 *pcrLabel = GenNullCheck(cu, info->args[0].s_reg_low, TargetReg(kArg1),
809 info->opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700810 }
buzbeefa57c472012-11-21 12:06:18 -0800811 return call_state;
buzbee31a4a6f2012-02-28 15:36:15 -0800812}
813
buzbee02031b12012-11-23 09:41:35 -0800814RegLocation Codegen::InlineTarget(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700815{
Bill Buzbeea114add2012-05-03 15:00:40 -0700816 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700817 if (info->result.location == kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800818 res = GetReturn(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700819 } else {
buzbee15bf9802012-06-12 17:49:27 -0700820 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700821 }
822 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700823}
824
buzbee02031b12012-11-23 09:41:35 -0800825RegLocation Codegen::InlineTargetWide(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700826{
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 RegLocation res;
buzbee15bf9802012-06-12 17:49:27 -0700828 if (info->result.location == kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800829 res = GetReturnWide(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700830 } else {
buzbee15bf9802012-06-12 17:49:27 -0700831 res = info->result;
Bill Buzbeea114add2012-05-03 15:00:40 -0700832 }
833 return res;
buzbeefc9e6fa2012-03-23 15:14:29 -0700834}
835
buzbee02031b12012-11-23 09:41:35 -0800836bool Codegen::GenInlinedCharAt(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700837{
buzbeefa57c472012-11-21 12:06:18 -0800838 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700839 // TODO - add Mips implementation
840 return false;
841 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700842 // Location of reference to data array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843 int value_offset = mirror::String::ValueOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -0700844 // Location of count
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800845 int count_offset = mirror::String::CountOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -0700846 // Starting offset within data array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800847 int offset_offset = mirror::String::OffsetOffset().Int32Value();
Bill Buzbeea114add2012-05-03 15:00:40 -0700848 // Start of char data with array_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800849 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
buzbeefc9e6fa2012-03-23 15:14:29 -0700850
buzbeefa57c472012-11-21 12:06:18 -0800851 RegLocation rl_obj = info->args[0];
852 RegLocation rl_idx = info->args[1];
853 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
854 rl_idx = LoadValue(cu, rl_idx, kCoreReg);
855 int reg_max;
856 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, info->opt_flags);
857 bool range_check = (!(info->opt_flags & MIR_IGNORE_RANGE_CHECK));
858 LIR* launch_pad = NULL;
859 int reg_off = INVALID_REG;
860 int reg_ptr = INVALID_REG;
861 if (cu->instruction_set != kX86) {
862 reg_off = AllocTemp(cu);
863 reg_ptr = AllocTemp(cu);
864 if (range_check) {
865 reg_max = AllocTemp(cu);
866 LoadWordDisp(cu, rl_obj.low_reg, count_offset, reg_max);
buzbeeb046e162012-10-30 15:48:42 -0700867 }
buzbeefa57c472012-11-21 12:06:18 -0800868 LoadWordDisp(cu, rl_obj.low_reg, offset_offset, reg_off);
869 LoadWordDisp(cu, rl_obj.low_reg, value_offset, reg_ptr);
870 if (range_check) {
buzbeeb046e162012-10-30 15:48:42 -0700871 // Set up a launch pad to allow retry in case of bounds violation */
buzbeefa57c472012-11-21 12:06:18 -0800872 launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
873 InsertGrowableList(cu, &cu->intrinsic_launchpads,
874 reinterpret_cast<uintptr_t>(launch_pad));
875 OpRegReg(cu, kOpCmp, rl_idx.low_reg, reg_max);
876 FreeTemp(cu, reg_max);
877 OpCondBranch(cu, kCondCs, launch_pad);
buzbeeb046e162012-10-30 15:48:42 -0700878 }
879 } else {
buzbeefa57c472012-11-21 12:06:18 -0800880 if (range_check) {
881 reg_max = AllocTemp(cu);
882 LoadWordDisp(cu, rl_obj.low_reg, count_offset, reg_max);
buzbeeb046e162012-10-30 15:48:42 -0700883 // Set up a launch pad to allow retry in case of bounds violation */
buzbeefa57c472012-11-21 12:06:18 -0800884 launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
885 InsertGrowableList(cu, &cu->intrinsic_launchpads,
886 reinterpret_cast<uintptr_t>(launch_pad));
887 OpRegReg(cu, kOpCmp, rl_idx.low_reg, reg_max);
888 FreeTemp(cu, reg_max);
889 OpCondBranch(cu, kCondCc, launch_pad);
buzbeeb046e162012-10-30 15:48:42 -0700890 }
buzbeefa57c472012-11-21 12:06:18 -0800891 reg_off = AllocTemp(cu);
892 reg_ptr = AllocTemp(cu);
893 LoadWordDisp(cu, rl_obj.low_reg, offset_offset, reg_off);
894 LoadWordDisp(cu, rl_obj.low_reg, value_offset, reg_ptr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700895 }
buzbeefa57c472012-11-21 12:06:18 -0800896 OpRegImm(cu, kOpAdd, reg_ptr, data_offset);
897 OpRegReg(cu, kOpAdd, reg_off, rl_idx.low_reg);
898 FreeTemp(cu, rl_obj.low_reg);
899 FreeTemp(cu, rl_idx.low_reg);
900 RegLocation rl_dest = InlineTarget(cu, info);
901 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
902 LoadBaseIndexed(cu, reg_ptr, reg_off, rl_result.low_reg, 1, kUnsignedHalf);
903 FreeTemp(cu, reg_off);
904 FreeTemp(cu, reg_ptr);
905 StoreValue(cu, rl_dest, rl_result);
906 if (range_check) {
907 launch_pad->operands[2] = 0; // no resumption
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 }
909 // Record that we've already inlined & null checked
buzbeefa57c472012-11-21 12:06:18 -0800910 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700912}
913
buzbeefa57c472012-11-21 12:06:18 -0800914// Generates an inlined String.is_empty or String.length.
buzbee02031b12012-11-23 09:41:35 -0800915bool Codegen::GenInlinedStringIsEmptyOrLength(CompilationUnit* cu, CallInfo* info, bool is_empty)
buzbeefc9e6fa2012-03-23 15:14:29 -0700916{
buzbeefa57c472012-11-21 12:06:18 -0800917 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700918 // TODO - add Mips implementation
919 return false;
920 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700921 // dst = src.length();
buzbeefa57c472012-11-21 12:06:18 -0800922 RegLocation rl_obj = info->args[0];
923 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
924 RegLocation rl_dest = InlineTarget(cu, info);
925 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
926 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, info->opt_flags);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800927 LoadWordDisp(cu, rl_obj.low_reg, mirror::String::CountOffset().Int32Value(),
buzbeefa57c472012-11-21 12:06:18 -0800928 rl_result.low_reg);
929 if (is_empty) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 // dst = (dst == 0);
buzbeefa57c472012-11-21 12:06:18 -0800931 if (cu->instruction_set == kThumb2) {
932 int t_reg = AllocTemp(cu);
933 OpRegReg(cu, kOpNeg, t_reg, rl_result.low_reg);
934 OpRegRegReg(cu, kOpAdc, rl_result.low_reg, rl_result.low_reg, t_reg);
buzbeeb046e162012-10-30 15:48:42 -0700935 } else {
buzbeefa57c472012-11-21 12:06:18 -0800936 DCHECK_EQ(cu->instruction_set, kX86);
937 OpRegImm(cu, kOpSub, rl_result.low_reg, 1);
938 OpRegImm(cu, kOpLsr, rl_result.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -0700939 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700940 }
buzbeefa57c472012-11-21 12:06:18 -0800941 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700943}
944
buzbee02031b12012-11-23 09:41:35 -0800945bool Codegen::GenInlinedAbsInt(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700946{
buzbeefa57c472012-11-21 12:06:18 -0800947 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700948 // TODO - add Mips implementation
949 return false;
950 }
buzbeefa57c472012-11-21 12:06:18 -0800951 RegLocation rl_src = info->args[0];
952 rl_src = LoadValue(cu, rl_src, kCoreReg);
953 RegLocation rl_dest = InlineTarget(cu, info);
954 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
955 int sign_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700956 // abs(x) = y<=x>>31, (x+y)^y.
buzbeefa57c472012-11-21 12:06:18 -0800957 OpRegRegImm(cu, kOpAsr, sign_reg, rl_src.low_reg, 31);
958 OpRegRegReg(cu, kOpAdd, rl_result.low_reg, rl_src.low_reg, sign_reg);
959 OpRegReg(cu, kOpXor, rl_result.low_reg, sign_reg);
960 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700961 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -0700962}
963
buzbee02031b12012-11-23 09:41:35 -0800964bool Codegen::GenInlinedAbsLong(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -0700965{
buzbeefa57c472012-11-21 12:06:18 -0800966 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -0700967 // TODO - add Mips implementation
968 return false;
969 }
buzbeefa57c472012-11-21 12:06:18 -0800970 if (cu->instruction_set == kThumb2) {
971 RegLocation rl_src = info->args[0];
972 rl_src = LoadValueWide(cu, rl_src, kCoreReg);
973 RegLocation rl_dest = InlineTargetWide(cu, info);
974 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
975 int sign_reg = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700976 // abs(x) = y<=x>>31, (x+y)^y.
buzbeefa57c472012-11-21 12:06:18 -0800977 OpRegRegImm(cu, kOpAsr, sign_reg, rl_src.high_reg, 31);
978 OpRegRegReg(cu, kOpAdd, rl_result.low_reg, rl_src.low_reg, sign_reg);
979 OpRegRegReg(cu, kOpAdc, rl_result.high_reg, rl_src.high_reg, sign_reg);
980 OpRegReg(cu, kOpXor, rl_result.low_reg, sign_reg);
981 OpRegReg(cu, kOpXor, rl_result.high_reg, sign_reg);
982 StoreValueWide(cu, rl_dest, rl_result);
buzbeeb046e162012-10-30 15:48:42 -0700983 return true;
984 } else {
buzbeefa57c472012-11-21 12:06:18 -0800985 DCHECK_EQ(cu->instruction_set, kX86);
buzbeeb046e162012-10-30 15:48:42 -0700986 // Reuse source registers to avoid running out of temps
buzbeefa57c472012-11-21 12:06:18 -0800987 RegLocation rl_src = info->args[0];
988 rl_src = LoadValueWide(cu, rl_src, kCoreReg);
989 RegLocation rl_dest = InlineTargetWide(cu, info);
990 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
991 OpRegCopyWide(cu, rl_result.low_reg, rl_result.high_reg, rl_src.low_reg, rl_src.high_reg);
992 FreeTemp(cu, rl_src.low_reg);
993 FreeTemp(cu, rl_src.high_reg);
994 int sign_reg = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700995 // abs(x) = y<=x>>31, (x+y)^y.
buzbeefa57c472012-11-21 12:06:18 -0800996 OpRegRegImm(cu, kOpAsr, sign_reg, rl_result.high_reg, 31);
997 OpRegReg(cu, kOpAdd, rl_result.low_reg, sign_reg);
998 OpRegReg(cu, kOpAdc, rl_result.high_reg, sign_reg);
999 OpRegReg(cu, kOpXor, rl_result.low_reg, sign_reg);
1000 OpRegReg(cu, kOpXor, rl_result.high_reg, sign_reg);
1001 StoreValueWide(cu, rl_dest, rl_result);
buzbeeb046e162012-10-30 15:48:42 -07001002 return true;
1003 }
buzbeefc9e6fa2012-03-23 15:14:29 -07001004}
1005
buzbee02031b12012-11-23 09:41:35 -08001006bool Codegen::GenInlinedFloatCvt(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -07001007{
buzbeefa57c472012-11-21 12:06:18 -08001008 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -07001009 // TODO - add Mips implementation
1010 return false;
1011 }
buzbeefa57c472012-11-21 12:06:18 -08001012 RegLocation rl_src = info->args[0];
1013 RegLocation rl_dest = InlineTarget(cu, info);
1014 StoreValue(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -07001015 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -07001016}
1017
buzbee02031b12012-11-23 09:41:35 -08001018bool Codegen::GenInlinedDoubleCvt(CompilationUnit *cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -07001019{
buzbeefa57c472012-11-21 12:06:18 -08001020 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -07001021 // TODO - add Mips implementation
1022 return false;
1023 }
buzbeefa57c472012-11-21 12:06:18 -08001024 RegLocation rl_src = info->args[0];
1025 RegLocation rl_dest = InlineTargetWide(cu, info);
1026 StoreValueWide(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -07001027 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -07001028}
1029
1030/*
buzbeefa57c472012-11-21 12:06:18 -08001031 * Fast string.index_of(I) & (II). Tests for simple case of char <= 0xffff,
buzbeefc9e6fa2012-03-23 15:14:29 -07001032 * otherwise bails to standard library code.
1033 */
buzbee02031b12012-11-23 09:41:35 -08001034bool Codegen::GenInlinedIndexOf(CompilationUnit* cu, CallInfo* info, bool zero_based)
buzbeefc9e6fa2012-03-23 15:14:29 -07001035{
buzbeefa57c472012-11-21 12:06:18 -08001036 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -07001037 // TODO - add Mips implementation
1038 return false;
1039 }
buzbeefa57c472012-11-21 12:06:18 -08001040 ClobberCalleeSave(cu);
1041 LockCallTemps(cu); // Using fixed registers
1042 int reg_ptr = TargetReg(kArg0);
1043 int reg_char = TargetReg(kArg1);
1044 int reg_start = TargetReg(kArg2);
buzbeefc9e6fa2012-03-23 15:14:29 -07001045
buzbeefa57c472012-11-21 12:06:18 -08001046 RegLocation rl_obj = info->args[0];
1047 RegLocation rl_char = info->args[1];
1048 RegLocation rl_start = info->args[2];
1049 LoadValueDirectFixed(cu, rl_obj, reg_ptr);
1050 LoadValueDirectFixed(cu, rl_char, reg_char);
1051 if (zero_based) {
1052 LoadConstant(cu, reg_start, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001053 } else {
buzbeefa57c472012-11-21 12:06:18 -08001054 LoadValueDirectFixed(cu, rl_start, reg_start);
Bill Buzbeea114add2012-05-03 15:00:40 -07001055 }
buzbeefa57c472012-11-21 12:06:18 -08001056 int r_tgt = (cu->instruction_set != kX86) ? LoadHelper(cu, ENTRYPOINT_OFFSET(pIndexOf)) : 0;
1057 GenNullCheck(cu, rl_obj.s_reg_low, reg_ptr, info->opt_flags);
1058 LIR* launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
1059 InsertGrowableList(cu, &cu->intrinsic_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
1060 OpCmpImmBranch(cu, kCondGt, reg_char, 0xFFFF, launch_pad);
buzbee8320f382012-09-11 16:29:42 -07001061 // NOTE: not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001062 if (cu->instruction_set != kX86) {
1063 OpReg(cu, kOpBlx, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001064 } else {
buzbeefa57c472012-11-21 12:06:18 -08001065 OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pIndexOf));
buzbeeb046e162012-10-30 15:48:42 -07001066 }
buzbeefa57c472012-11-21 12:06:18 -08001067 LIR* resume_tgt = NewLIR0(cu, kPseudoTargetLabel);
1068 launch_pad->operands[2] = reinterpret_cast<uintptr_t>(resume_tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -07001069 // Record that we've already inlined & null checked
buzbeefa57c472012-11-21 12:06:18 -08001070 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
1071 RegLocation rl_return = GetReturn(cu, false);
1072 RegLocation rl_dest = InlineTarget(cu, info);
1073 StoreValue(cu, rl_dest, rl_return);
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 return true;
buzbeefc9e6fa2012-03-23 15:14:29 -07001075}
1076
1077/* Fast string.compareTo(Ljava/lang/string;)I. */
buzbee02031b12012-11-23 09:41:35 -08001078bool Codegen::GenInlinedStringCompareTo(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -07001079{
buzbeefa57c472012-11-21 12:06:18 -08001080 if (cu->instruction_set == kMips) {
buzbeeb046e162012-10-30 15:48:42 -07001081 // TODO - add Mips implementation
1082 return false;
1083 }
buzbeefa57c472012-11-21 12:06:18 -08001084 ClobberCalleeSave(cu);
1085 LockCallTemps(cu); // Using fixed registers
1086 int reg_this = TargetReg(kArg0);
1087 int reg_cmp = TargetReg(kArg1);
buzbeefc9e6fa2012-03-23 15:14:29 -07001088
buzbeefa57c472012-11-21 12:06:18 -08001089 RegLocation rl_this = info->args[0];
1090 RegLocation rl_cmp = info->args[1];
1091 LoadValueDirectFixed(cu, rl_this, reg_this);
1092 LoadValueDirectFixed(cu, rl_cmp, reg_cmp);
1093 int r_tgt = (cu->instruction_set != kX86) ?
1094 LoadHelper(cu, ENTRYPOINT_OFFSET(pStringCompareTo)) : 0;
1095 GenNullCheck(cu, rl_this.s_reg_low, reg_this, info->opt_flags);
1096 //TUNING: check if rl_cmp.s_reg_low is already null checked
1097 LIR* launch_pad = RawLIR(cu, 0, kPseudoIntrinsicRetry, reinterpret_cast<uintptr_t>(info));
1098 InsertGrowableList(cu, &cu->intrinsic_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
1099 OpCmpImmBranch(cu, kCondEq, reg_cmp, 0, launch_pad);
buzbee8320f382012-09-11 16:29:42 -07001100 // NOTE: not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001101 if (cu->instruction_set != kX86) {
1102 OpReg(cu, kOpBlx, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001103 } else {
buzbeefa57c472012-11-21 12:06:18 -08001104 OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pStringCompareTo));
buzbeeb046e162012-10-30 15:48:42 -07001105 }
buzbeefa57c472012-11-21 12:06:18 -08001106 launch_pad->operands[2] = 0; // No return possible
Bill Buzbeea114add2012-05-03 15:00:40 -07001107 // Record that we've already inlined & null checked
buzbeefa57c472012-11-21 12:06:18 -08001108 info->opt_flags |= (MIR_INLINED | MIR_IGNORE_NULL_CHECK);
1109 RegLocation rl_return = GetReturn(cu, false);
1110 RegLocation rl_dest = InlineTarget(cu, info);
1111 StoreValue(cu, rl_dest, rl_return);
Bill Buzbeea114add2012-05-03 15:00:40 -07001112 return true;
Ian Rogers0183dd72012-09-17 23:06:51 -07001113}
1114
Ian Rogers07ec8e12012-12-01 01:26:51 -08001115bool Codegen::GenInlinedCurrentThread(CompilationUnit* cu, CallInfo* info) {
1116 RegLocation rl_dest = InlineTarget(cu, info);
1117 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1118 int offset = Thread::PeerOffset().Int32Value();
jeffhao12051ea2013-01-10 11:24:31 -08001119 if (cu->instruction_set == kThumb2 || cu->instruction_set == kMips) {
Ian Rogers07ec8e12012-12-01 01:26:51 -08001120 LoadWordDisp(cu, TargetReg(kSelf), offset, rl_result.low_reg);
1121 } else {
1122 CHECK(cu->instruction_set == kX86);
1123 ((X86Codegen*)this)->OpRegThreadMem(cu, kOpMov, rl_result.low_reg, offset);
1124 }
1125 StoreValue(cu, rl_dest, rl_result);
1126 return true;
1127}
1128
buzbee02031b12012-11-23 09:41:35 -08001129bool Codegen::GenIntrinsic(CompilationUnit* cu, CallInfo* info)
buzbeefc9e6fa2012-03-23 15:14:29 -07001130{
buzbeefa57c472012-11-21 12:06:18 -08001131 if (info->opt_flags & MIR_INLINED) {
buzbeefc9e6fa2012-03-23 15:14:29 -07001132 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001133 }
1134 /*
1135 * TODO: move these to a target-specific structured constant array
1136 * and use a generic match function. The list of intrinsics may be
1137 * slightly different depending on target.
1138 * TODO: Fold this into a matching function that runs during
1139 * basic block building. This should be part of the action for
1140 * small method inlining and recognition of the special object init
1141 * method. By doing this during basic block construction, we can also
1142 * take advantage of/generate new useful dataflow info.
1143 */
buzbeefa57c472012-11-21 12:06:18 -08001144 std::string tgt_method(PrettyMethod(info->index, *cu->dex_file));
1145 if (tgt_method.find(" java.lang") != std::string::npos) {
1146 if (tgt_method == "long java.lang.Double.doubleToRawLongBits(double)") {
1147 return GenInlinedDoubleCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001148 }
buzbeefa57c472012-11-21 12:06:18 -08001149 if (tgt_method == "double java.lang.Double.longBitsToDouble(long)") {
1150 return GenInlinedDoubleCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001151 }
buzbeefa57c472012-11-21 12:06:18 -08001152 if (tgt_method == "int java.lang.Float.float_to_raw_int_bits(float)") {
1153 return GenInlinedFloatCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001154 }
buzbeefa57c472012-11-21 12:06:18 -08001155 if (tgt_method == "float java.lang.Float.intBitsToFloat(int)") {
1156 return GenInlinedFloatCvt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001157 }
buzbeefa57c472012-11-21 12:06:18 -08001158 if (tgt_method == "int java.lang.Math.abs(int)" ||
1159 tgt_method == "int java.lang.StrictMath.abs(int)") {
1160 return GenInlinedAbsInt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001161 }
buzbeefa57c472012-11-21 12:06:18 -08001162 if (tgt_method == "long java.lang.Math.abs(long)" ||
1163 tgt_method == "long java.lang.StrictMath.abs(long)") {
1164 return GenInlinedAbsLong(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001165 }
buzbeefa57c472012-11-21 12:06:18 -08001166 if (tgt_method == "int java.lang.Math.max(int, int)" ||
1167 tgt_method == "int java.lang.StrictMath.max(int, int)") {
1168 return GenInlinedMinMaxInt(cu, info, false /* is_min */);
Ian Rogers0183dd72012-09-17 23:06:51 -07001169 }
buzbeefa57c472012-11-21 12:06:18 -08001170 if (tgt_method == "int java.lang.Math.min(int, int)" ||
1171 tgt_method == "int java.lang.StrictMath.min(int, int)") {
1172 return GenInlinedMinMaxInt(cu, info, true /* is_min */);
Ian Rogers0183dd72012-09-17 23:06:51 -07001173 }
buzbeefa57c472012-11-21 12:06:18 -08001174 if (tgt_method == "double java.lang.Math.sqrt(double)" ||
1175 tgt_method == "double java.lang.StrictMath.sqrt(double)") {
1176 return GenInlinedSqrt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001177 }
buzbeefa57c472012-11-21 12:06:18 -08001178 if (tgt_method == "char java.lang.String.charAt(int)") {
1179 return GenInlinedCharAt(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001180 }
buzbeefa57c472012-11-21 12:06:18 -08001181 if (tgt_method == "int java.lang.String.compareTo(java.lang.String)") {
1182 return GenInlinedStringCompareTo(cu, info);
Ian Rogers0183dd72012-09-17 23:06:51 -07001183 }
buzbeefa57c472012-11-21 12:06:18 -08001184 if (tgt_method == "boolean java.lang.String.is_empty()") {
1185 return GenInlinedStringIsEmptyOrLength(cu, info, true /* is_empty */);
Ian Rogers0183dd72012-09-17 23:06:51 -07001186 }
buzbeefa57c472012-11-21 12:06:18 -08001187 if (tgt_method == "int java.lang.String.index_of(int, int)") {
1188 return GenInlinedIndexOf(cu, info, false /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -07001189 }
buzbeefa57c472012-11-21 12:06:18 -08001190 if (tgt_method == "int java.lang.String.index_of(int)") {
1191 return GenInlinedIndexOf(cu, info, true /* base 0 */);
Ian Rogers0183dd72012-09-17 23:06:51 -07001192 }
buzbeefa57c472012-11-21 12:06:18 -08001193 if (tgt_method == "int java.lang.String.length()") {
1194 return GenInlinedStringIsEmptyOrLength(cu, info, false /* is_empty */);
Ian Rogers0183dd72012-09-17 23:06:51 -07001195 }
Ian Rogers07ec8e12012-12-01 01:26:51 -08001196 if (tgt_method == "java.lang.Thread java.lang.Thread.currentThread()") {
1197 return GenInlinedCurrentThread(cu, info);
1198 }
buzbeefa57c472012-11-21 12:06:18 -08001199 } else if (tgt_method.find("boolean sun.misc.Unsafe.compareAndSwap") != std::string::npos) {
1200 if (tgt_method == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
1201 return GenInlinedCas32(cu, info, false);
Ian Rogers0183dd72012-09-17 23:06:51 -07001202 }
buzbeefa57c472012-11-21 12:06:18 -08001203 if (tgt_method == "boolean sun.misc.Unsafe.compareAndSwapObject(java.lang.Object, long, java.lang.Object, java.lang.Object)") {
1204 return GenInlinedCas32(cu, info, true);
Ian Rogers0183dd72012-09-17 23:06:51 -07001205 }
Ian Rogerse13eafa2012-09-07 11:24:27 -07001206 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001207 return false;
buzbeefc9e6fa2012-03-23 15:14:29 -07001208}
1209
buzbee02031b12012-11-23 09:41:35 -08001210void Codegen::GenInvoke(CompilationUnit* cu, CallInfo* info)
buzbee1bc37c62012-11-20 13:35:41 -08001211{
buzbeefa57c472012-11-21 12:06:18 -08001212 if (GenIntrinsic(cu, info)) {
buzbee1bc37c62012-11-20 13:35:41 -08001213 return;
1214 }
buzbeefa57c472012-11-21 12:06:18 -08001215 InvokeType original_type = info->type; // avoiding mutation by ComputeInvokeInfo
1216 int call_state = 0;
1217 LIR* null_ck;
1218 LIR** p_null_ck = NULL;
1219 NextCallInsn next_call_insn;
1220 FlushAllRegs(cu); /* Everything to home location */
buzbee1bc37c62012-11-20 13:35:41 -08001221 // Explicit register usage
buzbeefa57c472012-11-21 12:06:18 -08001222 LockCallTemps(cu);
buzbee1bc37c62012-11-20 13:35:41 -08001223
buzbeefa57c472012-11-21 12:06:18 -08001224 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
TDYa127dc5daa02013-01-09 21:31:37 +08001225 *cu->dex_file, cu->code_item,
1226 cu->class_def_idx, cu->method_idx,
1227 cu->access_flags);
buzbee1bc37c62012-11-20 13:35:41 -08001228
buzbeefa57c472012-11-21 12:06:18 -08001229 uint32_t dex_method_idx = info->index;
1230 int vtable_idx;
1231 uintptr_t direct_code;
1232 uintptr_t direct_method;
1233 bool skip_this;
1234 bool fast_path =
1235 cu->compiler->ComputeInvokeInfo(dex_method_idx, &m_unit, info->type,
1236 vtable_idx, direct_code,
1237 direct_method)
buzbee1bc37c62012-11-20 13:35:41 -08001238 && !SLOW_INVOKE_PATH;
1239 if (info->type == kInterface) {
buzbeefa57c472012-11-21 12:06:18 -08001240 if (fast_path) {
1241 p_null_ck = &null_ck;
buzbee1bc37c62012-11-20 13:35:41 -08001242 }
buzbeefa57c472012-11-21 12:06:18 -08001243 next_call_insn = fast_path ? NextInterfaceCallInsn
buzbee52a77fc2012-11-20 19:50:46 -08001244 : NextInterfaceCallInsnWithAccessCheck;
buzbeefa57c472012-11-21 12:06:18 -08001245 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001246 } else if (info->type == kDirect) {
buzbeefa57c472012-11-21 12:06:18 -08001247 if (fast_path) {
1248 p_null_ck = &null_ck;
buzbee1bc37c62012-11-20 13:35:41 -08001249 }
buzbeefa57c472012-11-21 12:06:18 -08001250 next_call_insn = fast_path ? NextSDCallInsn : NextDirectCallInsnSP;
1251 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001252 } else if (info->type == kStatic) {
buzbeefa57c472012-11-21 12:06:18 -08001253 next_call_insn = fast_path ? NextSDCallInsn : NextStaticCallInsnSP;
1254 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001255 } else if (info->type == kSuper) {
buzbeefa57c472012-11-21 12:06:18 -08001256 DCHECK(!fast_path); // Fast path is a direct call.
1257 next_call_insn = NextSuperCallInsnSP;
1258 skip_this = false;
buzbee1bc37c62012-11-20 13:35:41 -08001259 } else {
1260 DCHECK_EQ(info->type, kVirtual);
buzbeefa57c472012-11-21 12:06:18 -08001261 next_call_insn = fast_path ? NextVCallInsn : NextVCallInsnSP;
1262 skip_this = fast_path;
buzbee1bc37c62012-11-20 13:35:41 -08001263 }
buzbeefa57c472012-11-21 12:06:18 -08001264 if (!info->is_range) {
1265 call_state = GenDalvikArgsNoRange(cu, info, call_state, p_null_ck,
1266 next_call_insn, dex_method_idx,
1267 vtable_idx, direct_code, direct_method,
1268 original_type, skip_this);
buzbee1bc37c62012-11-20 13:35:41 -08001269 } else {
buzbeefa57c472012-11-21 12:06:18 -08001270 call_state = GenDalvikArgsRange(cu, info, call_state, p_null_ck,
1271 next_call_insn, dex_method_idx, vtable_idx,
1272 direct_code, direct_method, original_type,
1273 skip_this);
buzbee1bc37c62012-11-20 13:35:41 -08001274 }
1275 // Finish up any of the call sequence not interleaved in arg loading
buzbeefa57c472012-11-21 12:06:18 -08001276 while (call_state >= 0) {
1277 call_state = next_call_insn(cu, info, call_state, dex_method_idx,
1278 vtable_idx, direct_code, direct_method,
1279 original_type);
buzbee1bc37c62012-11-20 13:35:41 -08001280 }
buzbeefa57c472012-11-21 12:06:18 -08001281 if (cu->enable_debug & (1 << kDebugDisplayMissingTargets)) {
1282 GenShowTarget(cu);
buzbee1bc37c62012-11-20 13:35:41 -08001283 }
buzbeefa57c472012-11-21 12:06:18 -08001284 LIR* call_inst;
1285 if (cu->instruction_set != kX86) {
1286 call_inst = OpReg(cu, kOpBlx, TargetReg(kInvokeTgt));
buzbee1bc37c62012-11-20 13:35:41 -08001287 } else {
buzbeefa57c472012-11-21 12:06:18 -08001288 if (fast_path && info->type != kInterface) {
1289 call_inst = OpMem(cu, kOpBlx, TargetReg(kArg0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001290 mirror::AbstractMethod::GetCodeOffset().Int32Value());
buzbee1bc37c62012-11-20 13:35:41 -08001291 } else {
1292 int trampoline = 0;
1293 switch (info->type) {
1294 case kInterface:
buzbeefa57c472012-11-21 12:06:18 -08001295 trampoline = fast_path ? ENTRYPOINT_OFFSET(pInvokeInterfaceTrampoline)
buzbee1bc37c62012-11-20 13:35:41 -08001296 : ENTRYPOINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck);
1297 break;
1298 case kDirect:
1299 trampoline = ENTRYPOINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck);
1300 break;
1301 case kStatic:
1302 trampoline = ENTRYPOINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck);
1303 break;
1304 case kSuper:
1305 trampoline = ENTRYPOINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck);
1306 break;
1307 case kVirtual:
1308 trampoline = ENTRYPOINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck);
1309 break;
1310 default:
1311 LOG(FATAL) << "Unexpected invoke type";
1312 }
buzbeefa57c472012-11-21 12:06:18 -08001313 call_inst = OpThreadMem(cu, kOpBlx, trampoline);
buzbee1bc37c62012-11-20 13:35:41 -08001314 }
1315 }
buzbeefa57c472012-11-21 12:06:18 -08001316 MarkSafepointPC(cu, call_inst);
buzbee1bc37c62012-11-20 13:35:41 -08001317
buzbeefa57c472012-11-21 12:06:18 -08001318 ClobberCalleeSave(cu);
buzbee1bc37c62012-11-20 13:35:41 -08001319 if (info->result.location != kLocInvalid) {
1320 // We have a following MOVE_RESULT - do it now.
1321 if (info->result.wide) {
buzbeefa57c472012-11-21 12:06:18 -08001322 RegLocation ret_loc = GetReturnWide(cu, info->result.fp);
1323 StoreValueWide(cu, info->result, ret_loc);
buzbee1bc37c62012-11-20 13:35:41 -08001324 } else {
buzbeefa57c472012-11-21 12:06:18 -08001325 RegLocation ret_loc = GetReturn(cu, info->result.fp);
1326 StoreValue(cu, info->result, ret_loc);
buzbee1bc37c62012-11-20 13:35:41 -08001327 }
1328 }
1329}
1330
1331/*
1332 * Build an array of location records for the incoming arguments.
1333 * Note: one location record per word of arguments, with dummy
1334 * high-word loc for wide arguments. Also pull up any following
1335 * MOVE_RESULT and incorporate it into the invoke.
1336 */
buzbee02031b12012-11-23 09:41:35 -08001337CallInfo* Codegen::NewMemCallInfo(CompilationUnit* cu, BasicBlock* bb, MIR* mir, InvokeType type,
1338 bool is_range)
buzbee1bc37c62012-11-20 13:35:41 -08001339{
buzbeefa57c472012-11-21 12:06:18 -08001340 CallInfo* info = static_cast<CallInfo*>(NewMem(cu, sizeof(CallInfo), true, kAllocMisc));
1341 MIR* move_result_mir = FindMoveResult(cu, bb, mir);
1342 if (move_result_mir == NULL) {
buzbee1bc37c62012-11-20 13:35:41 -08001343 info->result.location = kLocInvalid;
1344 } else {
buzbeefa57c472012-11-21 12:06:18 -08001345 info->result = GetRawDest(cu, move_result_mir);
buzbeea169e1d2012-12-05 14:26:44 -08001346 move_result_mir->meta.original_opcode = move_result_mir->dalvikInsn.opcode;
1347 move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
buzbee1bc37c62012-11-20 13:35:41 -08001348 }
buzbeefa57c472012-11-21 12:06:18 -08001349 info->num_arg_words = mir->ssa_rep->num_uses;
1350 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
1351 (NewMem(cu, sizeof(RegLocation) * info->num_arg_words, false, kAllocMisc));
1352 for (int i = 0; i < info->num_arg_words; i++) {
1353 info->args[i] = GetRawSrc(cu, mir, i);
buzbee1bc37c62012-11-20 13:35:41 -08001354 }
buzbeefa57c472012-11-21 12:06:18 -08001355 info->opt_flags = mir->optimization_flags;
buzbee1bc37c62012-11-20 13:35:41 -08001356 info->type = type;
buzbeefa57c472012-11-21 12:06:18 -08001357 info->is_range = is_range;
buzbee1bc37c62012-11-20 13:35:41 -08001358 info->index = mir->dalvikInsn.vB;
1359 info->offset = mir->offset;
1360 return info;
1361}
1362
buzbee31a4a6f2012-02-28 15:36:15 -08001363} // namespace art