-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompiler.cpp
282 lines (259 loc) · 8.78 KB
/
compiler.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>
#include "AST.h"
using namespace std;
string stdio = "\nprintln: \n\
pop edi \n\
pop edx \n\
pop ecx \n\
mov ebx, 1 \n\
mov eax, 4 \n\
int 0x80 \n\
\n\
mov edx, 1 \n\
mov ecx, NEWLINE \n\
mov ebx, 1 \n\
mov eax, 4 \n\
int 0x80 \n\
\n\
push edi \n\
ret \n\
\n\
print: \n\
pop edi \n\
pop edx \n\
pop ecx \n\
mov ebx, 1 \n\
mov eax, 4 \n\
int 0x80 \n\
\n\
mov eax, 0 \n\
push edi \n\
ret\n";
string stdlib = "\nend: \n\
mov eax, 1 \n\
int 0x80 \n";
string EXPR(string ETYPE, x86* a, vector<AbstractNode> _data, string _type, string _value) {
if(ETYPE == "VAR") {
if(_data[0]._LIT._data[0]._type == "LITERAL"){
if(_data[0]._LIT._data[0]._LIT._type != "WORD") a->add(a->variable(_data[0]._LIT._type, _type, _value, false, false));
else a->add(a->variable(_data[0]._LIT._type, _type, _value, true, false));
}else if(_data[0]._LIT._data[0]._type == "FCALL"){
// must be a function call
a->add(a->variable(_data[0]._LIT._type, _type, _data[0]._LIT._data[0]._FC.codegen("o",a), false, false));
}else if(_data[0]._LIT._data[0]._type == "OPP") {
a->add(a->variable(_data[0]._LIT._type, _type, _data[0]._LIT._data[0]._OPP.codegen("o",a), false, false));
}else if(_data[0]._LIT._data[0]._type == "PNTR") {
a->add(a->variable(_data[0]._LIT._type, _type, _data[0]._LIT._data[0]._PNTR.codegen("o",a), false, false));
}else return "";
}
else if(ETYPE == "RETURN") {
// cout << _data[0]._LIT._value << endl;
if(_data[0]._type == "LITERAL"){
if(_data[0]._LIT._type == "STRING")
return a->constant(_data[0]._LIT._type, _data[0]._LIT._value);
else if( _data[0]._LIT._type == "NUMBER")
return _data[0]._LIT._value;
else if(_data[0]._LIT._type == "WORD")
return "[" + _data[0]._LIT._value + "]";
else return "";
}else if(_data[0]._type == "FCALL") {
// must be a function call
return _data[0]._FC.codegen("o",a);
}else if(_data[0]._type == "OPP") {
return _data[0]._OPP.codegen("o",a);
}else if(_data[0]._type == "PNTR") {
return _data[0]._PNTR.codegen("o",a);
}else return "";
}else return "";
return "";
}
string IMP::codegen(string otype, x86* a) {
if (false) {
return "#include " + _value;
} else if (true) {
// get asm from package folder
// fstream file;
// cout << "./packages/" + _value.erase(0,1).erase(_value.size()-1,1) + ".asm" << endl;
// file.open("./packages/" + _value.erase(0,1).erase(_value.size()-1,1) + ".asm");
// string line;
// while (getline(file, line)) {
// cout << line << endl;
// a->add(line + "\n");
// }
// file.close();
if(_value == "\"stdio\"") a->add(stdio);
if(_value == "\"stdlib\"") a->add(stdlib);
return "\n";
} else
return "";
}
string LITERAL::codegen(string otype, x86* a) {
if (false) {
return _value;
} else if (true) {
return _value;
} else
return "";
}
string FCALL::codegen(string otype, x86* a) {
if (false) {
string code = _value + "(";
for (int i = 0; i < _data.size(); i++) {
code += _data[i]._LIT._value;
}
code += ")";
return code + ";";
} else if (true) {
// for (int i = 0; i < _data.size(); i++) {
// if(_data[i]._LIT._value != ","){
// if(_data[i]._LIT._type == "STRING")
// a->add(" mov eax, " + a->constant(_data[i]._LIT.ctype, _data[i]._LIT._value) + "\n push eax\n");
// else if(_data[i]._LIT._type == "WORD")
// a->add(" mov eax, [" + _data[i]._LIT._value + "]\n push eax\n");
// else if(_data[i]._LIT._type == "NUMBER")
// a->add(" mov eax, " + _data[i]._LIT._value + "\n push eax\n");
// }
// }
// use new expression function:
// EXPR("RETURN", a, _data, _type, _value)
for (int i = 0; i < _data.size(); i++) {
a->add("mov eax, " + EXPR("RETURN", a, {_data[i]}, _type, _value) + "\n push eax\n");
}
a->add(" call " + _value + "\n\n");
// a->out.app;
return "eax";
} else
return "";
}
string ASM::codegen(string otype, x86* a) {
a->add(_value.erase(0,1).erase(_value.size()-1, 1) + "\n");
return "";
}
string RET::codegen(string otype, x86* a) {
a->add("mov eax, " + EXPR("RETURN", a, _data, _type, _value) + "\n ret\n");
return "";
}
string ASSIGN::codegen(string otype, x86* a) {
return EXPR("VAR", a, _data, _type, _value);
}
string LCLASS::codegen(string otype, x86* a) {
if(_data[0]._LIT._data[0]._type == "LITERAL"){
if(_data[0]._LIT._data[0]._LIT._type != "WORD") a->add(a->variable(_data[0]._LIT._type, _type, _value, false, true));
else a->add(a->variable(_data[0]._LIT._type, _type, _value, true, true));
}else if(_data[0]._LIT._data[0]._type == "FCALL"){
// must be a function call
a->add(a->variable(_data[0]._LIT._type, _type, _data[0]._LIT._data[0]._FC.codegen("o",a), false, true));
}else if(_data[0]._LIT._data[0]._type == "OPP") {
a->add(a->variable(_data[0]._LIT._type, _type, _data[0]._LIT._data[0]._OPP.codegen("o",a), false, true));
}else if(_data[0]._LIT._data[0]._type == "PNTR") {
a->add(a->variable(_data[0]._LIT._type, _type, _data[0]._LIT._data[0]._PNTR.codegen("o",a), false, true));
}else return "";
return "";
}
string LCLDEF::codegen(string otype, x86* a) {
a->add("\%define " + _type + " " + _value + "\n");
return "";
}
string REFER::codegen(string otype, x86* a) {
a->add(a->point(_data[0]._LIT._type, _type, _value));
return "";
}
string DEREF::codegen(string otype, x86* a) {
a->add(a->deref(_data[0]._LIT._type, _type, _value));
return "";
}
string IE::codegen(string otype, x86* a) {
// cout << "heard" << endl;
if(_type == "import") {
a->add("extern " + _value + "\n");
}else {
a->add("global " + _value + "\n");
}
return "";
}
string OPP::codegen(string otype, x86* a) {
a->add("mov eax, " + _data[0]._LIT._value + "\n");
if(_type == "+") a->add("add eax, " + _data[1]._LIT._value + "\n");
if(_type == "-") a->add("sub eax, " + _data[1]._LIT._value + "\n");
// if(_type == "*") a->add("imul eax, " + _data[1]._LIT._value + "\n");
// if(_type == "/") a->add("div eax, " + _data[1]._LIT._value + "\n");
// if(_type == "%") a->add("mod eax, " + _data[1]._LIT._value + "\n");
// if(_type == "->") ...;
return "eax";
}
string PNTR::codegen(string otype, x86* a) {
if(_type == "REF") {
a->add("lea eax, " + _value + "\n");
return "eax";
}else if(_type == "DEREF") {
a->add("mov ebx, [" + _value + "]\n");
a->add("mov eax, [ebx]\n");
return "eax";
}else
return "";
}
string FDECL::codegen(string otype, x86* a) {
if (false) {
string code = "\n" + rtype + " " + _value + "(";
for (int i = 0; i < _data.size(); i++) {
code += _data[i]._LIT.ctype + " " + _data[i]._LIT.codegen(otype, a);
if (i != _data.size() - 1) {
code += ", ";
} else
return "";
}
code += ") {\n\n";
for (int i = 0; i < body.size(); i++) {
if (body[i]._type == "RET") {
code += " " + body[i]._RET.codegen(otype,a) + "\n";
} else if (body[i]._type == "FCALL") {
// code += " " + body[i]._FC.codegen(otype,a) + "\n";
body[i]._FC.codegen(otype,a);
} else if (body[i]._type == "IMP") {
code += " " + body[i]._IMP.codegen(otype,a) + "\n";
}else if(body[i]._type == "ASM"){
code += " " + body[i]._ASM.codegen(otype,a) + "\n";
}
}
return code + "\n}";
} else if (true) {
if(_value == "main") a->add("global _start\n _start:\n call main \n mov eax, 1\n int 0x80");
a->add("\n" + _value +":\n");
string code = "";
for (int i = 0; i < body.size(); i++) {
if(body[i]._type == "ASSIGN"){
code += body[i]._ASSIGN.codegen(otype,a);
}else if (body[i]._type == "RET") {
code += body[i]._RET.codegen(otype,a);
} else if (body[i]._type == "FCALL") {
// code += body[i]._FC.codegen(otype,a);
body[i]._FC.codegen(otype,a);
} else if (body[i]._type == "IMP") {
code += body[i]._IMP.codegen(otype,a);
}else if(body[i]._type == "ASM"){
code += body[i]._ASM.codegen(otype,a);
}else if(body[i]._type == "REFER"){
code += body[i]._REFER.codegen(otype,a);
}else if(body[i]._type == "DEREF"){
code += body[i]._DEREF.codegen(otype,a);
}else if(body[i]._type == "IE"){
code += body[i]._IE.codegen(otype,a);
}else if(body[i]._type == "LCLDEF"){
code += body[i]._LCLDEF.codegen(otype,a);
}else if(body[i]._type == "OPP"){
code += body[i]._OPP.codegen(otype,a);
}else if(body[i]._type == "PNTR"){
code += body[i]._PNTR.codegen(otype,a);
}else if(body[i]._type == "LCLASS"){
code += body[i]._LCLASS.codegen(otype,a);
}
}
a->add(code);
return code;
} else
return "";
}