-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_gen_new.c
380 lines (328 loc) · 8.54 KB
/
code_gen_new.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
#include <stdio.h>
#include "lex.h"
#include <stdlib.h>
#include "symbol.h"
char *expression(void);
char *great(void);
char *less(void);
char *sub(void);
char *sum(void);
char *product(void);
char *fraction(void);
char *factor(void);
char *current_lexeme(void);
extern char *newname( void );
extern void freename( char *name );
#define ACCUMULATOR "\%al"
int if_count = 0;
int while_count = 0;
int comp_count=0;
FILE *f = NULL;
/*Function definitions begin here*/
statement()
{
/* statement -> expression SEMI
* | expression SEMI statement
*/
/*
expression();
if( match( SEMI ) )
advance();
else
fprintf( stderr, "%d: Inserting missing semicolon\n", yylineno );
if( !match(EOI) )
statements(); // Do another statement.
*/
if(match(ID)){
char *var1 = current_lexeme(), *var2;
add_to_table(var1);
advance();
if(match(ASSIGN)){
advance();
var2 = expression();
if(!var2){
printf("%d: Expression expected\n", yylineno);
return;
}
//printf("%s = %s \n", var1,var2);
//printf("MOV %s,%s\n", var2,var1);
fprintf(f,"MOVB %s,%s\n", var2,var1);
freename(var2);
free(var1);
}
else
fprintf(stderr, "%d: Not a valid assignment\n",yylineno);
}
else if(match(IF)){
char *var1;
if_count++;
int temp_if_count = if_count;
advance();
var1 = expression();
if(!var1){
fprintf(stderr, "%d: Invalid Expression\n",yylineno);
return;
}
//printf("CMP %s, $0\n",var1 );
//printf("JLE Else%d\n",if_count);
fprintf(f, "MOVB $0, %s \n", ACCUMULATOR);
fprintf(f,"CMP %s, %s \n",ACCUMULATOR,var1);
fprintf(f,"JLE Else%d\n",temp_if_count);
if(match(THEN)){
//printf("if(%s){\n", var1);
freename(var1);
advance();
statement();
//printf("Else%d:\n",if_count);
fprintf(f,"Else%d:\n",temp_if_count);
//printf("}\n");
}
else
fprintf(stderr, "%d: Then expected after if\n", yylineno);
}
else if(match(WHILE)){
char *var;
while_count++;
int temp_while_count = while_count;
advance();
var = expression();
if(!var){
fprintf(stderr, "%d: Invalid Expression\n",yylineno);
return;
}
// printf("While%d:\n",while_count);
// printf("CMP %s, $0\n", var);
// printf("JLE Exit%d\n", while_count);
fprintf(f, "MOVB $0, %s \n", ACCUMULATOR);
fprintf(f,"While%d:\n",temp_while_count);
fprintf(f,"CMP %s, %s\n", ACCUMULATOR,var);
fprintf(f,"JLE Exit%d\n", temp_while_count);
if(match(DO)){
//printf("while\t(%s)\n", var);
//printf("do{\n");
advance();
statement();
//printf("}\n");
freename(var);
// printf("JMP While%d\n",while_count);
// printf("Exit%d:\n",while_count);
fprintf(f,"JMP While%d\n",temp_while_count);
fprintf(f,"Exit%d:\n",temp_while_count);
}
else
fprintf(stderr, "%d: Do expected after while\n", yylineno);
}
else if(match(BEGIN)){
printf("begin\n");
advance();
opt_statements();
if(match(END)){
printf("end\n");
advance();
}
else
fprintf(stderr, "%d End expected begin\n", yylineno);
}
else if(match(EOI)){
// printf("thu\n");
return;
}
else{
fprintf(stderr, "%d: Statement Expected\n", yylineno);
exit(1);
}
}
opt_statements(){
/*
opt_statements -> statement_list | epsilon
*/
statement_list();
}
statement_list(){
/*
statement_list -> statement statement_list_prime
*/
if(!f){
f = fopen("temp.txt","w");
}
statement();
while(match(SEMI)){
advance();
statement();
}
//fclose(f);
}
/*
statement_list_prime(){
if(match(SEMI)){
advance();
statement();
statement_list_prime();
}
else{
if(!match(EOI)){
statement_list();
}
}
}
*/
char *expression(void){
char *var1, *var2;
var1 = great();
while(match(EQUALS)){
comp_count++;
advance();
var2 = great();
//printf("%s = %s == %s\n", var1, var2);
fprintf(f, "CMP %s, %s\n", var1,var2);
fprintf(f, "JE CMP0%d\n", comp_count, comp_count);
fprintf(f, "MOVB $0, %s\n",var1 );
fprintf(f, "JMP CMP1%d\n", comp_count);
fprintf(f, "CMP0%d:\nMOVB $1, %s\n", comp_count, var1 );
fprintf(f, "CMP1%d:\n", comp_count);
freename(var2);
}
return var1;
}
char *great(void){
char *var1, *var2;
var1 = less();
while(match(GT)){
comp_count++;
advance();
var2 = less();
//printf("%s=%s > %s \n", var1,var1,var2 );
fprintf(f, "CMP %s, %s\n", var2,var1);
fprintf(f, "JG CMP0%d\n", comp_count, comp_count);
fprintf(f, "MOVB $0, %s\n",var1 );
fprintf(f, "JMP CMP1%d\n", comp_count);
fprintf(f, "CMP0%d:\nMOVB $1, %s\n", comp_count, var1 );
fprintf(f, "CMP1%d:\n", comp_count);
freename(var2);
}
return var1;
}
char *less(void){
char *var1, *var2;
var1 = sub();
while(match(LT)){
comp_count++;
advance();
var2 = sub();
//printf("%s=%s < %s\n", var1,var1,var2);
fprintf(f, "CMP %s, %s\n", var2,var1);
fprintf(f, "JL CMP0%d\n", comp_count, comp_count);
fprintf(f, "MOVB $0, %s\n",var1 );
fprintf(f, "JMP CMP1%d\n", comp_count);
fprintf(f, "CMP0%d:\nMOVB $1, %s\n", comp_count, var1 );
fprintf(f, "CMP1%d:\n", comp_count);
freename(var2);
}
return var1;
}
char *sub(void){
char *var1, *var2;
var1 = sum();
while(match(MINUS)){
advance();
var2 = sum();
//printf("%s = %s - %s\n", var1,var1,var2);
//printf("SUB %s, %s\n",var2,var1 );
fprintf(f,"SUB %s, %s\n",var2,var1 );
freename(var2);
}
return var1;
}
char *sum(void){
char *var1, *var2;
var1 = product();
while(match(PLUS)){
advance();
var2 = product();
//printf("%s = %s + %s\n", var1,var1,var2);
//printf("ADD %s, %s\n", var2,var1);
fprintf(f,"ADD %s, %s\n", var2,var1);
freename(var2);
}
return var1;
}
char *product(void){
char *var1, *var2;
var1 = fraction();
while(match(TIMES)){
advance();
var2 = fraction();
//printf("%s = %s * %s\n", var1,var1,var2);
//printf("IMUL %s, %s\n", var2, var1);
fprintf(f,"IMUL %s, %s\n", var2, var1);
freename(var2);
}
return var1;
}
char *fraction(void){
char *var1, *var2;
var1 = factor();
while(match(DIV)){
advance();
var2 = factor();
//printf("%s = %s / %s\n", var1,var1,var2);
//printf("MOV %s,%s\n", var1, ACCUMULATOR );
//printf("DIV %s\n", var2);
//printf("MOV %s, %s\n", ACCUMULATOR,var1);
fprintf(f, "MOVB $0, %s \n", "\%ah");
fprintf(f,"MOVB %s,%s\n", var1, ACCUMULATOR );
fprintf(f,"DIV %s\n", var2);
fprintf(f,"MOVB %s, %s\n", ACCUMULATOR,var1);
freename(var2);
}
return var1;
}
char *factor(void)
{
char *tempvar=NULL;
char *temp;
if( match(ID) )
{
/* Print the assignment instruction. The %0.*s conversion is a form of
* %X.Ys, where X is the field width and Y is the maximum number of
* characters that will be printed (even if the string is longer). I'm
* using the %0.*s to print the string because it's not \0 terminated.
* The field has a default width of 0, but it will grow the size needed
* to print the string. The ".*" tells printf() to take the maximum-
* number-of-characters count from the next argument (yyleng).
*/
//printf("MOV %0.*s, %s\n", yyleng, yytext ,tempvar=newname());
fprintf(f,"MOVB %0.*s, %s \n", yyleng, yytext ,tempvar=newname());
temp = current_lexeme();
add_to_table(temp);
free(temp);
advance();
}
else if (match(NUM)){
//printf("MOV $%0.*s, %s\n", yyleng, yytext ,tempvar=newname());
fprintf(f,"MOVB $%0.*s, %s\n", yyleng, yytext ,tempvar=newname());
advance();
}
else if( match(LP) )
{
advance();
tempvar = expression();
if( match(RP) )
advance();
else
fprintf(stderr, "%d: Mismatched parenthesis\n", yylineno );
}
else
fprintf( stderr, "%d: Number or identifier expected\n", yylineno );
return tempvar;
}
char * current_lexeme(){
char *temp;
int i;
temp = (char *) malloc(sizeof(char) * (yyleng+1));
temp[yyleng]='\0';
for(i=0;i<yyleng;i++){
temp[i]=yytext[i];
}
return temp;
}