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 / seed / ng / a64 / fibhb.om

245 lines · 7416 bytes Raw Blame History
1note fibhb: the D8 plan-breaker experiment (DECISIONS.md D8). Built as the concatenation seed/ng/src/ir.om (for its
2note input, region 0 of the arena) + seed/ng/src/a64.om + this file; it is a standalone experiment tool, not a mode of ng.
3note Input: the line `a64 fib V` (V one of A B D E) and then an omc-built AArch64 ELF whose program contains seed omc's
4note code for the bench function
5note fn fib n ulong to ulong / if lt n 2 / ret n / end / a = fib sub n 1 / b = fib sub n 2 / ret add a b / end
6note (~/handoffs/OmegaFast/bench/fib.om and OMC-EFF-1 kernels/fib.om). Output: the same ELF with only that function
7note replaced by hand-built code made of encoder words. Every other byte (main, putnum, the trap tail) is omc's, so
8note the difference measured is the code of fib alone. Every variant keeps omc's stack check (CMP SP,X28; B.LO to
9note omc's own trap-6 stub) before its first store, and builds no frame on the leaf path (n < 2).
10note A the allocator's intended convention: liveness-driven saves into one callee-saved register. x19 holds n
11note across the first call and fib(n-1) across the second (the live ranges do not overlap, so linear scan gives
12note them one register); one 16-byte frame, STP x19,x30 / LDP x19,x30,[sp],#16. Per non-leaf call 1 STP, 1 LDP.
13note B A with the pair split into STR/STR and LDR/LDR (separates pairing from save count).
14note E A plus the second call turned into a loop (fib(n) = sum over k of fib(n-1-2k), plus the final n < 2 term),
15note the shape gcc -O2 13.3 emits; not a save-convention change: it measures what call elimination is worth.
16note Wrapping ulong add is associative, so the result is exact; recursion depth is unchanged (the first call
17note chain still descends n-1, n-2, ...), so trap 6 still fires on deep inputs.
18note D A placed at the first 64-byte boundary at or after s+4, with `b` to it at s (main's one call takes the
19note hop; the recursive calls BL the aligned copy directly). Separates code placement from convention.
20note omc's fib (seed/omc/omc.om@943d9555 save/restore 4985/5050): per non-leaf call 9 stores and 7 loads, two frames.
21note Exit 5 with `a64: unsupported N` when the first line is wrong, the input does not contain omc's fib exactly once
22note (N = the number of matches), or its stack check does not reach a trap-6 stub.
23
24global a_fpat ulong 6
25global a_elen ulong 1
26
27fn a6_rdw o ulong to ulong
28b0 = ulong ar_bg 107 o
29b1 = ulong ar_bg 107 add o 1
30b2 = ulong ar_bg 107 add o 2
31b3 = ulong ar_bg 107 add o 3
32ret or or b0 shl b1 8 or shl b2 16 shl b3 24
33end
34
35note the first six words of omc's fib: cmp x0,#2; b.cs +8; ret; sub x2,x0,#1; sub sp,sp,#32; cmp sp,x28
36fn a6_fpini to void
37put a_fpat 0 0xf100081f
38put a_fpat 1 0x54000042
39put a_fpat 2 0xd65f03c0
40put a_fpat 3 0xd1000402
41put a_fpat 4 0xd10083ff
42put a_fpat 5 0xeb3c63ff
43end
44
45note a6_fibat: omc's 35-word fib starts at byte s (first six words, then add x0,x1,x2; ret at +132, +136)
46fn a6_fibat s ulong to bool
47i = ulong 0
48while lt i 6 bound 6
49if ne a6_rdw add s shl i 2 get a_fpat i
50ret false
51end
52i = add i 1
53end
54if ne a6_rdw add s 132 0x8b020020
55ret false
56end
57ret eq a6_rdw add s 136 0xd65f03c0
58end
59
60fn a6_fibfnd to ulong
61n = get a_elen 0
62s = ulong 176
63hit = n
64cnt = ulong 0
65while le add s 140 n bound 4000000
66if a6_fibat s
67hit = s
68cnt = add cnt 1
69end
70s = add s 4
71end
72if ne cnt 1
73a6_die 5 5 cnt
74end
75ret hit
76end
77
78note a6_bctgt: the byte target of the B.cond word w at byte pc (die 5 unless it is B.LO/B.CC)
79fn a6_bctgt w ulong pc ulong to ulong
80if ne and w 0xff00001f 0x54000003
81a6_die 5 5 w
82end
83imm = and shr w 5 0x7ffff
84if ne and imm 0x40000 0
85ret sub pc shl sub 0x80000 imm 2
86end
87ret add pc shl imm 2
88end
89
90note a6_wdist: the distance in words from the next emitted word (a_cn) to byte t, two's complement
91fn a6_wdist t ulong to ulong
92h = get a_cn 0
93if ge t h
94ret shr sub t h 2
95end
96ret sub 0 shr sub h t 2
97end
98
99note a6_fibpro: at byte s, the leaf test and a checked frame of fr bytes (cmp x0,#2; b.hs +8; ret;
100note sub sp,sp,#fr; cmp sp,x28; b.lo trap6)
101fn a6_fibpro s ulong t6 ulong fr ulong to void
102put a_cn 0 s
103a6_ew a_subsxi 31 0 2 0 0
104a6_ew a_bcond 2 2 0 0 0
105a6_ew a_ret 30 0 0 0 0
106a6_ew a_subxi 31 31 fr 0 0
107a6_ew a_subsxe 31 31 28 3 0
108a6_ew a_bcond 3 a6_wdist t6 0 0 0
109end
110
111note a6_fibmid: the two calls with x19 live across each (shared by A and B)
112fn a6_fibmid s ulong to void
113a6_ew a_orrx 19 31 0 0 0
114a6_ew a_subxi 0 0 1 0 0
115a6_ew a_bl a6_wdist s 0 0 0 0
116a6_ew a_subxi 1 19 2 0 0
117a6_ew a_orrx 19 31 0 0 0
118a6_ew a_orrx 0 31 1 0 0
119a6_ew a_bl a6_wdist s 0 0 0 0
120a6_ew a_addx 0 19 0 0 0
121end
122
123fn a6_fiba s ulong t6 ulong to void
124a6_fibpro s t6 16
125a6_ew a_stpx 19 30 31 0 0
126a6_fibmid s
127a6_ew a_ldpxpo 19 30 31 16 0
128a6_ew a_ret 30 0 0 0 0
129end
130
131fn a6_fibb s ulong t6 ulong to void
132a6_fibpro s t6 16
133a6_ew a_strx 19 31 0 0 0
134a6_ew a_strx 30 31 1 0 0
135a6_fibmid s
136a6_ew a_ldrx 19 31 0 0 0
137a6_ew a_ldrx 30 31 1 0 0
138a6_ew a_addxi 31 31 16 0 0
139a6_ew a_ret 30 0 0 0 0
140end
141
142note D: B to a, NOP fill, then A at a (a = the 64-byte boundary at or after s+4; A is 68 bytes)
143fn a6_fibd s ulong t6 ulong to void
144a = and add s 67 0xffffffffffffffc0
145if gt add a 68 add s 140
146a6_die 5 5 a
147end
148put a_cn 0 s
149a6_ew a_b a6_wdist a 0 0 0 0
150while lt get a_cn 0 a bound 16
151a6_emitw 0xd503201f
152end
153a6_fiba a t6
154end
155
156note E: x19 = n (counts down by 2), x20 = the sum of fib(n-1), fib(n-3), ...; the loop runs while x19 >= 2
157fn a6_fibe s ulong t6 ulong to void
158a6_fibpro s t6 32
159a6_ew a_stpx 19 20 31 0 0
160a6_ew a_strx 30 31 2 0 0
161a6_ew a_orrx 19 31 0 0 0
162a6_ew a_movzx 20 0 0 0 0
163lp = get a_cn 0
164a6_ew a_subxi 0 19 1 0 0
165a6_ew a_bl a6_wdist s 0 0 0 0
166a6_ew a_addx 20 20 0 0 0
167a6_ew a_subxi 19 19 2 0 0
168a6_ew a_subsxi 31 19 2 0 0
169a6_ew a_bcond 2 a6_wdist lp 0 0 0
170a6_ew a_addx 0 20 19 0 0
171a6_ew a_ldrx 30 31 2 0 0
172a6_ew a_ldpxpo 19 20 31 32 0
173a6_ew a_ret 30 0 0 0 0
174end
175
176note a6_fibhd: the first line is `a64 fib V` (10 bytes with the newline); returns V
177fn a6_fibhd to ulong
178if lt get a_inlen 0 10
179a6_die 5 5 0
180end
181ok = and and eq uchar and shr get ar_m 0 0 255 97 eq uchar and shr get ar_m 0 8 255 54 eq uchar and shr get ar_m 0 16 255 52
182ok2 = and and eq uchar and shr get ar_m 0 24 255 32 eq uchar and shr get ar_m 0 32 255 102 eq uchar and shr get ar_m 0 40 255 105
183ok3 = and and eq uchar and shr get ar_m 0 48 255 98 eq uchar and shr get ar_m 0 56 255 32 eq uchar and shr get ar_m 1 8 255 10
184if not and and ok ok2 ok3
185a6_die 5 5 0
186end
187ret ulong uchar and shr get ar_m 1 0 255
188end
189
190note a6_fbin: all of stdin into region 0 of the arena (ar_rdin), its length into a_inlen (die 3 class 2 when it does
191note not fit, 2 class 1 on error)
192fn a6_fbin to void
193r = ar_rdin 0
194if eq r 2
195a6_die 3 2 18446744073709551615
196end
197if eq r 1
198a6_die 2 1 18446744073709551615
199end
200put a_inlen 0 get ar_inl 0
201end
202
203fn main to int
204a6_init
205a6_fpini
206a6_fbin
207v = a6_fibhd
208n = sub get a_inlen 0 10
209if ne ar_need 29 n 0
210ar_die
211end
212i = ulong 0
213while lt i n bound 134217729
214ar_bp 107 i ar_ib add i 10
215i = add i 1
216end
217put a_elen 0 n
218s = a6_fibfnd
219t6 = a6_bctgt a6_rdw add s 24 add s 24
220if ne a6_rdw t6 0xd28000c1
221a6_die 5 5 t6
222end
223if not or or eq v 65 eq v 66 or eq v 68 eq v 69
224a6_die 5 5 v
225end
226if eq v 65
227a6_fiba s t6
228end
229if eq v 66
230a6_fibb s t6
231end
232if eq v 68
233a6_fibd s t6
234end
235if eq v 69
236a6_fibe s t6
237end
238note the rest of omc's 140 bytes is unreachable: NOP (C6.2.296, 0xd503201f)
239while lt get a_cn 0 add s 140 bound 35
240a6_emitw 0xd503201f
241end
242put a_cn 0 n
243a6_flush
244ret 0
245end