code wiki / _hdl_build / nx_ptr_ref.nx

nx_ptr_ref.nx source

↩ module page · 42 lines · 2225 B

1// nx_ptr_ref.nx -- SPEC CHECK: does NishiLang's production compiler nx_cc compute pointer-to-local (&x, *p) as I expect? 2// Reference oracle for whether the nxc RISC-V-backend pointer failure is a real BUG or my expectation is wrong. 3// 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 9// EXACTLY ptr.nx: write through a pointer to a local, read the local back. 10func ptr_local() -> i64 { 11 var x: i64 = 40 12 let p: *i64 = &x 13 *p = *p + 1 14 return x 15} 16// a second pointer test: two locals, pointer swap of values. 17func ptr_swap() -> i64 { 18 var a: i64 = 3 19 var b: i64 = 7 20 let pa: *i64 = &a 21 let pb: *i64 = &b 22 let t: i64 = *pa 23 *pa = *pb 24 *pb = t 25 return a * 10 + b // after swap: a=7,b=3 -> 73 26} 27 28func main() -> i64 { 29 g_puts("nx_ptr_ref (does nx_cc compute pointer-to-local as expected?)\n" as *u8) 30 var pass: i64=0; var total: i64=0 31 let a: i64=ptr_local() 32 g_puts(" ptr_local (x=40; *&x += 1; return x) = "); g_pn(a); g_puts(" (expect 41)\n" as *u8) 33 pass=pass+ck("A: write-through-pointer-to-local == 41 on nx_cc" as *u8, (a==41) as i64); total=total+1 34 let b: i64=ptr_swap() 35 g_puts(" ptr_swap (swap a=3,b=7 via pointers) = "); g_pn(b); g_puts(" (expect 73)\n" as *u8) 36 pass=pass+ck("B: pointer swap of two locals == 73 on nx_cc" as *u8, (b==73) as i64); total=total+1 37 38 var okall: i64=0; if pass==total { okall=1 } 39 g_puts("---- nx_ptr_ref: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8) 40 if okall==1 { g_puts("verdict=GREEN (nx_cc computes pointer-to-local CORRECTLY -> nxc RISC-V-backend pointer failure is a REAL BUG)\n" as *u8); sys_exit(0); return 0 } 41 g_puts("verdict=RED (nx_cc also diverges -> re-examine the pointer semantics/my expectation)\n" as *u8); sys_exit(1); return 1 42}