Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scopes refactor #40

Merged
merged 11 commits into from
Jan 5, 2025
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ members = [
"fir",
"oir",
"scir",
"datatable", "repr",
"datatable",
"repr",
"infotable",
]
resolver = "2"

86 changes: 33 additions & 53 deletions fir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use perror::*;
use scopetable::ScopeTable;
use symtable::SymTable;
use types::*;
use typetable::TypeTable;

// Function Intermediate Representation
pub struct Fir {
Expand All @@ -42,7 +41,7 @@ impl Fir {
index: u32,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> Function {
let sig = Signature::new(CallConv::Cold);
Expand All @@ -51,31 +50,18 @@ impl Fir {
let mut func = Function::with_name_signature(name, sig);
let mut builder = FunctionBuilder::new(&mut func, ctx);
let root_block = builder.create_block();
// todo:: this is the issue with function arguments not working simple repr add case
func_def.args.iter().for_each(|x| {
let z = self
.recurse(
x.as_ref().as_ref(),
&mut builder,
dtbl,
scopes,
type_tables,
oir,
)
.recurse(*x, &mut builder, dtbl, scopes, types, oir)
.unwrap();
builder.func.signature.params.push(AbiParam::new(I64));
//let res = builder.block_params(root_block)[z.as_u32() as usize];
});
builder.func.signature.returns.push(AbiParam::new(I64));
builder.append_block_params_for_function_params(root_block);
builder.switch_to_block(root_block);
let _result = self.recurse(
&func_def.block,
&mut builder,
dtbl,
scopes,
type_tables,
oir,
);
let _result = self.recurse(func_def.block, &mut builder, dtbl, scopes, types, oir);
builder.seal_block(root_block);
builder.finalize();
func
Expand All @@ -86,7 +72,7 @@ impl Fir {
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
Expand All @@ -99,20 +85,21 @@ impl Fir {
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let temp = self
.recurse(&op.right, builder, dtbl, scopes, type_tables, oir)
.recurse(op.right, builder, dtbl, scopes, types, oir)
.unwrap();
// todo:: optimization: not all paths need declare var if value is only ever read. or something similar, this statement is in the same ballpark, but might not be totally correct
let x = builder.use_var(temp);
let tt = types.get(op.left as usize).unwrap();

self.sym
.table
.insert(op.left.into_symbol_init().ident.clone(), temp.as_u32());
.insert(tt.into_symbol_init().ident.clone(), temp.as_u32());
builder.def_var(temp, x);
Ok(temp)
}
Expand All @@ -122,16 +109,14 @@ impl Fir {
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let args: Vec<Value> = op
.args
.iter()
.map(|x| {
let result = self
.recurse(&x, builder, dtbl, scopes, type_tables, oir)
.unwrap();
let result = self.recurse(*x, builder, dtbl, scopes, types, oir).unwrap();
return builder.use_var(result).clone();
})
.collect::<Vec<Value>>();
Expand All @@ -148,16 +133,14 @@ impl Fir {
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let temp: Vec<Variable> = op
.exprs
.iter()
.map(|x| {
return self
.recurse(&x, builder, dtbl, scopes, type_tables, oir)
.unwrap();
return self.recurse(*x, builder, dtbl, scopes, types, oir).unwrap();
})
.collect();
Ok(*temp.last().unwrap())
Expand All @@ -172,11 +155,11 @@ impl Fir {
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let temp = self
.recurse(&op.val, builder, dtbl, scopes, type_tables, oir)
.recurse(op.val, builder, dtbl, scopes, types, oir)
.unwrap();
let arg = builder.use_var(temp);
builder.ins().return_(&[arg]);
Expand All @@ -187,7 +170,7 @@ impl Fir {
op: &SymbolAccess,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
builder: &mut FunctionBuilder,
) -> ResultFir<Variable> {
Expand Down Expand Up @@ -227,16 +210,16 @@ impl Fir {
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let left = self
.recurse(&num.left, builder, dtbl, scopes, type_tables, oir)
.recurse(num.left, builder, dtbl, scopes, types, oir)
.unwrap();
let right = self
.recurse(&num.right, builder, dtbl, scopes, type_tables, oir)
.recurse(num.right, builder, dtbl, scopes, types, oir)
.unwrap();
let arg1 = builder.use_var(left);
let arg2 = builder.use_var(right);
Expand All @@ -250,16 +233,16 @@ impl Fir {
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let left = self
.recurse(&num.left, builder, dtbl, scopes, type_tables, oir)
.recurse(num.left, builder, dtbl, scopes, types, oir)
.unwrap();
let right = self
.recurse(&num.right, builder, dtbl, scopes, type_tables, oir)
.recurse(num.right, builder, dtbl, scopes, types, oir)
.unwrap();
let arg1 = builder.use_var(left);
let arg2 = builder.use_var(right);
Expand All @@ -269,30 +252,27 @@ impl Fir {
}
pub fn recurse(
&mut self,
expr: &TypeTree,
idx: TypeTreeIndex,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let expr = types.get(idx as usize).unwrap();
match expr {
TypeTree::Block(op) => self.handle_block(&op, builder, dtbl, scopes, type_tables, oir),
TypeTree::Invoke(op) => {
self.handle_invoke(&op, builder, dtbl, scopes, type_tables, oir)
}
TypeTree::Plus(op) => self.handle_plus(&op, builder, dtbl, scopes, type_tables, oir),
TypeTree::Minus(op) => self.handle_minus(&op, builder, dtbl, scopes, type_tables, oir),
TypeTree::Return(op) => self.handle_ret(&op, builder, dtbl, scopes, type_tables, oir),
TypeTree::Block(op) => self.handle_block(&op, builder, dtbl, scopes, types, oir),
TypeTree::Invoke(op) => self.handle_invoke(&op, builder, dtbl, scopes, types, oir),
TypeTree::Plus(op) => self.handle_plus(&op, builder, dtbl, scopes, types, oir),
TypeTree::Minus(op) => self.handle_minus(&op, builder, dtbl, scopes, types, oir),
TypeTree::Return(op) => self.handle_ret(&op, builder, dtbl, scopes, types, oir),
TypeTree::ReturnVoid(_) => self.handle_ret_void(builder),
TypeTree::ConstInit(op) => {
self.handle_const_init(&op, builder, dtbl, scopes, type_tables, oir)
}
TypeTree::ArgInit(op) => {
self.handle_arg_init(&op, builder, dtbl, scopes, type_tables, oir)
self.handle_const_init(&op, builder, dtbl, scopes, types, oir)
}
TypeTree::ArgInit(op) => self.handle_arg_init(&op, builder, dtbl, scopes, types, oir),
TypeTree::SymbolAccess(op) => {
self.handle_sym_access(&op, dtbl, scopes, type_tables, oir, builder)
self.handle_sym_access(&op, dtbl, scopes, types, oir, builder)
}
TypeTree::U64(op) => self.handle_u64(*op, builder),
TypeTree::I64(op) => self.handle_i64(*op, builder),
Expand Down
6 changes: 6 additions & 0 deletions infotable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "infotable"
version = "0.1.0"
edition = "2021"

[dependencies]
14 changes: 14 additions & 0 deletions infotable/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Loading
Loading