Skip to content

Commit

Permalink
Merge pull request rust-lang#1079 from CohenArthur/fmt-master
Browse files Browse the repository at this point in the history
Run cargo fmt on the whole project
  • Loading branch information
bjorn3 authored Aug 28, 2020
2 parents 5dec38e + fc9f16b commit 452656b
Show file tree
Hide file tree
Showing 40 changed files with 1,566 additions and 789 deletions.
32 changes: 21 additions & 11 deletions src/abi/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ pub(super) fn add_local_place_comments<'tcx>(
}
CPlaceInner::VarPair(place_local, var1, var2) => {
assert_eq!(local, place_local);
("ssa", Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())))
(
"ssa",
Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())),
)
}
CPlaceInner::VarLane(_local, _var, _lane) => unreachable!(),
CPlaceInner::Addr(ptr, meta) => {
Expand All @@ -90,15 +93,18 @@ pub(super) fn add_local_place_comments<'tcx>(
Cow::Borrowed("")
};
match ptr.base_and_offset() {
(crate::pointer::PointerBase::Addr(addr), offset) => {
("reuse", format!("storage={}{}{}", addr, offset, meta).into())
}
(crate::pointer::PointerBase::Stack(stack_slot), offset) => {
("stack", format!("storage={}{}{}", stack_slot, offset, meta).into())
}
(crate::pointer::PointerBase::Dangling(align), offset) => {
("zst", format!("align={},offset={}", align.bytes(), offset).into())
}
(crate::pointer::PointerBase::Addr(addr), offset) => (
"reuse",
format!("storage={}{}{}", addr, offset, meta).into(),
),
(crate::pointer::PointerBase::Stack(stack_slot), offset) => (
"stack",
format!("storage={}{}{}", stack_slot, offset, meta).into(),
),
(crate::pointer::PointerBase::Dangling(align), offset) => (
"zst",
format!("align={},offset={}", align.bytes(), offset).into(),
),
}
}
};
Expand All @@ -111,7 +117,11 @@ pub(super) fn add_local_place_comments<'tcx>(
size.bytes(),
align.abi.bytes(),
align.pref.bytes(),
if extra.is_empty() { "" } else { " " },
if extra.is_empty() {
""
} else {
" "
},
extra,
));
}
112 changes: 75 additions & 37 deletions src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ mod comments;
mod pass_mode;
mod returning;

use rustc_target::spec::abi::Abi;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_target::spec::abi::Abi;

use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};

Expand All @@ -14,6 +14,7 @@ use crate::prelude::*;
pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return};

// Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662/src/librustc_middle/ty/layout.rs#L2301
#[rustfmt::skip]
pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::PolyFnSig<'tcx> {
use rustc_middle::ty::subst::Subst;

Expand Down Expand Up @@ -102,8 +103,16 @@ fn clif_sig_from_fn_sig<'tcx>(
abi => abi,
};
let (call_conv, inputs, output): (CallConv, Vec<Ty<'tcx>>, Ty<'tcx>) = match abi {
Abi::Rust => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
Abi::C | Abi::Unadjusted => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
Abi::Rust => (
CallConv::triple_default(triple),
sig.inputs().to_vec(),
sig.output(),
),
Abi::C | Abi::Unadjusted => (
CallConv::triple_default(triple),
sig.inputs().to_vec(),
sig.output(),
),
Abi::SysV64 => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
Abi::RustCall => {
assert_eq!(sig.inputs().len(), 2);
Expand All @@ -116,7 +125,11 @@ fn clif_sig_from_fn_sig<'tcx>(
(CallConv::triple_default(triple), inputs, sig.output())
}
Abi::System => unreachable!(),
Abi::RustIntrinsic => (CallConv::triple_default(triple), sig.inputs().to_vec(), sig.output()),
Abi::RustIntrinsic => (
CallConv::triple_default(triple),
sig.inputs().to_vec(),
sig.output(),
),
_ => unimplemented!("unsupported abi {:?}", sig.abi),
};

