-
Notifications
You must be signed in to change notification settings - Fork 2
/
codegen.c
538 lines (498 loc) · 15.5 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
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
/*
BATCH NO. 27
Mayank Agarwal (2014A7PS111P)
Karan Deep Batra(2014A7PS160P)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "symboltable.h"
#include "hashtable.h"
#include "token.h"
#include "parserDef.h"
#include "mainsymboltable.h"
#include "idsymboltable.h"
#include "typeExtractor.h"
#include "codegen.h"
void endcode(FILE* fp)
{
fprintf(fp, "\tmov eax, 1\n");
fprintf(fp, "\tmov ebx, 1\n");
fprintf(fp, "\tint 80h\n");
// fprintf(fp, "\tret\n");
return;
}
void initialize(FILE* fp, mainsymboltable* globalTable)
{
fprintf(fp, "SECTION .bss\n");
// ffprintf(fp, fp, "")
mainsymboltablenode* temp = presentmainsymboltable(globalTable, "program");
idsymboltable* pt = temp->idst;
for(int i=0; i<idsymboltablesize; i++)
{
if(pt->buckets[i] != NULL)
{
idsymboltablenode* curr = pt->buckets[i];
while(curr != NULL)
{
fprintf(fp, "%s\tresb %d\n", curr->idlex, 2*curr->widthofid);
curr = curr->next;
}
}
}
fprintf(fp, "\n");
fprintf(fp, "SECTION .data\n");
fprintf(fp, "get_val: db \"%%d\", 0\n");
fprintf(fp, "print_val: db \"%%d\", 10, 0\n\n");
fprintf(fp, "SECTION .text\n\n");
fprintf(fp, "extern printf\n");
fprintf(fp, "extern scanf\n\n");
fprintf(fp, "GLOBAL main\n\n");
fprintf(fp, "main:\n");
return;
}
void codegenexp(FILE* fp, stacknode* curr)
{
if(strcmp(curr->ntortinfo->str, "<expression>") == 0)
codegenexp(fp, curr->child);
if(strcmp(curr->ntortinfo->str, "<var>") == 0)
{
if(strcmp(curr->child->ntortinfo->str, "NUM") == 0)
{
fprintf(fp, "\tmov r8, %s\n", curr->child->tokinfo->lexeme);
fprintf(fp, "\tpush r8\n");
return;
}
else if(strcmp(curr->child->ntortinfo->str, "RNUM") == 0)
{
// not to be considered
}
else
{
idsymboltablenode* temp = getidsymboltablenode(curr->child->tokinfo->lexeme, curr->child->idst);
fprintf(fp, "\tmov r8, [%s]\n", temp->idlex);
fprintf(fp, "\tand r8, 00000000000000ffh\n");
fprintf(fp, "\tpush r8\n");
}
}
if(strcmp(curr->ntortinfo->str, "MINUS") == 0)
{
if(curr->sibling != NULL)
{
codegenexp(fp, curr->sibling);
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tmov r8, 0\n");
fprintf(fp, "\tsub r8, r9\n");
fprintf(fp, "\tpush r8\n");
}
else
{
codegenexp(fp, curr->child);
codegenexp(fp, curr->child->sibling);
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tsub r8,r9\n");
fprintf(fp, "\tpush r8\n");
}
}
if(strcmp(curr->ntortinfo->str, "PLUS") == 0)
{
codegenexp(fp, curr->child);
codegenexp(fp, curr->child->sibling);
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tadd r8,r9\n");
fprintf(fp, "\tpush r8\n");
}
if(strcmp(curr->ntortinfo->str, "MUL") == 0)
{
codegenexp(fp, curr->child);
codegenexp(fp, curr->child->sibling);
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\timul r8,r9\n");
fprintf(fp, "\tpush r8\n");
}
if(strcmp(curr->ntortinfo->str, "DIV") == 0)
{
codegenexp(fp, curr->child);
codegenexp(fp, curr->child->sibling);
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tdiv r8,r9\n");
fprintf(fp, "\tpush r8\n");
}
if((strcmp(curr->ntortinfo->str, "LT") == 0) || (strcmp(curr->ntortinfo->str, "LE") == 0) || (strcmp(curr->ntortinfo->str, "GT") == 0) || (strcmp(curr->ntortinfo->str, "GE") == 0) || (strcmp(curr->ntortinfo->str, "EQ") == 0) || (strcmp(curr->ntortinfo->str, "NE") == 0))
{
codegenexp(fp, curr->child);
codegenexp(fp, curr->child->sibling);
int truelabel = getlabel();
int endlabel = getlabel();
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tcmp r8,r9\n");
if(strcmp(curr->ntortinfo->str, "LT") == 0)
fprintf(fp, "\tjl truelabel_%d\n", truelabel);
else if(strcmp(curr->ntortinfo->str, "LE") == 0)
fprintf(fp, "\tjle truelabel_%d\n", truelabel);
else if(strcmp(curr->ntortinfo->str, "GT") == 0)
fprintf(fp, "\tjg truelabel_%d\n", truelabel);
else if(strcmp(curr->ntortinfo->str, "GE") == 0)
fprintf(fp, "\tjge truelabel_%d\n", truelabel);
else if(strcmp(curr->ntortinfo->str, "EQ") == 0)
fprintf(fp, "\tje truelabel_%d\n", truelabel);
else if(strcmp(curr->ntortinfo->str, "NEQ") == 0)
fprintf(fp, "\tjne truelabel_%d\n", truelabel);
fprintf(fp, "\tmov r8, 0\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", endlabel);
fprintf(fp, "truelabel_%d:\n", truelabel);
fprintf(fp, "\tmov r8, 1\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "label_%d:\n", endlabel);
}
if(strcmp(curr->ntortinfo->str, "AND") == 0)
{
codegenexp(fp, curr->child);
codegenexp(fp, curr->child->sibling);
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
int firsttruelabel = getlabel();
int secondfalselabel = getlabel();
int endlabel = getlabel();
fprintf(fp, "\tcmp r8, 0000000000000001h\n");
fprintf(fp, "\tje label_%d\n", firsttruelabel);
fprintf(fp, "\tmov r8, 0000000000000000h\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", endlabel);
fprintf(fp, "label_%d:\n", firsttruelabel);
fprintf(fp, "\tcmp r9, 0000000000000001h\n");
fprintf(fp, "\tjne label_%d\n", secondfalselabel);
fprintf(fp, "\tmov r8, 0000000000000001h\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", endlabel);
fprintf(fp, "label_%d:\n",secondfalselabel);
fprintf(fp, "\tmov r8, 0000000000000000h\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "label_%d:\n",endlabel);
}
if(strcmp(curr->ntortinfo->str, "OR") == 0)
{
codegenexp(fp, curr->child);
codegenexp(fp, curr->child->sibling);
fprintf(fp, "\tpop r9\n");
fprintf(fp, "\tpop r8\n");
int firstfalselabel = getlabel();
int secondtruelabel = getlabel();
int endlabel = getlabel();
fprintf(fp, "\tcmp r8, 0000000000000001h\n");
fprintf(fp, "\tjne label_%d\n", firstfalselabel);
fprintf(fp, "\tmov r8, 0000000000000001h\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", endlabel);
fprintf(fp, "label_%d:\n", firstfalselabel);
fprintf(fp, "\tcmp r9, 0000000000000001h\n");
fprintf(fp, "\tje label_%d\n", secondtruelabel);
fprintf(fp, "\tmov r8, 0000000000000000h\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", endlabel);
fprintf(fp, "label_%d:\n",secondtruelabel);
fprintf(fp, "\tmov r8, 0000000000000001h\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "label_%d:\n",endlabel);
}
return;
}
int getlabel()
{
static int x = 0;
return x++;
}
void codegeniterative(FILE* fp, stacknode* temp)
{
if(strcmp(temp->child->ntortinfo->str, "FOR") == 0)
{
fprintf(fp, "\tmov r8, %s\n", temp->child->sibling->sibling->child->tokinfo->lexeme);
fprintf(fp, "\tpush r8\n");
int startlabel = getlabel();
int endlabel = getlabel();
fprintf(fp, "label_%d:\n", startlabel);
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tmov [%s], r8w\n", temp->child->sibling->tokinfo->lexeme);
fprintf(fp, "\tmov r9, %s\n", temp->child->sibling->sibling->child->sibling->tokinfo->lexeme);
fprintf(fp, "\tcmp r8, r9\n");
fprintf(fp, "\tjg label_%d\n", endlabel);
stacknode* temp2 = temp->child->sibling->sibling->sibling->sibling->child;
while(temp2 != NULL)
{
code_statement(fp, temp2);
temp2 = temp2->sibling;
}
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tinc r8\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", startlabel);
fprintf(fp, "label_%d: \n", endlabel);
fprintf(fp, "\tpop r8\n");
}
if(strcmp(temp->child->ntortinfo->str, "WHILE") == 0)
{
int startlabel = getlabel();
int endlabel = getlabel();
fprintf(fp, "label_%d:\n", startlabel);
codegenexp(fp, temp->child->sibling);
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tcmp r8, 0000000000000001h\n");
fprintf(fp, "\tjne label_%d\n", endlabel);
stacknode* temp2 = temp->child->sibling->sibling->sibling->child;
while(temp2 != NULL)
{
code_statement(fp, temp2);
temp2 = temp2->sibling;
}
fprintf(fp, "\tjmp label_%d\n", startlabel);
fprintf(fp, "label_%d:\n", endlabel);
}
return;
}
void codegenconditional(FILE* fp, stacknode* curr)
{
idsymboltable* idst = curr->child->idst;
idsymboltablenode* pt = getidsymboltablenode(curr->child->tokinfo->lexeme, idst);
fprintf(fp, "\tmov r8, [%s]\n", pt->idlex);
if(gettype(pt->type) == integer)
{
fprintf(fp, "\tand r8, 00000000000000ffh\n");
fprintf(fp, "\tpush r8\n");
stacknode* temp = curr->child->sibling->sibling; //<caseStmts>
stacknode* temp2 = temp->child;
int currlabel = getlabel();
int endlabel = getlabel();
while(temp2 != NULL)
{
int nextlabel = getlabel();
fprintf(fp, "label_%d:\n", currlabel);
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tmov r9, %s\n", temp2->child->tokinfo->lexeme);
fprintf(fp, "\tcmp r8,r9\n");
fprintf(fp, "\tjne label_%d\n", nextlabel);
stacknode* temp3 = temp2->sibling->child;
while(temp3 != NULL)
{
code_statement(fp, temp3);
temp3 = temp3->sibling;
}
fprintf(fp, "\tjmp label_%d\n", endlabel);
currlabel = nextlabel;
temp2 = temp2->sibling->sibling;
}
temp2 = temp->sibling->child->child;
fprintf(fp, "label_%d:\n", currlabel);
while(temp2 != NULL)
{
code_statement(fp, temp2);
temp2 = temp2->sibling;
}
fprintf(fp, "label_%d:\n", endlabel);
}
else
{
fprintf(fp, "\tand r8, 0000000000000001h\n");
fprintf(fp, "\tpush r8\n");
stacknode* temp = curr->child->sibling->sibling;
stacknode* temp2 = temp->child;
int currlabel = getlabel();
int endlabel = getlabel();
int nextlabel;
while(temp2 != NULL)
{
nextlabel = getlabel();
fprintf(fp, "label_%d:\n", currlabel);
fprintf(fp, "\tpop r8\n"); //pop id from stack
fprintf(fp, "\tpush r8\n"); //push id to stack
if(strcmp(temp2->child->tokinfo->lexeme, "true") == 0)
fprintf(fp, "\tmov r9, 0000000000000001h\n");
else
fprintf(fp, "\tmov r9, 0000000000000000h\n");
fprintf(fp, "\tcmp r8,r9\n");
fprintf(fp, "\tjne label_%d\n", nextlabel);
stacknode* temp3 = temp2->sibling->child;
while(temp3 != NULL)
{
code_statement(fp, temp3);
temp3 = temp3->sibling;
}
fprintf(fp, "\tjmp label_%d\n",endlabel);
currlabel = nextlabel;
temp2 = temp2->sibling->sibling;
}
fprintf(fp, "label_%d:\n", nextlabel);
fprintf(fp, "label_%d:\n", endlabel);
}
return;
}
void code_statement(FILE* fp, stacknode* temp)
{
if(strcmp(temp->ntortinfo->str, "<declareStmt>") == 0)
return;
if(strcmp(temp->ntortinfo->str, "<ioStmt>") == 0)
{
// mov rdi, scanint
// lea rsi, [x]
// mov al,0
// call scanf
if(strcmp(temp->child->ntortinfo->str, "GET_VALUE") == 0)
{
idsymboltable* idst = temp->child->sibling->idst;
idsymboltablenode* pt = getidsymboltablenode(temp->child->sibling->tokinfo->lexeme, idst);
if(strcmp(pt->type->ntortinfo->str, "INTEGER") == 0)
{
fprintf(fp, "\tmov rdi, get_val\n");
fprintf(fp, "\tlea rsi, [%s]\n", temp->child->sibling->tokinfo->lexeme);
fprintf(fp, "\tmov al, 0\n");
fprintf(fp, "\tcall scanf\n");
fprintf(fp, "\n");
}
else if(strcmp(pt->type->ntortinfo->str, "ARRAY") == 0)
{
if(strcmp(pt->type->child->sibling->ntortinfo->str, "INTEGER") == 0)
{
int startlabel = getlabel();
int endlabel = getlabel();
fprintf(fp, "\tmov r11, 0\n"); //array curr index
fprintf(fp, "\tmov r8, %s\n", pt->type->child->child->tokinfo->lexeme);
fprintf(fp, "\tpush r8\n");
fprintf(fp, "label_%d:\n",startlabel);
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tmov r9, %s\n", pt->type->child->child->sibling->tokinfo->lexeme);
fprintf(fp, "\tcmp r8, r9\n");
fprintf(fp, "\tjg label_%d\n",endlabel);
fprintf(fp, "\tmov rdi, get_val\n");
fprintf(fp, "\tlea rsi, [%s]\n", pt->idlex);
fprintf(fp, "\tadd rsi, r11\n");
fprintf(fp, "\tmov al,0\n");
fprintf(fp, "\tpush r11\n");
fprintf(fp, "\tcall scanf\n");
fprintf(fp, "\tpop r11\n");
fprintf(fp, "\tadd r11, 4\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tinc r8\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", startlabel);
fprintf(fp, "label_%d:\n", endlabel);
fprintf(fp, "\tpop r8\n");
}
}
return;
}
else if(strcmp(temp->child->ntortinfo->str, "PRINT") == 0)
{
stacknode* temp2 = temp->child->sibling->child;
if(strcmp(temp2->ntortinfo->str, "NUM") == 0)
{
fprintf(fp, "\tmov rdi, print_val\n");
fprintf(fp, "\tmov rsi, %s\n", temp2->tokinfo->lexeme);
fprintf(fp, "\tmov al, 0\n");
fprintf(fp, "\tcall printf\n");
fprintf(fp, "\n");
}
else
{
idsymboltable* idst = temp2->idst;
idsymboltablenode* pt = getidsymboltablenode(temp2->tokinfo->lexeme, idst);
if(pt->widthofid == 1 || pt->widthofid == 2)
{
fprintf(fp, "\tmov rdi, print_val\n");
fprintf(fp, "\tmov rax, [%s]\n", temp2->tokinfo->lexeme);
if(pt->widthofid == 2)
fprintf(fp, "\tand rax, 00000000000000ffh\n");
else if(pt->widthofid == 1)
fprintf(fp, "\tand rax, 000000000000000fh\n");
fprintf(fp, "\tmov rsi, rax\n");
fprintf(fp, "\tmov al, 0\n");
fprintf(fp, "\tcall printf\n");
fprintf(fp, "\n");
}
else
{
if(strcmp(pt->type->child->sibling->ntortinfo->str, "INTEGER") == 0)
{
int startlabel = getlabel();
int endlabel = getlabel();
fprintf(fp, "\tmov r11, 0\n"); //array curr index
fprintf(fp, "\tmov r8, %s\n", pt->type->child->child->tokinfo->lexeme);
fprintf(fp, "\tpush r8\n");
fprintf(fp, "label_%d:\n",startlabel);
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tmov r9, %s\n", pt->type->child->child->sibling->tokinfo->lexeme);
fprintf(fp, "\tcmp r8, r9\n");
fprintf(fp, "\tjg label_%d\n",endlabel);
fprintf(fp, "\tmov rdi, print_val\n");
fprintf(fp, "\tmov rax, [%s+r11]\n", pt->idlex);
fprintf(fp, "\tand rax, 00000000000000ffh\n");
// fprintf(fp, "\tadd rsi, r11\n");
fprintf(fp, "\tmov rsi, rax\n");
fprintf(fp, "\tmov al,0\n");
fprintf(fp, "\tpush r11\n");
fprintf(fp, "\tcall printf\n");
fprintf(fp, "\tpop r11\n");
fprintf(fp, "\tadd r11, 4\n");
fprintf(fp, "\tpop r8\n");
fprintf(fp, "\tinc r8\n");
fprintf(fp, "\tpush r8\n");
fprintf(fp, "\tjmp label_%d\n", startlabel);
fprintf(fp, "label_%d:\n", endlabel);
fprintf(fp, "\tpop r8\n");
}
}
}
}
}
if(strcmp(temp->ntortinfo->str, "<assignmentStmt>") == 0)
{
if(strcmp(temp->child->sibling->ntortinfo->str, "<lvalueIDStmt>") == 0)
{
codegenexp(fp, temp->child->sibling->child);
fprintf(fp, "\tpop r8\n");
idsymboltable* idst = temp->child->idst;
idsymboltablenode* pt = getidsymboltablenode(temp->child->tokinfo->lexeme, idst);
if(pt->widthofid == 2)
fprintf(fp, "\tmov [%s], r8w\n", temp->child->tokinfo->lexeme);//for integers
else
fprintf(fp, "\tmov [%s], r8b\n", temp->child->tokinfo->lexeme);//for booleans
}
}
if(strcmp(temp->ntortinfo->str, "<iterativeStmt>") == 0)
{
codegeniterative(fp, temp);
}
if(strcmp(temp->ntortinfo->str, "<condionalStmt>") == 0)
{
codegenconditional(fp, temp);
}
return;
}
void traverseAST_forCodegen(FILE* fp, stacknode* curr)
{
if(curr == NULL)
return;
stacknode* temp = curr->child->sibling->sibling;//<driverModule>
temp = temp->child->sibling;//<moduleDef>
temp = temp->child->sibling->child;//<statements>->child
while(temp != NULL)
{
code_statement(fp, temp);
temp = temp->sibling;
}
return;
}
void generate_code(FILE* fp, mainsymboltable* globalTable, stacknode* astroot)
{
initialize(fp, globalTable);
traverseAST_forCodegen(fp, astroot);
endcode(fp);
}