]>
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 | ||
7 | use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; | |
5869c6ff XL |
8 | use rustc_middle::ty::layout::FnAbiExt; |
9 | use rustc_target::abi::call::{Conv, FnAbi}; | |
29967ef6 XL |
10 | use rustc_target::spec::abi::Abi; |
11 | ||
5869c6ff XL |
12 | use cranelift_codegen::ir::AbiParam; |
13 | use smallvec::smallvec; | |
29967ef6 XL |
14 | |
15 | use self::pass_mode::*; | |
16 | use crate::prelude::*; | |
17 | ||
18 | pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return}; | |
19 | ||
5869c6ff | 20 | fn clif_sig_from_fn_abi<'tcx>( |
29967ef6 XL |
21 | tcx: TyCtxt<'tcx>, |
22 | triple: &target_lexicon::Triple, | |
5869c6ff | 23 | fn_abi: &FnAbi<'tcx, Ty<'tcx>>, |
29967ef6 | 24 | ) -> Signature { |
5869c6ff XL |
25 | let call_conv = match fn_abi.conv { |
26 | Conv::Rust | Conv::C => CallConv::triple_default(triple), | |
27 | Conv::X86_64SysV => CallConv::SystemV, | |
28 | Conv::X86_64Win64 => CallConv::WindowsFastcall, | |
29 | Conv::ArmAapcs | |
30 | | Conv::CCmseNonSecureCall | |
31 | | Conv::Msp430Intr | |
32 | | Conv::PtxKernel | |
33 | | Conv::X86Fastcall | |
34 | | Conv::X86Intr | |
35 | | Conv::X86Stdcall | |
36 | | Conv::X86ThisCall | |
37 | | Conv::X86VectorCall | |
38 | | Conv::AmdGpuKernel | |
39 | | Conv::AvrInterrupt | |
6a06907d | 40 | | Conv::AvrNonBlockingInterrupt => todo!("{:?}", fn_abi.conv), |
29967ef6 | 41 | }; |
6a06907d | 42 | let inputs = fn_abi.args.iter().map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()).flatten(); |
29967ef6 | 43 | |
5869c6ff XL |
44 | let (return_ptr, returns) = fn_abi.ret.get_abi_return(tcx); |
45 | // Sometimes the first param is an pointer to the place where the return value needs to be stored. | |
46 | let params: Vec<_> = return_ptr.into_iter().chain(inputs).collect(); | |
29967ef6 | 47 | |
6a06907d | 48 | Signature { params, returns, call_conv } |
29967ef6 XL |
49 | } |
50 | ||
5869c6ff | 51 | pub(crate) fn get_function_sig<'tcx>( |
29967ef6 XL |
52 | tcx: TyCtxt<'tcx>, |
53 | triple: &target_lexicon::Triple, | |
54 | inst: Instance<'tcx>, | |
5869c6ff | 55 | ) -> Signature { |
29967ef6 | 56 | assert!(!inst.substs.needs_infer()); |
6a06907d | 57 | clif_sig_from_fn_abi(tcx, triple, &FnAbi::of_instance(&RevealAllLayoutCx(tcx), inst, &[])) |
29967ef6 XL |
58 | } |
59 | ||
60 | /// Instance must be monomorphized | |
61 | pub(crate) fn import_function<'tcx>( | |
62 | tcx: TyCtxt<'tcx>, | |
6a06907d | 63 | module: &mut dyn Module, |
29967ef6 XL |
64 | inst: Instance<'tcx>, |
65 | ) -> FuncId { | |
5869c6ff XL |
66 | let name = tcx.symbol_name(inst).name.to_string(); |
67 | let sig = get_function_sig(tcx, module.isa().triple(), inst); | |
6a06907d | 68 | module.declare_function(&name, Linkage::Import, &sig).unwrap() |
29967ef6 XL |
69 | } |
70 | ||
6a06907d | 71 | impl<'tcx> FunctionCx<'_, '_, 'tcx> { |
29967ef6 XL |
72 | /// Instance must be monomorphized |
73 | pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef { | |
6a06907d XL |
74 | let func_id = import_function(self.tcx, self.cx.module, inst); |
75 | let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func); | |
29967ef6 | 76 | |
cdc7bbd5 XL |
77 | if self.clif_comments.enabled() { |
78 | self.add_comment(func_ref, format!("{:?}", inst)); | |
79 | } | |
29967ef6 XL |
80 | |
81 | func_ref | |
82 | } | |
83 | ||
84 | pub(crate) fn lib_call( | |
85 | &mut self, | |
86 | name: &str, | |
5869c6ff XL |
87 | params: Vec<AbiParam>, |
88 | returns: Vec<AbiParam>, | |
29967ef6 XL |
89 | args: &[Value], |
90 | ) -> &[Value] { | |
6a06907d XL |
91 | let sig = Signature { params, returns, call_conv: CallConv::triple_default(self.triple()) }; |
92 | let func_id = self.cx.module.declare_function(&name, Linkage::Import, &sig).unwrap(); | |
93 | let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func); | |
29967ef6 | 94 | let call_inst = self.bcx.ins().call(func_ref, args); |
cdc7bbd5 | 95 | if self.clif_comments.enabled() { |
29967ef6 XL |
96 | self.add_comment(call_inst, format!("easy_call {}", name)); |
97 | } | |
98 | let results = self.bcx.inst_results(call_inst); | |
99 | assert!(results.len() <= 2, "{}", results.len()); | |
100 | results | |
101 | } | |
102 | ||
103 | pub(crate) fn easy_call( | |
104 | &mut self, | |
105 | name: &str, | |
106 | args: &[CValue<'tcx>], | |
107 | return_ty: Ty<'tcx>, | |
108 | ) -> CValue<'tcx> { | |
109 | let (input_tys, args): (Vec<_>, Vec<_>) = args | |
110 | .iter() | |
111 | .map(|arg| { | |
6a06907d | 112 | (AbiParam::new(self.clif_type(arg.layout().ty).unwrap()), arg.load_scalar(self)) |
29967ef6 XL |
113 | }) |
114 | .unzip(); | |
115 | let return_layout = self.layout_of(return_ty); | |
116 | let return_tys = if let ty::Tuple(tup) = return_ty.kind() { | |
6a06907d | 117 | tup.types().map(|ty| AbiParam::new(self.clif_type(ty).unwrap())).collect() |
29967ef6 | 118 | } else { |
5869c6ff | 119 | vec![AbiParam::new(self.clif_type(return_ty).unwrap())] |
29967ef6 XL |
120 | }; |
121 | let ret_vals = self.lib_call(name, input_tys, return_tys, &args); | |
122 | match *ret_vals { | |
123 | [] => CValue::by_ref( | |
124 | Pointer::const_addr(self, i64::from(self.pointer_type.bytes())), | |
125 | return_layout, | |
126 | ), | |
127 | [val] => CValue::by_val(val, return_layout), | |
128 | [val, extra] => CValue::by_val_pair(val, extra, return_layout), | |
129 | _ => unreachable!(), | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | /// Make a [`CPlace`] capable of holding value of the specified type. | |
135 | fn make_local_place<'tcx>( | |
6a06907d | 136 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
29967ef6 XL |
137 | local: Local, |
138 | layout: TyAndLayout<'tcx>, | |
139 | is_ssa: bool, | |
140 | ) -> CPlace<'tcx> { | |
141 | let place = if is_ssa { | |
142 | if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi { | |
143 | CPlace::new_var_pair(fx, local, layout) | |
144 | } else { | |
145 | CPlace::new_var(fx, local, layout) | |
146 | } | |
147 | } else { | |
148 | CPlace::new_stack_slot(fx, layout) | |
149 | }; | |
150 | ||
29967ef6 XL |
151 | self::comments::add_local_place_comments(fx, place, local); |
152 | ||
153 | place | |
154 | } | |
155 | ||
6a06907d | 156 | pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_block: Block) { |
5869c6ff XL |
157 | fx.bcx.append_block_params_for_function_params(start_block); |
158 | ||
159 | fx.bcx.switch_to_block(start_block); | |
160 | fx.bcx.ins().nop(); | |
161 | ||
29967ef6 XL |
162 | let ssa_analyzed = crate::analyze::analyze(fx); |
163 | ||
29967ef6 XL |
164 | self::comments::add_args_header_comment(fx); |
165 | ||
6a06907d | 166 | let mut block_params_iter = fx.bcx.func.dfg.block_params(start_block).to_vec().into_iter(); |
5869c6ff XL |
167 | let ret_place = |
168 | self::returning::codegen_return_param(fx, &ssa_analyzed, &mut block_params_iter); | |
29967ef6 XL |
169 | assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE); |
170 | ||
171 | // None means pass_mode == NoPass | |
172 | enum ArgKind<'tcx> { | |
173 | Normal(Option<CValue<'tcx>>), | |
174 | Spread(Vec<Option<CValue<'tcx>>>), | |
175 | } | |
176 | ||
5869c6ff XL |
177 | let fn_abi = fx.fn_abi.take().unwrap(); |
178 | let mut arg_abis_iter = fn_abi.args.iter(); | |
179 | ||
29967ef6 XL |
180 | let func_params = fx |
181 | .mir | |
182 | .args_iter() | |
183 | .map(|local| { | |
fc512014 | 184 | let arg_ty = fx.monomorphize(fx.mir.local_decls[local].ty); |
29967ef6 XL |
185 | |
186 | // Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482 | |
187 | if Some(local) == fx.mir.spread_arg { | |
188 | // This argument (e.g. the last argument in the "rust-call" ABI) | |
189 | // is a tuple that was spread at the ABI level and now we have | |
190 | // to reconstruct it into a tuple local variable, from multiple | |
191 | // individual function arguments. | |
192 | ||
193 | let tupled_arg_tys = match arg_ty.kind() { | |
194 | ty::Tuple(ref tys) => tys, | |
195 | _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty), | |
196 | }; | |
197 | ||
198 | let mut params = Vec::new(); | |
5869c6ff XL |
199 | for (i, _arg_ty) in tupled_arg_tys.types().enumerate() { |
200 | let arg_abi = arg_abis_iter.next().unwrap(); | |
201 | let param = | |
202 | cvalue_for_param(fx, Some(local), Some(i), arg_abi, &mut block_params_iter); | |
29967ef6 XL |
203 | params.push(param); |
204 | } | |
205 | ||
206 | (local, ArgKind::Spread(params), arg_ty) | |
207 | } else { | |
5869c6ff XL |
208 | let arg_abi = arg_abis_iter.next().unwrap(); |
209 | let param = | |
210 | cvalue_for_param(fx, Some(local), None, arg_abi, &mut block_params_iter); | |
29967ef6 XL |
211 | (local, ArgKind::Normal(param), arg_ty) |
212 | } | |
213 | }) | |
214 | .collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>(); | |
215 | ||
216 | assert!(fx.caller_location.is_none()); | |
217 | if fx.instance.def.requires_caller_location(fx.tcx) { | |
218 | // Store caller location for `#[track_caller]`. | |
5869c6ff XL |
219 | let arg_abi = arg_abis_iter.next().unwrap(); |
220 | fx.caller_location = | |
221 | Some(cvalue_for_param(fx, None, None, arg_abi, &mut block_params_iter).unwrap()); | |
29967ef6 XL |
222 | } |
223 | ||
5869c6ff XL |
224 | assert!(arg_abis_iter.next().is_none(), "ArgAbi left behind"); |
225 | fx.fn_abi = Some(fn_abi); | |
226 | assert!(block_params_iter.next().is_none(), "arg_value left behind"); | |
29967ef6 | 227 | |
29967ef6 XL |
228 | self::comments::add_locals_header_comment(fx); |
229 | ||
230 | for (local, arg_kind, ty) in func_params { | |
231 | let layout = fx.layout_of(ty); | |
232 | ||
233 | let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa; | |
234 | ||
235 | // While this is normally an optimization to prevent an unnecessary copy when an argument is | |
236 | // not mutated by the current function, this is necessary to support unsized arguments. | |
237 | if let ArgKind::Normal(Some(val)) = arg_kind { | |
238 | if let Some((addr, meta)) = val.try_to_ptr() { | |
239 | let local_decl = &fx.mir.local_decls[local]; | |
240 | // v this ! is important | |
6a06907d XL |
241 | let internally_mutable = !val |
242 | .layout() | |
243 | .ty | |
244 | .is_freeze(fx.tcx.at(local_decl.source_info.span), ParamEnv::reveal_all()); | |
29967ef6 XL |
245 | if local_decl.mutability == mir::Mutability::Not && !internally_mutable { |
246 | // We wont mutate this argument, so it is fine to borrow the backing storage | |
247 | // of this argument, to prevent a copy. | |
248 | ||
249 | let place = if let Some(meta) = meta { | |
250 | CPlace::for_ptr_with_extra(addr, meta, val.layout()) | |
251 | } else { | |
252 | CPlace::for_ptr(addr, val.layout()) | |
253 | }; | |
254 | ||
29967ef6 XL |
255 | self::comments::add_local_place_comments(fx, place, local); |
256 | ||
257 | assert_eq!(fx.local_map.push(place), local); | |
258 | continue; | |
259 | } | |
260 | } | |
261 | } | |
262 | ||
263 | let place = make_local_place(fx, local, layout, is_ssa); | |
264 | assert_eq!(fx.local_map.push(place), local); | |
265 | ||
266 | match arg_kind { | |
267 | ArgKind::Normal(param) => { | |
268 | if let Some(param) = param { | |
269 | place.write_cvalue(fx, param); | |
270 | } | |
271 | } | |
272 | ArgKind::Spread(params) => { | |
273 | for (i, param) in params.into_iter().enumerate() { | |
274 | if let Some(param) = param { | |
6a06907d | 275 | place.place_field(fx, mir::Field::new(i)).write_cvalue(fx, param); |
29967ef6 XL |
276 | } |
277 | } | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | for local in fx.mir.vars_and_temps_iter() { | |
fc512014 | 283 | let ty = fx.monomorphize(fx.mir.local_decls[local].ty); |
29967ef6 XL |
284 | let layout = fx.layout_of(ty); |
285 | ||
286 | let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa; | |
287 | ||
288 | let place = make_local_place(fx, local, layout, is_ssa); | |
289 | assert_eq!(fx.local_map.push(place), local); | |
290 | } | |
291 | ||
6a06907d | 292 | fx.bcx.ins().jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]); |
29967ef6 XL |
293 | } |
294 | ||
295 | pub(crate) fn codegen_terminator_call<'tcx>( | |
6a06907d | 296 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
29967ef6 XL |
297 | span: Span, |
298 | current_block: Block, | |
299 | func: &Operand<'tcx>, | |
300 | args: &[Operand<'tcx>], | |
301 | destination: Option<(Place<'tcx>, BasicBlock)>, | |
302 | ) { | |
fc512014 | 303 | let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx)); |
6a06907d XL |
304 | let fn_sig = |
305 | fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx)); | |
29967ef6 XL |
306 | |
307 | let destination = destination.map(|(place, bb)| (codegen_place(fx, place), bb)); | |
308 | ||
309 | // Handle special calls like instrinsics and empty drop glue. | |
310 | let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() { | |
311 | let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs) | |
312 | .unwrap() | |
313 | .unwrap() | |
314 | .polymorphize(fx.tcx); | |
315 | ||
316 | if fx.tcx.symbol_name(instance).name.starts_with("llvm.") { | |
317 | crate::intrinsics::codegen_llvm_intrinsic_call( | |
318 | fx, | |
319 | &fx.tcx.symbol_name(instance).name, | |
320 | substs, | |
321 | args, | |
322 | destination, | |
323 | ); | |
324 | return; | |
325 | } | |
326 | ||
327 | match instance.def { | |
328 | InstanceDef::Intrinsic(_) => { | |
329 | crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span); | |
330 | return; | |
331 | } | |
332 | InstanceDef::DropGlue(_, None) => { | |
333 | // empty drop glue - a nop. | |
334 | let (_, dest) = destination.expect("Non terminating drop_in_place_real???"); | |
335 | let ret_block = fx.get_block(dest); | |
336 | fx.bcx.ins().jump(ret_block, &[]); | |
337 | return; | |
338 | } | |
339 | _ => Some(instance), | |
340 | } | |
341 | } else { | |
342 | None | |
343 | }; | |
344 | ||
5869c6ff XL |
345 | let extra_args = &args[fn_sig.inputs().len()..]; |
346 | let extra_args = extra_args | |
347 | .iter() | |
348 | .map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx))) | |
349 | .collect::<Vec<_>>(); | |
350 | let fn_abi = if let Some(instance) = instance { | |
351 | FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args) | |
352 | } else { | |
6a06907d | 353 | FnAbi::of_fn_ptr(&RevealAllLayoutCx(fx.tcx), fn_ty.fn_sig(fx.tcx), &extra_args) |
5869c6ff XL |
354 | }; |
355 | ||
29967ef6 | 356 | let is_cold = instance |
6a06907d | 357 | .map(|inst| fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD)) |
29967ef6 XL |
358 | .unwrap_or(false); |
359 | if is_cold { | |
360 | fx.cold_blocks.insert(current_block); | |
361 | } | |
362 | ||
363 | // Unpack arguments tuple for closures | |
364 | let args = if fn_sig.abi == Abi::RustCall { | |
365 | assert_eq!(args.len(), 2, "rust-call abi requires two arguments"); | |
366 | let self_arg = codegen_operand(fx, &args[0]); | |
367 | let pack_arg = codegen_operand(fx, &args[1]); | |
368 | ||
369 | let tupled_arguments = match pack_arg.layout().ty.kind() { | |
370 | ty::Tuple(ref tupled_arguments) => tupled_arguments, | |
371 | _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"), | |
372 | }; | |
373 | ||
374 | let mut args = Vec::with_capacity(1 + tupled_arguments.len()); | |
375 | args.push(self_arg); | |
376 | for i in 0..tupled_arguments.len() { | |
377 | args.push(pack_arg.value_field(fx, mir::Field::new(i))); | |
378 | } | |
379 | args | |
380 | } else { | |
6a06907d | 381 | args.iter().map(|arg| codegen_operand(fx, arg)).collect::<Vec<_>>() |
29967ef6 XL |
382 | }; |
383 | ||
384 | // | indirect call target | |
385 | // | | the first argument to be passed | |
5869c6ff XL |
386 | // v v |
387 | let (func_ref, first_arg) = match instance { | |
29967ef6 | 388 | // Trait object call |
6a06907d | 389 | Some(Instance { def: InstanceDef::Virtual(_, idx), .. }) => { |
cdc7bbd5 | 390 | if fx.clif_comments.enabled() { |
29967ef6 XL |
391 | let nop_inst = fx.bcx.ins().nop(); |
392 | fx.add_comment( | |
393 | nop_inst, | |
5869c6ff | 394 | format!("virtual call; self arg pass mode: {:?}", &fn_abi.args[0],), |
29967ef6 XL |
395 | ); |
396 | } | |
397 | let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx); | |
5869c6ff | 398 | (Some(method), smallvec![ptr]) |
29967ef6 XL |
399 | } |
400 | ||
401 | // Normal call | |
402 | Some(_) => ( | |
403 | None, | |
404 | args.get(0) | |
5869c6ff XL |
405 | .map(|arg| adjust_arg_for_abi(fx, *arg, &fn_abi.args[0])) |
406 | .unwrap_or(smallvec![]), | |
29967ef6 XL |
407 | ), |
408 | ||
409 | // Indirect call | |
410 | None => { | |
cdc7bbd5 | 411 | if fx.clif_comments.enabled() { |
29967ef6 XL |
412 | let nop_inst = fx.bcx.ins().nop(); |
413 | fx.add_comment(nop_inst, "indirect call"); | |
414 | } | |
415 | let func = codegen_operand(fx, func).load_scalar(fx); | |
416 | ( | |
417 | Some(func), | |
418 | args.get(0) | |
5869c6ff XL |
419 | .map(|arg| adjust_arg_for_abi(fx, *arg, &fn_abi.args[0])) |
420 | .unwrap_or(smallvec![]), | |
29967ef6 XL |
421 | ) |
422 | } | |
423 | }; | |
424 | ||
425 | let ret_place = destination.map(|(place, _)| place); | |
5869c6ff XL |
426 | let (call_inst, call_args) = self::returning::codegen_with_call_return_arg( |
427 | fx, | |
428 | &fn_abi.ret, | |
429 | ret_place, | |
430 | |fx, return_ptr| { | |
431 | let regular_args_count = args.len(); | |
29967ef6 XL |
432 | let mut call_args: Vec<Value> = return_ptr |
433 | .into_iter() | |
434 | .chain(first_arg.into_iter()) | |
435 | .chain( | |
436 | args.into_iter() | |
5869c6ff | 437 | .enumerate() |
29967ef6 | 438 | .skip(1) |
5869c6ff | 439 | .map(|(i, arg)| adjust_arg_for_abi(fx, arg, &fn_abi.args[i]).into_iter()) |
29967ef6 XL |
440 | .flatten(), |
441 | ) | |
442 | .collect::<Vec<_>>(); | |
443 | ||
6a06907d | 444 | if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) { |
29967ef6 XL |
445 | // Pass the caller location for `#[track_caller]`. |
446 | let caller_location = fx.get_caller_location(span); | |
5869c6ff XL |
447 | call_args.extend( |
448 | adjust_arg_for_abi(fx, caller_location, &fn_abi.args[regular_args_count]) | |
449 | .into_iter(), | |
450 | ); | |
451 | assert_eq!(fn_abi.args.len(), regular_args_count + 1); | |
452 | } else { | |
453 | assert_eq!(fn_abi.args.len(), regular_args_count); | |
29967ef6 XL |
454 | } |
455 | ||
456 | let call_inst = if let Some(func_ref) = func_ref { | |
5869c6ff | 457 | let sig = clif_sig_from_fn_abi(fx.tcx, fx.triple(), &fn_abi); |
29967ef6 XL |
458 | let sig = fx.bcx.import_signature(sig); |
459 | fx.bcx.ins().call_indirect(sig, func_ref, &call_args) | |
460 | } else { | |
461 | let func_ref = | |
462 | fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type")); | |
463 | fx.bcx.ins().call(func_ref, &call_args) | |
464 | }; | |
465 | ||
466 | (call_inst, call_args) | |
5869c6ff XL |
467 | }, |
468 | ); | |
29967ef6 XL |
469 | |
470 | // FIXME find a cleaner way to support varargs | |
471 | if fn_sig.c_variadic { | |
6a06907d | 472 | if !matches!(fn_sig.abi, Abi::C { .. }) { |
cdc7bbd5 | 473 | fx.tcx.sess.span_fatal(span, &format!("Variadic call for non-C abi {:?}", fn_sig.abi)); |
29967ef6 XL |
474 | } |
475 | let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap(); | |
476 | let abi_params = call_args | |
477 | .into_iter() | |
478 | .map(|arg| { | |
479 | let ty = fx.bcx.func.dfg.value_type(arg); | |
480 | if !ty.is_int() { | |
481 | // FIXME set %al to upperbound on float args once floats are supported | |
6a06907d | 482 | fx.tcx.sess.span_fatal(span, &format!("Non int ty {:?} for variadic call", ty)); |
29967ef6 XL |
483 | } |
484 | AbiParam::new(ty) | |
485 | }) | |
486 | .collect::<Vec<AbiParam>>(); | |
487 | fx.bcx.func.dfg.signatures[sig_ref].params = abi_params; | |
488 | } | |
489 | ||
490 | if let Some((_, dest)) = destination { | |
491 | let ret_block = fx.get_block(dest); | |
492 | fx.bcx.ins().jump(ret_block, &[]); | |
493 | } else { | |
494 | trap_unreachable(fx, "[corruption] Diverging function returned"); | |
495 | } | |
496 | } | |
497 | ||
498 | pub(crate) fn codegen_drop<'tcx>( | |
6a06907d | 499 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
29967ef6 XL |
500 | span: Span, |
501 | drop_place: CPlace<'tcx>, | |
502 | ) { | |
503 | let ty = drop_place.layout().ty; | |
5869c6ff | 504 | let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx); |
29967ef6 | 505 | |
5869c6ff | 506 | if let ty::InstanceDef::DropGlue(_, None) = drop_instance.def { |
29967ef6 XL |
507 | // we don't actually need to drop anything |
508 | } else { | |
29967ef6 XL |
509 | match ty.kind() { |
510 | ty::Dynamic(..) => { | |
511 | let (ptr, vtable) = drop_place.to_ptr_maybe_unsized(); | |
512 | let ptr = ptr.get_addr(fx); | |
513 | let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap()); | |
514 | ||
5869c6ff XL |
515 | // FIXME(eddyb) perhaps move some of this logic into |
516 | // `Instance::resolve_drop_in_place`? | |
517 | let virtual_drop = Instance { | |
518 | def: ty::InstanceDef::Virtual(drop_instance.def_id(), 0), | |
519 | substs: drop_instance.substs, | |
520 | }; | |
521 | let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), virtual_drop, &[]); | |
522 | ||
523 | let sig = clif_sig_from_fn_abi(fx.tcx, fx.triple(), &fn_abi); | |
29967ef6 XL |
524 | let sig = fx.bcx.import_signature(sig); |
525 | fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]); | |
526 | } | |
527 | _ => { | |
5869c6ff XL |
528 | assert!(!matches!(drop_instance.def, InstanceDef::Virtual(_, _))); |
529 | ||
530 | let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), drop_instance, &[]); | |
29967ef6 XL |
531 | |
532 | let arg_value = drop_place.place_ref( | |
533 | fx, | |
534 | fx.layout_of(fx.tcx.mk_ref( | |
535 | &ty::RegionKind::ReErased, | |
6a06907d | 536 | TypeAndMut { ty, mutbl: crate::rustc_hir::Mutability::Mut }, |
29967ef6 XL |
537 | )), |
538 | ); | |
5869c6ff | 539 | let arg_value = adjust_arg_for_abi(fx, arg_value, &fn_abi.args[0]); |
29967ef6 XL |
540 | |
541 | let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>(); | |
542 | ||
5869c6ff | 543 | if drop_instance.def.requires_caller_location(fx.tcx) { |
29967ef6 XL |
544 | // Pass the caller location for `#[track_caller]`. |
545 | let caller_location = fx.get_caller_location(span); | |
5869c6ff XL |
546 | call_args.extend( |
547 | adjust_arg_for_abi(fx, caller_location, &fn_abi.args[1]).into_iter(), | |
548 | ); | |
29967ef6 XL |
549 | } |
550 | ||
5869c6ff | 551 | let func_ref = fx.get_function_ref(drop_instance); |
29967ef6 XL |
552 | fx.bcx.ins().call(func_ref, &call_args); |
553 | } | |
554 | } | |
555 | } | |
556 | } |