diff --git a/Cargo.toml b/Cargo.toml index dc4f2d93faf..651fba4133a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,10 @@ +cargo-features = ["edition"] + [package] name = "rustc_codegen_cranelift" version = "0.1.0" authors = ["bjorn3 "] +edition = "2018" [lib] crate-type = ["dylib"] diff --git a/src/abi.rs b/src/abi.rs index 70af157e47c..144ba167c51 100644 --- a/src/abi.rs +++ b/src/abi.rs @@ -3,7 +3,7 @@ use std::iter; use rustc::hir; use rustc_target::spec::abi::Abi; -use prelude::*; +use crate::prelude::*; pub fn cton_sig_from_fn_ty<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fn_ty: Ty<'tcx>) -> Signature { let sig = ty_fn_sig(tcx, fn_ty); @@ -250,12 +250,12 @@ pub fn codegen_call<'a, 'tcx: 'a>( args: &[Operand<'tcx>], destination: &Option<(Place<'tcx>, BasicBlock)>, ) { - let func = ::base::trans_operand(fx, func); + let func = trans_operand(fx, func); let fn_ty = func.layout().ty; let sig = ty_fn_sig(fx.tcx, fn_ty); let return_place = if let Some((place, _)) = destination { - Some(::base::trans_place(fx, place)) + Some(trans_place(fx, place)) } else { None }; @@ -263,8 +263,8 @@ pub fn codegen_call<'a, 'tcx: 'a>( // Unpack arguments tuple for closures let args = if sig.abi == Abi::RustCall { assert_eq!(args.len(), 2, "rust-call abi requires two arguments"); - let self_arg = ::base::trans_operand(fx, &args[0]); - let pack_arg = ::base::trans_operand(fx, &args[1]); + let self_arg = trans_operand(fx, &args[0]); + let pack_arg = trans_operand(fx, &args[1]); let mut args = Vec::new(); args.push(self_arg); match pack_arg.layout().ty.sty { @@ -281,7 +281,7 @@ pub fn codegen_call<'a, 'tcx: 'a>( args .into_iter() .map(|arg| { - ::base::trans_operand(fx, arg) + trans_operand(fx, arg) }) .collect::>() }; @@ -319,7 +319,7 @@ pub fn codegen_call<'a, 'tcx: 'a>( } "discriminant_value" => { assert_eq!(args.len(), 1); - let discr = ::base::trans_get_discriminant(fx, args[0], ret.layout()); + let discr = crate::base::trans_get_discriminant(fx, args[0], ret.layout()); ret.write_cvalue(fx, discr); } "size_of" => { @@ -351,10 +351,10 @@ pub fn codegen_call<'a, 'tcx: 'a>( }; let res = match ret.layout().ty.sty { TypeVariants::TyUint(_) => { - ::base::trans_int_binop(fx, bin_op, args[0], args[1], ret.layout().ty, false, false) + crate::base::trans_int_binop(fx, bin_op, args[0], args[1], ret.layout().ty, false, false) } TypeVariants::TyInt(_) => { - ::base::trans_int_binop(fx, bin_op, args[0], args[1], ret.layout().ty, true, false) + crate::base::trans_int_binop(fx, bin_op, args[0], args[1], ret.layout().ty, true, false) } _ => panic!(), }; diff --git a/src/base.rs b/src/base.rs index 104b948f148..dbb562e4e5a 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1,4 +1,4 @@ -use prelude::*; +use crate::prelude::*; pub fn trans_mono_item<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>, context: &mut Context, mono_item: MonoItem<'tcx>) { let tcx = cx.tcx; @@ -33,7 +33,7 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend let mut f = Function::with_name_signature(ExternalName::user(0, func_id.index() as u32), sig); - let comments = match ::base::trans_fn(cx, &mut f, inst){ + let comments = match trans_fn(cx, &mut f, inst){ Ok(comments) => comments, Err(err) => { tcx.sess.err(&err); @@ -41,7 +41,7 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend } }; - let mut writer = ::pretty_clif::CommentWriter(comments); + let mut writer = crate::pretty_clif::CommentWriter(comments); let mut cton = String::new(); ::cranelift::codegen::write::decorate_function(&mut writer, &mut cton, &f, None).unwrap(); tcx.sess.warn(&cton); @@ -99,7 +99,7 @@ pub fn trans_fn<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>, f: & }; let fx = &mut fx; - ::abi::codegen_fn_prelude(fx, start_ebb); + crate::abi::codegen_fn_prelude(fx, start_ebb); fx.bcx.ins().jump(*fx.ebb_map.get(&START_BLOCK).unwrap(), &[]); @@ -143,13 +143,13 @@ pub fn trans_fn<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>, f: & let ebb = fx.get_ebb(targets[i]); jt_data.set_entry(*value as usize, ebb); } - let mut jump_table = fx.bcx.create_jump_table(jt_data); + let jump_table = fx.bcx.create_jump_table(jt_data); fx.bcx.ins().br_table(discr, jump_table); let otherwise_ebb = fx.get_ebb(targets[targets.len() - 1]); fx.bcx.ins().jump(otherwise_ebb, &[]); } TerminatorKind::Call { func, args, destination, cleanup: _ } => { - ::abi::codegen_call(fx, func, args, destination); + crate::abi::codegen_call(fx, func, args, destination); } TerminatorKind::Resume | TerminatorKind::Abort | TerminatorKind::Unreachable => { fx.bcx.ins().trap(TrapCode::User(!0)); @@ -319,13 +319,13 @@ 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 = ::common::cton_intcast(fx, from, from_ty, to_ty, false); + let res = crate::common::cton_intcast(fx, from, from_ty, to_ty, 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 = ::common::cton_intcast(fx, from, from_ty, to_ty, true); + let res = crate::common::cton_intcast(fx, from, from_ty, to_ty, true); lval.write_cvalue(fx, CValue::ByVal(res, dest_layout)); } _ => return Err(format!("rval misc {:?} {:?}", operand, to_ty)), @@ -602,6 +602,7 @@ fn trans_ptr_binop<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, bin_op: BinOp, l pub fn trans_place<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, place: &Place<'tcx>) -> CPlace<'tcx> { match place { Place::Local(local) => fx.get_local_place(*local), + Place::Promoted(promoted) => crate::constant::trans_promoted(fx, promoted.0), Place::Static(static_) => unimplemented!("static place {:?} ty {:?}", static_.def_id, static_.ty), Place::Projection(projection) => { let base = trans_place(fx, &projection.base); @@ -632,7 +633,7 @@ pub fn trans_operand<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx>, operand: &Operand< cplace.to_cvalue(fx) }, Operand::Constant(const_) => { - ::constant::trans_constant(fx, const_) + crate::constant::trans_constant(fx, const_) } } } diff --git a/src/common.rs b/src/common.rs index ba84b2d6461..2ef9d1d6772 100644 --- a/src/common.rs +++ b/src/common.rs @@ -4,7 +4,7 @@ use rustc_target::spec::{HasTargetSpec, Target}; use cranelift_module::{Module, FuncId, DataId}; -use prelude::*; +use crate::prelude::*; pub type CurrentBackend = ::cranelift_simplejit::SimpleJITBackend; @@ -313,7 +313,7 @@ impl<'a, 'tcx: 'a> fmt::Debug for FunctionCx<'a, 'tcx> { writeln!(f, "{:?}", self.local_map)?; let mut clif = String::new(); - let mut writer = ::pretty_clif::CommentWriter(self.comments.clone()); + let mut writer = crate::pretty_clif::CommentWriter(self.comments.clone()); ::cranelift::codegen::write::decorate_function( &mut writer, &mut clif, diff --git a/src/constant.rs b/src/constant.rs index 60fe3419271..5520a77bad3 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -1,21 +1,30 @@ -use prelude::*; +use crate::prelude::*; +use rustc::ty::Const; use rustc::mir::interpret::{ConstValue, GlobalId, AllocId, read_target_uint}; use rustc_mir::interpret::{CompileTimeEvaluator, Memory, MemoryKind}; use cranelift_module::*; -pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Constant<'tcx>) -> CValue<'tcx> { - let const_val = match const_.literal { - Literal::Value { value } => fx.monomorphize(&value), - Literal::Promoted { index } => fx - .tcx - .const_eval(ParamEnv::reveal_all().and(GlobalId { - instance: fx.instance, - promoted: Some(index), - })) - .unwrap(), - }; +pub fn trans_promoted<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, promoted: Promoted) -> CPlace<'tcx> { + let const_ = fx + .tcx + .const_eval(ParamEnv::reveal_all().and(GlobalId { + instance: fx.instance, + promoted: Some(promoted), + })) + .unwrap(); - let const_ = match const_val.val { + let const_ = force_eval_const(fx, const_); + trans_const_place(fx, const_) +} + +pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, constant: &Constant<'tcx>) -> CValue<'tcx> { + let const_ = fx.monomorphize(&constant.literal); + let const_ = force_eval_const(fx, const_); + trans_const_value(fx, const_) +} + +fn force_eval_const<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx>, const_: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> { + match const_.val { ConstValue::Unevaluated(def_id, ref substs) => { let param_env = ParamEnv::reveal_all(); let instance = Instance::resolve(fx.tcx, param_env, def_id, substs).unwrap(); @@ -25,11 +34,11 @@ pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Cons }; fx.tcx.const_eval(param_env.and(cid)).unwrap() }, - _ => const_val, - }; - - fx.tcx.sess.warn(&format!("const_val: {:?} const_: {:?}", const_val, const_)); + _ => const_, + } +} +fn trans_const_value<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &'tcx Const<'tcx>) -> CValue<'tcx> { let ty = fx.monomorphize(&const_.ty); let layout = fx.layout_of(ty); match ty.sty { @@ -50,24 +59,30 @@ pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Cons CValue::Func(func_ref, layout) } _ => { - if true { - // TODO: cranelift-module api seems to be used wrong, - // thus causing panics for some consts, so this disables it - return CValue::ByRef(fx.bcx.ins().iconst(types::I64, 0), layout); - } - let mut memory = Memory::::new(fx.tcx.at(DUMMY_SP), ()); - let alloc = fx.tcx.const_value_to_allocation(const_); - //println!("const value: {:?} allocation: {:?}", value, alloc); - let alloc_id = memory.allocate_value(alloc.clone(), MemoryKind::Stack).unwrap(); - let data_id = get_global_for_alloc_id(fx, &memory, alloc_id); - let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func); - // TODO: does global_value return a ptr of a val? - let global_ptr = fx.bcx.ins().global_value(types::I64, local_data_id); - CValue::ByRef(global_ptr, layout) + trans_const_place(fx, const_).to_cvalue(fx) } } } +fn trans_const_place<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &'tcx Const<'tcx>) -> CPlace<'tcx> { + let ty = fx.monomorphize(&const_.ty); + let layout = fx.layout_of(ty); + if true { + // TODO: cranelift-module api seems to be used wrong, + // thus causing panics for some consts, so this disables it + return CPlace::Addr(fx.bcx.ins().iconst(types::I64, 0), layout); + } + let mut memory = Memory::::new(fx.tcx.at(DUMMY_SP), ()); + let alloc = fx.tcx.const_value_to_allocation(const_); + //println!("const value: {:?} allocation: {:?}", value, alloc); + let alloc_id = memory.allocate_value(alloc.clone(), MemoryKind::Stack).unwrap(); + let data_id = get_global_for_alloc_id(fx, &memory, alloc_id); + let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func); + // TODO: does global_value return a ptr of a val? + let global_ptr = fx.bcx.ins().global_value(types::I64, local_data_id); + CPlace::Addr(global_ptr, layout) +} + // If ret.1 is true, then the global didn't exist before fn define_global_for_alloc_id(fx: &mut FunctionCx, alloc_id: AllocId, todo: &mut HashMap) -> (DataId, bool) { use std::collections::hash_map::Entry; diff --git a/src/lib.rs b/src/lib.rs index c26b7cd1947..e69c1625f3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,14 +76,15 @@ mod prelude { pub use cranelift_module::{Module, Backend, DataContext, FuncId, DataId, Linkage, Writability}; pub use cranelift_simplejit::{SimpleJITBuilder, SimpleJITBackend}; - pub use abi::*; - pub use common::Variable; - pub use common::*; + pub use crate::abi::*; + pub use crate::common::Variable; + pub use crate::common::*; + pub use crate::base::{trans_operand, trans_place}; - pub use CodegenCx; + pub use crate::CodegenCx; } -use prelude::*; +use crate::prelude::*; pub struct CodegenCx<'a, 'tcx: 'a, B: Backend + 'a> { pub tcx: TyCtxt<'a, 'tcx, 'tcx>, @@ -279,7 +280,7 @@ impl CodegenBackend for CraneliftCodegenBackend { .unwrap() ); - Box::new(::OngoingCodegen { + Box::new(OngoingCodegen { product: translated_module.finish(), metadata: metadata.raw_data, crate_name: tcx.crate_name(LOCAL_CRATE), diff --git a/src/pretty_clif.rs b/src/pretty_clif.rs index 5c38f8af119..51c17037561 100644 --- a/src/pretty_clif.rs +++ b/src/pretty_clif.rs @@ -4,7 +4,7 @@ use std::fmt; use cranelift::codegen::write::{FuncWriter, PlainWriter}; -use prelude::*; +use crate::prelude::*; pub struct CommentWriter(pub HashMap);