refactor: reduce coupling
This commit is contained in:
145
evaluator/nixjit_context/src/lib.rs
Normal file
145
evaluator/nixjit_context/src/lib.rs
Normal 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!()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user