Expand Down Expand Up @@ -163,10 +176,7 @@ fn clif_sig_from_fn_sig<'tcx>(
tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(),
) {
PassMode::NoPass => (inputs.collect(), vec![]),
PassMode::ByVal(ret_ty) => (
inputs.collect(),
vec![AbiParam::new(ret_ty)],
),
PassMode::ByVal(ret_ty) => (inputs.collect(), vec![AbiParam::new(ret_ty)]),
PassMode::ByValPair(ret_ty_a, ret_ty_b) => (
inputs.collect(),
vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)],
Expand Down Expand Up @@ -202,12 +212,24 @@ pub(crate) fn get_function_name_and_sig<'tcx>(
support_vararg: bool,
) -> (String, Signature) {
assert!(!inst.substs.needs_infer());
let fn_sig =
tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_sig_for_fn_abi(tcx, inst));
let fn_sig = tcx.normalize_erasing_late_bound_regions(
ParamEnv::reveal_all(),
&fn_sig_for_fn_abi(tcx, inst),
);
if fn_sig.c_variadic && !support_vararg {
tcx.sess.span_fatal(tcx.def_span(inst.def_id()), "Variadic function definitions are not yet supported");
tcx.sess.span_fatal(
tcx.def_span(inst.def_id()),
"Variadic function definitions are not yet supported",
);
}
let sig = clif_sig_from_fn_sig(tcx, triple, fn_sig, tcx.def_span(inst.def_id()), false, inst.def.requires_caller_location(tcx));
let sig = clif_sig_from_fn_sig(
tcx,
triple,
fn_sig,
tcx.def_span(inst.def_id()),
false,
inst.def.requires_caller_location(tcx),
);
(tcx.symbol_name(inst).name.to_string(), sig)
}

Expand All @@ -228,7 +250,8 @@ impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
let func_id = import_function(self.tcx, &mut self.cx.module, inst);
let func_ref = self
.cx.module
.cx
.module
.declare_func_in_func(func_id, &mut self.bcx.func);

#[cfg(debug_assertions)]
Expand All @@ -250,11 +273,13 @@ impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
call_conv: CallConv::triple_default(self.triple()),
};
let func_id = self
.cx.module
.cx
.module
.declare_function(&name, Linkage::Import, &sig)
.unwrap();
let func_ref = self
.cx.module
.cx
.module
.declare_func_in_func(func_id, &mut self.bcx.func);
let call_inst = self.bcx.ins().call(func_ref, args);
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -376,7 +401,9 @@ pub(crate) fn codegen_fn_prelude<'tcx>(
assert!(fx.caller_location.is_none());
if fx.instance.def.requires_caller_location(fx.tcx) {
// Store caller location for `#[track_caller]`.
fx.caller_location = Some(cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap());
fx.caller_location = Some(
cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap(),
);
}

fx.bcx.switch_to_block(start_block);
Expand Down Expand Up @@ -502,17 +529,20 @@ pub(crate) fn codegen_terminator_call<'tcx>(
fx.bcx.ins().jump(ret_block, &[]);
return;
}
_ => Some(instance)
_ => Some(instance),
}
} else {
None
};

let is_cold =
instance.map(|inst|
fx.tcx.codegen_fn_attrs(inst.def_id())
.flags.contains(CodegenFnAttrFlags::COLD))
.unwrap_or(false);
let is_cold = instance
.map(|inst| {
fx.tcx
.codegen_fn_attrs(inst.def_id())
.flags
.contains(CodegenFnAttrFlags::COLD)
})
.unwrap_or(false);
if is_cold {
fx.cold_blocks.insert(current_block);
}
Expand All @@ -524,9 +554,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
let pack_arg = trans_operand(fx, &args[1]);

let tupled_arguments = match pack_arg.layout().ty.kind {
ty::Tuple(ref tupled_arguments) => {
tupled_arguments
}
ty::Tuple(ref tupled_arguments) => tupled_arguments,
_ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
};

Expand Down Expand Up @@ -582,8 +610,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
let nop_inst = fx.bcx.ins().nop();
fx.add_comment(nop_inst, "indirect call");
}
let func = trans_operand(fx, func)
.load_scalar(fx);
let func = trans_operand(fx, func).load_scalar(fx);
(
Some(func),
args.get(0)
Expand All @@ -608,7 +635,10 @@ pub(crate) fn codegen_terminator_call<'tcx>(
)
.collect::<Vec<_>>();

