code wiki / (root) / nx_riscv_lib.nx

nx_riscv_lib.nx

buildroot/runtime/nx_riscv_lib.nx

9843 B135 linesdepth 3pulls 3 transitivereach 8 importersview sourcekind librarytopic riscv
docsdependenciesstructsconstsfunctions

about

nx_riscv_lib.nx -- GENERAL SOVEREIGN RISC-V CAPABILITIES (the shared ecosystem library for the rv64im toolchain). Extracted (rule 15) from nx_riscv_{emit,decode,elf,emu,codegen,loop} so the encoder / lowerer / ELF writer / emulator are ONE general capability instead of N copies. Pure library: no main. Imports only nx_syscalls. ENCODERS enc_r/enc_i/enc_u/enc_b (rv64im instruction encoding) BYTES put32/put64, rd64, memrd/memwr (little-endian buffer + simulated-memory I/O) SEXT sext12/sext32/sext_b (sign extension) ELF build_header (rv64 ELF64 + PT_LOAD header) CODEGEN lower (IR -> RISC-V, incl control flow: LABEL/BLE/ADDI) EMULATOR run (full rv64im interpreter: arith/lui/ld/sd/branch/jal/jalr/ecall) UI g_puts/g_pn/ck (shared output helpers)

dependencies 2 imports · 8 importers

nx_syscalls.nx nx_itoa_lib.nx nx_riscv_lib.nx nx_nishi_riscv_emit.nx nx_riscv_codegen.nx nx_riscv_decode.nx nx_riscv_elf.nx nx_riscv_emit.nx nx_riscv_emu.nx nx_riscv_gate.nx nx_riscv_loop.nx

imports: nx_syscalls.nxnx_itoa_lib.nx

imported by: nx_nishi_riscv_emit.nxnx_riscv_codegen.nxnx_riscv_decode.nxnx_riscv_elf.nxnx_riscv_emit.nxnx_riscv_emu.nxnx_riscv_gate.nxnx_riscv_loop.nx

structs

none

consts

13const K_MAGIC_4095: i64 = 4095
14const K_MAGIC_2048: i64 = 2048
15const K_MAGIC_4096: i64 = 4096
16const K_MAGIC_8192: i64 = 8192
17const K_MAGIC_200000: i64 = 200000
18const K_MAGIC_200001: i64 = 200001

functions

