chore: cargo fmt

This commit is contained in:
2025-05-28 21:59:45 +08:00
parent 99dce2e778
commit f3bf44ab97
3 changed files with 48 additions and 20 deletions

View File

@@ -103,7 +103,12 @@ impl<'ctx> Helpers<'ctx> {
let call = module.add_function( let call = module.add_function(
"call", "call",
value_type.fn_type( value_type.fn_type(
&[value_type.into(), value_type.into(), ptr_type.into(), ptr_type.into()], &[
value_type.into(),
value_type.into(),
ptr_type.into(),
ptr_type.into(),
],
false, false,
), ),
None, None,
@@ -115,7 +120,15 @@ impl<'ctx> Helpers<'ctx> {
); );
let force = module.add_function( let force = module.add_function(
"force", "force",
value_type.fn_type(&[value_type.into(), ptr_type.into(), ptr_type.into(), ptr_type.into()], false), value_type.fn_type(
&[
value_type.into(),
ptr_type.into(),
ptr_type.into(),
ptr_type.into(),
],
false,
),
None, None,
); );
@@ -341,7 +354,12 @@ extern "C" fn helper_lookup(sym: usize, env: *const VmEnv) -> JITValue {
val val
} }
extern "C" fn helper_force<'gc>(thunk: JITValue, vm: *const VM<'gc>, mc: *const Mutation<'gc>, jit: *const JITContext<'gc>) -> JITValue { extern "C" fn helper_force<'gc>(
thunk: JITValue,
vm: *const VM<'gc>,
mc: *const Mutation<'gc>,
jit: *const JITContext<'gc>,
) -> JITValue {
if !matches!(thunk.tag, ValueTag::Thunk) { if !matches!(thunk.tag, ValueTag::Thunk) {
return thunk; return thunk;
} }
@@ -350,10 +368,13 @@ extern "C" fn helper_force<'gc>(thunk: JITValue, vm: *const VM<'gc>, mc: *const
let mc = unsafe { mc.as_ref() }.unwrap(); let mc = unsafe { mc.as_ref() }.unwrap();
let thunk = Value::from(thunk).unwrap_thunk(); let thunk = Value::from(thunk).unwrap_thunk();
if let Some(val) = thunk.get_value() { if let Some(val) = thunk.get_value() {
return val.into() return val.into();
} }
let (opcodes, env) = thunk.suspend(mc).unwrap(); let (opcodes, env) = thunk.suspend(mc).unwrap();
let func = unsafe { jit.as_ref() }.unwrap().compile_seq(opcodes.iter().copied(), vm).unwrap(); let func = unsafe { jit.as_ref() }
.unwrap()
.compile_seq(opcodes.iter().copied(), vm)
.unwrap();
let val = unsafe { func.call(env.as_ref() as *const _, mc as *const _) }; let val = unsafe { func.call(env.as_ref() as *const _, mc as *const _) };
thunk.insert_value(val.into(), mc); thunk.insert_value(val.into(), mc);
val val

View File

@@ -427,7 +427,7 @@ impl<'gc> Value<'gc> {
(Value::String(a), Value::String(b)) => { (Value::String(a), Value::String(b)) => {
let mut a = a.clone(); let mut a = a.clone();
a.make_mut(mc).push_str(b.as_str()) a.make_mut(mc).push_str(b.as_str())
}, }
(_, Value::Catchable(_)) => *self = other, (_, Value::Catchable(_)) => *self = other,
(Value::Catchable(_), _) => (), (Value::Catchable(_), _) => (),
_ => todo!(), _ => todo!(),
@@ -608,7 +608,7 @@ impl<'gc> Thunk<'gc> {
let _Thunk::Code(opcodes, env) = let _Thunk::Code(opcodes, env) =
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended) std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended)
else { else {
return Err(Error::EvalError("infinite recursion occured".into())) return Err(Error::EvalError("infinite recursion occured".into()));
}; };
Ok((opcodes, env.unwrap())) Ok((opcodes, env.unwrap()))
} }

View File

@@ -34,7 +34,9 @@ pub fn run(mut prog: Program) -> Result<p::Value> {
let mut thunks = std::mem::take(&mut prog.thunks); let mut thunks = std::mem::take(&mut prog.thunks);
thunks.iter_mut().for_each(|code| code.reverse()); thunks.iter_mut().for_each(|code| code.reverse());
let mut funcs = std::mem::take(&mut prog.funcs); let mut funcs = std::mem::take(&mut prog.funcs);
funcs.iter_mut().for_each(|F { opcodes, .. }| opcodes.reverse()); funcs
.iter_mut()
.for_each(|F { opcodes, .. }| opcodes.reverse());
let symbols = std::mem::take(&mut prog.symbols); let symbols = std::mem::take(&mut prog.symbols);
let symmap = std::mem::take(&mut prog.symmap); let symmap = std::mem::take(&mut prog.symmap);
let consts = std::mem::take(&mut prog.consts); let consts = std::mem::take(&mut prog.consts);
@@ -133,8 +135,13 @@ pub fn eval<T, F: for<'gc> FnOnce(Value<'gc>, &mut GcRoot<'gc>, &Mutation<'gc>)
let count = func.count.get(); let count = func.count.get();
func.count.set(count + 1); func.count.set(count + 1);
if count >= 1 { if count >= 1 {
let compiled = func.compiled.borrow_mut(mc).get_or_insert_with(|| root.vm.compile_func(func.func)).clone(); let compiled = func
let ret = unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) }; .compiled
.borrow_mut(mc)
.get_or_insert_with(|| root.vm.compile_func(func.func))
.clone();
let ret =
unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) };
root.stack.push(ret.into())?; root.stack.push(ret.into())?;
} else { } else {
root.envs.push(env); root.envs.push(env);
@@ -173,15 +180,13 @@ fn single_op<'gc, const CAP: usize>(
) -> Result<Consq> { ) -> Result<Consq> {
match opcode { match opcode {
OpCode::Illegal => panic!("illegal opcode"), OpCode::Illegal => panic!("illegal opcode"),
OpCode::Const { idx } => { OpCode::Const { idx } => stack.push(match vm.get_const(idx) {
stack.push(match vm.get_const(idx) {
Const::Int(x) => Value::Int(x), Const::Int(x) => Value::Int(x),
Const::Float(x) => Value::Float(x), Const::Float(x) => Value::Float(x),
Const::Bool(x) => Value::Bool(x), Const::Bool(x) => Value::Bool(x),
Const::String(x) => Value::String(CoW::new(x.into(), mc)), Const::String(x) => Value::String(CoW::new(x.into(), mc)),
Const::Null => Value::Null Const::Null => Value::Null,
})? })?,
},
OpCode::LoadThunk { idx } => stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?, OpCode::LoadThunk { idx } => stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?,
OpCode::LoadValue { idx } => { OpCode::LoadValue { idx } => {
stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?; stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?;
@@ -415,6 +420,8 @@ impl<'gc> VM<'gc> {
} }
pub fn compile_func(&'gc self, func: &'gc F) -> JITFunc<'gc> { pub fn compile_func(&'gc self, func: &'gc F) -> JITFunc<'gc> {
self.jit.compile_seq(func.opcodes.iter().copied(), self).unwrap() self.jit
.compile_seq(func.opcodes.iter().copied(), self)
.unwrap()
} }
} }