simplify emit_debug with gas magic
This commit is contained in:
@@ -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");
|
||||||
|
|||||||
@@ -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!(
|
||||||
|
|||||||
Reference in New Issue
Block a user