feat: stack var (WIP)

This commit is contained in:
2025-08-09 08:12:53 +08:00
parent fd182b6233
commit d8ad7fe904
36 changed files with 1521 additions and 1058 deletions

View File

@@ -0,0 +1,61 @@
use std::cell::RefCell;
use nixjit_error::Result;
use nixjit_hir::{Downgrade, DowngradeContext, Hir};
use nixjit_ir::ExprId;
use super::Context;
pub struct DowngradeCtx<'ctx, 'bump> {
ctx: &'ctx mut Context<'bump>,
irs: Vec<RefCell<Hir>>,
}
impl<'ctx, 'bump> DowngradeCtx<'ctx, 'bump> {
pub fn new(ctx: &'ctx mut Context<'bump>) -> Self {
Self {
ctx,
irs: Vec::new(),
}
}
}
impl DowngradeCtx<'_, '_> {
fn get_ir(&self, id: ExprId) -> &RefCell<Hir> {
let idx = unsafe { id.raw() } - self.ctx.lirs.len() - self.ctx.hirs.len();
if cfg!(debug_assertions) {
self.irs.get(idx).unwrap()
} else {
unsafe { self.irs.get_unchecked(idx) }
}
}
}
impl DowngradeContext for DowngradeCtx<'_, '_> {
fn new_expr(&mut self, expr: Hir) -> ExprId {
self.irs.push(expr.into());
unsafe { ExprId::from_raw(self.ctx.lirs.len() + self.ctx.hirs.len() + self.irs.len() - 1) }
}
fn with_expr_mut<T>(&mut self, id: ExprId, f: impl FnOnce(&mut Hir, &mut Self) -> T) -> T {
unsafe {
let self_mut = &mut *(self as *mut Self);
f(&mut self.get_ir(id).borrow_mut(), self_mut)
}
}
fn downgrade_root(mut self, root: rnix::ast::Expr) -> Result<ExprId> {
let id = root.downgrade(&mut self)?;
self.ctx
.hirs
.extend(self.irs.into_iter().map(RefCell::into_inner));
for (idx, ir) in self.ctx.hirs.iter().enumerate() {
println!(
"{:?} {:#?}",
unsafe { ExprId::from_raw(idx + self.ctx.lirs.len()) },
&ir
);
}
Ok(id)
}
}