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