code wiki / _hdl_build / nx_dispatch_ref.nx
nx_dispatch_ref.nx source
↩ module page · 51 lines · 2671 B
1// nx_dispatch_ref.nx -- SPEC CHECK: does nx_cc compute an ARRAY-of-function-pointers dispatch table as expected?
2// Oracle for whether the nxc result (206, expected 50) is a real bug or an array-of-fn-ptr semantic quirk. expect_exit: 0
3import "nx_syscalls.nx"
4import "nx_g_puts_lib.nx"
5func 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 }
6func 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 }
7
8func op_add(a: i64, b: i64) -> i64 { return a + b }
9func op_sub(a: i64, b: i64) -> i64 { return a - b }
10func op_mul(a: i64, b: i64) -> i64 { return a * b }
11
12func main() -> i64 {
13 g_puts("nx_dispatch_ref (does nx_cc compute an array-of-fn-ptr dispatch table as expected?)\n" as *u8)
14 var pass: i64=0; var total: i64=0
15
16 var tbl: [3]func(i64, i64) -> i64
17 tbl[0] = &op_add
18 tbl[1] = &op_sub
19 tbl[2] = &op_mul
20
21 // element-by-element first (isolates array-of-fn-ptr store/load from the loop)
22 let f0: func(i64, i64) -> i64 = tbl[0]
23 let r0: i64 = f0(10, 3)
24 g_puts(" tbl[0](10,3) = "); g_pn(r0); g_puts(" (expect 13)\n" as *u8)
25 pass=pass+ck("A: tbl[0]=&op_add -> 13" as *u8, (r0==13) as i64); total=total+1
26
27 let f1: func(i64, i64) -> i64 = tbl[1]
28 let r1: i64 = f1(10, 3)
29 g_puts(" tbl[1](10,3) = "); g_pn(r1); g_puts(" (expect 7)\n" as *u8)
30 pass=pass+ck("B: tbl[1]=&op_sub -> 7 (stride check)" as *u8, (r1==7) as i64); total=total+1
31
32 let f2: func(i64, i64) -> i64 = tbl[2]
33 let r2: i64 = f2(10, 3)
34 g_puts(" tbl[2](10,3) = "); g_pn(r2); g_puts(" (expect 30)\n" as *u8)
35 pass=pass+ck("C: tbl[2]=&op_mul -> 30" as *u8, (r2==30) as i64); total=total+1
36
37 var acc: i64 = 0
38 var i: i64 = 0
39 while i < 3 {
40 let f: func(i64, i64) -> i64 = tbl[i]
41 acc = acc + f(10, 3)
42 i = i + 1
43 }
44 g_puts(" loop sum = "); g_pn(acc); g_puts(" (expect 50)\n" as *u8)
45 pass=pass+ck("D: dispatch loop sum == 50" as *u8, (acc==50) as i64); total=total+1
46
47 var okall: i64=0; if pass==total { okall=1 }
48 g_puts("---- nx_dispatch_ref: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8)
49 if okall==1 { g_puts("verdict=GREEN (nx_cc computes dispatch table CORRECTLY -> nxc result 206 is a REAL BUG)\n" as *u8); sys_exit(0); return 0 }
50 g_puts("verdict=RED (nx_cc also diverges -> array-of-fn-ptr is a semantic quirk, re-examine)\n" as *u8); sys_exit(1); return 1
51}