omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / wasm / v7run.js
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Read-only host executor for an irwasm v7 module. It never generates code: |
| 3 | // it instantiates the module irwasm emitted, runs a script and prints the |
| 4 | // same lines as `seed/wasm/v7/oracle.js run`, so the two outputs diff empty. |
| 5 | // |
| 6 | // usage: node seed/wasm/v7run.js MODULE.wasm SCRIPT |
| 7 | // Script lines: `call NAME ARG...` and `region NAME`; blank and `#` lines skip. |
| 8 | // Integer arguments are decimal; f32/f64 arguments are 0x + 8/16 hex digits. |
| 9 | // Output: `NAME = V`, `NAME TRAP`, `region NAME <hex>`. |
| 10 | // |
| 11 | // The OSC tags of every parameter, result and module region come from the |
| 12 | // custom section `osc.v7.abi` that irwasm appends to every v7 module; wasm |
| 13 | // types alone cannot say whether an i32 is u8 or i32. |
| 14 | // Exit: 0 done, 2 usage/script error, 3 non-trap engine error. |
| 15 | 'use strict'; |
| 16 | |
| 17 | const fs = require('fs'); |
| 18 | |
| 19 | const T_VOID = 0, T_BOOL = 1, T_F32 = 13, T_F64 = 14; |
| 20 | const WIDTH = { 1: 1, 2: 8, 3: 16, 4: 32, 5: 64, 6: 8, 7: 16, 8: 32, 9: 64, 13: 32, 14: 64 }; |
| 21 | const signed = (t) => t >= 6 && t <= 9; |
| 22 | const isFloat = (t) => t === T_F32 || t === T_F64; |
| 23 | |
| 24 | function usage(msg) { |
| 25 | process.stderr.write('v7run: ' + msg + '\nusage: v7run.js MODULE.wasm SCRIPT\n'); |
| 26 | process.exit(2); |
| 27 | } |
| 28 | |
| 29 | function readFile(p) { |
| 30 | try { return fs.readFileSync(p); } catch (e) { usage('cannot read ' + p + ': ' + e.code); } |
| 31 | } |
| 32 | |
| 33 | class Reader { |
| 34 | constructor(buf, pos, end) { this.buf = buf; this.pos = pos; this.end = end; } |
| 35 | byte() { |
| 36 | if (this.pos >= this.end) usage('truncated osc.v7.abi section'); |
| 37 | return this.buf[this.pos++]; |
| 38 | } |
| 39 | leb() { |
| 40 | let v = 0, shift = 0; |
| 41 | for (;;) { |
| 42 | const b = this.byte(); |
| 43 | v += (b & 127) * 2 ** shift; |
| 44 | if (b < 128) return v; |
| 45 | shift += 7; |
| 46 | } |
| 47 | } |
| 48 | name() { |
| 49 | const n = this.leb(); |
| 50 | if (this.pos + n > this.end) usage('truncated name'); |
| 51 | const s = this.buf.subarray(this.pos, this.pos + n).toString('latin1'); |
| 52 | this.pos += n; |
| 53 | return s; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Find the custom section and decode function signatures and module regions. |
| 58 | function readAbi(buf) { |
| 59 | const top = new Reader(buf, 8, buf.length); |
| 60 | while (top.pos < buf.length) { |
| 61 | const id = top.byte(); |
| 62 | const size = top.leb(); |
| 63 | const end = top.pos + size; |
| 64 | if (id === 0) { |
| 65 | const r = new Reader(buf, top.pos, end); |
| 66 | if (r.name() === 'osc.v7.abi') { |
| 67 | const funcs = []; |
| 68 | for (let n = r.leb(), i = 0; i < n; i++) { |
| 69 | const name = r.name(); |
| 70 | const params = []; |
| 71 | for (let k = r.leb(), j = 0; j < k; j++) params.push(r.leb()); |
| 72 | funcs.push({ name, params, ret: r.leb() }); |
| 73 | } |
| 74 | const regions = []; |
| 75 | for (let n = r.leb(), i = 0; i < n; i++) { |
| 76 | regions.push({ name: r.name(), tag: r.leb(), cls: r.leb(), count: r.leb() }); |
| 77 | } |
| 78 | return { funcs, regions }; |
| 79 | } |
| 80 | } |
| 81 | top.pos = end; |
| 82 | } |
| 83 | usage('module has no osc.v7.abi section (not an irwasm v7 module)'); |
| 84 | } |
| 85 | |
| 86 | const dv = new DataView(new ArrayBuffer(8)); |
| 87 | |
| 88 | function parseArg(t, s) { |
| 89 | if (isFloat(t)) { |
| 90 | const digits = t === T_F32 ? 8 : 16; |
| 91 | if (!new RegExp('^0x[0-9a-fA-F]{' + digits + '}$').test(s)) return null; |
| 92 | const bits = BigInt(s); |
| 93 | if (t === T_F32) { dv.setUint32(0, Number(bits)); return dv.getFloat32(0); } |
| 94 | dv.setBigUint64(0, bits); return dv.getFloat64(0); |
| 95 | } |
| 96 | if (!/^-?[0-9]+$/.test(s)) return null; |
| 97 | const v = BigInt(s); |
| 98 | const w = BigInt(WIDTH[t]); |
| 99 | const lo = t === T_BOOL ? 0n : signed(t) ? -(1n << (w - 1n)) : 0n; |
| 100 | const hi = t === T_BOOL ? 1n : signed(t) ? (1n << (w - 1n)) - 1n : (1n << w) - 1n; |
| 101 | if (v < lo || v > hi) return null; |
| 102 | if (WIDTH[t] === 64) return BigInt.asIntN(64, v); |
| 103 | return Number(BigInt.asIntN(32, v)); |
| 104 | } |
| 105 | |
| 106 | function formatValue(t, r) { |
| 107 | if (t === T_VOID) return 'void'; |
| 108 | if (t === T_F32) { dv.setFloat32(0, r); return '0x' + dv.getUint32(0).toString(16).padStart(8, '0'); } |
| 109 | if (t === T_F64) { dv.setFloat64(0, r); return '0x' + dv.getBigUint64(0).toString(16).padStart(16, '0'); } |
| 110 | const w = WIDTH[t]; |
| 111 | const v = BigInt(r); |
| 112 | return (signed(t) ? BigInt.asIntN(w, v) : BigInt.asUintN(w, v)).toString(10); |
| 113 | } |
| 114 | |
| 115 | function main(argv) { |
| 116 | if (argv.length !== 2) usage('bad arguments'); |
| 117 | const bytes = readFile(argv[0]); |
| 118 | const abi = readAbi(bytes); |
| 119 | let inst; |
| 120 | try { |
| 121 | inst = new WebAssembly.Instance(new WebAssembly.Module(bytes), {}); |
| 122 | } catch (e) { |
| 123 | usage('module rejected: ' + e.message); |
| 124 | } |
| 125 | const ex = inst.exports; |
| 126 | const script = readFile(argv[1]).toString('utf8').split('\n'); |
| 127 | const out = []; |
| 128 | const flush = () => { if (out.length) process.stdout.write(out.join('')); out.length = 0; }; |
| 129 | for (let ln = 0; ln < script.length; ln++) { |
| 130 | const line = script[ln].trim(); |
| 131 | if (line === '' || line.startsWith('#')) continue; |
| 132 | const w = line.split(/\s+/); |
| 133 | const bad = (m) => { flush(); usage('script line ' + (ln + 1) + ': ' + m); }; |
| 134 | if (w[0] === 'call') { |
| 135 | const f = abi.funcs.find((g) => g.name === w[1]); |
| 136 | if (!f) bad('no function ' + w[1]); |
| 137 | if (w.length - 2 !== f.params.length) bad('expected ' + f.params.length + ' arguments'); |
| 138 | const vals = w.slice(2).map((s, k) => { |
| 139 | const v = parseArg(f.params[k], s); |
| 140 | if (v === null) bad('argument ' + s + ' does not fit tag ' + f.params[k]); |
| 141 | return v; |
| 142 | }); |
| 143 | try { |
| 144 | out.push(f.name + ' = ' + formatValue(f.ret, ex[f.name](...vals)) + '\n'); |
| 145 | } catch (e) { |
| 146 | if (!(e instanceof WebAssembly.RuntimeError)) { |
| 147 | flush(); process.stderr.write('v7run: ' + e.message + '\n'); return 3; |
| 148 | } |
| 149 | out.push(f.name + ' TRAP\n'); |
| 150 | } |
| 151 | } else if (w[0] === 'region' && w.length === 2) { |
| 152 | const rg = abi.regions.find((g) => g.name === w[1]); |
| 153 | if (!rg) bad('no module region ' + w[1]); |
| 154 | const base = ex['region.' + rg.name].value >>> 0; |
| 155 | const len = rg.count * (WIDTH[rg.tag] / 8); |
| 156 | const mem = new Uint8Array(ex.memory.buffer, base, len); |
| 157 | out.push('region ' + rg.name + ' ' + Buffer.from(mem).toString('hex') + '\n'); |
| 158 | } else bad('unknown command ' + JSON.stringify(line)); |
| 159 | } |
| 160 | flush(); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | process.exitCode = main(process.argv.slice(2)); |