-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backend.cpp
327 lines (294 loc) · 12.2 KB
/
Backend.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
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
#include "Backend.hpp"
#include "File.hpp"
Backend::Backend()
: analyser(), currentFunctionScope(nullptr), translationOutputFile(nullptr)
{
}
void Backend::BackendDtor()
{
analyser.AbstractSyntaxTreeDumpAnalyserDtor();
}
void Backend::translate(const char *abstractSyntaxTreeInputFileName, const char *translationOutputFileName)
{
char *buffer = readFileToBuffer(abstractSyntaxTreeInputFileName, false);
analyser.analyseText(buffer);
std::free(buffer);
buffer = nullptr;
translationOutputFile = std::fopen(translationOutputFileName, "w");
std::fprintf(translationOutputFile, "push 0\n"
"pop rax\n"
"push 0\n"
"pop rbx\n"
"push 0\n"
"pop rcx\n"
"push 0\n"
"pop rdx\n"
"call :main\n"
"hlt\n");
translateFunctionDeclaration(analyser.tree.root);
std::fclose(translationOutputFile);
}
void Backend::translateFunctionDeclaration(const AbstractSyntaxTree::Node *node)
{
if (node == nullptr) return;
translateFunctionImplementation(node->left);
translateFunctionDeclaration(node->right);
}
void Backend::translateFunctionImplementation(const AbstractSyntaxTree::Node *node)
{
if (node == nullptr) return;
std::fprintf(translationOutputFile, "\n"
"; translation of function implementation\n"
"%s:\n", node->data.identifier);
currentFunctionScope = analyser.lookUpFunctionScope(node->data.identifier);
translateBlock(node->right);
}
void Backend::translateBlock(const AbstractSyntaxTree::Node *node)
{
translateConcatenation(node);
}
void
Backend::translateConcatenation(const AbstractSyntaxTree::Node *node)
{
if (node == nullptr) return;
translateStatement(node->left);
translateConcatenation(node->right);
}
void Backend::translateStatement(const AbstractSyntaxTree::Node *node)
{
switch (node->type) {
case AbstractSyntaxTree::Node::Type::If: {
translateIf(node);
break;
}
case AbstractSyntaxTree::Node::Type::While: {
translateWhile(node);
break;
}
default:
translateOperator(node);
break;
}
}
void Backend::translateIf(const AbstractSyntaxTree::Node *node)
{
static size_t uniqueLabelNo = 0;
size_t labelNo = uniqueLabelNo++;
translateCondition(node->left, true, labelNo);
translateBlock(node->right);
std::fprintf(translationOutputFile, "\n"
"; translation of label for if ending\n"
"EndIf%zu:\n", labelNo);
}
void Backend::translateWhile(const AbstractSyntaxTree::Node *node)
{
static size_t uniqueLabelNo = 0;
size_t labelNo = uniqueLabelNo++;
std::fprintf(translationOutputFile, "\n"
"; translation of label for while beginning\n"
"BeginWhile%zu:\n", labelNo);
translateCondition(node->left, false, labelNo);
translateBlock(node->right);
std::fprintf(translationOutputFile, "\n"
"; translation of jump to while beginning\n"
"jmp :BeginWhile%zu\n", labelNo);
std::fprintf(translationOutputFile, "; translation of label for while ending\n"
"EndWhile%zu:\n", labelNo);
}
void Backend::translateCondition(const AbstractSyntaxTree::Node *node, bool isIfCondition, size_t uniqueJumpLabelNo)
{
std::fprintf(translationOutputFile, "\n"
"; translation of jump for conditional statement\n");
switch (node->data.mathematicalOperator[0]) {
case '<': {
translateExpression(node->left);
translateExpression(node->right);
std::fprintf(translationOutputFile, "%s :%s%zu\n", (node->data.mathematicalOperator[1] == '=') ? "ja" : "jae",
isIfCondition ? "EndIf" : "EndWhile", uniqueJumpLabelNo);
break;
}
case '=': {
translateExpression(node->left);
translateExpression(node->right);
std::fprintf(translationOutputFile, "jne :%s%zu\n", isIfCondition ? "EndIf" : "EndWhile", uniqueJumpLabelNo);
break;
}
case '>': {
translateExpression(node->left);
translateExpression(node->right);
std::fprintf(translationOutputFile, "%s :%s%zu\n", (node->data.mathematicalOperator[1] == '=') ? "jb" : "jbe",
isIfCondition ? "EndIf" : "EndWhile", uniqueJumpLabelNo);
break;
}
}
}
void Backend::translateOperator(const AbstractSyntaxTree::Node *node)
{
switch (node->type) {
case AbstractSyntaxTree::Node::Type::AssignmentOperator: {
translateAssignmentOperator(node);
break;
}
case AbstractSyntaxTree::Node::Type::FunctionCallOperator: {
translateFunctionCallOperator(node);
break;
}
case AbstractSyntaxTree::Node::Type::ReturnOperator: {
translateReturnOperator(node);
break;
}
case AbstractSyntaxTree::Node::Type::MathematicalOperator: {
translateMathematicalOperator(node);
break;
}
default:
break;
}
}
void Backend::translateAssignmentOperator(const AbstractSyntaxTree::Node *node)
{
translateExpression(node->right);
translateVariable(node->left, true);
}
void Backend::translateVariable(const AbstractSyntaxTree::Node *node, bool forAssignment)
{
if (forAssignment) {
std::fprintf(translationOutputFile, "\n"
"; translation of variable \"%s\" assignment\n"
"pop [rbx+%zu]\n", node->data.identifier,
analyser.lookUpVariable(currentFunctionScope, node->data.identifier)->memoryOffset);
} else {
std::fprintf(translationOutputFile, "\n"
"; translation of variable \"%s\" access\n"
"push [rbx+%zu]\n", node->data.identifier,
analyser.lookUpVariable(currentFunctionScope, node->data.identifier)->memoryOffset);
}
}
void Backend::translateFunctionCallOperator(const AbstractSyntaxTree::Node *node)
{
if (strcmp(node->data.identifier, "print") == 0) {
translateExpression(node->left->right);
std::fprintf(translationOutputFile, "\n"
"; translation of \"print\" function\n"
"out\n");
} else if (strcmp(node->data.identifier, "scan") == 0) {
std::fprintf(translationOutputFile, "\n"
"; translation of \"scan\" function\n"
"in\n");
translateVariable(node->left->right, true);
} else if (strcmp(node->data.identifier, "sqrt") == 0) {
translateExpression(node->left->right);
std::fprintf(translationOutputFile, "\n"
"; translation of \"sqrt\" function\n"
"sqrt\n");
} else {
std::fprintf(translationOutputFile, "\n"
"; translation of function call operator (\"%s\" function)\n"
"; set rcx to next rpb\n"
"push %zu\n"
"push rbx\n"
"add\n"
"pop rcx\n", node->data.identifier, currentFunctionScope->variableTable.size);
translateFunctionCallParameter(node->left, 1);
std::fprintf(translationOutputFile, "\n"
"; remember current rbp\n"
"push rbx\n"
"pop [rcx]\n"
"; move rbp before function call operator\n"
"push rcx\n"
"pop rbx\n");
std::fprintf(translationOutputFile, "call :%s\n", node->data.identifier);
std::fprintf(translationOutputFile, "\n"
"; retrieve function's return value\n"
"push rax\n"
"; retrieve rbp after function call operator\n"
"push [rcx]\n"
"pop rbx\n"
"; set rcx to current rbp\n"
"push rbx\n"
"pop rcx\n");
}
}
void Backend::translateFunctionCallParameter(const AbstractSyntaxTree::Node *node, size_t parameterNo)
{
if (node == nullptr) return;
translateExpression(node->right);
std::fprintf(translationOutputFile, "\n"
"; translation of function call parameter\n"
"pop [rcx+%zu]\n", parameterNo);
translateFunctionCallParameter(node->left, parameterNo + 1);
}
void Backend::translateReturnOperator(const AbstractSyntaxTree::Node *node)
{
translateExpression(node->left);
std::fprintf(translationOutputFile, "\n"
"; translation of return operator\n"
"pop rax\n"
"ret\n");
}
void Backend::translateMathematicalOperator(const AbstractSyntaxTree::Node *node)
{
translateExpression(node->left);
translateExpression(node->right);
switch (node->data.mathematicalOperator[0]) {
case '+': {
std::fprintf(translationOutputFile, "\n"
"; translation of \'+\'\n"
"add\n");
break;
}
case '-': {
std::fprintf(translationOutputFile, "\n"
"; translation of \'-\'\n"
"sub\n");
break;
}
case '*': {
std::fprintf(translationOutputFile, "\n"
"; translation of \'*\'\n"
"mul\n");
break;
}
case '/': {
std::fprintf(translationOutputFile, "\n"
"; translation of \'/\'\n"
"div\n");
break;
}
case '^': {
std::fprintf(translationOutputFile, "\n"
"; translation of \'^\'\n"
"pow\n");
break;
}
}
}
void Backend::translateExpression(const AbstractSyntaxTree::Node *node)
{
switch (node->type) {
case AbstractSyntaxTree::Node::Type::Variable: {
translateVariable(node, false);
break;
}
case AbstractSyntaxTree::Node::Type::Number: {
translateNumber(node);
break;
}
case AbstractSyntaxTree::Node::Type::FunctionCallOperator: {
translateFunctionCallOperator(node);
break;
}
case AbstractSyntaxTree::Node::Type::MathematicalOperator: {
translateMathematicalOperator(node);
break;
}
default:
break;
}
}
void Backend::translateNumber(const AbstractSyntaxTree::Node *node)
{
std::fprintf(translationOutputFile, "\n"
"; translation of number\n"
"push %lg\n", node->data.number);
}