-
Notifications
You must be signed in to change notification settings - Fork 2
/
DagMap.cpp
440 lines (425 loc) · 11.9 KB
/
DagMap.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
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
440
#include"DagMap.h"
//叶子节点构造函数
DagNode::DagNode(int _leafInformation, bool _isImmediate) {
isLeaf = true;
leafOperand = _leafInformation;
isImmediate = _isImmediate;
if (!isImmediate) {
possibleNames.insert(leafOperand);
}
}
//非叶子节点构造函数
DagNode::DagNode(MidCodeOp _op, DagNode* _left, DagNode* _right) {
isLeaf = false;
op = _op;
left = _left;
right = _right;
if (nullptr != left) {
left->father.insert(this);
son.insert(left);
}
if (nullptr != right) {
right->father.insert(this);
son.insert(right);
}
}
DagMap::DagMap(set<int> _mustOut,set<int>_shouldAssign){
label=MIDNOLABEL;
mustOut=_mustOut;
shouldAssign=_shouldAssign;
fakeOutputStream=MidCode::tmpVarAlloc();
}
DagMap::~DagMap(){
for(DagNode* i:nodes){
delete i;
}
}
DagNode* DagMap::getNodeByVar(int id,bool isImmediate){
if(isImmediate){
if(immToNode.find(id)!=immToNode.end()){
return immToNode[id];
}
else {
DagNode* tmp=new DagNode(id,isImmediate);
immToNode[id]=tmp;
nodes.push_back(tmp);
return tmp;
}
}
else{
if(varToNode.find(id)!=varToNode.end()){
return varToNode[id];
}
else {
DagNode* tmp=new DagNode(id,isImmediate);
varToNode[id]=tmp;
nodes.push_back(tmp);
return tmp;
}
}
}
DagNode* DagMap::getNodeBySon(MidCodeOp op,DagNode* left,DagNode* right){
for(DagNode* i:nodes){
if(i->op==op&&i->left==left&&i->right==right){
return i;
}
else if((op == MIDADD || op == MIDMULT || op == MIDEQL || op == MIDNEQ)
&&i->op==op&&i->left==right&&i->right==left){
return i;
}
}
DagNode *tmp=new DagNode(op,left,right);
nodes.push_back(tmp);
return tmp;
}
void DagMap::handleMidCode(MidCode c){
if(c.labelNo!=MIDNOLABEL){
label=c.labelNo;//标签只能出现在一个基本块的第一个语句上;
}
switch (c.op) {
case MIDFUNC:
case MIDPARA:
case MIDNOP:
//只能出现在最前部,不会影响DAG
beginning.push_back(c);
break;
//只能出现在最后,不会影响DAG
case MIDGOTO:
case MIDCALL:
case MIDREADCHAR:
case MIDREADINTEGER:
ending.push_back(c);
break;
case MIDPUSH:
case MIDRET:
case MIDBNZ:
case MIDBZ:
mustOut.insert(c.operand1);
ending.push_back(c);
break;
case MIDADD:
case MIDSUB:
case MIDMULT:
case MIDDIV:
case MIDLSS:
case MIDLEQ:
case MIDGRE:
case MIDGEQ:
case MIDEQL:
case MIDNEQ:
case MIDREM:
case MIDARRAYGET:
{
DagNode* node1 = getNodeByVar(c.operand1, c.isImmediate1);
DagNode* node2 = getNodeByVar(c.operand2, c.isImmediate2);
DagNode* node = getNodeBySon(c.op, node1, node2);
if (varToNode.find(c.target) != varToNode.end() && varToNode[c.target] != node) {
//出现了先引用再赋值的情况
varToNode[c.target]->possibleNames.erase(c.target);//使旧变量无法再被命名为c.target;
if (assigned.find(c.target)==assigned.end()) {
//对于第一次重新赋值的变量,记录他的旧名字。
//这里要防范一个问题就是一个变量上来就直接被赋值了,用另一个变量或者立即数
//寻找的标准是,未被赋值但是使用过的变量,是否被使用过由varToNode判断,是否被赋值过由assigned判断
varToNode[c.target]->shouldReAssign = true;
varToNode[c.target]->formerName = c.target;
}
}
varToNode[c.target] = node;
node->possibleNames.insert(c.target);
assigned.insert(c.target);
if (c.target > 0) {
SymbolEntry* entry = MidCode::table->getSymbolById(c.target);
if (entry->scope == "") {
mustOut.insert(c.target);
}
}
break;
}
case MIDNEGATE:
{
DagNode* node1 = getNodeByVar(c.operand1, c.isImmediate1);
DagNode* node = getNodeBySon(c.op, node1, nullptr);
if (varToNode.find(c.target) != varToNode.end() && varToNode[c.target] != node) {
//出现了先引用再赋值的情况
varToNode[c.target]->possibleNames.erase(c.target);//使旧变量无法再被命名为c.target;
if (assigned.find(c.target) == assigned.end()) {
//对于第一次重新赋值的变量,记录他的旧名字。
varToNode[c.target]->shouldReAssign = true;
varToNode[c.target]->formerName = c.target;
}
}
varToNode[c.target] = node;
node->possibleNames.insert(c.target);
assigned.insert(c.target);
if (c.target > 0) {
SymbolEntry* entry = MidCode::table->getSymbolById(c.target);
if (entry->scope == "") {
mustOut.insert(c.target);
}
}
break;
}
case MIDASSIGN:
{
//这里有一个-1:RET的问题:拿返回值赋值只能是第一句,我们必须保证这是第一句
if (c.operand1 == -1) {
beginning.push_back(c);
shouldAssign.insert(c.operand1);
break;
}
DagNode* node1 = getNodeByVar(c.operand1, c.isImmediate1);
if (varToNode.find(c.target) != varToNode.end() && varToNode[c.target] != node1) {
//出现了先引用再赋值的情况
varToNode[c.target]->possibleNames.erase(c.target);//使旧变量无法再被命名为c.target;
if (assigned.find(c.target) == assigned.end()) {
//对于第一次重新赋值的变量,记录他的旧名字。
varToNode[c.target]->shouldReAssign = true;
varToNode[c.target]->formerName = c.target;
}
}
varToNode[c.target] = node1;
node1->possibleNames.insert(c.target);
assigned.insert(c.target);
if (c.target > 0) {
SymbolEntry* entry = MidCode::table->getSymbolById(c.target);
if (entry->scope == "") {
mustOut.insert(c.target);
}
}
break;
}
case MIDPRINTCHAR:
case MIDPRINTINT:
{
DagNode* node1 = getNodeByVar(c.operand1, c.isImmediate1);
DagNode* node2 = getNodeByVar(fakeOutputStream, false);
//必须直接新建,输出一样的东西两次不能当一次
DagNode* node = new DagNode(c.op, node1, node2);
nodes.push_back(node);
//肯定重新赋值,
if (varToNode.find(fakeOutputStream) != varToNode.end()) {
varToNode[fakeOutputStream]->possibleNames.erase(fakeOutputStream);
}
node->possibleNames.insert(fakeOutputStream);
varToNode[fakeOutputStream] = node;
break;
}
case MIDPRINTSTRING:
{
//把字符串编号偷偷藏在leafOperand里面
DagNode* node2 = getNodeByVar(fakeOutputStream, false);
DagNode* node = new DagNode(c.op, nullptr, node2);
node->leafOperand = c.operand1;//字符串编号
nodes.push_back(node);
//若有,肯定重新赋值
if (varToNode.find(fakeOutputStream) != varToNode.end()) {
varToNode[fakeOutputStream]->possibleNames.erase(fakeOutputStream);
}
node->possibleNames.insert(fakeOutputStream);
varToNode[fakeOutputStream] = node;
break;
}
case MIDARRAYWRITE:
{
DagNode* node1 = getNodeByVar(c.operand1, c.isImmediate1);
DagNode* node2 = getNodeByVar(c.operand2, c.isImmediate2);
DagNode* node = new DagNode(c.op, node1, node2);
nodes.push_back(node);
//bfs
if (varToNode.find(c.target) != varToNode.end()) {
deque<DagNode*>queue;
queue.push_back(varToNode[c.target]);
//没有环,不用检查重复
while (queue.size() > 0) {
DagNode* mark = queue.front();
for (DagNode* i : mark->father) {
if (i != node) {
queue.push_back(i);
}
}
queue.pop_front();
mark->father.insert(node);
node->son.insert(mark);
}
}
node->possibleNames.insert(c.target);
node->leafOperand = c.target;
varToNode[c.target] = node;
break;
}
default:
cout << "miss";
}
}
vector<MidCode> DagMap::dumpMidCode() {
//分配名字
for (DagNode* i : nodes) {
if (i->isLeaf&&i->isImmediate) {
//立即数
i->name = i->leafOperand;
}
else if (i->isLeaf && !i->isImmediate && i->possibleNames.find(i->leafOperand) != i->possibleNames.end()) {
//叶子节点要有初值,如果不发生先使用再赋值的事,必须考虑用初始节点
i->name = i->leafOperand;
mustOut.erase(i->name);
}
else {
//靠possibleNames的
if (i->possibleNames.size() == 0) {
i->name = MidCode::tmpVarAlloc();
}
else {
bool given = false;
for (int j : i->possibleNames) {
if (mustOut.find(j) != mustOut.end()) {
i->name = j;
given = true;
mustOut.erase(j);
break;
}
}
if (!given) {
i->name = *(i->possibleNames.begin());
}
}
if (i->shouldReAssign && shouldAssign.find(i->formerName) != shouldAssign.end()) {
//该变量shouldassign被标记为true且在开头处活跃,生成赋值语句
MidCode code;
int tmp1 = MidCode::tmpVarAlloc();
code = MidCode::generateMidCode(MIDASSIGN, tmp1,
i->formerName, false, MIDUNUSED, false, MIDNOLABEL);
/*.op = MIDASSIGN; code.target = i->name;
code.operand1 = i->formerName; code.isImmediate1 = false;
code.operand2 = MIDUNUSED; code.isImmediate2 = false, code.labelNo = MIDNOLABEL;
*/
beginning.push_back(code);
code = MidCode::generateMidCode(MIDASSIGN, i->name,
tmp1, false, MIDUNUSED, false, MIDNOLABEL);
middle.push_back(code);
}
}
}
//开始确定导出顺序
vector<DagNode*>q;
int i = 0;
DagNode* tmp = nullptr;
while (i < nodes.size() ) {
tmp = nullptr;
for (int j = 0; j < nodes.size(); j++) {
if (nodes[j]->dumped) { continue; }
else if (nodes[j]->isLeaf) {
i++;
nodes[j]->dumped = true;
}
else if (nodes[j]->father.size() == 0) {
tmp = nodes[j];
break;
}
}
while (tmp != nullptr && !tmp->dumped) {
i++;
tmp->dumped = true;
q.push_back(tmp);
for (DagNode* son : tmp->son) {
son->father.erase(tmp);
}
if (tmp->left != NULL&&tmp->left->father.size()==0) {
tmp = tmp->left;
continue;
}
if (tmp->right != NULL && tmp->right->father.size() == 0) {
tmp = tmp->right;
continue;
}
}
}
//开始按顺序生成中间代码
for (int i = q.size() - 1; i >= 0; i--) {
MidCode code = nodeToMidCode(q[i]);
middle.push_back(code);
}
//开始根据mustout生成赋值语句
for (int i : mustOut) {
if (varToNode.find(i) != varToNode.end()) {
DagNode* node = varToNode[i];
MidCode code;
code.op = MIDASSIGN; code.target = i;
code.operand1 = node->name; code.isImmediate1 = node->isImmediate;
code.operand2 = MIDUNUSED; code.isImmediate2 = false, code.labelNo = MIDNOLABEL;
middle.push_back(code);
}
}
//开始合并
beginning.insert(beginning.end(), middle.begin(), middle.end());
beginning.insert(beginning.end(), ending.begin(), ending.end());
if (label != MIDNOLABEL) {
if (beginning.size() == 0) {
MidCode code;
code.op = MIDNOP; code.target = i;
code.operand1 = -1; code.isImmediate1 =false;
code.operand2 = -1; code.isImmediate2 = false, code.labelNo = MIDNOLABEL;
beginning.push_back(code);
}
beginning[0].labelNo = label;
}
return beginning;
}
MidCode DagMap::nodeToMidCode(DagNode* node) {
switch (node->op) {
case MIDADD:
case MIDSUB:
case MIDMULT:
case MIDDIV:
case MIDLSS:
case MIDLEQ:
case MIDGRE:
case MIDGEQ:
case MIDEQL:
case MIDNEQ:
case MIDARRAYGET:
case MIDARRAYWRITE:
case MIDREM:
{
MidCode res;
res.op = node->op;
res.target = node->name;
res.operand1 = node->left->name;
res.isImmediate1 = node->left->isImmediate;
res.operand2 = node->right->name;
res.isImmediate2 = node->right->isImmediate;
res.labelNo = MIDNOLABEL;
return res;
}
case MIDNEGATE:
{
MidCode res;
res.op = node->op;
res.target = node->name;
res.operand1 = node->left->name;
res.isImmediate1 = node->left->isImmediate;
res.operand2 = -1; res.isImmediate2 = false; res.labelNo = MIDNOLABEL;
return res;
}
case MIDPRINTCHAR:
case MIDPRINTINT:
{
MidCode res;
res.op = node->op;
res.target = -1;
res.operand1 = node->left->name;
res.isImmediate1 = node->left->isImmediate;
res.operand2 = -1; res.isImmediate2 = false; res.labelNo = MIDNOLABEL;
return res;
}
case MIDPRINTSTRING:
{
MidCode res;
res.op = node->op;
res.target = -1;
res.operand1 = node->leafOperand;
res.isImmediate1 = false;
res.operand2 = -1; res.isImmediate2 = false; res.labelNo = MIDNOLABEL;
return res;
}
}
}