Skip to content

Commit 497b2c2

Browse files
committed
feat: pretty printing
1 parent 73c56b6 commit 497b2c2

38 files changed

+840
-786
lines changed

crates/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["rustc_codegen_c"]
3+
members = ["rustc_codegen_c", "rustc_codegen_c_ast"]
44

55
[workspace.package]
66
version = "0.1.0"

crates/rustc_codegen_c/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version.workspace = true
77
crate-type = ["dylib"]
88

99
[dependencies]
10+
rustc_codegen_c_ast = { path = "../rustc_codegen_c_ast" }
1011

1112
# This package uses rustc crates.
1213
[package.metadata.rust-analyzer]

crates/rustc_codegen_c/src/base.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
use std::time::Instant;
22

3+
use rustc_codegen_c_ast::{ModuleArena, ModuleCtxt};
34
use rustc_codegen_ssa::mono_item::MonoItemExt;
45
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
56
use rustc_middle::dep_graph;
67
use rustc_middle::ty::TyCtxt;
78

89
use crate::builder::Builder;
910
use crate::context::CodegenCx;
10-
use crate::module::ModuleContext;
11+
12+
/// Needed helper functions
13+
const HELPER: &str = include_str!("./helper.h");
1114

1215
// note: parallel
1316
// it seems this function will be invoked parallelly (if parallel codegen is enabled)
1417

