From 6fe971c6155332599b4f016c50bf70e41b9ce481 Mon Sep 17 00:00:00 2001 From: Toni Date: Fri, 26 Jun 2026 16:50:05 +0200 Subject: [PATCH] simplify emit_debug with gas magic --- src/codegen_x86_64.rs | 7 ++----- src/main.rs | 7 ++++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index e0b4306..1bb7892 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -77,14 +77,12 @@ pub struct CodegenX86_64<'a> { data_counter: usize, pub symbol_table: &'a SymbolTable, pub expr_types: &'a HashMap, - emit_debug: bool, } impl<'a> CodegenX86_64<'a> { pub fn new( symbol_table: &'a SymbolTable, expr_types: &'a HashMap, - emit_debug: bool, ) -> CodegenX86_64<'a> { CodegenX86_64 { output: String::new(), @@ -93,7 +91,6 @@ impl<'a> CodegenX86_64<'a> { data_counter: 1, symbol_table, expr_types, - emit_debug, } } @@ -372,10 +369,10 @@ _builtin_environ: exported, } => { let name = &name.lexeme; - if self.emit_debug || *exported || name == "main" { + if *exported || name == "main" { emit!(&mut self.output, ".globl {0}", name); - emit!(&mut self.output, ".type {0}, @function", name); } + emit!(&mut self.output, ".type {0}, @function", name); emit!(&mut self.output, ".section .text.{}", name); emit!(&mut self.output, "{}:", name); emit!(&mut self.output, " push rbp"); diff --git a/src/main.rs b/src/main.rs index 0d82bd0..ec843ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,8 +38,7 @@ fn compile_file(args: Args) -> Result<(), ZernError> { typechecker.typecheck_stmt(&mut typechecker::Env::new(), stmt)?; } - let mut codegen = - codegen_x86_64::CodegenX86_64::new(&symbol_table, &typechecker.expr_types, args.emit_debug); + let mut codegen = codegen_x86_64::CodegenX86_64::new(&symbol_table, &typechecker.expr_types); codegen.emit_prologue(args.use_crt)?; for stmt in statements { codegen.compile_stmt(&mut codegen_x86_64::Env::new(), &stmt)?; @@ -50,7 +49,9 @@ fn compile_file(args: Args) -> Result<(), ZernError> { fs::write(format!("{out}.s"), codegen.get_output()).unwrap(); - run_command(format!("as --64 -o {out}.o {out}.s")); + let debug_flag = if args.emit_debug { "-g" } else { "" }; + + run_command(format!("as --64 {debug_flag} -o {out}.o {out}.s")); if args.use_crt { run_command(format!(