-
Notifications
You must be signed in to change notification settings - Fork 0
/
actvar.cpp
299 lines (269 loc) · 8.49 KB
/
actvar.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
#include "stdafx.h"
vector<vector<bblk> > bblktbl;
bool isjmp(qi &q)
{
switch(q.op)
{
case qi::JEQ: case qi::JGE: case qi::JGT: case qi::JLE:
case qi::JLT: case qi::JNE: case qi::JMP: case qi::JZ:
case qi::RET:
return true;
default:
return false;
}
}
bool isjc(qi &q)
{
switch(q.op)
{
case qi::JEQ: case qi::JGE: case qi::JGT: case qi::JLE:
case qi::JLT: case qi::JZ: case qi::JNE:
return true;
default:
return false;
}
}
//return id of bblock; if the label points the exit, return -1
int lbltobblock(qoperand lbl, vector<qi> &qilist, vector<int> &entrcnt)
{
size_t n=0;
size_t len = qilist.size();
for(n=0; n<len; ++n)
{
if(qilist[n].op == qi::LABEL && qilist[n].A.value == lbl.value)
break;
}
assert(n<len);//supposed that would be found
do{++n;}while(n<len && qilist[n].op == qi::LABEL);//jump the label
if(n<len)
return entrcnt[n];
else
return -1;
}
//non-repeat push_back
void push_back_nr(vector<int> &vec, int val)
{
for(int item: vec)
if(item == val)
return;
vec.push_back(val);
}
//build basic block for all functions
void buildbblks()
{
//clear bblktbl
vector<vector<bblk> > newbblktbl(functbl.size(), vector<bblk>());
bblktbl.swap(newbblktbl);
newbblktbl.clear();
for(size_t ct=0; ct<functbl.size(); ++ct)
{//does here need LOADCONTEXT?
vector<qi> &qilist = functbl[ct].qilist;
size_t len = qilist.size();
vector<int> entrcnt(len, -1);
if(len == 0)
continue;
vector<bblk> &bblks = bblktbl[ct];
int cnt = 0;
//find all entrances
for(size_t n=0; n<len; ++n)
{
if(n==0)
{
while(n<len && qilist[n].op == qi::LABEL)
++n;
if(n<len)
bblks.push_back(bblk(cnt,n));
entrcnt[n]=cnt++;
}
//statement with labels or statement after jump instructions
if(qilist[n].op == qi::LABEL || isjmp(qilist[n]))
{
do{++n;}while(n<len && qilist[n].op == qi::LABEL);
if(n<len)
{
//cerr<<n<<" ";
bblks.push_back(bblk(cnt, n));
entrcnt[n--]=cnt++;//
}
}
}
//fill exits and flow info
for(bblk &b: bblks)
{
//fill exits point
size_t n=b.entrance;
do{++n;}while(n<len && entrcnt[n]<0);//move n to next entrance
do{--n;}while(qilist[n].op == qi::LABEL);//move n to the exit of the bblk
b.exit = n;
//fill flow info, i.e. to and from
if(qilist[n].op == qi::RET)
{
push_back_nr(b.to,-1);//flow to exit
continue;
}
if(qilist[n].op == qi::JMP)
{
int bbid = lbltobblock(qilist[n].D, qilist, entrcnt);
push_back_nr(b.to, bbid);//flow to destination
if(bbid>=0)
push_back_nr(bblks[bbid].from, b.id);//flow from b
continue;
}
//maybe flow into next bblock
int next = b.id+1;
if(size_t(next)>=bblks.size())
next = -1;//exit block
push_back_nr(b.to, next);
if(next>=0)
push_back_nr(bblks[next].from, b.id);
//maybe flow to other bblock
if(isjc(qilist[n]))
{
int bbid = lbltobblock(qilist[n].D, qilist, entrcnt);
push_back_nr(b.to, bbid);
if(bbid>=0)
push_back_nr(bblks[bbid].from, b.id);
}
}
push_back_nr(bblks[0].from,-1);//begin->block0
if(flg_debug){
cerr<<"BBLOCKs in "<<glbtbl[functbl[ct].index].name<<"():"<<endl;
for(bblk b : bblks)
{
cerr<<setw(4)<<b.id<<setw(4)<<b.entrance<<setw(4)<<b.exit<<" [";
for(int e: b.from)
cerr << setw(4) <<e;
cerr<<" ] [";
for(int t: b.to)
cerr << setw(4) <<t;
cerr<<" ]"<<endl;
}
cerr<<endl;
}
}
}
string actset2str(set<int> &theset, funcitem &func)
{
ostringstream o;
for(int i: theset)
o<<func.lcltbl[i].name<<" ";
return "{ "+o.str()+"}";
}
void actvar(funcitem &func, vector<bblk> &bblks, actvartbl &answer)
{
vector<qi> &qilist = func.qilist;
int bblkn = bblks.size();
vector<set<int>> uses(bblkn, set<int>());
vector<set<int>> defs(bblkn, set<int>());
vector<set<int>> ins(bblkn, set<int>());
vector<set<int>> outs(bblkn, set<int>());
//fill in use and def
for(int n=0; n<bblkn; ++n)//for each bblocks
{
bblk &b = bblks[n];
set<int> &use=uses[n];
set<int> &def=defs[n];
for(int m=b.entrance; m<=b.exit; ++m)//for each qi in bblocks
{
qi &q = qilist[m];
//use
switch(q.op)
{
case qi::ASSIGN: case qi::NEG:
case qi::JZ: case qi::RET:
case qi::PUSH: case qi::WR:
if(q.A.type == qoperand::LCL_OBJ &&
def.find(q.A.value)==def.end())
use.insert(q.A.value);
}
switch(q.op)
{
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL: //binary arithmatic
case qi::JEQ: case qi::JGE: case qi::JGT: case qi::JLE:
case qi::JLT: case qi::JNE: //binary cmp
if(q.A.type == qoperand::LCL_OBJ &&
def.find(q.A.value)==def.end())
use.insert(q.A.value);
if(q.B.type == qoperand::LCL_OBJ &&
def.find(q.B.value)==def.end())
use.insert(q.B.value);
}
switch(q.op)
{
case qi::ARRAYASS:
if(q.D.type == qoperand::LCL_OBJ &&
def.find(q.D.value)==def.end())
use.insert(q.D.value);
case qi::ARRAYLOAD:
if(q.B.type == qoperand::LCL_OBJ &&
def.find(q.B.value)==def.end())
use.insert(q.B.value);
}
//def
switch(q.op)
{
case qi::ASSIGN: case qi::NEG://unary arithmetic
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL: //binary arithmetic
case qi::ARRAYLOAD: case qi::CALL:
if(q.D.type == qoperand::LCL_OBJ &&
use.find(q.D.value)==use.end())
def.insert(q.D.value);
break;
case qi::RD:
if(q.A.type == qoperand::LCL_OBJ &&
use.find(q.A.value)==use.end())
def.insert(q.A.value);
}
}
}
//loop and expand sets by flow
bool flag=true;//need another loop?
int num;
while(flag)
{
flag = false;
for(int n=bblkn-1; n>=0; --n)//for each bblocks
{
bblk &b = bblks[n];
set<int> &use=uses[n];
set<int> &def=defs[n];
set<int> &in=ins[n];
set<int> &out=outs[n];
//`out` = Union( `in` of every `to` )
num=out.size();
for(int i: b.to)
{
if(i>=0)
out.insert(ins[i].begin(), ins[i].end());
}
if(out.size()-num>0)//changed
flag = true;
//`in` = (`out` - `def`) union `use`
in.clear();
in.insert(out.begin(), out.end());
for(int a: def)
{
auto iter = in.find(a);
if(iter!=in.end())
in.erase(iter);
}
in.insert(use.begin(), use.end());
}
}
if(flg_debug){
cerr<<"Active varible of function "<<glbtbl[func.index].name<<"():"<<endl;
cerr<<setw(4)<<"BLK"<<setw(15)<<"def"<<setw(15)<<"use"<<setw(15)<<"in"<<setw(15)<<"out"<<endl;
for(int n=0; n<bblkn; ++n)
cerr<<setw(4)<<n<<setw(15)<<actset2str(defs[n],func)
<<setw(15)<<actset2str(uses[n],func)
<<setw(15)<<actset2str(ins[n],func)
<<setw(15)<<actset2str(outs[n],func)<<endl;
cerr<<endl;
}
answer.use.swap(uses);
answer.def.swap(defs);
answer.in.swap(ins);
answer.out.swap(outs);
}