code wiki / (root) / nxasm_main.nx

nxasm_main.nx source

↩ module page · 55 lines · 1750 B

1// nxasm_main.nx -- CLI driver for nxasm_v2 + elf_writer. 2// 3// Usage: nxasm <input.s> > <output.elf> 4// 5// Reads input.s via sys_read_file, assembles to RV64 machine 6// bytes via nxasm_v2.assemble(), wraps in a static Linux ELF 7// via elf_writer.write_elf(), writes to stdout. 8// 9// Single-step asm+link. For programs whose entire link is a 10// single .s file (the common case for nxc.nx output today), 11// this replaces the entire `gcc-as + ld` chain in verify.sh. 12// 13// Exit codes: 14// 0 success 15// 1 no args 16// 2 read failed 17// 3 assemble failed (negative byte count) 18// 4 write failed 19// 20// Together with nxld_main (which handles multi-object link), 21// these two tools cover every gcc-as + ld invocation we need. 22 23import "syscalls.nx" 24import "nxasm_v2.nx" 25import "elf_writer.nx" 26 27func main(argc: i64, argv: *i64) -> i64 { 28 if argc < 2 { 29 return __syscall(93, 1, 0, 0, 0, 0, 0) 30 } 31 let path: *u8 = (argv[1]) as *u8 32 33 let len_raw: *u8 = sys_mmap(16) 34 let len_out: *i64 = len_raw as *i64 35 *len_out = 0 36 let src: *u8 = sys_read_file(path, len_out) 37 if src == (0 as *u8) { 38 return __syscall(93, 2, 0, 0, 0, 0, 0) 39 } 40 41 // Assemble. Generous code buffer (4 MB) covers up to ~1 M 42 // instructions; matches nxc.nx's code_buf for compile parity. 43 let code_buf: *u8 = sys_mmap(4194304) 44 let code_len: i64 = assemble(src, *len_out, code_buf, 4194304) 45 if code_len <= 0 { 46 return __syscall(93, 3, 0, 0, 0, 0, 0) 47 } 48 49 // Wrap in ELF + write to stdout. 50 let n: i64 = write_elf(code_buf, code_len, 1) 51 if n < 0 { 52 return __syscall(93, 4, 0, 0, 0, 0, 0) 53 } 54 return __syscall(93, 0, 0, 0, 0, 0, 0) 55}