comparison operators

This commit is contained in:
2025-05-30 18:07:26 +02:00
parent 3a876a97d3
commit 2bc24c394d
3 changed files with 31 additions and 9 deletions

4
.gitignore vendored
View File

@@ -1,3 +1,3 @@
/target /target
/*.py /out*
/*.mot /TODO

View File

@@ -68,10 +68,12 @@ impl Codegen {
&mut self.output, &mut self.output,
"section .data "section .data
format db \"%d\", 10, 0 format db \"%d\", 10, 0
section .text section .text
extern printf
global main global main
main: main:
extern printf
push rbp push rbp
mov rbp, rsp mov rbp, rsp
sub rsp, 256" // TODO sub rsp, 256" // TODO
@@ -100,7 +102,7 @@ section .note.GNU-stack
pub fn label(&mut self) -> String { pub fn label(&mut self) -> String {
self.label_counter += 1; self.label_counter += 1;
format!(".{}", self.label_counter) format!(".L{}", self.label_counter)
} }
pub fn compile_stmt(&mut self, env: &mut Env, stmt: Stmt) -> Result<(), Box<dyn Error>> { pub fn compile_stmt(&mut self, env: &mut Env, stmt: Stmt) -> Result<(), Box<dyn Error>> {
@@ -188,15 +190,31 @@ section .note.GNU-stack
writeln!(&mut self.output, " sete al")?; writeln!(&mut self.output, " sete al")?;
writeln!(&mut self.output, " movzx rax, al")?; writeln!(&mut self.output, " movzx rax, al")?;
} }
TokenType::NotEqual => todo!(), TokenType::NotEqual => {
TokenType::Greater => todo!(), writeln!(&mut self.output, " cmp rax, rbx")?;
TokenType::GreaterEqual => todo!(), writeln!(&mut self.output, " setne al")?;
writeln!(&mut self.output, " movzx rax, al")?;
}
TokenType::Greater => {
writeln!(&mut self.output, " cmp rax, rbx")?;
writeln!(&mut self.output, " setg al")?;
writeln!(&mut self.output, " movzx rax, al")?;
}
TokenType::GreaterEqual => {
writeln!(&mut self.output, " cmp rax, rbx")?;
writeln!(&mut self.output, " setge al")?;
writeln!(&mut self.output, " movzx rax, al")?;
}
TokenType::Less => { TokenType::Less => {
writeln!(&mut self.output, " cmp rax, rbx")?; writeln!(&mut self.output, " cmp rax, rbx")?;
writeln!(&mut self.output, " setl al")?; writeln!(&mut self.output, " setl al")?;
writeln!(&mut self.output, " movzx rax, al")?; writeln!(&mut self.output, " movzx rax, al")?;
} }
TokenType::LessEqual => todo!(), TokenType::LessEqual => {
writeln!(&mut self.output, " cmp rax, rbx")?;
writeln!(&mut self.output, " setle al")?;
writeln!(&mut self.output, " movzx rax, al")?;
}
_ => unreachable!(), _ => unreachable!(),
} }
} }
@@ -210,7 +228,11 @@ section .note.GNU-stack
self.compile_expr(env, *right)?; self.compile_expr(env, *right)?;
match op.token_type { match op.token_type {
TokenType::Minus => writeln!(&mut self.output, " neg rax")?, TokenType::Minus => writeln!(&mut self.output, " neg rax")?,
TokenType::Bang => todo!(), TokenType::Bang => {
writeln!(&mut self.output, " test rax, rax")?;
writeln!(&mut self.output, " sete al")?;
writeln!(&mut self.output, " movzx rax, al")?;
}
_ => unreachable!(), _ => unreachable!(),
} }
} }