-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpcodeTable.h
113 lines (87 loc) · 2.65 KB
/
OpcodeTable.h
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
#ifndef __OPCODE_H__
#define __OPCODE_H__
using namespace std;
#include <iostream>
#include <string>
// Listing of all supported MIPS instructions
enum Opcode {
ADD,
ADDI,
SUB,
MULT,
MFHI,
SRL,
SRA,
SLTI,
LW,
J,
UNDEFINED
};
// Different types of MIPS encodings
enum InstType{
RTYPE,
ITYPE,
JTYPE
};
/* This class represents templates for supported MIPS instructions. For every supported
* MIPS instruction, the OpcodeTable includes information about the opcode, expected
* operands, and other fields.
*/
class OpcodeTable {
public:
// Initializes all the fields for every instruction in Opcode enum
OpcodeTable();
// Given a valid MIPS assembly mnemonic, returns an Opcode which represents a
// template for that instruction.
Opcode getOpcode(string str);
// Given an Opcode, returns number of expected operands.
int numOperands(Opcode o);
// Given an Opcode, returns the position of RS field. If field is not
// appropriate for this Opcode, returns -1.
int RSposition(Opcode o);
// Given an Opcode, returns the position of RT field. If field is not
// appropriate for this Opcode, returns -1.
int RTposition(Opcode o);
// Given an Opcode, returns the position of RD field. If field is not
// appropriate for this Opcode, returns -1.
int RDposition(Opcode o);
// Given an Opcode, returns the position of IMM field. If field is not
// appropriate for this Opcode, returns -1.
int IMMposition(Opcode o);
// Given an Opcode, returns true if instruction expects a label in the instruction.
// See "J".
bool isIMMLabel(Opcode o);
// Given an Opcode, returns instruction type.
InstType getInstType(Opcode o);
// Given an Opcode, returns a string representing the binary encoding of the opcode
// field.
string getOpcodeField(Opcode o);
// Given an Opcode, returns a string representing the binary encoding of the function
// field.
string getFunctField(Opcode o);
private:
// Provides information about how where to find values in a MIPS assembly
// instruction and what pre-defined fields (opcode/funct) will be in
// the encoding for the given instruction.
struct OpcodeTableEntry{
string name;
int numOps;
int rdPos;
int rsPos;
int rtPos;
int immPos;
bool immLabel;
InstType instType;
string op_field;
string funct_field;
// Creates an initial OpcodeTableEntry with default values
OpcodeTableEntry(){
numOps = 0;
rdPos = rsPos = rtPos = immPos = -1;
immLabel = false;
};
};
// The array of OpcodeTableEntries, one for each MIPS instruction supported
OpcodeTableEntry myArray[UNDEFINED];
};
#endif