From 677521ca980236d079cb82f9622f22d8bbdb03f1 Mon Sep 17 00:00:00 2001 From: Toni Date: Mon, 16 Mar 2026 13:07:33 +0100 Subject: [PATCH] clean up main.rs --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/chip8.zr | 2 +- src/codegen_x86_64.rs | 52 +++++++++++++++++++++---------------------- src/main.rs | 25 ++++++++++++--------- src/std/net.zr | 1 - 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 410f5e0..97ce953 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -238,7 +238,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "zern" -version = "0.1.0" +version = "0.3.0" dependencies = [ "clap", ] diff --git a/Cargo.toml b/Cargo.toml index 190f328..6b87495 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zern" -version = "0.1.0" +version = "0.3.0" edition = "2024" [dependencies] diff --git a/examples/chip8.zr b/examples/chip8.zr index fba8a54..795755a 100644 --- a/examples/chip8.zr +++ b/examples/chip8.zr @@ -290,7 +290,7 @@ func main[argc: i64, argv: ptr] : i64 let c: CHIP8 = chip8_create() - let buffer: io.Buffer = io.read_binary_file(path) + let buffer: io.Buffer = must(io.read_binary_file?(path)) for i in 0..buffer->size c->memory[0x200 + i] = buffer->data[i] diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index 64a5c2b..94f4998 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -72,22 +72,22 @@ static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"]; // TODO: currently they are all just 64 bit values static BUILTIN_TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "any"]; -pub struct CodegenX86_64<'a> { +pub struct CodegenX86_64 { output: String, data_section: String, label_counter: usize, data_counter: usize, - pub analyzer: &'a mut Analyzer, + pub analyzer: Analyzer, } -impl<'a> CodegenX86_64<'a> { - pub fn new(analyzer: &'a mut Analyzer) -> CodegenX86_64<'a> { +impl CodegenX86_64 { + pub fn new() -> CodegenX86_64 { CodegenX86_64 { output: String::new(), data_section: String::new(), label_counter: 0, data_counter: 1, - analyzer, + analyzer: Analyzer::new(), } } @@ -115,27 +115,27 @@ section .bss section .text._builtin_heap_head _builtin_heap_head: - lea rax, [rel _heap_head] + lea rax, [rel _heap_head] ret section .text._builtin_heap_tail _builtin_heap_tail: - lea rax, [rel _heap_tail] + lea rax, [rel _heap_tail] ret section .text._builtin_err_code _builtin_err_code: - lea rax, [rel _err_code] + lea rax, [rel _err_code] ret section .text._builtin_err_msg _builtin_err_msg: - lea rax, [rel _err_msg] + lea rax, [rel _err_msg] ret section .text._builtin_read64 _builtin_read64: - mov rax, qword [rdi] + mov rax, QWORD [rdi] ret section .text._builtin_set64 @@ -150,8 +150,8 @@ _builtin_syscall: mov rsi, rdx mov rdx, rcx mov r10, r8 - mov r8, r9 - mov r9, [rsp+8] + mov r8, r9 + mov r9, [rsp+8] syscall ret @@ -160,8 +160,8 @@ io.printf: push rbp mov rbp, rsp sub rsp, 48 - mov [rsp], rsi - mov [rsp + 8], rdx + mov [rsp], rsi + mov [rsp + 8], rdx mov [rsp + 16], rcx mov [rsp + 24], r8 mov [rsp + 32], r9 @@ -178,28 +178,28 @@ io.printf: " section .text._builtin_environ _builtin_environ: - lea rax, [rel _environ] - mov rax, [rax] + lea rax, [rel _environ] + mov rax, [rax] ret global _start section .text _start: - xor rbp, rbp + xor rbp, rbp ; setup args - pop rdi - mov rsi, rsp + pop rdi + mov rsi, rsp ; save environ - lea rdx, [rsi + rdi*8 + 8] - lea rax, [rel _environ] - mov [rax], rdx + lea rdx, [rsi + rdi*8 + 8] + lea rax, [rel _environ] + mov [rax], rdx ; align stack - and rsp, -16 + and rsp, -16 ; main() - call main + call main ; exit - mov rdi, rax - mov rax, 60 + mov rdi, rax + mov rax, 60 syscall " ); diff --git a/src/main.rs b/src/main.rs index b5e4b72..5220485 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,39 +73,44 @@ fn compile_file(args: Args) -> Result<(), ZernError> { let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap(); - let mut analyzer = analyzer::Analyzer::new(); - let mut codegen = codegen_x86_64::CodegenX86_64::new(&mut analyzer); + let mut codegen = codegen_x86_64::CodegenX86_64::new(); codegen.emit_prologue(args.use_gcc)?; compile_std!(&mut codegen, "syscalls.zr", "std.zr", "net.zr"); compile_file_to(&mut codegen, filename, source)?; if !args.output_asm { - fs::write(format!("{}.s", args.out), codegen.get_output()).unwrap(); + let out = args.out.unwrap_or_else(|| "out".into()); - run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out)); + fs::write(format!("{}.s", out), codegen.get_output()).unwrap(); + + run_command(format!("nasm -f elf64 -o {}.o {}.s", out, out)); if args.use_gcc { run_command(format!( "gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}", - args.out, args.out, args.cflags + out, out, args.cflags )); } else { run_command(format!( "ld -static -o {} {}.o --gc-sections -e _start", - args.out, args.out + out, out )); } if args.run_exe { run_command( - std::fs::canonicalize(args.out) + std::fs::canonicalize(out) .unwrap() .to_string_lossy() .into_owned(), ); } } else { - fs::write(&args.out, codegen.get_output()).unwrap(); + fs::write( + args.out.unwrap_or_else(|| "out.s".into()), + codegen.get_output(), + ) + .unwrap(); } Ok(()) @@ -116,8 +121,8 @@ fn compile_file(args: Args) -> Result<(), ZernError> { struct Args { path: String, - #[arg(short, default_value = "out", help = "Output path")] - out: String, + #[arg(short, help = "Output path")] + out: Option, #[arg(short = 'S', help = "Only generate assembly")] output_asm: bool, diff --git a/src/std/net.zr b/src/std/net.zr index 7b95841..f47df90 100644 --- a/src/std/net.zr +++ b/src/std/net.zr @@ -42,7 +42,6 @@ func net.listen?[packed_host: i64, port: i64] : i64 return s -// TODO: resolve DNS func net.connect?[packed_host: i64, port: i64] : i64 err.clear() let s: i64 = _builtin_syscall(SYS_socket, AF_INET, SOCK_STREAM, 0)