-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintFuncs.c
495 lines (481 loc) · 13.9 KB
/
printFuncs.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
#include <stdio.h>
#include <string.h>
#include "stack.h"
#include "parser.h"
#include "ast.h"
char * makeLabel();
char * convert_int_string();
void printInstr(Instr* instr);
void printInstrList(InstrList* instrlist);
InstrList* compileExpr(Expr* expr);
InstrList* compileCmd(Cmd* cmd);
InstrList* compileCmdList(CmdList* cmdlist);
void writeInstrList(InstrList* instrlist);
void writeInstr(Instr* instr);
int label_num = 1;
varList* data;
char * convert_int_string(){
char * str = malloc(sizeof(char)*64);
sprintf(str,"%d",label_num);
return str;
}
char * makeLabel(){
char * str = malloc(sizeof(char)*64);
char * str2 = convert_int_string(label_num);
strcpy(str, "Label_");
strcat(str,str2);
free(str2);
label_num++;
return str;
}
void printInstr(Instr* instr){
switch(instr->kind){
case IAND:
printf("AND ");
break;
case LDC:
printf("LDC ");
break;
case ADI:
printf("ADI ");
break;
case SBI:
printf("SBI ");
break;
case DVI:
printf("DVI ");
break;
case MPI:
printf("MPI ");
break;
case MOD:
printf("MOD ");
break;
case IOR:
printf("IOR ");
break;
case LOD:
printf("LOD ");
break;
case STO:
printf("STO ");
break;
case RDI:
printf("RDI ");
break;
case FJP:
printf("FJP ");
break;
case GRT:
printf("GRT ");
break;
case LES:
printf("LES ");
break;
case WRI:
printf("WRI ");
break;
case UJP:
printf("UJP ");
break;
case LABEL:
printf("LABEL ");
break;
case EQU:
printf("EQU ");
break;
case NEQ:
printf("NEQ ");
break;
case GEQ:
printf("GEQ ");
break;
case LEQ:
printf("LEQ ");
break;
default:
printf("Error, instruction not recognised\n");
break;
}
switch(instr->arg_kind){
case var:
printf("%s",instr->attr.id);
break;
case val:
printf("%d",instr->attr.arg);
break;
case none:
break;
default:
printf("Error, instruction not recognised\n");
break;
}
printf("\n");
}
void printInstrList(InstrList* instrlist){
while(instrlist!=NULL){
printInstr(instrlist->code);
instrlist = instrlist->next;
}
}
InstrList* compileExpr(Expr* expr){
InstrList* l1;
InstrList* l2;
if(expr->kind == E_INTEGER)
return stack_InstrList(stack_Instr1(LDC,expr->attr.value),NULL);
else if(expr->kind == E_ID)
return stack_InstrList(stack_Instr2(LOD,expr->attr.id),NULL);
else{
switch(expr->attr.op.operator){
case PLUS:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(ADI),NULL));
return l2;
case LESS:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(SBI),NULL));
return l2;
case MULT:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(MPI),NULL));
return l2;
case DIV:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(DVI),NULL));
return l2;
case REST:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(MOD),NULL));
return l2;
case AND:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(IAND),NULL));
return l2;
case OR:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(IOR),NULL));
return l2;
case EQUAL:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(EQU),NULL));
return l2;
case NOTEQUAL:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(NEQ),NULL));
return l2;
case GREATER_OR_EQUAL:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(GEQ),NULL));
return l2;
case LESSER_OR_EQUAL:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(LEQ),NULL));
return l2;
case GREATER:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(GRT),NULL));
return l2;
case LESSER:
l1 = append(compileExpr(expr->attr.op.left),compileExpr(expr->attr.op.right));
l2 = append(l1,stack_InstrList(stack_Instr3(LES),NULL));
return l2;
}
}
}
InstrList* compileCmd(Cmd* cmd){
InstrList* l1;
if(cmd->kind == C_ASG){
if(varListContains(cmd->attr._asg.id,data)==0){
data = append2(data,stack_varList(cmd->attr._asg.id,NULL));
}
l1 = compileExpr(cmd->attr._asg.expr);
l1 = append(l1,stack_InstrList(stack_Instr2(STO,cmd->attr._asg.id),NULL));
return l1;
}
if(cmd->kind == C_IF){
l1 = compileExpr(cmd->attr._if.expr);
char * label = makeLabel();
l1 = append(l1,stack_InstrList(stack_Instr2(FJP,label),NULL));
l1 = append(l1,compileCmdList(cmd->attr._if.cmds));
l1 = append(l1,stack_InstrList(stack_Instr2(LABEL,label),NULL));
return l1;
}
if(cmd->kind == C_IF_ELSE){
l1 = compileExpr(cmd->attr._if_else.expr);
char * label = makeLabel();
char * label2 = makeLabel();
l1 = append(l1,stack_InstrList(stack_Instr2(FJP,label),NULL));
l1 = append(l1,compileCmdList(cmd->attr._if_else.if_part));
l1 = append(l1,stack_InstrList(stack_Instr2(UJP,label2),NULL));
l1 = append(l1,stack_InstrList(stack_Instr2(LABEL,label),NULL));
l1 = append(l1,compileCmdList(cmd->attr._if_else.else_part));
l1 = append(l1,stack_InstrList(stack_Instr2(LABEL,label2),NULL));
return l1;
}
if(cmd->kind == C_WHILE){
char * label2 = makeLabel();
char * label = makeLabel();
l1 = stack_InstrList(stack_Instr2(LABEL,label2),NULL);
l1 = append(l1,compileExpr(cmd->attr._while.expr));
l1 = append(l1,stack_InstrList(stack_Instr2(FJP,label),NULL));
l1 = append(l1,compileCmdList(cmd->attr._while.cmds));
l1 = append(l1,stack_InstrList(stack_Instr2(UJP,label2),NULL));
l1 = append(l1,stack_InstrList(stack_Instr2(LABEL,label),NULL));
return l1;
}
if(cmd->kind == C_PRINT){
l1 = stack_InstrList(stack_Instr2(WRI,cmd->attr._print.id),NULL);
return l1;
}
if(cmd->kind == C_SCAN){
l1 = stack_InstrList(stack_Instr2(RDI,cmd->attr._scan.id),NULL);
return l1;
}
}
InstrList* compileCmdList(CmdList* cmdlist){
InstrList* l1;
l1 = compileCmd(cmdlist->cmd);
while(cmdlist->next != NULL){
cmdlist = cmdlist->next;
l1 = append(l1,compileCmd(cmdlist->cmd));
}
return l1;
}
void writeInstrList(InstrList* instrlist){
while(instrlist!=NULL){
writeInstr(instrlist->code);
instrlist = instrlist->next;
}
}
void writeInstr(Instr* instr){
FILE *fp;
fp = fopen("temp.txt", "a");
if(fp==NULL){
perror("Error opening file.");
}
else{
switch(instr->kind){
case LDC:
fprintf(fp, "li $a0,%d\n",instr->attr.arg);
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case ADI:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "add $a0, $a0, $t1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case SBI:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "sub $a0, $a0, $t1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case DVI:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "div $a0, $t1\n");
fprintf(fp, "mflo $a0\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case MPI:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "mult $a0, $t1\n");
fprintf(fp, "mflo $a0\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case MOD:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "div $a0, $t1\n");
fprintf(fp, "mfhi $a0\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case IAND:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "and $a0, $a0, $t1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case IOR:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "or $a0, $a0, $t1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case LOD:
fprintf(fp, "lw $a0, %s\n",instr->attr.id);
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case STO:
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "sw $a0, %s\n",instr->attr.id);
break;
case RDI:
fprintf(fp, "li $v0, 5\n");
fprintf(fp, "syscall\n");
fprintf(fp, "sw $v0, %s\n",instr->attr.id);
break;
case WRI:
fprintf(fp, "lw $a0, %s\n",instr->attr.id);
fprintf(fp, "li $v0, 1\n");
fprintf(fp, "syscall\n");
break;
case GRT:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "slt $a0, $t1, $a0\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case LES:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "slt $a0, $a0, $t1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case UJP:
fprintf(fp, "j %s\n",instr->attr.id);
break;
case FJP:
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "beq $a0, $zero, %s\n",instr->attr.id);
break;
case LABEL:
fprintf(fp, "%s:\n",instr->attr.id);
break;
case EQU:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "subu $a0, $a0, $t1\n");
fprintf(fp, "sltu $a0, $zero, $a0\n");
fprintf(fp, "xori $a0, $a0, 1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case NEQ:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $a0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "subu $a0, $a0, $t1\n");
fprintf(fp, "sltu $a0, $zero, $a0\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case GEQ:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $t0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "slt $a0, $t1, $t0\n");
fprintf(fp, "subu $a1, $t0, $t1\n");
fprintf(fp, "sltu $a1, $zero, $a1\n");
fprintf(fp, "xori $a1, $a1, 1\n");
fprintf(fp, "or $a0, $a0, $a1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
case LEQ:
fprintf(fp, "lw $t1, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "lw $t0, 0($sp)\n");
fprintf(fp, "addiu $sp, $sp, 4\n");
fprintf(fp, "slt $a0, $t0, $t1\n");
fprintf(fp, "subu $a1, $t0, $t1\n");
fprintf(fp, "sltu $a1, $zero, $a1\n");
fprintf(fp, "xori $a1, $a1, 1\n");
fprintf(fp, "or $a0, $a0, $a1\n");
fprintf(fp, "addiu $sp, $sp, -4\n");
fprintf(fp, "sw $a0, 0($sp)\n");
break;
default:
printf("Error, instruction not recognised\n");
break;
}
}
fclose(fp);
}
void writeMIPS(){
char c;
FILE *fp;
FILE *fp2;
remove("mips.txt");
fp = fopen("temp.txt", "r");
fp2 = fopen("mips.txt", "a");
if(fp != NULL && fp2 != NULL){
fprintf(fp2,".data\n");
data = data->next;
while(data!=NULL){
fprintf(fp2, "%s: .word 4\n",data->var);
data = data->next;
}
fprintf(fp2,"\n");
fprintf(fp2,".text\n");
c = fgetc(fp);
while (c != EOF){
fputc(c, fp2);
c = fgetc(fp);
}
fprintf(fp2,"li $v0, 10\n");
fprintf(fp2,"syscall\n");
fclose(fp);
fclose(fp2);
remove("temp.txt");
}
else{
perror("Error opening file.");
}
}
int main(int argc, char** argv) {
--argc; ++argv;
if (argc != 0) {
yyin = fopen(*argv, "r");
if (!yyin) {
printf("'%s': could not open file\n", *argv);
return 1;
}
} // yyin = stdin
if (yyparse() == 0) { //parsing was successfull
data = stack_varList("",NULL);
while(root!=NULL){
writeInstrList(compileCmd(root->cmd));
root = root->next;
}
}
writeMIPS();
return 0;
}