-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpcodeTable.cpp
More file actions
257 lines (225 loc) · 6.25 KB
/
OpcodeTable.cpp
File metadata and controls
257 lines (225 loc) · 6.25 KB
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
//Name: Maxine Xin
#include "OpcodeTable.h"
OpcodeTable::OpcodeTable()
// Initializes all the fields for every instruction in Opcode enum
{
myArray[ADD].name = "add";
myArray[ADD].numOps = 3;
myArray[ADD].rdPos = 0;
myArray[ADD].rsPos = 1;
myArray[ADD].rtPos = 2;
myArray[ADD].immPos = -1;
myArray[ADD].instType = RTYPE;
myArray[ADD].op_field = "000000";
myArray[ADD].funct_field = "100000";
myArray[ADDI].name = "addi";
myArray[ADDI].numOps = 3;
myArray[ADDI].rdPos = -1;
myArray[ADDI].rsPos = 1;
myArray[ADDI].rtPos = 0;
myArray[ADDI].immPos = 2;
myArray[ADDI].instType = ITYPE;
myArray[ADDI].op_field = "001000";
myArray[ADDI].funct_field = "";
myArray[XOR].name = "xor";
myArray[XOR].numOps = 3;
myArray[XOR].rdPos = 0;
myArray[XOR].rsPos = 1;
myArray[XOR].rtPos = 2;
myArray[XOR].immPos = -1;
myArray[XOR].instType = RTYPE;
myArray[XOR].op_field = "000000";
myArray[XOR].funct_field = "100110";
myArray[MULT].name = "mult";
myArray[MULT].numOps = 2;
myArray[MULT].rdPos = -1;
myArray[MULT].rsPos = 0;
myArray[MULT].rtPos = 1;
myArray[MULT].immPos = -1;
myArray[MULT].instType = RTYPE;
myArray[MULT].op_field = "000000";
myArray[MULT].funct_field = "011000";
myArray[MFLO].name = "mflo";
myArray[MFLO].numOps = 1;
myArray[MFLO].rdPos = 0;
myArray[MFLO].rsPos = -1;
myArray[MFLO].rtPos = -1;
myArray[MFLO].immPos = -1;
myArray[MFLO].instType = RTYPE;
myArray[MFLO].op_field = "000000";
myArray[MFLO].funct_field = "010010";
myArray[SLL].name = "sll";
myArray[SLL].numOps = 3;
myArray[SLL].rdPos = 0;
myArray[SLL].rsPos = -1;
myArray[SLL].rtPos = 1;
myArray[SLL].immPos = 2;
myArray[SLL].instType = RTYPE;
myArray[SLL].op_field = "000000";
myArray[SLL].funct_field = "000000";
myArray[SLT].name = "slt";
myArray[SLT].numOps = 3;
myArray[SLT].rdPos = 0;
myArray[SLT].rsPos = 1;
myArray[SLT].rtPos = 2;
myArray[SLT].immPos = -1;
myArray[SLT].instType = RTYPE;
myArray[SLT].op_field = "000000";
myArray[SLT].funct_field = "101010";
myArray[SLTI].name = "slti";
myArray[SLTI].numOps = 3;
myArray[SLTI].rdPos = -1;
myArray[SLTI].rsPos = 1;
myArray[SLTI].rtPos = 0;
myArray[SLTI].immPos = 2;
myArray[SLTI].instType = ITYPE;
myArray[SLTI].op_field = "001010";
myArray[SLTI].funct_field = "";
myArray[LB].name = "lb";
myArray[LB].numOps = 3;
myArray[LB].rdPos = -1;
myArray[LB].rsPos = 2;
myArray[LB].rtPos = 0;
myArray[LB].immPos = 1;
myArray[LB].instType = ITYPE;
myArray[LB].op_field = "100000";
myArray[LB].funct_field = "";
myArray[LB].immMemory = true;
/*
myArray[BEQ].name = "beq";
myArray[BEQ].numOps = 3;
myArray[BEQ].rdPos = -1;
myArray[BEQ].rsPos = 0;
myArray[BEQ].rtPos = 1;
myArray[BEQ].immPos = 2;
myArray[BEQ].instType = ITYPE;
myArray[BEQ].op_field = "000100";
myArray[BEQ].funct_field = "";
myArray[BEQ].immLabel = true;
*/
myArray[J].name = "j";
myArray[J].numOps = 1;
myArray[J].rdPos = -1;
myArray[J].rsPos = -1;
myArray[J].rtPos = -1;
myArray[J].immPos = 0;
myArray[J].instType = JTYPE;
myArray[J].op_field = "000010";
myArray[J].funct_field = "000000";
myArray[J].immLabel = true;
}
Opcode OpcodeTable::getOpcode(string str)
// Given a valid MIPS assembly mnemonic, returns an Opcode which represents a
// template for that instruction.
{
for(int i = 0; i < (int)UNDEFINED; i++){
if(myArray[i].name == str){
return (Opcode)i;
}
}
return UNDEFINED;
}
Opcode OpcodeTable::getOpcode(string opStr, string funcStr)
// Given string of opcode field &/ func field,
// returns an Opcode which represents a template for
// the corresponding instruction
{
for (int i = 0; i < (int)UNDEFINED; i++)
{
if (myArray[i].op_field == opStr) {
if (myArray[i].instType == RTYPE) {
if (myArray[i].funct_field == funcStr)
return (Opcode)i;
} else {
// not RTYPE
return (Opcode)i;
}
}
}
return UNDEFINED;
}
int OpcodeTable::numOperands(Opcode o)
// Given an Opcode, returns number of expected operands.
{
if(o < 0 || o >= UNDEFINED)
return -1;
return myArray[o].numOps;
}
int OpcodeTable::RSposition(Opcode o)
// Given an Opcode, returns the position of RS field. If field is not
// appropriate for this Opcode, returns -1.
{
if(o < 0 || o >= UNDEFINED)
return -1;
return myArray[o].rsPos;
}
int OpcodeTable::RTposition(Opcode o)
// Given an Opcode, returns the position of RT field. If field is not
// appropriate for this Opcode, returns -1.
{
if(o < 0 || o >= UNDEFINED)
return -1;
return myArray[o].rtPos;
}
int OpcodeTable::RDposition(Opcode o)
// Given an Opcode, returns the position of RD field. If field is not
// appropriate for this Opcode, returns -1.
{
if(o < 0 || o >= UNDEFINED)
return -1;
return myArray[o].rdPos;
}
int OpcodeTable::IMMposition(Opcode o)
// Given an Opcode, returns the position of IMM field. If field is not
// appropriate for this Opcode, returns -1.
{
if(o < 0 || o >= UNDEFINED)
return -1;
return myArray[o].immPos;
}
InstType OpcodeTable::getInstType(Opcode o)
// Given an Opcode, returns instruction type.
{
if(o < 0 || o > UNDEFINED)
return (InstType)-1;
return myArray[o].instType;
}
string OpcodeTable::getOpcodeField(Opcode o)
// Given an Opcode, returns a string representing the binary encoding of the opcode
// field.
{
if(o < 0 || o > UNDEFINED)
return string("");
return myArray[o].op_field;
}
string OpcodeTable::getFunctField(Opcode o)
// Given an Opcode, returns a string representing the binary encoding of the function
// field.
{
if(o < 0 || o > UNDEFINED)
return string("");
return myArray[o].funct_field;
}
bool OpcodeTable::isIMMLabel(Opcode o)
// Given an Opcode, returns true if instruction expects a label in the instruction.
// See "J".
{
if(o < 0 || o > UNDEFINED)
return false;
return myArray[o].immLabel;
}
bool OpcodeTable::isIMMMemory(Opcode o)
// Given an Opcode, returns true is instruction expects an offset in the instruction.
// See "LB".
{
if(o < 0 || o > UNDEFINED)
return false;
return myArray[o].immMemory;
}
string OpcodeTable::getInstName(Opcode o)
// Given an Opcode, returns a string representing the name of the instruction
{
if(o < 0 || o > UNDEFINED)
return "";
return myArray[o].name;
}