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

+46 -1
View File
@@ -72,6 +72,46 @@ fn import_stub(m : String, name : String) -> Asm? {
s.i32(MEM_BASE)
s.b(0)
}
// --- raw host intrinsics for hand-written wasm shims (module "sys") ---
"sys::syscall" => {
// (nr, a1..a6 : i64) -> i64
s.bs([0x48, 0x8B, 0x45, 0x40]) // mov rax,[rbp+64]
s.bs([0x48, 0x8B, 0x7D, 0x38]) // mov rdi,[rbp+56]
s.bs([0x48, 0x8B, 0x75, 0x30]) // mov rsi,[rbp+48]
s.bs([0x48, 0x8B, 0x55, 0x28]) // mov rdx,[rbp+40]
s.bs([0x4C, 0x8B, 0x55, 0x20]) // mov r10,[rbp+32]
s.bs([0x4C, 0x8B, 0x45, 0x18]) // mov r8,[rbp+24]
s.bs([0x4C, 0x8B, 0x4D, 0x10]) // mov r9,[rbp+16]
s.bs([0x0F, 0x05])
}
"sys::membase" => {
s.b(0xB8)
s.i32(MEM_BASE)
}
"sys::argv_base" => {
// initial rsp saved by _start (points at argc)
s.bs([0x48, 0x8B, 0x04, 0x25])
s.i32(GLOB_BASE + 0xFF8)
}
"sys::peek64" =>
// (host addr : i64) -> i64
s.bs([0x48, 0x8B, 0x45, 0x10, 0x48, 0x8B, 0x00])
"sys::strlen" => {
// (host addr : i64) -> i64
s.bs([
0x48, 0x8B, 0x7D, 0x10, 0x31, 0xC0, 0x48, 0xC7, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF,
])
s.bs([0xF2, 0xAE, 0x48, 0xF7, 0xD1, 0x48, 0xFF, 0xC9, 0x48, 0x89, 0xC8])
}
"sys::copy_in" => {
// (dst guest i32, src host i64, n i32) -> ()
s.bs([0x8B, 0x7D, 0x20]) // mov edi,[rbp+32]
s.bs([0x48, 0x81, 0xC7])
s.i32(MEM_BASE)
s.bs([0x48, 0x8B, 0x75, 0x18]) // mov rsi,[rbp+24]
s.bs([0x8B, 0x4D, 0x10]) // mov ecx,[rbp+16]
s.bs([0xF3, 0xA4])
}
"wasi:cli/exit::exit" =>
// (status: result) -> () ; 0 = ok, 1 = err
s.bs([0x8B, 0x7D, 0x10, 0xB8, 0xE7, 0x00, 0x00, 0x00, 0x0F, 0x05])
@@ -216,7 +256,8 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
0x42 => globals.push(r.s64())
0x43 => globals.push(r.raw32().to_int64() & 0xFFFFFFFFL)
0x44 => globals.push(r.raw64())
_ => raise CompileError("global initializers must be a numeric const")
_ =>
raise CompileError("global initializers must be a numeric const")
}
if r.byte() != 0x0B {
raise CompileError("bad global initializer")
@@ -335,6 +376,10 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
let phnum = if has_data { 2 } else { 1 }
let headers = 64 + 56 * phnum
let a = Asm::new()
if has_data {
a.bs([0x48, 0x89, 0x24, 0x25]) // mov [GLOB_BASE+0xFF8], rsp (argc/argv)
a.i32(GLOB_BASE + 0xFF8)
}
let start_call = a.rel([0xE8])
a.bs([0x89, 0xC7]) // mov edi,eax
if types[func_types[entry_idx]].nresults == 0 {