-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysyAST.cpp
More file actions
executable file
·369 lines (360 loc) · 14 KB
/
Copy pathsysyAST.cpp
File metadata and controls
executable file
·369 lines (360 loc) · 14 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
#ifndef defs_hpp
#include "defs.hpp"
#define defs_hpp
#endif
using namespace std;
/* ================================================= */
/* eval */
/* - most parts in .hpp */
/* - here using varManager */
/* ================================================= */
int _VAR::eval() {
return varManager->query(name);
}
int _ARRAY_ITEM::eval() {
vector<int> addr{};
param->vectorize(addr);
return varManager->query(name, addr);
}
/* ================================================= */
/* atomize */
/* - target is the defualt target */
/* - if target is NULL, temporary'll var be used*/
/* - AND/ OR is special */
/* ================================================= */
eeyoreAST* _UNARY_OP::atomize(eeyoreAST* target) {
eeyoreAST* aop = op->atomize(NULL);
eeyoreAST* t = target ? target : new _eVAR(varManager->newt());
eeyoreStmt(new _eUNARY(t, symbol(), aop));
return t;
}
eeyoreAST* _BINARY_OP::atomize(eeyoreAST* target) {
eeyoreAST* alop = lop->atomize(NULL);
eeyoreAST* arop = rop->atomize(NULL);
eeyoreAST* t = target ? target : new _eVAR(varManager->newt());
eeyoreStmt(new _eBINARY(t, alop, symbol(), arop));
return t;
}
eeyoreAST* _AND::atomize(eeyoreAST* target) {
eeyoreAST* t = target ? target : new _eVAR(varManager->newt());
eeyoreStmt(new _eDIRECT(t, new _eNUM(0)));
eeyoreAST* alop = lop->atomize(NULL);
string cp = varManager->newl();
eeyoreStmt(new _eIFGOTO(alop, "==", new _eNUM(0), cp));
eeyoreAST* arop = rop->atomize(NULL);
eeyoreStmt(new _eIFGOTO(arop, "==", new _eNUM(0), cp));
eeyoreStmt(new _eDIRECT(t, new _eNUM(1)));
eeyoreStmt(new _eLABEL(cp));
return t;
}
eeyoreAST* _OR::atomize(eeyoreAST* target) {
eeyoreAST* t = target ? target : new _eVAR(varManager->newt());
eeyoreStmt(new _eDIRECT(t, new _eNUM(1)));
eeyoreAST* alop = lop->atomize(NULL);
string cp = varManager->newl();
eeyoreStmt(new _eIFGOTO(alop, "!=", new _eNUM(0), cp));
eeyoreAST* arop = rop->atomize(NULL);
eeyoreStmt(new _eIFGOTO(arop, "!=", new _eNUM(0), cp));
eeyoreStmt(new _eDIRECT(t, new _eNUM(0)));
eeyoreStmt(new _eLABEL(cp));
return t;
}
eeyoreAST* _ADDR_LIST::atomize(string name, eeyoreAST* target) {
vector<int> &shape = varManager->getshape(name);
assert(head != NULL);
eeyoreAST* ret = head->atomize(NULL);
_ADDR_LIST *cur = (_ADDR_LIST *)tail;
for (int k = 1; k < shape.size(); k++)
if (cur) {
assert(cur->head);
eeyoreAST* nxt = cur->head->atomize(NULL);
eeyoreAST* t1 = new _eVAR(varManager->newt());
eeyoreAST* t2 = new _eVAR(varManager->newt());
eeyoreStmt(new _eBINARY(t1, ret, "*", new _eNUM(shape[k])));
eeyoreStmt(new _eBINARY(t2, t1, "+", nxt));
ret = t2;
cur = (_ADDR_LIST *) (cur->tail);
}
else {
eeyoreAST* t1 = new _eVAR(varManager->newt());
eeyoreStmt(new _eBINARY(t1, ret, "*", new _eNUM(shape[k])));
ret = t1;
}
assert(cur == NULL);
eeyoreAST* t = target ? target : new _eVAR(varManager->newt());
eeyoreStmt(new _eBINARY(t, ret, "*", new _eNUM(4)));
return t;
}
eeyoreAST* _ARRAY_ITEM::atomize(eeyoreAST* target) {
/* potential optimization for constant array */
eeyoreAST* t = target ? target : new _eVAR(varManager->newt());
eeyoreAST* lval = param->atomize(name, NULL);
eeyoreStmt(new _eSEEK(t, varManager->getEeyore(name), lval));
return t;
}
eeyoreAST* _FUNC_CALL::atomize(eeyoreAST* target) {
eeyoreAST* t = target ? target : new _eVAR(varManager->newt());
if (param) pass();
eeyoreStmt(new _eFUNCRET(t, name));
return t;
}
eeyoreAST* _VAR::atomize(eeyoreAST* target) {
return new _eVAR(varManager->getEeyore(name)); /* atomize name */
}
eeyoreAST* _INTEGER::atomize(eeyoreAST* target) {
return new _eNUM(num);
}
/* ================================================= */
/* cjump */
/* - conditional jump to l */
/* - neg: jump when condition fails */
/* ================================================= */
void _EXPR::cjump(string l, bool neg) {
eeyoreAST* c = this->atomize(NULL);
eeyoreStmt(new _eIFGOTO(c, neg ? "==" : "!=", new _eNUM(0), l));
}
void _UNARY_OP::cjump(string l, bool neg) {
eeyoreAST* c = op->atomize(NULL);
if (symbol() == "!") neg = !neg;
eeyoreStmt(new _eIFGOTO(c, neg ? "==" : "!=", new _eNUM(0), l));
}
void _BINARY_OP::cjump(string l, bool neg) {
if (islogicop(symbol())) {
eeyoreAST* alop = lop->atomize(NULL);
eeyoreAST* arop = rop->atomize(NULL);
string _op = neg ? neg_logicop(symbol()) : symbol();
eeyoreStmt(new _eIFGOTO(alop, _op, arop, l));
}
else {
eeyoreAST* c = this->atomize(NULL);
eeyoreStmt(new _eIFGOTO(c, neg ? "==" : "!=", new _eNUM(0), l));
}
}
/* ================================================= */
/* pass */
/* - pass var within function call */
/* - using param, closely followed by calling */
/* ================================================= */
void _FUNC_CALL::pass() {
vector<eeyoreAST *> call_list {};
_CALL_LIST *cur = (_CALL_LIST *) param;
vector<int> &isvars = funcManager->query(name);
for (auto isvar: isvars) {
assert(cur->head != NULL);
if (isvar || cur->head->isvar())
call_list.push_back(cur->head->atomize(NULL));
else {
/* ought to compute addr */
_ARRAY_ITEM *ai = (_ARRAY_ITEM *) (cur->head);
eeyoreAST* t = new _eVAR(varManager->newt());
eeyoreAST* addr = ai->param->atomize(ai->name, NULL);
/* ai->name, name must be passed!!
no definition on _ADDR_LIST->atomize(void) */
eeyoreStmt(new _eBINARY(t, new _eVAR(ai->name), "+", addr));
call_list.push_back(t);
}
cur = (_CALL_LIST *) (cur->tail);
}
for (auto p: call_list)
eeyoreStmt(new _ePARAM(p));
}
/* ================================================= */
/* instantialize */
/* - eval constant var/arr */
/* - by varManager, must after construction */
/* ================================================= */
void _DEF_CONST_VAR::instantialize() {
varManager->instantialize(name);
}
void _DEF_CONST_ARR::instantialize() {
varManager->instantialize(name);
}
/* ================================================= */
/* initialize */
/* - initialize when construction */
/* - global var ought to be initialized in main */
/* ================================================= */
void _DEF_CONST_VAR::initialize(bool glb) {
assert(inits != NULL);
varManager->initialize(name, true, true);
}
void _DEF_CONST_ARR::initialize(bool glb) {
assert(inits != NULL);
varManager->initialize(name, false, true);
}
void _DEF_VAR::initialize(bool glb) {
bool zero_pad = !glb && (inits != NULL);
varManager->initialize(name, true, zero_pad);
}
void _DEF_ARR::initialize(bool glb) {
bool zero_pad = !glb && (inits != NULL);
varManager->initialize(name, false, zero_pad);
}
/* ================================================= */
/* translate */
/* - translate AST, and do the conversion. */
/* - ctn: continue to where */
/* - brk: break to where */
/* - glb: in global environment? */
/* ================================================= */
void _PROGRAM::translate(string ctn, string brk, bool glb) {
if (program)
program->translate(ctn, brk, glb);
eeyoreList.insert(eeyoreList.end(), eeyoreDeclList.begin(), eeyoreDeclList.end());
eeyoreDeclList.clear();
/* print decls in buffer into the entire code */
eeyoreRoot = new _eSEQ(eeyoreList);
}
void _EXPR::translate(string ctn, string brk, bool glb) {
/* do nothing , expr ;*/
}
void _STMT_SEQ::translate(string ctn, string brk, bool glb) {
if (head != NULL) head->translate(ctn, brk, glb);
if (tail != NULL) tail->translate(ctn, brk, glb);
}
void _IF::translate(string ctn, string brk, bool glb) {
string l = varManager->newl();
cond->cjump(l, true);
body->translate(ctn, brk, glb);
eeyoreStmt(new _eLABEL(l));
}
void _IF_ELSE::translate(string ctn, string brk, bool glb) {
string l1 = varManager->newl();
string l2 = varManager->newl();
cond->cjump(l1, true);
body->translate(ctn, brk, glb);
eeyoreStmt(new _eGOTO(l2));
eeyoreStmt(new _eLABEL(l1));
ebody->translate(ctn, brk, glb);
eeyoreStmt(new _eLABEL(l2));
}
void _WHILE::translate(string ctn, string brk, bool glb) {
string l1 = varManager->newl();
string l2 = varManager->newl();
eeyoreStmt(new _eLABEL(l1));
/* must set label first, then cjump */
cond->cjump(l2, true);
body->translate(l1, l2, glb);
eeyoreStmt(new _eGOTO(l1));
eeyoreStmt(new _eLABEL(l2));
}
void _RETURN_VOID::translate(string ctn, string brk, bool glb) {
eeyoreStmt(new _eRETVOID());
}
void _RETURN_EXPR::translate(string ctn, string brk, bool glb) {
eeyoreAST* t = expr->atomize(NULL);
eeyoreStmt(new _eRET(t));
}
void _CONTINUE::translate(string ctn, string brk, bool glb) {
eeyoreStmt(new _eGOTO(ctn));
}
void _BREAK::translate(string ctn, string brk, bool glb) {
eeyoreStmt(new _eGOTO(brk));
}
void _ASSIGN::translate(string ctn, string brk, bool glb) {
if (lop->isvar()) {
_VAR *var = (_VAR *)lop;
eeyoreAST* lval = new _eVAR(varManager->getEeyore(var->name));
eeyoreAST* rval = rop->atomize(lval);
if (lval != rval) /* optimization here: avoid redundant pass */
eeyoreStmt(new _eDIRECT(lval, rval));
}
else {
eeyoreAST* rval = rop->atomize(NULL);
_ARRAY_ITEM *ai = (_ARRAY_ITEM *)lop;
eeyoreAST* param = ai->param->atomize(ai->name, NULL);
/* remember to write ->atomize(name) !!! */
eeyoreStmt(new _eSAVE(varManager->getEeyore(ai->name), param, rval));
}
}
void _BLOCK::translate(string ctn, string brk, bool glb) {
varManager->newEnviron();
if (block) block->translate(ctn, brk, glb);
/* possible occassions: block body is empty */
varManager->deleteEnviron();
}
void _PARAM_LIST::translate(string ctn, string brk, bool glb) {
if (head) head->translate(ctn, brk, glb);
if (tail) tail->translate(ctn, brk, glb);
}
void _FUNC::translate(string ctn, string brk, bool glb) {
eeyoreList.insert(eeyoreList.end(), eeyoreDeclList.begin(), eeyoreDeclList.end());
eeyoreDeclList.clear();
/* print decls in buffer into the entire code */
varManager->newEnviron();
int p = funcManager->insert(name, param);
/* bool vector: is each param an var(T)/array(F) */
if (param) param->translate(ctn, brk, glb);
/* param = NULL in case func() {} */
if (name == "main") {
/* initialization of global var */
for (auto glb_var : globalInitList)
glb_var->initialize(true);
}
assert(body != NULL);
body->translate(ctn, brk, false);
if (isvoid) eeyoreStmt(new _eRETVOID());
// else eeyoreStmt(new _eRET(new _eNUM(0)));
eeyoreDeclList.insert(eeyoreDeclList.end(), eeyoreStmtList.begin(), eeyoreStmtList.end());
eeyoreStmtList.clear();
/* concantenate decls with normal codes */
eeyoreAST *func = new _eFUNC(name, p, new _eSEQ(eeyoreDeclList));
eeyoreDeclList.clear();
/* print codes buffer into the structure of function */
eeyoreDecl(func);
varManager->deleteEnviron();
}
void _FUNC_CALL::translate(string ctn, string brk, bool glb) {
if (param) pass();
/* param = NULL in case func() */
eeyoreStmt(new _eCALL(name));
}
void _DEF_VAR::translate(string ctn, string brk, bool glb) {
vector<int> shape {};
dataDescript *dd = new dataDescript(name, shape, (_TREE *)inits, isconst());
varManager->insert(dd);
instantialize();
if (!glb)
initialize(glb);
else /* delay global variables' initialization, do it in main */
globalInitList.push_back(this);
}
void _PARAM_VAR::translate(string ctn, string brk, bool glb) {
vector<int> shape {};
dataDescript *dd = new dataDescript(name, shape, (_TREE *)inits, isconst());
varManager->insert(dd, true);
}
void _DEF_ARR::translate(string ctn, string brk, bool glb) {
vector<int> shape {};
addr->vectorize(shape);
dataDescript *dd = new dataDescript(name, shape, (_TREE *)inits, isconst());
varManager->insert(dd);
instantialize();
if (!glb)
initialize(glb);
else
globalInitList.push_back(this);
}
void _PARAM_ARR::translate(string ctn, string brk, bool glb) {
vector<int> shape {0};
if (addr) addr->vectorize(shape);
/* addr = NULL in case name[] */
dataDescript *dd = new dataDescript(name, shape, (_TREE *)inits, isconst());
varManager->insert(dd, true);
}
/* ================================================= */
/* _FUNC_CALL::_FUNC_CALL */
/* - support for stoptime & starttime */
/* ================================================= */
_FUNC_CALL::_FUNC_CALL(string _name, sysyAST *_param):
name(_name), param(_param) {
/* stoptime & starttime; */
if (_name == "starttime") {
name = "_sysy_starttime";
param = (sysyAST *) new _CALL_LIST(new _INTEGER(yylineno), NULL);
}
if (_name == "stoptime") {
name = "_sysy_stoptime";
param = (sysyAST *) new _CALL_LIST(new _INTEGER(yylineno), NULL);
}
}