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 / c-u64-export.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | note Handwritten S1-family upper-to-C17 exporter (translation-v1 section 6, translation-compose-v1 section 2). |
| 3 | note Accepts the C, Rust and Python u64 members; output depends on the records only, never on the member. |
| 4 | note Prepended unchanged at build: src/source-aware/vendor/sha256.om and src/source-aware/core.om (decode n). |
| 5 | note Input frame: one byte name length L (0 = calc), L name bytes, then one OMSB envelope to EOF. |
| 6 | note Output is generated from the OMSA operation records only; the OMSP sidecar is validated, never read for output. |
| 7 | global Xn uchar 1 |
| 8 | global Xnm uchar 32 |
| 9 | global Xone uchar 1 |
| 10 | global Xob ulong 1 |
| 11 | global Xo uchar 4096 |
| 12 | global Xl ulong 1 |
| 13 | global Xni ulong 1 |
| 14 | global Xpr ulong 64 |
| 15 | global Xln ulong 64 |
| 16 | global Xdp ulong 64 |
| 17 | global Xcn ulong 64 |
| 18 | text Xcalc "calc" |
| 19 | text Xomg "omega_" |
| 20 | text Xhead "#include <stdint.h>\nuint64_t " |
| 21 | text Xpar "uint64_t i" |
| 22 | text Xvoid "void" |
| 23 | text Xret ") { return " |
| 24 | text Xtail "; }\n" |
| 25 | text XBad "C_EXPORT_BAD_COMMAND\n" |
| 26 | text XMal "C_EXPORT_MALFORMED\n" |
| 27 | text XLim "C_EXPORT_LIMIT\n" |
| 28 | text XProf "C_EXPORT_PROFILE_MISMATCH\n" |
| 29 | text XIO "C_EXPORT_IO\n" |
| 30 | |
| 31 | fn xdie code ulong to void |
| 32 | if eq code 64 |
| 33 | w = write 2 XBad 0 len XBad |
| 34 | end |
| 35 | if eq code 65 |
| 36 | w = write 2 XMal 0 len XMal |
| 37 | end |
| 38 | if eq code 67 |
| 39 | w = write 2 XLim 0 len XLim |
| 40 | end |
| 41 | if eq code 68 |
| 42 | w = write 2 XProf 0 len XProf |
| 43 | end |
| 44 | if eq code 77 |
| 45 | w = write 2 XIO 0 len XIO |
| 46 | end |
| 47 | exit int code |
| 48 | end |
| 49 | |
| 50 | fn xbyte c ulong to void |
| 51 | put Xo get Xl 0 uchar c |
| 52 | put Xl 0 add get Xl 0 1 |
| 53 | end |
| 54 | |
| 55 | fn xdigits v ulong to ulong |
| 56 | d = ulong 1 |
| 57 | x = div v 10 |
| 58 | while gt x 0 |
| 59 | d = add d 1 |
| 60 | x = div x 10 |
| 61 | end |
| 62 | ret d |
| 63 | end |
| 64 | |
| 65 | fn xdec v ulong to void |
| 66 | d = new uchar 20 0 |
| 67 | i = ulong 0 |
| 68 | x = v |
| 69 | put d 0 uchar add rem x 10 48 |
| 70 | x = div x 10 |
| 71 | i = add i 1 |
| 72 | while gt x 0 |
| 73 | put d i uchar add rem x 10 48 |
| 74 | x = div x 10 |
| 75 | i = add i 1 |
| 76 | end |
| 77 | while gt i 0 |
| 78 | i = sub i 1 |
| 79 | xbyte ulong get d i |
| 80 | end |
| 81 | end |
| 82 | |
| 83 | fn xidc c ulong to bool |
| 84 | if and ge c 48 le c 57 |
| 85 | ret true |
| 86 | end |
| 87 | if and ge c 65 le c 90 |
| 88 | ret true |
| 89 | end |
| 90 | if and ge c 97 le c 122 |
| 91 | ret true |
| 92 | end |
| 93 | ret eq c 95 |
| 94 | end |
| 95 | |
| 96 | fn xnameok to bool |
| 97 | n = ulong get Xn 0 |
| 98 | if eq n 0 |
| 99 | ret true |
| 100 | end |
| 101 | if eq n 4 |
| 102 | for i ulong 4 |
| 103 | if ne get Xnm i get Xcalc i |
| 104 | ret false |
| 105 | end |
| 106 | end |
| 107 | ret true |
| 108 | end |
| 109 | if lt n 7 |
| 110 | ret false |
| 111 | end |
| 112 | for i ulong 6 |
| 113 | if ne get Xnm i get Xomg i |
| 114 | ret false |
| 115 | end |
| 116 | end |
| 117 | for j ulong n |
| 118 | if not xidc ulong get Xnm j |
| 119 | ret false |
| 120 | end |
| 121 | end |
| 122 | ret true |
| 123 | end |
| 124 | |
| 125 | fn xkind i ulong to ulong |
| 126 | c = get Oc i |
| 127 | if or ne get Ot i 1 ne get Oef i 0 |
| 128 | ret 68 |
| 129 | end |
| 130 | if ne get Oal i 0 |
| 131 | ret 68 |
| 132 | end |
| 133 | if eq c 9 |
| 134 | if or ne get Oat i 0 ne get Oob i 1 |
| 135 | ret 68 |
| 136 | end |
| 137 | put Xni 0 add get Xni 0 1 |
| 138 | ret 0 |
| 139 | end |
| 140 | if eq c 1 |
| 141 | if or ne get Oat i 1 ne get Oob i 0 |
| 142 | ret 68 |
| 143 | end |
| 144 | ret 0 |
| 145 | end |
| 146 | if or lt c 2 gt c 4 |
| 147 | ret 68 |
| 148 | end |
| 149 | if or ne get Oat i 1 ne get Oob i get Xob 0 |
| 150 | ret 68 |
| 151 | end |
| 152 | ret 0 |
| 153 | end |
| 154 | |
| 155 | fn xs1 to ulong |
| 156 | l = get M 0 |
| 157 | p = get M 1 |
| 158 | put Xob 0 0 |
| 159 | if and eq l 1 eq p 1 |
| 160 | put Xob 0 1 |
| 161 | end |
| 162 | if and eq l 2 eq p 2 |
| 163 | put Xob 0 1 |
| 164 | end |
| 165 | if and eq l 3 eq p 4 |
| 166 | put Xob 0 5 |
| 167 | end |
| 168 | if eq get Xob 0 0 |
| 169 | ret 68 |
| 170 | end |
| 171 | if or ne get M 2 0 ne get M 3 1 |
| 172 | ret 68 |
| 173 | end |
| 174 | put Xni 0 0 |
| 175 | for i ulong get M 4 |
| 176 | if ne xkind i 0 |
| 177 | ret 68 |
| 178 | end |
| 179 | end |
| 180 | for j ulong get M 4 |
| 181 | if eq get Oc j 9 |
| 182 | if ge get Oi j get Xni 0 |
| 183 | ret 68 |
| 184 | end |
| 185 | end |
| 186 | end |
| 187 | ret 0 |
| 188 | end |
| 189 | |
| 190 | fn xpa k ulong to ulong |
| 191 | if lt get Xpr get Oa k get Xpr k |
| 192 | ret 1 |
| 193 | end |
| 194 | ret 0 |
| 195 | end |
| 196 | |
| 197 | fn xpb k ulong to ulong |
| 198 | if le get Xpr get Ob k get Xpr k |
| 199 | ret 1 |
| 200 | end |
| 201 | ret 0 |
| 202 | end |
| 203 | |
| 204 | fn xsat v ulong to ulong |
| 205 | if gt v 1048576 |
| 206 | ret 1048576 |
| 207 | end |
| 208 | ret v |
| 209 | end |
| 210 | |
| 211 | fn xmetric k ulong to void |
| 212 | c = get Oc k |
| 213 | put Xdp k 0 |
| 214 | if eq c 9 |
| 215 | put Xpr k 3 |
| 216 | put Xln k 2 |
| 217 | put Xcn k 0 |
| 218 | ret |
| 219 | end |
| 220 | if eq c 1 |
| 221 | put Xpr k 3 |
| 222 | put Xln k add xdigits get Oi k 3 |
| 223 | put Xcn k 1 |
| 224 | ret |
| 225 | end |
| 226 | put Xpr k 1 |
| 227 | if eq c 4 |
| 228 | put Xpr k 2 |
| 229 | end |
| 230 | a = get Oa k |
| 231 | b = get Ob k |
| 232 | pa = xpa k |
| 233 | pb = xpb k |
| 234 | l = add get Xln a get Xln b |
| 235 | l = add l add 3 mul 2 add pa pb |
| 236 | put Xln k xsat l |
| 237 | da = add get Xdp a pa |
| 238 | db = add get Xdp b pb |
| 239 | if gt da db |
| 240 | put Xdp k da |
| 241 | else |
| 242 | put Xdp k db |
| 243 | end |
| 244 | put Xcn k xsat add add get Xcn a get Xcn b 1 |
| 245 | end |
| 246 | |
| 247 | fn xemit k ulong to void |
| 248 | c = get Oc k |
| 249 | if eq c 9 |
| 250 | xbyte 105 |
| 251 | xbyte add 48 get Oi k |
| 252 | ret |
| 253 | end |
| 254 | if eq c 1 |
| 255 | xdec get Oi k |
| 256 | xbyte 85 |
| 257 | xbyte 76 |
| 258 | xbyte 76 |
| 259 | ret |
| 260 | end |
| 261 | pa = xpa k |
| 262 | pb = xpb k |
| 263 | if eq pa 1 |
| 264 | xbyte 40 |
| 265 | end |
| 266 | xemit get Oa k |
| 267 | if eq pa 1 |
| 268 | xbyte 41 |
| 269 | end |
| 270 | xbyte 32 |
| 271 | if eq c 2 |
| 272 | xbyte 43 |
| 273 | end |
| 274 | if eq c 3 |
| 275 | xbyte 45 |
| 276 | end |
| 277 | if eq c 4 |
| 278 | xbyte 42 |
| 279 | end |
| 280 | xbyte 32 |
| 281 | if eq pb 1 |
| 282 | xbyte 40 |
| 283 | end |
| 284 | xemit get Ob k |
| 285 | if eq pb 1 |
| 286 | xbyte 41 |
| 287 | end |
| 288 | end |
| 289 | |
| 290 | fn xroot to ulong |
| 291 | n = get M 4 |
| 292 | if lt n 3 |
| 293 | ret sub n 1 |
| 294 | end |
| 295 | z = sub n 2 |
| 296 | y = sub n 1 |
| 297 | if or ne get Oc z 1 ne get Oi z 0 |
| 298 | ret y |
| 299 | end |
| 300 | if or ne get Oc y 2 ne get Ob y z |
| 301 | ret y |
| 302 | end |
| 303 | if ge get Oa y z |
| 304 | ret y |
| 305 | end |
| 306 | ret get Oa y |
| 307 | end |
| 308 | |
| 309 | fn xparams to ulong |
| 310 | ni = get Xni 0 |
| 311 | if eq ni 0 |
| 312 | ret len Xvoid |
| 313 | end |
| 314 | ret sub add mul ni len Xpar mul ni 3 2 |
| 315 | end |
| 316 | |
| 317 | fn xsignature to void |
| 318 | for i ulong len Xhead |
| 319 | xbyte ulong get Xhead i |
| 320 | end |
| 321 | if eq ulong get Xn 0 0 |
| 322 | for j ulong len Xcalc |
| 323 | xbyte ulong get Xcalc j |
| 324 | end |
| 325 | else |
| 326 | for j ulong ulong get Xn 0 |
| 327 | xbyte ulong get Xnm j |
| 328 | end |
| 329 | end |
| 330 | xbyte 40 |
| 331 | if eq get Xni 0 0 |
| 332 | for k ulong len Xvoid |
| 333 | xbyte ulong get Xvoid k |
| 334 | end |
| 335 | end |
| 336 | for s ulong get Xni 0 |
| 337 | if gt s 0 |
| 338 | xbyte 44 |
| 339 | xbyte 32 |
| 340 | end |
| 341 | for t ulong len Xpar |
| 342 | xbyte ulong get Xpar t |
| 343 | end |
| 344 | xbyte add 48 s |
| 345 | end |
| 346 | for u ulong len Xret |
| 347 | xbyte ulong get Xret u |
| 348 | end |
| 349 | end |
| 350 | |
| 351 | fn xreadname to void |
| 352 | r = read 0 Xn 0 1 |
| 353 | if lt r 0 |
| 354 | xdie 77 |
| 355 | end |
| 356 | if eq r 0 |
| 357 | xdie 64 |
| 358 | end |
| 359 | n = ulong get Xn 0 |
| 360 | if gt n 32 |
| 361 | xdie 64 |
| 362 | end |
| 363 | got = ulong 0 |
| 364 | while lt got n |
| 365 | r = read 0 Xnm got sub n got |
| 366 | if lt r 0 |
| 367 | xdie 77 |
| 368 | end |
| 369 | if eq r 0 |
| 370 | xdie 64 |
| 371 | end |
| 372 | got = add got ulong r |
| 373 | end |
| 374 | if not xnameok |
| 375 | xdie 64 |
| 376 | end |
| 377 | end |
| 378 | |
| 379 | fn xreadunit to ulong |
| 380 | n = ulong 0 |
| 381 | more = ulong 1 |
| 382 | while and eq more 1 lt n 8192 |
| 383 | r = read 0 Ib n sub ulong 8192 n |
| 384 | if lt r 0 |
| 385 | xdie 77 |
| 386 | end |
| 387 | if eq r 0 |
| 388 | more = 0 |
| 389 | else |
| 390 | n = add n ulong r |
| 391 | end |
| 392 | end |
| 393 | if eq n 8192 |
| 394 | r = read 0 Xone 0 1 |
| 395 | if lt r 0 |
| 396 | xdie 77 |
| 397 | end |
| 398 | if gt r 0 |
| 399 | xdie 65 |
| 400 | end |
| 401 | end |
| 402 | ret n |
| 403 | end |
| 404 | |
| 405 | fn main to int |
| 406 | constants |
| 407 | xreadname |
| 408 | n = xreadunit |
| 409 | code = decode n |
| 410 | if eq code 68 |
| 411 | xdie 68 |
| 412 | end |
| 413 | if ne code 0 |
| 414 | xdie 65 |
| 415 | end |
| 416 | if ne xs1 0 |
| 417 | xdie 68 |
| 418 | end |
| 419 | for k ulong get M 4 |
| 420 | xmetric k |
| 421 | end |
| 422 | root = xroot |
| 423 | total = add add len Xhead len Xret len Xtail |
| 424 | total = add total add xparams 1 |
| 425 | if eq ulong get Xn 0 0 |
| 426 | total = add total len Xcalc |
| 427 | else |
| 428 | total = add total ulong get Xn 0 |
| 429 | end |
| 430 | total = add total get Xln root |
| 431 | if gt total 4096 |
| 432 | xdie 67 |
| 433 | end |
| 434 | if gt get Xdp root 16 |
| 435 | xdie 67 |
| 436 | end |
| 437 | if gt add add get Xni 0 get Xcn root 2 64 |
| 438 | xdie 67 |
| 439 | end |
| 440 | put Xl 0 0 |
| 441 | xsignature |
| 442 | xemit root |
| 443 | for i ulong len Xtail |
| 444 | xbyte ulong get Xtail i |
| 445 | end |
| 446 | if ne get Xl 0 total |
| 447 | xdie 65 |
| 448 | end |
| 449 | sent = ulong 0 |
| 450 | while lt sent total |
| 451 | r = write 1 Xo sent sub total sent |
| 452 | if le r 0 |
| 453 | xdie 77 |
| 454 | end |
| 455 | sent = add sent ulong r |
| 456 | end |
| 457 | ret 0 |
| 458 | end |