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 / om / aarch64 / parity / unit.py

77 lines · 2613 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3"""unit.py AARCH64.om NAME: print the function-mode unit that exports NAME.
4
5The unit is NAME and every library function it reaches through calls (the part of aarch64.om
6before the parity-harness marker), in file order, with NAME moved to the end: the seeded compiler
7enters a function-mode unit at its last function and the omega-bridge native call ABI v1 binds that
8one; the others stay private helpers. Note lines before a kept function travel with it.
9"""
10import sys
11
12MARK = "note ==== parity harness"
13
14
15def 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
43def 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/aarch64/aarch64.om by parity/unit.py %s\n\n" % name)
73 sys.stdout.write("\n\n".join(parts) + "\n")
74
75
76if __name__ == "__main__":
77 main()