interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / gpu / nvidia / driver / gate.om

849 lines · 18158 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note gpu/nvidia/driver/gate.om: the gpu-driver gate. It reads the cases the C oracle printed
3note (gpu/nvidia/driver/oracle-cases.tsv: CUDApp 84b524ca nvrm/ and m16/ C, run outside this tree) and
4note recomputes each one with card.om, diag.om, m16.om, nvrm.om and eng.om fcls. For the first case of each
5note kind it also runs a control: the same case with its expected answer changed, which must be caught.
6note Exit 0 is PASS.
7note Line kinds, tab separated, numbers decimal, bytes lowercase hex, "-" absent, "e" empty bytes:
8note card HAS BDF CARDS INDEX+1 / cls SM VENDOR DEVICE CLASSES M10 M11 / amem PREV SIZE MEMINFO MESSAGE /
9note chan PREV ATTEMPTS DELAY MEMINFO BUDDYINFO MESSAGE / maps MAPS HIT / name KIND CODE TEXT /
10note wge NOW TIMEOUT T MARKER TARGET RC+1 CLOCK_READS / ret RETIRED PUT UPTO NEWRETIRED /
11note enq PBSIZE COUNT RC+1 NWORDS (0: not enqueued) / slot HANDLES ARC+1 RC+1 SLOT (the slot count: none) /
12note cput NEXTRA PUT0 PUT1 PUT2 PUT3 CH RESULT / cret NEXTRA CH RETIRED1 PUT1 RETIRED2 PUT2 RETIRED3 PUT3 UPTO
13note NEWRETIRED1 NEWRETIRED2 NEWRETIRED3 / copy DIR NOBUF FAULTED OFF BYTES HANDLE VA SIZE CPU ROWS RC+1 MSG /
14note alloc VRAM DISCRETE SM FAULTED LIVECOUNT CAP SIZE RC+1 CODE VASIZE / vinfo DISCRETE IORC STATUS TOTAL FREE
15note RC+1 CODE OUTFREE OUTTOTAL / fcls RESULT KIND LAST EXPECTED MARKER2 MARKER2WANT PROGRESS CLASS.
16note A card record is 9 bytes: index, valid, domain u32 LE, bus, slot, function. CLASSES and HANDLES are u32
17note LE words. ROWS are live rows of 7 u64 LE: handle, va, size, cpu, cpu_live, rm_live, quarantined (cpu as an
18note offset from one buffer, as is CPU). alloc CODE: 0 none, 1 SM86, 2 size, 3 table full, 4 VA exhausted (the
19note oracle's va_take answers 0, so a case that passes every check ends there with VASIZE the size it asked
20note for). vinfo CODE: 0 none, 1 errno or status, 2 invalid heap information. fcls CLASS: 0 wrong_marker,
21note 1 late_progress, 2 no_progress. In wge the clock reads NOW, then T, then 2^64 - 1.
22note Lines starting with # are notes. One line is read into a 65536-byte array (an Omega array's size is fixed
23note when it is compiled), so a longer line is refused by name, never cut. Arrays are passed as arguments, so
24note they are main's locals: l line, d decoded fields,
25note c card table, u class words, o recomputed message.
26
27text gt_k "card|cls|amem|chan|maps|name|wge|ret|enq|slot|cput|cret|copy|alloc|vinfo|fcls|"
28text gt_mf "MemFree"
29text gt_ma "MemAvailable"
30text gt_use "usage: gate ORACLE-CASES.tsv\n"
31text gt_open "gate: cannot open, size or map the cases file\n"
32text gt_long "gate: a line of the cases file is longer than 65536 bytes; refused\n"
33text gt_fl1 "FAIL line "
34text gt_bad "FAIL malformed line "
35text gt_none "FAIL no cases of kind "
36text gt_ctl "FAIL control not caught, kind "
37text gt_sum "OMEGA-GPU-DRIVER "
38text gt_pass "PASS"
39text gt_fail "FAIL"
40text gt_cs " cases="
41text gt_co " controls="
42text gt_ca " caught="
43text gt_hw " hardware=NOT_RUN\n"
44text gt_nl "\n"
45global g_p uchar 4096
46global g_st uchar 256
47global g_nb uchar 24
48global g_fs ulong 16
49global g_fe ulong 16
50global g_ds ulong 8
51global g_dl ulong 8
52global g_kc ulong 16
53note g_n: 0 decode top, 1 control on, 2 fields, 3 line number, 4 cases, 5 fails, 6 controls, 7 caught,
54note 8 malformed lines, 9 clock reads, 10 clock NOW, 11 clock T
55global g_n ulong 16
56
57fn gnum v ulong to void
58k = ulong 0
59x = v
60while true bound 20
61put g_nb k uchar add 48 rem x 10
62k = add k 1
63x = div x 10
64if eq x 0
65break
66end
67end
68for j ulong div k 2 bound 10
69a = get g_nb j
70put g_nb j get g_nb sub sub k j 1
71put g_nb sub sub k j 1 a
72end
73r = write 1 g_nb 0 k
74end
75
76fn gbad to void
77put g_n 8 add get g_n 8 1
78end
79
80note decimal field i: 1 to 20 digits below 2^64; anything else marks the line malformed
81fn fdec i ulong l view uchar 65536 to ulong
82v = ulong 0
83s = get g_fs i
84n = sub get g_fe i s
85if or eq n 0 gt n 20
86gbad
87ret 0
88end
89for j ulong n bound 20
90c = ulong get l add s j
91if or lt c 48 gt c 57
92gbad
93ret 0
94end
95if gt v div sub ulong 18446744073709551615 sub c 48 10
96gbad
97ret 0
98end
99v = add mul v 10 sub c 48
100end
101ret v
102end
103
104note the byte whose two hex digits start at l[q], or 256 when they are not lowercase hex
105fn ghex2 q ulong l view uchar 65536 to ulong
106h = drv_hexd ulong get l q
107x = drv_hexd ulong get l add q 1
108if or gt h 15 gt x 15
109ret 256
110end
111ret or shl h 4 x
112end
113
114note hex field i decoded into d as segment slot; false when the field is "-" (absent)
115fn fhex i ulong slot ulong l view uchar 65536 d edit uchar 65536 to bool
116s = get g_fs i
117n = sub get g_fe i s
118p = get g_n 0
119put g_ds slot p
120put g_dl slot 0
121if eq n 1
122if eq get l s 45
123ret false
124end
125if eq get l s 101
126ret true
127end
128end
129if ne rem n 2 0
130gbad
131ret false
132end
133n = div n 2
134if gt add p n 65536
135gbad
136ret false
137end
138for j ulong n bound 65536
139x = ghex2 add s mul j 2 l
140if gt x 255
141gbad
142ret false
143end
144put d add p j uchar x
145end
146put g_n 0 add p n
147put g_dl slot n
148ret true
149end
150
151note o[0, n) equals segment slot of d; with the control on, the first expected byte is changed
152fn gcmp n ulong slot ulong d view uchar 65536 o view uchar 65536 to bool
153if ne n get g_dl slot
154ret false
155end
156b = get g_ds slot
157for i ulong n bound 65536
158x = ulong get d add b i
159if and eq i 0 eq get g_n 1 1
160x = xor x 1
161end
162if ne ulong get o i x
163ret false
164end
165end
166ret true
167end
168
169note place the 9-byte card record at d[q] into the 64 x 72-byte table c; false when its index is past 63
170fn gcrec q ulong d view uchar 65536 c edit uchar 4608 to bool
171p = mul ulong get d q 72
172if ge p 4608
173gbad
174ret false
175end
176put c p get d add q 1
177for j ulong 4
178put c add add p 4 j get d add add q 2 j
179end
180put c add p 8 get d add q 6
181put c add p 9 get d add q 7
182put c add p 10 get d add q 8
183ret true
184end
185
186fn hcard l view uchar 65536 d edit uchar 65536 c edit uchar 4608 to bool
187pb = fhex 2 0 l d
188pc = fhex 3 1 l d
189for z ulong 4608
190put c z 0
191end
192for r ulong div get g_dl 1 9 bound 65536
193if not gcrec add get g_ds 1 mul r 9 d c
194ret false
195end
196end
197got = drv_card c ne fdec 1 l 0 part d get g_ds 0 get g_dl 0
198ret eq got add fdec 4 l get g_n 1
199end
200
201note the little-endian u32 at d[q]
202fn gu32 q ulong d view uchar 65536 to ulong
203x = or ulong get d q shl ulong get d add q 1 8
204ret or x or shl ulong get d add q 2 16 shl ulong get d add q 3 24
205end
206
207fn hcls l view uchar 65536 d edit uchar 65536 u edit uint 16384 to bool
208pc = fhex 4 0 l d
209n = div get g_dl 0 4
210for i ulong n bound 16384
211put u i uint gu32 add get g_ds 0 mul i 4 d
212end
213m = new ulong 16 0
214drv_marshal fdec 1 l fdec 2 l fdec 3 l part u 0 n m
215if ne get m 10 add fdec 5 l get g_n 1
216ret false
217end
218ret eq get m 11 fdec 6 l
219end
220
221note MemFree (w 0) or MemAvailable (w 1) of segment slot of d into r[0]
222fn gmem slot ulong w ulong d view uchar 65536 r edit ulong 1 to bool
223if eq w 0
224ret drv_meminfo part d get g_ds slot get g_dl slot part gt_mf 0 len gt_mf r
225end
226ret drv_meminfo part d get g_ds slot get g_dl slot part gt_ma 0 len gt_ma r
227end
228
229fn hamem l view uchar 65536 d edit uchar 65536 o edit uchar 65536 to bool
230pp = fhex 1 0 l d
231pm = fhex 3 1 l d
232px = fhex 4 2 l d
233v = new ulong 3 0
234r = new ulong 1 0
235put v 0 fdec 2 l
236if pm
237if not gmem 1 0 d r
238ret false
239end
240put v 1 get r 0
241if not gmem 1 1 d r
242ret false
243end
244put v 2 get r 0
245end
246n = drv_amsg part d get g_ds 0 get g_dl 0 v o
247ret gcmp n 2 d o
248end
249
250fn hchan l view uchar 65536 d edit uchar 65536 o edit uchar 65536 to bool
251pp = fhex 1 0 l d
252pm = fhex 4 1 l d
253pb = fhex 5 2 l d
254px = fhex 6 3 l d
255v = new ulong 4 0
256r = new ulong 1 0
257put v 0 fdec 2 l
258put v 1 fdec 3 l
259if pm
260if not gmem 1 0 d r
261ret false
262end
263put v 2 get r 0
264end
265put v 3 18446744073709551615
266if pb
267if not drv_buddy part d get g_ds 2 get g_dl 2 9 r
268ret false
269end
270put v 3 get r 0
271end
272n = drv_cmsg part d get g_ds 0 get g_dl 0 v o
273ret gcmp n 3 d o
274end
275
276fn hmaps l view uchar 65536 d edit uchar 65536 to bool
277if not fhex 1 0 l d
278ret false
279end
280got = drv_maps part d get g_ds 0 get g_dl 0
281ret eq got add fdec 2 l get g_n 1
282end
283
284fn hname l view uchar 65536 d edit uchar 65536 o edit uchar 65536 to bool
285p = fhex 3 0 l d
286code = fdec 2 l
287r = long code
288if ge code 2147483648
289r = neg long sub 4294967296 code
290end
291w = new ulong 2 0
292if eq fdec 1 l 0
293drv_wres r w
294else
295drv_wkind r w
296end
297for i ulong get w 1 bound 64
298put o i get drv_m16t add get w 0 i
299end
300ret gcmp get w 1 0 d o
301end
302
303note the scripted clock of a wge case: NOW, then T, then 2^64 - 1
304fn gclk to ulong
305c = get g_n 9
306put g_n 9 add c 1
307if eq c 0
308ret get g_n 10
309end
310if eq c 1
311ret get g_n 11
312end
313ret 18446744073709551615
314end
315
316note m16_native_wait_marker_ge on the scripted clock: the loop of drv_wait_ge with gclk in place of monons
317note and the case's marker in place of mapget; each look is drv_wstep, the function drv_wait_ge calls
318fn hwge l view uchar 65536 to bool
319put g_n 10 fdec 1 l
320put g_n 11 fdec 3 l
321put g_n 9 0
322mk = fdec 4 l
323tg = fdec 5 l
324dur = drv_wdur fdec 2 l
325endt = ulong 18446744073709551615
326if ne dur 18446744073709551615
327endt = drv_wend gclk dur
328end
329st = ulong 1
330while eq st 1 bound 8
331st = drv_wstep lt gclk endt mk tg
332end
333rc = ulong 0
334if eq st 0
335rc = ulong 1
336end
337if ne rc add fdec 6 l get g_n 1
338ret false
339end
340ret eq get g_n 9 fdec 7 l
341end
342
343note nvrm_retire: the retired count after a completion report
344fn hret l view uchar 65536 to bool
345got = drv_retire fdec 1 l fdec 2 l fdec 3 l
346ret eq got add fdec 4 l get g_n 1
347end
348
349note m16_native_enqueue_methods with nvrm_enqueue answering 0: RC+1 1 and NWORDS = COUNT when drv_enqok
350fn henq l view uchar 65536 to bool
351cnt = fdec 2 l
352rc = ulong 0
353nw = ulong 0
354if drv_enqok cnt fdec 1 l
355rc = ulong 1
356nw = cnt
357end
358if ne rc add fdec 3 l get g_n 1
359ret false
360end
361ret eq nw fdec 4 l
362end
363
364note m16_native_alloc_memory: drv_slot over the handles, then nvrm_alloc's answer ARC decides RC
365fn hslot l view uchar 65536 d edit uchar 65536 u edit uint 16384 to bool
366if not fhex 1 0 l d
367ret false
368end
369n = div get g_dl 0 4
370for i ulong n bound 16384
371put u i uint gu32 add get g_ds 0 mul i 4 d
372end
373s = drv_slot part u 0 n
374if ne s add fdec 4 l get g_n 1
375ret false
376end
377rc = ulong 0
378if and lt s n eq fdec 2 l 1
379rc = ulong 1
380end
381ret eq rc fdec 3 l
382end
383
384note nvrm_chan_put: PUT0 for the main channel, PUT(ch) for an extra one, 0 for no channel. A line carries
385note three extra puts, so NEXTRA above 3 is malformed.
386fn hcput l view uchar 65536 to bool
387nx = fdec 1 l
388if gt nx 3
389gbad
390ret false
391end
392ch = fdec 6 l
393s = drv_chan ch nx
394got = ulong 0
395if eq s 0
396got = fdec 2 l
397end
398if eq s 1
399got = fdec add 2 ch l
400end
401ret eq got add fdec 7 l get g_n 1
402end
403
404note nvrm_chan_retire: only the entry ch names moves, by drv_retire; the others keep their retired count
405fn hcret l view uchar 65536 to bool
406nx = fdec 1 l
407if gt nx 3
408gbad
409ret false
410end
411ch = fdec 2 l
412s = drv_chan ch nx
413up = fdec 9 l
414for i ulong 3
415want = fdec add 3 mul i 2 l
416if and eq s 1 eq add i 1 ch
417want = drv_retire want fdec add 4 mul i 2 l up
418end
419if eq i 0
420want = add want get g_n 1
421end
422if ne want fdec add 10 i l
423ret false
424end
425end
426ret true
427end
428
429note the little-endian u64 at d[q]
430fn gu64 q ulong d view uchar 65536 to ulong
431ret or gu32 q d shl gu32 add q 4 d 32
432end
433
434note true when one of the live rows decoded into d is the allocation m (drv_lrow)
435fn glive d view uchar 65536 m view ulong 4 to bool
436a = new ulong 7 0
437for r ulong div get g_dl 0 56 bound 65536
438q = add get g_ds 0 mul r 56
439for j ulong 7
440put a j gu64 add q mul j 8 d
441end
442if drv_lrow a m
443ret true
444end
445end
446ret false
447end
448
449note nvrm_copy_to and nvrm_copy_from up to the byte copy: glive, then drv_copy;
450note 0 is RC+1 1 with no message, 1 RC+1 0 with no message, 2 RC+1 0 with the message
451fn hcopy l view uchar 65536 d edit uchar 65536 to bool
452m = new ulong 4 0
453for j ulong 4
454put m j fdec add 6 j l
455end
456if not fhex 10 0 l d
457ret false
458end
459live = glive d m
460st = drv_copy eq fdec 2 l 0 ne fdec 3 l 0 fdec 4 l fdec 5 l get m 2 live
461rc = ulong 0
462if eq st 0
463rc = ulong 1
464end
465if ne rc add fdec 11 l get g_n 1
466ret false
467end
468ret eq bv eq st 2 fdec 12 l
469end
470
471note nvrm_alloc_vram (VRAM 1) and alloc_with_cacheability up to va_take: the first check that refuses gives
472note CODE; a case that passes every check asks va_take for drv_asize's size (CODE 4, the oracle's va_take
473note answers 0). RC+1 is 0 on every line.
474fn halloc l view uchar 65536 to bool
475code = ulong 0
476vs = ulong 0
477if and eq fdec 1 l 1 not drv_vramok ne fdec 2 l 0 fdec 3 l
478code = ulong 1
479end
480if eq code 0
481vs = drv_asize ne fdec 4 l 0 fdec 7 l
482if eq vs 0
483code = ulong 2
484end
485end
486if eq code 0
487if drv_livefull fdec 5 l fdec 6 l
488code = ulong 3
489vs = ulong 0
490end
491end
492if eq code 0
493code = ulong 4
494end
495if ne code add fdec 9 l get g_n 1
496ret false
497end
498if ne fdec 8 l 0
499ret false
500end
501ret eq vs fdec 10 l
502end
503
504note nvrm_vram_info: drv_vinfo; on 0 the call returns the heap's free and total bytes, otherwise it leaves
505note the caller's two numbers (7 in the oracle) alone
506fn hvinfo l view uchar 65536 to bool
507t = fdec 4 l
508f = fdec 5 l
509st = drv_vinfo ne fdec 1 l 0 fdec 2 l fdec 3 l t f
510rc = ulong 0
511code = ulong 0
512of = ulong 7
513ot = ulong 7
514if eq st 0
515rc = ulong 1
516of = f
517ot = t
518end
519if or eq st 1 eq st 2
520code = st
521end
522if ne rc add fdec 6 l get g_n 1
523ret false
524end
525if ne code fdec 7 l
526ret false
527end
528ret and eq of fdec 8 l eq ot fdec 9 l
529end
530
531note m16_gpu_wait_failure_class, C branch: eng.om fcls with the report in m[1] to m[7]
532fn hfcls l view uchar 65536 to bool
533m = new ulong 24 0
534for j ulong 7
535put m add j 1 fdec add 1 j l
536end
537w = new ulong 16 0
538fcls m w
539ret eq get w 0 add fdec 8 l get g_n 1
540end
541
542note fields a line of kind k has
543fn gnf k ulong to ulong
544if or eq k 0 eq k 2
545ret 5
546end
547if or eq k 1 eq k 3
548ret 7
549end
550if eq k 4
551ret 3
552end
553if eq k 5
554ret 4
555end
556if or eq k 7 or eq k 8 eq k 9
557ret 5
558end
559if or eq k 6 eq k 10
560ret 8
561end
562if or eq k 11 eq k 12
563ret 13
564end
565if eq k 13
566ret 11
567end
568if eq k 14
569ret 10
570end
571ret 9
572end
573
574note kind of field 0 (index of its piece in gt_k), or 16 when it names none
575fn gkind l view uchar 65536 to ulong
576s = get g_fs 0
577n = sub get g_fe 0 s
578i = ulong 0
579for k ulong 16
580st = i
581while ne get gt_k i 124 bound 16
582i = add i 1
583end
584if eq sub i st n
585same = true
586for j ulong n bound 16
587if ne get l add s j get gt_k add st j
588same = false
589end
590end
591if same
592ret k
593end
594end
595i = add i 1
596end
597ret 16
598end
599
600fn hrun k ulong l view uchar 65536 d edit uchar 65536 c edit uchar 4608 u edit uint 16384 o edit uchar 65536 to bool
601put g_n 0 0
602if eq k 0
603ret hcard l d c
604end
605if eq k 1
606ret hcls l d u
607end
608if eq k 2
609ret hamem l d o
610end
611if eq k 3
612ret hchan l d o
613end
614if eq k 4
615ret hmaps l d
616end
617if eq k 5
618ret hname l d o
619end
620if eq k 6
621ret hwge l
622end
623if eq k 7
624ret hret l
625end
626if eq k 8
627ret henq l
628end
629if eq k 9
630ret hslot l d u
631end
632if eq k 10
633ret hcput l
634end
635if eq k 11
636ret hcret l
637end
638if eq k 12
639ret hcopy l d
640end
641if eq k 13
642ret halloc l
643end
644if eq k 14
645ret hvinfo l
646end
647ret hfcls l
648end
649
650note split l[0, n) into fields at tabs; false when it has more than 16
651fn gsplit n ulong l view uchar 65536 to bool
652f = ulong 0
653put g_fs 0 0
654for i ulong n bound 65536
655if eq get l i 9
656if ge add f 1 16
657ret false
658end
659put g_fe f i
660f = add f 1
661put g_fs f add i 1
662end
663end
664put g_fe f n
665put g_n 2 add f 1
666ret true
667end
668
669note report a case result: a failing case prints its line number
670fn gcase ok bool to void
671put g_n 4 add get g_n 4 1
672if not ok
673put g_n 5 add get g_n 5 1
674r = write 1 gt_fl1 0 len gt_fl1
675gnum get g_n 3
676r = write 1 gt_nl 0 len gt_nl
677end
678end
679
680note report a control result: a control that is not caught prints its kind
681fn gctl caught bool k ulong to void
682put g_n 6 add get g_n 6 1
683if caught
684put g_n 7 add get g_n 7 1
685else
686r = write 1 gt_ctl 0 len gt_ctl
687gnum k
688r = write 1 gt_nl 0 len gt_nl
689end
690end
691
692note one line l[0, n): split, check, run, and the control for a kind's first case
693fn gline n ulong l view uchar 65536 d edit uchar 65536 c edit uchar 4608 u edit uint 16384 o edit uchar 65536 to void
694put g_n 3 add get g_n 3 1
695if eq n 0
696ret
697end
698if eq get l 0 35
699ret
700end
701bad = get g_n 8
702if not gsplit n l
703gbad
704end
705k = ulong 16
706if eq get g_n 8 bad
707k = gkind l
708if or eq k 16 ne get g_n 2 gnf k
709gbad
710end
711end
712if eq get g_n 8 bad
713put g_n 1 0
714ok = hrun k l d c u o
715if eq get g_n 8 bad
716gcase ok
717if eq get g_kc k 0
718put g_n 1 1
719cc = hrun k l d c u o
720put g_n 1 0
721gctl not cc k
722end
723put g_kc k add get g_kc k 1
724end
725end
726if ne get g_n 8 bad
727r = write 1 gt_bad 0 len gt_bad
728gnum get g_n 3
729r = write 1 gt_nl 0 len gt_nl
730end
731end
732
733note the size of the open file fd from fstat (st_size, byte 48 of the AArch64 struct stat), 0 on failure
734fn gsize fd int to ulong
735if ne fstat fd g_st 0 128 0
736ret 0
737end
738size = ulong 0
739for j ulong 8
740size = or size shl ulong get g_st add 48 j mul j 8
741end
742ret size
743end
744
745fn gsummary to int
746fl = add get g_n 5 get g_n 8
747for k ulong 16
748if eq get g_kc k 0
749fl = add fl 1
750r = write 1 gt_none 0 len gt_none
751gnum k
752r = write 1 gt_nl 0 len gt_nl
753end
754end
755if ne get g_n 6 get g_n 7
756fl = add fl 1
757end
758r = write 1 gt_sum 0 len gt_sum
759if eq fl 0
760r = write 1 gt_pass 0 len gt_pass
761else
762r = write 1 gt_fail 0 len gt_fail
763end
764r = write 1 gt_cs 0 len gt_cs
765gnum get g_n 4
766r = write 1 gt_co 0 len gt_co
767gnum get g_n 6
768r = write 1 gt_ca 0 len gt_ca
769gnum get g_n 7
770r = write 1 gt_hw 0 len gt_hw
771if ne fl 0
772ret 1
773end
774ret 0
775end
776
777note open argv[1] and map it whole; g_n 12 gets the descriptor and g_n 13 the size. 0 when it cannot.
778fn gopen to long
779if ne argcnt 2
780r = write 2 gt_use 0 len gt_use
781ret 0
782end
783pl = argread 1 g_p 0 4095
784if lt pl 1
785r = write 2 gt_use 0 len gt_use
786ret 0
787end
788fd = int fopen g_p 0 ulong pl 524288 0
789if lt fd 0
790r = write 2 gt_open 0 len gt_open
791ret 0
792end
793size = gsize fd
794put g_n 12 ulong fd
795put g_n 13 size
796h = long 0
797if gt size 0
798h = mapnew fd 0 size 1 2
799end
800if le h 0
801r = write 2 gt_open 0 len gt_open
802ret 0
803end
804ret h
805end
806
807note the offset of the first newline in l[0, k), or k when there is none
808fn geol k ulong l view uchar 65536 to ulong
809for i ulong k bound 65536
810if eq get l i 10
811ret i
812end
813end
814ret k
815end
816
817fn main to int
818l = new uchar 65536 0
819d = new uchar 65536 0
820c = new uchar 4608 0
821u = new uint 16384 0
822o = new uchar 65536 0
823h = gopen
824if le h 0
825ret 2
826end
827size = get g_n 13
828pos = ulong 0
829while lt pos size bound 9223372036854775807
830k = sub size pos
831if gt k 65536
832k = ulong 65536
833end
834if ne mapread h pos l 0 k long k
835r = write 2 gt_open 0 len gt_open
836ret 2
837end
838e = geol k l
839if and eq e k eq k 65536
840r = write 2 gt_long 0 len gt_long
841ret 2
842end
843gline e l d c u o
844pos = add pos add e 1
845end
846r = mapdrop h
847q = fclose int get g_n 12
848ret gsummary
849end