solidity/libyul/AsmPrinter.cpp

321 lines
9.2 KiB
C++
Raw Normal View History

2017-02-09 13:04:23 +00:00
/*
This file is part of solidity.
solidity 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.
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
2017-02-09 13:04:23 +00:00
/**
* @author Christian <c@ethdev.com>
* @date 2017
* Converts a parsed assembly into its textual form.
*/
#include <libyul/AsmPrinter.h>
#include <libyul/AST.h>
#include <libyul/Exceptions.h>
#include <libyul/Dialect.h>
2017-02-09 13:04:23 +00:00
#include <libsolutil/CommonData.h>
#include <libsolutil/StringUtils.h>
2017-02-09 13:04:23 +00:00
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
2021-04-08 12:08:14 +00:00
#include <range/v3/view/transform.hpp>
2017-02-09 13:04:23 +00:00
#include <memory>
#include <functional>
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::langutil;
2019-12-11 16:31:36 +00:00
using namespace solidity::util;
using namespace solidity::yul;
2017-02-09 13:04:23 +00:00
string AsmPrinter::operator()(Literal const& _literal)
2017-02-09 13:04:23 +00:00
{
string const locationComment = formatSourceLocationComment(_literal);
switch (_literal.kind)
{
case LiteralKind::Number:
yulAssert(isValidDecimal(_literal.value.str()) || isValidHex(_literal.value.str()), "Invalid number literal");
return locationComment + _literal.value.str() + appendTypeName(_literal.type);
case LiteralKind::Boolean:
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal.");
return locationComment + ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type, true);
case LiteralKind::String:
break;
}
return locationComment + escapeAndQuoteString(_literal.value.str()) + appendTypeName(_literal.type);
2017-02-09 13:04:23 +00:00
}
string AsmPrinter::operator()(Identifier const& _identifier)
2017-02-09 13:04:23 +00:00
{
yulAssert(!_identifier.name.empty(), "Invalid identifier.");
return formatSourceLocationComment(_identifier) + _identifier.name.str();
2017-02-09 13:04:23 +00:00
}
string AsmPrinter::operator()(ExpressionStatement const& _statement)
2017-12-08 13:01:22 +00:00
{
string const locationComment = formatSourceLocationComment(_statement);
return locationComment + std::visit(*this, _statement.expression);
2017-12-08 13:01:22 +00:00
}
string AsmPrinter::operator()(Assignment const& _assignment)
2017-02-09 13:04:23 +00:00
{
string const locationComment = formatSourceLocationComment(_assignment);
yulAssert(_assignment.variableNames.size() >= 1, "");
string variables = (*this)(_assignment.variableNames.front());
for (size_t i = 1; i < _assignment.variableNames.size(); ++i)
variables += ", " + (*this)(_assignment.variableNames[i]);
return locationComment + variables + " := " + std::visit(*this, *_assignment.value);
2017-02-09 13:04:23 +00:00
}
string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration)
2017-02-09 13:04:23 +00:00
{
string out = formatSourceLocationComment(_variableDeclaration);
out += "let ";
out += boost::algorithm::join(
2021-04-08 12:08:14 +00:00
_variableDeclaration.variables | ranges::views::transform(
[this](TypedName argument) { return formatTypedName(argument); }
),
", "
);
if (_variableDeclaration.value)
{
out += " := ";
out += std::visit(*this, *_variableDeclaration.value);
}
return out;
2017-02-09 13:04:23 +00:00
}
string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition)
2017-02-09 13:04:23 +00:00
{
yulAssert(!_functionDefinition.name.empty(), "Invalid function name.");
string out = formatSourceLocationComment(_functionDefinition);
out += "function " + _functionDefinition.name.str() + "(";
2017-05-03 18:32:38 +00:00
out += boost::algorithm::join(
2021-04-08 12:08:14 +00:00
_functionDefinition.parameters | ranges::views::transform(
[this](TypedName argument) { return formatTypedName(argument); }
2017-05-03 18:32:38 +00:00
),
", "
);
2017-04-26 22:58:34 +00:00
out += ")";
if (!_functionDefinition.returnVariables.empty())
2017-04-26 22:58:34 +00:00
{
out += " -> ";
2017-05-03 18:32:38 +00:00
out += boost::algorithm::join(
2021-04-08 12:08:14 +00:00
_functionDefinition.returnVariables | ranges::views::transform(
[this](TypedName argument) { return formatTypedName(argument); }
2017-05-03 18:32:38 +00:00
),
", "
);
2017-04-26 22:58:34 +00:00
}
2017-02-09 13:04:23 +00:00
return out + "\n" + (*this)(_functionDefinition.body);
}
string AsmPrinter::operator()(FunctionCall const& _functionCall)
2017-02-09 13:04:23 +00:00
{
string const locationComment = formatSourceLocationComment(_functionCall);
string const functionName = (*this)(_functionCall.functionName);
2017-02-09 13:04:23 +00:00
return
locationComment +
functionName + "(" +
2017-02-09 13:04:23 +00:00
boost::algorithm::join(
2021-04-08 12:08:14 +00:00
_functionCall.arguments | ranges::views::transform([&](auto&& _node) { return std::visit(*this, _node); }),
2017-02-09 13:04:23 +00:00
", " ) +
")";
}
string AsmPrinter::operator()(If const& _if)
{
yulAssert(_if.condition, "Invalid if condition.");
string out = formatSourceLocationComment(_if);
out += "if " + std::visit(*this, *_if.condition);
string body = (*this)(_if.body);
char delim = '\n';
if (body.find('\n') == string::npos)
delim = ' ';
return out + delim + body;
}
string AsmPrinter::operator()(Switch const& _switch)
{
yulAssert(_switch.expression, "Invalid expression pointer.");
string out = formatSourceLocationComment(_switch);
out += "switch " + std::visit(*this, *_switch.expression);
for (auto const& _case: _switch.cases)
{
2017-05-17 10:21:37 +00:00
if (!_case.value)
2017-05-19 16:54:55 +00:00
out += "\ndefault ";
else
2017-05-19 16:54:55 +00:00
out += "\ncase " + (*this)(*_case.value) + " ";
out += (*this)(_case.body);
}
return out;
}
string AsmPrinter::operator()(ForLoop const& _forLoop)
{
yulAssert(_forLoop.condition, "Invalid for loop condition.");
string const locationComment = formatSourceLocationComment(_forLoop);
string pre = (*this)(_forLoop.pre);
string condition = std::visit(*this, *_forLoop.condition);
string post = (*this)(_forLoop.post);
char delim = '\n';
if (
pre.size() + condition.size() + post.size() < 60 &&
pre.find('\n') == string::npos &&
post.find('\n') == string::npos
)
delim = ' ';
return
locationComment +
("for " + move(pre) + delim + move(condition) + delim + move(post) + "\n") +
(*this)(_forLoop.body);
}
string AsmPrinter::operator()(Break const& _break)
{
return formatSourceLocationComment(_break) + "break";
}
string AsmPrinter::operator()(Continue const& _continue)
{
return formatSourceLocationComment(_continue) + "continue";
}
// '_leave' and '__leave' is reserved in VisualStudio
string AsmPrinter::operator()(Leave const& leave_)
2019-10-28 14:25:02 +00:00
{
return formatSourceLocationComment(leave_) + "leave";
2019-10-28 14:25:02 +00:00
}
string AsmPrinter::operator()(Block const& _block)
2017-02-09 13:04:23 +00:00
{
string const locationComment = formatSourceLocationComment(_block);
2017-02-14 14:41:07 +00:00
if (_block.statements.empty())
return locationComment + "{ }";
2017-02-09 13:04:23 +00:00
string body = boost::algorithm::join(
2021-04-08 12:08:14 +00:00
_block.statements | ranges::views::transform([&](auto&& _node) { return std::visit(*this, _node); }),
2017-02-09 13:04:23 +00:00
"\n"
);
if (body.size() < 30 && body.find('\n') == string::npos)
return locationComment + "{ " + body + " }";
else
{
boost::replace_all(body, "\n", "\n ");
return locationComment + "{\n " + body + "\n}";
}
2017-02-09 13:04:23 +00:00
}
2017-04-26 22:58:34 +00:00
string AsmPrinter::formatTypedName(TypedName _variable)
{
yulAssert(!_variable.name.empty(), "Invalid variable name.");
return formatSourceLocationComment(_variable) + _variable.name.str() + appendTypeName(_variable.type);
}
string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
2017-04-26 22:58:34 +00:00
{
if (m_dialect && !_type.empty())
{
if (!_isBoolLiteral && _type == m_dialect->defaultType)
_type = {};
else if (_isBoolLiteral && _type == m_dialect->boolType && !m_dialect->defaultType.empty())
// Special case: If we have a bool type but empty default type, do not remove the type.
_type = {};
}
if (_type.empty())
return {};
else
return ":" + _type.str();
2017-04-26 22:58:34 +00:00
}
string AsmPrinter::formatSourceLocationComment(
SourceLocation const& _location,
map<string, unsigned> const& _nameToSourceIndex,
bool _statement,
CharStreamProvider const* _soliditySourceProvider
)
{
yulAssert(!_nameToSourceIndex.empty(), "");
string sourceIndex = "-1";
string solidityCodeSnippet = "";
if (_location.sourceName)
{
sourceIndex = to_string(_nameToSourceIndex.at(*_location.sourceName));
if (_soliditySourceProvider)
{
solidityCodeSnippet = escapeAndQuoteString(
_soliditySourceProvider->charStream(*_location.sourceName).singleLineSnippet(_location)
);
// On top of escaping quotes we also escape the slash inside any `*/` to guard against
// it prematurely terminating multi-line comment blocks. We do not escape all slashes
// because the ones without `*` are not dangerous and ignoring them reduces visual noise.
boost::replace_all(solidityCodeSnippet, "*/", "*\\/");
}
}
string sourceLocation =
"@src " +
sourceIndex +
":" +
to_string(_location.start) +
":" +
to_string(_location.end);
return
_statement ?
"/// " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") :
"/** " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") + " */ ";
}
string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const& _debugData, bool _statement)
{
if (
!_debugData ||
m_lastLocation == _debugData->location ||
m_nameToSourceIndex.empty()
)
return "";
m_lastLocation = _debugData->location;
return formatSourceLocationComment(
_debugData->location,
m_nameToSourceIndex,
_statement,
m_soliditySourceProvider
) + (_statement ? "\n" : "");
}