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 / go-u64-export.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | note Handwritten S1-family upper-to-Go 1.27 exporter (spec/universal/go-u64-v1.md section 5). |
| 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 (OMSA version 1 or 2). Output is generated from the OMSA operation |
| 5 | note records only; the OMSP sidecar is validated by the v2 decoder, never read for output. |
| 6 | global Gone uchar 1 |
| 7 | global Gout uchar 4096 |
| 8 | global Gl ulong 1 |
| 9 | global Gni ulong 1 |
| 10 | global Gob ulong 1 |
| 11 | global Gpr ulong 64 |
| 12 | global Gdp ulong 64 |
| 13 | global Gcn ulong 64 |
| 14 | global Gwb ulong 64 |
| 15 | global Grc ulong 64 |
| 16 | global Gkc ulong 64 |
| 17 | global Gkv ulong 64 |
| 18 | global Gov ulong 64 |
| 19 | text Ghead "package main\n\nfunc calc(" |
| 20 | text Gpar " uint64" |
| 21 | text Gret ") uint64 {\n\treturn " |
| 22 | text Gtail "\n}\n" |
| 23 | text GMal "GO_EXPORT_MALFORMED\n" |
| 24 | text GLim "GO_EXPORT_LIMIT\n" |
| 25 | text GProf "GO_EXPORT_PROFILE_MISMATCH\n" |
| 26 | text GIO "GO_EXPORT_IO\n" |
| 27 | |
| 28 | fn gdie code ulong to void |
| 29 | if eq code 65 |
| 30 | w = write 2 GMal 0 len GMal |
| 31 | end |
| 32 | if eq code 67 |
| 33 | w = write 2 GLim 0 len GLim |
| 34 | end |
| 35 | if eq code 68 |
| 36 | w = write 2 GProf 0 len GProf |
| 37 | end |
| 38 | if eq code 77 |
| 39 | w = write 2 GIO 0 len GIO |
| 40 | end |
| 41 | exit int code |
| 42 | end |
| 43 | |
| 44 | note Every emitted byte is bounded: an export longer than 4096 bytes would not re-import (67). |
| 45 | fn gbyte c ulong to void |
| 46 | if ge get Gl 0 4096 |
| 47 | gdie 67 |
| 48 | end |
| 49 | put Gout get Gl 0 uchar c |
| 50 | put Gl 0 add get Gl 0 1 |
| 51 | end |
| 52 | |
| 53 | fn gdec v ulong to void |
| 54 | d = new uchar 20 0 |
| 55 | i = ulong 0 |
| 56 | x = v |
| 57 | put d 0 uchar add rem x 10 48 |
| 58 | x = div x 10 |
| 59 | i = add i 1 |
| 60 | while gt x 0 |
| 61 | put d i uchar add rem x 10 48 |
| 62 | x = div x 10 |
| 63 | i = add i 1 |
| 64 | end |
| 65 | while gt i 0 |
| 66 | i = sub i 1 |
| 67 | gbyte ulong get d i |
| 68 | end |
| 69 | end |
| 70 | |
| 71 | note Family membership per translation-compose-v2 section 1, on fields already validated by decode. |
| 72 | fn gkind i ulong to ulong |
| 73 | c = get Oc i |
| 74 | if or ne get Ot i 1 ne get Oef i 0 |
| 75 | ret 68 |
| 76 | end |
| 77 | if ne get Oal i 0 |
| 78 | ret 68 |
| 79 | end |
| 80 | if eq c 9 |
| 81 | if or ne get Oat i 0 ne get Oob i 1 |
| 82 | ret 68 |
| 83 | end |
| 84 | put Gni 0 add get Gni 0 1 |
| 85 | ret 0 |
| 86 | end |
| 87 | if eq c 1 |
| 88 | if or ne get Oat i 1 ne get Oob i 0 |
| 89 | ret 68 |
| 90 | end |
| 91 | ret 0 |
| 92 | end |
| 93 | if or lt c 2 gt c 4 |
| 94 | ret 68 |
| 95 | end |
| 96 | if or ne get Oat i 1 ne get Oob i get Gob 0 |
| 97 | ret 68 |
| 98 | end |
| 99 | ret 0 |
| 100 | end |
| 101 | |
| 102 | note (OMSA version, language, profile) -> arithmetic obligations; 0 = not a family member. |
| 103 | fn gmember to ulong |
| 104 | v = get M 8 |
| 105 | l = get M 0 |
| 106 | p = get M 1 |
| 107 | if eq v 1 |
| 108 | if and eq l 1 eq p 1 |
| 109 | ret 1 |
| 110 | end |
| 111 | if and eq l 2 eq p 2 |
| 112 | ret 1 |
| 113 | end |
| 114 | if and eq l 3 eq p 4 |
| 115 | ret 5 |
| 116 | end |
| 117 | end |
| 118 | if eq v 2 |
| 119 | if and eq l 5 eq p 7 |
| 120 | ret 1 |
| 121 | end |
| 122 | if and eq l 6 eq p 8 |
| 123 | ret 1 |
| 124 | end |
| 125 | if and eq l 7 eq p 9 |
| 126 | ret 1 |
| 127 | end |
| 128 | end |
| 129 | ret 0 |
| 130 | end |
| 131 | |
| 132 | fn gs1 to ulong |
| 133 | ob = gmember |
| 134 | if eq ob 0 |
| 135 | ret 68 |
| 136 | end |
| 137 | put Gob 0 ob |
| 138 | if or ne get M 2 0 ne get M 3 1 |
| 139 | ret 68 |
| 140 | end |
| 141 | put Gni 0 0 |
| 142 | for i ulong get M 4 |
| 143 | if ne gkind i 0 |
| 144 | ret 68 |
| 145 | end |
| 146 | end |
| 147 | for j ulong get M 4 |
| 148 | if eq get Oc j 9 |
| 149 | if ge get Oi j get Gni 0 |
| 150 | ret 68 |
| 151 | end |
| 152 | end |
| 153 | end |
| 154 | ret 0 |
| 155 | end |
| 156 | |
| 157 | fn gsat v ulong to ulong |
| 158 | if gt v 1048576 |
| 159 | ret 1048576 |
| 160 | end |
| 161 | ret v |
| 162 | end |
| 163 | |
| 164 | note Go precedence: operand 6, * 5, + and - 4 (left associative). A left operand is parenthesised iff |
| 165 | note its precedence is lower than the parent's, a right operand iff lower or equal. |
| 166 | fn gpa k ulong to ulong |
| 167 | if lt get Gpr get Oa k get Gpr k |
| 168 | ret 1 |
| 169 | end |
| 170 | ret 0 |
| 171 | end |
| 172 | |
| 173 | fn gpb k ulong to ulong |
| 174 | if le get Gpr get Ob k get Gpr k |
| 175 | ret 1 |
| 176 | end |
| 177 | ret 0 |
| 178 | end |
| 179 | |
| 180 | note Exact untyped-constant evaluation of a constant-only operation: overflow outside 0..2^64-1 sets Gov. |
| 181 | fn gfold k ulong to void |
| 182 | c = get Oc k |
| 183 | x = get Gkv get Oa k |
| 184 | y = get Gkv get Ob k |
| 185 | v = ulong 0 |
| 186 | if eq c 2 |
| 187 | v = add x y |
| 188 | if lt v x |
| 189 | put Gov k 1 |
| 190 | end |
| 191 | end |
| 192 | if eq c 3 |
| 193 | v = sub x y |
| 194 | if lt x y |
| 195 | put Gov k 1 |
| 196 | end |
| 197 | end |
| 198 | if eq c 4 |
| 199 | v = mul x y |
| 200 | if ne x 0 |
| 201 | if ne div v x y |
| 202 | put Gov k 1 |
| 203 | end |
| 204 | end |
| 205 | end |
| 206 | put Gkv k v |
| 207 | end |
| 208 | |
| 209 | note Precedence, parenthesis depth, re-import operation count, gofmt operator classes (1 = has |
| 210 | note precedence 4, 2 = has precedence 5, over the unparenthesised binary region) and constant folding. |
| 211 | fn gmetric k ulong to void |
| 212 | c = get Oc k |
| 213 | put Gdp k 0 |
| 214 | put Gwb k 0 |
| 215 | put Gov k 0 |
| 216 | put Gkc k 0 |
| 217 | put Gkv k 0 |
| 218 | if eq c 9 |
| 219 | put Gpr k 6 |
| 220 | put Gcn k 0 |
| 221 | ret |
| 222 | end |
| 223 | if eq c 1 |
| 224 | put Gpr k 6 |
| 225 | put Gcn k 1 |
| 226 | put Gkc k 1 |
| 227 | put Gkv k get Oi k |
| 228 | ret |
| 229 | end |
| 230 | put Gpr k 4 |
| 231 | put Gwb k 1 |
| 232 | if eq c 4 |
| 233 | put Gpr k 5 |
| 234 | put Gwb k 2 |
| 235 | end |
| 236 | a = get Oa k |
| 237 | b = get Ob k |
| 238 | pa = gpa k |
| 239 | pb = gpb k |
| 240 | da = add get Gdp a pa |
| 241 | db = add get Gdp b pb |
| 242 | if gt da db |
| 243 | put Gdp k da |
| 244 | else |
| 245 | put Gdp k db |
| 246 | end |
| 247 | put Gcn k gsat add add get Gcn a get Gcn b 1 |
| 248 | if and eq pa 0 lt get Gpr a 6 |
| 249 | put Gwb k or get Gwb k get Gwb a |
| 250 | end |
| 251 | if and eq pb 0 lt get Gpr b 6 |
| 252 | put Gwb k or get Gwb k get Gwb b |
| 253 | end |
| 254 | if and eq get Gkc a 1 eq get Gkc b 1 |
| 255 | put Gkc k 1 |
| 256 | gfold k |
| 257 | end |
| 258 | end |
| 259 | |
| 260 | note go/printer cutoff for a binary expression at the given depth (no unary operands are ever emitted). |
| 261 | fn gcut k ulong depth ulong to ulong |
| 262 | if eq get Gwb k 3 |
| 263 | if eq depth 1 |
| 264 | ret 5 |
| 265 | end |
| 266 | ret 4 |
| 267 | end |
| 268 | if eq depth 1 |
| 269 | ret 6 |
| 270 | end |
| 271 | ret 4 |
| 272 | end |
| 273 | |
| 274 | note go/printer layout: blanks around an operator iff its precedence is below the cutoff; a left |
| 275 | note operand of equal precedence keeps the depth, any other operand gets depth + 1, and a |
| 276 | note parenthesised operand is printed at that depth minus one. |
| 277 | fn gemit k ulong depth ulong to void |
| 278 | c = get Oc k |
| 279 | if eq c 9 |
| 280 | gbyte 105 |
| 281 | gdec get Oi k |
| 282 | ret |
| 283 | end |
| 284 | if eq c 1 |
| 285 | gdec get Oi k |
| 286 | ret |
| 287 | end |
| 288 | sp = ulong 0 |
| 289 | if lt get Gpr k gcut k depth |
| 290 | sp = 1 |
| 291 | end |
| 292 | a = get Oa k |
| 293 | pa = gpa k |
| 294 | if eq pa 1 |
| 295 | gbyte 40 |
| 296 | gemit a depth |
| 297 | gbyte 41 |
| 298 | else |
| 299 | if eq get Gpr a get Gpr k |
| 300 | gemit a depth |
| 301 | else |
| 302 | gemit a add depth 1 |
| 303 | end |
| 304 | end |
| 305 | if eq sp 1 |
| 306 | gbyte 32 |
| 307 | end |
| 308 | if eq c 2 |
| 309 | gbyte 43 |
| 310 | end |
| 311 | if eq c 3 |
| 312 | gbyte 45 |
| 313 | end |
| 314 | if eq c 4 |
| 315 | gbyte 42 |
| 316 | end |
| 317 | if eq sp 1 |
| 318 | gbyte 32 |
| 319 | end |
| 320 | if eq gpb k 1 |
| 321 | gbyte 40 |
| 322 | gemit get Ob k depth |
| 323 | gbyte 41 |
| 324 | else |
| 325 | gemit get Ob k add depth 1 |
| 326 | end |
| 327 | end |
| 328 | |
| 329 | note translation-v1 section 4: the pre-normalization root of a NORMALIZED unit, else the last result. |
| 330 | fn groot to ulong |
| 331 | n = get M 4 |
| 332 | if lt n 3 |
| 333 | ret sub n 1 |
| 334 | end |
| 335 | z = sub n 2 |
| 336 | y = sub n 1 |
| 337 | if or ne get Oc z 1 ne get Oi z 0 |
| 338 | ret y |
| 339 | end |
| 340 | if or ne get Oc y 2 ne get Ob y z |
| 341 | ret y |
| 342 | end |
| 343 | if ge get Oa y z |
| 344 | ret y |
| 345 | end |
| 346 | ret get Oa y |
| 347 | end |
| 348 | |
| 349 | note A constant-only operation reachable from the root must evaluate exactly within 0..2^64-1, |
| 350 | note otherwise the generated Go is a constant overflow compile error (refuse 67, never unimportable Go). |
| 351 | fn gconstok root ulong to bool |
| 352 | for i ulong get M 4 |
| 353 | put Grc i 0 |
| 354 | end |
| 355 | put Grc root 1 |
| 356 | for j ulong add root 1 |
| 357 | k = sub root j |
| 358 | if eq get Grc k 1 |
| 359 | if eq get Gov k 1 |
| 360 | ret false |
| 361 | end |
| 362 | c = get Oc k |
| 363 | if and ge c 2 le c 4 |
| 364 | put Grc get Oa k 1 |
| 365 | put Grc get Ob k 1 |
| 366 | end |
| 367 | end |
| 368 | end |
| 369 | ret true |
| 370 | end |
| 371 | |
| 372 | fn gsignature to void |
| 373 | for i ulong len Ghead |
| 374 | gbyte ulong get Ghead i |
| 375 | end |
| 376 | for s ulong get Gni 0 |
| 377 | if gt s 0 |
| 378 | gbyte 44 |
| 379 | gbyte 32 |
| 380 | end |
| 381 | gbyte 105 |
| 382 | gdec s |
| 383 | for t ulong len Gpar |
| 384 | gbyte ulong get Gpar t |
| 385 | end |
| 386 | end |
| 387 | for u ulong len Gret |
| 388 | gbyte ulong get Gret u |
| 389 | end |
| 390 | end |
| 391 | |
| 392 | fn greadunit to ulong |
| 393 | n = ulong 0 |
| 394 | more = ulong 1 |
| 395 | while and eq more 1 lt n 8192 |
| 396 | r = read 0 Ib n sub ulong 8192 n |
| 397 | if lt r 0 |
| 398 | gdie 77 |
| 399 | end |
| 400 | if eq r 0 |
| 401 | more = 0 |
| 402 | else |
| 403 | n = add n ulong r |
| 404 | end |
| 405 | end |
| 406 | if eq n 8192 |
| 407 | r = read 0 Gone 0 1 |
| 408 | if lt r 0 |
| 409 | gdie 77 |
| 410 | end |
| 411 | if gt r 0 |
| 412 | gdie 65 |
| 413 | end |
| 414 | end |
| 415 | ret n |
| 416 | end |
| 417 | |
| 418 | fn main to int |
| 419 | constants |
| 420 | n = greadunit |
| 421 | code = decode n |
| 422 | if eq code 68 |
| 423 | gdie 68 |
| 424 | end |
| 425 | if ne code 0 |
| 426 | gdie 65 |
| 427 | end |
| 428 | if ne gs1 0 |
| 429 | gdie 68 |
| 430 | end |
| 431 | for k ulong get M 4 |
| 432 | gmetric k |
| 433 | end |
| 434 | root = groot |
| 435 | if gt get Gdp root 16 |
| 436 | gdie 67 |
| 437 | end |
| 438 | if gt add add get Gni 0 get Gcn root 2 64 |
| 439 | gdie 67 |
| 440 | end |
| 441 | if not gconstok root |
| 442 | gdie 67 |
| 443 | end |
| 444 | put Gl 0 0 |
| 445 | gsignature |
| 446 | gemit root 1 |
| 447 | for i ulong len Gtail |
| 448 | gbyte ulong get Gtail i |
| 449 | end |
| 450 | total = get Gl 0 |
| 451 | sent = ulong 0 |
| 452 | while lt sent total |
| 453 | r = write 1 Gout sent sub total sent |
| 454 | if le r 0 |
| 455 | gdie 77 |
| 456 | end |
| 457 | sent = add sent ulong r |
| 458 | end |
| 459 | ret 0 |
| 460 | end |