Codegen minicore

This commit is contained in:
bjorn3
2018-08-14 12:13:07 +02:00
parent 4b10e6e613
commit 0978710ffd
4 changed files with 60 additions and 35 deletions

View File

@@ -14,7 +14,7 @@ fi
RUSTC="rustc -Zcodegen-backend=$(pwd)/target/debug/librustc_codegen_cranelift.$dylib_ext -L crate=." 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/example.rs --crate-type lib &&
$RUSTC examples/mini_core_hello_world.rs --crate-type bin && $RUSTC examples/mini_core_hello_world.rs --crate-type bin &&

View File

@@ -278,33 +278,42 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, start_ebb
Spread(Vec<Value>), Spread(Vec<Value>),
} }
let func_params = fx.mir.args_iter().map(|local| { let func_params = fx
let arg_ty = fx.monomorphize(&fx.mir.local_decls[local].ty); .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 // 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 { if Some(local) == fx.mir.spread_arg {
// This argument (e.g. the last argument in the "rust-call" ABI) // 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 // is a tuple that was spread at the ABI level and now we have
// to reconstruct it into a tuple local variable, from multiple // to reconstruct it into a tuple local variable, from multiple
// individual function arguments. // individual function arguments.
let tupled_arg_tys = match arg_ty.sty { let tupled_arg_tys = match arg_ty.sty {
ty::TyTuple(ref tys) => tys, ty::TyTuple(ref tys) => tys,
_ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty), _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty),
}; };
let mut ebb_params = Vec::new(); let mut ebb_params = Vec::new();
for arg_ty in tupled_arg_tys.iter() { 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); let cton_type =
ebb_params.push(fx.bcx.append_ebb_param(start_ebb, 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,
)
} }
}).collect::<Vec<(Local, ArgKind, 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::<Vec<(Local, ArgKind, Ty)>>();
match output_pass_mode { match output_pass_mode {
PassMode::NoPass => { PassMode::NoPass => {

View File

@@ -50,13 +50,7 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
context.func = func; context.func = func;
// TODO: cranelift doesn't yet support some of the things needed // TODO: cranelift doesn't yet support some of the things needed
if cx if should_codegen(cx.tcx) {
.tcx
.sess
.crate_types
.get()
.contains(&CrateType::Executable)
{
cx.module.define_function(func_id, context).unwrap(); cx.module.define_function(func_id, context).unwrap();
cx.defined_functions.push(func_id); cx.defined_functions.push(func_id);
} }
@@ -171,7 +165,7 @@ pub fn trans_fn<'a, 'tcx: 'a>(
} => { } => {
fx.bcx.ins().trap(TrapCode::User(0)); fx.bcx.ins().trap(TrapCode::User(0));
// TODO: prevent panics on large and negative disciminants // 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 discr = trans_operand(fx, discr).load_value(fx);
let mut jt_data = JumpTableData::new(); let mut jt_data = JumpTableData::new();
for (i, value) in values.iter().enumerate() { 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::TyInt(_))
| (TypeVariants::TyUint(_), TypeVariants::TyUint(_)) => { | (TypeVariants::TyUint(_), TypeVariants::TyUint(_)) => {
let from = operand.load_value(fx); 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)); lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
} }
(TypeVariants::TyInt(_), TypeVariants::TyInt(_)) (TypeVariants::TyInt(_), TypeVariants::TyInt(_))
| (TypeVariants::TyInt(_), TypeVariants::TyUint(_)) => { | (TypeVariants::TyInt(_), TypeVariants::TyUint(_)) => {
let from = operand.load_value(fx); 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)); lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
} }
(TypeVariants::TyFloat(from_flt), TypeVariants::TyFloat(to_flt)) => { (TypeVariants::TyFloat(from_flt), TypeVariants::TyFloat(to_flt)) => {
@@ -552,7 +556,8 @@ pub fn trans_get_discriminant<'a, 'tcx: 'a>(
lldiscr, lldiscr,
*niche_variants.end() as u64 as i64, *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 let if_false = fx
.bcx .bcx
.ins() .ins()

View File

@@ -89,6 +89,11 @@ mod prelude {
pub use crate::common::*; pub use crate::common::*;
pub use crate::CodegenCx; 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::*; use crate::prelude::*;
@@ -317,6 +322,12 @@ impl CodegenBackend for CraneliftCodegenBackend {
tcx.sess.warn(&format!("main returned {}", res)); tcx.sess.warn(&format!("main returned {}", res));
module.finish(); 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<FaerieBackend> = Module::new( let mut translated_module: Module<FaerieBackend> = Module::new(