-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen.c
279 lines (248 loc) · 6.35 KB
/
codegen.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
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=
BATCH 26
-=-=-=-=-=-=-=-=-=-=-=-=-=
AAYUSH AHUJA 2010A7PS023P
MAYANK GUPTA 2010A7PS022P
-=-=-=-=-=-=-=-=-=-=-=-=-=
codegen.c
-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include"lexerDef.h"
#include"lexer.h"
#include"parserDef.h"
#include"parser.h"
#include"symbolTable.h"
#include"semantic.h"
//#include"codegen.h"
void evaluate(parseTree A, FILE *fp)
{
//printf("%s\n", A->t->lexeme);
int c1=0,c2=0,i;
char temp[30]="";
for(i=0;A->next[i]!=NULL;i++)
c1++;
//printf("c=%d\n",c1);
if(c1 == 2)
{
evaluate(A->next[0],fp);
fprintf(fp,"push AX\n");
//push();
evaluate(A->next[1],fp);
//fprintf(fp,"pop DX\n");
// if(A->next[1]->t->s == TK_PLUS)
// fprintf(fp,"add AX,DX\n");
// if(A->next[1]->t->s == TK_MINUS)
// fprintf(fp,"sub AX,DX\n");
// if(A->next[1]->t->s == TK_MUL)
// fprintf(fp,"imul DX\n");
// if(A->next[1]->t->s == TK_DIV)
// fprintf(fp,"idiv DX\n");
//calculate();
}
else if(c1==1)
{
if(A->t->s != TK_DIV)
{
evaluate(A->next[0],fp);
fprintf( fp,"pop DX\n");
if(A->t->s == TK_PLUS)
fprintf(fp,"add AX,DX\n");
if(A->t->s == TK_MINUS)
{fprintf(fp,"sub DX,AX\n");fprintf(fp, "mov AX,DX\n");}
if(A->t->s == TK_MUL)
fprintf(fp,"imul DX\n");
}
else
{
evaluate(A->next[0],fp);
fprintf(fp, "push AX\n");
fprintf(fp, "pop DX\n");
fprintf(fp, "pop AX\n");
fprintf(fp, "idiv DX\n");
}
//calculate();
}
else if(c1==0)
{
char temp[30];
strcpy(temp,A->t->lexeme);
//printf("%s\n",toStr(A->parent->t->s));
if(A->parent->next[1] && A->parent->t->s == all)
{
strcat(temp,"_");
strcat(temp,A->parent->next[1]->t->lexeme);
}
//fprintf(fp, "A->t->s=%s\n",toStr(A->t->s) );
fprintf(fp,"mov AX,%s\n",temp);
}
}
void ifelse(variable GT[], funTable FT[], recTable RT[], parseTree A, FILE * fp)
{
//set value of AX=1 for true and AX=0 for false
if(A->t->s==TK_AND )
{
ifelse(GT,FT,RT,A->next[0],fp);
fprintf(fp, "push AX\n");
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "pop DX\n");
fprintf(fp,"and AX,DX\n");
}
else if(A->t->s==TK_OR )
{
ifelse(GT,FT,RT,A->next[0],fp);
fprintf(fp, "push AX\n");
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "pop DX\n");
fprintf(fp,"or AX,DX\n");
}
else if(A->t->s==TK_GT)
{
ifelse(GT,FT,RT,A->next[0],fp);
fprintf(fp, "push AX\n");
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "pop DX\n");
fprintf(fp, "cmp AX,DX\n");
int r = rand()%10000;
fprintf(fp, "jle true_%d_%d\n",A->lineno,r);
fprintf(fp, "jmp false_%d_%d\n",A->lineno,r);
fprintf(fp, "true_%d_%d:\nmov AX, 1\njmp resume_%d_%d\n",A->lineno,r,A->lineno,r );
fprintf(fp, "false_%d_%d:\nmov AX, 0\nresume_%d_%d:\n",A->lineno,r,A->lineno,r );
}
else if(A->t->s==TK_GE)
{
ifelse(GT,FT,RT,A->next[0],fp);
fprintf(fp, "push AX\n");
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "pop DX\n");
fprintf(fp, "cmp AX,DX\n");
int r = rand()%10000;
fprintf(fp, "jl true_%d_%d\n",A->lineno,r);
fprintf(fp, "jmp false_%d_%d\n",A->lineno,r);
fprintf(fp, "true_%d_%d:\nmov AX, 0\n",A->lineno,r );
fprintf(fp, "false_%d_%d:\nmov AX, 1\n",A->lineno,r );
}
else if(A->t->s==TK_EQ)
{
ifelse(GT,FT,RT,A->next[0],fp);
fprintf(fp, "push AX\n");
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "pop DX\n");
fprintf(fp, "cmp AX,DX\n");
int r = rand()%10000;
fprintf(fp, "jne true_%d_%d\n",A->lineno,r);
fprintf(fp, "jmp false_%d_%d\n",A->lineno,r);
fprintf(fp, "true_%d_%d:\nmov AX, 0\n",A->lineno,r );
fprintf(fp, "false_%d_%d:\nmov AX, 1\n",A->lineno,r );
}
else if(A->t->s==TK_LE)
{
ifelse(GT,FT,RT,A->next[0],fp);
fprintf(fp, "push AX\n");
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "pop DX\n");
fprintf(fp, "cmp AX,DX\n");
int r = rand()%10000;
fprintf(fp, "jg true_%d_%d\n",A->lineno,r);
fprintf(fp, "jmp false_%d_%d\n",A->lineno,r);
fprintf(fp, "true_%d_%d:\nmov AX, 0\n",A->lineno,r );
fprintf(fp, "false_%d_%d:\nmov AX, 1\n",A->lineno,r );
}
else if(A->t->s==TK_EQ)
{
ifelse(GT,FT,RT,A->next[0],fp);
fprintf(fp, "push AX\n");
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "pop DX\n");
fprintf(fp, "cmp AX,DX\n");
int r = rand()%10000;
fprintf(fp, "jge true_%d_%d\n",A->lineno,r);
fprintf(fp, "jmp false_%d_%d\n",A->lineno,r);
fprintf(fp, "true_%d_%d:\nmov AX, 0\n",A->lineno,r );
fprintf(fp, "false_%d_%d:\nmov AX, 1\n",A->lineno,r );
}
else if(A->t->s == TK_ID || A->t->s == TK_NUM)
{
fprintf(fp, "mov AX,%s\n",A->t->lexeme );
}
}
void generateCode(variable GT[], funTable FT[], recTable RT[],parseTree A, FILE *fp)
{
parseTree temp;
int j;
char idname[30]="",fieldname[30]="",name[60]="";
int f=0;
if(A->t->s ==TK_READ)
{
temp = A->parent;
fprintf(fp, "call scanf\n");
fprintf(fp, "mov %s, AX\n",temp->next[1]->t->lexeme );
}
else if(A->t->s == TK_WRITE)
{
temp = A->parent;
fprintf(fp, "mov AX,%s\n",temp->next[1]->t->lexeme );
fprintf(fp, "call printf\n");
}
else if(A->t->s == assignmentstmt)
{
if(A->next[0]->t->s == TK_ID)
{
strcpy(idname,A->next[0]->t->lexeme);
strcat(name,idname);
strcat(name,fieldname);
}
else
{
strcpy(idname,A->next[0]->next[0]->t->lexeme);
strcpy(fieldname,A->next[0]->next[1]->t->lexeme);
strcat(name,idname);
strcat(name,"_");
strcat(name,fieldname);
}
temp = A->next[2];
// while(1)
// {
// if(temp->t->s == arithmeticexpression || temp->t->s == term)
// temp = temp->next[0];
// }
evaluate(temp,fp);
fprintf(fp, "mov %s, AX\n",name );
}
else if(A->t->s == conditionalstmt)
{
int r = rand()%100;
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "cmp AX,1\n");
fprintf(fp, "jne else_%d_%d\n", A->lineno,r);
fprintf(fp, "jmp then_%d_%d\n", A->lineno,r);
fprintf(fp, "then_%d_%d:\n",A->lineno,r );
//fprintf(fp,"%s\n",toStr(A->next[3]->t->s));
generateCode(GT,FT,RT,A->next[3],fp);
fprintf(fp, "jmp cont_%d_%d\n",A->lineno,r );
fprintf(fp, "else_%d_%d:\n", A->lineno,r);
generateCode(GT,FT,RT,A->next[4],fp);
fprintf(fp, "cont_%d_%d:\n",A->lineno,r );
}
else if(A->t->s == iterativestmt)
{
fprintf(fp, "label_%d:\n",A->lineno );
ifelse(GT,FT,RT,A->next[1],fp);
fprintf(fp, "cmp AX,1\n");
fprintf(fp, "je label_%d\n",A->lineno );
//fprintf(fp, "label_%d\n",A->lineno);
}
else
{
for(j=0;A->next[j]!=NULL;j++)
{
temp = A->next[j];
generateCode(GT,FT,RT,temp,fp);
}
}
}