2018-09-28 17:54:21 +02:00
|
|
|
// OpenSTA, Static Timing Analyzer
|
2026-03-10 21:21:17 +01:00
|
|
|
// Copyright (c) 2026, 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 "ParseBus.hh"
|
2020-04-05 20:35:51 +02:00
|
|
|
|
2018-09-28 17:54:21 +02:00
|
|
|
#include <string>
|
2026-03-15 22:35:24 +01:00
|
|
|
#include <string_view>
|
2020-04-05 20:35:51 +02:00
|
|
|
|
2020-04-05 23:53:44 +02:00
|
|
|
#include "StringUtil.hh"
|
2018-09-28 17:54:21 +02:00
|
|
|
|
|
|
|
|
namespace sta {
|
|
|
|
|
|
|
|
|
|
bool
|
2026-03-15 22:35:24 +01:00
|
|
|
isBusName(std::string_view name,
|
2026-01-04 01:59:35 +01:00
|
|
|
const char brkt_left,
|
|
|
|
|
const char brkt_right,
|
|
|
|
|
char escape)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2026-03-15 22:35:24 +01:00
|
|
|
size_t len = name.size();
|
2019-08-14 06:34:35 +02:00
|
|
|
// Shortest bus name is a[0].
|
|
|
|
|
if (len >= 4
|
|
|
|
|
// Escaped bus brackets are not buses.
|
|
|
|
|
&& name[len - 2] != escape
|
|
|
|
|
&& name[len - 1] == brkt_right) {
|
2026-03-15 22:35:24 +01:00
|
|
|
size_t left = name.rfind(brkt_left);
|
|
|
|
|
return left != std::string_view::npos;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2026-03-15 22:35:24 +01:00
|
|
|
parseBusName(std::string_view name,
|
2026-01-04 01:59:35 +01:00
|
|
|
const char brkt_left,
|
|
|
|
|
const char brkt_right,
|
|
|
|
|
const char escape,
|
|
|
|
|
// Return values.
|
|
|
|
|
bool &is_bus,
|
2026-03-02 21:13:13 +01:00
|
|
|
std::string &bus_name,
|
2026-01-04 01:59:35 +01:00
|
|
|
int &index)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2026-03-15 22:35:24 +01:00
|
|
|
parseBusName(name, std::string_view(&brkt_left, 1),
|
|
|
|
|
std::string_view(&brkt_right, 1), escape,
|
2023-03-26 15:34:36 +02:00
|
|
|
is_bus, bus_name, index);
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2026-03-15 22:35:24 +01:00
|
|
|
parseBusName(std::string_view name,
|
|
|
|
|
std::string_view brkts_left,
|
|
|
|
|
std::string_view brkts_right,
|
2026-01-04 01:59:35 +01:00
|
|
|
char escape,
|
|
|
|
|
// Return values.
|
|
|
|
|
bool &is_bus,
|
2026-03-02 21:13:13 +01:00
|
|
|
std::string &bus_name,
|
2026-01-04 01:59:35 +01:00
|
|
|
int &index)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2023-03-26 15:34:36 +02:00
|
|
|
is_bus = false;
|
2026-03-15 22:35:24 +01:00
|
|
|
size_t len = name.size();
|
2019-08-14 06:34:35 +02:00
|
|
|
// Shortest bus name is a[0].
|
|
|
|
|
if (len >= 4
|
|
|
|
|
// Escaped bus brackets are not buses.
|
|
|
|
|
&& name[len - 2] != escape) {
|
|
|
|
|
char last_ch = name[len - 1];
|
2026-03-15 22:35:24 +01:00
|
|
|
size_t brkt_index = brkts_right.find(last_ch);
|
|
|
|
|
if (brkt_index != std::string_view::npos) {
|
|
|
|
|
char brkt_left_ch = brkts_left[brkt_index];
|
|
|
|
|
size_t left = name.rfind(brkt_left_ch);
|
|
|
|
|
if (left != std::string_view::npos) {
|
2023-03-26 15:34:36 +02:00
|
|
|
is_bus = true;
|
2026-03-15 22:35:24 +01:00
|
|
|
bus_name.append(name.data(), left);
|
2026-01-04 01:59:35 +01:00
|
|
|
// Simple bus subscript.
|
2026-03-15 22:35:24 +01:00
|
|
|
index = std::stoi(std::string(name.substr(left + 1)));
|
2019-08-14 06:34:35 +02:00
|
|
|
}
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2026-03-15 22:35:24 +01:00
|
|
|
parseBusName(std::string_view name,
|
2023-06-04 18:34:29 +02:00
|
|
|
const char brkt_left,
|
|
|
|
|
const char brkt_right,
|
|
|
|
|
char escape,
|
|
|
|
|
// Return values.
|
|
|
|
|
bool &is_bus,
|
|
|
|
|
bool &is_range,
|
2026-03-02 21:13:13 +01:00
|
|
|
std::string &bus_name,
|
2023-06-04 18:34:29 +02:00
|
|
|
int &from,
|
|
|
|
|
int &to,
|
|
|
|
|
bool &subscript_wild)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2026-03-15 22:35:24 +01:00
|
|
|
parseBusName(name, std::string_view(&brkt_left, 1),
|
|
|
|
|
std::string_view(&brkt_right, 1), escape,
|
2023-06-04 18:34:29 +02:00
|
|
|
is_bus, is_range, bus_name, from, to, subscript_wild);
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2026-03-15 22:35:24 +01:00
|
|
|
parseBusName(std::string_view name,
|
|
|
|
|
std::string_view brkts_left,
|
|
|
|
|
std::string_view brkts_right,
|
2023-06-04 18:34:29 +02:00
|
|
|
char escape,
|
|
|
|
|
// Return values.
|
|
|
|
|
bool &is_bus,
|
|
|
|
|
bool &is_range,
|
2026-03-02 21:13:13 +01:00
|
|
|
std::string &bus_name,
|
2023-06-04 18:34:29 +02:00
|
|
|
int &from,
|
|
|
|
|
int &to,
|
|
|
|
|
bool &subscript_wild)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2023-03-26 15:34:36 +02:00
|
|
|
is_bus = false;
|
2023-06-04 18:34:29 +02:00
|
|
|
is_range = false;
|
|
|
|
|
subscript_wild = false;
|
2026-03-15 22:35:24 +01:00
|
|
|
size_t len = name.size();
|
2023-06-04 18:34:29 +02:00
|
|
|
// Shortest bus is a[0].
|
|
|
|
|
if (len >= 4
|
2019-08-14 06:34:35 +02:00
|
|
|
// Escaped bus brackets are not buses.
|
|
|
|
|
&& name[len - 2] != escape) {
|
|
|
|
|
char last_ch = name[len - 1];
|
2026-03-15 22:35:24 +01:00
|
|
|
size_t brkt_index = brkts_right.find(last_ch);
|
|
|
|
|
if (brkt_index != std::string_view::npos) {
|
|
|
|
|
char brkt_left_ch = brkts_left[brkt_index];
|
|
|
|
|
size_t left = name.rfind(brkt_left_ch);
|
|
|
|
|
if (left != std::string_view::npos) {
|
2023-06-04 18:34:29 +02:00
|
|
|
is_bus = true;
|
2026-03-15 22:35:24 +01:00
|
|
|
bus_name.append(name.data(), left);
|
2026-01-04 01:59:35 +01:00
|
|
|
// Check for bus range.
|
2026-03-15 22:35:24 +01:00
|
|
|
size_t range = name.find(':', left);
|
|
|
|
|
if (range != std::string_view::npos) {
|
2023-06-04 18:34:29 +02:00
|
|
|
is_range = true;
|
2026-03-15 22:35:24 +01:00
|
|
|
from = std::stoi(std::string(name.substr(left + 1)));
|
|
|
|
|
to = std::stoi(std::string(name.substr(range + 1)));
|
2026-01-04 01:59:35 +01:00
|
|
|
}
|
2023-06-04 18:34:29 +02:00
|
|
|
else {
|
2026-03-15 22:35:24 +01:00
|
|
|
if (left + 1 < len && name[left + 1] == '*')
|
2023-06-04 18:34:29 +02:00
|
|
|
subscript_wild = true;
|
|
|
|
|
else
|
2026-03-15 22:35:24 +01:00
|
|
|
from = to = std::stoi(std::string(name.substr(left + 1)));
|
2023-06-04 18:34:29 +02:00
|
|
|
}
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 21:13:13 +01:00
|
|
|
std::string
|
2026-03-15 22:35:24 +01:00
|
|
|
escapeChars(std::string_view token,
|
2026-01-04 01:59:35 +01:00
|
|
|
const char ch1,
|
|
|
|
|
const char ch2,
|
|
|
|
|
const char escape)
|
2018-09-28 17:54:21 +02:00
|
|
|
{
|
2026-03-02 21:13:13 +01:00
|
|
|
std::string escaped;
|
2026-03-15 22:35:24 +01:00
|
|
|
escaped.reserve(token.size());
|
|
|
|
|
for (size_t i = 0; i < token.size(); i++) {
|
|
|
|
|
char ch = token[i];
|
2018-09-28 17:54:21 +02:00
|
|
|
if (ch == escape) {
|
2026-03-15 22:35:24 +01:00
|
|
|
if (i + 1 < token.size()) {
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped += ch;
|
2026-03-15 22:35:24 +01:00
|
|
|
escaped += token[i + 1];
|
|
|
|
|
i++;
|
2023-03-26 15:34:36 +02:00
|
|
|
}
|
2026-03-15 22:35:24 +01:00
|
|
|
else
|
|
|
|
|
escaped += ch;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
else if (ch == ch1 || ch == ch2) {
|
2023-03-26 15:34:36 +02:00
|
|
|
escaped += escape;
|
|
|
|
|
escaped += ch;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
2023-03-26 15:34:36 +02:00
|
|
|
else
|
|
|
|
|
escaped += ch;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
2023-03-26 15:34:36 +02:00
|
|
|
return escaped;
|
2018-09-28 17:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|