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