-
Notifications
You must be signed in to change notification settings - Fork 1
/
intermediate.c
408 lines (365 loc) · 10.4 KB
/
intermediate.c
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
/******************************************************************************
* C code file : intermediate.c
* Project : Tony Compiler
* Version : 1.0 alpha
* Written by : Manolis Androulidakis
* Date : September 21, 2015
* Description : Intermediate code (quad generation and optimization)
*
* ---------
* Εθνικό Μετσόβιο Πολυτεχνείο.
* Σχολή Ηλεκτρολόγων Μηχανικών και Μηχανικών Υπολογιστών.
* Τομέας Τεχνολογίας Πληροφορικής και Υπολογιστών.
* Εργαστήριο Τεχνολογίας Λογισμικού
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "intermediate.h"
#include "general.h"
#include "symbol.h"
#include "error.h"
/* -------------------------------------------------------------
---------------------- Global variables ---------------------
------------------------------------------------------------- */
FILE *iout = NULL;
/*
* buffer for quads and size of buffer. Initial size = QUAD_ARRAY_SIZE
* max number of quads supported in a function = QUAD_ARRAY_SIZE - 1.
* If a function needs more quads then we resize q with realloc
* Placement of quads starts from q[1] (so that offset==quad_num)
*/
Quad *q;
static int qSize;
static struct Operand_tag operandConst [] = {
{ OPERAND_PASSMODE, "V", NULL },
{ OPERAND_PASSMODE, "R", NULL },
{ OPERAND_PASSMODE, "RET", NULL },
{ OPERAND_NULL, "-", NULL },
{ OPERAND_STAR, "*", NULL },
{ OPERAND_RESULT, "$$", NULL }
};
const Operand oV = &(operandConst[0]);
const Operand oR = &(operandConst[1]);
const Operand oRET = &(operandConst[2]);
const Operand o_ = &(operandConst[3]);
const Operand oSTAR = &(operandConst[4]);
const Operand oRESULT = &(operandConst[5]);
/* -------------------------------------------------------------
------------------------- Functions -------------------------
------------------------------------------------------------- */
void initIntermediate()
{
qSize = QUAD_ARRAY_SIZE;
q = (Quad *) malloc(qSize * sizeof(Quad));
}
void genquad(OperatorType op,Operand x,Operand y,Operand z)
{
q[quadNext].num= quadNext;
q[quadNext].op = op;
q[quadNext].x = x;
q[quadNext].y = y;
q[quadNext].z = z;
quadNext++;
if (quadNext == qSize) {
fprintf(stderr, "quad limit reached\n");
qSize = 2 * qSize;
q = (Quad *) realloc(q, qSize * sizeof(Quad)); //double quad array size
}
}
void printQuads()
{
int i;
for (i = 1; i < quadNext; i++){
if (ISACTIVE(q[i].num))
fprintf(iout,"%d: %s, %s, %s, %s\n", q[i].num, otostr(q[i].op), q[i].x->name, q[i].y->name, q[i].z->name);
}
}
Operand oS(SymbolEntry * s)
{
Operand o = (Operand ) new(sizeof(struct Operand_tag));
o->type = OPERAND_SYMBOL;
o->name = s->id;
o->u.symbol = s;
return o;
}
Operand oL(int quadLabel)
{
Operand o = (Operand ) new(sizeof(struct Operand_tag));
o->type = OPERAND_QLABEL;
char buf[12]; //12 = max letter of int
snprintf(buf,12,"%d",quadLabel);
o->name = strdup(buf);
o->u.quadLabel = quadLabel;
return o;
}
//called both on definition and on call of a function (block)
Operand oU(SymbolEntry * s)
{
#ifdef DEBUG
//printf("oU: %s\n", s->id);
#endif
if(s == NULL) internal("oU: function not declared in SymbolTable");
Operand o = (Operand ) new(sizeof(struct Operand_tag));
o->type = OPERAND_UNIT;
o->name = s->id;
o->u.symbol = s;
#ifdef DEBUG
//printf("oU: %s finished\n", s->id);
#endif
return o;
}
Operand oD(SymbolEntry * s)
{
Operand o = (Operand ) new(sizeof(struct Operand_tag));
o->type = OPERAND_DEREFERENCE;
int buf_size = 3+strlen(s->id);
char buf[buf_size];
snprintf(buf,buf_size,"[%s]",s->id);
o->name = strdup(buf);
o->u.symbol = s;
return o;
}
Operand oA(SymbolEntry * s)
{
Operand o = (Operand ) new(sizeof(struct Operand_tag));
o->type = OPERAND_ADDRESS;
int buf_size = 3+strlen(s->id);
char buf[buf_size];
snprintf(buf,buf_size,"{%s}",s->id);
o->name = strdup(buf);
o->u.symbol = s;
return o;
}
ListPair createCondition(Operand place)
{
#ifdef DEBUG
printf("got in createCondition!\n");
#endif
ListPair l;
l.TRUE = makelist(quadNext);
genquad(O_IFB,place,o_,oSTAR);
l.FALSE = makelist(quadNext);
genquad(O_JUMP,o_,o_,oSTAR);
#ifdef DEBUG
printf("got out of createCondition!\n");
#endif
return l;
}
Operand evaluateCondition(List * TRUE, List * FALSE)
{
#ifdef DEBUG
printf("got in evaluateCondition!\n");
#endif
SymbolEntry * w = newTemporary(typeBoolean);
backpatch(TRUE,quadNext);
genquad(O_ASSIGN,oS(newConstant("true",typeBoolean,true)),o_,oS(w));
genquad(O_JUMP,o_,o_,oL(quadNext+2));
backpatch(FALSE,quadNext);
genquad(O_ASSIGN,oS(newConstant("false",typeBoolean,false)),o_,oS(w));
#ifdef DEBUG
printf("got out of evaluateCondition!\n");
#endif
return oS(w);
}
/* -------------------------------------------------------------
----------------------- Optimizations -----------------------
------------------------------------------------------------- */
/* Optimizations
1. inverse copy propagation
2. constant propagation
3. algebraic transformations
4. remove jumps to next instr
*/
static void opt_inverseCopyPropagation()
{
int i;
//up to quadNext-2 because the quads that will be transformed always go in pairs
for (i = 1; i < quadNext-1; i++){
if (!ISACTIVE(q[i].num)) continue;
OperatorType op1 = q[i].op;
OperatorType op2 = q[i+1].op;
if( (op1==O_ADD || op1==O_SUB || op1==O_MULT || op1==O_DIV || op1==O_MOD) && op2==O_ASSIGN ) {
if (getSymbol(q[i].z) == getSymbol(q[i+1].x) ) {
q[i].z = q[i+1].z;
q[i+1].num = -1; //remove quad, deactivate
#ifdef DEBUG
printf("opt: inverseCopyPropagation: quad %d modified, quad %d removed\n", i, i+1);
#endif
}
}
}
}
static void opt_constantFolding()
{
int i;
for (i = 1; i < quadNext; i++) {
if (!ISACTIVE(q[i].num)) continue;
OperatorType op = q[i].op;
if ( (op==O_ADD || op==O_SUB || op==O_MULT || op==O_DIV || op==O_MOD) &&
(getSymbol(q[i].x)->entryType==ENTRY_CONSTANT && getSymbol(q[i].y)->entryType==ENTRY_CONSTANT))
{
int v1 = getSymbol(q[i].x)->u.eConstant.value.vInteger;
int v2 = getSymbol(q[i].y)->u.eConstant.value.vInteger;
int res;
switch(op) {
case O_ADD: res = v1 + v2; break;
case O_SUB: res = v1 - v2; break;
case O_MULT: res = v1 * v2; break;
case O_DIV: res = v1 / v2; break;
case O_MOD: res = v1 % v2; break;
}
q[i].op = O_ASSIGN;
q[i].x = oS(newConstant(NULL, typeInteger, res));
q[i].y = o_;
}
}
}
static void opt_algebraicTransformations()
{
int i;
SymbolEntry * s;
for (i = 1 ; i < quadNext; i++){
if(!ISACTIVE(q[i].num)) continue;
if(q[i].op==O_ADD) {
// 0 + x = x
s = getSymbol(q[i].x);
if(s->entryType==ENTRY_CONSTANT && s->u.eConstant.value.vInteger==0)
{ q[i].op=O_ASSIGN; q[i].x=q[i].y; q[i].y=o_; continue; }
// x + 0 = x
s = getSymbol(q[i].y);
if(s->entryType==ENTRY_CONSTANT && s->u.eConstant.value.vInteger==0)
{ q[i].op=O_ASSIGN; q[i].y=o_; continue; }
}
else if(q[i].op==O_MULT) {
// 0 * x = 0
s = getSymbol(q[i].x);
if(s->entryType==ENTRY_CONSTANT && s->u.eConstant.value.vInteger==0)
{ q[i].op=O_ASSIGN; q[i].y=o_; continue; }
// 1 * x = x
if(s->entryType==ENTRY_CONSTANT && s->u.eConstant.value.vInteger==1)
{ q[i].op=O_ASSIGN; q[i].x=q[i].y; q[i].y=o_; continue; }
s = getSymbol(q[i].y);
// x * 0 = 0
if(s->entryType==ENTRY_CONSTANT && s->u.eConstant.value.vInteger==0)
{ q[i].op=O_ASSIGN; q[i].x=q[i].y; q[i].y=o_; continue; }
// x * 1 = x
if(s->entryType==ENTRY_CONSTANT && s->u.eConstant.value.vInteger==1)
{ q[i].op=O_ASSIGN; q[i].y=o_; continue; }
}
}
}
//ommits jumps to the following quad (flow will get there anyway)
static void opt_oneStepJumps()
{
int i;
for (i = 1 ; i < quadNext; i++){
if (!ISACTIVE(q[i].num)) continue;
if (q[i].op==O_JUMP && q[i].z->u.quadLabel==i+1)
q[i].num = -1;
}
}
void optimize()
{
opt_inverseCopyPropagation(); //first, if constantFolding first, it will not work
opt_constantFolding();
opt_algebraicTransformations();
opt_oneStepJumps();
}
/* -------------------------------------------------------------
---------------------- List Functions ----------------------
------------------------------------------------------------- */
List* emptylist()
{
List *l = (List *)new(sizeof(List));
l->head = NULL;
return l;
}
List* makelist(int qnum)
{
List *l = (List *)new(sizeof(List));
Node *n = (Node *)new(sizeof(Node));
n->data = qnum;
n->next = NULL;
l->head = n;
return l;
}
//crosses 2nd list, so put large list first
List* merge(List *l1, List *l2)
{
if(l2->head==NULL) return l1;
Node * p = l2->head;
while(p->next!=NULL) p=p->next;
p->next=l1->head;
return l2;
}
void backpatch(List *l,int qnum)
{
Operand dest = oL(qnum);
Node * p = l->head;
Node * t;
while(p!=NULL){
int index = p->data;
Quad qd = q[index];
if(qd.x==oSTAR) q[index].x = dest;
if(qd.y==oSTAR) q[index].y = dest;
if(qd.z==oSTAR) q[index].z = dest;
/* As we traverse we delete the elements of the list */
t=p;
p=p->next;
delete(t);
}
l->head=NULL; //list has been emptied
}
/* -------------------------------------------------------------
------------------ Other Helper Functions -------------------
------------------------------------------------------------- */
SymbolEntry * getSymbol(Operand o){
switch(o->type){
case OPERAND_SYMBOL:
case OPERAND_DEREFERENCE:
case OPERAND_ADDRESS:
case OPERAND_UNIT:
return o->u.symbol;
default:
return NULL;
}
}
//operator to string - alternatively we can allocate an const char * array with those values
const char * otostr(OperatorType op)
{
switch(op){
case O_ASSIGN: return ":=";
case O_ARRAY: return "array";
case O_ADD: return "+";
case O_SUB: return "-";
case O_MULT: return "*";
case O_DIV: return "/";
case O_MOD: return "mod";
case O_EQ: return "=";
case O_NE: return "<>";
case O_LT: return "<";
case O_GT: return ">";
case O_LE: return "<=";
case O_GE: return ">=";
case O_IFB: return "ifb";
case O_JUMP: return "jump";
case O_UNIT: return "unit";
case O_ENDU: return "endu";
case O_CALL: return "call";
case O_RET: return "ret";
case O_PAR: return "par";
default: return NULL;
}
}
#ifdef DEBUG
void printList(List *l){
Node *p = l->head;
if (!p) printf("<empty>");
while(p){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
#endif