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