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

@@ -8,6 +8,7 @@ use nixjit_eval::{Args, EvalContext, Evaluate, StackFrame, Value};
use nixjit_ir::ExprId;
use nixjit_jit::JITContext;
use nixjit_lir::Lir;
use petgraph::visit::{Topo, Walker};
use super::Context;
@@ -40,10 +41,7 @@ impl<'ctx, 'bump> EvalCtx<'ctx, 'bump> {
dbg!(&deps, &self.stack);
for (dep, idx) in deps {
unsafe {
if matches!(
&**self.ctx.lirs.get_unchecked(dep.raw()),
Lir::Arg(_)
) {
if matches!(&**self.ctx.lirs.get_unchecked(dep.raw()), Lir::Arg(_)) {
*frame.get_unchecked_mut(idx.raw()) = arg.as_ref().unwrap().clone();
continue;
}
@@ -76,7 +74,7 @@ impl EvalContext for EvalCtx<'_, '_> {
self.stack.push(frame);
if let Err(err) = self.eval_deps(func, arg) {
self.stack.pop();
return Err(err)
return Err(err);
}
let ret = self.eval(func);
self.stack.pop();

View File

@@ -1,4 +1,4 @@
use std::{marker::PhantomPinned, ops::Deref};
use std::{marker::PhantomPinned, ops::{Deref, DerefMut}};
use bumpalo::{Bump, boxed::Box};
use hashbrown::HashMap;
@@ -32,6 +32,12 @@ struct Pin<'bump, T> {
_marker: PhantomPinned,
}
impl<T> Pin<'_, T> {
fn into_inner(self) -> T {
Box::into_inner(self.ptr)
}
}
impl<T> Deref for Pin<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
@@ -39,6 +45,12 @@ impl<T> Deref for Pin<'_, T> {
}
}
impl<T> DerefMut for Pin<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ptr.as_mut()
}
}
impl<'bump, T> Pin<'bump, T> {
fn new_in(x: T, bump: &'bump Bump) -> Self {
Self {

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>,