added breakpoint
This commit is contained in:
parent
d7c744bc77
commit
ba3455fcff
|
@ -1,17 +1,5 @@
|
||||||
use crate::parse::ast::*;
|
use crate::parse::ast::*;
|
||||||
|
|
||||||
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 {
|
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();
|
||||||
|
@ -24,8 +12,8 @@ pub fn fasm_codegen(exprs: Vec<Expr>) -> String {
|
||||||
for expr in exprs.iter() {
|
for expr in exprs.iter() {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::MathExpr(e) => {
|
Expr::MathExpr(e) => {
|
||||||
asm_text.push_str(format!("\tmov r10, {}\n", parse_value(e.left)).as_str());
|
asm_text.push_str(format!("\tmov r10, {}\n", e.left.unwrap()).as_str());
|
||||||
asm_text.push_str(format!("\tmov r11, {}\n", parse_value(e.right)).as_str());
|
asm_text.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 => {
|
||||||
|
@ -65,52 +53,56 @@ 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", parse_value(p.value)).as_str());
|
asm_text.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", parse_value(p.value)).as_str());
|
asm_text.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", parse_value(p.value)).as_str());
|
asm_text.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", parse_value(p.value)).as_str());
|
asm_text.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", parse_value(p.value)).as_str());
|
asm_text.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", parse_value(p.value)).as_str());
|
asm_text.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", parse_value(p.value)).as_str());
|
asm_text.push_str(format!("\tpush {}\n", p.value.unwrap()).as_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
asm_text.push_str(format!("call {:?}", e.name).as_str());
|
asm_text.push_str(format!("call {}", e.name).as_str());
|
||||||
},
|
},
|
||||||
|
|
||||||
Expr::VarDefenition(e) => {
|
Expr::VarDefenition(e) => {
|
||||||
asm_data.push_str(format!("\t{} db {:?}", e.name, parse_value(e.value)).as_str());
|
asm_data.push_str(format!("\t{} dq {}", e.name, e.value.unwrap()).as_str());
|
||||||
},
|
},
|
||||||
|
|
||||||
Expr::VarReference(e) => {
|
Expr::VarReference(e) => {
|
||||||
asm_text.push_str(e.name);
|
asm_text.push_str(e.name);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Expr::Breakpoint => {
|
||||||
|
asm_text.push_str("\tint3\n");
|
||||||
|
},
|
||||||
|
|
||||||
_ => unimplemented!("sorry unimplemented"),
|
_ => unimplemented!("sorry unimplemented"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,5 @@ use crate::parse::ast::*;
|
||||||
pub mod parse;
|
pub mod parse;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
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})]);
|
fasm_codegen(vec![Expr::VarDefenition(VarDefenition {name: "goren", value: Value::Number(10)}), Expr::MathExpr(Math { left: Value::Var(VarReference { name: "goren"}), right: Value::Number(17), operator: MathOperator::OP_MOD}), Expr::Breakpoint]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ pub enum Expr<'a> {
|
||||||
FunDefenition(FunDefenition<'a>),
|
FunDefenition(FunDefenition<'a>),
|
||||||
VarDefenition(VarDefenition<'a>),
|
VarDefenition(VarDefenition<'a>),
|
||||||
VarReference(VarReference<'a>),
|
VarReference(VarReference<'a>),
|
||||||
|
Breakpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
// MATH EXPRESSION
|
// MATH EXPRESSION
|
||||||
|
@ -69,3 +70,17 @@ pub enum Value<'a> {
|
||||||
Var(VarReference<'a>),
|
Var(VarReference<'a>),
|
||||||
Number(u64),
|
Number(u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> Value<'a> {
|
||||||
|
pub fn unwrap(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Value::Number(e) => {
|
||||||
|
return e.to_string();
|
||||||
|
},
|
||||||
|
|
||||||
|
Value::Var(e) => {
|
||||||
|
return format!("[{}]", e.name.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue