-
Notifications
You must be signed in to change notification settings - Fork 1
/
codeGen.c
473 lines (446 loc) · 18.4 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
// Batch Number - 38
// 2014A7PS102P - RISHABH JOSHI
// 2014A7PS248P - DEEP VYAS
#include <stdio.h>
#include <stdlib.h>
#include "codeGenDef.h"
#include "typeCheckingDef.h"
// extern ASTNode *astroot;
//extern HashTreeNode *htroot;
ASTNode *ast_root;
HashTreeNode *htroot;
FILE *fp;
#define _arrtype 3
#define encodingMul 1000000
#define INTEGEROFF 8
int typeCheck(char *filename){
if (ast_root == NULL) // Checking this will ensure parse tree is not
// populated with wrong line numbers etc again for option 6
ast_root = makeAST(filename);
htroot = initTree();
int check = 1, flag = 1, check2 = 1;
check = parseAST(ast_root, htroot);
if (check == 0)
flag = 0;
check2 = parseASTAgain(ast_root, htroot);
if (check2 == 0)
flag = 0;
return flag;
}
int getOffset(ASTNode *astroot, HashTreeNode *htroot){
HashTableNode *entry;
entry = find2(astroot->tokenptr->lexeme.iden, htroot, 0);
if (entry == NULL) {
//printf("SOME PROBLEM!\n");
return -1;
}
else {
//printf("key = %s\n", entry->key);
//printf("offset %d\n", entry->offset);
return entry->offset;
}
}
int pushOffset = 0;
int initialOffset = 0;
int labelNumber = 0;
void expression_cg(ASTNode *astroot, HashTreeNode *htroot){
int offset;
int dochild = 1;
//printf("ASTROOT : %d\n", astroot->gnode.non_term);
if (astroot->t == NONTERMINAL && astroot->gnode.non_term == VAR){
if (astroot->vartype == 1){ //ID
astroot->memoryLocation = getOffset(astroot, htroot);
}
else if(astroot->vartype == 2){ //array
offset = getOffset(astroot->child, htroot); // memloc of arr
offset = offset*encodingMul + getOffset(astroot->child->sibling, htroot); // memloc of index 2
astroot->memoryLocation = offset;
astroot->decode=1;
}
else if (astroot->vartype == 0){
int memLoc = pushOffset+initialOffset;
pushOffset += INTEGEROFF;
fprintf(fp, "\n; Var number to memory\n");
fprintf(fp, " mov qword [location+%d], %d\n",
memLoc, astroot->value.num);
astroot->memoryLocation = memLoc;
}
if (astroot->sign == -1){
fprintf(fp, ";negating var\n");
fprintf(fp, " mov r8, [location+%d]\n", astroot->memoryLocation+initialOffset);
fprintf(fp, " neg r8\n");
int newMemLoc = pushOffset+initialOffset;
pushOffset += INTEGEROFF;
fprintf(fp, " mov [location+%d], r8\n", newMemLoc);
astroot->memoryLocation = newMemLoc;
}
dochild = 0;
}
// r8 and r9 fixed for the operators
else if(astroot->t == TERMINAL && (astroot->gnode.term == PLUS || astroot->gnode.term == MINUS || astroot->gnode.term == MUL || astroot->gnode.term == DIV)){
expression_cg(astroot->child, htroot);
expression_cg(astroot->child->sibling, htroot);
int memloc1 = astroot->child->memoryLocation+initialOffset,
memloc2 = astroot->child->sibling->memoryLocation+initialOffset;
fprintf(fp, "\n;evaluating arith operator\n");
int flag = 0;
if (astroot->child->decode==1){
int arr = memloc1/encodingMul;
int indx = memloc1 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov r8, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (astroot->child->sibling->decode==1){
int arr = memloc2/encodingMul;
int indx = memloc2 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov r9, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (!flag){
fprintf(fp, " mov r8, [location+%d]\n", memloc1);
fprintf(fp, " mov r9, [location+%d]\n", memloc2);
}
if (astroot->gnode.non_term == PLUS)
fprintf(fp, " add r8, r9\n");
else if (astroot->gnode.non_term == MINUS)
fprintf(fp, " sub r8, r9\n");
else if (astroot->gnode.non_term == MUL)
fprintf(fp, " imul r8, r9\n");
else if (astroot->gnode.non_term == DIV){
fprintf(fp, " xor rax, rax\n");// TO clear the contents
fprintf(fp, " xor rdx,rdx\n");
fprintf(fp, " mov rax, r8\n");
fprintf(fp, " idiv r9\n");
fprintf(fp, " mov r8, rax\n");// move back
}
int storeloc = pushOffset+initialOffset;
pushOffset += INTEGEROFF;
if(astroot->sign == -1){
fprintf(fp, ";negating arith operator\n");
fprintf(fp, " neg r8\n");
}
fprintf(fp, " mov [location+%d], r8\n", storeloc);
astroot->memoryLocation = storeloc;
dochild = 0;
}
else if(astroot->t == TERMINAL && (astroot->gnode.term == LE || astroot->gnode.term == LT || astroot->gnode.term == NE || astroot->gnode.term == GE || astroot->gnode.term == GT || astroot->gnode.term == EQ)){
expression_cg(astroot->child, htroot);
expression_cg(astroot->child->sibling, htroot);
int memloc1 = astroot->child->memoryLocation+initialOffset,
memloc2 = astroot->child->sibling->memoryLocation + initialOffset;
fprintf(fp, "\n;evaluating relational operator\n");
int flag = 0;
if (astroot->child->decode==1){
int arr = memloc1/encodingMul;
int indx = memloc1 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov r8, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (astroot->child->sibling->decode==1){
int arr = memloc2/encodingMul;
int indx = memloc2 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov r9, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (!flag){
fprintf(fp, " mov r8, [location+%d]\n", memloc1);
fprintf(fp, " mov r9, [location+%d]\n", memloc2);
}
fprintf(fp, " cmp r8, r9\n");
if (astroot->gnode.non_term == LE)
fprintf(fp, " jle _true_%d\n", labelNumber);
else if (astroot->gnode.non_term == LT)
fprintf(fp, " jl _true_%d\n", labelNumber);
else if (astroot->gnode.non_term == NE)
fprintf(fp, " jne _true_%d\n", labelNumber);
else if (astroot->gnode.non_term == EQ)
fprintf(fp, " je _true_%d\n", labelNumber);
else if (astroot->gnode.non_term == GE)
fprintf(fp, " jge _true_%d\n", labelNumber);
else if (astroot->gnode.non_term == GT)
fprintf(fp, " jg _true_%d\n", labelNumber);
fprintf(fp, "_false_%d:\n mov r8, 0\n jmp _continue_%d\n", labelNumber, labelNumber);
fprintf(fp, "_true_%d:\n mov r8, 1\n", labelNumber);
fprintf(fp, "_continue_%d:\n \n", labelNumber++);
int storeloc = pushOffset+initialOffset;
pushOffset += INTEGEROFF;
if(astroot->sign == -1){
fprintf(fp, ";negating relational operator\n");
fprintf(fp, " neg r8\n");
}
fprintf(fp, " mov [location+%d], r8\n", storeloc);
astroot->memoryLocation = storeloc;
dochild = 0;
}
else if(astroot->t == TERMINAL && (astroot->gnode.term == AND || astroot->gnode.term == OR)){
expression_cg(astroot->child, htroot);
expression_cg(astroot->child->sibling, htroot);
int memloc1 = astroot->child->memoryLocation + initialOffset,
memloc2 = astroot->child->sibling->memoryLocation + initialOffset;
fprintf(fp, "\n;evaluating logical operator\n");
int flag = 0;
if (astroot->child->decode==1){
int arr = memloc1/encodingMul;
int indx = memloc1 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov r8, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (astroot->child->sibling->decode==1){
int arr = memloc2/encodingMul;
int indx = memloc2 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov r9, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (!flag){
fprintf(fp, " mov r8, [location+%d]\n", memloc1);
fprintf(fp, " mov r9, [location+%d]\n", memloc2);
}
if (astroot->gnode.non_term == AND)
fprintf(fp, " and r8, r9\n");
else if (astroot->gnode.non_term == OR)
fprintf(fp, " or r8, r9\n");
int storeloc = pushOffset + initialOffset;
pushOffset += INTEGEROFF;
if(astroot->sign == -1){
fprintf(fp, ";negating logical operator\n");
fprintf(fp, " neg r8\n");
}
fprintf(fp, " mov [location+%d], r8\n", storeloc);
astroot->memoryLocation = storeloc;
dochild = 0;
}
if (dochild){
ASTNode *tempemp = astroot->child;
while(tempemp != NULL){
expression_cg(tempemp, htroot);
tempemp = tempemp->sibling;
}
}
// ASTNode *children = astroot->child;
// expression_cg(children);
}
void codegen(ASTNode *astroot){
HashTreeNode *htroot = (HashTreeNode*)astroot->htPointer;
if(astroot->gnode.non_term==DRIVERMODULE){
pushOffset = ((HashTreeNode *)astroot->htPointer)->curr_offset;
fprintf(fp,"global main\n");
fprintf(fp,"extern printf\n");
fprintf(fp,"extern scanf\n");
fprintf(fp,"section .text\n");
fprintf(fp,"main:\n");
fprintf(fp, " mov rbp,rsp\n");
ASTNode *it=astroot->child;
while(it!=NULL){
codegen(it);
it=it->sibling;
}
}
else if(astroot->t == TERMINAL && astroot->gnode.term == ID){
astroot->memoryLocation = getOffset(astroot, htroot);
}
else if(astroot->gnode.non_term==ASSIGNMENTSTMT){
codegen(astroot->child->sibling);
expression_cg(astroot->child, htroot);
int memloc1 = astroot->child->memoryLocation + initialOffset;
int memloc2 = astroot->child->sibling->memoryLocation+initialOffset;
fprintf(fp, "\n; assignment\n");
int flag = 0, flag2 = 0;
if (astroot->child->sibling->decode==1){
int arr = memloc2/encodingMul;
int indx = memloc2 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov r8, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (astroot->child->decode==1){
int arr = memloc1/encodingMul;
int indx = memloc1 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov [location + r11*%d + %d], r8\n",INTEGEROFF,arr);
flag2 = 1;
}
if (flag == 0){
fprintf(fp, " mov r8, [location+%d]\n", memloc2);
}
if (flag2 == 0){
fprintf(fp, " mov [location+%d], r8\n", memloc1);
}
// Other logic here.
}
else if(astroot->gnode.non_term==EXPRESSION){
//printf("Expression code starts.\n");
expression_cg(astroot->child, htroot);
astroot->memoryLocation = astroot->child->memoryLocation;
astroot->decode = astroot->child->decode;
//printf("value here : %d\n", astroot->memoryLocation);
}
else if(astroot->t == NONTERMINAL && astroot->gnode.non_term == STATEMENT &&
astroot->stmttype == IOSTMT){
if(astroot->child->gnode.term == GET_VALUE){
HashTableNode *entry = find2(astroot->child->sibling->tokenptr->lexeme.iden, htroot,0);
int datatype = entry->datatype;
if (datatype == 3){
ASTNode *idnode = astroot->child->sibling;
int lrange = entry->ast_node->lrange, rrange = entry->ast_node->rrange;
codegen(idnode);
int memloc1 = idnode->memoryLocation + initialOffset;
while(lrange != rrange+1){
fprintf(fp, "\n; getting array value\n");
fprintf(fp, " push rbp\n");
fprintf(fp, " mov rdi, formatin\n");
fprintf(fp, " lea rsi, [location+%d]\n", memloc1);
fprintf(fp, " xor rax, rax\n");
fprintf(fp, " call scanf\n");
fprintf(fp, " pop rbp\n");
memloc1 += INTEGEROFF;
lrange++;
}
}
else {
codegen(astroot->child->sibling);
int memloc1 = astroot->child->sibling->memoryLocation + initialOffset;
// Read value into memloc 1
fprintf(fp, "\n; getting value\n");
fprintf(fp, " push rbp\n");
fprintf(fp, " mov rdi, formatin\n");
fprintf(fp, " lea rsi, [location+%d]\n", memloc1);
fprintf(fp, " xor rax, rax\n");
fprintf(fp, " call scanf\n");
fprintf(fp, " pop rbp\n");
}
}
else{
expression_cg(astroot->child->sibling, htroot);
int memloc1 = astroot->child->sibling->memoryLocation + initialOffset;
// print value in memloc 1
fprintf(fp, "\n; printing value\n");
fprintf(fp, " push rbp\n");
fprintf(fp, " mov rdi, formatout\n");
int flag = 0;
if (astroot->child->sibling->decode==1){
int arr = memloc1/encodingMul;
int indx = memloc1 - arr*encodingMul;
fprintf(fp, " mov r11, [location+%d]\n", indx);
fprintf(fp, " mov rsi, [location + r11*%d + %d]\n",INTEGEROFF,arr);
flag = 1;
}
if (!flag){
fprintf(fp, " mov rsi, [location+%d]\n", memloc1);
}
fprintf(fp, " xor rax, rax\n");
fprintf(fp, " call printf\n");
fprintf(fp, " pop rbp\n");
}
}
else if(astroot->t == NONTERMINAL && astroot->gnode.non_term == STATEMENT &&
astroot->stmttype == ITERATIVESTMT&&astroot->looptype==FOR){
codegen(astroot->child);
fprintf(fp, "\n; For loop generation\n");
fprintf(fp, " mov r14, %d\n",astroot->child->lrange);
fprintf(fp, " mov r15, %d\n",astroot->child->rrange);
fprintf(fp, "_for_%d:\n",labelNumber);
int initlabel=labelNumber;
codegen(astroot->child->sibling);
fprintf(fp, " add qword [location+%d], 1\n", astroot->child->memoryLocation+initialOffset);
fprintf(fp, " sub r15, 1\n");
fprintf(fp, " cmp r14, r15\n");
fprintf(fp, " jle _for_%d\n", initlabel);
labelNumber++;
fprintf(fp, "\n; For loop end\n");
}
else if(astroot->t == NONTERMINAL && astroot->gnode.non_term == STATEMENT &&
astroot->stmttype == ITERATIVESTMT&&astroot->looptype==WHILE){
fprintf(fp, "\n; While loop generation\n");
fprintf(fp, "_while_%d:\n",labelNumber);
int initlabel=labelNumber;
//printf("While loop child :%d %d\n",astroot->child->t, astroot->child->gnode.non_term);
expression_cg(astroot->child,htroot);
fprintf(fp, " mov r13,[location+%d]\n", astroot->child->memoryLocation+initialOffset);
codegen(astroot->child->sibling);
fprintf(fp, " cmp r13, 0\n");
fprintf(fp, " jne _while_%d\n", initlabel);
labelNumber++;
fprintf(fp, "\n; While loop end\n");
}
else if(astroot->t == NONTERMINAL && astroot->gnode.non_term == VAR){
expression_cg(astroot,htroot);
}
else if(astroot->t == NONTERMINAL && astroot->gnode.non_term == STATEMENT &&
astroot->stmttype == CONDITIONALSTMT){
codegen(astroot->child);
int memloc1=astroot->child->memoryLocation+initialOffset;
fprintf(fp, "\n; Switch statement\n");
int initlabel=labelNumber;
fprintf(fp, " mov r10, [location+%d]\n", memloc1);
ASTNode *child=astroot->child->sibling->child;
int value;
while(child!=NULL){
if(child->dtype==0){
value = child->value.num;
}
else{
value= child->value.tval;
}
fprintf(fp, " cmp r10,%d\n",value);
fprintf(fp, " je _case_%d_%d_\n",initlabel,value);
child = child->sibling;
}
if (astroot->child->sibling->sibling->child != NULL){
fprintf(fp, " jmp _case_%d_default_\n", initlabel);
}
else {
fprintf(fp, " jmp _case_%d_end_\n", initlabel);
}
child = astroot->child->sibling->child;
while(child!=NULL){
if(child->dtype==0){
value = child->value.num;
}
else{
value= child->value.tval;
}
fprintf(fp, "\n_case_%d_%d_:\n", initlabel, value);
codegen(child);
fprintf(fp, " jmp _case_%d_end_\n", initlabel);
child = child->sibling;
}
if (astroot->child->sibling->sibling->child != NULL){
fprintf(fp, "\n_case_%d_default_:\n", initlabel);
codegen(astroot->child->sibling->sibling->child);
}
fprintf(fp, "\n_case_%d_end_:\n", initlabel);
fprintf(fp, "\n; Switch statement end\n");
labelNumber++;
}
else{
//printf("Entering here : %d %d\n",astroot->gnode.non_term,EXPRESSION);
ASTNode *it=astroot->child;
while(it!=NULL){
codegen(it);
it=it->sibling;
}
}
}
//int main(int argc,char* argv[]){
//fp = fopen(argv[2],"w");
//int check = typeCheck(argv[1]);
//ASTNode *astroot = ast_root;
//if(check)
//codegen(astroot->child->sibling->sibling);
//// _printAST(ast_root);
//fprintf(fp," mov rax, 0\n");
//fprintf(fp," ret\n");
//char format[] = "db \"%d\", 10, 0";
//char formatin[] = "db \"%d\", 0";
//fprintf(fp,"section .data\n");
//fprintf(fp," formatout: %s\n", format);
//fprintf(fp," formatin: %s\n", formatin);
//fprintf(fp," location: times 65536 db 0\n");
//return 0;
//}