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 / qualification / om2 / oracle / s4fuel / fuel_e16f.py

54 lines · 2246 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# E14/E16 fuel model, per-function form (oracle, outside the delivered closure; Python 3.12.3).
4# Reads a fixture in the subset used by sec/s4fuel (fn NAME ... [inline], `for V T K unroll`, a line that is
5# a bare function name = a call, end, note, ret). Every function, inline ones included, is compiled in
6# source order with its own fuel 65536 (E14 "per function"; OMEGESE 9.13: an inline function is also
7# compiled standalone). E16: an unrolled loop is charged K at its line when it starts; an inline expansion
8# 1 at its call line; error 14 names the construct whose charge takes the total above 65536.
9# Compilation stops at the first error: the first function, in source order, to exhaust its fuel.
10import sys
11class Out(Exception): pass
12
13def parse(path):
14 fns, order, cur, stack = {}, [], None, []
15 for no, raw in enumerate(open(path), 1):
16 w = raw.split()
17 if not w or w[0] == "note" or w[0] == "om2":
18 continue
19 if w[0] == "fn":
20 cur = w[1]; fns[cur] = {"inline": w[-1] == "inline", "body": []}; order.append(cur)
21 stack = [fns[cur]["body"]]
22 elif w[0] == "for" and w[-1] == "unroll":
23 node = ("loop", no, int(w[3]), []); stack[-1].append(node); stack.append(node[3])
24 elif w[0] == "end":
25 stack.pop()
26 elif len(w) == 1 and w[0] in fns or (len(w) == 1 and w[0] not in ("ret",)):
27 stack[-1].append(("call", no, w[0]))
28 return fns, order
29
30def compile_fn(fns, name):
31 used = [0]
32 def charge(n, line):
33 used[0] += n
34 if used[0] > 65536:
35 raise Out(line)
36 def walk(body):
37 for it in body:
38 if it[0] == "loop":
39 charge(it[2], it[1])
40 for _ in range(it[2]):
41 walk(it[3])
42 elif fns.get(it[2], {}).get("inline"):
43 charge(1, it[1]); walk(fns[it[2]]["body"])
44 walk(fns[name]["body"])
45 return used[0]
46
47fns, order = parse(sys.argv[1])
48for f in order:
49 try:
50 n = compile_fn(fns, f)
51 print("fn %s total %d" % (f, n))
52 except Out as e:
53 print("fn %s exhausted: error 14 at line %d" % (f, e.args[0]))
54 break