-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.cpp
237 lines (224 loc) · 10.1 KB
/
decode.cpp
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
#include "decode.h"
Decode::Decode(
deque<Instruction> & fInstructionQueue,
unordered_map<int, pair<int, int>> & btb,
deque<Instruction> & dInstructionQueue,
unordered_map<string, string> & mappingTable,
deque<string> & freeList,
deque<unordered_map<string, string>> & mappingTableHistory,
deque<deque<string>> & freeListHistory,
unordered_map<string, int> & branchLabelsTable,
BranchPredictor * dbp,
const int nf,
const int ni,
const bool debugMode
) :
fInstructionQueue(fInstructionQueue),
btb(btb),
dInstructionQueue(dInstructionQueue),
mappingTable(mappingTable),
freeList(freeList),
mappingTableHistory(mappingTableHistory),
freeListHistory(freeListHistory),
branchLabelsTable(branchLabelsTable),
dbp(dbp),
nf(nf),
ni(ni),
debugMode(debugMode)
{
this->stageType = StageType::DECODE;
if (debugMode) {
printStageType();
cout << "\nNF in Decode stage = " << nf << "\n"
<< "\nNI in Decode stage = " << ni << "\n";
}
};
string Decode::performRegisterRenaming(string inpRegName, bool isDestinationReg) {
string newRegName = inpRegName;
bool inpContainedOffset = containsOffset(newRegName);
// if there exists an offset, remove from string
if (inpContainedOffset) newRegName = trimOffset(newRegName);
// handle zero register
if (newRegName == ZERO_REGISTER_NAME) return ZERO_REGISTER_NAME;
// no available free registers, return unique failure code
if (freeList.empty()) return FREE_LIST_EMPTY_CODE;
if (mappingTable.count(newRegName)) {
// mappingTable contains key for inpRegName
if (!isDestinationReg) return mappingTable[newRegName];
}
// get free reg for new reg, insert into mappingTable
string freeReg = freeList.front();
freeList.pop_front();
mappingTable[newRegName] = freeReg;
return freeReg;
}
bool Decode::dispatch() {
cout << "\nd_dispatch called (nf=" << nf << " ni=" << ni << ")=\n";
// throughput syncronized with fetch stage i.e. ni param
for (int i = 0; i < nf; i++) {
if (fInstructionQueue.empty() || dInstructionQueue.size() >= ni) {
// either no instructions are available from fetch stage,
// or dInstructionQueue is full. stall!
if (dInstructionQueue.size() >= ni) return false;
else return true; // don't count empty fetch queue as a stall
} else {
Instruction iInstr = this->fInstructionQueue.front();
// check if original reg name contains an offset
int immediateOffset = 0;
bool offsetExists = containsOffset(iInstr.rs);
if (offsetExists) immediateOffset = getOffset(iInstr.rs);
string regName;
switch(iInstr.opcode) {
// if ever regName == free list empty code,
// there are no available physical regs. stall!
case InstructionType::FLD:
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::FSD:
// rt
regName = performRegisterRenaming(iInstr.rt, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rt = regName;
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
break;
case InstructionType::ADD: // rd, rs, rt
// rt
regName = performRegisterRenaming(iInstr.rt, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rt = regName;
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::ADDI: // rs, rd
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::SLT: // rd, rs, rt
// rt
regName = performRegisterRenaming(iInstr.rt, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rt = regName;
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::FADD: // rd, rs, rt
// rt
regName = performRegisterRenaming(iInstr.rt, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rt = regName;
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::FSUB: // rd, rs, rt
// rt
regName = performRegisterRenaming(iInstr.rt, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rt = regName;
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::FMUL: // rd, rs, rt
// rt
regName = performRegisterRenaming(iInstr.rt, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rt = regName;
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::FDIV: // rd, rs, rt
// rt
regName = performRegisterRenaming(iInstr.rt, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rt = regName;
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, true);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
case InstructionType::BNE:
// rs
regName = performRegisterRenaming(iInstr.rs, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rs = regName;
// rd
regName = performRegisterRenaming(iInstr.rd, false);
if (regName == FREE_LIST_EMPTY_CODE) return false;
iInstr.rd = regName;
break;
default:
// instruction type not recognized
return false;
}
// if original reg name contained offset, add into immediate
if (offsetExists) iInstr.immediate += immediateOffset;
// add current instruction with renamed reg to dInstructionQueue
fInstructionQueue.pop_front();
dInstructionQueue.push_back(iInstr);
// btb insertion
if (iInstr.opcode == InstructionType::BNE) {
// store a historic copy of the current mapping table and free list
mappingTableHistory.push_back(mappingTable);
freeListHistory.push_back(freeList);
if (BTB_ENTRIES_COUNT)
if (btb.count(iInstr.address) == 0) {
// current instruction is not contained in btb
btb[iInstr.address] = {
branchLabelsTable[iInstr.rt],
dbp->getBranchPrediction()
};
}
}
}
}
return true;
}