SILICON
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
23namespace SILICON::waveform::fst {
24
25// --- Reader -------------------------------------------------------------------------
26
27Reader::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
40std::string Reader::getVersion() const
41{
42 assert(context);
43
44 const char* str = fstReaderGetVersionString(context.get());
45 return str ? std::string(str) : "";
46}
47
48std::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
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
98Reader::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
122HierarchyBuilder::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
135void 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
144fstHandle 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
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
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
Stateful writer for waveform value emission.
HierarchyBuilder(std::string_view fileName, int use_compressed_hier=1)
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.
void setScope(fstScopeType scope_type, std::string_view scope_name, std::string_view scope_comp="")
Pushes a new scope onto the writer hierarchy stack.
std::unique_ptr< fstWriterContext, WriterDeleter > context
fstHandle createVar(fstVarType var_type, fstVarDir var_dir, uint32_t len, std::string_view name, fstHandle aliasHandle=0)
Creates a waveform variable.
DataWriter finish() &&
Final typestate transition.
std::unique_ptr< fstReaderContext, ReaderDeleter > context
std::string getVersion() const
void iterateHierarchy(Func &&callback)
Generic hierarchy iterator.
FstScopeNode buildHierarchyTree()
Reconstructs hierarchy into a recursive tree structure.
std::string getDate() const
static EnumTable parseEnumTable(std::string_view enumString)
Parses a textual enum encoding into structured data.
Reader(std::string_view fileName)
Opens an FST file for reading.
Decoded enum table extracted from libfst.
std::vector< std::pair< std::string, std::string > > mapping
Recursive hierarchy node representing scopes/modules/packages/etc.