interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / wasm / v7 / writer.js

453 lines · 24329 bytes Raw Blame History
1#!/usr/bin/env node
2// SPDX-License-Identifier: Apache-2.0
3// Hand-written OSC IR v1/v7 byte writer and fixture set for the v7 oracle.
4//
5// Independent of seed/wasm/v7/oracle.js (no shared code: this file encodes,
6// the oracle decodes) and of every producer/backend. Wire layout from
7// spec/one-layer-format.md sections 3 and 10.
8//
9// node writer.js OUTDIR writes OUTDIR/<fixture>.unit for every fixture
10// require('./writer.js').encode(desc) -> Buffer
11//
12// desc = { version: 1|7, regions: [{ name, tag, cls, owner, count, init }],
13// funcs: [{ name, params: [tag..], ret: tag, regs: [tag..] (after the
14// params), blocks: [[insn..]..] }] }
15// Tags may be names (u8 .. f64) or numbers. Register operand 'none' = 0xFFFF.
16'use strict';
17
18const fs = require('fs');
19const path = require('path');
20
21const TAG = { void: 0, bool: 1, u8: 2, u16: 3, u32: 4, u64: 5, i8: 6, i16: 7, i32: 8, i64: 9, f32: 13, f64: 14 };
22const tagOf = (t) => (typeof t === 'number' ? t : TAG[t]);
23const BINSUB = { add: 1, sub: 2, mul: 3, div: 4, rem: 5, and: 6, or: 7, xor: 8, shl: 9, shr: 10 };
24const UNSUB = { neg: 1, bnot: 2, lnot: 3 };
25const CMPSUB = { eq: 1, ne: 2, lt: 3, le: 4, gt: 5, ge: 6 };
26
27class Bytes {
28 constructor() { this.a = []; }
29 u8(v) { if (v < 0 || v > 255) throw new Error('u8 ' + v); this.a.push(v); }
30 u16(v) { this.u8(v & 0xff); this.u8((v >>> 8) & 0xff); }
31 u32(v) { for (let k = 0; k < 4; k++) this.u8(Math.floor(v / 2 ** (8 * k)) % 256); }
32 u64(v) { let x = BigInt.asUintN(64, BigInt(v)); for (let k = 0; k < 8; k++) { this.u8(Number(x & 0xffn)); x >>= 8n; } }
33 raw(list) { for (const b of list) this.u8(b); }
34 str(s, lenWidth) {
35 const b = Buffer.from(s, 'latin1');
36 if (lenWidth === 2) this.u16(b.length); else this.u8(b.length);
37 this.raw(b);
38 }
39}
40
41const regField = (v) => (v === 'none' ? 0xffff : v);
42
43function insn(w, ins) {
44 const [op, ...f] = ins;
45 const r = (v) => w.u16(regField(v));
46 switch (op) {
47 case 'const': w.u8(1); r(f[0]); w.u64(f[1]); break;
48 case 'mov': w.u8(2); r(f[0]); r(f[1]); break;
49 case 'bin': w.u8(3); w.u8(typeof f[0] === 'number' ? f[0] : BINSUB[f[0]]); r(f[1]); r(f[2]); r(f[3]); break;
50 case 'un': w.u8(4); w.u8(typeof f[0] === 'number' ? f[0] : UNSUB[f[0]]); r(f[1]); r(f[2]); break;
51 case 'cmp': w.u8(5); w.u8(CMPSUB[f[0]]); r(f[1]); r(f[2]); r(f[3]); break;
52 case 'cast': w.u8(6); r(f[0]); r(f[1]); break;
53 case 'call': w.u8(11); r(f[0]); r(f[1]); w.u8(f.length - 2); f.slice(2).forEach(r); break;
54 case 'trap': w.u8(12); w.u8(f[0]); break;
55 case 'br': w.u8(13); r(f[0]); break;
56 case 'cbr': w.u8(14); r(f[0]); r(f[1]); r(f[2]); break;
57 case 'ret': w.u8(15); r(f[0]); break;
58 case 'rload': w.u8(30); r(f[0]); r(f[1]); r(f[2]); break;
59 case 'rstore': w.u8(31); r(f[0]); r(f[1]); r(f[2]); break;
60 case 'bitcast': w.u8(32); r(f[0]); r(f[1]); break;
61 case 'rinit': w.u8(33); r(f[0]); r(f[1]); break;
62 case 'raw': w.raw(f); break; // opcode byte and fields verbatim
63 default: throw new Error('writer: unknown insn ' + op);
64 }
65}
66
67const TERMS = new Set(['br', 'cbr', 'ret']);
68
69function func(w, fn, version) {
70 w.str(fn.name, 1);
71 const params = fn.params || [];
72 w.u8(fn.nparams === undefined ? params.length : fn.nparams);
73 w.u8(tagOf(fn.ret || 'void'));
74 const regs = params.concat(fn.regs || []);
75 w.u16(regs.length);
76 regs.forEach((t) => w.u8(tagOf(t)));
77 const blocks = fn.blocks;
78 w.u16(blocks.length);
79 let first = 0;
80 for (const b of blocks) { w.u32(first); w.u32(b.length); first += b.length; }
81 w.u32(first);
82 for (const b of blocks) for (const i of b) insn(w, i);
83 w.str(fn.requires || '', 2);
84 w.str(fn.ensures || '', 2);
85 void version;
86}
87
88function encode(desc) {
89 const w = new Bytes();
90 w.raw([0x4f, 0x53, 0x43, 0x31, 0x49, 0x52, 0x00]);
91 w.u8(desc.version);
92 if (desc.version !== 1) {
93 w.u8(desc.nstructs || 0);
94 const regions = desc.regions || [];
95 w.u16(desc.nregions === undefined ? regions.length : desc.nregions);
96 for (const g of regions) {
97 w.str(g.name, 1);
98 w.u8(tagOf(g.tag));
99 w.u8(g.cls);
100 w.u16(g.owner === undefined ? (g.cls === 1 ? 0 : 0xffff) : g.owner);
101 w.u32(g.count);
102 const init = g.init ? Buffer.from(g.init, 'latin1') : Buffer.alloc(0);
103 w.u32(g.initlen === undefined ? init.length : g.initlen);
104 w.raw(init);
105 }
106 }
107 w.u16(desc.nfuncs === undefined ? desc.funcs.length : desc.nfuncs);
108 for (const fn of desc.funcs) func(w, fn, desc.version);
109 return Buffer.from(w.a);
110}
111
112// ------------------------------------------------------------- fixtures
113// Register numbering in each function: params first, then `regs`.
114const fx = {};
115const mod = (name, tag, count) => ({ name, tag, cls: 0, owner: 0xffff, count });
116const text = (name, s) => ({ name, tag: 'u8', cls: 2, owner: 0xffff, count: Buffer.from(s, 'latin1').length, init: s });
117const loc = (name, tag, count, owner) => ({ name, tag, cls: 1, owner, count });
118
119// set(i, v) -> void ; get(i) -> T over one module region 0 of element T.
120function setget(tag, idx, count, extra) {
121 return {
122 version: 7,
123 regions: [mod('g', tag, count)],
124 funcs: [
125 { name: 'set', params: [idx, tag], blocks: [[['rstore', 0, 0, 1], ['ret', 'none']]] },
126 { name: 'get', params: [idx], ret: tag, regs: [tag], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] },
127 ].concat(extra || []),
128 };
129}
130
131fx.rt_u8 = setget('u8', 'i32', 4);
132fx.rt_i32 = setget('i32', 'u32', 3);
133fx.rt_u64 = setget('u64', 'u64', 2, [
134 { name: 'geti', params: ['i64'], ret: 'u64', regs: ['u64'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] },
135]);
136fx.rt_f32 = setget('f32', 'u8', 2);
137fx.rt_f64 = setget('f64', 'i16', 2);
138
139// Text read, including an empty text table.
140fx.text_read = {
141 version: 7,
142 regions: [text('t', 'RGBX'), text('e', '')],
143 funcs: [
144 { name: 'tget', params: ['u32'], ret: 'u8', regs: ['u8'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] },
145 { name: 'eget', params: ['u32'], ret: 'u8', regs: ['u8'], blocks: [[['rload', 1, 1, 0], ['ret', 1]]] },
146 ],
147};
148
149// Local storage: initialized to 5 on every call, old value returned, then overwritten.
150// reinit: RINIT 5, store 9, RINIT 7, load -> 7 (re-init resets the whole region).
151fx.local_fresh = {
152 version: 7,
153 regions: [loc('l', 'u32', 4, 0), loc('l', 'u32', 2, 1)],
154 funcs: [
155 { name: 'swap', params: ['u32', 'u32'], ret: 'u32', regs: ['u32', 'u32'],
156 blocks: [[['const', 2, 5], ['rinit', 0, 2], ['rload', 3, 0, 0], ['rstore', 0, 0, 1], ['ret', 3]]] },
157 { name: 'reinit', params: ['u32'], ret: 'u32', regs: ['u32', 'u32', 'u32', 'u32'],
158 blocks: [[['const', 1, 5], ['rinit', 1, 1], ['const', 2, 9], ['rstore', 1, 0, 2],
159 ['const', 3, 7], ['rinit', 1, 3], ['rload', 4, 1, 0], ['ret', 4]]] },
160 ],
161};
162
163// Module persistence across calls, a partial write before a bounds trap, and
164// an overflow trap before any write.
165fx.persist = {
166 version: 7,
167 regions: [mod('c', 'u32', 1)],
168 funcs: [
169 { name: 'bump', ret: 'u32', regs: ['u64', 'u32', 'u32', 'u32'],
170 blocks: [[['const', 0, 0], ['rload', 1, 0, 0], ['const', 2, 1], ['bin', 'add', 3, 1, 2],
171 ['rstore', 0, 0, 3], ['ret', 3]]] },
172 { name: 'partial', ret: 'u32', regs: ['u64', 'u32', 'u32', 'u32', 'u64', 'u32'],
173 blocks: [[['const', 0, 0], ['rload', 1, 0, 0], ['const', 2, 1], ['bin', 'add', 3, 1, 2],
174 ['rstore', 0, 0, 3], ['const', 4, 1], ['rload', 5, 0, 4], ['ret', 5]]] },
175 { name: 'ovf', params: ['i32'], ret: 'u32', regs: ['i32', 'u64', 'u32', 'u32'],
176 blocks: [[['bin', 'add', 1, 0, 0], ['const', 2, 0], ['const', 3, 77], ['rstore', 0, 2, 3],
177 ['ret', 3]]] },
178 ],
179};
180
181// Bitcasts across each width set.
182const bc = (name, from, to) => ({ name, params: [from], ret: to, regs: [to], blocks: [[['bitcast', 1, 0], ['ret', 1]]] });
183fx.bitcast = {
184 version: 7,
185 funcs: [
186 bc('f32_u32', 'f32', 'u32'), bc('u32_f32', 'u32', 'f32'), bc('i32_f32', 'i32', 'f32'),
187 bc('f64_i64', 'f64', 'i64'), bc('u64_f64', 'u64', 'f64'), bc('u8_i8', 'u8', 'i8'),
188 bc('i8_u8', 'i8', 'u8'), bc('i16_u16', 'i16', 'u16'), bc('u16_i16', 'u16', 'i16'),
189 bc('f32_f32', 'f32', 'f32'),
190 ],
191};
192
193// Conversions (CAST).
194const cv = (name, from, to) => ({ name, params: [from], ret: to, regs: [to], blocks: [[['cast', 1, 0], ['ret', 1]]] });
195fx.convert = {
196 version: 7,
197 funcs: [
198 cv('f64_i32', 'f64', 'i32'), cv('f64_u8', 'f64', 'u8'), cv('f32_i8', 'f32', 'i8'),
199 cv('f64_u64', 'f64', 'u64'), cv('f64_i64', 'f64', 'i64'), cv('f32_u32', 'f32', 'u32'),
200 cv('f32_i16', 'f32', 'i16'),
201 cv('u64_f32', 'u64', 'f32'), cv('i32_f32', 'i32', 'f32'), cv('u64_f64', 'u64', 'f64'),
202 cv('i64_f64', 'i64', 'f64'), cv('f64_f32', 'f64', 'f32'), cv('f32_f64', 'f32', 'f64'),
203 cv('f64_f64', 'f64', 'f64'), cv('i32_u8', 'i32', 'u8'), cv('u64_i64', 'u64', 'i64'),
204 ],
205};
206
207// Float arithmetic, negation and comparison.
208const fb = (name, t, sub) => ({ name, params: [t, t], ret: t, regs: [t], blocks: [[['bin', sub, 2, 0, 1], ['ret', 2]]] });
209const fc = (name, t, sub) => ({ name, params: [t, t], ret: 'bool', regs: ['bool'], blocks: [[['cmp', sub, 2, 0, 1], ['ret', 2]]] });
210fx.float_ops = {
211 version: 7,
212 funcs: [
213 fb('fadd', 'f32', 'add'), fb('fmul', 'f32', 'mul'), fb('dsub', 'f64', 'sub'), fb('ddiv', 'f64', 'div'),
214 { name: 'fneg', params: ['f32'], ret: 'f32', regs: ['f32'], blocks: [[['un', 'neg', 1, 0], ['ret', 1]]] },
215 { name: 'dneg', params: ['f64'], ret: 'f64', regs: ['f64'], blocks: [[['un', 'neg', 1, 0], ['ret', 1]]] },
216 fc('feq', 'f32', 'eq'), fc('fne', 'f32', 'ne'), fc('flt', 'f32', 'lt'), fc('dge', 'f64', 'ge'),
217 { name: 'kf', ret: 'f32', regs: ['f32'], blocks: [[['const', 0, 0x7fa00001], ['ret', 0]]] },
218 { name: 'kd', ret: 'f64', regs: ['f64'], blocks: [[['const', 0, 0xfff0000000000001n], ['ret', 0]]] },
219 ],
220};
221
222// Initialization on both branches of a diamond, used after the join (accept).
223fx.init_diamond = {
224 version: 7,
225 regions: [loc('d', 'i16', 3, 0)],
226 funcs: [
227 { name: 'pick', params: ['bool'], ret: 'i16', regs: ['i16', 'i16', 'u8', 'i16'],
228 blocks: [
229 [['cbr', 0, 1, 2]],
230 [['const', 1, 0xfffffffffffffffdn], ['rinit', 0, 1], ['br', 3]],
231 [['const', 2, 4], ['rinit', 0, 2], ['br', 3]],
232 [['const', 3, 2], ['rload', 4, 0, 3], ['ret', 4]],
233 ] },
234 ],
235};
236
237// v1 units (no region, no float, no v7 opcode).
238fx.v1_arith = {
239 version: 1,
240 funcs: [
241 { name: 'addi', params: ['i32', 'i32'], ret: 'i32', regs: ['i32'], blocks: [[['bin', 'add', 2, 0, 1], ['ret', 2]]] },
242 { name: 'addu', params: ['u8', 'u8'], ret: 'u8', regs: ['u8'], blocks: [[['bin', 'add', 2, 0, 1], ['ret', 2]]] },
243 { name: 'divi', params: ['i64', 'i64'], ret: 'i64', regs: ['i64'], blocks: [[['bin', 'div', 2, 0, 1], ['ret', 2]]] },
244 { name: 'remi', params: ['i64', 'i64'], ret: 'i64', regs: ['i64'], blocks: [[['bin', 'rem', 2, 0, 1], ['ret', 2]]] },
245 { name: 'shl8', params: ['i8', 'u32'], ret: 'i8', regs: ['i8'], blocks: [[['bin', 'shl', 2, 0, 1], ['ret', 2]]] },
246 { name: 'shr8', params: ['i8', 'u32'], ret: 'i8', regs: ['i8'], blocks: [[['bin', 'shr', 2, 0, 1], ['ret', 2]]] },
247 { name: 'negu', params: ['u16'], ret: 'u16', regs: ['u16'], blocks: [[['un', 'neg', 1, 0], ['ret', 1]]] },
248 { name: 'memory', params: ['bool'], ret: 'bool', regs: ['bool'], blocks: [[['un', 'lnot', 1, 0], ['ret', 1]]] },
249 { name: 'stop', params: [], ret: 'void', blocks: [[['trap', 9], ['ret', 'none']]] },
250 ],
251};
252// sum(n) = 0 + 1 + .. + (n-1), looping; twice(n) calls sum.
253fx.v1_loop = {
254 version: 1,
255 funcs: [
256 { name: 'sum', params: ['u32'], ret: 'u32', regs: ['u32', 'u32', 'bool', 'u32'],
257 blocks: [
258 [['const', 1, 0], ['const', 2, 0], ['br', 1]],
259 [['cmp', 'lt', 3, 2, 0], ['cbr', 3, 2, 3]],
260 [['bin', 'add', 1, 1, 2], ['const', 4, 1], ['bin', 'add', 2, 2, 4], ['br', 1]],
261 [['ret', 1]],
262 ] },
263 { name: 'twice', params: ['u32'], ret: 'u32', regs: ['u32', 'u32'],
264 blocks: [[['call', 1, 0, 0], ['bin', 'add', 2, 1, 1], ['ret', 2]]] },
265 ],
266};
267
268// ----------------------------------------------------- malformed units
269const tiny = (extra) => Object.assign({
270 version: 7,
271 regions: [mod('g', 'u32', 2)],
272 funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }],
273}, extra || {});
274const local1 = (body, regs, extra) => Object.assign({
275 version: 7,
276 regions: [loc('l', 'u32', 2, 0)],
277 funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: regs || ['u32', 'u32'], blocks: body }],
278}, extra || {});
279
280// Reader classes.
281const bytesOf = {};
282bytesOf.bad_magic = () => { const b = encode(tiny()); b[3] = 0x32; return b; };
283bytesOf.bad_version2 = () => { const b = encode(tiny()); b[7] = 2; return b; };
284bytesOf.bad_version6 = () => { const b = encode(tiny()); b[7] = 6; return b; };
285bytesOf.bad_version8 = () => { const b = encode(tiny()); b[7] = 8; return b; };
286bytesOf.trunc_empty = () => Buffer.alloc(0);
287bytesOf.trunc_magic = () => encode(tiny()).subarray(0, 4);
288bytesOf.trunc_header = () => encode(tiny()).subarray(0, 8);
289bytesOf.trunc_last = () => { const b = encode(tiny()); return b.subarray(0, b.length - 1); };
290bytesOf.trunc_initdata = () => encode({ version: 7, regions: [text('t', 'RGBX')], funcs: [] }).subarray(0, 8 + 1 + 2 + 2 + 1 + 1 + 2 + 4 + 4 + 2);
291bytesOf.trailing = () => Buffer.concat([encode(tiny()), Buffer.from([0])]);
292bytesOf.cap_v7_input = () => { const b = Buffer.alloc(8388609); encode(tiny()).copy(b); return b; };
293bytesOf.cap_v1_input = () => { const b = Buffer.alloc(2620252); encode(fx.v1_arith).copy(b); return b; };
294bytesOf.v7_input_at_v1_cap = () => {
295 // A v7 header over the v1 cap is not a capacity refusal; it fails later as TRAILING.
296 const b = Buffer.alloc(2620252); encode(tiny()).copy(b); return b;
297};
298
299fx.cap_nfuncs = { version: 7, regions: [mod('g', 'u8', 1)], nfuncs: 33, funcs: [] };
300fx.cap_fname = tiny({ funcs: [{ name: 'x'.repeat(64), ret: 'void', blocks: [[['ret', 'none']]] }] });
301fx.cap_nstructs = tiny({ nstructs: 17 });
302fx.unsup_structs = tiny({ nstructs: 1 });
303fx.unsup_class3 = tiny({ regions: [{ name: 'w', tag: 'u32', cls: 3, owner: 0xffff, count: 4 }] });
304fx.unsup_op7 = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['raw', 7, 1, 0, 0, 0], ['ret', 0]]] }] });
305fx.unsup_op20 = tiny({ funcs: [{ name: 'f', ret: 'void', blocks: [[['raw', 20, 0, 0], ['ret', 'none']]] }] });
306fx.unsup_op34 = tiny({ funcs: [{ name: 'f', ret: 'void', blocks: [[['raw', 34], ['ret', 'none']]] }] });
307fx.unsup_op0 = tiny({ funcs: [{ name: 'f', ret: 'void', blocks: [[['raw', 0], ['ret', 'none']]] }] });
308fx.unsup_op9 = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['raw', 9, 1, 0, 0, 0, 0, 0], ['ret', 1]]] }] });
309fx.v1_unsup_op10 = { version: 1, funcs: [{ name: 'f', params: ['u32'], ret: 'void', blocks: [[['raw', 10, 0, 0, 0, 0, 0, 0], ['ret', 'none']]] }] };
310fx.unsup_tag10 = tiny({ funcs: [{ name: 'f', ret: 'void', regs: [10], blocks: [[['ret', 'none']]] }] });
311fx.unsup_tag15 = tiny({ funcs: [{ name: 'f', ret: 'void', regs: [15], blocks: [[['ret', 'none']]] }] });
312fx.v1_op30 = { version: 1, funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['raw', 30, 1, 0, 0, 0, 0, 0], ['ret', 1]]] }] };
313fx.v1_tag13 = { version: 1, funcs: [{ name: 'f', params: ['f32'], ret: 'void', blocks: [[['ret', 'none']]] }] };
314fx.v1_tag14_ret = { version: 1, funcs: [{ name: 'f', ret: 'f64', regs: ['f64'], blocks: [[['const', 0, 0], ['ret', 0]]] }] };
315
316// Region table INVALIDs.
317const oneRegion = (g) => tiny({ regions: [g] });
318fx.inv_class4 = oneRegion({ name: 'g', tag: 'u32', cls: 4, owner: 0xffff, count: 2 });
319fx.inv_rtag_bool = oneRegion({ name: 'g', tag: 'bool', cls: 0, owner: 0xffff, count: 2 });
320fx.inv_rtag_cells = oneRegion({ name: 'g', tag: 12, cls: 0, owner: 0xffff, count: 2 });
321fx.inv_rname_empty = oneRegion({ name: '', tag: 'u32', cls: 0, owner: 0xffff, count: 2 });
322fx.inv_rname_dot = oneRegion({ name: 'a.b', tag: 'u32', cls: 0, owner: 0xffff, count: 2 });
323fx.inv_rname_nul = oneRegion({ name: 'a\u0000', tag: 'u32', cls: 0, owner: 0xffff, count: 2 });
324fx.inv_rname_8bit = oneRegion({ name: 'g\u00e9', tag: 'u32', cls: 0, owner: 0xffff, count: 2 });
325fx.inv_rname_64 = oneRegion({ name: 'r'.repeat(64), tag: 'u32', cls: 0, owner: 0xffff, count: 2 });
326fx.inv_mod_init = oneRegion({ name: 'g', tag: 'u32', cls: 0, owner: 0xffff, count: 1, init: 'abcd' });
327fx.inv_text_len = oneRegion({ name: 'g', tag: 'u8', cls: 2, owner: 0xffff, count: 5, init: 'RGBX' });
328fx.inv_text_u32 = oneRegion({ name: 'g', tag: 'u32', cls: 2, owner: 0xffff, count: 4, init: 'RGBX' });
329fx.inv_count0 = oneRegion({ name: 'g', tag: 'u32', cls: 0, owner: 0xffff, count: 0 });
330fx.inv_count_big = oneRegion({ name: 'g', tag: 'u8', cls: 0, owner: 0xffff, count: 16777217 });
331fx.ok_count_max = tiny({ regions: [mod('g', 'u32', 2), mod('h', 'u8', 16777216)] });
332fx.inv_nregions = tiny({ nregions: 1025, regions: [] });
333fx.inv_owner_local_module = tiny({ regions: [mod('g', 'u32', 2), loc('l', 'u32', 2, 0xffff)] });
334fx.inv_owner_missing = tiny({ regions: [mod('g', 'u32', 2), loc('l', 'u32', 2, 1)] });
335fx.inv_owner_module0 = oneRegion({ name: 'g', tag: 'u32', cls: 0, owner: 0, count: 2 });
336fx.inv_dup_module = tiny({ regions: [mod('g', 'u32', 2), text('g', 'xy')] });
337fx.inv_shadow = tiny({ regions: [mod('g', 'u32', 2), loc('g', 'u32', 2, 0)] });
338fx.inv_dup_local = tiny({ regions: [mod('g', 'u32', 2), loc('l', 'u8', 1, 0), loc('l', 'u8', 1, 0)] });
339
340// Export collisions (v7 reserves memory and region.NAME); duplicate functions.
341const named = (n) => ({ name: n, ret: 'void', blocks: [[['ret', 'none']]] });
342fx.inv_export_memory = tiny({ funcs: tiny().funcs.concat([named('memory')]) });
343fx.inv_export_region = tiny({ funcs: tiny().funcs.concat([named('region.g')]) });
344fx.inv_dup_func = tiny({ funcs: tiny().funcs.concat([named('f')]) });
345fx.inv_v1_dup_func = { version: 1, funcs: [named('f'), named('f')] };
346fx.ok_fname_empty = tiny({ funcs: tiny().funcs.concat([named('')]) });
347fx.inv_fname_8bit = tiny({ funcs: tiny().funcs.concat([named('h\u00e9')]) });
348
349// Local initialization controls.
350fx.inv_missing_init = local1([[['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]]]);
351fx.inv_init_after_use = local1([[['const', 1, 0], ['rload', 2, 0, 1], ['rinit', 0, 0], ['ret', 2]]]);
352fx.inv_store_before_init = local1([[['const', 1, 0], ['rstore', 0, 1, 0], ['rinit', 0, 0], ['ret', 0]]]);
353fx.inv_init_one_branch = local1([
354 [['const', 1, 0], ['cmp', 'eq', 3, 0, 1], ['cbr', 3, 1, 2]],
355 [['rinit', 0, 0], ['br', 2]],
356 [['rload', 2, 0, 1], ['ret', 2]],
357], ['u32', 'u32', 'bool']);
358// Header 1 loads; RINIT happens only in body 2, which loops back to 1.
359fx.inv_loop_carried = local1([
360 [['const', 1, 0], ['br', 1]],
361 [['rload', 2, 0, 1], ['cmp', 'eq', 3, 2, 0], ['cbr', 3, 3, 2]],
362 [['rinit', 0, 0], ['br', 1]],
363 [['ret', 2]],
364], ['u32', 'u32', 'bool']);
365fx.ok_loop_init_before = local1([
366 [['const', 1, 0], ['rinit', 0, 0], ['br', 1]],
367 [['rload', 2, 0, 1], ['cmp', 'eq', 3, 2, 0], ['cbr', 3, 3, 2]],
368 [['rinit', 0, 1], ['br', 1]],
369 [['ret', 2]],
370], ['u32', 'u32', 'bool']);
371fx.inv_unreachable_use = local1([
372 [['rinit', 0, 0], ['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]],
373 [['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]],
374]);
375fx.inv_reg_undef = local1([[['rinit', 0, 0], ['rload', 2, 0, 1], ['ret', 2]]]);
376
377// Ownership.
378const twoFuncLocal = (body1) => ({
379 version: 7,
380 regions: [loc('l', 'u32', 2, 0), mod('g', 'u32', 2)],
381 funcs: [
382 { name: 'a', params: ['u32'], ret: 'u32', regs: ['u32', 'u32'],
383 blocks: [[['rinit', 0, 0], ['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]]] },
384 { name: 'b', params: ['u32'], ret: 'u32', regs: ['u32', 'u32'], blocks: [body1] },
385 ],
386});
387fx.ok_own_local = twoFuncLocal([['rload', 2, 1, 0], ['ret', 2]]);
388fx.inv_foreign_load = twoFuncLocal([['const', 1, 0], ['rload', 2, 0, 1], ['ret', 2]]);
389fx.inv_foreign_init = twoFuncLocal([['rinit', 0, 0], ['ret', 0]]);
390fx.inv_foreign_store = twoFuncLocal([['const', 1, 0], ['rstore', 0, 1, 0], ['ret', 0]]);
391fx.inv_rinit_module = twoFuncLocal([['rinit', 1, 0], ['ret', 0]]);
392
393// Read-only text.
394const textUnit = (body, regs) => ({
395 version: 7,
396 regions: [text('t', 'RGBX')],
397 funcs: [{ name: 'f', params: ['u8'], ret: 'u8', regs: regs || ['u32'], blocks: [body] }],
398});
399fx.inv_text_store = textUnit([['const', 1, 0], ['rstore', 0, 1, 0], ['ret', 0]]);
400fx.inv_text_init = textUnit([['rinit', 0, 0], ['ret', 0]]);
401
402// Operands: sentinel / out of table.
403fx.inv_rload_dst_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', blocks: [[['rload', 'none', 0, 0], ['ret', 0]]] }] });
404fx.inv_rload_region_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 'none', 0], ['ret', 1]]] }] });
405fx.inv_rload_index_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 'none'], ['ret', 1]]] }] });
406fx.inv_rload_region_oob = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 1, 0], ['ret', 1]]] }] });
407fx.inv_rload_index_8000 = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0x8000], ['ret', 1]]] }] });
408fx.inv_rstore_src_fffe = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', blocks: [[['rstore', 0, 0, 0xfffe], ['ret', 0]]] }] });
409fx.inv_rstore_src_oob = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'u32', blocks: [[['rstore', 0, 0, 1], ['ret', 0]]] }] });
410fx.inv_bitcast_none = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'i32', regs: ['i32'], blocks: [[['bitcast', 1, 'none'], ['ret', 1]]] }] });
411fx.inv_rinit_none = local1([[['rinit', 0, 'none'], ['ret', 0]]]);
412fx.inv_rinit_region_none = local1([[['rinit', 'none', 0], ['ret', 0]]]);
413
414// Typing.
415fx.inv_index_float = tiny({ funcs: [{ name: 'f', params: ['f32'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }] });
416fx.inv_index_bool = tiny({ funcs: [{ name: 'f', params: ['bool'], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }] });
417fx.inv_rload_type = tiny({ funcs: [{ name: 'f', params: ['u32'], ret: 'i32', regs: ['i32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }] });
418fx.inv_rstore_type = tiny({ funcs: [{ name: 'f', params: ['u32', 'u64'], ret: 'void', blocks: [[['rstore', 0, 0, 1], ['ret', 'none']]] }] });
419fx.inv_rinit_type = local1([[['const', 1, 0], ['cast', 2, 1], ['rinit', 0, 2], ['ret', 0]]], ['u32', 'i32']);
420fx.inv_bitcast_cross = { version: 7, funcs: [bc('f', 'u32', 'f64')] };
421fx.inv_bitcast_bool = { version: 7, funcs: [bc('f', 'bool', 'u8'), bc('g', 'f32', 'f32')] };
422fx.inv_bitcast_width = { version: 7, funcs: [bc('f', 'u8', 'u16'), bc('g', 'f32', 'f32')] };
423fx.inv_float_rem = { version: 7, funcs: [fb('f', 'f32', 'rem')] };
424fx.inv_float_shl = { version: 7, funcs: [fb('f', 'f64', 'shl')] };
425fx.inv_float_and = { version: 7, funcs: [fb('f', 'f64', 'and')] };
426fx.inv_float_bnot = { version: 7, funcs: [{ name: 'f', params: ['f32'], ret: 'f32', regs: ['f32'], blocks: [[['un', 'bnot', 1, 0], ['ret', 1]]] }] };
427fx.inv_float_lnot = { version: 7, funcs: [{ name: 'f', params: ['f64'], ret: 'f64', regs: ['f64'], blocks: [[['un', 'lnot', 1, 0], ['ret', 1]]] }] };
428fx.inv_f32_const_upper = { version: 7, funcs: [{ name: 'f', ret: 'f32', regs: ['f32'], blocks: [[['const', 0, 0x100000000n], ['ret', 0]]] }] };
429fx.inv_cast_bool = { version: 7, funcs: [cv('f', 'bool', 'f32')] };
430fx.inv_cast_to_bool = { version: 7, funcs: [cv('f', 'f64', 'bool')] };
431
432// Version selection.
433fx.gratuitous_v7 = { version: 7, funcs: fx.v1_loop.funcs };
434fx.gratuitous_v7_empty = { version: 7, funcs: [] };
435
436function writeAll(dir) {
437 fs.mkdirSync(dir, { recursive: true });
438 const names = [];
439 for (const [n, d] of Object.entries(fx)) { fs.writeFileSync(path.join(dir, n + '.unit'), encode(d)); names.push(n); }
440 for (const [n, f] of Object.entries(bytesOf)) { fs.writeFileSync(path.join(dir, n + '.unit'), f()); names.push(n); }
441 return names;
442}
443
444module.exports = { encode, fixtures: fx, rawFixtures: bytesOf, writeAll };
445
446if (require.main === module) {
447 if (process.argv.length !== 3) {
448 process.stderr.write('usage: writer.js OUTDIR\n');
449 process.exit(2);
450 }
451 const names = writeAll(process.argv[2]);
452 process.stdout.write(names.length + ' fixtures\n');
453}