From e749f579a4f9c5c30b59083e26ec9a2e88a68740 Mon Sep 17 00:00:00 2001 From: Goren Barak Date: Tue, 21 Nov 2023 13:32:09 -0500 Subject: [PATCH] added return from procedure after function, and made `FunCall.params` a `Vec --- src/codegen/fasm.rs | 27 ++++++++++++++------------- src/main.rs | 15 ++++++++++++--- src/parse/ast.rs | 7 +------ 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/codegen/fasm.rs b/src/codegen/fasm.rs index 224c5cd..d1901e3 100644 --- a/src/codegen/fasm.rs +++ b/src/codegen/fasm.rs @@ -58,42 +58,42 @@ pub fn fasm_codegen(exprs: &Vec, not_a_function: bool) -> String { match i { 0 => { // First parameter. Put in %rdi. - asm_start.push_str(format!("\tmov rdi, {}\n", p.value.unwrap()).as_str()); + asm_start.push_str(format!("\tmov rdi, {}\n", p.unwrap()).as_str()); }, 1 => { // Second parameter. Put in %rsi. - asm_start.push_str(format!("\tmov rsi, {}\n", p.value.unwrap()).as_str()); + asm_start.push_str(format!("\tmov rsi, {}\n", p.unwrap()).as_str()); }, 2 => { // Third parameter. Put in %rdx. - asm_start.push_str(format!("\tmov rdx, {}\n", p.value.unwrap()).as_str()); + asm_start.push_str(format!("\tmov rdx, {}\n", p.unwrap()).as_str()); }, 3 => { // Fourth parameter. Put in %rcx. - asm_start.push_str(format!("\tmov rcx, {}\n", p.value.unwrap()).as_str()); + asm_start.push_str(format!("\tmov rcx, {}\n", p.unwrap()).as_str()); }, 4 => { // Fifth parameter. Put in %r8. - asm_start.push_str(format!("\tmov r8, {}\n", p.value.unwrap()).as_str()); + asm_start.push_str(format!("\tmov r8, {}\n", p.unwrap()).as_str()); }, 5 => { // Sixth parameter. Put in %r9. - asm_start.push_str(format!("\tmov r9, {}\n", p.value.unwrap()).as_str()); + asm_start.push_str(format!("\tmov r9, {}\n", p.unwrap()).as_str()); }, _ => { // Parameters after the sixth parameter are pushed to the stack. - asm_start.push_str(format!("\tpush {}\n", p.value.unwrap()).as_str()); + asm_start.push_str(format!("\tpush {}\n", p.unwrap()).as_str()); } } } - asm_start.push_str(format!("call {}", e.name).as_str()); + asm_start.push_str(format!("\tcall {}\n", e.name).as_str()); }, Expr::VarDefinition(e) => { @@ -111,6 +111,7 @@ pub fn fasm_codegen(exprs: &Vec, not_a_function: bool) -> String { Expr::FunDefinition(e) => { asm_text.push_str(format!("{}:\n", e.name).as_str()); asm_text.push_str(fasm_codegen(&e.contents, false).as_str()); + asm_text.push_str("\tret\n"); } _ => break, @@ -118,12 +119,12 @@ pub fn fasm_codegen(exprs: &Vec, not_a_function: bool) -> String { } - if not_a_function { - asm_start.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n"); - asm_start.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n"); - asm_start.push_str("\tsyscall ; this is the instruction to actually perform the system call.\n"); - } + if not_a_function { + asm_start.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n"); + asm_start.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n"); + asm_start.push_str("\tsyscall ; this is the instruction to actually perform the system call.\n"); + } let asm = format!("{}{}{}", asm_start, asm_text, asm_data); asm } diff --git a/src/main.rs b/src/main.rs index ea39b7c..36dd435 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,16 +18,25 @@ fn main() { ), Expr::FunDefinition(FunDefinition { name: "adder", contents: vec![ - Expr::Return(Value::Math( + Expr::MathExpr( Math { left: &Value::Param(ParamReference {param_number: 0}), right: &Value::Param(ParamReference {param_number: 1}), operator: MathOperator::OP_ADD } - )) + ) ] }), - Expr::Breakpoint], + + Expr::FunCall( + FunCall { + name: "adder", + params: vec![Value::Var(VarReference {name: "goren"}), Value::Number(6)] + } + ), + + Expr::Breakpoint + ], true ); println!("{}", fc); diff --git a/src/parse/ast.rs b/src/parse/ast.rs index 5bf5acb..3973551 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -31,7 +31,7 @@ pub enum MathOperator { #[derive(Debug)] pub struct FunCall<'a> { pub name: &'a str, - pub params: Vec>, + pub params: Vec>, } #[derive(Debug)] @@ -75,7 +75,6 @@ pub enum Value<'a> { Var(VarReference<'a>), Param(ParamReference), Number(u64), - Math(Math<'a>), } impl<'a> Value<'a> { @@ -100,10 +99,6 @@ impl<'a> Value<'a> { Value::Var(e) => { return format!("[{}]", e.name.to_string()); }, - - Value::Math(e) => { - return String::from("rax"); - } } } }