use std::iter;
use rustc::hir;
+use rustc::ty::layout::{Scalar, Primitive, Integer, FloatTy};
use rustc_target::spec::abi::Abi;
use crate::prelude::*;
}
}
+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),
+ }
+}
+
fn get_pass_mode<'a, 'tcx: 'a>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
- abi: Abi,
ty: Ty<'tcx>,
is_return: bool,
) -> PassMode {
- assert!(!tcx
+ let layout = tcx
.layout_of(ParamEnv::reveal_all().and(ty))
- .unwrap()
- .is_unsized());
- if let ty::Never = ty.sty {
- if is_return {
- PassMode::NoPass
- } else {
- PassMode::ByRef
- }
- } else if ty.sty == tcx.mk_unit().sty {
+ .unwrap();
+ assert!(!layout.is_unsized());
+
+ if layout.size.bytes() == 0 {
if is_return {
PassMode::NoPass
} else {
PassMode::ByRef
}
- } else if let Some(ret_ty) = crate::common::clif_type_from_ty(tcx, ty) {
- PassMode::ByVal(ret_ty)
} else {
- if abi == Abi::C {
- unimpl!(
- "Non scalars are not yet supported for \"C\" abi ({:?}) is_return: {:?}",
- ty,
- is_return
- );
+ 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,
}
- PassMode::ByRef
}
}
fn adjust_arg_for_abi<'a, 'tcx: 'a>(
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
- sig: FnSig<'tcx>,
arg: CValue<'tcx>,
) -> Value {
- match get_pass_mode(fx.tcx, sig.abi, arg.layout().ty, false) {
+ match get_pass_mode(fx.tcx, arg.layout().ty, false) {
PassMode::NoPass => unimplemented!("pass mode nopass"),
- PassMode::ByVal(_) => arg.load_value(fx),
+ PassMode::ByVal(_) => arg.load_scalar(fx),
PassMode::ByRef => arg.force_stack(fx),
}
}
-fn clif_sig_from_fn_ty<'a, 'tcx: 'a>(
+fn clif_sig_from_fn_sig<'a, 'tcx: 'a>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
- fn_ty: Ty<'tcx>,
+ sig: FnSig<'tcx>,
) -> Signature {
- let sig = ty_fn_sig(tcx, fn_ty);
- if sig.variadic {
- unimpl!("Variadic function are not yet supported");
- }
let (call_conv, inputs, output): (CallConv, Vec<Ty>, Ty) = match sig.abi {
Abi::Rust => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
Abi::C => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
let inputs = inputs
.into_iter()
- .filter_map(|ty| match get_pass_mode(tcx, sig.abi, ty, false) {
+ .filter_map(|ty| match get_pass_mode(tcx, ty, false) {
PassMode::ByVal(clif_ty) => Some(clif_ty),
PassMode::NoPass => unimplemented!("pass mode nopass"),
PassMode::ByRef => Some(pointer_ty(tcx)),
});
- let (params, returns) = match get_pass_mode(tcx, sig.abi, output, true) {
+ let (params, returns) = match get_pass_mode(tcx, output, true) {
PassMode::NoPass => (inputs.map(AbiParam::new).collect(), vec![]),
PassMode::ByVal(ret_ty) => (
inputs.map(AbiParam::new).collect(),
) -> (String, Signature) {
assert!(!inst.substs.needs_infer() && !inst.substs.has_param_types());
let fn_ty = inst.ty(tcx);
- let sig = clif_sig_from_fn_ty(tcx, fn_ty);
+ let fn_sig = ty_fn_sig(tcx, fn_ty);
+ if fn_sig.variadic {
+ unimpl!("Variadic functions are not yet supported");
+ }
+ let sig = clif_sig_from_fn_sig(tcx, fn_sig);
(tcx.symbol_name(inst).as_str().to_string(), sig)
}
-impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
- /// Instance must be monomorphized
- pub fn get_function_id(&mut self, inst: Instance<'tcx>) -> FuncId {
- let (name, sig) = get_function_name_and_sig(self.tcx, inst);
- self.module
- .declare_function(&name, Linkage::Import, &sig)
- .unwrap()
- }
+/// 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 {
+ let (name, sig) = get_function_name_and_sig(tcx, inst);
+ module
+ .declare_function(&name, Linkage::Import, &sig)
+ .unwrap()
+}
+impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
/// Instance must be monomorphized
pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
- let func_id = self.get_function_id(inst);
+ let func_id = import_function(self.tcx, self.module, inst);
let func_ref = self.module
.declare_func_in_func(func_id, &mut self.bcx.func);
+
+ #[cfg(debug_assertions)]
self.add_entity_comment(func_ref, format!("{:?}", inst));
+
func_ref
}
.map(|arg| {
(
self.clif_type(arg.layout().ty).unwrap(),
- arg.load_value(self),
+ arg.load_scalar(self),
)
})
.unzip();
}
}
+#[cfg(debug_assertions)]
fn add_arg_comment<'a, 'tcx: 'a>(
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
msg: &str,
));
}
+#[cfg(debug_assertions)]
fn add_local_header_comment(fx: &mut FunctionCx<impl Backend>) {
fx.add_global_comment(format!("msg loc.idx param pass mode ssa flags ty"));
}
fx.bcx.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
CPlace::Var(local, layout)
} else {
- let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
- kind: StackSlotKind::ExplicitSlot,
- size: layout.size.bytes() as u32,
- offset: None,
- });
+ let place = CPlace::new_stack_slot(fx, layout.ty);
- let TyLayout { ty, details } = layout;
- let ty::layout::LayoutDetails { size, align, abi: _, variants: _, fields: _ } = details;
- fx.add_entity_comment(stack_slot, format!(
- "{:?}: {:?} size={} align={},{}",
- local, ty, size.bytes(), align.abi.bytes(), align.pref.bytes(),
- ));
+ #[cfg(debug_assertions)]
+ {
+ let TyLayout { ty, details } = layout;
+ let ty::layout::LayoutDetails { size, align, abi: _, variants: _, fields: _ } = details;
+ 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(),
+ )),
+ _ => unreachable!(),
+ }
+ }
- CPlace::from_stack_slot(fx, stack_slot, layout.ty)
+ // Take stack_addr in advance to avoid many duplicate instructions
+ CPlace::Addr(place.to_addr(fx), None, layout)
};
let prev_place = fx.local_map.insert(local, place);
ssa_flags: crate::analyze::Flags,
) -> CValue<'tcx> {
let layout = fx.layout_of(arg_ty);
- let pass_mode = get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false);
+ let pass_mode = get_pass_mode(fx.tcx, arg_ty, false);
let clif_type = pass_mode.get_param_ty(fx);
let ebb_param = fx.bcx.append_ebb_param(start_ebb, clif_type);
+
+ #[cfg(debug_assertions)]
add_arg_comment(fx, "arg", local, local_field, Some(ebb_param), pass_mode, ssa_flags, arg_ty);
+
match pass_mode {
PassMode::NoPass => unimplemented!("pass mode nopass"),
PassMode::ByVal(_) => CValue::ByVal(ebb_param, layout),
start_ebb: Ebb,
) {
let ssa_analyzed = crate::analyze::analyze(fx);
+
+ #[cfg(debug_assertions)]
fx.add_global_comment(format!("ssa {:?}", ssa_analyzed));
let ret_layout = fx.layout_of(fx.return_type());
- let output_pass_mode = get_pass_mode(fx.tcx, fx.self_sig().abi, fx.return_type(), true);
+ let output_pass_mode = get_pass_mode(fx.tcx, fx.return_type(), true);
let ret_param = match output_pass_mode {
PassMode::NoPass => None,
PassMode::ByVal(_) => None,
PassMode::ByRef => Some(fx.bcx.append_ebb_param(start_ebb, fx.pointer_type)),
};
- 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);
+ #[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);
+ }
enum ArgKind<'tcx> {
Normal(CValue<'tcx>),
match output_pass_mode {
PassMode::NoPass => {
- let null = fx.bcx.ins().iconst(fx.pointer_type, 0);
- fx.local_map.insert(
- RETURN_PLACE,
- CPlace::Addr(null, None, fx.layout_of(fx.return_type())),
- );
+ fx.local_map.insert(RETURN_PLACE, CPlace::NoPlace(ret_layout));
}
- PassMode::ByVal(ret_ty) => {
- fx.bcx.declare_var(mir_var(RETURN_PLACE), ret_ty);
- fx.local_map
- .insert(RETURN_PLACE, CPlace::Var(RETURN_PLACE, ret_layout));
+ 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);
}
PassMode::ByRef => {
fx.local_map.insert(
args: Vec<CValue<'tcx>>,
ret_place: Option<CPlace<'tcx>>,
) {
- let sig = ty_fn_sig(fx.tcx, fn_ty);
+ let fn_sig = ty_fn_sig(fx.tcx, fn_ty);
- let ret_layout = fx.layout_of(sig.output());
+ let ret_layout = fx.layout_of(fn_sig.output());
- let output_pass_mode = get_pass_mode(fx.tcx, sig.abi, sig.output(), true);
+ let output_pass_mode = get_pass_mode(fx.tcx, fn_sig.output(), true);
let return_ptr = match output_pass_mode {
PassMode::NoPass => None,
PassMode::ByRef => match ret_place {
- Some(ret_place) => Some(ret_place.expect_addr()),
+ Some(ret_place) => Some(ret_place.to_addr(fx)),
None => Some(fx.bcx.ins().iconst(fx.pointer_type, 0)),
},
PassMode::ByVal(_) => None,
} else {
func_ref = if instance.is_none() {
let func = trans_operand(fx, func.expect("indirect call without func Operand"));
- Some(func.load_value(fx))
+ Some(func.load_scalar(fx))
} else {
None
};
- args.get(0).map(|arg| adjust_arg_for_abi(fx, sig, *arg))
+ args.get(0).map(|arg| adjust_arg_for_abi(fx, *arg))
}
.into_iter()
};
.chain(
args.into_iter()
.skip(1)
- .map(|arg| adjust_arg_for_abi(fx, sig, arg)),
+ .map(|arg| adjust_arg_for_abi(fx, arg)),
)
.collect::<Vec<_>>();
- let sig = fx.bcx.import_signature(clif_sig_from_fn_ty(fx.tcx, fn_ty));
let call_inst = if let Some(func_ref) = func_ref {
+ let sig = fx.bcx.import_signature(clif_sig_from_fn_sig(fx.tcx, fn_sig));
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"));
}
pub fn codegen_return(fx: &mut FunctionCx<impl Backend>) {
- match get_pass_mode(fx.tcx, fx.self_sig().abi, fx.return_type(), true) {
+ match get_pass_mode(fx.tcx, fx.return_type(), true) {
PassMode::NoPass | PassMode::ByRef => {
fx.bcx.ins().return_(&[]);
}
PassMode::ByVal(_) => {
let place = fx.get_local_place(RETURN_PLACE);
- let ret_val = place.to_cvalue(fx).load_value(fx);
+ let ret_val = place.to_cvalue(fx).load_scalar(fx);
fx.bcx.ins().return_(&[ret_val]);
}
}