code wiki / _hdl_build / nx_riscv_codegen.nx
nx_riscv_codegen.nx source
↩ module page · 66 lines · 4433 B
1// nx_riscv_codegen.nx -- SOVEREIGN RISC-V backend, RUNG 4: IR -> RISC-V CODEGEN. Now on the GENERAL nx_riscv_lib
2// (shared encoders, lower w/ regalloc+instr-sel, build_header, emulator) -- only the qmul IR + tests are local.
3// Lowers qmul(2.0,3.0)=(a*b)>>16 then >>16 (integer part), runs it; retargets when the IR input changes.
4// T1 lowers to 8 words ending in ecall. T2 run -> exit 6 (2.0*3.0 int part). T3 (teeth) input 2.0->4.0 -> exit 12.
5// expect_exit: 0 Sovereign: nx_riscv_lib (+ nx_syscalls).
6import "nx_riscv_lib.nx"
7import "nx_syscalls.nx"
8const K_MAGIC_196608: i64 = 196608
9const K_MAGIC_131072: i64 = 131072
10const K_MAGIC_262144: i64 = 262144
11
12func gen(irimm0: i64) -> i64 { // build the qmul IR with input-a const = irimm0, lower + run, return exit
13 let BASE: i64=0x10000; let HDR: i64=120; let ENTRY: i64=0x10078
14 let irop: *i64=sys_mmap(8*8) as *i64; let irdst: *i64=sys_mmap(8*8) as *i64; let irs1: *i64=sys_mmap(8*8) as *i64
15 let irs2: *i64=sys_mmap(8*8) as *i64; let irimm: *i64=sys_mmap(8*8) as *i64; let labelpos: *i64=sys_mmap(8*8) as *i64
16 let preg: *i64=sys_mmap(8*8) as *i64; preg[0]=5; preg[1]=6; preg[2]=7; preg[3]=28; preg[4]=29
17 irop[0]=0; irdst[0]=0; irimm[0]=irimm0 // CONST a
18 irop[1]=0; irdst[1]=1; irimm[1]=K_MAGIC_196608 // CONST 3.0
19 irop[2]=1; irdst[2]=2; irs1[2]=0; irs2[2]=1 // MUL
20 irop[3]=4; irdst[3]=3; irs1[3]=2; irimm[3]=16 // SHR 16 (Q16 result)
21 irop[4]=4; irdst[4]=4; irs1[4]=3; irimm[4]=16 // SHR 16 (integer part)
22 irop[5]=5; irs1[5]=4 // RET
23 let m: *u8=sys_mmap(0x20000)
24 let nw: i64=lower(m, HDR, irop, irdst, irs1, irs2, irimm, 6, preg, labelpos)
25 build_header(m, HDR+nw*4, ENTRY, BASE)
26 let fd: i64=sys_openat_wr("knowledge/research/riscv_qmul.elf" as *u8, 0x1ed); if fd>=0 { sys_write(fd,m,HDR+nw*4); sys_close(fd) }
27 return run(m, ENTRY, BASE)
28}
29
30func main() -> i64 {
31 g_puts("nx_riscv_codegen (RUNG 4 IR->RISC-V codegen; now on the GENERAL nx_riscv_lib)\n" as *u8)
32 var pass: i64=0; var total: i64=0
33 let BASE: i64=0x10000; let HDR: i64=120; let ENTRY: i64=0x10078
34 // lower once to inspect the stream
35 let irop: *i64=sys_mmap(8*8) as *i64; let irdst: *i64=sys_mmap(8*8) as *i64; let irs1: *i64=sys_mmap(8*8) as *i64
36 let irs2: *i64=sys_mmap(8*8) as *i64; let irimm: *i64=sys_mmap(8*8) as *i64; let labelpos: *i64=sys_mmap(8*8) as *i64
37 let preg: *i64=sys_mmap(8*8) as *i64; preg[0]=5; preg[1]=6; preg[2]=7; preg[3]=28; preg[4]=29
38 irop[0]=0; irdst[0]=0; irimm[0]=K_MAGIC_131072; irop[1]=0; irdst[1]=1; irimm[1]=K_MAGIC_196608
39 irop[2]=1; irdst[2]=2; irs1[2]=0; irs2[2]=1; irop[3]=4; irdst[3]=3; irs1[3]=2; irimm[3]=16
40 irop[4]=4; irdst[4]=4; irs1[4]=3; irimm[4]=16; irop[5]=5; irs1[5]=4
41 let m: *u8=sys_mmap(0x20000)
42 let nw: i64=lower(m, HDR, irop, irdst, irs1, irs2, irimm, 6, preg, labelpos)
43 g_puts(" lowered IR -> "); g_pn(nw); g_puts(" RISC-V words\n" as *u8)
44 let lastw: i64=(m[HDR+(nw-1)*4] as i64)&127
45 var t1: i64=0; if nw==8 { if lastw==0x73 { t1=1 } }
46 pass=pass+ck("T1: codegen lowered the IR to 8 RISC-V words ending in ecall" as *u8, t1); total=total+1
47
48 let r1: i64=gen(K_MAGIC_131072)
49 g_puts(" RAN lowered qmul(2.0,3.0) int part -> exit = "); g_pn(r1); g_puts(" (expect 6)\n" as *u8)
50 var t2: i64=0; if r1==6 { t2=1 }
51 pass=pass+ck("T2: a Q16 qmul LOWERED FROM IR ran on RISC-V -> exit==6 (2.0*3.0)" as *u8, t2); total=total+1
52
53 let r2: i64=gen(K_MAGIC_262144)
54 g_puts(" RE-LOWERED with input 4.0 -> exit = "); g_pn(r2); g_puts(" (expect 12)\n" as *u8)
55 var t3: i64=0; if r2==12 { t3=1 }
56 pass=pass+ck("T3 (teeth): changed IR input 2.0->4.0 -> exit==12 (codegen is GENERAL)" as *u8, t3); total=total+1
57
58 var okall: i64=0; if pass==total { okall=1 }
59 g_puts("---- riscv_codegen: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8)
60 if okall==1 {
61 let logf: i64=sys_openat_append("knowledge/status/riscv_codegen.log" as *u8, 420)
62 if logf>=0 { let z: i64=sys_write(logf,"RISCVCODEGEN rung4 on nx_riscv_lib GREEN: qmul lowered+ran exit 6, retargeted->12\n" as *u8,77); sys_close(logf) }
63 g_puts("verdict=GREEN (RUNG 4 on the GENERAL nx_riscv_lib: Q16 op lowered IR->RISC-V + run -> correct; retargets on the IR)\n" as *u8); sys_exit(0); return 0
64 }
65 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
66}