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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// 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.
|
|
|
|
*/
|
|
|
|
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmPrinter.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-11-26 21:52:09 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2020-01-16 11:03:19 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2017-02-09 13:04:23 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2021-08-31 13:10:40 +00:00
|
|
|
#include <libsolutil/StringUtils.h>
|
2018-10-16 15:58:17 +00:00
|
|
|
|
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;
|
2021-08-31 13:08:16 +00:00
|
|
|
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
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(Literal const& _literal)
|
2017-02-09 13:04:23 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
string const locationComment = formatSourceLocationComment(_literal);
|
|
|
|
|
2017-05-02 17:26:33 +00:00
|
|
|
switch (_literal.kind)
|
|
|
|
{
|
|
|
|
case LiteralKind::Number:
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(isValidDecimal(_literal.value.str()) || isValidHex(_literal.value.str()), "Invalid number literal");
|
2021-07-13 14:06:07 +00:00
|
|
|
return locationComment + _literal.value.str() + appendTypeName(_literal.type);
|
2017-05-02 17:26:33 +00:00
|
|
|
case LiteralKind::Boolean:
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal.");
|
2021-07-13 14:06:07 +00:00
|
|
|
return locationComment + ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type, true);
|
2017-05-02 17:26:33 +00:00
|
|
|
case LiteralKind::String:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
return locationComment + escapeAndQuoteString(_literal.value.str()) + appendTypeName(_literal.type);
|
2017-02-09 13:04:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(Identifier const& _identifier)
|
2017-02-09 13:04:23 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_identifier.name.empty(), "Invalid identifier.");
|
2021-07-13 14:06:07 +00:00
|
|
|
return formatSourceLocationComment(_identifier) + _identifier.name.str();
|
2017-02-09 13:04:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(ExpressionStatement const& _statement)
|
2017-12-08 13:01:22 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
string const locationComment = formatSourceLocationComment(_statement);
|
|
|
|
|
|
|
|
return locationComment + std::visit(*this, _statement.expression);
|
2017-12-08 13:01:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(Assignment const& _assignment)
|
2017-02-09 13:04:23 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
string const locationComment = formatSourceLocationComment(_assignment);
|
|
|
|
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_assignment.variableNames.size() >= 1, "");
|
2017-05-19 16:06:26 +00:00
|
|
|
string variables = (*this)(_assignment.variableNames.front());
|
|
|
|
for (size_t i = 1; i < _assignment.variableNames.size(); ++i)
|
|
|
|
variables += ", " + (*this)(_assignment.variableNames[i]);
|
2021-07-13 14:06:07 +00:00
|
|
|
|
|
|
|
return locationComment + variables + " := " + std::visit(*this, *_assignment.value);
|
2017-02-09 13:04:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration)
|
2017-02-09 13:04:23 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
string out = formatSourceLocationComment(_variableDeclaration);
|
|
|
|
|
|
|
|
out += "let ";
|
2017-05-05 17:35:40 +00:00
|
|
|
out += boost::algorithm::join(
|
2021-04-08 12:08:14 +00:00
|
|
|
_variableDeclaration.variables | ranges::views::transform(
|
2018-10-16 15:58:17 +00:00
|
|
|
[this](TypedName argument) { return formatTypedName(argument); }
|
2017-05-05 17:35:40 +00:00
|
|
|
),
|
|
|
|
", "
|
|
|
|
);
|
2017-05-05 15:46:26 +00:00
|
|
|
if (_variableDeclaration.value)
|
|
|
|
{
|
|
|
|
out += " := ";
|
2019-11-19 15:42:49 +00:00
|
|
|
out += std::visit(*this, *_variableDeclaration.value);
|
2017-05-05 15:46:26 +00:00
|
|
|
}
|
2017-05-05 17:35:40 +00:00
|
|
|
return out;
|
2017-02-09 13:04:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition)
|
2017-02-09 13:04:23 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_functionDefinition.name.empty(), "Invalid function name.");
|
2021-07-13 14:06:07 +00:00
|
|
|
|
|
|
|
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(
|
2018-10-16 15:58:17 +00:00
|
|
|
[this](TypedName argument) { return formatTypedName(argument); }
|
2017-05-03 18:32:38 +00:00
|
|
|
),
|
|
|
|
", "
|
|
|
|
);
|
2017-04-26 22:58:34 +00:00
|
|
|
out += ")";
|
2017-12-01 13:10:49 +00:00
|
|
|
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(
|
2018-10-16 15:58:17 +00:00
|
|
|
[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);
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(FunctionCall const& _functionCall)
|
2017-02-09 13:04:23 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
string const locationComment = formatSourceLocationComment(_functionCall);
|
|
|
|
string const functionName = (*this)(_functionCall.functionName);
|
2017-02-09 13:04:23 +00:00
|
|
|
return
|
2021-07-13 14:06:07 +00:00
|
|
|
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
|
|
|
", " ) +
|
|
|
|
")";
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(If const& _if)
|
2017-11-21 12:36:41 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_if.condition, "Invalid if condition.");
|
2021-07-13 14:06:07 +00:00
|
|
|
|
|
|
|
string out = formatSourceLocationComment(_if);
|
|
|
|
out += "if " + std::visit(*this, *_if.condition);
|
|
|
|
|
2019-05-08 10:41:19 +00:00
|
|
|
string body = (*this)(_if.body);
|
|
|
|
char delim = '\n';
|
|
|
|
if (body.find('\n') == string::npos)
|
|
|
|
delim = ' ';
|
2021-07-13 14:06:07 +00:00
|
|
|
|
|
|
|
return out + delim + body;
|
2017-11-21 12:36:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(Switch const& _switch)
|
2017-04-28 13:27:56 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_switch.expression, "Invalid expression pointer.");
|
2021-07-13 14:06:07 +00:00
|
|
|
|
|
|
|
string out = formatSourceLocationComment(_switch);
|
|
|
|
out += "switch " + std::visit(*this, *_switch.expression);
|
|
|
|
|
2017-04-28 13:27:56 +00:00
|
|
|
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 ";
|
2017-04-28 13:27:56 +00:00
|
|
|
else
|
2017-05-19 16:54:55 +00:00
|
|
|
out += "\ncase " + (*this)(*_case.value) + " ";
|
2017-04-28 13:27:56 +00:00
|
|
|
out += (*this)(_case.body);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(ForLoop const& _forLoop)
|
2017-04-28 13:33:52 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_forLoop.condition, "Invalid for loop condition.");
|
2021-07-13 14:06:07 +00:00
|
|
|
string const locationComment = formatSourceLocationComment(_forLoop);
|
|
|
|
|
2019-05-08 10:41:19 +00:00
|
|
|
string pre = (*this)(_forLoop.pre);
|
2019-11-19 15:42:49 +00:00
|
|
|
string condition = std::visit(*this, *_forLoop.condition);
|
2019-05-08 10:41:19 +00:00
|
|
|
string post = (*this)(_forLoop.post);
|
2021-07-13 14:06:07 +00:00
|
|
|
|
2019-05-08 10:41:19 +00:00
|
|
|
char delim = '\n';
|
|
|
|
if (
|
|
|
|
pre.size() + condition.size() + post.size() < 60 &&
|
|
|
|
pre.find('\n') == string::npos &&
|
|
|
|
post.find('\n') == string::npos
|
|
|
|
)
|
|
|
|
delim = ' ';
|
|
|
|
return
|
2021-07-13 14:06:07 +00:00
|
|
|
locationComment +
|
2019-05-08 10:41:19 +00:00
|
|
|
("for " + move(pre) + delim + move(condition) + delim + move(post) + "\n") +
|
|
|
|
(*this)(_forLoop.body);
|
2017-04-28 13:33:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(Break const& _break)
|
2019-03-04 14:38:05 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
return formatSourceLocationComment(_break) + "break";
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(Continue const& _continue)
|
2019-03-04 14:38:05 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
return formatSourceLocationComment(_continue) + "continue";
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
// '_leave' and '__leave' is reserved in VisualStudio
|
|
|
|
string AsmPrinter::operator()(Leave const& leave_)
|
2019-10-28 14:25:02 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
return formatSourceLocationComment(leave_) + "leave";
|
2019-10-28 14:25:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::operator()(Block const& _block)
|
2017-02-09 13:04:23 +00:00
|
|
|
{
|
2021-07-13 14:06:07 +00:00
|
|
|
string const locationComment = formatSourceLocationComment(_block);
|
|
|
|
|
2017-02-14 14:41:07 +00:00
|
|
|
if (_block.statements.empty())
|
2021-07-13 14:06:07 +00:00
|
|
|
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"
|
|
|
|
);
|
2019-05-08 10:41:19 +00:00
|
|
|
if (body.size() < 30 && body.find('\n') == string::npos)
|
2021-07-13 14:06:07 +00:00
|
|
|
return locationComment + "{ " + body + " }";
|
2019-05-08 10:41:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
boost::replace_all(body, "\n", "\n ");
|
2021-07-13 14:06:07 +00:00
|
|
|
return locationComment + "{\n " + body + "\n}";
|
2019-05-08 10:41:19 +00:00
|
|
|
}
|
2017-02-09 13:04:23 +00:00
|
|
|
}
|
2017-04-26 22:58:34 +00:00
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string AsmPrinter::formatTypedName(TypedName _variable)
|
2018-10-16 15:58:17 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_variable.name.empty(), "Invalid variable name.");
|
2021-07-13 14:06:07 +00:00
|
|
|
return formatSourceLocationComment(_variable) + _variable.name.str() + appendTypeName(_variable.type);
|
2018-10-16 15:58:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 17:14:20 +00:00
|
|
|
string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
|
2017-04-26 22:58:34 +00:00
|
|
|
{
|
2020-01-29 17:14:20 +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())
|
2020-01-16 11:03:19 +00:00
|
|
|
return {};
|
|
|
|
else
|
2018-10-29 14:12:02 +00:00
|
|
|
return ":" + _type.str();
|
2017-04-26 22:58:34 +00:00
|
|
|
}
|
2021-07-13 14:06:07 +00:00
|
|
|
|
2021-08-31 13:08:16 +00:00
|
|
|
string AsmPrinter::formatSourceLocationComment(
|
|
|
|
SourceLocation const& _location,
|
|
|
|
map<string, unsigned> const& _nameToSourceIndex,
|
2021-08-31 13:10:40 +00:00
|
|
|
bool _statement,
|
|
|
|
CharStreamProvider const* _soliditySourceProvider
|
2021-08-31 13:08:16 +00:00
|
|
|
)
|
2021-07-13 14:06:07 +00:00
|
|
|
{
|
2021-08-31 13:08:16 +00:00
|
|
|
yulAssert(!_nameToSourceIndex.empty(), "");
|
2021-07-13 14:06:07 +00:00
|
|
|
|
|
|
|
string sourceIndex = "-1";
|
2021-08-31 13:10:40 +00:00
|
|
|
string solidityCodeSnippet = "";
|
2021-08-31 13:08:16 +00:00
|
|
|
if (_location.sourceName)
|
2021-08-31 13:10:40 +00:00
|
|
|
{
|
2021-08-31 13:08:16 +00:00
|
|
|
sourceIndex = to_string(_nameToSourceIndex.at(*_location.sourceName));
|
2021-07-13 14:06:07 +00:00
|
|
|
|
2021-08-31 13:10:40 +00:00
|
|
|
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, "*/", "*\\/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
string sourceLocation =
|
|
|
|
"@src " +
|
|
|
|
sourceIndex +
|
|
|
|
":" +
|
2021-08-31 13:08:16 +00:00
|
|
|
to_string(_location.start) +
|
2021-07-13 14:06:07 +00:00
|
|
|
":" +
|
2021-08-31 13:08:16 +00:00
|
|
|
to_string(_location.end);
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
return
|
|
|
|
_statement ?
|
2021-08-31 13:10:40 +00:00
|
|
|
"/// " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") :
|
|
|
|
"/** " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") + " */ ";
|
2021-07-13 14:06:07 +00:00
|
|
|
}
|
2021-08-31 13:08:16 +00:00
|
|
|
|
|
|
|
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,
|
2021-08-31 13:10:40 +00:00
|
|
|
_statement,
|
|
|
|
m_soliditySourceProvider
|
2021-08-31 13:08:16 +00:00
|
|
|
) + (_statement ? "\n" : "");
|
|
|
|
}
|