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 / omc / gate-ir-trap.py
| 1 | #!/usr/bin/env python3 |
| 2 | """L524 reference-only harness. Never used to build/run a shipped Omega program. |
| 3 | om4 builds the compiler; C interpreter/backend, checker2 and Wolfram are oracles. |
| 4 | Deleting the trap emission must fail both byte equality and native behavior. |
| 5 | """ |
| 6 | from pathlib import Path |
| 7 | import hashlib |
| 8 | import json |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | ROOT = Path(__file__).resolve().parents[2] |
| 14 | B = ROOT / 'build/seed/ir-trap' |
| 15 | B.mkdir(parents=True, exist_ok=True) |
| 16 | checks = failures = 0 |
| 17 | |
| 18 | def 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 | |
| 22 | def 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 | |
| 28 | def 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 | |
| 36 | source = Path(os.environ.get('OMC_SOURCE', ROOT / 'seed/omc/omc.om')).read_bytes() |
| 37 | compiler = ROOT / 'seed/rung4/om4.bin' |
| 38 | stages = [] |
| 39 | for i in range(1, 4): |
| 40 | target = B / f'stage{i}' |
| 41 | stages.append(compile_elf(compiler, source, target)) |
| 42 | compiler = target |
| 43 | check(stages[0] == stages[1] == stages[2], 'om4/omc parents and stage2/stage3 fixpoint') |
| 44 | print('COMPILER_SHA256=' + hashlib.sha256(stages[2]).hexdigest()) |
| 45 | if '--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. |
| 50 | p = 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']) |
| 53 | if p.returncode: |
| 54 | raise RuntimeError(p.stderr.decode()) |
| 55 | |
| 56 | calc = json.loads((ROOT / 'seed/omc/tests/ir-trap-wolfram.json').read_text()) |
| 57 | inputs = ''.join(str(row['input']) + '\n' for row in calc['trapFixture']).encode() |
| 58 | (B/'inputs').write_bytes(inputs) |
| 59 | valid = [] |
| 60 | for 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. |
| 63 | valid += [ |
| 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 | ] |
| 68 | for 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. |
| 99 | for 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. |
| 117 | bad = [ |
| 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 | ] |
| 124 | for 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 | |
| 130 | print(f'CHECKS={checks} FAILURES={failures} VALID_PROGRAMS={len(valid)+2} REFUSALS={len(bad)}') |
| 131 | print('RC=' + str(int(bool(failures)))) |
| 132 | raise SystemExit(int(bool(failures))) |