@@ -41,41 +41,43 @@ class VerilogWriter
4141 CellSeq *remove_cells,
4242 FILE *stream,
4343 Network *network);
44- void writeModule (Instance *inst );
44+ void writeModules ( );
4545
4646protected:
47- void writePorts (Cell *cell);
48- void writePortDcls (Cell *cell);
49- void writeWireDcls (Instance *inst);
47+ void writeModule (const Instance *inst);
48+ InstanceSeq findHierChildren ();
49+ void findHierChildren (const Instance *inst,
50+ InstanceSeq &children,
51+ CellSet &cells);
52+ void writePorts (const Cell *cell);
53+ void writePortDcls (const Cell *cell);
54+ void writeWireDcls (const Instance *inst);
5055 const char *verilogPortDir (PortDirection *dir);
51- void writeChildren (Instance *inst);
52- void writeChild (Instance *child);
53- void writeInstPin (Instance *inst,
54- Port *port,
56+ void writeChildren (const Instance *inst);
57+ void writeChild (const Instance *child);
58+ void writeInstPin (const Instance *inst,
59+ const Port *port,
5560 bool &first_port);
56- void writeInstBusPin (Instance *inst,
57- Port *port,
61+ void writeInstBusPin (const Instance *inst,
62+ const Port *port,
5863 bool &first_port);
59- void writeInstBusPinBit (Instance *inst,
60- Port *port,
64+ void writeInstBusPinBit (const Instance *inst,
65+ const Port *port,
6166 bool &first_member);
62- void writeAssigns (Instance *inst);
67+ void writeAssigns (const Instance *inst);
6368
6469 int findUnconnectedNetCount ();
65- int findNCcount (Instance *inst);
66- int findChildNCcount (Instance *child);
67- int findPortNCcount (Instance *inst,
68- Port *port);
70+ int findNCcount (const Instance *inst);
71+ int findChildNCcount (const Instance *child);
72+ int findPortNCcount (const Instance *inst,
73+ const Port *port);
6974
7075 const char *filename_;
7176 bool sort_;
7277 bool include_pwr_gnd_;
7378 CellSet remove_cells_;
7479 FILE *stream_;
7580 Network *network_;
76-
77- CellSet written_cells_;
78- Vector<Instance*> pending_children_;
7981 int unconnected_net_index_;
8082};
8183
@@ -91,7 +93,7 @@ writeVerilog(const char *filename,
9193 if (stream) {
9294 VerilogWriter writer (filename, sort, include_pwr_gnd,
9395 remove_cells, stream, network);
94- writer.writeModule (network-> topInstance () );
96+ writer.writeModules ( );
9597 fclose (stream);
9698 }
9799 else
@@ -111,7 +113,6 @@ VerilogWriter::VerilogWriter(const char *filename,
111113 remove_cells_ (network),
112114 stream_ (stream),
113115 network_ (network),
114- written_cells_ (network),
115116 unconnected_net_index_ (1 )
116117{
117118 if (remove_cells) {
@@ -121,7 +122,54 @@ VerilogWriter::VerilogWriter(const char *filename,
121122}
122123
123124void
124- VerilogWriter::writeModule (Instance *inst)
125+ VerilogWriter::writeModules ()
126+ {
127+ // Write the top level modeule first.
128+ writeModule (network_->topInstance ());
129+ InstanceSeq hier_childrenn = findHierChildren ();
130+ for (const Instance *child : hier_childrenn)
131+ writeModule (child);
132+ }
133+
134+ InstanceSeq
135+ VerilogWriter::findHierChildren ()
136+ {
137+ InstanceSeq children;
138+ CellSet cells (network_);
139+ findHierChildren (network_->topInstance (), children, cells);
140+
141+ if (sort_)
142+ sort (children, [this ](const Instance *inst1,
143+ const Instance *inst2) {
144+ const char *cell_name1 = network_->cellName (inst1);
145+ const char *cell_name2 = network_->cellName (inst2);
146+ return stringLess (cell_name1, cell_name2);
147+ });
148+
149+ return children;
150+ }
151+
152+ void
153+ VerilogWriter::findHierChildren (const Instance *inst,
154+ InstanceSeq &children,
155+ CellSet &cells)
156+ {
157+ InstanceChildIterator *child_iter = network_->childIterator (inst);
158+ while (child_iter->hasNext ()) {
159+ const Instance *child = child_iter->next ();
160+ const Cell *cell = network_->cell (child);
161+ if (network_->isHierarchical (child)
162+ && !cells.hasKey (cell)) {
163+ children.push_back (child);
164+ cells.insert (cell);
165+ findHierChildren (child, children, cells);
166+ }
167+ }
168+ delete child_iter;
169+ }
170+
171+ void
172+ VerilogWriter::writeModule (const Instance *inst)
125173{
126174 Cell *cell = network_->cell (inst);
127175 fprintf (stream_, " module %s (" ,
@@ -134,22 +182,10 @@ VerilogWriter::writeModule(Instance *inst)
134182 writeChildren (inst);
135183 writeAssigns (inst);
136184 fprintf (stream_, " endmodule\n " );
137- written_cells_.insert (cell);
138-
139- if (sort_)
140- sort (pending_children_, [this ](const Instance *inst1,
141- const Instance *inst2) {
142- return stringLess (network_->cellName (inst1), network_->cellName (inst2));
143- });
144- for (auto child : pending_children_) {
145- Cell *child_cell = network_->cell (child);
146- if (!written_cells_.hasKey (child_cell))
147- writeModule (child);
148- }
149185}
150186
151187void
152- VerilogWriter::writePorts (Cell *cell)
188+ VerilogWriter::writePorts (const Cell *cell)
153189{
154190 bool first = true ;
155191 CellPortIterator *port_iter = network_->portIterator (cell);
@@ -170,7 +206,7 @@ VerilogWriter::writePorts(Cell *cell)
170206}
171207
172208void
173- VerilogWriter::writePortDcls (Cell *cell)
209+ VerilogWriter::writePortDcls (const Cell *cell)
174210{
175211 CellPortIterator *port_iter = network_->portIterator (cell);
176212 while (port_iter->hasNext ()) {
@@ -228,7 +264,7 @@ VerilogWriter::verilogPortDir(PortDirection *dir)
228264typedef std::pair<int , int > BusIndexRange;
229265
230266void
231- VerilogWriter::writeWireDcls (Instance *inst)
267+ VerilogWriter::writeWireDcls (const Instance *inst)
232268{
233269 Cell *cell = network_->cell (inst);
234270 char escape = network_->pathEscape ();
@@ -274,16 +310,13 @@ VerilogWriter::writeWireDcls(Instance *inst)
274310}
275311
276312void
277- VerilogWriter::writeChildren (Instance *inst)
313+ VerilogWriter::writeChildren (const Instance *inst)
278314{
279315 Vector<Instance*> children;
280316 InstanceChildIterator *child_iter = network_->childIterator (inst);
281317 while (child_iter->hasNext ()) {
282318 Instance *child = child_iter->next ();
283319 children.push_back (child);
284- if (network_->isHierarchical (child)) {
285- pending_children_.push_back (child);
286- }
287320 }
288321 delete child_iter;
289322
@@ -298,7 +331,7 @@ VerilogWriter::writeChildren(Instance *inst)
298331}
299332
300333void
301- VerilogWriter::writeChild (Instance *child)
334+ VerilogWriter::writeChild (const Instance *child)
302335{
303336 Cell *child_cell = network_->cell (child);
304337 if (!remove_cells_.hasKey (child_cell)) {
@@ -325,8 +358,8 @@ VerilogWriter::writeChild(Instance *child)
325358}
326359
327360void
328- VerilogWriter::writeInstPin (Instance *inst,
329- Port *port,
361+ VerilogWriter::writeInstPin (const Instance *inst,
362+ const Port *port,
330363 bool &first_port)
331364{
332365 Pin *pin = network_->findPin (inst, port);
@@ -348,8 +381,8 @@ VerilogWriter::writeInstPin(Instance *inst,
348381}
349382
350383void
351- VerilogWriter::writeInstBusPin (Instance *inst,
352- Port *port,
384+ VerilogWriter::writeInstBusPin (const Instance *inst,
385+ const Port *port,
353386 bool &first_port)
354387{
355388 if (!first_port)
@@ -382,8 +415,8 @@ VerilogWriter::writeInstBusPin(Instance *inst,
382415}
383416
384417void
385- VerilogWriter::writeInstBusPinBit (Instance *inst,
386- Port *port,
418+ VerilogWriter::writeInstBusPinBit (const Instance *inst,
419+ const Port *port,
387420 bool &first_member)
388421{
389422 Pin *pin = network_->findPin (inst, port);
@@ -405,7 +438,7 @@ VerilogWriter::writeInstBusPinBit(Instance *inst,
405438// Use an assign statement to alias the net when it is connected to
406439// multiple output ports.
407440void
408- VerilogWriter::writeAssigns (Instance *inst)
441+ VerilogWriter::writeAssigns (const Instance *inst)
409442{
410443 InstancePinIterator *pin_iter = network_->pinIterator (inst);
411444 while (pin_iter->hasNext ()) {
@@ -445,7 +478,7 @@ VerilogWriter::findUnconnectedNetCount()
445478}
446479
447480int
448- VerilogWriter::findNCcount (Instance *inst)
481+ VerilogWriter::findNCcount (const Instance *inst)
449482{
450483 int nc_count = 0 ;
451484 InstanceChildIterator *child_iter = network_->childIterator (inst);
@@ -458,7 +491,7 @@ VerilogWriter::findNCcount(Instance *inst)
458491}
459492
460493int
461- VerilogWriter::findChildNCcount (Instance *child)
494+ VerilogWriter::findChildNCcount (const Instance *child)
462495{
463496 int nc_count = 0 ;
464497 Cell *child_cell = network_->cell (child);
@@ -475,8 +508,8 @@ VerilogWriter::findChildNCcount(Instance *child)
475508}
476509
477510int
478- VerilogWriter::findPortNCcount (Instance *inst,
479- Port *port)
511+ VerilogWriter::findPortNCcount (const Instance *inst,
512+ const Port *port)
480513{
481514 int nc_count = 0 ;
482515 LibertyPort *lib_port = network_->libertyPort (port);
0 commit comments