2018-12-22 10:42:38 -06:00
|
|
|
use std::borrow::Cow;
|
2018-07-20 06:36:53 -05:00
|
|
|
use std::iter;
|
2018-07-19 12:33:42 -05:00
|
|
|
|
2018-11-07 06:29:38 -06:00
|
|
|
use rustc::hir;
|
2019-01-02 06:37:56 -06:00
|
|
|
use rustc::ty::layout::{Scalar, Primitive, Integer, FloatTy};
|
2018-11-07 06:29:38 -06:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2018-07-19 12:33:42 -05:00
|
|
|
|
2018-07-30 09:57:40 -05:00
|
|
|
use crate::prelude::*;
|
2018-07-19 12:33:42 -05:00
|
|
|
|
2018-12-25 09:47:33 -06:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-08-11 05:34:46 -05:00
|
|
|
enum PassMode {
|
|
|
|
NoPass,
|
|
|
|
ByVal(Type),
|
|
|
|
ByRef,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PassMode {
|
2018-08-18 10:10:02 -05:00
|
|
|
fn get_param_ty(self, fx: &FunctionCx<impl Backend>) -> Type {
|
2018-08-11 05:34:46 -05:00
|
|
|
match self {
|
|
|
|
PassMode::NoPass => unimplemented!("pass mode nopass"),
|
2018-11-12 09:23:39 -06:00
|
|
|
PassMode::ByVal(clif_type) => clif_type,
|
2018-11-03 07:14:28 -05:00
|
|
|
PassMode::ByRef => fx.pointer_type,
|
2018-08-11 05:34:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:37:56 -06:00
|
|
|
pub fn scalar_to_clif_type(tcx: TyCtxt, scalar: Scalar) -> Type {
|
|
|
|
match scalar.value {
|
|
|
|
Primitive::Int(int, _sign) => match int {
|
|
|
|
Integer::I8 => types::I8,
|
|
|
|
Integer::I16 => types::I16,
|
|
|
|
Integer::I32 => types::I32,
|
|
|
|
Integer::I64 => types::I64,
|
|
|
|
Integer::I128 => unimpl!("u/i128"),
|
|
|
|
}
|
|
|
|
Primitive::Float(flt) => match flt {
|
|
|
|
FloatTy::F32 => types::F32,
|
|
|
|
FloatTy::F64 => types::F64,
|
|
|
|
}
|
|
|
|
Primitive::Pointer => pointer_ty(tcx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 05:34:46 -05:00
|
|
|
fn get_pass_mode<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
is_return: bool,
|
|
|
|
) -> PassMode {
|
2019-01-02 07:03:56 -06:00
|
|
|
let layout = tcx
|
2018-11-07 06:32:02 -06:00
|
|
|
.layout_of(ParamEnv::reveal_all().and(ty))
|
2019-01-02 07:03:56 -06:00
|
|
|
.unwrap();
|
|
|
|
assert!(!layout.is_unsized());
|
|
|
|
|
|
|
|
if layout.size.bytes() == 0 {
|
2018-08-11 05:34:46 -05:00
|
|
|
if is_return {
|
|
|
|
PassMode::NoPass
|
|
|
|
} else {
|
|
|
|
PassMode::ByRef
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-02 07:03:56 -06:00
|
|
|
match &layout.abi {
|
|
|
|
layout::Abi::Uninhabited => {
|
|
|
|
if is_return {
|
|
|
|
PassMode::NoPass
|
|
|
|
} else {
|
|
|
|
PassMode::ByRef
|
|
|
|
}
|
|
|
|
}
|
|
|
|
layout::Abi::Scalar(scalar) => PassMode::ByVal(scalar_to_clif_type(tcx, scalar.clone())),
|
|
|
|
|
|
|
|
// FIXME implement ScalarPair and Vector Abi in a cg_llvm compatible way
|
|
|
|
layout::Abi::ScalarPair(_, _) => PassMode::ByRef,
|
|
|
|
layout::Abi::Vector { .. } => PassMode::ByRef,
|
|
|
|
|
|
|
|
layout::Abi::Aggregate { .. } => PassMode::ByRef,
|
2018-08-11 05:34:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 11:00:06 -05:00
|
|
|
fn adjust_arg_for_abi<'a, 'tcx: 'a>(
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
arg: CValue<'tcx>,
|
|
|
|
) -> Value {
|
2019-01-02 07:03:56 -06:00
|
|
|
match get_pass_mode(fx.tcx, arg.layout().ty, false) {
|
2018-09-08 11:00:06 -05:00
|
|
|
PassMode::NoPass => unimplemented!("pass mode nopass"),
|
2019-01-02 06:37:56 -06:00
|
|
|
PassMode::ByVal(_) => arg.load_scalar(fx),
|
2018-09-08 11:00:06 -05:00
|
|
|
PassMode::ByRef => arg.force_stack(fx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 05:20:32 -06:00
|
|
|
fn clif_sig_from_fn_sig<'a, 'tcx: 'a>(
|
2018-07-31 05:25:16 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2019-01-02 05:20:32 -06:00
|
|
|
sig: FnSig<'tcx>,
|
2018-07-31 05:25:16 -05:00
|
|
|
) -> Signature {
|
2018-08-11 05:34:46 -05:00
|
|
|
let (call_conv, inputs, output): (CallConv, Vec<Ty>, Ty) = match sig.abi {
|
2018-12-11 08:13:45 -06:00
|
|
|
Abi::Rust => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
|
2018-07-30 11:20:37 -05:00
|
|
|
Abi::C => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
|
2018-07-20 06:36:53 -05:00
|
|
|
Abi::RustCall => {
|
2018-07-27 12:01:38 -05:00
|
|
|
assert_eq!(sig.inputs().len(), 2);
|
2018-07-20 06:38:49 -05:00
|
|
|
let extra_args = match sig.inputs().last().unwrap().sty {
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Tuple(ref tupled_arguments) => tupled_arguments,
|
2018-07-20 06:38:49 -05:00
|
|
|
_ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
|
|
|
|
};
|
2018-07-27 12:01:38 -05:00
|
|
|
let mut inputs: Vec<Ty> = vec![sig.inputs()[0]];
|
2018-07-20 06:38:49 -05:00
|
|
|
inputs.extend(extra_args.into_iter());
|
2018-12-11 08:13:45 -06:00
|
|
|
(CallConv::SystemV, inputs, sig.output())
|
2018-07-20 06:36:53 -05:00
|
|
|
}
|
|
|
|
Abi::System => bug!("system abi should be selected elsewhere"),
|
2018-07-20 07:20:37 -05:00
|
|
|
Abi::RustIntrinsic => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
|
2018-07-20 06:36:53 -05:00
|
|
|
_ => unimplemented!("unsupported abi {:?}", sig.abi),
|
2018-07-19 12:33:42 -05:00
|
|
|
};
|
2018-08-11 05:34:46 -05:00
|
|
|
|
|
|
|
let inputs = inputs
|
|
|
|
.into_iter()
|
2019-01-02 07:03:56 -06:00
|
|
|
.filter_map(|ty| match get_pass_mode(tcx, ty, false) {
|
2018-11-12 09:23:39 -06:00
|
|
|
PassMode::ByVal(clif_ty) => Some(clif_ty),
|
2018-08-11 05:34:46 -05:00
|
|
|
PassMode::NoPass => unimplemented!("pass mode nopass"),
|
2018-08-18 10:10:02 -05:00
|
|
|
PassMode::ByRef => Some(pointer_ty(tcx)),
|
2018-08-11 05:34:46 -05:00
|
|
|
});
|
|
|
|
|
2019-01-02 07:03:56 -06:00
|
|
|
let (params, returns) = match get_pass_mode(tcx, output, true) {
|
2018-08-11 05:34:46 -05:00
|
|
|
PassMode::NoPass => (inputs.map(AbiParam::new).collect(), vec![]),
|
|
|
|
PassMode::ByVal(ret_ty) => (
|
|
|
|
inputs.map(AbiParam::new).collect(),
|
|
|
|
vec![AbiParam::new(ret_ty)],
|
|
|
|
),
|
|
|
|
PassMode::ByRef => {
|
|
|
|
(
|
2018-09-08 10:24:52 -05:00
|
|
|
Some(pointer_ty(tcx)) // First param is place to put return val
|
|
|
|
.into_iter()
|
2018-08-11 05:34:46 -05:00
|
|
|
.chain(inputs)
|
|
|
|
.map(AbiParam::new)
|
|
|
|
.collect(),
|
|
|
|
vec![],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-19 12:33:42 -05:00
|
|
|
Signature {
|
2018-08-11 05:34:46 -05:00
|
|
|
params,
|
|
|
|
returns,
|
2018-07-19 12:33:42 -05:00
|
|
|
call_conv,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 14:42:29 -06:00
|
|
|
pub fn ty_fn_sig<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> ty::FnSig<'tcx> {
|
2018-07-20 06:38:49 -05:00
|
|
|
let sig = match ty.sty {
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::FnDef(..) |
|
2018-07-20 06:36:53 -05:00
|
|
|
// Shims currently have type TyFnPtr. Not sure this should remain.
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::FnPtr(_) => ty.fn_sig(tcx),
|
|
|
|
ty::Closure(def_id, substs) => {
|
2018-07-20 06:36:53 -05:00
|
|
|
let sig = substs.closure_sig(def_id, tcx);
|
|
|
|
|
|
|
|
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
|
|
|
|
sig.map_bound(|sig| tcx.mk_fn_sig(
|
|
|
|
iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
|
|
|
|
sig.output(),
|
|
|
|
sig.variadic,
|
|
|
|
sig.unsafety,
|
|
|
|
sig.abi
|
|
|
|
))
|
|
|
|
}
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Generator(def_id, substs, _) => {
|
2018-07-20 06:36:53 -05:00
|
|
|
let sig = substs.poly_sig(def_id, tcx);
|
|
|
|
|
|
|
|
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
|
|
|
|
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
|
|
|
|
|
|
|
|
sig.map_bound(|sig| {
|
|
|
|
let state_did = tcx.lang_items().gen_state().unwrap();
|
|
|
|
let state_adt_ref = tcx.adt_def(state_did);
|
|
|
|
let state_substs = tcx.intern_substs(&[
|
|
|
|
sig.yield_ty.into(),
|
|
|
|
sig.return_ty.into(),
|
|
|
|
]);
|
|
|
|
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
|
|
|
|
|
|
|
|
tcx.mk_fn_sig(iter::once(env_ty),
|
|
|
|
ret_ty,
|
|
|
|
false,
|
|
|
|
hir::Unsafety::Normal,
|
|
|
|
Abi::Rust
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => bug!("unexpected type {:?} to ty_fn_sig", ty)
|
2018-07-20 06:38:49 -05:00
|
|
|
};
|
|
|
|
tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &sig)
|
2018-07-20 06:36:53 -05:00
|
|
|
}
|
|
|
|
|
2018-08-11 07:10:00 -05:00
|
|
|
pub fn get_function_name_and_sig<'a, 'tcx>(
|
2018-08-11 06:59:08 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
inst: Instance<'tcx>,
|
2019-02-11 12:18:52 -06:00
|
|
|
support_vararg: bool
|
2018-08-11 06:59:08 -05:00
|
|
|
) -> (String, Signature) {
|
|
|
|
assert!(!inst.substs.needs_infer() && !inst.substs.has_param_types());
|
|
|
|
let fn_ty = inst.ty(tcx);
|
2019-01-02 05:20:32 -06:00
|
|
|
let fn_sig = ty_fn_sig(tcx, fn_ty);
|
2019-02-11 12:18:52 -06:00
|
|
|
if fn_sig.variadic && !support_vararg {
|
|
|
|
unimpl!("Variadic function definitions are not yet supported");
|
2019-01-02 05:20:32 -06:00
|
|
|
}
|
|
|
|
let sig = clif_sig_from_fn_sig(tcx, fn_sig);
|
2018-08-11 07:29:32 -05:00
|
|
|
(tcx.symbol_name(inst).as_str().to_string(), sig)
|
2018-08-11 06:59:08 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 05:20:32 -06:00
|
|
|
/// Instance must be monomorphized
|
|
|
|
pub fn import_function<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
module: &mut Module<impl Backend>,
|
|
|
|
inst: Instance<'tcx>,
|
|
|
|
) -> FuncId {
|
2019-02-11 12:18:52 -06:00
|
|
|
let (name, sig) = get_function_name_and_sig(tcx, inst, true);
|
2019-01-02 05:20:32 -06:00
|
|
|
module
|
|
|
|
.declare_function(&name, Linkage::Import, &sig)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2018-09-08 11:00:06 -05:00
|
|
|
|
2019-01-02 05:20:32 -06:00
|
|
|
impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
|
2018-09-08 11:00:06 -05:00
|
|
|
/// Instance must be monomorphized
|
|
|
|
pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
|
2019-01-02 05:20:32 -06:00
|
|
|
let func_id = import_function(self.tcx, self.module, inst);
|
2018-12-27 03:59:01 -06:00
|
|
|
let func_ref = self.module
|
|
|
|
.declare_func_in_func(func_id, &mut self.bcx.func);
|
2018-12-28 10:07:40 -06:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
2018-12-27 03:59:01 -06:00
|
|
|
self.add_entity_comment(func_ref, format!("{:?}", inst));
|
2018-12-28 10:07:40 -06:00
|
|
|
|
2018-12-27 03:59:01 -06:00
|
|
|
func_ref
|
2018-07-19 12:33:42 -05:00
|
|
|
}
|
2018-07-20 06:36:53 -05:00
|
|
|
|
2018-07-30 08:58:46 -05:00
|
|
|
fn lib_call(
|
2018-07-30 08:34:34 -05:00
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
input_tys: Vec<types::Type>,
|
2018-07-30 08:58:46 -05:00
|
|
|
output_ty: Option<types::Type>,
|
2018-07-30 08:34:34 -05:00
|
|
|
args: &[Value],
|
2018-07-30 08:58:46 -05:00
|
|
|
) -> Option<Value> {
|
2018-07-30 08:34:34 -05:00
|
|
|
let sig = Signature {
|
|
|
|
params: input_tys.iter().cloned().map(AbiParam::new).collect(),
|
2018-09-08 10:24:52 -05:00
|
|
|
returns: output_ty
|
|
|
|
.map(|output_ty| vec![AbiParam::new(output_ty)])
|
|
|
|
.unwrap_or(Vec::new()),
|
2018-07-30 08:34:34 -05:00
|
|
|
call_conv: CallConv::SystemV,
|
|
|
|
};
|
2018-07-31 05:25:16 -05:00
|
|
|
let func_id = self
|
|
|
|
.module
|
|
|
|
.declare_function(&name, Linkage::Import, &sig)
|
|
|
|
.unwrap();
|
|
|
|
let func_ref = self
|
|
|
|
.module
|
|
|
|
.declare_func_in_func(func_id, &mut self.bcx.func);
|
2018-07-30 08:34:34 -05:00
|
|
|
let call_inst = self.bcx.ins().call(func_ref, args);
|
2018-07-30 08:58:46 -05:00
|
|
|
if output_ty.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
2018-07-30 08:34:34 -05:00
|
|
|
let results = self.bcx.inst_results(call_inst);
|
|
|
|
assert_eq!(results.len(), 1);
|
2018-07-30 08:58:46 -05:00
|
|
|
Some(results[0])
|
2018-07-30 08:34:34 -05:00
|
|
|
}
|
|
|
|
|
2018-07-31 05:25:16 -05:00
|
|
|
pub fn easy_call(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
args: &[CValue<'tcx>],
|
|
|
|
return_ty: Ty<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
|
|
|
let (input_tys, args): (Vec<_>, Vec<_>) = args
|
|
|
|
.into_iter()
|
|
|
|
.map(|arg| {
|
|
|
|
(
|
2018-11-12 09:23:39 -06:00
|
|
|
self.clif_type(arg.layout().ty).unwrap(),
|
2019-01-02 06:37:56 -06:00
|
|
|
arg.load_scalar(self),
|
2018-07-31 05:25:16 -05:00
|
|
|
)
|
2018-10-10 12:07:13 -05:00
|
|
|
})
|
|
|
|
.unzip();
|
2018-07-30 08:34:34 -05:00
|
|
|
let return_layout = self.layout_of(return_ty);
|
2018-08-24 07:51:02 -05:00
|
|
|
let return_ty = if let ty::Tuple(tup) = return_ty.sty {
|
2018-07-30 08:58:46 -05:00
|
|
|
if !tup.is_empty() {
|
|
|
|
bug!("easy_call( (...) -> <non empty tuple> ) is not allowed");
|
|
|
|
}
|
|
|
|
None
|
|
|
|
} else {
|
2018-11-12 09:23:39 -06:00
|
|
|
Some(self.clif_type(return_ty).unwrap())
|
2018-07-30 08:58:46 -05:00
|
|
|
};
|
|
|
|
if let Some(val) = self.lib_call(name, input_tys, return_ty, &args) {
|
|
|
|
CValue::ByVal(val, return_layout)
|
|
|
|
} else {
|
2018-11-07 06:32:02 -06:00
|
|
|
CValue::ByRef(self.bcx.ins().iconst(self.pointer_type, 0), return_layout)
|
2018-07-30 08:58:46 -05:00
|
|
|
}
|
2018-07-30 08:34:34 -05:00
|
|
|
}
|
|
|
|
|
2018-07-20 06:36:53 -05:00
|
|
|
fn self_sig(&self) -> FnSig<'tcx> {
|
2018-07-20 06:38:49 -05:00
|
|
|
ty_fn_sig(self.tcx, self.instance.ty(self.tcx))
|
2018-07-20 06:36:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn return_type(&self) -> Ty<'tcx> {
|
|
|
|
self.self_sig().output()
|
|
|
|
}
|
2018-07-19 12:33:42 -05:00
|
|
|
}
|
|
|
|
|
2018-12-28 10:07:40 -06:00
|
|
|
#[cfg(debug_assertions)]
|
2018-12-27 03:59:01 -06:00
|
|
|
fn add_arg_comment<'a, 'tcx: 'a>(
|
2018-12-22 10:42:38 -06:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
msg: &str,
|
|
|
|
local: mir::Local,
|
|
|
|
local_field: Option<usize>,
|
|
|
|
param: Option<Value>,
|
2018-12-27 03:59:01 -06:00
|
|
|
pass_mode: PassMode,
|
2018-12-22 10:42:38 -06:00
|
|
|
ssa: crate::analyze::Flags,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) {
|
|
|
|
let local_field = if let Some(local_field) = local_field {
|
|
|
|
Cow::Owned(format!(".{}", local_field))
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed("")
|
|
|
|
};
|
|
|
|
let param = if let Some(param) = param {
|
|
|
|
Cow::Owned(format!("= {:?}", param))
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed("-")
|
|
|
|
};
|
2018-12-27 03:59:01 -06:00
|
|
|
let pass_mode = format!("{:?}", pass_mode);
|
2018-12-22 10:42:38 -06:00
|
|
|
fx.add_global_comment(format!(
|
|
|
|
"{msg:5} {local:>3}{local_field:<5} {param:10} {pass_mode:20} {ssa:10} {ty:?}",
|
|
|
|
msg=msg, local=format!("{:?}", local), local_field=local_field, param=param, pass_mode=pass_mode, ssa=format!("{:?}", ssa), ty=ty,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-12-28 10:07:40 -06:00
|
|
|
#[cfg(debug_assertions)]
|
2018-12-22 10:42:38 -06:00
|
|
|
fn add_local_header_comment(fx: &mut FunctionCx<impl Backend>) {
|
|
|
|
fx.add_global_comment(format!("msg loc.idx param pass mode ssa flags ty"));
|
|
|
|
}
|
|
|
|
|
2018-12-26 04:15:42 -06:00
|
|
|
fn local_place<'a, 'tcx: 'a>(
|
2018-12-25 09:47:33 -06:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
local: Local,
|
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
is_ssa: bool,
|
|
|
|
) -> CPlace<'tcx> {
|
|
|
|
let place = if is_ssa {
|
|
|
|
fx.bcx.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
|
|
|
|
CPlace::Var(local, layout)
|
|
|
|
} else {
|
2019-02-04 12:27:58 -06:00
|
|
|
let place = CPlace::new_stack_slot(fx, layout.ty);
|
2018-12-25 09:47:33 -06:00
|
|
|
|
2018-12-28 10:07:40 -06:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
let TyLayout { ty, details } = layout;
|
|
|
|
let ty::layout::LayoutDetails { size, align, abi: _, variants: _, fields: _ } = details;
|
2019-02-04 12:58:07 -06:00
|
|
|
match place {
|
|
|
|
CPlace::Stack(stack_slot, _) => fx.add_entity_comment(stack_slot, format!(
|
|
|
|
"{:?}: {:?} size={} align={},{}",
|
|
|
|
local, ty, size.bytes(), align.abi.bytes(), align.pref.bytes(),
|
|
|
|
)),
|
|
|
|
CPlace::NoPlace(_) => fx.add_global_comment(format!(
|
|
|
|
"zst {:?}: {:?} size={} align={}, {}",
|
|
|
|
local, ty, size.bytes(), align.abi.bytes(), align.pref.bytes(),
|
|
|
|
)),
|
2019-02-04 12:27:58 -06:00
|
|
|
_ => unreachable!(),
|
2019-02-06 12:07:21 -06:00
|
|
|
}
|
2018-12-28 10:07:40 -06:00
|
|
|
}
|
2018-12-27 03:59:01 -06:00
|
|
|
|
2019-02-04 12:44:16 -06:00
|
|
|
// Take stack_addr in advance to avoid many duplicate instructions
|
|
|
|
CPlace::Addr(place.to_addr(fx), None, layout)
|
2018-12-25 09:47:33 -06:00
|
|
|
};
|
|
|
|
|
2018-12-28 08:18:17 -06:00
|
|
|
let prev_place = fx.local_map.insert(local, place);
|
|
|
|
debug_assert!(prev_place.is_none());
|
2018-12-25 09:47:33 -06:00
|
|
|
fx.local_map[&local]
|
|
|
|
}
|
|
|
|
|
2018-12-27 03:59:01 -06:00
|
|
|
fn cvalue_for_param<'a, 'tcx: 'a>(
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
start_ebb: Ebb,
|
|
|
|
local: mir::Local,
|
|
|
|
local_field: Option<usize>,
|
|
|
|
arg_ty: Ty<'tcx>,
|
|
|
|
ssa_flags: crate::analyze::Flags,
|
|
|
|
) -> CValue<'tcx> {
|
|
|
|
let layout = fx.layout_of(arg_ty);
|
2019-01-02 07:03:56 -06:00
|
|
|
let pass_mode = get_pass_mode(fx.tcx, arg_ty, false);
|
2018-12-27 03:59:01 -06:00
|
|
|
let clif_type = pass_mode.get_param_ty(fx);
|
|
|
|
let ebb_param = fx.bcx.append_ebb_param(start_ebb, clif_type);
|
2018-12-28 10:07:40 -06:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
2018-12-27 03:59:01 -06:00
|
|
|
add_arg_comment(fx, "arg", local, local_field, Some(ebb_param), pass_mode, ssa_flags, arg_ty);
|
2018-12-28 10:07:40 -06:00
|
|
|
|
2018-12-27 03:59:01 -06:00
|
|
|
match pass_mode {
|
2018-12-25 09:47:33 -06:00
|
|
|
PassMode::NoPass => unimplemented!("pass mode nopass"),
|
|
|
|
PassMode::ByVal(_) => CValue::ByVal(ebb_param, layout),
|
|
|
|
PassMode::ByRef => CValue::ByRef(ebb_param, layout),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
start_ebb: Ebb,
|
|
|
|
) {
|
2018-08-09 03:46:56 -05:00
|
|
|
let ssa_analyzed = crate::analyze::analyze(fx);
|
2018-12-28 10:07:40 -06:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
2018-12-26 04:15:42 -06:00
|
|
|
fx.add_global_comment(format!("ssa {:?}", ssa_analyzed));
|
2018-08-09 03:46:56 -05:00
|
|
|
|
2018-08-11 05:34:46 -05:00
|
|
|
let ret_layout = fx.layout_of(fx.return_type());
|
2019-01-02 07:03:56 -06:00
|
|
|
let output_pass_mode = get_pass_mode(fx.tcx, fx.return_type(), true);
|
2018-08-11 05:34:46 -05:00
|
|
|
let ret_param = match output_pass_mode {
|
2018-08-11 06:59:34 -05:00
|
|
|
PassMode::NoPass => None,
|
2018-08-11 08:21:26 -05:00
|
|
|
PassMode::ByVal(_) => None,
|
2018-11-03 07:14:28 -05:00
|
|
|
PassMode::ByRef => Some(fx.bcx.append_ebb_param(start_ebb, fx.pointer_type)),
|
2018-08-11 05:34:46 -05:00
|
|
|
};
|
2018-07-19 12:33:42 -05:00
|
|
|
|
2018-12-28 10:07:40 -06:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
add_local_header_comment(fx);
|
|
|
|
add_arg_comment(fx, "ret", RETURN_PLACE, None, ret_param, output_pass_mode, ssa_analyzed[&RETURN_PLACE], ret_layout.ty);
|
|
|
|
}
|
2018-12-27 03:59:01 -06:00
|
|
|
|
|
|
|
enum ArgKind<'tcx> {
|
|
|
|
Normal(CValue<'tcx>),
|
|
|
|
Spread(Vec<CValue<'tcx>>),
|
2018-07-27 12:27:20 -05:00
|
|
|
}
|
|
|
|
|
2018-08-14 05:13:07 -05:00
|
|
|
let func_params = fx
|
|
|
|
.mir
|
|
|
|
.args_iter()
|
|
|
|
.map(|local| {
|
|
|
|
let arg_ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
|
|
|
|
|
|
|
|
// Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482
|
|
|
|
if Some(local) == fx.mir.spread_arg {
|
|
|
|
// This argument (e.g. the last argument in the "rust-call" ABI)
|
|
|
|
// is a tuple that was spread at the ABI level and now we have
|
|
|
|
// to reconstruct it into a tuple local variable, from multiple
|
|
|
|
// individual function arguments.
|
|
|
|
|
|
|
|
let tupled_arg_tys = match arg_ty.sty {
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Tuple(ref tys) => tys,
|
2018-08-14 05:13:07 -05:00
|
|
|
_ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty),
|
|
|
|
};
|
|
|
|
|
2018-12-27 03:59:01 -06:00
|
|
|
let mut params = Vec::new();
|
2018-12-25 09:47:33 -06:00
|
|
|
for (i, arg_ty) in tupled_arg_tys.iter().enumerate() {
|
2018-12-27 03:59:01 -06:00
|
|
|
let param = cvalue_for_param(fx, start_ebb, local, Some(i), arg_ty, ssa_analyzed[&local]);
|
|
|
|
params.push(param);
|
2018-08-14 05:13:07 -05:00
|
|
|
}
|
|
|
|
|
2018-12-27 03:59:01 -06:00
|
|
|
(local, ArgKind::Spread(params), arg_ty)
|
2018-08-14 05:13:07 -05:00
|
|
|
} else {
|
2018-12-27 03:59:01 -06:00
|
|
|
let param = cvalue_for_param(fx, start_ebb, local, None, arg_ty, ssa_analyzed[&local]);
|
2018-08-14 05:13:07 -05:00
|
|
|
(
|
|
|
|
local,
|
2018-12-27 03:59:01 -06:00
|
|
|
ArgKind::Normal(param),
|
2018-08-14 05:13:07 -05:00
|
|
|
arg_ty,
|
|
|
|
)
|
2018-07-27 12:27:20 -05:00
|
|
|
}
|
2018-10-10 12:07:13 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<(Local, ArgKind, Ty)>>();
|
2018-07-27 12:27:20 -05:00
|
|
|
|
2018-08-14 11:52:43 -05:00
|
|
|
fx.bcx.switch_to_block(start_ebb);
|
|
|
|
|
2018-08-11 05:34:46 -05:00
|
|
|
match output_pass_mode {
|
|
|
|
PassMode::NoPass => {
|
2019-02-04 12:58:07 -06:00
|
|
|
fx.local_map.insert(RETURN_PLACE, CPlace::NoPlace(ret_layout));
|
2018-08-11 05:34:46 -05:00
|
|
|
}
|
2019-01-02 06:37:56 -06:00
|
|
|
PassMode::ByVal(_) => {
|
|
|
|
let is_ssa = !ssa_analyzed
|
|
|
|
.get(&RETURN_PLACE)
|
|
|
|
.unwrap()
|
|
|
|
.contains(crate::analyze::Flags::NOT_SSA);
|
|
|
|
|
|
|
|
local_place(fx, RETURN_PLACE, ret_layout, is_ssa);
|
2018-08-11 05:34:46 -05:00
|
|
|
}
|
|
|
|
PassMode::ByRef => {
|
2018-08-22 08:38:10 -05:00
|
|
|
fx.local_map.insert(
|
|
|
|
RETURN_PLACE,
|
|
|
|
CPlace::Addr(ret_param.unwrap(), None, ret_layout),
|
|
|
|
);
|
2018-08-11 05:34:46 -05:00
|
|
|
}
|
|
|
|
}
|
2018-07-27 12:27:20 -05:00
|
|
|
|
|
|
|
for (local, arg_kind, ty) in func_params {
|
|
|
|
let layout = fx.layout_of(ty);
|
2018-08-09 03:46:56 -05:00
|
|
|
|
2018-12-25 09:47:33 -06:00
|
|
|
let is_ssa = !ssa_analyzed
|
|
|
|
.get(&local)
|
|
|
|
.unwrap()
|
|
|
|
.contains(crate::analyze::Flags::NOT_SSA);
|
|
|
|
|
2018-12-26 04:15:42 -06:00
|
|
|
let place = local_place(fx, local, layout, is_ssa);
|
|
|
|
|
2018-12-22 10:42:38 -06:00
|
|
|
match arg_kind {
|
2018-12-27 03:59:01 -06:00
|
|
|
ArgKind::Normal(param) => {
|
|
|
|
place.write_cvalue(fx, param);
|
2018-12-22 10:42:38 -06:00
|
|
|
}
|
2018-12-27 03:59:01 -06:00
|
|
|
ArgKind::Spread(params) => {
|
|
|
|
for (i, param) in params.into_iter().enumerate() {
|
|
|
|
place.place_field(fx, mir::Field::new(i)).write_cvalue(fx, param);
|
2018-07-27 12:27:20 -05:00
|
|
|
}
|
|
|
|
}
|
2018-07-19 12:33:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for local in fx.mir.vars_and_temps_iter() {
|
|
|
|
let ty = fx.mir.local_decls[local].ty;
|
|
|
|
let layout = fx.layout_of(ty);
|
2018-08-09 03:46:56 -05:00
|
|
|
|
2018-12-26 04:15:42 -06:00
|
|
|
let is_ssa = !ssa_analyzed
|
2018-08-09 04:25:14 -05:00
|
|
|
.get(&local)
|
|
|
|
.unwrap()
|
2018-12-26 04:15:42 -06:00
|
|
|
.contains(crate::analyze::Flags::NOT_SSA);
|
2018-08-09 03:46:56 -05:00
|
|
|
|
2018-12-26 04:15:42 -06:00
|
|
|
local_place(fx, local, layout, is_ssa);
|
2018-07-19 12:33:42 -05:00
|
|
|
}
|
2018-08-14 11:52:43 -05:00
|
|
|
|
|
|
|
fx.bcx
|
|
|
|
.ins()
|
|
|
|
.jump(*fx.ebb_map.get(&START_BLOCK).unwrap(), &[]);
|
2018-07-19 12:33:42 -05:00
|
|
|
}
|
|
|
|
|
2018-09-11 12:27:57 -05:00
|
|
|
pub fn codegen_terminator_call<'a, 'tcx: 'a>(
|
2018-08-14 13:31:16 -05:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-19 12:33:42 -05:00
|
|
|
func: &Operand<'tcx>,
|
|
|
|
args: &[Operand<'tcx>],
|
|
|
|
destination: &Option<(Place<'tcx>, BasicBlock)>,
|
2018-07-20 06:51:34 -05:00
|
|
|
) {
|
2018-11-05 11:03:47 -06:00
|
|
|
let fn_ty = fx.monomorphize(&func.ty(fx.mir, fx.tcx));
|
2018-07-20 06:38:49 -05:00
|
|
|
let sig = ty_fn_sig(fx.tcx, fn_ty);
|
2018-07-21 11:38:08 -05:00
|
|
|
|
2018-07-20 06:38:49 -05:00
|
|
|
// Unpack arguments tuple for closures
|
|
|
|
let args = if sig.abi == Abi::RustCall {
|
|
|
|
assert_eq!(args.len(), 2, "rust-call abi requires two arguments");
|
2018-07-30 09:57:40 -05:00
|
|
|
let self_arg = trans_operand(fx, &args[0]);
|
|
|
|
let pack_arg = trans_operand(fx, &args[1]);
|
2018-07-20 06:38:49 -05:00
|
|
|
let mut args = Vec::new();
|
|
|
|
args.push(self_arg);
|
|
|
|
match pack_arg.layout().ty.sty {
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Tuple(ref tupled_arguments) => {
|
2018-07-20 06:38:49 -05:00
|
|
|
for (i, _) in tupled_arguments.iter().enumerate() {
|
|
|
|
args.push(pack_arg.value_field(fx, mir::Field::new(i)));
|
|
|
|
}
|
2018-07-31 05:25:16 -05:00
|
|
|
}
|
2018-07-20 06:38:49 -05:00
|
|
|
_ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
|
|
|
|
}
|
|
|
|
args
|
|
|
|
} else {
|
2018-07-31 05:25:16 -05:00
|
|
|
args.into_iter()
|
|
|
|
.map(|arg| trans_operand(fx, arg))
|
2018-07-20 06:38:49 -05:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
};
|
2018-07-21 11:38:08 -05:00
|
|
|
|
2018-08-11 06:59:34 -05:00
|
|
|
let destination = destination
|
|
|
|
.as_ref()
|
2018-09-11 12:27:57 -05:00
|
|
|
.map(|&(ref place, bb)| (trans_place(fx, place), bb));
|
|
|
|
|
2018-10-03 11:21:52 -05:00
|
|
|
if let ty::FnDef(def_id, substs) = fn_ty.sty {
|
2018-12-20 07:57:21 -06:00
|
|
|
let instance = ty::Instance::resolve(
|
|
|
|
fx.tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
def_id,
|
|
|
|
substs,
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
match instance.def {
|
|
|
|
InstanceDef::Intrinsic(_) => {
|
|
|
|
crate::intrinsics::codegen_intrinsic_call(fx, def_id, substs, args, destination);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InstanceDef::DropGlue(_, None) => {
|
|
|
|
// empty drop glue - a nop.
|
|
|
|
let (_, dest) = destination.expect("Non terminating drop_in_place_real???");
|
|
|
|
let ret_ebb = fx.get_ebb(dest);
|
|
|
|
fx.bcx.ins().jump(ret_ebb, &[]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-09-11 12:27:57 -05:00
|
|
|
}
|
2018-07-21 11:38:08 -05:00
|
|
|
}
|
2018-10-03 11:21:52 -05:00
|
|
|
|
|
|
|
codegen_call_inner(
|
|
|
|
fx,
|
|
|
|
Some(func),
|
|
|
|
fn_ty,
|
|
|
|
args,
|
|
|
|
destination.map(|(place, _)| place),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some((_, dest)) = destination {
|
|
|
|
let ret_ebb = fx.get_ebb(dest);
|
|
|
|
fx.bcx.ins().jump(ret_ebb, &[]);
|
|
|
|
} else {
|
2018-11-16 10:35:47 -06:00
|
|
|
trap_unreachable(&mut fx.bcx);
|
2018-10-03 11:21:52 -05:00
|
|
|
}
|
2018-09-11 12:27:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn codegen_call_inner<'a, 'tcx: 'a>(
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
func: Option<&Operand<'tcx>>,
|
|
|
|
fn_ty: Ty<'tcx>,
|
|
|
|
args: Vec<CValue<'tcx>>,
|
|
|
|
ret_place: Option<CPlace<'tcx>>,
|
|
|
|
) {
|
2019-01-02 05:20:32 -06:00
|
|
|
let fn_sig = ty_fn_sig(fx.tcx, fn_ty);
|
2018-07-21 11:38:08 -05:00
|
|
|
|
2019-01-02 05:20:32 -06:00
|
|
|
let ret_layout = fx.layout_of(fn_sig.output());
|
2018-08-11 05:34:46 -05:00
|
|
|
|
2019-01-02 07:03:56 -06:00
|
|
|
let output_pass_mode = get_pass_mode(fx.tcx, fn_sig.output(), true);
|
2018-08-11 05:34:46 -05:00
|
|
|
let return_ptr = match output_pass_mode {
|
|
|
|
PassMode::NoPass => None,
|
2018-09-11 12:27:57 -05:00
|
|
|
PassMode::ByRef => match ret_place {
|
2019-02-04 12:27:58 -06:00
|
|
|
Some(ret_place) => Some(ret_place.to_addr(fx)),
|
2018-11-03 07:14:28 -05:00
|
|
|
None => Some(fx.bcx.ins().iconst(fx.pointer_type, 0)),
|
2018-08-11 05:34:46 -05:00
|
|
|
},
|
|
|
|
PassMode::ByVal(_) => None,
|
2018-07-21 11:38:08 -05:00
|
|
|
};
|
|
|
|
|
2018-09-08 11:00:06 -05:00
|
|
|
let instance = match fn_ty.sty {
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::FnDef(def_id, substs) => {
|
2018-09-08 11:00:06 -05:00
|
|
|
Some(Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap())
|
2018-08-19 03:50:39 -05:00
|
|
|
}
|
2018-09-08 11:00:06 -05:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2019-02-07 13:28:55 -06:00
|
|
|
// | Indirect call target
|
|
|
|
// v v the first argument to be passed
|
|
|
|
let (func_ref, first_arg) = match instance {
|
|
|
|
// Trait object call
|
|
|
|
Some(Instance {
|
2018-09-08 11:00:06 -05:00
|
|
|
def: InstanceDef::Virtual(_, idx),
|
|
|
|
..
|
2019-02-07 13:28:55 -06:00
|
|
|
}) => {
|
2018-09-08 11:00:06 -05:00
|
|
|
let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx);
|
2019-02-07 13:28:55 -06:00
|
|
|
(Some(method), Some(ptr))
|
|
|
|
}
|
2018-09-08 11:00:06 -05:00
|
|
|
|
2019-02-07 13:28:55 -06:00
|
|
|
// Normal call
|
|
|
|
Some(_) => (None, args.get(0).map(|arg| adjust_arg_for_abi(fx, *arg))),
|
|
|
|
|
|
|
|
// Indirect call
|
|
|
|
None => {
|
|
|
|
let func = trans_operand(fx, func.expect("indirect call without func Operand")).load_scalar(fx);
|
|
|
|
(Some(func), args.get(0).map(|arg| adjust_arg_for_abi(fx, *arg)))
|
2018-10-10 12:07:13 -05:00
|
|
|
}
|
2018-09-08 11:00:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let call_args: Vec<Value> = return_ptr
|
|
|
|
.into_iter()
|
|
|
|
.chain(first_arg)
|
|
|
|
.chain(
|
|
|
|
args.into_iter()
|
|
|
|
.skip(1)
|
2019-01-02 07:03:56 -06:00
|
|
|
.map(|arg| adjust_arg_for_abi(fx, arg)),
|
2018-10-10 12:07:13 -05:00
|
|
|
)
|
|
|
|
.collect::<Vec<_>>();
|
2018-09-08 11:00:06 -05:00
|
|
|
|
|
|
|
let call_inst = if let Some(func_ref) = func_ref {
|
2019-01-02 05:20:32 -06:00
|
|
|
let sig = fx.bcx.import_signature(clif_sig_from_fn_sig(fx.tcx, fn_sig));
|
2018-09-08 11:00:06 -05:00
|
|
|
fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
|
|
|
|
} else {
|
|
|
|
let func_ref = fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type"));
|
|
|
|
fx.bcx.ins().call(func_ref, &call_args)
|
2018-08-11 05:34:46 -05:00
|
|
|
};
|
|
|
|
|
2019-02-11 12:18:52 -06:00
|
|
|
// FIXME find a cleaner way to support varargs
|
|
|
|
if fn_sig.variadic {
|
|
|
|
if fn_sig.abi != Abi::C {
|
|
|
|
unimpl!("Variadic call for non-C abi {:?}", fn_sig.abi);
|
|
|
|
}
|
|
|
|
let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
|
|
|
|
let abi_params = call_args.into_iter().map(|arg| {
|
|
|
|
let ty = fx.bcx.func.dfg.value_type(arg);
|
|
|
|
if !ty.is_int() {
|
|
|
|
// FIXME set %al to upperbound on float args once floats are supported
|
|
|
|
unimpl!("Non int ty {:?} for variadic call", ty);
|
|
|
|
}
|
|
|
|
AbiParam::new(ty)
|
|
|
|
}).collect::<Vec<AbiParam>>();
|
|
|
|
fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
|
|
|
|
}
|
|
|
|
|
2018-08-11 05:34:46 -05:00
|
|
|
match output_pass_mode {
|
|
|
|
PassMode::NoPass => {}
|
|
|
|
PassMode::ByVal(_) => {
|
2018-09-11 12:27:57 -05:00
|
|
|
if let Some(ret_place) = ret_place {
|
2018-08-19 03:50:39 -05:00
|
|
|
let results = fx.bcx.inst_results(call_inst);
|
2018-08-11 05:34:46 -05:00
|
|
|
ret_place.write_cvalue(fx, CValue::ByVal(results[0], ret_layout));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PassMode::ByRef => {}
|
2018-07-20 06:51:34 -05:00
|
|
|
}
|
2018-07-19 12:33:42 -05:00
|
|
|
}
|
2018-08-10 12:20:13 -05:00
|
|
|
|
2019-02-07 13:45:15 -06:00
|
|
|
pub fn codegen_drop<'a, 'tcx: 'a>(
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
drop_place: CPlace<'tcx>,
|
|
|
|
drop_fn_ty: Ty<'tcx>,
|
|
|
|
) {
|
|
|
|
let (ptr, vtable) = drop_place.to_addr_maybe_unsized(fx);
|
|
|
|
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
|
|
|
|
|
|
|
|
let fn_sig = ty_fn_sig(fx.tcx, drop_fn_ty);
|
|
|
|
|
|
|
|
match get_pass_mode(fx.tcx, fn_sig.output(), true) {
|
|
|
|
PassMode::NoPass => {},
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let sig = fx.bcx.import_signature(clif_sig_from_fn_sig(fx.tcx, fn_sig));
|
|
|
|
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn codegen_return(fx: &mut FunctionCx<impl Backend>) {
|
2019-01-02 07:03:56 -06:00
|
|
|
match get_pass_mode(fx.tcx, fx.return_type(), true) {
|
2018-08-11 05:34:46 -05:00
|
|
|
PassMode::NoPass | PassMode::ByRef => {
|
|
|
|
fx.bcx.ins().return_(&[]);
|
2018-08-11 06:59:34 -05:00
|
|
|
}
|
2018-08-11 05:34:46 -05:00
|
|
|
PassMode::ByVal(_) => {
|
|
|
|
let place = fx.get_local_place(RETURN_PLACE);
|
2019-01-02 06:37:56 -06:00
|
|
|
let ret_val = place.to_cvalue(fx).load_scalar(fx);
|
2018-08-11 05:34:46 -05:00
|
|
|
fx.bcx.ins().return_(&[ret_val]);
|
|
|
|
}
|
|
|
|
}
|
2018-08-11 04:01:48 -05:00
|
|
|
}
|