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 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<Value> {
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<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 {
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,
@@ -88,7 +95,8 @@ impl Engine {
pub fn call_func(&mut self, idx: usize, env: &mut Env) -> Result<i::Value> {
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();
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<Dep, usize>) }.iter()
{
for (&dep, _) in unsafe { &*(&self.func_deps[idx] as *const HashMap<Dep, usize>) }.iter() {
match dep {
Dep::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; }"#,
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]

View File

@@ -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),
}
}
}

View File

@@ -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,11 +18,7 @@ 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
)
func.call(args.into_iter().map(Value::from).collect(), engine, env)
.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));
}
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
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<Value>, 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<Value>,
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<Value>) {
pub unsafe extern "C" fn helper_create_string(
ptr: *const u8,
len: usize,
ret: &mut MaybeUninit<Value>,
) {
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
.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]);
}

View File

@@ -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()
}
}
}