1518
pub fn compile_codegen_unit(
1619
tcx: TyCtxt<'_>,
1720
cgu_name: rustc_span::Symbol,
18-
) -> (ModuleCodegen<ModuleContext>, u64) {
21+
) -> (ModuleCodegen<String>, u64) {
1922
let start_time = Instant::now();
2023

2124
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
@@ -36,24 +39,23 @@ pub fn compile_codegen_unit(
3639
(module, cost)
3740
}
3841

39-
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegen<ModuleContext> {
42+
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegen<String> {
4043
let cgu = tcx.codegen_unit(cgu_name);
4144

42-
let cx = CodegenCx::new(tcx);
45+
let mcx = ModuleArena::new(HELPER);
46+
let mcx = ModuleCtxt(&mcx);
47+
let cx = CodegenCx::new(tcx, mcx);
4348

4449
let mono_items = cgu.items_in_deterministic_order(tcx);
4550
for &(mono_item, data) in &mono_items {
46-
mono_item.predefine::<Builder<'_, '_>>(&cx, data.linkage, data.visibility);
51+
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, data.linkage, data.visibility);
4752
}
4853

4954
// ... and now that we have everything pre-defined, fill out those definitions.
5055
for &(mono_item, _) in &mono_items {
51-
mono_item.define::<Builder<'_, '_>>(&cx);
56+
mono_item.define::<Builder<'_, '_, '_>>(&cx);
5257
}
5358

54-
ModuleCodegen {
55-
name: cgu_name.to_string(),
56-
module_llvm: cx.finish(),
57-
kind: ModuleKind::Regular,
58-
}
59+
let module = mcx.to_string();
60+
ModuleCodegen { name: cgu_name.to_string(), module_llvm: module, kind: ModuleKind::Regular }
5961
}

crates/rustc_codegen_c/src/builder.rs

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use std::ops::Deref;
44

55
use rustc_abi::{HasDataLayout, TargetDataLayout};
6+
use rustc_codegen_c_ast::func::CFunc;
7+
use rustc_codegen_c_ast::r#type::CTy;
68
use rustc_codegen_ssa::traits::{BackendTypes, BuilderMethods, HasCodegen};
79
use rustc_middle::ty::layout::{
810
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers,
@@ -12,10 +14,7 @@ use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
1214
use rustc_target::abi::call::FnAbi;
1315
use rustc_target::spec::{HasTargetSpec, Target};
1416

15-
use crate::c_expr;
16-
use crate::context::{CFunctionBuilder, CodegenCx};
17-
use crate::module::{CDecl, CExpr, CStmt, CType};
18-
use crate::utils::slab::Id;
17+
use crate::context::CodegenCx;
1918

2019
mod abi;
2120
mod asm;
@@ -24,68 +23,68 @@ mod debug_info;
2423
mod intrinsic_call;
2524
mod r#static;
2625

27-
pub struct Builder<'a, 'tcx> {
28-
pub cx: &'a CodegenCx<'tcx>,
29-
bb: Id<CFunctionBuilder>,
26+
pub struct Builder<'a, 'tcx, 'mx> {
27+
pub cx: &'a CodegenCx<'tcx, 'mx>,
28+
bb: CFunc<'mx>,
3029
}
3130

32-
impl<'a, 'tcx> Deref for Builder<'a, 'tcx> {
33-
type Target = CodegenCx<'tcx>;
31+
impl<'a, 'tcx, 'mx> Deref for Builder<'a, 'tcx, 'mx> {
32+
type Target = CodegenCx<'tcx, 'mx>;
3433

3534
fn deref<'b>(&'b self) -> &'a Self::Target {
3635
self.cx
3736
}
3837
}
3938

40-
impl<'tcx> HasCodegen<'tcx> for Builder<'_, 'tcx> {
41-
type CodegenCx = CodegenCx<'tcx>;
39+
impl<'tcx, 'mx> HasCodegen<'tcx> for Builder<'_, 'tcx, 'mx> {
40+
type CodegenCx = CodegenCx<'tcx, 'mx>;
4241
}
4342

44-
impl<'tcx> HasDataLayout for Builder<'_, 'tcx> {
43+
impl<'tcx, 'mx> HasDataLayout for Builder<'_, 'tcx, 'mx> {
4544
fn data_layout(&self) -> &TargetDataLayout {
4645
todo!()
4746
}
4847
}
4948

50-
impl<'tcx> HasTyCtxt<'tcx> for Builder<'_, 'tcx> {
49+
impl<'tcx, 'mx> HasTyCtxt<'tcx> for Builder<'_, 'tcx, 'mx> {
5150
fn tcx(&self) -> TyCtxt<'tcx> {
5251
self.cx.tcx()
5352
}
5453
}
5554

56-
impl<'tcx> HasParamEnv<'tcx> for Builder<'_, 'tcx> {
55+
impl<'tcx, 'mx> HasParamEnv<'tcx> for Builder<'_, 'tcx, 'mx> {
5756
fn param_env(&self) -> ParamEnv<'tcx> {
5857
self.cx.param_env()
5958
}
6059
}
6160

62-
impl<'tcx> BackendTypes for Builder<'_, 'tcx> {
63-
type Value = <CodegenCx<'tcx> as BackendTypes>::Value;
64-
type Function = <CodegenCx<'tcx> as BackendTypes>::Function;
65-
type BasicBlock = <CodegenCx<'tcx> as BackendTypes>::BasicBlock;
66-
type Type = <CodegenCx<'tcx> as BackendTypes>::Type;
67-
type Funclet = <CodegenCx<'tcx> as BackendTypes>::Funclet;
61+
impl<'tcx, 'mx> BackendTypes for Builder<'_, 'tcx, 'mx> {
62+
type Value = <CodegenCx<'tcx, 'mx> as BackendTypes>::Value;
63+
type Function = <CodegenCx<'tcx, 'mx> as BackendTypes>::Function;
64+
type BasicBlock = <CodegenCx<'tcx, 'mx> as BackendTypes>::BasicBlock;
65+
type Type = <CodegenCx<'tcx, 'mx> as BackendTypes>::Type;
66+
type Funclet = <CodegenCx<'tcx, 'mx> as BackendTypes>::Funclet;
6867

69-
type DIScope = <CodegenCx<'tcx> as BackendTypes>::DIScope;
70-
type DILocation = <CodegenCx<'tcx> as BackendTypes>::DILocation;
71-
type DIVariable = <CodegenCx<'tcx> as BackendTypes>::DIVariable;
68+
type DIScope = <CodegenCx<'tcx, 'mx> as BackendTypes>::DIScope;
69+
type DILocation = <CodegenCx<'tcx, 'mx> as BackendTypes>::DILocation;
70+
type DIVariable = <CodegenCx<'tcx, 'mx> as BackendTypes>::DIVariable;
7271
}
7372

