-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrutil.c
150 lines (136 loc) · 4.02 KB
/
instrutil.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
/**********************************************
CS515 Compiler
Project 1
Spring 2016
Author: Ulrich Kremer
**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include "instrutil.h"
static int next_register = 1;
static int next_label = 0;
static int next_offset = 0;
int NextRegister()
{
return next_register++;
}
int NextLabel()
{
return next_label++;
}
int NextOffset(int units)
{
int current_offset = next_offset;
next_offset = next_offset + 4*units;
return current_offset;
}
void
emitComment(char *comment)
{
fprintf(outfile, "\t// %s\n", comment);
}
void
emit(int label_index,
Opcode_Name opcode,
int field1,
int field2,
int field3)
{
char *label = " ";
if (label_index < NOLABEL) {
printf("ERROR: \"%d\" is an illegal label index.\n", label_index);
return;
}
if (label_index > NOLABEL) {
label = (char *) malloc(100);
sprintf(label, "L%d:", label_index);
}
switch (opcode) {
case NOP:
fprintf(outfile, "%s\t nop \n", label);
break;
case ADDI:
/* Example: addI r1, 1024 => r1 */
fprintf(outfile, "%s\t addI r%d, %d \t=> r%d \n", label, field1, field2, field3);
break;
case ADD:
fprintf(outfile, "%s\t add r%d, r%d \t=> r%d \n", label, field1, field2, field3);
break;
case SUBI:
/* Example: subI r1, 1024 => r1 */
fprintf(outfile, "%s\t subI r%d, %d \t=> r%d \n", label, field1, field2, field3);
break;
case SUB:
fprintf(outfile, "%s\t sub r%d, r%d \t=> r%d \n", label, field1, field2, field3);
break;
case MULT:
fprintf(outfile, "%s\t mult r%d, r%d \t=> r%d \n", label, field1, field2, field3);
break;
case LOAD:
/* Example: load r1 => r1 */
fprintf(outfile, "%s\t load r%d \t=> r%d \n", label, field1, field2);
break;
case LOADI:
/* Example: loadI 1024 => r1 */
fprintf(outfile, "%s\t loadI %d \t=> r%d \n", label, field1, field2);
break;
case LOADAI:
/* Example: loadAI r1, 16 => r3 */
fprintf(outfile, "%s\t loadAI r%d, %d \t=> r%d \n", label, field1, field2, field3);
break;
case LOADAO:
/* Example: loadAO r1, r2 => r3 */
fprintf(outfile, "%s\t loadAO r%d, r%d \t=> r%d \n", label, field1, field2, field3);
break;
case STORE:
/* Example: store r1 => r2 */
fprintf(outfile, "%s\t store r%d \t=> r%d \n", label, field1, field2);
break;
case STOREAI:
/* Example: storeAI r1 => r2, 16 */
fprintf(outfile, "%s\t storeAI r%d \t=> r%d, %d \n", label, field1, field2, field3);
break;
case STOREAO:
/* Example: storeAO r1 => r2, r3 */
fprintf(outfile, "%s\t storeAO r%d \t=> r%d, r%d \n", label, field1, field2, field3);
break;
case BR:
/* Example: br L1 */
fprintf(outfile, "%s\t br L%d\n", label, field1);
break;
case CBR:
/* Example: cbr r1 => L1, L2 */
fprintf(outfile, "%s\t cbr r%d \t=> L%d, L%d\n", label, field1, field2, field3);
break;
case CMPLT:
/* Example: cmp_LT r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_LT r%d, r%d \t=> r%d\n", label, field1, field2, field3);
break;
case CMPLE:
/* Example: cmp_LE r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_LE r%d, r%d \t=> r%d\n", label, field1, field2, field3);
break;
case CMPGT:
/* Example: cmp_GT r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_GT r%d, r%d \t=> r%d\n", label, field1, field2, field3);
break;
case CMPGE:
/* Example: cmp_GE r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_GE r%d, r%d \t=> r%d\n", label, field1, field2, field3);
break;
case CMPEQ:
/* Example: cmp_EQ r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_EQ r%d, r%d \t=> r%d\n", label, field1, field2, field3);
break;
case CMPNE:
/* Example: cmp_NE r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_NE r%d, r%d \t=> r%d\n", label, field1, field2, field3);
break;
case OUTPUT:
/* Example: output 1024 */
fprintf(outfile, "%s\t output %d\n", label, field1);
break;
default:
fprintf(stderr, "Illegal instruction in \"emit\" \n");
}
}