From 38f7b934b52490ec32953f94acec5a6872d40b7c Mon Sep 17 00:00:00 2001 From: nandithebull Date: Sat, 21 Nov 2026 01:08:26 -0800 Subject: [PATCH 1/3] Make fact example a wasi:cli command that prints its result Co-Authored-By: Claude Sonnet 5.5 --- README.mbt.md | 1 + examples/fact.wat | 36 +++++++++++++++++++++++++++++++++--- examples/wit/world.wit | 4 ++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/README.mbt.md b/README.mbt.md index 23ed880..19adb15 100644 --- a/README.mbt.md +++ b/README.mbt.md @@ -6,6 +6,7 @@ runtime: the output is hand-encoded machine code in one or two `PT_LOAD` segment ``` just run sum # core module -> ELF, runs it, prints the exit code just run-component hello # wasi:cli component -> ELF, prints "Hello from wasi:cli!" +just run-component fact # wasi:cli component -> ELF, prints "fact(5) = 120" just examples # everything in examples/ ``` diff --git a/examples/fact.wat b/examples/fact.wat index 2e08c87..f53faa6 100644 --- a/examples/fact.wat +++ b/examples/fact.wat @@ -1,8 +1,38 @@ -;; recursive factorial(5) = 120, plus if/else with result +;; wasi:cli command: computes factorial(5) recursively and prints "fact(5) = 120". +;; Build with: just run-component fact (module + (import "wasi:cli/stdout@0.2.0" "get-stdout" (func $get_stdout (result i32))) + (import "wasi:io/streams@0.2.0" "[method]output-stream.blocking-write-and-flush" + (func $write (param i32 i32 i32 i32))) + (memory (export "memory") 1) + (data (i32.const 16) "fact(5) = ") + (func $fact (param $n i32) (result i32) (if (result i32) (i32.le_s (local.get $n) (i32.const 1)) (then (i32.const 1)) (else (i32.mul (local.get $n) (call $fact (i32.sub (local.get $n) (i32.const 1))))))) - (func (export "main") (result i32) - (call $fact (i32.const 5)))) + + (func (export "wasi:cli/run@0.2.0#run") (result i32) + (local $v i32) (local $p i32) + (local.set $v (call $fact (i32.const 5))) + ;; write decimal digits backwards, ending just before a trailing newline at 63 + (local.set $p (i32.const 63)) + (i32.store8 (i32.const 63) (i32.const 10)) + (loop $digits + (local.set $p (i32.sub (local.get $p) (i32.const 1))) + (i32.store8 (local.get $p) + (i32.add (i32.const 48) (i32.rem_u (local.get $v) (i32.const 10)))) + (local.set $v (i32.div_u (local.get $v) (i32.const 10))) + (br_if $digits (local.get $v))) + ;; prefix "fact(5) = " (10 bytes at 16..26); copy digits right after it + (local.set $v (i32.const 26)) + (block $done + (loop $copy + (br_if $done (i32.gt_u (local.get $p) (i32.const 63))) + (i32.store8 (local.get $v) (i32.load8_u (local.get $p))) + (local.set $v (i32.add (local.get $v) (i32.const 1))) + (local.set $p (i32.add (local.get $p) (i32.const 1))) + (br $copy))) + (call $write (call $get_stdout) (i32.const 16) + (i32.sub (local.get $v) (i32.const 16)) (i32.const 128)) + (i32.load8_u (i32.const 128)))) diff --git a/examples/wit/world.wit b/examples/wit/world.wit index 2c21a96..3f9fea2 100644 --- a/examples/wit/world.wit +++ b/examples/wit/world.wit @@ -3,3 +3,7 @@ package local:examples; world hello { include wasi:cli/command@0.2.0; } + +world fact { + include wasi:cli/command@0.2.0; +} From fdc1cd4b3c6fffc985e37ecb7fba7930197d5a8a Mon Sep 17 00:00:00 2001 From: nandithebull Date: Sat, 21 Nov 2026 18:12:06 -0800 Subject: [PATCH 2/3] 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 --- compile.mbt | 104 +++++++++++++++++++++++++++++++++++++++++++++++----- module.mbt | 47 +++++++++++++++++++++++- 2 files changed, 141 insertions(+), 10 deletions(-) diff --git a/compile.mbt b/compile.mbt index 5b90389..ec047e3 100644 --- a/compile.mbt +++ b/compile.mbt @@ -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}") + } +} diff --git a/module.mbt b/module.mbt index 30d21d7..13be1f8 100644 --- a/module.mbt +++ b/module.mbt @@ -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 { From 143e0035a34c05c45edb54b9b5d97eaaf086aa8b Mon Sep 17 00:00:00 2001 From: V G Date: Sun, 22 Nov 2026 21:23:33 +0000 Subject: [PATCH 3/3] Add sweet release config (#4) --- .sweet.config.yml | 15 +++++++++++++++ .sweet.yml | 5 +++++ AGENTS.md | 8 ++++++++ moon.mod | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .sweet.config.yml create mode 100644 .sweet.yml diff --git a/.sweet.config.yml b/.sweet.config.yml new file mode 100644 index 0000000..fe5ece1 --- /dev/null +++ b/.sweet.config.yml @@ -0,0 +1,15 @@ +# Sweet repository configuration +types: + fix: + bump: patch + section: Fixes + feature: + bump: minor + section: Features + chore: + bump: none + refactor: + bump: none + performance: + bump: patch + section: Performance diff --git a/.sweet.yml b/.sweet.yml new file mode 100644 index 0000000..edfded8 --- /dev/null +++ b/.sweet.yml @@ -0,0 +1,5 @@ +name: wasm2elf +version: 0.1.0 +version-files: + - path: moon.mod + format: annotated diff --git a/AGENTS.md b/AGENTS.md index c0e8474..605c037 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -51,3 +51,11 @@ You can browse and install extra skills here: scientific computations), prefer assertion tests. You can use `moon coverage analyze > uncovered.log` to see which parts of your code are not covered by tests. + +## Releases + +Releases are managed with `sweet` +(config in `.sweet.config.yml` and `.sweet.yml`). For any user-visible change, +add a release note with `sweet change`. `sweet bump` updates the version in +`moon.mod` (the line tagged `// sweet-version`; keep that comment). Run +`sweet doctor` to validate. diff --git a/moon.mod b/moon.mod index 8723905..679f7a6 100644 --- a/moon.mod +++ b/moon.mod @@ -11,7 +11,7 @@ name = "nandi/wasm2elf" -version = "0.1.0" +version = "0.1.0" // sweet-version readme = "README.mbt.md"