-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_mips.c
155 lines (138 loc) · 4.54 KB
/
print_mips.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
#include <stdio.h>
#include "parser.h"
#include "tac.h"
#include <stdlib.h>
#include <string.h>
char* get_address_content(Address* addr) {
if(addr == NULL)
return " ";
else if(addr->operator==Var)
return addr->content.var;
else { //itoa is not standard and shouldnt be used
char* tmp = malloc(sizeof(char)*10);
sprintf(tmp, "%d", addr->content.value);
return tmp;
}
}
void compilers_assemble(ListCL* list, char** vars){
char *str1, *str2, *str3;
//head
printf(".data\n");
int i = 0;
while(vars[i]!=NULL) {
printf(" %s\n",vars[i]);
i++;
}
printf(".text\n");
//body
Codline* line = NULL;
while(list != NULL) {
line = list->line;
str1 = get_address_content(line->addr1);
str2 = get_address_content(line->addr2);
str3 = get_address_content(line->addr3);
switch(line->op){
case TAC_SUM:
printf(" add %s, %s, %s\n", str1, str2, str3);
break;
case TAC_SUB:
printf(" sub %s, %s, %s\n", str1, str2, str3);
break;
case TAC_DIV:
printf(" div %s, %s\n", str2, str3);
printf(" mflo %s\n", str1);
break;
case TAC_MUL:
printf(" mult %s, %s\n", str2, str3);
printf(" mflo %s\n", str1);
break;
case TAC_MODUL:
printf(" div %s, %s\n", str2, str3);
printf(" mfhi %s\n", str1);
break;
case TAC_GT:
printf(" sgt %s, %s, %s\n", str1, str2, str3);
break;
case TAC_LT:
printf(" slt %s, %s, %s\n", str1, str2, str3);
break;
case TAC_GET:
printf(" sge %s, %s, %s\n", str1, str2, str3);
break;
case TAC_LET:
printf(" sle %s, %s, %s\n", str1, str2, str3);
break;
case TAC_DIF:
printf(" sne %s, %s, %s\n", str1, str2, str3);
break;
case TAC_COMP:
printf(" seq %s, %s, %s\n", str1, str2, str3);
break;
case TAC_AND:
printf(" and %s, %s, %s\n", str1, str2, str3);
break;
case TAC_OR:
printf(" or %s, %s, %s\n", str1, str2, str3);
break;
case TAC_NOT:
printf(" nor %s, %s, %s\n", str1, str1, str1);
break;
case TAC_ATRIB:
printf(" add %s, %s, $0\n", str1, str2);
break;
case TAC_SPRINT:
printf(" li $v0, 1\n");
printf(" li $a0, %s\n", str1);
printf(" syscall\n");
break;
case TAC_SSCAN:
printf(" li $v0, 5\n");
printf(" syscall\n");
printf(" move %s, $v0\n", str2);
printf(" sw %s, %s\n", str2, str1);
break;
case TAC_IF:
printf(" beq %s, $0, %s\n", str1, str2);
break;
case TAC_GOTO:
printf(" j %s\n", str1);
break;
case TAC_LABEL:
printf("%s:\n", str1);
break;
case TAC_SW:
printf(" sw %s, %s\n", str1, str2);
break;
case TAC_LI:
printf(" li %s, %s\n", str1, str2);
break;
case TAC_LW:
printf(" lw %s, %s\n", str1, str2);
break;
default:
yyerror("Invalid operand type received while printing mips.\n");
}
list = list->next;
}
//tail
printf("Exit:\n");
printf(" li $v0, 10\n");
printf(" syscall\n");
}
int main(int argc, char** argv) {
ListCL* list = NULL;
char** vars = (char**)malloc( 512*sizeof(char*));
--argc; ++argv;
if (argc != 0) {
yyin = fopen(*argv, "r");
if (!yyin) {
printf("'%s': could not open file\n", *argv);
return 1;
}
if (yyparse() == 0) {
list = compile_tac(root, vars);
}
compilers_assemble(list, vars);
}
return 0;
}