#!/usr/bin/env python3 """Deep pruned-SSA queries through the public CLI; Q-061 control lowering stays open.""" import json from pathlib import Path import subprocess import tempfile REPO = Path(__file__).resolve().parents[2] AJI = REPO / 'zig-out/bin/aji' checks = 0 def cli(command, path, *options, code=0): global checks result = subprocess.run([str(AJI), command, *map(str, options), str(path)], capture_output=True) assert (result.returncode, result.stderr) == (code, b''), (command, path, result.returncode, result.stdout, result.stderr) checks += 1 return result.stdout def source(kind, depth, branch, killed=False, leaf='x'): flag = 'true' if branch else 'false' prefix = 'module depth\nfn main() -> i64 effects [] { var x: i64 = 0i64; ' chain = '- ' * depth if kind == 'read': expression = chain + leaf text = prefix + f'if {flag} {{ x = 1i64; }} let y: i64 = {expression}; return y; }}\n' fuel = 2 * depth + (6 if branch else 5) expected = (1 if depth % 2 == 0 else -1) if branch else 0 else: expression = chain + f'(if {flag} {{ x = 1i64; 0i64 }} else {{ 1i64 }})' suffix = 'x = 9i64; ' if killed else '' text = prefix + f'let y: i64 = {expression}; {suffix}return x; }}\n' fuel = 2 * depth + (7 if branch else 6) + int(killed) expected = 9 if killed else int(branch) return text, fuel, expected with tempfile.TemporaryDirectory(prefix='aji-liveness-depth-') as directory: root = Path(directory) for depth in (0, 1, 16, 1024, 16384, 32768, 65536): for kind, killed in (('read', False), ('after', False), ('after', True)): for branch in (False, True): path = root / f'{kind}-{killed}-{branch}-{depth}.aji' text, fuel, expected = source(kind, depth, branch, killed) path.write_text(text) assert cli('check', path) == b'' lowered = cli('lower', path) # Check the liveness decision directly in canonical AIR: # assignment before a later kill must not be carried to the join. join = next(line for line in lowered.splitlines() if line.startswith(b'endif_')) assert (b'%x_' in join) == (not killed), join formatted = cli('fmt', path) formatted_path = root / 'formatted.aji' formatted_path.write_bytes(formatted) assert cli('fmt', formatted_path) == formatted assert cli('check', formatted_path) == b'' assert cli('lower', formatted_path) == lowered air = root / 'canonical.air' air.write_bytes(lowered) assert cli('check', air) == b'' if depth <= 1024: record = cli('run', path, '--fuel', fuel) assert json.loads(record) == {'status': 'returned', 'value': expected} assert cli('run', air, '--fuel', fuel) == record assert json.loads(cli('run', path, '--fuel', fuel - 1, code=5)) == { 'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1} hosted = root / 'hosted.json' assert cli('run', path, '--profile', 'hosted', '--result-file', hosted, '--fuel', fuel) == b'' assert json.loads(hosted.read_text()) == json.loads(record) print(f'liveness depth: {kind} killed={killed} branch={branch} {depth}', flush=True) for depth in (0, 1024, 16384, 65536): path = root / f'missing-{depth}.aji' text, _, _ = source('read', depth, True, leaf='missing') path.write_text(text) errors = cli('check', path, code=1).splitlines() assert len(errors) == 1, errors error = json.loads(errors[0]) line = text.splitlines()[1] assert (error['code'], error['file'], error['line'], error['col']) == ( 'E7002', str(path), 2, line.index('missing') + 1), error for command in ('lower', 'run', 'report', 'explore'): assert json.loads(cli(command, path, code=1)) == error formatted_path = root / 'rejected-formatted.aji' formatted_path.write_bytes(cli('fmt', path)) formatted_error = json.loads(cli('check', formatted_path, code=1)) contents = formatted_path.read_text().splitlines() at = next((i + 1, line.index('missing') + 1) for i, line in enumerate(contents) if 'missing' in line) assert (formatted_error['code'], formatted_error['file'], formatted_error['line'], formatted_error['col']) == ( 'E7002', str(formatted_path), *at), formatted_error print(f'source liveness depth: {checks} CLI checks passed')