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 / gas.py

107 lines · 4052 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3"""gas.py CORPDIR OUTDIR WORKDIR: cross-check words that aarch64.om wrote against GNU as.
4
5For each encoder stream eNN_*, up to 200 records whose .om output is a word (evenly spaced over the
6stream) are written back as assembly text from their fields, assembled with `as`, and the .text words
7must equal the .om words. A 31 register is written as xzr/wzr where the form reads ZR and sp where it
8reads SP. Exit 0 and one summary line when every word is identical, 1 otherwise. Needs `as` and
9`objcopy` for AArch64 on PATH.
10"""
11import os
12import subprocess
13import sys
14
15CC = "eq ne cs cc mi pl vs vc hi ls ge lt gt le al nv".split()
16
17
18def sgn(v):
19 return v - (1 << 64) if v >> 63 else v
20
21
22def zr(r, sf):
23 if r == 31:
24 return "xzr" if sf else "wzr"
25 return ("x%d" if sf else "w%d") % r
26
27
28def sp(r, sf=1):
29 if r == 31:
30 return "sp" if sf else "wsp"
31 return ("x%d" if sf else "w%d") % r
32
33
34def rel(n):
35 return ". + %d" % n if n >= 0 else ". - %d" % -n
36
37
38def text(form, f):
39 a, b, c, d = f
40 if 1 <= form <= 6 or form == 25:
41 op = ["", "add", "sub", "mul", "and", "orr", "eor"][form] if form != 25 else "subs"
42 return "%s %s, %s, %s" % (op, zr(b, a), zr(c, a), zr(d, a))
43 if form == 7:
44 return "orr %s, %s, %s" % (zr(b, a), zr(31, a), zr(c, a))
45 if form in (8, 14):
46 return "%s %s, #%d, lsl #%d" % ("movz" if form == 8 else "movk", zr(b, a), c, d)
47 if form == 9:
48 return "ret"
49 if form == 10:
50 return "b %s" % rel(4 * sgn(a))
51 if form == 11:
52 return "b.%s %s" % (CC[a], rel(4 * sgn(b)))
53 if form in (12, 13):
54 return "%s %s, %s" % ("cbz" if form == 12 else "cbnz", zr(b, a), rel(4 * sgn(c)))
55 if form == 15:
56 return "adr %s, %s" % (zr(a, 1), rel(sgn(b)))
57 if form in (16, 17):
58 return "%s %s, [%s, #%d]" % ("ldr" if form == 16 else "str", zr(b, a), sp(c), d)
59 if form in (18, 19):
60 return "%s %s, [%s, #%d]" % ("ldrb" if form == 18 else "strb", zr(a, 0), sp(b), c)
61 if 20 <= form <= 23:
62 op = "ldr" if form in (20, 22) else "str"
63 return "%s %s, [%s], #%d" % (op, zr(a, form >= 22), sp(b), sgn(c))
64 if form == 24:
65 return "subs %s, %s, #%d" % (zr(b, a), sp(c, a), d)
66 raise AssertionError(form)
67
68
69def main():
70 if len(sys.argv) != 4:
71 sys.exit(__doc__)
72 corp, outd, work = sys.argv[1:]
73 lines, want = [], []
74 forms = set()
75 for name in sorted(os.listdir(corp)):
76 if not (name.startswith("e") and name.endswith(".txt")):
77 continue
78 s = name[:-4]
79 recs = open(os.path.join(corp, name)).read().split("\n")
80 outs = open(os.path.join(outd, s + ".out")).read().split("\n")
81 ok = [i for i, o in enumerate(outs) if o.startswith("E ")]
82 step = max(1, len(ok) // 200)
83 for i in ok[::step][:200]:
84 w = recs[i].split()
85 form, f = int(w[1], 16), [int(x, 16) for x in w[2:6]]
86 lines.append(text(form, f))
87 want.append(int(outs[i].split()[1], 16))
88 forms.add(form)
89 src = os.path.join(work, "gas.s")
90 obj = os.path.join(work, "gas.o")
91 binf = os.path.join(work, "gas.bin")
92 open(src, "w").write("\n".join(lines) + "\n")
93 subprocess.run(["as", src, "-o", obj], check=True, stderr=subprocess.DEVNULL)
94 subprocess.run(["objcopy", "-O", "binary", "--only-section=.text", obj, binf], check=True)
95 data = open(binf, "rb").read()
96 got = [int.from_bytes(data[4 * i:4 * i + 4], "little") for i in range(len(data) // 4)]
97 bad = [i for i in range(len(want)) if i >= len(got) or got[i] != want[i]]
98 if len(got) != len(want) or bad:
99 for i in bad[:20]:
100 print("gas MISMATCH %s: as %s, om %08x" % (lines[i], "%08x" % got[i] if i < len(got) else "-", want[i]))
101 print("gas FAIL: %d of %d differ" % (len(bad), len(want)))
102 sys.exit(1)
103 print("gas: %d words over %d forms identical to GNU as" % (len(want), len(forms)))
104
105
106if __name__ == "__main__":
107 main()