-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpcodeTable.h
More file actions
126 lines (101 loc) · 3.16 KB
/
OpcodeTable.h
File metadata and controls
126 lines (101 loc) · 3.16 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
//Name: Maxine Xin
#ifndef __OPCODE_H__
#define __OPCODE_H__
#include <iostream>
#include <string>
using namespace std;
// Listing of all supported MIPS instructions
enum Opcode {
ADD,
ADDI,
XOR,
MULT,
MFLO,
SLL,
SLT,
SLTI,
LB,
//BEQ,
J,
UNDEFINED
};
// Different types of MIPS encodings
enum InstType{
RTYPE,
ITYPE,
JTYPE,
INVALID
};
/* 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 string of opcode field &/ func field,
// returns an Opcode which represents a template for
// the corresponding instruction
Opcode getOpcode(string opStr, string funcStr);
// 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 true is instruction expects an memory adress in the instruction.
// See "LB".
bool isIMMMemory(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);
// Given an Opcode, returns a string representing the name of the instruction
string getInstName(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;
bool immMemory;
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;
immMemory = false;
};
};
// The array of OpcodeTableEntries, one for each MIPS instruction supported
OpcodeTableEntry myArray[UNDEFINED];
};
#endif