code wiki / _hdl_build / nx_nested_loop_ref.nx

nx_nested_loop_ref.nx source

↩ module page · 74 lines · 3655 B

1// nx_nested_loop_ref.nx -- DIFFERENTIAL SPEC CHECK: does the LANGUAGE's PRODUCTION compiler (nx_cc, which builds every 2// organ) compute nested while-loops the way I expect? This is the reference oracle for "is the nxc nested-loop failure 3// a BUG or a DESIGN CHOICE". Runs the EXACT nested-loop programs nxc miscompiled, on nx_cc (this very build path), and 4// prints the results. If nx_cc gives 25/25, nested loops ARE supported+correct in NishiLang -> nxc has a real bug. If 5// nx_cc disagrees, it's a design/semantics difference and my expectation was wrong. expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_g_puts_lib.nx" 8 9func g_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var x: i64=v; if x==0{b[0]=48;sys_write(1,b,1);return 0} var d: i64=0; var y: i64=x; while y>0{d=d+1;y=y/10} var i: i64=d-1; y=x; while i>=0{b[i]=(48+(y%10)) as u8;y=y/10;i=i-1} sys_write(1,b,d); return 0 } 10func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c } 11 12// variant A: EXACTLY nested.nx -- inner `var j` declared inside the outer loop body. 13func nested_inner_var() -> i64 { 14 var acc: i64 = 0 15 var i: i64 = 0 16 while i < 5 { 17 var j: i64 = 0 18 while j < 5 { 19 acc = acc + 1 20 j = j + 1 21 } 22 i = i + 1 23 } 24 return acc 25} 26// variant B: EXACTLY nested2.nx -- j at function scope, reset each outer iteration. 27func nested_func_var() -> i64 { 28 var acc: i64 = 0 29 var i: i64 = 0 30 var j: i64 = 0 31 while i < 5 { 32 j = 0 33 while j < 5 { 34 acc = acc + 1 35 j = j + 1 36 } 37 i = i + 1 38 } 39 return acc 40} 41// variant C: nested SUM (not just count) -- triangular over the pair, catches loop-carried acc bugs harder. 42func nested_sum() -> i64 { 43 var acc: i64 = 0 44 var i: i64 = 1 45 while i <= 4 { 46 var j: i64 = 1 47 while j <= i { 48 acc = acc + i * j 49 j = j + 1 50 } 51 i = i + 1 52 } 53 return acc 54} 55 56func main() -> i64 { 57 g_puts("nx_nested_loop_ref (does NishiLang's PRODUCTION compiler nx_cc compute nested loops as expected?)\n" as *u8) 58 var pass: i64=0; var total: i64=0 59 let a: i64=nested_inner_var() 60 g_puts(" nested_inner_var (5x5 count, inner `var j`) = "); g_pn(a); g_puts(" (expect 25)\n" as *u8) 61 pass=pass+ck("A: inner-var nested loop == 25 on nx_cc (the exact program nxc miscompiled)" as *u8, (a==25) as i64); total=total+1 62 let b: i64=nested_func_var() 63 g_puts(" nested_func_var (5x5 count, func-scope j) = "); g_pn(b); g_puts(" (expect 25)\n" as *u8) 64 pass=pass+ck("B: func-scope nested loop == 25 on nx_cc" as *u8, (b==25) as i64); total=total+1 65 // nested_sum: sum_{i=1..4} sum_{j=1..i} i*j = i*(i(i+1)/2): 1*1 + 2*3 + 3*6 + 4*10 = 1+6+18+40 = 65 66 let c: i64=nested_sum() 67 g_puts(" nested_sum (sum i*j) = "); g_pn(c); g_puts(" (expect 65)\n" as *u8) 68 pass=pass+ck("C: nested loop-carried arithmetic == 65 on nx_cc" as *u8, (c==65) as i64); total=total+1 69 70 var okall: i64=0; if pass==total { okall=1 } 71 g_puts("---- nx_nested_loop_ref: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8) 72 if okall==1 { g_puts("verdict=GREEN (nx_cc, the PRODUCTION NishiLang compiler, computes nested loops CORRECTLY -> nested loops ARE supported+correct in the language -> the nxc RISC-V-backend failure is a REAL BUG, not a design choice)\n" as *u8); sys_exit(0); return 0 } 73 g_puts("verdict=RED (nx_cc ALSO diverges -> the difference is language-level, re-examine the semantics before calling nxc buggy)\n" as *u8); sys_exit(1); return 1 74}