mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-29 02:03:53 +00:00
Introduce CodegenCx
This commit is contained in:
parent
4f32e703a8
commit
6fa3140222
39
src/base.rs
39
src/base.rs
@ -9,13 +9,11 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trans_mono_item<'a, 'tcx: 'a>(
|
||||
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
module: &mut Module<impl Backend>,
|
||||
caches: &mut Caches<'tcx>,
|
||||
ccx: &mut crate::constant::ConstantCx,
|
||||
pub fn trans_mono_item<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
|
||||
cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
|
||||
mono_item: MonoItem<'tcx>,
|
||||
) {
|
||||
let tcx = cx.tcx;
|
||||
match mono_item {
|
||||
MonoItem::Fn(inst) => {
|
||||
let _inst_guard =
|
||||
@ -44,10 +42,10 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
|
||||
}
|
||||
});
|
||||
|
||||
trans_fn(tcx, module, ccx, caches, inst);
|
||||
trans_fn(cx, inst);
|
||||
}
|
||||
MonoItem::Static(def_id) => {
|
||||
crate::constant::codegen_static(ccx, def_id);
|
||||
crate::constant::codegen_static(&mut cx.ccx, def_id);
|
||||
}
|
||||
MonoItem::GlobalAsm(node_id) => tcx
|
||||
.sess
|
||||
@ -55,19 +53,18 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
|
||||
}
|
||||
}
|
||||
|
||||
fn trans_fn<'a, 'tcx: 'a>(
|
||||
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
module: &mut Module<impl Backend>,
|
||||
constants: &mut crate::constant::ConstantCx,
|
||||
caches: &mut Caches<'tcx>,
|
||||
fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
|
||||
cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
|
||||
instance: Instance<'tcx>,
|
||||
) {
|
||||
let tcx = cx.tcx;
|
||||
|
||||
// Step 1. Get mir
|
||||
let mir = tcx.instance_mir(instance.def);
|
||||
|
||||
// Step 2. Declare function
|
||||
let (name, sig) = get_function_name_and_sig(tcx, instance);
|
||||
let func_id = module
|
||||
let func_id = cx.module
|
||||
.declare_function(&name, Linkage::Export, &sig)
|
||||
.unwrap();
|
||||
|
||||
@ -84,10 +81,10 @@ fn trans_fn<'a, 'tcx: 'a>(
|
||||
}
|
||||
|
||||
// Step 5. Make FunctionCx
|
||||
let pointer_type = module.target_config().pointer_type();
|
||||
let pointer_type = cx.module.target_config().pointer_type();
|
||||
let mut fx = FunctionCx {
|
||||
tcx,
|
||||
module,
|
||||
module: cx.module,
|
||||
pointer_type,
|
||||
|
||||
instance,
|
||||
@ -98,8 +95,8 @@ fn trans_fn<'a, 'tcx: 'a>(
|
||||
local_map: HashMap::new(),
|
||||
|
||||
comments: HashMap::new(),
|
||||
constants,
|
||||
caches,
|
||||
constants: &mut cx.ccx,
|
||||
caches: &mut cx.caches,
|
||||
|
||||
top_nop: None,
|
||||
};
|
||||
@ -132,11 +129,11 @@ fn trans_fn<'a, 'tcx: 'a>(
|
||||
verify_func(tcx, writer, &func);
|
||||
|
||||
// Step 9. Define function
|
||||
caches.context.func = func;
|
||||
module
|
||||
.define_function(func_id, &mut caches.context)
|
||||
cx.caches.context.func = func;
|
||||
cx.module
|
||||
.define_function(func_id, &mut cx.caches.context)
|
||||
.unwrap();
|
||||
caches.context.clear();
|
||||
cx.caches.context.clear();
|
||||
}
|
||||
|
||||
fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {
|
||||
|
@ -611,6 +611,7 @@ pub fn clif_intcast<'a, 'tcx: 'a>(
|
||||
}
|
||||
|
||||
pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
|
||||
// FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
|
||||
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
pub module: &'a mut Module<B>,
|
||||
pub pointer_type: Type, // Cached from module
|
||||
|
36
src/lib.rs
36
src/lib.rs
@ -97,7 +97,7 @@ mod prelude {
|
||||
pub use crate::common::*;
|
||||
pub use crate::trap::*;
|
||||
pub use crate::unimpl::{unimpl, with_unimpl_span};
|
||||
pub use crate::Caches;
|
||||
pub use crate::{Caches, CodegenCx};
|
||||
}
|
||||
|
||||
pub struct Caches<'tcx> {
|
||||
@ -105,8 +105,8 @@ pub struct Caches<'tcx> {
|
||||
pub vtables: HashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>), DataId>,
|
||||
}
|
||||
|
||||
impl<'tcx> Caches<'tcx> {
|
||||
fn new() -> Self {
|
||||
impl<'tcx> Default for Caches<'tcx> {
|
||||
fn default() -> Self {
|
||||
Caches {
|
||||
context: Context::new(),
|
||||
vtables: HashMap::new(),
|
||||
@ -114,6 +114,28 @@ impl<'tcx> Caches<'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CodegenCx<'a, 'clif, 'tcx, B: Backend + 'static> {
|
||||
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
module: &'clif mut Module<B>,
|
||||
ccx: ConstantCx,
|
||||
caches: Caches<'tcx>,
|
||||
}
|
||||
|
||||
impl<'a, 'clif, 'tcx, B: Backend + 'static> CodegenCx<'a, 'clif, 'tcx, B> {
|
||||
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, module: &'clif mut Module<B>) -> Self {
|
||||
CodegenCx {
|
||||
tcx,
|
||||
module,
|
||||
ccx: ConstantCx::default(),
|
||||
caches: Caches::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn finalize(self) {
|
||||
self.ccx.finalize(self.tcx, self.module);
|
||||
}
|
||||
}
|
||||
|
||||
struct CraneliftCodegenBackend;
|
||||
|
||||
impl CodegenBackend for CraneliftCodegenBackend {
|
||||
@ -369,17 +391,15 @@ fn codegen_mono_items<'a, 'tcx: 'a>(
|
||||
log: &mut Option<File>,
|
||||
mono_items: FxHashMap<MonoItem<'tcx>, (RLinkage, Visibility)>,
|
||||
) {
|
||||
let mut cx = CodegenCx::new(tcx, module);
|
||||
time("codegen mono items", move || {
|
||||
let mut caches = Caches::new();
|
||||
let mut ccx = ConstantCx::default();
|
||||
|
||||
for (mono_item, (_linkage, _vis)) in mono_items {
|
||||
unimpl::try_unimpl(tcx, log, || {
|
||||
base::trans_mono_item(tcx, module, &mut caches, &mut ccx, mono_item);
|
||||
base::trans_mono_item(&mut cx, mono_item);
|
||||
});
|
||||
}
|
||||
|
||||
ccx.finalize(tcx, module);
|
||||
cx.finalize();
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user