THIS DOES NOT COMPILE, but i still want to be open source

This commit is contained in:
Goren Barak 2023-11-19 17:33:17 -05:00
parent 518ce7b3ad
commit af2785eabf
4 changed files with 77 additions and 55 deletions

View file

@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
[profile.dev]
debug=true

View file

@ -1,49 +1,54 @@
use crate::parse::ast::*; use crate::parse::ast::*;
pub fn fasm_codegen(exprs: Vec<Expr>) -> String { pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> 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"); let mut asm_start = String::new();
asm_text.push_str("segment readable executable\n"); if not_a_function {
asm_text.push_str("entry _start\n"); asm_start.push_str("format ELF64 executable 3\n");
asm_text.push_str("_start:\n"); asm_start.push_str("segment readable executable\n");
asm_data.push_str("\nsegment readable writable\n"); asm_start.push_str("entry _start\n");
asm_start.push_str("_start:\n");
asm_data.push_str("\nsegment readable writable\n");
}
let topush = if not_a_function { &mut asm_start } else { &mut asm_text };
for expr in exprs.iter() { for mut expr in exprs.iter() {
match expr { match &mut expr {
Expr::MathExpr(e) => { Expr::MathExpr(e) => {
asm_text.push_str(format!("\tmov r10, {}\n", e.left.unwrap()).as_str()); topush.push_str(format!("\tmov r10, {}\n", e.left.unwrap()).as_str());
asm_text.push_str(format!("\tmov r11, {}\n", e.right.unwrap()).as_str()); topush.push_str(format!("\tmov r11, {}\n", e.right.unwrap()).as_str());
match e.operator { match e.operator {
// If the operator is addition. // If the operator is addition.
MathOperator::OP_ADD => { MathOperator::OP_ADD => {
asm_text.push_str("\tadd r10, r11\n"); topush.push_str("\tadd r10, r11\n");
asm_text.push_str("\tmov rax, r10\n"); topush.push_str("\tmov rax, r10\n");
}, },
// If the operator is multiplication. // If the operator is multiplication.
MathOperator::OP_MULT => { MathOperator::OP_MULT => {
asm_text.push_str("\timul r10, r11\n"); topush.push_str("\timul r10, r11\n");
asm_text.push_str("\tmov rax, r10\n"); topush.push_str("\tmov rax, r10\n");
}, },
// If the operator is division. // If the operator is division.
MathOperator::OP_DIV => { MathOperator::OP_DIV => {
asm_text.push_str("\tmov rax, r10\n"); topush.push_str("\tmov rax, r10\n");
asm_text.push_str("\txor rdx, rdx\n"); topush.push_str("\txor rdx, rdx\n");
asm_text.push_str("\tidiv r11\n"); topush.push_str("\tidiv r11\n");
// The quotient is now stored in %rax. // The quotient is now stored in %rax.
}, },
// If the operators is subtraction. // If the operators is subtraction.
MathOperator::OP_SUB => { MathOperator::OP_SUB => {
asm_text.push_str("\tsub r10, r11\n"); topush.push_str("\tsub r10, r11\n");
asm_text.push_str("\tmov rax, r10\n"); topush.push_str("\tmov rax, r10\n");
}, },
// If the operator is modulo. // If the operator is modulo.
MathOperator::OP_MOD => { MathOperator::OP_MOD => {
asm_text.push_str("\tmov rax, r10\n"); topush.push_str("\tmov rax, r10\n");
asm_text.push_str("\txor rdx, rdx\n"); topush.push_str("\txor rdx, rdx\n");
asm_text.push_str("\tidiv r11\n"); topush.push_str("\tidiv r11\n");
asm_text.push_str("\tmov rax, rdx\n"); topush.push_str("\tmov rax, rdx\n");
// The remainder will now be stored in the %rax register. // The remainder will now be stored in the %rax register.
} }
} }
}, },
@ -53,64 +58,72 @@ pub fn fasm_codegen(exprs: Vec<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.unwrap()).as_str()); topush.push_str(format!("\tmov rdi, {}\n", p.value.unwrap()).as_str());
}, },
1 => { 1 => {
// Second parameter. Put in %rsi. // Second parameter. Put in %rsi.
asm_text.push_str(format!("\tmov rsi, {}\n", p.value.unwrap()).as_str()); topush.push_str(format!("\tmov rsi, {}\n", p.value.unwrap()).as_str());
}, },
2 => { 2 => {
// Third parameter. Put in %rdx. // Third parameter. Put in %rdx.
asm_text.push_str(format!("\tmov rdx, {}\n", p.value.unwrap()).as_str()); topush.push_str(format!("\tmov rdx, {}\n", p.value.unwrap()).as_str());
}, },
3 => { 3 => {
// Fourth parameter. Put in %rcx. // Fourth parameter. Put in %rcx.
asm_text.push_str(format!("\tmov rcx, {}\n", p.value.unwrap()).as_str()); topush.push_str(format!("\tmov rcx, {}\n", p.value.unwrap()).as_str());
}, },
4 => { 4 => {
// Fifth parameter. Put in %r8. // Fifth parameter. Put in %r8.
asm_text.push_str(format!("\tmov r8, {}\n", p.value.unwrap()).as_str()); topush.push_str(format!("\tmov r8, {}\n", p.value.unwrap()).as_str());
}, },
5 => { 5 => {
// Sixth parameter. Put in %r9. // Sixth parameter. Put in %r9.
asm_text.push_str(format!("\tmov r9, {}\n", p.value.unwrap()).as_str()); topush.push_str(format!("\tmov r9, {}\n", p.value.unwrap()).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.unwrap()).as_str()); topush.push_str(format!("\tpush {}\n", p.value.unwrap()).as_str());
} }
} }
} }
asm_text.push_str(format!("call {}", e.name).as_str()); topush.push_str(format!("call {}", e.name).as_str());
}, },
Expr::VarDefinition(e) => { Expr::VarDefinition(e) => {
asm_data.push_str(format!("\t{} dq {}", e.name, e.value.unwrap()).as_str()); asm_data.push_str(format!("\t{} dq {}", e.name, e.value.unwrap()).as_str());
}, },
Expr::VarReference(e) => { Expr::FunDefinition(e) => {
asm_text.push_str(e.name); topush.push_str(format!("{}:\n", e.name).as_str());
topush.push_str(fasm_codegen(&e.contents, false).as_str());
}, },
Expr::Breakpoint => { Expr::Return(e) => {
asm_text.push_str("\tint3\n"); topush.push_str(format!("mov rax, {}", e.unwrap()).as_str());
}, },
Expr::Breakpoint => {
topush.push_str("\tint3\n");
},
_ => unimplemented!("sorry unimplemented"), _ => unimplemented!("sorry unimplemented"),
} }
} }
asm_text.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n"); if not_a_function {
asm_text.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n"); asm_start.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n");
asm_text.push_str("\tsyscall ; this is the instruction to actually perform the system call."); asm_start.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n");
let asm = format!("{}{}", asm_text, asm_data); asm_start.push_str("\tsyscall ; this is the instruction to actually perform the system call.");
}
let asm = format!("{}{}{}", asm_start, asm_text, asm_data);
println!("{}", asm); println!("{}", asm);
asm asm
} }

