+cargo-features = ["edition"]
+
[package]
name = "rustc_codegen_cranelift"
version = "0.1.0"
authors = ["bjorn3 <bjorn3@users.noreply.github.com>"]
+edition = "2018"
[lib]
crate-type = ["dylib"]
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);
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
};
// 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 {
args
.into_iter()
.map(|arg| {
- ::base::trans_operand(fx, arg)
+ trans_operand(fx, arg)
})
.collect::<Vec<_>>()
};
}
"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" => {
};
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!(),
};
-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;
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);
}
};
- 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);
};
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(), &[]);
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));
(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)),
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);
cplace.to_cvalue(fx)
},
Operand::Constant(const_) => {
- ::constant::trans_constant(fx, const_)
+ crate::constant::trans_constant(fx, const_)
}
}
}
use cranelift_module::{Module, FuncId, DataId};
-use prelude::*;
+use crate::prelude::*;
pub type CurrentBackend = ::cranelift_simplejit::SimpleJITBackend;
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,
-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(),
- };
-
- let const_ = match const_val.val {
+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_ = 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();
};
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 {
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::<CompileTimeEvaluator>::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::<CompileTimeEvaluator>::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<AllocId, DataId>) -> (DataId, bool) {
use std::collections::hash_map::Entry;
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>,
.unwrap()
);
- Box::new(::OngoingCodegen {
+ Box::new(OngoingCodegen {
product: translated_module.finish(),
metadata: metadata.raw_data,
crate_name: tcx.crate_name(LOCAL_CRATE),
use cranelift::codegen::write::{FuncWriter, PlainWriter};
-use prelude::*;
+use crate::prelude::*;
pub struct CommentWriter(pub HashMap<Inst, String>);