Skip to content

Commit e58c882

Browse files
committed
support for comments (module, net def, io def, statement)
1 parent df09fab commit e58c882

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

include/netlistDB/netlist.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@ class NETLISTDB_PUBLIC SensitivityInfo {
6060
* @ivar parent parent instance of HdlStatement or nullptr
6161
* @ivar _inputs OrderedSet of input signals for this statement
6262
* @ivar _outputs OrderedSet of output signals for this statement
63-
64-
* @ivar rank sum of numbers of used branches in statement, used as prefilter
63+
*
64+
* @ivar rank sum of numbers of used branches in statement, used as pre-filter
6565
* for statement comparing
66+
* @ivar sens the container for informations about sensitivity to nets
67+
* @ivar __doc__ documentation string which will be added as a comment to target HDL
6668
*
6769
* @attention the sensitivity has to be discovered explicitly
6870
*/
6971
class NETLISTDB_PUBLIC Statement: public OperationNode {
7072
public:
7173
Statement * parent;
72-
7374
// IO of this statement
7475
utils::OrderedSet<Net*> _inputs;
7576
utils::OrderedSet<Net*> _outputs;
@@ -78,6 +79,9 @@ class NETLISTDB_PUBLIC Statement: public OperationNode {
7879

7980
size_t rank;
8081

82+
// __doc__ documentation string which will be added as a comment to target HDL
83+
std::string __doc__;
84+
8185
Statement() :
8286
parent(nullptr), rank(0) {
8387
}
@@ -95,7 +99,7 @@ class NETLISTDB_PUBLIC Statement: public OperationNode {
9599
/*
96100
* walk all direct child statements with specified function fn
97101
*
98-
* @param fn callback which is called on each node, if the function returns true the iteration is stoped
102+
* @param fn callback which is called on each node, if the function returns true the iteration is stopped
99103
* and the visit_child_stm returns
100104
* */
101105
virtual void visit_child_stm(const std::function<bool(Statement &)> & fn) = 0;
@@ -165,11 +169,11 @@ class NETLISTDB_PUBLIC ComponentMap: public OperationNode {
165169
* There are multiple types of node in this directed graph
166170
* The signal, statement and FunctionCall (which represents also the operator usage)
167171
*
168-
* Each node has own list of neighbors. This is the case because the graph
172+
* Each node has own list of neighbours. This is the case because the graph
169173
* is expected to change frequently and be larger than 10k nodes this representation
170174
* by linked list or connection matrix would not be efficient.
171175
*
172-
* Also for some nodes the order of neighbors does matter for some does not.
176+
* Also for some nodes the order of neighbours does matter for some does not.
173177
* For example order of Signal endpoints does not matter. The order of Signals
174178
* in FunctionCall does.
175179
*
@@ -182,6 +186,8 @@ class NETLISTDB_PUBLIC Netlist {
182186
std::vector<Net*> nets;
183187
// all nets, statements, operators, etc.
184188
std::vector<iNode*> nodes;
189+
// __doc__ documentation string which will be added as a comment to target HDL
190+
std::string __doc__;
185191

186192
Netlist(const Netlist & other) = delete;
187193
Netlist(const std::string & name);
@@ -221,7 +227,7 @@ class NETLISTDB_PUBLIC Netlist {
221227
~Netlist();
222228
};
223229

224-
/** The net in Netlist instance also the hyperedge which connects
230+
/** The net in Netlist instance also the hyper-edge which connects
225231
* the statements, expressions, etc.
226232
*
227233
* @note The overloaded operators are building the expression in the netlist
@@ -256,6 +262,9 @@ class NETLISTDB_PUBLIC Net: public iNode {
256262
// index used for last priority ordering
257263
// represent the sequential number of the signal generated in parent context
258264

265+
// documentation string which will be added as a comment to target HDL
266+
std::string __doc__;
267+
259268
Net(const Net & other) = delete;
260269
// use methods from Netlist
261270
Net(Netlist & ctx, hw_type::iHwType & t, const std::string & name,
@@ -374,9 +383,4 @@ class NETLISTDB_PUBLIC Net: public iNode {
374383
Net & wrap_val_to_const_net(unsigned val);
375384
};
376385

377-
378-
379-
380-
381386
}
382-

include/netlistDB/serializer/serializer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class Serializer {
2323
bool delete_io_after;
2424
iSerializationIO & io;
2525
Serializer(iSerializationIO & io, bool delete_io_after);
26+
27+
/*
28+
* Serialize the comment string
29+
*
30+
* @note may contain newlines etc.
31+
* */
32+
virtual void serialize_comment(const std::string & comment) = 0;
33+
2634
/*
2735
* Serialize the type id
2836
*

include/netlistDB/serializer/verilog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class Verilog2001: public Serializer {
6464
using Serializer::serialize_stm;
6565
using Serializer::serialize_value;
6666

67+
virtual void serialize_comment(const std::string & comment) override;
68+
6769
virtual bool serialize_type_usage(const hw_type::iHwType & t);
6870
virtual void serialize_value(
6971
const typename hw_type::HwInt::value_type & val) override;

src/serializer/verilog.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ Verilog2001::Verilog2001(const std::string & root_dir,
3131
Verilog2001(*new SerializeToFiles(root_dir), reserved_names, true) {
3232
}
3333

34+
void Verilog2001::serialize_comment(const std::string & comment) {
35+
if (comment.size()) {
36+
auto & str = io.str();
37+
indent() << "// ";
38+
for (auto ch: comment) {
39+
str << ch;
40+
if (ch == '\n')
41+
indent() << "// ";
42+
}
43+
str << endl;
44+
}
45+
}
46+
3447
void Verilog2001::print_array_indexes(const hw_type::iHwType * t, bool first) {
3548
auto & str = io.str();
3649
auto at = dynamic_cast<const hw_type::iHwType_array*>(t);
@@ -59,6 +72,7 @@ const hw_type::iHwType & Verilog2001::get_non_array_t(
5972
}
6073

6174
void Verilog2001::serialize_net_def(const Net & n) {
75+
serialize_comment(n.__doc__);
6276
auto & str = io.str();
6377
indent();
6478
auto v_t = verilogTypeOfSig(n);

src/serializer/verilog_structural.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void Verilog2001::serialize_direction(Direction d) {
2424
}
2525

2626
void Verilog2001::serialize_io(const Net & io_net) {
27+
serialize_comment(io_net.__doc__);
2728
auto & str = io.str();
2829
indent();
2930
serialize_direction(io_net.direction);
@@ -56,7 +57,7 @@ void Verilog2001::serialize_module_head(const Netlist & netlist) {
5657
//{{indent}} ){% endif %}{% if ports|length >0 %}({{
5758
// ports|join(',\n' + indent + ' ')}}
5859
//{{indent}} );{% endif %}
59-
60+
serialize_comment(netlist.__doc__);
6061
indent() << "module "
6162
<< name_scope.checkedName(netlist.name, &netlist, true);
6263
// [TODO] parameters if present

0 commit comments

Comments
 (0)