diff --git a/src/codegen/codegen.rs b/src/codegen/codegen.rs index c530655..d8cd6cc 100644 --- a/src/codegen/codegen.rs +++ b/src/codegen/codegen.rs @@ -44,14 +44,7 @@ impl<'a> CodeGen<'a> { text_section: formatdoc!( " section .text - global _start - - _start: - call main - - mov rax, 60 - mov rdi, 0 - syscall + global main main: " @@ -110,11 +103,23 @@ impl<'a> CodeGen<'a> { self.mov(name, right); - return 69; + 69 } else { panic!("Cant assign to non ident"); } } + BinOp::Add => { + let left = self.expr(expr.left.as_ref()); + let right = self.expr(expr.right.as_ref()); + + self.add(left, right) + } + BinOp::Sub => { + let left = self.expr(expr.left.as_ref()); + let right = self.expr(expr.right.as_ref()); + + self.sub(left, right) + } _ => panic!("lasjdf"), } } @@ -162,6 +167,38 @@ impl<'a> CodeGen<'a> { r } + fn add(&mut self, r1: usize, r2: usize) -> usize { + writedoc!( + self.text_section, + " + \tadd {}, {} + ", + &self.registers[r1].name, + &self.registers[r2].name, + ) + .unwrap(); + + self.free(r2); + + r1 + } + + fn sub(&mut self, r1: usize, r2: usize) -> usize { + writedoc!( + self.text_section, + " + \tsub {}, {} + ", + &self.registers[r1].name, + &self.registers[r2].name, + ) + .unwrap(); + + self.free(r2); + + r1 + } + pub fn compile(&mut self, program: Vec, path: &str) { let mut file = File::create(path).expect(&format!("Failed to open a file {}", path)); diff --git a/src/main.rs b/src/main.rs index 11912aa..7566528 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ fn main() { " u8 bar; - bar = 69; + bar = 5 + 10 - 12; " .to_string(), );