code wiki / _hdl_build / nx_fnptr_ref.nx

nx_fnptr_ref.nx source

↩ module page · 35 lines · 2229 B

1// nx_fnptr_ref.nx -- SPEC CHECK: does NishiLang's production compiler nx_cc compute function-pointer indirect 2// calls (&fn + f(x,y)) as I expect? Reference oracle for whether the nxc RISC-V-backend fn-ptr failure (returns 0) 3// is a real BUG or my expectation/syntax is wrong. expect_exit: 0 4import "nx_syscalls.nx" 5import "nx_g_puts_lib.nx" 6func 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 } 7func 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 } 8 9func add(a: i64, b: i64) -> i64 { return a + b } 10func mul(a: i64, b: i64) -> i64 { return a * b } 11func apply(f: func(i64, i64) -> i64, x: i64, y: i64) -> i64 { return f(x, y) } 12 13func main() -> i64 { 14 g_puts("nx_fnptr_ref (does nx_cc compute fn-ptr indirect calls as expected?)\n" as *u8) 15 var pass: i64=0; var total: i64=0 16 17 let fa: func(i64, i64) -> i64 = &add 18 let fm: func(i64, i64) -> i64 = &mul 19 let r1: i64 = apply(fa, 3, 4) 20 g_puts(" apply(&add,3,4) = "); g_pn(r1); g_puts(" (expect 7)\n" as *u8) 21 pass=pass+ck("A: indirect call &add == 7 on nx_cc" as *u8, (r1==7) as i64); total=total+1 22 23 let r2: i64 = apply(fm, 5, 6) 24 g_puts(" apply(&mul,5,6) = "); g_pn(r2); g_puts(" (expect 30)\n" as *u8) 25 pass=pass+ck("B: indirect call &mul == 30 on nx_cc" as *u8, (r2==30) as i64); total=total+1 26 27 let tot: i64 = r1 + r2 28 g_puts(" sum = "); g_pn(tot); g_puts(" (expect 37)\n" as *u8) 29 pass=pass+ck("C: fn-ptr sum == 37 on nx_cc" as *u8, (tot==37) as i64); total=total+1 30 31 var okall: i64=0; if pass==total { okall=1 } 32 g_puts("---- nx_fnptr_ref: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8) 33 if okall==1 { g_puts("verdict=GREEN (nx_cc computes fn-ptr indirect calls CORRECTLY -> nxc RISC-V-backend fn-ptr failure is a REAL BUG)\n" as *u8); sys_exit(0); return 0 } 34 g_puts("verdict=RED (nx_cc also diverges -> re-examine fn-ptr semantics/my expectation)\n" as *u8); sys_exit(1); return 1 35}