code wiki / _hdl_build / nx_autorace_test.nx

nx_autorace_test.nx source

↩ module page · 114 lines · 5499 B

1// nx_autorace_test.nx -- the team races gcc -O2 AUTONOMOUSLY. For each kernel it: writes 2// the C source itself, synthesizes+emits its own machine code and VERIFIES it 1:1 by 3// execution, then uses its OWN gcc-race capability (nx_gcc_race: fork gcc -> fork objdump 4// -> parse) to measure gcc's instruction count, and JUDGES win/tie/loss. No shell, no me. 5// Gate: every team sequence verified by execution AND gcc measured (>0) -> the team raced 6// correctly. Whether it WINS is then reported honestly, by the team's own measurement. 7 8import "nx_mulchain.nx" // GENERATOR (uniform: minimal shift-add chain for any C) 9import "nx_superopt_emit.nx" // EMITTER (se_emit_full, lea fusion) 10import "nx_engineer_crash.nx" // ENGINEER (eng_link + eng_run) 11import "nx_gcc_race.nx" // the team's own gcc measurement (gr_race_c) 12 13func ar_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func ar_num(v: i64) -> i64 { 15 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m } 16 let t: *u8 = sys_mmap(28); var k: i64 = 0 17 if m == 0 { t[0] = 48; k = 1 } 18 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 } 19 if v < 0 { ar_puts("-" as *u8) } 20 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 21 sys_write(1, bb, k); return 0 22} 23func tally(r: *i64, n: i64) -> i64 { var ec: i64 = 0; var i: i64 = 0; while i < n { if r[i] != 1 { if ec == 0 { ec = i + 1 } } i = i + 1 } return ec } 24func ar_write(path: *u8, buf: *u8, len: i64) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 - 1 } sys_write(fd, buf, len); sys_close(fd); return 0 } 25 26// the team writes its own C source for x*C: "long f(long x){return x*C;}\n" 27func build_c(c: i64, path: *u8) -> i64 { 28 let buf: *u8 = sys_mmap(128) 29 var oi: i64 = 0 30 let pre: *u8 = "long f(long x){return x*" as *u8 31 var j: i64 = 0; while pre[j] != (0 as u8) { buf[oi] = pre[j]; oi = oi + 1; j = j + 1 } 32 let t: *u8 = sys_mmap(28); var m: i64 = c; var k: i64 = 0 33 if m == 0 { t[0] = 48; k = 1 } 34 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 } 35 var i: i64 = 0; while i < k { buf[oi] = t[k - 1 - i]; oi = oi + 1; i = i + 1 } 36 let post: *u8 = ";}\n" as *u8 37 j = 0; while post[j] != (0 as u8) { buf[oi] = post[j]; oi = oi + 1; j = j + 1 } 38 ar_write(path, buf, oi) 39 return 0 40} 41 42// race one kernel. res: 0=L 1=team 2=verified 3=gcc 4=verdict(1 win/0 tie/-1 loss) 43func race_one(c: i64, res: *i64) -> i64 { 44 let op: *i64 = sys_mmap(8 * 12) as *i64 45 let a: *i64 = sys_mmap(8 * 12) as *i64 46 let b: *i64 = sys_mmap(8 * 12) as *i64 47 let dd: *i64 = sys_mmap(8 * 16) as *i64 48 let buf: *u8 = sys_mmap(8192) 49 let icnt: *i64 = sys_mmap(8) as *i64 50 dd[0]=5; dd[1]=0-8; dd[2]=123; dd[3]=0-456; dd[4]=77; dd[5]=0-1; dd[6]=1000; dd[7]=0-1000 51 var ref: i64 = 0 52 var kk: i64 = 0; while kk < 8 { ref = ref + dd[kk] * c; kk = kk + 1 } 53 ref = ref & 255 54 55 let L: i64 = mulchain_find(c, 8, op, a, b) 56 res[0] = L 57 let blen: i64 = se_emit_full(op, a, b, L, dd, 8, buf, icnt) 58 res[1] = icnt[0] 59 ar_write("/tmp/ar.s" as *u8, buf, blen) 60 res[2] = 0 61 if eng_link("/tmp/ar.s" as *u8, "/tmp/ar.elf" as *u8) == 0 { 62 if eng_run("/tmp/ar.elf" as *u8, 0 as *u8) == ref { res[2] = 1 } 63 } 64 build_c(c, "/tmp/ar.c" as *u8) 65 res[3] = gr_race_c("/tmp/ar.c" as *u8, "/tmp/ar.o" as *u8, "/tmp/ar.txt" as *u8, "f" as *u8) 66 res[4] = 0 67 if res[1] < res[3] { res[4] = 1 } 68 if res[1] > res[3] { res[4] = 0 - 1 } 69 return 0 70} 71 72func run_all(ks: *i64, nk: i64, mat: *i64, r: *i64) -> i64 { 73 let res: *i64 = sys_mmap(8 * 8) as *i64 74 var i: i64 = 0 75 while i < nk { 76 race_one(ks[i], res) 77 mat[i * 5 + 0] = res[0]; mat[i * 5 + 1] = res[1]; mat[i * 5 + 2] = res[2]; mat[i * 5 + 3] = res[3]; mat[i * 5 + 4] = res[4] 78 r[i] = 0 79 if res[2] == 1 { if res[3] > 0 { r[i] = 1 } } // verified AND gcc measured = a valid race 80 i = i + 1 81 } 82 return 0 83} 84 85func report(ks: *i64, nk: i64, mat: *i64) -> i64 { 86 var wins: i64 = 0 87 var ties: i64 = 0 88 var k: i64 = 0 89 while k < nk { 90 ar_puts(" x*" as *u8); ar_num(ks[k]) 91 ar_puts(": team " as *u8); ar_num(mat[k * 5 + 1]); ar_puts(" insns vs gcc-O2 " as *u8); ar_num(mat[k * 5 + 3]) 92 ar_puts(" verified=" as *u8); if mat[k * 5 + 2] == 1 { ar_puts("YES" as *u8) } else { ar_puts("NO" as *u8) } 93 ar_puts(" -> " as *u8) 94 if mat[k * 5 + 4] == 1 { ar_puts("WIN\n" as *u8); wins = wins + 1 } else { if mat[k * 5 + 4] == 0 { ar_puts("tie\n" as *u8); ties = ties + 1 } else { ar_puts("behind\n" as *u8) } } 95 k = k + 1 96 } 97 ar_puts("----------------------------------------------------------------\n" as *u8) 98 ar_puts(" team raced gcc -O2 ITSELF (fork gcc + objdump + parse): " as *u8); ar_num(wins); ar_puts(" wins, " as *u8); ar_num(ties); ar_puts(" ties.\n" as *u8) 99 return 0 100} 101 102func main() -> i64 { 103 ar_puts("=== TEAM races gcc -O2 autonomously (writes C, emits+verifies, measures gcc, judges) ===\n" as *u8) 104 let ks: *i64 = sys_mmap(8 * 16) as *i64 105 ks[0]=3; ks[1]=5; ks[2]=7; ks[3]=9; ks[4]=10; ks[5]=11; ks[6]=45; ks[7]=100 106 let nk: i64 = 8 107 let mat: *i64 = sys_mmap(8 * 5 * 16) as *i64 108 let r: *i64 = sys_mmap(8 * 16) as *i64 109 run_all(ks, nk, mat, r) 110 report(ks, nk, mat) 111 let ec: i64 = tally(r, nk) // tally right before exit, no calls between 112 sys_exit(ec) 113 return ec 114}