View file

@ -7,5 +7,5 @@ use crate::parse::ast::*;
pub mod parse; pub mod parse;
fn main() { fn main() {
fasm_codegen(vec![Expr::VarDefinition(VarDefinition {name: "goren", value: Value::Number(10)}), Expr::MathExpr(Math { left: Value::Var(VarReference { name: "goren"}), right: Value::Number(17), operator: MathOperator::OP_MULT}), Expr::Breakpoint]); fasm_codegen(&vec![Expr::VarDefinition(VarDefinition {name: "goren", value: Value::Number(10)}), Expr::MathExpr(Math { left: Value::Var(VarReference { name: "goren"}), right: Value::Number(17), operator: MathOperator::OP_MULT}), Expr::FunDefinition(FunDefinition {name: "adder", contents: vec![Expr::MathExpr(Math {left: Value::Param(ParamReference {param_number: 0}), right: Value::Param(ParamReference {param_number: 1}), operator: MathOperator::OP_ADD})]}), Expr::Breakpoint], true);
} }

View file

@ -4,7 +4,7 @@ pub enum Expr<'a> {
FunCall(FunCall<'a>), FunCall(FunCall<'a>),
FunDefinition(FunDefinition<'a>), FunDefinition(FunDefinition<'a>),
VarDefinition(VarDefinition<'a>), VarDefinition(VarDefinition<'a>),
VarReference(VarReference<'a>), Return(Value<'a>),
Breakpoint Breakpoint
} }
@ -36,10 +36,8 @@ pub struct FunCall<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct FunDefinition<'a> { pub struct FunDefinition<'a> {
name: &'a str, pub name: &'a str,
params: Vec<FunParamDef<'a>>, pub contents: Vec<Expr<'a>>,
contents: Vec<Expr<'a>>,
return_value: Value<'a>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -67,14 +65,14 @@ pub struct VarReference<'a> {
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct ParamReference<'a> { pub struct ParamReference {
pub param_number: u64, pub param_number: u64,
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum Value<'a> { pub enum Value<'a> {
Var(VarReference<'a>), Var(VarReference<'a>),
Param(ParamReference<'a>), Param(ParamReference),
Number(u64), Number(u64),
} }
@ -82,7 +80,15 @@ impl<'a> Value<'a> {
pub fn unwrap(&self) -> String { pub fn unwrap(&self) -> String {
match self { match self {
Value::Param(e) => { Value::Param(e) => {
match e.param_number {
0 => { return "rdi".to_string(); },
1 => { return "rsi".to_string(); },
2 => { return "rdx".to_string(); },
3 => { return "rcx".to_string(); },
4 => { return "r8".to_string(); },
5 => { return "r9".to_string(); },
_ => { unimplemented!() }
}
}, },
Value::Number(e) => { Value::Number(e) => {