1
Fork 0

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 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 &&

View file

@ -278,7 +278,10 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, start_ebb
Spread(Vec<Value>),
}
let func_params = fx.mir.args_iter().map(|local| {
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
@ -295,14 +298,20 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, start_ebb
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);
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)
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)>>();

View file

@ -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()

View file

@ -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<FaerieBackend> = Module::new(