74-
impl<'tcx> HasTargetSpec for Builder<'_, 'tcx> {
73+
impl<'tcx, 'mx> HasTargetSpec for Builder<'_, 'tcx, 'mx> {
7574
fn target_spec(&self) -> &Target {
7675
todo!()
7776
}
7877
}
7978

80-
impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, 'tcx> {
79+
impl<'tcx, 'mx> LayoutOfHelpers<'tcx> for Builder<'_, 'tcx, 'mx> {
8180
type LayoutOfResult = TyAndLayout<'tcx>;
8281

8382
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: rustc_span::Span, ty: Ty<'tcx>) -> ! {
8483
todo!()
8584
}
8685
}
8786

88-
impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, 'tcx> {
87+
impl<'tcx, 'mx> FnAbiOfHelpers<'tcx> for Builder<'_, 'tcx, 'mx> {
8988
type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
9089

9190
fn handle_fn_abi_err(
@@ -98,7 +97,7 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, 'tcx> {
9897
}
9998
}
10099

101-
impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
100+
impl<'a, 'tcx, 'mx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx, 'mx> {
102101
fn build(cx: &'a Self::CodegenCx, llbb: Self::BasicBlock) -> Self {
103102
Self { cx, bb: llbb }
104103
}
@@ -128,13 +127,11 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
128127
}
129128

130129
fn ret_void(&mut self) {
131-
let mut functions = self.cx.functions.borrow_mut();
132-
functions[self.bb].push_stmt(CStmt::Return(None));
130+
self.bb.0.push_stmt(self.cx.mcx.ret(None));
133131
}
134132

135133
fn ret(&mut self, v: Self::Value) {
136-
let mut functions = self.cx.functions.borrow_mut();
137-
functions[self.bb].push_stmt(CStmt::Return(Some(Box::new(CExpr::Value(v)))));
134+
self.bb.0.push_stmt(self.cx.mcx.ret(Some(self.cx.mcx.value(v))))
138135
}
139136

