interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / d8_lib.om

630 lines · 10765 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3note tests/d8_lib.om -- the shared part of the D8 programs (tests/d8_process_tax.om, tests/d8_mutants.om). Make
4note concatenates it in front of each program and compiles the result with the pinned seeded omc (omega-mv e5b32567,
5note sha256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f); see mk/dx-d8.mk.
6note Byte regions: B holds 32 regions of 4096 bytes (Bn their lengths). Globals are never passed to functions, so
7note helpers take region numbers. By convention region 4 holds the output directory; regions 2, 3, 22, 23, 29 and 30
8note are scratch of these helpers (2 output, 3 argument and capture, 22 environment entry, 23 PATH, 29 capture
9note path, 30 path), region 19 receives the program found by which.
10note L is one loaded file (Ln its length). AL is a packed argument list for execprog: u16 count, then a u16 length
11note and the bytes of each argument (AP: write position and count). ST is a waitpid status.
12note execprog starts every child with an empty environment; callers that need variables pass them through
13note /usr/bin/env K=V (envkv) until omega-mv has an execprog form that takes an environment.
14global B uchar 131072
15global Bn ulong 32
16global L uchar 524288
17global Ln ulong 1
18global AL uchar 4096
19global AP ulong 2
20global ST uchar 8
21
22text TBig "d8: refused (a buffer bound or a failed host call)\n"
23text TSl "/"
24text TDevNul "/dev/null"
25text TPathV "PATH="
26
27note ---- regions
28fn bfail to void
29r = write 2 TBig 0 len TBig
30exit 90
31end
32
33fn bc r ulong c uchar to void
34n = get Bn r
35if ge n 4095
36bfail
37end
38put B add mul r 4096 n c
39put Bn r add n 1
40end
41
42fn bclr r ulong to void
43put Bn r 0
44end
45
46fn bt r ulong t view uchar * to void
47for i ulong len t
48bc r get t i
49end
50end
51
52fn bs r ulong t view uchar * to void
53bclr r
54bt r t
55end
56
57fn bb d ulong s ulong to void
58for i ulong get Bn s
59bc d get B add mul s 4096 i
60end
61end
62
63fn bu r ulong v ulong to void
64d = ulong 1
65while ge div v d 10 bound 20
66d = mul d 10
67end
68while gt d 0 bound 21
69bc r uchar add 48 rem div v d 10
70d = div d 10
71end
72end
73
74fn bz r ulong v ulong w ulong to void
75d = ulong 1
76for i ulong sub w 1
77d = mul d 10
78end
79while gt d 0 bound 21
80bc r uchar add 48 rem div v d 10
81d = div d 10
82end
83end
84
85fn bl r ulong v long to void
86if lt v 0
87bc r 45
88bu r ulong neg v
89else
90bu r ulong v
91end
92end
93
94fn bist r ulong t view uchar * to bool
95n = get Bn r
96if ne n len t
97ret false
98end
99for i ulong n
100if ne get B add mul r 4096 i get t i
101ret false
102end
103end
104ret true
105end
106
107fn beq a ulong b ulong to bool
108n = get Bn a
109if ne n get Bn b
110ret false
111end
112oa = mul a 4096
113ob = mul b 4096
114for i ulong n
115if ne get B add oa i get B add ob i
116ret false
117end
118end
119ret true
120end
121
122note ^[0-9]+$ of at most 18 digits as a value; -1 otherwise
123fn bnum r ulong to long
124n = get Bn r
125if or eq n 0 gt n 18
126ret neg long 1
127end
128v = long 0
129for i ulong n
130c = get B add mul r 4096 i
131if or lt c 48 gt c 57
132ret neg long 1
133end
134v = add mul v 10 long sub c 48
135end
136ret v
137end
138
139fn bout fd int r ulong to void
140o = mul r 4096
141n = get Bn r
142k = ulong 0
143while lt k n bound 4096
144w = write fd B add o k sub n k
145if le w 0
146bfail
147end
148k = add k ulong w
149end
150end
151
152fn say t view uchar * to void
153bs 2 t
154bout 1 2
155end
156
157fn barg i ulong r ulong to void
158n = argread i B mul r 4096 4095
159if lt n 0
160bfail
161end
162put Bn r ulong n
163end
164
165note path region d = OUTDIR "/" nm sx
166fn bpath d ulong nm view uchar * sx view uchar * to void
167bclr d
168bb d 4
169bt d TSl
170bt d nm
171bt d sx
172end
173
174note path region d = OUTDIR "/" nm
175fn bpath1 d ulong nm view uchar * to void
176bclr d
177bb d 4
178bt d TSl
179bt d nm
180end
181
182fn bopen r ulong flags int to long
183n = get Bn r
184if eq n 0
185bfail
186end
187ret fopen B mul r 4096 n flags 420
188end
189
190note writes region s to the file named by region p: flags 578 create/truncate, 1089 create/append
191fn bfile p ulong s ulong flags int to void
192fd = bopen p flags
193if lt fd 0
194bfail
195end
196bout int fd s
197c = fclose int fd
198end
199
200fn bexists r ulong to bool
201n = get Bn r
202if eq n 0
203ret false
204end
205ret eq faccess B mul r 4096 n 0 0
206end
207
208note UTC from realns. style 0 %Y-%m-%dT%H:%M:%S.%NZ, 1 %FT%TZ, 2 %Y%m%dT%H%M%SZ
209fn bsep r ulong style ulong c uchar to void
210if ne style 2
211bc r c
212end
213end
214
215note the civil date of a day count since 1970-01-01 (Hinnant's civil_from_days)
216fn bdate r ulong days ulong style ulong to void
217z = add days 719468
218era = div z 146097
219doe = sub z mul era 146097
220z = sub doe div doe 1460
221z = add z div doe 36524
222yoe = div sub z div doe 146096 365
223y = add yoe mul era 400
224z = add mul 365 yoe div yoe 4
225doe = sub doe sub z div yoe 100
226z = div add mul 5 doe 2 153
227era = add sub doe div add mul 153 z 2 5 1
228if ge z 10
229z = sub z 9
230else
231z = add z 3
232end
233if le z 2
234y = add y 1
235end
236bz r y 4
237bsep r style 45
238bz r z 2
239bsep r style 45
240bz r era 2
241end
242
243fn bstamp r ulong style ulong to void
244ns = ulong realns
245s = div ns 1000000000
246bdate r div s 86400 style
247s = rem s 86400
248bc r 84
249bz r div s 3600 2
250bsep r style 58
251bz r rem div s 60 60 2
252bsep r style 58
253bz r rem s 60 2
254if eq style 0
255bc r 46
256bz r rem ns 1000000000 9
257end
258bc r 90
259end
260
261note ---- the loaded file L
262fn fload r ulong to bool
263put Ln 0 0
264fd = bopen r 0
265if lt fd 0
266ret false
267end
268n = ulong 0
269while lt n len L bound 999999
270k = read int fd L n sub len L n
271if le k 0
272break
273end
274n = add n ulong k
275end
276put Ln 0 n
277c = fclose int fd
278ret true
279end
280
281fn lend p ulong to ulong
282n = get Ln 0
283q = p
284while lt q n bound 999999
285if eq get L q 10
286ret q
287end
288q = add q 1
289end
290ret n
291end
292
293fn lat p ulong t view uchar * to bool
294for i ulong len t
295if ne get L add p i get t i
296ret false
297end
298end
299ret true
300end
301
302fn leq p ulong e ulong t view uchar * to bool
303if ne sub e p len t
304ret false
305end
306ret lat p t
307end
308
309fn lpre p ulong e ulong t view uchar * to bool
310if lt sub e p len t
311ret false
312end
313ret lat p t
314end
315
316fn lhas p ulong e ulong t view uchar * to bool
317if lt sub e p len t
318ret false
319end
320for s ulong add sub sub e p len t 1
321if lat add p s t
322ret true
323end
324end
325ret false
326end
327
328fn lcopy p ulong e ulong r ulong to void
329q = p
330while lt q e bound 999999
331bc r get L q
332q = add q 1
333end
334end
335
336note lines equal to t (mode 0) or starting with t (mode 1)
337fn lcnt t view uchar * mode ulong to ulong
338c = ulong 0
339p = ulong 0
340while lt p get Ln 0 bound 999999
341e = lend p
342if eq mode 0
343if leq p e t
344c = add c 1
345end
346else
347if lpre p e t
348c = add c 1
349end
350end
351p = add e 1
352end
353ret c
354end
355
356note the first line starting with t, appended to region r; false when none
357fn lfirst t view uchar * r ulong to bool
358p = ulong 0
359while lt p get Ln 0 bound 999999
360e = lend p
361if lpre p e t
362lcopy p e r
363ret true
364end
365p = add e 1
366end
367ret false
368end
369
370note the first line containing a or b, appended to region r
371fn lfirst2 a view uchar * b view uchar * r ulong to void
372p = ulong 0
373while lt p get Ln 0 bound 999999
374e = lend p
375if or lhas p e a lhas p e b
376lcopy p e r
377ret
378end
379p = add e 1
380end
381end
382
383note every line starting with key: the rest, newline separated, into region r (sed -n 's/^key//p')
384fn lvals key view uchar * r ulong to void
385bclr r
386k = ulong 0
387p = ulong 0
388while lt p get Ln 0 bound 999999
389e = lend p
390if lpre p e key
391if gt k 0
392bc r 10
393end
394lcopy add p len key e r
395k = add k 1
396end
397p = add e 1
398end
399end
400
401note the digits of the last line "# exit: [0-9]*" into region r (empty when none)
402note ---- processes. AL is a packed argument list: u16 count, then a u16 length and the bytes per argument.
403fn alclr to void
404put AP 0 2
405put AP 1 0
406end
407
408fn alb r ulong to void
409n = get Bn r
410p = get AP 0
411if or ge get AP 1 64 gt add add p n 2 4096
412bfail
413end
414put AL p uchar and n 255
415put AL add p 1 uchar shr n 8
416o = mul r 4096
417for i ulong n
418put AL add add p 2 i get B add o i
419end
420put AP 0 add add p 2 n
421put AP 1 add get AP 1 1
422end
423
424fn alt t view uchar * to void
425bs 3 t
426alb 3
427end
428
429note the command, arguments joined by spaces (the "# command:" line)
430fn bcmd r ulong to void
431p = ulong 2
432for a ulong get AP 1
433n = add ulong get AL p shl ulong get AL add p 1 8
434if gt a 0
435bc r 32
436end
437for i ulong n
438bc r get AL add add p 2 i
439end
440p = add add p 2 n
441end
442end
443
444note fork and execprog AL. out 1: stdout and stderr appended to the file named by region r; out 2: stdout to
445note descriptor fd, stderr to /dev/null; out 0: inherited.
446fn spawn how ulong r ulong fd int to long
447put AL 0 uchar get AP 1
448put AL 1 0
449pid = fork
450if lt pid 0
451bfail
452end
453if eq pid 0
454if eq how 1
455f = bopen r 1089
456if lt f 0
457exit 127
458end
459x = dupfd int f 1 0
460x = dupfd int f 2 0
461end
462if eq how 2
463x = dupfd fd 1 0
464g = fopen TDevNul 0 len TDevNul 1 0
465if ge g 0
466x = dupfd int g 2 0
467end
468end
469x = execprog AL 0 get AP 0
470exit 127
471end
472ret pid
473end
474
475note exit code, or 128 + signal; -1 while running (opt 1, WNOHANG)
476fn reap pid long opt int to long
477w = waitpid int pid ST 0 4 opt
478if eq w 0
479ret neg long 1
480end
481if lt w 0
482bfail
483end
484s = get ST 0
485if ne s 0
486ret add 128 long and s 127
487end
488ret long get ST 1
489end
490
491fn runwait how ulong r ulong to long
492pid = spawn how r 0
493ret reap pid 0
494end
495
496note ---- environment and PATH
497fn bpfx r ulong t view uchar * to bool
498if lt get Bn r len t
499ret false
500end
501o = mul r 4096
502for i ulong len t
503if ne get B add o i get t i
504ret false
505end
506end
507ret true
508end
509
510note region d gets bytes [k, e) of region s
511fn bmid d ulong s ulong k ulong e ulong to void
512o = mul s 4096
513while lt k e bound 4096
514bc d get B add o k
515k = add k 1
516end
517end
518
519note region d gets region s from byte k on
520fn bfrom d ulong s ulong k ulong to void
521o = mul s 4096
522n = get Bn s
523while lt k n bound 4096
524bc d get B add o k
525k = add k 1
526end
527end
528
529fn envget key view uchar * r ulong to bool
530bclr r
531for i ulong ulong envcnt
532n = envread i B 90112 4095
533if lt n 0
534bfail
535end
536put Bn 22 ulong n
537if bpfx 22 key
538bfrom r 22 len key
539ret true
540end
541end
542ret false
543end
544
545note a program name searched along PATH into region 19 (command -v)
546fn which nm view uchar * to bool
547if not envget TPathV 23
548ret false
549end
550n = get Bn 23
551s = ulong 0
552p = ulong 0
553while le p n bound 4096
554if or eq p n eq get B add 94208 p 58
555bclr 19
556bmid 19 23 s p
557if eq get Bn 19 0
558bc 19 46
559end
560bt 19 TSl
561bt 19 nm
562if eq faccess B mul ulong 19 4096 get Bn 19 1 0
563ret true
564end
565s = add p 1
566end
567p = add p 1
568end
569ret false
570end
571
572fn mkdirp r ulong to void
573n = get Bn r
574for i ulong n
575last = eq add i 1 n
576if not last
577last = eq get B add mul r 4096 add i 1 47
578end
579if and last gt i 0
580x = makedir B mul r 4096 add i 1 493
581end
582end
583end
584
585note the D8_CORES pin: false when unpinned; the cores in region 16
586fn envkv key view uchar * to void
587if envget key 3
588bs 30 key
589bb 30 3
590alb 30
591end
592end
593
594note /usr/bin/env with the variables spark-window needs, then the spark-window found on PATH (region 18)
595fn capture to long
596bclr 3
597bfile 29 3 578
598rc = runwait 1 29
599bclr 3
600x = fload 29
601n = get Ln 0
602while gt n 0 bound 999999
603if ne get L sub n 1 10
604break
605end
606n = sub n 1
607end
608if gt n 4000
609n = ulong 4000
610end
611lcopy 0 n 3
612ret rc
613end
614
615fn catfile r ulong to void
616if fload r
617bclr 2
618n = get Ln 0
619k = ulong 0
620while lt k n bound 999999
621bc 2 get L k
622k = add k 1
623if ge get Bn 2 4000
624bout 1 2
625bclr 2
626end
627end
628bout 1 2
629end
630end