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 / zig-u64-export.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | note Handwritten extended-S1-family upper-to-Zig 0.17 exporter (spec/universal/zig-u64-v1.md section 4). |
| 3 | note Prepended unchanged at build: src/source-aware/vendor/sha256.om and src/source-aware/core-v2.om (decode n). |
| 4 | note Input: one OMSB envelope to EOF. Output is generated from the OMSA operation records only; the |
| 5 | note OMSP sidecar is validated by the v2 decoder, never read for output. |
| 6 | global Zone uchar 1 |
| 7 | global Zo uchar 4096 |
| 8 | global Zl ulong 1 |
| 9 | global Zni ulong 1 |
| 10 | global Zob ulong 1 |
| 11 | global Zpr ulong 64 |
| 12 | global Zln ulong 64 |
| 13 | global Zdp ulong 64 |
| 14 | global Zcn ulong 64 |
| 15 | global Zrc ulong 64 |
| 16 | global Zus ulong 6 |
| 17 | text Zhead "pub fn calc(" |
| 18 | text Zpar ": u64" |
| 19 | text Zret ") u64 {\n return " |
| 20 | text Ztail ";\n}\n" |
| 21 | text Zas "@as(u64, " |
| 22 | text Zop " +% -% *% " |
| 23 | text ZMal "ZIG_EXPORT_MALFORMED\n" |
| 24 | text ZLim "ZIG_EXPORT_LIMIT\n" |
| 25 | text ZProf "ZIG_EXPORT_PROFILE_MISMATCH\n" |
| 26 | text ZIO "ZIG_EXPORT_IO\n" |
| 27 | |
| 28 | fn zdie code ulong to void |
| 29 | if eq code 65 |
| 30 | w = write 2 ZMal 0 len ZMal |
| 31 | end |
| 32 | if eq code 67 |
| 33 | w = write 2 ZLim 0 len ZLim |
| 34 | end |
| 35 | if eq code 68 |
| 36 | w = write 2 ZProf 0 len ZProf |
| 37 | end |
| 38 | if eq code 77 |
| 39 | w = write 2 ZIO 0 len ZIO |
| 40 | end |
| 41 | exit int code |
| 42 | end |
| 43 | |
| 44 | fn zbyte c ulong to void |
| 45 | put Zo get Zl 0 uchar c |
| 46 | put Zl 0 add get Zl 0 1 |
| 47 | end |
| 48 | |
| 49 | fn ztext off ulong n ulong to void |
| 50 | for i ulong n |
| 51 | zbyte ulong get Zop add off i |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | fn zdigits 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 zdec 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 | zbyte ulong get d i |
| 80 | end |
| 81 | end |
| 82 | |
| 83 | note Family membership per translation-compose-v2 section 1, on fields already validated by decode. |
| 84 | fn zkind i ulong to ulong |
| 85 | c = get Oc i |
| 86 | if or ne get Ot i 1 ne get Oef i 0 |
| 87 | ret 68 |
| 88 | end |
| 89 | if ne get Oal i 0 |
| 90 | ret 68 |
| 91 | end |
| 92 | if eq c 9 |
| 93 | if or ne get Oat i 0 ne get Oob i 1 |
| 94 | ret 68 |
| 95 | end |
| 96 | put Zni 0 add get Zni 0 1 |
| 97 | ret 0 |
| 98 | end |
| 99 | if eq c 1 |
| 100 | if or ne get Oat i 1 ne get Oob i 0 |
| 101 | ret 68 |
| 102 | end |
| 103 | ret 0 |
| 104 | end |
| 105 | if or lt c 2 gt c 4 |
| 106 | ret 68 |
| 107 | end |
| 108 | if or ne get Oat i 1 ne get Oob i get Zob 0 |
| 109 | ret 68 |
| 110 | end |
| 111 | ret 0 |
| 112 | end |
| 113 | |
| 114 | note (language, profile) -> arithmetic obligations: C 1/1, Rust 2/2, Python 3/4 (5), C++ 5/7, Zig 6/8, Go 7/9. |
| 115 | fn zmember to ulong |
| 116 | l = get M 0 |
| 117 | p = get M 1 |
| 118 | if and eq l 1 eq p 1 |
| 119 | ret 1 |
| 120 | end |
| 121 | if and eq l 2 eq p 2 |
| 122 | ret 1 |
| 123 | end |
| 124 | if and eq l 3 eq p 4 |
| 125 | ret 5 |
| 126 | end |
| 127 | if and eq l 5 eq p 7 |
| 128 | ret 1 |
| 129 | end |
| 130 | if and eq l 6 eq p 8 |
| 131 | ret 1 |
| 132 | end |
| 133 | if and eq l 7 eq p 9 |
| 134 | ret 1 |
| 135 | end |
| 136 | ret 0 |
| 137 | end |
| 138 | |
| 139 | fn zs1 to ulong |
| 140 | ob = zmember |
| 141 | if eq ob 0 |
| 142 | ret 68 |
| 143 | end |
| 144 | put Zob 0 ob |
| 145 | if or ne get M 2 0 ne get M 3 1 |
| 146 | ret 68 |
| 147 | end |
| 148 | put Zni 0 0 |
| 149 | for i ulong get M 4 |
| 150 | if ne zkind i 0 |
| 151 | ret 68 |
| 152 | end |
| 153 | end |
| 154 | for j ulong get M 4 |
| 155 | if eq get Oc j 9 |
| 156 | if ge get Oi j get Zni 0 |
| 157 | ret 68 |
| 158 | end |
| 159 | end |
| 160 | end |
| 161 | ret 0 |
| 162 | end |
| 163 | |
| 164 | fn zsat v ulong to ulong |
| 165 | if gt v 1048576 |
| 166 | ret 1048576 |
| 167 | end |
| 168 | ret v |
| 169 | end |
| 170 | |
| 171 | note Left operand parenthesised iff its precedence is lower than the parent's, right iff lower or equal. |
| 172 | fn zpa k ulong to ulong |
| 173 | if lt get Zpr get Oa k get Zpr k |
| 174 | ret 1 |
| 175 | end |
| 176 | ret 0 |
| 177 | end |
| 178 | |
| 179 | fn zpb k ulong to ulong |
| 180 | if le get Zpr get Ob k get Zpr k |
| 181 | ret 1 |
| 182 | end |
| 183 | ret 0 |
| 184 | end |
| 185 | |
| 186 | note Two constant operands: the left one is written @as(u64, N) so the operation is u64, not comptime_int. |
| 187 | fn zwrap k ulong to ulong |
| 188 | if and eq get Oc get Oa k 1 eq get Oc get Ob k 1 |
| 189 | ret 1 |
| 190 | end |
| 191 | ret 0 |
| 192 | end |
| 193 | |
| 194 | note Length, parenthesis depth and re-import operation count of each result's expression. |
| 195 | fn zmetric k ulong to void |
| 196 | c = get Oc k |
| 197 | put Zdp k 0 |
| 198 | if eq c 9 |
| 199 | put Zpr k 3 |
| 200 | put Zln k 2 |
| 201 | put Zcn k 0 |
| 202 | ret |
| 203 | end |
| 204 | if eq c 1 |
| 205 | put Zpr k 3 |
| 206 | put Zln k zdigits get Oi k |
| 207 | put Zcn k 1 |
| 208 | ret |
| 209 | end |
| 210 | put Zpr k 1 |
| 211 | if eq c 4 |
| 212 | put Zpr k 2 |
| 213 | end |
| 214 | a = get Oa k |
| 215 | b = get Ob k |
| 216 | pa = zpa k |
| 217 | pb = zpb k |
| 218 | wr = zwrap k |
| 219 | l = add get Zln a get Zln b |
| 220 | l = add l add 4 mul 2 add pa pb |
| 221 | l = add l mul 10 wr |
| 222 | put Zln k zsat l |
| 223 | da = add add get Zdp a pa wr |
| 224 | db = add get Zdp b pb |
| 225 | if gt da db |
| 226 | put Zdp k da |
| 227 | else |
| 228 | put Zdp k db |
| 229 | end |
| 230 | put Zcn k zsat add add get Zcn a get Zcn b 1 |
| 231 | end |
| 232 | |
| 233 | fn zemit k ulong to void |
| 234 | c = get Oc k |
| 235 | if eq c 9 |
| 236 | zbyte 120 |
| 237 | zbyte add 48 get Oi k |
| 238 | ret |
| 239 | end |
| 240 | if eq c 1 |
| 241 | zdec get Oi k |
| 242 | ret |
| 243 | end |
| 244 | pa = zpa k |
| 245 | pb = zpb k |
| 246 | wr = zwrap k |
| 247 | if eq pa 1 |
| 248 | zbyte 40 |
| 249 | end |
| 250 | if eq wr 1 |
| 251 | for i ulong len Zas |
| 252 | zbyte ulong get Zas i |
| 253 | end |
| 254 | end |
| 255 | zemit get Oa k |
| 256 | if eq wr 1 |
| 257 | zbyte 41 |
| 258 | end |
| 259 | if eq pa 1 |
| 260 | zbyte 41 |
| 261 | end |
| 262 | ztext mul sub c 2 4 4 |
| 263 | if eq pb 1 |
| 264 | zbyte 40 |
| 265 | end |
| 266 | zemit get Ob k |
| 267 | if eq pb 1 |
| 268 | zbyte 41 |
| 269 | end |
| 270 | end |
| 271 | |
| 272 | note translation-v1 section 4: the pre-normalization root of a NORMALIZED unit, else the last result. |
| 273 | fn zroot to ulong |
| 274 | n = get M 4 |
| 275 | if lt n 3 |
| 276 | ret sub n 1 |
| 277 | end |
| 278 | z = sub n 2 |
| 279 | y = sub n 1 |
| 280 | if or ne get Oc z 1 ne get Oi z 0 |
| 281 | ret y |
| 282 | end |
| 283 | if or ne get Oc y 2 ne get Ob y z |
| 284 | ret y |
| 285 | end |
| 286 | if ge get Oa y z |
| 287 | ret y |
| 288 | end |
| 289 | ret get Oa y |
| 290 | end |
| 291 | |
| 292 | note Input slots reached from the root are named x<slot>; every other slot is the discard _ (Zig rejects |
| 293 | note an unused named parameter). |
| 294 | fn zreach root ulong to void |
| 295 | for i ulong 64 |
| 296 | put Zrc i 0 |
| 297 | end |
| 298 | for s ulong 6 |
| 299 | put Zus s 0 |
| 300 | end |
| 301 | put Zrc root 1 |
| 302 | k = add root 1 |
| 303 | while gt k 0 |
| 304 | k = sub k 1 |
| 305 | if eq get Zrc k 1 |
| 306 | c = get Oc k |
| 307 | if and ge c 2 le c 4 |
| 308 | put Zrc get Oa k 1 |
| 309 | put Zrc get Ob k 1 |
| 310 | end |
| 311 | if eq c 9 |
| 312 | put Zus get Oi k 1 |
| 313 | end |
| 314 | end |
| 315 | end |
| 316 | end |
| 317 | |
| 318 | fn zparams to ulong |
| 319 | ni = get Zni 0 |
| 320 | if eq ni 0 |
| 321 | ret 0 |
| 322 | end |
| 323 | t = sub mul ni 8 2 |
| 324 | for s ulong ni |
| 325 | t = add t get Zus s |
| 326 | end |
| 327 | ret t |
| 328 | end |
| 329 | |
| 330 | note ", " separators; a used slot is "x<s>: u64" (7 bytes), an unused one "_: u64" (6 bytes). |
| 331 | fn zsignature to void |
| 332 | for i ulong len Zhead |
| 333 | zbyte ulong get Zhead i |
| 334 | end |
| 335 | for s ulong get Zni 0 |
| 336 | if gt s 0 |
| 337 | zbyte 44 |
| 338 | zbyte 32 |
| 339 | end |
| 340 | if eq get Zus s 1 |
| 341 | zbyte 120 |
| 342 | zbyte add 48 s |
| 343 | else |
| 344 | zbyte 95 |
| 345 | end |
| 346 | for t ulong len Zpar |
| 347 | zbyte ulong get Zpar t |
| 348 | end |
| 349 | end |
| 350 | for u ulong len Zret |
| 351 | zbyte ulong get Zret u |
| 352 | end |
| 353 | end |
| 354 | |
| 355 | fn zreadunit to ulong |
| 356 | n = ulong 0 |
| 357 | more = ulong 1 |
| 358 | while and eq more 1 lt n 8192 |
| 359 | r = read 0 Ib n sub ulong 8192 n |
| 360 | if lt r 0 |
| 361 | zdie 77 |
| 362 | end |
| 363 | if eq r 0 |
| 364 | more = 0 |
| 365 | else |
| 366 | n = add n ulong r |
| 367 | end |
| 368 | end |
| 369 | if eq n 8192 |
| 370 | r = read 0 Zone 0 1 |
| 371 | if lt r 0 |
| 372 | zdie 77 |
| 373 | end |
| 374 | if gt r 0 |
| 375 | zdie 65 |
| 376 | end |
| 377 | end |
| 378 | ret n |
| 379 | end |
| 380 | |
| 381 | fn main to int |
| 382 | constants |
| 383 | n = zreadunit |
| 384 | code = decode n |
| 385 | if eq code 68 |
| 386 | zdie 68 |
| 387 | end |
| 388 | if ne code 0 |
| 389 | zdie 65 |
| 390 | end |
| 391 | if ne zs1 0 |
| 392 | zdie 68 |
| 393 | end |
| 394 | for k ulong get M 4 |
| 395 | zmetric k |
| 396 | end |
| 397 | root = zroot |
| 398 | zreach root |
| 399 | total = add add len Zhead len Zret len Ztail |
| 400 | total = add total zparams |
| 401 | total = add total get Zln root |
| 402 | if gt total 4096 |
| 403 | zdie 67 |
| 404 | end |
| 405 | if gt get Zdp root 16 |
| 406 | zdie 67 |
| 407 | end |
| 408 | if gt add add get Zni 0 get Zcn root 2 64 |
| 409 | zdie 67 |
| 410 | end |
| 411 | put Zl 0 0 |
| 412 | zsignature |
| 413 | zemit root |
| 414 | for i ulong len Ztail |
| 415 | zbyte ulong get Ztail i |
| 416 | end |
| 417 | if ne get Zl 0 total |
| 418 | zdie 65 |
| 419 | end |
| 420 | sent = ulong 0 |
| 421 | while lt sent total |
| 422 | r = write 1 Zo sent sub total sent |
| 423 | if le r 0 |
| 424 | zdie 77 |
| 425 | end |
| 426 | sent = add sent ulong r |
| 427 | end |
| 428 | ret 0 |
| 429 | end |