feat: a lot

This commit is contained in:
2025-08-06 18:30:19 +08:00
parent 32c602f21c
commit f946cb2fd1
22 changed files with 735 additions and 591 deletions

View File

@@ -88,10 +88,10 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let float_block = ctx.builder.create_block();
let float_check_block = ctx.builder.create_block();
let is_int =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::INT as i64);
let is_int = ctx
.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::INT as i64);
ctx.builder
.ins()
.brif(is_int, int_block, [], float_check_block, []);
@@ -109,7 +109,7 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let is_float =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::FLOAT as i64);
.icmp_imm(IntCC::Equal, lhs_tag, Value::FLOAT as i64);
ctx.builder
.ins()
.brif(is_float, float_block, [], default_block, []);
@@ -141,10 +141,10 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let float_block = ctx.builder.create_block();
let float_check_block = ctx.builder.create_block();
let is_int =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::INT as i64);
let is_int = ctx
.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::INT as i64);
ctx.builder
.ins()
.brif(is_int, int_block, [], float_check_block, []);
@@ -162,7 +162,7 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let is_float =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::FLOAT as i64);
.icmp_imm(IntCC::Equal, lhs_tag, Value::FLOAT as i64);
ctx.builder
.ins()
.brif(is_float, float_block, [], default_block, []);
@@ -194,10 +194,10 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let float_block = ctx.builder.create_block();
let float_check_block = ctx.builder.create_block();
let is_int =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::INT as i64);
let is_int = ctx
.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::INT as i64);
ctx.builder
.ins()
.brif(is_int, int_block, [], float_check_block, []);
@@ -215,7 +215,7 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let is_float =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::FLOAT as i64);
.icmp_imm(IntCC::Equal, lhs_tag, Value::FLOAT as i64);
ctx.builder
.ins()
.brif(is_float, float_block, [], default_block, []);
@@ -245,10 +245,10 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let bool_block = ctx.builder.create_block();
let non_bool_block = ctx.builder.create_block();
let is_bool =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::BOOL as i64);
let is_bool = ctx
.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::BOOL as i64);
ctx.builder
.ins()
.brif(is_bool, bool_block, [], non_bool_block, []);
@@ -275,10 +275,10 @@ impl<Ctx: JITContext> JITCompile<Ctx> for BinOp {
let bool_block = ctx.builder.create_block();
let non_bool_block = ctx.builder.create_block();
let is_bool =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::<Ctx>::BOOL as i64);
let is_bool = ctx
.builder
.ins()
.icmp_imm(IntCC::Equal, lhs_tag, Value::BOOL as i64);
ctx.builder
.ins()
.brif(is_bool, bool_block, [], non_bool_block, []);
@@ -378,10 +378,10 @@ impl<Ctx: JITContext> JITCompile<Ctx> for If {
let judge_block = ctx.builder.create_block();
let slot = ctx.alloca();
let is_bool =
ctx.builder
.ins()
.icmp_imm(IntCC::Equal, cond_type, Value::<Ctx>::BOOL as i64);
let is_bool = ctx
.builder
.ins()
.icmp_imm(IntCC::Equal, cond_type, Value::BOOL as i64);
ctx.builder
.ins()
.brif(is_bool, judge_block, [], error_block, []);
@@ -480,37 +480,25 @@ impl<Ctx: JITContext> JITCompile<Ctx> for Const {
let slot = ctx.alloca();
match self.val {
Bool(x) => {
let tag = ctx
.builder
.ins()
.iconst(types::I64, Value::<Ctx>::BOOL as i64);
let tag = ctx.builder.ins().iconst(types::I64, Value::BOOL as i64);
let val = ctx.builder.ins().iconst(types::I64, x as i64);
ctx.builder.ins().stack_store(tag, slot, 0);
ctx.builder.ins().stack_store(val, slot, 8);
}
Int(x) => {
let tag = ctx
.builder
.ins()
.iconst(types::I64, Value::<Ctx>::INT as i64);
let tag = ctx.builder.ins().iconst(types::I64, Value::INT as i64);
let val = ctx.builder.ins().iconst(types::I64, x);
ctx.builder.ins().stack_store(tag, slot, 0);
ctx.builder.ins().stack_store(val, slot, 8);
}
Float(x) => {
let tag = ctx
.builder
.ins()
.iconst(types::I64, Value::<Ctx>::FLOAT as i64);
let tag = ctx.builder.ins().iconst(types::I64, Value::FLOAT as i64);
let val = ctx.builder.ins().f64const(x);
ctx.builder.ins().stack_store(tag, slot, 0);
ctx.builder.ins().stack_store(val, slot, 8);
}
Null => {
let tag = ctx
.builder
.ins()
.iconst(types::I64, Value::<Ctx>::NULL as i64);
let tag = ctx.builder.ins().iconst(types::I64, Value::NULL as i64);
ctx.builder.ins().stack_store(tag, slot, 0);
}
}

View File

@@ -11,21 +11,21 @@ use nixjit_eval::{AttrSet, EvalContext, List, Value};
use super::JITContext;
pub extern "C" fn helper_call<Ctx: JITContext>(
func: &mut Value<Ctx>,
args_ptr: *mut Value<Ctx>,
func: &mut Value,
args_ptr: *mut Value,
args_len: usize,
ctx: &mut Ctx,
) {
// 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().collect(), ctx).unwrap();
func.call(args.into_iter().map(Ok), ctx).unwrap();
}
pub extern "C" fn helper_lookup_stack<Ctx: JITContext>(
ctx: &Ctx,
offset: usize,
ret: &mut MaybeUninit<Value<Ctx>>,
ret: &mut MaybeUninit<Value>,
) {
ret.write(ctx.lookup_stack(offset).clone());
}
@@ -33,7 +33,7 @@ pub extern "C" fn helper_lookup_stack<Ctx: JITContext>(
pub extern "C" fn helper_lookup_arg<Ctx: JITContext>(
ctx: &Ctx,
offset: usize,
ret: &mut MaybeUninit<Value<Ctx>>,
ret: &mut MaybeUninit<Value>,
) {
ret.write(JITContext::lookup_arg(ctx, offset).clone());
}
@@ -42,7 +42,7 @@ pub extern "C" fn helper_lookup<Ctx: JITContext>(
ctx: &Ctx,
sym_ptr: *const u8,
sym_len: usize,
ret: &mut MaybeUninit<Value<Ctx>>,
ret: &mut MaybeUninit<Value>,
) {
// TODO: Error Handling
unsafe {
@@ -57,8 +57,8 @@ pub extern "C" fn helper_lookup<Ctx: JITContext>(
}
pub extern "C" fn helper_select<Ctx: JITContext>(
val: &mut Value<Ctx>,
path_ptr: *mut Value<Ctx>,
val: &mut Value,
path_ptr: *mut Value,
path_len: usize,
) {
let path = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len);
@@ -71,10 +71,10 @@ pub extern "C" fn helper_select<Ctx: JITContext>(
}
pub extern "C" fn helper_select_with_default<Ctx: JITContext>(
val: &mut Value<Ctx>,
path_ptr: *mut Value<Ctx>,
val: &mut Value,
path_ptr: *mut Value,
path_len: usize,
default: NonNull<Value<Ctx>>,
default: NonNull<Value>,
) {
let path = core::ptr::slice_from_raw_parts_mut(path_ptr, path_len);
let path = unsafe { Box::from_raw(path) };
@@ -88,14 +88,14 @@ pub extern "C" fn helper_select_with_default<Ctx: JITContext>(
.unwrap();
}
pub extern "C" fn helper_eq<Ctx: JITContext>(lhs: &mut Value<Ctx>, rhs: &Value<Ctx>) {
pub extern "C" fn helper_eq<Ctx: JITContext>(lhs: &mut Value, rhs: &Value) {
lhs.eq(rhs);
}
pub unsafe extern "C" fn helper_create_string<Ctx: JITContext>(
ptr: *const u8,
len: usize,
ret: &mut MaybeUninit<Value<Ctx>>,
ret: &mut MaybeUninit<Value>,
) {
unsafe {
ret.write(Value::String(
@@ -105,9 +105,9 @@ pub unsafe extern "C" fn helper_create_string<Ctx: JITContext>(
}
pub unsafe extern "C" fn helper_create_list<Ctx: JITContext>(
ptr: *mut Value<Ctx>,
ptr: *mut Value,
len: usize,
ret: &mut MaybeUninit<Value<Ctx>>,
ret: &mut MaybeUninit<Value>,
) {
unsafe {
ret.write(Value::List(
@@ -117,16 +117,16 @@ pub unsafe extern "C" fn helper_create_list<Ctx: JITContext>(
}
pub unsafe extern "C" fn helper_create_attrs<Ctx: JITContext>(
ret: &mut MaybeUninit<HashMap<String, Value<Ctx>>>,
ret: &mut MaybeUninit<HashMap<String, Value>>,
) {
ret.write(HashMap::new());
}
pub unsafe extern "C" fn helper_push_attr<Ctx: JITContext>(
attrs: &mut HashMap<String, Value<Ctx>>,
attrs: &mut HashMap<String, Value>,
sym_ptr: *const u8,
sym_len: usize,
val: NonNull<Value<Ctx>>,
val: NonNull<Value>,
) {
unsafe {
attrs.insert(
@@ -137,8 +137,8 @@ pub unsafe extern "C" fn helper_push_attr<Ctx: JITContext>(
}
pub unsafe extern "C" fn helper_finalize_attrs<Ctx: JITContext>(
attrs: NonNull<HashMap<String, Value<Ctx>>>,
ret: &mut MaybeUninit<Value<Ctx>>,
attrs: NonNull<HashMap<String, Value>>,
ret: &mut MaybeUninit<Value>,
) {
ret.write(Value::AttrSet(
AttrSet::from(unsafe { attrs.read() }).into(),
@@ -147,7 +147,7 @@ pub unsafe extern "C" fn helper_finalize_attrs<Ctx: JITContext>(
pub unsafe extern "C" fn helper_enter_with<Ctx: JITContext>(
ctx: &mut Ctx,
namespace: NonNull<Value<Ctx>>,
namespace: NonNull<Value>,
) {
ctx.enter_with(unsafe { namespace.read() }.unwrap_attr_set().into_inner());
}
@@ -157,9 +157,9 @@ pub unsafe extern "C" fn helper_exit_with<Ctx: JITContext>(ctx: &mut Ctx) {
}
pub unsafe extern "C" fn helper_alloc_array<Ctx: JITContext>(len: usize) -> *mut u8 {
unsafe { alloc(Layout::array::<Value<Ctx>>(len).unwrap()) }
unsafe { alloc(Layout::array::<Value>(len).unwrap()) }
}
pub extern "C" fn helper_dbg<Ctx: JITContext>(value: &Value<Ctx>) {
pub extern "C" fn helper_dbg<Ctx: JITContext>(value: &Value) {
println!("{value:?}")
}

View File

@@ -19,13 +19,13 @@ pub use compile::JITCompile;
use helpers::*;
pub trait JITContext: EvalContext + Sized {
fn lookup_stack(&self, offset: usize) -> &Value<Self>;
fn lookup_arg(&self, offset: usize) -> &Value<Self>;
fn enter_with(&mut self, namespace: Rc<HashMap<String, Value<Self>>>);
fn lookup_stack(&self, offset: usize) -> &Value;
fn lookup_arg(&self, offset: usize) -> &Value;
fn enter_with(&mut self, namespace: Rc<HashMap<String, Value>>);
fn exit_with(&mut self);
}
type F<Ctx: JITContext> = unsafe extern "C" fn(*const Ctx, *mut Value<Ctx>);
type F<Ctx> = unsafe extern "C" fn(*const Ctx, *mut Value);
pub struct JITFunc<Ctx: JITContext> {
func: F<Ctx>,