Repository navigation
Expand file tree
/
Copy pathParser.cpp
More file actions
280 lines (238 loc) · 11.6 KB
/
Copy pathParser.cpp
File metadata and controls
280 lines (238 loc) · 11.6 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
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
271
272
273
274
275
276
277
278
279
280
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <iostream>
#include "Parser.h"
#include <assert.h>
using namespace std;
void trim_comments(string* str){
size_t found = str->find("//");
while(found != string::npos){
size_t found_2 = str->find("\n", found + 2); //find the fist \n after //
str->erase(found, found_2 - found + 1);
found = str->find("//");
}
size_t found_3 = str->find("/*");
while(found_3 != string::npos){
size_t found_4 = str->find("*/", found_3 + 2); //find the fist */ after /*
str->erase(found_3, found_4 - found_3 + 2);
found_3 = str->find("/*");
}
}
bool parse_circuit(char* filename, Circuit* circuit){
ifstream file;
file.open(filename);
string line_str;
//for trim the comments---------
stringstream file_ss;
file_ss << file.rdbuf();
string file_str;
file_str = file_ss.str();
trim_comments(&file_str);
file_ss.str(file_str);
//------------------------------
while(getline(file_ss, line_str, ';')){ //get line until meeting a ';'
line_str = line_str + ';'; //store back ';' to allow more flexible coding format
stringstream line_ss(line_str);
string keyword;
line_ss >> keyword;
getline(line_ss, line_str, '\0'); //convert stringstream (with keyword removed) to string
transform(keyword.begin(), keyword.end(), keyword.begin(), ::tolower); //lowercase the keyword
if(keyword == "module" || keyword == "endmodule" || keyword == "wire"){
//wire object will be created only if it is connected to a gate, so I skipped this condition.
continue;
}else if(keyword == "input"){
string signal_name = "";
int from = 0, to = 0; //handle the [from:to] condition
bool from_to_format = false; //handle the [from:to] condition
for(int i = 0; i < line_str.size(); i++){ //iterate each char to skip unwanted chars.
if(line_str[i] == '['){
if(!signal_name.empty()){
from_to_format = false;
signal_name += line_str[i];
}else{
from_to_format = true;
}
}else if(line_str[i] == ':'){ //handle the [from:to] condition
assert(from_to_format);
from = stoi(signal_name);
signal_name = "";
}else if(line_str[i] == ']'){
if(from_to_format){
to = stoi(signal_name); //handle the [from:to] condition
signal_name = "";
}else{
signal_name += line_str[i];
}
}else if(line_str[i] == ',' || line_str[i] == ';'){
if(!signal_name.empty()){
for(int k = 0; k <= abs(from-to); k++){ //expand [7:0] S to S[7], S[6], S[5], ...
Wire* new_w = new Wire(); //store input as wire object
if(from_to_format)
new_w->name = signal_name + "[" + to_string(k) + "]";
else
new_w->name = signal_name;
circuit->wireName_2_wireObject[new_w->name] = new_w;
circuit->PIs.push_back(new_w->name);
circuit->wires.push_back(new_w);
}
signal_name = "";
}
}else if(line_str[i] == ' ' || line_str[i] == '\n' || line_str[i] == '\t' || line_str[i] == '\\'){
continue; //skip the blank
}else{
signal_name += line_str[i];
}
}
}else if(keyword == "output"){
string signal_name = "";
int from = 0, to = 0; //handle the [from:to] condition
bool from_to_format = false; //handle the [from:to] condition
for(int i = 0; i < line_str.size(); i++){ //iterate each char to skip unwanted chars.
if(line_str[i] == '['){
if(!signal_name.empty()){
from_to_format = false;
signal_name += line_str[i];
}else{
from_to_format = true;
}
}else if(line_str[i] == ':'){ //handle the [from:to] condition
assert(from_to_format);
from = stoi(signal_name);
signal_name = "";
}else if(line_str[i] == ']'){
if(from_to_format){
to = stoi(signal_name); //handle the [from:to] condition
signal_name = "";
}else{
signal_name += line_str[i];
}
}else if(line_str[i] == ',' || line_str[i] == ';'){
if(!signal_name.empty()){
for(int k = 0; k <= abs(from-to); k++){ //expand [7:0] S to S[7], S[6], S[5], ...
Wire* new_w = new Wire(); //store input as wire object
if(from_to_format)
new_w->name = signal_name + "[" + to_string(k) + "]";
else
new_w->name = signal_name;
circuit->wireName_2_wireObject[new_w->name] = new_w;
circuit->POs.push_back(new_w->name);
circuit->wires.push_back(new_w);
}
signal_name = "";
}
}else if(line_str[i] == ' ' || line_str[i] == '\n' || line_str[i] == '\t' || line_str[i] == '\\'){
continue; //skip the blank
}else{
signal_name += line_str[i];
}
}
}else if(keyword == "not" || keyword == "buf" || keyword == "and" || keyword == "nand"
|| keyword == "or" || keyword == "nor" || keyword == "xor" || keyword =="xnor"){
Gate* new_g = new Gate();
new_g->type = keyword;
new_g->use = 1;
circuit->gates.push_back(new_g);
string signal_name = "";
for(int i = 0; i < line_str.size(); i++){ //iterate each char to skip unwanted chars.
//cout << line_str[i] << endl;
if(line_str[i] == '('){
if(!signal_name.empty()){ //the gate has a gate name
//since we dont care the gate name, I skipped this feature for now.
signal_name = "";
continue;
}
}else if(line_str[i] == ')' || line_str[i] == ','){
if(!signal_name.empty()){
Wire* w;
if(circuit->wireName_2_wireObject.find(signal_name) == circuit->wireName_2_wireObject.end()){
//the wire is not yet defined.
w = new Wire();
w->name = signal_name;
circuit->wireName_2_wireObject[signal_name] = w;
if(w->name == "1'b1")
circuit->const_1 = true;
else if(w->name == "1'b0")
circuit->const_0 = true;
else
circuit->wires.push_back(w);
}
w = circuit->wireName_2_wireObject[signal_name];
if(new_g->out == NULL){ //the wire conntected to the output of the gate
new_g->out = w;
w->in = new_g;
}else{ //the wire connected to the input of the gate
new_g->fanin.push_back(w);
w->fanout.push_back(new_g);
}
signal_name = "";
}
}else if(line_str[i] == ' ' || line_str[i] == '\n' || line_str[i] == '\t' || line_str[i] == '\\'){
continue; //skip the blank
}else{
signal_name += line_str[i];
}
}
}else if(keyword == "assign"){
//assign a = b; --> make a gate: (GATE.gate_type = BUF, fanin = b, and out = a)
string signal_name = "";
string assigned_to_signal = "", assigned_from_signal = "";
Gate* new_g = new Gate();
new_g->use = 1;
new_g->type = "buf";
circuit->gates.push_back(new_g);
for(int i = 0; i < line_str.size(); i++){ //iterate each char to skip unwanted chars.
if(line_str[i] == '='){
assigned_to_signal = signal_name;
signal_name = "";
}else if(line_str[i] == ';'){
assigned_from_signal = signal_name;
if(circuit->wireName_2_wireObject.find(assigned_to_signal) == circuit->wireName_2_wireObject.end()){
//the wire is not yet defined.
Wire* w = new Wire();
w->name = assigned_to_signal;
circuit->wireName_2_wireObject[assigned_to_signal] = w;
if(w->name == "1'b1")
circuit->const_1 = true;
else if(w->name == "1'b0")
circuit->const_0 = true;
else
circuit->wires.push_back(w);
}
if(circuit->wireName_2_wireObject.find(assigned_from_signal) == circuit->wireName_2_wireObject.end()){
//the wire is not yet defined.
Wire* w = new Wire();
w->name = assigned_from_signal;
circuit->wireName_2_wireObject[assigned_from_signal] = w;
if(w->name == "1'b1")
circuit->const_1 = true;
else if(w->name == "1'b0")
circuit->const_0 = true;
else
circuit->wires.push_back(w);
}
Wire* w_to = circuit->wireName_2_wireObject[assigned_to_signal];
Wire* w_from = circuit->wireName_2_wireObject[assigned_from_signal];
new_g->out = w_to;
w_to->in = new_g;
new_g->fanin.push_back(w_from);
w_from->fanout.push_back(new_g);
}else if(line_str[i] == ' ' || line_str[i] == '\n' || line_str[i] == '\t' || line_str[i] == '\\'){
continue; //skip the blank
}else{
signal_name += line_str[i];
}
}
}else{
cout << "unknown type: " << keyword << endl;
//file.close();
//return false;
}
}
file.close();
return true;
}