-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstruction.c
180 lines (172 loc) · 4.55 KB
/
instruction.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "instruction.h"
int NewRegister()
{
if (available_register > MAXRID) available_register = 1;
return available_register++;
}
int NewLabel()
{
if (available_label < 0) {
fprintf(stderr, "available_label[%d] error!\n", available_label);
exit(0);
}
return available_label++;
}
int NewOffset()
{
int tmp = available_offset;
if (tmp > DATAAREA_END) {
fprintf(stderr, "memoey is used up!\n");
exit(0);
}
available_offset += 4;
return tmp;
}
void emitcomment(char *comment, ...)
{
va_list ap;
va_start(ap, comment);
fprintf(outfile, "//");
vfprintf(outfile, comment, ap);
fprintf(outfile, "\n");
}
void emit(int label_index, Opcode opcode,
int op1, int op2, int op3)
{
char label[LABEL_LEN] = {0};
if (label_index > NONE)
sprintf(label, "L%d:", label_index);
switch (opcode) {
case NOP:
fprintf(outfile, "%s\t nop \n", label); break;
case ADDI:
fprintf(outfile, "%s\t addI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case ADD:
fprintf(outfile, "%s\t add \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case SUBI:
fprintf(outfile, "%s\t subI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case RSUBI:
fprintf(outfile, "%s\t rsubI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case SUB:
fprintf(outfile, "%s\t sub \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case MULTI:
fprintf(outfile, "%s\t multI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case MULT:
fprintf(outfile, "%s\t mult \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case DIVI:
fprintf(outfile, "%s\t divI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case RDIVI:
fprintf(outfile, "%s\t rdivI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case DIV:
fprintf(outfile, "%s\t div \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case ANDOP:
fprintf(outfile, "%s\t and \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case ANDI:
fprintf(outfile, "%s\t andI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case OROP:
fprintf(outfile, "%s\t or \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case ORI:
fprintf(outfile, "%s\t orI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case XOROP:
fprintf(outfile, "%s\t xor \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case XORI:
fprintf(outfile, "%s\t xorI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case LOAD:
fprintf(outfile, "%s\t load \t r%d \t => r%d \n", label, op1, op2); break;
case LOADI:
fprintf(outfile, "%s\t loadI \t %d \t => r%d \n", label, op1, op2); break;
case LOADAI:
fprintf(outfile, "%s\t loadAI \t r%d, %d \t => r%d \n",
label, op1, op2, op3);
break;
case LOADAO:
fprintf(outfile, "%s\t loadAO \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case STORE:
fprintf(outfile, "%s\t store \t r%d \t => r%d \n", label, op1, op2); break;
case STOREAI:
fprintf(outfile, "%s\t storeAI \t r%d \t => r%d, %d \n",
label, op1, op2, op3);
break;
case STOREAO:
fprintf(outfile, "%s\t storeAO \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case BR: /* br L1 */
fprintf(outfile, "%s\t br \t L%d \n", label, op1); break;
case CBR: /* cbr r1 => L1, L2 */
fprintf(outfile, "%s\t cbr \t r%d \t => L%d, L%d \n",
label, op1, op2, op3);
break;
case CMPLT: /* cmp_LT r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_LT \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case CMPLE: /* cmp_TE r1, r2 => r3 */
fprintf(outfile, "%s\t cmp_LE \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case CMPEQ:
fprintf(outfile, "%s\t cmp_EQ \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case CMPNE:
fprintf(outfile, "%s\t cmp_NE \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case CMPGE:
fprintf(outfile, "%s\t cmp_GE \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case CMPGT:
fprintf(outfile, "%s\t cmp_GT \t r%d, r%d \t => r%d \n",
label, op1, op2, op3);
break;
case OUTPUTAI:
fprintf(outfile, "%s\t outputAI \t r%d, %d \n", label, op1, op2);
break;
case I2I:
fprintf(outfile, "%s\t i2i \t r%d => r%d \n", label, op1, op2);
break;
default:
fprintf(stderr, "Invalid instruction:emit(%d, %d, %d, %d, %d)\n",
label_index, opcode, op1, op2, op3);
exit(0);
}
}