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 / om / receipt / parity / unit.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | """unit.py RECEIPT.om NAME: print the function-mode unit that exports NAME. |
| 4 | |
| 5 | Copied from om/gpumath/parity/unit.py (aien-dev/omega-mv#751); only the file names differ. |
| 6 | |
| 7 | The unit is NAME and every library function it reaches through calls (the part of receipt.om |
| 8 | before the parity-harness marker), in file order, with NAME moved to the end: the seeded compiler |
| 9 | enters a function-mode unit at its last function and the omega-bridge native call ABI v1 binds that |
| 10 | one; the others stay private helpers. Note lines before a kept function travel with it. |
| 11 | """ |
| 12 | import sys |
| 13 | |
| 14 | MARK = "note ==== parity harness" |
| 15 | |
| 16 | |
| 17 | def functions(lib): |
| 18 | """[(name, first line incl. leading notes, end line)] of the library.""" |
| 19 | out = [] |
| 20 | i = 0 |
| 21 | while i < len(lib): |
| 22 | w = lib[i].split() |
| 23 | if w[:1] == ["fn"]: |
| 24 | j = i |
| 25 | while j > 0 and lib[j - 1].startswith("note") and not lib[j - 1].startswith("note ----"): |
| 26 | j -= 1 |
| 27 | depth = 0 |
| 28 | for k in range(i, len(lib)): |
| 29 | t = lib[k].split()[:1] |
| 30 | if t and t[0] in ("fn", "if", "while", "for"): |
| 31 | depth += 1 |
| 32 | elif t == ["end"]: |
| 33 | depth -= 1 |
| 34 | if depth == 0: |
| 35 | break |
| 36 | else: |
| 37 | sys.exit("unit.py: %s has no closing end" % w[1]) |
| 38 | out.append((w[1], j, k)) |
| 39 | i = k + 1 |
| 40 | else: |
| 41 | i += 1 |
| 42 | return out |
| 43 | |
| 44 | |
| 45 | def main(): |
| 46 | if len(sys.argv) != 3: |
| 47 | sys.exit(__doc__) |
| 48 | src, name = sys.argv[1], sys.argv[2] |
| 49 | lines = open(src).read().split("\n") |
| 50 | lib = lines[:next(i for i, l in enumerate(lines) if l.startswith(MARK))] |
| 51 | fns = functions(lib) |
| 52 | span = {n: (a, b) for n, a, b in fns} |
| 53 | if name not in span: |
| 54 | sys.exit("unit.py: no function %s" % name) |
| 55 | calls = {} |
| 56 | for n, a, b in fns: |
| 57 | words = set() |
| 58 | for l in lib[a:b + 1]: |
| 59 | if not l.startswith("note"): |
| 60 | words.update(l.split()) |
| 61 | calls[n] = {m for m in words if m in span and m != n} |
| 62 | keep = {name} |
| 63 | todo = [name] |
| 64 | while todo: |
| 65 | for m in calls[todo.pop()]: |
| 66 | if m not in keep: |
| 67 | keep.add(m) |
| 68 | todo.append(m) |
| 69 | order = [n for n, _, _ in fns if n in keep and n != name] + [name] |
| 70 | parts = [] |
| 71 | for n in order: |
| 72 | a, b = span[n] |
| 73 | parts.append("\n".join(lib[a:b + 1])) |
| 74 | sys.stdout.write("note SPDX-License-Identifier: Apache-2.0\nnote generated from om/receipt/receipt.om by parity/unit.py %s\n\n" % name) |
| 75 | sys.stdout.write("\n\n".join(parts) + "\n") |
| 76 | |
| 77 | |
| 78 | if __name__ == "__main__": |
| 79 | main() |