code wiki / _hdl_build / nx_self_host_candidate_verify.nx
nx_self_host_candidate_verify.nx source
↩ module page · 74 lines · 4014 B
1// nx_self_host_candidate_verify.nx -- the TEAM's capability to drive codegen
2// integration itself: BUILD a candidate compiler from source, then VERIFY it.
3//
4// This is the machine the team turns to change codegen (e.g. widen nx_x86_regalloc)
5// safely, with no human at a shell:
6// 1. SELF-HOST: the pinned known-good compiler compiles the compiler's OWN source
7// (runtime/nx_compile_x86.nx + imports) -> a CANDIDATE compiler ELF. This is
8// the bootstrap step; if a codegen-source change is bad, it shows up here.
9// 2. DIFFERENTIAL VERIFY: run a corpus of self-verifying programs through the
10// known-good AND the freshly-built candidate, and assert IDENTICAL behaviour.
11// A miscompiling candidate DIVERGES and is caught -- the known-good stays
12// pinned (Warden); only an all-green candidate is eligible for promotion.
13//
14// Built non-destructively: the candidate lands in /tmp; nothing protected is
15// touched. With the source UNCHANGED, the candidate must behave identically to
16// known-good -- proving the build+verify pipeline end to end so the team can then
17// plug a real regalloc change into the SAME machine. Known answer: exit 0.
18
19import "nx_codegen_exec.nx"
20
21func main() -> i64 {
22 gd_puts("=== TEAM CAPABILITY: build a candidate compiler from source + verify ===\n" as *u8)
23 let kg: *u8 = "_offc/nx_cc_known_good.elf" as *u8
24 let cc_src: *u8 = "runtime/nx_compile_x86.nx" as *u8
25
26 // 1. SELF-HOST: known-good compiles the compiler source -> candidate compiler.
27 gd_puts(" [1] self-hosting a candidate compiler from source ...\n" as *u8)
28 let bc: i64 = gd_build(kg, cc_src, "/tmp/cand_cc.s" as *u8, "/tmp/cand_cc.elf" as *u8)
29 gd_emit(" candidate compiler build rc (0=ok) : " as *u8, bc)
30 if bc != 0 {
31 gd_puts(" candidate did NOT build -- a source change broke the bootstrap.\n" as *u8)
32 sys_exit(1); return 1
33 }
34 let cand: *u8 = "/tmp/cand_cc.elf" as *u8
35
36 // 2. DIFFERENTIAL VERIFY the candidate compiler vs known-good on a corpus.
37 gd_puts(" [2] differential-verifying the candidate vs known-good ...\n" as *u8)
38 let corpus: *i64 = sys_mmap(8 * 8) as *i64
39 var nc: i64 = 0
40 corpus[nc] = ("runtime/_hdl_build/nx_u128_test.nx" as *u8) as i64; nc = nc + 1
41 corpus[nc] = ("runtime/_hdl_build/nx_regalloc_linscan_test.nx" as *u8) as i64; nc = nc + 1
42
43 var diverge: i64 = 0
44 var pipefail: i64 = 0
45 var i: i64 = 0
46 while i < nc {
47 let src: *u8 = corpus[i] as *u8
48 let ka: i64 = gd_build_run(kg, src, "/tmp/sv_k.s" as *u8, "/tmp/sv_k.elf" as *u8)
49 let ca: i64 = gd_build_run(cand, src, "/tmp/sv_c.s" as *u8, "/tmp/sv_c.elf" as *u8)
50 gd_puts(" prog: " as *u8); gd_puts(src); gd_puts("\n" as *u8)
51 if ka < 0 { pipefail = pipefail + 1; gd_emit(" known-good pipeline FAILED : " as *u8, ka) }
52 if ca < 0 { pipefail = pipefail + 1; gd_emit(" candidate pipeline FAILED : " as *u8, ca) }
53 if ka >= 0 { if ca >= 0 {
54 gd_emit(" known-good exit : " as *u8, ka)
55 gd_emit(" candidate exit : " as *u8, ca)
56 if ka != ca { gd_puts(" *** DIVERGE -- candidate compiler miscompiles ***\n" as *u8); diverge = diverge + 1 }
57 else { gd_puts(" agree\n" as *u8) }
58 } }
59 i = i + 1
60 }
61
62 gd_puts("----------------------------------------------------------------\n" as *u8)
63 gd_emit(" candidate built (0=ok) : " as *u8, bc)
64 gd_emit(" divergences : " as *u8, diverge)
65 gd_emit(" pipeline failures : " as *u8, pipefail)
66 gd_puts(" -> the team can now build a candidate compiler + verify it with no human.\n" as *u8)
67 gd_puts(" Plug a regalloc change into step 1's source and the SAME machine gates it.\n" as *u8)
68
69 // GATE: candidate built AND behaves identically to known-good on the corpus.
70 if pipefail != 0 { sys_exit(2); return 2 }
71 if diverge != 0 { sys_exit(3); return 3 }
72 sys_exit(0)
73 return 0
74}