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,18 +1,31 @@
use crate::parse::ast::*; 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_text = String::new();
let mut asm_data = String::new(); let mut asm_data = String::new();
asm_text.push_str("format ELF64 executable 3\n"); asm_text.push_str("format ELF64 executable 3\n");
asm_text.push_str("segment readable executable\n"); asm_text.push_str("segment readable executable\n");
asm_text.push_str("entry _start\n"); asm_text.push_str("entry _start\n");
asm_text.push_str("_start:\n"); asm_text.push_str("_start:\n");
asm_data.push_str("segment readable writeable\n"); asm_data.push_str("\nsegment readable writable\n");
for expr in exprs.iter() {
match expr { match expr {
Expr::MathExpr(e) => { Expr::MathExpr(e) => {
asm_text.push_str(format!("\tmov r10, {:?}\n", e.left).as_str()); asm_text.push_str(format!("\tmov r10, {}\n", parse_value(e.left)).as_str());
asm_text.push_str(format!("\tmov r11, {:?}\n", e.right).as_str()); asm_text.push_str(format!("\tmov r11, {}\n", parse_value(e.right)).as_str());
match e.operator { match e.operator {
// If the operator is addition. // If the operator is addition.
MathOperator::OP_ADD => { MathOperator::OP_ADD => {
@ -52,37 +65,37 @@ pub fn fasm_codegen(expr: Expr) -> String {
match i { match i {
0 => { 0 => {
// First parameter. Put in %rdi. // First parameter. Put in %rdi.
asm_text.push_str(format!("\tmov rdi, {:?}\n", p.value).as_str()); asm_text.push_str(format!("\tmov rdi, {:?}\n", parse_value(p.value)).as_str());
}, },
1 => { 1 => {
// Second parameter. Put in %rsi. // Second parameter. Put in %rsi.
asm_text.push_str(format!("\tmov rsi, {:?}\n", p.value).as_str()); asm_text.push_str(format!("\tmov rsi, {:?}\n", parse_value(p.value)).as_str());
}, },
2 => { 2 => {
// Third parameter. Put in %rdx. // Third parameter. Put in %rdx.
asm_text.push_str(format!("\tmov rdx, {:?}\n", p.value).as_str()); asm_text.push_str(format!("\tmov rdx, {:?}\n", parse_value(p.value)).as_str());
}, },
3 => { 3 => {
// Fourth parameter. Put in %rcx. // Fourth parameter. Put in %rcx.
asm_text.push_str(format!("\tmov rcx, {:?}\n", p.value).as_str()); asm_text.push_str(format!("\tmov rcx, {:?}\n", parse_value(p.value)).as_str());
}, },
4 => { 4 => {
// Fifth parameter. Put in %r8. // Fifth parameter. Put in %r8.
asm_text.push_str(format!("\tmov r8, {:?}\n", p.value).as_str()); asm_text.push_str(format!("\tmov r8, {:?}\n", parse_value(p.value)).as_str());
}, },
5 => { 5 => {
// Sixth parameter. Put in %r9. // Sixth parameter. Put in %r9.
asm_text.push_str(format!("\tmov r9, {:?}\n", p.value).as_str()); asm_text.push_str(format!("\tmov r9, {:?}\n", parse_value(p.value)).as_str());
}, },
_ => { _ => {
// Parameters after the sixth parameter are pushed to the stack. // Parameters after the sixth parameter are pushed to the stack.
asm_text.push_str(format!("\tpush {:?}\n", p.value).as_str()); asm_text.push_str(format!("\tpush {:?}\n", parse_value(p.value)).as_str());
} }
} }
} }
@ -91,7 +104,7 @@ pub fn fasm_codegen(expr: Expr) -> String {
}, },
Expr::VarDefenition(e) => { Expr::VarDefenition(e) => {
asm_data.push_str(format!("\t{} dq {:?}", e.name, e.value).as_str()); asm_data.push_str(format!("\t{} db {:?}", e.name, parse_value(e.value)).as_str());
}, },
Expr::VarReference(e) => { Expr::VarReference(e) => {
@ -100,6 +113,8 @@ pub fn fasm_codegen(expr: Expr) -> String {
_ => 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("\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("\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."); 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 lex;
pub mod codegen; pub mod codegen;
use crate::codegen::fasm::*; use crate::codegen::fasm::*;
@ -5,5 +7,5 @@ use crate::parse::ast::*;
pub mod parse; pub mod parse;
fn main() { 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)] #[derive(Debug)]
pub enum Expr<'a> { pub enum Expr<'a> {
MathExpr(Math), MathExpr(Math<'a>),
FunCall(FunCall<'a>), FunCall(FunCall<'a>),
FunDefenition(FunDefenition<'a>), FunDefenition(FunDefenition<'a>),
VarDefenition(VarDefenition<'a>), VarDefenition(VarDefenition<'a>),
@ -10,9 +10,9 @@ pub enum Expr<'a> {
// MATH EXPRESSION // MATH EXPRESSION
#[derive(Debug)] #[derive(Debug)]
pub struct Math { pub struct Math<'a> {
pub left: i64, pub left: Value<'a>,
pub right: i64, pub right: Value<'a>,
pub operator: MathOperator pub operator: MathOperator
} }
@ -30,7 +30,7 @@ pub enum MathOperator {
#[derive(Debug)] #[derive(Debug)]
pub struct FunCall<'a> { pub struct FunCall<'a> {
pub name: &'a str, pub name: &'a str,
pub params: Vec<FunParamCall>, pub params: Vec<FunParamCall<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -47,9 +47,8 @@ pub struct FunParamDef<'a> {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct FunParamCall { pub struct FunParamCall<'a> {
// Everything is a u64 for now. pub value: Value<'a>,
pub value: u64,
} }
// VARIABLES // VARIABLES
@ -57,10 +56,16 @@ pub struct FunParamCall {
#[derive(Debug)] #[derive(Debug)]
pub struct VarDefenition<'a> { pub struct VarDefenition<'a> {
pub name: &'a str, pub name: &'a str,
pub value: u64, pub value: Value<'a>,
} }
#[derive(Debug)] #[derive(Debug, Copy, Clone)]
pub struct VarReference<'a> { pub struct VarReference<'a> {
pub name: &'a str, pub name: &'a str,
} }
#[derive(Debug, Copy, Clone)]
pub enum Value<'a> {
Var(VarReference<'a>),
Number(u64),
}