Repository navigation
Expand file tree
/
Copy pathNode.h
More file actions
439 lines (428 loc) · 12.7 KB
/
Copy pathNode.h
File metadata and controls
439 lines (428 loc) · 12.7 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#pragma once
#include <list>
#include <string>
#include <iostream>
#include <memory>
#include <vector>
#include <map>
#include <stack>
#include "Type.h"
#include "ErrorParser.h"
enum NodeKind {
NODE_STATEMENT,
NODE_EXPRESSION,
NODE_FUNCTION,
NODE_ERROR_TYPE
};
class Expression;
class Function;
class Program {
public:
inline static std::map<std::string, std::shared_ptr<Expression>> variable_table;
inline static std::map<std::string, std::shared_ptr<Function>> function_table;
inline static std::map<std::string, int> v_line;
inline static std::map<std::string, int> f_line;
};
class Node
{
public:
int line; int offset;
inline static std::stack<std::pair<int, int>>loops;
inline int static lab_id = 0;
inline Node(NodeKind nk):kind(nk){}
virtual std::vector<std::string> exec()=0;
NodeKind kind;
};
class Expression :public Node {
public:
std::vector<std::shared_ptr<Expression>> parameters;
std::string fname;
bool is_left = false;
bool is_const = false;
enum EXPRESSION_TYPE {
EXP_NUMERIC,
EXP_VARIABLE,
EXP_FUNCCALL,
EXP_NEG,
EXP_NOT,
EXP_LNOT,
EXP_ADD,
EXP_SUB,
EXP_MULT,
EXP_DIV,
EXP_REM,
EXP_LAND,
EXP_LOR,
EXP_EQ,
EXP_NE,
EXP_LT,
10BC8
EXP_GT,
EXP_LE,
EXP_GE,
EXP_ASSIGN,
EXP_TRI,
EXP_ADDRESSOF,
EXP_POINTER_WRAPPER,
EXP_TYPE_CAST,
EXP_ARRAY_WRAPPER
} expr_type;
inline Expression(EXPRESSION_TYPE type) :Node(NODE_EXPRESSION), expr_type(type),val(0){
if (type == EXP_VARIABLE || type==EXP_POINTER_WRAPPER)this->is_left = true;
else this->is_left = false;
this->type = TypeFactory::int_type();
if (type == EXP_NUMERIC)this->is_const = true;
else this->is_const = false;
}
int val;
std::shared_ptr<Expression> cond;
std::shared_ptr<Expression> op;
std::shared_ptr<Expression> op2;
std::shared_ptr<Type> type;
int id_in_frame = 0;
int vsize = 4;
virtual std::vector<std::string> exec();
inline std::vector<std::string> address_of() {
if ((!is_left) && (!(expr_type == EXP_VARIABLE || type->kind == TYPE_ARRAY))) {
std::cerr << "Unexpected error"<< std::endl;
std::cerr << "Fetching the address of something without address!" << std::endl;
exit(1);
}
std::vector<std::string> r;
switch (expr_type) {
case EXP_VARIABLE:
if (id_in_frame == -1)
r.push_back(std::string("globaladdr ") + fname);
else
r.push_back(std::string("frameaddr ") + std::to_string(id_in_frame));
break;
case EXP_POINTER_WRAPPER:
r = op->exec();
break;
case EXP_ARRAY_WRAPPER:
{
if (op->type->kind != TYPE_ARRAY && op->type->kind != TYPE_POINTER)exit(1);
if (op->type->kind == TYPE_ARRAY) {
r = op->address_of();
}
else {
r = op->exec();
}
if (op2->is_const) {
r.push_back("push " + std::to_string(op2->val * op->type->point_to->size()));
r.push_back("add");
}
else {
auto r2 = op2->exec();
for (auto& i : r2)r.push_back(i);
r.push_back("push " + std::to_string(op->type->point_to->size()));
r.push_back("mul");
r.push_back("add");
}
}
}
return r;
}
};
class Statement :public Node {
public:
enum STATEMENT_TYPE {
STA_RETURN,
STA_DONOTUSE,
STA_NOOP,
STA_IF,
STA_BLOCK,
STA_DOWHILE,
STA_WHILE,
STA_BREAK,
STA_CONTINUE,
STA_FOR
} stat_type;
std::vector<std::shared_ptr<Statement>> body;
std::shared_ptr<Statement> child1;
std::shared_ptr<Statement> child2;
std::shared_ptr<Statement> child3;
std::shared_ptr<Expression> op;
inline Statement(STATEMENT_TYPE type) :Node(NODE_STATEMENT), stat_type(type){}
inline std::vector<std::string> exec() {
std::vector<std::string> r;
switch (stat_type) {
case STA_RETURN:
r = op->exec();
r.push_back("ret");
return r;
case STA_DONOTUSE:
r = op->exec();
r.push_back("pop");
return r;
case STA_NOOP:
r.clear();
return r;
case STA_FOR:
{
if (op->type->kind != TYPE_INT)exit(1);
int id1 = lab_id;//loop begin
int id2 = lab_id + 1;// continue target
int id3 = lab_id + 2;// loop finish
lab_id += 3;
Node::loops.push(std::make_pair(id2, id3));
r = child1->exec();
r.push_back(std::string("label L") + std::to_string(id1));
auto r2 = op->exec();
for (auto& i : r2)r.push_back(i);
r.push_back("beqz L" + std::to_string(id3));
r2 = child2->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("label L") + std::to_string(id2));
r2 = child3->exec();
for (auto& i : r2)r.push_back(i);
r.push_back("br L" + std::to_string(id1));
r.push_back(std::string("label L") + std::to_string(id3));
Node::loops.pop();
return r;
}
case STA_IF:
{
if (op->type->kind != TYPE_INT)exit(1);
if (op->is_const) {
if (op->val)
return child1->exec();
else
return child2->exec();
}
r = op->exec();
int id1 = lab_id;
int id2 = lab_id + 1;
lab_id += 2;
r.push_back(std::string("beqz L") + std::to_string(id1));
auto r2 = child1->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("br L") + std::to_string(id2));
r.push_back(std::string("label L") + std::to_string(id1));
r2 = child2->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("label L") + std::to_string(id2));
return r;
}
case STA_BLOCK :
{
r.clear();
for (auto& c : body) {
auto r2=c->exec();
for (auto& i : r2)
r.push_back(i);
}
return r;
}
case STA_DOWHILE:
{
if (op->type->kind != TYPE_INT)exit(1);
if (op->is_const) {
if (op->val) {
int id1 = lab_id;
int id2 = lab_id + 1;
lab_id += 2;
Node::loops.push(std::make_pair(id1, id2));
r.push_back(std::string("label L") + std::to_string(id1));
auto r2 = child1->exec();
for (auto& i : r2)r.push_back(i);;
r.push_back(std::string("br L") + std::to_string(id1));
r.push_back(std::string("label L") + std::to_string(id2));
Node::loops.pop();
return r;
}
else {
int id1 = lab_id;
lab_id += 1;
Node::loops.push(std::make_pair(id1, id1));
r = child1->exec();
r.push_back(std::string("label L") + std::to_string(id1));
Node::loops.pop();
return r;
}
}
int id1 = lab_id;
int id2 = lab_id + 1;
int id3 = lab_id + 2;
lab_id += 3;
Node::loops.push(std::make_pair(id2, id3));
r.push_back(std::string("label L") + std::to_string(id1));
auto r2 = child1->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("label L") + std::to_string(id2));
r2 = op->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("bnez L") + std::to_string(id1));
r.push_back(std::string("label L") + std::to_string(id3));
Node::loops.pop();
return r;
}
case STA_BREAK:
{
if (Node::loops.empty()) {
std::cerr << "Parser error at line " << line << std::endl;
std::cerr << ErrorParser::lines[line - 1] << std::endl;
std::cerr << std::string(offset, ' ') << "^" << std::endl;
std::cerr << "break can only be used inside loop body!." << std::endl;
exit(1);
}
r.push_back(std::string("br L" + std::to_string(Node::loops.top().second)));
return r;
}
case STA_CONTINUE:
{
if (Node::loops.empty()) {
std::cerr << "Parser error at line " << line << std::endl;
std::cerr << ErrorParser::lines[line - 1] << std::endl;
std::cerr << std::string(offset, ' ') << "^" << std::endl;
std::cerr << "continue can only be used inside loop body!." << std::endl;
exit(1);
}
r.push_back(std::string("br L" + std::to_string(Node::loops.top().first)));
return r;
}
case STA_WHILE:
{
if (op->type->kind != TYPE_INT)exit(1);
if (op->is_const) {
if (op->val) {
int id1 = lab_id;
int id2 = lab_id + 1;
lab_id += 2;
Node::loops.push(std::make_pair(id1, id2));
r.push_back(std::string("label L") + std::to_string(id1));
auto r2 = child1->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("br L") + std::to_string(id1));
r.push_back(std::string("label L") + std::to_string(id2));
Node::loops.pop();
return r;
}
else {
return std::vector<std::string>();
}
}
int id1 = lab_id;
int id2 = lab_id + 1;
lab_id += 2;
Node::loops.push(std::make_pair(id1, id2));
r.push_back(std::string("label L") + std::to_string(id1));
auto r2 = op->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("beqz L") + std::to_string(id2));
r2 = child1->exec();
for (auto& i : r2)r.push_back(i);
r.push_back(std::string("br L") + std::to_string(id1));
r.push_back(std::string("label L") + std::to_string(id2));
Node::loops.pop();
return r;
}
}
return r;
}
};
class Function:public Node{
public:
bool finished=false;
inline Function() :Node(NODE_FUNCTION){}
std::string func_name;
std::list<std::shared_ptr<Statement>> body;
std::vector<std::pair<std::string, std::shared_ptr<Expression>>> arguments;
//std::map<std::string,std::shared_ptr<Expression>> local_variables;
std::list<std::map<std::string, std::pair<std::shared_ptr<Expression>,int>>>local_variables;
std::shared_ptr<Type> return_type;
std::shared_ptr<Expression> find_arg(std::string name){
for (size_t i = 0; i < arguments.size(); i++)
if (arguments[i].first == name)return arguments[i].second;
return nullptr;
}
int stack_frame_size = 0;
int current_sfsize=0;
int current_sfcnt = 0;
inline void enter_scope() {
local_variables.push_front(std::map<std::string, std::pair<std::shared_ptr<Expression>,int>>());
}
inline void exit_scope() {
for (auto& v : local_variables.front()) {
current_sfsize -= v.second.first->vsize;
}
current_sfcnt -= local_variables.front().size();
local_variables.pop_front();
}
inline std::shared_ptr<Expression> find_variable(std::string name) {
for (auto& m : local_variables) {
if (m.find(name) != m.cend())
return m[name].first;
}
auto f = find_arg(name);
if (f)return f;
return nullptr;
}
inline void find_variable_line(std::string name) {
for (auto& m : local_variables) {
if (m.find(name) != m.cend()) {
ErrorParser::tmp = m[name].second;
return;
}
}
ErrorParser::tmp = 0;
return;
}
inline std::shared_ptr<Expression> push_variable(std::string name,std::shared_ptr<Type> type,int line) {
if (local_variables.front().find(name) != local_variables.front().cend()) {
ErrorParser::tmp = local_variables.front()[name].second;
return nullptr;
}
if (local_variables.size() <= 1 && find_arg(name)) {
ErrorParser::tmp = 0;
return nullptr;
}
local_variables.front()[name] = std::make_pair(std::make_shared<Expression>(Expression::EXP_VARIABLE),line);
if (type->kind == TYPE_ARRAY)local_variables.front()[name].first->is_left = false;
current_sfsize += type->size();
current_sfcnt++;
local_variables.front()[name].first->id_in_frame = current_sfsize / 4 - 1;
local_variables.front()[name].first->vsize = type->size();
if (current_sfsize > stack_frame_size)stack_frame_size = current_sfsize;
return local_variables.front()[name].first;
}
inline std::vector<std::string> prologue() {
int frame_size = stack_frame_size+8;
std::vector<std::string> r;
r.push_back(std::string("func ") + func_name);
r.push_back(std::string("#addi sp,sp,-") + std::to_string(frame_size));
r.push_back(std::string("#sw ra,") + std::to_string(frame_size - 4) + "(sp)");
r.push_back(std::string("#sw fp,") + std::to_string(frame_size - 8) + "(sp)");
r.push_back(std::string("#addi fp,sp,") + std::to_string(frame_size));
return r;
}
inline std::vector<std::string> epilogue() {
int frame_size = stack_frame_size+8;
std::vector<std::string> r;
r.push_back(std::string("#")+this->func_name+"_EPILOGUE:");
r.push_back("#addi sp, sp, -4");
r.push_back("#li t1, 0");
r.push_back("#sw t1, 0(sp)");
r.push_back("#addi sp,fp," + std::to_string(-frame_size));
r.push_back("#addi sp, sp, -4");
r.push_back("#lw a0,0(sp)");
r.push_back("#addi sp,sp,4");
r.push_back(std::string("#lw fp,")+std::to_string(frame_size-8)+"(sp)");
r.push_back(std::string("#lw ra,") + std::to_string(frame_size - 4) + "(sp)");
r.push_back(std::string("#addi sp,sp,") + std::to_string(frame_size));
r.push_back("#jr ra");
return r;
}
inline std::vector<std::string> exec() {
std::vector<std::string> r= prologue();
for (auto& s : body) {
auto p=s->exec();
for (auto& pi : p) {
r.push_back(pi);
}
}
std::vector<std::string> r2 = epilogue();
for (auto& ri : r2)
r.push_back(ri);
return r;
}
};