]>
Commit | Line | Data |
---|---|---|
29967ef6 XL |
1 | //! Handling of everything related to the calling convention. Also fills `fx.local_map`. |
2 | ||
29967ef6 XL |
3 | mod comments; |
4 | mod pass_mode; | |
5 | mod returning; | |
6 | ||
064997fb | 7 | use cranelift_module::ModuleError; |
29967ef6 | 8 | use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; |
c295e0f8 | 9 | use rustc_middle::ty::layout::FnAbiOf; |
5869c6ff | 10 | use rustc_target::abi::call::{Conv, FnAbi}; |
29967ef6 XL |
11 | use rustc_target::spec::abi::Abi; |
12 | ||
94222f64 | 13 | use cranelift_codegen::ir::{AbiParam, SigRef}; |
29967ef6 XL |
14 | |
15 | use self::pass_mode::*; | |
16 | use crate::prelude::*; | |
17 | ||
94222f64 | 18 | pub(crate) use self::returning::codegen_return; |
29967ef6 | 19 | |
5869c6ff | 20 | fn clif_sig_from_fn_abi<'tcx>( |
29967ef6 | 21 | tcx: TyCtxt<'tcx>, |
a2a8927a | 22 | default_call_conv: CallConv, |
5869c6ff | 23 | fn_abi: &FnAbi<'tcx, Ty<'tcx>>, |
29967ef6 | 24 | ) -> Signature { |
487cf647 FG |
25 | let call_conv = conv_to_call_conv(fn_abi.conv, default_call_conv); |
26 | ||
27 | let inputs = fn_abi.args.iter().map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()).flatten(); | |
28 | ||
29 | let (return_ptr, returns) = fn_abi.ret.get_abi_return(tcx); | |
30 | // Sometimes the first param is an pointer to the place where the return value needs to be stored. | |
31 | let params: Vec<_> = return_ptr.into_iter().chain(inputs).collect(); | |
32 | ||
33 | Signature { params, returns, call_conv } | |
34 | } | |
35 | ||
36 | pub(crate) fn conv_to_call_conv(c: Conv, default_call_conv: CallConv) -> CallConv { | |
37 | match c { | |
a2a8927a | 38 | Conv::Rust | Conv::C => default_call_conv, |
923072b8 | 39 | Conv::RustCold => CallConv::Cold, |
5869c6ff XL |
40 | Conv::X86_64SysV => CallConv::SystemV, |
41 | Conv::X86_64Win64 => CallConv::WindowsFastcall, | |
42 | Conv::ArmAapcs | |
43 | | Conv::CCmseNonSecureCall | |
44 | | Conv::Msp430Intr | |
45 | | Conv::PtxKernel | |
46 | | Conv::X86Fastcall | |
47 | | Conv::X86Intr | |
48 | | Conv::X86Stdcall | |
49 | | Conv::X86ThisCall | |
50 | | Conv::X86VectorCall | |
51 | | Conv::AmdGpuKernel | |
52 | | Conv::AvrInterrupt | |
487cf647 FG |
53 | | Conv::AvrNonBlockingInterrupt => todo!("{:?}", c), |
54 | } | |
29967ef6 XL |
55 | } |
56 | ||
5869c6ff | 57 | pub(crate) fn get_function_sig<'tcx>( |
29967ef6 XL |
58 | tcx: TyCtxt<'tcx>, |
59 | triple: &target_lexicon::Triple, | |
60 | inst: Instance<'tcx>, | |
5869c6ff | 61 | ) -> Signature { |
29967ef6 | 62 | assert!(!inst.substs.needs_infer()); |
c295e0f8 XL |
63 | clif_sig_from_fn_abi( |
64 | tcx, | |
a2a8927a | 65 | CallConv::triple_default(triple), |
c295e0f8 XL |
66 | &RevealAllLayoutCx(tcx).fn_abi_of_instance(inst, ty::List::empty()), |
67 | ) | |
29967ef6 XL |
68 | } |
69 | ||
70 | /// Instance must be monomorphized | |
71 | pub(crate) fn import_function<'tcx>( | |
72 | tcx: TyCtxt<'tcx>, | |
6a06907d | 73 | module: &mut dyn Module, |
29967ef6 XL |
74 | inst: Instance<'tcx>, |
75 | ) -> FuncId { | |
17df50a5 | 76 | let name = tcx.symbol_name(inst).name; |
5869c6ff | 77 | let sig = get_function_sig(tcx, module.isa().triple(), inst); |
064997fb FG |
78 | match module.declare_function(name, Linkage::Import, &sig) { |
79 | Ok(func_id) => func_id, | |
80 | Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!( | |
81 | "attempt to declare `{name}` as function, but it was already declared as static" | |
82 | )), | |
83 | Err(ModuleError::IncompatibleSignature(_, prev_sig, new_sig)) => tcx.sess.fatal(&format!( | |
84 | "attempt to declare `{name}` with signature {new_sig:?}, \ | |
85 | but it was already declared with signature {prev_sig:?}" | |
86 | )), | |
87 | Err(err) => Err::<_, _>(err).unwrap(), | |
88 | } | |
29967ef6 XL |
89 | } |
90 | ||
6a06907d | 91 | impl<'tcx> FunctionCx<'_, '_, 'tcx> { |
29967ef6 XL |
92 | /// Instance must be monomorphized |
93 | pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef { | |
17df50a5 XL |
94 | let func_id = import_function(self.tcx, self.module, inst); |
95 | let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func); | |
29967ef6 | 96 | |
cdc7bbd5 XL |
97 | if self.clif_comments.enabled() { |
98 | self.add_comment(func_ref, format!("{:?}", inst)); | |
99 | } | |
29967ef6 XL |
100 | |
101 | func_ref | |
102 | } | |
103 | ||
104 | pub(crate) fn lib_call( | |
105 | &mut self, | |
106 | name: &str, | |
5869c6ff XL |
107 | params: Vec<AbiParam>, |
108 | returns: Vec<AbiParam>, | |
29967ef6 XL |
109 | args: &[Value], |
110 | ) -> &[Value] { | |
a2a8927a | 111 | let sig = Signature { params, returns, call_conv: self.target_config.default_call_conv }; |
17df50a5 XL |
112 | let func_id = self.module.declare_function(name, Linkage::Import, &sig).unwrap(); |
113 | let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func); | |
5e7ed085 FG |
114 | if self.clif_comments.enabled() { |
115 | self.add_comment(func_ref, format!("{:?}", name)); | |
116 | } | |
29967ef6 | 117 | let call_inst = self.bcx.ins().call(func_ref, args); |
cdc7bbd5 | 118 | if self.clif_comments.enabled() { |
29967ef6 XL |
119 | self.add_comment(call_inst, format!("easy_call {}", name)); |
120 | } | |
121 | let results = self.bcx.inst_results(call_inst); | |
122 | assert!(results.len() <= 2, "{}", results.len()); | |
123 | results | |
124 | } | |
125 | ||
126 | pub(crate) fn easy_call( | |
127 | &mut self, | |
128 | name: &str, | |
129 | args: &[CValue<'tcx>], | |
130 | return_ty: Ty<'tcx>, | |
131 | ) -> CValue<'tcx> { | |
132 | let (input_tys, args): (Vec<_>, Vec<_>) = args | |
133 | .iter() | |
134 | .map(|arg| { | |
6a06907d | 135 | (AbiParam::new(self.clif_type(arg.layout().ty).unwrap()), arg.load_scalar(self)) |
29967ef6 XL |
136 | }) |
137 | .unzip(); | |
138 | let return_layout = self.layout_of(return_ty); | |
139 | let return_tys = if let ty::Tuple(tup) = return_ty.kind() { | |
5e7ed085 | 140 | tup.iter().map(|ty| AbiParam::new(self.clif_type(ty).unwrap())).collect() |
29967ef6 | 141 | } else { |
5869c6ff | 142 | vec![AbiParam::new(self.clif_type(return_ty).unwrap())] |
29967ef6 XL |
143 | }; |
144 | let ret_vals = self.lib_call(name, input_tys, return_tys, &args); | |
145 | match *ret_vals { | |
146 | [] => CValue::by_ref( | |
147 | Pointer::const_addr(self, i64::from(self.pointer_type.bytes())), | |
148 | return_layout, | |
149 | ), | |
150 | [val] => CValue::by_val(val, return_layout), | |
151 | [val, extra] => CValue::by_val_pair(val, extra, return_layout), | |
152 | _ => unreachable!(), | |
153 | } | |
154 | } | |
155 | } | |
156 | ||
157 | /// Make a [`CPlace`] capable of holding value of the specified type. | |
158 | fn make_local_place<'tcx>( | |
6a06907d | 159 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
29967ef6 XL |
160 | local: Local, |
161 | layout: TyAndLayout<'tcx>, | |
162 | is_ssa: bool, | |
163 | ) -> CPlace<'tcx> { | |
164 | let place = if is_ssa { | |
165 | if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi { | |
166 | CPlace::new_var_pair(fx, local, layout) | |
167 | } else { | |
168 | CPlace::new_var(fx, local, layout) | |
169 | } | |
170 | } else { | |
171 | CPlace::new_stack_slot(fx, layout) | |
172 | }; | |
173 | ||
29967ef6 XL |
174 | self::comments::add_local_place_comments(fx, place, local); |
175 | ||
176 | place | |
177 | } | |
178 | ||
6a06907d | 179 | pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_block: Block) { |
5869c6ff XL |
180 | fx.bcx.append_block_params_for_function_params(start_block); |
181 | ||
182 | fx.bcx.switch_to_block(start_block); | |
183 | fx.bcx.ins().nop(); | |
184 | ||
29967ef6 XL |
185 | let ssa_analyzed = crate::analyze::analyze(fx); |
186 | ||
29967ef6 XL |
187 | self::comments::add_args_header_comment(fx); |
188 | ||
6a06907d | 189 | let mut block_params_iter = fx.bcx.func.dfg.block_params(start_block).to_vec().into_iter(); |
5869c6ff XL |
190 | let ret_place = |
191 | self::returning::codegen_return_param(fx, &ssa_analyzed, &mut block_params_iter); | |
29967ef6 XL |
192 | assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE); |
193 | ||
194 | // None means pass_mode == NoPass | |
195 | enum ArgKind<'tcx> { | |
196 | Normal(Option<CValue<'tcx>>), | |
197 | Spread(Vec<Option<CValue<'tcx>>>), | |
198 | } | |
199 | ||
5869c6ff | 200 | let fn_abi = fx.fn_abi.take().unwrap(); |
064997fb FG |
201 | |
202 | // FIXME implement variadics in cranelift | |
203 | if fn_abi.c_variadic { | |
204 | fx.tcx.sess.span_fatal( | |
205 | fx.mir.span, | |
206 | "Defining variadic functions is not yet supported by Cranelift", | |
207 | ); | |
208 | } | |
209 | ||
5869c6ff XL |
210 | let mut arg_abis_iter = fn_abi.args.iter(); |
211 | ||
29967ef6 XL |
212 | let func_params = fx |
213 | .mir | |
214 | .args_iter() | |
215 | .map(|local| { | |
fc512014 | 216 | let arg_ty = fx.monomorphize(fx.mir.local_decls[local].ty); |
29967ef6 XL |
217 | |
218 | // Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482 | |
219 | if Some(local) == fx.mir.spread_arg { | |
220 | // This argument (e.g. the last argument in the "rust-call" ABI) | |
221 | // is a tuple that was spread at the ABI level and now we have | |
222 | // to reconstruct it into a tuple local variable, from multiple | |
223 | // individual function arguments. | |
224 | ||
225 | let tupled_arg_tys = match arg_ty.kind() { | |
226 | ty::Tuple(ref tys) => tys, | |
227 | _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty), | |
228 | }; | |
229 | ||
230 | let mut params = Vec::new(); | |
5e7ed085 | 231 | for (i, _arg_ty) in tupled_arg_tys.iter().enumerate() { |
5869c6ff XL |
232 | let arg_abi = arg_abis_iter.next().unwrap(); |
233 | let param = | |
234 | cvalue_for_param(fx, Some(local), Some(i), arg_abi, &mut block_params_iter); | |
29967ef6 XL |
235 | params.push(param); |
236 | } | |
237 | ||
238 | (local, ArgKind::Spread(params), arg_ty) | |
239 | } else { | |
5869c6ff XL |
240 | let arg_abi = arg_abis_iter.next().unwrap(); |
241 | let param = | |
242 | cvalue_for_param(fx, Some(local), None, arg_abi, &mut block_params_iter); | |
29967ef6 XL |
243 | (local, ArgKind::Normal(param), arg_ty) |
244 | } | |
245 | }) | |
246 | .collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>(); | |
247 | ||
248 | assert!(fx.caller_location.is_none()); | |
249 | if fx.instance.def.requires_caller_location(fx.tcx) { | |
250 | // Store caller location for `#[track_caller]`. | |
5869c6ff XL |
251 | let arg_abi = arg_abis_iter.next().unwrap(); |
252 | fx.caller_location = | |
253 | Some(cvalue_for_param(fx, None, None, arg_abi, &mut block_params_iter).unwrap()); | |
29967ef6 XL |
254 | } |
255 | ||
5869c6ff XL |
256 | assert!(arg_abis_iter.next().is_none(), "ArgAbi left behind"); |
257 | fx.fn_abi = Some(fn_abi); | |
258 | assert!(block_params_iter.next().is_none(), "arg_value left behind"); | |
29967ef6 | 259 | |
29967ef6 XL |
260 | self::comments::add_locals_header_comment(fx); |
261 | ||
262 | for (local, arg_kind, ty) in func_params { | |
263 | let layout = fx.layout_of(ty); | |
264 | ||
265 | let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa; | |
266 | ||
267 | // While this is normally an optimization to prevent an unnecessary copy when an argument is | |
268 | // not mutated by the current function, this is necessary to support unsized arguments. | |
269 | if let ArgKind::Normal(Some(val)) = arg_kind { | |
270 | if let Some((addr, meta)) = val.try_to_ptr() { | |
94222f64 XL |
271 | // Ownership of the value at the backing storage for an argument is passed to the |
272 | // callee per the ABI, so it is fine to borrow the backing storage of this argument | |
273 | // to prevent a copy. | |
274 | ||
275 | let place = if let Some(meta) = meta { | |
276 | CPlace::for_ptr_with_extra(addr, meta, val.layout()) | |
277 | } else { | |
278 | CPlace::for_ptr(addr, val.layout()) | |
279 | }; | |
280 | ||
281 | self::comments::add_local_place_comments(fx, place, local); | |
282 | ||
283 | assert_eq!(fx.local_map.push(place), local); | |
284 | continue; | |
29967ef6 XL |
285 | } |
286 | } | |
287 | ||
288 | let place = make_local_place(fx, local, layout, is_ssa); | |
289 | assert_eq!(fx.local_map.push(place), local); | |
290 | ||
291 | match arg_kind { | |
292 | ArgKind::Normal(param) => { | |
293 | if let Some(param) = param { | |
294 | place.write_cvalue(fx, param); | |
295 | } | |
296 | } | |
297 | ArgKind::Spread(params) => { | |
298 | for (i, param) in params.into_iter().enumerate() { | |
299 | if let Some(param) = param { | |
6a06907d | 300 | place.place_field(fx, mir::Field::new(i)).write_cvalue(fx, param); |
29967ef6 XL |
301 | } |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | ||
307 | for local in fx.mir.vars_and_temps_iter() { | |
fc512014 | 308 | let ty = fx.monomorphize(fx.mir.local_decls[local].ty); |
29967ef6 XL |
309 | let layout = fx.layout_of(ty); |
310 | ||
311 | let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa; | |
312 | ||
313 | let place = make_local_place(fx, local, layout, is_ssa); | |
314 | assert_eq!(fx.local_map.push(place), local); | |
315 | } | |
316 | ||
6a06907d | 317 | fx.bcx.ins().jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]); |
29967ef6 XL |
318 | } |
319 | ||
94222f64 XL |
320 | struct CallArgument<'tcx> { |
321 | value: CValue<'tcx>, | |
322 | is_owned: bool, | |
323 | } | |
324 | ||
325 | // FIXME avoid intermediate `CValue` before calling `adjust_arg_for_abi` | |
326 | fn codegen_call_argument_operand<'tcx>( | |
327 | fx: &mut FunctionCx<'_, '_, 'tcx>, | |
328 | operand: &Operand<'tcx>, | |
329 | ) -> CallArgument<'tcx> { | |
330 | CallArgument { | |
331 | value: codegen_operand(fx, operand), | |
332 | is_owned: matches!(operand, Operand::Move(_)), | |
333 | } | |
334 | } | |
335 | ||
29967ef6 | 336 | pub(crate) fn codegen_terminator_call<'tcx>( |
6a06907d | 337 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
923072b8 | 338 | source_info: mir::SourceInfo, |
29967ef6 XL |
339 | func: &Operand<'tcx>, |
340 | args: &[Operand<'tcx>], | |
923072b8 FG |
341 | destination: Place<'tcx>, |
342 | target: Option<BasicBlock>, | |
29967ef6 | 343 | ) { |
fc512014 | 344 | let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx)); |
6a06907d XL |
345 | let fn_sig = |
346 | fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx)); | |
29967ef6 | 347 | |
923072b8 | 348 | let ret_place = codegen_place(fx, destination); |
29967ef6 | 349 | |
f2b60f7d | 350 | // Handle special calls like intrinsics and empty drop glue. |
29967ef6 XL |
351 | let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() { |
352 | let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs) | |
353 | .unwrap() | |
354 | .unwrap() | |
355 | .polymorphize(fx.tcx); | |
356 | ||
357 | if fx.tcx.symbol_name(instance).name.starts_with("llvm.") { | |
358 | crate::intrinsics::codegen_llvm_intrinsic_call( | |
359 | fx, | |
360 | &fx.tcx.symbol_name(instance).name, | |
361 | substs, | |
362 | args, | |
923072b8 FG |
363 | ret_place, |
364 | target, | |
29967ef6 XL |
365 | ); |
366 | return; | |
367 | } | |
368 | ||
369 | match instance.def { | |
370 | InstanceDef::Intrinsic(_) => { | |
923072b8 FG |
371 | crate::intrinsics::codegen_intrinsic_call( |
372 | fx, | |
373 | instance, | |
374 | args, | |
375 | ret_place, | |
376 | target, | |
377 | source_info, | |
378 | ); | |
29967ef6 XL |
379 | return; |
380 | } | |
381 | InstanceDef::DropGlue(_, None) => { | |
382 | // empty drop glue - a nop. | |
923072b8 | 383 | let dest = target.expect("Non terminating drop_in_place_real???"); |
29967ef6 XL |
384 | let ret_block = fx.get_block(dest); |
385 | fx.bcx.ins().jump(ret_block, &[]); | |
386 | return; | |
387 | } | |
388 | _ => Some(instance), | |
389 | } | |
390 | } else { | |
391 | None | |
392 | }; | |
393 | ||
5869c6ff | 394 | let extra_args = &args[fn_sig.inputs().len()..]; |
c295e0f8 XL |
395 | let extra_args = fx |
396 | .tcx | |
397 | .mk_type_list(extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx)))); | |
5869c6ff | 398 | let fn_abi = if let Some(instance) = instance { |
c295e0f8 | 399 | RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args) |
5869c6ff | 400 | } else { |
c295e0f8 | 401 | RevealAllLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_ty.fn_sig(fx.tcx), extra_args) |
5869c6ff XL |
402 | }; |
403 | ||
064997fb FG |
404 | let is_cold = if fn_sig.abi == Abi::RustCold { |
405 | true | |
406 | } else { | |
407 | instance | |
408 | .map(|inst| { | |
409 | fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD) | |
410 | }) | |
411 | .unwrap_or(false) | |
412 | }; | |
29967ef6 | 413 | if is_cold { |
5e7ed085 | 414 | fx.bcx.set_cold_block(fx.bcx.current_block().unwrap()); |
923072b8 | 415 | if let Some(destination_block) = target { |
5e7ed085 FG |
416 | fx.bcx.set_cold_block(fx.get_block(destination_block)); |
417 | } | |
29967ef6 XL |
418 | } |
419 | ||
420 | // Unpack arguments tuple for closures | |
94222f64 | 421 | let mut args = if fn_sig.abi == Abi::RustCall { |
29967ef6 | 422 | assert_eq!(args.len(), 2, "rust-call abi requires two arguments"); |
94222f64 XL |
423 | let self_arg = codegen_call_argument_operand(fx, &args[0]); |
424 | let pack_arg = codegen_call_argument_operand(fx, &args[1]); | |
29967ef6 | 425 | |
94222f64 | 426 | let tupled_arguments = match pack_arg.value.layout().ty.kind() { |
29967ef6 XL |
427 | ty::Tuple(ref tupled_arguments) => tupled_arguments, |
428 | _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"), | |
429 | }; | |
430 | ||
431 | let mut args = Vec::with_capacity(1 + tupled_arguments.len()); | |
432 | args.push(self_arg); | |
433 | for i in 0..tupled_arguments.len() { | |
94222f64 XL |
434 | args.push(CallArgument { |
435 | value: pack_arg.value.value_field(fx, mir::Field::new(i)), | |
436 | is_owned: pack_arg.is_owned, | |
437 | }); | |
29967ef6 XL |
438 | } |
439 | args | |
440 | } else { | |
94222f64 | 441 | args.iter().map(|arg| codegen_call_argument_operand(fx, arg)).collect::<Vec<_>>() |
29967ef6 XL |
442 | }; |
443 | ||
94222f64 XL |
444 | // Pass the caller location for `#[track_caller]`. |
445 | if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) { | |
923072b8 | 446 | let caller_location = fx.get_caller_location(source_info); |
94222f64 XL |
447 | args.push(CallArgument { value: caller_location, is_owned: false }); |
448 | } | |
449 | ||
450 | let args = args; | |
451 | assert_eq!(fn_abi.args.len(), args.len()); | |
452 | ||
453 | enum CallTarget { | |
454 | Direct(FuncRef), | |
455 | Indirect(SigRef, Value), | |
456 | } | |
457 | ||
458 | let (func_ref, first_arg_override) = match instance { | |
29967ef6 | 459 | // Trait object call |
6a06907d | 460 | Some(Instance { def: InstanceDef::Virtual(_, idx), .. }) => { |
cdc7bbd5 | 461 | if fx.clif_comments.enabled() { |
29967ef6 XL |
462 | let nop_inst = fx.bcx.ins().nop(); |
463 | fx.add_comment( | |
464 | nop_inst, | |
94222f64 | 465 | format!("virtual call; self arg pass mode: {:?}", &fn_abi.args[0]), |
29967ef6 XL |
466 | ); |
467 | } | |
94222f64 XL |
468 | |
469 | let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0].value, idx); | |
a2a8927a | 470 | let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi); |
94222f64 XL |
471 | let sig = fx.bcx.import_signature(sig); |
472 | ||
2b03887a | 473 | (CallTarget::Indirect(sig, method), Some(ptr.get_addr(fx))) |
29967ef6 XL |
474 | } |
475 | ||
476 | // Normal call | |
94222f64 XL |
477 | Some(instance) => { |
478 | let func_ref = fx.get_function_ref(instance); | |
479 | (CallTarget::Direct(func_ref), None) | |
480 | } | |
29967ef6 XL |
481 | |
482 | // Indirect call | |
483 | None => { | |
cdc7bbd5 | 484 | if fx.clif_comments.enabled() { |
29967ef6 XL |
485 | let nop_inst = fx.bcx.ins().nop(); |
486 | fx.add_comment(nop_inst, "indirect call"); | |
487 | } | |
94222f64 | 488 | |
29967ef6 | 489 | let func = codegen_operand(fx, func).load_scalar(fx); |
a2a8927a | 490 | let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi); |
94222f64 XL |
491 | let sig = fx.bcx.import_signature(sig); |
492 | ||
493 | (CallTarget::Indirect(sig, func), None) | |
29967ef6 XL |
494 | } |
495 | }; | |
496 | ||
94222f64 XL |
497 | self::returning::codegen_with_call_return_arg(fx, &fn_abi.ret, ret_place, |fx, return_ptr| { |
498 | let call_args = return_ptr | |
499 | .into_iter() | |
500 | .chain(first_arg_override.into_iter()) | |
501 | .chain( | |
502 | args.into_iter() | |
503 | .enumerate() | |
504 | .skip(if first_arg_override.is_some() { 1 } else { 0 }) | |
505 | .map(|(i, arg)| { | |
506 | adjust_arg_for_abi(fx, arg.value, &fn_abi.args[i], arg.is_owned).into_iter() | |
507 | }) | |
508 | .flatten(), | |
509 | ) | |
510 | .collect::<Vec<Value>>(); | |
511 | ||
512 | let call_inst = match func_ref { | |
513 | CallTarget::Direct(func_ref) => fx.bcx.ins().call(func_ref, &call_args), | |
514 | CallTarget::Indirect(sig, func_ptr) => { | |
515 | fx.bcx.ins().call_indirect(sig, func_ptr, &call_args) | |
29967ef6 | 516 | } |
94222f64 | 517 | }; |
29967ef6 | 518 | |
94222f64 XL |
519 | // FIXME find a cleaner way to support varargs |
520 | if fn_sig.c_variadic { | |
521 | if !matches!(fn_sig.abi, Abi::C { .. }) { | |
923072b8 FG |
522 | fx.tcx.sess.span_fatal( |
523 | source_info.span, | |
524 | &format!("Variadic call for non-C abi {:?}", fn_sig.abi), | |
525 | ); | |
94222f64 XL |
526 | } |
527 | let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap(); | |
528 | let abi_params = call_args | |
529 | .into_iter() | |
530 | .map(|arg| { | |
531 | let ty = fx.bcx.func.dfg.value_type(arg); | |
532 | if !ty.is_int() { | |
533 | // FIXME set %al to upperbound on float args once floats are supported | |
923072b8 FG |
534 | fx.tcx.sess.span_fatal( |
535 | source_info.span, | |
536 | &format!("Non int ty {:?} for variadic call", ty), | |
537 | ); | |
94222f64 XL |
538 | } |
539 | AbiParam::new(ty) | |
540 | }) | |
541 | .collect::<Vec<AbiParam>>(); | |
542 | fx.bcx.func.dfg.signatures[sig_ref].params = abi_params; | |
29967ef6 | 543 | } |
94222f64 XL |
544 | |
545 | call_inst | |
546 | }); | |
29967ef6 | 547 | |
923072b8 | 548 | if let Some(dest) = target { |
29967ef6 XL |
549 | let ret_block = fx.get_block(dest); |
550 | fx.bcx.ins().jump(ret_block, &[]); | |
551 | } else { | |
5e7ed085 | 552 | fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); |
29967ef6 XL |
553 | } |
554 | } | |
555 | ||
556 | pub(crate) fn codegen_drop<'tcx>( | |
6a06907d | 557 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
923072b8 | 558 | source_info: mir::SourceInfo, |
29967ef6 XL |
559 | drop_place: CPlace<'tcx>, |
560 | ) { | |
561 | let ty = drop_place.layout().ty; | |
5869c6ff | 562 | let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx); |
29967ef6 | 563 | |
5869c6ff | 564 | if let ty::InstanceDef::DropGlue(_, None) = drop_instance.def { |
29967ef6 XL |
565 | // we don't actually need to drop anything |
566 | } else { | |
29967ef6 | 567 | match ty.kind() { |
2b03887a FG |
568 | ty::Dynamic(_, _, ty::Dyn) => { |
569 | // IN THIS ARM, WE HAVE: | |
570 | // ty = *mut (dyn Trait) | |
571 | // which is: exists<T> ( *mut T, Vtable<T: Trait> ) | |
572 | // args[0] args[1] | |
573 | // | |
574 | // args = ( Data, Vtable ) | |
575 | // | | |
576 | // v | |
577 | // /-------\ | |
578 | // | ... | | |
579 | // \-------/ | |
580 | // | |
29967ef6 XL |
581 | let (ptr, vtable) = drop_place.to_ptr_maybe_unsized(); |
582 | let ptr = ptr.get_addr(fx); | |
583 | let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap()); | |
584 | ||
5869c6ff XL |
585 | // FIXME(eddyb) perhaps move some of this logic into |
586 | // `Instance::resolve_drop_in_place`? | |
587 | let virtual_drop = Instance { | |
588 | def: ty::InstanceDef::Virtual(drop_instance.def_id(), 0), | |
589 | substs: drop_instance.substs, | |
590 | }; | |
c295e0f8 XL |
591 | let fn_abi = |
592 | RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(virtual_drop, ty::List::empty()); | |
5869c6ff | 593 | |
a2a8927a | 594 | let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi); |
29967ef6 XL |
595 | let sig = fx.bcx.import_signature(sig); |
596 | fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]); | |
597 | } | |
2b03887a FG |
598 | ty::Dynamic(_, _, ty::DynStar) => { |
599 | // IN THIS ARM, WE HAVE: | |
600 | // ty = *mut (dyn* Trait) | |
601 | // which is: *mut exists<T: sizeof(T) == sizeof(usize)> (T, Vtable<T: Trait>) | |
602 | // | |
603 | // args = [ * ] | |
604 | // | | |
605 | // v | |
606 | // ( Data, Vtable ) | |
607 | // | | |
608 | // v | |
609 | // /-------\ | |
610 | // | ... | | |
611 | // \-------/ | |
612 | // | |
613 | // | |
614 | // WE CAN CONVERT THIS INTO THE ABOVE LOGIC BY DOING | |
615 | // | |
616 | // data = &(*args[0]).0 // gives a pointer to Data above (really the same pointer) | |
617 | // vtable = (*args[0]).1 // loads the vtable out | |
618 | // (data, vtable) // an equivalent Rust `*mut dyn Trait` | |
619 | // | |
620 | // SO THEN WE CAN USE THE ABOVE CODE. | |
621 | let (data, vtable) = drop_place.to_cvalue(fx).dyn_star_force_data_on_stack(fx); | |
622 | let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable); | |
623 | ||
624 | let virtual_drop = Instance { | |
625 | def: ty::InstanceDef::Virtual(drop_instance.def_id(), 0), | |
626 | substs: drop_instance.substs, | |
627 | }; | |
628 | let fn_abi = | |
629 | RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(virtual_drop, ty::List::empty()); | |
630 | ||
631 | let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi); | |
632 | let sig = fx.bcx.import_signature(sig); | |
633 | fx.bcx.ins().call_indirect(sig, drop_fn, &[data]); | |
634 | } | |
29967ef6 | 635 | _ => { |
5869c6ff XL |
636 | assert!(!matches!(drop_instance.def, InstanceDef::Virtual(_, _))); |
637 | ||
c295e0f8 XL |
638 | let fn_abi = |
639 | RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(drop_instance, ty::List::empty()); | |
29967ef6 XL |
640 | |
641 | let arg_value = drop_place.place_ref( | |
642 | fx, | |
643 | fx.layout_of(fx.tcx.mk_ref( | |
5099ac24 | 644 | fx.tcx.lifetimes.re_erased, |
6a06907d | 645 | TypeAndMut { ty, mutbl: crate::rustc_hir::Mutability::Mut }, |
29967ef6 XL |
646 | )), |
647 | ); | |
94222f64 | 648 | let arg_value = adjust_arg_for_abi(fx, arg_value, &fn_abi.args[0], true); |
29967ef6 XL |
649 | |
650 | let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>(); | |
651 | ||
5869c6ff | 652 | if drop_instance.def.requires_caller_location(fx.tcx) { |
29967ef6 | 653 | // Pass the caller location for `#[track_caller]`. |
923072b8 | 654 | let caller_location = fx.get_caller_location(source_info); |
5869c6ff | 655 | call_args.extend( |
94222f64 | 656 | adjust_arg_for_abi(fx, caller_location, &fn_abi.args[1], false).into_iter(), |
5869c6ff | 657 | ); |
29967ef6 XL |
658 | } |
659 | ||
5869c6ff | 660 | let func_ref = fx.get_function_ref(drop_instance); |
29967ef6 XL |
661 | fx.bcx.ins().call(func_ref, &call_args); |
662 | } | |
663 | } | |
664 | } | |
665 | } |