continued beefing up the backend
This commit is contained in:
parent
ffb76dd38e
commit
2f20415224
|
@ -1,102 +0,0 @@
|
|||
use crate::parse::ast::*;
|
||||
|
||||
pub fn fasm_codegen(expr: 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("\tmov rdx, r11\n");
|
||||
asm_text.push_str("\tidiv r10, r11\n");
|
||||
asm_text.push_str("\tmov rax, r10\n");
|
||||
},
|
||||
// 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("\tmov rdx, r11\n");
|
||||
asm_text.push_str("\tidiv r10, r11\n");
|
||||
asm_text.push_str("\tmov rax, rdx\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());
|
||||
},
|
||||
|
||||
1 => {
|
||||
// Second parameter. Put in %rsi.
|
||||
asm_text.push_str(format!("\tmov rsi, {:?}\n", p.value).as_str());
|
||||
},
|
||||
|
||||
2 => {
|
||||
// Third parameter. Put in %rdx.
|
||||
asm_text.push_str(format!("\tmov rdx, {:?}\n", p.value).as_str());
|
||||
},
|
||||
|
||||
3 => {
|
||||
// Fourth parameter. Put in %rcx.
|
||||
asm_text.push_str(format!("\tmov rcx, {:?}\n", p.value).as_str());
|
||||
},
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
asm_text.push_str(format!("call {:?}", e.name).as_str());
|
||||
},
|
||||
|
||||
Expr::VarDefenition(e) => {
|
||||
asm_data.push_str(format!("{} dq {:?}", e.name, e.value).as_str());
|
||||
},
|
||||
|
||||
_ => unimplemented!("sorry unimplemented"),
|
||||
}
|
||||
let asm = format!("{}{}", asm_text, asm_data);
|
||||
println!("{}", asm);
|
||||
asm
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
goren@goren.13806:1700158488
|
|
@ -27,9 +27,9 @@ pub fn fasm_codegen(expr: Expr) -> String {
|
|||
// If the operator is division.
|
||||
MathOperator::OP_DIV => {
|
||||
asm_text.push_str("\tmov rax, r10\n");
|
||||
asm_text.push_str("\tmov rdx, r11\n");
|
||||
asm_text.push_str("\tidiv r10, r11\n");
|
||||
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 => {
|
||||
|
@ -39,10 +39,10 @@ pub fn fasm_codegen(expr: Expr) -> String {
|
|||
// If the operator is modulo.
|
||||
MathOperator::OP_MOD => {
|
||||
asm_text.push_str("\tmov rax, r10\n");
|
||||
asm_text.push_str("\tmov rdx, r11\n");
|
||||
asm_text.push_str("\tidiv r10, r11\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.
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -91,11 +91,18 @@ pub fn fasm_codegen(expr: Expr) -> String {
|
|||
},
|
||||
|
||||
Expr::VarDefenition(e) => {
|
||||
asm_data.push_str(format!("{} dq {:?}", e.name, e.value).as_str());
|
||||
asm_data.push_str(format!("\t{} dq {:?}", e.name, e.value).as_str());
|
||||
},
|
||||
|
||||
Expr::VarReference(e) => {
|
||||
asm_text.push_str(e.name);
|
||||
},
|
||||
|
||||
_ => 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.");
|
||||
let asm = format!("{}{}", asm_text, asm_data);
|
||||
println!("{}", asm);
|
||||
asm
|
||||
|
|
|
@ -5,8 +5,5 @@ use crate::parse::ast::*;
|
|||
pub mod parse;
|
||||
|
||||
fn main() {
|
||||
fasm_codegen(Expr::VarDefenition(VarDefenition {
|
||||
name: "hi",
|
||||
value: 100,
|
||||
}));
|
||||
fasm_codegen(Expr::MathExpr(Math { left: 30, right: 4, operator: MathOperator::OP_DIV}));
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ pub enum Expr<'a> {
|
|||
FunCall(FunCall<'a>),
|
||||
FunDefenition(FunDefenition<'a>),
|
||||
VarDefenition(VarDefenition<'a>),
|
||||
VarReference(VarReference<'a>),
|
||||
}
|
||||
|
||||
// MATH EXPRESSION
|
||||
|
@ -51,8 +52,15 @@ pub struct FunParamCall {
|
|||
pub value: u64,
|
||||
}
|
||||
|
||||
// VARIABLES
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VarDefenition<'a> {
|
||||
pub name: &'a str,
|
||||
pub value: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VarReference<'a> {
|
||||
pub name: &'a str,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue