-
Notifications
You must be signed in to change notification settings - Fork 3
/
inst_memory.cc
271 lines (250 loc) · 9.98 KB
/
inst_memory.cc
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "inst_memory.h"
using namespace std;
inst_memory_t::inst_memory_t(const char *m_program_code) {
memory.reserve(100); // Reserve space for instructions.
load_program_code(m_program_code); // Load a program code.
}
inst_memory_t::~inst_memory_t() {
}
// Read an instruction from memory.
inst_t* inst_memory_t::read(uint64_t m_pc) {
inst_t *inst = 0;
// PC should be in units of 4 bytes.
m_pc = m_pc >> 2;
// PC = 0 is reserved as invalid.
if(m_pc && (m_pc < memory.size())) { inst = new inst_t(memory[m_pc]); }
return inst;
}
// Get the total number of instructions in memory.
size_t inst_memory_t::num_insts() const{
return memory.size();
}
// Load a program code.
void inst_memory_t::load_program_code(const char *m_program_code) {
// Open a program code file.
fstream file_stream;
file_stream.open(m_program_code, fstream::in);
if(!file_stream.is_open()) {
cerr << "Error: failed to open " << m_program_code << endl;
exit(1);
}
// Insert a nop instruction at PC = 0 to make it as invalid.
memory.insert(memory.begin(), inst_t());
// Read and parse a program code.
string line;
size_t line_num = 0;
while(getline(file_stream, line)) {
line_num++;
// Erase leading spaces.
line.erase(0, line.find_first_not_of(" \t"));
// Crop everything after a comment symbol.
if(line.find_first_of("#") != string::npos) { line.erase(line.find_first_of("#")); }
// Skip blank lines.
if(!line.size()) { continue; }
// Parse an instruction string.
transform(line.begin(), line.end(), line.begin(), ::tolower);
parse_inst_str(line, line_num);
}
// Close the program code file.
file_stream.close();
// Revisit instructions, and replace labels with immediate values.
for(size_t i = 0; i < memory.size(); i++) {
inst_t &inst = memory[i];
if((get_op_type(inst.op) == op_sb_type) || (get_op_type(inst.op) == op_uj_type)) {
map<string, int64_t>::iterator it = labels.find(inst.label);
if(it == labels.end()) {
cerr << "Error: unknown label : " << get_inst_str(&inst) << endl;
exit(1);
}
// PC-relative distance
inst.imm = ((it->second) - int64_t(inst.pc)) >> 1;
// Check if the PC-relative distance fits into the immediate field of instruction.
unsigned imm_width = (get_op_type(inst.op) == op_sb_type ? 12 : 20) - 1;
if(inst.imm >= 0 ? inst.imm >> imm_width : (inst.imm >> imm_width) != -1) {
cerr << "Error: branch target is too far away for "
<< get_inst_str(&inst) << endl;
exit(1);
}
}
}
// Labels are no longer needed.
labels.clear();
}
// Parse an instruction string, and convert it to a Kite instruction.
void inst_memory_t::parse_inst_str(std::string m_inst_str, size_t m_line_num) {
string inst_str = m_inst_str;
// Parse an instruction string.
vector<string> args;
while(inst_str.size()) {
// Get the next argument in the instruction string.
size_t l = inst_str.find_first_of(" \t,():");
// Append a colon symbol to the label.
l += (inst_str[l] == ':' ? 1 : 0);
args.push_back(inst_str.substr(0, l));
// Trim the string.
inst_str.erase(0, l);
inst_str.erase(0, inst_str.find_first_not_of(" \t,()"));
}
// Check if args have a label.
string lbl = args[0];
if(lbl[lbl.size()-1] == ':') {
// Remove the label from args.
args.erase(args.begin());
// Record a pair of label and PC in the map.
labels.insert(pair<string, int64_t>(lbl.substr(0, lbl.size()-1), memory.size()<<2));
}
// Line has no instruction but only a label.
if(!args.size()) { return; }
inst_t inst;
// Set the PC of instruction.
inst.pc = memory.size() << 2;
// Get the opcode of instruction.
inst.op = get_opcode(args[0]);
if(inst.op >= num_kite_opcodes) {
cerr << "Error: unknown opcode " << args[0]
<< " at line #" << m_line_num << endl;
exit(1);
}
// Set an ALU execution latency.
inst.alu_latency = get_op_latency(inst.op);
// Decode the instruction based on its type.
switch(get_op_type(inst.op)) {
case op_r_type: {
// R-type format: op rd, rs1, rs2
if(args.size() != 4) {
cerr << "Error: incomplete instruction: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
if(!is_reg_str(args[1]) || !is_reg_str(args[2]) || !is_reg_str(args[3])) {
cerr << "Error: invalid instruction format: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rd_num = get_regnum(args[1]);
inst.rs1_num = get_regnum(args[2]);
inst.rs2_num = get_regnum(args[3]);
break;
}
case op_i_type: {
if(args.size() != 4) {
cerr << "Error: incomplete instruction: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
if((inst.op == op_jalr) || (inst.op == op_ld)) {
// jalr and ld format: op rd, imm(rs1)
if(!is_reg_str(args[1]) || !is_num_str(args[2]) || !is_reg_str(args[3])) {
cerr << "Error: invalid instruction format: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rd_num = get_regnum(args[1]);
inst.imm = get_imm(args[2]);
inst.rs1_num = get_regnum(args[3]);
}
else {
// I-type format: op rd, rs1, imm
if(!is_reg_str(args[1]) || !is_reg_str(args[2]) || !is_num_str(args[3])) {
cerr << "Error: invalid instruction format: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rd_num = get_regnum(args[1]);
inst.rs1_num = get_regnum(args[2]);
inst.imm = get_imm(args[3]);
}
// Check if the immediate value fits into 12 bits.
if(inst.imm >= 0 ? inst.imm >> 11 : (inst.imm >> 11) != -1) {
cerr << "Error: invalid immediate value: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
break;
}
case op_s_type: {
// S-type format: op rs2, imm(rs1)
if(args.size() != 4) {
cerr << "Error: incomplete instruction: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
if(!is_reg_str(args[1]) || !is_num_str(args[2]) || !is_reg_str(args[3])) {
cerr << "Error: invalid instruction format: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rs2_num = get_regnum(args[1]);
inst.imm = get_imm(args[2]);
// Check if the immediate value fits into 12 bits.
if(inst.imm >= 0 ? inst.imm >> 11 : (inst.imm >> 11) != -1) {
cerr << "Error: invalid immediate value: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rs1_num = get_regnum(args[3]);
break;
}
case op_sb_type: {
// SB-type format: op rs1, rs2, label
if(args.size() != 4) {
cerr << "Error: incomplete instruction: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
if(!is_reg_str(args[1]) || !is_reg_str(args[2])) {
cerr << "Error: invalid instruction format: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rs1_num = get_regnum(args[1]);
inst.rs2_num = get_regnum(args[2]);
inst.label = args[3];
break;
}
case op_u_type: {
// U-type format: op rd, imm
if(args.size() != 3) {
cerr << "Error: incomplete instruction: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
if(!is_reg_str(args[1]) || !is_num_str(args[2])) {
cerr << "Error: invalid instruction format: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rd_num = get_regnum(args[1]);
inst.imm = get_imm(args[2]);
// Check if the immediate value fits into 20 bits.
if(inst.imm >= 0 ? inst.imm >> 19 : (inst.imm >> 19) != -1) {
cerr << "Error: invalid immediate value: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
break;
}
case op_uj_type: {
// UL-type format: op rd, label
if(args.size() != 3) {
cerr << "Error: incomplete instruction: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
if(!is_reg_str(args[1])) {
cerr << "Error: invalid instruction format: " << m_inst_str
<< " at line #" << m_line_num << endl;
exit(1);
}
inst.rd_num = get_regnum(args[1]);
inst.label = args[2];
break;
}
default: { break; } // Nothing to do
}
// Store instruction in memory.
memory.push_back(inst);
}