-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
162 lines (148 loc) · 5.51 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::collections::HashMap;
use crate::{
ast::model::*,
lex::{BinaryOperator, UnaryOperator},
utility::SyntaxError,
};
static FUNCTION_PROLOGUE_START: &str = "push\t%rbp\n\tmov\t%rsp, %rbp";
static FUNCTION_PROLOGUE_END: &str = "mov\t%rbp, %rsp\n\tpop\t%rbp\n\tret\n";
pub struct CodeGenerator {
symbol_table: HashMap<String, i64>,
stack_index: i64,
clause_count: u64,
}
impl CodeGenerator {
pub fn new() -> Self {
Self {
symbol_table: HashMap::new(),
stack_index: 0,
clause_count: 0,
}
}
pub fn generate(&mut self, root: &Program) -> Result<String, SyntaxError> {
self.generate_function(&root.func)
}
fn generate_clause(&mut self) -> String {
self.clause_count += 1;
format!("_clause{}", self.clause_count)
}
fn generate_end(&self) -> String {
format!("_end{}", self.clause_count)
}
fn generate_function(&mut self, func: &Function) -> Result<String, SyntaxError> {
let mut body = vec![];
let mut return_flag = false;
for x in &func.body {
let mut st = self.generate_statement(x)?;
st = st.replace("\n", "\n\t");
match x {
Statement::Return(_) => {
st = format!("{}\n\t{}", st, FUNCTION_PROLOGUE_END);
return_flag = true;
}
_ => {}
}
body.push(st);
}
let end = if return_flag {
format!("")
} else {
format!("mov\t$0, %rax\n\t{}", FUNCTION_PROLOGUE_END)
};
Ok(format!(
"
\t.globl {name}
{name}:
\t{start}
\t{body_text}
\t{end}
",
start = FUNCTION_PROLOGUE_START,
name = func.name,
body_text = body.join("\n\t")
))
}
fn generate_statement(&mut self, st: &Statement) -> Result<String, SyntaxError> {
match st {
Statement::Return(val) => {
let exp = self.generate_expression(val)?;
Ok(format!("{}", exp))
}
Statement::Declare { name, exp } => {
if self.symbol_table.contains_key(name.as_ref()) {
Err(SyntaxError::new_codegen_error(
format!("re-declaration of variable {}", name).to_string(),
))
} else {
let mut assembly_exp = self.generate_expression(&Expression::Const(0))?;
if let Some(exp_some) = exp {
assembly_exp = self.generate_expression(exp_some)?;
};
self.stack_index -= 8;
self.symbol_table.insert(name.to_string(), self.stack_index);
Ok(format!("{}\npush\t%rax", assembly_exp))
}
}
Statement::Exp(val) => self.generate_expression(val),
}
}
fn generate_expression(&mut self, exp: &Expression) -> Result<String, SyntaxError> {
match exp {
Expression::Const(val) => Ok(format!("mov\t${}, %rax", val.to_string())),
Expression::Unary { op, exp } => {
let inner_exp = self.generate_expression(exp)?;
let ext_exp = match op {
UnaryOperator::Negation => format!("neg\t%rax"),
UnaryOperator::BitwiseComplement => format!("not\t%rax"),
UnaryOperator::LogicalNegation => {
format!("cmp\t$0, %rax\nmov\t$0, %rax\nsete\t%al")
}
};
Ok(format!("{}\n{}", inner_exp, ext_exp))
}
Expression::Binary { exp1, op, exp2 } => {
let exp1 = self.generate_expression(exp1)?;
let exp2 = self.generate_expression(exp2)?;
let inner_exp = format!("{}\npush\t%rax\n{}\npop\t%rcx", exp1, exp2);
let ext_exp = match op {
BinaryOperator::Addition => format!("add\t%rcx, %rax"),
BinaryOperator::Multiplication => format!("imul\t%rcx, %rax"),
BinaryOperator::Minus => format!("sub\t%rax, %rcx\nmov\t%rcx, %rax"),
BinaryOperator::Division => {
format!("mov\t%rax, %rbx\nmov\t%rcx, %rax\ncqo\nidiv\t%rbx")
}
BinaryOperator::Equal => format!("cmp\t%rax, %rcx\nsete\t%al"),
BinaryOperator::NotEqual => {
format!("cmp\t%rax, %rcx\ncmp\t$0, %rax\nsetne\t%al")
}
BinaryOperator::LessThan => format!("cmp\t%rax, %rcx\nsetl\t%al"),
BinaryOperator::LessThanOrEqual => format!("cmp\t%rax, %rcx\nsetle\t%al"),
BinaryOperator::GreaterThan => format!("cmp\t%rax, %rcx\nsetg\t%al"),
BinaryOperator::GreaterThanOrEqual => format!("cmp\t%rax, %rcx\nsetge\t%al"),
BinaryOperator::And => {
return Ok(format!("{}\ncmp\t$0, %rax\njne\t{_clause2}\njmp\t{_end}\n{_clause2}:\n{}\ncmp\t$0, %rax\ncmp $0, %rax\nsetne\t%al\n{_end}:", exp1, exp2, _clause2 = self.generate_clause(), _end = self.generate_end()));
}
BinaryOperator::Or => {
return Ok(format!("{}\ncmp\t$0, %rax\nje\t{_clause2}\nmov\t$1, %rax\njmp\t{_end}\n{_clause2}:\n{}\ncmp\t$0, %rax\ncmp $0, %rax\nsetne\t%al\n{_end}:", exp1, exp2, _clause2 = self.generate_clause(), _end = self.generate_end()));
}
};
Ok(format!("{}\n{}", inner_exp, ext_exp))
}
Expression::Assign { name, exp } => match self.symbol_table.get(name.as_ref()) {
None => Err(SyntaxError::new_codegen_error(
format!("variable not declared {}", name).to_string(),
)),
Some(&offset) => {
let assign_exp = self.generate_expression(exp)?;
Ok(format!("{}\nmov\t%rax, {}(%rbp)", assign_exp, offset))
}
},
Expression::Var { name } => match self.symbol_table.get(name.as_ref()) {
None => Err(SyntaxError::new_codegen_error(
format!("variable not declared {}", name).to_string(),
)),
Some(&offset) => Ok(format!("mov\t{}(%rbp), %rax", offset)),
},
}
}
}