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 / seed / wasm / irwasm.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note OSC IR v1 scalar and v7 memory profile to WebAssembly; no host or foreign generator. |
| 3 | note Wire authority spec/one-layer-format.md sections 3 and 10; referee src/compiler/osc_interp.c. |
| 4 | note Wasm opcodes https://webassembly.github.io/spec/core/binary/instructions.html |
| 5 | note Buffered output: every parse/type/capacity refusal occurs before stdout writes. |
| 6 | note Input holds a v7 unit of 8388608 bytes plus one sentinel byte; v1 keeps 2620251. |
| 7 | global input uchar 8388609 |
| 8 | global output uchar 8388608 |
| 9 | note state slots: 0 input cursor, 1 input size, 2 output size, 3 nparams, |
| 10 | note 4 nvregs, 5 nblocks, 6 ninsns, 7 nfuncs, 8 version, 9 nregions, |
| 11 | note 10 memory pages, 11 v7 feature seen. Current function arrays are reused. |
| 12 | note Wasm params retain ABI types. Internal vregs are canonical i64 bit patterns; |
| 13 | note f32 is its zero-extended bits and f64 its bits. |
| 14 | note Locals after params: nvregs i64, dispatcher i32, arithmetic scratch i64. |
| 15 | global state ulong 16 |
| 16 | global starts ulong 32 |
| 17 | global names ulong 32 |
| 18 | global namelen ulong 32 |
| 19 | global params ulong 32 |
| 20 | global returns ulong 32 |
| 21 | global sigs ulong 192 |
| 22 | global types ulong 480 |
| 23 | global first ulong 512 |
| 24 | global counts ulong 512 |
| 25 | global ops ulong 4096 |
| 26 | global subs ulong 4096 |
| 27 | global dest ulong 4096 |
| 28 | global arg0 ulong 4096 |
| 29 | global arg1 ulong 4096 |
| 30 | global immed ulong 4096 |
| 31 | global nargs ulong 4096 |
| 32 | global args ulong 24576 |
| 33 | global cover uchar 4096 |
| 34 | global dins ulong 4096 |
| 35 | global douts ulong 4096 |
| 36 | global work ulong 8 |
| 37 | note v7 region table: name offset/length in input, tag, class, owner, count, |
| 38 | note initial data offset in input, assigned linear-memory byte offset. |
| 39 | global rname ulong 1024 |
| 40 | global rnlen ulong 1024 |
| 41 | global rtag ulong 1024 |
| 42 | global rclass ulong 1024 |
| 43 | global rowner ulong 1024 |
| 44 | global rcount ulong 1024 |
| 45 | global rdata ulong 1024 |
| 46 | global rbase ulong 1024 |
| 47 | note Local-region initialization bits: 16 words of 64 regions per block. |
| 48 | global rin ulong 8192 |
| 49 | global rout ulong 8192 |
| 50 | global rwork ulong 16 |
| 51 | global reach uchar 512 |
| 52 | text magic6 "OSC1IR" |
| 53 | text memname "memory" |
| 54 | text regpfx "region." |
| 55 | text abiname "osc.v7.abi" |
| 56 | text emagic "irwasm: MAGIC\n" |
| 57 | text ever "irwasm: VERSION\n" |
| 58 | text etrunc "irwasm: TRUNCATED\n" |
| 59 | text ecap "irwasm: CAPACITY\n" |
| 60 | text eop "irwasm: UNSUPPORTED_OP\n" |
| 61 | text etype "irwasm: UNSUPPORTED_TAG\n" |
| 62 | text einval "irwasm: INVALID\n" |
| 63 | text etrail "irwasm: TRAILING\n" |
| 64 | text eio "irwasm: IO\n" |
| 65 | fn refuse code int to void |
| 66 | k = long 0 |
| 67 | if eq code 20 |
| 68 | k = write 2 emagic 0 len emagic |
| 69 | end |
| 70 | if eq code 21 |
| 71 | k = write 2 ever 0 len ever |
| 72 | end |
| 73 | if eq code 22 |
| 74 | k = write 2 etrunc 0 len etrunc |
| 75 | end |
| 76 | if eq code 23 |
| 77 | k = write 2 ecap 0 len ecap |
| 78 | end |
| 79 | if eq code 24 |
| 80 | k = write 2 eop 0 len eop |
| 81 | end |
| 82 | if eq code 25 |
| 83 | k = write 2 etype 0 len etype |
| 84 | end |
| 85 | if eq code 26 |
| 86 | k = write 2 einval 0 len einval |
| 87 | end |
| 88 | if eq code 27 |
| 89 | k = write 2 etrail 0 len etrail |
| 90 | end |
| 91 | if eq code 28 |
| 92 | k = write 2 eio 0 len eio |
| 93 | end |
| 94 | exit code |
| 95 | end |
| 96 | fn need ok bool to void |
| 97 | if not ok |
| 98 | refuse 26 |
| 99 | end |
| 100 | end |
| 101 | fn rd n ulong to ulong |
| 102 | p = get state 0 |
| 103 | if gt n sub get state 1 p |
| 104 | refuse 22 |
| 105 | end |
| 106 | v = ulong 0 |
| 107 | for j ulong n |
| 108 | v = or v shl ulong get input add p j mul j 8 |
| 109 | end |
| 110 | put state 0 add p n |
| 111 | ret v |
| 112 | end |
| 113 | fn cap n ulong maxv ulong to ulong |
| 114 | if gt n maxv |
| 115 | refuse 23 |
| 116 | end |
| 117 | ret n |
| 118 | end |
| 119 | fn isflt t ulong to bool |
| 120 | ret or eq t 13 eq t 14 |
| 121 | end |
| 122 | fn sgn t ulong to bool |
| 123 | ret and ge t 6 le t 9 |
| 124 | end |
| 125 | fn isint t ulong to bool |
| 126 | ret and ge t 2 le t 9 |
| 127 | end |
| 128 | fn feat to void |
| 129 | put state 11 1 |
| 130 | end |
| 131 | fn typ to ulong |
| 132 | t = rd 1 |
| 133 | if and eq get state 8 7 isflt t |
| 134 | feat |
| 135 | ret t |
| 136 | end |
| 137 | if gt t 9 |
| 138 | refuse 25 |
| 139 | end |
| 140 | ret t |
| 141 | end |
| 142 | fn vr to ulong |
| 143 | v = rd 2 |
| 144 | need lt v get state 4 |
| 145 | ret v |
| 146 | end |
| 147 | fn rg to ulong |
| 148 | v = rd 2 |
| 149 | need lt v get state 9 |
| 150 | ret v |
| 151 | end |
| 152 | fn width t ulong to ulong |
| 153 | if eq t 1 |
| 154 | ret 1 |
| 155 | end |
| 156 | if or eq t 2 eq t 6 |
| 157 | ret 8 |
| 158 | end |
| 159 | if or eq t 3 eq t 7 |
| 160 | ret 16 |
| 161 | end |
| 162 | if or or eq t 4 eq t 8 eq t 13 |
| 163 | ret 32 |
| 164 | end |
| 165 | ret 64 |
| 166 | end |
| 167 | note log2 of the element byte width: wasm alignment hint and index shift. |
| 168 | fn lg t ulong to ulong |
| 169 | w = width t |
| 170 | if eq w 8 |
| 171 | ret 0 |
| 172 | end |
| 173 | if eq w 16 |
| 174 | ret 1 |
| 175 | end |
| 176 | if eq w 32 |
| 177 | ret 2 |
| 178 | end |
| 179 | ret 3 |
| 180 | end |
| 181 | fn mask t ulong to ulong |
| 182 | w = width t |
| 183 | if eq w 64 |
| 184 | ret 18446744073709551615 |
| 185 | end |
| 186 | ret sub shl ulong 1 w 1 |
| 187 | end |
| 188 | fn canon v ulong t ulong to ulong |
| 189 | w = width t |
| 190 | if eq w 64 |
| 191 | ret v |
| 192 | end |
| 193 | v = and v mask t |
| 194 | if sgn t |
| 195 | m = shl ulong 1 sub w 1 |
| 196 | v = sub xor v m m |
| 197 | end |
| 198 | ret v |
| 199 | end |
| 200 | fn txt n ulong to void |
| 201 | for j ulong n |
| 202 | v = rd 1 |
| 203 | need ne v 0 |
| 204 | end |
| 205 | end |
| 206 | fn nametxt n ulong to void |
| 207 | for j ulong n |
| 208 | v = rd 1 |
| 209 | need and ne v 0 lt v 128 |
| 210 | end |
| 211 | end |
| 212 | fn same a ulong b ulong to bool |
| 213 | if ne get namelen a get namelen b |
| 214 | ret false |
| 215 | end |
| 216 | for j ulong get namelen a |
| 217 | x = get input add get names a j |
| 218 | y = get input add get names b j |
| 219 | if ne x y |
| 220 | ret false |
| 221 | end |
| 222 | end |
| 223 | ret true |
| 224 | end |
| 225 | note Region names: 1..63 ASCII bytes, no NUL and no dot (spec 10.1). |
| 226 | fn rsame a ulong b ulong to bool |
| 227 | if ne get rnlen a get rnlen b |
| 228 | ret false |
| 229 | end |
| 230 | for j ulong get rnlen a |
| 231 | x = get input add get rname a j |
| 232 | y = get input add get rname b j |
| 233 | if ne x y |
| 234 | ret false |
| 235 | end |
| 236 | end |
| 237 | ret true |
| 238 | end |
| 239 | fn skip n ulong to void |
| 240 | p = get state 0 |
| 241 | if gt n sub get state 1 p |
| 242 | refuse 22 |
| 243 | end |
| 244 | put state 0 add p n |
| 245 | end |
| 246 | note Every field is checked in byte order as it is read (spec 10.1 reader classes). |
| 247 | fn region r ulong to void |
| 248 | n = rd 1 |
| 249 | need and ge n 1 le n 63 |
| 250 | put rnlen r n |
| 251 | put rname r get state 0 |
| 252 | for j ulong n |
| 253 | v = rd 1 |
| 254 | need and and ne v 0 lt v 128 ne v 46 |
| 255 | end |
| 256 | t = rd 1 |
| 257 | need or isint t isflt t |
| 258 | put rtag r t |
| 259 | c = rd 1 |
| 260 | if eq c 3 |
| 261 | refuse 24 |
| 262 | end |
| 263 | need le c 2 |
| 264 | put rclass r c |
| 265 | put rowner r rd 2 |
| 266 | k = rd 4 |
| 267 | put rcount r k |
| 268 | need le k 16777216 |
| 269 | if ne c 2 |
| 270 | need ne k 0 |
| 271 | end |
| 272 | n = rd 4 |
| 273 | if eq c 2 |
| 274 | need and eq t 2 eq n k |
| 275 | else |
| 276 | need eq n 0 |
| 277 | end |
| 278 | put rdata r get state 0 |
| 279 | skip n |
| 280 | end |
| 281 | fn regions to void |
| 282 | nr = rd 2 |
| 283 | need le nr 1024 |
| 284 | put state 9 nr |
| 285 | if ne nr 0 |
| 286 | feat |
| 287 | end |
| 288 | for r ulong nr |
| 289 | region r |
| 290 | end |
| 291 | end |
| 292 | fn insn i ulong fi ulong to void |
| 293 | o = rd 1 |
| 294 | put ops i o |
| 295 | put dest i 65535 |
| 296 | put arg0 i 65535 |
| 297 | put arg1 i 65535 |
| 298 | put nargs i 0 |
| 299 | if eq o 1 |
| 300 | d = vr |
| 301 | put dest i d |
| 302 | v = rd 8 |
| 303 | need eq v canon v get types d |
| 304 | put immed i v |
| 305 | ret |
| 306 | end |
| 307 | if or eq o 2 eq o 6 |
| 308 | put dest i vr |
| 309 | put arg0 i vr |
| 310 | ret |
| 311 | end |
| 312 | if or or eq o 3 eq o 4 eq o 5 |
| 313 | put subs i rd 1 |
| 314 | put dest i vr |
| 315 | put arg0 i vr |
| 316 | if ne o 4 |
| 317 | put arg1 i vr |
| 318 | end |
| 319 | ret |
| 320 | end |
| 321 | if eq o 11 |
| 322 | d = rd 2 |
| 323 | if ne d 65535 |
| 324 | need lt d get state 4 |
| 325 | end |
| 326 | put dest i d |
| 327 | c = rd 2 |
| 328 | need lt c fi |
| 329 | put immed i c |
| 330 | n = cap rd 1 6 |
| 331 | put nargs i n |
| 332 | need eq n get params c |
| 333 | for j ulong n |
| 334 | put args add mul i 6 j vr |
| 335 | end |
| 336 | ret |
| 337 | end |
| 338 | if eq o 12 |
| 339 | t = rd 1 |
| 340 | need and ge t 1 le t 14 |
| 341 | put immed i t |
| 342 | ret |
| 343 | end |
| 344 | if eq o 13 |
| 345 | v = rd 2 |
| 346 | need lt v get state 5 |
| 347 | put immed i v |
| 348 | ret |
| 349 | end |
| 350 | if eq o 14 |
| 351 | put arg0 i vr |
| 352 | v = rd 2 |
| 353 | need lt v get state 5 |
| 354 | put immed i v |
| 355 | v = rd 2 |
| 356 | need lt v get state 5 |
| 357 | put arg1 i v |
| 358 | ret |
| 359 | end |
| 360 | if eq o 15 |
| 361 | v = rd 2 |
| 362 | if ne v 65535 |
| 363 | need lt v get state 4 |
| 364 | end |
| 365 | put arg0 i v |
| 366 | ret |
| 367 | end |
| 368 | if and eq get state 8 7 and ge o 30 le o 33 |
| 369 | insn7 i o |
| 370 | ret |
| 371 | end |
| 372 | refuse 24 |
| 373 | end |
| 374 | note v7: 30 RLOAD dst region index, 31 RSTORE region index src, |
| 375 | note 32 BITCAST dst src, 33 RINIT region init. Region id lives in immed. |
| 376 | fn insn7 i ulong o ulong to void |
| 377 | feat |
| 378 | if eq o 30 |
| 379 | put dest i vr |
| 380 | put immed i rg |
| 381 | put arg0 i vr |
| 382 | ret |
| 383 | end |
| 384 | if eq o 31 |
| 385 | put immed i rg |
| 386 | put arg0 i vr |
| 387 | put arg1 i vr |
| 388 | ret |
| 389 | end |
| 390 | if eq o 32 |
| 391 | put dest i vr |
| 392 | put arg0 i vr |
| 393 | ret |
| 394 | end |
| 395 | put immed i rg |
| 396 | put arg0 i vr |
| 397 | end |
| 398 | fn callchk i ulong to void |
| 399 | c = get immed i |
| 400 | d = get dest i |
| 401 | if eq get returns c 0 |
| 402 | need eq d 65535 |
| 403 | else |
| 404 | need ne d 65535 |
| 405 | need eq get types d get returns c |
| 406 | end |
| 407 | for j ulong get nargs i |
| 408 | v = get args add mul i 6 j |
| 409 | t = get types v |
| 410 | u = get sigs add mul c 6 j |
| 411 | need eq t u |
| 412 | end |
| 413 | end |
| 414 | fn check i ulong fi ulong to void |
| 415 | o = get ops i |
| 416 | d = get dest i |
| 417 | a = get arg0 i |
| 418 | b = get arg1 i |
| 419 | s = get subs i |
| 420 | t = ulong 0 |
| 421 | u = ulong 0 |
| 422 | if ne a 65535 |
| 423 | t = get types a |
| 424 | end |
| 425 | if ne d 65535 |
| 426 | u = get types d |
| 427 | end |
| 428 | if eq o 2 |
| 429 | need eq t u |
| 430 | end |
| 431 | if eq o 3 |
| 432 | need and ge s 1 le s 10 |
| 433 | need eq t u |
| 434 | if ge s 9 |
| 435 | need and ge t 2 ge get types b 2 |
| 436 | else |
| 437 | need eq t get types b |
| 438 | if le s 5 |
| 439 | need ge t 2 |
| 440 | end |
| 441 | end |
| 442 | end |
| 443 | if eq o 4 |
| 444 | need and ge s 1 le s 3 |
| 445 | need eq t u |
| 446 | if eq s 3 |
| 447 | need eq t 1 |
| 448 | else |
| 449 | need ge t 2 |
| 450 | end |
| 451 | end |
| 452 | if eq o 5 |
| 453 | need and ge s 1 le s 6 |
| 454 | need eq u 1 |
| 455 | need eq t get types b |
| 456 | end |
| 457 | if eq o 6 |
| 458 | need and ge t 2 ge u 2 |
| 459 | end |
| 460 | if eq o 11 |
| 461 | callchk i |
| 462 | end |
| 463 | if eq o 14 |
| 464 | need eq t 1 |
| 465 | end |
| 466 | if eq o 15 |
| 467 | if eq get returns fi 0 |
| 468 | need eq a 65535 |
| 469 | else |
| 470 | need eq t get returns fi |
| 471 | end |
| 472 | end |
| 473 | if eq get state 8 7 |
| 474 | check7 i fi |
| 475 | end |
| 476 | end |
| 477 | note BITCAST width sets {u8,i8} {u16,i16} {u32,i32,f32} {u64,i64,f64}; 0 = none. |
| 478 | fn bclass t ulong to ulong |
| 479 | if or eq t 2 eq t 6 |
| 480 | ret 1 |
| 481 | end |
| 482 | if or eq t 3 eq t 7 |
| 483 | ret 2 |
| 484 | end |
| 485 | if or or eq t 4 eq t 8 eq t 13 |
| 486 | ret 3 |
| 487 | end |
| 488 | if or or eq t 5 eq t 9 eq t 14 |
| 489 | ret 4 |
| 490 | end |
| 491 | ret 0 |
| 492 | end |
| 493 | note A function-local region is accessible only by its owning function. |
| 494 | fn regok r ulong fi ulong to void |
| 495 | if eq get rclass r 1 |
| 496 | need eq get rowner r fi |
| 497 | end |
| 498 | end |
| 499 | fn check7 i ulong fi ulong to void |
| 500 | o = get ops i |
| 501 | s = get subs i |
| 502 | a = get arg0 i |
| 503 | t = ulong 0 |
| 504 | if ne a 65535 |
| 505 | t = get types a |
| 506 | end |
| 507 | if and eq o 3 isflt t |
| 508 | need le s 4 |
| 509 | end |
| 510 | if and eq o 3 ge s 9 |
| 511 | need not isflt get types get arg1 i |
| 512 | end |
| 513 | if and eq o 4 isflt t |
| 514 | need eq s 1 |
| 515 | end |
| 516 | r = get immed i |
| 517 | if eq o 30 |
| 518 | need isint t |
| 519 | need eq get types get dest i get rtag r |
| 520 | regok r fi |
| 521 | end |
| 522 | if eq o 31 |
| 523 | need isint t |
| 524 | need eq get types get arg1 i get rtag r |
| 525 | need ne get rclass r 2 |
| 526 | regok r fi |
| 527 | end |
| 528 | if eq o 32 |
| 529 | b = bclass t |
| 530 | need and ne b 0 eq b bclass get types get dest i |
| 531 | end |
| 532 | if eq o 33 |
| 533 | need eq get rclass r 1 |
| 534 | regok r fi |
| 535 | need eq t get rtag r |
| 536 | end |
| 537 | end |
| 538 | fn blocks to void |
| 539 | ni = get state 6 |
| 540 | for j ulong get state 5 |
| 541 | f = get first j |
| 542 | n = get counts j |
| 543 | need and ne n 0 le f ni |
| 544 | need le n sub ni f |
| 545 | for k ulong n |
| 546 | ix = add f k |
| 547 | need eq get cover ix 0 |
| 548 | put cover ix 1 |
| 549 | o = get ops ix |
| 550 | term = and ge o 13 le o 15 |
| 551 | need eq term eq k sub n 1 |
| 552 | end |
| 553 | end |
| 554 | for j ulong ni |
| 555 | need eq get cover j 1 |
| 556 | end |
| 557 | end |
| 558 | fn loadfn fi ulong to void |
| 559 | put state 0 get starts fi |
| 560 | n = cap rd 1 63 |
| 561 | put namelen fi n |
| 562 | put names fi get state 0 |
| 563 | nametxt n |
| 564 | for j ulong fi |
| 565 | need not same fi j |
| 566 | end |
| 567 | np = cap rd 1 6 |
| 568 | put params fi np |
| 569 | put returns fi typ |
| 570 | nv = cap rd 2 480 |
| 571 | need ge nv np |
| 572 | put state 3 np |
| 573 | put state 4 nv |
| 574 | for j ulong nv |
| 575 | t = typ |
| 576 | need ne t 0 |
| 577 | put types j t |
| 578 | if lt j np |
| 579 | put sigs add mul fi 6 j t |
| 580 | end |
| 581 | end |
| 582 | nb = cap rd 2 512 |
| 583 | need ne nb 0 |
| 584 | put state 5 nb |
| 585 | for j ulong nb |
| 586 | put first j rd 4 |
| 587 | put counts j rd 4 |
| 588 | end |
| 589 | ni = cap rd 4 4096 |
| 590 | need ne ni 0 |
| 591 | put state 6 ni |
| 592 | for j ulong ni |
| 593 | insn j fi |
| 594 | check j fi |
| 595 | put cover j 0 |
| 596 | end |
| 597 | blocks |
| 598 | n = cap rd 2 255 |
| 599 | txt n |
| 600 | n = cap rd 2 255 |
| 601 | txt n |
| 602 | end |
| 603 | fn bitset v ulong to void |
| 604 | k = div v 64 |
| 605 | put work k or get work k shl ulong 1 rem v 64 |
| 606 | end |
| 607 | fn use v ulong to void |
| 608 | if eq v 65535 |
| 609 | ret |
| 610 | end |
| 611 | need ne and get work div v 64 shl ulong 1 rem v 64 0 |
| 612 | end |
| 613 | fn calluse i ulong to void |
| 614 | for k ulong get nargs i |
| 615 | v = get args add mul i 6 k |
| 616 | use v |
| 617 | end |
| 618 | end |
| 619 | fn transfer b ulong verify bool to void |
| 620 | for k ulong 8 |
| 621 | put work k get dins add mul b 8 k |
| 622 | end |
| 623 | for j ulong get counts b |
| 624 | i = add get first b j |
| 625 | o = get ops i |
| 626 | if verify |
| 627 | if and ge o 2 le o 6 |
| 628 | use get arg0 i |
| 629 | end |
| 630 | if or eq o 3 eq o 5 |
| 631 | use get arg1 i |
| 632 | end |
| 633 | if or eq o 14 eq o 15 |
| 634 | use get arg0 i |
| 635 | end |
| 636 | if eq o 11 |
| 637 | calluse i |
| 638 | end |
| 639 | if and ge o 30 le o 33 |
| 640 | use get arg0 i |
| 641 | end |
| 642 | if eq o 31 |
| 643 | use get arg1 i |
| 644 | end |
| 645 | end |
| 646 | d = get dest i |
| 647 | if ne d 65535 |
| 648 | bitset d |
| 649 | end |
| 650 | end |
| 651 | end |
| 652 | fn merge b ulong pred ulong to void |
| 653 | i = add get first pred sub get counts pred 1 |
| 654 | o = get ops i |
| 655 | target = false |
| 656 | if or eq o 13 eq o 14 |
| 657 | target = eq get immed i b |
| 658 | if eq o 14 |
| 659 | target = or target eq get arg1 i b |
| 660 | end |
| 661 | end |
| 662 | if target |
| 663 | for k ulong 8 |
| 664 | ix = add mul b 8 k |
| 665 | v = get douts add mul pred 8 k |
| 666 | v = and v get dins ix |
| 667 | put dins ix v |
| 668 | end |
| 669 | end |
| 670 | end |
| 671 | fn entrydef to void |
| 672 | for k ulong 8 |
| 673 | put dins k 0 |
| 674 | end |
| 675 | for v ulong get state 3 |
| 676 | k = div v 64 |
| 677 | m = shl ulong 1 rem v 64 |
| 678 | put dins k or get dins k m |
| 679 | end |
| 680 | end |
| 681 | fn defined to void |
| 682 | nb = get state 5 |
| 683 | for b ulong nb |
| 684 | for k ulong 8 |
| 685 | put dins add mul b 8 k 18446744073709551615 |
| 686 | put douts add mul b 8 k 18446744073709551615 |
| 687 | end |
| 688 | end |
| 689 | changed = true |
| 690 | note At most one bit per (block,register) is removed per changing round. |
| 691 | while changed bound 245761 |
| 692 | changed = false |
| 693 | for b ulong nb |
| 694 | if eq b 0 |
| 695 | entrydef |
| 696 | else |
| 697 | for k ulong 8 |
| 698 | put dins add mul b 8 k 18446744073709551615 |
| 699 | end |
| 700 | for p ulong nb |
| 701 | merge b p |
| 702 | end |
| 703 | end |
| 704 | transfer b false |
| 705 | for k ulong 8 |
| 706 | ix = add mul b 8 k |
| 707 | v = get work k |
| 708 | if ne v get douts ix |
| 709 | changed = true |
| 710 | put douts ix v |
| 711 | end |
| 712 | end |
| 713 | end |
| 714 | end |
| 715 | for b ulong nb |
| 716 | transfer b true |
| 717 | end |
| 718 | end |
| 719 | note v7 local-region initialization (spec 10.3): a block entry bit is the AND of |
| 720 | note its reachable predecessors' exit bits; entry and unreachable blocks start false. |
| 721 | fn mark b ulong to bool |
| 722 | if eq get reach b 0 |
| 723 | put reach b 1 |
| 724 | ret true |
| 725 | end |
| 726 | ret false |
| 727 | end |
| 728 | fn reachall to void |
| 729 | nb = get state 5 |
| 730 | for b ulong nb |
| 731 | put reach b 0 |
| 732 | end |
| 733 | put reach 0 1 |
| 734 | changed = true |
| 735 | note Each changing round marks at least one of at most 512 blocks. |
| 736 | while changed bound 513 |
| 737 | changed = false |
| 738 | for b ulong nb |
| 739 | if ne get reach b 0 |
| 740 | i = add get first b sub get counts b 1 |
| 741 | o = get ops i |
| 742 | if or eq o 13 eq o 14 |
| 743 | if mark get immed i |
| 744 | changed = true |
| 745 | end |
| 746 | end |
| 747 | if eq o 14 |
| 748 | if mark get arg1 i |
| 749 | changed = true |
| 750 | end |
| 751 | end |
| 752 | end |
| 753 | end |
| 754 | end |
| 755 | end |
| 756 | fn rbit r ulong to bool |
| 757 | ret ne and get rwork div r 64 shl ulong 1 rem r 64 0 |
| 758 | end |
| 759 | fn rset r ulong to void |
| 760 | k = div r 64 |
| 761 | put rwork k or get rwork k shl ulong 1 rem r 64 |
| 762 | end |
| 763 | fn rtrans b ulong verify bool to void |
| 764 | for k ulong 16 |
| 765 | put rwork k get rin add mul b 16 k |
| 766 | end |
| 767 | for j ulong get counts b |
| 768 | i = add get first b j |
| 769 | o = get ops i |
| 770 | if and verify or eq o 30 eq o 31 |
| 771 | if eq get rclass get immed i 1 |
| 772 | need rbit get immed i |
| 773 | end |
| 774 | end |
| 775 | if eq o 33 |
| 776 | rset get immed i |
| 777 | end |
| 778 | end |
| 779 | end |
| 780 | fn rmerge b ulong pred ulong to void |
| 781 | i = add get first pred sub get counts pred 1 |
| 782 | o = get ops i |
| 783 | target = false |
| 784 | if or eq o 13 eq o 14 |
| 785 | target = eq get immed i b |
| 786 | if eq o 14 |
| 787 | target = or target eq get arg1 i b |
| 788 | end |
| 789 | end |
| 790 | if target |
| 791 | for k ulong 16 |
| 792 | ix = add mul b 16 k |
| 793 | v = get rout add mul pred 16 k |
| 794 | put rin ix and get rin ix v |
| 795 | end |
| 796 | end |
| 797 | end |
| 798 | fn rentry b ulong to void |
| 799 | live = and ne b 0 ne get reach b 0 |
| 800 | v = ulong 0 |
| 801 | if live |
| 802 | v = 18446744073709551615 |
| 803 | end |
| 804 | for k ulong 16 |
| 805 | put rin add mul b 16 k v |
| 806 | end |
| 807 | if live |
| 808 | for p ulong get state 5 |
| 809 | if ne get reach p 0 |
| 810 | rmerge b p |
| 811 | end |
| 812 | end |
| 813 | end |
| 814 | end |
| 815 | fn inited to void |
| 816 | reachall |
| 817 | nb = get state 5 |
| 818 | for b ulong nb |
| 819 | for k ulong 16 |
| 820 | put rout add mul b 16 k 18446744073709551615 |
| 821 | end |
| 822 | end |
| 823 | changed = true |
| 824 | note At most one of 512*1024 = 524288 (block,region) bits drops per changing round. |
| 825 | while changed bound 524289 |
| 826 | changed = false |
| 827 | for b ulong nb |
| 828 | rentry b |
| 829 | rtrans b false |
| 830 | for k ulong 16 |
| 831 | ix = add mul b 16 k |
| 832 | v = get rwork k |
| 833 | if ne v get rout ix |
| 834 | changed = true |
| 835 | put rout ix v |
| 836 | end |
| 837 | end |
| 838 | end |
| 839 | end |
| 840 | for b ulong nb |
| 841 | rtrans b true |
| 842 | end |
| 843 | end |
| 844 | fn emit v ulong to void |
| 845 | p = get state 2 |
| 846 | if ge p len output |
| 847 | refuse 23 |
| 848 | end |
| 849 | put output p uchar v |
| 850 | put state 2 add p 1 |
| 851 | end |
| 852 | fn leb v ulong to void |
| 853 | while ge v 128 bound 10 |
| 854 | emit or and v 127 128 |
| 855 | v = shr v 7 |
| 856 | end |
| 857 | emit v |
| 858 | end |
| 859 | fn sleb v ulong to void |
| 860 | x = bitcast long v |
| 861 | while true bound 10 |
| 862 | b = and bitcast ulong x 127 |
| 863 | x = shr x 7 |
| 864 | if or and eq x 0 eq and b 64 0 and eq x -1 ne and b 64 0 |
| 865 | emit b |
| 866 | break |
| 867 | end |
| 868 | emit or b 128 |
| 869 | end |
| 870 | end |
| 871 | fn hole to ulong |
| 872 | p = get state 2 |
| 873 | for j ulong 5 |
| 874 | emit 0 |
| 875 | end |
| 876 | ret p |
| 877 | end |
| 878 | fn patch p ulong to void |
| 879 | n = sub sub get state 2 p 5 |
| 880 | for j ulong 5 |
| 881 | b = and n 127 |
| 882 | if lt j 4 |
| 883 | b = or b 128 |
| 884 | end |
| 885 | put output add p j uchar b |
| 886 | n = shr n 7 |
| 887 | end |
| 888 | end |
| 889 | fn inst op ulong ix ulong to void |
| 890 | emit op |
| 891 | leb ix |
| 892 | end |
| 893 | fn con v ulong to void |
| 894 | emit 66 |
| 895 | sleb v |
| 896 | end |
| 897 | fn local v ulong to void |
| 898 | inst 32 add get state 3 v |
| 899 | end |
| 900 | fn store v ulong to void |
| 901 | inst 33 add get state 3 v |
| 902 | end |
| 903 | fn scratch to ulong |
| 904 | ret add add get state 3 get state 4 1 |
| 905 | end |
| 906 | fn condtrap to void |
| 907 | emit 4 |
| 908 | emit 64 |
| 909 | emit 0 |
| 910 | emit 11 |
| 911 | end |
| 912 | fn norm t ulong to void |
| 913 | w = width t |
| 914 | if lt w 64 |
| 915 | if sgn t |
| 916 | con sub 64 w |
| 917 | emit 134 |
| 918 | con sub 64 w |
| 919 | emit 135 |
| 920 | else |
| 921 | con mask t |
| 922 | emit 131 |
| 923 | end |
| 924 | end |
| 925 | end |
| 926 | fn range t ulong signed bool to void |
| 927 | note Stack value saved once; check destination range without truncation. |
| 928 | inst 34 scratch |
| 929 | if lt width t 64 |
| 930 | if ge t 6 |
| 931 | con sub shl ulong 1 sub width t 1 1 |
| 932 | else |
| 933 | con mask t |
| 934 | end |
| 935 | if signed |
| 936 | emit 85 |
| 937 | else |
| 938 | emit 86 |
| 939 | end |
| 940 | condtrap |
| 941 | inst 32 scratch |
| 942 | if ge t 6 |
| 943 | con sub ulong 0 shl ulong 1 sub width t 1 |
| 944 | emit 83 |
| 945 | else |
| 946 | con 0 |
| 947 | emit 83 |
| 948 | end |
| 949 | condtrap |
| 950 | else |
| 951 | if ne signed ge t 6 |
| 952 | con 0 |
| 953 | emit 83 |
| 954 | condtrap |
| 955 | end |
| 956 | end |
| 957 | inst 32 scratch |
| 958 | end |
| 959 | fn binop s ulong signed bool to ulong |
| 960 | if le s 3 |
| 961 | ret add 123 s |
| 962 | end |
| 963 | if eq s 4 |
| 964 | if signed |
| 965 | ret 127 |
| 966 | end |
| 967 | ret 128 |
| 968 | end |
| 969 | if eq s 5 |
| 970 | if signed |
| 971 | ret 129 |
| 972 | end |
| 973 | ret 130 |
| 974 | end |
| 975 | if le s 8 |
| 976 | ret add 125 s |
| 977 | end |
| 978 | if eq s 9 |
| 979 | ret 134 |
| 980 | end |
| 981 | if signed |
| 982 | ret 135 |
| 983 | end |
| 984 | ret 136 |
| 985 | end |
| 986 | fn overflow i ulong to void |
| 987 | s = get subs i |
| 988 | a = get arg0 i |
| 989 | b = get arg1 i |
| 990 | r = scratch |
| 991 | if or eq s 1 eq s 2 |
| 992 | local a |
| 993 | inst 32 r |
| 994 | emit 133 |
| 995 | if eq s 1 |
| 996 | local b |
| 997 | inst 32 r |
| 998 | else |
| 999 | local a |
| 1000 | local b |
| 1001 | end |
| 1002 | emit 133 |
| 1003 | emit 131 |
| 1004 | con 0 |
| 1005 | emit 83 |
| 1006 | condtrap |
| 1007 | end |
| 1008 | if eq s 3 |
| 1009 | note Check division only when a != 0. MIN/-1 is itself an overflow. |
| 1010 | local a |
| 1011 | emit 80 |
| 1012 | emit 69 |
| 1013 | emit 4 |
| 1014 | emit 64 |
| 1015 | inst 32 r |
| 1016 | local a |
| 1017 | emit 127 |
| 1018 | local b |
| 1019 | emit 82 |
| 1020 | condtrap |
| 1021 | emit 11 |
| 1022 | end |
| 1023 | if eq s 9 |
| 1024 | inst 32 r |
| 1025 | local b |
| 1026 | emit 135 |
| 1027 | local a |
| 1028 | emit 82 |
| 1029 | condtrap |
| 1030 | end |
| 1031 | end |
| 1032 | fn binary i ulong to void |
| 1033 | s = get subs i |
| 1034 | a = get arg0 i |
| 1035 | b = get arg1 i |
| 1036 | t = get types a |
| 1037 | signed = ge t 6 |
| 1038 | if ge s 9 |
| 1039 | local b |
| 1040 | con width t |
| 1041 | emit 90 |
| 1042 | condtrap |
| 1043 | end |
| 1044 | local a |
| 1045 | local b |
| 1046 | emit binop s signed |
| 1047 | inst 33 scratch |
| 1048 | if signed |
| 1049 | if or or le s 3 eq s 4 eq s 9 |
| 1050 | if or le s 3 eq s 9 |
| 1051 | overflow i |
| 1052 | end |
| 1053 | inst 32 scratch |
| 1054 | range t true |
| 1055 | else |
| 1056 | inst 32 scratch |
| 1057 | end |
| 1058 | else |
| 1059 | inst 32 scratch |
| 1060 | norm t |
| 1061 | end |
| 1062 | store get dest i |
| 1063 | end |
| 1064 | fn compare i ulong to void |
| 1065 | s = get subs i |
| 1066 | a = get arg0 i |
| 1067 | t = get types a |
| 1068 | op = ulong 81 |
| 1069 | if eq s 2 |
| 1070 | op = 82 |
| 1071 | end |
| 1072 | if eq s 3 |
| 1073 | op = 84 |
| 1074 | end |
| 1075 | if eq s 4 |
| 1076 | op = 88 |
| 1077 | end |
| 1078 | if eq s 5 |
| 1079 | op = 86 |
| 1080 | end |
| 1081 | if eq s 6 |
| 1082 | op = 90 |
| 1083 | end |
| 1084 | if and ge s 3 ge t 6 |
| 1085 | op = sub op 1 |
| 1086 | end |
| 1087 | local a |
| 1088 | local get arg1 i |
| 1089 | emit op |
| 1090 | emit 173 |
| 1091 | store get dest i |
| 1092 | end |
| 1093 | fn unary i ulong to void |
| 1094 | s = get subs i |
| 1095 | a = get arg0 i |
| 1096 | t = get types a |
| 1097 | if eq s 1 |
| 1098 | con 0 |
| 1099 | local a |
| 1100 | emit 125 |
| 1101 | if ge t 6 |
| 1102 | inst 34 scratch |
| 1103 | con sub ulong 0 shl ulong 1 sub width t 1 |
| 1104 | emit 81 |
| 1105 | condtrap |
| 1106 | inst 32 scratch |
| 1107 | range t true |
| 1108 | else |
| 1109 | norm t |
| 1110 | end |
| 1111 | else |
| 1112 | local a |
| 1113 | if eq s 2 |
| 1114 | con 18446744073709551615 |
| 1115 | emit 133 |
| 1116 | norm t |
| 1117 | else |
| 1118 | emit 80 |
| 1119 | emit 173 |
| 1120 | end |
| 1121 | end |
| 1122 | store get dest i |
| 1123 | end |
| 1124 | fn wasmtype t ulong to ulong |
| 1125 | if or eq t 5 eq t 9 |
| 1126 | ret 126 |
| 1127 | end |
| 1128 | if eq t 13 |
| 1129 | ret 125 |
| 1130 | end |
| 1131 | if eq t 14 |
| 1132 | ret 124 |
| 1133 | end |
| 1134 | ret 127 |
| 1135 | end |
| 1136 | note Internal f32 is i64(u32 bits): wrap+reinterpret, reinterpret+extend_u. |
| 1137 | fn tof t ulong to void |
| 1138 | if eq t 13 |
| 1139 | emit 167 |
| 1140 | emit 190 |
| 1141 | else |
| 1142 | emit 191 |
| 1143 | end |
| 1144 | end |
| 1145 | fn fromf t ulong to void |
| 1146 | if eq t 13 |
| 1147 | emit 188 |
| 1148 | emit 173 |
| 1149 | else |
| 1150 | emit 189 |
| 1151 | end |
| 1152 | end |
| 1153 | fn extend t ulong to void |
| 1154 | if isflt t |
| 1155 | fromf t |
| 1156 | ret |
| 1157 | end |
| 1158 | if eq wasmtype t 127 |
| 1159 | if ge t 6 |
| 1160 | emit 172 |
| 1161 | else |
| 1162 | emit 173 |
| 1163 | end |
| 1164 | end |
| 1165 | end |
| 1166 | fn toabi t ulong to void |
| 1167 | if isflt t |
| 1168 | tof t |
| 1169 | ret |
| 1170 | end |
| 1171 | if eq wasmtype t 127 |
| 1172 | emit 167 |
| 1173 | end |
| 1174 | end |
| 1175 | fn callins i ulong to void |
| 1176 | c = get immed i |
| 1177 | for j ulong get nargs i |
| 1178 | v = get args add mul i 6 j |
| 1179 | local v |
| 1180 | toabi get types v |
| 1181 | end |
| 1182 | inst 16 c |
| 1183 | if ne get returns c 0 |
| 1184 | extend get returns c |
| 1185 | store get dest i |
| 1186 | end |
| 1187 | end |
| 1188 | fn ldf a ulong t ulong to void |
| 1189 | local a |
| 1190 | tof t |
| 1191 | end |
| 1192 | fn fop t ulong x ulong y ulong to void |
| 1193 | if eq t 13 |
| 1194 | emit x |
| 1195 | else |
| 1196 | emit y |
| 1197 | end |
| 1198 | end |
| 1199 | note Float constant: f32.const 4 or f64.const 8 little-endian bit bytes. |
| 1200 | fn fk t ulong x ulong y ulong to void |
| 1201 | v = y |
| 1202 | n = ulong 8 |
| 1203 | if eq t 13 |
| 1204 | emit 67 |
| 1205 | v = x |
| 1206 | n = 4 |
| 1207 | else |
| 1208 | emit 68 |
| 1209 | end |
| 1210 | for j ulong n |
| 1211 | emit and shr v mul j 8 255 |
| 1212 | end |
| 1213 | end |
| 1214 | fn ifl to void |
| 1215 | emit 4 |
| 1216 | emit 126 |
| 1217 | end |
| 1218 | fn fbin i ulong to void |
| 1219 | s = get subs i |
| 1220 | a = get arg0 i |
| 1221 | t = get types a |
| 1222 | ldf a t |
| 1223 | ldf get arg1 i t |
| 1224 | fop t add 145 s add 159 s |
| 1225 | fromf t |
| 1226 | store get dest i |
| 1227 | end |
| 1228 | fn fneg i ulong to void |
| 1229 | a = get arg0 i |
| 1230 | t = get types a |
| 1231 | ldf a t |
| 1232 | fop t 140 154 |
| 1233 | fromf t |
| 1234 | store get dest i |
| 1235 | end |
| 1236 | note eq ne lt le gt ge: f32 91 92 93 95 94 96; f64 adds 6. |
| 1237 | fn fcmp i ulong to void |
| 1238 | s = get subs i |
| 1239 | a = get arg0 i |
| 1240 | t = get types a |
| 1241 | op = add 90 s |
| 1242 | if eq s 4 |
| 1243 | op = 95 |
| 1244 | end |
| 1245 | if eq s 5 |
| 1246 | op = 94 |
| 1247 | end |
| 1248 | if eq s 6 |
| 1249 | op = 96 |
| 1250 | end |
| 1251 | ldf a t |
| 1252 | ldf get arg1 i t |
| 1253 | fop t op add op 6 |
| 1254 | emit 173 |
| 1255 | store get dest i |
| 1256 | end |
| 1257 | note Signed float-to-i64 without trapping: NaN 0, >= 2^63 MAX, < -2^63 MIN. |
| 1258 | note f32 bits 2^63 1593835520, -2^63 3741319168; f64 4890909195324358656, 14114281232179134464. |
| 1259 | fn fsat a ulong t ulong to void |
| 1260 | ldf a t |
| 1261 | fk t 1593835520 4890909195324358656 |
| 1262 | fop t 96 102 |
| 1263 | ifl |
| 1264 | con 9223372036854775807 |
| 1265 | emit 5 |
| 1266 | ldf a t |
| 1267 | fk t 3741319168 14114281232179134464 |
| 1268 | fop t 93 99 |
| 1269 | ifl |
| 1270 | con 9223372036854775808 |
| 1271 | emit 5 |
| 1272 | ldf a t |
| 1273 | fop t 174 176 |
| 1274 | emit 11 |
| 1275 | emit 11 |
| 1276 | end |
| 1277 | note Unsigned: not (x > 0) gives 0 (NaN too), >= 2^64 gives UINT64_MAX. |
| 1278 | note f32 bits 2^64 1602224128; f64 4895412794951729152. |
| 1279 | fn fusat a ulong t ulong to void |
| 1280 | ldf a t |
| 1281 | fk t 0 0 |
| 1282 | fop t 94 100 |
| 1283 | emit 69 |
| 1284 | ifl |
| 1285 | con 0 |
| 1286 | emit 5 |
| 1287 | ldf a t |
| 1288 | fk t 1602224128 4895412794951729152 |
| 1289 | fop t 96 102 |
| 1290 | ifl |
| 1291 | con 18446744073709551615 |
| 1292 | emit 5 |
| 1293 | ldf a t |
| 1294 | fop t 175 177 |
| 1295 | emit 11 |
| 1296 | emit 11 |
| 1297 | end |
| 1298 | note Saturate the 64-bit result to a narrow destination's own range. |
| 1299 | fn clamp u ulong to void |
| 1300 | if eq width u 64 |
| 1301 | ret |
| 1302 | end |
| 1303 | s = scratch |
| 1304 | sg = sgn u |
| 1305 | hi = mask u |
| 1306 | lo = ulong 0 |
| 1307 | if sg |
| 1308 | hi = sub shl ulong 1 sub width u 1 1 |
| 1309 | lo = sub ulong 0 shl ulong 1 sub width u 1 |
| 1310 | end |
| 1311 | inst 34 s |
| 1312 | con hi |
| 1313 | if sg |
| 1314 | emit 85 |
| 1315 | else |
| 1316 | emit 86 |
| 1317 | end |
| 1318 | ifl |
| 1319 | con hi |
| 1320 | emit 5 |
| 1321 | inst 32 s |
| 1322 | if sg |
| 1323 | con lo |
| 1324 | emit 83 |
| 1325 | ifl |
| 1326 | con lo |
| 1327 | emit 5 |
| 1328 | inst 32 s |
| 1329 | emit 11 |
| 1330 | end |
| 1331 | emit 11 |
| 1332 | end |
| 1333 | fn ftoint i ulong to void |
| 1334 | a = get arg0 i |
| 1335 | t = get types a |
| 1336 | u = get types get dest i |
| 1337 | ldf a t |
| 1338 | ldf a t |
| 1339 | fop t 92 98 |
| 1340 | ifl |
| 1341 | con 0 |
| 1342 | emit 5 |
| 1343 | if sgn u |
| 1344 | fsat a t |
| 1345 | else |
| 1346 | fusat a t |
| 1347 | end |
| 1348 | emit 11 |
| 1349 | clamp u |
| 1350 | store get dest i |
| 1351 | end |
| 1352 | fn fcast i ulong to void |
| 1353 | a = get arg0 i |
| 1354 | t = get types a |
| 1355 | u = get types get dest i |
| 1356 | if not isflt u |
| 1357 | ftoint i |
| 1358 | ret |
| 1359 | end |
| 1360 | local a |
| 1361 | if isflt t |
| 1362 | if ne t u |
| 1363 | tof t |
| 1364 | fop t 187 182 |
| 1365 | fromf u |
| 1366 | end |
| 1367 | else |
| 1368 | op = ulong 185 |
| 1369 | if eq u 13 |
| 1370 | op = 180 |
| 1371 | end |
| 1372 | if not sgn t |
| 1373 | op = add op 1 |
| 1374 | end |
| 1375 | emit op |
| 1376 | fromf u |
| 1377 | end |
| 1378 | store get dest i |
| 1379 | end |
| 1380 | note Region bounds check precedes address formation; negative indexes are huge unsigned. |
| 1381 | fn boundchk x ulong r ulong to void |
| 1382 | local x |
| 1383 | con get rcount r |
| 1384 | emit 90 |
| 1385 | condtrap |
| 1386 | end |
| 1387 | fn addr x ulong r ulong to void |
| 1388 | local x |
| 1389 | emit 167 |
| 1390 | s = lg get rtag r |
| 1391 | if ne s 0 |
| 1392 | emit 65 |
| 1393 | sleb s |
| 1394 | emit 116 |
| 1395 | end |
| 1396 | end |
| 1397 | note memarg: alignment log2 width, offset the region's static base. |
| 1398 | fn marg r ulong to void |
| 1399 | leb lg get rtag r |
| 1400 | leb get rbase r |
| 1401 | end |
| 1402 | fn ldop t ulong to ulong |
| 1403 | if eq t 2 |
| 1404 | ret 49 |
| 1405 | end |
| 1406 | if eq t 6 |
| 1407 | ret 48 |
| 1408 | end |
| 1409 | if eq t 3 |
| 1410 | ret 51 |
| 1411 | end |
| 1412 | if eq t 7 |
| 1413 | ret 50 |
| 1414 | end |
| 1415 | if or eq t 4 eq t 13 |
| 1416 | ret 53 |
| 1417 | end |
| 1418 | if eq t 8 |
| 1419 | ret 52 |
| 1420 | end |
| 1421 | ret 41 |
| 1422 | end |
| 1423 | fn stop t ulong to ulong |
| 1424 | s = lg t |
| 1425 | if eq s 0 |
| 1426 | ret 60 |
| 1427 | end |
| 1428 | if eq s 1 |
| 1429 | ret 61 |
| 1430 | end |
| 1431 | if eq s 2 |
| 1432 | ret 62 |
| 1433 | end |
| 1434 | ret 55 |
| 1435 | end |
| 1436 | fn rload i ulong to void |
| 1437 | r = get immed i |
| 1438 | x = get arg0 i |
| 1439 | boundchk x r |
| 1440 | addr x r |
| 1441 | emit ldop get rtag r |
| 1442 | marg r |
| 1443 | store get dest i |
| 1444 | end |
| 1445 | fn rstore i ulong to void |
| 1446 | r = get immed i |
| 1447 | x = get arg0 i |
| 1448 | boundchk x r |
| 1449 | addr x r |
| 1450 | local get arg1 i |
| 1451 | emit stop get rtag r |
| 1452 | marg r |
| 1453 | end |
| 1454 | note RINIT fills every element; the scratch local is the element counter. |
| 1455 | fn rinit i ulong to void |
| 1456 | r = get immed i |
| 1457 | s = scratch |
| 1458 | con 0 |
| 1459 | inst 33 s |
| 1460 | emit 2 |
| 1461 | emit 64 |
| 1462 | emit 3 |
| 1463 | emit 64 |
| 1464 | inst 32 s |
| 1465 | con get rcount r |
| 1466 | emit 90 |
| 1467 | inst 13 1 |
| 1468 | inst 32 s |
| 1469 | emit 167 |
| 1470 | if ne lg get rtag r 0 |
| 1471 | emit 65 |
| 1472 | sleb lg get rtag r |
| 1473 | emit 116 |
| 1474 | end |
| 1475 | local get arg0 i |
| 1476 | emit stop get rtag r |
| 1477 | marg r |
| 1478 | inst 32 s |
| 1479 | con 1 |
| 1480 | emit 124 |
| 1481 | inst 33 s |
| 1482 | inst 12 0 |
| 1483 | emit 11 |
| 1484 | emit 11 |
| 1485 | end |
| 1486 | fn emit7 i ulong to void |
| 1487 | o = get ops i |
| 1488 | if eq o 30 |
| 1489 | rload i |
| 1490 | end |
| 1491 | if eq o 31 |
| 1492 | rstore i |
| 1493 | end |
| 1494 | if eq o 32 |
| 1495 | local get arg0 i |
| 1496 | norm get types get dest i |
| 1497 | store get dest i |
| 1498 | end |
| 1499 | if eq o 33 |
| 1500 | rinit i |
| 1501 | end |
| 1502 | end |
| 1503 | fn emitins i ulong fi ulong to void |
| 1504 | o = get ops i |
| 1505 | a = get arg0 i |
| 1506 | fl = false |
| 1507 | if ne a 65535 |
| 1508 | fl = isflt get types a |
| 1509 | end |
| 1510 | if eq o 1 |
| 1511 | con get immed i |
| 1512 | store get dest i |
| 1513 | end |
| 1514 | if eq o 2 |
| 1515 | local get arg0 i |
| 1516 | store get dest i |
| 1517 | end |
| 1518 | if eq o 3 |
| 1519 | if fl |
| 1520 | fbin i |
| 1521 | else |
| 1522 | binary i |
| 1523 | end |
| 1524 | end |
| 1525 | if eq o 4 |
| 1526 | if fl |
| 1527 | fneg i |
| 1528 | else |
| 1529 | unary i |
| 1530 | end |
| 1531 | end |
| 1532 | if eq o 5 |
| 1533 | if fl |
| 1534 | fcmp i |
| 1535 | else |
| 1536 | compare i |
| 1537 | end |
| 1538 | end |
| 1539 | if eq o 6 |
| 1540 | if or fl isflt get types get dest i |
| 1541 | fcast i |
| 1542 | else |
| 1543 | local get arg0 i |
| 1544 | range get types get dest i ge get types get arg0 i 6 |
| 1545 | store get dest i |
| 1546 | end |
| 1547 | end |
| 1548 | if ge o 30 |
| 1549 | emit7 i |
| 1550 | end |
| 1551 | if eq o 11 |
| 1552 | callins i |
| 1553 | end |
| 1554 | if eq o 12 |
| 1555 | emit 0 |
| 1556 | end |
| 1557 | if eq o 13 |
| 1558 | emit 65 |
| 1559 | sleb get immed i |
| 1560 | inst 33 add get state 3 get state 4 |
| 1561 | inst 12 1 |
| 1562 | end |
| 1563 | if eq o 14 |
| 1564 | emit 65 |
| 1565 | sleb get arg1 i |
| 1566 | inst 33 add get state 3 get state 4 |
| 1567 | local get arg0 i |
| 1568 | emit 167 |
| 1569 | emit 69 |
| 1570 | inst 13 1 |
| 1571 | emit 65 |
| 1572 | sleb get immed i |
| 1573 | inst 33 add get state 3 get state 4 |
| 1574 | inst 12 1 |
| 1575 | end |
| 1576 | if eq o 15 |
| 1577 | if ne get returns fi 0 |
| 1578 | local get arg0 i |
| 1579 | toabi get returns fi |
| 1580 | end |
| 1581 | emit 15 |
| 1582 | end |
| 1583 | end |
| 1584 | fn body fi ulong to void |
| 1585 | loadfn fi |
| 1586 | defined |
| 1587 | h = hole |
| 1588 | emit 3 |
| 1589 | leb get state 4 |
| 1590 | emit 126 |
| 1591 | leb 1 |
| 1592 | emit 127 |
| 1593 | leb 1 |
| 1594 | emit 126 |
| 1595 | for j ulong get state 3 |
| 1596 | inst 32 j |
| 1597 | extend get types j |
| 1598 | if not isflt get types j |
| 1599 | norm get types j |
| 1600 | end |
| 1601 | store j |
| 1602 | end |
| 1603 | emit 2 |
| 1604 | emit 64 |
| 1605 | emit 3 |
| 1606 | emit 64 |
| 1607 | for b ulong get state 5 |
| 1608 | inst 32 add get state 3 get state 4 |
| 1609 | emit 65 |
| 1610 | sleb b |
| 1611 | emit 70 |
| 1612 | emit 4 |
| 1613 | emit 64 |
| 1614 | for j ulong get counts b |
| 1615 | emitins add get first b j fi |
| 1616 | end |
| 1617 | emit 11 |
| 1618 | end |
| 1619 | emit 0 |
| 1620 | emit 11 |
| 1621 | emit 11 |
| 1622 | emit 0 |
| 1623 | emit 11 |
| 1624 | patch h |
| 1625 | end |
| 1626 | fn module to void |
| 1627 | emit 0 |
| 1628 | emit 97 |
| 1629 | emit 115 |
| 1630 | emit 109 |
| 1631 | emit 1 |
| 1632 | emit 0 |
| 1633 | emit 0 |
| 1634 | emit 0 |
| 1635 | emit 1 |
| 1636 | h = hole |
| 1637 | leb get state 7 |
| 1638 | for f ulong get state 7 |
| 1639 | emit 96 |
| 1640 | leb get params f |
| 1641 | for j ulong get params f |
| 1642 | emit wasmtype get sigs add mul f 6 j |
| 1643 | end |
| 1644 | if eq get returns f 0 |
| 1645 | leb 0 |
| 1646 | else |
| 1647 | leb 1 |
| 1648 | emit wasmtype get returns f |
| 1649 | end |
| 1650 | end |
| 1651 | patch h |
| 1652 | emit 3 |
| 1653 | h = hole |
| 1654 | leb get state 7 |
| 1655 | for f ulong get state 7 |
| 1656 | leb f |
| 1657 | end |
| 1658 | patch h |
| 1659 | if eq get state 8 7 |
| 1660 | memsec |
| 1661 | globsec |
| 1662 | end |
| 1663 | emit 7 |
| 1664 | h = hole |
| 1665 | leb add get state 7 nexp |
| 1666 | for f ulong get state 7 |
| 1667 | leb get namelen f |
| 1668 | for j ulong get namelen f |
| 1669 | emit ulong get input add get names f j |
| 1670 | end |
| 1671 | emit 0 |
| 1672 | leb f |
| 1673 | end |
| 1674 | if eq get state 8 7 |
| 1675 | expmore |
| 1676 | end |
| 1677 | patch h |
| 1678 | emit 10 |
| 1679 | h = hole |
| 1680 | leb get state 7 |
| 1681 | for f ulong get state 7 |
| 1682 | body f |
| 1683 | end |
| 1684 | patch h |
| 1685 | if eq get state 8 7 |
| 1686 | datasec |
| 1687 | abisec |
| 1688 | end |
| 1689 | end |
| 1690 | fn ismod r ulong to bool |
| 1691 | ret ne get rclass r 1 |
| 1692 | end |
| 1693 | fn nmods to ulong |
| 1694 | c = ulong 0 |
| 1695 | for r ulong get state 9 |
| 1696 | if ismod r |
| 1697 | c = add c 1 |
| 1698 | end |
| 1699 | end |
| 1700 | ret c |
| 1701 | end |
| 1702 | note v1 exports functions only; v7 adds memory and one global per module region. |
| 1703 | fn nexp to ulong |
| 1704 | if eq get state 8 7 |
| 1705 | ret add 1 nmods |
| 1706 | end |
| 1707 | ret 0 |
| 1708 | end |
| 1709 | note Memory limits flag 1: min == max == the exact page count; never grown. |
| 1710 | fn memsec to void |
| 1711 | emit 5 |
| 1712 | h = hole |
| 1713 | leb 1 |
| 1714 | emit 1 |
| 1715 | leb get state 10 |
| 1716 | leb get state 10 |
| 1717 | patch h |
| 1718 | end |
| 1719 | fn globsec to void |
| 1720 | if eq nmods 0 |
| 1721 | ret |
| 1722 | end |
| 1723 | emit 6 |
| 1724 | h = hole |
| 1725 | leb nmods |
| 1726 | for r ulong get state 9 |
| 1727 | if ismod r |
| 1728 | emit 127 |
| 1729 | emit 0 |
| 1730 | emit 65 |
| 1731 | sleb get rbase r |
| 1732 | emit 11 |
| 1733 | end |
| 1734 | end |
| 1735 | patch h |
| 1736 | end |
| 1737 | fn rnameout r ulong to void |
| 1738 | for j ulong get rnlen r |
| 1739 | emit ulong get input add get rname r j |
| 1740 | end |
| 1741 | end |
| 1742 | fn expmore to void |
| 1743 | leb len memname |
| 1744 | for j ulong len memname |
| 1745 | emit ulong get memname j |
| 1746 | end |
| 1747 | emit 2 |
| 1748 | leb 0 |
| 1749 | g = ulong 0 |
| 1750 | for r ulong get state 9 |
| 1751 | if ismod r |
| 1752 | leb add len regpfx get rnlen r |
| 1753 | for j ulong len regpfx |
| 1754 | emit ulong get regpfx j |
| 1755 | end |
| 1756 | rnameout r |
| 1757 | emit 3 |
| 1758 | leb g |
| 1759 | g = add g 1 |
| 1760 | end |
| 1761 | end |
| 1762 | end |
| 1763 | fn ntext to ulong |
| 1764 | c = ulong 0 |
| 1765 | for r ulong get state 9 |
| 1766 | if and eq get rclass r 2 ne get rcount r 0 |
| 1767 | c = add c 1 |
| 1768 | end |
| 1769 | end |
| 1770 | ret c |
| 1771 | end |
| 1772 | note Text regions become active data segments at their static offsets. |
| 1773 | fn segment r ulong to void |
| 1774 | emit 0 |
| 1775 | emit 65 |
| 1776 | sleb get rbase r |
| 1777 | emit 11 |
| 1778 | leb get rcount r |
| 1779 | p = get rdata r |
| 1780 | for j ulong get rcount r |
| 1781 | emit ulong get input add p j |
| 1782 | end |
| 1783 | end |
| 1784 | fn datasec to void |
| 1785 | n = ntext |
| 1786 | if eq n 0 |
| 1787 | ret |
| 1788 | end |
| 1789 | emit 11 |
| 1790 | h = hole |
| 1791 | leb n |
| 1792 | for r ulong get state 9 |
| 1793 | if and eq get rclass r 2 ne get rcount r 0 |
| 1794 | segment r |
| 1795 | end |
| 1796 | end |
| 1797 | patch h |
| 1798 | end |
| 1799 | note Custom section osc.v7.abi: IR signatures and module region tags/counts so a |
| 1800 | note host executor prints signedness and region bytes. Engines ignore it. |
| 1801 | fn abisec to void |
| 1802 | emit 0 |
| 1803 | h = hole |
| 1804 | leb len abiname |
| 1805 | for j ulong len abiname |
| 1806 | emit ulong get abiname j |
| 1807 | end |
| 1808 | leb get state 7 |
| 1809 | for f ulong get state 7 |
| 1810 | leb get namelen f |
| 1811 | for j ulong get namelen f |
| 1812 | emit ulong get input add get names f j |
| 1813 | end |
| 1814 | leb get params f |
| 1815 | for j ulong get params f |
| 1816 | leb get sigs add mul f 6 j |
| 1817 | end |
| 1818 | leb get returns f |
| 1819 | end |
| 1820 | leb nmods |
| 1821 | for r ulong get state 9 |
| 1822 | if ismod r |
| 1823 | leb get rnlen r |
| 1824 | rnameout r |
| 1825 | leb get rtag r |
| 1826 | leb get rclass r |
| 1827 | leb get rcount r |
| 1828 | end |
| 1829 | end |
| 1830 | patch h |
| 1831 | end |
| 1832 | note A v1 unit stays capped at 2620251 bytes; only a v7 header admits more. |
| 1833 | fn hdr7 to bool |
| 1834 | for j ulong len magic6 |
| 1835 | if ne get input j get magic6 j |
| 1836 | ret false |
| 1837 | end |
| 1838 | end |
| 1839 | ret and eq get input 6 0 eq get input 7 7 |
| 1840 | end |
| 1841 | fn sizechk n ulong to void |
| 1842 | if le n 2620251 |
| 1843 | ret |
| 1844 | end |
| 1845 | if not hdr7 |
| 1846 | refuse 23 |
| 1847 | end |
| 1848 | end |
| 1849 | fn version to void |
| 1850 | v = rd 1 |
| 1851 | if eq v 7 |
| 1852 | put state 8 7 |
| 1853 | n = rd 1 |
| 1854 | if gt n 16 |
| 1855 | refuse 23 |
| 1856 | end |
| 1857 | if ne n 0 |
| 1858 | refuse 25 |
| 1859 | end |
| 1860 | regions |
| 1861 | ret |
| 1862 | end |
| 1863 | if ne v 1 |
| 1864 | refuse 21 |
| 1865 | end |
| 1866 | put state 8 1 |
| 1867 | end |
| 1868 | fn validate to void |
| 1869 | defined |
| 1870 | if eq get state 8 7 |
| 1871 | inited |
| 1872 | end |
| 1873 | end |
| 1874 | fn ownchk r ulong to void |
| 1875 | if eq get rclass r 1 |
| 1876 | need lt get rowner r get state 7 |
| 1877 | else |
| 1878 | need eq get rowner r 65535 |
| 1879 | end |
| 1880 | end |
| 1881 | fn fnmem f ulong to bool |
| 1882 | if ne get namelen f len memname |
| 1883 | ret false |
| 1884 | end |
| 1885 | for j ulong len memname |
| 1886 | if ne get input add get names f j get memname j |
| 1887 | ret false |
| 1888 | end |
| 1889 | end |
| 1890 | ret true |
| 1891 | end |
| 1892 | fn fnreg f ulong r ulong to bool |
| 1893 | n = get rnlen r |
| 1894 | if ne get namelen f add n len regpfx |
| 1895 | ret false |
| 1896 | end |
| 1897 | p = get names f |
| 1898 | for j ulong len regpfx |
| 1899 | if ne get input add p j get regpfx j |
| 1900 | ret false |
| 1901 | end |
| 1902 | end |
| 1903 | p = add p len regpfx |
| 1904 | q = get rname r |
| 1905 | for j ulong n |
| 1906 | if ne get input add p j get input add q j |
| 1907 | ret false |
| 1908 | end |
| 1909 | end |
| 1910 | ret true |
| 1911 | end |
| 1912 | note Region owners, duplicate or shadowing names and computed export collisions. |
| 1913 | note Equal names are legal only for two local regions of different functions. |
| 1914 | fn rdup q ulong r ulong to void |
| 1915 | if rsame q r |
| 1916 | lq = eq get rclass q 1 |
| 1917 | lr = eq get rclass r 1 |
| 1918 | need and and lq lr ne get rowner q get rowner r |
| 1919 | end |
| 1920 | end |
| 1921 | fn postchk to void |
| 1922 | nr = get state 9 |
| 1923 | for r ulong nr |
| 1924 | ownchk r |
| 1925 | for q ulong r |
| 1926 | rdup q r |
| 1927 | end |
| 1928 | end |
| 1929 | for f ulong get state 7 |
| 1930 | need not fnmem f |
| 1931 | for r ulong nr |
| 1932 | if ismod r |
| 1933 | need not fnreg f r |
| 1934 | end |
| 1935 | end |
| 1936 | end |
| 1937 | end |
| 1938 | note Static backing in table order, each aligned to its element width; the |
| 1939 | note total is capped at 2^31 bytes = 32768 pages so every base fits wasm32 i32.const. |
| 1940 | fn layout to void |
| 1941 | off = ulong 0 |
| 1942 | for r ulong get state 9 |
| 1943 | w = div width get rtag r 8 |
| 1944 | off = mul div add off sub w 1 w w |
| 1945 | put rbase r off |
| 1946 | off = add off mul get rcount r w |
| 1947 | if gt off 2147483648 |
| 1948 | refuse 23 |
| 1949 | end |
| 1950 | end |
| 1951 | put state 10 div add off 65535 65536 |
| 1952 | end |
| 1953 | fn tail to void |
| 1954 | v7 = eq get state 8 7 |
| 1955 | if v7 |
| 1956 | postchk |
| 1957 | end |
| 1958 | if ne get state 0 get state 1 |
| 1959 | refuse 27 |
| 1960 | end |
| 1961 | if v7 |
| 1962 | if eq get state 11 0 |
| 1963 | refuse 21 |
| 1964 | end |
| 1965 | layout |
| 1966 | end |
| 1967 | end |
| 1968 | fn main to int |
| 1969 | n = ulong 0 |
| 1970 | for attempt ulong 8388609 |
| 1971 | if eq n len input |
| 1972 | refuse 23 |
| 1973 | end |
| 1974 | k = read 0 input n sub len input n |
| 1975 | if lt k 0 |
| 1976 | refuse 28 |
| 1977 | end |
| 1978 | if eq k 0 |
| 1979 | break |
| 1980 | end |
| 1981 | n = add n ulong k |
| 1982 | end |
| 1983 | if eq n len input |
| 1984 | refuse 23 |
| 1985 | end |
| 1986 | put state 1 n |
| 1987 | sizechk n |
| 1988 | magic = new uchar 7 0 |
| 1989 | put magic 0 79 |
| 1990 | put magic 1 83 |
| 1991 | put magic 2 67 |
| 1992 | put magic 3 49 |
| 1993 | put magic 4 73 |
| 1994 | put magic 5 82 |
| 1995 | for j ulong 7 |
| 1996 | v = rd 1 |
| 1997 | if ne v ulong get magic j |
| 1998 | refuse 20 |
| 1999 | end |
| 2000 | end |
| 2001 | version |
| 2002 | nf = cap rd 2 32 |
| 2003 | put state 7 nf |
| 2004 | for f ulong nf |
| 2005 | put starts f get state 0 |
| 2006 | loadfn f |
| 2007 | validate |
| 2008 | end |
| 2009 | tail |
| 2010 | module |
| 2011 | p = ulong 0 |
| 2012 | for attempt ulong 8388608 |
| 2013 | if eq p get state 2 |
| 2014 | break |
| 2015 | end |
| 2016 | k = write 1 output p sub get state 2 p |
| 2017 | if le k 0 |
| 2018 | refuse 28 |
| 2019 | end |
| 2020 | p = add p ulong k |
| 2021 | end |
| 2022 | ret 0 |
| 2023 | end |