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