SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
fstWrapper.cpp
Go to the documentation of this file.
1
/*
2
Copyright (c) 2026. Giulio Cocconi
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include "
fstWrapper.hpp
"
19
20
#include <format>
21
#include <stdexcept>
22
23
namespace
SILICON::waveform::fst
{
24
25
// --- Reader -------------------------------------------------------------------------
26
27
Reader::Reader
(std::string_view fileName) :
fn
(fileName)
28
{
29
// Acquire raw libfst reader context.
30
const
std::string fileNameStorage(fileName);
31
auto
* raw_ctx = fstReaderOpen(fileNameStorage.c_str());
32
33
if
(!raw_ctx)
34
throw
std::runtime_error(std::format(
"Failed to open FST file for reading: {}"
,
fn
));
35
36
// Transfer ownership
37
context
.reset(raw_ctx);
38
}
39
40
std::string
Reader::getVersion
()
const
41
{
42
assert(
context
);
43
44
const
char
* str = fstReaderGetVersionString(
context
.get());
45
return
str ? std::string(str) :
""
;
46
}
47
48
std::string
Reader::getDate
()
const
49
{
50
assert(
context
);
51
52
const
char
* str = fstReaderGetDateString(
context
.get());
53
54
return
str ? std::string(str) :
""
;
55
}
56
57
Reader::FstScopeNode
Reader::buildHierarchyTree
()
58
{
59
assert(
context
);
60
61
std::vector<FstScopeNode> stack;
62
63
iterateHierarchy
([&stack](
const
fstHier* hier) {
64
if
(hier->htyp == FST_HT_SCOPE) {
65
// Enter a new scope
66
stack.push_back(
67
{std::string(hier->u.scope.name, hier->u.scope.name_length),
68
std::string(hier->u.scope.component, hier->u.scope.component_length),
69
static_cast<
fstScopeType
>
(hier->u.scope.typ),
70
{},
71
{}});
72
}
else
if
(hier->htyp == FST_HT_VAR) {
73
// Variable encountered inside current scope.
74
if
(!stack.empty()) {
75
stack.back().vars.push_back(
76
{std::string(hier->u.var.name, hier->u.var.name_length), hier->u.var.handle,
77
hier->u.var.length,
static_cast<
fstVarDir
>
(hier->u.var.direction),
78
static_cast<
fstVarType
>
(hier->u.var.typ)});
79
}
80
}
else
if
(hier->htyp == FST_HT_UPSCOPE) {
81
// Exit current scope. The completed scope node is moved into its parent.
82
if
(!stack.empty() && stack.size() > 1) {
83
FstScopeNode
completed = std::move(stack.back());
84
stack.pop_back();
85
stack.back().children.push_back(std::move(completed));
86
}
87
}
88
});
89
90
if
(stack.size() != 1)
91
throw
std::runtime_error(std::format(
92
"SILICON only supports FSTs with exactly one top-level scope, {} has {}"
,
fn
,
93
stack.size()));
94
95
return
std::move(stack.front());
96
}
97
98
Reader::EnumTable
Reader::parseEnumTable
(std::string_view enumString)
99
{
100
const
std::string enumStringStorage(enumString);
101
fstETab* etab = fstUtilityExtractEnumTableFromString(enumStringStorage.c_str());
102
if
(!etab)
103
return
{};
104
105
EnumTable
ret;
106
if
(etab->name)
107
ret.
name
= etab->name;
108
109
ret.
mapping
.reserve(etab->elem_count);
110
for
(uint32_t i = 0; i < etab->elem_count; ++i) {
111
const
char
* literal = etab->literal_arr[i] ? etab->literal_arr[i] :
""
;
112
const
char
* value = etab->val_arr[i] ? etab->val_arr[i] :
""
;
113
ret.
mapping
.emplace_back(literal, value);
114
}
115
116
fstUtilityFreeEnumTable(etab);
117
return
ret;
118
}
119
120
// --- HierarchyBuilder ---------------------------------------------------------------
121
122
HierarchyBuilder::HierarchyBuilder
(std::string_view fileName,
int
use_compressed_hier)
123
:
fn
(fileName)
124
{
125
const
std::string fileNameStorage(fileName);
126
auto
* raw_ctx = fstWriterCreate(fileNameStorage.c_str(), use_compressed_hier);
127
128
if
(!raw_ctx)
129
throw
std::runtime_error(
130
std::format(
"Failed to create FST file for writing: {}"
,
fn
));
131
132
context
.reset(raw_ctx);
133
}
134
135
void
HierarchyBuilder::setScope
(fstScopeType scope_type, std::string_view scope_name,
136
std::string_view scope_comp)
137
{
138
assert(
context
);
139
const
std::string scopeName(scope_name);
140
const
std::string scopeComp(scope_comp);
141
fstWriterSetScope(
context
.get(), scope_type, scopeName.c_str(), scopeComp.c_str());
142
}
143
144
fstHandle
HierarchyBuilder::createVar
(fstVarType var_type, fstVarDir var_dir,
145
uint32_t len, std::string_view name,
146
fstHandle aliasHandle)
147
{
148
assert(
context
);
149
150
const
std::string nameStorage(name);
151
return
fstWriterCreateVar(
context
.get(), var_type, var_dir, len, nameStorage.c_str(),
152
aliasHandle);
153
}
154
155
fstEnumHandle
HierarchyBuilder::createEnumTable
(
156
std::string_view name,
unsigned
int
min_valbits,
157
const
std::vector<std::pair<const std::string, const std::string>>& values)
158
{
159
assert(
context
);
160
161
// libfst expects separate arrays of pointers rather than a vector of pairs. These
162
// temporary arrays are constructed as lightweight pointer views into the
163
// caller-provided strings.
164
std::vector<const char*> literals;
165
std::vector<const char*> vals;
166
167
literals.reserve(values.size());
168
vals.reserve(values.size());
169
170
for
(
const
auto
& [lit, val] : values) {
171
literals.push_back(lit.c_str());
172
vals.push_back(val.c_str());
173
}
174
175
const
std::string nameStorage(name);
176
return
fstWriterCreateEnumTable(
context
.get(), nameStorage.c_str(), values.size(),
177
min_valbits, literals.data(), vals.data());
178
}
179
180
DataWriter
HierarchyBuilder::finish
() &&
181
{
182
assert(
context
&&
"Builder already consumed!"
);
183
184
// Transfer ownership of the configured writer context into the runtime waveform writer
185
// phase.
186
DataWriter
writer(std::move(
context
));
187
188
return
writer;
189
}
190
191
}
// namespace SILICON::waveform::fst
SILICON::waveform::fst::DataWriter
Stateful writer for waveform value emission.
Definition
fstWrapper.hpp:406
SILICON::waveform::fst::HierarchyBuilder::HierarchyBuilder
HierarchyBuilder(std::string_view fileName, int use_compressed_hier=1)
Definition
fstWrapper.cpp:122
SILICON::waveform::fst::HierarchyBuilder::fn
std::string fn
Definition
fstWrapper.hpp:633
SILICON::waveform::fst::HierarchyBuilder::createEnumTable
fstEnumHandle createEnumTable(std::string_view name, unsigned int min_valbits, const std::vector< std::pair< const std::string, const std::string > > &values)
Creates a symbolic enum mapping table.
Definition
fstWrapper.cpp:155
SILICON::waveform::fst::HierarchyBuilder::setScope
void setScope(fstScopeType scope_type, std::string_view scope_name, std::string_view scope_comp="")
Pushes a new scope onto the writer hierarchy stack.
Definition
fstWrapper.cpp:135
SILICON::waveform::fst::HierarchyBuilder::context
std::unique_ptr< fstWriterContext, WriterDeleter > context
Definition
fstWrapper.hpp:638
SILICON::waveform::fst::HierarchyBuilder::createVar
fstHandle createVar(fstVarType var_type, fstVarDir var_dir, uint32_t len, std::string_view name, fstHandle aliasHandle=0)
Creates a waveform variable.
Definition
fstWrapper.cpp:144
SILICON::waveform::fst::HierarchyBuilder::finish
DataWriter finish() &&
Final typestate transition.
Definition
fstWrapper.cpp:180
SILICON::waveform::fst::Reader::fn
std::string fn
Definition
fstWrapper.hpp:383
SILICON::waveform::fst::Reader::context
std::unique_ptr< fstReaderContext, ReaderDeleter > context
Definition
fstWrapper.hpp:388
SILICON::waveform::fst::Reader::getVersion
std::string getVersion() const
Definition
fstWrapper.cpp:40
SILICON::waveform::fst::Reader::iterateHierarchy
void iterateHierarchy(Func &&callback)
Generic hierarchy iterator.
Definition
fstWrapper.hpp:246
SILICON::waveform::fst::Reader::buildHierarchyTree
FstScopeNode buildHierarchyTree()
Reconstructs hierarchy into a recursive tree structure.
Definition
fstWrapper.cpp:57
SILICON::waveform::fst::Reader::getDate
std::string getDate() const
Definition
fstWrapper.cpp:48
SILICON::waveform::fst::Reader::parseEnumTable
static EnumTable parseEnumTable(std::string_view enumString)
Parses a textual enum encoding into structured data.
Definition
fstWrapper.cpp:98
SILICON::waveform::fst::Reader::Reader
Reader(std::string_view fileName)
Opens an FST file for reading.
Definition
fstWrapper.cpp:27
fstWrapper.hpp
SILICON::waveform::fst
Definition
fstTraceWriter.cpp:27
SILICON::waveform::fst::Reader::EnumTable
Decoded enum table extracted from libfst.
Definition
fstWrapper.hpp:87
SILICON::waveform::fst::Reader::EnumTable::mapping
std::vector< std::pair< std::string, std::string > > mapping
Definition
fstWrapper.hpp:100
SILICON::waveform::fst::Reader::EnumTable::name
std::string name
Definition
fstWrapper.hpp:88
SILICON::waveform::fst::Reader::FstScopeNode
Recursive hierarchy node representing scopes/modules/packages/etc.
Definition
fstWrapper.hpp:138
src
core
fstWrapper.cpp
Generated by
1.17.0