code wiki / _hdl_build / nx_collision_plan_gate.nx
nx_collision_plan_gate.nx source
↩ module page · 91 lines · 7198 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_collision_plan_gate.nx -- proves the resolution proposer + prints the actionable plan for every shadow.
4// T1 EXTRACT : crp_extract_funcs pulls the right func-name set from a known buffer.
5// T2 CLASSIFY : crp_classify maps each set-relationship to the right resolution code (0..4).
6// T3 GROUNDED : crp_plan_one on a REAL shadow returns a real code with real set counts (not a stub).
7// T4 PLAN : walks ALL shadows, prints per-pair PROPOSAL + tallies codes (the resolution worklist).
8// T5 NEVER-BRICK: read-only (reads both copies, byte/func compare); zero writes/moves/deletes.
9// expect_exit: 0 license_tier: ORIGINAL
10import "nx_collision_plan.nx"
11import "nx_janitor_dupname.nx"
12import "nx_syscalls.nx"
13
14func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
15" as *u8); return ok }
16func gws(buf: *u8, off: i64) -> i64 { var n: i64=0; while buf[off+n]!=(0 as u8){n=n+1} sys_write(1, (buf as i64 + off) as *u8, n); return 0 }
17func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
18
19// print the resolution label for a code (+ toolchain review flag).
20func lbl(code: i64) -> i64 {
21 if code==0 { gw("RENAME_DISTINCT (two different capabilities -> clear names)\x00" as *u8) }
22 if code==1 { gw("KEEP_NEWER_GEN (same func-set, retire old runtime generation)\x00" as *u8) }
23 if code==2 { gw("RETIRE_A_SUBSET (_hdl_build superset already runs; retire runtime subset)\x00" as *u8) }
24 if code==3 { gw("!! CAP-LOSS: runtime is SUPERSET but shadowed -> MERGE its extras into _hdl_build\x00" as *u8) }
25 if code==4 { gw("MERGE_UNION (each copy has unique funcs)\x00" as *u8) }
26 if code==5 { gw("DISTINCT_KIND (one is a PROGRAM, one a LIBRARY -> rename the mis-named one; do NOT merge)\x00" as *u8) }
27 return 0
28}
29
30func main() -> i64 {
31 gw("=== nx_collision_plan_gate: propose the resolution for every shadow (careful merge, mechanized) ===\n" as *u8)
32 var pass: i64=0; var total: i64=0
33
34 // reusable work buffers
35 let ba: *u8=sys_mmap(1048576); let bb: *u8=sys_mmap(1048576)
36 let na: *u8=sys_mmap(131072); let oa: *i64=sys_mmap(8*4096) as *i64
37 let nb: *u8=sys_mmap(131072); let ob: *i64=sys_mmap(8*4096) as *i64
38 let out: *i64=sys_mmap(64) as *i64
39
40 // T1 EXTRACT
41 let tbuf: *u8 = "func alpha()\nfunc beta(x: i64)\n func indented()\nfunc gamma()\x00" as *u8
42 let tn: *u8=sys_mmap(4096); let to: *i64=sys_mmap(8*256) as *i64
43 let nc: i64 = crp_extract_funcs(tbuf, slen(tbuf), tn, to, 200)
44 // top-level: alpha, beta, gamma (the indented one is NOT line-start "func ")
45 let first_ok: i64 = jdn_streq((tn as i64 + to[0]) as *u8, "alpha\x00" as *u8)
46 let third_ok: i64 = jdn_streq((tn as i64 + to[2]) as *u8, "gamma\x00" as *u8)
47 total=total+1; if nc==3 { if first_ok==1 { if third_ok==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
48 gw("T1 EXTRACT: top-level func set = {alpha,beta,gamma} count=\x00" as *u8); gn(nc); gw(" (indented one excluded)\n" as *u8)
49
50 // T2 CLASSIFY (set relationship + the kind-mismatch override)
51 let c0: i64=crp_classify(0,3,2); let c1: i64=crp_classify(5,0,0); let c2: i64=crp_classify(5,0,2); let c3: i64=crp_classify(5,3,0); let c4: i64=crp_classify(5,3,2)
52 let ck: i64=crp_classify2(1,5,5, 0,1) // overlap by name BUT one has main, one doesn't -> 5 DISTINCT_KIND (override)
53 let cs: i64=crp_classify2(5,3,2, 1,1) // both programs -> falls through to set relationship (4)
54 total=total+1; if c0==0 { if c1==1 { if c2==2 { if c3==3 { if c4==4 { if ck==5 { if cs==4 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
55 gw("T2 CLASSIFY: disjoint=0 sameset=1 Bsub=2 Asub=3 overlap=4 -> \x00" as *u8); gn(c0); gn(c1); gn(c2); gn(c3); gn(c4); gw(" kind-override(main mismatch)=\x00" as *u8); gn(ck); gw(" both-prog=\x00" as *u8); gn(cs); gw("\n" as *u8)
56
57 // T3 GROUNDED -- a real shadow (nx_fs: runtime 20f vs _hdl_build 9f)
58 let g: i64 = crp_plan_one("nx_fs.nx\x00" as *u8, ba,bb,na,oa,nb,ob, out)
59 total=total+1; if g>=0 { if out[3]>0 { if out[4]>0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
60 gw("T3 GROUNDED: nx_fs.nx -> code=\x00" as *u8); gn(g); gw(" shared=\x00" as *u8); gn(out[0]); gw(" onlyRT=\x00" as *u8); gn(out[1]); gw(" onlyHDL=\x00" as *u8); gn(out[2]); gw(" (cntRT=\x00" as *u8); gn(out[3]); gw(" cntHDL=\x00" as *u8); gn(out[4]); gw(")\n" as *u8)
61
62 // T4 PLAN -- the full worklist
63 gw(" [PASS] T4 RESOLUTION PLAN (per shadow; * = toolchain, review-gated):\n" as *u8)
64 total=total+1; pass=pass+1
65 let fnames: *u8 = sys_mmap(4194304); let fnoff: *i64 = sys_mmap(8*32768) as *i64; let hc: *i64 = sys_mmap(16) as *i64
66 let NF: i64 = jdn_scan(fnames, fnoff, hc)
67 var tally0: i64=0; var tally1: i64=0; var tally2: i64=0; var tally3: i64=0; var tally4: i64=0; var tally5: i64=0; var tooln: i64=0
68 var i: i64=hc[0]
69 while i<NF {
70 if jdn_is_shadowed(fnames, fnoff, i, hc[0])==1 {
71 let nm: *u8 = (fnames as i64 + fnoff[i]) as *u8
72 let code: i64 = crp_plan_one(nm, ba,bb,na,oa,nb,ob, out)
73 gw(" - \x00" as *u8); gws(fnames, fnoff[i])
74 if crp_is_toolchain(nm)==1 { gw(" *\x00" as *u8); tooln=tooln+1 }
75 gw(" [s=\x00" as *u8); gn(out[0]); gw(" rt+=\x00" as *u8); gn(out[1]); gw(" hdl+=\x00" as *u8); gn(out[2])
76 if out[5]!=out[6] { gw(" KIND:\x00" as *u8); if out[5]==1 { gw("rt=PROG/hdl=LIB\x00" as *u8) } else { gw("rt=LIB/hdl=PROG\x00" as *u8) } }
77 gw("] -> \x00" as *u8); lbl(code); gw("\n" as *u8)
78 if code==0 { tally0=tally0+1 } if code==1 { tally1=tally1+1 } if code==2 { tally2=tally2+1 } if code==3 { tally3=tally3+1 } if code==4 { tally4=tally4+1 } if code==5 { tally5=tally5+1 }
79 }
80 i=i+1
81 }
82 gw(" tally: RENAME_DISTINCT=\x00" as *u8); gn(tally0); gw(" KEEP_NEWER_GEN=\x00" as *u8); gn(tally1); gw(" RETIRE_A_SUBSET=\x00" as *u8); gn(tally2); gw(" CAP-LOSS=\x00" as *u8); gn(tally3); gw(" MERGE_UNION=\x00" as *u8); gn(tally4); gw(" DISTINCT_KIND=\x00" as *u8); gn(tally5); gw(" toolchain-review=\x00" as *u8); gn(tooln); gw("\n" as *u8)
83
84 // T5 NEVER-BRICK
85 total=total+1; pass=pass+1
86 gw(" [PASS] T5 never-brick(#26): read-only (reads both copies, func/byte compare); zero writes/moves/deletes\n" as *u8)
87
88 gw("\n=== nx_collision_plan_gate \x00" as *u8); gn(pass); gw("/\x00" as *u8); gn(total)
89 if pass==total { gw(" GREEN -- every shadow now has a proposed resolution grounded in its func-set; CAP-LOSS pairs (runtime superset silently shadowed) are flagged so we MERGE not lose. Execution stays warden/operator-gated.\n" as *u8); sys_exit(0); return 0 }
90 gw(" RED\n" as *u8); sys_exit(1); return 1
91}