refactor: reduce coupling

This commit is contained in:
2025-07-28 21:37:27 +08:00
parent 78e3c5a26e
commit 7afb2a7b1c
53 changed files with 2964 additions and 3444 deletions

View File

@@ -0,0 +1,21 @@
[package]
name = "nixjit_context"
version = "0.1.0"
edition = "2024"
[dependencies]
hashbrown = "0.15"
itertools = "0.14"
cranelift = "0.122"
cranelift-module = "0.122"
cranelift-jit = "0.122"
cranelift-native = "0.122"
nixjit_error = { path = "../nixjit_error" }
nixjit_eval = { path = "../nixjit_eval" }
nixjit_hir = { path = "../nixjit_hir" }
nixjit_ir = { path = "../nixjit_ir" }
nixjit_jit = { path = "../nixjit_jit" }
nixjit_lir = { path = "../nixjit_lir" }
nixjit_value = { path = "../nixjit_value" }

View File

@@ -0,0 +1,145 @@
use core::mem::MaybeUninit;
use std::cell::{OnceCell, RefCell};
use hashbrown::{HashMap, HashSet};
use itertools::Itertools;
use nixjit_error::Result;
use nixjit_eval::EvalContext;
use nixjit_hir::{DowngradeContext, Hir};
use nixjit_ir::{ExprId, Param};
use nixjit_lir::{Lir, ResolveContext};
use nixjit_value::Value;
use nixjit_jit::{JITCompiler, JITContext, JITFunc};
#[derive(Default)]
pub struct Context {
hirs: Vec<RefCell<Hir>>,
lirs: Vec<MaybeUninit<Lir>>,
jit: JITCompiler<Self>,
compiled: Vec<OnceCell<JITFunc<Self>>>,
}
impl Drop for Context {
fn drop(&mut self) {
unsafe {
self.lirs.iter_mut().for_each(|lir| lir.assume_init_drop());
}
}
}
impl Context {
pub fn new() -> Self {
Self::default()
}
}
impl DowngradeContext for Context {
fn new_expr(&mut self, expr: Hir) -> ExprId {
let id = unsafe { core::mem::transmute(self.hirs.len() + self.lirs.len()) };
self.hirs.push(expr.into());
id
}
fn with_expr<T>(&self, id: ExprId, f: impl FnOnce(&Hir, &Self) -> T) -> T {
unsafe {
let idx: usize = core::mem::transmute(id);
f(&self.hirs.get_unchecked(idx).borrow(), self)
}
}
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 Context);
let idx: usize = core::mem::transmute(id);
f(&mut self.hirs.get_unchecked_mut(idx).borrow_mut(), self_mut)
}
}
}
impl ResolveContext for Context {
fn lookup(&self, name: &str) -> nixjit_lir::LookupResult {
todo!()
}
fn new_dep(&mut self, expr: ExprId, dep: ExprId) {
todo!()
}
fn resolve(&mut self, expr: ExprId) -> Result<()> {
todo!()
}
fn new_func(&mut self, body: ExprId, param: Param) {
todo!()
}
fn with_let_env<'a, T>(
&mut self,
bindings: impl IntoIterator<Item = (&'a String, &'a ExprId)>,
f: impl FnOnce(&mut Self) -> T,
) -> T {
todo!()
}
fn with_with_env<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> (bool, T) {
todo!()
}
fn with_param_env<'a, T>(
&mut self,
ident: Option<&'a str>,
f: impl FnOnce(&mut Self) -> T,
) -> T {
todo!()
}
}
impl EvalContext for Context {
fn eval(&mut self, expr: ExprId) -> Result<nixjit_eval::Value<Self>>
where
Self: Sized,
{
todo!()
}
fn pop_frame(&mut self) -> Vec<nixjit_eval::Value<Self>>
where
Self: Sized,
{
todo!()
}
fn lookup_with<'a>(&'a self, ident: &str) -> Option<&'a nixjit_eval::Value<Self>>
where
Self: Sized,
{
todo!()
}
fn with_with_env<T>(
&mut self,
namespace: std::rc::Rc<HashMap<String, nixjit_eval::Value<Self>>>,
f: impl FnOnce(&mut Self) -> T,
) -> T
where
Self: Sized,
{
todo!()
}
fn with_args_env<T>(
&mut self,
args: Vec<nixjit_eval::Value<Self>>,
f: impl FnOnce(&mut Self) -> T,
) -> (Vec<nixjit_eval::Value<Self>>, T)
where
Self: Sized,
{
todo!()
}
}
impl JITContext for Context {
fn lookup_arg(&self, offset: usize) -> &nixjit_eval::Value<Self> {
todo!()
}
fn lookup_stack(&self, offset: usize) -> &nixjit_eval::Value<Self> {
todo!()
}
fn enter_with(&mut self, namespace: std::rc::Rc<HashMap<String, nixjit_eval::Value<Self>>>) {
todo!()
}
fn exit_with(&mut self) {
todo!()
}
}