interplane.Get Omega
Read in your language

omega-mcp

aien-dev/omega-mcp

Brought by drakestapleton

branch main Branches Tags History

omega-mcp / tools / ref_examples.py

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