-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysyLUT.cpp
More file actions
163 lines (157 loc) · 6.41 KB
/
Copy pathsysyLUT.cpp
File metadata and controls
163 lines (157 loc) · 6.41 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
#ifndef defs_hpp
#include "defs.hpp"
#define defs_hpp
#endif
using namespace std;
/* ====================================================== */
/* dataDescript */
/* - declaration */
/* ====================================================== */
dataDescript::dataDescript(string _name, vector<int> _shape, _TREE *_node, bool _isconst):
name(_name), shape(_shape), isconst(_isconst), inits(NULL) {
int length = shape.size();
if (length > 0) {
shape_all.resize(length);
shape_all[length - 1] = 1;
for (int i = length - 2; i >= 0; i--)
shape_all[i] = shape_all[i + 1] * shape[i + 1];
}
/* the total length of dim i */
if (_node == NULL) return ;
if (_node->leaf())
inits = (dataCell *) new dataLeaf(shape.size(), _node->getExpr());
else {
assert(_node->getSibling() == NULL);
inits = construct(new dataAggr(-1), _node->getChild());
}
while (inits->depth > 0) {
dataAggr *anc = new dataAggr(inits->depth - 1);
anc->aggr.push_back(inits);
inits = anc;
}
}
/* ====================================================== */
/* dataDescript::eval(addr) */
/* - return value array's given position */
/* ====================================================== */
int dataDescript::eval(vector<int> &addr) {
if (inits == NULL) return 0;
dataAggr *node = (dataAggr *)inits;
for (auto x: addr) {
if (node->aggr.size() < x) return 0;
if (!node->aggr[x]) return 0;
node = (dataAggr *)node->aggr[x];
}
return ((dataLeaf *)node)->expr->eval();
}
/* ====================================================== */
/* dataDescript::construct(tree, node) */
/* - private internal function */
/* - construct dataAggr from original _TREE */
/* ====================================================== */
dataCell* dataDescript::construct(dataAggr *tree, _TREE *node) {
assert(node != NULL);
dataCell *incr;
if (node->leaf())
incr = (dataCell*) new dataLeaf(shape.size(), node->getExpr());
else
incr = construct(new dataAggr(-1), node->getChild());
tree->liftup(shape, incr->depth);
tree->aggr.push_back(incr);
return node->sibling ? construct(tree, node->sibling) : tree->prune(shape);
}
/* ====================================================== */
/* VarManager::insert(dd, param ) */
/* - given data-descipt, register for var */
/* - param, if it is param in function */
/* ====================================================== */
void VarManager::insert(dataDescript* dd, bool param) {
if (param)
dd->eeyore = newp();
else
dd->eeyore = dd->shape.empty() ? newT(dd->isconst) : newT(flatten(dd->shape), dd->isconst);
/* decl print in newT */
if (table.find(dd->name) != table.end()) {
dataDescript* old = table[dd->name];
record.top().push_back( ddPair(old, dd));
}
else {
record.top().push_back( ddPair(NULL, dd));
}
table[dd->name] = dd;
}
/* ====================================================== */
/* VarManager::deleteEnviron */
/* - exit a function, and delete its environ */
/* - vars with multiple names outside this function */
/* is now restored */
/* ====================================================== */
void VarManager::deleteEnviron() {
for (auto item: record.top())
table[item.second->name] = item.first;
record.pop();
}
/* ====================================================== */
/* VarManager::instantialize */
/* - convert constant expr to integer */
/* - called after var inserted into varManager */
/* ====================================================== */
void VarManager::instantialize(string name) {
/* convert constant expr to integer */
assert(table.find(name) != table.end());
assert(table[name] != NULL);
vector<int> &shape = table[name]->shape;
dataCell *inits = table[name]->inits;
if (inits) inits->instantialize();
}
/* ====================================================== */
/* VarManager::initialization */
/* - initialize the vars */
/* - called after var inserted into varManager */
/* - zero-pad: for global vars, 0 padded when flag on*/
/* ====================================================== */
void VarManager::initialize(string name, bool var, bool zero_pad) {
/* do original assignment */
assert(table.find(name) != table.end());
assert(table[name] != NULL);
vector<int> &shape = table[name]->shape;
dataCell *&inits = table[name]->inits;
string eeyore = table[name]->eeyore;
if (zero_pad) zero_padding(inits, shape, 0);
if (inits) inits->initialize(eeyore, shape, 0, var);
}
/* ====================================================== */
/* FuncManager::FuncManager */
/* - runtime function should be registered here */
/* ====================================================== */
FuncManager::FuncManager() {
vector<int> uniarr {0};
vector<int> univar {1};
vector<int> novar {};
vector<int> mix {1, 0};
table["getint"] = novar;
table["getch"] = novar;
table["getarray"] = uniarr;
table["putint"] = univar;
table["putch"] = univar;
table["putarray"] = mix;
table["_sysy_starttime"] = univar;
table["_sysy_stoptime"] = univar;
}
/* ====================================================== */
/* FuncManager::insert(name, param */
/* - register a function */
/* ====================================================== */
int FuncManager::insert(string name, sysyAST *param) {
assert(table.find(name) == table.end());
vector<int> plist {};
if (param != NULL) {
_PARAM_LIST *cur = (_PARAM_LIST *) param;
while (cur != NULL) {
plist.push_back(cur->head->isvar());
cur = (_PARAM_LIST *) (cur->tail);
}
}
table[name] = plist;
return plist.size();
}