-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
366 lines (337 loc) · 11.5 KB
/
Parser.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Author: Cynthia Wang
* Date created: 10/27/2023
* Organization: ECE 4122
*
* Description:
* Source file for Parser class and functions. See Parser.hpp for function descriptions.
*/
#include "Parser.hpp"
#include <unistd.h>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <thread>
#include <ncurses.h>
#include <regex>
#if USE_OMP
#include <omp.h>
#endif
void Parser::parse()
{
// Determine the number of threads that can run concurrently
uint64_t numThreads = std::thread::hardware_concurrency();
printw("Your computer supports %llu concurrent threads.\n\r", numThreads);
#ifdef USE_OMP
printw("Using OpenMP.\n\r", numThreads);
#endif
refresh();
std::ifstream infile(filename);
std::string line;
VcdScope* currScope = nullptr;
std::string token;
std::regex unpackedVecRegex("^.+\\[[0-9]+\\]$");
startMeasureTime("Parsing...");
// read in line by line
while (std::getline(infile, line))
{
boost::algorithm::trim(line);
if (line.empty()) continue;
std::istringstream ss(line);
// parse each line token by token (space-separated)
while (ss >> token)
{
if (token.at(0) == '$')
{
currState = getParseState(token);
if (currState == PARSE_UPSCOPE)
{
currScope = currScope->parent;
break;
}
}
else
{
switch (currState)
{
case PARSE_VERSION:
{
version += ' ' + token;
break;
}
case PARSE_DATE:
{
date += ' ' + token;
break;
}
case PARSE_TIMESCALE:
{
timescale += ' ' + token;
break;
}
case PARSE_SCOPE:
{
VcdScope* nextScope = new VcdScope();
nextScope->parent = currScope;
scopes.emplace_back(nextScope);
ss >> token;
nextScope->name = token;
if (!currScope) // top level
{
if (!topScope)
{
topScope = nextScope;
currScope = nextScope;
}
else
{ // visited before
currScope = topScope;
}
}
else if (!currScope->children.count(nextScope->name)) // non-top level, haven't visited before
{
currScope->children[nextScope->name] = nextScope;
currScope = nextScope;
}
else // top level, have visited before
currScope = dynamic_cast<VcdScope*>(currScope->children[nextScope->name]);
break;
}
case PARSE_VAR:
{
std::string type = token;
ss >> token;
uint64_t size = stoi(token);
ss >> token;
std::string hash = token;
ss >> token;
std::string name = token;
ss >> token;
std::string dimensions = token;
if (!varMap.count(hash))
{
VcdVar* curr_var = new VcdVar();
curr_var->parent = currScope;
curr_var->size = size;
curr_var->hash = hash;
curr_var->name = name;
curr_var->dimensions = dimensions;
varMap[hash] = curr_var;
varHashes.push_back(hash);
}
if (std::regex_match(name, unpackedVecRegex))
{
// unpacked array, extract name to use as scope
std::string scopeName = name.substr(0, name.find("["));
VcdScope* arrScope;
if (arrScopes.count(scopeName))
{
arrScope = arrScopes[scopeName];
}
else
{
arrScope = new VcdArrScope();
arrScope->name = scopeName;
arrScope->parent = currScope;
arrScope->type = VcdNode::ARR_SCOPE;
currScope->children[scopeName] = arrScope;
scopes.emplace_back(arrScope);
arrScopes[scopeName] = arrScope;
}
varMap[hash]->parent = arrScope;
arrScope->children[varMap[hash]->name] = varMap[hash];
}
else
{
currScope->children[varMap[hash]->name] = varMap[hash];
}
break;
}
case PARSE_VALUES:
{
if (token.at(0) == '#')
{
currTime = stoi(token.substr(1, token.size()));
}
else if (token.at(0) == 'b')
{
std::string value = token;
ss >> token;
varMap[token]->vcdValues.emplace_back(
std::pair(currTime, value));
}
else
{
std::string hash = token.substr(1, token.size());
varMap[hash]->vcdValues.emplace_back(
std::pair(currTime, token.substr(0, 1)));
}
break;
}
default:
assert(false);
}
}
}
}
maxTime = currTime;
if (!version.empty()) version = version.substr(1, version.size());
if (!timescale.empty()) timescale = timescale.substr(1, timescale.size());
if (!date.empty()) date = date.substr(1, date.size());
endMeasureTime("Parse Time");
startMeasureTime("Processing data into value intervals...");
#ifdef USE_OMP
constructValueIntervals();
#else
std::list<std::thread> threads;
uint64_t numVars = varHashes.size();
uint64_t varsPerThread = floor((numVars) / (double) (numThreads - 1)); // min # vars each thread should have
uint64_t remainder = numVars % (numThreads - 1);
uint64_t i = 0;
// Create the threads (max n-1) and distribute responsibility among the threads
while (threads.size() < (numThreads - 1))
{
uint64_t end = threads.size() >= remainder ? i + varsPerThread : i + varsPerThread + 1;
threads.emplace_back(
std::thread(
[this, start = i, end]()
{
this->constructValueIntervals(start, end);
}
)
);
i = end;
}
// wait for threads to finish
for (auto& thread : threads)
{
thread.join();
}
#endif
endMeasureTime("Value Interval Processing Time");
printw(
"\n\r"
"Version: %s\n\r"
"Date: %s\n\r"
"Timescale: %s\n\r"
"Top scope: %s\n\r",
version.c_str(),
date.c_str(),
timescale.c_str(),
topScope->name.c_str()
);
refresh();
}
#ifdef USE_OMP
void Parser::constructValueIntervals()
{
constructValueIntervals(0, varHashes.size());
}
#endif
void Parser::constructValueIntervals(uint64_t startIdx, uint64_t endIdx)
{
#ifdef USE_OMP
#pragma omp parallel for
#endif
for (uint64_t i = startIdx; i < endIdx; i++)
{
std::string hash = varHashes[i];
VcdVar* var = varMap[hash];
auto it = var->vcdValues.begin();
uint64_t prevTimestamp = it->first;
std::string prevValue = it->second;
++it;
for (; it != var->vcdValues.end(); ++it)
{
var->intervalValues +=
std::make_pair(
boost::icl::interval<uint64_t>::right_open(
prevTimestamp, it->first
),
prevValue);
prevTimestamp = it->first;
prevValue = it->second;
}
var->intervalValues +=
std::make_pair(
boost::icl::interval<uint64_t>::right_open(
prevTimestamp, maxTime + 1
),
prevValue);
}
}
Parser::~Parser()
{
// delete scopes
for (auto& scope : scopes)
{
delete scope;
}
// delete vars
for (auto& x : varMap)
{
delete x.second;
}
}
inline Parser::State Parser::getParseState(std::string token)
{
if (currState == PARSE_VALUES) return PARSE_VALUES;
if (token == "$version") return PARSE_VERSION;
if (token == "$date") return PARSE_DATE;
if (token == "$timescale") return PARSE_TIMESCALE;
if (token == "$scope") return PARSE_SCOPE;
if (token == "$upscope") return PARSE_UPSCOPE;
if (token == "$var") return PARSE_VAR;
if (token == "$enddefinitions") return PARSE_ENDDEFINITIONS;
if (token == "$end")
{
if (currState == PARSE_ENDDEFINITIONS) return PARSE_VALUES;
return PARSE_NONE;
}
return PARSE_ERR;
}
VcdScope* Parser::getTop()
{
return topScope;
}
VcdVar* Parser::getVcdVar(std::string hierarchicalName, VcdScope* scope)
{
size_t pos = hierarchicalName.find('.');
std::string token;
VcdScope* currScope = scope;
token = hierarchicalName.substr(0, pos);
assert(currScope->name == token);
hierarchicalName.erase(0, pos + 1);
while ((pos = hierarchicalName.find('.')) != std::string::npos)
{
token = hierarchicalName.substr(0, pos);
currScope = dynamic_cast<VcdScope*>(currScope->children[token]);
hierarchicalName.erase(0, pos + 1);
}
return dynamic_cast<VcdVar*>(currScope->children[hierarchicalName]);
}
VcdVar* Parser::getVcdVar(std::string hierarchicalName)
{
return getVcdVar(hierarchicalName, topScope);
}
void Parser::startMeasureTime(const char* message)
{
startTime = std::chrono::high_resolution_clock::now();
printw("%s\n\r", message);
refresh();
}
void Parser::endMeasureTime(const char* desc)
{
auto elapsed = std::chrono::high_resolution_clock::now() - startTime;
printw("%s: %llu us\n\r", desc, std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count());
refresh();
}
size_t Parser::getMaxTime()
{
return maxTime;
}
std::string Parser::getTimescale()
{
return timescale;
}