From ba3455fcff0e1d4fce89a4147630a7cdd915bcea Mon Sep 17 00:00:00 2001 From: Goren Barak Date: Fri, 17 Nov 2023 14:03:42 -0500 Subject: [PATCH] added breakpoint --- src/codegen/fasm.rs | 40 ++++++++++++++++------------------------ src/main.rs | 2 +- src/parse/ast.rs | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/codegen/fasm.rs b/src/codegen/fasm.rs index b121b9a..a17236d 100644 --- a/src/codegen/fasm.rs +++ b/src/codegen/fasm.rs @@ -1,17 +1,5 @@ use crate::parse::ast::*; -pub fn parse_value(value: Value) -> String { - match value { - Value::Number(e) => { - return e.to_string(); - }, - - Value::Var(e) => { - return e.name.to_string(); - } - } -} - pub fn fasm_codegen(exprs: Vec) -> String { let mut asm_text = String::new(); let mut asm_data = String::new(); @@ -24,8 +12,8 @@ pub fn fasm_codegen(exprs: Vec) -> String { for expr in exprs.iter() { match expr { Expr::MathExpr(e) => { - asm_text.push_str(format!("\tmov r10, {}\n", parse_value(e.left)).as_str()); - asm_text.push_str(format!("\tmov r11, {}\n", parse_value(e.right)).as_str()); + asm_text.push_str(format!("\tmov r10, {}\n", e.left.unwrap()).as_str()); + asm_text.push_str(format!("\tmov r11, {}\n", e.right.unwrap()).as_str()); match e.operator { // If the operator is addition. MathOperator::OP_ADD => { @@ -65,52 +53,56 @@ pub fn fasm_codegen(exprs: Vec) -> String { match i { 0 => { // First parameter. Put in %rdi. - asm_text.push_str(format!("\tmov rdi, {:?}\n", parse_value(p.value)).as_str()); + asm_text.push_str(format!("\tmov rdi, {}\n", p.value.unwrap()).as_str()); }, 1 => { // Second parameter. Put in %rsi. - asm_text.push_str(format!("\tmov rsi, {:?}\n", parse_value(p.value)).as_str()); + asm_text.push_str(format!("\tmov rsi, {}\n", p.value.unwrap()).as_str()); }, 2 => { // Third parameter. Put in %rdx. - asm_text.push_str(format!("\tmov rdx, {:?}\n", parse_value(p.value)).as_str()); + asm_text.push_str(format!("\tmov rdx, {}\n", p.value.unwrap()).as_str()); }, 3 => { // Fourth parameter. Put in %rcx. - asm_text.push_str(format!("\tmov rcx, {:?}\n", parse_value(p.value)).as_str()); + asm_text.push_str(format!("\tmov rcx, {}\n", p.value.unwrap()).as_str()); }, 4 => { // Fifth parameter. Put in %r8. - asm_text.push_str(format!("\tmov r8, {:?}\n", parse_value(p.value)).as_str()); + asm_text.push_str(format!("\tmov r8, {}\n", p.value.unwrap()).as_str()); }, 5 => { // Sixth parameter. Put in %r9. - asm_text.push_str(format!("\tmov r9, {:?}\n", parse_value(p.value)).as_str()); + asm_text.push_str(format!("\tmov r9, {}\n", p.value.unwrap()).as_str()); }, _ => { // Parameters after the sixth parameter are pushed to the stack. - asm_text.push_str(format!("\tpush {:?}\n", parse_value(p.value)).as_str()); + asm_text.push_str(format!("\tpush {}\n", p.value.unwrap()).as_str()); } } } - asm_text.push_str(format!("call {:?}", e.name).as_str()); + asm_text.push_str(format!("call {}", e.name).as_str()); }, Expr::VarDefenition(e) => { - asm_data.push_str(format!("\t{} db {:?}", e.name, parse_value(e.value)).as_str()); + asm_data.push_str(format!("\t{} dq {}", e.name, e.value.unwrap()).as_str()); }, Expr::VarReference(e) => { asm_text.push_str(e.name); }, - + + Expr::Breakpoint => { + asm_text.push_str("\tint3\n"); + }, + _ => unimplemented!("sorry unimplemented"), } } diff --git a/src/main.rs b/src/main.rs index 8a532d5..8172a89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,5 +7,5 @@ use crate::parse::ast::*; pub mod parse; fn main() { - fasm_codegen(vec![Expr::VarDefenition(VarDefenition {name: "goren", value: Value::Number(10)}), Expr::MathExpr(Math { left: Value::Var(VarReference {name: "goren"}), right: Value::Number(4), operator: MathOperator::OP_DIV})]); + fasm_codegen(vec![Expr::VarDefenition(VarDefenition {name: "goren", value: Value::Number(10)}), Expr::MathExpr(Math { left: Value::Var(VarReference { name: "goren"}), right: Value::Number(17), operator: MathOperator::OP_MOD}), Expr::Breakpoint]); } diff --git a/src/parse/ast.rs b/src/parse/ast.rs index 9305ea5..345c9a9 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -5,6 +5,7 @@ pub enum Expr<'a> { FunDefenition(FunDefenition<'a>), VarDefenition(VarDefenition<'a>), VarReference(VarReference<'a>), + Breakpoint } // MATH EXPRESSION @@ -69,3 +70,17 @@ pub enum Value<'a> { Var(VarReference<'a>), Number(u64), } + +impl<'a> Value<'a> { + pub fn unwrap(&self) -> String { + match self { + Value::Number(e) => { + return e.to_string(); + }, + + Value::Var(e) => { + return format!("[{}]", e.name.to_string()); + } + } + } +}