forked from devaigergely81/flex-bison-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_generator_implementation.cc
More file actions
338 lines (303 loc) · 10.2 KB
/
code_generator_implementation.cc
File metadata and controls
338 lines (303 loc) · 10.2 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#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;
if (left->is_const_expr()) {
ss << "mov eax, " << left->get_value() << std::endl;
} else {
ss << left->get_code();
}
ss << "push eax" << std::endl;
if (right->is_const_expr()) {
ss << "mov ecx, " << right->get_value() << std::endl;
} else {
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 trinaryop_expression::get_code() const {
std::string else_label = next_label();
std::string end_label = next_label();
std::stringstream ss;
if (cond->is_const_expr()) {
ss << "mov al, " << cond->get_value() << std::endl;
} else {
ss << cond->get_code();
}
ss << "cmp al,1" << std::endl;
ss << "jne near " << else_label << std::endl;
if (left->is_const_expr()) {
ss << "mov eax, " << left->get_value() << std::endl;
} else {
ss << left->get_code();
}
ss << "jmp " << end_label << std::endl;
ss << else_label << ":" << std::endl;
if (right->is_const_expr()) {
ss << "mov eax, " << right->get_value() << std::endl;
} else {
ss << right->get_code();
}
ss << end_label << ":" << std::endl;
return ss.str();
}
std::string not_expression::get_code() const {
std::stringstream ss;
if (operand->is_const_expr()) {
ss << "mov al, " << operand->get_value() << std::endl;
} else {
ss << operand->get_code();
}
ss << "xor al,1" << std::endl;
return ss.str();
}
std::string assign_instructions::get_code() {
std::stringstream ss;
auto right_it = right->begin();
for (auto left_it = left->begin(); left_it != left->end(); ++left_it, ++right_it) {
if ((*right_it)->is_const_expr()) {
//do not consume stack space
} else {
ss << (*right_it)->get_code();
ss << "push eax" << std::endl;
}
}
auto rright_it = right->rbegin();
for (auto left_it = left->rbegin(); left_it != left->rend(); ++left_it, ++rright_it) {
if ((*rright_it)->is_const_expr()) {
ss << "mov " << get_register(symbol_table[*left_it].symbol_type) << ", " << (*rright_it)->get_value() << std::endl;
} else {
ss << "pop eax" << std::endl;
}
ss << "mov [" + symbol_table[*left_it].label + "]," << get_register(symbol_table[*left_it].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();
}
std::string for_instruction::get_code() {
std::string begin_label = next_label();
std::string end_label = next_label();
std::stringstream ss;
//loop unroll
if (lowerlimit->is_const_expr() && upperlimit->is_const_expr()) {
for (unsigned i = lowerlimit->get_value(); i < upperlimit->get_value(); ++i) {
ss << "mov eax, " << i << std::endl;
ss << "mov [" + symbol_table[id].label + "], eax" << std::endl;
generate_code_of_commands(ss, body);
ss << std::endl;
}
return ss.str();
}
if (lowerlimit->is_const_expr()) {
ss << "mov eax, " << lowerlimit->get_value() << std::endl;
} else {
ss << lowerlimit->get_code();
}
ss << "mov [" + symbol_table[id].label + "]," << get_register(symbol_table[id].symbol_type) << std::endl;
ss << begin_label << ":" << std::endl;
ss << "mov " << get_register(symbol_table[id].symbol_type) << ", [" + symbol_table[id].label + "]" << std::endl;
ss << "push eax" << std::endl;
if (upperlimit->is_const_expr()) {
ss << "mov ecx, " << upperlimit->get_value() << std::endl;
} else {
ss << upperlimit->get_code();
}
ss << "mov ecx,eax" << std::endl;
ss << "pop eax" << std::endl;
// i < upperlimit
ss << "cmp eax,ecx" << std::endl;
ss << "mov al,0" << std::endl;
ss << "mov cx,1" << std::endl;
ss << "cmovb ax,cx" << std::endl;
ss << "cmp al,1" << std::endl;
ss << "jne near " << end_label << std::endl;
generate_code_of_commands(ss, body);
ss << "mov " << get_register(symbol_table[id].symbol_type) << ", [" + symbol_table[id].label + "]" << std::endl;
ss << "inc " << get_register(symbol_table[id].symbol_type) << std::endl;
ss << "mov [" + symbol_table[id].label + "]," << get_register(symbol_table[id].symbol_type) << std::endl;
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::unordered_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;
}