-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.c
399 lines (387 loc) · 8.75 KB
/
generate.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
#include "operators.h"
#include "types.h"
#include "symbol.h"
extern symnode *root,*Lroot;
extern Lbind_base;
static int reg[8];
static int labelcount;
int initialiseCodeGen()
{
int i;
for(i=0;i<8;i++)
reg[i]=0;
labelcount=0;
printf("START\n");
printf("JMP main\n");
return 0;
}
int completeCodeGen()
{
printf("HALT\n");
return 0;
}
int getLabel()
{
return labelcount++;
}
int codegen(node *ptr)
{
int op=ptr->op,temp,reg1,reg2,reg3,label1,label2;
char buf[30];
symnode *symentry;
switch(op)
{
case CH_CONST:
reg1=getreg();
printf("MOV R%d, %d\n",reg1,ptr->value);
return reg1;
case CH_ADD:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("ADD R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_SUB:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("SUB R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_MUL:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("MUL R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_DIV:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("DIV R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_UMINUS:
reg2=codegen(ptr->left);
reg1=getreg();
printf("MOV R%d, 0\n",reg1);
printf("SUB R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_ADDR:
symentry=lookup(ptr->name,Lroot);
reg1=getreg();
if(symentry==NULL)
{
symentry=lookup(ptr->name,root);
printf("MOV R%d, %d\n",reg1,symentry->bind);
}
else
{
printf("MOV R%d, %d\n",reg1,symentry->bind);
reg2=getreg();
printf("MOV R%d, BP\n",reg2);
printf("ADD R%d, R%d\n",reg1,reg2);
if(symentry->atype==ATYPEADDR)
printf("MOV R%d, [R%d]\n",reg1,reg1);
freereg(reg2);
}
return reg1;
case CH_IDENT:
symentry=lookup(ptr->name,Lroot);
reg1=getreg();
/*printf("MOV R%d, %d\n",reg1,symentry->bind);
printf("MOV R%d, [R%d]\n",reg1,reg1);*/
if(symentry==NULL)
{
symentry=lookup(ptr->name,root);
printf("MOV R%d, [%d]\n",reg1,symentry->bind);
}
else
{
printf("MOV R%d, %d\n",reg1,symentry->bind);
reg2=getreg();
printf("MOV R%d, BP\n",reg2);
printf("ADD R%d, R%d\n",reg1,reg2);
printf("MOV R%d, [R%d]\n",reg1,reg1);
if(symentry->atype==ATYPEADDR)
printf("MOV R%d, [R%d]\n",reg1,reg1);
freereg(reg2);
}
return reg1;
case CH_IDENTARR:
symentry=lookup(ptr->name,root);
reg2=codegen(ptr->left);//expr in var[expr]
reg1=getreg();
printf("MOV R%d, %d\n",reg1,symentry->bind);
printf("ADD R%d, R%d\n",reg1,reg2);
printf("MOV R%d, [R%d]\n",reg1,reg1);
freereg(reg2);
return reg1;
case CH_IF:
reg1=codegen(ptr->left);
label1=getLabel();
printf("JZ R%d, L%d\n",reg1,label1);
freereg(reg1);
codegen(ptr->right);
printf("L%d:\n",label1);
return 0;
case CH_IFELSE:
reg1=codegen(ptr->left);
label1=getLabel();
label2=getLabel();
printf("JZ R%d, L%d\n",reg1,label1);
freereg(reg1);
codegen(ptr->right);
printf("JMP L%d\n",label2);
printf("L%d:\n",label1);
codegen(ptr->middle);
printf("L%d:\n",label2);
return 0;
case CH_STMNT:
codegen(ptr->left);
return codegen(ptr->right);
case CH_READ:
reg1=getreg();
printf("IN R%d\n",reg1);
return reg1;
case CH_WRITE:
reg1=codegen(ptr->left);
printf("OUT R%d\n",reg1);
freereg(reg1);
return 0;
case CH_ASSIGN:
symentry=lookup(ptr->name,Lroot);
reg3=codegen(ptr->right);
/*reg1=getreg();
printf("MOV R%d, %d\n",reg1,symentry->bind);
printf("MOV [R%d], R%d\n",reg1,reg2);*/
if(symentry==NULL)
{
symentry=lookup(ptr->name,root);
printf("MOV [%d], R%d\n",symentry->bind,reg3);
}
else
{
reg1=getreg();
reg2=getreg();
printf("MOV R%d, %d\n",reg1,symentry->bind);
printf("MOV R%d, BP\n",reg2);
printf("ADD R%d, R%d\n",reg1,reg2);
if(symentry->atype==ATYPEADDR)
printf("MOV R%d, [R%d]\n",reg1,reg1);
printf("MOV [R%d], R%d\n",reg1,reg3);
freereg(reg2);
freereg(reg1);
}
freereg(reg3);
//freereg(reg1);
return 0;
case CH_ASSIGNARR:
symentry=lookup(ptr->name,root);
reg2=codegen(ptr->left);//expr in var[expr]
reg1=getreg();
printf("MOV R%d, %d\n",reg1,symentry->bind);
printf("ADD R%d, R%d\n",reg1,reg2);
freereg(reg2);
reg2=codegen(ptr->right);
printf("MOV [R%d], R%d\n",reg1,reg2);
freereg(reg2);
freereg(reg1);
return 0;
case CH_RELOP:
switch(ptr->value)
{
case CH_GE:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("GE R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_LE:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("LE R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_EQ:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("EQ R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_NE:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("NE R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_GT:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("GT R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_LT:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("LT R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
default:
return 0;
}
case CH_LOGOP:
switch(ptr->value)
{
case CH_LOGAND:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("MUL R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
case CH_LOGOR:
reg1=codegen(ptr->left);
reg2=codegen(ptr->right);
printf("ADD R%d, R%d\n",reg1,reg2);
freereg(reg2);
return reg1;
default:
return 0;
}
case CH_WHILE:
label1=getLabel();
label2=getLabel();
printf("L%d:\n",label1);
reg1=codegen(ptr->left);
printf("JZ R%d, L%d\n",reg1,label2);
freereg(reg1);
codegen(ptr->right);
printf("JMP L%d\n",label1);
printf("L%d:\n",label2);
return 0;
case CH_MAINFUNC:
printf("main:\n");
reg1=getreg();
reg2=getreg();
//pushing space for global variable
printf("MOV R%d, %d\n",reg1,bind_base);
printf("MOV R%d, BP\n",reg2);
printf("ADD R%d, R%d\n",reg1,reg2);
printf("MOV SP, R%d\n",reg1);
//pushing space for global variables
printf("PUSH BP\n");
printf("MOV BP, SP\n");
printf("MOV R%d, %d\n",reg1,Lbind_base);
printf("MOV R%d, BP\n",reg2);
printf("ADD R%d, R%d\n",reg1,reg2);
printf("MOV SP, R%d\n",reg1);
freereg(reg1);
freereg(reg2);
codegen(ptr->left);
printf("MOV SP, BP\n");
printf("POP BP\n");
return 0;
case CH_FUNC:
printf("%s:\n",ptr->name);
reg1=getreg();
reg2=getreg();
printf("PUSH BP\n");
printf("MOV BP, SP\n");
printf("MOV R%d, %d\n",reg1,Lbind_base-1);
printf("MOV R%d, BP\n",reg2);
printf("ADD R%d, R%d\n",reg1,reg2);
printf("MOV SP, R%d\n",reg1);
freereg(reg1);
freereg(reg2);
codegen(ptr->left);
reg1=codegen(ptr->right);
reg2=getreg();
reg3=getreg();
printf("MOV R%d, BP\n",reg2);
printf("MOV R%d, -2\n",reg3);
printf("ADD R%d, R%d\n",reg2,reg3);
printf("MOV [R%d], R%d\n",reg2,reg1);
freereg(reg1);
freereg(reg2);
freereg(reg3);
printf("MOV SP, BP\n");
printf("POP BP\n");
printf("RET\n");
return 0;
case CH_FUNCCALL:
return generate_code_func_call(ptr->name,ptr->left);
default:
sprintf(buf,"Syntax error %d",op);
yyerror(buf);
}
}
int generate_code_func_call(char* name, node *actarg)
{
int reg1,regpost1,regpost2,i,regstatus[8],n_args=0,returnreg;
for(i=0;i<8;i++)
{
regstatus[i]=getregstatus(i);
if(regstatus[i])
printf("PUSH R%d\n",i);//pushing all the current register values to stack
}
returnreg=getreg();
regpost1=getreg();
regpost2=getreg();
resetreg();
while(actarg!=NULL)
{
reg1=codegen(actarg->left);
printf("PUSH R%d\n",reg1);//pushing the arguments
freereg(reg1);
actarg=actarg->right;
n_args++;
}
printf("PUSH R0\n");//space for return value
printf("CALL %s\n",name);//calling function
//post call
printf("POP R%d\n",returnreg);
printf("MOV R%d, -%d\n",regpost1,n_args);
printf("MOV R%d, SP\n",regpost2);
printf("ADD R%d, R%d\n",regpost1,regpost2);
printf("MOV SP, R%d\n",regpost1);
for(i=0;i<8;i++)
{
setregstatus(i,regstatus[i]);
if(regstatus[i])
printf("POP R%d\n",i);
}
setregstatus(returnreg,1);
return returnreg;
}
int freereg(int regnum)
{
reg[regnum]=0;
return 0;
}
int resetreg()
{
int i;
for(i=0;i<8;i++)
reg[i]=0;
}
int setregstatus(int regnum,int status)
{
reg[regnum]=status;
return 0;
}
int getregstatus(int regnum)
{
return reg[regnum];
}
int getreg()
{
int i;
for(i=0;i<8;i++)
{
if(reg[i]==0)
{
reg[i]=1;
return i;
}
}
return -1;
}