-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeutils.cc
326 lines (301 loc) · 8.64 KB
/
codeutils.cc
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
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
#include "codeutils.h"
#include "astree.h"
#include "stringset.h"
#include "lyutils.h"
#include "symtable.h"
#include "typetable.h"
int counter = 0;
string indent = " ";
string functions = "";
bool inAssignment = false;
map<int, string> reg_map;
char mangle_letter(string type) {
if (type.compare("int") == 0) {
return 'i';
} else if (type.compare("ubyte") == 0) {
return 'b';
}
return 'p';
}
string inttostr(int i) {
ostringstream ss;
ss << i;
return ss.str();
}
string save_in_reg(string code, string type, int depth) {
char buffer[10];
if (type.compare("void") == 0)
type = "int";
sprintf (buffer, "%c%d", mangle_letter(type), ++counter);
string s = indent + type + " " + buffer + " = " + code + ";\n";
if (reg_map.count(depth) == 0) {
reg_map[depth] = "";
}
reg_map[depth] += s;
return buffer;
}
string get_regs() {
string s = "";
for( map<int, string>::reverse_iterator it=reg_map.rbegin(); it!=reg_map.rend(); ++it ) {
// s += "[" + inttostr(it->first);
s += it->second;
// s += inttostr(it->first) + "]";
}
reg_map.clear();
return s;
}
string mangle_name(string name) {
int level = global_table->lookupBlock(name);
if (level == 0)
return "__" + name;
else
return "_" + inttostr(level) + "_" + name;
}
string map_type(string type) {
if (type.length() > 2 && type.substr(type.length()-2, 2).compare("[]") == 0) {
return map_type(type.substr(0, type.length()-2)) + "*";
} else {
if (type.compare("bool") == 0 || type.compare("char") == 0) {
return "ubyte";
} else if (type.compare("int") == 0) {
return "int";
} else if (type.compare("string") == 0) {
return "ubyte*";
} else if (type.compare("null") == 0) {
return "void";
} else if (type.compare("void") == 0) {
return "void";
}
}
return "struct " + type + " *";
}
string clearRegs(string code, int depth) {
if (depth == 0) {
return get_regs() + code;
}
return code;
}
string makeBlock(astree* child, int depth) {
string returnStr = codegen(child, false, depth);
if (child->symbol != BLOCK) {
return returnStr + ";\n";
}
return returnStr;
}
string codegen(astree* root, bool save, int depth) {
string code = "";
if (save) depth++;
switch (root->symbol) {
case TOK_ROOT: {
for(size_t i = 0; i < root->children.size(); i++) {
string s = codegen(root->children[i], false, depth);
if (!s.length())
continue;
code += s + ";\n";
}
break;
}
case DECL:
// TODO: Check this
code = map_type(codegen(root->children[0], false, depth));
if (root->children.size() > 1) {
code += " " + codegen(root->children[1], false, depth);
}
break;
case TYPE:
// code = *root->lexinfo;
code = root->type;
break;
case BLOCK: {
for(size_t i = 0; i < root->children.size(); i++) {
code += codegen(root->children[i], false, depth) + ";\n";
}
break;
}
case IFELSE: {
string num = inttostr(++counter);
code += indent + "if (!" + codegen(root->children[0], true, depth) + ") goto else_" + num + ";\n";
code = clearRegs(code, depth);
code += makeBlock(root->children[1], depth);
code += indent + "goto fi_" + num + ";\n";
code += "else_" + num + ":;\n";
if (root->children.size() > 2) {
code = clearRegs(code, depth);
code += makeBlock(root->children[2], depth);
}
code += "fi_" + num + ":";
break;
}
case WHILE: {
string num = inttostr(++counter);
code += indent + "if (!" + codegen(root->children[0], true, depth) + ") goto break_" + num + ";\n";
code = clearRegs(code, depth);
code += makeBlock(root->children[1], depth);
code += indent + "goto while_" + num + ";\n";
code += "break_" + num + ":";
code = "\nwhile_" + num + ":;\n" + code;
break;
}
case FUNCTION: {
if (root->children[root->children.size()-1]->symbol != BLOCK
|| root->children[root->children.size()-1]->children.size() == 0)
break;
string type = map_type(root->children[0]->type);
string name = mangle_name(*root->children[1]->lexinfo);
string params = "(";
if (root->children.size() > 3) {
for (size_t i = 0; i < root->children[2]->children.size(); i++) {
if (i) params += ",";
params += "\n" + indent;
params += codegen(root->children[2]->children[i], false, depth);
}
}
params += ")";
int lastChld = root->children.size()-1;
string block = codegen(root->children[lastChld], false, depth);
functions += "\n" + type + "\n" + name + params + "\n{\n" + block + "}\n";
break;
}
case CALL: {
code += mangle_name(*root->children[0]->lexinfo);
code += "(";
if (root->children.size() > 1) {
for (size_t i = 0; i < root->children[1]->children.size(); i++) {
if (i) code += ", ";
code += codegen(root->children[1]->children[i], false, depth+1);
}
}
code += ")";
break;
}
case BINOP: {
string op = *root->children[1]->lexinfo;
inAssignment = (op.compare("=") == 0);
string left = codegen(root->children[0], !inAssignment, depth);
string right = codegen(root->children[2], true, depth);
code = left + " " + op + " " + right;
break;
}
case UNOP: {
string op = *root->children[0]->lexinfo;
if (op.compare("ord") == 0) {
op = "(int) ";
}
string right = codegen(root->children[1], false, depth);
code = op + right;
break;
}
case ALLOCATOR: {
string size = "1";
if (root->children.size() > 1) {
size = codegen(root->children[1]->children[0], false, depth);
}
code += "xcalloc(" + size + ", sizeof(" + map_type(root->type) + "))";
break;
}
case TOK_RETURN: {
code = "return ";
if (root->children.size() > 0)
code += codegen(root->children[0], false, depth+1);
break;
}
case VARIABLE: {
code = codegen(root->children[0], !inAssignment, depth);
if (root->children.size() > 1) {
if (root->children[1]->symbol == '[') {
code += "[" + codegen(root->children[2], true, depth) + "]";
} else if (root->children[1]->symbol == '.') {
code += "." + codegen(root->children[2], true, depth);
}
}
inAssignment = false;
break;
}
case CONSTANT: {
code = codegen(root->children[0], false, depth);
break;
}
case VARDECL: {
string type = map_type(root->children[0]->type);
string left = codegen(root->children[1], false, depth);
string right = codegen(root->children[2], true, depth);
if (left.substr(0,2).compare("__") == 0) {
code = indent + left + " = " + right;
} else {
code = indent + type + " " + left + " = " + right;
}
break;
}
case TOK_IDENT:
code = mangle_name(*root->lexinfo);
break;
case TOK_INTCON:
code = *root->lexinfo;
break;
case TOK_CHARCON:
code = *root->lexinfo;
break;
case TOK_STRINGCON:
code = *root->lexinfo;
break;
case TOK_FALSE:
code = "0";
break;
case TOK_TRUE:
code = "1";
break;
case TOK_NULL:
code = "0";
break;
}
if (save) {
code = save_in_reg(code, map_type(root->type), depth);
}
code = clearRegs(code, depth);
return code;
}
void print_section_break(FILE* oil_file) {
fprintf(oil_file, "\n");
}
void print_preamble(FILE* oil_file) {
fprintf(oil_file, "#define __OCLIB_C__\n#include \"oclib.oh\"\n");
print_section_break(oil_file);
}
void print_structs(FILE* oil_file) {
type_table->print_types(oil_file);
print_section_break(oil_file);
}
void print_globals(FILE* oil_file) {
global_table->print_globals(oil_file);
print_section_break(oil_file);
}
void print_functions(FILE* oil_file) {
fprintf(oil_file, "%s", functions.c_str());
print_section_break(oil_file);
}
void print_main(FILE* oil_file) {
fprintf(oil_file, "void __ocmain() {\n");
}
void print_main_end(FILE* oil_file) {
fprintf(oil_file, "}\n");
}
void run_code_gen(astree* yyparse_astree, FILE* oil_file) {
// Generate Code
string out = codegen(yyparse_astree, false, 0);
print_preamble(oil_file);
print_structs(oil_file);
print_globals(oil_file);
print_functions(oil_file);
print_main(oil_file);
fprintf(oil_file, "%s", out.c_str());
print_main_end(oil_file);
}