interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / freest.py

182 lines · 10520 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Host-only scalar profile checks. Test infrastructure, never a source converter."""
3import os
4from pathlib import Path
5import shlex
6import subprocess
7import tempfile
8
9ROOT = Path(__file__).resolve().parents[1]
10CC = os.environ.get('CC', 'cc')
11RUNNER = shlex.split(os.environ.get('FREEST_RUNNER', ''))
12
13
14def run(args, **kw):
15 p = subprocess.run([str(a) for a in args], capture_output=True, **kw)
16 assert p.returncode == 0, (args, p.returncode, p.stdout, p.stderr)
17 return p.stdout
18
19
20def main():
21 with tempfile.TemporaryDirectory(prefix='bridge-freest-check-') as td:
22 t = Path(td)
23 objects = []
24 for name in ('revision', 'epoch', 'scalar'):
25 src = ROOT / f'tests/freest-fixtures/{name}.om'
26 if name != 'scalar' and os.environ.get('FREEST_CUDAPP_ROOT'):
27 src = Path(os.environ['FREEST_CUDAPP_ROOT']) / f'om/pipeline/{name}.om'
28 c = t / f'{name}.c'
29 c.write_bytes(run([ROOT / 'tools/bridge-freest', 'c', src]))
30 (t / f'{name}.h').write_bytes(run([ROOT / 'tools/bridge-freest', 'h', src]))
31 for opt in ('0', '2'):
32 obj = t / f'{name}-{opt}.o'
33 run([CC, '-std=c11', f'-O{opt}', '-Wall', '-Wextra', '-Werror',
34 '-ffreestanding', '-fno-builtin', '-fno-stack-protector', '-fPIC',
35 '-c', c, '-o', obj])
36 assert not run(['nm', '-u', obj]).strip(), obj
37 sections = run(['readelf', '-SW', obj])
38 assert b'.tdata' not in sections and b'.tbss' not in sections, sections
39 if opt == '2':
40 objects.append(obj)
41 # Hosted roots remain required and are independently emitted by toc.om.
42 if name != 'scalar':
43 hc = t / f'hosted_{name}.c'
44 hc.write_bytes(run([ROOT / 'tools/bridge-seeded', 'c', src]))
45 run([CC, '-std=gnu11', '-O' + os.environ.get('HOSTED_OPT', '2'), '-Wall', '-Wextra', '-Werror',
46 '-c', hc, '-o', t / f'hosted_{name}.o'])
47 linked = t / 'freest.o'
48 run([CC, '-r', '-nostdlib', *objects, '-o', linked])
49 assert not run(['nm', '-u', linked]).strip()
50 caller = t / 'caller.c'
51 caller.write_text('''#include <stdint.h>
52#include "revision.h"
53#include "epoch.h"
54#include "scalar.h"
55int main(void) {
56 uint64_t out=17;
57 if(omega_native_revision(&out,0)||out!=1)return 1;
58 if(omega_native_revision(&out,UINT64_MAX)||out!=0)return 2;
59 if(omega_native_revision(&out,UINT64_MAX-1)||out!=UINT64_MAX)return 3;
60 if(omega_native_epoch(&out,0)||out!=1)return 4;
61 if(omega_native_epoch(&out,UINT64_MAX)||out!=UINT64_MAX)return 5;
62 if(omega_native_epoch(&out,UINT64_MAX-1)||out!=UINT64_MAX)return 6;
63 out=17;
64 if(omega_native_epoch(0,0)!=32)return 7;
65 if(omega_native_epoch((uint64_t *)(uintptr_t)1,0)!=32)return 8;
66 if(omega_native_epoch((uint64_t *)UINTPTR_MAX,0)!=32)return 9;
67 if(omega_portable_scalar(&out,3,UINT64_MAX)||out!=11)return 10;
68 if(omega_portable_scalar(&out,UINT32_MAX,0)||out!=1)return 11;
69 out=17;
70 if(omega_portable_scalar(&out,UINT64_MAX,0)!=32||out!=17)return 12;
71 return 0;
72}
73''')
74 run([CC, '-std=c11', '-O2', '-Wall', '-Wextra', '-Werror', '-I', t,
75 caller, linked, '-o', t / 'caller'])
76 run([*RUNNER, t / 'caller'])
77 # Same caller against hosted roots, with the independent scalar freest object.
78 run([CC, '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-I', t,
79 caller, t / 'hosted_revision.o', t / 'hosted_epoch.o', objects[-1],
80 '-o', t / 'hosted-caller'])
81 run([*RUNNER, t / 'hosted-caller'])
82 print('PASS hosted roots at O' + os.environ.get('HOSTED_OPT', '2') + '; import-free freest roots at O0/O2; MAX boundaries, checked ABI, arbitrary helper names, wrap, signed comparisons and branch locals')
83 print('PASS O0/O2 objects and -r -nostdlib link: nm -u empty; no TLS sections')
84 # A wrong expected result must actually fail the caller, not merely compile.
85 wrong = t / 'wrong.c'
86 wrong.write_text(caller.read_text().replace('out!=11', 'out!=12'))
87 run([CC, '-std=c11', '-O2', '-I', t, wrong, linked, '-o', t / 'wrong'])
88 p = subprocess.run([*RUNNER, t / 'wrong'], capture_output=True)
89 assert p.returncode == 10, p.returncode
90 print('PASS wrong-result negative control exits 10')
91 # WolframLanguageEvaluator (parent routed): Table[{w,w-1,2^w-1,
92 # BitAnd[BitShiftLeft[2^w-1,w-1],2^w-1],
93 # BitShiftRight[2^w-1,w-1]},{w,{8,16,32,64}}] ->
94 # {{8,7,255,128,1},{16,15,65535,32768,1},
95 # {32,31,4294967295,2147483648,1},
96 # {64,63,18446744073709551615,9223372036854775808,1}}.
97 # Native seed results independently check the projected result byte.
98 for typ, count, projection, value, left, right in (
99 ('uchar', 0, 0, 1, None, None),
100 ('uchar', 1, 0, 1, None, None),
101 ('ushort', 1, 0, 1, None, None),
102 ('uint', 1, 0, 1, None, None),
103 ('ulong', 63, 63, 1, None, None),
104 ('uchar', 7, 0, 255, 128, 1),
105 ('ushort', 15, 0, 65535, 32768, 1),
106 ('uint', 31, 0, 4294967295, 2147483648, 1),
107 ('ulong', 63, 0, 18446744073709551615, 9223372036854775808, 1)):
108 for op in ('shl', 'shr'):
109 fixture = t / 'shift.om'
110 body = f'fn shift x {typ} to {typ}\nret {op} x {count}\nend\n'
111 fixture.write_text(body)
112 portable = run([ROOT / 'tools/bridge-freest', 'c', fixture])
113 native_source = body + f'fn main to int\nr = shift {typ} {value}\nret int and shr ulong r {projection} 255\nend\n'
114 native = t / 'shift-native'
115 native.write_bytes(run([os.environ['OMEGA_SEEDED_OMC']], input=native_source.encode()))
116 native.chmod(0o755)
117 expected = subprocess.run([native], capture_output=True).returncode
118 assert expected not in range(101, 115), expected
119 c = t / 'shift.c'
120 bound = left if op == 'shl' else right
121 check = '' if bound is None else f'if(out != UINT64_C({bound}))return 98;'
122 caller = '\nint main(void){uint64_t out=0;if(omega_shift(&out,UINT64_C(' + str(value) + ')))return 99;' + check + 'return (int)((out >> ' + str(projection) + ')&255);}\n'
123 c.write_bytes(portable + caller.encode())
124 run([CC, '-std=c11', '-O2', '-Wall', '-Wextra', '-Werror', c, '-o', t / 'shift'])
125 assert subprocess.run([*RUNNER, t / 'shift'], capture_output=True).returncode == expected, (typ, op, count)
126 if typ == 'ulong' and op == 'shl':
127 c.write_bytes(portable.replace(b'case 15:return (a<<b)&mask;', b'case 15:return (a>>b)&mask;') + caller.encode())
128 run([CC, '-std=c11', '-O2', '-Wall', '-Wextra', '-Werror', c, '-o', t / 'shift-wrong'])
129 assert subprocess.run([*RUNNER, t / 'shift-wrong'], capture_output=True).returncode != expected
130 print('PASS literal-count shifts: seed/native-C parity and wrong-direction negative control')
131 root = 'fn arbitrary x ulong to ulong\nret x\nend\n'
132 hidden = lambda body: f'fn hidden x ulong to ulong\n{body}\nend\n' + root
133 refusals = {
134 'loop': hidden('for i ulong 1\nx = add x 1\nend\nret x'),
135 'while': hidden('while false bound 1\nx = add x 1\nend\nret x'),
136 'array-local': hidden('a = new ulong 1 0\nret x'),
137 'global': 'global state ulong 1\n' + root,
138 'text': 'text msg "x"\n' + root,
139 'host': hidden('v = getpid\nret x'),
140 'io': hidden('b = new uchar 1 0\nv = write 1 b 0 1\nret x'),
141 'main': 'fn main to int\nret 0\nend\n',
142 'recursion': 'fn hidden x ulong to ulong\nret hidden x\nend\n' + root,
143 'mutual-recursion': 'fn a x ulong to ulong\nret b x\nend\nfn b x ulong to ulong\nret a x\nend\n' + root,
144 'float': 'fn hidden x f64 to f64\nret x\nend\n' + root,
145 'signed-add': 'fn hidden x long to long\nret add x 1\nend\n' + root,
146 'signed-neg': 'fn hidden x long to long\nret neg x\nend\n' + root,
147 'signed-mul': 'fn hidden x long to long\nret mul x 2\nend\n' + root,
148 'div': hidden('ret div x 1'),
149 'rem': hidden('ret rem x 1'),
150 'shift': hidden('ret shl x x'),
151 'right-shift': hidden('ret shr x x'),
152 'shift-at-width': hidden('ret shl x 64'),
153 'right-shift-at-width': hidden('ret shr x 64'),
154 'negative-shift': hidden('ret shl x -1'),
155 'boolean-shift': hidden('ret shr x true'),
156 'signed-shift': 'fn hidden x long to long\nret shr x 1\nend\n' + root,
157 'narrow': hidden('v = uint x\nret ulong v'),
158 'sign-conversion': hidden('v = long x\nret ulong v'),
159 'trap': hidden('trap 1'),
160 'om2': 'om2\n' + root,
161 }
162 for name, source in refusals.items():
163 src = t / f'{name}.om'
164 src.write_text(source)
165 for mode in ('c', 'h'):
166 p = subprocess.run([ROOT / 'tools/bridge-freest', mode, src], capture_output=True)
167 assert p.returncode != 0 and not p.stdout, (name, mode, p.returncode, p.stdout, p.stderr)
168 print(f'PASS {len(refusals)} whole-unit refusal controls in c and h modes, including unused helpers; stdout empty')
169 # Structural refusal is independently challenged without the seed frontend.
170 converter = t / 'converter'
171 converter.write_bytes(run([os.environ['OMEGA_SEED']], input=(ROOT / 'src/freest.om').read_bytes()))
172 converter.chmod(0o755)
173 for name in ('loop', 'while', 'global', 'text', 'host', 'float', 'signed-add',
174 'div', 'rem', 'shift', 'narrow', 'recursion', 'mutual-recursion', 'main'):
175 p = subprocess.run([converter], input=b'F' + refusals[name].encode(), capture_output=True)
176 expected = 7 if name == 'float' else 24
177 assert p.returncode == expected and not p.stdout, (name, p.returncode, p.stdout, p.stderr)
178 print('PASS direct structural converter refusals independently of frontend, including recursive call graph')
179
180
181if __name__ == '__main__':
182 main()