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 / ngdis.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/ngdis.om: ΩIR binary on stdin to its one canonical text on stdout (seed/ng/IR.md s10). |
| 3 | note Owner: ng-ir lane. Build: cat seed/ng/ngtxt.om seed/ng/ngdis.om | seed/out/omc > ngdis |
| 4 | note ngdis decodes structure only (it is not the validator): it prints every unit whose bytes the |
| 5 | note text can spell, ids out of range and semantic faults included, and refuses the rest with the |
| 6 | note IR.md s11 class as its exit code and "ngdis: refused class C at OFFSET" on stderr: |
| 7 | note 10 MAGIC, 11 VERSION, 12 TRUNCATED, 13 an enumeration or field the text cannot spell (a type |
| 8 | note kind, access, space, field order, role, global class or function kind outside its set, a |
| 9 | note nonzero ty on a row without one, a non-canonical const), 14 TRAILING, 15 UNKNOWN_OP, |
| 10 | note 23 LIMIT: input over 16777216 bytes (at 16777216), more than 65536 types, globals, functions, |
| 11 | note blocks in a function, or parameters of a function or block (at the count), 27 IO. |
| 12 | text z_msg "ngdis: refused class \n at \n" |
| 13 | global z_in uchar 16777216 |
| 14 | global z_s ulong 4 |
| 15 | global z_pt uint 65536 |
| 16 | global z_k ulong 8 |
| 17 | global z_pb uchar 8 |
| 18 | fn dz_say a ulong b ulong to void |
| 19 | r = write 2 z_msg a sub b a |
| 20 | end |
| 21 | fn dz_bad c ulong off ulong to void |
| 22 | dz_say 0 21 |
| 23 | put x_on 0 0 |
| 24 | tx_num c |
| 25 | r = write 2 x_out 0 get x_on 0 |
| 26 | dz_say 22 26 |
| 27 | put x_on 0 0 |
| 28 | tx_num off |
| 29 | tx_c 10 |
| 30 | r = write 2 x_out 0 get x_on 0 |
| 31 | exit int c |
| 32 | end |
| 33 | fn dz_at to ulong |
| 34 | ret get z_s 0 |
| 35 | end |
| 36 | fn dz_need k ulong to ulong |
| 37 | p = get z_s 0 |
| 38 | if gt add p k get z_s 1 |
| 39 | dz_bad 12 p |
| 40 | end |
| 41 | put z_s 0 add p k |
| 42 | ret p |
| 43 | end |
| 44 | fn dz_un k ulong to ulong |
| 45 | p = dz_need k |
| 46 | v = ulong 0 |
| 47 | for j ulong k bound 8 |
| 48 | x = ulong get z_in add p j |
| 49 | v = or v shl x mul j 8 |
| 50 | end |
| 51 | ret v |
| 52 | end |
| 53 | fn dz_u8 to ulong |
| 54 | ret dz_un 1 |
| 55 | end |
| 56 | fn dz_u32 to ulong |
| 57 | ret dz_un 4 |
| 58 | end |
| 59 | note a table count (IR.md s12): over 65536 is LIMIT at the count, before what it counts |
| 60 | fn dz_cnt to ulong |
| 61 | o = dz_at |
| 62 | n = dz_u32 |
| 63 | if gt n 65536 |
| 64 | dz_bad 23 o |
| 65 | end |
| 66 | ret n |
| 67 | end |
| 68 | note ---- header and tables ------------------------------------------------------------------------ |
| 69 | fn dz_head to void |
| 70 | n = get z_s 1 |
| 71 | for j ulong 7 |
| 72 | if ge j n |
| 73 | dz_bad 12 0 |
| 74 | end |
| 75 | x = ulong get z_in j |
| 76 | if lt j 6 |
| 77 | if ne x dz_magic j |
| 78 | dz_bad 10 0 |
| 79 | end |
| 80 | else |
| 81 | if ne x 0 |
| 82 | dz_bad 10 0 |
| 83 | end |
| 84 | end |
| 85 | end |
| 86 | put z_s 0 7 |
| 87 | if ne dz_u8 9 |
| 88 | dz_bad 11 7 |
| 89 | end |
| 90 | p = dz_need 16 |
| 91 | for j ulong 16 |
| 92 | if ne ulong get z_in add 8 j dz_tag j |
| 93 | dz_bad 11 8 |
| 94 | end |
| 95 | end |
| 96 | end |
| 97 | note magic "OSC1IR" and tag "omega.ng.ir.v1" then two zero bytes |
| 98 | fn dz_magic j ulong to ulong |
| 99 | if eq j 0 |
| 100 | ret 79 |
| 101 | end |
| 102 | if eq j 1 |
| 103 | ret 83 |
| 104 | end |
| 105 | if eq j 2 |
| 106 | ret 67 |
| 107 | end |
| 108 | if eq j 3 |
| 109 | ret 49 |
| 110 | end |
| 111 | if eq j 4 |
| 112 | ret 73 |
| 113 | end |
| 114 | ret 82 |
| 115 | end |
| 116 | fn dz_tag j ulong to ulong |
| 117 | if ge j 14 |
| 118 | ret 0 |
| 119 | end |
| 120 | if eq j 0 |
| 121 | ret 111 |
| 122 | end |
| 123 | if eq j 1 |
| 124 | ret 109 |
| 125 | end |
| 126 | if eq j 2 |
| 127 | ret 101 |
| 128 | end |
| 129 | if eq j 3 |
| 130 | ret 103 |
| 131 | end |
| 132 | if eq j 4 |
| 133 | ret 97 |
| 134 | end |
| 135 | if or eq j 5 eq j 8 |
| 136 | ret 46 |
| 137 | end |
| 138 | if eq j 6 |
| 139 | ret 110 |
| 140 | end |
| 141 | if eq j 7 |
| 142 | ret 103 |
| 143 | end |
| 144 | if eq j 9 |
| 145 | ret 105 |
| 146 | end |
| 147 | if eq j 10 |
| 148 | ret 114 |
| 149 | end |
| 150 | if eq j 11 |
| 151 | ret 46 |
| 152 | end |
| 153 | if eq j 12 |
| 154 | ret 118 |
| 155 | end |
| 156 | ret 49 |
| 157 | end |
| 158 | fn dz_types to void |
| 159 | n = dz_cnt |
| 160 | for k ulong n bound 16777216 |
| 161 | dz_type add k 32 |
| 162 | end |
| 163 | end |
| 164 | fn dz_type t ulong to void |
| 165 | o = dz_at |
| 166 | kd = dz_u8 |
| 167 | if or eq kd 0 gt kd 6 |
| 168 | dz_bad 13 o |
| 169 | end |
| 170 | tx_kw 27 |
| 171 | tx_sp |
| 172 | tx_ty t |
| 173 | tx_c 32 |
| 174 | tx_c 61 |
| 175 | tx_sp |
| 176 | tx_kw add 14 kd |
| 177 | sz = ulong 0 |
| 178 | if or eq kd 1 eq kd 2 |
| 179 | sz = dz_tva kd |
| 180 | end |
| 181 | if eq kd 3 |
| 182 | sz = dz_tstr o |
| 183 | end |
| 184 | if or eq kd 4 eq kd 5 |
| 185 | dz_tref o |
| 186 | end |
| 187 | if eq kd 6 |
| 188 | dz_tfrg o |
| 189 | end |
| 190 | tx_nl |
| 191 | tx_tset t kd sz |
| 192 | end |
| 193 | note vec E L, array E C; the entry's byte size |
| 194 | fn dz_tva kd ulong to ulong |
| 195 | e = dz_u32 |
| 196 | c = dz_u32 |
| 197 | tx_sp |
| 198 | tx_ty e |
| 199 | tx_sp |
| 200 | tx_num c |
| 201 | ret tx_mul c tx_size e |
| 202 | end |
| 203 | fn dz_tstr o ulong to ulong |
| 204 | sz = dz_u32 |
| 205 | nf = dz_u32 |
| 206 | tx_sp |
| 207 | tx_num sz |
| 208 | for j ulong nf bound 16777216 |
| 209 | ft = dz_u32 |
| 210 | of = dz_u32 |
| 211 | od = dz_u8 |
| 212 | if gt od 1 |
| 213 | dz_bad 13 o |
| 214 | end |
| 215 | tx_sp |
| 216 | tx_ty ft |
| 217 | tx_c 64 |
| 218 | tx_num of |
| 219 | tx_c 58 |
| 220 | tx_kw add 13 od |
| 221 | end |
| 222 | ret sz |
| 223 | end |
| 224 | fn dz_tref o ulong to void |
| 225 | x = dz_u32 |
| 226 | ac = dz_u8 |
| 227 | sp = dz_u8 |
| 228 | if or gt ac 1 gt sp 4 |
| 229 | dz_bad 13 o |
| 230 | end |
| 231 | tx_sp |
| 232 | tx_kw add 3 ac |
| 233 | tx_sp |
| 234 | tx_kw add 5 sp |
| 235 | tx_sp |
| 236 | tx_ty x |
| 237 | end |
| 238 | fn dz_tfrg o ulong to void |
| 239 | ro = dz_u8 |
| 240 | if gt ro 2 |
| 241 | dz_bad 13 o |
| 242 | end |
| 243 | tx_sp |
| 244 | tx_kw add 10 ro |
| 245 | for j ulong 3 |
| 246 | tx_sp |
| 247 | tx_num dz_un 2 |
| 248 | end |
| 249 | tx_sp |
| 250 | tx_ty dz_u32 |
| 251 | end |
| 252 | fn dz_globs to void |
| 253 | n = dz_cnt |
| 254 | for g ulong n bound 16777216 |
| 255 | dz_glob g |
| 256 | end |
| 257 | end |
| 258 | fn dz_glob g ulong to void |
| 259 | o = dz_at |
| 260 | t = dz_u32 |
| 261 | c = dz_u8 |
| 262 | if gt c 2 |
| 263 | dz_bad 13 o |
| 264 | end |
| 265 | tx_kw 6 |
| 266 | tx_c 32 |
| 267 | tx_c 64 |
| 268 | tx_c 103 |
| 269 | tx_num g |
| 270 | tx_sp |
| 271 | tx_ty t |
| 272 | tx_sp |
| 273 | if eq c 2 |
| 274 | tx_kw 32 |
| 275 | else |
| 276 | tx_kw add 21 c |
| 277 | end |
| 278 | if eq c 1 |
| 279 | n = tx_size t |
| 280 | p = dz_need n |
| 281 | if gt n 0 |
| 282 | tx_sp |
| 283 | end |
| 284 | for j ulong n bound 1099511627776 |
| 285 | tx_hexn ulong get z_in add p j 2 |
| 286 | end |
| 287 | end |
| 288 | tx_nl |
| 289 | end |
| 290 | note ---- functions ------------------------------------------------------------------------------ |
| 291 | fn dz_funcs to void |
| 292 | n = dz_cnt |
| 293 | for f ulong n bound 16777216 |
| 294 | dz_fn f |
| 295 | end |
| 296 | end |
| 297 | fn dz_fn f ulong to void |
| 298 | o = dz_at |
| 299 | k = dz_u8 |
| 300 | fl = dz_u8 |
| 301 | if gt k 2 |
| 302 | dz_bad 13 o |
| 303 | end |
| 304 | po = dz_at |
| 305 | np = dz_u32 |
| 306 | if gt np 65536 |
| 307 | dz_bad 23 po |
| 308 | end |
| 309 | for j ulong np bound 65536 |
| 310 | put z_pt j uint dz_u32 |
| 311 | end |
| 312 | rs = dz_u32 |
| 313 | tx_kw k |
| 314 | tx_c 32 |
| 315 | tx_c 64 |
| 316 | tx_c 102 |
| 317 | tx_num f |
| 318 | tx_sp |
| 319 | tx_kw 23 |
| 320 | tx_sp |
| 321 | tx_num fl |
| 322 | if eq k 1 |
| 323 | dz_kern |
| 324 | end |
| 325 | for j ulong np bound 65536 |
| 326 | tx_sp |
| 327 | tx_val add j 1 |
| 328 | tx_c 58 |
| 329 | tx_ty ulong get z_pt j |
| 330 | end |
| 331 | tx_c 32 |
| 332 | tx_c 45 |
| 333 | tx_c 62 |
| 334 | tx_sp |
| 335 | tx_ty rs |
| 336 | tx_nl |
| 337 | put z_k 0 add np 1 |
| 338 | nb = dz_cnt |
| 339 | for j ulong nb bound 16777216 |
| 340 | dz_blk j |
| 341 | end |
| 342 | tx_kw 28 |
| 343 | tx_nl |
| 344 | end |
| 345 | fn dz_kern to void |
| 346 | tx_sp |
| 347 | tx_kw 24 |
| 348 | for j ulong 3 |
| 349 | tx_sp |
| 350 | tx_num dz_u32 |
| 351 | end |
| 352 | tx_sp |
| 353 | tx_kw 25 |
| 354 | tx_sp |
| 355 | tx_num dz_u8 |
| 356 | end |
| 357 | fn dz_blk j ulong to void |
| 358 | tx_kw 26 |
| 359 | tx_sp |
| 360 | tx_c 94 |
| 361 | tx_num j |
| 362 | np = dz_cnt |
| 363 | for k ulong np bound 16777216 |
| 364 | tx_sp |
| 365 | v = get z_k 0 |
| 366 | tx_val v |
| 367 | put z_k 0 add v 1 |
| 368 | tx_c 58 |
| 369 | tx_ty dz_u32 |
| 370 | end |
| 371 | tx_nl |
| 372 | n = dz_u32 |
| 373 | for k ulong n bound 16777216 |
| 374 | dz_op |
| 375 | end |
| 376 | end |
| 377 | fn dz_op to void |
| 378 | o = dz_at |
| 379 | op = dz_u8 |
| 380 | if eq get x_rok op 0 |
| 381 | dz_bad 15 o |
| 382 | end |
| 383 | t = dz_u32 |
| 384 | c = ulong get x_rty op |
| 385 | if and eq c 0 ne t 0 |
| 386 | dz_bad 13 o |
| 387 | end |
| 388 | m = ulong 0 |
| 389 | if ne get x_rim op 0 |
| 390 | m = dz_un 8 |
| 391 | end |
| 392 | if and eq op 1 not tx_canon t m |
| 393 | dz_bad 13 o |
| 394 | end |
| 395 | n = ulong 0 |
| 396 | if ne get x_rva op 0 |
| 397 | n = dz_u32 |
| 398 | end |
| 399 | dz_defs op t |
| 400 | tx_rname op |
| 401 | if ne c 0 |
| 402 | tx_sp |
| 403 | tx_ty t |
| 404 | end |
| 405 | if ne get x_rim op 0 |
| 406 | tx_sp |
| 407 | dz_imm op t m |
| 408 | end |
| 409 | k = add add tx_ntok op ulong get x_rfx op n |
| 410 | for s ulong k bound 16777216 |
| 411 | tx_sp |
| 412 | dz_opnd op s dz_u32 |
| 413 | end |
| 414 | tx_nl |
| 415 | end |
| 416 | fn dz_defs op ulong t ulong to void |
| 417 | d = tx_ndef op t |
| 418 | if eq d 0 |
| 419 | ret |
| 420 | end |
| 421 | v = get z_k 0 |
| 422 | tx_val v |
| 423 | if eq d 2 |
| 424 | tx_sp |
| 425 | tx_val add v 1 |
| 426 | end |
| 427 | tx_sp |
| 428 | tx_c 61 |
| 429 | tx_sp |
| 430 | put z_k 0 add v d |
| 431 | end |
| 432 | fn dz_imm op ulong t ulong m ulong to void |
| 433 | if or eq op 52 and ge op 111 le op 116 |
| 434 | tx_c 64 |
| 435 | tx_c 103 |
| 436 | tx_num m |
| 437 | ret |
| 438 | end |
| 439 | if ne op 1 |
| 440 | tx_num m |
| 441 | ret |
| 442 | end |
| 443 | f = tx_cf t |
| 444 | if eq f 1 |
| 445 | tx_kw sub 31 m |
| 446 | ret |
| 447 | end |
| 448 | if eq f 4 |
| 449 | tx_c 48 |
| 450 | tx_c 120 |
| 451 | tx_hexn m mul tx_sb t 2 |
| 452 | ret |
| 453 | end |
| 454 | if and eq f 3 ne shr m 63 0 |
| 455 | tx_c 45 |
| 456 | tx_num sub 0 m |
| 457 | ret |
| 458 | end |
| 459 | tx_num m |
| 460 | end |
| 461 | fn dz_opnd op ulong s ulong v ulong to void |
| 462 | k = tx_slot op s |
| 463 | if eq k 1 |
| 464 | tx_c 94 |
| 465 | tx_num v |
| 466 | ret |
| 467 | end |
| 468 | if eq k 2 |
| 469 | tx_c 64 |
| 470 | tx_c 102 |
| 471 | tx_num v |
| 472 | ret |
| 473 | end |
| 474 | tx_val v |
| 475 | end |
| 476 | fn main to int |
| 477 | tx_init |
| 478 | n = ulong 0 |
| 479 | while lt n len z_in bound 16777217 |
| 480 | k = read 0 z_in n sub len z_in n |
| 481 | if lt k 0 |
| 482 | dz_bad 27 n |
| 483 | end |
| 484 | if eq k 0 |
| 485 | break |
| 486 | end |
| 487 | n = add n ulong k |
| 488 | end |
| 489 | if eq n len z_in |
| 490 | k = read 0 z_pb 0 1 |
| 491 | if lt k 0 |
| 492 | dz_bad 27 n |
| 493 | end |
| 494 | if gt k 0 |
| 495 | dz_bad 23 n |
| 496 | end |
| 497 | end |
| 498 | put z_s 1 n |
| 499 | dz_head |
| 500 | tx_kw 29 |
| 501 | tx_sp |
| 502 | tx_c 49 |
| 503 | tx_nl |
| 504 | dz_types |
| 505 | dz_globs |
| 506 | dz_funcs |
| 507 | if lt dz_at n |
| 508 | dz_bad 14 dz_at |
| 509 | end |
| 510 | tx_flush |
| 511 | ret 0 |
| 512 | end |