1
+ #include " BlockOptimization.h"
2
+ BlockOptimization::BlockOptimization (set<int >&_activeOut) {
3
+ activeOut = _activeOut;
4
+ }
5
+ vector<MidCode>BlockOptimization::propagationInBlock (vector<MidCode>& v) {
6
+ map<int , Item>substitution;
7
+ vector<MidCode>res;
8
+ vector<MidCode>resEnd;
9
+ for (int i = 0 ; i < v.size (); i++) {
10
+ MidCode c = v[i];
11
+ if (c.labelNo != -1 ) {
12
+ label = c.labelNo ;
13
+ }
14
+ switch (c.op ) {
15
+ // 只使用op1,且只会出现在最后
16
+ case MIDPUSH:
17
+ case MIDRET:
18
+ case MIDBNZ:
19
+ case MIDBZ:
20
+ {
21
+ MidCode tmp = c;
22
+ if (c.operand1 != -1 && !c.isImmediate1 &&
23
+ substitution.find (c.operand1 ) != substitution.end ()) {
24
+ tmp.operand1 = substitution[c.operand1 ].id ;
25
+ tmp.isImmediate1 = substitution[c.operand1 ].isImmediate ;
26
+ }
27
+ resEnd.push_back (tmp);
28
+ break ;
29
+ }
30
+ // 只使用op1
31
+ case MIDPRINTINT:
32
+ case MIDPRINTCHAR:
33
+ {
34
+ MidCode tmp = c;
35
+ if (c.operand1 != -1 && !c.isImmediate1 &&
36
+ substitution.find (c.operand1 ) != substitution.end ()) {
37
+ tmp.operand1 = substitution[c.operand1 ].id ;
38
+ tmp.isImmediate1 = substitution[c.operand1 ].isImmediate ;
39
+ }
40
+ res.push_back (tmp);
41
+ break ;
42
+ }
43
+ case MIDADD:
44
+ case MIDSUB:
45
+ case MIDMULT:
46
+ case MIDDIV:
47
+ case MIDLSS:
48
+ case MIDLEQ:
49
+ case MIDGRE:
50
+ case MIDGEQ:
51
+ case MIDEQL:
52
+ case MIDNEQ:
53
+ {
54
+ MidCode tmp = c;
55
+ if (c.operand1 != -1 && !c.isImmediate1 &&
56
+ substitution.find (c.operand1 ) != substitution.end ()) {
57
+ tmp.operand1 = substitution[c.operand1 ].id ;
58
+ tmp.isImmediate1 = substitution[c.operand1 ].isImmediate ;
59
+ }
60
+ if (c.operand2 != -1 && !c.isImmediate2 &&
61
+ substitution.find (c.operand2 ) != substitution.end ()) {
62
+ tmp.operand2 = substitution[c.operand2 ].id ;
63
+ tmp.isImmediate2 = substitution[c.operand2 ].isImmediate ;
64
+ }
65
+ set<int >del;
66
+ for (map<int , Item>::iterator itr = substitution.begin ();
67
+ itr != substitution.end (); itr++) {
68
+ if (!itr->second .isImmediate && itr->second .id == c.target ) {
69
+ MidCode tmp2;
70
+ tmp2.op = MIDASSIGN; tmp2.target = itr->first ;
71
+ tmp2.operand1 = itr->second .id ; tmp2.isImmediate1 = itr->second .isImmediate ;
72
+ tmp2.operand2 = -1 ; tmp2.isImmediate2 = false ;
73
+ tmp2.labelNo = -1 ;
74
+ res.push_back (tmp2);
75
+ del.insert (itr->first );
76
+ }
77
+ }
78
+ for (int j : del) {
79
+ substitution.erase (j);
80
+ }
81
+ res.push_back (tmp);
82
+ break ;
83
+ }
84
+ case MIDNEGATE:
85
+ {
86
+ MidCode tmp = c;
87
+ if (c.operand1 != -1 && !c.isImmediate1 &&
88
+ substitution.find (c.operand1 ) != substitution.end ()) {
89
+ tmp.operand1 = substitution[c.operand1 ].id ;
90
+ tmp.isImmediate1 = substitution[c.operand1 ].isImmediate ;
91
+ }
92
+ set<int >del;
93
+ for (map<int , Item>::iterator itr = substitution.begin ();
94
+ itr != substitution.end (); itr++) {
95
+ if (!itr->second .isImmediate && itr->second .id == c.target ) {
96
+ MidCode tmp2;
97
+ tmp2.op = MIDASSIGN; tmp2.target = itr->first ;
98
+ tmp2.operand1 = itr->second .id ; tmp2.isImmediate1 = itr->second .isImmediate ;
99
+ tmp2.operand2 = -1 ; tmp2.isImmediate2 = false ;
100
+ tmp2.labelNo = -1 ;
101
+ res.push_back (tmp2);
102
+ del.insert (itr->first );
103
+ }
104
+ }
105
+ for (int j : del) {
106
+ substitution.erase (j);
107
+ }
108
+ res.push_back (tmp);
109
+ break ;
110
+ }
111
+ case MIDASSIGN:
112
+ {
113
+ if (!c.isImmediate1 && c.operand1 == -1 ) {
114
+ // ret
115
+ res.push_back (c);
116
+ break ;
117
+ }
118
+ Item tmp;
119
+ tmp.id = c.operand1 ;
120
+ tmp.isImmediate = c.isImmediate1 ;
121
+ substitution[c.target ] = tmp;
122
+ break ;
123
+ }
124
+ case MIDARRAYGET:
125
+ {
126
+ MidCode tmp = c;
127
+ if (c.operand2 != -1 && !c.isImmediate2 &&
128
+ substitution.find (c.operand2 ) != substitution.end ()) {
129
+ tmp.operand2 = substitution[c.operand2 ].id ;
130
+ tmp.isImmediate2 = substitution[c.operand2 ].isImmediate ;
131
+ }
132
+ set<int >del;
133
+ for (map<int , Item>::iterator itr = substitution.begin ();
134
+ itr != substitution.end (); itr++) {
135
+ if (!itr->second .isImmediate && itr->second .id == c.target ) {
136
+ MidCode tmp2;
137
+ tmp2.op = MIDASSIGN; tmp2.target = itr->first ;
138
+ tmp2.operand1 = itr->second .id ; tmp2.isImmediate1 = itr->second .isImmediate ;
139
+ tmp2.operand2 = -1 ; tmp2.isImmediate2 = false ;
140
+ tmp2.labelNo = -1 ;
141
+ res.push_back (tmp2);
142
+ del.insert (itr->first );
143
+ }
144
+ }
145
+ for (int j : del) {
146
+ substitution.erase (j);
147
+ }
148
+ res.push_back (tmp);
149
+ break ;
150
+ }
151
+ case MIDARRAYWRITE:
152
+ {
153
+ MidCode tmp = c;
154
+ if (c.operand1 != -1 && !c.isImmediate1 &&
155
+ substitution.find (c.operand1 ) != substitution.end ()) {
156
+ tmp.operand1 = substitution[c.operand1 ].id ;
157
+ tmp.isImmediate1 = substitution[c.operand1 ].isImmediate ;
158
+ }
159
+ if (c.operand2 != -1 && !c.isImmediate2 &&
160
+ substitution.find (c.operand2 ) != substitution.end ()) {
161
+ tmp.operand2 = substitution[c.operand2 ].id ;
162
+ tmp.isImmediate2 = substitution[c.operand2 ].isImmediate ;
163
+ }
164
+ res.push_back (tmp);
165
+ break ;
166
+ }
167
+ case MIDREADINTEGER:
168
+ case MIDREADCHAR:
169
+ {
170
+ MidCode tmp=c;
171
+ set<int >del;
172
+ for (map<int , Item>::iterator itr = substitution.begin ();
173
+ itr != substitution.end (); itr++) {
174
+ if (!itr->second .isImmediate && itr->second .id == c.target ) {
175
+ MidCode tmp2;
176
+ tmp2.op = MIDASSIGN; tmp2.target = itr->first ;
177
+ tmp2.operand1 = itr->second .id ; tmp2.isImmediate1 = itr->second .isImmediate ;
178
+ tmp2.operand2 = -1 ; tmp2.isImmediate2 = false ;
179
+ tmp2.labelNo = -1 ;
180
+ res.push_back (tmp2);
181
+ del.insert (itr->first );
182
+ }
183
+ }
184
+ for (int j : del) {
185
+ substitution.erase (j);
186
+ }
187
+ res.push_back (tmp);
188
+ break ;
189
+ }
190
+ case MIDFUNC:
191
+ case MIDPARA:
192
+ case MIDNOP:
193
+ res.push_back (c);
194
+ break ;
195
+ case MIDGOTO:
196
+ case MIDCALL:
197
+ resEnd.push_back (c);
198
+ break ;
199
+ case MIDPRINTSTRING:
200
+ res.push_back (c);
201
+ }
202
+
203
+ }
204
+ // todo implement
205
+ for (auto & i : substitution) {
206
+ if (activeOut.find (i.first ) != activeOut.end ()) {
207
+ MidCode tmp2;
208
+ tmp2.op = MIDASSIGN; tmp2.target = i.first ;
209
+ tmp2.operand1 = i.second .id ; tmp2.isImmediate1 = i.second .isImmediate ;
210
+ tmp2.operand2 = -1 ; tmp2.isImmediate2 = false ;
211
+ tmp2.labelNo = -1 ;
212
+ res.push_back (tmp2);
213
+ }
214
+ }
215
+ res.insert (res.end (), resEnd.begin (), resEnd.end ());
216
+ if (label != -1 ) {
217
+ if (res.size () != 0 ) {
218
+ res[0 ].labelNo = label;
219
+ }
220
+ else {
221
+ MidCode tmp;
222
+ tmp.op = MIDNOP;
223
+ tmp.target = tmp.operand1 = tmp.operand2 = -1 ;
224
+ tmp.isImmediate1 = tmp.isImmediate2 = false ;
225
+ tmp.labelNo =label;
226
+ res.push_back (tmp);
227
+ }
228
+ }
229
+ return res;
230
+ }
0 commit comments