feat: graph (WIP)

This commit is contained in:
2025-08-15 12:38:11 +08:00
parent d8ad7fe904
commit 3c7722a3d2
7 changed files with 78 additions and 30 deletions

View File

@@ -1,7 +1,5 @@
use std::cell::RefCell;
use std::pin::Pin;
use bumpalo::boxed::Box;
use derive_more::Unwrap;
use hashbrown::HashMap;
@@ -11,7 +9,7 @@ use nixjit_ir::{Const, ExprId, Param, StackIdx};
use nixjit_lir::{Lir, LookupResult, Resolve, ResolveContext};
use replace_with::replace_with_and_return;
use super::Context;
use super::{Context, Pin};
#[derive(Clone)]
enum Scope<'ctx> {
@@ -20,7 +18,7 @@ enum Scope<'ctx> {
/// A function argument scope. `Some` holds the name of the argument set if present.
Arg(Option<String>),
Builtins(&'ctx HashMap<&'static str, ExprId>),
Repl(&'ctx HashMap<String, ExprId>)
Repl(&'ctx HashMap<String, ExprId>),
}
/// Represents an expression at different stages of compilation.
@@ -44,7 +42,7 @@ impl Ir {
pub struct ResolveCtx<'ctx, 'bump> {
ctx: &'ctx mut Context<'bump>,
irs: Vec<Pin<Box<'bump, RefCell<Ir>>>>,
irs: Vec<Pin<'bump, RefCell<Ir>>>,
scopes: Vec<Scope<'ctx>>,
has_with: bool,
with_used: bool,
@@ -58,15 +56,14 @@ impl<'ctx, 'bump> ResolveCtx<'ctx, 'bump> {
Self {
scopes: vec![
Scope::Builtins(&ctx.global_scope),
Scope::Repl(&ctx.repl_scope)
Scope::Repl(&ctx.repl_scope),
],
has_with: false,
with_used: false,
irs: core::mem::take(&mut ctx.hirs)
.into_iter()
.map(|hir| Ir::Hir(hir).into())
.map(|ir| Box::new_in(ir, ctx.bump))
.map(Pin::new)
.map(|ir| Pin::new_in(ir, ctx.bump))
.collect(),
ctx: ctx_mut,
closures: Vec::new(),
@@ -105,10 +102,10 @@ impl<'ctx, 'bump> ResolveCtx<'ctx, 'bump> {
}
fn new_lir(&mut self, lir: Lir) -> ExprId {
self.irs.push(Pin::new(Box::new_in(
self.irs.push(Pin::new_in(
RefCell::new(Ir::Lir(lir)),
self.ctx.bump,
)));
));
unsafe { ExprId::from_raw(self.ctx.lirs.len() + self.irs.len() - 1) }
}
}
@@ -144,6 +141,10 @@ impl ResolveContext for ResolveCtx<'_, '_> {
result
}
fn resolve_call(&mut self, func: ExprId, arg: ExprId) -> Result<()> {
todo!()
}
fn resolve_root(mut self, expr: ExprId) -> Result<()> {
self.closures.push((expr, None, 0));
let ret = self.resolve(expr);
@@ -151,11 +152,10 @@ impl ResolveContext for ResolveCtx<'_, '_> {
self.ctx.lirs.extend(
self.irs
.into_iter()
.map(|pin| unsafe { core::mem::transmute::<Pin<_>, Box<_>>(pin) })
.map(Box::into_inner)
.map(Pin::into_inner)
.map(RefCell::into_inner)
.map(Ir::unwrap_lir)
.map(|lir| crate::Pin::new_in(lir, self.ctx.bump))
.map(|lir| crate::Pin::new_in(lir, self.ctx.bump)),
);
}
ret
@@ -215,7 +215,9 @@ impl ResolveContext for ResolveCtx<'_, '_> {
}
fn lookup_arg(&mut self) -> StackIdx {
let Some((func, Some(arg), count)) = unsafe { &mut *(self as *mut Self) }.closures.last_mut() else {
let Some((func, Some(arg), count)) =
unsafe { &mut *(self as *mut Self) }.closures.last_mut()
else {
unreachable!()
};
self.add_dep(*func, *arg, count)
@@ -246,7 +248,7 @@ impl ResolveContext for ResolveCtx<'_, '_> {
(core::mem::replace(&mut self.with_used, with_used), res)
}
fn with_param_env<T>(
fn with_closure_env<T>(
&mut self,
func: ExprId,
ident: Option<String>,