added a Value type

This commit is contained in:
Goren Barak 2023-11-17 13:35:22 -05:00
parent 2f20415224
commit d7c744bc77
3 changed files with 115 additions and 93 deletions

View file

@ -1,105 +1,120 @@
use crate::parse::ast::*;
pub fn fasm_codegen(expr: Expr) -> String {
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<Expr>) -> String {
let mut asm_text = String::new();
let mut asm_data = String::new();
asm_text.push_str("format ELF64 executable 3\n");
asm_text.push_str("segment readable executable\n");
asm_text.push_str("entry _start\n");
asm_text.push_str("_start:\n");
asm_data.push_str("segment readable writeable\n");
match expr {
Expr::MathExpr(e) => {
asm_text.push_str(format!("\tmov r10, {:?}\n", e.left).as_str());
asm_text.push_str(format!("\tmov r11, {:?}\n", e.right).as_str());
match e.operator {
// If the operator is addition.
MathOperator::OP_ADD => {
asm_text.push_str("\tadd r10, r11\n");
asm_text.push_str("\tmov rax, r10\n");
},
// If the operator is multiplication.
MathOperator::OP_MULT => {
asm_text.push_str("\timul r10, r11\n");
asm_text.push_str("\tmov rax, r10\n");
},
// If the operator is division.
MathOperator::OP_DIV => {
asm_text.push_str("\tmov rax, r10\n");
asm_text.push_str("\txor rdx, rdx\n");
asm_text.push_str("\tidiv r11\n");
// The quotient is now stored in %rax.
},
// If the operators is subtraction.
MathOperator::OP_SUB => {
asm_text.push_str("\tsub r10, r11\n");
asm_text.push_str("\tmov rax, r10\n");
},
// If the operator is modulo.
MathOperator::OP_MOD => {
asm_text.push_str("\tmov rax, r10\n");
asm_text.push_str("\txor rdx, rdx\n");
asm_text.push_str("\tidiv r11\n");
asm_text.push_str("\tmov rax, rdx\n");
// The remainder will now be stored in the %rax register.
}
}
},
asm_data.push_str("\nsegment readable writable\n");
Expr::FunCall(e) => {
for (i, p) in e.params.iter().enumerate() {
match i {
0 => {
// First parameter. Put in %rdi.
asm_text.push_str(format!("\tmov rdi, {:?}\n", p.value).as_str());
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());
match e.operator {
// If the operator is addition.
MathOperator::OP_ADD => {
asm_text.push_str("\tadd r10, r11\n");
asm_text.push_str("\tmov rax, r10\n");
},
1 => {
// Second parameter. Put in %rsi.
asm_text.push_str(format!("\tmov rsi, {:?}\n", p.value).as_str());
// If the operator is multiplication.
MathOperator::OP_MULT => {
asm_text.push_str("\timul r10, r11\n");
asm_text.push_str("\tmov rax, r10\n");
},
2 => {
// Third parameter. Put in %rdx.
asm_text.push_str(format!("\tmov rdx, {:?}\n", p.value).as_str());
// If the operator is division.
MathOperator::OP_DIV => {
asm_text.push_str("\tmov rax, r10\n");
asm_text.push_str("\txor rdx, rdx\n");
asm_text.push_str("\tidiv r11\n");
// The quotient is now stored in %rax.
},
3 => {
// Fourth parameter. Put in %rcx.
asm_text.push_str(format!("\tmov rcx, {:?}\n", p.value).as_str());
// If the operators is subtraction.
MathOperator::OP_SUB => {
asm_text.push_str("\tsub r10, r11\n");
asm_text.push_str("\tmov rax, r10\n");
},
4 => {
// Fifth parameter. Put in %r8.
asm_text.push_str(format!("\tmov r8, {:?}\n", p.value).as_str());
},
5 => {
// Sixth parameter. Put in %r9.
asm_text.push_str(format!("\tmov r9, {:?}\n", p.value).as_str());
},
_ => {
// Parameters after the sixth parameter are pushed to the stack.
asm_text.push_str(format!("\tpush {:?}\n", p.value).as_str());
// If the operator is modulo.
MathOperator::OP_MOD => {
asm_text.push_str("\tmov rax, r10\n");
asm_text.push_str("\txor rdx, rdx\n");
asm_text.push_str("\tidiv r11\n");
asm_text.push_str("\tmov rax, rdx\n");
// The remainder will now be stored in the %rax register.
}
}
}
},
asm_text.push_str(format!("call {:?}", e.name).as_str());
},
Expr::FunCall(e) => {
for (i, p) in e.params.iter().enumerate() {
match i {
0 => {
// First parameter. Put in %rdi.
asm_text.push_str(format!("\tmov rdi, {:?}\n", parse_value(p.value)).as_str());
},
Expr::VarDefenition(e) => {
asm_data.push_str(format!("\t{} dq {:?}", e.name, e.value).as_str());
},
1 => {
// Second parameter. Put in %rsi.
asm_text.push_str(format!("\tmov rsi, {:?}\n", parse_value(p.value)).as_str());
},
Expr::VarReference(e) => {
asm_text.push_str(e.name);
},
2 => {
// Third parameter. Put in %rdx.
asm_text.push_str(format!("\tmov rdx, {:?}\n", parse_value(p.value)).as_str());
},
3 => {
// Fourth parameter. Put in %rcx.
asm_text.push_str(format!("\tmov rcx, {:?}\n", parse_value(p.value)).as_str());
},
4 => {
// Fifth parameter. Put in %r8.
asm_text.push_str(format!("\tmov r8, {:?}\n", parse_value(p.value)).as_str());
},
5 => {
// Sixth parameter. Put in %r9.
asm_text.push_str(format!("\tmov r9, {:?}\n", parse_value(p.value)).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!("call {:?}", e.name).as_str());
},
Expr::VarDefenition(e) => {
asm_data.push_str(format!("\t{} db {:?}", e.name, parse_value(e.value)).as_str());
},
Expr::VarReference(e) => {
asm_text.push_str(e.name);
},
_ => unimplemented!("sorry unimplemented"),
_ => unimplemented!("sorry unimplemented"),
}
}
asm_text.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n");
asm_text.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n");
asm_text.push_str("\tsyscall ; this is the instruction to actually perform the system call.");

View file

@ -1,3 +1,5 @@
#![allow(warnings)]
pub mod lex;
pub mod codegen;
use crate::codegen::fasm::*;
@ -5,5 +7,5 @@ use crate::parse::ast::*;
pub mod parse;
fn main() {
fasm_codegen(Expr::MathExpr(Math { left: 30, right: 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(4), operator: MathOperator::OP_DIV})]);
}

View file

@ -1,6 +1,6 @@
#[derive(Debug)]
pub enum Expr<'a> {
MathExpr(Math),
MathExpr(Math<'a>),
FunCall(FunCall<'a>),
FunDefenition(FunDefenition<'a>),
VarDefenition(VarDefenition<'a>),
@ -10,9 +10,9 @@ pub enum Expr<'a> {
// MATH EXPRESSION
#[derive(Debug)]
pub struct Math {
pub left: i64,
pub right: i64,
pub struct Math<'a> {
pub left: Value<'a>,
pub right: Value<'a>,
pub operator: MathOperator
}
@ -30,7 +30,7 @@ pub enum MathOperator {
#[derive(Debug)]
pub struct FunCall<'a> {
pub name: &'a str,
pub params: Vec<FunParamCall>,
pub params: Vec<FunParamCall<'a>>,
}
#[derive(Debug)]
@ -47,9 +47,8 @@ pub struct FunParamDef<'a> {
}
#[derive(Debug)]
pub struct FunParamCall {
// Everything is a u64 for now.
pub value: u64,
pub struct FunParamCall<'a> {
pub value: Value<'a>,
}
// VARIABLES
@ -57,10 +56,16 @@ pub struct FunParamCall {
#[derive(Debug)]
pub struct VarDefenition<'a> {
pub name: &'a str,
pub value: u64,
pub value: Value<'a>,
}
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct VarReference<'a> {
pub name: &'a str,
}
#[derive(Debug, Copy, Clone)]
pub enum Value<'a> {
Var(VarReference<'a>),
Number(u64),
}