omega-mcp
aien-dev/omega-mcp
Brought by drakestapleton
omega-mcp / tools / ref_examples.py
| 1 | # Example programs for data/examples (authoring input of tools/gen-reference.py). id -> (source, expect) |
| 2 | EX = {} |
| 3 | |
| 4 | |
| 5 | def ex(i, src, **exp): |
| 6 | EX[i] = (src.lstrip("\n"), exp) |
| 7 | |
| 8 | |
| 9 | ex("hello", r''' |
| 10 | note write the bytes of a text to stdout (fd 1); main's value is the exit status |
| 11 | text msg "hello\n" |
| 12 | fn main to int |
| 13 | w = write 1 msg 0 len msg |
| 14 | ret 0 |
| 15 | end |
| 16 | ''', mode="run", exit=0, stdout="hello\n") |
| 17 | |
| 18 | ex("ints", ''' |
| 19 | note every integer width; each value converts to long and the sum modulo 256 is the exit status |
| 20 | fn main to int |
| 21 | s = long uchar 200 |
| 22 | s = add s long u8 1 |
| 23 | s = add s long ushort 300 |
| 24 | s = add s long u16 2 |
| 25 | s = add s long uint 3 |
| 26 | s = add s long u32 4 |
| 27 | s = add s long ulong 5 |
| 28 | s = add s long u64 6 |
| 29 | s = add s long byte -7 |
| 30 | s = add s long i8 8 |
| 31 | s = add s long short -9 |
| 32 | s = add s long i16 10 |
| 33 | s = add s long int 11 |
| 34 | s = add s long i32 12 |
| 35 | s = add s long 13 |
| 36 | s = add s i64 14 |
| 37 | ret int rem s 256 |
| 38 | end |
| 39 | ''', mode="run", exit=61) |
| 40 | |
| 41 | ex("arith", ''' |
| 42 | note signed and unsigned arithmetic, bit operations, comparisons and not |
| 43 | fn main to int |
| 44 | a = int 17 |
| 45 | b = int 5 |
| 46 | s = add a b |
| 47 | s = sub s mul a b |
| 48 | s = add s div a b |
| 49 | s = add s rem a b |
| 50 | x = uint 12 |
| 51 | x = and x 10 |
| 52 | x = or x 3 |
| 53 | x = xor x 6 |
| 54 | x = shl x 2 |
| 55 | x = shr x 1 |
| 56 | n = neg s |
| 57 | k = int 0 |
| 58 | if eq n 58 |
| 59 | k = add k 1 |
| 60 | end |
| 61 | if ne a b |
| 62 | k = add k 1 |
| 63 | end |
| 64 | if lt b a |
| 65 | k = add k 1 |
| 66 | end |
| 67 | if le a 17 |
| 68 | k = add k 1 |
| 69 | end |
| 70 | if gt a b |
| 71 | k = add k 1 |
| 72 | end |
| 73 | if ge b 5 |
| 74 | k = add k 1 |
| 75 | end |
| 76 | if not lt a b |
| 77 | k = add k 1 |
| 78 | end |
| 79 | ret add add n int x k |
| 80 | end |
| 81 | ''', mode="run", exit=91) |
| 82 | |
| 83 | ex("control", ''' |
| 84 | note while with a bound, continue, break, for, if/else and bool values |
| 85 | fn pick c bool to uint |
| 86 | if c |
| 87 | ret 100 |
| 88 | else |
| 89 | ret 1 |
| 90 | end |
| 91 | end |
| 92 | fn main to int |
| 93 | s = uint 0 |
| 94 | i = uint 0 |
| 95 | while lt i 100 bound 1000 |
| 96 | i = add i 1 |
| 97 | if eq rem i 2 0 |
| 98 | continue |
| 99 | end |
| 100 | if gt i 9 |
| 101 | break |
| 102 | end |
| 103 | s = add s i |
| 104 | end |
| 105 | for j uint 4 |
| 106 | s = add s j |
| 107 | end |
| 108 | t = true |
| 109 | f = false |
| 110 | s = add s pick and t not f |
| 111 | ret int s |
| 112 | end |
| 113 | ''', mode="run", exit=131) |
| 114 | |
| 115 | ex("for_guard", ''' |
| 116 | note a for with a number count is unguarded; any other count traps 4 after 1000000 passes |
| 117 | fn main to int |
| 118 | s = ulong 0 |
| 119 | for i ulong 2000000 |
| 120 | s = add s 1 |
| 121 | end |
| 122 | k = ulong 1000001 |
| 123 | for j ulong k |
| 124 | s = add s 1 |
| 125 | end |
| 126 | ret int sub s 3000001 |
| 127 | end |
| 128 | ''', mode="trap", trap=4) |
| 129 | |
| 130 | ex("arrays", ''' |
| 131 | note a local array (new), a global pool, view and edit parameters, get, put and len |
| 132 | global tot ulong 1 |
| 133 | fn sum a view uint 4 to uint |
| 134 | s = uint 0 |
| 135 | for i ulong len a |
| 136 | s = add s get a i |
| 137 | end |
| 138 | ret s |
| 139 | end |
| 140 | fn fill a edit uint 4 to void |
| 141 | for i ulong 4 |
| 142 | put a i uint add i 1 |
| 143 | end |
| 144 | end |
| 145 | fn main to int |
| 146 | a = new uint 4 0 |
| 147 | fill a |
| 148 | put tot 0 ulong sum a |
| 149 | ret int get tot 0 |
| 150 | end |
| 151 | ''', mode="run", exit=10) |
| 152 | |
| 153 | ex("calls", ''' |
| 154 | note recursion, a void function, ret with and without a value, grouping parentheses |
| 155 | fn fib n uint to uint |
| 156 | if lt n 2 |
| 157 | ret n |
| 158 | end |
| 159 | ret add (fib sub n 1) (fib sub n 2) |
| 160 | end |
| 161 | fn nothing v uint to void |
| 162 | if gt v 1 |
| 163 | ret |
| 164 | end |
| 165 | end |
| 166 | fn main to int |
| 167 | nothing 3 |
| 168 | x = (fib 10) |
| 169 | ret int x |
| 170 | end |
| 171 | ''', mode="run", exit=55) |
| 172 | |
| 173 | ex("exit", ''' |
| 174 | note exit ends the program at once; the status is the value modulo 256 |
| 175 | fn main to int |
| 176 | exit 300 |
| 177 | end |
| 178 | ''', mode="run", exit=44) |
| 179 | |
| 180 | ex("trapword", ''' |
| 181 | note trap N stops the program; in program mode the exit status is 100 + N |
| 182 | fn main to int |
| 183 | trap 4 |
| 184 | end |
| 185 | ''', mode="trap", trap=4) |
| 186 | |
| 187 | ex("free_refused", ''' |
| 188 | fn main to int |
| 189 | a = new uchar 4 0 |
| 190 | free a |
| 191 | ret 0 |
| 192 | end |
| 193 | ''', mode="error", code=9, line=3) |
| 194 | |
| 195 | ex("own_refused", ''' |
| 196 | fn main to int |
| 197 | a = new uchar 4 0 |
| 198 | own a |
| 199 | ret 0 |
| 200 | end |
| 201 | ''', mode="error", code=9, line=3) |
| 202 | |
| 203 | ex("structs", ''' |
| 204 | note a struct on the stack, field get and put, sizeof and offsetof |
| 205 | struct Pt x long y long end |
| 206 | fn main to int |
| 207 | p = new Pt 3 4 |
| 208 | put p x add get p x 10 |
| 209 | s = add get p x get p y |
| 210 | z = add sizeof Pt offsetof Pt y |
| 211 | ret int add s long z |
| 212 | end |
| 213 | ''', mode="run", exit=41) |
| 214 | |
| 215 | ex("floats", ''' |
| 216 | note f64 and f32 values, fsqrt, fabs, fmin, fmax, conversions and bitcast |
| 217 | fn main to int |
| 218 | if ne bitcast ulong 1.0f64 4607182418800017408 |
| 219 | ret 1 |
| 220 | end |
| 221 | r = fsqrt 16.0f64 |
| 222 | b = fabs neg 1.5f64 |
| 223 | c = fmax b fmin 0.25f64 3.0f64 |
| 224 | d = f32 1.5f64 |
| 225 | v = add mul r 2.0f64 add c f64 d |
| 226 | ret int v |
| 227 | end |
| 228 | ''', mode="run", exit=11) |
| 229 | |
| 230 | ex("consts", ''' |
| 231 | note named typed constants, used as an array length, a for count and a value |
| 232 | const lim uint 5 |
| 233 | const seven uint 7 |
| 234 | fn main to int |
| 235 | a = new uint lim seven |
| 236 | s = uint 0 |
| 237 | for i uint lim |
| 238 | s = add s get a i |
| 239 | end |
| 240 | ret int s |
| 241 | end |
| 242 | ''', mode="run", exit=35) |
| 243 | |
| 244 | ex("om2_rotate", ''' |
| 245 | om2 |
| 246 | note rotr and rotl rotate an unsigned value; amounts outside 0..width-1 trap 7 |
| 247 | fn main to int |
| 248 | x = uint 0x80000001 |
| 249 | y = rotr x 1 |
| 250 | z = rotl y 2 |
| 251 | ret int z |
| 252 | end |
| 253 | ''', mode="run", exit=3) |
| 254 | |
| 255 | ex("om2_unroll", ''' |
| 256 | om2 |
| 257 | note unroll compiles the body K times with a constant counter; inline compiles each call in place |
| 258 | fn sq x ulong to ulong inline |
| 259 | ret mul x x |
| 260 | end |
| 261 | fn main to int |
| 262 | s = ulong 0 |
| 263 | for i ulong 4 unroll |
| 264 | s = add s sq i |
| 265 | end |
| 266 | ret int s |
| 267 | end |
| 268 | ''', mode="run", exit=14) |
| 269 | |
| 270 | ex("om2_abstract", ''' |
| 271 | om2 |
| 272 | note abstract types: values made only by seal in NAMEmk, read by open, operations listed by ops; |
| 273 | note holds dup32 checks every seal at run time (trap 10) |
| 274 | abstract tag uint ops and or |
| 275 | abstract dup32 ulong ops xor |
| 276 | holds dup32 dup32 |
| 277 | fn tagmk x uint to tag |
| 278 | ret seal tag x |
| 279 | end |
| 280 | fn dup32mk x uint to dup32 |
| 281 | ret seal dup32 or shl ulong x 32 ulong x |
| 282 | end |
| 283 | fn main to int |
| 284 | t = or tagmk 12 tagmk 3 |
| 285 | u = and t tagmk 5 |
| 286 | d = xor dup32mk 7 dup32mk 4 |
| 287 | if ne open d 12884901891 |
| 288 | ret 1 |
| 289 | end |
| 290 | ret int open u |
| 291 | end |
| 292 | ''', mode="run", exit=5) |
| 293 | |
| 294 | ex("om2_fresh", ''' |
| 295 | om2 |
| 296 | note fresh: incoming elements are unspecified, so every element must be written before it is read |
| 297 | fn f s edit uchar 4 fresh to ulong |
| 298 | for i ulong 4 unroll |
| 299 | put s i uchar add i 1 |
| 300 | end |
| 301 | ret add ulong get s 0 ulong get s 3 |
| 302 | end |
| 303 | fn main to int |
| 304 | a = new uchar 4 0 |
| 305 | ret int f a |
| 306 | end |
| 307 | ''', mode="run", exit=5) |
| 308 | |
| 309 | ex("om2_layout", ''' |
| 310 | om2 |
| 311 | note an external byte layout: fields at fixed offsets with a byte order, over a uchar array of SIZE bytes |
| 312 | layout hdr 6 |
| 313 | field magic ushort at 0 big |
| 314 | field size uint at 2 little |
| 315 | end |
| 316 | fn setc c edit hdr to void |
| 317 | put c magic 0x4f4d |
| 318 | put c size 258 |
| 319 | end |
| 320 | fn main to int |
| 321 | c = new uchar 6 0 |
| 322 | setc c |
| 323 | ret int add get c 0 get c 2 |
| 324 | end |
| 325 | ''', mode="run", exit=81) |
| 326 | |
| 327 | ex("om2_part", ''' |
| 328 | om2 |
| 329 | note part A o k passes elements o..o+k-1 to an unsized view or edit parameter (T *) |
| 330 | text msg "0123456789" |
| 331 | fn first v view uchar * to ulong |
| 332 | ret ulong get v 0 |
| 333 | end |
| 334 | fn main to int |
| 335 | ret int first part msg 7 3 |
| 336 | end |
| 337 | ''', mode="run", exit=55) |
| 338 | |
| 339 | ex("om2_out", ''' |
| 340 | om2 |
| 341 | note an out parameter is written by the unit; inside the unit it is an edit of T N |
| 342 | fn fill r out ulong 3 v ulong to ulong |
| 343 | for i ulong 3 |
| 344 | put r i add v i |
| 345 | end |
| 346 | ret get r 2 |
| 347 | end |
| 348 | fn main to int |
| 349 | a = new ulong 3 0 |
| 350 | ret int fill a 10 |
| 351 | end |
| 352 | ''', mode="run", exit=12) |
| 353 | |
| 354 | ex("om2_export", ''' |
| 355 | om2 |
| 356 | note function mode: export marks an entry; word 0 branches to it. Compile only (no fn main). |
| 357 | export fn twice a ulong to ulong |
| 358 | ret add a a |
| 359 | end |
| 360 | ''', mode="compile") |
| 361 | |
| 362 | ex("contracts", ''' |
| 363 | om2 |
| 364 | note requires and ensures form a function's prefix; invariant forms a loop body's prefix; |
| 365 | note result is the returned value inside ensures |
| 366 | fn step x uint to uint |
| 367 | requires lt x 10 |
| 368 | ensures eq result add x 1 |
| 369 | ret add x 1 |
| 370 | end |
| 371 | fn main to int |
| 372 | i = uint 0 |
| 373 | while lt i 3 |
| 374 | invariant le i 3 |
| 375 | i = step i |
| 376 | end |
| 377 | ret int sub i 3 |
| 378 | end |
| 379 | ''', mode="run", exit=0) |
| 380 | |
| 381 | ex("contract_fail", ''' |
| 382 | om2 |
| 383 | note a false requires stops the program with trap 9 (exit 109) |
| 384 | fn step x uint to uint |
| 385 | requires lt x 2 |
| 386 | ret add x 1 |
| 387 | end |
| 388 | fn main to int |
| 389 | ret int step 5 |
| 390 | end |
| 391 | ''', mode="trap", trap=9) |
| 392 | |
| 393 | ex("files", ''' |
| 394 | note open, write, sync, lock, seek, read, positioned read and write, truncate, stat, chmod, unlink, memfd |
| 395 | text path "demo.txt" |
| 396 | global data uchar 5 |
| 397 | global buf uchar 128 |
| 398 | fn ck yes bool to void |
| 399 | if not yes |
| 400 | exit 80 |
| 401 | end |
| 402 | end |
| 403 | fn main to int |
| 404 | for i ulong 5 |
| 405 | put data i uchar add 97 i |
| 406 | end |
| 407 | old = umask 18 |
| 408 | fd = int fopen path 0 len path 578 420 |
| 409 | ck ge fd 0 |
| 410 | ck eq write fd data 0 len data 5 |
| 411 | ck eq fsync fd 0 |
| 412 | ck eq fdsync fd 0 |
| 413 | ck eq flock fd 2 0 |
| 414 | ck eq syncfs fd 0 |
| 415 | ck eq pwrite fd data 0 1 5 1 |
| 416 | ck eq fseek fd 0 0 0 |
| 417 | ck eq read fd buf 0 6 6 |
| 418 | ck eq pread fd buf 0 2 3 2 |
| 419 | ck eq ftrunc fd 3 0 |
| 420 | ck eq fstat fd buf 0 128 0 |
| 421 | ck eq fchmod fd 384 0 |
| 422 | ck eq fclose fd 0 |
| 423 | ck eq stat path 0 len path buf 0 128 0 |
| 424 | ck eq lstat path 0 len path buf 0 128 0 |
| 425 | ck eq chmod path 0 len path 420 0 |
| 426 | ck eq faccess path 0 len path 4 0 |
| 427 | ck eq truncate path 0 len path 1 0 |
| 428 | ck eq unlink path 0 len path 0 |
| 429 | m = memfd path 0 len path 0 |
| 430 | ck ge m 0 |
| 431 | ret 0 |
| 432 | end |
| 433 | ''', mode="run", exit=0) |
| 434 | |
| 435 | ex("dirs", ''' |
| 436 | note directories, links and renames; paths pairs are packed lists (u16 count, then u16 length + bytes) |
| 437 | text dir "sub" |
| 438 | text gone "tmp" |
| 439 | text fa "a" |
| 440 | text dot "." |
| 441 | global buf uchar 512 |
| 442 | fn ck yes bool to void |
| 443 | if not yes |
| 444 | exit 80 |
| 445 | end |
| 446 | end |
| 447 | fn main to int |
| 448 | ck eq makedir dir 0 len dir 448 0 |
| 449 | ck eq makedir gone 0 len gone 448 0 |
| 450 | ck eq rmdir gone 0 len gone 0 |
| 451 | ck eq chdir dir 0 len dir 0 |
| 452 | ck gt cwd buf 0 512 0 |
| 453 | fd = int fopen fa 0 len fa 577 384 |
| 454 | ck ge fd 0 |
| 455 | ck eq fclose fd 0 |
| 456 | p = new uchar 8 0 |
| 457 | put p 0 2 |
| 458 | put p 2 1 |
| 459 | put p 4 97 |
| 460 | put p 5 1 |
| 461 | put p 7 98 |
| 462 | ck eq link p 0 8 0 |
| 463 | put p 7 99 |
| 464 | ck eq symlnk p 0 8 0 |
| 465 | c = new uchar 1 99 |
| 466 | ck eq readlnk c 0 1 buf 0 64 1 |
| 467 | put p 4 98 |
| 468 | put p 7 100 |
| 469 | ck eq renames p 0 8 1 0 |
| 470 | ck eq chown fa 0 len fa int getuid int getgid 0 |
| 471 | ck ge add geteuid getegid 0 |
| 472 | d = int fopen dot 0 len dot 0 0 |
| 473 | ck ge d 0 |
| 474 | ck gt getdnts d buf 0 512 0 |
| 475 | ck eq fchdir d 0 |
| 476 | ret 0 |
| 477 | end |
| 478 | ''', mode="run", exit=0) |
| 479 | |
| 480 | ex("procs", ''' |
| 481 | note fork, wait, signals, pipes, dup, process groups and sessions, and a failed execprog |
| 482 | global st uchar 4 |
| 483 | global ends uchar 8 |
| 484 | fn ck yes bool to void |
| 485 | if not yes |
| 486 | exit 80 |
| 487 | end |
| 488 | end |
| 489 | fn main to int |
| 490 | ck eq pipefds ends 0 8 0 0 |
| 491 | p = fork |
| 492 | if eq p 0 |
| 493 | exit 7 |
| 494 | end |
| 495 | ck gt p 0 |
| 496 | ck eq waitpid int p st 0 4 0 p |
| 497 | ck eq get st 1 7 |
| 498 | ck eq killpid int getpid 0 0 |
| 499 | ck ge getppid 0 |
| 500 | ck ge getpgid 0 0 |
| 501 | ck ge getsid 0 0 |
| 502 | r = setpgid 0 0 |
| 503 | s = setsid |
| 504 | ck eq dupfd 1 9 0 9 |
| 505 | x = new uchar 6 0 |
| 506 | put x 0 1 |
| 507 | put x 2 2 |
| 508 | put x 4 47 |
| 509 | put x 5 120 |
| 510 | ck eq execprog x 0 6 neg long 2 |
| 511 | ck eq execpure x 0 6 neg long 2 |
| 512 | ret 0 |
| 513 | end |
| 514 | ''', mode="run", exit=0) |
| 515 | |
| 516 | ex("argsenv", ''' |
| 517 | note the argument and environment vectors; the server runs programs with no arguments and an empty environment |
| 518 | global buf uchar 64 |
| 519 | fn main to int |
| 520 | n = argcnt |
| 521 | r = argread 0 buf 0 64 |
| 522 | if gt envcnt 0 |
| 523 | x = envread 0 buf 0 64 |
| 524 | end |
| 525 | ret int n |
| 526 | end |
| 527 | ''', mode="run", exit=1) |
| 528 | |
| 529 | ex("argtrap", ''' |
| 530 | note argread of an index at or past argcnt traps 3 (it does not return an errno); the server passes no arguments |
| 531 | global buf uchar 64 |
| 532 | fn main to int |
| 533 | r = argread 1 buf 0 64 |
| 534 | ret 0 |
| 535 | end |
| 536 | ''', mode="trap", trap=3) |
| 537 | |
| 538 | ex("clock", ''' |
| 539 | note monotonic and realtime clocks, a short sleep and random bytes |
| 540 | global buf uchar 16 |
| 541 | fn main to int |
| 542 | a = monons |
| 543 | r = sleepns 1000000 |
| 544 | b = monons |
| 545 | if lt sub b a 1000000 |
| 546 | ret 1 |
| 547 | end |
| 548 | if le realns 0 |
| 549 | ret 2 |
| 550 | end |
| 551 | if ne random buf 0 16 0 16 |
| 552 | ret 3 |
| 553 | end |
| 554 | ret 0 |
| 555 | end |
| 556 | ''', mode="run", exit=0) |
| 557 | |
| 558 | ex("sockets", ''' |
| 559 | note a Unix socket pair in the working directory: socket, option, bind, listen, connect, accept, |
| 560 | note send, wait, receive, peer identity, sendmsg, poll and shutdown |
| 561 | global buf uchar 64 |
| 562 | global four uchar 4 |
| 563 | global pfd uchar 8 |
| 564 | text msg "ping" |
| 565 | fn ck yes bool to void |
| 566 | if not yes |
| 567 | exit 80 |
| 568 | end |
| 569 | end |
| 570 | fn main to int |
| 571 | na = new uchar 3 0 |
| 572 | put na 0 1 |
| 573 | put na 2 115 |
| 574 | s = int socknew 1 1 0 |
| 575 | ck ge s 0 |
| 576 | put four 0 1 |
| 577 | ck eq sockopt s 1 2 four 0 4 0 |
| 578 | ck eq bind s na 0 3 0 |
| 579 | ck eq listen s 4 0 |
| 580 | ck gt sockname s buf 0 64 0 |
| 581 | c = int socknew 1 1 0 |
| 582 | ck eq connect c na 0 3 0 |
| 583 | a = int accept s 0 |
| 584 | ck ge a 0 |
| 585 | ck eq send c msg 0 4 0 4 |
| 586 | ck gt waitio a 1 1000000000 0 |
| 587 | ck eq recv a buf 0 4 0 4 |
| 588 | ck eq peercred a buf 0 12 0 |
| 589 | ck eq sendmsg a msg 0 4 0 4 |
| 590 | put pfd 0 uchar c |
| 591 | put pfd 4 1 |
| 592 | ck eq pollfds pfd 0 8 1000000000 1 |
| 593 | ck eq shutdown c 2 0 |
| 594 | ret 0 |
| 595 | end |
| 596 | ''', mode="run", exit=0) |
| 597 | |
| 598 | ex("mapping", ''' |
| 599 | note owned mappings and checked device calls; device calls use fd -1 here, so no device is opened |
| 600 | global hdr uchar 48 |
| 601 | global prm uchar 32 |
| 602 | global data uchar 64 |
| 603 | global ends uchar 8 |
| 604 | global cnt uchar 4 |
| 605 | global buf uchar 8 |
| 606 | text name "m" |
| 607 | fn ck yes bool to void |
| 608 | if not yes |
| 609 | exit 80 |
| 610 | end |
| 611 | end |
| 612 | fn main to int |
| 613 | h = mapnew neg int 1 0 4096 3 34 |
| 614 | ck gt h 0 |
| 615 | ck gt mapaddr h 4096 |
| 616 | ck eq mapset h 8 8 77 0 |
| 617 | ck eq mapget h 8 8 77 |
| 618 | put buf 0 5 |
| 619 | ck eq mapwrite h 0 buf 0 1 1 |
| 620 | ck eq mapread h 0 buf 1 1 1 |
| 621 | ck eq get buf 1 5 |
| 622 | ck eq mapdrop h 0 |
| 623 | ck eq pipefds ends 0 8 0 0 |
| 624 | rd = int get ends 0 |
| 625 | ck eq devctl rd uint 21531 cnt 0 4 0 |
| 626 | ck eq devcall neg int 1 uint 3223340587 hdr 0 32 prm 0 0 neg long 9 |
| 627 | ck eq uvmctl neg int 1 uint 75 data 8 8 neg long 9 |
| 628 | m = int memfd name 0 len name 0 |
| 629 | ck ge m 0 |
| 630 | ck eq ftrunc m 4096 0 |
| 631 | r = maphold 68719476736 4096 |
| 632 | ck gt r 0 |
| 633 | ck eq mapbind r m 0 3 0 |
| 634 | ret 0 |
| 635 | end |
| 636 | ''', mode="run", exit=0) |
| 637 | |
| 638 | ex("limits", ''' |
| 639 | note prlimit reads and sets the open-file limit (res 7); a failed execenv (no such file) returns -2 |
| 640 | global lim uchar 32 |
| 641 | fn ck yes bool to void |
| 642 | if not yes |
| 643 | exit 80 |
| 644 | end |
| 645 | end |
| 646 | fn main to int |
| 647 | ck eq prlimit 0 7 lim 0 16 0 |
| 648 | ck eq prlimit 0 7 lim 0 8 neg long 22 |
| 649 | ck eq prlimit 0 7 lim 0 32 0 |
| 650 | for i ulong 16 |
| 651 | ck eq get lim i get lim add i 16 |
| 652 | end |
| 653 | x = new uchar 6 0 |
| 654 | put x 0 1 |
| 655 | put x 2 2 |
| 656 | put x 4 47 |
| 657 | put x 5 120 |
| 658 | e = new uchar 7 0 |
| 659 | put e 0 1 |
| 660 | put e 2 3 |
| 661 | put e 4 97 |
| 662 | put e 5 61 |
| 663 | put e 6 98 |
| 664 | ck eq execenv x 0 6 e 0 7 neg long 2 |
| 665 | ret 0 |
| 666 | end |
| 667 | ''', mode="run", exit=0) |
| 668 | |
| 669 | ex("sched", ''' |
| 670 | note CPU affinity, the current CPU and the thread's scheduling priority (nice 19 is always allowed) |
| 671 | global mask uchar 128 |
| 672 | fn ck yes bool to void |
| 673 | if not yes |
| 674 | exit 80 |
| 675 | end |
| 676 | end |
| 677 | fn main to int |
| 678 | n = cpuget 0 mask 0 128 |
| 679 | ck gt n 0 |
| 680 | ck eq cpuset 0 mask 0 ulong n 0 |
| 681 | ck ge cpunow 0 |
| 682 | ck eq setprio 0 0 19 0 |
| 683 | ck eq getprio 0 0 1 |
| 684 | ret 0 |
| 685 | end |
| 686 | ''', mode="run", exit=0) |
| 687 | |
| 688 | ex("vfio", ''' |
| 689 | note VFIO words with fd -1, so no device is opened: the ioctl and both region mappings return -9 (EBADF); |
| 690 | note an eventfd holds a counter that read returns as eight little-endian bytes |
| 691 | global buf uchar 8 |
| 692 | global ri uchar 32 |
| 693 | fn ck yes bool to void |
| 694 | if not yes |
| 695 | exit 80 |
| 696 | end |
| 697 | end |
| 698 | fn main to int |
| 699 | ck eq vfioctl neg int 1 uint 15204 buf 0 0 neg long 9 |
| 700 | put ri 4 5 |
| 701 | put ri 16 16 |
| 702 | ck eq vfiomap neg int 1 ri 0 32 neg long 9 |
| 703 | put ri 4 7 |
| 704 | ck eq vfiomapw neg int 1 ri 0 32 neg long 9 |
| 705 | e = int eventfd uint 3 |
| 706 | ck ge e 0 |
| 707 | ck eq read e buf 0 8 8 |
| 708 | ck eq get buf 0 3 |
| 709 | ck eq fclose e 0 |
| 710 | ret 0 |
| 711 | end |
| 712 | ''', mode="run", exit=0) |
| 713 | |
| 714 | ex("vfioirq_trap", ''' |
| 715 | note vfioirq admits only a device fd acquired by vfioctl GROUP_GET_DEVICE_FD; any other fd traps 3 before the ioctl |
| 716 | global buf uchar 16 |
| 717 | fn main to int |
| 718 | r = vfioirq neg int 1 uint 15213 buf 0 16 |
| 719 | ret 0 |
| 720 | end |
| 721 | ''', mode="trap", trap=3) |
| 722 | |
| 723 | ex("kfd", ''' |
| 724 | note KFD device words without a device: a reservation is real, and a call on fd -1 returns -9 (EBADF) |
| 725 | global ver uchar 8 |
| 726 | fn ck yes bool to void |
| 727 | if not yes |
| 728 | exit 80 |
| 729 | end |
| 730 | end |
| 731 | fn main to int |
| 732 | r = kfdres 2097152 |
| 733 | ck gt r 0 |
| 734 | ck eq rem mapaddr r 2097152 0 |
| 735 | ck eq mapdrop r 0 |
| 736 | ck eq kfdctl neg int 1 uint 2148027137 ver 0 8 neg long 9 |
| 737 | ret 0 |
| 738 | end |
| 739 | ''', mode="run", exit=0) |
| 740 | |
| 741 | ex("kfdmap_trap", ''' |
| 742 | note kfdmap admits only a DRM render descriptor or /dev/kfd; any other descriptor (here a pipe) traps 3 before mmap |
| 743 | global ends uchar 8 |
| 744 | fn main to int |
| 745 | x = pipefds ends 0 8 0 |
| 746 | h = kfdmap int get ends 0 0 4096 |
| 747 | ret 0 |
| 748 | end |
| 749 | ''', mode="trap", trap=3) |
| 750 | |
| 751 | ex("fma", ''' |
| 752 | note fma rounds once: (1+2^-52)*(1-2^-52)-1 is -2^-104, while mul then add rounds twice and gives 0 |
| 753 | fn main to int |
| 754 | a = bitcast f64 ulong 4607182418800017409 |
| 755 | b = bitcast f64 ulong 4607182418800017406 |
| 756 | m = neg 1.0f64 |
| 757 | f = fma a b m |
| 758 | s = add mul a b m |
| 759 | if ne s 0.0f64 |
| 760 | ret 1 |
| 761 | end |
| 762 | if ge f 0.0f64 |
| 763 | ret 2 |
| 764 | end |
| 765 | ret int fma 2.0f64 3.0f64 1.0f64 |
| 766 | end |
| 767 | ''', mode="run", exit=7) |
| 768 | |
| 769 | # compiler error examples (gate15 cases and OM-2 obligations) |
| 770 | ex("e03", "fn main to int\nret q\nend\n", mode="error", code=3, line=2) |
| 771 | ex("e04", "struct S a uint\nfn main to int\nret 0\nend\n", mode="error", code=4, line=2) |
| 772 | ex("e05", "fn main to int\nret 0 extra\nend\n", mode="error", code=5, line=2) |
| 773 | ex("e06", "fn main to int\nx = 12a\nret 0\nend\n", mode="error", code=6, line=2) |
| 774 | ex("e07", "fn main to int\nx = add int 1 uint 2\nret 0\nend\n", mode="error", code=7, line=2) |
| 775 | ex("e08", "fn main to int\nx = int 2147483648\nret 0\nend\n", mode="error", code=8, line=2) |
| 776 | ex("e09", "fn main to int\nbreak\nret 0\nend\n", mode="error", code=9, line=2) |
| 777 | ex("e10", "fn f x int x int to int\nret 0\nend\nfn main to int\nret 0\nend\n", mode="error", code=10, line=1) |
| 778 | ex("e11", "fn main to int\nx = 5\nret 0\nend\n", mode="error", code=11, line=2) |
| 779 | ex("e12", "fn main to int\n" + "".join(f"a{i} = int {i}\n" for i in range(1, 18)) |
| 780 | + "ret add a1 add a2 add a3 add a4 add a5 add a6 add a7 add a8 add a9 add a10 add a11 add a12 add a13 add a14 add a15 add a16 a17\nend\n", |
| 781 | mode="error", code=12, line=18) |
| 782 | ex("e13", "fn main to int\nabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl = int 1\nret 0\nend\n", mode="error", code=13, line=2) |
| 783 | ex("e14", "struct S a long b long c long d long e long f long g long h long i long j long k long l long m long n long o long p long q long end\nfn main to int\nret 0\nend\n", |
| 784 | mode="error", code=14, line=1) |
| 785 | ex("e15", "fn main to int\nret 0\n", mode="error", code=15, line=3) |
| 786 | ex("e16", "wat\n", mode="error", code=16, line=1) |
| 787 | ex("e17", "fn main to int\nret 0\nret 1\nend\n", mode="error", code=17, line=3) |
| 788 | ex("e18", "fn f to int\nend\nfn main to int\nret 0\nend\n", mode="error", code=18, line=3) |
| 789 | ex("e19", "global g uchar 4\nfn f a edit uchar 4 to void\nend\nfn main to int\nf g\nret 0\nend\n", mode="error", code=19, line=5) |
| 790 | ex("e20", "text t \"abc\"\nfn main to int\nput t 0 1\nret 0\nend\n", mode="error", code=20, line=3) |
| 791 | ex("e21", "fn main to int\na = new uint 4 0\nput a 4 1\nret 0\nend\n", mode="error", code=21, line=3) |
| 792 | ex("e22", "text t \"a\\q\"\nfn main to int\nret 0\nend\n", mode="error", code=22, line=1) |
| 793 | ex("e23", "fn main to int\na = new uchar 16777217 0\nret 0\nend\n", mode="error", code=23, line=2) |
| 794 | ex("e25", "target x86efi\nfn f to int\nret 0\nend\n", mode="error", code=25, line=1) |
| 795 | ex("e27", "om2\nfn f s edit uchar 4 fresh to ulong\nret ulong get s 0\nend\nfn main to int\na = new uchar 4 0\nret int f a\nend\n", |
| 796 | mode="error", code=27, line=3) |
| 797 | ex("e28", "om2\nabstract tag uint\nfn main to int\nt = seal tag 1\nret 0\nend\n", mode="error", code=28, line=4) |
| 798 | ex("e29", "om2\nlayout hdr 4\nfield a uint at 2 little\nend\nfn main to int\nret 0\nend\n", mode="error", code=29, line=3) |
| 799 | ex("e30", "om2\nfn f x uint to uint\nrequires eq result 0\nret x\nend\nfn main to int\nret int f 1\nend\n", mode="error", code=30, line=3) |
| 800 | |
| 801 | # trap examples |
| 802 | ex("t01", "fn main to int\nx = int 2147483647\ny = add x 1\nret 0\nend\n", mode="trap", trap=1) |
| 803 | ex("t02", "fn main to int\nz = int 0\nret div 7 z\nend\n", mode="trap", trap=2) |
| 804 | ex("t03", "fn main to int\na = new uchar 4 0\ni = ulong 4\nret int get a i\nend\n", mode="trap", trap=3) |
| 805 | ex("t04", "fn main to int\ni = int 0\nwhile ge i 0 bound 10\ni = add i 1\nend\nret 0\nend\n", mode="trap", trap=4) |
| 806 | ex("t05", "fn main to int\nx = int 300\ny = uchar x\nret 0\nend\n", mode="trap", trap=5) |
| 807 | ex("t06", "fn deep n ulong to ulong\nret add 1 deep add n 1\nend\nfn main to int\nret int deep 0\nend\n", mode="trap", trap=6) |
| 808 | ex("t07", "fn main to int\ns = uint 40\nx = shl uint 1 s\nret 0\nend\n", mode="trap", trap=7) |
| 809 | for n in (8, 11, 12, 13, 14): |
| 810 | ex(f"t{n:02d}", f"note native program mode raises trap {n} only through the trap word\nfn main to int\ntrap {n}\nend\n", |
| 811 | mode="trap", trap=n) |
| 812 | ex("t10", "om2\nfn one to uint\nensures eq result 2\nret 1\nend\nfn main to int\nret int one\nend\n", mode="trap", trap=10) |
| 813 | |
| 814 | ex("cpu_order", ''' |
| 815 | note cpufence: publication barrier (AArch64 DSB SY); cpurelax: spin-wait hint (YIELD); no operands, no value |
| 816 | fn main to int |
| 817 | cpufence |
| 818 | cpurelax |
| 819 | ret 5 |
| 820 | end |
| 821 | ''', mode="run", exit=5) |
| 822 | |
| 823 | ex("qty", ''' |
| 824 | note a qty is an unsigned quantity whose add/sub/mul trap 1 instead of wrapping; convert with qty w and ulong q |
| 825 | fn main to int |
| 826 | q = qty 40 |
| 827 | r = add q 2 |
| 828 | ret int ulong r |
| 829 | end |
| 830 | ''', mode="run", exit=42) |
| 831 | |
| 832 | ex("secrets", ''' |
| 833 | om2 |
| 834 | note dit sets PSTATE.DIT and gives the old bit; scrub zeroes dead stack and registers and gives 0 (OM-2, program mode) |
| 835 | fn main to int |
| 836 | old = dit 1 |
| 837 | z = scrub 64 |
| 838 | was = dit 0 |
| 839 | ret int add add mul 10 was z mul 100 old |
| 840 | end |
| 841 | ''', mode="run", exit=10) |
| 842 | |
| 843 | ex("peername", ''' |
| 844 | note peername on a descriptor that is not open gives minus EBADF (9) |
| 845 | fn main to int |
| 846 | x = new uchar 16 0 |
| 847 | ret int neg peername 99 x 0 16 |
| 848 | end |
| 849 | ''', mode="run", exit=9) |
| 850 | |
| 851 | ex("pools", ''' |
| 852 | note a run-time pool (omc only): global NAME T * has length 0 at start and r = enlarge NAME n grows it (r is 0, |
| 853 | note or -22 / the host's negative errno when refused, NAME unchanged); get, put and len see the current length |
| 854 | global P ulong * |
| 855 | fn main to int |
| 856 | if ne len P 0 |
| 857 | ret 1 |
| 858 | end |
| 859 | r = enlarge P ulong 3 |
| 860 | if ne r 0 |
| 861 | ret 2 |
| 862 | end |
| 863 | put P 2 39 |
| 864 | r = enlarge P ulong 2 |
| 865 | put P 0 len P |
| 866 | ret int add get P 0 get P 2 |
| 867 | end |
| 868 | ''', mode="run", exit=42, stdout="") |