21func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
26func g_pn(v: i64) -> i64 { nxi_out(v); return 0 }
called by 7: mainmainmainmainmainmain+1 calls 1: nxi_out
27func 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 }
called by 7: mainmainmainmainmainmain+1 calls 1: g_puts
30func enc_r(op: i64, f3: i64, f7: i64, rd: i64, rs1: i64, rs2: i64) -> i64 { return (f7<<25)|(rs2<<20)|(rs1<<15)|(f3<<12)|(rd<<7)|op }
called by 4: mainmainmainlower
31func enc_i(op: i64, f3: i64, rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm&K_MAGIC_4095)<<20)|(rs1<<15)|(f3<<12)|(rd<<7)|op }
32func enc_u(op: i64, rd: i64, imm20: i64) -> i64 { return ((imm20&0xFFFFF)<<12)|(rd<<7)|op }
called by 1: lower
33func enc_b(op: i64, f3: i64, rs1: i64, rs2: i64, imm: i64) -> i64
called by 1: lower
37func enc_s(op: i64, f3: i64, rs1: i64, rs2: i64, imm: i64) -> i64 { let hi: i64=(imm>>5)&127; let lo: i64=imm&31; return (hi<<25)|(rs2<<20)|(rs1<<15)|(f3<<12)|(lo<<7)|op }
called by 1: main
40func d_op(w: i64) -> i64 { return w&127 }
called by 1: main
41func d_rd(w: i64) -> i64 { return (w>>7)&31 }
called by 1: main
42func d_f3(w: i64) -> i64 { return (w>>12)&7 }
called by 1: main
43func d_rs1(w: i64) -> i64 { return (w>>15)&31 }
called by 1: main
44func d_rs2(w: i64) -> i64 { return (w>>20)&31 }
called by 1: main
45func d_f7(w: i64) -> i64 { return (w>>25)&127 }
called by 1: main
46func d_immi(w: i64) -> i64 { return (w>>20)&K_MAGIC_4095 }
called by 1: main
48func ident(w: i64) -> i64
called by 2: mainmain
61func put8(m: *u8, p: i64, v: i64) -> i64 { m[p]=(v&255) as u8; return p+1 }
62func put16(m: *u8, p: i64, v: i64) -> i64 { m[p]=(v&255) as u8; m[p+1]=((v>>8)&255) as u8; return p+2 }
63func put32(m: *u8, p: i64, w: i64) -> i64 { m[p]=(w&255) as u8; m[p+1]=((w>>8)&255) as u8; m[p+2]=((w>>16)&255) as u8; m[p+3]=((w>>24)&255) as u8; return p+4 }
64func rd16(m: *u8, o: i64) -> i64 { return (m[o] as i64)|((m[o+1] as i64)<<8) }
called by 1: main
65func rd32(m: *u8, o: i64) -> i64 { return (m[o] as i64)|((m[o+1] as i64)<<8)|((m[o+2] as i64)<<16)|((m[o+3] as i64)<<24) }
called by 1: main
66func put64(m: *u8, p: i64, v: i64) -> i64 { var i: i64=0; while i<8 { m[p+i]=((v>>(i*8))&255) as u8; i=i+1 } return p+8 }
called by 1: build_header
67func rd64(m: *u8, o: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=v|((m[o+i] as i64)<<(i*8)); i=i+1 } return v }
called by 2: mainmain
68func memrd32(m: *u8, idx: i64) -> i64 { return ((m[idx] as i64)|((m[idx+1] as i64)<<8)|((m[idx+2] as i64)<<16)|((m[idx+3] as i64)<<24)) & 0xFFFFFFFF }
called by 1: run
69func memrd64(m: *u8, idx: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=v|((m[idx+i] as i64)<<(i*8)); i=i+1 } return v }
called by 1: run
70func memwr32(m: *u8, idx: i64, w: i64) -> i64 { m[idx]=(w&255) as u8; m[idx+1]=((w>>8)&255) as u8; m[idx+2]=((w>>16)&255) as u8; m[idx+3]=((w>>24)&255) as u8; return 0 }
called by 1: main
71func memwr64(m: *u8, idx: i64, w: i64) -> i64 { var i: i64=0; while i<8 { m[idx+i]=((w>>(i*8))&255) as u8; i=i+1 } return 0 }
called by 1: run
74func sext12(v: i64) -> i64 { let x: i64=v&K_MAGIC_4095; if (x&K_MAGIC_2048)!=0 { return x-K_MAGIC_4096 } return x }
called by 1: run
75func sext32(v: i64) -> i64 { let x: i64=v&0xFFFFFFFF; if (x&0x80000000)!=0 { return x-0x100000000 } return x }
called by 1: run
76func sext_b(w: i64) -> i64 { let imm: i64=(((w>>31)&1)<<12)|(((w>>7)&1)<<11)|(((w>>25)&63)<<5)|(((w>>8)&15)<<1); if (imm&K_MAGIC_4096)!=0 { return imm-K_MAGIC_8192 } return imm }
called by 1: run
79func build_header(m: *u8, total: i64, entry: i64, base: i64) -> i64
called by 4: genmaingen_and_runmain calls 2: put32put64
92func lower(m: *u8, off: i64, irop: *i64, irdst: *i64, irs1: *i64, irs2: *i64, irimm: *i64, n: i64, preg: *i64, labelpos: *i64) -> i64
112func run(m: *u8, entry: i64, BASE: i64) -> i64