diff --git a/build.sh b/build.sh index d282c788c36..59922bb7399 100755 --- a/build.sh +++ b/build.sh @@ -14,7 +14,7 @@ fi RUSTC="rustc -Zcodegen-backend=$(pwd)/target/debug/librustc_codegen_cranelift.$dylib_ext -L crate=." -$RUSTC examples/mini_core.rs --crate-name mini_core --crate-type lib && +SHOULD_CODEGEN=1 $RUSTC examples/mini_core.rs --crate-name mini_core --crate-type lib && $RUSTC examples/example.rs --crate-type lib && $RUSTC examples/mini_core_hello_world.rs --crate-type bin && diff --git a/src/abi.rs b/src/abi.rs index 9b9c59162ff..da28dc04c4b 100644 --- a/src/abi.rs +++ b/src/abi.rs @@ -278,33 +278,42 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, start_ebb Spread(Vec), } - let func_params = fx.mir.args_iter().map(|local| { - let arg_ty = fx.monomorphize(&fx.mir.local_decls[local].ty); + 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. + // 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 { - ty::TyTuple(ref tys) => tys, - _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty), - }; + let tupled_arg_tys = match arg_ty.sty { + ty::TyTuple(ref tys) => tys, + _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty), + }; - let mut ebb_params = Vec::new(); - for arg_ty in tupled_arg_tys.iter() { - let cton_type = get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false).get_param_ty(fx); - ebb_params.push(fx.bcx.append_ebb_param(start_ebb, cton_type)); + let mut ebb_params = Vec::new(); + for arg_ty in tupled_arg_tys.iter() { + let cton_type = + get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false).get_param_ty(fx); + ebb_params.push(fx.bcx.append_ebb_param(start_ebb, cton_type)); + } + + (local, ArgKind::Spread(ebb_params), arg_ty) + } else { + let cton_type = + get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false).get_param_ty(fx); + ( + local, + ArgKind::Normal(fx.bcx.append_ebb_param(start_ebb, cton_type)), + arg_ty, + ) } - - (local, ArgKind::Spread(ebb_params), arg_ty) - } else { - let cton_type = get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false).get_param_ty(fx); - (local, ArgKind::Normal(fx.bcx.append_ebb_param(start_ebb, cton_type)), arg_ty) - } - }).collect::>(); + }).collect::>(); match output_pass_mode { PassMode::NoPass => { diff --git a/src/base.rs b/src/base.rs index 508cbcf1b7f..40c858508d3 100644 --- a/src/base.rs +++ b/src/base.rs @@ -50,13 +50,7 @@ pub fn trans_mono_item<'a, 'tcx: 'a>( context.func = func; // TODO: cranelift doesn't yet support some of the things needed - if cx - .tcx - .sess - .crate_types - .get() - .contains(&CrateType::Executable) - { + if should_codegen(cx.tcx) { cx.module.define_function(func_id, context).unwrap(); cx.defined_functions.push(func_id); } @@ -171,7 +165,7 @@ pub fn trans_fn<'a, 'tcx: 'a>( } => { fx.bcx.ins().trap(TrapCode::User(0)); // TODO: prevent panics on large and negative disciminants - if false { + if should_codegen(fx.tcx) { let discr = trans_operand(fx, discr).load_value(fx); let mut jt_data = JumpTableData::new(); for (i, value) in values.iter().enumerate() { @@ -382,13 +376,23 @@ fn trans_stmt<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, cur_ebb: Ebb, stmt: & | (TypeVariants::TyUint(_), TypeVariants::TyInt(_)) | (TypeVariants::TyUint(_), TypeVariants::TyUint(_)) => { let from = operand.load_value(fx); - let res = crate::common::cton_intcast(fx, from, fx.cton_type(to_ty).unwrap(), false); + let res = crate::common::cton_intcast( + fx, + from, + fx.cton_type(to_ty).unwrap(), + false, + ); lval.write_cvalue(fx, CValue::ByVal(res, dest_layout)); } (TypeVariants::TyInt(_), TypeVariants::TyInt(_)) | (TypeVariants::TyInt(_), TypeVariants::TyUint(_)) => { let from = operand.load_value(fx); - let res = crate::common::cton_intcast(fx, from, fx.cton_type(to_ty).unwrap(), true); + let res = crate::common::cton_intcast( + fx, + from, + fx.cton_type(to_ty).unwrap(), + true, + ); lval.write_cvalue(fx, CValue::ByVal(res, dest_layout)); } (TypeVariants::TyFloat(from_flt), TypeVariants::TyFloat(to_flt)) => { @@ -552,7 +556,8 @@ pub fn trans_get_discriminant<'a, 'tcx: 'a>( lldiscr, *niche_variants.end() as u64 as i64, ); - let if_true = cton_intcast(fx, lldiscr, fx.cton_type(dest_layout.ty).unwrap(), false); + let if_true = + cton_intcast(fx, lldiscr, fx.cton_type(dest_layout.ty).unwrap(), false); let if_false = fx .bcx .ins() diff --git a/src/lib.rs b/src/lib.rs index 1f202dc9c4b..b89586c9415 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,6 +89,11 @@ mod prelude { pub use crate::common::*; pub use crate::CodegenCx; + + pub fn should_codegen(tcx: TyCtxt) -> bool { + ::std::env::var("SHOULD_CODEGEN").is_ok() + || tcx.sess.crate_types.get().contains(&CrateType::Executable) + } } use crate::prelude::*; @@ -317,6 +322,12 @@ impl CodegenBackend for CraneliftCodegenBackend { tcx.sess.warn(&format!("main returned {}", res)); module.finish(); + } else if should_codegen(tcx) { + for func_id in defined_functions { + module.finalize_function(func_id); + } + + tcx.sess.warn("Finalized everything"); } let mut translated_module: Module = Module::new(