code wiki / _hdl_build / nx_portability_census.nx

nx_portability_census.nx source

↩ module page · 143 lines · 6736 B

1// nx_portability_census.nx -- the HARDWARE-ABSTRACTION-LAYER reach census (operator 2026-06-13: 2// "translate our work across all hardware as a layer while keeping S-class-exceed performance, as 3// foundation work"). 4// 5// AUTHOR=ORGAN, measured (no-wave): reads portability_targets.tsv (the ISAs Nishi should reach) and 6// COMPUTES PRESENT/ABSENT per target from REAL evidence -- does the codegen backend file actually 7// exist (= nxc2 can emit NATIVE code for that ISA)? -- via sys_openat_rd, NOT asserted. The bottom 8// HAL = these compile-time native backends (zero-cost portability: native per arch, no runtime VM). 9// Writes portability_census.tsv + an honest bm_verdict. Self-validating: pos control x86_64.c MUST 10// exist, neg control MUST NOT. Composes nx_benchmark (honest verdict, refuses cherry-pick). 11// The remaining foundation work (ABSENT targets + making the rv64-only kernel emitters ride these 12// backends) is the build-to-portability backlog. license_tier: ORIGINAL 13import "nx_syscalls.nx" 14import "nx_benchmark.nx" 15const KP_MAGIC_80000: i64 = 80000 16const KP_MAGIC_200000: i64 = 200000 17 18const KP_REF: *u8 = "knowledge/registry/portability_targets.tsv" 19const KP_OUT: *u8 = "knowledge/registry/portability_census.tsv" 20const KP_LOG: *u8 = "knowledge/status/portability_census.log" 21const KP_MAXF: i64 = 256 22 23func kp_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 24func kp_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 } 25 26func kp_read_file(path: *u8, buf: *u8, cap: i64) -> i64 { 27 let fd: i64 = sys_openat_rd(path) 28 if fd < 0 { return 0 - 1 } 29 var tot: i64 = 0 30 var r: i64 = 1 31 while r > 0 { 32 let dst: *u8 = ((buf as i64) + tot) as *u8 33 r = sys_read(fd, dst, cap - tot) 34 if r > 0 { tot = tot + r } 35 } 36 sys_close(fd) 37 return tot 38} 39 40// REAL evidence: the codegen backend file exists -> nxc2 can emit native for this ISA. 41func kp_exists(path: *u8) -> i64 { 42 let fd: i64 = sys_openat_rd(path) 43 if fd < 0 { return 0 } 44 sys_close(fd) 45 return 1 46} 47 48func kp_scan_to(buf: *u8, n: i64, start: i64, delim: i64) -> i64 { 49 var i: i64 = start 50 var s: i64 = 1 51 while s == 1 { if i >= n { s = 0 } else { if buf[i] == (delim as u8) { s = 0 } else { i = i + 1 } } } 52 return i 53} 54 55func kp_app(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } return off + i } 56 57func kp_vstr(v: i64) -> *u8 { 58 if v == BM_AHEAD { return "AHEAD" as *u8 } 59 if v == BM_PARITY { return "PARITY" as *u8 } 60 if v == BM_BEHIND { return "BEHIND" as *u8 } 61 if v == BM_DIFFERENTIATED { return "DIFFERENTIATED" as *u8 } 62 return "REFUSE-cherrypick" as *u8 63} 64 65func kp_emit(fd: i64, nf: i64, present: i64, cpos: i64, cneg: i64, cov: i64, verdict: i64, ok: i64) -> i64 { 66 kp_w(fd, "PORTGATE authored=organ layer=compile-time-native-emit source=portability_targets.tsv targets=" as *u8); kp_wn(fd, nf) 67 kp_w(fd, " present=" as *u8); kp_wn(fd, present) 68 kp_w(fd, " absent=" as *u8); kp_wn(fd, nf - present) 69 kp_w(fd, " control_pos=" as *u8); kp_wn(fd, cpos) 70 kp_w(fd, " control_neg=" as *u8); kp_wn(fd, cneg) 71 kp_w(fd, " reach_permil=" as *u8); kp_wn(fd, cov) 72 kp_w(fd, " perf=zero-cost-native exceed_verdict=" as *u8); kp_w(fd, kp_vstr(verdict)) 73 if ok == 1 { kp_w(fd, " verdict=GREEN\n" as *u8) } else { kp_w(fd, " verdict=RED reason=control-or-empty\n" as *u8) } 74 return 0 75} 76 77func main() -> i64 { 78 let rb: *u8 = sys_mmap(KP_MAGIC_80000) 79 let rbn: i64 = kp_read_file(KP_REF, rb, KP_MAGIC_80000) 80 if rbn <= 0 { kp_w(1, "PORTGATE verdict=RED reason=ref-unreadable\n" as *u8); return 1 } 81 82 // parse isa \t backend_file \t class 83 let isa: *i64 = sys_mmap(8 * KP_MAXF) as *i64 84 let bfp: *i64 = sys_mmap(8 * KP_MAXF) as *i64 85 let cls: *i64 = sys_mmap(8 * KP_MAXF) as *i64 86 var nf: i64 = 0 87 var p: i64 = 0 88 while p < rbn { 89 if rb[p] == (35 as u8) { let e: i64 = kp_scan_to(rb, rbn, p, 10); p = e + 1 } 90 else { if rb[p] == (10 as u8) { p = p + 1 } 91 else { 92 let a0: i64 = p 93 let t1: i64 = kp_scan_to(rb, rbn, a0, 9); rb[t1] = 0 as u8 94 let a1: i64 = t1 + 1 95 let t2: i64 = kp_scan_to(rb, rbn, a1, 9); rb[t2] = 0 as u8 96 let a2: i64 = t2 + 1 97 let t3: i64 = kp_scan_to(rb, rbn, a2, 10); rb[t3] = 0 as u8 98 if nf < KP_MAXF { isa[nf] = (rb as i64) + a0; bfp[nf] = (rb as i64) + a1; cls[nf] = (rb as i64) + a2; nf = nf + 1 } 99 p = t3 + 1 100 } } 101 } 102 103 // compute reach per ISA from REAL backend-file existence + author the census. 104 let ob: *u8 = sys_mmap(KP_MAGIC_200000) 105 var o: i64 = 0 106 o = kp_app(ob, o, "# AUTHORED BY nx_portability_census -- Nishi cross-hardware reach (HAL). Status COMPUTED from REAL codegen-backend existence (sys_openat_rd), NOT asserted. PRESENT=nxc2 emits NATIVE for this ISA (zero-cost layer). The rv64-only kernel emitters riding these = the foundation gap.\n" as *u8) 107 o = kp_app(ob, o, "# columns: status\tisa\tclass\tbackend\n" as *u8) 108 var present: i64 = 0 109 var i: i64 = 0 110 while i < nf { 111 let ex: i64 = kp_exists((bfp[i]) as *u8) 112 if ex == 1 { o = kp_app(ob, o, "PRESENT\t" as *u8); present = present + 1 } else { o = kp_app(ob, o, "ABSENT\t" as *u8) } 113 o = kp_app(ob, o, (isa[i]) as *u8) 114 o = kp_app(ob, o, "\t" as *u8) 115 o = kp_app(ob, o, (cls[i]) as *u8) 116 o = kp_app(ob, o, "\t" as *u8) 117 o = kp_app(ob, o, (bfp[i]) as *u8) 118 o = kp_app(ob, o, "\n" as *u8) 119 i = i + 1 120 } 121 let wfd: i64 = sys_openat_wr(KP_OUT, 420) 122 if wfd < 0 { kp_w(1, "PORTGATE verdict=RED reason=out-unwritable\n" as *u8); return 1 } 123 sys_write(wfd, ob, o) 124 sys_close(wfd) 125 126 // self-validating controls + honest verdict. 127 let cpos: i64 = kp_exists("x86_64.c" as *u8) 128 let cneg: i64 = kp_exists("zzqnonexistent_backend.c" as *u8) 129 let valid: i64 = bm_is_valid(nf, nf, 800) 130 var cov: i64 = 0 131 if nf > 0 { cov = (present * 1000) / nf } 132 let verdict: i64 = bm_verdict(valid, cov, 1000) 133 var ok: i64 = 1 134 if cpos != 1 { ok = 0 } 135 if cneg != 0 { ok = 0 } 136 if nf <= 0 { ok = 0 } 137 138 kp_emit(1, nf, present, cpos, cneg, cov, verdict, ok) 139 let lf: i64 = sys_openat_append(KP_LOG, 420) 140 if lf >= 0 { kp_emit(lf, nf, present, cpos, cneg, cov, verdict, ok); sys_close(lf) } 141 if ok == 1 { return 0 } 142 return 1 143}