-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
193 lines (168 loc) · 6.81 KB
/
Copy pathparser.cpp
File metadata and controls
193 lines (168 loc) · 6.81 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
#include "parser.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
int regNumber(std::string r) {
std::string num_str;
for (char c : r) if (isdigit(c)) num_str += c;
if (num_str.empty()) return 0;
return std::stoi(num_str);
}
std::string cleanOpcode(std::string op) {
op.erase(std::remove_if(op.begin(), op.end(), ::isspace), op.end());
return op;
}
std::vector<Instruction> Parser::parseFile(const std::string& filename) {
std::ifstream file(filename);
std::vector<Instruction> program;
std::map<std::string,int> labels;
std::string line;
int lineNo = 0;
while (getline(file, line)) {
size_t comment = line.find("#");
if (comment != std::string::npos) line = line.substr(0, comment);
std::replace(line.begin(), line.end(), ',', ' ');
if (line.find_first_not_of(" \t\r\n") == std::string::npos) continue;
if (line.find(":") != std::string::npos) {
std::string label = line.substr(0, line.find(":"));
labels[label] = lineNo;
line = line.substr(line.find(":")+1);
if (line.find_first_not_of(" \t\r\n") == std::string::npos) continue;
}
std::stringstream ss(line);
Instruction inst;
ss >> inst.opcode;
inst.opcode = cleanOpcode(inst.opcode);
if(inst.opcode=="add"||inst.opcode=="sub") {
std::string rd, rs1, rs2; ss >> rd >> rs1 >> rs2;
inst.rd = regNumber(rd); inst.rs1 = regNumber(rs1); inst.rs2 = regNumber(rs2);
}
else if(inst.opcode=="addi") {
std::string rd, rs1; ss >> rd >> rs1 >> inst.imm;
inst.rd = regNumber(rd); inst.rs1 = regNumber(rs1);
}
else if(inst.opcode=="lw"||inst.opcode=="sw") {
std::string r, mem; ss >> r >> mem;
size_t p = mem.find("(");
if(p!=std::string::npos) {
inst.imm = std::stoi(mem.substr(0, p));
inst.rs1 = regNumber(mem.substr(p+1));
if(inst.opcode=="sw") { inst.rs2 = regNumber(r); inst.rd=-1; }
else { inst.rd = regNumber(r); inst.rs2=-1; }
}
}
else if(inst.opcode=="bne"||inst.opcode=="blt") {
std::string r1, r2, l; ss >> r1 >> r2 >> l;
inst.rs1 = regNumber(r1); inst.rs2 = regNumber(r2); inst.label=l;
}
else if(inst.opcode=="jal") {
std::string r, l; ss >> r >> l;
inst.rs1 = regNumber(r); inst.label=l;
}
program.push_back(inst);
lineNo++;
}
// FIX: Removed the '- 1' from the offset calculation
for(int i=0; i<program.size(); i++) {
if(!program[i].label.empty() && labels.count(program[i].label))
program[i].imm = labels[program[i].label] - i; // CHANGED HERE
}
return program;
}
// =============================================================================
// Phase 3 - Trace File Parser
// Supports trace replay mode with virtual addresses.
// Format:
// L 0xADDR xRD - Load from virtual address into RD
// S 0xADDR xRS - Store from RS to virtual address
// ADD xRD xRS1 xRS2 - Add (1 cycle)
// MUL xRD xRS1 xRS2 - Multiply (3 cycles)
// Blank lines and # comments are skipped.
// Malformed lines produce a stderr warning and are skipped safely.
// =============================================================================
std::vector<Instruction> Parser::parseTraceFile(const std::string& filename) {
std::ifstream file(filename);
std::vector<Instruction> program;
if (!file.is_open()) {
std::cerr << "[TraceParser] ERROR: Cannot open trace file: "
<< filename << "\n";
return program;
}
std::string line;
int lineNo = 0;
while (std::getline(file, line)) {
lineNo++;
// Strip inline comments
size_t comment = line.find('#');
if (comment != std::string::npos) line = line.substr(0, comment);
// Skip blank / whitespace-only lines
if (line.find_first_not_of(" \t\r\n") == std::string::npos) continue;
std::stringstream ss(line);
std::string opcode_raw;
ss >> opcode_raw;
if (opcode_raw.empty()) continue;
// Normalise opcode to lowercase for consistent pipeline matching
std::string op = opcode_raw;
for (char& c : op) c = (char)std::tolower((unsigned char)c);
Instruction inst;
inst.opcode = op;
inst.is_trace = true;
if (op == "l") {
// L 0xADDR xRD
std::string addr_str, reg_str;
if (!(ss >> addr_str >> reg_str)) {
std::cerr << "[TraceParser] WARNING line " << lineNo
<< ": malformed L - \"" << line << "\"\n";
continue;
}
try {
inst.vaddr = (uint32_t)std::stoull(addr_str, nullptr, 16);
} catch (...) {
std::cerr << "[TraceParser] WARNING line " << lineNo
<< ": bad address '" << addr_str << "'\n";
continue;
}
inst.rd = regNumber(reg_str);
inst.rs1 = -1;
inst.rs2 = -1;
} else if (op == "s") {
// S 0xADDR xRS
std::string addr_str, reg_str;
if (!(ss >> addr_str >> reg_str)) {
std::cerr << "[TraceParser] WARNING line " << lineNo
<< ": malformed S - \"" << line << "\"\n";
continue;
}
try {
inst.vaddr = (uint32_t)std::stoull(addr_str, nullptr, 16);
} catch (...) {
std::cerr << "[TraceParser] WARNING line " << lineNo
<< ": bad address '" << addr_str << "'\n";
continue;
}
// Store: source register in rs1, no writeback register (rd = -1)
inst.rd = -1;
inst.rs1 = regNumber(reg_str);
inst.rs2 = -1;
} else if (op == "add" || op == "mul") {
// ADD/MUL xRD xRS1 xRS2
std::string rd_str, rs1_str, rs2_str;
if (!(ss >> rd_str >> rs1_str >> rs2_str)) {
std::cerr << "[TraceParser] WARNING line " << lineNo
<< ": malformed " << opcode_raw
<< " - \"" << line << "\"\n";
continue;
}
inst.rd = regNumber(rd_str);
inst.rs1 = regNumber(rs1_str);
inst.rs2 = regNumber(rs2_str);
} else {
std::cerr << "[TraceParser] WARNING line " << lineNo
<< ": unknown opcode '" << opcode_raw << "' - skipping\n";
continue;
}
program.push_back(inst);
}
return program;
}