code wiki / _hdl_build / nx_rv64_asm.nx

nx_rv64_asm.nx

buildroot/runtime/_hdl_build/nx_rv64_asm.nx

17509 B179 linesdepth 2pulls 2 transitivereach 27 importersview sourcekind librarytopic rv64
docsdependenciesstructsconstsfunctions

about

nx_rv64_asm.nx -- SOVEREIGN RV64 BARE-METAL TEXT-ASSEMBLER. Two-pass (collect labels -> encode with resolved branch/jump offsets) assembler emitting a FLAT binary whose entry is at offset 0 (loads at 0x80000000 via QEMU `-bios`, no syscall trampoline; a guest halts via the SiFive test finisher). Reuses the RV64 format encoders proven in nx_rv64_runproof_gate + adds U-type (lui) + B-type (branches). This is the tool for writing REAL device drivers that boot in qemu-system-riscv64 -- readable .s text, correct bytes. license_tier: ORIGINAL API: rvasm_assemble(src: *u8, n: i64, out: *u8, cap: i64) -> nbytes (or negative on error) Supported: lui li addi add sub and or xor sll srl slli srli andi ori lw lbu lhu ld sw sb sh sd beq bne blt bge bltu bgeu jal j mv ret nop ecall (branch/jal targets are LABELS)

dependencies 1 imports · 27 importers

nx_syscalls.nx nx_rv64_asm.nx nx_amo_payload.nx nx_asm_oracle.nx nx_emu_bench.nx nx_jit_ra_probe.nx nx_nishi_riscv_emit.nx nx_nxc_baremetal.nx nx_nxc_run.nx nx_rv64_asm_test.nx nx_rv64_asm_verify.nx nx_rv64_csr_trap_oracle.nx

diagram shows first 10 each side; +0 more imports, +17 more importers in the complete lists below.

imports: nx_syscalls.nx

imported by: nx_amo_payload.nxnx_asm_oracle.nxnx_emu_bench.nxnx_jit_ra_probe.nxnx_nishi_riscv_emit.nxnx_nxc_baremetal.nxnx_nxc_run.nxnx_rv64_asm_test.nxnx_rv64_asm_verify.nxnx_rv64_csr_trap_oracle.nxnx_rv64_fast_dev_gate.nxnx_rv64_fast_gate.nxnx_rv64_jit_fwd_gate.nxnx_rv64_jit_gate.nxnx_rv64_jit_mem_gate.nxnx_rv64_jit_ra_gate.nxnx_rv64_jit_w_gate.nxnx_rv64_mmu_oracle.nxnx_rv64_qemu_fuzz.nxnx_rv64_qemu_oracle.nxnx_rv64_sdeleg_oracle.nxnx_rv64_snapshot_gate.nxnx_rv64_tier_gate.nxnx_rv64_timer_oracle.nxnx_rv64_toolchain_gate.nxnx_virtio_rng_emit.nxnx_virtio_scan_emit.nx

structs

none

consts

10const RA_MAGIC_2047: i64 = 2047
11const RA_MAGIC_2048: i64 = 2048
139const RA_MAXSYM: i64 = 256

functions

