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 / src / adapters / rust-i64-export.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | note Handwritten S2 upper-to-Rust 2021 exporter (spec/universal/translation-s2-v1.md sections 2 and 4). |
| 3 | note Prepended unchanged at build: src/source-aware/vendor/sha256.om and src/source-aware/core-v3.om (decode n). |
| 4 | note Input: one OMSB envelope to EOF, a source-aware v3 unit of profile 10..15. Output is generated from the |
| 5 | note OMSA operation records only; the OMSP sidecar is validated by the v3 decoder, never read for output. |
| 6 | note u64 arithmetic and i64 attribute 5 use wrapping_add/sub/mul; i64 attribute 6 uses plain + - *, which |
| 7 | note panics on overflow in a debug build; select is `if c { x } else { y }`, evaluated lazily like op 16. |
| 8 | global Yone uchar 1 |
| 9 | global Yo uchar 4096 |
| 10 | global Yl ulong 1 |
| 11 | global Yni ulong 1 |
| 12 | global Ypt ulong 6 |
| 13 | global Yln ulong 64 |
| 14 | global Ynm uchar 64 |
| 15 | global Ynl ulong 1 |
| 16 | global Ylb uchar 1 |
| 17 | text Yhead "pub fn " |
| 18 | text Ydflt "calc" |
| 19 | text Ykw "_ as break const continue crate else enum extern false fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait true type unsafe use where while async await dyn abstract become box do final macro override priv typeof unsized virtual yield try gen" |
| 20 | text Yu ": u64" |
| 21 | text Yi ": i64" |
| 22 | text Yru ") -> u64 { " |
| 23 | text Yri ") -> i64 { " |
| 24 | text Yrb ") -> bool { " |
| 25 | text Ytail " }\n" |
| 26 | text Ymeth ".wrapping_add(.wrapping_sub(.wrapping_mul(" |
| 27 | text Yops " + - * " |
| 28 | text Ycmp " == != < <= > >= " |
| 29 | text Ysel1 "if " |
| 30 | text Ysel2 " { " |
| 31 | text Ysel3 " } else { " |
| 32 | text Ysel4 " }" |
| 33 | text Ytrue "true" |
| 34 | text Yfalse "false" |
| 35 | text YMal "RUST_EXPORT_MALFORMED\n" |
| 36 | text YLim "RUST_EXPORT_LIMIT\n" |
| 37 | text YProf "RUST_EXPORT_PROFILE_MISMATCH\n" |
| 38 | text YIO "RUST_EXPORT_IO\n" |
| 39 | text YName "RUST_EXPORT_BAD_NAME\n" |
| 40 | |
| 41 | fn ydie code ulong to void |
| 42 | if eq code 65 |
| 43 | w = write 2 YMal 0 len YMal |
| 44 | end |
| 45 | if eq code 67 |
| 46 | w = write 2 YLim 0 len YLim |
| 47 | end |
| 48 | if eq code 64 |
| 49 | w = write 2 YName 0 len YName |
| 50 | end |
| 51 | if eq code 68 |
| 52 | w = write 2 YProf 0 len YProf |
| 53 | end |
| 54 | if eq code 77 |
| 55 | w = write 2 YIO 0 len YIO |
| 56 | end |
| 57 | exit int code |
| 58 | end |
| 59 | |
| 60 | fn yalnum c ulong to bool |
| 61 | if and ge c 65 le c 90 |
| 62 | ret true |
| 63 | end |
| 64 | if and ge c 97 le c 122 |
| 65 | ret true |
| 66 | end |
| 67 | if and ge c 48 le c 57 |
| 68 | ret true |
| 69 | end |
| 70 | ret eq c 95 |
| 71 | end |
| 72 | |
| 73 | fn ykwchar p ulong to bool |
| 74 | if ge p len Ykw |
| 75 | ret false |
| 76 | end |
| 77 | ret ne get Ykw p 32 |
| 78 | end |
| 79 | |
| 80 | fn ykwsame b ulong e ulong to bool |
| 81 | if ne sub e b get Ynl 0 |
| 82 | ret false |
| 83 | end |
| 84 | for i ulong get Ynl 0 |
| 85 | if ne get Ykw add b i get Ynm i |
| 86 | ret false |
| 87 | end |
| 88 | end |
| 89 | ret true |
| 90 | end |
| 91 | |
| 92 | note ykeyword: NAME is a Rust 2021 strict or reserved keyword (or the lone `_`), so not a function name. |
| 93 | fn ykeyword to bool |
| 94 | p = ulong 0 |
| 95 | while lt p len Ykw |
| 96 | b = p |
| 97 | while ykwchar p |
| 98 | p = add p 1 |
| 99 | end |
| 100 | if ykwsame b p |
| 101 | ret true |
| 102 | end |
| 103 | p = add p 1 |
| 104 | end |
| 105 | ret false |
| 106 | end |
| 107 | |
| 108 | note ytakename (amendment A8): input frame as c-export: one byte name length L (0 = calc), L name bytes, then |
| 109 | note the envelope. NAME is a C identifier of at most 63 bytes and not a Rust keyword; otherwise 64. |
| 110 | fn ytakename to void |
| 111 | r = read 0 Ylb 0 1 |
| 112 | if lt r 0 |
| 113 | ydie 77 |
| 114 | end |
| 115 | if eq r 0 |
| 116 | ydie 64 |
| 117 | end |
| 118 | n = ulong get Ylb 0 |
| 119 | if eq n 0 |
| 120 | for i ulong len Ydflt |
| 121 | put Ynm i get Ydflt i |
| 122 | end |
| 123 | put Ynl 0 len Ydflt |
| 124 | ret |
| 125 | end |
| 126 | if gt n 63 |
| 127 | ydie 64 |
| 128 | end |
| 129 | got = ulong 0 |
| 130 | while lt got n |
| 131 | r = read 0 Ynm got sub n got |
| 132 | if lt r 0 |
| 133 | ydie 77 |
| 134 | end |
| 135 | if eq r 0 |
| 136 | ydie 64 |
| 137 | end |
| 138 | got = add got ulong r |
| 139 | end |
| 140 | put Ynl 0 n |
| 141 | f = ulong get Ynm 0 |
| 142 | if and ge f 48 le f 57 |
| 143 | ydie 64 |
| 144 | end |
| 145 | for i ulong n |
| 146 | if not yalnum ulong get Ynm i |
| 147 | ydie 64 |
| 148 | end |
| 149 | end |
| 150 | if ykeyword |
| 151 | ydie 64 |
| 152 | end |
| 153 | end |
| 154 | |
| 155 | fn ybyte c ulong to void |
| 156 | put Yo get Yl 0 uchar c |
| 157 | put Yl 0 add get Yl 0 1 |
| 158 | end |
| 159 | |
| 160 | fn ydigits v ulong to ulong |
| 161 | d = ulong 1 |
| 162 | x = div v 10 |
| 163 | while gt x 0 |
| 164 | d = add d 1 |
| 165 | x = div x 10 |
| 166 | end |
| 167 | ret d |
| 168 | end |
| 169 | |
| 170 | fn ydec v ulong to void |
| 171 | d = new uchar 20 0 |
| 172 | i = ulong 0 |
| 173 | x = v |
| 174 | put d 0 uchar add rem x 10 48 |
| 175 | x = div x 10 |
| 176 | i = add i 1 |
| 177 | while gt x 0 |
| 178 | put d i uchar add rem x 10 48 |
| 179 | x = div x 10 |
| 180 | i = add i 1 |
| 181 | end |
| 182 | while gt i 0 |
| 183 | i = sub i 1 |
| 184 | ybyte ulong get d i |
| 185 | end |
| 186 | end |
| 187 | |
| 188 | note ys2: a profile 10..15 unit (already fully validated by decode); records each input slot's type. |
| 189 | fn ys2 to ulong |
| 190 | p = get M 1 |
| 191 | if or lt p 10 gt p 15 |
| 192 | ret 68 |
| 193 | end |
| 194 | put Yni 0 0 |
| 195 | for i ulong get M 4 |
| 196 | if eq get Oc i 9 |
| 197 | put Yni 0 add get Yni 0 1 |
| 198 | end |
| 199 | end |
| 200 | for j ulong get M 4 |
| 201 | if eq get Oc j 9 |
| 202 | if ge get Oi j get Yni 0 |
| 203 | ret 68 |
| 204 | end |
| 205 | put Ypt get Oi j get Ot j |
| 206 | end |
| 207 | end |
| 208 | ret 0 |
| 209 | end |
| 210 | |
| 211 | fn ysat v ulong to ulong |
| 212 | if gt v 1048576 |
| 213 | ret 1048576 |
| 214 | end |
| 215 | ret v |
| 216 | end |
| 217 | |
| 218 | note ywrapped: the record is printed inside one pair of parentheses (dropped at the root). |
| 219 | fn ywrapped k ulong to bool |
| 220 | c = get Oc k |
| 221 | if ge c 10 |
| 222 | ret true |
| 223 | end |
| 224 | if and ge c 2 le c 4 |
| 225 | ret and eq get Ot k 5 eq get Oat k 6 |
| 226 | end |
| 227 | if eq c 1 |
| 228 | ret and eq get Ot k 5 ge get Oi k 9223372036854775808 |
| 229 | end |
| 230 | ret false |
| 231 | end |
| 232 | |
| 233 | fn ytrim k ulong to ulong |
| 234 | if ywrapped k |
| 235 | ret 2 |
| 236 | end |
| 237 | ret 0 |
| 238 | end |
| 239 | |
| 240 | fn yclen c ulong to ulong |
| 241 | if or eq c 12 eq c 14 |
| 242 | ret 3 |
| 243 | end |
| 244 | ret 4 |
| 245 | end |
| 246 | |
| 247 | note Length of each result's expression as printed inside a larger expression. |
| 248 | fn ymetric k ulong to void |
| 249 | c = get Oc k |
| 250 | t = get Ot k |
| 251 | if eq c 9 |
| 252 | put Yln k 2 |
| 253 | ret |
| 254 | end |
| 255 | if eq c 1 |
| 256 | v = get Oi k |
| 257 | if eq t 6 |
| 258 | put Yln k sub 5 v |
| 259 | ret |
| 260 | end |
| 261 | if and eq t 5 ge v 9223372036854775808 |
| 262 | put Yln k add ydigits sub 0 v 6 |
| 263 | ret |
| 264 | end |
| 265 | put Yln k add ydigits v 3 |
| 266 | ret |
| 267 | end |
| 268 | a = get Oa k |
| 269 | if eq c 17 |
| 270 | put Yln k ysat add get Yln a 3 |
| 271 | ret |
| 272 | end |
| 273 | b = get Ob k |
| 274 | if eq c 16 |
| 275 | s = add add get Yln a get Yln b get Yln get Oi k |
| 276 | s = sub s add add ytrim a ytrim b ytrim get Oi k |
| 277 | put Yln k ysat add s 20 |
| 278 | ret |
| 279 | end |
| 280 | if ge c 10 |
| 281 | put Yln k ysat add add add get Yln a get Yln b yclen c 2 |
| 282 | ret |
| 283 | end |
| 284 | if ywrapped k |
| 285 | put Yln k ysat add add get Yln a get Yln b 5 |
| 286 | ret |
| 287 | end |
| 288 | put Yln k ysat add add get Yln a get Yln b 15 |
| 289 | end |
| 290 | |
| 291 | fn ytext off ulong n ulong to void |
| 292 | for i ulong n |
| 293 | ybyte ulong get Ycmp add off i |
| 294 | end |
| 295 | end |
| 296 | |
| 297 | fn yemit k ulong top ulong to void |
| 298 | c = get Oc k |
| 299 | t = get Ot k |
| 300 | if eq c 9 |
| 301 | ybyte 105 |
| 302 | ybyte add 48 get Oi k |
| 303 | ret |
| 304 | end |
| 305 | if eq c 1 |
| 306 | v = get Oi k |
| 307 | if eq t 6 |
| 308 | if eq v 1 |
| 309 | for i ulong len Ytrue |
| 310 | ybyte ulong get Ytrue i |
| 311 | end |
| 312 | else |
| 313 | for i ulong len Yfalse |
| 314 | ybyte ulong get Yfalse i |
| 315 | end |
| 316 | end |
| 317 | ret |
| 318 | end |
| 319 | if and eq t 5 ge v 9223372036854775808 |
| 320 | if eq top 0 |
| 321 | ybyte 40 |
| 322 | end |
| 323 | ybyte 45 |
| 324 | ydec sub 0 v |
| 325 | ybyte 105 |
| 326 | ybyte 54 |
| 327 | ybyte 52 |
| 328 | if eq top 0 |
| 329 | ybyte 41 |
| 330 | end |
| 331 | ret |
| 332 | end |
| 333 | ydec v |
| 334 | if eq t 5 |
| 335 | ybyte 105 |
| 336 | else |
| 337 | ybyte 117 |
| 338 | end |
| 339 | ybyte 54 |
| 340 | ybyte 52 |
| 341 | ret |
| 342 | end |
| 343 | if eq c 16 |
| 344 | if eq top 0 |
| 345 | ybyte 40 |
| 346 | end |
| 347 | for i ulong len Ysel1 |
| 348 | ybyte ulong get Ysel1 i |
| 349 | end |
| 350 | yemit get Oa k 1 |
| 351 | for i ulong len Ysel2 |
| 352 | ybyte ulong get Ysel2 i |
| 353 | end |
| 354 | yemit get Ob k 1 |
| 355 | for i ulong len Ysel3 |
| 356 | ybyte ulong get Ysel3 i |
| 357 | end |
| 358 | yemit get Oi k 1 |
| 359 | for i ulong len Ysel4 |
| 360 | ybyte ulong get Ysel4 i |
| 361 | end |
| 362 | if eq top 0 |
| 363 | ybyte 41 |
| 364 | end |
| 365 | ret |
| 366 | end |
| 367 | if eq c 17 |
| 368 | if eq top 0 |
| 369 | ybyte 40 |
| 370 | end |
| 371 | ybyte 33 |
| 372 | yemit get Oa k 0 |
| 373 | if eq top 0 |
| 374 | ybyte 41 |
| 375 | end |
| 376 | ret |
| 377 | end |
| 378 | if ge c 10 |
| 379 | if eq top 0 |
| 380 | ybyte 40 |
| 381 | end |
| 382 | yemit get Oa k 0 |
| 383 | off = mul sub c 10 4 |
| 384 | ytext off yclen c |
| 385 | yemit get Ob k 0 |
| 386 | if eq top 0 |
| 387 | ybyte 41 |
| 388 | end |
| 389 | ret |
| 390 | end |
| 391 | if ywrapped k |
| 392 | if eq top 0 |
| 393 | ybyte 40 |
| 394 | end |
| 395 | yemit get Oa k 0 |
| 396 | off = mul sub c 2 2 |
| 397 | for i ulong 3 |
| 398 | ybyte ulong get Yops add off i |
| 399 | end |
| 400 | yemit get Ob k 0 |
| 401 | if eq top 0 |
| 402 | ybyte 41 |
| 403 | end |
| 404 | ret |
| 405 | end |
| 406 | yemit get Oa k 0 |
| 407 | off = mul sub c 2 14 |
| 408 | for i ulong 14 |
| 409 | ybyte ulong get Ymeth add off i |
| 410 | end |
| 411 | yemit get Ob k 0 |
| 412 | ybyte 41 |
| 413 | end |
| 414 | |
| 415 | note translation-v1 section 4 on S2 (translation-s2-v1 amendment A3): the pre-normalization root of a |
| 416 | note NORMALIZED unit (const 0 then add attribute 5 of an i64 root, or the S1 u64 pair), else the last result. |
| 417 | fn yroot to ulong |
| 418 | n = get M 4 |
| 419 | if lt n 3 |
| 420 | ret sub n 1 |
| 421 | end |
| 422 | z = sub n 2 |
| 423 | y = sub n 1 |
| 424 | if or ne get Oc z 1 ne get Oi z 0 |
| 425 | ret y |
| 426 | end |
| 427 | if or ne get Oc y 2 ne get Ob y z |
| 428 | ret y |
| 429 | end |
| 430 | if ge get Oa y z |
| 431 | ret y |
| 432 | end |
| 433 | if eq get Ot y 5 |
| 434 | if ne get Oat y 5 |
| 435 | ret y |
| 436 | end |
| 437 | end |
| 438 | ret get Oa y |
| 439 | end |
| 440 | |
| 441 | fn yparams to ulong |
| 442 | ni = get Yni 0 |
| 443 | if eq ni 0 |
| 444 | ret 0 |
| 445 | end |
| 446 | ret sub mul ni 9 2 |
| 447 | end |
| 448 | |
| 449 | fn yrettext t ulong to void |
| 450 | if eq t 1 |
| 451 | for u ulong len Yru |
| 452 | ybyte ulong get Yru u |
| 453 | end |
| 454 | end |
| 455 | if eq t 5 |
| 456 | for u ulong len Yri |
| 457 | ybyte ulong get Yri u |
| 458 | end |
| 459 | end |
| 460 | if eq t 6 |
| 461 | for u ulong len Yrb |
| 462 | ybyte ulong get Yrb u |
| 463 | end |
| 464 | end |
| 465 | end |
| 466 | |
| 467 | fn ysignature t ulong to void |
| 468 | for i ulong len Yhead |
| 469 | ybyte ulong get Yhead i |
| 470 | end |
| 471 | for i ulong get Ynl 0 |
| 472 | ybyte ulong get Ynm i |
| 473 | end |
| 474 | ybyte 40 |
| 475 | for s ulong get Yni 0 |
| 476 | if gt s 0 |
| 477 | ybyte 44 |
| 478 | ybyte 32 |
| 479 | end |
| 480 | ybyte 105 |
| 481 | ybyte add 48 s |
| 482 | if eq get Ypt s 5 |
| 483 | for q ulong len Yi |
| 484 | ybyte ulong get Yi q |
| 485 | end |
| 486 | else |
| 487 | for q ulong len Yu |
| 488 | ybyte ulong get Yu q |
| 489 | end |
| 490 | end |
| 491 | end |
| 492 | yrettext t |
| 493 | end |
| 494 | |
| 495 | fn yreadunit to ulong |
| 496 | n = ulong 0 |
| 497 | more = ulong 1 |
| 498 | while and eq more 1 lt n 8192 |
| 499 | r = read 0 Ib n sub ulong 8192 n |
| 500 | if lt r 0 |
| 501 | ydie 77 |
| 502 | end |
| 503 | if eq r 0 |
| 504 | more = 0 |
| 505 | else |
| 506 | n = add n ulong r |
| 507 | end |
| 508 | end |
| 509 | if eq n 8192 |
| 510 | r = read 0 Yone 0 1 |
| 511 | if lt r 0 |
| 512 | ydie 77 |
| 513 | end |
| 514 | if gt r 0 |
| 515 | ydie 65 |
| 516 | end |
| 517 | end |
| 518 | ret n |
| 519 | end |
| 520 | |
| 521 | fn main to int |
| 522 | constants |
| 523 | ytakename |
| 524 | n = yreadunit |
| 525 | code = decode n |
| 526 | if eq code 68 |
| 527 | ydie 68 |
| 528 | end |
| 529 | if ne code 0 |
| 530 | ydie 65 |
| 531 | end |
| 532 | if ne ys2 0 |
| 533 | ydie 68 |
| 534 | end |
| 535 | for k ulong get M 4 |
| 536 | ymetric k |
| 537 | end |
| 538 | root = yroot |
| 539 | t = get Ot root |
| 540 | total = add add len Yhead len Yru len Ytail |
| 541 | total = add total add get Ynl 0 1 |
| 542 | if eq t 6 |
| 543 | total = add total 1 |
| 544 | end |
| 545 | total = add total yparams |
| 546 | total = add total get Yln root |
| 547 | if ywrapped root |
| 548 | total = sub total 2 |
| 549 | end |
| 550 | if gt total 4096 |
| 551 | ydie 67 |
| 552 | end |
| 553 | put Yl 0 0 |
| 554 | ysignature t |
| 555 | yemit root 1 |
| 556 | for i ulong len Ytail |
| 557 | ybyte ulong get Ytail i |
| 558 | end |
| 559 | if ne get Yl 0 total |
| 560 | ydie 65 |
| 561 | end |
| 562 | sent = ulong 0 |
| 563 | while lt sent total |
| 564 | r = write 1 Yo sent sub total sent |
| 565 | if le r 0 |
| 566 | ydie 77 |
| 567 | end |
| 568 | sent = add sent ulong r |
| 569 | end |
| 570 | ret 0 |
| 571 | end |