}
}
-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 =
}
});
- 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
}
}
-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();
}
// 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,
local_map: HashMap::new(),
comments: HashMap::new(),
- constants,
- caches,
+ constants: &mut cx.ccx,
+ caches: &mut cx.caches,
top_nop: None,
};
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) {
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> {
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(),
}
}
+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 {
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();
});
}