added return from procedure after function, and made FunCall.params
a Vec<Value<'a
>
This commit is contained in:
parent
fdb04a1a43
commit
e749f579a4
|
@ -58,42 +58,42 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
|
|||
match i {
|
||||
0 => {
|
||||
// First parameter. Put in %rdi.
|
||||
asm_start.push_str(format!("\tmov rdi, {}\n", p.value.unwrap()).as_str());
|
||||
asm_start.push_str(format!("\tmov rdi, {}\n", p.unwrap()).as_str());
|
||||
},
|
||||
|
||||
1 => {
|
||||
// Second parameter. Put in %rsi.
|
||||
asm_start.push_str(format!("\tmov rsi, {}\n", p.value.unwrap()).as_str());
|
||||
asm_start.push_str(format!("\tmov rsi, {}\n", p.unwrap()).as_str());
|
||||
},
|
||||
|
||||
2 => {
|
||||
// Third parameter. Put in %rdx.
|
||||
asm_start.push_str(format!("\tmov rdx, {}\n", p.value.unwrap()).as_str());
|
||||
asm_start.push_str(format!("\tmov rdx, {}\n", p.unwrap()).as_str());
|
||||
},
|
||||
|
||||
3 => {
|
||||
// Fourth parameter. Put in %rcx.
|
||||
asm_start.push_str(format!("\tmov rcx, {}\n", p.value.unwrap()).as_str());
|
||||
asm_start.push_str(format!("\tmov rcx, {}\n", p.unwrap()).as_str());
|
||||
},
|
||||
|
||||
4 => {
|
||||
// Fifth parameter. Put in %r8.
|
||||
asm_start.push_str(format!("\tmov r8, {}\n", p.value.unwrap()).as_str());
|
||||
asm_start.push_str(format!("\tmov r8, {}\n", p.unwrap()).as_str());
|
||||
},
|
||||
|
||||
5 => {
|
||||
// Sixth parameter. Put in %r9.
|
||||
asm_start.push_str(format!("\tmov r9, {}\n", p.value.unwrap()).as_str());
|
||||
asm_start.push_str(format!("\tmov r9, {}\n", p.unwrap()).as_str());
|
||||
},
|
||||
|
||||
_ => {
|
||||
// Parameters after the sixth parameter are pushed to the stack.
|
||||
asm_start.push_str(format!("\tpush {}\n", p.value.unwrap()).as_str());
|
||||
asm_start.push_str(format!("\tpush {}\n", p.unwrap()).as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
asm_start.push_str(format!("call {}", e.name).as_str());
|
||||
asm_start.push_str(format!("\tcall {}\n", e.name).as_str());
|
||||
},
|
||||
|
||||
Expr::VarDefinition(e) => {
|
||||
|
@ -111,6 +111,7 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
|
|||
Expr::FunDefinition(e) => {
|
||||
asm_text.push_str(format!("{}:\n", e.name).as_str());
|
||||
asm_text.push_str(fasm_codegen(&e.contents, false).as_str());
|
||||
asm_text.push_str("\tret\n");
|
||||
}
|
||||
|
||||
_ => break,
|
||||
|
@ -118,12 +119,12 @@ pub fn fasm_codegen(exprs: &Vec<Expr>, not_a_function: bool) -> String {
|
|||
|
||||
}
|
||||
|
||||
if not_a_function {
|
||||
asm_start.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n");
|
||||
asm_start.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n");
|
||||
asm_start.push_str("\tsyscall ; this is the instruction to actually perform the system call.\n");
|
||||
}
|
||||
|
||||
if not_a_function {
|
||||
asm_start.push_str("\tmov rax, 60 ; 60 is the system call number for exit.\n");
|
||||
asm_start.push_str("\txor rdi, rdi ; 0 is the exit code we want.\n");
|
||||
asm_start.push_str("\tsyscall ; this is the instruction to actually perform the system call.\n");
|
||||
}
|
||||
let asm = format!("{}{}{}", asm_start, asm_text, asm_data);
|
||||
asm
|
||||
}
|
||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -18,16 +18,25 @@ fn main() {
|
|||
),
|
||||
Expr::FunDefinition(FunDefinition {
|
||||
name: "adder", contents: vec![
|
||||
Expr::Return(Value::Math(
|
||||
Expr::MathExpr(
|
||||
Math {
|
||||
left: &Value::Param(ParamReference {param_number: 0}),
|
||||
right: &Value::Param(ParamReference {param_number: 1}),
|
||||
operator: MathOperator::OP_ADD
|
||||
}
|
||||
))
|
||||
)
|
||||
]
|
||||
}),
|
||||
Expr::Breakpoint],
|
||||
|
||||
Expr::FunCall(
|
||||
FunCall {
|
||||
name: "adder",
|
||||
params: vec![Value::Var(VarReference {name: "goren"}), Value::Number(6)]
|
||||
}
|
||||
),
|
||||
|
||||
Expr::Breakpoint
|
||||
],
|
||||
true
|
||||
);
|
||||
println!("{}", fc);
|
||||
|
|
|
@ -31,7 +31,7 @@ pub enum MathOperator {
|
|||
#[derive(Debug)]
|
||||
pub struct FunCall<'a> {
|
||||
pub name: &'a str,
|
||||
pub params: Vec<FunParamCall<'a>>,
|
||||
pub params: Vec<Value<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -75,7 +75,6 @@ pub enum Value<'a> {
|
|||
Var(VarReference<'a>),
|
||||
Param(ParamReference),
|
||||
Number(u64),
|
||||
Math(Math<'a>),
|
||||
}
|
||||
|
||||
impl<'a> Value<'a> {
|
||||
|
@ -100,10 +99,6 @@ impl<'a> Value<'a> {
|
|||
Value::Var(e) => {
|
||||
return format!("[{}]", e.name.to_string());
|
||||
},
|
||||
|
||||
Value::Math(e) => {
|
||||
return String::from("rax");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue