Skip to content

Commit 6fa3140

Browse files
committed
Introduce CodegenCx
1 parent 4f32e70 commit 6fa3140

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

src/base.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
99
}
1010
}
1111

12-
pub fn trans_mono_item<'a, 'tcx: 'a>(
13-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
14-
module: &mut Module<impl Backend>,
15-
caches: &mut Caches<'tcx>,
16-
ccx: &mut crate::constant::ConstantCx,
12+
pub fn trans_mono_item<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
13+
cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
1714
mono_item: MonoItem<'tcx>,
1815
) {
16+
let tcx = cx.tcx;
1917
match mono_item {
2018
MonoItem::Fn(inst) => {
2119
let _inst_guard =
@@ -44,30 +42,29 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
4442
}
4543
});
4644

47-
trans_fn(tcx, module, ccx, caches, inst);
45+
trans_fn(cx, inst);
4846
}
4947
MonoItem::Static(def_id) => {
50-
crate::constant::codegen_static(ccx, def_id);
48+
crate::constant::codegen_static(&mut cx.ccx, def_id);
5149
}
5250
MonoItem::GlobalAsm(node_id) => tcx
5351
.sess
5452
.fatal(&format!("Unimplemented global asm mono item {:?}", node_id)),
5553
}
5654
}
5755

58-
fn trans_fn<'a, 'tcx: 'a>(
59-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
60-
module: &mut Module<impl Backend>,
61-
constants: &mut crate::constant::ConstantCx,
62-
caches: &mut Caches<'tcx>,
56+
fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
57+
cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
6358
instance: Instance<'tcx>,
6459
) {
60+
let tcx = cx.tcx;
61+
6562
// Step 1. Get mir
6663
let mir = tcx.instance_mir(instance.def);
6764

6865
// Step 2. Declare function
6966
let (name, sig) = get_function_name_and_sig(tcx, instance);
70-
let func_id = module
67+
let func_id = cx.module
7168
.declare_function(&name, Linkage::Export, &sig)
7269
.unwrap();
7370

@@ -84,10 +81,10 @@ fn trans_fn<'a, 'tcx: 'a>(
8481
}
8582

8683
// Step 5. Make FunctionCx
87-
let pointer_type = module.target_config().pointer_type();
84+
let pointer_type = cx.module.target_config().pointer_type();
8885
let mut fx = FunctionCx {
8986
tcx,
90-
module,
87+
module: cx.module,
9188
pointer_type,
9289

9390
instance,
@@ -98,8 +95,8 @@ fn trans_fn<'a, 'tcx: 'a>(
9895
local_map: HashMap::new(),
9996

10097
comments: HashMap::new(),
101-
constants,
102-
caches,
98+
constants: &mut cx.ccx,
99+
caches: &mut cx.caches,
103100

104101
top_nop: None,
105102
};
@@ -132,11 +129,11 @@ fn trans_fn<'a, 'tcx: 'a>(
132129
verify_func(tcx, writer, &func);
133130

134131
// Step 9. Define function
135-
caches.context.func = func;
136-
module
137-
.define_function(func_id, &mut caches.context)
132+
cx.caches.context.func = func;
133+
cx.module
134+
.define_function(func_id, &mut cx.caches.context)
138135
.unwrap();
139-
caches.context.clear();
136+
cx.caches.context.clear();
140137
}
141138

142139
fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {

src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ pub fn clif_intcast<'a, 'tcx: 'a>(
611611
}
612612

613613
pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
614+
// FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
614615
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
615616
pub module: &'a mut Module<B>,
616617
pub pointer_type: Type, // Cached from module

src/lib.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,45 @@ mod prelude {
9797
pub use crate::common::*;
9898
pub use crate::trap::*;
9999
pub use crate::unimpl::{unimpl, with_unimpl_span};
100-
pub use crate::Caches;
100+
pub use crate::{Caches, CodegenCx};
101101
}
102102

103103
pub struct Caches<'tcx> {
104104
pub context: Context,
105105
pub vtables: HashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>), DataId>,
106106
}
107107

108-
impl<'tcx> Caches<'tcx> {
109-
fn new() -> Self {
108+
impl<'tcx> Default for Caches<'tcx> {
109+
fn default() -> Self {
110110
Caches {
111111
context: Context::new(),
112112
vtables: HashMap::new(),
113113
}
114114
}
115115
}
116116

117+
pub struct CodegenCx<'a, 'clif, 'tcx, B: Backend + 'static> {
118+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
119+
module: &'clif mut Module<B>,
120+
ccx: ConstantCx,
121+
caches: Caches<'tcx>,
122+
}
123+
124+
impl<'a, 'clif, 'tcx, B: Backend + 'static> CodegenCx<'a, 'clif, 'tcx, B> {
125+
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, module: &'clif mut Module<B>) -> Self {
126+
CodegenCx {
127+
tcx,
128+
module,
129+
ccx: ConstantCx::default(),
130+
caches: Caches::default(),
131+
}
132+
}
133+
134+
fn finalize(self) {
135+
self.ccx.finalize(self.tcx, self.module);
136+
}
137+
}
138+
117139
struct CraneliftCodegenBackend;
118140

119141
impl CodegenBackend for CraneliftCodegenBackend {
@@ -369,17 +391,15 @@ fn codegen_mono_items<'a, 'tcx: 'a>(
369391
log: &mut Option<File>,
370392
mono_items: FxHashMap<MonoItem<'tcx>, (RLinkage, Visibility)>,
371393
) {
394+
let mut cx = CodegenCx::new(tcx, module);
372395
time("codegen mono items", move || {
373-
let mut caches = Caches::new();
374-
let mut ccx = ConstantCx::default();
375-
376396
for (mono_item, (_linkage, _vis)) in mono_items {
377397
unimpl::try_unimpl(tcx, log, || {
378-
base::trans_mono_item(tcx, module, &mut caches, &mut ccx, mono_item);
398+
base::trans_mono_item(&mut cx, mono_item);
379399
});
380400
}
381401

382-
ccx.finalize(tcx, module);
402+
cx.finalize();
383403
});
384404
}
385405

0 commit comments

Comments
 (0)