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 / ng / ngrun.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/ngrun.om: the ΩIR reference interpreter (lane ng-ir). Build: |
| 3 | note cat seed/ng/src/ir.om seed/ng/ngrun.om | seed/out/omc > ngrun |
| 4 | note Usage: ngrun FILE.ir [STEPS]. It runs the program entry (IR.md s6.1) of a unit that ngcheck accepts, with |
| 5 | note the meaning of IR.md s7, one op at a time: host words reach the real process (stdout, stderr, |
| 6 | note stdin), the exit status is the entry's i32 result, `exit v` exits v, a failing check or `trap` |
| 7 | note exits 100 + code. Call depth past 65535 frames, more than 4194304 live values or more than |
| 8 | note 16 MiB of memory (globals plus alloca) trap 6 (IR.md s7.9: call and alloca may trap 6). |
| 9 | note It is the oracle of the differential gate (D9): no optimization, nothing shared with a back end; |
| 10 | note it shares only ir.om's reader, which the ΩIR gate checks byte for byte. |
| 11 | note Outside the supported subset it prints one stderr line "ngrun: unsupported op N at OFFSET" (or |
| 12 | note "unsupported host word N") and exits 125; OFFSET is the op's byte offset in the file. |
| 13 | note A file over 16777216 bytes is "ngrun: refused class 23 at 16777216" and exit 23; an unreadable one |
| 14 | note "ngrun: cannot read input" and exit 27. |
| 15 | note Supported: const select, integer and f32/f64 arithmetic, compare, conv, bitcast (10..37), every |
| 16 | note check (70..82), facts (90..93, no effect), load store atom atomcas alloca fill gaddr len slice |
| 17 | note fload fstore fref on scalar elements, br cbr ret retv call trap exit, host read write monons |
| 18 | note realns cpufence cpurelax; fused f32/f64 fma (46); every f32/f64 conv rounding/saturation mode; |
| 19 | note packed 16-byte float vectors vec f32 4 and vec f64 2 on vload vstore vsplat vextract add sub mul fma. |
| 20 | note Not yet: other vectors and tiles (102..106), kernels (40, 41, 44, 45, 67), fmath (28), |
| 21 | note f16/bf16/fp8. |
| 22 | note Its own limits never exit 100..114 (those are the IR's traps): after STEPS executed ops (default 2^36 = |
| 23 | note 68719476736, about an hour on the Spark, above the 7.8e9 of the sort kernel; a loop without chk.fuel |
| 24 | note may run forever) it prints "ngrun: step limit at OFFSET" (the next |
| 25 | note op) and exits 124; an access whose element lies outside the object its REF or SLICE names (only an |
| 26 | note input ngcheck refuses, UNCHECKED, can reach one) prints "ngrun: access outside storage at OFFSET" and |
| 27 | note exits 126. Other input that ngcheck refuses has no meaning here: ngrun reads it with ir.om and its |
| 28 | note exit status is not an IR result; judge a run only on a unit ngcheck accepts. |
| 29 | note ---- state ------------------------------------------------------------------------------------ |
| 30 | note r_s: 0 slot offset (slot = r_s0 + value handle), 1 current function, 2 memory top, 3 call |
| 31 | note depth, 4 value stack top, 5 step budget, 6 steps run |
| 32 | global r_s ulong 8 |
| 33 | global r_v ulong 4194304 |
| 34 | global r_w ulong 4194304 |
| 35 | global r_m uchar 16777216 |
| 36 | global r_pm uchar 16777216 |
| 37 | global r_pb ulong 65537 |
| 38 | global r_pl ulong 65537 |
| 39 | global r_ps ulong 1 |
| 40 | global r_ga ulong 65537 |
| 41 | global r_cf uint 65536 |
| 42 | global r_co uint 65536 |
| 43 | global r_cs ulong 65536 |
| 44 | global r_cm ulong 65536 |
| 45 | global r_ct ulong 65536 |
| 46 | global r_tv ulong 64 |
| 47 | global r_tw ulong 64 |
| 48 | global r_dg uchar 24 |
| 49 | global r_pa uchar 4096 |
| 50 | text r_msg "ngrun: unsupported op \n at \nngrun: unsupported host word \nngrun: refused class \nngrun: usage: ngrun FILE.ir [STEPS]\nngrun: cannot read input\nngrun: step limit at \nngrun: access outside storage at \n" |
| 51 | note ---- messages --------------------------------------------------------------------------------- |
| 52 | fn rn_say a ulong b ulong to void |
| 53 | k = write 2 r_msg a sub b a |
| 54 | end |
| 55 | fn rn_num v ulong to void |
| 56 | x = v |
| 57 | i = ulong 24 |
| 58 | for j ulong 21 |
| 59 | i = sub i 1 |
| 60 | put r_dg i uchar add 48 rem x 10 |
| 61 | x = div x 10 |
| 62 | if eq x 0 |
| 63 | k = write 2 r_dg i sub 24 i |
| 64 | ret |
| 65 | end |
| 66 | end |
| 67 | end |
| 68 | fn rn_nl to void |
| 69 | put r_dg 0 10 |
| 70 | k = write 2 r_dg 0 1 |
| 71 | end |
| 72 | note one stderr line, message r_msg[a, b) then the offset of op o |
| 73 | fn rn_lim a ulong b ulong o uint to void |
| 74 | rn_say a b |
| 75 | rn_num ulong get i_oof o |
| 76 | rn_nl |
| 77 | end |
| 78 | note an access outside its object (input ngcheck refuses): one line, exit 126 |
| 79 | fn rn_oob o uint to void |
| 80 | rn_lim 163 196 o |
| 81 | exit 126 |
| 82 | end |
| 83 | note elements [off, off + n) of operand slot k lie inside its object, else rn_oob |
| 84 | fn rn_ext o uint k uint off ulong n ulong to void |
| 85 | l = rn_len o k |
| 86 | if gt off l |
| 87 | rn_oob o |
| 88 | end |
| 89 | if gt n sub l off |
| 90 | rn_oob o |
| 91 | end |
| 92 | end |
| 93 | note an op outside the supported subset: one line, exit 125 |
| 94 | fn rn_uns o uint to void |
| 95 | op = ulong get i_oop o |
| 96 | if eq op 110 |
| 97 | rn_say 28 57 |
| 98 | rn_num get i_oim o |
| 99 | else |
| 100 | rn_say 0 22 |
| 101 | rn_num op |
| 102 | end |
| 103 | rn_say 23 27 |
| 104 | rn_num ulong get i_oof o |
| 105 | rn_nl |
| 106 | exit 125 |
| 107 | end |
| 108 | note ---- values ----------------------------------------------------------------------------------- |
| 109 | fn rn_g h uint to ulong |
| 110 | ret get r_v add get r_s 0 ulong h |
| 111 | end |
| 112 | fn rn_gw h uint to ulong |
| 113 | ret get r_w add get r_s 0 ulong h |
| 114 | end |
| 115 | fn rn_p h uint x ulong to void |
| 116 | put r_v add get r_s 0 ulong h x |
| 117 | end |
| 118 | fn rn_pw h uint x ulong to void |
| 119 | put r_w add get r_s 0 ulong h x |
| 120 | end |
| 121 | note value and type of operand slot k of op o |
| 122 | fn rn_a o uint k uint to ulong |
| 123 | ret rn_g ir_arg o k |
| 124 | end |
| 125 | fn rn_aw o uint k uint to ulong |
| 126 | ret rn_gw ir_arg o k |
| 127 | end |
| 128 | fn rn_at o uint k uint to uint |
| 129 | ret get i_vty ir_arg o k |
| 130 | end |
| 131 | note set op o's result (both words) |
| 132 | fn rn_r o uint x ulong to void |
| 133 | h = get i_ors o |
| 134 | if ne h 0 |
| 135 | rn_p h x |
| 136 | rn_pw h 0 |
| 137 | end |
| 138 | end |
| 139 | fn rn_rw o uint y ulong to void |
| 140 | h = get i_ors o |
| 141 | if ne h 0 |
| 142 | rn_pw h y |
| 143 | end |
| 144 | end |
| 145 | note ---- scalar types ----------------------------------------------------------------------------- |
| 146 | fn rn_w t uint to ulong |
| 147 | if eq t 1 |
| 148 | ret 1 |
| 149 | end |
| 150 | if or eq t 2 eq t 6 |
| 151 | ret 8 |
| 152 | end |
| 153 | if or eq t 3 eq t 7 |
| 154 | ret 16 |
| 155 | end |
| 156 | if or eq t 4 or eq t 8 eq t 13 |
| 157 | ret 32 |
| 158 | end |
| 159 | ret 64 |
| 160 | end |
| 161 | fn rn_sg t uint to bool |
| 162 | ret and ge t 6 le t 9 |
| 163 | end |
| 164 | fn rn_isi t uint to bool |
| 165 | ret and ge t 2 le t 9 |
| 166 | end |
| 167 | fn rn_isf t uint to bool |
| 168 | ret or eq t 13 eq t 14 |
| 169 | end |
| 170 | note the canonical form of x as type t: zero-extended when unsigned, sign-extended when signed |
| 171 | fn rn_cn t uint x ulong to ulong |
| 172 | w = rn_w t |
| 173 | if eq w 64 |
| 174 | ret x |
| 175 | end |
| 176 | y = and x sub shl ulong 1 w 1 |
| 177 | if rn_sg t |
| 178 | s = shl ulong 1 sub w 1 |
| 179 | ret sub xor y s s |
| 180 | end |
| 181 | ret y |
| 182 | end |
| 183 | fn rn_min t uint to ulong |
| 184 | ret rn_cn t shl ulong 1 sub rn_w t 1 |
| 185 | end |
| 186 | fn rn_lt t uint a ulong b ulong to bool |
| 187 | if rn_sg t |
| 188 | ret lt bitcast long a bitcast long b |
| 189 | end |
| 190 | ret lt a b |
| 191 | end |
| 192 | note ---- integer arithmetic (IR.md s7.3: total) ----------------------------------------------------- |
| 193 | fn rn_div t uint a ulong b ulong to ulong |
| 194 | if eq b 0 |
| 195 | ret 0 |
| 196 | end |
| 197 | if rn_sg t |
| 198 | if and eq a rn_min t eq b sub ulong 0 1 |
| 199 | ret a |
| 200 | end |
| 201 | ret rn_cn t bitcast ulong div bitcast long a bitcast long b |
| 202 | end |
| 203 | ret div a b |
| 204 | end |
| 205 | fn rn_rem t uint a ulong b ulong to ulong |
| 206 | if eq b 0 |
| 207 | ret 0 |
| 208 | end |
| 209 | if rn_sg t |
| 210 | if eq b sub ulong 0 1 |
| 211 | ret 0 |
| 212 | end |
| 213 | ret rn_cn t bitcast ulong rem bitcast long a bitcast long b |
| 214 | end |
| 215 | ret rem a b |
| 216 | end |
| 217 | fn rn_rot t uint a ulong b ulong l bool to ulong |
| 218 | w = rn_w t |
| 219 | s = and b sub w 1 |
| 220 | if eq s 0 |
| 221 | ret a |
| 222 | end |
| 223 | if not l |
| 224 | s = sub w s |
| 225 | end |
| 226 | ret rn_cn t or shl a s shr a sub w s |
| 227 | end |
| 228 | fn rn_ia op uint t uint a ulong b ulong to ulong |
| 229 | if eq op 10 |
| 230 | ret rn_cn t add a b |
| 231 | end |
| 232 | if eq op 11 |
| 233 | ret rn_cn t sub a b |
| 234 | end |
| 235 | if eq op 12 |
| 236 | ret rn_cn t mul a b |
| 237 | end |
| 238 | if eq op 13 |
| 239 | ret rn_div t a b |
| 240 | end |
| 241 | if eq op 14 |
| 242 | ret rn_rem t a b |
| 243 | end |
| 244 | if eq op 15 |
| 245 | ret rn_cn t sub ulong 0 a |
| 246 | end |
| 247 | if eq op 16 |
| 248 | ret and a b |
| 249 | end |
| 250 | if eq op 17 |
| 251 | ret or a b |
| 252 | end |
| 253 | if eq op 18 |
| 254 | ret xor a b |
| 255 | end |
| 256 | if eq op 19 |
| 257 | ret rn_cn t not a |
| 258 | end |
| 259 | s = and b sub rn_w t 1 |
| 260 | if eq op 20 |
| 261 | ret rn_cn t shl a s |
| 262 | end |
| 263 | if eq op 21 |
| 264 | if rn_sg t |
| 265 | ret bitcast ulong shr bitcast long a bitcast long s |
| 266 | end |
| 267 | ret shr a s |
| 268 | end |
| 269 | if eq op 22 |
| 270 | ret rn_rot t a b true |
| 271 | end |
| 272 | if eq op 23 |
| 273 | ret rn_rot t a b false |
| 274 | end |
| 275 | if eq op 24 |
| 276 | if rn_lt t b a |
| 277 | ret b |
| 278 | end |
| 279 | ret a |
| 280 | end |
| 281 | if rn_lt t a b |
| 282 | ret b |
| 283 | end |
| 284 | ret a |
| 285 | end |
| 286 | fn rn_icmp op uint t uint a ulong b ulong to bool |
| 287 | if eq op 30 |
| 288 | ret eq a b |
| 289 | end |
| 290 | if eq op 31 |
| 291 | ret ne a b |
| 292 | end |
| 293 | if eq op 32 |
| 294 | ret rn_lt t a b |
| 295 | end |
| 296 | if eq op 33 |
| 297 | ret not rn_lt t b a |
| 298 | end |
| 299 | if eq op 34 |
| 300 | ret rn_lt t b a |
| 301 | end |
| 302 | ret not rn_lt t a b |
| 303 | end |
| 304 | note ---- floats: raw bits in the low 32 (f32) or 64 (f64) bits of a value ---------------------------- |
| 305 | fn rn_tof t uint a ulong to f64 |
| 306 | if eq t 13 |
| 307 | ret f64 bitcast f32 uint a |
| 308 | end |
| 309 | ret bitcast f64 a |
| 310 | end |
| 311 | fn rn_f64 op uint a ulong b ulong to ulong |
| 312 | x = bitcast f64 a |
| 313 | y = bitcast f64 b |
| 314 | if eq op 10 |
| 315 | ret bitcast ulong add x y |
| 316 | end |
| 317 | if eq op 11 |
| 318 | ret bitcast ulong sub x y |
| 319 | end |
| 320 | if eq op 12 |
| 321 | ret bitcast ulong mul x y |
| 322 | end |
| 323 | if eq op 13 |
| 324 | ret bitcast ulong div x y |
| 325 | end |
| 326 | if eq op 26 |
| 327 | ret bitcast ulong fsqrt x |
| 328 | end |
| 329 | ret 0 |
| 330 | end |
| 331 | fn rn_f32 op uint a ulong b ulong to ulong |
| 332 | x = bitcast f32 uint a |
| 333 | y = bitcast f32 uint b |
| 334 | if eq op 10 |
| 335 | ret ulong bitcast uint add x y |
| 336 | end |
| 337 | if eq op 11 |
| 338 | ret ulong bitcast uint sub x y |
| 339 | end |
| 340 | if eq op 12 |
| 341 | ret ulong bitcast uint mul x y |
| 342 | end |
| 343 | if eq op 13 |
| 344 | ret ulong bitcast uint div x y |
| 345 | end |
| 346 | if eq op 26 |
| 347 | ret ulong bitcast uint fsqrt x |
| 348 | end |
| 349 | ret 0 |
| 350 | end |
| 351 | note min and max (IR.md s7.3, OMEGESE fmin/fmax, FMINNM/FMAXNM): one quiet NaN gives the other operand; |
| 352 | note a signaling NaN in either operand gives that operand quieted (a first); two quiet NaNs give a; -0 < +0 |
| 353 | fn rn_fmm op uint t uint a ulong b ulong to ulong |
| 354 | x = rn_tof t a |
| 355 | y = rn_tof t b |
| 356 | q = ulong 0x0008000000000000 |
| 357 | if eq t 13 |
| 358 | q = 0x00400000 |
| 359 | end |
| 360 | na = ne x x |
| 361 | nb = ne y y |
| 362 | if or na nb |
| 363 | sa = and na eq and a q 0 |
| 364 | sb = and nb eq and b q 0 |
| 365 | if sa |
| 366 | ret or a q |
| 367 | end |
| 368 | if sb |
| 369 | ret or b q |
| 370 | end |
| 371 | if and na nb |
| 372 | ret or a q |
| 373 | end |
| 374 | if na |
| 375 | ret b |
| 376 | end |
| 377 | ret a |
| 378 | end |
| 379 | if eq op 24 |
| 380 | if lt x y |
| 381 | ret a |
| 382 | end |
| 383 | if lt y x |
| 384 | ret b |
| 385 | end |
| 386 | ret or a b |
| 387 | end |
| 388 | if gt x y |
| 389 | ret a |
| 390 | end |
| 391 | if gt y x |
| 392 | ret b |
| 393 | end |
| 394 | ret and a b |
| 395 | end |
| 396 | fn rn_fa op uint t uint a ulong b ulong to ulong |
| 397 | sb = shl ulong 1 sub rn_w t 1 |
| 398 | if eq op 15 |
| 399 | ret xor a sb |
| 400 | end |
| 401 | if eq op 27 |
| 402 | ret and a sub sb 1 |
| 403 | end |
| 404 | if or eq op 24 eq op 25 |
| 405 | ret rn_fmm op t a b |
| 406 | end |
| 407 | if eq t 13 |
| 408 | ret rn_f32 op a b |
| 409 | end |
| 410 | ret rn_f64 op a b |
| 411 | end |
| 412 | fn rn_fcmp op uint t uint a ulong b ulong to bool |
| 413 | x = rn_tof t a |
| 414 | y = rn_tof t b |
| 415 | if eq op 30 |
| 416 | ret eq x y |
| 417 | end |
| 418 | if eq op 31 |
| 419 | ret ne x y |
| 420 | end |
| 421 | if eq op 32 |
| 422 | ret lt x y |
| 423 | end |
| 424 | if eq op 33 |
| 425 | ret le x y |
| 426 | end |
| 427 | if eq op 34 |
| 428 | ret gt x y |
| 429 | end |
| 430 | ret ge x y |
| 431 | end |
| 432 | fn rn_b2u c bool to ulong |
| 433 | if c |
| 434 | ret 1 |
| 435 | end |
| 436 | ret 0 |
| 437 | end |
| 438 | note ---- conv and bitcast (IR.md s7.3) ------------------------------------------------------------- |
| 439 | note float x to int type t: truncate toward zero, saturate, NaN 0 (omc's conversion has that meaning) |
| 440 | fn rn_f2i t uint x f64 to ulong |
| 441 | if eq t 2 |
| 442 | ret ulong uchar x |
| 443 | end |
| 444 | if eq t 3 |
| 445 | ret ulong ushort x |
| 446 | end |
| 447 | if eq t 4 |
| 448 | ret ulong uint x |
| 449 | end |
| 450 | if eq t 5 |
| 451 | ret ulong x |
| 452 | end |
| 453 | if eq t 6 |
| 454 | ret bitcast ulong long byte x |
| 455 | end |
| 456 | if eq t 7 |
| 457 | ret bitcast ulong long short x |
| 458 | end |
| 459 | if eq t 8 |
| 460 | ret bitcast ulong long int x |
| 461 | end |
| 462 | ret bitcast ulong long x |
| 463 | end |
| 464 | fn rn_i2f t uint s uint a ulong to ulong |
| 465 | if eq t 14 |
| 466 | if rn_sg s |
| 467 | ret bitcast ulong f64 bitcast long a |
| 468 | end |
| 469 | ret bitcast ulong f64 a |
| 470 | end |
| 471 | if rn_sg s |
| 472 | ret ulong bitcast uint f32 bitcast long a |
| 473 | end |
| 474 | ret ulong bitcast uint f32 a |
| 475 | end |
| 476 | note Integer to float toward zero: retain the leading precision bits, never round upward. |
| 477 | note No intermediate float, so u64 and i64 cannot double-round through f64. |
| 478 | fn rn_i2z t uint s uint a ulong to ulong |
| 479 | v = a |
| 480 | sg = ulong 0 |
| 481 | if rn_sg s |
| 482 | if lt bitcast long a 0 |
| 483 | v = sub ulong 0 a |
| 484 | sg = shl ulong 1 sub rn_w t 1 |
| 485 | end |
| 486 | end |
| 487 | if eq v 0 |
| 488 | ret 0 |
| 489 | end |
| 490 | p = ulong 23 |
| 491 | bias = ulong 127 |
| 492 | if eq t 14 |
| 493 | p = 52 |
| 494 | bias = 1023 |
| 495 | end |
| 496 | e = ulong 0 |
| 497 | for j ulong 64 |
| 498 | if ne shr v j 0 |
| 499 | e = j |
| 500 | end |
| 501 | end |
| 502 | m = v |
| 503 | if gt e p |
| 504 | m = shr v sub e p |
| 505 | else |
| 506 | m = shl v sub p e |
| 507 | end |
| 508 | ret or sg or shl add e bias p and m sub shl ulong 1 p 1 |
| 509 | end |
| 510 | fn rn_conv o uint to void |
| 511 | t = get i_oty o |
| 512 | s = rn_at o 0 |
| 513 | a = rn_a o 0 |
| 514 | im = get i_oim o |
| 515 | if and rn_isi t rn_isi s |
| 516 | rn_r o rn_cn t a |
| 517 | ret |
| 518 | end |
| 519 | |
| 520 | if or not or rn_isi t rn_isf t not or rn_isi s rn_isf s |
| 521 | rn_uns o |
| 522 | end |
| 523 | if rn_isi s |
| 524 | if eq and im 3 1 |
| 525 | rn_r o rn_i2z t s a |
| 526 | ret |
| 527 | end |
| 528 | rn_r o rn_i2f t s a |
| 529 | ret |
| 530 | end |
| 531 | x = rn_tof s a |
| 532 | if rn_isi t |
| 533 | rn_r o rn_f2i t x |
| 534 | ret |
| 535 | end |
| 536 | if eq t 14 |
| 537 | rn_r o bitcast ulong x |
| 538 | ret |
| 539 | end |
| 540 | y = f32 x |
| 541 | inf = div 1.0f64 0.0f64 |
| 542 | note The nearest result beyond the exact magnitude has the toward-zero predecessor. |
| 543 | if eq and im 3 1 |
| 544 | if gt fabs f64 y fabs x |
| 545 | bits = sub ulong bitcast uint y 1 |
| 546 | y = bitcast f32 uint bits |
| 547 | end |
| 548 | end |
| 549 | sat = ne and im 4 0 |
| 550 | yi = eq fabs f64 y inf |
| 551 | xf = ne fabs x inf |
| 552 | if and sat and yi xf |
| 553 | mx = bitcast f32 uint 2139095039 |
| 554 | y = mx |
| 555 | if lt x 0.0f64 |
| 556 | y = neg mx |
| 557 | end |
| 558 | end |
| 559 | rn_r o ulong bitcast uint y |
| 560 | end |
| 561 | fn rn_bcast o uint to void |
| 562 | t = get i_oty o |
| 563 | a = rn_a o 0 |
| 564 | if rn_isf t |
| 565 | rn_r o rn_cn t a |
| 566 | ret |
| 567 | end |
| 568 | rn_r o rn_cn t a |
| 569 | end |
| 570 | note ---- pure ops (1..37, 46) ----------------------------------------------------------------------- |
| 571 | note FMA is one native operation at the declared precision, never widened or split. |
| 572 | fn rn_fused t uint a ulong b ulong c ulong to ulong |
| 573 | if eq t 13 |
| 574 | x = bitcast f32 uint a |
| 575 | y = bitcast f32 uint b |
| 576 | z = bitcast f32 uint c |
| 577 | ret ulong bitcast uint fma x y z |
| 578 | end |
| 579 | x = bitcast f64 a |
| 580 | y = bitcast f64 b |
| 581 | z = bitcast f64 c |
| 582 | ret bitcast ulong fma x y z |
| 583 | end |
| 584 | fn rn_isvf t uint to bool |
| 585 | if ne get i_tk t 1 |
| 586 | ret false |
| 587 | end |
| 588 | e = get i_t1 t |
| 589 | l = get i_t2 t |
| 590 | if and eq e 13 eq l 4 |
| 591 | ret true |
| 592 | end |
| 593 | ret and eq e 14 eq l 2 |
| 594 | end |
| 595 | fn rn_vget t uint lo ulong hi ulong k ulong to ulong |
| 596 | if eq get i_t1 t 14 |
| 597 | if eq k 0 |
| 598 | ret lo |
| 599 | end |
| 600 | ret hi |
| 601 | end |
| 602 | w = lo |
| 603 | if ge k 2 |
| 604 | w = hi |
| 605 | end |
| 606 | if ne and k 1 0 |
| 607 | ret shr w 32 |
| 608 | end |
| 609 | ret and w 4294967295 |
| 610 | end |
| 611 | fn rn_vf32w a ulong b ulong c ulong to ulong |
| 612 | r0 = rn_fused 13 and a 4294967295 and b 4294967295 and c 4294967295 |
| 613 | r1 = rn_fused 13 shr a 32 shr b 32 shr c 32 |
| 614 | ret or r0 shl r1 32 |
| 615 | end |
| 616 | fn rn_vfma o uint t uint to void |
| 617 | if eq get i_t1 t 14 |
| 618 | rn_r o rn_fused 14 rn_a o 0 rn_a o 1 rn_a o 2 |
| 619 | rn_rw o rn_fused 14 rn_aw o 0 rn_aw o 1 rn_aw o 2 |
| 620 | ret |
| 621 | end |
| 622 | rn_r o rn_vf32w rn_a o 0 rn_a o 1 rn_a o 2 |
| 623 | rn_rw o rn_vf32w rn_aw o 0 rn_aw o 1 rn_aw o 2 |
| 624 | end |
| 625 | fn rn_vb32w op uint a ulong b ulong to ulong |
| 626 | r0 = rn_fa op 13 and a 4294967295 and b 4294967295 |
| 627 | r1 = rn_fa op 13 shr a 32 shr b 32 |
| 628 | ret or r0 shl r1 32 |
| 629 | end |
| 630 | fn rn_vbin o uint op uint t uint to void |
| 631 | if or lt op 10 gt op 12 |
| 632 | rn_uns o |
| 633 | end |
| 634 | if eq get i_t1 t 14 |
| 635 | rn_r o rn_fa op 14 rn_a o 0 rn_a o 1 |
| 636 | rn_rw o rn_fa op 14 rn_aw o 0 rn_aw o 1 |
| 637 | ret |
| 638 | end |
| 639 | rn_r o rn_vb32w op rn_a o 0 rn_a o 1 |
| 640 | rn_rw o rn_vb32w op rn_aw o 0 rn_aw o 1 |
| 641 | end |
| 642 | fn rn_vec o uint op uint to void |
| 643 | if eq op 100 |
| 644 | t = get i_oty o |
| 645 | if not rn_isvf t |
| 646 | rn_uns o |
| 647 | end |
| 648 | x = rn_a o 0 |
| 649 | if eq get i_t1 t 14 |
| 650 | rn_r o x |
| 651 | rn_rw o x |
| 652 | ret |
| 653 | end |
| 654 | w = or and x 4294967295 shl and x 4294967295 32 |
| 655 | rn_r o w |
| 656 | rn_rw o w |
| 657 | ret |
| 658 | end |
| 659 | vt = rn_at o 0 |
| 660 | if not rn_isvf vt |
| 661 | rn_uns o |
| 662 | end |
| 663 | m = get i_oim o |
| 664 | if ge m ulong get i_t2 vt |
| 665 | rn_uns o |
| 666 | end |
| 667 | rn_r o rn_vget vt rn_a o 0 rn_aw o 0 m |
| 668 | end |
| 669 | fn rn_pure o uint op uint to void |
| 670 | t = get i_oty o |
| 671 | if eq op 46 |
| 672 | if rn_isvf t |
| 673 | rn_vfma o t |
| 674 | ret |
| 675 | end |
| 676 | if not rn_isf t |
| 677 | rn_uns o |
| 678 | end |
| 679 | rn_r o rn_fused t rn_a o 0 rn_a o 1 rn_a o 2 |
| 680 | ret |
| 681 | end |
| 682 | if eq op 1 |
| 683 | rn_r o get i_oim o |
| 684 | ret |
| 685 | end |
| 686 | if eq op 2 |
| 687 | h = ir_arg o 2 |
| 688 | if ne rn_a o 0 0 |
| 689 | h = ir_arg o 1 |
| 690 | end |
| 691 | rn_r o rn_g h |
| 692 | rn_rw o rn_gw h |
| 693 | ret |
| 694 | end |
| 695 | if eq op 36 |
| 696 | rn_conv o |
| 697 | ret |
| 698 | end |
| 699 | if eq op 37 |
| 700 | rn_bcast o |
| 701 | ret |
| 702 | end |
| 703 | s = rn_at o 0 |
| 704 | a = rn_a o 0 |
| 705 | b = ulong 0 |
| 706 | if gt get i_ona o 1 |
| 707 | b = rn_a o 1 |
| 708 | end |
| 709 | if and ge op 30 le op 35 |
| 710 | if rn_isf s |
| 711 | rn_r o rn_b2u rn_fcmp op s a b |
| 712 | ret |
| 713 | end |
| 714 | if gt s 9 |
| 715 | rn_uns o |
| 716 | end |
| 717 | rn_r o rn_b2u rn_icmp op s a b |
| 718 | ret |
| 719 | end |
| 720 | if rn_isvf t |
| 721 | rn_vbin o op t |
| 722 | ret |
| 723 | end |
| 724 | if or eq op 28 gt t 14 |
| 725 | rn_uns o |
| 726 | end |
| 727 | if rn_isf t |
| 728 | rn_r o rn_fa op t a b |
| 729 | ret |
| 730 | end |
| 731 | if or eq op 26 or eq op 27 gt t 9 |
| 732 | rn_uns o |
| 733 | end |
| 734 | if eq t 1 |
| 735 | rn_r o and rn_ia op 5 a b 1 |
| 736 | ret |
| 737 | end |
| 738 | rn_r o rn_ia op t a b |
| 739 | end |
| 740 | note ---- checks (70..82): IR.md s7.9 --------------------------------------------------------------- |
| 741 | fn rn_ckadd t uint a ulong b ulong to bool |
| 742 | r = add a b |
| 743 | if lt rn_w t 64 |
| 744 | ret ne rn_cn t r r |
| 745 | end |
| 746 | if rn_sg t |
| 747 | ret ne and and xor a r xor b r shl ulong 1 63 0 |
| 748 | end |
| 749 | ret lt r a |
| 750 | end |
| 751 | fn rn_cksub t uint a ulong b ulong to bool |
| 752 | r = sub a b |
| 753 | if lt rn_w t 64 |
| 754 | ret ne rn_cn t r r |
| 755 | end |
| 756 | if rn_sg t |
| 757 | ret ne and and xor a b xor a r shl ulong 1 63 0 |
| 758 | end |
| 759 | ret lt a b |
| 760 | end |
| 761 | fn rn_ckmul t uint a ulong b ulong to bool |
| 762 | r = mul a b |
| 763 | if lt rn_w t 64 |
| 764 | ret ne rn_cn t r r |
| 765 | end |
| 766 | if eq a 0 |
| 767 | ret false |
| 768 | end |
| 769 | if not rn_sg t |
| 770 | ret ne div r a b |
| 771 | end |
| 772 | m = shl ulong 1 63 |
| 773 | if eq a sub ulong 0 1 |
| 774 | ret eq b m |
| 775 | end |
| 776 | if eq b sub ulong 0 1 |
| 777 | ret eq a m |
| 778 | end |
| 779 | ret ne div bitcast long r bitcast long a bitcast long b |
| 780 | end |
| 781 | note the exact a x 2^b fits the signed type t when MIN >> b <= a <= MAX >> b |
| 782 | fn rn_ckshl t uint a ulong b ulong to bool |
| 783 | w = rn_w t |
| 784 | if ge b w |
| 785 | ret ne a 0 |
| 786 | end |
| 787 | mn = bitcast long rn_min t |
| 788 | mx = long 9223372036854775807 |
| 789 | if lt w 64 |
| 790 | mx = sub neg mn 1 |
| 791 | end |
| 792 | sa = bitcast long a |
| 793 | sb = bitcast long b |
| 794 | if lt sa shr mn sb |
| 795 | ret true |
| 796 | end |
| 797 | ret gt sa shr mx sb |
| 798 | end |
| 799 | note v (type s) outside int type t, float v truncated toward zero first, NaN outside. An unsigned v into a |
| 800 | note signed t of any width is outside from 2^(w-1) up: the canonical test alone would take 2^64 - 1 into i32 |
| 801 | note as -1 (bug hunt, ledger L1060; tests/ir x-convu) |
| 802 | fn rn_ckcv t uint s uint v ulong to bool |
| 803 | w = rn_w t |
| 804 | if rn_isf s |
| 805 | x = rn_tof s v |
| 806 | if ne x x |
| 807 | ret true |
| 808 | end |
| 809 | hi = mul f64 shl ulong 1 sub w 1 2.0f64 |
| 810 | lo = f64 -1.0 |
| 811 | if rn_sg t |
| 812 | hi = f64 shl ulong 1 sub w 1 |
| 813 | lo = sub neg hi 1.0 |
| 814 | if eq w 64 |
| 815 | ret or lt x neg hi ge x hi |
| 816 | end |
| 817 | end |
| 818 | ret or le x lo ge x hi |
| 819 | end |
| 820 | if rn_sg s |
| 821 | sv = bitcast long v |
| 822 | if rn_sg t |
| 823 | ret ne rn_cn t v v |
| 824 | end |
| 825 | if lt sv 0 |
| 826 | ret true |
| 827 | end |
| 828 | end |
| 829 | if rn_sg t |
| 830 | ret ge v shl ulong 1 sub w 1 |
| 831 | end |
| 832 | ret ne rn_cn t v v |
| 833 | end |
| 834 | fn rn_len o uint k uint to ulong |
| 835 | h = ir_arg o k |
| 836 | rt = get i_vty h |
| 837 | if eq get i_tk rt 5 |
| 838 | ret rn_gw h |
| 839 | end |
| 840 | tt = get i_t1 rt |
| 841 | ret ulong get i_t2 tt |
| 842 | end |
| 843 | fn rn_ckfail o uint op uint to bool |
| 844 | t = rn_at o 1 |
| 845 | a = rn_a o 1 |
| 846 | b = ulong 0 |
| 847 | if gt get i_ona o 2 |
| 848 | b = rn_a o 2 |
| 849 | end |
| 850 | if eq op 70 |
| 851 | ret rn_ckadd t a b |
| 852 | end |
| 853 | if eq op 71 |
| 854 | ret rn_cksub t a b |
| 855 | end |
| 856 | if eq op 72 |
| 857 | ret rn_ckmul t a b |
| 858 | end |
| 859 | if eq op 73 |
| 860 | if rn_sg t |
| 861 | ret eq a rn_min t |
| 862 | end |
| 863 | ret ne a 0 |
| 864 | end |
| 865 | if eq op 74 |
| 866 | ret eq a 0 |
| 867 | end |
| 868 | if eq op 75 |
| 869 | ret and eq a rn_min t eq b sub ulong 0 1 |
| 870 | end |
| 871 | if eq op 76 |
| 872 | ret rn_ckshl t a b |
| 873 | end |
| 874 | if eq op 77 |
| 875 | ret ge a rn_w t |
| 876 | end |
| 877 | if eq op 78 |
| 878 | ret rn_ckcv get i_oty o t a |
| 879 | end |
| 880 | if eq op 81 |
| 881 | ret ge a b |
| 882 | end |
| 883 | if eq op 82 |
| 884 | ret eq a 0 |
| 885 | end |
| 886 | n = rn_len o 1 |
| 887 | x = rn_a o 3 |
| 888 | if eq op 79 |
| 889 | ret or eq x 0 or gt b n gt x sub n b |
| 890 | end |
| 891 | ret or gt b n gt x sub n b |
| 892 | end |
| 893 | fn rn_check o uint to void |
| 894 | if rn_ckfail o uint get i_oop o |
| 895 | exit int add 100 get i_oim o |
| 896 | end |
| 897 | end |
| 898 | note ---- memory ------------------------------------------------------------------------------------ |
| 899 | fn rn_ld p ulong n ulong be bool to ulong |
| 900 | x = ulong 0 |
| 901 | for j ulong n bound 8 |
| 902 | c = ulong get r_m add p j |
| 903 | if be |
| 904 | x = or shl x 8 c |
| 905 | else |
| 906 | x = or x shl c mul j 8 |
| 907 | end |
| 908 | end |
| 909 | ret x |
| 910 | end |
| 911 | fn rn_st p ulong n ulong x ulong be bool to void |
| 912 | for j ulong n bound 8 |
| 913 | s = mul j 8 |
| 914 | if be |
| 915 | s = mul sub sub n 1 j 8 |
| 916 | end |
| 917 | put r_m add p j uchar and shr x s 255 |
| 918 | end |
| 919 | end |
| 920 | note element type of a REF to ARRAY or a SLICE |
| 921 | fn rn_elt rt uint to uint |
| 922 | if eq get i_tk rt 5 |
| 923 | ret get i_t1 rt |
| 924 | end |
| 925 | ret get i_t1 get i_t1 rt |
| 926 | end |
| 927 | note address of element i of operand slot k of op o; refuses non-scalar elements and an i outside the |
| 928 | note object (rn_oob) |
| 929 | fn rn_ea o uint k uint i ulong to ulong |
| 930 | if ge i rn_len o k |
| 931 | rn_oob o |
| 932 | end |
| 933 | ret rn_eau o k i |
| 934 | end |
| 935 | note element i's address without the extent test (i may be the object's end) |
| 936 | fn rn_eau o uint k uint i ulong to ulong |
| 937 | et = rn_elt rn_at o k |
| 938 | if ge et 32 |
| 939 | rn_uns o |
| 940 | end |
| 941 | ret add rn_a o k mul i ir_tsize et |
| 942 | end |
| 943 | fn rn_ldv t uint p ulong be bool to ulong |
| 944 | x = rn_ld p ir_tsize t be |
| 945 | if rn_isi t |
| 946 | ret rn_cn t x |
| 947 | end |
| 948 | ret x |
| 949 | end |
| 950 | note memory top aligned to 8, then n zero bytes; trap 6 past 16 MiB |
| 951 | fn rn_alloc n ulong to ulong |
| 952 | p = and add get r_s 2 7 not ulong 7 |
| 953 | if gt p sub 16777216 get r_ps 0 |
| 954 | exit 106 |
| 955 | end |
| 956 | if gt n sub sub 16777216 p get r_ps 0 |
| 957 | exit 106 |
| 958 | end |
| 959 | for j ulong n bound 16777216 |
| 960 | put r_m add p j 0 |
| 961 | end |
| 962 | put r_s 2 add p n |
| 963 | ret p |
| 964 | end |
| 965 | note field slot of field number k of the STRUCT that REF type rt targets |
| 966 | fn rn_fld rt uint k ulong to ulong |
| 967 | ret add ulong get i_t3 get i_t1 rt k |
| 968 | end |
| 969 | fn rn_atom o uint im ulong t uint old ulong v ulong to ulong |
| 970 | f = and im 255 |
| 971 | if eq f 0 |
| 972 | ret rn_cn t add old v |
| 973 | end |
| 974 | if eq f 1 |
| 975 | ret rn_ia 24 t old v |
| 976 | end |
| 977 | if eq f 2 |
| 978 | ret rn_ia 25 t old v |
| 979 | end |
| 980 | if eq f 3 |
| 981 | ret and old v |
| 982 | end |
| 983 | if eq f 4 |
| 984 | ret or old v |
| 985 | end |
| 986 | if eq f 5 |
| 987 | ret xor old v |
| 988 | end |
| 989 | ret v |
| 990 | end |
| 991 | fn rn_mem o uint op uint to void |
| 992 | if eq op 52 |
| 993 | rn_r o get r_ga get i_oim o |
| 994 | ret |
| 995 | end |
| 996 | if eq op 50 |
| 997 | rn_r o rn_alloc ir_tsize get i_t1 get i_oty o |
| 998 | ret |
| 999 | end |
| 1000 | if eq op 53 |
| 1001 | rn_r o rn_len o 0 |
| 1002 | ret |
| 1003 | end |
| 1004 | if eq op 42 |
| 1005 | p = rn_ea o 1 rn_a o 2 |
| 1006 | rn_r o rn_ldv get i_oty o p false |
| 1007 | ret |
| 1008 | end |
| 1009 | if eq op 43 |
| 1010 | p = rn_ea o 1 rn_a o 2 |
| 1011 | rn_st p ir_tsize rn_elt rn_at o 1 rn_a o 3 false |
| 1012 | ret |
| 1013 | end |
| 1014 | if eq op 54 |
| 1015 | rn_ext o 1 rn_a o 2 rn_a o 3 |
| 1016 | p = rn_eau o 1 rn_a o 2 |
| 1017 | rn_r o p |
| 1018 | rn_rw o rn_a o 3 |
| 1019 | ret |
| 1020 | end |
| 1021 | if eq op 51 |
| 1022 | n = rn_len o 1 |
| 1023 | et = rn_elt rn_at o 1 |
| 1024 | v = rn_a o 2 |
| 1025 | p = rn_eau o 1 0 |
| 1026 | for j ulong n bound 16777216 |
| 1027 | rn_st add p mul j ir_tsize et ir_tsize et v false |
| 1028 | end |
| 1029 | ret |
| 1030 | end |
| 1031 | if or eq op 47 eq op 48 |
| 1032 | t = get i_oty o |
| 1033 | if not rn_isi t |
| 1034 | rn_uns o |
| 1035 | end |
| 1036 | p = rn_ea o 1 rn_a o 2 |
| 1037 | sz = ir_tsize t |
| 1038 | old = rn_ldv t p false |
| 1039 | v = rn_a o 3 |
| 1040 | nv = rn_atom o get i_oim o t old v |
| 1041 | if eq op 48 |
| 1042 | nv = old |
| 1043 | if eq old v |
| 1044 | nv = rn_a o 4 |
| 1045 | end |
| 1046 | end |
| 1047 | rn_st p sz nv false |
| 1048 | rn_r o old |
| 1049 | ret |
| 1050 | end |
| 1051 | if and ge op 55 le op 57 |
| 1052 | k = uint 1 |
| 1053 | if eq op 57 |
| 1054 | k = 0 |
| 1055 | end |
| 1056 | rt = rn_at o k |
| 1057 | fx = rn_fld rt get i_oim o |
| 1058 | ft = get i_xty fx |
| 1059 | p = add rn_a o k ulong get i_xof fx |
| 1060 | be = eq get i_xor fx 1 |
| 1061 | if eq op 57 |
| 1062 | rn_r o p |
| 1063 | ret |
| 1064 | end |
| 1065 | if ge ft 32 |
| 1066 | rn_uns o |
| 1067 | end |
| 1068 | if eq op 55 |
| 1069 | rn_r o rn_ldv ft p be |
| 1070 | ret |
| 1071 | end |
| 1072 | rn_st p ir_tsize ft rn_a o 2 be |
| 1073 | ret |
| 1074 | end |
| 1075 | if eq op 58 |
| 1076 | t = get i_oty o |
| 1077 | if not rn_isvf t |
| 1078 | rn_uns o |
| 1079 | end |
| 1080 | rn_ext o 1 rn_a o 2 ulong get i_t2 t |
| 1081 | p = rn_eau o 1 rn_a o 2 |
| 1082 | rn_r o rn_ld p 8 false |
| 1083 | rn_rw o rn_ld add p 8 8 false |
| 1084 | ret |
| 1085 | end |
| 1086 | if eq op 59 |
| 1087 | vt = rn_at o 3 |
| 1088 | if not rn_isvf vt |
| 1089 | rn_uns o |
| 1090 | end |
| 1091 | rn_ext o 1 rn_a o 2 ulong get i_t2 vt |
| 1092 | p = rn_eau o 1 rn_a o 2 |
| 1093 | rn_st p 8 rn_a o 3 false |
| 1094 | rn_st add p 8 8 rn_aw o 3 false |
| 1095 | ret |
| 1096 | end |
| 1097 | rn_uns o |
| 1098 | end |
| 1099 | note ---- host words (IR.md s7.11) -------------------------------------------------------------------- |
| 1100 | note slot base of a host op's 3-operand buffer argument starting at slot k: address of A + off |
| 1101 | fn rn_buf o uint k uint to ulong |
| 1102 | ret add rn_a o k rn_a o add k 1 |
| 1103 | end |
| 1104 | fn rn_host o uint to void |
| 1105 | rn_safehost o |
| 1106 | end |
| 1107 | note ---- control (60..67) --------------------------------------------------------------------------- |
| 1108 | note values handed to a list of parameter values starting at v (block or function parameters) |
| 1109 | fn rn_pars v0 uint n ulong to void |
| 1110 | v = v0 |
| 1111 | for k ulong n bound 64 |
| 1112 | if eq v 0 |
| 1113 | ret |
| 1114 | end |
| 1115 | rn_p v get r_tv k |
| 1116 | rn_pw v get r_tw k |
| 1117 | v = get i_vnx v |
| 1118 | end |
| 1119 | end |
| 1120 | fn rn_take o uint k0 uint to ulong |
| 1121 | n = sub ulong get i_ona o ulong k0 |
| 1122 | for k ulong n bound 64 |
| 1123 | h = ir_arg o uint add ulong k0 k |
| 1124 | put r_tv k rn_g h |
| 1125 | put r_tw k rn_gw h |
| 1126 | end |
| 1127 | ret n |
| 1128 | end |
| 1129 | note values of function f (they are contiguous handles from its entry token) |
| 1130 | fn rn_nv f uint to ulong |
| 1131 | if lt ulong f get i_n 3 |
| 1132 | ret sub ulong get i_ftk add f 1 ulong get i_ftk f |
| 1133 | end |
| 1134 | ret sub add get i_n 6 1 ulong get i_ftk f |
| 1135 | end |
| 1136 | note enter function f with r_tv[0..n) as its parameters; returns its first op |
| 1137 | fn rn_enter f uint n ulong to uint |
| 1138 | b = get r_s 4 |
| 1139 | nv = rn_nv f |
| 1140 | if gt nv sub 4194304 b |
| 1141 | exit 106 |
| 1142 | end |
| 1143 | put r_s 4 add b nv |
| 1144 | put r_s 0 sub b ulong get i_ftk f |
| 1145 | put r_s 1 ulong f |
| 1146 | rn_pars get i_fp1 f n |
| 1147 | ret get i_bo1 get i_fb1 f |
| 1148 | end |
| 1149 | fn rn_call o uint to uint |
| 1150 | n = rn_take o 2 |
| 1151 | d = get r_s 3 |
| 1152 | if ge d 65535 |
| 1153 | exit 106 |
| 1154 | end |
| 1155 | put r_cf d uint get r_s 1 |
| 1156 | put r_co d o |
| 1157 | put r_cs d get r_s 0 |
| 1158 | put r_cm d get r_s 2 |
| 1159 | put r_ct d get r_s 4 |
| 1160 | put r_s 3 add d 1 |
| 1161 | ret rn_enter ir_arg o 1 n |
| 1162 | end |
| 1163 | fn rn_ret x ulong y ulong to uint |
| 1164 | d = get r_s 3 |
| 1165 | if eq d 0 |
| 1166 | exit int bitcast long x |
| 1167 | end |
| 1168 | d = sub d 1 |
| 1169 | put r_s 3 d |
| 1170 | c = get r_co d |
| 1171 | put r_s 0 get r_cs d |
| 1172 | put r_s 1 ulong get r_cf d |
| 1173 | put r_s 2 get r_cm d |
| 1174 | put r_s 4 get r_ct d |
| 1175 | rn_r c x |
| 1176 | rn_rw c y |
| 1177 | ret get i_onx c |
| 1178 | end |
| 1179 | fn rn_ctl o uint op uint to uint |
| 1180 | if eq op 60 |
| 1181 | b = ir_arg o 0 |
| 1182 | n = rn_take o 1 |
| 1183 | rn_pars get i_bp1 b n |
| 1184 | ret get i_bo1 b |
| 1185 | end |
| 1186 | if eq op 61 |
| 1187 | b = ir_arg o 3 |
| 1188 | if ne rn_a o 1 0 |
| 1189 | b = ir_arg o 2 |
| 1190 | end |
| 1191 | ret get i_bo1 b |
| 1192 | end |
| 1193 | if eq op 62 |
| 1194 | ret rn_ret 0 0 |
| 1195 | end |
| 1196 | if eq op 63 |
| 1197 | ret rn_ret rn_a o 1 rn_aw o 1 |
| 1198 | end |
| 1199 | if eq op 64 |
| 1200 | ret rn_call o |
| 1201 | end |
| 1202 | if eq op 65 |
| 1203 | exit int add 100 get i_oim o |
| 1204 | end |
| 1205 | if eq op 66 |
| 1206 | exit int bitcast long rn_a o 1 |
| 1207 | end |
| 1208 | rn_uns o |
| 1209 | ret 0 |
| 1210 | end |
| 1211 | note ---- one op: returns the next op ----------------------------------------------------------------- |
| 1212 | fn rn_step o uint to uint |
| 1213 | op = uint get i_oop o |
| 1214 | if or lt op 38 eq op 46 |
| 1215 | rn_pure o op |
| 1216 | ret get i_onx o |
| 1217 | end |
| 1218 | if and ge op 70 le op 82 |
| 1219 | rn_check o |
| 1220 | ret get i_onx o |
| 1221 | end |
| 1222 | if and ge op 90 le op 93 |
| 1223 | ret get i_onx o |
| 1224 | end |
| 1225 | if and ge op 60 le op 67 |
| 1226 | ret rn_ctl o op |
| 1227 | end |
| 1228 | if eq op 110 |
| 1229 | rn_host o |
| 1230 | ret get i_onx o |
| 1231 | end |
| 1232 | if and ge op 111 le op 116 |
| 1233 | rn_pool o op |
| 1234 | ret get i_onx o |
| 1235 | end |
| 1236 | if or eq op 42 or eq op 43 or eq op 47 eq op 48 |
| 1237 | rn_mem o op |
| 1238 | ret get i_onx o |
| 1239 | end |
| 1240 | if and ge op 50 le op 59 |
| 1241 | rn_mem o op |
| 1242 | ret get i_onx o |
| 1243 | end |
| 1244 | if or eq op 100 eq op 101 |
| 1245 | rn_vec o op |
| 1246 | ret get i_onx o |
| 1247 | end |
| 1248 | rn_uns o |
| 1249 | ret 0 |
| 1250 | end |
| 1251 | note ---- start -------------------------------------------------------------------------------------- |
| 1252 | note globals at 8-aligned addresses from 8 (address 0 is never an object); class 1 copies its bytes |
| 1253 | fn rn_globs to void |
| 1254 | put r_s 2 8 |
| 1255 | note ngrun keeps its own address table of IR.md s12's 65536 globals (r_ga), as ngcheck does; src/ir.om reads |
| 1256 | note more since the no-caps arena, so a unit with more is LIMIT here (class 23 at offset 0), never a trap |
| 1257 | if gt get i_n 2 65536 |
| 1258 | rn_say 58 79 |
| 1259 | rn_num 23 |
| 1260 | rn_say 23 27 |
| 1261 | rn_num 0 |
| 1262 | rn_nl |
| 1263 | exit 23 |
| 1264 | end |
| 1265 | for g ulong get i_n 2 bound 65536 |
| 1266 | h = add g 1 |
| 1267 | sz = ulong 0 |
| 1268 | if ne ar_c 88 h 2 |
| 1269 | sz = ir_tsize ar_u 87 h |
| 1270 | end |
| 1271 | p = rn_alloc sz |
| 1272 | put r_ga h p |
| 1273 | if eq uchar get ar_m add get ar_rb 88 h 1 |
| 1274 | d0 = get ar_m add get ar_rb 89 h |
| 1275 | for j ulong sz bound 16777216 |
| 1276 | c = ar_bg 2 add d0 j |
| 1277 | put r_m add p j c |
| 1278 | end |
| 1279 | end |
| 1280 | end |
| 1281 | end |
| 1282 | note the program entry: the export with no parameters and result i32 (IR.md s6.1) |
| 1283 | fn rn_entry to uint |
| 1284 | for i ulong get i_n 3 bound 65536 |
| 1285 | f = add i 1 |
| 1286 | ok = and eq get i_fk f 2 eq get i_fnp f 0 |
| 1287 | if and ok eq get i_frs f 8 |
| 1288 | ret uint f |
| 1289 | end |
| 1290 | end |
| 1291 | ret 0 |
| 1292 | end |
| 1293 | note the STEPS argument: 1 to 19 decimal digits, else usage and exit 2 |
| 1294 | fn rn_steps to ulong |
| 1295 | m = argread 2 r_pa 0 4096 |
| 1296 | if or le m 0 gt m 19 |
| 1297 | rn_say 80 116 |
| 1298 | exit 2 |
| 1299 | end |
| 1300 | x = ulong 0 |
| 1301 | for j ulong ulong m bound 19 |
| 1302 | c = ulong get r_pa j |
| 1303 | if or lt c 48 gt c 57 |
| 1304 | rn_say 80 116 |
| 1305 | exit 2 |
| 1306 | end |
| 1307 | x = add mul x 10 sub c 48 |
| 1308 | end |
| 1309 | ret x |
| 1310 | end |
| 1311 | fn rn_load to ulong |
| 1312 | m = argread 1 r_pa 0 4096 |
| 1313 | if or le m 0 ge m 4096 |
| 1314 | rn_say 80 116 |
| 1315 | exit 2 |
| 1316 | end |
| 1317 | fd = fopen r_pa 0 ulong m 524288 0 |
| 1318 | if lt fd 0 |
| 1319 | rn_say 116 141 |
| 1320 | exit 27 |
| 1321 | end |
| 1322 | note the unit into region 0 of the arena (src/ir.om ar_rdin; no-caps 2026-11-11): a read error is exit 27, a |
| 1323 | note unit the arena cannot hold is class 23 at the bytes read |
| 1324 | x = ar_rdin int fd |
| 1325 | if eq x 1 |
| 1326 | rn_say 116 141 |
| 1327 | exit 27 |
| 1328 | end |
| 1329 | n = get ar_inl 0 |
| 1330 | if eq x 2 |
| 1331 | rn_say 58 79 |
| 1332 | rn_num 23 |
| 1333 | rn_say 23 27 |
| 1334 | rn_num n |
| 1335 | rn_nl |
| 1336 | exit 23 |
| 1337 | end |
| 1338 | r = fclose int fd |
| 1339 | ret n |
| 1340 | end |
| 1341 | fn main to int |
| 1342 | rn_opts |
| 1343 | n = rn_load |
| 1344 | c = ir_read 0 n |
| 1345 | if ne c 0 |
| 1346 | rn_say 58 79 |
| 1347 | rn_num c |
| 1348 | rn_say 23 27 |
| 1349 | rn_num ir_badat |
| 1350 | rn_nl |
| 1351 | ret int c |
| 1352 | end |
| 1353 | f = rn_entry |
| 1354 | if eq f 0 |
| 1355 | rn_say 58 79 |
| 1356 | rn_num 30 |
| 1357 | rn_nl |
| 1358 | ret 30 |
| 1359 | end |
| 1360 | rn_setup |
| 1361 | rn_globs |
| 1362 | o = rn_enter f 0 |
| 1363 | while true bound 9223372036854775807 |
| 1364 | if ge get r_s 6 get r_s 5 |
| 1365 | rn_lim 141 162 o |
| 1366 | exit 124 |
| 1367 | end |
| 1368 | put r_s 6 add get r_s 6 1 |
| 1369 | o = rn_step o |
| 1370 | end |
| 1371 | ret 0 |
| 1372 | end |
| 1373 | note ---- confined host-word execution --------------------------------------------------------------- |
| 1374 | note r_cfg: 0 scratch option argument, 1 first program argument, 2 scratch enabled. |
| 1375 | note Virtual descriptors expose only stdio and descriptors created by an interpreted host word. |
| 1376 | global r_cfg ulong 3 |
| 1377 | global r_fd long 256 |
| 1378 | global r_own uchar 256 |
| 1379 | global r_root uchar 4096 |
| 1380 | note r_pst: the stat structure of a name opened without following a link; r_pp: /proc/self/fd/N for it. |
| 1381 | global r_pst uchar 128 |
| 1382 | global r_pp uchar 32 |
| 1383 | text r_ppre "/proc/self/fd/" |
| 1384 | text r_hmsg "ngrun: refused host class " |
| 1385 | text r_hword " word " |
| 1386 | text r_hat " at " |
| 1387 | text r_help "ngrun: usage: ngrun FILE.ir [STEPS] [--scratch NEW-DIR] [-- ARGS...]\n" |
| 1388 | text r_opt0 "--scratch" |
| 1389 | text r_opt1 "--" |
| 1390 | fn rn_href o uint code int to void |
| 1391 | k = write 2 r_hmsg 0 len r_hmsg |
| 1392 | rn_num ulong code |
| 1393 | k = write 2 r_hword 0 len r_hword |
| 1394 | word = get i_oim o |
| 1395 | if or eq get i_oop o 115 eq get i_oop o 116 |
| 1396 | word = sub ulong get i_oop o 62 |
| 1397 | end |
| 1398 | rn_num word |
| 1399 | k = write 2 r_hat 0 len r_hat |
| 1400 | rn_num ulong get i_oof o |
| 1401 | rn_nl |
| 1402 | exit code |
| 1403 | end |
| 1404 | fn rn_usage to void |
| 1405 | k = write 2 r_help 0 len r_help |
| 1406 | exit 2 |
| 1407 | end |
| 1408 | fn rn_argis idx ulong word view uchar * to bool |
| 1409 | n = argread idx r_pa 0 len r_pa |
| 1410 | if ne n long len word |
| 1411 | ret false |
| 1412 | end |
| 1413 | for j ulong len word bound 64 |
| 1414 | if ne get r_pa j get word j |
| 1415 | ret false |
| 1416 | end |
| 1417 | end |
| 1418 | ret true |
| 1419 | end |
| 1420 | fn rn_opts to void |
| 1421 | for j ulong 256 |
| 1422 | put r_fd j -1 |
| 1423 | end |
| 1424 | put r_fd 0 0 |
| 1425 | put r_fd 1 1 |
| 1426 | put r_fd 2 2 |
| 1427 | put r_s 5 68719476736 |
| 1428 | put r_cfg 1 ulong argcnt |
| 1429 | if or lt argcnt 2 gt argcnt 4096 |
| 1430 | rn_usage |
| 1431 | end |
| 1432 | i = ulong 2 |
| 1433 | while lt i ulong argcnt bound 4096 |
| 1434 | if rn_argis i r_opt1 |
| 1435 | put r_cfg 1 add i 1 |
| 1436 | ret |
| 1437 | end |
| 1438 | if rn_argis i r_opt0 |
| 1439 | if ne get r_cfg 0 0 |
| 1440 | rn_usage |
| 1441 | end |
| 1442 | i = add i 1 |
| 1443 | if ge i ulong argcnt |
| 1444 | rn_usage |
| 1445 | end |
| 1446 | put r_cfg 0 i |
| 1447 | else |
| 1448 | if ne i 2 |
| 1449 | rn_usage |
| 1450 | end |
| 1451 | put r_s 5 rn_steps |
| 1452 | end |
| 1453 | i = add i 1 |
| 1454 | end |
| 1455 | end |
| 1456 | fn rn_setup to void |
| 1457 | idx = get r_cfg 0 |
| 1458 | if eq idx 0 |
| 1459 | ret |
| 1460 | end |
| 1461 | n = argread idx r_root 0 len r_root |
| 1462 | if or le n 0 ge n long len r_root |
| 1463 | rn_usage |
| 1464 | end |
| 1465 | note A fresh private directory is required. Existing trees, hard links and preplanted symlinks are not admitted. |
| 1466 | k = makedir r_root 0 ulong n 448 |
| 1467 | if ne k 0 |
| 1468 | rn_usage |
| 1469 | end |
| 1470 | note AArch64 Linux: O_DIRECTORY 16384, O_NOFOLLOW 32768, O_CLOEXEC 524288. |
| 1471 | flags = or int 16384 or int 32768 int 524288 |
| 1472 | fd = fopen r_root 0 ulong n flags 0 |
| 1473 | if lt fd 0 |
| 1474 | rn_usage |
| 1475 | end |
| 1476 | k = fchdir int fd |
| 1477 | cl = fclose int fd |
| 1478 | if ne k 0 |
| 1479 | rn_usage |
| 1480 | end |
| 1481 | put r_cfg 2 1 |
| 1482 | end |
| 1483 | fn rn_hr o uint value long to void |
| 1484 | rn_r o bitcast ulong value |
| 1485 | end |
| 1486 | fn rn_fget o uint slot uint to int |
| 1487 | v = bitcast long rn_a o slot |
| 1488 | if or lt v 0 ge v 256 |
| 1489 | ret -1 |
| 1490 | end |
| 1491 | ret int get r_fd ulong v |
| 1492 | end |
| 1493 | fn rn_fadd o uint actual long to long |
| 1494 | if lt actual 0 |
| 1495 | ret actual |
| 1496 | end |
| 1497 | for j ulong 256 |
| 1498 | if lt get r_fd j 0 |
| 1499 | put r_fd j actual |
| 1500 | put r_own j 1 |
| 1501 | ret long j |
| 1502 | end |
| 1503 | end |
| 1504 | k = fclose int actual |
| 1505 | rn_href o 123 |
| 1506 | ret 0 |
| 1507 | end |
| 1508 | note A path over 4095 bytes or holding a zero byte traps 3 natively before any kernel call (OMEGESE s10), |
| 1509 | note with or without --scratch, so ngrun gives that trap before any confinement refusal. |
| 1510 | fn rn_pathspan o uint p ulong n ulong to void |
| 1511 | if gt n 4095 |
| 1512 | exit 103 |
| 1513 | end |
| 1514 | for i ulong n bound 4095 |
| 1515 | if eq get r_m add p i 0 |
| 1516 | exit 103 |
| 1517 | end |
| 1518 | end |
| 1519 | if eq get r_cfg 2 0 |
| 1520 | rn_href o 119 |
| 1521 | end |
| 1522 | if eq n 0 |
| 1523 | rn_href o 119 |
| 1524 | end |
| 1525 | for j ulong n bound 4095 |
| 1526 | c = get r_m add p j |
| 1527 | if or eq c 0 eq c 47 |
| 1528 | rn_href o 119 |
| 1529 | end |
| 1530 | end |
| 1531 | if eq get r_m p 46 |
| 1532 | if eq n 1 |
| 1533 | rn_href o 119 |
| 1534 | end |
| 1535 | if eq n 2 |
| 1536 | if eq get r_m add p 1 46 |
| 1537 | rn_href o 119 |
| 1538 | end |
| 1539 | end |
| 1540 | end |
| 1541 | end |
| 1542 | fn rn_path o uint slot uint to ulong |
| 1543 | rn_ext o slot rn_a o add slot 1 rn_a o add slot 2 |
| 1544 | p = rn_buf o slot |
| 1545 | rn_pathspan o p rn_a o add slot 2 |
| 1546 | ret p |
| 1547 | end |
| 1548 | note A packed pair contains exactly two confined component names. No symlink creation is allowed. |
| 1549 | note The whole packet is checked first as the native words check it (count 2, records inside the packet, no |
| 1550 | note trailing bytes, no empty record, no zero byte: trap 3); only then is each name confined. |
| 1551 | fn rn_pair o uint to ulong |
| 1552 | rn_ext o 1 rn_a o 2 rn_a o 3 |
| 1553 | p = rn_buf o 1 |
| 1554 | n = rn_a o 3 |
| 1555 | if or lt n 6 gt n 4096 |
| 1556 | exit 103 |
| 1557 | end |
| 1558 | if ne rn_ld p 2 false 2 |
| 1559 | exit 103 |
| 1560 | end |
| 1561 | cur = add p 2 |
| 1562 | stop = add p n |
| 1563 | for j ulong 2 |
| 1564 | if gt add cur 2 stop |
| 1565 | exit 103 |
| 1566 | end |
| 1567 | m = rn_ld cur 2 false |
| 1568 | cur = add cur 2 |
| 1569 | if gt m sub stop cur |
| 1570 | exit 103 |
| 1571 | end |
| 1572 | if eq m 0 |
| 1573 | exit 103 |
| 1574 | end |
| 1575 | for i ulong m bound 4096 |
| 1576 | if eq get r_m add cur i 0 |
| 1577 | exit 103 |
| 1578 | end |
| 1579 | end |
| 1580 | cur = add cur m |
| 1581 | end |
| 1582 | if ne cur stop |
| 1583 | exit 103 |
| 1584 | end |
| 1585 | cur = add p 2 |
| 1586 | for j ulong 2 |
| 1587 | m = rn_ld cur 2 false |
| 1588 | cur = add cur 2 |
| 1589 | rn_pathspan o cur m |
| 1590 | cur = add cur m |
| 1591 | end |
| 1592 | ret p |
| 1593 | end |
| 1594 | note rn_pkt: the native packed-list check (OMEGESE s10, omc hpack) of the packet at operand slot k. Kind 0 |
| 1595 | note (an exec argument packet: execprog, execpure, execenv's program) is 2..65536 bytes with count 1..1024, |
| 1596 | note as omc since the exec argv limits (ee5fc83e; pinned omc 564c5835); kinds 1 and 2 are 2..4096 bytes with |
| 1597 | note count 1..64 (exactly 2 for kind 1). Every record lies inside it, no bytes trail the last record and no |
| 1598 | note record holds a zero byte. Kind 0 needs a nonempty first record; kind 1 (symlnk) and kind 2 (execenv's |
| 1599 | note environment) need every record nonempty. |
| 1600 | note A violation traps 3, as natively, before ngrun's refusal of the word. |
| 1601 | fn rn_pkt o uint k uint kind ulong to void |
| 1602 | rn_ext o k rn_a o add k 1 rn_a o add k 2 |
| 1603 | p = rn_buf o k |
| 1604 | n = rn_a o add k 2 |
| 1605 | lim = ulong 4096 |
| 1606 | if eq kind 0 |
| 1607 | lim = ulong 65536 |
| 1608 | end |
| 1609 | if or lt n 2 gt n lim |
| 1610 | exit 103 |
| 1611 | end |
| 1612 | rn_pkrec p n kind |
| 1613 | end |
| 1614 | note rn_pkrec: the count and records of the packet of n bytes at p (rn_pkt); rn_pknul: no zero byte in m bytes. |
| 1615 | fn rn_pknul q ulong m ulong to void |
| 1616 | for i ulong m bound 65536 |
| 1617 | if eq get r_m add q i 0 |
| 1618 | exit 103 |
| 1619 | end |
| 1620 | end |
| 1621 | end |
| 1622 | fn rn_pkrec p ulong n ulong kind ulong to void |
| 1623 | c = rn_ld p 2 false |
| 1624 | cl = ulong 64 |
| 1625 | if eq kind 0 |
| 1626 | cl = ulong 1024 |
| 1627 | end |
| 1628 | if or eq c 0 gt c cl |
| 1629 | exit 103 |
| 1630 | end |
| 1631 | if and eq kind 1 ne c 2 |
| 1632 | exit 103 |
| 1633 | end |
| 1634 | cur = add p 2 |
| 1635 | stop = add p n |
| 1636 | for j ulong c bound 1024 |
| 1637 | if gt add cur 2 stop |
| 1638 | exit 103 |
| 1639 | end |
| 1640 | m = rn_ld cur 2 false |
| 1641 | cur = add cur 2 |
| 1642 | if gt m sub stop cur |
| 1643 | exit 103 |
| 1644 | end |
| 1645 | if eq m 0 |
| 1646 | if or ne kind 0 eq j 0 |
| 1647 | exit 103 |
| 1648 | end |
| 1649 | end |
| 1650 | rn_pknul cur m |
| 1651 | cur = add cur m |
| 1652 | end |
| 1653 | if ne cur stop |
| 1654 | exit 103 |
| 1655 | end |
| 1656 | end |
| 1657 | note rn_htrap: native trap 3 checks that precede any kernel call of a word ngrun refuses (OMEGESE s10; omc |
| 1658 | note hwaitio, hpeercr, hpollfds, hbuffer, hpack and the pipefds lowering): waitio events outside 0..65535, |
| 1659 | note a peercred output under 12 bytes, a pollfds length not a multiple of 8 or over 8192, a waitpid output |
| 1660 | note under 4 bytes, a pipefds output under 8 bytes and a malformed execprog, execpure, execenv or symlnk |
| 1661 | note packet. ngrun gives that trap before its refusal, so the program result equals the native one. |
| 1662 | fn rn_htrap o uint w ulong to void |
| 1663 | if eq w 123 |
| 1664 | e = bitcast long rn_a o 2 |
| 1665 | if or lt e 0 gt e 65535 |
| 1666 | exit 103 |
| 1667 | end |
| 1668 | end |
| 1669 | if eq w 220 |
| 1670 | if lt rn_a o 4 12 |
| 1671 | exit 103 |
| 1672 | end |
| 1673 | end |
| 1674 | if eq w 221 |
| 1675 | n = rn_a o 3 |
| 1676 | if or ne and n 7 0 gt n 8192 |
| 1677 | exit 103 |
| 1678 | end |
| 1679 | end |
| 1680 | if eq w 116 |
| 1681 | if lt rn_a o 4 4 |
| 1682 | exit 103 |
| 1683 | end |
| 1684 | end |
| 1685 | if eq w 128 |
| 1686 | if lt rn_a o 3 8 |
| 1687 | exit 103 |
| 1688 | end |
| 1689 | end |
| 1690 | if or eq w 124 eq w 198 |
| 1691 | rn_pkt o 1 0 |
| 1692 | end |
| 1693 | if eq w 233 |
| 1694 | rn_pkt o 1 0 |
| 1695 | rn_pkt o 4 2 |
| 1696 | end |
| 1697 | if eq w 142 |
| 1698 | rn_pkt o 1 1 |
| 1699 | end |
| 1700 | end |
| 1701 | fn rn_hargs o uint w ulong to void |
| 1702 | base = get r_cfg 1 |
| 1703 | count = add sub ulong argcnt base 1 |
| 1704 | if eq w 121 |
| 1705 | rn_hr o long count |
| 1706 | ret |
| 1707 | end |
| 1708 | if eq w 136 |
| 1709 | rn_hr o envcnt |
| 1710 | ret |
| 1711 | end |
| 1712 | rn_ext o 2 rn_a o 3 rn_a o 4 |
| 1713 | idx = rn_a o 1 |
| 1714 | p = rn_buf o 2 |
| 1715 | n = rn_a o 4 |
| 1716 | if eq w 137 |
| 1717 | rn_hr o envread idx r_m p n |
| 1718 | ret |
| 1719 | end |
| 1720 | if ge idx count |
| 1721 | exit 103 |
| 1722 | end |
| 1723 | real = ulong 1 |
| 1724 | if ne idx 0 |
| 1725 | real = add base sub idx 1 |
| 1726 | end |
| 1727 | rn_hr o argread real r_m p n |
| 1728 | end |
| 1729 | fn rn_hio o uint w ulong to void |
| 1730 | fd = rn_fget o 1 |
| 1731 | rn_ext o 2 rn_a o 3 rn_a o 4 |
| 1732 | if lt fd 0 |
| 1733 | rn_hr o -9 |
| 1734 | ret |
| 1735 | end |
| 1736 | vfd = rn_a o 1 |
| 1737 | if eq get r_own vfd 0 |
| 1738 | if eq w 53 |
| 1739 | if ne vfd 0 |
| 1740 | rn_href o 119 |
| 1741 | end |
| 1742 | else |
| 1743 | if eq w 54 |
| 1744 | if and ne vfd 1 ne vfd 2 |
| 1745 | rn_href o 119 |
| 1746 | end |
| 1747 | else |
| 1748 | rn_href o 119 |
| 1749 | end |
| 1750 | end |
| 1751 | end |
| 1752 | p = rn_buf o 2 |
| 1753 | n = rn_a o 4 |
| 1754 | if eq w 53 |
| 1755 | rn_hr o read fd r_m p n |
| 1756 | ret |
| 1757 | end |
| 1758 | if eq w 54 |
| 1759 | rn_hr o write fd r_m p n |
| 1760 | ret |
| 1761 | end |
| 1762 | pos = bitcast long rn_a o 5 |
| 1763 | if eq w 161 |
| 1764 | rn_hr o pread fd r_m p n pos |
| 1765 | ret |
| 1766 | end |
| 1767 | rn_hr o pwrite fd r_m p n pos |
| 1768 | end |
| 1769 | fn rn_hfd o uint w ulong to void |
| 1770 | if eq w 117 |
| 1771 | note fstat traps 3 natively on an output under 128 bytes before any kernel call, so ngrun gives that |
| 1772 | note trap before its descriptor checks. |
| 1773 | if lt rn_a o 4 128 |
| 1774 | exit 103 |
| 1775 | end |
| 1776 | end |
| 1777 | fd = rn_fget o 1 |
| 1778 | if lt fd 0 |
| 1779 | rn_hr o -9 |
| 1780 | ret |
| 1781 | end |
| 1782 | v = rn_a o 1 |
| 1783 | if eq w 100 |
| 1784 | put r_fd v -1 |
| 1785 | put r_own v 0 |
| 1786 | rn_hr o fclose fd |
| 1787 | ret |
| 1788 | end |
| 1789 | if eq get r_own v 0 |
| 1790 | rn_href o 119 |
| 1791 | end |
| 1792 | if eq w 101 |
| 1793 | rn_hr o fsync fd |
| 1794 | ret |
| 1795 | end |
| 1796 | if eq w 156 |
| 1797 | rn_hr o fdsync fd |
| 1798 | ret |
| 1799 | end |
| 1800 | if eq w 102 |
| 1801 | mode = int bitcast long rn_a o 2 |
| 1802 | rn_hr o fchmod fd mode |
| 1803 | ret |
| 1804 | end |
| 1805 | if eq w 103 |
| 1806 | pos = bitcast long rn_a o 2 |
| 1807 | whence = int bitcast long rn_a o 3 |
| 1808 | rn_hr o fseek fd pos whence |
| 1809 | ret |
| 1810 | end |
| 1811 | if eq w 139 |
| 1812 | rn_hr o ftrunc fd bitcast long rn_a o 2 |
| 1813 | ret |
| 1814 | end |
| 1815 | if eq w 153 |
| 1816 | rn_hr o fchdir fd |
| 1817 | ret |
| 1818 | end |
| 1819 | if eq w 197 |
| 1820 | op = int bitcast long rn_a o 2 |
| 1821 | if and ne op 8 eq and op 4 0 |
| 1822 | rn_href o 121 |
| 1823 | end |
| 1824 | rn_hr o flock fd op |
| 1825 | ret |
| 1826 | end |
| 1827 | rn_ext o 2 rn_a o 3 rn_a o 4 |
| 1828 | p = rn_buf o 2 |
| 1829 | n = rn_a o 4 |
| 1830 | if eq w 113 |
| 1831 | rn_hr o getdnts fd r_m p n |
| 1832 | ret |
| 1833 | end |
| 1834 | rn_hr o fstat fd r_m p n |
| 1835 | end |
| 1836 | note chmod, faccess, stat, truncate and chown follow a final link natively, so a link another process |
| 1837 | note plants in the scratch tree would take them outside it. ngrun opens the name O_PATH|O_NOFOLLOW|O_CLOEXEC |
| 1838 | note (AArch64 2097152, 32768, 524288), answers a link with -40 (ELOOP) as a NOFOLLOW open does, and gives |
| 1839 | note the native word /proc/self/fd/N, which names the inode that open found. rn_nofol returns that |
| 1840 | note descriptor or -errno; rn_ppath writes /proc/self/fd/N into r_pp and returns its length. |
| 1841 | fn rn_nofol p ulong n ulong to long |
| 1842 | flags = or int 2097152 or int 32768 int 524288 |
| 1843 | fd = fopen r_m p n flags 0 |
| 1844 | if lt fd 0 |
| 1845 | ret fd |
| 1846 | end |
| 1847 | k = fstat int fd r_pst 0 len r_pst |
| 1848 | if lt k 0 |
| 1849 | cl = fclose int fd |
| 1850 | ret k |
| 1851 | end |
| 1852 | note st_mode is bytes 16..19 of the AArch64 stat structure; S_IFMT 0xF000 and S_IFLNK 0xA000 are the |
| 1853 | note high nibble of byte 17. |
| 1854 | if eq and get r_pst 17 240 160 |
| 1855 | cl = fclose int fd |
| 1856 | ret -40 |
| 1857 | end |
| 1858 | ret fd |
| 1859 | end |
| 1860 | fn rn_ppath fd long to ulong |
| 1861 | for j ulong len r_ppre bound 16 |
| 1862 | put r_pp j get r_ppre j |
| 1863 | end |
| 1864 | i = ulong len r_ppre |
| 1865 | v = ulong fd |
| 1866 | d = ulong 1 |
| 1867 | while ge div v d 10 bound 20 |
| 1868 | d = mul d 10 |
| 1869 | end |
| 1870 | while gt d 0 bound 20 |
| 1871 | put r_pp i uchar add 48 rem div v d 10 |
| 1872 | i = add i 1 |
| 1873 | d = div d 10 |
| 1874 | end |
| 1875 | ret i |
| 1876 | end |
| 1877 | note The kernel's EINVAL checks that precede the lookup (faccess mode outside 0..7, truncate length below |
| 1878 | note 0) come first, as natively; then the name is opened without following a link (rn_nofol). |
| 1879 | fn rn_hnofol o uint w ulong p ulong n ulong to void |
| 1880 | a = bitcast long rn_a o 4 |
| 1881 | if and eq w 144 or lt a 0 gt a 7 |
| 1882 | rn_hr o -22 |
| 1883 | ret |
| 1884 | end |
| 1885 | if and eq w 150 lt a 0 |
| 1886 | rn_hr o -22 |
| 1887 | ret |
| 1888 | end |
| 1889 | outp = ulong 0 |
| 1890 | if eq w 140 |
| 1891 | rn_ext o 4 rn_a o 5 rn_a o 6 |
| 1892 | outp = rn_buf o 4 |
| 1893 | end |
| 1894 | fd = rn_nofol p n |
| 1895 | if lt fd 0 |
| 1896 | rn_hr o fd |
| 1897 | ret |
| 1898 | end |
| 1899 | m = rn_ppath fd |
| 1900 | k = long 0 |
| 1901 | if eq w 140 |
| 1902 | k = stat r_pp 0 m r_m outp rn_a o 6 |
| 1903 | end |
| 1904 | if eq w 143 |
| 1905 | k = chmod r_pp 0 m int a |
| 1906 | end |
| 1907 | if eq w 144 |
| 1908 | k = faccess r_pp 0 m int a |
| 1909 | end |
| 1910 | if eq w 150 |
| 1911 | k = truncate r_pp 0 m a |
| 1912 | end |
| 1913 | if eq w 151 |
| 1914 | k = chown r_pp 0 m int a int bitcast long rn_a o 5 |
| 1915 | end |
| 1916 | cl = fclose int fd |
| 1917 | rn_hr o k |
| 1918 | end |
| 1919 | fn rn_hpath o uint w ulong to void |
| 1920 | if or or eq w 133 eq w 140 eq w 149 |
| 1921 | note stat, lstat and readlnk trap 3 natively on an empty path, and stat and lstat on an output under |
| 1922 | note 128 bytes, before any kernel call and with or without --scratch, so ngrun gives that trap first. |
| 1923 | if eq rn_a o 3 0 |
| 1924 | exit 103 |
| 1925 | end |
| 1926 | if and ne w 133 lt rn_a o 6 128 |
| 1927 | exit 103 |
| 1928 | end |
| 1929 | end |
| 1930 | p = rn_path o 1 |
| 1931 | n = rn_a o 3 |
| 1932 | if eq w 110 |
| 1933 | flags = int bitcast long rn_a o 4 |
| 1934 | mode = int bitcast long rn_a o 5 |
| 1935 | note NOFOLLOW prevents an open through a link even if another same-user process plants one. |
| 1936 | flags = or flags 32768 |
| 1937 | rn_hr o rn_fadd o fopen r_m p n flags mode |
| 1938 | ret |
| 1939 | end |
| 1940 | if eq w 111 |
| 1941 | rn_hr o unlink r_m p n |
| 1942 | ret |
| 1943 | end |
| 1944 | if eq w 112 |
| 1945 | mode = int bitcast long rn_a o 4 |
| 1946 | rn_hr o makedir r_m p n mode |
| 1947 | ret |
| 1948 | end |
| 1949 | if eq w 126 |
| 1950 | rn_hr o rmdir r_m p n |
| 1951 | ret |
| 1952 | end |
| 1953 | if eq w 138 |
| 1954 | note chdir through an O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC descriptor (AArch64 2097152, 16384, 32768, |
| 1955 | note 524288): a final link another process plants is not followed, so the working directory stays inside |
| 1956 | note the scratch tree. Search permission is checked by fchdir as chdir checks it. |
| 1957 | flags = or int 2097152 or int 16384 or int 32768 int 524288 |
| 1958 | fd = fopen r_m p n flags 0 |
| 1959 | if lt fd 0 |
| 1960 | rn_hr o fd |
| 1961 | ret |
| 1962 | end |
| 1963 | k = fchdir int fd |
| 1964 | cl = fclose int fd |
| 1965 | rn_hr o k |
| 1966 | ret |
| 1967 | end |
| 1968 | if or or or eq w 140 eq w 143 or eq w 144 eq w 150 eq w 151 |
| 1969 | rn_hnofol o w p n |
| 1970 | ret |
| 1971 | end |
| 1972 | rn_ext o 4 rn_a o 5 rn_a o 6 |
| 1973 | outp = rn_buf o 4 |
| 1974 | cap = rn_a o 6 |
| 1975 | if eq w 133 |
| 1976 | rn_hr o readlnk r_m p n r_m outp cap |
| 1977 | ret |
| 1978 | end |
| 1979 | rn_hr o lstat r_m p n r_m outp cap |
| 1980 | end |
| 1981 | fn rn_safehost o uint to void |
| 1982 | w = get i_oim o |
| 1983 | rn_htrap o w |
| 1984 | if or eq w 199 eq w 200 |
| 1985 | ret |
| 1986 | end |
| 1987 | if eq w 109 |
| 1988 | rn_hr o monons |
| 1989 | ret |
| 1990 | end |
| 1991 | if eq w 130 |
| 1992 | rn_hr o realns |
| 1993 | ret |
| 1994 | end |
| 1995 | if or or eq w 121 eq w 122 or eq w 136 eq w 137 |
| 1996 | rn_hargs o w |
| 1997 | ret |
| 1998 | end |
| 1999 | if or or eq w 53 eq w 54 or eq w 161 eq w 162 |
| 2000 | rn_hio o w |
| 2001 | ret |
| 2002 | end |
| 2003 | if or eq w 145 eq w 146 |
| 2004 | if eq w 145 |
| 2005 | rn_hr o getuid |
| 2006 | else |
| 2007 | rn_hr o geteuid |
| 2008 | end |
| 2009 | ret |
| 2010 | end |
| 2011 | if or eq w 147 eq w 148 |
| 2012 | if eq w 147 |
| 2013 | rn_hr o getgid |
| 2014 | else |
| 2015 | rn_hr o getegid |
| 2016 | end |
| 2017 | ret |
| 2018 | end |
| 2019 | if eq w 235 |
| 2020 | rn_href o 116 |
| 2021 | end |
| 2022 | if or and ge w 166 le w 177 and ge w 228 le w 232 |
| 2023 | rn_href o 117 |
| 2024 | end |
| 2025 | if or and ge w 104 le w 106 or eq w 114 eq w 115 |
| 2026 | rn_href o 118 |
| 2027 | end |
| 2028 | if or or eq w 118 eq w 119 or eq w 123 eq w 131 |
| 2029 | rn_href o 118 |
| 2030 | end |
| 2031 | if or eq w 134 or eq w 165 or and ge w 220 le w 222 eq w 224 |
| 2032 | rn_href o 118 |
| 2033 | end |
| 2034 | if or eq w 135 eq w 120 |
| 2035 | if eq w 120 |
| 2036 | note sleepns traps 3 natively on a negative interval (OMEGESE s10): a deterministic result, not a wait. |
| 2037 | if lt bitcast long rn_a o 1 0 |
| 2038 | exit 103 |
| 2039 | end |
| 2040 | if eq rn_a o 1 0 |
| 2041 | rn_hr o sleepns 0 |
| 2042 | ret |
| 2043 | end |
| 2044 | end |
| 2045 | rn_href o 120 |
| 2046 | end |
| 2047 | note the library words of `target macos` call code outside the program: refused as class 117 |
| 2048 | if and ge w 241 le w 243 |
| 2049 | rn_href o 117 |
| 2050 | end |
| 2051 | if or or eq w 107 eq w 108 or eq w 116 eq w 124 |
| 2052 | rn_href o 115 |
| 2053 | end |
| 2054 | if or or eq w 127 eq w 128 or eq w 129 eq w 154 |
| 2055 | rn_href o 115 |
| 2056 | end |
| 2057 | if or and ge w 158 le w 160 or eq w 163 or eq w 198 eq w 233 |
| 2058 | rn_href o 115 |
| 2059 | end |
| 2060 | note cpuset, cpuget, setprio and getprio read or change the scheduling state of the process (115); |
| 2061 | note cpunow gives the CPU the thread ran on, which may change between calls (120). IR.md s7.11. |
| 2062 | if or and ge w 236 le w 237 or eq w 239 eq w 240 |
| 2063 | rn_href o 115 |
| 2064 | end |
| 2065 | if eq w 238 |
| 2066 | rn_href o 120 |
| 2067 | end |
| 2068 | if or eq w 142 eq w 164 |
| 2069 | rn_href o 121 |
| 2070 | end |
| 2071 | if or or eq w 100 eq w 101 or eq w 102 eq w 103 |
| 2072 | rn_hfd o w |
| 2073 | ret |
| 2074 | end |
| 2075 | if or or eq w 113 eq w 117 or eq w 139 eq w 153 |
| 2076 | rn_hfd o w |
| 2077 | ret |
| 2078 | end |
| 2079 | if or eq w 156 eq w 197 |
| 2080 | rn_hfd o w |
| 2081 | ret |
| 2082 | end |
| 2083 | if or and ge w 110 le w 112 or eq w 126 eq w 138 |
| 2084 | rn_hpath o w |
| 2085 | ret |
| 2086 | end |
| 2087 | if or or eq w 133 eq w 140 or eq w 143 eq w 144 |
| 2088 | rn_hpath o w |
| 2089 | ret |
| 2090 | end |
| 2091 | if or eq w 149 or eq w 150 eq w 151 |
| 2092 | rn_hpath o w |
| 2093 | ret |
| 2094 | end |
| 2095 | if eq w 132 |
| 2096 | if eq get r_cfg 2 0 |
| 2097 | rn_href o 119 |
| 2098 | end |
| 2099 | rn_ext o 1 rn_a o 2 rn_a o 3 |
| 2100 | rn_hr o cwd r_m rn_buf o 1 rn_a o 3 |
| 2101 | ret |
| 2102 | end |
| 2103 | if eq w 155 |
| 2104 | if eq get r_cfg 2 0 |
| 2105 | rn_href o 119 |
| 2106 | end |
| 2107 | rn_hr o umask int bitcast long rn_a o 1 |
| 2108 | ret |
| 2109 | end |
| 2110 | if eq w 157 |
| 2111 | rn_ext o 1 rn_a o 2 rn_a o 3 |
| 2112 | p = rn_buf o 1 |
| 2113 | flags = int bitcast long rn_a o 4 |
| 2114 | rn_hr o rn_fadd o memfd r_m p rn_a o 3 flags |
| 2115 | ret |
| 2116 | end |
| 2117 | if or eq w 125 eq w 141 |
| 2118 | p = rn_pair o |
| 2119 | if eq w 125 |
| 2120 | flags = int bitcast long rn_a o 4 |
| 2121 | rn_hr o renames r_m p rn_a o 3 flags |
| 2122 | else |
| 2123 | rn_hr o link r_m p rn_a o 3 |
| 2124 | end |
| 2125 | ret |
| 2126 | end |
| 2127 | rn_href o 122 |
| 2128 | end |
| 2129 | |
| 2130 | |
| 2131 | note Pool storage is separate from invocation frames. The combined live byte budget stays 16 MiB. |
| 2132 | note References never escape a pool op; compaction after growth cannot invalidate an IR value. |
| 2133 | fn rn_pgrow g ulong n ulong to long |
| 2134 | old = get r_pl g |
| 2135 | if le n old |
| 2136 | ret 0 |
| 2137 | end |
| 2138 | sz = ir_tsize ar_u 87 g |
| 2139 | if gt n div 9223372036854775807 sz |
| 2140 | ret -22 |
| 2141 | end |
| 2142 | delta = mul sub n old sz |
| 2143 | used = get r_ps 0 |
| 2144 | if gt delta sub sub 16777216 get r_s 2 used |
| 2145 | ret -12 |
| 2146 | end |
| 2147 | cut = add get r_pb g mul old sz |
| 2148 | rn_pmove g cut delta |
| 2149 | put r_pl g n |
| 2150 | put r_ps 0 add used delta |
| 2151 | ret 0 |
| 2152 | end |
| 2153 | |
| 2154 | fn rn_pio o uint op uint g ulong to void |
| 2155 | off = rn_a o 2 |
| 2156 | n = rn_a o 3 |
| 2157 | ln = get r_pl g |
| 2158 | if gt off ln |
| 2159 | exit 103 |
| 2160 | end |
| 2161 | if gt n sub ln off |
| 2162 | exit 103 |
| 2163 | end |
| 2164 | fd = rn_fget o 1 |
| 2165 | if lt fd 0 |
| 2166 | rn_hr o -9 |
| 2167 | ret |
| 2168 | end |
| 2169 | vfd = rn_a o 1 |
| 2170 | if eq get r_own vfd 0 |
| 2171 | if eq op 115 |
| 2172 | if ne vfd 0 |
| 2173 | rn_href o 119 |
| 2174 | end |
| 2175 | else |
| 2176 | if and ne vfd 1 ne vfd 2 |
| 2177 | rn_href o 119 |
| 2178 | end |
| 2179 | end |
| 2180 | end |
| 2181 | p = add get r_pb g off |
| 2182 | if eq op 115 |
| 2183 | rn_hr o read fd r_pm p n |
| 2184 | else |
| 2185 | rn_hr o write fd r_pm p n |
| 2186 | end |
| 2187 | end |
| 2188 | |
| 2189 | fn rn_pool o uint op uint to void |
| 2190 | g = get i_oim o |
| 2191 | if eq op 111 |
| 2192 | rn_hr o rn_pgrow g rn_a o 1 |
| 2193 | ret |
| 2194 | end |
| 2195 | if eq op 112 |
| 2196 | rn_r o get r_pl g |
| 2197 | ret |
| 2198 | end |
| 2199 | if ge op 115 |
| 2200 | rn_pio o op g |
| 2201 | ret |
| 2202 | end |
| 2203 | ix = rn_a o 1 |
| 2204 | if ge ix get r_pl g |
| 2205 | exit 103 |
| 2206 | end |
| 2207 | t = ar_u 87 g |
| 2208 | sz = ir_tsize t |
| 2209 | p = add get r_pb g mul ix sz |
| 2210 | if eq op 114 |
| 2211 | rn_pst p sz rn_a o 2 |
| 2212 | ret |
| 2213 | end |
| 2214 | v = rn_pld p sz |
| 2215 | if rn_isi t |
| 2216 | v = rn_cn t v |
| 2217 | end |
| 2218 | rn_r o v |
| 2219 | end |
| 2220 | |
| 2221 | fn rn_pmove g ulong cut ulong delta ulong to void |
| 2222 | used = get r_ps 0 |
| 2223 | for j ulong sub used cut bound 16777216 |
| 2224 | src = sub sub used 1 j |
| 2225 | put r_pm add src delta get r_pm src |
| 2226 | end |
| 2227 | for j ulong delta bound 16777216 |
| 2228 | put r_pm add cut j 0 |
| 2229 | end |
| 2230 | for j ulong get i_n 2 bound 65536 |
| 2231 | h = add j 1 |
| 2232 | if and gt h g eq ar_c 88 h 2 |
| 2233 | put r_pb h add get r_pb h delta |
| 2234 | end |
| 2235 | end |
| 2236 | end |
| 2237 | |
| 2238 | |
| 2239 | fn rn_pst p ulong sz ulong v ulong to void |
| 2240 | for j ulong sz bound 8 |
| 2241 | put r_pm add p j uchar and shr v mul j 8 255 |
| 2242 | end |
| 2243 | end |
| 2244 | fn rn_pld p ulong sz ulong to ulong |
| 2245 | v = ulong 0 |
| 2246 | for j ulong sz bound 8 |
| 2247 | v = or v shl ulong get r_pm add p j mul j 8 |
| 2248 | end |
| 2249 | ret v |
| 2250 | end |