140137
fn br(&mut self, dest: Self::BasicBlock) {
@@ -510,33 +507,23 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
510507
/// integer via explicit conversion. Then, use a helper function to cast the
511508
/// result to a signed integer.
512509
fn intcast(&mut self, val: Self::Value, dest_ty: Self::Type, is_signed: bool) -> Self::Value {
513-
let mut functions = self.cx.functions.borrow_mut();
514-
let function = &mut functions[self.bb];
515-
let ret = function.next_value();
516-
517-
let dest = if let CType::Primitive(ty) = dest_ty { ty } else { unreachable!() };
518-
let cast = if !dest.is_signed() {
519-
CExpr::Cast { ty: CType::Primitive(dest), expr: Box::new(CExpr::Value(val)) }
520-
} else {
521-
let cast = CExpr::Cast {
522-
ty: CType::Primitive(dest.to_unsigned()),
523-
expr: Box::new(CExpr::Value(val)),
524-
};
525-
CExpr::Call {
526-
callee: c_expr!("__rust_utos"),
527-
args: vec![
528-
c_expr!(dest.to_unsigned().to_string()),
529-
c_expr!(dest.to_string()),
510+
let mcx = self.cx.mcx;
511+
let ret = self.bb.0.next_local_var();
512+
513+
let dest = if let CTy::Primitive(ty) = dest_ty { ty } else { unreachable!() };
514+
let mut cast = mcx.cast(CTy::Primitive(dest), mcx.value(val));
515+
if dest.is_signed() {
516+
cast = mcx.call(
517+
mcx.raw("__rust_utos"),
518+
vec![
519+
mcx.raw(dest.to_unsigned().to_str()),
520+
mcx.raw(dest.to_str()),
530521
cast,
531-
c_expr!(dest.max_value()),
522+
mcx.raw(dest.max_value()),
532523
],
533-
}
534-
};
535-
function.push_stmt(CStmt::Decl(Box::new(CDecl::Var {
536-
name: ret,
537-
ty: dest_ty,
538-
init: Some(cast),
539-
})));
524+
);
525+
}
526+
self.bb.0.push_stmt(mcx.decl_stmt(mcx.var(ret, dest_ty, Some(cast))));
540527
ret
541528
}
542529

crates/rustc_codegen_c/src/builder/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
use rustc_codegen_c_ast::expr::CValue;
12
use rustc_codegen_ssa::mir::place::PlaceRef;
23
use rustc_codegen_ssa::traits::{AbiBuilderMethods, ArgAbiMethods};
34
use rustc_middle::ty::Ty;
45
use rustc_target::abi::call::ArgAbi;
56

67
use crate::builder::Builder;
7-
use crate::module::CValue;
88

9-
impl<'tcx> AbiBuilderMethods<'tcx> for Builder<'_, 'tcx> {
9+
impl<'tcx, 'mx> AbiBuilderMethods<'tcx> for Builder<'_, 'tcx, 'mx> {
1010
fn get_param(&mut self, index: usize) -> Self::Value {
1111
// Params are first n variables in the function
1212
CValue::Local(index)
1313
}
1414
}
1515

16-
impl<'tcx> ArgAbiMethods<'tcx> for Builder<'_, 'tcx> {
16+
impl<'tcx, 'mx> ArgAbiMethods<'tcx> for Builder<'_, 'tcx, 'mx> {
1717
fn store_fn_arg(
1818
&mut self,
1919
arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,

crates/rustc_codegen_c/src/builder/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::ty::Instance;
44

55
use crate::builder::Builder;
66

7-
impl<'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'tcx> {
7+
impl<'tcx, 'mx> AsmBuilderMethods<'tcx> for Builder<'_, 'tcx, 'mx> {
88
fn codegen_inline_asm(
99
&mut self,
1010
template: &[InlineAsmTemplatePiece],

crates/rustc_codegen_c/src/builder/coverage_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_codegen_ssa::traits::CoverageInfoBuilderMethods;
22

33
use crate::builder::Builder;
44

5-
impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, 'tcx> {
5+
impl<'tcx, 'mx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, 'tcx, 'mx> {
66
fn add_coverage(
77
&mut self,
88
instance: rustc_middle::ty::Instance<'tcx>,

crates/rustc_codegen_c/src/builder/debug_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_codegen_ssa::traits::DebugInfoBuilderMethods;
22

33
use crate::builder::Builder;
44

5-
impl DebugInfoBuilderMethods for Builder<'_, '_> {
5+
impl DebugInfoBuilderMethods for Builder<'_, '_, '_> {
66
fn dbg_var_addr(
77
&mut self,
88
dbg_var: Self::DIVariable,

crates/rustc_codegen_c/src/builder/intrinsic_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_target::abi::call::FnAbi;
55

66
use crate::builder::Builder;
77

8-
impl<'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'tcx> {
8+
impl<'tcx, 'mx> IntrinsicCallMethods<'tcx> for Builder<'_, 'tcx, 'mx> {
99
fn codegen_intrinsic_call(
1010
&mut self,
1111
instance: Instance<'tcx>,

crates/rustc_codegen_c/src/builder/static.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::def_id::DefId;
33

44
use crate::builder::Builder;
55

6-
impl<'tcx> StaticBuilderMethods for Builder<'_, 'tcx> {
6+
impl<'tcx, 'mx> StaticBuilderMethods for Builder<'_, 'tcx, 'mx> {
77
fn get_static(&mut self, def_id: DefId) -> Self::Value {
88
todo!()
99
}

0 commit comments

Comments
 (0)