Add bulk memory ops and raw syscall intrinsics

- memory.copy / memory.fill (0xFC 0x0A/0x0B) with bounds checks via rep movsb/stosb
- module "sys" host intrinsics for hand-written wasm shims: syscall, membase,
  argv_base, peek64, strlen, copy_in; _start saves the initial rsp for argv
  (only when the module has a data segment)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-21 18:12:12 -08:00
1 parent 38f7b934b5
commit fdc1cd4b3c
2 files changed
+141 -10

No files matched your search

+95 -9
View File
@@ -355,6 +355,15 @@ fn compile_body(
0x43 => ignore(r.raw32())
0x44 => ignore(r.raw64())
0x3F | 0x40 => ignore(r.byte())
0xFC =>
match r.u32() {
0x0A => {
ignore(r.byte())
ignore(r.byte())
}
0x0B => ignore(r.byte())
_ => ()
}
n if n >= 0x28 && n <= 0x3E => {
ignore(r.u32())
ignore(r.u32())
@@ -487,6 +496,7 @@ fn compile_body(
f.code.bs([0x6A, 0xFF])
f.height += 1
}
0xFC => f.bulk_mem(r.u32(), r)
0x41 => {
f.code.b(0x68)
f.code.i32(r.s32())
@@ -614,7 +624,7 @@ fn Fn::fp_store(self : Fn, wide : Bool) -> Unit {
///|
/// Scalar-op prefix (F2 for f64, F3 for f32).
fn sp_of(wide : Bool) -> Int {
fn sp_of(wide : Bool) -> Int32 {
if wide {
0xF2
} else {
@@ -632,7 +642,7 @@ fn Fn::pu(self : Fn, wide : Bool) -> Unit {
///|
/// idx: eq, ne, lt, gt, le, ge. NaN compares false (true for ne).
fn Fn::fp_cmp(self : Fn, idx : Int, wide : Bool) -> Unit {
fn Fn::fp_cmp(self : Fn, idx : Int32, wide : Bool) -> Unit {
self.fp_load(wide, true)
self.pu(wide)
// lt/le compare (b, a) so that "above" is false on unordered
@@ -649,7 +659,7 @@ fn Fn::fp_cmp(self : Fn, idx : Int, wide : Bool) -> Unit {
///|
/// op: abs, neg, ceil, floor, trunc, nearest, sqrt.
fn Fn::fp_unop(self : Fn, op : Int, wide : Bool) -> Unit {
fn Fn::fp_unop(self : Fn, op : Int32, wide : Bool) -> Unit {
match op {
0 | 1 => {
self.pop(0)
@@ -675,7 +685,18 @@ fn Fn::fp_unop(self : Fn, op : Int, wide : Bool) -> Unit {
4 => 0x0B
_ => 0x08
}
self.code.bs([0x66, 0x0F, 0x3A, if wide { 0x0B } else { 0x0A }, 0xC0, mode])
self.code.bs([
0x66,
0x0F,
0x3A,
if wide {
0x0B
} else {
0x0A
},
0xC0,
mode,
])
self.fp_store(wide)
}
}
@@ -683,13 +704,15 @@ fn Fn::fp_unop(self : Fn, op : Int, wide : Bool) -> Unit {
///|
/// op: add, sub, mul, div, min, max, copysign.
fn Fn::fp_binop(self : Fn, op : Int, wide : Bool) -> Unit {
fn Fn::fp_binop(self : Fn, op : Int32, wide : Bool) -> Unit {
if op == 6 {
self.pop(1)
self.pop(0)
if wide {
self.code.bs([0x48, 0xD1, 0xE0, 0x48, 0xD1, 0xE8]) // clear sign of a
self.code.bs([0x48, 0xC1, 0xE9, 0x3F, 0x48, 0xC1, 0xE1, 0x3F, 0x48, 0x09, 0xC8])
self.code.bs([
0x48, 0xC1, 0xE9, 0x3F, 0x48, 0xC1, 0xE1, 0x3F, 0x48, 0x09, 0xC8,
])
} else {
self.code.bs([0xD1, 0xE0, 0xD1, 0xE8])
self.code.bs([0xC1, 0xE9, 0x1F, 0xC1, 0xE1, 0x1F, 0x09, 0xC8])
@@ -750,7 +773,9 @@ fn Fn::fp_convert(
let done = self.code.rel8([0xEB])
self.code.patch_here8(big)
// halve with sticky low bit, convert, double
self.code.bs([0x48, 0x89, 0xC1, 0x48, 0xD1, 0xE8, 0x83, 0xE1, 0x01, 0x48, 0x09, 0xC8])
self.code.bs([
0x48, 0x89, 0xC1, 0x48, 0xD1, 0xE8, 0x83, 0xE1, 0x01, 0x48, 0x09, 0xC8,
])
cvt()
self.code.bs([sp, 0x0F, 0x58, 0xC0])
self.code.patch_here8(done)
@@ -768,14 +793,19 @@ fn Fn::fp_resize(self : Fn, src_wide : Bool, dst_wide : Bool) -> Unit {
///|
/// Trapping float -> int truncation (NaN and out-of-range trap).
fn Fn::fp_trunc(self : Fn, src_wide : Bool, dst64 : Bool, signed : Bool) -> Unit {
fn Fn::fp_trunc(
self : Fn,
src_wide : Bool,
dst64 : Bool,
signed : Bool,
) -> Unit {
self.fp_load(src_wide, false)
self.pu(src_wide)
self.code.bs([0x0F, 0x2E, 0xC0]) // ucomis xmm0,xmm0
self.trap_jmp(TRAP_INVALID, [0x0F, 0x8A]) // jp
let sp = sp_of(src_wide)
let cvtt = fn() { self.code.bs([sp, 0x48, 0x0F, 0x2C, 0xC0]) } // cvttsd2si rax,xmm0
let load_const = fn(hi_f64 : Int, f32_bits : Int) {
let load_const = fn(hi_f64 : Int32, f32_bits : Int32) {
if src_wide {
self.code.bs([0x48, 0xB9]) // mov rcx, imm64
self.code.i32(0)
@@ -831,3 +861,59 @@ fn Fn::fp_trunc(self : Fn, src_wide : Bool, dst64 : Bool, signed : Bool) -> Unit
}
self.push_rax()
}
///|
/// Bulk memory ops: memory.copy (0xFC 0x0A, overlap-safe) and memory.fill (0xFC 0x0B),
/// with bounds checks, via rep movsb / rep stosb.
fn Fn::bulk_mem(self : Fn, sub : Int32, r : Reader) -> Unit raise CompileError {
if self.mem_bytes == 0 {
raise CompileError("memory access but the module defines no memory")
}
match sub {
0x0A => {
ignore(r.byte())
ignore(r.byte())
self.pop(1) // rcx = n
self.pop(6) // rsi = src
self.pop(7) // rdi = dst
self.code.bs([0x89, 0xC9, 0x89, 0xF6, 0x89, 0xFF]) // zero-extend ecx,esi,edi
self.code.bs([0x48, 0x8D, 0x04, 0x0E]) // lea rax,[rsi+rcx]
self.code.bs([0x48, 0x3D])
self.code.i32(self.mem_bytes)
self.trap_jmp(TRAP_OOB, [0x0F, 0x87])
self.code.bs([0x48, 0x8D, 0x04, 0x0F]) // lea rax,[rdi+rcx]
self.code.bs([0x48, 0x3D])
self.code.i32(self.mem_bytes)
self.trap_jmp(TRAP_OOB, [0x0F, 0x87])
self.code.bs([0x48, 0x81, 0xC6])
self.code.i32(MEM_BASE)
self.code.bs([0x48, 0x81, 0xC7])
self.code.i32(MEM_BASE)
self.code.bs([0x48, 0x39, 0xF7]) // cmp rdi,rsi
let fwd = self.code.rel8([0x76]) // jbe forward
self.code.bs([0xFD]) // std
self.code.bs([0x48, 0x8D, 0x74, 0x0E, 0xFF]) // lea rsi,[rsi+rcx-1]
self.code.bs([0x48, 0x8D, 0x7C, 0x0F, 0xFF]) // lea rdi,[rdi+rcx-1]
self.code.bs([0xF3, 0xA4, 0xFC]) // rep movsb; cld
let done = self.code.rel8([0xEB])
self.code.patch_here8(fwd)
self.code.bs([0xF3, 0xA4])
self.code.patch_here8(done)
}
0x0B => {
ignore(r.byte())
self.pop(1) // rcx = n
self.pop(0) // rax = value
self.pop(7) // rdi = dst
self.code.bs([0x89, 0xC9, 0x89, 0xFF])
self.code.bs([0x48, 0x8D, 0x14, 0x0F]) // lea rdx,[rdi+rcx]
self.code.bs([0x48, 0x81, 0xFA])
self.code.i32(self.mem_bytes)
self.trap_jmp(TRAP_OOB, [0x0F, 0x87])
self.code.bs([0x48, 0x81, 0xC7])
self.code.i32(MEM_BASE)
self.code.bs([0xF3, 0xAA])
}
_ => raise CompileError("unsupported 0xFC opcode \{sub}")
}
}