13func ra_isws(c: i64) -> i64 { if c==32 { return 1 } if c==9 { return 1 } if c==13 { return 1 } if c==10 { return 1 } return 0 }
14func ra_isdig(c: i64) -> i64 { if c>=48 { if c<=57 { return 1 } } return 0 }
15func ra_ishex(c: i64) -> i64 { if ra_isdig(c)==1 { return 1 } if c>=97 { if c<=102 { return 1 } } if c>=65 { if c<=70 { return 1 } } return 0 }
called by 1: ra_imm calls 1: ra_isdig
16func ra_hexval(c: i64) -> i64 { if c>=48 { if c<=57 { return c-48 } } if c>=97 { if c<=102 { return c-87 } } if c>=65 { if c<=70 { return c-55 } } return 0 }
called by 1: ra_imm
19func ra_r(op: i64, f3: i64, f7: i64, rd: i64, rs1: i64, rs2: i64) -> i64 { return (f7<<25)|((rs2&0x1F)<<20)|((rs1&0x1F)<<15)|(f3<<12)|((rd&0x1F)<<7)|op }
20func ra_i(op: i64, f3: i64, rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm&0xFFF)<<20)|((rs1&0x1F)<<15)|(f3<<12)|((rd&0x1F)<<7)|op }
21func ra_s(op: i64, f3: i64, rs1: i64, rs2: i64, imm: i64) -> i64 { return (((imm>>5)&0x7F)<<25)|((rs2&0x1F)<<20)|((rs1&0x1F)<<15)|(f3<<12)|((imm&0x1F)<<7)|op }
22func ra_u(op: i64, rd: i64, imm: i64) -> i64 { return ((imm&0xFFFFF)<<12)|((rd&0x1F)<<7)|op }
23func ra_b(f3: i64, rs1: i64, rs2: i64, imm: i64) -> i64 { return (((imm>>12)&1)<<31)|(((imm>>5)&0x3F)<<25)|((rs2&0x1F)<<20)|((rs1&0x1F)<<15)|((f3&7)<<12)|(((imm>>1)&0xF)<<8)|(((imm>>11)&1)<<7)|0x63 }
24func ra_j(rd: i64, imm: i64) -> i64 { return 0x6F|((rd&0x1F)<<7)|(((imm>>12)&0xFF)<<12)|(((imm>>11)&0x1)<<20)|(((imm>>1)&0x3FF)<<21)|(((imm>>20)&0x1)<<31) }
25func ra_put(out: *u8, o: i64, w: i64) -> i64 { out[o]=(w&0xff) as u8; out[o+1]=((w>>8)&0xff) as u8; out[o+2]=((w>>16)&0xff) as u8; out[o+3]=((w>>24)&0xff) as u8; return o+4 }
29func ra_imm12(im: i64) -> i64 { if im > RA_MAGIC_2047 { return 0 } if im < (0 - RA_MAGIC_2048) { return 0 } return 1 }
called by 1: ra_encode
31func ra_sh6(im: i64) -> i64 { if im < 0 { return 0 } if im > 63 { return 0 } return 1 }
called by 1: ra_encode
34func ra_skipsep(s: *u8, cur: *i64) -> i64 { var p: i64=cur[0]; var go: i64=1; while go==1 { let c: i64=s[p] as i64; if c==44 { p=p+1 } else { if ra_isws(c)==1 { p=p+1 } else { go=0 } } } cur[0]=p; return p }
called by 3: ra_immra_regra_label_target calls 1: ra_isws
35func ra_num(s: *u8, cur: *i64) -> i64 { var p: i64=cur[0]; var v: i64=0; while ra_isdig(s[p] as i64)==1 { v=v*10+((s[p] as i64)-48); p=p+1 } cur[0]=p; return v }
called by 1: ra_reg calls 1: ra_isdig
36func ra_imm(s: *u8, cur: *i64) -> i64
44func ra_past(s: *u8, cur: *i64, ch: i64) -> i64 { var p: i64=cur[0]; var go: i64=1; while go==1 { let c: i64=s[p] as i64; if c==0 { go=0 } else { if c==ch { p=p+1; go=0 } else { p=p+1 } } } cur[0]=p; return p }
called by 1: ra_encode
46func ra_reg(s: *u8, cur: *i64) -> i64
called by 1: ra_encode calls 2: ra_skipsepra_num
60func ra_word_is(s: *u8, p: i64, lit: *u8) -> i64 { var i: i64=0; while (lit[i] as i64)!=0 { if (s[p+i] as i64)!=(lit[i] as i64) { return 0 } i=i+1 } let nc: i64=s[p+i] as i64; if ra_isws(nc)==1 { return 1 } if nc==0 { return 1 } return 0 }
called by 1: ra_encode calls 1: ra_isws
61func ra_afterword(s: *u8, p: i64) -> i64 { var q: i64=p; var go: i64=1; while go==1 { let c: i64=s[q] as i64; if c==0 { go=0 } else { if ra_isws(c)==1 { go=0 } else { q=q+1 } } } return q }
called by 2: ra_label_targetra_encode calls 1: ra_isws
64func ra_find_label(s: *u8, ts: i64, tlen: i64, lst: *i64, lln: *i64, lab: *i64, nl: i64) -> i64
called by 1: ra_label_target
70func ra_label_target(s: *u8, cur: *i64, lst: *i64, lln: *i64, lab: *i64, nl: i64) -> i64
76func ra_encode(s: *u8, fp: i64, abs: i64, lst: *i64, lln: *i64, lab: *i64, nl: i64) -> i64
141func rvasm_assemble(s: *u8, n: i64, out: *u8, cap: i64) -> i64
179func rvasm_assemble_str(s: *u8, out: *u8, cap: i64) -> i64 { var n: i64=0; while (s[n] as i64)!=0 { n=n+1 } return rvasm_assemble(s, n, out, cap) }
called by 20: mainmainmainmainmainmain+14 calls 1: rvasm_assemble