Skip to content

Commit

Permalink
write_verilog escaped bus port name "input [7:0] \in[0] ;"
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcherry56 committed Jul 4, 2019
1 parent 7af6906 commit 74e287a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 56 deletions.
4 changes: 4 additions & 0 deletions network/ParseBus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void
parseBusName(const char *name,
const char brkt_left,
const char brkt_right,
// Return values.
char *&bus_name,
int &index)
{
Expand All @@ -55,6 +56,7 @@ void
parseBusName(const char *name,
const char *brkts_left,
const char *brkts_right,
// Return values.
char *&bus_name,
int &index)
{
Expand All @@ -81,6 +83,7 @@ void
parseBusRange(const char *name,
const char brkt_left,
const char brkt_right,
// Return values.
char *&bus_name,
int &from,
int &to)
Expand All @@ -94,6 +97,7 @@ void
parseBusRange(const char *name,
const char *brkts_left,
const char *brkts_right,
// Return values.
char *&bus_name,
int &from,
int &to)
Expand Down
6 changes: 5 additions & 1 deletion network/ParseBus.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ isBusName(const char *name,

// Parse name as a bus.
// signal
// bus_name = 0
// bus_name = nullptr
// bus[bit]
// bus_name = "bus"
// index = bit
Expand All @@ -36,13 +36,15 @@ void
parseBusName(const char *name,
const char brkt_left,
const char brkt_right,
// Return values.
char *&bus_name,
int &index);
// Allow multiple different left/right bus brackets.
void
parseBusName(const char *name,
const char *brkts_left,
const char *brkts_right,
// Return values.
char *&bus_name,
int &index);

Expand All @@ -53,6 +55,7 @@ void
parseBusRange(const char *name,
const char brkt_left,
const char brkt_right,
// Return values.
char *&bus_name,
int &from,
int &to);
Expand All @@ -62,6 +65,7 @@ void
parseBusRange(const char *name,
const char *brkts_left,
const char *brkts_right,
// Return values.
char *&bus_name,
int &from,
int &to);
Expand Down
18 changes: 13 additions & 5 deletions network/VerilogNamespace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <ctype.h>
#include "Machine.hh"
#include "StringUtil.hh"
#include "ParseBus.hh"
#include "VerilogNamespace.hh"

namespace sta {
Expand All @@ -25,21 +26,29 @@ const char *
instanceVerilogName(const char *sta_name,
const char escape)
{
return staToVerilog(sta_name, true, escape);
return staToVerilog(sta_name, escape);
}

const char *
netVerilogName(const char *sta_name,
const char escape)
{
return staToVerilog(sta_name, false, escape);
char *bus_name;
int index;
parseBusName(sta_name, '[', ']', bus_name, index);
if (bus_name)
return stringPrintTmp("%s[%d]",
staToVerilog(bus_name, escape),
index);
else
return staToVerilog(sta_name, escape);
}

const char *
portVerilogName(const char *sta_name,
const char escape)
{
return staToVerilog(sta_name, false, escape);
return staToVerilog(sta_name, escape);
}

// Append ch to str at insert. Resize str if necessary.
Expand All @@ -63,7 +72,6 @@ vstringAppend(char *&str,

const char *
staToVerilog(const char *sta_name,
bool escape_brkts,
const char escape)
{
const char bus_brkt_left = '[';
Expand Down Expand Up @@ -93,7 +101,7 @@ staToVerilog(const char *sta_name,
else {
bool is_brkt = (ch == bus_brkt_left || ch == bus_brkt_right);
if ((!(isalnum(ch) || ch == '_') && !is_brkt)
|| (is_brkt && escape_brkts))
|| is_brkt)
escaped = true;
vstringAppend(verilog_name, verilog_name_end, v, ch);
}
Expand Down
1 change: 0 additions & 1 deletion network/VerilogNamespace.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace sta {

const char *
staToVerilog(const char *sta_name,
bool escape_brkts,
const char escape);
const char *
verilogToSta(const char *verilog_name);
Expand Down
5 changes: 4 additions & 1 deletion verilog/Verilog.i
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ void
write_verilog_cmd(const char *filename,
bool sort)
{
Network *network = cmdNetwork();
// This does NOT want the SDC (cmd) network because it wants
// to see the sta internal names.
Sta *sta = Sta::sta();
Network *network = sta->network();
writeVerilog(filename, sort, network);
}

Expand Down
62 changes: 34 additions & 28 deletions verilog/VerilogReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1721,38 +1721,44 @@ VerilogReader::linkNetwork(const char *top_cell_name,
bool make_black_boxes,
Report *report)
{
Cell *top_cell = network_->findCell(library_, top_cell_name);
VerilogModule *module = verilog_reader->module(top_cell);
if (module) {
// Seed the recursion for expansion with the top level instance.
Instance *top_instance = network_->makeInstance(top_cell, "", nullptr);
VerilogBindingTbl bindings(zero_net_name_, one_net_name_);
VerilogNetSeq::Iterator port_iter(module->ports());
while (port_iter.hasNext()) {
VerilogNet *mod_port = port_iter.next();
VerilogNetNameIterator *net_name_iter = mod_port->nameIterator(module,
this);
while (net_name_iter->hasNext()) {
const char *net_name = net_name_iter->next();
Port *port = network_->findPort(top_cell, net_name);
Net *net = bindings.ensureNetBinding(net_name, top_instance, network_);
// Guard against repeated port name.
if (network_->findPin(top_instance, port) == nullptr) {
Pin *pin = network_->makePin(top_instance, port, nullptr);
network_->makeTerm(pin, net);
if (library_) {
Cell *top_cell = network_->findCell(library_, top_cell_name);
VerilogModule *module = this->module(top_cell);
if (module) {
// Seed the recursion for expansion with the top level instance.
Instance *top_instance = network_->makeInstance(top_cell, "", nullptr);
VerilogBindingTbl bindings(zero_net_name_, one_net_name_);
VerilogNetSeq::Iterator port_iter(module->ports());
while (port_iter.hasNext()) {
VerilogNet *mod_port = port_iter.next();
VerilogNetNameIterator *net_name_iter = mod_port->nameIterator(module,
this);
while (net_name_iter->hasNext()) {
const char *net_name = net_name_iter->next();
Port *port = network_->findPort(top_cell, net_name);
Net *net = bindings.ensureNetBinding(net_name, top_instance, network_);
// Guard against repeated port name.
if (network_->findPin(top_instance, port) == nullptr) {
Pin *pin = network_->makePin(top_instance, port, nullptr);
network_->makeTerm(pin, net);
}
}
delete net_name_iter;
}
delete net_name_iter;
makeModuleInstBody(module, top_instance, &bindings, make_black_boxes);
bool errors = reportLinkErrors(report);
deleteModules();
if (errors) {
network_->deleteInstance(top_instance);
return nullptr;
}
else
return top_instance;
}
makeModuleInstBody(module, top_instance, &bindings, make_black_boxes);
bool errors = reportLinkErrors(report);
deleteModules();
if (errors) {
network_->deleteInstance(top_instance);
else {
report->error("%s is not a verilog module.\n", top_cell_name);
return nullptr;
}
else
return top_instance;
}
else {
report->error("%s is not a verilog module.\n", top_cell_name);
Expand Down Expand Up @@ -1848,7 +1854,7 @@ VerilogReader::makeModuleInstNetwork(VerilogModuleInst *mod_inst,
parent_module, parent_bindings, is_leaf);
}
if (!is_leaf) {
VerilogModule *module = verilog_reader->module(cell);
VerilogModule *module = this->module(cell);
makeModuleInstBody(module, inst, &bindings, make_black_boxes);
}
}
Expand Down
43 changes: 23 additions & 20 deletions verilog/VerilogWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ writeVerilog(const char *filename,
bool sort,
Network *network)
{
FILE *stream = fopen(filename, "w");
if (stream) {
VerilogWriter writer(filename, sort, stream, network);
writer.writeModule(network->topInstance());
fclose(stream);
if (network->topInstance()) {
FILE *stream = fopen(filename, "w");
if (stream) {
VerilogWriter writer(filename, sort, stream, network);
writer.writeModule(network->topInstance());
fclose(stream);
}
else
throw FileNotWritable(filename);
}
else
throw FileNotWritable(filename);
}

VerilogWriter::VerilogWriter(const char *filename,
Expand Down Expand Up @@ -117,8 +119,8 @@ VerilogWriter::writePorts(Cell *cell)
Port *port = port_iter->next();
if (!first)
fprintf(stream_, ",\n ");
fprintf(stream_, "%s",
network_->name(port));
fprintf(stream_, "%s", portVerilogName(network_->name(port),
network_->pathEscape()));
first = false;
}
delete port_iter;
Expand All @@ -132,23 +134,23 @@ VerilogWriter::writePortDcls(Cell *cell)
while (port_iter->hasNext()) {
Port *port = port_iter->next();
PortDirection *dir = network_->direction(port);
if (dir) {
fprintf(stream_, " %s",
verilogPortDir(dir));
const char *port_name = portVerilogName(network_->name(port),
network_->pathEscape());
const char *vtype = verilogPortDir(dir);
if (vtype) {
fprintf(stream_, " %s", vtype);
if (network_->isBus(port))
fprintf(stream_, " [%d:%d]",
network_->fromIndex(port),
network_->toIndex(port));
fprintf(stream_, " %s;\n",
network_->name(port));
fprintf(stream_, " %s;\n", port_name);
if (dir->isTristate()) {
fprintf(stream_, " tri");
if (network_->isBus(port))
fprintf(stream_, " [%d:%d]",
network_->fromIndex(port),
network_->toIndex(port));
fprintf(stream_, " %s;\n",
network_->name(port));
fprintf(stream_, " [%d:%d]",
network_->fromIndex(port),
network_->toIndex(port));
fprintf(stream_, " %s;\n", port_name);
}
}
}
Expand Down Expand Up @@ -226,7 +228,8 @@ VerilogWriter::writeInstPin(Instance *inst,
const char *net_vname = netVerilogName(net_name, network_->pathEscape());
if (!first_port)
fprintf(stream_, ",\n ");
const char *port_name = network_->name(port);
const char *port_name = portVerilogName(network_->name(port),
network_->pathEscape());
fprintf(stream_, ".%s(%s)",
port_name,
net_vname);
Expand Down

0 comments on commit 74e287a

Please sign in to comment.