2018-09-28 17:54:21 +02:00
|
|
|
// OpenSTA, Static Timing Analyzer
|
2025-01-22 02:54:33 +01:00
|
|
|
// Copyright (c) 2025, Parallax Software, Inc.
|
2018-09-28 17:54:21 +02:00
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2022-01-04 18:17:08 +01:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-09-28 17:54:21 +02:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2022-01-04 18:17:08 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2025-01-22 02:54:33 +01:00
|
|
|
//
|
|
|
|
|
// The origin of this software must not be misrepresented; you must not
|
|
|
|
|
// claim that you wrote the original software.
|
|
|
|
|
//
|
|
|
|
|
// Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
// misrepresented as being the original software.
|
|
|
|
|
//
|
|
|
|
|
// This notice may not be removed or altered from any source distribution.
|
2018-09-28 17:54:21 +02:00
|
|
|
|
2020-04-05 23:53:44 +02:00
|
|
|
#include "VerilogNamespace.hh"
|
2020-04-05 20:35:51 +02:00
|
|
|
|
2023-06-15 17:59:56 +02:00
|
|
|
#include <cctype>
|
2020-04-05 20:35:51 +02:00
|
|
|
|
2020-04-05 23:53:44 +02:00
|
|
|
#include "StringUtil.hh"
|
|
|
|
|
#include "ParseBus.hh"
|
2018-09-28 17:54:21 +02:00
|
|
|
|
|
|
|
|
namespace sta {
|
|
|
|
|
|
2025-01-22 02:35:21 +01:00
|
|
|
constexpr char verilog_escape = '\\';
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
static string
|
2025-01-22 02:35:21 +01:00
|
|
|
staToVerilog(const char *sta_name);
|
2023-03-26 15:34:36 +02:00
|
|
|
static string
|
2025-01-22 02:35:21 +01:00
|
|
|
staToVerilog2(const char *sta_name);
|
2023-03-26 15:34:36 +02:00
|
|
|
static string
|
2025-01-22 02:35:21 +01:00
|
|
|
verilogToSta(const string *verilog_name);
|
2023-02-19 01:20:40 +01:00
|
|
|
|
2025-01-21 19:00:11 +01:00
|
|
|
string
|
|
|
|
|
cellVerilogName(const char *sta_name)
|
|
|
|
|
{
|
2025-01-22 02:35:21 +01:00
|
|
|
return staToVerilog(sta_name);
|
2025-01-21 19:00:11 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
string
|
2025-01-22 02:35:21 +01:00
|
|
|
instanceVerilogName(const char *sta_name)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2025-01-22 02:35:21 +01:00
|
|
|
return staToVerilog(sta_name);
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
string
|
2025-01-22 02:35:21 +01:00
|
|
|
netVerilogName(const char *sta_name)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2023-03-26 15:34:36 +02:00
|
|
|
bool is_bus;
|
|
|
|
|
string bus_name;
|
2019-07-04 06:18:38 +02:00
|
|
|
int index;
|
2025-01-22 02:35:21 +01:00
|
|
|
parseBusName(sta_name, '[', ']', verilog_escape, is_bus, bus_name, index);
|
2023-03-26 15:34:36 +02:00
|
|
|
if (is_bus) {
|
2025-01-22 02:35:21 +01:00
|
|
|
string bus_vname = staToVerilog(bus_name.c_str());
|
2023-03-26 15:34:36 +02:00
|
|
|
string vname;
|
|
|
|
|
stringPrint(vname, "%s[%d]", bus_vname.c_str(), index);
|
2019-08-13 07:56:32 +02:00
|
|
|
return vname;
|
|
|
|
|
}
|
2019-07-04 06:18:38 +02:00
|
|
|
else
|
2025-01-22 02:35:21 +01:00
|
|
|
return staToVerilog2(sta_name);
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
string
|
2025-01-22 02:35:21 +01:00
|
|
|
portVerilogName(const char *sta_name)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2025-01-22 02:35:21 +01:00
|
|
|
return staToVerilog2(sta_name);
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
static string
|
2025-01-22 02:35:21 +01:00
|
|
|
staToVerilog(const char *sta_name)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2023-02-19 01:20:40 +01:00
|
|
|
// Leave room for leading escape and trailing space if the name
|
|
|
|
|
// needs to be escaped.
|
|
|
|
|
// Assume the name has to be escaped and start copying while scanning.
|
2023-03-26 15:34:36 +02:00
|
|
|
string escaped_name = "\\";
|
2023-02-19 01:20:40 +01:00
|
|
|
bool escaped = false;
|
|
|
|
|
for (const char *s = sta_name; *s ; s++) {
|
|
|
|
|
char ch = s[0];
|
2025-01-22 02:35:21 +01:00
|
|
|
if (ch == verilog_escape) {
|
2023-02-19 01:20:40 +01:00
|
|
|
char next_ch = s[1];
|
2025-01-22 02:35:21 +01:00
|
|
|
if (next_ch == verilog_escape) {
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped_name += ch;
|
|
|
|
|
escaped_name += next_ch;
|
2023-02-19 01:20:40 +01:00
|
|
|
s++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// Skip escape.
|
|
|
|
|
escaped = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if ((!(isalnum(ch) || ch == '_')))
|
|
|
|
|
escaped = true;
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped_name += ch;
|
2023-02-19 01:20:40 +01:00
|
|
|
}
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
2023-02-19 01:20:40 +01:00
|
|
|
if (escaped) {
|
|
|
|
|
// Add a terminating space.
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped_name += ' ';
|
|
|
|
|
return escaped_name;
|
2023-02-19 01:20:40 +01:00
|
|
|
}
|
|
|
|
|
else
|
2023-03-26 15:34:36 +02:00
|
|
|
return string(sta_name);
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
static string
|
2025-01-22 02:35:21 +01:00
|
|
|
staToVerilog2(const char *sta_name)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2023-02-19 01:20:40 +01:00
|
|
|
constexpr char bus_brkt_left = '[';
|
|
|
|
|
constexpr char bus_brkt_right = ']';
|
2018-09-28 17:54:21 +02:00
|
|
|
// Leave room for leading escape and trailing space if the name
|
|
|
|
|
// needs to be escaped.
|
2023-03-26 15:34:36 +02:00
|
|
|
string escaped_name = "\\";
|
2018-09-28 17:54:21 +02:00
|
|
|
// Assume the name has to be escaped and start copying while scanning.
|
|
|
|
|
bool escaped = false;
|
|
|
|
|
for (const char *s = sta_name; *s ; s++) {
|
|
|
|
|
char ch = s[0];
|
2025-01-22 02:35:21 +01:00
|
|
|
if (ch == verilog_escape) {
|
2018-09-28 17:54:21 +02:00
|
|
|
char next_ch = s[1];
|
2025-01-22 02:35:21 +01:00
|
|
|
if (next_ch == verilog_escape) {
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped_name += ch;
|
|
|
|
|
escaped_name += next_ch;
|
2018-09-28 17:54:21 +02:00
|
|
|
s++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// Skip escape.
|
|
|
|
|
escaped = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
bool is_brkt = (ch == bus_brkt_left || ch == bus_brkt_right);
|
|
|
|
|
if ((!(isalnum(ch) || ch == '_') && !is_brkt)
|
2019-07-04 06:18:38 +02:00
|
|
|
|| is_brkt)
|
2018-09-28 17:54:21 +02:00
|
|
|
escaped = true;
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped_name += ch;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (escaped) {
|
|
|
|
|
// Add a terminating space.
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped_name += ' ';
|
|
|
|
|
return escaped_name;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
else
|
2023-03-26 15:34:36 +02:00
|
|
|
return string(sta_name);
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-19 01:20:40 +01:00
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
string
|
2025-01-22 02:35:21 +01:00
|
|
|
moduleVerilogToSta(const string *module_name)
|
2023-02-19 01:20:40 +01:00
|
|
|
{
|
|
|
|
|
return verilogToSta(module_name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
string
|
2025-01-22 02:35:21 +01:00
|
|
|
instanceVerilogToSta(const string *inst_name)
|
2023-02-19 01:20:40 +01:00
|
|
|
{
|
|
|
|
|
return verilogToSta(inst_name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
string
|
2025-01-22 02:35:21 +01:00
|
|
|
netVerilogToSta(const string *net_name)
|
2023-02-19 01:20:40 +01:00
|
|
|
{
|
|
|
|
|
return verilogToSta(net_name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
string
|
2025-01-22 02:35:21 +01:00
|
|
|
portVerilogToSta(const string *port_name)
|
2023-02-19 01:20:40 +01:00
|
|
|
{
|
|
|
|
|
return verilogToSta(port_name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:34:36 +02:00
|
|
|
static string
|
2025-01-22 02:35:21 +01:00
|
|
|
verilogToSta(const string *verilog_name)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2025-01-22 02:35:21 +01:00
|
|
|
if (verilog_name->front() == '\\') {
|
2023-02-19 01:20:40 +01:00
|
|
|
constexpr char divider = '/';
|
|
|
|
|
constexpr char bus_brkt_left = '[';
|
|
|
|
|
constexpr char bus_brkt_right = ']';
|
|
|
|
|
|
2025-01-22 02:35:21 +01:00
|
|
|
size_t verilog_name_length = verilog_name->size();
|
|
|
|
|
if (isspace(verilog_name->back()))
|
2023-02-19 01:20:40 +01:00
|
|
|
verilog_name_length--;
|
2023-03-26 15:34:36 +02:00
|
|
|
string sta_name;
|
2025-01-22 02:35:21 +01:00
|
|
|
// Ignore leading '\'.
|
|
|
|
|
for (size_t i = 1; i < verilog_name_length; i++) {
|
|
|
|
|
char ch = verilog_name->at(i);
|
2023-02-19 01:20:40 +01:00
|
|
|
if (ch == bus_brkt_left
|
|
|
|
|
|| ch == bus_brkt_right
|
|
|
|
|
|| ch == divider
|
2025-01-22 02:35:21 +01:00
|
|
|
|| ch == verilog_escape)
|
2023-02-19 01:20:40 +01:00
|
|
|
// Escape bus brackets, dividers and escapes.
|
2025-01-22 02:35:21 +01:00
|
|
|
sta_name += verilog_escape;
|
2023-03-26 15:34:36 +02:00
|
|
|
sta_name += ch;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
return sta_name;
|
|
|
|
|
}
|
|
|
|
|
else
|
2025-01-22 02:35:21 +01:00
|
|
|
return string(*verilog_name);
|
2023-02-19 01:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-28 17:54:21 +02:00
|
|
|
} // namespace
|