OpenSTA/liberty/LibertyExpr.cc

184 lines
3.8 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
2020-03-07 03:50:37 +01:00
// Copyright (c) 2020, 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
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-04-05 23:53:44 +02:00
#include "FuncExpr.hh"
2020-04-05 20:35:51 +02:00
2020-07-07 03:35:36 +02:00
#include <algorithm> // min
2020-04-05 23:53:44 +02:00
#include "Report.hh"
#include "StringUtil.hh"
#include "Liberty.hh"
#include "LibertyExprPvt.hh"
2018-09-28 17:54:21 +02:00
extern int
LibertyExprParse_parse();
namespace sta {
LibExprParser *libexpr_parser;
FuncExpr *
parseFuncExpr(const char *func,
LibertyCell *cell,
const char *error_msg,
2018-09-28 17:54:21 +02:00
Report *report)
{
2019-03-13 01:25:53 +01:00
if (func != nullptr && func[0] != '\0') {
2018-09-28 17:54:21 +02:00
LibExprParser parser(func, cell, error_msg, report);
libexpr_parser = &parser;
LibertyExprParse_parse();
return parser.result();
}
else
2019-03-13 01:25:53 +01:00
return nullptr;
2018-09-28 17:54:21 +02:00
}
LibExprParser::LibExprParser(const char *func,
LibertyCell *cell,
const char *error_msg,
Report *report) :
2018-09-28 17:54:21 +02:00
func_(func),
cell_(cell),
error_msg_(error_msg),
report_(report),
2019-03-13 01:25:53 +01:00
result_(nullptr),
2018-09-28 17:54:21 +02:00
token_length_(100),
token_(new char[token_length_]),
token_next_(token_)
{
}
LibExprParser::~LibExprParser()
{
stringDelete(token_);
}
FuncExpr *
LibExprParser::makeFuncExprPort(const char *port_name)
{
LibertyPort *port = cell_->findLibertyPort(port_name);
2019-03-13 01:25:53 +01:00
FuncExpr *expr = nullptr;
2018-09-28 17:54:21 +02:00
if (port)
expr = FuncExpr::makePort(port);
else
2020-12-14 02:21:35 +01:00
report_->error(7, "%s references unknown port %s.",
2018-09-28 17:54:21 +02:00
error_msg_, port_name);
stringDelete(port_name);
return expr;
}
FuncExpr *
LibExprParser::makeFuncExprNot(FuncExpr *arg)
{
if (arg)
return FuncExpr::makeNot(arg);
else
2019-03-13 01:25:53 +01:00
return nullptr;
2018-09-28 17:54:21 +02:00
}
FuncExpr *
LibExprParser::makeFuncExprXor(FuncExpr *arg1,
FuncExpr *arg2)
2018-09-28 17:54:21 +02:00
{
if (arg1 && arg2)
return FuncExpr::makeXor(arg1, arg2);
else
2019-03-13 01:25:53 +01:00
return nullptr;
2018-09-28 17:54:21 +02:00
}
FuncExpr *
LibExprParser::makeFuncExprAnd(FuncExpr *arg1,
FuncExpr *arg2)
2018-09-28 17:54:21 +02:00
{
if (arg1 && arg2)
return FuncExpr::makeAnd(arg1, arg2);
else
2019-03-13 01:25:53 +01:00
return nullptr;
2018-09-28 17:54:21 +02:00
}
FuncExpr *
LibExprParser::makeFuncExprOr(FuncExpr *arg1,
FuncExpr *arg2)
2018-09-28 17:54:21 +02:00
{
if (arg1 && arg2)
return FuncExpr::makeOr(arg1, arg2);
else
2019-03-13 01:25:53 +01:00
return nullptr;
2018-09-28 17:54:21 +02:00
}
void
LibExprParser::setResult(FuncExpr *result)
{
result_ = result;
}
size_t
LibExprParser::copyInput(char *buf,
size_t max_size)
2018-09-28 17:54:21 +02:00
{
2020-09-26 01:09:26 +02:00
strncpy(buf, func_, max_size);
int count = strlen(buf);
func_ += count;
return count;
2018-09-28 17:54:21 +02:00
}
char *
LibExprParser::tokenCopy()
{
return stringCopy(token_);
}
void
LibExprParser::tokenErase()
{
token_next_ = token_;
}
void
LibExprParser::tokenAppend(char ch)
{
if (token_next_ + 1 - token_ >= static_cast<signed int>(token_length_)) {
size_t index = token_next_ - token_;
token_length_ *= 2;
char *prev_token = token_;
token_ = new char[token_length_];
strcpy(token_, prev_token);
stringDelete(prev_token);
token_next_ = &token_[index];
}
*token_next_++ = ch;
// Make sure the token is always terminated.
*token_next_ = '\0';
}
void
LibExprParser::parseError(const char *msg)
{
2020-12-14 02:21:35 +01:00
report_->error(206, "%s %s.", error_msg_, msg);
2018-09-28 17:54:21 +02:00
}
} // namespace
////////////////////////////////////////////////////////////////
// Global namespace
int
LibertyExprParse_error(const char *msg)
{
sta::libexpr_parser->parseError(msg);
libertyExprFlushBuffer();
return 0;
}