-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
448 lines (429 loc) · 11.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
// BATCH NUMBER 30
// SHIVAM GUPTA 2014A7PS066P
// PULKIT GUPTA 2014A7PS157P
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "codegen.h"
int num_relop = 0;
int num_switch = 0;
int pr_switch = 0;
int num_for = 0;
int num_while = 0;
int index_fg = 0;
void code_generator_assignment(node* head)
{
if(head==NULL)
return;
node *temp = head->child;
while(temp!=NULL)
{
code_generator_assignment(temp);
temp = temp->next;
}
int op;
//CURRENT NODE
if(head->is_non_term==0)
{
if(strcmp(head->ptr->tokenName,"ID")==0)
{
//printf("in ID\n");
type_checker_st[++tc_ptr] = head;
printf("MOV AX,word[MEM+%d]\n",head->sym_ptr->offset);
printf("PUSH AX\n");
}
else if(strcmp(head->ptr->tokenName,"NUM")==0)
{
//printf("in NUM\n");
type_checker_st[++tc_ptr] = head;
printf("MOV AX,%s\n",head->ptr->tokenVal->tokenVal);
printf("PUSH AX\n");
}
else if(strcmp(head->ptr->tokenName,"TRUE")==0)
{
//printf("in NUM\n");
type_checker_st[++tc_ptr] = head;
printf("MOV AX,1\n");
printf("PUSH AX\n");
}
else if(strcmp(head->ptr->tokenName,"FALSE")==0)
{
//printf("in NUM\n");
type_checker_st[++tc_ptr] = head;
printf("MOV AX,0\n");
printf("PUSH AX\n");
}
else if(check_op(head->ptr->tokenName)==NEGATION)
{
//printf("in NEGATION\n");
printf("POP AX\n");
printf("IMUL -1\n");
printf("PUSH AX\n");
}
else if(op=check_op(head->ptr->tokenName))
{
node* temp = getMem_PT(0,0,NULL);
printf("POP BX\n");
printf("POP AX\n");
tc_ptr--;
// checking for array
if(type_checker_st[tc_ptr]->par!=NULL && type_checker_st[tc_ptr]->par->child!=type_checker_st[tc_ptr])
{
//printf("got in\n");
node* tt = type_checker_st[tc_ptr]->par->child;
while(tt->next!=type_checker_st[tc_ptr])
tt = tt->next;
if(tt->is_non_term==0 && strcmp(tt->ptr->tokenName,"SQBO")==0)
{
//printf("yes found\n");
if(type_checker_st[tc_ptr]->sym_ptr==NULL)
{
int a = atoi(type_checker_st[tc_ptr]->ptr->tokenVal->tokenVal);
printf("MOV word[arr],%d\n",2*a-2);
}
else
{
printf("MOV AX,word[MEM+%d]\n",type_checker_st[tc_ptr]->sym_ptr->offset);
if(type_checker_st[tc_ptr]->sym_ptr->subtype==INTEGER)
{
printf("IMUL 2\n");
printf("SUB AX,2\n");
}
else
printf("SUB AX,1\n");
printf("MOV word[arr],AX\n");
}
index_fg = 1;
printf("POP AX\n");
tc_ptr--;
}
else
tc_ptr--;
}
if(op==ASSIGNOP)
{
if(type_checker_st[tc_ptr]->sym_ptr->type==BOOLEAN)
{
if(index_fg)
{
index_fg = 0;
printf("MOV CX,word[arr]\n");
printf("AND ECX,0x00001111\n");
printf("MOV byte[MEM+%d+ECX],BL\n",type_checker_st[tc_ptr]->sym_ptr->offset);
}
else
printf("MOV byte[MEM+%d],BL\n",type_checker_st[tc_ptr]->sym_ptr->offset);
}
else
{
if(index_fg)
{
index_fg = 0;
printf("MOV CX,word[arr]\n");
printf("AND ECX,0x00001111\n");
printf("MOV word[MEM+%d+ECX],BX\n",type_checker_st[tc_ptr]->sym_ptr->offset);
}
else
printf("MOV word[MEM+%d],BX\n",type_checker_st[tc_ptr]->sym_ptr->offset);
}
tc_ptr--;
}
else if(strcmp(head->ptr->tokenName,"PLUS")==0)
{
printf("ADD AX,BX\n");
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
}
else if(strcmp(head->ptr->tokenName,"MINUS")==0)
{
printf("SUB AX,BX\n");
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
}
else if(strcmp(head->ptr->tokenName,"MUL")==0)
{
printf("IMUL BX\n");
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
}
else if(strcmp(head->ptr->tokenName,"DIV")==0)
{
printf("IDIV BX\n");
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
}
else if(strcmp(head->ptr->tokenName,"AND")==0)
{
printf("AND AX,BX\n");
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
}
else if(strcmp(head->ptr->tokenName,"OR")==0)
{
printf("AND AX,BX\n");
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
}
else if(strcmp(head->ptr->tokenName,"LT")==0)
{
printf("CMP AX,BX\n");
printf("JL TRUE%d\n",num_relop);
printf("MOV AX,0\n");
printf("JMP FALSE%d\n",num_relop);
printf("TRUE%d: MOV AX,1\n",num_relop);
printf("FALSE%d:\n",num_relop);
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
num_relop++;
}
else if(strcmp(head->ptr->tokenName,"LE")==0)
{
printf("CMP AX,BX\n");
printf("JLE TRUE%d\n",num_relop);
printf("MOV AX,0\n");
printf("JMP FALSE%d\n",num_relop);
printf("TRUE%d: MOV AX,1\n",num_relop);
printf("FALSE%d:\n",num_relop);
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
num_relop++;
}
else if(strcmp(head->ptr->tokenName,"GT")==0)
{
printf("CMP AX,BX\n");
printf("JG TRUE%d\n",num_relop);
printf("MOV AX,0\n");
printf("JMP FALSE%d\n",num_relop);
printf("TRUE%d: MOV AX,1\n",num_relop);
printf("FALSE%d:\n",num_relop);
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
num_relop++;
}
else if(strcmp(head->ptr->tokenName,"GE")==0)
{
printf("CMP AX,BX\n");
printf("JGE TRUE%d\n",num_relop);
printf("MOV AX,0\n");
printf("JMP FALSE%d\n",num_relop);
printf("TRUE%d: MOV AX,1\n",num_relop);
printf("FALSE%d:\n",num_relop);
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
num_relop++;
}
else if(strcmp(head->ptr->tokenName,"EQ")==0)
{
printf("CMP AX,BX\n");
printf("JE TRUE%d\n",num_relop);
printf("MOV AX,0\n");
printf("JMP FALSE%d\n",num_relop);
printf("TRUE%d: MOV AX,1\n",num_relop);
printf("FALSE%d:\n",num_relop);
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
num_relop++;
}
else if(strcmp(head->ptr->tokenName,"NQ")==0)
{
printf("CMP AX,BX\n");
printf("JNE TRUE%d\n",num_relop);
printf("MOV AX,0\n");
printf("JMP FALSE%d\n",num_relop);
printf("TRUE%d: MOV AX,1\n",num_relop);
printf("FALSE%d:\n",num_relop);
printf("PUSH AX\n");
type_checker_st[tc_ptr] = temp;
num_relop++;
}
}
}
}
void code_generator_conditional(node* head)
{
pr_switch = 0;
node* curr = head->child->next;
node* temp;
if(curr->sym_ptr==NULL)
printf("null in switch Please check\n");
else if(curr->sym_ptr->type==BOOLEAN)
{
printf("MOV CL,byte[MEM+%d]\n",curr->sym_ptr->offset);
temp = curr->next;
while(!(temp->is_non_term==0 && strcmp(temp->ptr->tokenName,"epsilon")==0))
{
if(strcmp((temp->child->next)->ptr->tokenName,"TRUE")==0)
printf("CMP CL,1\n");
else if(strcmp((temp->child->next)->ptr->tokenName,"FALSE")==0)
printf("CMP CL,0\n");
else
printf("CMP CL,%s\n",(temp->child->next->ptr->tokenVal->tokenVal));
printf("JNZ C%d%d\n",num_switch,pr_switch);
code_generator(temp->child->next->next);
printf("JMP SWITCH_EXIT%d\n",num_switch);
printf("C%d%d:\n",num_switch,pr_switch);
pr_switch++;
temp = temp->child->next->next->next;
}
printf("SWITCH_EXIT%d:\n",num_switch);
}
else if(curr->sym_ptr->type==INTEGER)
{
printf("MOV CX,word[MEM+%d]\n",curr->sym_ptr->offset);
temp = curr->next;
while(!(temp->is_non_term==0 && strcmp(temp->ptr->tokenName,"epsilon")==0))
{
printf("CMP CX,%s\n",(temp->child->next->ptr->tokenVal->tokenVal));
printf("JNZ C%d%d\n",num_switch,pr_switch);
code_generator(temp->child->next->next);
printf("JMP SWITCH_EXIT%d\n",num_switch);
printf("C%d%d:\n",num_switch,pr_switch);
pr_switch++;
temp = temp->child->next->next->next;
}
//default statement
code_generator(curr->next->next->child->next);
printf("SWITCH_EXIT%d:\n",num_switch);
}
else
printf("ERROR_22: invalid input in code_generator_conditional\n");
num_switch++;
}
void code_generator_iterative(node* head)
{
if(strcmp(head->child->ptr->tokenName,"FOR")==0)
{
num_for++;
node* curr = head->child->next;
node* num1 = curr->next->child;
node* num2 = num1->next;
printf("MOV CX,%s\n",num1->ptr->tokenVal->tokenVal);
printf("MOV word[MEM+%d],CX\n",curr->sym_ptr->offset);
printf("FOR_LOOP%d:\n",num_for);
code_generator(curr->next->next);
printf("MOV CX,word[MEM+%d]\n",curr->sym_ptr->offset);
printf("INC CX\n");
printf("MOV word[MEM+%d],CX\n",curr->sym_ptr->offset);
printf("MOV AX,%s\n",num2->ptr->tokenVal->tokenVal);
printf("CMP CX,AX\n");
printf("JLE FOR_LOOP%d\n",num_for);
}
else if(strcmp(head->child->ptr->tokenName,"WHILE")==0)
{
num_while++;
code_generator(head->child->next);
printf("POP AX\n");
printf("CMP AL,1\n");
printf("JNZ WHILE_END%d\n",num_while);
printf("WHILE_LOOP%d:\n",num_while);
code_generator(head->child->next->next);
code_generator(head->child->next);
printf("POP AX\n");
printf("CMP AL,1\n");
printf("JZ WHILE_LOOP%d\n",num_while);
printf("WHILE_END%d:\n",num_while);
}
}
void code_generator_print(node *head)
{
printf("\n");
printf("mov rax,0\n");
printf("MOV rbx,0xFFFFFFFFFFFFFFFF\n");
printf("cmovl rax,rbx\n");
if(head->child->next->is_non_term==0)
{
if(strcmp(head->child->next->ptr->tokenName,"TRUE")==0)
printf("mov AX,1\n");
else if(strcmp(head->child->next->ptr->tokenName,"FALSE")==0)
printf("mov AX,0\n");
else
printf("mov AX,%s\n",head->child->next->ptr->tokenVal->tokenVal);
}
else if(head->child->next->child->next->is_non_term==0 && strcmp(head->child->next->child->next->ptr->tokenName,"epsilon")==0)
printf("mov AX,word[MEM+%d]\n",head->child->next->child->sym_ptr->offset);
else
{
node* curr = head->child->next->child;
node* temp = curr->next->child->next;
if(temp->sym_ptr==NULL)
{
int a = atoi(temp->ptr->tokenVal->tokenVal);
printf("MOV word[arr],%d\n",a=a*2-2);
}
else
{
printf("MOV AX,word[MEM+%d]\n",temp->sym_ptr->offset);
if(curr->sym_ptr->subtype==INTEGER)
{
printf("IMUL 2\n");
printf("SUB AX,2\n");
}
else
printf("SUB AX,1\n");
printf("MOV word[arr],AX\n");
}
printf("MOV CX,word[arr]\n");
printf("AND ECX,0x00001111\n");
printf("mov AX,word[MEM+%d+ECX]\n",curr->sym_ptr->offset);
}
printf("push rax\n");
printf(" push rcx\n");
printf("\tmov rdi, men\n");
printf("\tmov rsi, rax\n");
printf("\txor rax, rax\n");
printf("\tcall printf\n");
printf(" pop rcx\n");
printf("pop rax\n");
}
void code_generator_input(node* head)
{
node* curr = head->child->next;
printf("\n");
printf("MOV RSI,var+0\n");
printf("MOV RDI,inputFormat\n");
printf("XOR RAX,RAX\n");
printf("call scanf\n");
printf("MOV AX,word[var+0]\n");
if(curr->sym_ptr->type==INTEGER)
printf("MOV word[MEM+%d],AX\n",curr->sym_ptr->offset);
else
printf("MOV byte[MEM+%d],AL\n",curr->sym_ptr->offset);
}
//DO THIS WHEN NO ERRORS
void code_generator(node* head)
{
if(head==NULL)
return;
if(head->is_non_term && strcmp(hash_nonterm[head->hash_val],"<assignmentStmt")==0)
{
tc_ptr = -1;
code_generator_assignment(head);
return;
}
else if(head->is_non_term && strcmp(hash_nonterm[head->hash_val],"<condionalStmt")==0)
{
code_generator_conditional(head);
return;
}
else if(head->is_non_term && strcmp(hash_nonterm[head->hash_val],"<iterativeStmt")==0)
{
code_generator_iterative(head);
return;
}
else if(head->is_non_term && strcmp(hash_nonterm[head->hash_val],"<ioStmt")==0 && strcmp(head->child->ptr->tokenName,"PRINT")==0)
{
code_generator_print(head);
return;
}
else if(head->is_non_term && strcmp(hash_nonterm[head->hash_val],"<ioStmt")==0 && strcmp(head->child->ptr->tokenName,"GET_VALUE")==0)
{
code_generator_input(head);
return;
}
node *temp = head->child;
while(temp!=NULL)
{
code_generator(temp);
temp = temp->next;
}
}