code wiki / _hdl_build / nx_eoe.nx

nx_eoe.nx source

↩ module page · 77 lines · 4868 B

1// nx_eoe.nx -- EMITTER-OF-EMITTERS (X-AUT-006c/006e): the keystone for autonomous capability 2// creation. The verbatim/constant transducers (X-AUT-006/006b) CANNOT synthesize new control flow. 3// THE HOW (X-AUT-006e): the team ALREADY owns a control-flow synthesizer -- the COMPILER. So the 4// emitter-of-emitters is the parser's DUAL: a STRUCTURAL SPEC (control-flow skeleton as DATA) -> 5// PRETTY-PRINT NishiLang SOURCE -> the existing nx_cc compiles it (control-flow -> machine code 6// DELEGATED to the compiler the team owns). This organ generates a module with `depth` NESTED loops 7// (count driven by DATA, not a fixed template) summing the product of the loop indices -- so the 8// CONTROL-FLOW STRUCTURE is synthesized from the spec, which constant-lifting provably cannot do 9// (it cannot add/remove loops). nx_cc then turns that source into native code. 10// nx_eoe <basename> <depth> <bound> -> writes runtime/_hdl_build/<basename>.nx 11// Validated (no-false-green, X-AUT-006d): depth=1 and depth=2 produce DIFFERENT control flow, both 12// compile + compute the correct (different) result. Sovereign, no gcc/.sh. license_tier: ORIGINAL 13import "nx_syscalls.nx" 14const K_MAGIC_65536: i64 = 65536 15 16func eo_atoi(s: *u8) -> i64 { var n: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { if s[i] >= (48 as u8) { if s[i] <= (57 as u8) { n = n * 10 + ((s[i] as i64) - 48) } } i = i + 1 } return n } 17func eo_cat(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 } 18func eo_catn(dst: *u8, off: i64, v: i64) -> i64 { 19 var o: i64 = off 20 if v == 0 { dst[o] = 48 as u8; return o + 1 } 21 var m: i64 = v 22 let t: *u8 = sys_mmap(28) 23 var k: i64 = 0 24 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 25 var i: i64 = 0 26 while i < k { dst[o+i] = t[k-1-i]; i = i + 1 } 27 return o + k 28} 29func eo_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 30 31func main(argc: i64, argv: *i64) -> i64 { 32 if argc < 4 { eo_p("usage: nx_eoe <basename> <depth> <bound>\n" as *u8); return 2 } 33 let base: *u8 = argv[1] as *u8 34 let depth: i64 = eo_atoi(argv[2] as *u8) 35 let bound: i64 = eo_atoi(argv[3] as *u8) 36 37 // build the output path runtime/_hdl_build/<base>.nx 38 let path: *u8 = sys_mmap(512) 39 var po: i64 = 0 40 po = eo_cat(path, po, "runtime/_hdl_build/" as *u8) 41 po = eo_cat(path, po, base) 42 po = eo_cat(path, po, ".nx" as *u8) 43 path[po] = 0 as u8 44 45 // PRETTY-PRINT NishiLang SOURCE from the structural spec (depth nested loops). 46 let buf: *u8 = sys_mmap(K_MAGIC_65536) 47 var o: i64 = 0 48 o = eo_cat(buf, o, "// GENERATED BY nx_eoe (emitter-of-emitters) -- control flow synthesized from a DATA spec, compiled by nx_cc. license_tier: ORIGINAL\n" as *u8) 49 o = eo_cat(buf, o, "import \"nx_syscalls.nx\"\n" as *u8) 50 o = eo_cat(buf, o, "func main() -> i64 {\n" as *u8) 51 o = eo_cat(buf, o, " var acc: i64 = 0\n" as *u8) 52 // interleave each loop var's INIT with its WHILE-open, so inner counters are RESET inside the 53 // outer loop body each iteration (the classic nested-loop correctness point). 54 var d: i64 = 0 55 while d < depth { 56 o = eo_cat(buf, o, " var i" as *u8); o = eo_catn(buf, o, d); o = eo_cat(buf, o, ": i64 = 1\n" as *u8) 57 o = eo_cat(buf, o, " while i" as *u8); o = eo_catn(buf, o, d); o = eo_cat(buf, o, " <= " as *u8); o = eo_catn(buf, o, bound); o = eo_cat(buf, o, " {\n" as *u8) 58 d = d + 1 59 } 60 o = eo_cat(buf, o, " acc = acc + " as *u8) 61 d = 0 62 while d < depth { if d > 0 { o = eo_cat(buf, o, " * " as *u8) } o = eo_cat(buf, o, "i" as *u8); o = eo_catn(buf, o, d); d = d + 1 } 63 o = eo_cat(buf, o, "\n" as *u8) 64 d = depth - 1 65 while d >= 0 { o = eo_cat(buf, o, " i" as *u8); o = eo_catn(buf, o, d); o = eo_cat(buf, o, " = i" as *u8); o = eo_catn(buf, o, d); o = eo_cat(buf, o, " + 1\n }\n" as *u8); d = d - 1 } 66 o = eo_cat(buf, o, " let b: *u8 = sys_mmap(8)\n" as *u8) 67 o = eo_cat(buf, o, " b[0] = (acc & 0xff) as u8\n" as *u8) 68 o = eo_cat(buf, o, " sys_write(1, b, 1)\n" as *u8) 69 o = eo_cat(buf, o, " return 0\n}\n" as *u8) 70 71 let fd: i64 = sys_openat_wr(path, 420) 72 if fd < 0 { eo_p("EOEGEN verdict=RED reason=out-unwritable\n" as *u8); return 1 } 73 sys_write(fd, buf, o) 74 sys_close(fd) 75 eo_p("EOEGEN name=" as *u8); eo_p(path); eo_p(" depth=" as *u8); let bb: *u8 = sys_mmap(8); bb[0]=(48+depth) as u8; sys_write(1, bb, 1); eo_p(" src_bytes=" as *u8); let nb: *u8 = sys_mmap(28); var nn: i64=o; var nk: i64=0; if nn==0{nb[0]=48;nk=1}; while nn>0{nb[nk]=(48+(nn%10)) as u8;nn=nn/10;nk=nk+1}; var ni: i64=0; let rev: *u8=sys_mmap(28); while ni<nk{rev[ni]=nb[nk-1-ni];ni=ni+1}; sys_write(1,rev,nk); eo_p(" verdict=EMITTED\n" as *u8) 76 return 0 77}