diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 239168c..4184b79 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -1,5 +1,5 @@ -use std::cell::OnceCell; use core::mem::MaybeUninit; +use std::cell::OnceCell; use std::rc::Rc; use hashbrown::{HashMap, HashSet}; @@ -7,8 +7,8 @@ use priority_queue::PriorityQueue; use crate::env::Env; use crate::error::Result; -use crate::eval::jit::{JITCompiler, JITFunc}; use crate::eval::Evaluate; +use crate::eval::jit::{JITCompiler, JITFunc}; use crate::ir::{Dep, Downgraded, Ir, SccNode}; use crate::ty::internal as i; use crate::ty::public::Value; @@ -33,15 +33,22 @@ pub fn eval(downgraded: Downgraded) -> Result { downgraded.thunks, downgraded.funcs, downgraded.func_deps, - JITCompiler::new() + JITCompiler::new(), ); engine.eval(downgraded.graph) } impl Engine { - pub fn new(thunks: Box<[Ir]>, funcs: Box<[Ir]>, func_deps: Vec>, jit: JITCompiler) -> Self { + pub fn new( + thunks: Box<[Ir]>, + funcs: Box<[Ir]>, + func_deps: Vec>, + jit: JITCompiler, + ) -> Self { Self { - compiled: (0..thunks.len() + funcs.len()).map(|_| OnceCell::new()).collect(), + compiled: (0..thunks.len() + funcs.len()) + .map(|_| OnceCell::new()) + .collect(), tasks: PriorityQueue::new(), thunks, funcs, @@ -78,7 +85,7 @@ impl Engine { pub fn eval_thunk(&mut self, idx: usize, env: &mut Env) -> Result { let engine = self as *const Engine; - let func = self.compiled[idx].get_or_init(|| self.jit.compile(&self.thunks[idx], idx)); + let func = self.compiled[idx].get_or_init(|| self.jit.compile(&self.thunks[idx], idx)); let mut ret: MaybeUninit = MaybeUninit::uninit(); unsafe { func(engine, env, ret.as_mut_ptr()); @@ -88,7 +95,8 @@ impl Engine { pub fn call_func(&mut self, idx: usize, env: &mut Env) -> Result { let engine = self as *const Engine; - let func = self.compiled[idx + self.thunks.len()].get_or_init(|| self.jit.compile(&self.funcs[idx], idx)); + let func = self.compiled[idx + self.thunks.len()] + .get_or_init(|| self.jit.compile(&self.funcs[idx], idx)); let mut ret: MaybeUninit = MaybeUninit::uninit(); unsafe { func(engine, env, ret.as_mut_ptr()); @@ -97,9 +105,7 @@ impl Engine { } pub fn eval_func_deps(&mut self, idx: usize, env: &mut Env) -> Result<()> { - for (&dep, _) in - unsafe { &*(&self.func_deps[idx] as *const HashMap) }.iter() - { + for (&dep, _) in unsafe { &*(&self.func_deps[idx] as *const HashMap) }.iter() { match dep { Dep::Arg(idx) => { if let i::Value::Thunk(idx) = env.lookup_arg(idx) { diff --git a/src/engine/test.rs b/src/engine/test.rs index 6f30fb6..ca04571 100644 --- a/src/engine/test.rs +++ b/src/engine/test.rs @@ -190,11 +190,10 @@ fn test_let() { r#"let b = "c"; in { a.b = 1; } // { a."a${b}" = 2; }"#, attrs! { symbol!("a") => attrs!{ symbol!("ac") => int!(2) } }, ); - // FIXME: - /* test_expr( + test_expr( "let f = n: let a = n; f = x: a + x; in f; in f 0 1", int!(1), - ); */ + ); } #[test] diff --git a/src/eval/jit/compile.rs b/src/eval/jit/compile.rs index 36bfa99..2272729 100644 --- a/src/eval/jit/compile.rs +++ b/src/eval/jit/compile.rs @@ -298,7 +298,7 @@ impl JITCompile for Attr { match self { Str(string) => ctx.create_string(string), Dynamic(ir) => ir.compile(ctx, engine, env), - Strs(strings) => strings.compile(ctx, engine, env) + Strs(strings) => strings.compile(ctx, engine, env), } } } diff --git a/src/eval/jit/helpers.rs b/src/eval/jit/helpers.rs index 235edcc..cb28eaf 100644 --- a/src/eval/jit/helpers.rs +++ b/src/eval/jit/helpers.rs @@ -1,6 +1,6 @@ +use core::{slice, str}; use std::alloc::Layout; use std::alloc::alloc; -use core::{slice, str}; use std::mem::MaybeUninit; use std::ptr::NonNull; @@ -18,50 +18,68 @@ pub extern "C" fn helper_call( // TODO: Error Handling let args = core::ptr::slice_from_raw_parts_mut(args_ptr, args_len); let args = unsafe { Box::from_raw(args) }; - func.call( - args.into_iter().map(Value::from).collect(), - engine, - env - ) - .unwrap(); + func.call(args.into_iter().map(Value::from).collect(), engine, env) + .unwrap(); } pub extern "C" fn helper_lookup_arg(env: &Env, level: usize, ret: &mut MaybeUninit) { ret.write(env.lookup_arg(level as usize)); } -pub extern "C" fn helper_lookup(env: &Env, sym_ptr: *const u8, sym_len: usize, ret: &mut MaybeUninit) { +pub extern "C" fn helper_lookup( + env: &Env, + sym_ptr: *const u8, + sym_len: usize, + ret: &mut MaybeUninit, +) { // TODO: Error Handling unsafe { - ret.write(env - .lookup_with(str::from_utf8_unchecked(slice::from_raw_parts(sym_ptr, sym_len))) - .unwrap()); + ret.write( + env.lookup_with(str::from_utf8_unchecked(slice::from_raw_parts( + sym_ptr, sym_len, + ))) + .unwrap(), + ); } } -pub extern "C" fn helper_select(val: &mut Value, path_ptr: *mut Value, path_len: usize, engine: &mut Engine, env: &mut Env) { +pub extern "C" fn helper_select( + val: &mut Value, + path_ptr: *mut Value, + path_len: usize, + engine: &mut Engine, + env: &mut Env, +) { let path = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len); let path = unsafe { Box::from_raw(path) }; val.select(path.into_iter().map(|mut val| { val.force(engine, env)?.coerce_to_string(); Ok(val.unwrap_string()) - })).unwrap(); + })) + .unwrap(); } -pub extern "C" fn helper_select_with_default(val: &mut Value, path_ptr: *mut Value, path_len: usize, default: NonNull, engine: &mut Engine, env: &mut Env) { - let path = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len); - let path = unsafe { Box::from_raw(path) }; - val.select_with_default(path.into_iter().map(|mut val| { - val.force(engine, env)?.coerce_to_string(); - Ok(val.unwrap_string()) - }), unsafe { default.read() }).unwrap(); -} - -pub extern "C" fn helper_force( - thunk: &mut Value, +pub extern "C" fn helper_select_with_default( + val: &mut Value, + path_ptr: *mut Value, + path_len: usize, + default: NonNull, engine: &mut Engine, env: &mut Env, ) { + let path = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len); + let path = unsafe { Box::from_raw(path) }; + val.select_with_default( + path.into_iter().map(|mut val| { + val.force(engine, env)?.coerce_to_string(); + Ok(val.unwrap_string()) + }), + unsafe { default.read() }, + ) + .unwrap(); +} + +pub extern "C" fn helper_force(thunk: &mut Value, engine: &mut Engine, env: &mut Env) { thunk.force(engine, env).unwrap(); } @@ -69,9 +87,15 @@ pub extern "C" fn helper_eq(lhs: &mut Value, rhs: &Value) { lhs.eq(rhs); } -pub unsafe extern "C" fn helper_create_string(ptr: *const u8, len: usize, ret: &mut MaybeUninit) { +pub unsafe extern "C" fn helper_create_string( + ptr: *const u8, + len: usize, + ret: &mut MaybeUninit, +) { unsafe { - ret.write(Value::String(str::from_utf8_unchecked(slice::from_raw_parts(ptr, len)).to_string())); + ret.write(Value::String( + str::from_utf8_unchecked(slice::from_raw_parts(ptr, len)).to_string(), + )); } } diff --git a/src/eval/jit/mod.rs b/src/eval/jit/mod.rs index 18754ff..edefa0d 100644 --- a/src/eval/jit/mod.rs +++ b/src/eval/jit/mod.rs @@ -196,7 +196,10 @@ impl<'comp, 'ctx> JITContext<'comp, 'ctx> { .builder .ins() .stack_addr(self.compiler.ptr_type, rhs, 0); - let eq = self.compiler.module.declare_func_in_func(self.compiler.eq, self.builder.func); + let eq = self + .compiler + .module + .declare_func_in_func(self.compiler.eq, self.builder.func); self.builder.ins().call(eq, &[lhs, rhs]); } diff --git a/src/ir/ctx.rs b/src/ir/ctx.rs index 070cc4a..5479018 100644 --- a/src/ir/ctx.rs +++ b/src/ir/ctx.rs @@ -169,14 +169,16 @@ impl DowngradeContext { match this { Index::Thunk(idx) => { let len = self.thunk_deps.len(); - self.thunk_deps[idx].entry(dep.unwrap_thunk()).or_insert(len); + self.thunk_deps[idx] + .entry(dep.unwrap_thunk()) + .or_insert(len); len } Index::Func(idx) => { let len = self.thunk_deps.len(); self.func_deps[idx].entry(dep).or_insert(len); len - }, + } } } @@ -255,12 +257,12 @@ impl Downgraded { Self { graph: SccAnalyzer::new(&ctx).analyze(), func_deps: ctx.func_deps, - thunks: ctx - .thunks + thunks: ctx.thunks.into_iter().map(|(ir, _)| ir).collect(), + funcs: ctx + .funcs .into_iter() - .map(|(ir, _)| ir) + .map(|Func { body, .. }| *body) .collect(), - funcs: ctx.funcs.into_iter().map(|Func { body, .. }| *body).collect() } } }