Skip to content

Commit e428e32

Browse files
authored
Merge pull request #128 from Pivot-Studio/refactor/seperate_code_gen
refactor: separate codegen logic
2 parents 082929e + 87066c8 commit e428e32

25 files changed

+2821
-1523
lines changed

src/ast/builder/llvmbuilder.rs

Lines changed: 1263 additions & 0 deletions
Large diffs are not rendered by default.

src/ast/builder/mod.rs

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/// 为以后codegen逻辑完全分离作准备,此包代码应该遵循以下原则:
2+
/// 1. 所有Builder的字段都应该private,不应该被外部直接访问
3+
/// 2. 所有涉及llvm类型的函数(包括参数或返回值)都应该是private的
4+
pub mod llvmbuilder;
5+
pub mod no_op_builder;
6+
use std::{cell::RefCell, path::Path, rc::Rc};
7+
8+
use enum_dispatch::enum_dispatch;
9+
use inkwell::{values::BasicValueEnum, FloatPredicate, IntPredicate};
10+
11+
use self::llvmbuilder::LLVMBuilder;
12+
use self::no_op_builder::NoOpBuilder;
13+
14+
use super::{
15+
ctx::{Ctx, PLDiag},
16+
node::{types::TypedIdentifierNode, TypeNodeEnum},
17+
pltype::{FNType, Field, PLType, PriType},
18+
range::{Pos, Range},
19+
};
20+
21+
#[enum_dispatch]
22+
pub trait IRBuilder<'a, 'ctx> {
23+
fn get_global_var_handle(&self, name: &str) -> Option<ValueHandle>;
24+
fn new_subscope(&self, start: Pos);
25+
fn add_global(
26+
&self,
27+
name: &str,
28+
pltype: Rc<RefCell<PLType>>,
29+
ctx: &mut Ctx<'a>,
30+
line: u32,
31+
pltp: &PLType,
32+
) -> ValueHandle;
33+
fn alloc_vtp(&self, name: &str, v: ValueHandle) -> ValueHandle;
34+
fn alloc(&self, name: &str, pltype: &PLType, ctx: &mut Ctx<'a>) -> ValueHandle;
35+
fn build_conditional_branch(
36+
&self,
37+
cond: ValueHandle,
38+
then_bb: BlockHandle,
39+
else_bb: BlockHandle,
40+
);
41+
fn build_const_in_bounds_gep(&self, ptr: ValueHandle, index: &[u64], name: &str)
42+
-> ValueHandle;
43+
fn build_dbg_location(&self, pos: Pos);
44+
fn build_in_bounds_gep(
45+
&self,
46+
ptr: ValueHandle,
47+
index: &[ValueHandle],
48+
name: &str,
49+
) -> ValueHandle;
50+
fn build_return(&self, v: Option<ValueHandle>);
51+
fn build_store(&self, ptr: ValueHandle, value: ValueHandle);
52+
fn build_struct_gep(
53+
&self,
54+
structv: ValueHandle,
55+
index: u32,
56+
name: &str,
57+
) -> Result<ValueHandle, ()>;
58+
fn build_sub_program(
59+
&self,
60+
paralist: Vec<Box<TypedIdentifierNode>>,
61+
ret: Box<TypeNodeEnum>,
62+
fntype: &FNType,
63+
fnvalue: ValueHandle,
64+
child: &mut Ctx<'a>,
65+
) -> Result<(), PLDiag>;
66+
fn clear_insertion_position(&self);
67+
fn const_string(&self, s: &str) -> ValueHandle;
68+
fn create_parameter_variable(
69+
&self,
70+
fntype: &FNType,
71+
pos: Pos,
72+
i: usize,
73+
child: &mut Ctx<'a>,
74+
fnvalue: ValueHandle,
75+
alloca: ValueHandle,
76+
allocab: BlockHandle,
77+
);
78+
fn delete_block(&self, b: BlockHandle);
79+
fn finalize_debug(&self);
80+
fn float_value(&self, ty: &PriType, v: f64) -> ValueHandle;
81+
fn get_first_basic_block(&self, v: ValueHandle) -> BlockHandle;
82+
fn get_first_instruction(&self, bb: BlockHandle) -> Option<ValueHandle>;
83+
fn get_last_basic_block(&self, v: ValueHandle) -> BlockHandle;
84+
fn insert_var_declare(
85+
&self,
86+
name: &str,
87+
pos: Pos,
88+
pltype: &PLType,
89+
v: ValueHandle,
90+
ctx: &mut Ctx<'a>,
91+
);
92+
fn int_value(&self, ty: &PriType, v: u64, sign_ext: bool) -> ValueHandle;
93+
fn position_at(&self, v: ValueHandle);
94+
fn print_to_file(&self, file: &Path) -> Result<(), String>;
95+
fn rm_curr_debug_location(&self);
96+
fn try_set_fn_dbg(&self, pos: Pos, f: ValueHandle);
97+
fn write_bitcode_to_path(&self, path: &Path) -> bool;
98+
fn build_unconditional_branch(&self, bb: BlockHandle);
99+
fn position_at_end_block(&self, block: BlockHandle);
100+
fn add_body_to_struct_type(&self, name: &str, order_fields: &[Field], ctx: &mut Ctx<'a>);
101+
fn get_or_insert_fn_handle(&self, pltp: &FNType, ctx: &mut Ctx<'a>) -> ValueHandle;
102+
fn mv2heap(&self, val: ValueHandle, ctx: &mut Ctx<'a>) -> ValueHandle;
103+
fn gc_add_root(&self, stackptr: BasicValueEnum<'ctx>, ctx: &mut Ctx<'a>);
104+
fn gc_rm_root(&self, stackptr: ValueHandle, ctx: &mut Ctx<'a>);
105+
fn gc_rm_root_current(&self, stackptr: ValueHandle, ctx: &mut Ctx<'a>);
106+
fn gc_collect(&self, ctx: &mut Ctx<'a>);
107+
fn get_or_add_global(
108+
&self,
109+
name: &str,
110+
pltype: Rc<RefCell<PLType>>,
111+
ctx: &mut Ctx<'a>,
112+
) -> ValueHandle;
113+
fn build_load(&self, ptr: ValueHandle, name: &str) -> ValueHandle;
114+
fn try_load2var(
115+
&self,
116+
range: Range,
117+
v: ValueHandle,
118+
tp: Rc<RefCell<PLType>>,
119+
ctx: &mut Ctx<'a>,
120+
) -> Result<(ValueHandle, Rc<RefCell<PLType>>), PLDiag>;
121+
fn get_function(&self, name: &str) -> Option<ValueHandle>;
122+
fn build_call(&self, f: ValueHandle, args: &[ValueHandle]) -> Option<ValueHandle>;
123+
fn add_function(
124+
&self,
125+
name: &str,
126+
paramtps: &[PLType],
127+
ret: PLType,
128+
ctx: &mut Ctx<'a>,
129+
) -> ValueHandle;
130+
fn opaque_struct_type(&self, name: &str);
131+
fn build_int_z_extend(&self, v: ValueHandle, ty: &PriType, name: &str) -> ValueHandle;
132+
fn build_or(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
133+
fn build_and(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
134+
fn build_float_compare(
135+
&self,
136+
op: FloatPredicate,
137+
lhs: ValueHandle,
138+
rhs: ValueHandle,
139+
name: &str,
140+
) -> ValueHandle;
141+
fn build_int_compare(
142+
&self,
143+
op: IntPredicate,
144+
lhs: ValueHandle,
145+
rhs: ValueHandle,
146+
name: &str,
147+
) -> ValueHandle;
148+
fn build_int_add(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
149+
fn build_int_sub(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
150+
fn build_int_mul(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
151+
fn build_int_signed_div(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
152+
fn build_float_neg(&self, v: ValueHandle, name: &str) -> ValueHandle;
153+
fn build_float_add(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
154+
fn build_float_sub(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
155+
fn build_float_mul(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
156+
fn build_float_div(&self, lhs: ValueHandle, rhs: ValueHandle, name: &str) -> ValueHandle;
157+
fn append_basic_block(&self, func: ValueHandle, name: &str) -> BlockHandle;
158+
fn build_int_truncate(&self, v: ValueHandle, dest_ty: &PriType, name: &str) -> ValueHandle;
159+
fn build_int_neg(&self, v: ValueHandle, name: &str) -> ValueHandle;
160+
}
161+
162+
pub type ValueHandle = usize;
163+
pub type BlockHandle = usize;
164+
165+
#[enum_dispatch(IRBuilder)]
166+
pub enum BuilderEnum<'a, 'ctx> {
167+
LLVM(LLVMBuilder<'a, 'ctx>),
168+
NoOp(NoOpBuilder),
169+
}

0 commit comments

Comments
 (0)