Add wasi:cli component support, linear memory and globals

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-21 01:03:32 -08:00
1 parent cb1cec8b13
commit 05488c9783
11 files changed
+518 -52

No files matched your search

+92 -1
View File
@@ -20,6 +20,8 @@ priv struct Fn {
code : Asm
ft : FuncType
nlocals : Int // total locals including params
mem_bytes : Int
nglobals : Int
frames : Array[Frame]
calls : Array[(Int, Int)] // (rel32 position, callee function index)
traps : Array[(Int, Int)] // (rel32 position, trap kind)
@@ -36,6 +38,9 @@ const TRAP_DIV_ZERO : Int = 1
///|
const TRAP_OVERFLOW : Int = 2
///|
const TRAP_OOB : Int = 3
///|
fn Fn::push_rax(self : Fn) -> Unit {
self.code.b(0x50)
@@ -166,6 +171,57 @@ fn Fn::divrem(self : Fn, op : Int) -> Unit {
self.push_rax()
}
///|
/// Linear-memory load/store: bounds check, then access at MEM_BASE + addr + offset.
fn Fn::mem_access(self : Fn, r : Reader, op : Int) -> Unit raise CompileError {
ignore(r.u32()) // alignment hint
let offset = r.u32()
if self.mem_bytes == 0 {
raise CompileError("memory access but the module defines no memory")
}
if offset < 0 {
raise CompileError("memory offset too large")
}
let size = match op {
0x28 | 0x36 => 4
0x2C | 0x2D | 0x3A => 1
_ => 2
}
let is_store = op >= 0x36
if is_store {
self.pop(1)
}
self.pop(0)
self.code.bs([0x89, 0xC0]) // mov eax,eax (zero-extend address)
self.code.bs([0x48, 0x05]) // add rax, offset
self.code.i32(offset)
self.code.bs([0x48, 0x3D]) // cmp rax, mem_bytes - size
self.code.i32(self.mem_bytes - size)
self.trap_jmp(TRAP_OOB, [0x0F, 0x87]) // ja trap
match op {
0x28 => self.code.bs([0x8B, 0x80])
0x2C => self.code.bs([0x0F, 0xBE, 0x80])
0x2D => self.code.bs([0x0F, 0xB6, 0x80])
0x2E => self.code.bs([0x0F, 0xBF, 0x80])
0x2F => self.code.bs([0x0F, 0xB7, 0x80])
0x36 => self.code.bs([0x89, 0x88])
0x3A => self.code.bs([0x88, 0x88])
_ => self.code.bs([0x66, 0x89, 0x88])
}
self.code.i32(MEM_BASE)
if !is_store {
self.push_rax()
}
}
///|
fn Fn::global_addr(self : Fn, idx : Int) -> Int raise CompileError {
if idx < 0 || idx >= self.nglobals {
raise CompileError("global index out of range")
}
GLOB_BASE + 8 * idx
}
///|
/// Compile one function body. Returns the generated Fn (machine code + patch lists).
fn compile_body(
@@ -175,11 +231,15 @@ fn compile_body(
nlocals : Int,
all_types : Array[FuncType],
func_types : Array[Int],
mem_bytes : Int,
nglobals : Int,
) -> Fn raise CompileError {
let f : Fn = {
code: Asm::new(),
ft,
nlocals,
mem_bytes,
nglobals,
frames: [],
calls: [],
traps: [],
@@ -238,8 +298,13 @@ fn compile_body(
}
}
}
0x0C | 0x0D | 0x10 | 0x20 | 0x21 | 0x22 => ignore(r.u32())
0x0C | 0x0D | 0x10 | 0x20 | 0x21 | 0x22 | 0x23 | 0x24 => ignore(r.u32())
0x41 => ignore(r.s32())
0x3F | 0x40 => ignore(r.byte())
n if n >= 0x28 && n <= 0x3E => {
ignore(r.u32())
ignore(r.u32())
}
_ => ()
}
continue
@@ -342,6 +407,32 @@ fn compile_body(
f.code.bs([0x48, 0x89, 0x85])
f.code.i32(d)
}
0x23 => {
let a = f.global_addr(r.u32())
f.code.bs([0x8B, 0x04, 0x25]) // mov eax,[abs]
f.code.i32(a)
f.push_rax()
}
0x24 => {
let a = f.global_addr(r.u32())
f.pop(0)
f.code.bs([0x89, 0x04, 0x25]) // mov [abs],eax
f.code.i32(a)
}
0x28 | 0x2C | 0x2D | 0x2E | 0x2F | 0x36 | 0x3A | 0x3B => f.mem_access(r, op)
0x3F => {
ignore(r.byte())
f.code.b(0x68)
f.code.i32(f.mem_bytes / 65536)
f.height += 1
}
0x40 => {
// memory is fixed-size: memory.grow always fails (-1)
ignore(r.byte())
f.pop(0)
f.code.bs([0x6A, 0xFF])
f.height += 1
}
0x41 => {
f.code.b(0x68)
f.code.i32(r.s32())