feat: WIP

This commit is contained in:
2025-08-23 23:47:24 +08:00
parent 65bcfcb47b
commit 2fbd2a26a9
8 changed files with 284 additions and 238 deletions

View File

@@ -22,10 +22,15 @@ impl<'ctx, 'bump> DowngradeCtx<'ctx, 'bump> {
impl DowngradeCtx<'_, '_> {
fn get_ir(&self, id: ExprId) -> &RefCell<Hir> {
// SAFETY: The `ExprId` is guaranteed to be valid and correspond to an expression
// allocated within this context, making the raw index access safe.
let idx = unsafe { id.raw() } - self.ctx.lirs.len() - self.ctx.hirs.len();
if cfg!(debug_assertions) {
self.irs.get(idx).unwrap()
} else {
// SAFETY: The index calculation is guarded by the logic that creates `ExprId`s,
// ensuring it's always within the bounds of the `irs` vector in release builds.
// The debug build's `unwrap()` serves as a runtime check for this invariant.
unsafe { self.irs.get_unchecked(idx) }
}
}

View File

@@ -16,7 +16,6 @@ use petgraph::prelude::DiGraph;
use super::Context;
#[derive(Default)]
struct ValueCache(
#[cfg(debug_assertions)]
Option<Value>,
@@ -24,6 +23,19 @@ struct ValueCache(
MaybeUninit<Value>
);
impl Default for ValueCache {
fn default() -> Self {
#[cfg(debug_assertions)]
{
Self(None)
}
#[cfg(not(debug_assertions))]
{
Self(MaybeUninit::uninit())
}
}
}
impl ValueCache {
fn insert(&mut self, val: Value) {
#[cfg(debug_assertions)]
@@ -39,7 +51,9 @@ impl ValueCache {
impl Drop for ValueCache {
fn drop(&mut self) {
#[cfg(not(debug_assertions))]
self.0.assume_init_drop();
unsafe {
self.0.assume_init_drop();
}
}
}

View File

@@ -1,15 +1,10 @@
use std::cell::Cell;
use std::marker::PhantomPinned;
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;
use bumpalo::{Bump, boxed::Box};
use hashbrown::HashMap;
use itertools::Itertools;
use petgraph::{
dot::{Config, Dot},
graphmap::DiGraphMap,
};
use petgraph::graphmap::DiGraphMap;
use nixjit_builtins::{
Builtins, BuiltinsContext,
@@ -19,7 +14,7 @@ use nixjit_error::{Error, Result};
use nixjit_eval::{Args, EvalContext, Value};
use nixjit_hir::{DowngradeContext, Hir};
use nixjit_ir::{AttrSet, ExprId, Param, PrimOpId, StackIdx};
use nixjit_lir::{Lir, ResolveContext};
use nixjit_lir::Lir;
use crate::downgrade::DowngradeCtx;
use crate::eval::EvalCtx;
@@ -29,40 +24,6 @@ mod downgrade;
mod eval;
mod resolve;
#[derive(Debug)]
struct Pin<'bump, T> {
ptr: Box<'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 {
self.ptr.as_ref()
}
}
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 {
ptr: Box::new_in(x, bump),
_marker: PhantomPinned,
}
}
}
/// The main evaluation context.
///
/// This struct orchestrates the entire Nix expression evaluation process,
@@ -70,7 +31,7 @@ impl<'bump, T> Pin<'bump, T> {
pub struct Context<'bump> {
ir_count: usize,
hirs: Vec<Hir>,
lirs: Vec<Pin<'bump, Lir>>,
lirs: Vec<Box<'bump, Lir>>,
/// Maps a function's body `ExprId` to its parameter definition.
funcs: HashMap<ExprId, Param>,
@@ -112,17 +73,17 @@ impl<'bump> Context<'bump> {
.chain(scoped.iter().map(|&(_, arity, f)| (arity, f)))
.collect_array()
.unwrap();
let lirs = (0..global.len()).map(|idx| Lir::PrimOp(unsafe { PrimOpId::from_raw(idx) }))
let lirs = (0..global.len())
.map(|idx| Lir::PrimOp(unsafe { PrimOpId::from_raw(idx) }))
.chain(
(0..scoped.len())
.map(|idx| Lir::PrimOp(unsafe { PrimOpId::from_raw(idx + GLOBAL_LEN) })),
)
.chain(core::iter::once(Lir::AttrSet(AttrSet {
stcs: global.into_iter().enumerate().map(|(idx, (name, ..))| {
(name.to_string(), unsafe {
ExprId::from_raw(idx)
})
})
stcs: global
.into_iter()
.enumerate()
.map(|(idx, (name, ..))| (name.to_string(), unsafe { ExprId::from_raw(idx) }))
.chain(scoped.into_iter().enumerate().map(|(idx, (name, ..))| {
(name.to_string(), unsafe {
ExprId::from_raw(idx + GLOBAL_LEN)
@@ -137,10 +98,10 @@ impl<'bump> Context<'bump> {
.chain(core::iter::once(Lir::Thunk(unsafe {
ExprId::from_raw(GLOBAL_LEN + SCOPED_LEN)
})))
.map(|lir| Pin::new_in(lir, bump))
.collect();
.map(|lir| Box::new_in(lir, bump))
.collect_vec();
Self {
ir_count: 0,
ir_count: lirs.len(),
hirs: Vec::new(),
lirs,
funcs: HashMap::new(),
@@ -159,8 +120,8 @@ impl<'bump> Context<'bump> {
DowngradeCtx::new(self)
}
pub fn resolve_ctx<'a>(&'a mut self) -> ResolveCtx<'a, 'bump> {
ResolveCtx::new(self)
pub fn resolve_ctx<'a>(&'a mut self, root: ExprId) -> ResolveCtx<'a, 'bump> {
ResolveCtx::new(self, root)
}
pub fn eval_ctx<'a>(&'a mut self) -> EvalCtx<'a, 'bump> {
@@ -183,27 +144,19 @@ impl<'bump> Context<'bump> {
let root = self
.downgrade_ctx()
.downgrade_root(root.tree().expr().unwrap())?;
self.resolve_ctx().resolve_root(root)?;
println!(
"{:?}",
Dot::with_config(&self.graph, &[Config::EdgeNoLabel])
);
for (idx, ir) in self.lirs.iter().enumerate() {
println!("{:?} {:#?}", unsafe { ExprId::from_raw(idx) }, &ir);
}
self.resolve_ctx(root).resolve_root()?;
Ok(self.eval_ctx().eval_root(root)?.to_public())
}
pub fn add_binding(&mut self, ident: &str, expr: &str) -> Result<()> {
let root = rnix::Root::parse(expr);
if !root.errors().is_empty() {
return Err(Error::parse_error(
root.errors().iter().map(|err| err.to_string()).join("; "),
));
}
let root_expr = root.tree().expr().unwrap();
let root_expr = root
.ok()
.map_err(|err| Error::parse_error(err.to_string()))?
.expr()
.unwrap();
let expr_id = self.downgrade_ctx().downgrade_root(root_expr)?;
self.resolve_ctx().resolve_root(expr_id)?;
self.resolve_ctx(expr_id).resolve_root()?;
unsafe { self.repl_scope.as_mut() }.insert(ident.to_string(), expr_id);
Ok(())
}

View File

@@ -1,15 +1,16 @@
use std::cell::{Cell, RefCell};
use bumpalo::boxed::Box;
use derive_more::Unwrap;
use hashbrown::HashMap;
use nixjit_error::Result;
use nixjit_hir::Hir;
use nixjit_ir::{Const, ExprId, Param, PrimOpId, StackIdx};
use nixjit_ir::{Const, ExprId, Param, StackIdx};
use nixjit_lir::{Lir, LookupResult, Resolve, ResolveContext};
use replace_with::replace_with_and_return;
use super::{Context, Pin};
use super::Context;
#[derive(Clone)]
enum Scope<'ctx> {
@@ -30,18 +31,35 @@ enum Ir {
Lir(Lir),
}
struct Closure {
id: ExprId,
arg: ExprId,
deps: Cell<usize>
}
impl Closure {
fn new(id: ExprId, arg: ExprId) -> Self {
Self {
id,
arg,
deps: Cell::new(0)
}
}
}
pub struct ResolveCtx<'ctx, 'bump> {
ctx: &'ctx mut Context<'bump>,
irs: Vec<Pin<'bump, RefCell<Ir>>>,
irs: Vec<Box<'bump, RefCell<Ir>>>,
root: ExprId,
root_deps: Cell<usize>,
closures: Vec<Closure>,
scopes: Vec<Scope<'ctx>>,
has_with: bool,
with_used: bool,
closures: Vec<(ExprId, Option<ExprId>, Cell<usize>)>,
current_expr: Option<ExprId>,
}
impl<'ctx, 'bump> ResolveCtx<'ctx, 'bump> {
pub fn new(ctx: &'ctx mut Context<'bump>) -> Self {
pub fn new(ctx: &'ctx mut Context<'bump>, root: ExprId) -> Self {
Self {
scopes: vec![
Scope::Builtins(unsafe { ctx.global_scope.as_ref() }),
@@ -52,14 +70,30 @@ impl<'ctx, 'bump> ResolveCtx<'ctx, 'bump> {
irs: core::mem::take(&mut ctx.hirs)
.into_iter()
.map(|hir| Ir::Hir(hir).into())
.map(|ir| Pin::new_in(ir, ctx.bump))
.map(|ir| Box::new_in(ir, ctx.bump))
.collect(),
ctx,
root,
root_deps: Cell::new(0),
closures: Vec::new(),
current_expr: None,
}
}
pub fn resolve_root(mut self) -> Result<()> {
let ret = self.resolve(self.root);
if ret.is_ok() {
self.ctx.lirs.extend(
self.irs
.into_iter()
.map(Box::into_inner)
.map(RefCell::into_inner)
.map(Ir::unwrap_lir)
.map(|lir| Box::new_in(lir, self.ctx.bump)),
);
}
ret
}
fn get_ir(&self, id: ExprId) -> &RefCell<Ir> {
let idx = unsafe { id.raw() } - self.ctx.lirs.len();
if cfg!(debug_assertions) {
@@ -71,14 +105,13 @@ impl<'ctx, 'bump> ResolveCtx<'ctx, 'bump> {
fn new_lir(&mut self, lir: Lir) -> ExprId {
self.irs
.push(Pin::new_in(RefCell::new(Ir::Lir(lir)), self.ctx.bump));
.push(Box::new_in(RefCell::new(Ir::Lir(lir)), self.ctx.bump));
self.ctx.alloc_id()
}
}
impl ResolveContext for ResolveCtx<'_, '_> {
fn resolve(&mut self, expr: ExprId) -> Result<()> {
let prev_expr = self.current_expr.replace(expr);
let result = unsafe {
let ctx = &mut *(self as *mut Self);
let ir = self.get_ir(expr);
@@ -103,26 +136,9 @@ impl ResolveContext for ResolveCtx<'_, '_> {
},
)
};
self.current_expr = prev_expr;
result
}
fn resolve_root(mut self, expr: ExprId) -> Result<()> {
self.closures.push((expr, None, Cell::new(0)));
let ret = self.resolve(expr);
if ret.is_ok() {
self.ctx.lirs.extend(
self.irs
.into_iter()
.map(Pin::into_inner)
.map(RefCell::into_inner)
.map(Ir::unwrap_lir)
.map(|lir| crate::Pin::new_in(lir, self.ctx.bump)),
);
}
ret
}
fn lookup(&mut self, name: &str) -> LookupResult {
let mut closure_depth = 0;
// Then search from outer to inner scopes for dependencies
@@ -135,11 +151,8 @@ impl ResolveContext for ResolveCtx<'_, '_> {
}
Scope::Let(scope) | &Scope::Repl(scope) => {
if let Some(&dep) = scope.get(name) {
let (expr, _, deps) = self
.closures
.last()
.unwrap();
let idx = self.ctx.add_dep(*expr, dep, deps);
let (expr, deps) = self.closures.last().map_or_else(|| (self.root, &self.root_deps), |closure| (closure.id, &closure.deps));
let idx = self.ctx.add_dep(expr, dep, deps);
return LookupResult::Stack(idx);
}
}
@@ -149,12 +162,10 @@ impl ResolveContext for ResolveCtx<'_, '_> {
// We need to find the corresponding parameter expression to create dependency
// For now, we need to handle this case by creating a dependency to the parameter
let mut iter = self.closures.iter().rev().take(closure_depth + 1).rev();
let Some((func, Some(arg), count)) = iter.next() else {
unreachable!()
};
let Closure { id: func, arg, deps: count } = iter.next().unwrap();
let mut cur = self.ctx.add_dep(*func, *arg, count);
for (func, _, count) in iter {
self.irs.push(Pin::new_in(
for Closure { id: func, deps: count, .. } in iter {
self.irs.push(Box::new_in(
RefCell::new(Ir::Lir(Lir::StackRef(cur))),
self.ctx.bump,
));
@@ -176,10 +187,8 @@ impl ResolveContext for ResolveCtx<'_, '_> {
}
fn lookup_arg(&mut self) -> StackIdx {
let Some((func, Some(arg), count)) = self.closures.last() else {
unreachable!()
};
self.ctx.add_dep(*func, *arg, count)
let Closure { id: func, arg, deps } = self.closures.last().unwrap();
self.ctx.add_dep(*func, *arg, deps)
}
fn new_func(&mut self, body: ExprId, param: Param) {
@@ -214,7 +223,7 @@ impl ResolveContext for ResolveCtx<'_, '_> {
f: impl FnOnce(&mut Self) -> T,
) -> T {
let arg = self.new_lir(Lir::Arg(nixjit_ir::Arg));
self.closures.push((func, Some(arg), Cell::new(0)));
self.closures.push(Closure::new(func, arg));
self.scopes.push(Scope::Arg(ident));
let res = f(self);
self.scopes.pop();