forked from toful/Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeAddressCode.c
115 lines (91 loc) · 3.41 KB
/
ThreeAddressCode.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
/*######################################################################
# Compiladors
# Cristòfol Daudén Esmel
# 3 Address Code file
######################################################################*/
#include "./ThreeAddressCode.h"
extern FILE *yyout;
void init_3AC(){
instructions.MAXLINES = 200;
instructions.line = (char **) malloc( sizeof( char *) * instructions.MAXLINES );
instructions.lineNumber = 0;
instructions.temporalNumber = 1;
}
void emit( char * line ){
if( instructions.lineNumber == instructions.MAXLINES ){
instructions.MAXLINES = instructions.MAXLINES + 50;
instructions.line = realloc( instructions.line, sizeof( char * ) * instructions.MAXLINES );
}
int lineSize = strlen(line) + 5;
char * temp;
temp = ( char * ) malloc( sizeof(char) * lineSize );
sprintf( temp, "%d: %s", instructions.lineNumber, line );
instructions.line[ instructions.lineNumber ] = ( char * ) malloc( sizeof( char ) * lineSize );
memcpy( instructions.line[ instructions.lineNumber ], temp, lineSize );
instructions.lineNumber++;
}
void drop( ){
int i;
for ( i = 0; i < instructions.lineNumber; ++i )
{
fprintf( yyout, "%s\n", instructions.line[i] );
}
}
int getNextTemporal(){
int temp = instructions.temporalNumber;
instructions.temporalNumber++;
return temp;
}
lineNumberList * createList( int lineNumber ){
lineNumberList * result = malloc( sizeof(lineNumberList) );
result->lineNumber = lineNumber;
result->next = NULL;
return result;
}
void complete( lineNumberList * line, int pos ){
if (line == NULL) return;
/*don't know wht sometimes some lines to complete are not written yet*/
if( line->lineNumber >= instructions.lineNumber ) return;
char * aux = (char * ) malloc( sizeof(char) * 4 );
sprintf( aux, "%d", pos );
int x = 0;
while( !( instructions.line[ line->lineNumber ][x] == 'G' && instructions.line[ line->lineNumber ][x+1] == 'O'
&& instructions.line[ line->lineNumber ][x+2] == 'T' && instructions.line[ line->lineNumber ][x+3] == 'O' ) ){
x++;
}
x=x+5;
int lineSize = strlen( aux );
instructions.line[ line->lineNumber ] = realloc( instructions.line[ line->lineNumber ], sizeof( char ) * lineSize );
memcpy( &instructions.line[ line->lineNumber ][x], aux, lineSize );
free( aux );
if( line->next != NULL ){
line = line->next;
complete( line, pos );
}
line = line->next;
}
lineNumberList * merge( lineNumberList * list1, lineNumberList * list2 ){
if ( list1 == NULL ) return list2;
lineNumberList * result = list1;
while (list1->next != NULL){
list1 = ( lineNumberList * ) list1->next;
}
list1->next = list2;
return result;
}
switchLineNumberList * createSwitchList( int lineNumber, int caseValue ){
switchLineNumberList * result = malloc( sizeof(switchLineNumberList) );
result->lineNumber = lineNumber;
result->caseValue = caseValue;
result->next = NULL;
return result;
}
switchLineNumberList * mergeSwitchList( switchLineNumberList * list1, switchLineNumberList * list2 ){
if ( list1 == NULL ) return list2;
switchLineNumberList * result = list1;
while (list1->next != NULL){
list1 = ( switchLineNumberList * ) list1->next;
}
list1->next = list2;
return result;
}