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 / ngtxt.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/ngtxt.om: shared part of the ΩIR text tools ngas.om and ngdis.om (seed/ng/IR.md s10). |
| 3 | note Owner: ng-ir lane. Program mode, no fn main; globals x_*, functions tx_*. Built as |
| 4 | note cat seed/ng/ngtxt.om seed/ng/ngas.om | omc and cat seed/ng/ngtxt.om seed/ng/ngdis.om | omc |
| 5 | note Neither tool shares code with seed/ng/src/ir.om or ngcheck.om: the text tools are the third |
| 6 | note independent decoder of the wire format, and the gate compares all three. |
| 7 | note Row table "OP NAME TK IMM FIX VAR TY" (IR.md s7.12; TK 0 P 1 E 2 R 3 T; TY 0 none 1 r 2 o 3 p). |
| 8 | text x_rows "1 const 0 1 0 0 1\n2 select 0 0 3 0 1\n10 add 0 0 2 0 1\n11 sub 0 0 2 0 1\n12 mul 0 0 2 0 1\n13 div 0 0 2 0 1\n14 rem 0 0 2 0 1\n15 neg 0 0 1 0 1\n16 and 0 0 2 0 1\n17 or 0 0 2 0 1\n18 xor 0 0 2 0 1\n19 not 0 0 1 0 1\n20 shl 0 0 2 0 1\n21 shr 0 0 2 0 1\n22 rotl 0 0 2 0 1\n23 rotr 0 0 2 0 1\n24 min 0 0 2 0 1\n25 max 0 0 2 0 1\n26 fsqrt 0 0 1 0 1\n27 fabs 0 0 1 0 1\n28 fmath 0 1 1 0 1\n30 eq 0 0 2 0 1\n31 ne 0 0 2 0 1\n32 lt 0 0 2 0 1\n33 le 0 0 2 0 1\n34 gt 0 0 2 0 1\n35 ge 0 0 2 0 1\n36 conv 0 1 1 0 1\n37 bitcast 0 0 1 0 1\n40 gpu.id 0 1 0 0 1\n41 shared 0 0 0 0 1\n42 load 2 0 2 0 1\n43 store 1 0 3 0 0\n44 sync 1 0 0 0 0\n45 shfl 0 1 2 0 1\n46 fma 0 0 3 0 1\n47 atom 1 1 3 0 1\n48 atomcas 1 1 4 0 1\n50 alloca 1 0 0 0 1\n51 fill 1 0 2 0 0\n52 gaddr 0 1 0 0 1\n53 len 0 0 1 0 1\n54 slice 2 0 3 0 1\n55 fload 2 1 1 0 1\n56 fstore 1 1 2 0 0\n57 fref 0 1 1 0 1\n58 vload 2 0 2 0 1\n59 vstore 1 0 3 0 0\n60 br 3 0 1 1 0\n61 cbr 3 0 3 0 0\n62 ret 3 0 0 0 0\n63 retv 3 0 1 0 0\n64 call 1 0 1 1 2\n65 trap 3 1 0 0 0\n66 exit 3 0 1 0 0\n67 launch 1 0 4 1 0\n70 chk.add 1 1 2 0 0\n71 chk.sub 1 1 2 0 0\n72 chk.mul 1 1 2 0 0\n73 chk.neg 1 1 1 0 0\n74 chk.divz 1 1 1 0 0\n75 chk.divovf 1 1 2 0 0\n76 chk.shl 1 1 2 0 0\n77 chk.shift 1 1 1 0 0\n78 chk.conv 1 1 1 0 3\n79 chk.bounds 1 1 3 0 0\n80 chk.slice 1 1 3 0 0\n81 chk.fuel 1 1 2 0 0\n82 chk.true 1 1 1 0 0\n90 fact.true 0 1 1 0 0\n91 fact.range 0 1 3 0 0\n92 fact.inbounds 0 1 3 0 0\n93 fact.slice 0 1 3 0 0\n100 vsplat 0 0 1 0 1\n101 vextract 0 1 1 0 1\n102 vinsert 0 1 2 0 1\n103 vreduce 0 1 1 0 1\n104 mmald 2 1 4 0 1\n105 mmast 1 1 5 0 0\n106 mma 0 1 3 0 1\n110 host 1 1 0 1 2\n111 pool.grow 1 1 1 0 1\n112 pool.len 2 1 0 0 1\n113 pool.load 2 1 1 0 1\n114 pool.store 1 1 2 0 0\n115 pool.read 1 1 3 0 1\n116 pool.write 1 1 3 0 1\n" |
| 9 | note built-in type names by id (IR.md s4.1); "-" marks an id without a name (printed %tN) |
| 10 | text x_tyn "void bool u8 u16 u32 u64 i8 i16 i32 i64 - - - f32 f64 - f16 bf16 e4m3 e5m2 tok " |
| 11 | note words: 0 fn 1 kernel 2 export 3 view 4 edit 5 host 6 global 7 shared 8 constant 9 private |
| 12 | note 10 a 11 b 12 c 13 le 14 be 15 vec 16 array 17 struct 18 ref 19 slice 20 frag 21 zero 22 data |
| 13 | note 23 flags 24 threads 25 subgroup 26 block 27 type 28 end 29 omegair 30 true 31 false |
| 14 | text x_kw "fn kernel export view edit host global shared constant private a b c le be vec array struct ref slice frag zero data flags threads subgroup block type end omegair true false pool " |
| 15 | global x_rok uchar 256 |
| 16 | global x_rtk uchar 256 |
| 17 | global x_rim uchar 256 |
| 18 | global x_rfx uchar 256 |
| 19 | global x_rva uchar 256 |
| 20 | global x_rty uchar 256 |
| 21 | global x_rno uint 256 |
| 22 | global x_rnl uint 256 |
| 23 | global x_kwo uint 64 |
| 24 | global x_kwl uint 64 |
| 25 | global x_tno uint 32 |
| 26 | global x_tnl uint 32 |
| 27 | global x_tk uchar 65568 |
| 28 | global x_tsz ulong 65568 |
| 29 | global x_nt ulong 1 |
| 30 | global x_out uchar 1048576 |
| 31 | global x_on ulong 1 |
| 32 | global x_dg uchar 24 |
| 33 | note ---- init --------------------------------------------------------------------------------------- |
| 34 | fn tx_init to void |
| 35 | tx_rinit |
| 36 | w = ulong 0 |
| 37 | s = ulong 0 |
| 38 | for i ulong len x_kw |
| 39 | if eq get x_kw i 32 |
| 40 | put x_kwo w uint s |
| 41 | put x_kwl w uint sub i s |
| 42 | w = add w 1 |
| 43 | s = add i 1 |
| 44 | end |
| 45 | end |
| 46 | w = 0 |
| 47 | s = 0 |
| 48 | for i ulong len x_tyn |
| 49 | if eq get x_tyn i 32 |
| 50 | put x_tno w uint s |
| 51 | put x_tnl w uint sub i s |
| 52 | w = add w 1 |
| 53 | s = add i 1 |
| 54 | end |
| 55 | end |
| 56 | end |
| 57 | note decimal at p in x_rows (no sign); the next position goes to x_on (reset by tx_init callers) |
| 58 | fn tx_rdec p ulong to ulong |
| 59 | i = p |
| 60 | v = ulong 0 |
| 61 | while lt i len x_rows bound 12 |
| 62 | c = get x_rows i |
| 63 | if or lt c 48 gt c 57 |
| 64 | break |
| 65 | end |
| 66 | v = add mul v 10 ulong sub c 48 |
| 67 | i = add i 1 |
| 68 | end |
| 69 | put x_on 0 i |
| 70 | ret v |
| 71 | end |
| 72 | fn tx_rinit to void |
| 73 | p = ulong 0 |
| 74 | while lt p len x_rows bound 256 |
| 75 | op = tx_rdec p |
| 76 | s = add get x_on 0 1 |
| 77 | e = s |
| 78 | while ne get x_rows e 32 bound 32 |
| 79 | e = add e 1 |
| 80 | end |
| 81 | put x_rok op 1 |
| 82 | put x_rno op uint s |
| 83 | put x_rnl op uint sub e s |
| 84 | p = add e 1 |
| 85 | put x_rtk op uchar tx_rdec p |
| 86 | p = add get x_on 0 1 |
| 87 | put x_rim op uchar tx_rdec p |
| 88 | p = add get x_on 0 1 |
| 89 | put x_rfx op uchar tx_rdec p |
| 90 | p = add get x_on 0 1 |
| 91 | put x_rva op uchar tx_rdec p |
| 92 | p = add get x_on 0 1 |
| 93 | put x_rty op uchar tx_rdec p |
| 94 | p = add get x_on 0 1 |
| 95 | end |
| 96 | put x_on 0 0 |
| 97 | end |
| 98 | note ---- rows ---------------------------------------------------------------------------------------- |
| 99 | note 1 when op has a token operand at slot 0 (E, R and T ops other than br) |
| 100 | fn tx_ntok op ulong to ulong |
| 101 | if or eq get x_rtk op 0 eq op 60 |
| 102 | ret 0 |
| 103 | end |
| 104 | ret 1 |
| 105 | end |
| 106 | note operand slot kind: 0 value, 1 block, 2 function |
| 107 | fn tx_slot op ulong s ulong to ulong |
| 108 | if and eq op 60 eq s 0 |
| 109 | ret 1 |
| 110 | end |
| 111 | if and eq op 61 or eq s 2 eq s 3 |
| 112 | ret 1 |
| 113 | end |
| 114 | if and eq s 1 or eq op 64 eq op 67 |
| 115 | ret 2 |
| 116 | end |
| 117 | ret 0 |
| 118 | end |
| 119 | note values op defines with result type t: its token (E ops), then its result (ty r or o, t != 0) |
| 120 | fn tx_ndef op ulong t ulong to ulong |
| 121 | n = ulong 0 |
| 122 | if eq get x_rtk op 1 |
| 123 | n = 1 |
| 124 | end |
| 125 | c = get x_rty op |
| 126 | if and ne t 0 or eq c 1 eq c 2 |
| 127 | n = add n 1 |
| 128 | end |
| 129 | ret n |
| 130 | end |
| 131 | note ---- types -------------------------------------------------------------------------------------- |
| 132 | fn tx_sb t ulong to ulong |
| 133 | if or eq t 1 eq t 2 |
| 134 | ret 1 |
| 135 | end |
| 136 | if or eq t 6 or eq t 18 eq t 19 |
| 137 | ret 1 |
| 138 | end |
| 139 | if or eq t 3 or eq t 7 or eq t 16 eq t 17 |
| 140 | ret 2 |
| 141 | end |
| 142 | if or eq t 4 or eq t 8 eq t 13 |
| 143 | ret 4 |
| 144 | end |
| 145 | if or eq t 5 or eq t 9 eq t 14 |
| 146 | ret 8 |
| 147 | end |
| 148 | ret 0 |
| 149 | end |
| 150 | note byte size of type t for global data (scalars, and table VEC, ARRAY, STRUCT); 0 otherwise |
| 151 | fn tx_size t ulong to ulong |
| 152 | if lt t 32 |
| 153 | ret tx_sb t |
| 154 | end |
| 155 | if ge t add 32 get x_nt 0 |
| 156 | ret 0 |
| 157 | end |
| 158 | ret get x_tsz t |
| 159 | end |
| 160 | fn tx_mul a ulong b ulong to ulong |
| 161 | x = ulong 1099511627776 |
| 162 | if and ne a 0 gt b div x a |
| 163 | ret x |
| 164 | end |
| 165 | ret mul a b |
| 166 | end |
| 167 | note record table entry t (kind k) with size sz, as the next entry |
| 168 | fn tx_tset t ulong k ulong sz ulong to void |
| 169 | put x_tk t uchar k |
| 170 | put x_tsz t sz |
| 171 | put x_nt 0 sub t 31 |
| 172 | end |
| 173 | note const formats: 1 bool, 2 unsigned decimal, 3 signed decimal, 4 hex bits, 0 plain decimal |
| 174 | fn tx_cf t ulong to ulong |
| 175 | if eq t 1 |
| 176 | ret 1 |
| 177 | end |
| 178 | if and ge t 2 le t 5 |
| 179 | ret 2 |
| 180 | end |
| 181 | if and ge t 6 le t 9 |
| 182 | ret 3 |
| 183 | end |
| 184 | if ne tx_sb t 0 |
| 185 | ret 4 |
| 186 | end |
| 187 | ret 0 |
| 188 | end |
| 189 | note canonical const bits of a scalar type (IR.md s7.2) |
| 190 | fn tx_canon t ulong v ulong to bool |
| 191 | f = tx_cf t |
| 192 | if eq f 0 |
| 193 | ret true |
| 194 | end |
| 195 | if eq f 1 |
| 196 | ret le v 1 |
| 197 | end |
| 198 | w = mul tx_sb t 8 |
| 199 | if eq w 64 |
| 200 | ret true |
| 201 | end |
| 202 | if eq f 3 |
| 203 | h = shl ulong 1 sub w 1 |
| 204 | ret lt add v h shl ulong 1 w |
| 205 | end |
| 206 | ret lt v shl ulong 1 w |
| 207 | end |
| 208 | note ---- output ------------------------------------------------------------------------------------- |
| 209 | fn tx_flush to void |
| 210 | n = get x_on 0 |
| 211 | o = ulong 0 |
| 212 | while lt o n bound 1048577 |
| 213 | k = write 1 x_out o sub n o |
| 214 | if le k 0 |
| 215 | exit 27 |
| 216 | end |
| 217 | o = add o ulong k |
| 218 | end |
| 219 | put x_on 0 0 |
| 220 | end |
| 221 | fn tx_c b ulong to void |
| 222 | n = get x_on 0 |
| 223 | if ge n len x_out |
| 224 | tx_flush |
| 225 | n = 0 |
| 226 | end |
| 227 | put x_out n uchar b |
| 228 | put x_on 0 add n 1 |
| 229 | end |
| 230 | fn tx_num v ulong to void |
| 231 | x = v |
| 232 | i = ulong 24 |
| 233 | for j ulong 21 |
| 234 | i = sub i 1 |
| 235 | put x_dg i uchar add 48 rem x 10 |
| 236 | x = div x 10 |
| 237 | if eq x 0 |
| 238 | break |
| 239 | end |
| 240 | end |
| 241 | for j ulong sub 24 i bound 24 |
| 242 | tx_c ulong get x_dg add i j |
| 243 | end |
| 244 | end |
| 245 | fn tx_hx d ulong to void |
| 246 | if lt d 10 |
| 247 | tx_c add d 48 |
| 248 | else |
| 249 | tx_c add d 87 |
| 250 | end |
| 251 | end |
| 252 | note n hex digits of the low bits of v, most significant first |
| 253 | fn tx_hexn v ulong n ulong to void |
| 254 | for j ulong n bound 16 |
| 255 | tx_hx and shr v mul sub sub n 1 j 4 15 |
| 256 | end |
| 257 | end |
| 258 | fn tx_kw i ulong to void |
| 259 | s = ulong get x_kwo i |
| 260 | for j ulong ulong get x_kwl i bound 16 |
| 261 | tx_c ulong get x_kw add s j |
| 262 | end |
| 263 | end |
| 264 | fn tx_sp to void |
| 265 | tx_c 32 |
| 266 | end |
| 267 | fn tx_nl to void |
| 268 | tx_c 10 |
| 269 | end |
| 270 | fn tx_rname op ulong to void |
| 271 | s = ulong get x_rno op |
| 272 | for j ulong ulong get x_rnl op bound 16 |
| 273 | tx_c ulong get x_rows add s j |
| 274 | end |
| 275 | end |
| 276 | note the built-in name of t, or %tN |
| 277 | fn tx_ty t ulong to void |
| 278 | if le t 20 |
| 279 | if ne get x_tnl t 1 |
| 280 | s = ulong get x_tno t |
| 281 | for j ulong ulong get x_tnl t bound 8 |
| 282 | tx_c ulong get x_tyn add s j |
| 283 | end |
| 284 | ret |
| 285 | end |
| 286 | end |
| 287 | tx_c 37 |
| 288 | tx_c 116 |
| 289 | tx_num t |
| 290 | end |
| 291 | fn tx_val v ulong to void |
| 292 | tx_c 37 |
| 293 | tx_num v |
| 294 | end |