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 / cpp-u64-export.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | note Handwritten extended-family upper-to-C++17 exporter (#739, spec/universal/cpp-u64-v1.md section 4). |
| 3 | note Accepts the C, Rust, Python (v1) and C++, Zig, Go (v2) u64 members; output depends on the records only. |
| 4 | note Prepended unchanged at build: src/source-aware/vendor/sha256.om and src/source-aware/core-v2.om (decode n). |
| 5 | note Input: one OMSB envelope to EOF. The OMSP sidecar is validated by decode, never read for output. |
| 6 | global Xone uchar 1 |
| 7 | global Xob ulong 1 |
| 8 | global Xo uchar 4096 |
| 9 | global Xl ulong 1 |
| 10 | global Xni ulong 1 |
| 11 | global Xpr ulong 64 |
| 12 | global Xln ulong 64 |
| 13 | global Xdp ulong 64 |
| 14 | global Xcn ulong 64 |
| 15 | text Xhead "#include <cstdint>\nstd::uint64_t calc(" |
| 16 | text Xpar "std::uint64_t i" |
| 17 | text Xlit "std::uint64_t{" |
| 18 | text Xsuf "ULL}" |
| 19 | text Xret ") { return " |
| 20 | text Xtail "; }\n" |
| 21 | text XMal "CPP_EXPORT_MALFORMED\n" |
| 22 | text XLim "CPP_EXPORT_LIMIT\n" |
| 23 | text XProf "CPP_EXPORT_PROFILE_MISMATCH\n" |
| 24 | text XIO "CPP_EXPORT_IO\n" |
| 25 | |
| 26 | fn xdie code ulong to void |
| 27 | if eq code 65 |
| 28 | w = write 2 XMal 0 len XMal |
| 29 | end |
| 30 | if eq code 67 |
| 31 | w = write 2 XLim 0 len XLim |
| 32 | end |
| 33 | if eq code 68 |
| 34 | w = write 2 XProf 0 len XProf |
| 35 | end |
| 36 | if eq code 77 |
| 37 | w = write 2 XIO 0 len XIO |
| 38 | end |
| 39 | exit int code |
| 40 | end |
| 41 | |
| 42 | fn xbyte c ulong to void |
| 43 | put Xo get Xl 0 uchar c |
| 44 | put Xl 0 add get Xl 0 1 |
| 45 | end |
| 46 | |
| 47 | fn xtext0 to void |
| 48 | for i ulong len Xlit |
| 49 | xbyte ulong get Xlit i |
| 50 | end |
| 51 | end |
| 52 | |
| 53 | fn xtext1 to void |
| 54 | for i ulong len Xsuf |
| 55 | xbyte ulong get Xsuf i |
| 56 | end |
| 57 | end |
| 58 | |
| 59 | fn xdigits v ulong to ulong |
| 60 | d = ulong 1 |
| 61 | x = div v 10 |
| 62 | while gt x 0 |
| 63 | d = add d 1 |
| 64 | x = div x 10 |
| 65 | end |
| 66 | ret d |
| 67 | end |
| 68 | |
| 69 | fn xdec v ulong to void |
| 70 | d = new uchar 20 0 |
| 71 | i = ulong 0 |
| 72 | x = v |
| 73 | put d 0 uchar add rem x 10 48 |
| 74 | x = div x 10 |
| 75 | i = add i 1 |
| 76 | while gt x 0 |
| 77 | put d i uchar add rem x 10 48 |
| 78 | x = div x 10 |
| 79 | i = add i 1 |
| 80 | end |
| 81 | while gt i 0 |
| 82 | i = sub i 1 |
| 83 | xbyte ulong get d i |
| 84 | end |
| 85 | end |
| 86 | |
| 87 | fn xkind i ulong to ulong |
| 88 | c = get Oc i |
| 89 | if or ne get Ot i 1 ne get Oef i 0 |
| 90 | ret 68 |
| 91 | end |
| 92 | if ne get Oal i 0 |
| 93 | ret 68 |
| 94 | end |
| 95 | if eq c 9 |
| 96 | if or ne get Oat i 0 ne get Oob i 1 |
| 97 | ret 68 |
| 98 | end |
| 99 | put Xni 0 add get Xni 0 1 |
| 100 | ret 0 |
| 101 | end |
| 102 | if eq c 1 |
| 103 | if or ne get Oat i 1 ne get Oob i 0 |
| 104 | ret 68 |
| 105 | end |
| 106 | ret 0 |
| 107 | end |
| 108 | if or lt c 2 gt c 4 |
| 109 | ret 68 |
| 110 | end |
| 111 | if or ne get Oat i 1 ne get Oob i get Xob 0 |
| 112 | ret 68 |
| 113 | end |
| 114 | ret 0 |
| 115 | end |
| 116 | |
| 117 | fn xmember l ulong p ulong to ulong |
| 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 xfamily to ulong |
| 140 | put Xob 0 xmember get M 0 get M 1 |
| 141 | if eq get Xob 0 0 |
| 142 | ret 68 |
| 143 | end |
| 144 | if or ne get M 2 0 ne get M 3 1 |
| 145 | ret 68 |
| 146 | end |
| 147 | put Xni 0 0 |
| 148 | for i ulong get M 4 |
| 149 | if ne xkind i 0 |
| 150 | ret 68 |
| 151 | end |
| 152 | end |
| 153 | for j ulong get M 4 |
| 154 | if eq get Oc j 9 |
| 155 | if ge get Oi j get Xni 0 |
| 156 | ret 68 |
| 157 | end |
| 158 | end |
| 159 | end |
| 160 | ret 0 |
| 161 | end |
| 162 | |
| 163 | fn xpa k ulong to ulong |
| 164 | if lt get Xpr get Oa k get Xpr k |
| 165 | ret 1 |
| 166 | end |
| 167 | ret 0 |
| 168 | end |
| 169 | |
| 170 | fn xpb k ulong to ulong |
| 171 | if le get Xpr get Ob k get Xpr k |
| 172 | ret 1 |
| 173 | end |
| 174 | ret 0 |
| 175 | end |
| 176 | |
| 177 | fn xsat v ulong to ulong |
| 178 | if gt v 1048576 |
| 179 | ret 1048576 |
| 180 | end |
| 181 | ret v |
| 182 | end |
| 183 | |
| 184 | fn xmetric k ulong to void |
| 185 | c = get Oc k |
| 186 | put Xdp k 0 |
| 187 | if eq c 9 |
| 188 | put Xpr k 3 |
| 189 | put Xln k 2 |
| 190 | put Xcn k 0 |
| 191 | ret |
| 192 | end |
| 193 | if eq c 1 |
| 194 | put Xpr k 3 |
| 195 | put Xln k add xdigits get Oi k add len Xlit len Xsuf |
| 196 | put Xcn k 1 |
| 197 | ret |
| 198 | end |
| 199 | put Xpr k 1 |
| 200 | if eq c 4 |
| 201 | put Xpr k 2 |
| 202 | end |
| 203 | a = get Oa k |
| 204 | b = get Ob k |
| 205 | pa = xpa k |
| 206 | pb = xpb k |
| 207 | l = add get Xln a get Xln b |
| 208 | l = add l add 3 mul 2 add pa pb |
| 209 | put Xln k xsat l |
| 210 | da = add get Xdp a pa |
| 211 | db = add get Xdp b pb |
| 212 | if gt da db |
| 213 | put Xdp k da |
| 214 | else |
| 215 | put Xdp k db |
| 216 | end |
| 217 | put Xcn k xsat add add get Xcn a get Xcn b 1 |
| 218 | end |
| 219 | |
| 220 | fn xemit k ulong to void |
| 221 | c = get Oc k |
| 222 | if eq c 9 |
| 223 | xbyte 105 |
| 224 | xbyte add 48 get Oi k |
| 225 | ret |
| 226 | end |
| 227 | if eq c 1 |
| 228 | xtext0 |
| 229 | xdec get Oi k |
| 230 | xtext1 |
| 231 | ret |
| 232 | end |
| 233 | pa = xpa k |
| 234 | pb = xpb k |
| 235 | if eq pa 1 |
| 236 | xbyte 40 |
| 237 | end |
| 238 | xemit get Oa k |
| 239 | if eq pa 1 |
| 240 | xbyte 41 |
| 241 | end |
| 242 | xbyte 32 |
| 243 | if eq c 2 |
| 244 | xbyte 43 |
| 245 | end |
| 246 | if eq c 3 |
| 247 | xbyte 45 |
| 248 | end |
| 249 | if eq c 4 |
| 250 | xbyte 42 |
| 251 | end |
| 252 | xbyte 32 |
| 253 | if eq pb 1 |
| 254 | xbyte 40 |
| 255 | end |
| 256 | xemit get Ob k |
| 257 | if eq pb 1 |
| 258 | xbyte 41 |
| 259 | end |
| 260 | end |
| 261 | |
| 262 | fn xroot to ulong |
| 263 | n = get M 4 |
| 264 | if lt n 3 |
| 265 | ret sub n 1 |
| 266 | end |
| 267 | z = sub n 2 |
| 268 | y = sub n 1 |
| 269 | if or ne get Oc z 1 ne get Oi z 0 |
| 270 | ret y |
| 271 | end |
| 272 | if or ne get Oc y 2 ne get Ob y z |
| 273 | ret y |
| 274 | end |
| 275 | if ge get Oa y z |
| 276 | ret y |
| 277 | end |
| 278 | ret get Oa y |
| 279 | end |
| 280 | |
| 281 | fn xparams to ulong |
| 282 | ni = get Xni 0 |
| 283 | if eq ni 0 |
| 284 | ret 0 |
| 285 | end |
| 286 | ret sub add mul ni len Xpar mul ni 3 2 |
| 287 | end |
| 288 | |
| 289 | fn xsignature to void |
| 290 | for i ulong len Xhead |
| 291 | xbyte ulong get Xhead i |
| 292 | end |
| 293 | for s ulong get Xni 0 |
| 294 | if gt s 0 |
| 295 | xbyte 44 |
| 296 | xbyte 32 |
| 297 | end |
| 298 | for t ulong len Xpar |
| 299 | xbyte ulong get Xpar t |
| 300 | end |
| 301 | xbyte add 48 s |
| 302 | end |
| 303 | for u ulong len Xret |
| 304 | xbyte ulong get Xret u |
| 305 | end |
| 306 | end |
| 307 | |
| 308 | fn xreadunit to ulong |
| 309 | n = ulong 0 |
| 310 | more = ulong 1 |
| 311 | while and eq more 1 lt n 8192 |
| 312 | r = read 0 Ib n sub ulong 8192 n |
| 313 | if lt r 0 |
| 314 | xdie 77 |
| 315 | end |
| 316 | if eq r 0 |
| 317 | more = 0 |
| 318 | else |
| 319 | n = add n ulong r |
| 320 | end |
| 321 | end |
| 322 | if eq n 8192 |
| 323 | r = read 0 Xone 0 1 |
| 324 | if lt r 0 |
| 325 | xdie 77 |
| 326 | end |
| 327 | if gt r 0 |
| 328 | xdie 65 |
| 329 | end |
| 330 | end |
| 331 | ret n |
| 332 | end |
| 333 | |
| 334 | fn main to int |
| 335 | constants |
| 336 | n = xreadunit |
| 337 | code = decode n |
| 338 | if eq code 68 |
| 339 | xdie 68 |
| 340 | end |
| 341 | if ne code 0 |
| 342 | xdie 65 |
| 343 | end |
| 344 | if ne xfamily 0 |
| 345 | xdie 68 |
| 346 | end |
| 347 | for k ulong get M 4 |
| 348 | xmetric k |
| 349 | end |
| 350 | root = xroot |
| 351 | total = add add len Xhead len Xret len Xtail |
| 352 | total = add total add xparams get Xln root |
| 353 | if gt total 4096 |
| 354 | xdie 67 |
| 355 | end |
| 356 | if gt get Xdp root 16 |
| 357 | xdie 67 |
| 358 | end |
| 359 | if gt add add get Xni 0 get Xcn root 2 64 |
| 360 | xdie 67 |
| 361 | end |
| 362 | put Xl 0 0 |
| 363 | xsignature |
| 364 | xemit root |
| 365 | for i ulong len Xtail |
| 366 | xbyte ulong get Xtail i |
| 367 | end |
| 368 | if ne get Xl 0 total |
| 369 | xdie 65 |
| 370 | end |
| 371 | sent = ulong 0 |
| 372 | while lt sent total |
| 373 | r = write 1 Xo sent sub total sent |
| 374 | if le r 0 |
| 375 | xdie 77 |
| 376 | end |
| 377 | sent = add sent ulong r |
| 378 | end |
| 379 | ret 0 |
| 380 | end |