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 / v7cases.js
| 1 | #!/usr/bin/env node |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | // irwasm v7 run cases: unit descriptions and scripts for gate-irwasm.sh. |
| 4 | // |
| 5 | // node seed/wasm/v7cases.js OUTDIR |
| 6 | // writes OUTDIR/<case>.unit and OUTDIR/<case>.script. The bytes come from the |
| 7 | // independent hand writer seed/wasm/v7/writer.js; neither file shares code |
| 8 | // with irwasm.om. Scripts use the oracle `run` format (seed/wasm/v7/oracle.js). |
| 9 | // Float arguments are raw IEEE bit patterns; every pattern below was |
| 10 | // computed with Wolfram (query quoted in the PR that added this file). |
| 11 | // NaN payloads never cross the JS Number boundary here: they are checked |
| 12 | // through region bytes and BITCAST to integers (case `nanpay`). |
| 13 | 'use strict'; |
| 14 | |
| 15 | const fs = require('fs'); |
| 16 | const path = require('path'); |
| 17 | const { encode, fixtures } = require('./v7/writer.js'); |
| 18 | |
| 19 | const mod = (name, tag, count) => ({ name, tag, cls: 0, owner: 0xffff, count }); |
| 20 | const text = (name, s) => ({ name, tag: 'u8', cls: 2, owner: 0xffff, count: s.length, init: s }); |
| 21 | const loc = (name, tag, count, owner) => ({ name, tag, cls: 1, owner, count }); |
| 22 | const cases = {}; |
| 23 | const W = 16, H = 8; |
| 24 | |
| 25 | // RGBX gradient: fill() writes W*H packed u32 pixels into the global |
| 26 | // framebuffer `fb`; the byte lane of each channel is its position in the text |
| 27 | // region `order`, found by shift(c). R = 16x, G = 32y, B = 8(x+y), X = 255. |
| 28 | function rgbx(order, bmul) { |
| 29 | return { |
| 30 | version: 7, |
| 31 | regions: [mod('fb', 'u32', W * H), text('order', order)], |
| 32 | funcs: [ |
| 33 | // shift(c u8) -> u32: 8 * (index of c in order) |
| 34 | { name: 'shift', params: ['u8'], ret: 'u32', regs: ['u32', 'u8', 'bool', 'u32', 'u32', 'u32'], |
| 35 | blocks: [ |
| 36 | [['const', 1, 0], ['br', 1]], |
| 37 | [['rload', 2, 1, 1], ['cmp', 'eq', 3, 2, 0], ['cbr', 3, 2, 3]], |
| 38 | [['const', 4, 8], ['bin', 'mul', 5, 1, 4], ['ret', 5]], |
| 39 | [['const', 6, 1], ['bin', 'add', 1, 1, 6], ['br', 1]], |
| 40 | ] }, |
| 41 | // fill() -> u32: pixel count written |
| 42 | { name: 'fill', params: [], ret: 'u32', |
| 43 | // 0 i, 1 n, 2 w, 3 cond, 4 x, 5 y, 6 k, 7 R, 8 G, 9 B, 10 X, |
| 44 | // 11 c, 12 s, 13 p, 14 q, 15 one |
| 45 | regs: ['u32', 'u32', 'u32', 'bool', 'u32', 'u32', 'u32', 'u32', 'u32', 'u32', 'u32', |
| 46 | 'u8', 'u32', 'u32', 'u32', 'u32'], |
| 47 | blocks: [ |
| 48 | [['const', 0, 0], ['const', 1, W * H], ['const', 2, W], ['br', 1]], |
| 49 | [['cmp', 'lt', 3, 0, 1], ['cbr', 3, 2, 3]], |
| 50 | [['bin', 'rem', 4, 0, 2], ['bin', 'div', 5, 0, 2], |
| 51 | ['const', 6, 16], ['bin', 'mul', 7, 4, 6], |
| 52 | ['const', 6, 32], ['bin', 'mul', 8, 5, 6], |
| 53 | ['bin', 'add', 9, 4, 5], ['const', 6, bmul], ['bin', 'mul', 9, 9, 6], |
| 54 | ['const', 10, 255], |
| 55 | ['const', 11, 82], ['call', 12, 0, 11], ['bin', 'shl', 13, 7, 12], |
| 56 | ['const', 11, 71], ['call', 12, 0, 11], ['bin', 'shl', 14, 8, 12], ['bin', 'or', 13, 13, 14], |
| 57 | ['const', 11, 66], ['call', 12, 0, 11], ['bin', 'shl', 14, 9, 12], ['bin', 'or', 13, 13, 14], |
| 58 | ['const', 11, 88], ['call', 12, 0, 11], ['bin', 'shl', 14, 10, 12], ['bin', 'or', 13, 13, 14], |
| 59 | ['rstore', 0, 0, 13], ['const', 15, 1], ['bin', 'add', 0, 0, 15], ['br', 1]], |
| 60 | [['ret', 1]], |
| 61 | ] }, |
| 62 | ], |
| 63 | }; |
| 64 | } |
| 65 | const rgbxScript = 'region fb\ncall fill\nregion fb\nregion order\ncall shift 88\n'; |
| 66 | cases.rgbx = [rgbx('RGBX', 8), rgbxScript]; |
| 67 | // Mutants for the checksum negative controls: B channel 9(x+y), and order BGRX. |
| 68 | cases.rgbx_mut_b = [rgbx('RGBX', 9), rgbxScript]; |
| 69 | cases.rgbx_mut_order = [rgbx('BGRX', 8), rgbxScript]; |
| 70 | |
| 71 | // Out-of-bounds: module region g (u32 x 5) and a local region l (u8 x 3), |
| 72 | // indexed through every integer width. Index == count, count + 1, negative, |
| 73 | // and wide values whose low 32 bits are in range must all trap. |
| 74 | const ld = (name, idx) => ({ name, params: [idx], ret: 'u32', regs: ['u32'], blocks: [[['rload', 1, 0, 0], ['ret', 1]]] }); |
| 75 | cases.oob = [{ |
| 76 | version: 7, |
| 77 | regions: [mod('g', 'u32', 5), loc('l', 'u8', 3, 6)], |
| 78 | funcs: [ |
| 79 | ld('gu32', 'u32'), ld('gi32', 'i32'), ld('gi64', 'i64'), ld('gu64', 'u64'), ld('gi8', 'i8'), |
| 80 | { name: 'si64', params: ['i64', 'u32'], ret: 'void', blocks: [[['rstore', 0, 0, 1], ['ret', 'none']]] }, |
| 81 | { name: 'lu64', params: ['u64'], ret: 'u8', regs: ['u8', 'u8'], |
| 82 | blocks: [[['const', 1, 9], ['rinit', 1, 1], ['rload', 2, 1, 0], ['ret', 2]]] }, |
| 83 | ], |
| 84 | }, [ |
| 85 | 'call si64 0 11', 'call si64 4 44', 'call gu32 0', 'call gu32 4', |
| 86 | 'call gu32 5', 'call gu32 6', 'call gu32 4294967295', |
| 87 | 'call gi32 -1', 'call gi32 -2147483648', 'call gi32 2147483647', 'call gi32 4', |
| 88 | 'call gi64 -1', 'call gi64 -9223372036854775808', 'call gi64 4294967296', 'call gi64 4294967300', 'call gi64 3', |
| 89 | 'call gu64 18446744073709551615', 'call gu64 4294967296', 'call gu64 4294967300', 'call gu64 5', 'call gu64 2', |
| 90 | 'call gi8 -1', 'call gi8 -128', 'call gi8 5', 'call gi8 1', |
| 91 | 'call si64 5 99', 'call si64 -1 99', 'call si64 4294967296 99', 'region g', |
| 92 | 'call lu64 2', 'call lu64 3', 'call lu64 4294967298', 'call lu64 18446744073709551615', |
| 93 | ].join('\n') + '\n']; |
| 94 | |
| 95 | // NaN payloads through CONST -> RSTORE -> region bytes, and RLOAD -> BITCAST -> integer. |
| 96 | cases.nanpay = [{ |
| 97 | version: 7, |
| 98 | regions: [mod('nf', 'f32', 2), mod('nd', 'f64', 2)], |
| 99 | funcs: [ |
| 100 | { name: 'put', ret: 'void', regs: ['u64', 'f32', 'f64', 'u64', 'f32', 'f64'], |
| 101 | blocks: [[['const', 0, 0], ['const', 1, 0x7fa00001], ['rstore', 0, 0, 1], |
| 102 | ['const', 2, 0xfff0000000000001n], ['rstore', 1, 0, 2], |
| 103 | ['const', 3, 1], ['const', 4, 0xffc00123], ['rstore', 0, 3, 4], |
| 104 | ['const', 5, 0x7ff8000000000abcn], ['rstore', 1, 3, 5], ['ret', 'none']]] }, |
| 105 | { name: 'fbits', params: ['u32'], ret: 'u32', regs: ['f32', 'u32'], |
| 106 | blocks: [[['rload', 1, 0, 0], ['bitcast', 2, 1], ['ret', 2]]] }, |
| 107 | { name: 'dbits', params: ['u32'], ret: 'u64', regs: ['f64', 'u64'], |
| 108 | blocks: [[['rload', 1, 1, 0], ['bitcast', 2, 1], ['ret', 2]]] }, |
| 109 | // u32 -> f32 bits -> negate -> back: sign flip keeps the payload |
| 110 | { name: 'negbits', params: ['u32'], ret: 'u32', regs: ['f32', 'f32', 'u32'], |
| 111 | blocks: [[['bitcast', 1, 0], ['un', 'neg', 2, 1], ['bitcast', 3, 2], ['ret', 3]]] }, |
| 112 | ], |
| 113 | }, 'region nf\nregion nd\ncall put\nregion nf\nregion nd\ncall fbits 0\ncall fbits 1\n' + |
| 114 | 'call dbits 0\ncall dbits 1\ncall negbits 2141192193\ncall negbits 1065353216\ncall fbits 2\n']; |
| 115 | |
| 116 | // Scripts for the accepting fixtures of the independent writer. |
| 117 | const F64 = { |
| 118 | nan: '0x7ff8000000000000', inf: '0x7ff0000000000000', ninf: '0xfff0000000000000', |
| 119 | nzero: '0x8000000000000000', zero: '0x0000000000000000', |
| 120 | p39: '0x400f333333333333', n39: '0xc00f333333333333', i32hi: '0x41dfffffffe00000', |
| 121 | t31: '0x41e0000000000000', i32lo: '0xc1e0000000200000', big: '0x7e37e43c8800759c', |
| 122 | nbig: '0xfe37e43c8800759c', u8hi: '0x406ffccccccccccd', t8: '0x4070000000000000', |
| 123 | m1: '0xbff0000000000000', mhalf: '0xbfe0000000000000', t63: '0x43e0000000000000', |
| 124 | nt63: '0xc3e0000000000000', t64: '0x43f0000000000000', u64hi: '0x43efffffffffffff', |
| 125 | i64hi: '0x43dfffffffffffff', i64lo: '0xc3e0000000000001', tenth: '0x3fb999999999999a', |
| 126 | p15: '0x3ff8000000000000', p25: '0x4004000000000000', k1: '0x408f400000000000', |
| 127 | }; |
| 128 | const F32 = { |
| 129 | nan: '0x7fc00000', inf: '0x7f800000', ninf: '0xff800000', nzero: '0x80000000', |
| 130 | p39: '0x4079999a', n39: '0xc079999a', t7: '0x43000000', n129: '0xc3010000', p127: '0x42ff0000', |
| 131 | t32: '0x4f800000', u32hi: '0x4f7fffff', m1: '0xbf800000', i16hi: '0x46ffff00', i16lo: '0xc7000100', |
| 132 | big: '0x7149f2ca', tenth: '0x3dcccccd', p15: '0x3fc00000', t31: '0x4f000000', nt31: '0xcf000000', |
| 133 | k1: '0x447a0000', |
| 134 | }; |
| 135 | const calls = (name, args) => args.map((a) => 'call ' + name + ' ' + a).join('\n') + '\n'; |
| 136 | const d = (...k) => k.map((x) => F64[x]); |
| 137 | const f = (...k) => k.map((x) => F32[x]); |
| 138 | // Signed narrow elements: sign extension on RLOAD, truncation on RSTORE. |
| 139 | const sg = (tag) => [ |
| 140 | { name: 'set' + tag, params: ['u32', tag], ret: 'void', blocks: [[['rstore', tag === 'i8' ? 0 : 1, 0, 1], ['ret', 'none']]] }, |
| 141 | { name: 'get' + tag, params: ['u32'], ret: tag, regs: [tag], blocks: [[['rload', 1, tag === 'i8' ? 0 : 1, 0], ['ret', 1]]] }, |
| 142 | ]; |
| 143 | cases.signed = [{ |
| 144 | version: 7, |
| 145 | regions: [mod('b', 'i8', 2), mod('h', 'i16', 2)], |
| 146 | funcs: sg('i8').concat(sg('i16'), [ |
| 147 | // widen after load: a non-canonical (zero-extended) register would show here |
| 148 | { name: 'wide8', params: ['u32'], ret: 'i64', regs: ['i8', 'i64'], |
| 149 | blocks: [[['rload', 1, 0, 0], ['cast', 2, 1], ['ret', 2]]] }, |
| 150 | { name: 'wide16', params: ['u32'], ret: 'i64', regs: ['i16', 'i64'], |
| 151 | blocks: [[['rload', 1, 1, 0], ['cast', 2, 1], ['ret', 2]]] }, |
| 152 | ]), |
| 153 | }, 'call seti8 0 -3\ncall seti8 1 127\ncall geti8 0\ncall geti8 1\ncall seti16 1 -32768\ncall geti16 1\n' + |
| 154 | 'call geti16 0\ncall wide8 0\ncall wide8 1\ncall wide16 1\nregion b\nregion h\n']; |
| 155 | |
| 156 | const scripts = { |
| 157 | rt_u8: 'call set 0 7\ncall set 3 255\ncall get 0\ncall get 3\ncall get 1\ncall get 4\ncall get -1\ncall set 4 1\nregion g\n', |
| 158 | rt_i32: 'call set 0 -5\ncall set 2 2147483647\ncall get 0\ncall get 2\ncall get 3\ncall set 3 1\nregion g\n', |
| 159 | rt_u64: 'call set 1 18446744073709551615\ncall get 1\ncall geti 1\ncall geti -1\ncall get 2\nregion g\n', |
| 160 | rt_f32: 'call set 1 0x4079999a\ncall set 0 0xbf800000\ncall get 1\ncall get 0\ncall get 2\nregion g\n', |
| 161 | rt_f64: 'call set 1 0x400f333333333333\ncall set 0 0x8000000000000000\ncall get 1\ncall get 0\ncall get -1\ncall get 2\nregion g\n', |
| 162 | text_read: 'region t\nregion e\ncall tget 0\ncall tget 3\ncall tget 4\ncall eget 0\n', |
| 163 | local_fresh: 'call swap 0 9\ncall swap 0 11\ncall swap 3 1\ncall swap 4 1\ncall reinit 0\ncall reinit 1\ncall reinit 2\n', |
| 164 | persist: 'region c\ncall bump\ncall bump\ncall partial\nregion c\ncall ovf 5\ncall ovf 2000000000\nregion c\ncall bump\n', |
| 165 | bitcast: calls('f32_u32', f('p39', 'nzero', 'inf')) + calls('u32_f32', ['1065353216', '0', '4286578688']) + |
| 166 | calls('i32_f32', ['-1082130432', '1']) + calls('f64_i64', d('n39', 'nzero', 'm1')) + |
| 167 | calls('u64_f64', ['4607182418800017408', '9223372036854775808']) + calls('u8_i8', ['255', '128', '127']) + |
| 168 | calls('i8_u8', ['-1', '-128']) + calls('i16_u16', ['-2']) + calls('u16_i16', ['65535', '32768']) + |
| 169 | calls('f32_f32', f('m1')), |
| 170 | convert: calls('f64_i32', d('nan', 'inf', 'ninf', 'nzero', 'p39', 'n39', 'i32hi', 't31', 'i32lo', 'big', 'nbig')) + |
| 171 | calls('f64_u8', d('m1', 'mhalf', 'u8hi', 't8', 'p39', 'nan', 'inf')) + |
| 172 | calls('f32_i8', f('t7', 'n129', 'p127', 'n39', 'nan', 'ninf')) + |
| 173 | calls('f64_u64', d('t64', 'u64hi', 't63', 'm1', 'nan', 'inf', 'big')) + |
| 174 | calls('f64_i64', d('t63', 'nt63', 'i64hi', 'i64lo', 'nan', 'ninf', 'p25')) + |
| 175 | calls('f32_u32', f('t32', 'u32hi', 'm1', 'nan', 'big', 't31')) + |
| 176 | calls('f32_i16', f('i16hi', 'i16lo', 'nt31', 'tenth')) + |
| 177 | calls('u64_f32', ['0', '18446744073709551615', '16777217', '9223372036854775808']) + |
| 178 | calls('i32_f32', ['-2147483648', '16777217', '-1']) + |
| 179 | calls('u64_f64', ['18446744073709551615', '9007199254740993', '0']) + |
| 180 | calls('i64_f64', ['-9223372036854775808', '9223372036854775807', '-1']) + |
| 181 | calls('f64_f32', d('tenth', 'big', 'nbig', 'nzero', 'p15', 'nan', 'inf')) + |
| 182 | calls('f32_f64', f('tenth', 'big', 'nzero', 'ninf')) + |
| 183 | calls('f64_f64', d('p39')) + calls('i32_u8', ['-1', '256', '255']) + |
| 184 | calls('u64_i64', ['18446744073709551615', '9223372036854775807']), |
| 185 | float_ops: 'call fadd 0x3fc00000 0x3dcccccd\ncall fadd 0x7f800000 0xff800000\ncall fmul 0x7149f2ca 0x7149f2ca\n' + |
| 186 | 'call fmul 0x80000000 0x447a0000\ncall dsub 0x400f333333333333 0x3fb999999999999a\n' + |
| 187 | 'call ddiv 0x3ff8000000000000 0x0000000000000000\ncall ddiv 0xbff0000000000000 0x0000000000000000\n' + |
| 188 | 'call fneg 0x447a0000\ncall fneg 0x00000000\ncall dneg 0x408f400000000000\n' + |
| 189 | 'call feq 0x00000000 0x80000000\ncall feq 0x7fc00000 0x7fc00000\ncall fne 0x7fc00000 0x7fc00000\n' + |
| 190 | 'call flt 0xbf800000 0x00000000\ncall flt 0x7fc00000 0x3fc00000\n' + |
| 191 | 'call dge 0x7ff8000000000000 0x3ff8000000000000\ncall dge 0x408f400000000000 0x408f400000000000\n', |
| 192 | init_diamond: 'call pick 1\ncall pick 0\n', |
| 193 | ok_loop_init_before: 'call f 5\ncall f 0\n', |
| 194 | ok_own_local: 'call a 1\ncall b 1\n', |
| 195 | }; |
| 196 | |
| 197 | function main(argv) { |
| 198 | if (argv.length !== 1) { |
| 199 | process.stderr.write('usage: v7cases.js OUTDIR\n'); |
| 200 | return 2; |
| 201 | } |
| 202 | const dir = argv[0]; |
| 203 | fs.mkdirSync(dir, { recursive: true }); |
| 204 | let n = 0; |
| 205 | for (const [name, [desc, script]] of Object.entries(cases)) { |
| 206 | fs.writeFileSync(path.join(dir, name + '.unit'), encode(desc)); |
| 207 | fs.writeFileSync(path.join(dir, name + '.script'), script); |
| 208 | n++; |
| 209 | } |
| 210 | for (const [name, script] of Object.entries(scripts)) { |
| 211 | if (!fixtures[name]) throw new Error('v7cases: writer has no fixture ' + name); |
| 212 | fs.writeFileSync(path.join(dir, name + '.unit'), encode(fixtures[name])); |
| 213 | fs.writeFileSync(path.join(dir, name + '.script'), script); |
| 214 | n++; |
| 215 | } |
| 216 | process.stdout.write(n + ' run cases\n'); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | process.exitCode = main(process.argv.slice(2)); |