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 / omc / gate-ir-trap.py

132 lines · 6361 bytes Raw Blame History
1#!/usr/bin/env python3
2"""L524 reference-only harness. Never used to build/run a shipped Omega program.
3om4 builds the compiler; C interpreter/backend, checker2 and Wolfram are oracles.
4Deleting the trap emission must fail both byte equality and native behavior.
5"""
6from pathlib import Path
7import hashlib
8import json
9import os
10import subprocess
11import sys
12
13ROOT = Path(__file__).resolve().parents[2]
14B = ROOT / 'build/seed/ir-trap'
15B.mkdir(parents=True, exist_ok=True)
16checks = failures = 0
17
18def run(args, data=None):
19 return subprocess.run([str(x) for x in args], input=data, cwd=ROOT,
20 stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30)
21
22def check(ok, name):
23 global checks, failures
24 checks += 1
25 failures += not ok
26 print(('PASS ' if ok else 'FAIL ') + name, flush=True)
27
28def compile_elf(compiler, source, target):
29 p = run([compiler], source)
30 if p.returncode or len(p.stdout) < 64 or p.stdout[:4] != b'\x7fELF' or p.stdout[18:20] != b'\xb7\x00':
31 raise RuntimeError(f'{target}: compiler RC={p.returncode}; not nonempty AArch64 ELF: {p.stderr!r}')
32 target.write_bytes(p.stdout)
33 target.chmod(0o755)
34 return p.stdout
35
36source = Path(os.environ.get('OMC_SOURCE', ROOT / 'seed/omc/omc.om')).read_bytes()
37compiler = ROOT / 'seed/rung4/om4.bin'
38stages = []
39for i in range(1, 4):
40 target = B / f'stage{i}'
41 stages.append(compile_elf(compiler, source, target))
42 compiler = target
43check(stages[0] == stages[1] == stages[2], 'om4/omc parents and stage2/stage3 fixpoint')
44print('COMPILER_SHA256=' + hashlib.sha256(stages[2]).hexdigest())
45if '--build-only' in sys.argv:
46 raise SystemExit(0 if not failures else 1)
47
48# This derived test-only runner adds interpreter/native differential checking to
49# the immutable seed/check/runner.c; never a compiler in the seed ancestry.
50p = run(['cc', '-O1', '-Isrc', '-Isrc/compiler', '-o', B/'oracle-runner',
51 'seed/check/ir-trap-runner.c',
52 *sorted(x for x in Path('src/compiler').glob('*.c') if x.name != 'oscc_main.c'), 'src/omega_om_units.c'])
53if p.returncode:
54 raise RuntimeError(p.stderr.decode())
55
56calc = json.loads((ROOT / 'seed/omc/tests/ir-trap-wolfram.json').read_text())
57inputs = ''.join(str(row['input']) + '\n' for row in calc['trapFixture']).encode()
58(B/'inputs').write_bytes(inputs)
59valid = []
60for code in range(1, 15):
61 valid.append((f'trap{code:02}', f'fn f a u32 to u32\nif gt a 10\ntrap {code}\nend\nret a\nend\n', code))
62# return types exercise the unreachable return terminator required by the IR.
63valid += [
64 ('signed', 'fn f a u32 to i64\nif gt a 10\ntrap 3\nend\nret i64 a\nend\n', 3),
65 ('nested', 'fn f a u32 to u32\nif gt a 10\nif gt a 0\ntrap 3\nelse\nret 9\nend\nend\nret a\nend\n', 3),
66 ('bothtrap', 'fn f a u32 to u32\nif gt a 10\ntrap 3\nelse\nret a\nend\nend\n', 3),
67]
68for name, text, code in valid:
69 f = B / (name + '.om')
70 f.write_text(text)
71 oracle = B/(name + '.oracle')
72 p = run(['build/omegese/omegese', 'emit', f, B/(name+'.json')])
73 if p.returncode:
74 raise RuntimeError(f'oracle frontend refused {name}: {p.stdout!r} {p.stderr!r}')
75 p = run(['build/omega_emit/omega_emit', 'emit', B/(name+'.json'), oracle])
76 if p.returncode:
77 raise RuntimeError(f'oracle emitter refused {name}: {p.stdout!r}')
78 p = run([compiler], b'ir\n' + text.encode())
79 got = B/(name+'.got')
80 got.write_bytes(p.stdout)
81 check(p.returncode == 0 and p.stdout == oracle.read_bytes(), name+' exact C-oracle IR')
82 q = run(['build/checker2/checker2', 'check', got])
83 check(q.returncode == 0 and b'checker2=ACCEPT ' in q.stdout, name+' checker2 ACCEPT')
84 q = run([B/'oracle-runner', got, 'f', B/'inputs'])
85 expected = []
86 for row in calc['trapFixture']:
87 if row['trap']:
88 expected.append(f'TRAP {code}\t-')
89 else:
90 v = row['return']
91 expected.append(f'{v}\t0x{v:016x}')
92 actual = q.stdout.decode().splitlines()[1:]
93 check(q.returncode == 0 and actual == expected,
94 name+f' Wolfram boundary outputs + interpreter/native agreement ({len(expected)} runs)')
95 if q.returncode or actual != expected:
96 print(' ACTUAL', actual, 'EXPECTED', expected, 'ERR', q.stderr.decode().strip())
97
98# Void and bool terminators, no-input and boolean-input cases.
99for name, text, inp, expected in [
100 ('void', 'fn f to void\ntrap 3\nend\n', b'\n', ['TRAP 3\t-']),
101 ('bool', 'fn f a bool to bool\nif a\ntrap 3\nend\nret a\nend\n', b'0\n1\n', ['0\t0x0000000000000000', 'TRAP 3\t-']),
102]:
103 f = B/(name+'.om'); f.write_text(text)
104 p = run(['build/omegese/omegese', 'emit', f, B/(name+'.json')])
105 if p.returncode: raise RuntimeError(f'oracle refused {name}: {p.stdout!r}')
106 p = run(['build/omega_emit/omega_emit', 'emit', B/(name+'.json'), B/(name+'.oracle')])
107 if p.returncode: raise RuntimeError(f'oracle refused {name}: {p.stdout!r}')
108 p = run([compiler], b'ir\n'+text.encode())
109 got = B/(name+'.got'); got.write_bytes(p.stdout)
110 check(p.returncode == 0 and p.stdout == (B/(name+'.oracle')).read_bytes(), name+' exact C-oracle IR')
111 (B/(name+'.inputs')).write_bytes(inp)
112 q = run([B/'oracle-runner', got, 'f', B/(name+'.inputs')])
113 check(q.returncode == 0 and q.stdout.decode().splitlines()[1:] == expected, name+' interpreter/native expected behavior')
114
115# Refusal codes follow docs/one_layer/OMEGESE.md 9.8 and seed/README.md ("trap N": error 4 when the line ends,
116# 16 when the token is not a number, 8 out of range): `trap a` and `trap uint 3` are error 16, `trap` alone is 4.
117bad = [
118 ('zero', 'trap 0', 8), ('too_large', 'trap 15', 8), ('negative', 'trap -1', 8),
119 ('missing', 'trap', 4), ('named', 'trap a', 16), ('extra', 'trap 3 4', 4),
120 ('extra_word', 'trap 3 ret a', 4), ('typed', 'trap uint 3', 16),
121 ('after', 'trap 3\nret a', 17),
122 ('unsupported_break', 'break', 9), ('unsupported_own', 'own', 9),
123]
124for name, stmt, code in bad:
125 text = f'ir\nfn f a u32 to u32\n{stmt}\nend\n'.encode()
126 p = run([compiler], text)
127 check(p.returncode == code and not p.stdout and f'error {code}'.encode() in p.stderr,
128 name+f' refused error {code}; no output')
129
130print(f'CHECKS={checks} FAILURES={failures} VALID_PROGRAMS={len(valid)+2} REFUSALS={len(bad)}')
131print('RC=' + str(int(bool(failures))))
132raise SystemExit(int(bool(failures)))