-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmips_code.c
132 lines (127 loc) · 5.26 KB
/
mips_code.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
#include "parser.h"
#include "code.h"
#include "mips_code.h"
#include <stdio.h> // for printf
// Translation of intermediate code to MIPS (prints the mips program)
void printMIPS(InstrList* intermCode)
{
Instr* instr = intermCode->first;
// Begining of data section
printf(".data\n");
for(int i = 0; i < HASH_SIZE; i++)
{
if(variables->array[i] != NULL)
{
LIST_HT cur = variables->array[i];
while(cur != NULL)
{
printf("\t%s: .word 0\n", cur->variable);
cur = cur->next;
}
}
}
printf("\tnewline: .asciiz \"\\n\"\n");
// End of data section
// Begining of text section
printf(".text\n");
printf("main:\n");
while(instr)
{
switch(instr->op)
{
case I_ATRIB:
printf("\tli %s, %d\n", instr->el1->u.name, instr->el2->u.value);
break;
case I_PLUS:
printf("\tadd %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_ADDI:
printf("\taddi %s, %s, %d\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.value);
break;
case I_MINUS:
printf("\tsub %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_SUBI:
printf("\tsubi %s, %s, %d\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.value);
break;
case I_DIV:
printf("\tdiv %s, %s\n", instr->el2->u.name, instr->el3->u.name);
printf("\tmflo %s\n", instr->el1->u.name);
break;
case I_MULT:
printf("\tmul %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_MOD:
printf("\tdiv %s, %s\n", instr->el2->u.name, instr->el3->u.name);
printf("\tmfhi %s\n", instr->el1->u.name);
break;
case I_NEG:
printf("\tneg %s, %s\n", instr->el1->u.name, instr->el2->u.name);
break;
case I_LABEL:
printf("%s:\n", instr->el1->u.name);
break;
case I_GOTO:
printf("\tj %s\n", instr->el1->u.name);
break;
case I_IF:
printf("\tbne %s, $zero, %s\n", instr->el1->u.name, instr->el2->u.name);
printf("\tj %s\n", instr->el3->u.name);
break;
case I_BEQ:
printf("\tseq %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_BNE:
printf("\tsne %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_BGT:
printf("\tsgt %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_BGE:
printf("\tsge %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_BLT:
printf("\tslt %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_BLE:
printf("\tsle %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_OR:
printf("\tor %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_ORI:
printf("\tori %s, %s, %d\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.value);
break;
case I_AND:
printf("\tand %s, %s, %s\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.name);
break;
case I_ANDI:
printf("\tandi %s, %s, %d\n", instr->el1->u.name, instr->el2->u.name, instr->el3->u.value);
break;
case I_NOT:
printf("\tseq %s, %s, $zero\n", instr->el1->u.name, instr->el2->u.name);
break;
case I_PRINT:
printf("\tli $v0, 1\n");
printf("\tlw $a0, %s\n", instr->el1->u.name);
printf("\tsyscall\n");
printf("\tli $v0, 4\n");
printf("\tla $a0, newline\n");
printf("\tsyscall\n");
break;
case I_READ:
printf("\tli $v0, 5\n");
printf("\tsyscall\n");
printf("\tsw $v0, %s\n", instr->el1->u.name);
break;
case I_LOAD:
printf("\tlw %s, %s\n", instr->el1->u.name, instr->el2->u.name);
break;
case I_STORE:
printf("\tsw %s, %s\n", instr->el1->u.name, instr->el2->u.name);
break;
}
instr = instr->next;
}
// End of text section
}