-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcode_generator_implementation.cc
More file actions
224 lines (204 loc) · 6.47 KB
/
code_generator_implementation.cc
File metadata and controls
224 lines (204 loc) · 6.47 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "implementation.hh"
#include <iostream>
#include <sstream>
std::string number_expression::get_code() const {
std::stringstream ss;
ss << "mov eax," << value << std::endl;
return ss.str();
}
std::string boolean_expression::get_code() const {
std::stringstream ss;
ss << "mov al," << (value ? 1 : 0) << std::endl;
return ss.str();
}
std::string next_label() {
std::stringstream ss;
ss << "label" << id++;
return ss.str();
}
std::string symbol::get_code() {
std::stringstream ss;
ss << label << ": resb " << get_size() << "\t; variable: " << name << std::endl;
return ss.str();
}
int symbol::get_size() {
if(symbol_type == boolean) {
return 1;
} else {
return 4;
}
}
std::string get_register(type t) {
if(t == boolean) {
return "al";
} else {
return "eax";
}
}
std::string id_expression::get_code() const {
if(symbol_table.count(name) == 0) {
error(line, std::string("Undefined variable: ") + name);
}
return std::string("mov eax,[") + symbol_table[name].label + "]\n";
}
std::string operator_code(std::string op) {
std::stringstream ss;
if(op == "+") {
ss << "add eax,ecx" << std::endl;
} else if(op == "-") {
ss << "sub eax,ecx" << std::endl;
} else if(op == "*") {
ss << "xor edx,edx" << std::endl;
ss << "mul ecx" << std::endl;
} else if(op == "/") {
ss << "xor edx,edx" << std::endl;
ss << "div ecx" << std::endl;
} else if(op == "%") {
ss << "xor edx,edx" << std::endl;
ss << "div ecx" << std::endl;
ss << "mov eax,edx" << std::endl;
} else if(op == "<") {
ss << "cmp eax,ecx" << std::endl;
ss << "mov al,0" << std::endl;
ss << "mov cx,1" << std::endl;
ss << "cmovb ax,cx" << std::endl;
} else if(op == "<=") {
ss << "cmp eax,ecx" << std::endl;
ss << "mov al,0" << std::endl;
ss << "mov cx,1" << std::endl;
ss << "cmovbe ax,cx" << std::endl;
} else if(op == ">") {
ss << "cmp eax,ecx" << std::endl;
ss << "mov al,0" << std::endl;
ss << "mov cx,1" << std::endl;
ss << "cmova ax,cx" << std::endl;
} else if(op == ">=") {
ss << "cmp eax,ecx" << std::endl;
ss << "mov al,0" << std::endl;
ss << "mov cx,1" << std::endl;
ss << "cmovae ax,cx" << std::endl;
} else if(op == "and") {
ss << "cmp al,1" << std::endl;
ss << "cmove ax,cx" << std::endl;
} else if(op == "or") {
ss << "cmp al,0" << std::endl;
ss << "cmove ax,cx" << std::endl;
} else {
error(-1, std::string("Bug: Unsupported binary operator: ") + op);
}
return ss.str();
}
std::string eq_code(type t) {
std::stringstream ss;
if(t == natural) {
ss << "cmp eax,ecx" << std::endl;
} else {
ss << "cmp al,cl" << std::endl;
}
ss << "mov al,0" << std::endl;
ss << "mov cx,1" << std::endl;
ss << "cmove ax,cx" << std::endl;
return ss.str();
}
std::string binop_expression::get_code() const {
std::stringstream ss;
ss << left->get_code();
ss << "push eax" << std::endl;
ss << right->get_code();
ss << "mov ecx,eax" << std::endl;
ss << "pop eax" << std::endl;
ss << (op == "=" ? eq_code(left->get_type()) : operator_code(op));
return ss.str();
}
std::string not_expression::get_code() const {
std::stringstream ss;
ss << operand->get_code();
ss << "xor al,1" << std::endl;
return ss.str();
}
std::string assign_instruction::get_code() {
std::stringstream ss;
ss << right->get_code();
ss << "mov [" + symbol_table[left].label + "]," << get_register(symbol_table[left].symbol_type) << std::endl;
return ss.str();
}
std::string get_type_name(type t) {
if(t == boolean) {
return "boolean";
} else {
return "natural";
}
}
std::string read_instruction::get_code() {
type t = symbol_table[id].symbol_type;
std::stringstream ss;
ss << "call read_" << get_type_name(t) << std::endl;
ss << "mov [" << symbol_table[id].label << "]," << get_register(t) << std::endl;
return ss.str();
}
std::string write_instruction::get_code() {
std::stringstream ss;
ss << exp->get_code();
if(exp_type == boolean) {
ss << "and eax,1" << std::endl;
}
ss << "push eax" << std::endl;
ss << "call write_" << get_type_name(exp_type) << std::endl;
ss << "add esp,4" << std::endl;
return ss.str();
}
std::string if_instruction::get_code() {
std::string else_label = next_label();
std::string end_label = next_label();
std::stringstream ss;
ss << condition->get_code();
ss << "cmp al,1" << std::endl;
ss << "jne near " << else_label << std::endl;
generate_code_of_commands(ss, true_branch);
ss << "jmp " << end_label << std::endl;
ss << else_label << ":" << std::endl;
generate_code_of_commands(ss, false_branch);
ss << end_label << ":" << std::endl;
return ss.str();
}
std::string while_instruction::get_code() {
std::string begin_label = next_label();
std::string end_label = next_label();
std::stringstream ss;
ss << begin_label << ":" << std::endl;
ss << condition->get_code();
ss << "cmp al,1" << std::endl;
ss << "jne near " << end_label << std::endl;
generate_code_of_commands(ss, body);
ss << "jmp " << begin_label << std::endl;
ss << end_label << ":" << std::endl;
return ss.str();
}
void generate_code_of_commands(std::ostream& out, std::list<instruction*>* commands) {
if(!commands) {
return;
}
std::list<instruction*>::iterator it;
for(it = commands->begin(); it != commands->end(); ++it) {
out << (*it)->get_code();
}
}
void generate_code(std::list<instruction*>* commands) {
std::cout << "global main" << std::endl;
std::cout << "extern write_natural" << std::endl;
std::cout << "extern read_natural" << std::endl;
std::cout << "extern write_boolean" << std::endl;
std::cout << "extern read_boolean" << std::endl;
std::cout << std::endl;
std::cout << "section .bss" << std::endl;
std::map<std::string,symbol>::iterator it;
for(it = symbol_table.begin(); it != symbol_table.end(); ++it) {
std::cout << it->second.get_code();
}
std::cout << std::endl;
std::cout << "section .text" << std::endl;
std::cout << "main:" << std::endl;
generate_code_of_commands(std::cout, commands);
std::cout << "xor eax,eax" << std::endl;
std::cout << "ret" << std::endl;
}