chore: cargo fmt

This commit is contained in:
2025-07-13 19:27:43 +08:00
parent 4b567ab022
commit f6ae509c13
6 changed files with 81 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
use std::cell::OnceCell;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use std::cell::OnceCell;
use std::rc::Rc; use std::rc::Rc;
use hashbrown::{HashMap, HashSet}; use hashbrown::{HashMap, HashSet};
@@ -7,8 +7,8 @@ use priority_queue::PriorityQueue;
use crate::env::Env; use crate::env::Env;
use crate::error::Result; use crate::error::Result;
use crate::eval::jit::{JITCompiler, JITFunc};
use crate::eval::Evaluate; use crate::eval::Evaluate;
use crate::eval::jit::{JITCompiler, JITFunc};
use crate::ir::{Dep, Downgraded, Ir, SccNode}; use crate::ir::{Dep, Downgraded, Ir, SccNode};
use crate::ty::internal as i; use crate::ty::internal as i;
use crate::ty::public::Value; use crate::ty::public::Value;
@@ -33,15 +33,22 @@ pub fn eval(downgraded: Downgraded) -> Result<Value> {
downgraded.thunks, downgraded.thunks,
downgraded.funcs, downgraded.funcs,
downgraded.func_deps, downgraded.func_deps,
JITCompiler::new() JITCompiler::new(),
); );
engine.eval(downgraded.graph) engine.eval(downgraded.graph)
} }
impl Engine { impl Engine {
pub fn new(thunks: Box<[Ir]>, funcs: Box<[Ir]>, func_deps: Vec<HashMap<Dep, usize>>, jit: JITCompiler) -> Self { pub fn new(
thunks: Box<[Ir]>,
funcs: Box<[Ir]>,
func_deps: Vec<HashMap<Dep, usize>>,
jit: JITCompiler,
) -> Self {
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(), tasks: PriorityQueue::new(),
thunks, thunks,
funcs, funcs,
@@ -88,7 +95,8 @@ impl Engine {
pub fn call_func(&mut self, idx: usize, env: &mut Env) -> Result<i::Value> { pub fn call_func(&mut self, idx: usize, env: &mut Env) -> Result<i::Value> {
let engine = self as *const Engine; 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<i::Value> = MaybeUninit::uninit(); let mut ret: MaybeUninit<i::Value> = MaybeUninit::uninit();
unsafe { unsafe {
func(engine, env, ret.as_mut_ptr()); 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<()> { pub fn eval_func_deps(&mut self, idx: usize, env: &mut Env) -> Result<()> {
for (&dep, _) in for (&dep, _) in unsafe { &*(&self.func_deps[idx] as *const HashMap<Dep, usize>) }.iter() {
unsafe { &*(&self.func_deps[idx] as *const HashMap<Dep, usize>) }.iter()
{
match dep { match dep {
Dep::Arg(idx) => { Dep::Arg(idx) => {
if let i::Value::Thunk(idx) = env.lookup_arg(idx) { if let i::Value::Thunk(idx) = env.lookup_arg(idx) {

View File

@@ -190,11 +190,10 @@ fn test_let() {
r#"let b = "c"; in { a.b = 1; } // { a."a${b}" = 2; }"#, r#"let b = "c"; in { a.b = 1; } // { a."a${b}" = 2; }"#,
attrs! { symbol!("a") => attrs!{ symbol!("ac") => int!(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", "let f = n: let a = n; f = x: a + x; in f; in f 0 1",
int!(1), int!(1),
); */ );
} }
#[test] #[test]

View File

@@ -298,7 +298,7 @@ impl JITCompile for Attr {
match self { match self {
Str(string) => ctx.create_string(string), Str(string) => ctx.create_string(string),
Dynamic(ir) => ir.compile(ctx, engine, env), Dynamic(ir) => ir.compile(ctx, engine, env),
Strs(strings) => strings.compile(ctx, engine, env) Strs(strings) => strings.compile(ctx, engine, env),
} }
} }
} }

View File

@@ -1,6 +1,6 @@
use core::{slice, str};
use std::alloc::Layout; use std::alloc::Layout;
use std::alloc::alloc; use std::alloc::alloc;
use core::{slice, str};
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::ptr::NonNull; use std::ptr::NonNull;
@@ -18,11 +18,7 @@ pub extern "C" fn helper_call(
// TODO: Error Handling // TODO: Error Handling
let args = core::ptr::slice_from_raw_parts_mut(args_ptr, args_len); let args = core::ptr::slice_from_raw_parts_mut(args_ptr, args_len);
let args = unsafe { Box::from_raw(args) }; let args = unsafe { Box::from_raw(args) };
func.call( func.call(args.into_iter().map(Value::from).collect(), engine, env)
args.into_iter().map(Value::from).collect(),
engine,
env
)
.unwrap(); .unwrap();
} }
@@ -30,38 +26,60 @@ pub extern "C" fn helper_lookup_arg(env: &Env, level: usize, ret: &mut MaybeUnin
ret.write(env.lookup_arg(level as usize)); 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<Value>) { pub extern "C" fn helper_lookup(
env: &Env,
sym_ptr: *const u8,
sym_len: usize,
ret: &mut MaybeUninit<Value>,
) {
// TODO: Error Handling // TODO: Error Handling
unsafe { unsafe {
ret.write(env ret.write(
.lookup_with(str::from_utf8_unchecked(slice::from_raw_parts(sym_ptr, sym_len))) env.lookup_with(str::from_utf8_unchecked(slice::from_raw_parts(
.unwrap()); 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 = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len);
let path = unsafe { Box::from_raw(path) }; let path = unsafe { Box::from_raw(path) };
val.select(path.into_iter().map(|mut val| { val.select(path.into_iter().map(|mut val| {
val.force(engine, env)?.coerce_to_string(); val.force(engine, env)?.coerce_to_string();
Ok(val.unwrap_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<Value>, engine: &mut Engine, env: &mut Env) { pub extern "C" fn helper_select_with_default(
let path = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len); val: &mut Value,
let path = unsafe { Box::from_raw(path) }; path_ptr: *mut Value,
val.select_with_default(path.into_iter().map(|mut val| { path_len: usize,
val.force(engine, env)?.coerce_to_string(); default: NonNull<Value>,
Ok(val.unwrap_string())
}), unsafe { default.read() }).unwrap();
}
pub extern "C" fn helper_force(
thunk: &mut Value,
engine: &mut Engine, engine: &mut Engine,
env: &mut Env, 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(); thunk.force(engine, env).unwrap();
} }
@@ -69,9 +87,15 @@ pub extern "C" fn helper_eq(lhs: &mut Value, rhs: &Value) {
lhs.eq(rhs); lhs.eq(rhs);
} }
pub unsafe extern "C" fn helper_create_string(ptr: *const u8, len: usize, ret: &mut MaybeUninit<Value>) { pub unsafe extern "C" fn helper_create_string(
ptr: *const u8,
len: usize,
ret: &mut MaybeUninit<Value>,
) {
unsafe { 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(),
));
} }
} }

View File

@@ -196,7 +196,10 @@ impl<'comp, 'ctx> JITContext<'comp, 'ctx> {
.builder .builder
.ins() .ins()
.stack_addr(self.compiler.ptr_type, rhs, 0); .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]); self.builder.ins().call(eq, &[lhs, rhs]);
} }

View File

@@ -169,14 +169,16 @@ impl DowngradeContext {
match this { match this {
Index::Thunk(idx) => { Index::Thunk(idx) => {
let len = self.thunk_deps.len(); 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 len
} }
Index::Func(idx) => { Index::Func(idx) => {
let len = self.thunk_deps.len(); let len = self.thunk_deps.len();
self.func_deps[idx].entry(dep).or_insert(len); self.func_deps[idx].entry(dep).or_insert(len);
len len
}, }
} }
} }
@@ -255,12 +257,12 @@ impl Downgraded {
Self { Self {
graph: SccAnalyzer::new(&ctx).analyze(), graph: SccAnalyzer::new(&ctx).analyze(),
func_deps: ctx.func_deps, func_deps: ctx.func_deps,
thunks: ctx thunks: ctx.thunks.into_iter().map(|(ir, _)| ir).collect(),
.thunks funcs: ctx
.funcs
.into_iter() .into_iter()
.map(|(ir, _)| ir) .map(|Func { body, .. }| *body)
.collect(), .collect(),
funcs: ctx.funcs.into_iter().map(|Func { body, .. }| *body).collect()
} }
} }
} }