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 / seed / semhash / semhash.om

2350 lines · 35435 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/semhash/semhash.om: per-function meaning hash, canonical form and semantic diff for
3note Omega units, written in seeded Omega. Built on top of om/sha256/sha256-om2.om (see README.md
4note for the build line); this file has no om2 line of its own and reuses none of that file's names.
5note Modes: hash (default), canon, diff A B, keywords, dups. Exit 0 ok, 1 diff found a difference,
6note 2 usage, 3 refusal (CR, capacity, malformed), 90+ internal.
7note Keyword tables. tkw1 is the genkw.awk word:code lists (OM-1 and host words), tkw2 the OM-2-only words of
8note omc.om fn kw2, tctx the contextual OM-2 names, tspc the profile and special words this tool needs.
9text tkw1 "bool 1\nuchar 2\nu8 2\nushort 3\nu16 3\nuint 4\nu32 4\nulong 5\nu64 5\nbyte 6\ni8 6\nshort 7\ni16 7\nint 8\ni32 8\nlong 9\ni64 9\nadd 33\nsub 34\nmul 35\ndiv 36\nrem 37\nand 38\nor 39\nxor 40\nshl 41\nshr 42\neq 43\nne 44\nlt 45\nle 46\ngt 47\nge 48\nneg 49\nnot 50\nget 51\nlen 52\nread 53\nwrite 54\ntrue 55\nfalse 56\nview 57\nedit 58\nfn 64\nto 65\nend 66\nif 67\nelse 68\nwhile 69\nbound 70\nfor 71\nret 72\n= 73\n( 74\n) 75\nvoid 76\nglobal 78\ntext 79\nput 80\nfree 81\nexit 82\nnote 83\nnew 84\nstruct 85\nbreak 86\ntrap 87\nown 77\nf32 88\nf64 89\nbitcast 90\nfsqrt 91\nfabs 92\nfmin 93\nfmax 94\nsizeof 95\noffsetof 96\ncontinue 97\nfclose 100\nfsync 101\nfchmod 102\nfseek 103\nsocknew 104\nlisten 105\naccept 106\nfork 107\ndupfd 108\nmonons 109\nfopen 110\nunlink 111\nmakedir 112\ngetdnts 113\nconnect 114\nbind 115\nwaitpid 116\nfstat 117\nrecv 118\nsend 119\nsleepns 120\nargcnt 121\nargread 122\nwaitio 123\nexecprog 124\nrenames 125\nrmdir 126\nkillpid 127\npipefds 128\ngetpid 129\nrealns 130\nsockname 131\ncwd 132\nreadlnk 133\nsockopt 134\nrandom 135\nenvcnt 136\nenvread 137\nchdir 138\nftrunc 139\nstat 140\nlink 141\nsymlnk 142\nchmod 143\nfaccess 144\ngetuid 145\ngeteuid 146\ngetgid 147\ngetegid 148\nlstat 149\ntruncate 150\nchown 151\nconst 152\nfchdir 153\ngetppid 154\numask 155\nfdsync 156\nmemfd 157\nsetsid 158\ngetpgid 159\nsetpgid 160\npread 161\npwrite 162\ngetsid 163\nsyncfs 164\nshutdown 165\ndevctl 166\nmapnew 167\nmapdrop 168\nmapread 169\nmapwrite 170\ndevcall 171\nuvmctl 172\nmapaddr 173\nmapget 174\nmapset 175\nmaphold 176\nmapbind 177\nflock 197\nexecpure 198\npeercred 220\npollfds 221\nsendmsg 222\nqty 223\ncpufence 199\ncpurelax 200\npeername 224\nfma 227\nvfioctl 228\nvfiomap 229\nvfiomapw 230\neventfd 231\nvfioirq 232\nexecenv 233\nprlimit 235\ncpuset 236\ncpuget 237\ncpunow 238\nsetprio 239\ngetprio 240\nkfdctl 241\nkfdmap 242\nkfdres 243\nenlarge 244\n"
10text tkw2 "rotr 59\nrotl 60\nunroll 181\ninline 182\nfresh 183\nabstract 184\nseal 185\nopen 186\nholds 187\nlayout 188\nfield 189\nat 190\nlittle 191\nbig 192\npart 193\nout 194\nexport 195\nops 196\ndit 225\nscrub 226\n"
11text tctx "requires 1\nensures 1\ninvariant 1\nresult 1\n"
12text tspc "om2 1\nir 2\ndiag 3\nfmt 4\ndebug 5\nrelease 6\n* 7\nmain 8\n"
13text tom1 "P om1"
14text tself "@self"
15text tfn "FN "
16text tline " line="
17text tverb " verbatim"
18text tcyc " cycle"
19text tunit "UNIT "
20text tfns " fns="
21text tsame "SAME "
22text tchg "CHANGED "
23text tadd "ADDED "
24text trem "REMOVED "
25text tren "RENAMED "
26text tarr "->"
27text tent "ENTRY CHANGED "
28text texp "EXPORTS CHANGED\n"
29text tdsame "DIFF SAME\n"
30text tdchg "DIFF CHANGED n="
31text tdup "DUP "
32text tdn " n="
33text tdups "DUPS groups="
34text tdfns " fns="
35text tdrec " records="
36text tdskp " skipped="
37text mhash "hash"
38text mcanon "canon"
39text mdiff "diff"
40text mkw "keywords"
41text mdups "dups"
42text eusage "usage: semhash [hash|canon|keywords|dups] < unit.om | semhash diff A.om B.om\n"
43text ecr "semhash: refused: CR byte in input\n"
44text ebig "semhash: refused: input over 8388608 bytes\n"
45text ecap "semhash: refused: capacity exceeded\n"
46text ectl "semhash: refused: control byte in input\n"
47text etxt "semhash: refused: unterminated text literal\n"
48text eblk "semhash: refused: unterminated fn or layout block\n"
49text ersv "semhash: refused: token starts with a reserved byte ($ # @)\n"
50text edup "semhash: refused: duplicate function name\n"
51text eopen "semhash: cannot open input file\n"
52text eread "semhash: read failed\n"
53text eletr "semhash: refused: no free canon letter\n"
54text efmt "semhash: refused: canon of an om2 fmt unit (its output is source text, not code)\n"
55note input
56global gin uchar 8388616
57global ginn ulong 1
58note output buffer
59global gob uchar 1048576
60global gobn ulong 1
61note symbols: bytes pool, offsets, lengths, open-addressing table
62global gsb uchar 16777216
63global gsbn ulong 1
64global gso ulong 1048576
65global gsl ulong 1048576
66global gsn ulong 1
67global ght ulong 2097152
68note symbol attributes
69global gkw ulong 1048576
70global gk2 ulong 1048576
71global gcx uchar 1048576
72global gfm ulong 1048576
73global gdh ulong 1048576
74global gfd uchar 1048576
75global gls ulong 1048576
76global gns ulong 1048576
77global gnu ulong 1048576
78note tokens and kept lines
79global gts ulong 4194304
80global gtn ulong 1
81global glf ulong 2097152
82global glc ulong 2097152
83global gll ulong 2097152
84global glr ulong 2097152
85global glw ulong 2097152
86global gln ulong 1
87note top-level items: kind 1 fn, 2 decl, 3 other, 4 profile lead
88global gik uchar 65536
89global gia ulong 65536
90global gib ulong 65536
91global gix ulong 65536
92global gis ulong 65536
93global gitn ulong 1
94note functions
95global gfi ulong 8192
96global gfs ulong 8192
97global gfp ulong 8192
98global gfv uchar 8192
99global gfx uchar 8192
100global gfh uchar 262144
101global gfg uchar 262144
102global gfo ulong 8192
103global gfk uchar 8192
104global gfc ulong 8192
105global gfz ulong 8192
106global gfcs ulong 8192
107global gfcn ulong 8192
108global gfrs ulong 8192
109global gfrn ulong 8192
110global gcur ulong 8192
111global gm1 ulong 8192
112global gm2 ulong 8192
113global gcst ulong 8192
114global gce ulong 524288
115global gre ulong 524288
116global gcen ulong 1
117global gfnn ulong 1
118global gq ulong 8192
119note stream buffer and hash result
120global gsf uchar 16777216
121global gsfn ulong 1
122global ghr uchar 32
123global ggr uchar 32
124note state: gpf 0 om2 flag, 1 lead line + 1, 2 canon letter; gsp special symbols
125global gpf ulong 4
126global gsp ulong 16
127global gep ulong 1
128global gkn ulong 1
129global gdl ulong 65536
130global gdln ulong 1
131global gbad uchar 8
132note diff sides: names, hashes, flags (side 1 starts at 8192)
133global gwb uchar 1048576
134global gwp ulong 1
135global gwo ulong 16384
136global gwl ulong 16384
137global gwh uchar 524288
138global gwm uchar 16384
139global gwx uchar 16384
140global gwc ulong 2
141
142note ---------------------------------------------------------------- output and exits
143fn ob_flush to void
144n = get gobn 0
145o = ulong 0
146for a ulong 1000000
147if ge o n
148break
149end
150r = write 1 gob o sub n o
151if le r 0
152exit 93
153end
154o = add o ulong r
155end
156put gobn 0 0
157end
158
159fn ob_byte b uchar to void
160n = get gobn 0
161if ge n 1048000
162ob_flush
163n = ulong 0
164end
165put gob n b
166put gobn 0 add n 1
167end
168
169fn ob_str t view uchar * to void
170for i ulong len t
171ob_byte get t i
172end
173end
174
175fn ob_dec v ulong to void
176d = new uchar 24 0
177k = ulong 0
178x = v
179for i ulong 24
180put d k uchar add rem x 10 48
181k = add k 1
182x = div x 10
183if eq x 0
184break
185end
186end
187for j ulong k
188ob_byte get d sub sub k j 1
189end
190end
191
192fn ob_sym s ulong to void
193o = get gso s
194for i ulong get gsl s
195ob_byte get gsb add o i
196end
197end
198
199fn hx_dig v ulong to uchar
200if lt v 10
201ret uchar add v 48
202end
203ret uchar add v 87
204end
205
206fn ob_fhash f ulong to void
207o = mul f 32
208for i ulong 32
209v = ulong get gfh add o i
210ob_byte hx_dig shr v 4
211ob_byte hx_dig and v 15
212end
213end
214
215fn die_msg t view uchar * c int to void
216ob_flush
217r = write 2 t 0 len t
218exit c
219end
220
221fn rf_die t view uchar * to void
222die_msg t 3
223end
224
225note ---------------------------------------------------------------- stream buffer
226fn sf_byte b uchar to void
227n = get gsfn 0
228if ge n 16777000
229rf_die ecap
230end
231put gsf n b
232put gsfn 0 add n 1
233end
234
235fn sf_str t view uchar * to void
236for i ulong len t
237sf_byte get t i
238end
239end
240
241fn sf_sym s ulong to void
242o = get gso s
243for i ulong get gsl s
244sf_byte get gsb add o i
245end
246end
247
248fn sf_dec v ulong to void
249d = new uchar 24 0
250k = ulong 0
251x = v
252for i ulong 24
253put d k uchar add rem x 10 48
254k = add k 1
255x = div x 10
256if eq x 0
257break
258end
259end
260for j ulong k
261sf_byte get d sub sub k j 1
262end
263end
264
265fn sf_fhash f ulong to void
266o = mul f 32
267for i ulong 32
268v = ulong get gfh add o i
269sf_byte hx_dig shr v 4
270sf_byte hx_dig and v 15
271end
272end
273
274note sha256 of gsf[0..gsfn) into ghr, 4096 bytes at a time through a local chunk
275fn sh_stream to void
276c = new uchar 104 0
277sha256_init c
278k = new uchar 4096 0
279n = get gsfn 0
280p = ulong 0
281for a ulong 1000000
282if ge p n
283break
284end
285m = sub n p
286if gt m 4096
287m = ulong 4096
288end
289for j ulong m
290put k j get gsf add p j
291end
292sha256_update c part k 0 m
293p = add p m
294end
295h = new uchar 32 0
296sha256_final c h
297for i ulong 32
298put ghr i get h i
299end
300end
301
302fn hr_tofn f ulong to void
303o = mul f 32
304for i ulong 32
305put gfh add o i get ghr i
306end
307end
308
309note ---------------------------------------------------------------- symbols
310fn sy_eq s ulong b ulong n ulong to bool
311if ne get gsl s n
312ret false
313end
314o = get gso s
315for i ulong n
316if ne get gsb add o i get gsb add b i
317ret false
318end
319end
320ret true
321end
322
323fn sy_new b ulong n ulong to ulong
324s = get gsn 0
325if ge s 1048576
326rf_die ecap
327end
328put gso s b
329put gsl s n
330put gsbn 0 add b n
331put gsn 0 add s 1
332put gkw s 0
333put gk2 s 0
334put gcx s 0
335put gfm s 0
336put gdh s 0
337put gfd s 0
338put gls s 0
339put gns s 0
340put gnu s 0
341ret s
342end
343
344note intern the n bytes staged at gsb[gsbn..]; returns the symbol
345fn sy_intern n ulong to ulong
346b = get gsbn 0
347h = ulong 14695981039346656037
348for i ulong n
349h = mul xor h ulong get gsb add b i 1099511628211
350end
351p = and h 2097151
352for t ulong 2097152
353e = get ght p
354if eq e 0
355s = sy_new b n
356put ght p add s 1
357ret s
358end
359if sy_eq sub e 1 b n
360ret sub e 1
361end
362p = and add p 1 2097151
363end
364exit 94
365end
366
367fn kw_eff s ulong to ulong
368c = get gkw s
369if ne c 0
370ret c
371end
372if ne get gpf 0 0
373ret get gk2 s
374end
375ret 0
376end
377
378fn is_num s ulong to bool
379c = get gsb get gso s
380if eq c 45
381ret true
382end
383if and ge c 48 le c 57
384ret true
385end
386ret false
387end
388
389fn is_txt s ulong to bool
390ret eq get gsb get gso s 34
391end
392
393note load "word code\n" lines; k 1 OM-1 code, 2 OM-2 code, 3 contextual, 4 special slot
394fn kw_load t view uchar * k ulong to void
395i = ulong 0
396n = len t
397for z ulong 100000
398if ge i n
399break
400end
401b = get gsbn 0
402m = ulong 0
403for y ulong 64
404if eq get t i 32
405break
406end
407put gsb add b m get t i
408m = add m 1
409i = add i 1
410end
411i = add i 1
412v = ulong 0
413for w ulong 64
414if eq get t i 10
415break
416end
417v = add mul v 10 sub ulong get t i 48
418i = add i 1
419end
420i = add i 1
421s = sy_intern m
422kw_set s k v
423end
424end
425
426fn kw_set s ulong k ulong v ulong to void
427if eq k 1
428put gkw s v
429end
430if eq k 2
431put gk2 s v
432end
433if eq k 3
434put gcx s 1
435end
436if eq k 4
437put gsp v s
438end
439end
440
441fn st_reset to void
442for i ulong 2097152
443put ght i 0
444end
445put gsn 0 0
446put gsbn 0 0
447put gtn 0 0
448put gln 0 0
449put gitn 0 0
450put gfnn 0 0
451put gcen 0 0
452put gsfn 0 0
453put gpf 0 0
454put gpf 1 0
455put ginn 0 0
456kw_load tkw1 1
457kw_load tkw2 2
458kw_load tctx 3
459kw_load tspc 4
460end
461
462note ---------------------------------------------------------------- input
463fn in_stdin to void
464n = ulong 0
465for a ulong 1000000
466if ge n 8388609
467break
468end
469r = read 0 gin n sub 8388609 n
470if lt r 0
471die_msg eread 93
472end
473if eq r 0
474break
475end
476n = add n ulong r
477end
478put ginn 0 n
479end
480
481fn in_file ai ulong to void
482pb = new uchar 4096 0
483r = argread ai pb 0 4095
484if le r 0
485die_msg eopen 2
486end
487fl = fopen pb 0 ulong r 0 0
488if lt fl 0
489die_msg eopen 2
490end
491fd = int fl
492n = ulong 0
493for a ulong 1000000
494if ge n 8388609
495break
496end
497q = read fd gin n sub 8388609 n
498if lt q 0
499die_msg eread 93
500end
501if eq q 0
502break
503end
504n = add n ulong q
505end
506c = fclose fd
507put ginn 0 n
508end
509
510fn in_check to void
511n = get ginn 0
512if gt n 8388608
513rf_die ebig
514end
515for i ulong 8388609
516if ge i n
517break
518end
519if eq get gin i 13
520rf_die ecr
521end
522end
523end
524
525note ---------------------------------------------------------------- tokenizer
526fn tk_push s ulong to void
527t = get gtn 0
528if ge t 4194304
529rf_die ecap
530end
531c = get gsb get gso s
532if or or eq c 36 eq c 35 eq c 64
533rf_die ersv
534end
535put gts t s
536put gtn 0 add t 1
537end
538
539fn tk_text q ulong to ulong
540b = get gsbn 0
541n = get ginn 0
542put gsb b 34
543m = ulong 1
544p = add q 1
545for a ulong 8388609
546if ge p n
547rf_die etxt
548end
549c = get gin p
550if eq c 10
551rf_die etxt
552end
553if ge m 65536
554rf_die ecap
555end
556put gsb add b m c
557m = add m 1
558p = add p 1
559if eq c 34
560break
561end
562if eq c 92
563if ge p n
564rf_die etxt
565end
566if eq get gin p 10
567rf_die etxt
568end
569put gsb add b m get gin p
570m = add m 1
571p = add p 1
572end
573end
574tk_push sy_intern m
575ret p
576end
577
578fn tk_word q ulong to ulong
579b = get gsbn 0
580if gt b 16000000
581rf_die ecap
582end
583c = get gin q
584if or eq c 40 eq c 41
585put gsb b c
586tk_push sy_intern 1
587ret add q 1
588end
589if eq c 34
590ret tk_text q
591end
592n = get ginn 0
593m = ulong 0
594p = q
595for a ulong 8388609
596if ge p n
597break
598end
599c = get gin p
600if le c 32
601break
602end
603if or eq c 40 eq c 41
604break
605end
606if ge m 65536
607rf_die ecap
608end
609put gsb add b m c
610m = add m 1
611p = add p 1
612end
613tk_push sy_intern m
614ret p
615end
616
617note one source line from q; returns the offset after its LF
618fn tk_line q ulong ln ulong to ulong
619n = get ginn 0
620l = get gln 0
621if ge l 2097152
622rf_die ecap
623end
624t0 = get gtn 0
625put glf l t0
626put gll l ln
627put glr l q
628p = q
629dn = false
630for a ulong 8388609
631if ge p n
632break
633end
634c = get gin p
635if eq c 10
636break
637end
638if or eq c 32 eq c 9
639p = add p 1
640continue
641end
642if dn
643p = add p 1
644continue
645end
646if lt c 32
647rf_die ectl
648end
649p = tk_word p
650if eq get gtn 0 add t0 1
651if eq get gkw get gts t0 83
652dn = true
653end
654end
655end
656put glw l p
657if dn
658put gtn 0 t0
659end
660c2 = sub get gtn 0 t0
661if gt c2 0
662put glc l c2
663put gln 0 add l 1
664end
665ret add p 1
666end
667
668fn tk_all to void
669n = get ginn 0
670p = ulong 0
671ln = ulong 1
672for a ulong 8388609
673if ge p n
674break
675end
676p = tk_line p ln
677ln = add ln 1
678end
679end
680
681note ---------------------------------------------------------------- items
682fn it_new k ulong a ulong b ulong to ulong
683i = get gitn 0
684if ge i 65536
685rf_die ecap
686end
687put gik i uchar k
688put gia i a
689put gib i b
690put gix i 0
691put gis i 0
692put gitn 0 add i 1
693ret i
694end
695
696note chain decl item i under key symbol s
697fn it_key i ulong s ulong to void
698h = get gdh s
699if eq h 0
700put gdh s add i 1
701ret
702end
703j = sub h 1
704for a ulong 65536
705x = get gix j
706if eq x 0
707break
708end
709j = sub x 1
710end
711put gix j add i 1
712end
713
714fn ln_tok l ulong k ulong to ulong
715ret get gts add get glf l k
716end
717
718note exact raw line: no leading, trailing or doubled blanks
719fn ln_exact l ulong to bool
720c = get glc l
721w = sub c 1
722for k ulong c
723w = add w get gsl ln_tok l k
724end
725ret eq sub get glw l get glr l w
726end
727
728fn pf_lead to void
729if eq get gln 0 0
730ret
731end
732c = get glc 0
733s = ln_tok 0 0
734if not ln_exact 0
735ret
736end
737if and eq s get gsp 2 eq c 1
738put gpf 1 1
739ret
740end
741if ne s get gsp 1
742ret
743end
744if eq c 1
745pf_om2
746ret
747end
748t = ln_tok 0 1
749if eq c 2
750if pf_word t
751pf_om2
752end
753ret
754end
755if eq c 3
756u = ln_tok 0 2
757if and eq t get gsp 3 or eq u get gsp 5 eq u get gsp 6
758pf_om2
759end
760end
761end
762
763note second word of an om2 lead: diag, fmt, debug or release
764fn pf_word t ulong to bool
765for k ulong 4
766if eq t get gsp add k 3
767ret true
768end
769end
770ret false
771end
772
773fn pf_om2 to void
774put gpf 0 1
775put gpf 1 1
776end
777
778note register a fn starting at line l; returns its last line
779fn it_fn l ulong k ulong to ulong
780nl = get gln 0
781d = ulong 0
782e = l
783for a ulong 2097152
784if ge e nl
785rf_die eblk
786end
787c = kw_eff ln_tok e 0
788if or or or or eq c 64 eq c 67 eq c 69 eq c 71 eq c 195
789d = add d 1
790end
791if eq c 66
792d = sub d 1
793if eq d 0
794break
795end
796end
797e = add e 1
798end
799i = it_new 1 l e
800fn_reg i l k
801ret e
802end
803
804fn fn_reg i ulong l ulong k ulong to void
805f = get gfnn 0
806if ge f 8192
807rf_die ecap
808end
809if le get glc l add k 1
810rf_die eblk
811end
812t = add add get glf l k 1
813s = get gts t
814if ne get gfm s 0
815rf_die edup
816end
817put gfm s add f 1
818put gfi f i
819put gfs f s
820put gfp f t
821put gfv f 0
822put gfx f uchar k
823put gfk f 0
824put gfc f 0
825put gfz f 0
826put gfcn f 0
827put gfrn f 0
828put gcst f 0
829put gm1 f 0
830put gm2 f 0
831put gfnn 0 add f 1
832end
833
834fn it_layout l ulong to ulong
835nl = get gln 0
836e = l
837for a ulong 2097152
838if ge e nl
839rf_die eblk
840end
841if eq kw_eff ln_tok e 0 66
842break
843end
844if and eq kw_eff ln_tok e 0 189 gt get glc e 1
845put gfd ln_tok e 1 1
846end
847e = add e 1
848end
849i = it_new 2 l e
850if gt get glc l 1
851it_key i ln_tok l 1
852end
853ret e
854end
855
856fn it_struct l ulong to void
857i = it_new 2 l l
858c = get glc l
859if gt c 1
860it_key i ln_tok l 1
861end
862for k ulong c
863if gt k 1
864fd_mark ln_tok l k
865end
866end
867end
868
869fn fd_mark s ulong to void
870if ne kw_eff s 0
871ret
872end
873if is_num s
874ret
875end
876put gfd s 1
877end
878
879fn it_line l ulong to ulong
880c = kw_eff ln_tok l 0
881if eq c 64
882ret it_fn l 0
883end
884if and eq c 195 gt get glc l 1
885if eq kw_eff ln_tok l 1 64
886ret it_fn l 1
887end
888end
889if eq c 188
890ret it_layout l
891end
892if eq c 85
893it_struct l
894ret l
895end
896if or or or or eq c 78 eq c 79 eq c 152 eq c 184 eq c 187
897i = it_new 2 l l
898if gt get glc l 1
899it_key i ln_tok l 1
900end
901ret l
902end
903j = it_new 3 l l
904ret l
905end
906
907fn it_all to void
908pf_lead
909l = get gpf 1
910if ne l 0
911i = it_new 4 0 0
912end
913nl = get gln 0
914for a ulong 2097152
915if ge l nl
916break
917end
918l = add it_line l 1
919end
920end
921
922note ---------------------------------------------------------------- functions: locals
923fn is_scal c ulong to bool
924if and ge c 1 le c 9
925ret true
926end
927ret or eq c 88 eq c 89
928end
929
930fn lc_cand s ulong to bool
931if ne kw_eff s 0
932ret false
933end
934if or ne get gfm s 0 ne get gdh s 0
935ret false
936end
937if ne get gcx s 0
938ret false
939end
940if or is_num s is_txt s
941ret false
942end
943ret true
944end
945
946note one parameter type starting at token q (e = end of line); next token or 0 when unparseable
947fn pa_type q ulong e ulong to ulong
948s = get gts q
949c = kw_eff s
950if is_scal c
951ret add q 1
952end
953if and eq c 0 ne get gdh s 0
954ret add q 1
955end
956if not or or or eq c 57 eq c 58 eq c 77 eq c 194
957ret 0
958end
959if ge add q 1 e
960ret 0
961end
962s = get gts add q 1
963c = kw_eff s
964if and eq c 0 ne get gdh s 0
965ret add q 2
966end
967if not is_scal c
968ret 0
969end
970ret pa_len add q 2 e
971end
972
973fn pa_len q ulong e ulong to ulong
974if ge q e
975ret 0
976end
977s = get gts q
978if not or or is_num s ne get gdh s 0 eq s get gsp 7
979ret 0
980end
981r = add q 1
982if lt r e
983if eq kw_eff get gts r 183
984r = add r 1
985end
986end
987ret r
988end
989
990fn fn_params f ulong to void
991ep = get gep 0
992l = get gia get gfi f
993e = add get glf l get glc l
994q = add get gfp f 1
995for k ulong 64
996if ge q e
997ret
998end
999s = get gts q
1000if eq kw_eff s 65
1001ret
1002end
1003note a contextual name as a parameter is parsed but never renamed
1004if eq get gcx s 0
1005if not lc_cand s
1006put gfv f 1
1007ret
1008end
1009put gls s ep
1010end
1011q = pa_type add q 1 e
1012if eq q 0
1013put gfv f 1
1014ret
1015end
1016end
1017put gfv f 1
1018end
1019
1020fn fn_prep f ulong to void
1021ep = add get gep 0 1
1022put gep 0 ep
1023put gkn 0 0
1024fn_params f
1025it = get gfi f
1026b = get gib it
1027l = add get gia it 1
1028for a ulong 2097152
1029if ge l b
1030break
1031end
1032t = get glf l
1033s = get gts t
1034if eq kw_eff s 71
1035if gt get glc l 1
1036s = get gts add t 1
1037end
1038end
1039if lc_cand s
1040put gls s ep
1041end
1042l = add l 1
1043end
1044end
1045
1046fn rn_ok f ulong s ulong to bool
1047if ne get gls s get gep 0
1048ret false
1049end
1050if ne get gfv f 0
1051ret false
1052end
1053if ne get gfd s 0
1054ret false
1055end
1056ret le get gsl s 8
1057end
1058
1059note ---------------------------------------------------------------- emission
1060fn dl_add s ulong to void
1061h = get gdh s
1062ep = get gep 0
1063for a ulong 65536
1064if eq h 0
1065break
1066end
1067i = sub h 1
1068if ne get gis i ep
1069put gis i ep
1070n = get gdln 0
1071if ge n 65536
1072rf_die ecap
1073end
1074put gdl n i
1075put gdln 0 add n 1
1076end
1077h = get gix i
1078end
1079end
1080
1081fn em_line l ulong to void
1082t = get glf l
1083c = get glc l
1084for i ulong c
1085if gt i 0
1086sf_byte 32
1087end
1088s = get gts add t i
1089sf_sym s
1090if ne get gdh s 0
1091dl_add s
1092end
1093end
1094sf_byte 10
1095end
1096
1097fn em_item i ulong to void
1098l = get gia i
1099b = get gib i
1100for a ulong 2097152
1101if gt l b
1102break
1103end
1104em_line l
1105l = add l 1
1106end
1107end
1108
1109fn em_hdr to void
1110if eq get gpf 1 0
1111sf_str tom1
1112sf_byte 10
1113ret
1114end
1115sf_byte 80
1116sf_byte 32
1117em_line 0
1118end
1119
1120fn em_local s ulong md ulong to void
1121ep = get gep 0
1122if ne get gns s ep
1123put gns s ep
1124k = get gkn 0
1125put gnu s k
1126put gkn 0 add k 1
1127end
1128if eq md 1
1129sf_byte uchar get gpf 2
1130else
1131sf_byte 36
1132end
1133sf_dec get gnu s
1134end
1135
1136fn em_tok f ulong t ulong md ulong to void
1137s = get gts t
1138if rn_ok f s
1139em_local s md
1140ret
1141end
1142g = get gfm s
1143if and ne g 0 ne md 1
1144em_call f sub g 1 s md
1145ret
1146end
1147sf_sym s
1148if ne get gdh s 0
1149dl_add s
1150end
1151end
1152
1153fn em_call f ulong g ulong s ulong md ulong to void
1154if eq g f
1155sf_str tself
1156ret
1157end
1158if eq md 2
1159if eq get gfc g get gfc f
1160sf_sym s
1161ret
1162end
1163end
1164sf_byte 35
1165sf_fhash g
1166if ne get gfd s 0
1167sf_byte 58
1168sf_sym s
1169end
1170end
1171
1172note emit fn f: md 0 hash, 1 canon, 2 hash inside a call cycle (own name and member names kept)
1173fn em_fn f ulong md ulong to void
1174it = get gfi f
1175a = get gia it
1176b = get gib it
1177t0 = get gfp f
1178put gdln 0 0
1179if ne md 1
1180em_hdr
1181em_other
1182end
1183for x ulong 2097152
1184if gt a b
1185break
1186end
1187em_fl f a t0 md
1188a = add a 1
1189end
1190if ne md 1
1191em_decls
1192end
1193end
1194
1195fn em_fl f ulong l ulong t0 ulong md ulong to void
1196t = get glf l
1197c = get glc l
1198w = false
1199for i ulong c
1200tt = add t i
1201if eq tt t0
1202if ne md 0
1203if w
1204sf_byte 32
1205end
1206sf_sym get gts tt
1207w = true
1208end
1209continue
1210end
1211if w
1212sf_byte 32
1213end
1214w = true
1215em_tok f tt md
1216end
1217sf_byte 10
1218end
1219
1220fn em_other to void
1221n = get gitn 0
1222ep = get gep 0
1223for i ulong 65536
1224if ge i n
1225break
1226end
1227if eq get gik i 3
1228if ne get gis i ep
1229put gis i ep
1230k = get gdln 0
1231put gdl k i
1232put gdln 0 add k 1
1233end
1234end
1235end
1236end
1237
1238fn em_decls to void
1239i = ulong 0
1240for a ulong 65536
1241if ge i get gdln 0
1242break
1243end
1244sf_byte 68
1245sf_byte 10
1246em_item get gdl i
1247i = add i 1
1248end
1249end
1250
1251note ---------------------------------------------------------------- call graph and hashes
1252fn cg_edges f ulong to void
1253it = get gfi f
1254l = get gia it
1255b = get gib it
1256t0 = get gfp f
1257put gfcs f get gcen 0
1258for a ulong 2097152
1259if gt l b
1260break
1261end
1262t = get glf l
1263for i ulong get glc l
1264cg_tok f add t i t0
1265end
1266l = add l 1
1267end
1268put gfo f get gfcn f
1269end
1270
1271fn cg_tok f ulong t ulong t0 ulong to void
1272if eq t t0
1273ret
1274end
1275g = get gfm get gts t
1276if eq g 0
1277ret
1278end
1279g = sub g 1
1280if eq g f
1281ret
1282end
1283if eq get gcst g add f 1
1284ret
1285end
1286put gcst g add f 1
1287n = get gcen 0
1288if ge n 524288
1289rf_die ecap
1290end
1291put gce n g
1292put gcen 0 add n 1
1293put gfcn f add get gfcn f 1
1294put gfrn g add get gfrn g 1
1295end
1296
1297fn cg_build to void
1298nf = get gfnn 0
1299for f ulong nf
1300cg_edges f
1301end
1302s = ulong 0
1303for g ulong nf
1304put gfrs g s
1305put gcur g s
1306s = add s get gfrn g
1307end
1308for h ulong nf
1309cg_rev h
1310end
1311end
1312
1313fn cg_rev f ulong to void
1314o = get gfcs f
1315for i ulong get gfcn f
1316g = get gce add o i
1317c = get gcur g
1318put gre c f
1319put gcur g add c 1
1320end
1321end
1322
1323fn hs_one f ulong to void
1324fn_prep f
1325put gsfn 0 0
1326em_fn f 0
1327sh_stream
1328hr_tofn f
1329put gfk f 1
1330end
1331
1332note Kahn order over the call graph: a fn is hashed once all its callees are
1333fn hs_dag to void
1334nf = get gfnn 0
1335qh = ulong 0
1336qt = ulong 0
1337for f ulong nf
1338if eq get gfo f 0
1339put gq qt f
1340qt = add qt 1
1341end
1342end
1343for a ulong 8192
1344if ge qh qt
1345break
1346end
1347g = get gq qh
1348qh = add qh 1
1349hs_one g
1350qt = hs_rel g qt
1351end
1352end
1353
1354fn hs_rel g ulong qt ulong to ulong
1355o = get gfrs g
1356n = qt
1357for i ulong get gfrn g
1358c = get gre add o i
1359d = sub get gfo c 1
1360put gfo c d
1361if eq d 0
1362put gq n c
1363n = add n 1
1364end
1365end
1366ret n
1367end
1368
1369note reachability from r among unhashed fns; dir 0 callees into gm1, 1 callers into gm2
1370fn sc_reach r ulong dir ulong to void
1371st = add r 1
1372qh = ulong 0
1373qt = ulong 1
1374put gq 0 r
1375sc_mark r dir st
1376for a ulong 8192
1377if ge qh qt
1378break
1379end
1380f = get gq qh
1381qh = add qh 1
1382qt = sc_step f dir st qt
1383end
1384end
1385
1386fn sc_mark f ulong dir ulong st ulong to void
1387if eq dir 0
1388put gm1 f st
1389else
1390put gm2 f st
1391end
1392end
1393
1394fn sc_seen f ulong dir ulong st ulong to bool
1395if eq dir 0
1396ret eq get gm1 f st
1397end
1398ret eq get gm2 f st
1399end
1400
1401fn sc_step f ulong dir ulong st ulong qt ulong to ulong
1402n = qt
1403o = get gfcs f
1404c = get gfcn f
1405if eq dir 1
1406o = get gfrs f
1407c = get gfrn f
1408end
1409for i ulong c
1410g = sc_nb o i dir
1411if eq get gfk g 0
1412if not sc_seen g dir st
1413sc_mark g dir st
1414put gq n g
1415n = add n 1
1416end
1417end
1418end
1419ret n
1420end
1421
1422fn sc_nb o ulong i ulong dir ulong to ulong
1423if eq dir 0
1424ret get gce add o i
1425end
1426ret get gre add o i
1427end
1428
1429fn sc_all to void
1430nf = get gfnn 0
1431for r ulong nf
1432if and eq get gfk r 0 eq get gfc r 0
1433sc_one r
1434end
1435end
1436end
1437
1438fn sc_one r ulong to void
1439sc_reach r 0
1440sc_reach r 1
1441st = add r 1
1442nf = get gfnn 0
1443z = ulong 0
1444for f ulong nf
1445if and eq get gm1 f st eq get gm2 f st
1446put gfc f st
1447z = add z 1
1448end
1449end
1450put gfz r z
1451end
1452
1453fn sc_ready r ulong to bool
1454st = add r 1
1455nf = get gfnn 0
1456for f ulong nf
1457if eq get gfc f st
1458if not sc_fnok f st
1459ret false
1460end
1461end
1462end
1463ret true
1464end
1465
1466fn sc_fnok f ulong st ulong to bool
1467o = get gfcs f
1468for i ulong get gfcn f
1469g = get gce add o i
1470if ne get gfc g st
1471if eq get gfk g 0
1472ret false
1473end
1474end
1475end
1476ret true
1477end
1478
1479fn sc_hash r ulong to void
1480if eq get gfz r 1
1481hs_one r
1482ret
1483end
1484st = add r 1
1485nf = get gfnn 0
1486k = ulong 0
1487for f ulong nf
1488if eq get gfc f st
1489fn_prep f
1490put gsfn 0 0
1491em_fn f 2
1492sh_stream
1493dg_put f
1494put gq k f
1495k = add k 1
1496end
1497end
1498dg_sort k
1499put gsfn 0 0
1500for i ulong k
1501dg_emit get gq i
1502end
1503sh_stream
1504for j ulong 32
1505put ggr j get ghr j
1506end
1507for m ulong k
1508sc_final get gq m
1509end
1510end
1511
1512fn dg_put f ulong to void
1513o = mul f 32
1514for i ulong 32
1515put gfg add o i get ghr i
1516end
1517end
1518
1519fn dg_emit f ulong to void
1520o = mul f 32
1521for i ulong 32
1522sf_byte get gfg add o i
1523end
1524end
1525
1526fn sc_final f ulong to void
1527put gsfn 0 0
1528sf_byte 83
1529dg_emit f
1530for i ulong 32
1531sf_byte get ggr i
1532end
1533sh_stream
1534hr_tofn f
1535put gfk f 1
1536end
1537
1538note true when digest of fn a sorts before digest of fn b
1539fn dg_lt a ulong b ulong to bool
1540x = mul a 32
1541y = mul b 32
1542for i ulong 32
1543u = get gfg add x i
1544v = get gfg add y i
1545if ne u v
1546ret lt u v
1547end
1548end
1549ret false
1550end
1551
1552fn dg_sort k ulong to void
1553for i ulong k
1554m = i
1555for j ulong k
1556if gt j i
1557if dg_lt get gq j get gq m
1558m = j
1559end
1560end
1561end
1562t = get gq i
1563put gq i get gq m
1564put gq m t
1565end
1566end
1567
1568fn hs_all to void
1569cg_build
1570hs_dag
1571sc_all
1572nf = get gfnn 0
1573for p ulong 8192
1574if not hs_pass nf
1575break
1576end
1577end
1578for f ulong nf
1579if eq get gfk f 0
1580exit 91
1581end
1582end
1583end
1584
1585fn hs_pass nf ulong to bool
1586pr = false
1587for r ulong nf
1588if and eq get gfk r 0 eq get gfc r add r 1
1589if sc_ready r
1590sc_hash r
1591pr = true
1592end
1593end
1594end
1595ret pr
1596end
1597
1598fn un_hash to void
1599put gsfn 0 0
1600em_hdr
1601n = get gitn 0
1602for i ulong n
1603un_item i
1604end
1605sh_stream
1606end
1607
1608fn un_item i ulong to void
1609k = get gik i
1610if eq k 4
1611ret
1612end
1613if ne k 1
1614em_item i
1615ret
1616end
1617f = sub get gfm get gts get gfp un_fnof i 1
1618sf_str tfn
1619sf_sym get gfs f
1620sf_byte 32
1621sf_fhash f
1622sf_byte 10
1623end
1624
1625fn un_fnof i ulong to ulong
1626nf = get gfnn 0
1627for f ulong nf
1628if eq get gfi f i
1629ret f
1630end
1631end
1632exit 92
1633end
1634
1635fn hp_run to void
1636in_check
1637tk_all
1638it_all
1639hs_all
1640end
1641
1642note ---------------------------------------------------------------- modes
1643fn md_hash to void
1644hp_run
1645nf = get gfnn 0
1646for f ulong nf
1647ob_str tfn
1648ob_sym get gfs f
1649ob_byte 32
1650ob_fhash f
1651ob_str tline
1652ob_dec get gll get gia get gfi f
1653if ne get gfv f 0
1654ob_str tverb
1655end
1656if ne get gfc f 0
1657if gt get gfz sub get gfc f 1 1
1658ob_str tcyc
1659end
1660end
1661ob_byte 10
1662end
1663un_hash
1664ob_str tunit
1665for i ulong 32
1666v = ulong get ghr i
1667ob_byte hx_dig shr v 4
1668ob_byte hx_dig and v 15
1669end
1670ob_str tfns
1671ob_dec nf
1672ob_byte 10
1673end
1674
1675note canon letter: first of v w y z q that no kept token uses as letter+digits
1676fn cn_letidx s ulong to ulong
1677n = get gsl s
1678if lt n 2
1679ret 9
1680end
1681o = get gso s
1682for i ulong n
1683if gt i 0
1684c = get gsb add o i
1685if or lt c 48 gt c 57
1686ret 9
1687end
1688end
1689end
1690c = get gsb o
1691if eq c 118
1692ret 0
1693end
1694if eq c 119
1695ret 1
1696end
1697if eq c 121
1698ret 2
1699end
1700if eq c 122
1701ret 3
1702end
1703if eq c 113
1704ret 4
1705end
1706ret 9
1707end
1708
1709fn cn_mark s ulong to void
1710k = cn_letidx s
1711if lt k 5
1712put gbad k 1
1713end
1714end
1715
1716fn cn_scanfn f ulong to void
1717fn_prep f
1718it = get gfi f
1719l = get gia it
1720b = get gib it
1721for a ulong 2097152
1722if gt l b
1723break
1724end
1725t = get glf l
1726for i ulong get glc l
1727s = get gts add t i
1728if not rn_ok f s
1729cn_mark s
1730end
1731end
1732l = add l 1
1733end
1734end
1735
1736fn cn_letter to void
1737for k ulong 5
1738put gbad k 0
1739end
1740nf = get gfnn 0
1741for f ulong nf
1742cn_scanfn f
1743end
1744nl = get gln 0
1745for l ulong nl
1746t = get glf l
1747for i ulong get glc l
1748cn_mark get gts add t i
1749end
1750end
1751put gpf 2 cn_pick
1752end
1753
1754fn cn_pick to ulong
1755if eq get gbad 0 0
1756ret 118
1757end
1758if eq get gbad 1 0
1759ret 119
1760end
1761if eq get gbad 2 0
1762ret 121
1763end
1764if eq get gbad 3 0
1765ret 122
1766end
1767if eq get gbad 4 0
1768ret 113
1769end
1770rf_die eletr
1771ret 0
1772end
1773
1774fn cn_raw0 to void
1775a = get glr 0
1776b = get glw 0
1777for i ulong 8388609
1778if ge add a i b
1779break
1780end
1781sf_byte get gin add a i
1782end
1783sf_byte 10
1784end
1785
1786fn md_canon to void
1787in_check
1788tk_all
1789it_all
1790if ne get gpf 1 0
1791if gt get glc 0 1
1792if eq ln_tok 0 1 get gsp 4
1793rf_die efmt
1794end
1795end
1796end
1797cn_letter
1798n = get gitn 0
1799for i ulong n
1800cn_item i
1801end
1802end
1803
1804note a first line that is not an exact profile lead (om2 with a trailing blank, say) is copied raw,
1805note so canon never turns it into a lead omc would accept
1806fn cn_item i ulong to void
1807put gsfn 0 0
1808k = get gik i
1809if eq k 1
1810f = un_fnof i
1811fn_prep f
1812em_fn f 1
1813end
1814if and eq k 3 eq get gia i 0
1815cn_raw0
1816end
1817if or eq k 2 eq k 4
1818em_item i
1819end
1820if and eq k 3 ne get gia i 0
1821em_item i
1822end
1823n = get gsfn 0
1824for j ulong 16777216
1825if ge j n
1826break
1827end
1828ob_byte get gsf j
1829end
1830end
1831
1832note ---------------------------------------------------------------- diff
1833fn df_save sd ulong to void
1834nf = get gfnn 0
1835base = mul sd 8192
1836put gwc sd nf
1837for f ulong nf
1838df_one add base f f
1839end
1840end
1841
1842fn df_one w ulong f ulong to void
1843s = get gfs f
1844p = get gwp 0
1845n = get gsl s
1846if gt add p n 1048576
1847rf_die ecap
1848end
1849o = get gso s
1850for i ulong n
1851put gwb add p i get gsb add o i
1852end
1853put gwo w p
1854put gwl w n
1855put gwp 0 add p n
1856put gwm w 0
1857put gwx w get gfx f
1858x = mul w 32
1859y = mul f 32
1860for j ulong 32
1861put gwh add x j get gfh add y j
1862end
1863end
1864
1865fn df_neq a ulong b ulong to bool
1866n = get gwl a
1867if ne n get gwl b
1868ret false
1869end
1870x = get gwo a
1871y = get gwo b
1872for i ulong n
1873if ne get gwb add x i get gwb add y i
1874ret false
1875end
1876end
1877ret true
1878end
1879
1880fn df_heq a ulong b ulong to bool
1881x = mul a 32
1882y = mul b 32
1883for i ulong 32
1884if ne get gwh add x i get gwh add y i
1885ret false
1886end
1887end
1888ret true
1889end
1890
1891note index of the fn named like w on side sd, or 16384
1892fn df_find w ulong sd ulong to ulong
1893base = mul sd 8192
1894for i ulong get gwc sd
1895if df_neq w add base i
1896ret add base i
1897end
1898end
1899ret 16384
1900end
1901
1902fn df_name w ulong to void
1903o = get gwo w
1904for i ulong get gwl w
1905ob_byte get gwb add o i
1906end
1907end
1908
1909fn df_line t view uchar * w ulong to void
1910ob_str t
1911df_name w
1912ob_byte 10
1913end
1914
1915fn df_left a ulong to ulong
1916j = df_find a 1
1917if ne j 16384
1918put gwm j 1
1919if df_heq a j
1920df_line tsame a
1921ret 0
1922end
1923df_line tchg a
1924ret 1
1925end
1926for i ulong get gwc 1
1927j = add 8192 i
1928if eq get gwm j 0
1929if df_heq a j
1930if eq df_find j 0 16384
1931put gwm j 1
1932ob_str tren
1933df_name a
1934ob_str tarr
1935df_name j
1936ob_byte 10
1937ret 1
1938end
1939end
1940end
1941end
1942df_line trem a
1943ret 1
1944end
1945
1946fn df_main sd ulong to bool
1947base = mul sd 8192
1948for i ulong get gwc sd
1949if df_ismain add base i
1950ret true
1951end
1952end
1953ret false
1954end
1955
1956fn df_ismain w ulong to bool
1957s = get gsp 8
1958if ne get gwl w get gsl s
1959ret false
1960end
1961o = get gwo w
1962q = get gso s
1963for i ulong get gwl w
1964u = get gwb add o i
1965if ne u get gsb add q i
1966ret false
1967end
1968end
1969ret true
1970end
1971
1972fn df_nexp sd ulong k ulong to ulong
1973base = mul sd 8192
1974for i ulong get gwc sd
1975w = add base i
1976if ne get gwx w 0
1977if eq k 0
1978ret w
1979end
1980k = sub k 1
1981end
1982end
1983ret 16384
1984end
1985
1986fn df_exports to ulong
1987for k ulong 8193
1988a = df_nexp 0 k
1989b = df_nexp 1 k
1990if and eq a 16384 eq b 16384
1991ret 0
1992end
1993if or eq a 16384 eq b 16384
1994ob_str texp
1995ret 1
1996end
1997if not df_neq a b
1998ob_str texp
1999ret 1
2000end
2001end
2002ret 0
2003end
2004
2005fn df_entry to ulong
2006if or df_main 0 df_main 1
2007ret 0
2008end
2009if or ne df_nexp 0 0 16384 ne df_nexp 1 0 16384
2010ret 0
2011end
2012na = get gwc 0
2013nb = get gwc 1
2014if or eq na 0 eq nb 0
2015ret 0
2016end
2017a = sub na 1
2018b = add 8192 sub nb 1
2019if df_neq a b
2020ret 0
2021end
2022ob_str tent
2023df_name a
2024ob_str tarr
2025df_name b
2026ob_byte 10
2027ret 1
2028end
2029
2030fn md_diff to int
2031put gwp 0 0
2032for sd ulong 2
2033if gt sd 0
2034st_reset
2035end
2036in_file add sd 2
2037hp_run
2038df_save sd
2039end
2040nd = ulong 0
2041for i ulong get gwc 0
2042nd = add nd df_left i
2043end
2044for j ulong get gwc 1
2045w = add 8192 j
2046if eq get gwm w 0
2047df_line tadd w
2048nd = add nd 1
2049end
2050end
2051nd = add nd df_exports
2052nd = add nd df_entry
2053if eq nd 0
2054ob_str tdsame
2055ret 0
2056end
2057ob_str tdchg
2058ob_dec nd
2059ob_byte 10
2060ret 1
2061end
2062
2063note dups mode: stdin holds "PATH FN name hex line=N..." records (hash-mode output with a path
2064note prefixed). Functions with the same 64-digit hash are grouped; "SKIP " lines are echoed and counted.
2065note Per record: line start, the space after PATH, the space after name, the line end, a
2066note 16-digit key, and the group id (0: not grouped yet). gqn: record count, SKIP count.
2067global gqs ulong 262144
2068global gqp ulong 262144
2069global gqr ulong 262144
2070global gqe ulong 262144
2071global gqk ulong 262144
2072global gqg ulong 262144
2073global gqn ulong 2
2074text tskip "SKIP "
2075
2076fn dp_hexv c uchar to ulong
2077v = ulong c
2078if and ge v 48 le v 57
2079ret sub v 48
2080end
2081if and ge v 97 le v 102
2082ret sub v 87
2083end
2084ret 16
2085end
2086
2087note first space in s..e (e excluded), or e when there is none
2088fn dp_sp s ulong e ulong to ulong
2089p = s
2090for a ulong 8388610
2091if ge p e
2092ret e
2093end
2094if eq get gin p 32
2095ret p
2096end
2097p = add p 1
2098end
2099ret e
2100end
2101
2102note a FN record s..e (e excluded): returns the space after name, or 0 when the line is not one
2103fn dp_rec s ulong e ulong to ulong
2104p = dp_sp s e
2105if eq p s
2106ret 0
2107end
2108if gt add p 4 e
2109ret 0
2110end
2111if ne get gin add p 1 70
2112ret 0
2113end
2114if ne get gin add p 2 78
2115ret 0
2116end
2117if ne get gin add p 3 32
2118ret 0
2119end
2120r = dp_sp add p 4 e
2121if eq r add p 4
2122ret 0
2123end
2124if gt add r 65 e
2125ret 0
2126end
2127for i ulong 64
2128if eq dp_hexv get gin add add r 1 i 16
2129ret 0
2130end
2131end
2132if lt add r 65 e
2133if ne get gin add r 65 32
2134ret 0
2135end
2136end
2137ret r
2138end
2139
2140fn dp_skip s ulong e ulong to bool
2141if lt sub e s len tskip
2142ret false
2143end
2144for i ulong len tskip
2145if ne get gin add s i get tskip i
2146ret false
2147end
2148end
2149ret true
2150end
2151
2152fn dp_heq a ulong b ulong to bool
2153for i ulong 64
2154if ne get gin add a i get gin add b i
2155ret false
2156end
2157end
2158ret true
2159end
2160
2161fn ob_span a ulong b ulong to void
2162for i ulong sub b a
2163ob_byte get gin add a i
2164end
2165end
2166
2167note store one record of line s..e whose name ends at the space r
2168fn dp_put s ulong e ulong r ulong to void
2169nr = get gqn 0
2170if ge nr 262144
2171rf_die ecap
2172end
2173k = ulong 0
2174for i ulong 16
2175k = or shl k 4 dp_hexv get gin add add r 1 i
2176end
2177put gqs nr s
2178put gqp nr dp_sp s e
2179put gqr nr r
2180put gqe nr e
2181put gqk nr k
2182put gqg nr 0
2183put gqn 0 add nr 1
2184end
2185
2186fn dp_parse to void
2187n = get ginn 0
2188if gt n 8388608
2189rf_die ebig
2190end
2191put gqn 0 0
2192put gqn 1 0
2193s = ulong 0
2194for a ulong 8388610
2195if ge s n
2196break
2197end
2198e = s
2199for b ulong 8388610
2200if ge e n
2201break
2202end
2203if eq get gin e 10
2204break
2205end
2206e = add e 1
2207end
2208if dp_skip s e
2209put gqn 1 add get gqn 1 1
2210ob_span s e
2211ob_byte 10
2212end
2213r = dp_rec s e
2214if ne r 0
2215dp_put s e r
2216end
2217s = add e 1
2218end
2219end
2220
2221note " PATH name line=N..." for record j
2222fn dp_out j ulong to void
2223ob_byte 32
2224ob_byte 32
2225ob_span get gqs j get gqp j
2226ob_byte 32
2227ob_span add get gqp j 4 get gqr j
2228ob_span add get gqr j 65 get gqe j
2229ob_byte 10
2230end
2231
2232note mark every later ungrouped record with record i's hash as group g; returns how many
2233fn dp_mark i ulong g ulong to ulong
2234nr = get gqn 0
2235ki = get gqk i
2236hi = add get gqr i 1
2237cnt = ulong 0
2238for t ulong sub nr add i 1
2239j = add add i 1 t
2240if eq get gqk j ki
2241if eq get gqg j 0
2242if dp_heq hi add get gqr j 1
2243put gqg j g
2244cnt = add cnt 1
2245end
2246end
2247end
2248end
2249ret cnt
2250end
2251
2252fn md_dups to void
2253dp_parse
2254nr = get gqn 0
2255ng = ulong 0
2256nf = ulong 0
2257for i ulong nr
2258if eq get gqg i 0
2259cnt = dp_mark i add ng 1
2260if gt cnt 0
2261ng = add ng 1
2262put gqg i ng
2263nf = add nf add cnt 1
2264hi = add get gqr i 1
2265ob_str tdup
2266ob_span hi add hi 64
2267ob_str tdn
2268ob_dec add cnt 1
2269ob_byte 10
2270for t ulong sub nr i
2271if eq get gqg add i t ng
2272dp_out add i t
2273end
2274end
2275end
2276end
2277end
2278ob_str tdups
2279ob_dec ng
2280ob_str tdfns
2281ob_dec nf
2282ob_str tdrec
2283ob_dec nr
2284ob_str tdskp
2285ob_dec get gqn 1
2286ob_byte 10
2287end
2288
2289fn ar_eq a view uchar * n ulong t view uchar * to bool
2290if ne n len t
2291ret false
2292end
2293for i ulong n
2294if ne get a i get t i
2295ret false
2296end
2297end
2298ret true
2299end
2300
2301fn main to int
2302st_reset
2303c = argcnt
2304if lt c 2
2305in_stdin
2306md_hash
2307ob_flush
2308ret 0
2309end
2310pb = new uchar 64 0
2311r = argread 1 pb 0 64
2312if le r 0
2313die_msg eusage 2
2314end
2315n = ulong r
2316if and ar_eq pb n mdiff eq c 4
2317x = md_diff
2318ob_flush
2319ret x
2320end
2321if ne c 2
2322die_msg eusage 2
2323end
2324if ar_eq pb n mhash
2325in_stdin
2326md_hash
2327ob_flush
2328ret 0
2329end
2330if ar_eq pb n mcanon
2331in_stdin
2332md_canon
2333ob_flush
2334ret 0
2335end
2336if ar_eq pb n mdups
2337in_stdin
2338md_dups
2339ob_flush
2340ret 0
2341end
2342if ar_eq pb n mkw
2343ob_str tkw1
2344ob_str tkw2
2345ob_flush
2346ret 0
2347end
2348die_msg eusage 2
2349ret 2
2350end