feat(jit): attrs & list

This commit is contained in:
2025-07-17 15:57:02 +08:00
parent 2909483afb
commit dedf84a1a9
3 changed files with 247 additions and 19 deletions

View File

@@ -14,13 +14,38 @@ pub trait JITCompile {
impl JITCompile for Attrs {
fn compile(&self, ctx: &mut JITContext, engine: ir::Value, env: ir::Value) -> StackSlot {
todo!()
let attrs = ctx.create_attrs();
for (k, v) in self.stcs.iter() {
let v = v.compile(ctx, engine, env);
ctx.push_attr(attrs, k, v);
}
ctx.finalize_attrs(attrs)
}
}
impl JITCompile for List {
fn compile(&self, ctx: &mut JITContext, engine: ir::Value, env: ir::Value) -> StackSlot {
todo!()
let array = ctx.alloc_array(self.items.len());
for (i, item) in self.items.iter().enumerate() {
let item = item.compile(ctx, engine, env);
let tag = ctx.builder.ins().stack_load(types::I64, item, 0);
let val0 = ctx.builder.ins().stack_load(types::I64, item, 8);
let val1 = ctx.builder.ins().stack_load(types::I64, item, 16);
let val2 = ctx.builder.ins().stack_load(types::I64, item, 24);
ctx.builder
.ins()
.store(MemFlags::new(), tag, array, i as i32 * 32);
ctx.builder
.ins()
.store(MemFlags::new(), val0, array, i as i32 * 32 + 8);
ctx.builder
.ins()
.store(MemFlags::new(), val1, array, i as i32 * 32 + 16);
ctx.builder
.ins()
.store(MemFlags::new(), val2, array, i as i32 * 32 + 24);
}
ctx.create_list(array, self.items.len())
}
}
@@ -281,6 +306,7 @@ impl JITCompile for BinOp {
ctx.builder.seal_block(eq_block);
ctx.builder.seal_block(neq_block);
ctx.builder.switch_to_block(exit_block);
ctx.free_slot(rhs);
lhs
}
@@ -343,8 +369,7 @@ impl JITCompile for If {
let exit_block = ctx.builder.create_block();
let error_block = ctx.builder.create_block();
let judge_block = ctx.builder.create_block();
let slot = StackSlotData::new(StackSlotKind::ExplicitSlot, 32, 3);
let slot = ctx.builder.create_sized_stack_slot(slot);
let slot = ctx.alloca();
let is_bool = ctx
.builder
@@ -393,8 +418,7 @@ impl JITCompile for If {
impl JITCompile for LoadFunc {
fn compile(&self, ctx: &mut JITContext, engine: ir::Value, env: ir::Value) -> StackSlot {
let slot = StackSlotData::new(StackSlotKind::ExplicitSlot, 32, 3);
let slot = ctx.builder.create_sized_stack_slot(slot);
let slot = ctx.alloca();
let tag = ctx.builder.ins().iconst(types::I64, Value::FUNC as i64);
let val = ctx.builder.ins().iconst(types::I64, self.idx as i64);
ctx.builder.ins().stack_store(tag, slot, 0);
@@ -459,8 +483,7 @@ impl JITCompile for ConcatStrings {
impl JITCompile for Const {
fn compile(&self, ctx: &mut JITContext, engine: ir::Value, env: ir::Value) -> StackSlot {
use c::Const::*;
let slot = StackSlotData::new(StackSlotKind::ExplicitSlot, 32, 3);
let slot = ctx.builder.create_sized_stack_slot(slot);
let slot = ctx.alloca();
match self.val {
Bool(x) => {
let tag = ctx.builder.ins().iconst(types::I64, Value::BOOL as i64);
@@ -491,7 +514,7 @@ impl JITCompile for Const {
impl JITCompile for Str {
fn compile(&self, ctx: &mut JITContext, engine: ir::Value, env: ir::Value) -> StackSlot {
todo!()
ctx.create_string(&self.val)
}
}
@@ -515,8 +538,7 @@ impl JITCompile for LetVar {
impl JITCompile for Thunk {
fn compile(&self, ctx: &mut JITContext, engine: ir::Value, env: ir::Value) -> StackSlot {
let slot = StackSlotData::new(StackSlotKind::ExplicitSlot, 32, 3);
let slot = ctx.builder.create_sized_stack_slot(slot);
let slot = ctx.alloca();
let tag = ctx.builder.ins().iconst(types::I64, Value::THUNK as i64);
let val = ctx.builder.ins().iconst(types::I64, self.idx as i64);
ctx.builder.ins().stack_store(tag, slot, 0);