code wiki / _hdl_build / nx_wasm_emit_seed.nx

nx_wasm_emit_seed.nx source

↩ module page · 85 lines · 3802 B

1// nx_wasm_emit_seed.nx -- SEED of the sovereign WAT->WASM emitter (G-WASM-001 milestone 1). 2// Emits a real WebAssembly BINARY module BYTE-BY-BYTE from the encoding rules (NOT a memcpy 3// of known output) for: (module (func (export "main") (result i64) i64.const 42)). 4// Reuses runtime/leb128.nx (the verified LEB128 organ) for all sizes/counts/operands. 5// Proves the sovereign encoding is byte-correct vs WABT used ONCE as an alignment oracle 6// (then WABT leaves the path) -- the same pattern as qemu aligning the rv64 emu. 7// NO node, NO python, NO WABT in this organ. license_tier: ORIGINAL 8import "leb128.nx" 9import "nx_syscalls.nx" 10const K_MAGIC_1024: i64 = 1024 11 12func w8(b: *u8, p: i64, v: i64) -> i64 { b[p] = v as u8; return p + 1 } 13func wcopy(dst: *u8, dp: i64, src: *u8, n: i64) -> i64 { 14 var i: i64 = 0 15 while i < n { dst[dp + i] = src[i]; i = i + 1 } 16 return dp + n 17} 18// section = id byte + ULEB(content_len) + content (two-level length handled by building 19// content in a scratch buffer first, then prefixing its measured length). 20func emit_section(out: *u8, p: i64, id: i64, sc: *u8, sclen: i64) -> i64 { 21 var q: i64 = w8(out, p, id) 22 q = q + uleb128_encode(sclen, out, q) // leb encoders return BYTES-WRITTEN 23 q = wcopy(out, q, sc, sclen) 24 return q 25} 26 27func main() -> i64 { 28 let out: *u8 = sys_mmap(K_MAGIC_1024) 29 let sc: *u8 = sys_mmap(K_MAGIC_1024) 30 let body: *u8 = sys_mmap(256) 31 var p: i64 = 0 32 // ---- preamble: magic "\0asm" + version 1 (fixed 8 bytes, NOT LEB) ---- 33 p = w8(out, p, 0); p = w8(out, p, 0x61); p = w8(out, p, 0x73); p = w8(out, p, 0x6D) 34 p = w8(out, p, 1); p = w8(out, p, 0); p = w8(out, p, 0); p = w8(out, p, 0) 35 // ---- type section (id 1): 1 type = func ()->(i64) ---- 36 var s: i64 = 0 37 s = w8(sc, s, 1) // type count 38 s = w8(sc, s, 0x60) // functype tag 39 s = w8(sc, s, 0) // param count 40 s = w8(sc, s, 1) // result count 41 s = w8(sc, s, 0x7E) // i64 42 p = emit_section(out, p, 1, sc, s) 43 // ---- function section (id 3): 1 func, typeidx 0 ---- 44 s = 0 45 s = w8(sc, s, 1) // func count 46 s = w8(sc, s, 0) // typeidx 0 47 p = emit_section(out, p, 3, sc, s) 48 // ---- export section (id 7): 1 export "main" -> func 0 ---- 49 s = 0 50 s = w8(sc, s, 1) // export count 51 s = w8(sc, s, 4) // name len 52 s = w8(sc, s, 0x6D); s = w8(sc, s, 0x61); s = w8(sc, s, 0x69); s = w8(sc, s, 0x6E) // "main" 53 s = w8(sc, s, 0) // kind = func 54 s = w8(sc, s, 0) // funcidx 0 55 p = emit_section(out, p, 7, sc, s) 56 // ---- code section (id 10): 1 body = (no locals) i64.const 42 end ---- 57 var bp: i64 = 0 58 bp = w8(body, bp, 0) // local group count 59 bp = w8(body, bp, 0x42) // i64.const 60 bp = bp + sleb128_encode(42, body, bp) // operand (SLEB128), returns bytes-written 61 bp = w8(body, bp, 0x0B) // end 62 s = 0 63 s = w8(sc, s, 1) // code count 64 s = s + uleb128_encode(bp, sc, s) // code entry size (bytes-written) 65 s = wcopy(sc, s, body, bp) // body 66 p = emit_section(out, p, 10, sc, s) 67 // ---- write the .wasm ---- 68 let fd: i64 = sys_openat_wr("knowledge/status/_seed.wasm" as *u8, 0x1a4) 69 if fd < 0 { sys_exit(1); return 1 } 70 sys_write(fd, out, p) 71 sys_close(fd) 72 sys_write(1, "nx_wasm_emit_seed: wrote knowledge/status/_seed.wasm bytes=" as *u8, 58) 73 let nb: *u8 = sys_mmap(16) 74 var m: i64 = p 75 var k: i64 = 0 76 if m == 0 { nb[0] = 48 as u8; k = 1 } 77 let tmp: *u8 = sys_mmap(16) 78 while m > 0 { tmp[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 79 var i: i64 = 0 80 let ob: *u8 = sys_mmap(16) 81 while i < k { ob[i] = tmp[k - 1 - i]; i = i + 1 } 82 sys_write(1, ob, k); sys_write(1, "\n" as *u8, 1) 83 sys_exit(0) 84 return 0 85}