/
implement.c
553 lines (460 loc) · 10.3 KB
/
implement.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include"CFG.h"
#include<string.h>
#include<ctype.h>
#include"set.h"
#include"impl.h"
int get_num_of_quadruples( basic_block_t *ptr_basic_block )
{
quadruple_t *ptr_quad = ptr_basic_block->head;
int n = 0;
while( ptr_quad )
{
++n;
ptr_quad = ptr_quad->link;
}
return n;
}
void reverse_basic_block( basic_block_t *ptr_basic_block)
{
int n = get_num_of_quadruples( ptr_basic_block );
if(n>1)
{
//reverse is only done when we have more than 1 quadraple
quadruple_t *ptr_prev = 0;
quadruple_t *ptr_mid = ptr_basic_block->head; //Head points to the linked list of quadraples
quadruple_t *ptr_next = ptr_basic_block->head->link;
while(ptr_mid)
{
ptr_mid->link = ptr_prev;
ptr_prev = ptr_mid;
ptr_mid = ptr_next;
if( ptr_next ) ptr_next = ptr_next->link;
}
ptr_basic_block->head = ptr_prev;
}
}
_Bool isFirstTokenLoad( char *str )
{
char load[] = "LOAD";
//to preserve the value of string
char strDummy[50];
strcpy( strDummy , str );
//use strDummy for all the other manipulation.
char *temp = strtok(strDummy , " */+-");
char *t = temp;
while( *t != '\0')
{
*t = toupper(*t);
t++;
}
return !strcmp( load , temp);
}
_Bool isFirstTokenStore( char *tok)
{
char store[] = "STORE";
char tokDummy[15];
//for safety
strcpy( tokDummy , tok );
//converting to upper case for comparing
char *ptr = tokDummy;
while( *ptr != '\0')
{
*ptr = toupper( *ptr );
++ptr;
}
return ( strcmp( tokDummy , store ) == 0 );
}
_Bool findEqualsInString(char a[])
{
int n= strlen(a);
int i=0;
for(i=0;i<n;++i)
{
if(a[i] == '=')
{
return 1;
}
}
return 0;
}
char* find_definition( const char* const str )
{
//copy the string to a temporary for safety
char* temp = (char*)malloc( sizeof(char) * 50);
strcpy( temp , str );
if( findEqualsInString( temp ) )
{
//put the first token as the def
return strtok( temp , "= */+-");
}
else if( isFirstTokenLoad( temp )) //the first token is load
{
//put the second token as the definition.
strtok( temp , "= */+-");
return strtok( NULL , "= */+-");
}
strcpy(temp , "");
return temp;
}
quadruple_t * make_quadruple( char str[50] )
{
quadruple_t * temp = (quadruple_t*)malloc(sizeof(quadruple_t));
strcpy(temp->inst,str);
temp->link = 0;
initSet( &temp->live );
temp->def[0] = '\0';
return (quadruple_t*)temp;
}
void add_at_end(basic_block_t* ptr_basic_block, char str[50])
{
// make a quadruple
// fillup the quadruple
//copy the char array; ground the pointer
quadruple_t* temp = make_quadruple( str );
// go to the end and add
// add in the beginning if nothing is there
if(ptr_basic_block->head == 0)
{
ptr_basic_block->head = temp;
temp->link = 0;
}
else // for a non empty basic_block
{
quadruple_t* pres = ptr_basic_block->head;
while(pres->link)
{
pres = pres->link;
}
pres->link = temp;
temp->link = 0;
}
}
void set_definition_of_block( basic_block_t *ptr_block)
{
quadruple_t * head = ptr_block->head;
while(head)
{
strcpy( head->def , find_definition( head->inst ) );
head = head->link;
}
}
void set_all_definitions( basic_block_t blocks[] , int n )
{
int i = 0;
for( i = 0 ; i < n ; ++i)
{
set_definition_of_block(&blocks[i]);
}
}
void display_definitions( const basic_block_t * const ptr_block)
{
//get the head of the quadruple linked list;
//traverse through the linked list printing the definitions
quadruple_t * q = ptr_block->head;
while(q)
{
printf("%s ",q->def);
q = q->link;
if(q) printf("-->");
}
printf("\n");
}
void input_flow_matrix(int n ; int flow[n][n] , int n)
{
FILE *fp = fopen("flow_matrix","r");
int i,j;
for(i = 0 ; i < n ; ++i)
{
for( j = 0 ; j < n ; ++j)
{
fscanf( fp , "%d" , &flow[i][j] );
}
}
fclose(fp);
}
_Bool checkId(char arr[50]);
void input_live_on_exit(basic_block_t* ptr_basic_block)
{
char *exit_var = (char*)malloc( sizeof(char)*200);
gets(exit_var);
char *tok =strtok(exit_var," ,");
while(tok != NULL)
{
if(checkId(tok))
{
addElements(&ptr_basic_block->exit, tok);
}
tok=strtok(NULL, " ,");
}
free( exit_var );
}
void init_all_exits( basic_block_t blocks[] , int n)
{
//get the flow matrix
// for all basic blocks that have an exit
// input the variables live on exit.
int flow[n+2][n+2]; //to hold the flow matrix with the entry and exit information.
input_flow_matrix( flow , n+2);
int i;
for( i = 0 ; i < n+2 ; ++i)
{
if( flow[i][n+1] )
{
//input the variables on exit
printf("Enter the variables that are live on exit of block %d: \n",i-1);
input_live_on_exit( &blocks[i-1] );
}
}
}
//********************************************************************************************************************************************
void set_to_minus_1( int n ; int mat_redef[n][n] , int n , int index )
{
int i = 0;
for( i = 0 ; i < n ; ++i)
{
mat_redef[index][i] = -1;
}
}
_Bool is_row_0( int n ; int mat[n][n] , int n , int row)
{
_Bool zero_row = 1;
int i;
for(i = 0 ; i < n ; ++i)
{
zero_row = zero_row && !mat[row][i];
}
return zero_row;
}
int get_all_0_row( int n ; int mat_redef[n][n] , int n )
{
int i;
for( i = 0 ; i < n ; ++i)
if( is_row_0( mat_redef , n , i ) )
return i;
printf("There is no 0 row.....\n line : 275 , file : implement.c");
exit(0);
}
void remove_edge_to_index(int n; int a[][n],int n, int index)
{
int i,j;
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j)
{
if(j==index)
{
if( a[i][j] != -1 )
a[i][j]=0;
}
}
}
}
_Bool is_propogation_finished(int n; int a[][n], int n)
{
int i,j;
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j)
{
if(a[i][j] != -1)
{
return 0;
}
}
}
return 1;
}
void get_redefindedMat( int n ; int mat_redef[n-2][n-2] , int flow_matrix[n][n] , int n)
{
int i,j;
for( i = 1 ; i < n-1 ; ++i)
{
for( j = 1 ; j < n-1 ; j++)
{
mat_redef[i-1][j-1] = flow_matrix[i][j];
}
}
}
void set_exit_for_particular_block(int n; basic_block_t blocks[] , int index, int mat_redef[][n], int n)
{
int i;
for(i=0; i<n; ++i)
{
if( mat_redef[index][i] )
{
blocks[index].exit = doUnion( &blocks[index].exit , &blocks[i].entry );
}
}
}
void propogate_liveness( basic_block_t * ptr_block );
void propogate_live_var_in_all( int n ; basic_block_t blocks[] , int flow_matrix[n][n] , int n )
{
//remember that n is +2 than actual number of blocks
int mat_redef[n-2][n-2];
get_redefindedMat( mat_redef , flow_matrix , n);
int temp[n-2][n-2];
copy_matrix(temp,mat_redef,n-2);
printf("CFG graph after removing the cycles and ENTRY EXIT block : \n");
displayMatrix(n-2,mat_redef);
//now we have everything to propogate
//Till every block row is -1 ie propogated
// find the first all 0 row
// propogate it
// set everything in that row to -1
// whichever block it is pointed by make that edge 0
while( !is_propogation_finished( mat_redef , n-2 ) )
{
int index = get_all_0_row( mat_redef , n-2 );
//printf("%4d\n",index);
//setting the exits of that particular block
//find the union of all the variables in the entry set of its connections...
set_exit_for_particular_block(blocks, index, temp ,n-2);
propogate_liveness( &blocks[index] );
set_to_minus_1( mat_redef , n-2 , index );
remove_edge_to_index( mat_redef , n-2 , index);
}
}
//********************************************************************************************************************************************
//finds the usage of all the statements
_Bool checkId(char arr[50])
{
int i=0;
if (!(isalpha(arr[i])) || arr[i]=='_') return 0;
++i;
while(arr[i] != '\0')
{
if(!(isalnum(arr[i]) || arr[i]=='_'))
return 0;
++i;
}
return 1;
}
void setUsage( quadruple_t * q )
{
//for safety copy into another temporary variable.
char instDummy[50];
strcpy( instDummy , q->inst);
char *tok1 = strtok( instDummy , " =-+");
if( findEqualsInString( q->inst ) )
{
char* tok2 = strtok( NULL , " =-+*/");
if( checkId(tok2 ))
{
//add the usage to the quadruple set
addElements(&q->live,tok2);
}
char *tok3 = strtok(NULL , "+-*/ ");
if( tok3 && checkId(tok3) )
{
//add the usage to the quadruple set
addElements( &q->live, tok3 );
}
}
else if( isFirstTokenStore( tok1 ) )
{
char *tok2 = strtok( NULL , " -+");
if( checkId(tok2) )
{
//add the usage to the quadruple set
addElements( &q->live , tok2 );
}
}
}
void set_usages( basic_block_t *ptr_block )
{
quadruple_t *ptr_quad = ptr_block->head;
while( ptr_quad )
{
//printf("%s : ",ptr_quad->inst);
setUsage( ptr_quad );
//display( &ptr_quad->live );
ptr_quad = ptr_quad->link;
}
}
void set_all_usages( basic_block_t blocks[] , int n )
{
int i;
for( i = 0 ; i < n ; ++i)
{
set_usages( &blocks[i] );
}
}
void display_usage( const basic_block_t *const ptr_block)
{
//get the head of the quadruple linked list;
//traverse through the linked list printing the usage sets
quadruple_t * q = ptr_block->head;
while(q)
{
display( &q->live );
q = q->link;
if(q) printf("-->");
}
printf("\n");
}
//**********************************************work on 28/11/12**************************
int get_index(set_t* s, char* var)
{
int index =-1;
node_set_t* temp = s->head;
while(temp )
{
index++;
if(strcmp(temp->variable, var) == 0) break;
temp= temp->link;
}
if(index<0)
{
printf("index less than zero\n");
exit(0);
}
return index;
}
void set_graph_for_quad(int n; basic_block_t* block , quadruple_t* q, int input[][n],int n, set_t* all_var_set)
{
//for a particular quadruple
// get the set.
set_t * ptr_set = &q->live;
node_set_t * i = ptr_set->head;
node_set_t *j = NULL;
while(i)
{
j = i->link;
while(j)
{
int index1 = get_index( all_var_set , i->variable);
int index2 = get_index( all_var_set , j->variable );
input[index1][index2] = input[index2][index1] = 1;
j = j->link;
}
i = i->link;
}
}
void getGraphMatrix_forEveryBlocks(int n;basic_block_t* block ,int input[][n],int n, set_t* all_var_set)
{
quadruple_t* q= block->head;
while(q)
{
set_graph_for_quad(block,q, input, n,all_var_set);
q = q->link;
}
}
void getGraphMatrix(int n;int input[][n], basic_block_t blocks[], int n, set_t* all_var_set , int num_of_blocks)
{
int i=0;
for(i=0;i<num_of_blocks;i++)
{
getGraphMatrix_forEveryBlocks(&blocks[i],input,n,all_var_set);
}
}
void init_matrix(int n; int input[][n], int n)
{
int i,j;
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j)
{
input[i][j]=0;
}
}
}