code wiki / _hdl_build / nx_ncf_synth.nx

nx_ncf_synth.nx source

↩ module page · 499 lines · 23083 B

1// nx_ncf_synth.nx -- the SHARED control-flow SYNTHESIS CORE (no main; imported by the rungs + the WIRING). 2// The clean DRY home for X-AUT-NCF-001 synthesis: rungs 1-2 (nx_ncf_synth_gate, nx_ncf_loop_gate) proved the 3// CONSTRUCTS (conditional, bounded loop) self-contained; this is the importable core + the unified entry 4// `ncf_synth` that the router uses to route a NOVEL spec (input->output examples) to synthesis. Grammar: 5// affine ops, a single conditional, a bounded loop. ALL identifiers are ncf_-prefixed so importers that also 6// pull nx_pattern_classify/library never collide. license_tier: ORIGINAL 7import "nx_syscalls.nx" 8const NCF_MAGIC_8192: i64 = 8192 9const NCF_MAGIC_999999: i64 = 999999 10const NCF_MAGIC_888888: i64 = 888888 11 12const NCF_ORGAN: *u8 = "runtime/_hdl_build/ncf_organ.nx" 13const NCF_NAME: *u8 = "ncf_organ" 14const NCF_RESULT: *u8 = "knowledge/status/ncf_out.bin" 15const NCF_RUNNER: *u8 = "_offc/nx_sov_build_run.elf" 16 17func ncf_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func ncf_n(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 } 19func ncf_cat(buf: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){ buf[o]=s[i]; o=o+1; i=i+1 } return o } 20func ncf_catn(buf: *u8, off: i64, v: i64) -> i64 { var o: i64=off; if v==0 { buf[o]=48 as u8; return o+1 } var m: i64=v; if m<0 { buf[o]=45 as u8; o=o+1; m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } var i: i64=0; while i<k { buf[o]=t[k-1-i]; o=o+1; i=i+1 } return o } 21 22// ---- grammar ---- 23func ncf_op_eval(op: i64, arg: i64, x: i64) -> i64 { 24 if op==0 { return x } 25 if op==1 { return 0 - x } 26 if op==2 { return x + arg } 27 if op==3 { return x * arg } 28 if op==4 { return x * 0 } 29 return x 30} 31func ncf_pred_eval(pred: i64, k: i64, x: i64) -> i64 { 32 if pred==0 { if x<0 { return 1 } return 0 } 33 if pred==1 { if x<k { return 1 } return 0 } 34 if pred==2 { if x>k { return 1 } return 0 } 35 return 0 36} 37func ncf_loop_eval(init: i64, bop: i64, c: i64, x: i64) -> i64 { 38 var acc: i64=init; var i: i64=1 39 while i<=x { 40 if bop==0 { acc=acc+c } 41 if bop==1 { acc=acc*c } 42 if bop==2 { acc=acc+i } 43 if bop==3 { acc=acc*i } 44 i=i+1 45 } 46 return acc 47} 48 49// affine-index loop body: acc = acc + (c1*i + c0) -- covers x^2 and the quadratic/arithmetic-series family 50func ncf_loop_eval2(init: i64, c1: i64, c0: i64, x: i64) -> i64 { 51 var acc: i64=init; var i: i64=1 52 while i<=x { acc=acc+((c1*i)+c0); i=i+1 } 53 return acc 54} 55 56// quadratic-index loop body: acc = acc + (c2*i*i + c1*i + c0) -- covers x^3 and the cubic family 57func ncf_loop_eval3(init: i64, c2: i64, c1: i64, c0: i64, x: i64) -> i64 { 58 var acc: i64=init; var i: i64=1 59 while i<=x { acc=acc+(((c2*i)*i)+(c1*i)+c0); i=i+1 } 60 return acc 61} 62 63// TWO-STATE linear recurrence: state (a,b); each step (a,b) <- (paa*a+pab*b, pba*a+pbb*b); return a. 64// The first construct with MULTIPLE accumulators -- covers Fibonacci/Lucas/Pell (order-2 recurrences) that 65// NO single-accumulator construct (rungs 1-4) can express. This is small-program synthesis, not curve-fitting. 66func ncf_loop_eval4(a0: i64, b0: i64, paa: i64, pab: i64, pba: i64, pbb: i64, x: i64) -> i64 { 67 var a: i64=a0; var b: i64=b0; var i: i64=1 68 while i<=x { let na: i64=(paa*a)+(pab*b); let nb: i64=(pba*a)+(pbb*b); a=na; b=nb; i=i+1 } 69 return a 70} 71 72// GENERALIZED k-order linear recurrence (companion / shift-register form): a(n) = sum_t c[t]*a(n-k+t), with 73// the k-element window initialized from the spec's first k outputs. ONE construct, parameterized by order k -- 74// it SUBSUMES the two-state rung (k=2 = Fibonacci) and covers ANY order (k=3 Tribonacci, k=4 Tetranacci, ...) 75// without a hand-authored rung per order. form = [k, c0..c_{k-1}, v0..v_{k-1}]. (Assumes xs = 0,1,..,n-1.) 76func ncf_pow3(k: i64) -> i64 { var r: i64=1; var t: i64=0; while t<k { r=r*3; t=t+1 } return r } 77func ncf_recur_eval(form: *i64, x: i64) -> i64 { 78 let k: i64=form[0] 79 if x<k { return form[1+k+x] } 80 let w: *i64=sys_mmap(128) as *i64 81 var j: i64=0; while j<k { w[j]=form[1+k+j]; j=j+1 } 82 var nn: i64=k 83 while nn<=x { 84 var nv: i64=0; var t: i64=0 85 while t<k { nv=nv+(form[1+t]*w[t]); t=t+1 } 86 var s: i64=0; while s<k-1 { w[s]=w[s+1]; s=s+1 } 87 w[k-1]=nv; nn=nn+1 88 } 89 return w[k-1] 90} 91 92// ---- searchers ---- 93func ncf_search_linear(xs: *i64, ys: *i64, n: i64) -> i64 { 94 var op1: i64=0 95 while op1<5 { 96 var a1: i64=0-4 97 while a1<=4 { 98 var ok: i64=1; var i: i64=0 99 while i<n { if ncf_op_eval(op1,a1,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 100 if ok==1 { return 1 } 101 a1=a1+1 102 } 103 op1=op1+1 104 } 105 var p: i64=0 106 while p<5 { 107 var qq: i64=0 108 while qq<5 { 109 var a: i64=0-3 110 while a<=3 { 111 var b: i64=0-3 112 while b<=3 { 113 var ok2: i64=1; var j: i64=0 114 while j<n { let r1: i64=ncf_op_eval(p,a,xs[j]); if ncf_op_eval(qq,b,r1)!=ys[j] { ok2=0; j=n } else { j=j+1 } } 115 if ok2==1 { return 1 } 116 b=b+1 117 } 118 a=a+1 119 } 120 qq=qq+1 121 } 122 p=p+1 123 } 124 return 0 125} 126func ncf_search_branch(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 127 var pred: i64=0 128 while pred<3 { 129 var k: i64=0-3 130 while k<=3 { 131 var top: i64=0 132 while top<5 { 133 var ta: i64=0-3 134 while ta<=3 { 135 var eop: i64=0 136 while eop<5 { 137 var ea: i64=0-3 138 while ea<=3 { 139 var ok: i64=1; var i: i64=0 140 while i<n { 141 var r: i64=0 142 if ncf_pred_eval(pred,k,xs[i])==1 { r=ncf_op_eval(top,ta,xs[i]) } else { r=ncf_op_eval(eop,ea,xs[i]) } 143 if r!=ys[i] { ok=0; i=n } else { i=i+1 } 144 } 145 if ok==1 { form[0]=pred; form[1]=k; form[2]=top; form[3]=ta; form[4]=eop; form[5]=ea; return 1 } 146 ea=ea+1 147 } 148 eop=eop+1 149 } 150 ta=ta+1 151 } 152 top=top+1 153 } 154 k=k+1 155 } 156 pred=pred+1 157 } 158 return 0 159} 160func ncf_search_loop(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 161 var init: i64=0 162 while init<3 { 163 var bop: i64=0 164 while bop<4 { 165 var c: i64=0-2 166 while c<=3 { 167 var ok: i64=1; var i: i64=0 168 while i<n { if ncf_loop_eval(init,bop,c,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 169 if ok==1 { form[0]=init; form[1]=bop; form[2]=c; return 1 } 170 c=c+1 171 } 172 bop=bop+1 173 } 174 init=init+1 175 } 176 return 0 177} 178 179// search the affine-index loop: form=[init,c1,c0]; covers x^2 etc. (the construct rung-2 simple loop cannot) 180func ncf_search_loop2(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 181 var init: i64=0-2 182 while init<=4 { 183 var c1: i64=0-3 184 while c1<=4 { 185 var c0: i64=0-3 186 while c0<=3 { 187 var ok: i64=1; var i: i64=0 188 while i<n { if ncf_loop_eval2(init,c1,c0,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 189 if ok==1 { form[0]=init; form[1]=c1; form[2]=c0; return 1 } 190 c0=c0+1 191 } 192 c1=c1+1 193 } 194 init=init+1 195 } 196 return 0 197} 198 199// search the quadratic-index loop: form=[init,c2,c1,c0]; covers x^3 (the affine-index loop cannot -- it is quadratic-in-x) 200func ncf_search_loop3(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 201 var init: i64=0-2 202 while init<=3 { 203 var c2: i64=0-2 204 while c2<=4 { 205 var c1: i64=0-4 206 while c1<=3 { 207 var c0: i64=0-2 208 while c0<=3 { 209 var ok: i64=1; var i: i64=0 210 while i<n { if ncf_loop_eval3(init,c2,c1,c0,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 211 if ok==1 { form[0]=init; form[1]=c2; form[2]=c1; form[3]=c0; return 1 } 212 c0=c0+1 213 } 214 c1=c1+1 215 } 216 c2=c2+1 217 } 218 init=init+1 219 } 220 return 0 221} 222 223// search the two-state recurrence: form=[a0,b0,paa,pab,pba,pbb]; covers order-2 recurrences (Fibonacci etc.) 224// that single-accumulator loops cannot. Coefficients in {0,1,2}, inits in {0,1,2} -- the classic family. 225func ncf_search_loop4(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 226 var a0: i64=0 227 while a0<=2 { 228 var b0: i64=0 229 while b0<=2 { 230 var paa: i64=0 231 while paa<=2 { 232 var pab: i64=0 233 while pab<=2 { 234 var pba: i64=0 235 while pba<=2 { 236 var pbb: i64=0 237 while pbb<=2 { 238 var ok: i64=1; var i: i64=0 239 while i<n { if ncf_loop_eval4(a0,b0,paa,pab,pba,pbb,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 240 if ok==1 { form[0]=a0; form[1]=b0; form[2]=paa; form[3]=pab; form[4]=pba; form[5]=pbb; return 1 } 241 pbb=pbb+1 242 } 243 pba=pba+1 244 } 245 pab=pab+1 246 } 247 paa=paa+1 248 } 249 b0=b0+1 250 } 251 a0=a0+1 252 } 253 return 0 254} 255 256// search the generalized recurrence: try orders k=2..4 (smallest first = minimal order); init window = first k 257// outputs (read, not searched); odometer over constant coeffs in {0,1,2}. Covers Fibonacci/Lucas/Pell/Tribonacci/ 258// Tetranacci -- the whole constant-coefficient linear-recurrence family from ONE construct. 259func ncf_search_recur(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 260 var k: i64=2 261 while k<=4 { 262 if n>k { 263 let combos: i64=ncf_pow3(k) 264 var combo: i64=0 265 while combo<combos { 266 form[0]=k 267 var tmp: i64=combo; var t: i64=0 268 while t<k { form[1+t]=tmp%3; tmp=tmp/3; t=t+1 } 269 var j: i64=0; while j<k { form[1+k+j]=ys[j]; j=j+1 } 270 var ok: i64=1; var i: i64=0 271 while i<n { if ncf_recur_eval(form,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 272 if ok==1 { return 1 } 273 combo=combo+1 274 } 275 } 276 k=k+1 277 } 278 return 0 279} 280 281// UNIFIED ENTRY: try conditional, then loop; returns 0=UNHANDLED(stays NOVEL->tutor, no fake), 1=branch, 2=loop. 282func ncf_synth(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 283 if ncf_search_branch(xs,ys,n,form)==1 { return 1 } 284 if ncf_search_loop(xs,ys,n,form)==1 { return 2 } 285 if ncf_search_loop2(xs,ys,n,form)==1 { return 3 } 286 if ncf_search_loop3(xs,ys,n,form)==1 { return 4 } 287 if ncf_search_loop4(xs,ys,n,form)==1 { return 5 } 288 if ncf_search_recur(xs,ys,n,form)==1 { return 6 } 289 return 0 290} 291 292// evaluate a synthesized FORM at x (the organ's logic, in-process) -- dispatches by construct kind. Lets a caller 293// verify held-out generalization without a god-build (ncf_emit faithfully emits this form, proven in the gates). 294func ncf_eval(kind: i64, form: *i64, x: i64) -> i64 { 295 if kind==1 { if ncf_pred_eval(form[0],form[1],x)==1 { return ncf_op_eval(form[2],form[3],x) } return ncf_op_eval(form[4],form[5],x) } 296 if kind==2 { return ncf_loop_eval(form[0],form[1],form[2],x) } 297 if kind==3 { return ncf_loop_eval2(form[0],form[1],form[2],x) } 298 if kind==4 { return ncf_loop_eval3(form[0],form[1],form[2],form[3],x) } 299 if kind==5 { return ncf_loop_eval4(form[0],form[1],form[2],form[3],form[4],form[5],x) } 300 if kind==6 { return ncf_recur_eval(form,x) } 301 return 0 302} 303 304// ---- materialize ---- 305func ncf_cat_ret(buf: *u8, off: i64, op: i64, arg: i64, ind: *u8) -> i64 { 306 var o: i64=off 307 o=ncf_cat(buf,o,ind); o=ncf_cat(buf,o,"return " as *u8) 308 if op==0 { o=ncf_cat(buf,o,"x" as *u8) } 309 if op==1 { o=ncf_cat(buf,o,"0 - x" as *u8) } 310 if op==2 { o=ncf_cat(buf,o,"x + " as *u8); o=ncf_catn(buf,o,arg) } 311 if op==3 { o=ncf_cat(buf,o,"x * " as *u8); o=ncf_catn(buf,o,arg) } 312 if op==4 { o=ncf_cat(buf,o,"x * 0" as *u8) } 313 o=ncf_cat(buf,o,"\n" as *u8) 314 return o 315} 316func ncf_cat_pred(buf: *u8, off: i64, pred: i64, k: i64) -> i64 { 317 var o: i64=off 318 if pred==0 { o=ncf_cat(buf,o,"x < 0" as *u8) } 319 if pred==1 { o=ncf_cat(buf,o,"x < " as *u8); o=ncf_catn(buf,o,k) } 320 if pred==2 { o=ncf_cat(buf,o,"x > " as *u8); o=ncf_catn(buf,o,k) } 321 return o 322} 323func ncf_cat_body(buf: *u8, off: i64, bop: i64, c: i64) -> i64 { 324 var o: i64=off 325 if bop==0 { o=ncf_cat(buf,o," acc = acc + " as *u8); o=ncf_catn(buf,o,c) } 326 if bop==1 { o=ncf_cat(buf,o," acc = acc * " as *u8); o=ncf_catn(buf,o,c) } 327 if bop==2 { o=ncf_cat(buf,o," acc = acc + i" as *u8) } 328 if bop==3 { o=ncf_cat(buf,o," acc = acc * i" as *u8) } 329 o=ncf_cat(buf,o,"\n" as *u8) 330 return o 331} 332// emit a branch organ; held-out main computes (f(ha)*scale)+f(hb) -> NCF_RESULT. ha emitted as 0-|ha| if <0. 333// affine-index loop body source: acc = acc + ((c1 * i) +/- c0) 334func ncf_cat_body2(buf: *u8, off: i64, c1: i64, c0: i64) -> i64 { 335 var o: i64=off 336 o=ncf_cat(buf,o," acc = acc + ((" as *u8); o=ncf_catn(buf,o,c1); o=ncf_cat(buf,o," * i)" as *u8) 337 if c0<0 { o=ncf_cat(buf,o," - " as *u8); o=ncf_catn(buf,o,0-c0) } else { o=ncf_cat(buf,o," + " as *u8); o=ncf_catn(buf,o,c0) } 338 o=ncf_cat(buf,o,")\n" as *u8) 339 return o 340} 341// quadratic-index loop body source: acc = acc (+/-)(c2 * i * i) (+/-)(c1 * i) (+/-) c0 with explicit signs 342func ncf_cat_body3(buf: *u8, off: i64, c2: i64, c1: i64, c0: i64) -> i64 { 343 var o: i64=off 344 o=ncf_cat(buf,o," acc = acc" as *u8) 345 if c2<0 { o=ncf_cat(buf,o," - ((" as *u8); o=ncf_catn(buf,o,0-c2); o=ncf_cat(buf,o," * i) * i)" as *u8) } else { o=ncf_cat(buf,o," + ((" as *u8); o=ncf_catn(buf,o,c2); o=ncf_cat(buf,o," * i) * i)" as *u8) } 346 if c1<0 { o=ncf_cat(buf,o," - (" as *u8); o=ncf_catn(buf,o,0-c1); o=ncf_cat(buf,o," * i)" as *u8) } else { o=ncf_cat(buf,o," + (" as *u8); o=ncf_catn(buf,o,c1); o=ncf_cat(buf,o," * i)" as *u8) } 347 if c0<0 { o=ncf_cat(buf,o," - " as *u8); o=ncf_catn(buf,o,0-c0) } else { o=ncf_cat(buf,o," + " as *u8); o=ncf_catn(buf,o,c0) } 348 o=ncf_cat(buf,o,"\n" as *u8) 349 return o 350} 351func ncf_cat_arg(buf: *u8, off: i64, v: i64) -> i64 { 352 var o: i64=off 353 if v<0 { o=ncf_cat(buf,o,"0 - " as *u8); o=ncf_catn(buf,o,0-v) } else { o=ncf_catn(buf,o,v) } 354 return o 355} 356func ncf_emit_main(buf: *u8, off: i64, ha: i64, hb: i64, scale: i64) -> i64 { 357 var o: i64=off 358 o=ncf_cat(buf,o,"func main() -> i64 {\n let p: *i64 = sys_mmap(8) as *i64\n p[0] = (f(" as *u8) 359 o=ncf_cat_arg(buf,o,ha) 360 o=ncf_cat(buf,o,") * " as *u8); o=ncf_catn(buf,o,scale); o=ncf_cat(buf,o,") + f(" as *u8) 361 o=ncf_cat_arg(buf,o,hb) 362 o=ncf_cat(buf,o,")\n let fd: i64 = sys_openat_wr(\"knowledge/status/ncf_out.bin\" as *u8, 420)\n if fd >= 0 { sys_write(fd, p as *u8, 8); sys_close(fd) }\n return 0\n}\n" as *u8) 363 return o 364} 365func ncf_emit_branch(form: *i64, ha: i64, hb: i64, scale: i64) -> i64 { 366 let buf: *u8=sys_mmap(NCF_MAGIC_8192); var o: i64=0 367 o=ncf_cat(buf,o,"// SYNTHESIZED CONTROL FLOW (a branch) + GOD-BUILT via nx_ncf_synth. license_tier: ORIGINAL\n" as *u8) 368 o=ncf_cat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n if " as *u8) 369 o=ncf_cat_pred(buf,o,form[0],form[1]) 370 o=ncf_cat(buf,o," {\n" as *u8) 371 o=ncf_cat_ret(buf,o,form[2],form[3]," " as *u8) 372 o=ncf_cat(buf,o," }\n" as *u8) 373 o=ncf_cat_ret(buf,o,form[4],form[5]," " as *u8) 374 o=ncf_cat(buf,o,"}\n" as *u8) 375 o=ncf_emit_main(buf,o,ha,hb,scale) 376 let fd: i64=sys_openat_wr(NCF_ORGAN,420) 377 if fd<0 { return 0-1 } 378 sys_write(fd,buf,o); sys_close(fd) 379 return 0 380} 381func ncf_emit_loop(form: *i64, ha: i64, hb: i64, scale: i64) -> i64 { 382 let buf: *u8=sys_mmap(NCF_MAGIC_8192); var o: i64=0 383 o=ncf_cat(buf,o,"// SYNTHESIZED CONTROL FLOW (a bounded loop) + GOD-BUILT via nx_ncf_synth. license_tier: ORIGINAL\n" as *u8) 384 o=ncf_cat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n var acc: i64 = " as *u8) 385 o=ncf_catn(buf,o,form[0]) 386 o=ncf_cat(buf,o,"\n var i: i64 = 1\n while i <= x {\n" as *u8) 387 o=ncf_cat_body(buf,o,form[1],form[2]) 388 o=ncf_cat(buf,o," i = i + 1\n }\n return acc\n}\n" as *u8) 389 o=ncf_emit_main(buf,o,ha,hb,scale) 390 let fd: i64=sys_openat_wr(NCF_ORGAN,420) 391 if fd<0 { return 0-1 } 392 sys_write(fd,buf,o); sys_close(fd) 393 return 0 394} 395func ncf_emit_loop2(form: *i64, ha: i64, hb: i64, scale: i64) -> i64 { 396 let buf: *u8=sys_mmap(NCF_MAGIC_8192); var o: i64=0 397 o=ncf_cat(buf,o,"// SYNTHESIZED CONTROL FLOW (affine-index loop) + GOD-BUILT via nx_ncf_synth. license_tier: ORIGINAL\n" as *u8) 398 o=ncf_cat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n var acc: i64 = " as *u8) 399 o=ncf_catn(buf,o,form[0]) 400 o=ncf_cat(buf,o,"\n var i: i64 = 1\n while i <= x {\n" as *u8) 401 o=ncf_cat_body2(buf,o,form[1],form[2]) 402 o=ncf_cat(buf,o," i = i + 1\n }\n return acc\n}\n" as *u8) 403 o=ncf_emit_main(buf,o,ha,hb,scale) 404 let fd: i64=sys_openat_wr(NCF_ORGAN,420) 405 if fd<0 { return 0-1 } 406 sys_write(fd,buf,o); sys_close(fd) 407 return 0 408} 409func ncf_emit_loop3(form: *i64, ha: i64, hb: i64, scale: i64) -> i64 { 410 let buf: *u8=sys_mmap(NCF_MAGIC_8192); var o: i64=0 411 o=ncf_cat(buf,o,"// SYNTHESIZED CONTROL FLOW (quadratic-index loop) + GOD-BUILT via nx_ncf_synth. license_tier: ORIGINAL\n" as *u8) 412 o=ncf_cat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n var acc: i64 = " as *u8) 413 o=ncf_catn(buf,o,form[0]) 414 o=ncf_cat(buf,o,"\n var i: i64 = 1\n while i <= x {\n" as *u8) 415 o=ncf_cat_body3(buf,o,form[1],form[2],form[3]) 416 o=ncf_cat(buf,o," i = i + 1\n }\n return acc\n}\n" as *u8) 417 o=ncf_emit_main(buf,o,ha,hb,scale) 418 let fd: i64=sys_openat_wr(NCF_ORGAN,420) 419 if fd<0 { return 0-1 } 420 sys_write(fd,buf,o); sys_close(fd) 421 return 0 422} 423// materialize the two-state recurrence: two registers a,b with a parallel update via temps. 424func ncf_emit_loop4(form: *i64, ha: i64, hb: i64, scale: i64) -> i64 { 425 let buf: *u8=sys_mmap(NCF_MAGIC_8192); var o: i64=0 426 o=ncf_cat(buf,o,"// SYNTHESIZED CONTROL FLOW (two-state linear recurrence) + GOD-BUILT via nx_ncf_synth. license_tier: ORIGINAL\n" as *u8) 427 o=ncf_cat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n var a: i64 = " as *u8) 428 o=ncf_catn(buf,o,form[0]) 429 o=ncf_cat(buf,o,"\n var b: i64 = " as *u8) 430 o=ncf_catn(buf,o,form[1]) 431 o=ncf_cat(buf,o,"\n var i: i64 = 1\n while i <= x {\n let na: i64 = (" as *u8) 432 o=ncf_catn(buf,o,form[2]); o=ncf_cat(buf,o," * a) + (" as *u8); o=ncf_catn(buf,o,form[3]); o=ncf_cat(buf,o," * b)\n let nb: i64 = (" as *u8) 433 o=ncf_catn(buf,o,form[4]); o=ncf_cat(buf,o," * a) + (" as *u8); o=ncf_catn(buf,o,form[5]); o=ncf_cat(buf,o," * b)\n a = na\n b = nb\n i = i + 1\n }\n return a\n}\n" as *u8) 434 o=ncf_emit_main(buf,o,ha,hb,scale) 435 let fd: i64=sys_openat_wr(NCF_ORGAN,420) 436 if fd<0 { return 0-1 } 437 sys_write(fd,buf,o); sys_close(fd) 438 return 0 439} 440// materialize the order-k recurrence: a k-element shift-register window, linear update, base cases for x<k. 441func ncf_emit_recur(form: *i64, ha: i64, hb: i64, scale: i64) -> i64 { 442 let k: i64=form[0] 443 let buf: *u8=sys_mmap(NCF_MAGIC_8192); var o: i64=0 444 o=ncf_cat(buf,o,"// SYNTHESIZED CONTROL FLOW (order-k linear recurrence) + GOD-BUILT via nx_ncf_synth. license_tier: ORIGINAL\n" as *u8) 445 o=ncf_cat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n let w: *i64 = sys_mmap(128) as *i64\n" as *u8) 446 var j: i64=0 447 while j<k { o=ncf_cat(buf,o," w[" as *u8); o=ncf_catn(buf,o,j); o=ncf_cat(buf,o,"] = " as *u8); o=ncf_catn(buf,o,form[1+k+j]); o=ncf_cat(buf,o,"\n" as *u8); j=j+1 } 448 o=ncf_cat(buf,o," if x < " as *u8); o=ncf_catn(buf,o,k); o=ncf_cat(buf,o," { return w[x] }\n" as *u8) 449 o=ncf_cat(buf,o," var nn: i64 = " as *u8); o=ncf_catn(buf,o,k); o=ncf_cat(buf,o,"\n while nn <= x {\n let nv: i64 = " as *u8) 450 var t: i64=0 451 while t<k { 452 o=ncf_cat(buf,o,"(" as *u8); o=ncf_catn(buf,o,form[1+t]); o=ncf_cat(buf,o," * w[" as *u8); o=ncf_catn(buf,o,t); o=ncf_cat(buf,o,"])" as *u8) 453 if t<k-1 { o=ncf_cat(buf,o," + " as *u8) } 454 t=t+1 455 } 456 o=ncf_cat(buf,o,"\n var s: i64 = 0\n while s < " as *u8); o=ncf_catn(buf,o,k-1); o=ncf_cat(buf,o," { w[s] = w[s+1]; s = s + 1 }\n" as *u8) 457 o=ncf_cat(buf,o," w[" as *u8); o=ncf_catn(buf,o,k-1); o=ncf_cat(buf,o,"] = nv\n nn = nn + 1\n }\n return w[" as *u8); o=ncf_catn(buf,o,k-1); o=ncf_cat(buf,o,"]\n}\n" as *u8) 458 o=ncf_emit_main(buf,o,ha,hb,scale) 459 let fd: i64=sys_openat_wr(NCF_ORGAN,420) 460 if fd<0 { return 0-1 } 461 sys_write(fd,buf,o); sys_close(fd) 462 return 0 463} 464// emit whichever construct was synthesized (kind from ncf_synth) 465func ncf_emit(kind: i64, form: *i64, ha: i64, hb: i64, scale: i64) -> i64 { 466 if kind==1 { return ncf_emit_branch(form,ha,hb,scale) } 467 if kind==2 { return ncf_emit_loop(form,ha,hb,scale) } 468 if kind==3 { return ncf_emit_loop2(form,ha,hb,scale) } 469 if kind==4 { return ncf_emit_loop3(form,ha,hb,scale) } 470 if kind==5 { return ncf_emit_loop4(form,ha,hb,scale) } 471 if kind==6 { return ncf_emit_recur(form,ha,hb,scale) } 472 return 0-1 473} 474 475// ---- GOD-BUILD + read ---- 476func ncf_god_build(name: *u8) -> i64 { 477 let pid: i64=sys_fork() 478 if pid==0 { 479 let dn: i64=sys_openat_wr("/dev/null\x00" as *u8,420) 480 if dn>=0 { sys_dup3(dn,1,0); sys_dup3(dn,2,0) } 481 let argv: *i64=sys_mmap(64) as *i64 482 argv[0]=NCF_RUNNER as i64; argv[1]=name as i64; argv[2]=0 483 let envp: *i64=sys_mmap(16) as *i64 484 envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0 485 sys_execve(NCF_RUNNER,argv,envp) 486 sys_exit(127) 487 } 488 let st: *i64=sys_mmap(16) as *i64 489 sys_wait4(pid,st,0) 490 return (st[0]>>8)&0xff 491} 492func ncf_read(path: *u8) -> i64 { 493 let lenp: *i64=sys_mmap(16) as *i64 494 let buf: *u8=sys_read_file(path,lenp) 495 if (buf as i64)==0 { return 0-NCF_MAGIC_999999 } 496 if lenp[0]<8 { return 0-NCF_MAGIC_888888 } 497 let p: *i64=buf as *i64 498 return p[0] 499}