simplify emit_debug with gas magic

This commit is contained in:
2026-06-26 16:50:05 +02:00
parent 8ad69be5aa
commit 6fe971c615
2 changed files with 6 additions and 8 deletions

View File

@@ -77,14 +77,12 @@ pub struct CodegenX86_64<'a> {
data_counter: usize, data_counter: usize,
pub symbol_table: &'a SymbolTable, pub symbol_table: &'a SymbolTable,
pub expr_types: &'a HashMap<usize, String>, pub expr_types: &'a HashMap<usize, String>,
emit_debug: bool,
} }
impl<'a> CodegenX86_64<'a> { impl<'a> CodegenX86_64<'a> {
pub fn new( pub fn new(
symbol_table: &'a SymbolTable, symbol_table: &'a SymbolTable,
expr_types: &'a HashMap<usize, String>, expr_types: &'a HashMap<usize, String>,
emit_debug: bool,
) -> CodegenX86_64<'a> { ) -> CodegenX86_64<'a> {
CodegenX86_64 { CodegenX86_64 {
output: String::new(), output: String::new(),
@@ -93,7 +91,6 @@ impl<'a> CodegenX86_64<'a> {
data_counter: 1, data_counter: 1,
symbol_table, symbol_table,
expr_types, expr_types,
emit_debug,
} }
} }
@@ -372,10 +369,10 @@ _builtin_environ:
exported, exported,
} => { } => {
let name = &name.lexeme; 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, ".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, ".section .text.{}", name);
emit!(&mut self.output, "{}:", name); emit!(&mut self.output, "{}:", name);
emit!(&mut self.output, " push rbp"); emit!(&mut self.output, " push rbp");

View File

@@ -38,8 +38,7 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
typechecker.typecheck_stmt(&mut typechecker::Env::new(), stmt)?; typechecker.typecheck_stmt(&mut typechecker::Env::new(), stmt)?;
} }
let mut codegen = let mut codegen = codegen_x86_64::CodegenX86_64::new(&symbol_table, &typechecker.expr_types);
codegen_x86_64::CodegenX86_64::new(&symbol_table, &typechecker.expr_types, args.emit_debug);
codegen.emit_prologue(args.use_crt)?; codegen.emit_prologue(args.use_crt)?;
for stmt in statements { for stmt in statements {
codegen.compile_stmt(&mut codegen_x86_64::Env::new(), &stmt)?; 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(); 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 { if args.use_crt {
run_command(format!( run_command(format!(