if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) {
if instance
.map(|inst| inst.def.requires_caller_location(fx.tcx))
.unwrap_or(false)
{
// Pass the caller location for `#[track_caller]`.
let caller_location = fx.get_caller_location(span);
call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
Expand Down Expand Up @@ -637,7 +667,10 @@ pub(crate) fn codegen_terminator_call<'tcx>(
// FIXME find a cleaner way to support varargs
if fn_sig.c_variadic {
if fn_sig.abi != Abi::C {
fx.tcx.sess.span_fatal(span, &format!("Variadic call for non-C abi {:?}", fn_sig.abi));
fx.tcx.sess.span_fatal(
span,
&format!("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
Expand All @@ -646,7 +679,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
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
fx.tcx.sess.span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
fx.tcx
.sess
.span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
}
AbiParam::new(ty)
})
Expand Down Expand Up @@ -700,13 +735,16 @@ pub(crate) fn codegen_drop<'tcx>(
_ => {
assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));

let arg_value = drop_place.place_ref(fx, fx.layout_of(fx.tcx.mk_ref(
&ty::RegionKind::ReErased,
TypeAndMut {
ty,
mutbl: crate::rustc_hir::Mutability::Mut,
},
)));
let arg_value = drop_place.place_ref(
fx,
fx.layout_of(fx.tcx.mk_ref(
&ty::RegionKind::ReErased,
TypeAndMut {
ty,
mutbl: crate::rustc_hir::Mutability::Mut,
},
)),
);
let arg_value = adjust_arg_for_abi(fx, arg_value);

let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
Expand Down
37 changes: 20 additions & 17 deletions src/abi/pass_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ pub(super) fn get_pass_mode<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>)
} else {
match &layout.abi {
Abi::Uninhabited => PassMode::NoPass,
Abi::Scalar(scalar) => {
PassMode::ByVal(scalar_to_clif_type(tcx, scalar.clone()))
}
Abi::Scalar(scalar) => PassMode::ByVal(scalar_to_clif_type(tcx, scalar.clone())),
Abi::ScalarPair(a, b) => {
let a = scalar_to_clif_type(tcx, a.clone());
let b = scalar_to_clif_type(tcx, b.clone());
if a == types::I128 && b == types::I128 {
// Returning (i128, i128) by-val-pair would take 4 regs, while only 3 are
// available on x86_64. Cranelift gets confused when too many return params
// are used.
PassMode::ByRef { size: Some(layout.size) }
PassMode::ByRef {
size: Some(layout.size),
}
} else {
PassMode::ByValPair(a, b)
}
Expand All @@ -104,11 +104,15 @@ pub(super) fn get_pass_mode<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>)
if let Some(vector_ty) = crate::intrinsics::clif_vector_type(tcx, layout) {
PassMode::ByVal(vector_ty)
} else {
PassMode::ByRef { size: Some(layout.size) }
PassMode::ByRef {
size: Some(layout.size),
}
}
}

Abi::Aggregate { sized: true } => PassMode::ByRef { size: Some(layout.size) },
Abi::Aggregate { sized: true } => PassMode::ByRef {
size: Some(layout.size),
},
Abi::Aggregate { sized: false } => PassMode::ByRef { size: None },
}
}
Expand All @@ -125,22 +129,18 @@ pub(super) fn adjust_arg_for_abi<'tcx>(
let (a, b) = arg.load_scalar_pair(fx);
Pair(a, b)
}
PassMode::ByRef { size: _ } => {
match arg.force_stack(fx) {
(ptr, None) => Single(ptr.get_addr(fx)),
(ptr, Some(meta)) => Pair(ptr.get_addr(fx), meta),
}
}
PassMode::ByRef { size: _ } => match arg.force_stack(fx) {
(ptr, None) => Single(ptr.get_addr(fx)),
(ptr, Some(meta)) => Pair(ptr.get_addr(fx), meta),
},
}
}

pub(super) fn cvalue_for_param<'tcx>(
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
start_block: Block,
#[cfg_attr(not(debug_assertions), allow(unused_variables))]
local: Option<mir::Local>,
#[cfg_attr(not(debug_assertions), allow(unused_variables))]
local_field: Option<usize>,
#[cfg_attr(not(debug_assertions), allow(unused_variables))] local: Option<mir::Local>,
#[cfg_attr(not(debug_assertions), allow(unused_variables))] local_field: Option<usize>,
arg_ty: Ty<'tcx>,
) -> Option<CValue<'tcx>> {
let layout = fx.layout_of(arg_ty);
Expand Down Expand Up @@ -171,7 +171,10 @@ pub(super) fn cvalue_for_param<'tcx>(
let (a, b) = block_params.assert_pair();
Some(CValue::by_val_pair(a, b, layout))
}
PassMode::ByRef { size: Some(_) } => Some(CValue::by_ref(Pointer::new(block_params.assert_single()), layout)),
PassMode::ByRef { size: Some(_) } => Some(CValue::by_ref(
Pointer::new(block_params.assert_single()),
layout,
)),
PassMode::ByRef { size: None } => {
let (ptr, meta) = block_params.assert_pair();
Some(CValue::by_ref_unsized(Pointer::new(ptr), meta, layout))
Expand Down
Loading

0 comments on commit 452656b

Please sign in to comment.