-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutilities.c
483 lines (435 loc) · 11.2 KB
/
utilities.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/********************************************************************/
/* */
/* */
/* COMPILER UTILITIES */
/* */
/* */
/* */
/********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symbol.h"
#include "tac.h"
#include "stack.h"
#include "utilities.h"
void error(char* s){
fprintf(stderr, "Compiler: %s\n", s);
}
void *safe_malloc(int n){
void *t = malloc(n);
if(t == NULL){
error("malloc() failed");
exit(0);
}
return t;
}
void dump_table(SYMBTABLE *table){
int i = 0;
int count = 0;
SYMBTABLE* temp[100];
printf( "\n*********************************************************************************************************************************\n" );
printf( "SYMBOL TABLE NAME: %s \nWIDTH: %d \nPARAMETERS: %d\n\n", table->name, table->width, table->parameters);
printf( "\nName\t Hash\t Line\t Hops\t TokenType1\t TokenType2\t DataType\t Size\t Dimensions\t Offset\t c\n");
printf( "___________________________________________________________________________________________________________________\n\n" );
for ( i = 1; i < 997; i++ ){
if ( table->symtab[i] != NULL ){
int x = table->symtab[i]->token_type_2;
int y = table->symtab[i]->token_type_1;
if ((y == T_SELF) && ((x == T_PROC) || (x == T_FUNC))){
temp[count] = table->symtab[i]->function_st_ptr;
count++;
}
if(table->symtab[i]->token_type_2 == T_TEMP){
printf( "%s\t %d\t %d\t %d\t %s \t\t %s \t%s\t\t %d\t %d\t\t %d\t %d\n",
table->symtab[i]->name,
i,
table->symtab[i]->line,
table->symtab[i]->hops,
token_to_string(table->symtab[i]->token_type_1),
token_to_string(table->symtab[i]->token_type_2),
token_to_string(table->symtab[i]->data_type),
table->symtab[i]->size,
table->symtab[i]->dimensions,
table->symtab[i]->offset,
table->symtab[i]->c);
}
else{
printf( "%s\t %d\t %d\t %d\t %s \t %s \t%s\t\t %d\t %d\t\t %d\t %d\n",
table->symtab[i]->name,
i,
table->symtab[i]->line,
table->symtab[i]->hops,
token_to_string(table->symtab[i]->token_type_1),
token_to_string(table->symtab[i]->token_type_2),
token_to_string(table->symtab[i]->data_type),
table->symtab[i]->size,
table->symtab[i]->dimensions,
table->symtab[i]->offset,
table->symtab[i]->c);
}
}
}
printf( "\n*********************************************************************************************************************************\n" );
for(i = 0; i < count; i++)
dump_table(temp[i]);
printf("\n\n");
}
void dump_constant_table(SYMBTABLE *constant_symbol_table){
int i;
printf("\n\n%s\n", constant_symbol_table->name);
printf("********************************************\n");
for(i = 1; i < 997; i++){
if(constant_symbol_table->symtab[i] != NULL){
printf("CONSTANT_SYMBOL: %s\n", constant_symbol_table->symtab[i]->name);
}
}
}
char *token_to_string(int token){
switch (token){
case 0:
return "T_UNDEF";
case 1:
return "T_VAR";
case 2:
return "T_PROC";
case 3:
return "T_FUNC";
case 4:
return "T_INT";
case 5:
return "T_LABEL";
case 6:
return "T_REAL";
case 7:
return "T_ARRAY";
case 8:
return "T_STRING";
case 9:
return "T_BOOL";
case 10:
return "T_SPECIAL";
case 11:
return "T_PROGRAM";
case 12:
return "T_NOT_LOCAL";
case 13:
return "T_PARAM";
case 14:
return "T_ARG";
case 15:
return "T_LOCAL";
case 16:
return "T_FILE";
case 17:
return "T_CONSTANT";
case 18:
return "T_SELF";
case 19:
return "T_TEMP";
case 20:
return "T_RETURN";
default:
return "ERROR NO TYPE";
}
}
void print_tac(TAC *i){
char sa[12];
char sb[12];
char sc[12];
printf("\nTAC\n");
printf(" op: ");
switch(i->op)
{
case TAC_UNDEF:
printf("UNDEF\n");
break;
case TAC_ADD:
printf("ADD\n");
break;
case TAC_SUB:
printf("SUB\n");
break;
case TAC_MUL:
printf("MUL\n");
break;
case TAC_DIV:
printf("DIV\n");
break;
case TAC_NEG:
printf("NEG\n");
break;
case TAC_COPY:
printf("COPY\n");
break;
case TAC_GOTO:
printf("GOTO\n");
break;
case TAC_IFZ:
printf("IFZ\n");
break;
case TAC_IFNZ:
printf("IFNZ\n");
break;
case TAC_ARG:
printf("ARG\n");
break;
case TAC_CALL:
printf("CALL\n");
break;
case TAC_RETURN:
printf("RETURN\n");
break;
case TAC_LABEL:
printf("LABEL\n");
break;
case TAC_VAR:
printf("VAR\n");
break;
case TAC_BEGINFUNC:
printf("BEGINFUNC\n");
break;
case TAC_ENDFUNC:
printf("ENDFUNC\n");
break;
case TAC_BEGINPROC:
printf("BEGINPROC\n");
break;
case TAC_ENDPROC:
printf("ENDPROC\n");
break;
case TAC_BEGINPROG:
printf("BEGINPROC\n");
break;
case TAC_ENDPROG:
printf("ENDPROC\n");
break;
case TAC_MAINIDLIST:
printf("MAIN_ID_LIST\n");
break;
case TAC_MOD:
printf("MOD\n");
break;
case TAC_AND:
printf("AND\n");
break;
case TAC_OR:
printf("OR\n");
break;
case TAC_TDIV:
printf("DIV\n");
break;
case TAC_POS:
printf("POS\n");
break;
case TAC_NOT:
printf("NOT\n");
break;
case TAC_EQUAL:
printf("EQUAL\n");
break;
case TAC_NE:
printf("NE\n");
break;
case TAC_LE:
printf("LE\n");
break;
case TAC_GE:
printf("GE\n");
break;
case TAC_GRTRTHAN:
printf("GRTRTHAN\n");
break;
case TAC_LESSTHAN:
printf("LESSTHAN\n");
break;
case TAC_OFFSET:
printf("OFFSET\n");
break;
case TAC_PRINT:
printf("PRINT\n");
break;
default:
error("unknown TAC opcode");
break;
}
printf(" instruction: ");
switch(i->op)
{
case TAC_UNDEF:
printf("undef\n");
break;
case TAC_ADD:
printf("%s := %s + %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_SUB:
printf("%s := %s - %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_MUL:
printf("%s := %s * %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_DIV:
printf("%s := %s / %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_NEG:
printf("%s := %s\n", ts(i->a.var, sa), ts(i->c.var, sc));
break;
case TAC_COPY:
printf("%s := %s\n", ts(i->a.var, sa), ts(i->b.var, sb));
break;
case TAC_GOTO:
printf("goto L%d\n", i->a.lab->a.var->int_value);
break;
case TAC_IFZ:
printf("ifz %s goto L%d\n", ts(i->b.var, sb), i->a.lab->a.var->int_value);
break;
case TAC_IFNZ:
printf("ifnz %s goto L%d\n", ts(i->b.var, sb), i->a.lab->a.var->int_value);
break;
case TAC_ARG:
printf("arg %s\n", ts(i->a.var, sa));
break;
case TAC_CALL:
{
if(i->a.var == NULL)
printf("call L%d\n", i->b.lab->a.var->int_value);
else
printf("%s = call L%d\n", ts(i->a.var, sa), i->b.lab->a.var->int_value);
break;
}
case TAC_RETURN:
printf("return %s\n", ts(i->a.var, sa));
break;
case TAC_LABEL:
printf("label L%d\n", i->a.var->int_value);
break;
case TAC_VAR:
printf("var %s\n", ts(i->a.var, sa));
break;
case TAC_BEGINFUNC:
printf("beginfunc\n");
break;
case TAC_ENDFUNC:
printf("endfunc\n");
break;
case TAC_BEGINPROC:
printf("beginproc\n");
break;
case TAC_ENDPROC:
printf("endproc\n");
break;
case TAC_BEGINPROG:
printf("beginprog\n");
break;
case TAC_ENDPROG:
printf("endprog\n");
break;
case TAC_MAINIDLIST:
printf("main_identifier_list\n");
break;
case TAC_MOD:
printf("%s := %s MOD %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_AND:
printf("%s := %s AND %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_OR:
printf("%s := %s OR %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_TDIV:
printf("%s := %s DIV %s\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_POS:
printf("%s := + %s\n", ts(i->a.var, sa), ts(i->c.var, sc));
break;
case TAC_NOT:
printf("%s := NOT( %s )\n", ts(i->a.var, sa), ts(i->c.var, sc));
break;
case TAC_EQUAL:
printf("%s := ( %s = %s )\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_NE:
printf("%s := ( %s <> %s )\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_LE:
printf("%s := ( %s <= %s )\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_GE:
printf("%s := ( %s >= %s )\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_GRTRTHAN:
printf("%s := ( %s > %s )\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_LESSTHAN:
printf("%s := ( %s < %s )\n", ts(i->a.var, sa), ts(i->c.var, sb), ts(i->b.var, sc));
break;
case TAC_OFFSET:
printf("%s := %s[%s]\n", ts(i->a.var, sa), ts(i->b.var, sb), ts(i->b.var, sc));
break;
case TAC_PRINT:
printf("print %s\n", ts(i->a.var, sa));
break;
default:
{
error("print_tac(): unknown TAC opcode");
printf("unknown %d\n", i->op);
break;
}
fflush(stdout);
}
}
char *ts(SYMB *s, char *str){
if(s == NULL)
return "NULL";
switch(s->token_type_2)
{
case T_FUNC:
return s->name;
case T_VAR:
return s->name;
case T_INT:
sprintf(str, "%d" , s->int_value);
return str;
case T_REAL:
sprintf(str, "%lf" , s->dbl_value);
return str;
case T_ARRAY:
sprintf(str, "%s", s->name);
return str;
case T_TEMP:
sprintf(str, "%s", s->name);
return str;
case T_RETURN:
sprintf(str, "%s", s->name);
return str;
default:
printf("ts(): unknown TAC arg type, symb name: %s", s->name);
return;
}
}
TAC *insert_tac(TAC *t, TAC *list){
if(list == NULL){
t->next = NULL;
return t;
}
t->next = list;
list = t;
return list;
}
void dump_tac(TAC *tac_list){
if(tac_list == NULL)
printf("dump_tac() failed: NULL argument\n");
TAC *list = NULL;
TAC *temp = tac_list;
while(temp->prev != NULL){
list = insert_tac(temp, list);
temp = temp->prev;
}
printf("\nTHREE ADDRESS CODES\n");
printf("********************************************");
print_tac(list);
while(list->next != NULL){
list = list->next;
print_tac(list);
}
}