Print @src and @use-src locations in AsmPrinter

This commit is contained in:
Mathias Baumann
2021-08-03 15:29:52 +02:00
parent c89b46c55e
commit 2ee6c7be64
34 changed files with 1319 additions and 109 deletions
+90 -35
View File
@@ -41,48 +41,55 @@ using namespace solidity;
using namespace solidity::util;
using namespace solidity::yul;
//@TODO source locations
string AsmPrinter::operator()(Literal const& _literal) const
string AsmPrinter::operator()(Literal const& _literal)
{
string const locationComment = formatSourceLocationComment(_literal);
switch (_literal.kind)
{
case LiteralKind::Number:
yulAssert(isValidDecimal(_literal.value.str()) || isValidHex(_literal.value.str()), "Invalid number literal");
return _literal.value.str() + appendTypeName(_literal.type);
return locationComment + _literal.value.str() + appendTypeName(_literal.type);
case LiteralKind::Boolean:
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal.");
return ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type, true);
return locationComment + ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type, true);
case LiteralKind::String:
break;
}
return escapeAndQuoteString(_literal.value.str()) + appendTypeName(_literal.type);
return locationComment + escapeAndQuoteString(_literal.value.str()) + appendTypeName(_literal.type);
}
string AsmPrinter::operator()(Identifier const& _identifier) const
string AsmPrinter::operator()(Identifier const& _identifier)
{
yulAssert(!_identifier.name.empty(), "Invalid identifier.");
return _identifier.name.str();
return formatSourceLocationComment(_identifier) + _identifier.name.str();
}
string AsmPrinter::operator()(ExpressionStatement const& _statement) const
string AsmPrinter::operator()(ExpressionStatement const& _statement)
{
return std::visit(*this, _statement.expression);
string const locationComment = formatSourceLocationComment(_statement);
return locationComment + std::visit(*this, _statement.expression);
}
string AsmPrinter::operator()(Assignment const& _assignment) const
string AsmPrinter::operator()(Assignment const& _assignment)
{
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 variables + " := " + std::visit(*this, *_assignment.value);
return locationComment + variables + " := " + std::visit(*this, *_assignment.value);
}
string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration) const
string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration)
{
string out = "let ";
string out = formatSourceLocationComment(_variableDeclaration);
out += "let ";
out += boost::algorithm::join(
_variableDeclaration.variables | ranges::views::transform(
[this](TypedName argument) { return formatTypedName(argument); }
@@ -97,10 +104,12 @@ string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration) c
return out;
}
string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition) const
string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition)
{
yulAssert(!_functionDefinition.name.empty(), "Invalid function name.");
string out = "function " + _functionDefinition.name.str() + "(";
string out = formatSourceLocationComment(_functionDefinition);
out += "function " + _functionDefinition.name.str() + "(";
out += boost::algorithm::join(
_functionDefinition.parameters | ranges::views::transform(
[this](TypedName argument) { return formatTypedName(argument); }
@@ -122,30 +131,41 @@ string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition) con
return out + "\n" + (*this)(_functionDefinition.body);
}
string AsmPrinter::operator()(FunctionCall const& _functionCall) const
string AsmPrinter::operator()(FunctionCall const& _functionCall)
{
string const locationComment = formatSourceLocationComment(_functionCall);
string const functionName = (*this)(_functionCall.functionName);
return
(*this)(_functionCall.functionName) + "(" +
locationComment +
functionName + "(" +
boost::algorithm::join(
_functionCall.arguments | ranges::views::transform([&](auto&& _node) { return std::visit(*this, _node); }),
", " ) +
")";
}
string AsmPrinter::operator()(If const& _if) const
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 "if " + std::visit(*this, *_if.condition) + delim + (*this)(_if.body);
return out + delim + body;
}
string AsmPrinter::operator()(Switch const& _switch) const
string AsmPrinter::operator()(Switch const& _switch)
{
yulAssert(_switch.expression, "Invalid expression pointer.");
string out = "switch " + std::visit(*this, *_switch.expression);
string out = formatSourceLocationComment(_switch);
out += "switch " + std::visit(*this, *_switch.expression);
for (auto const& _case: _switch.cases)
{
if (!_case.value)
@@ -157,12 +177,15 @@ string AsmPrinter::operator()(Switch const& _switch) const
return out;
}
string AsmPrinter::operator()(ForLoop const& _forLoop) const
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 &&
@@ -171,46 +194,50 @@ string AsmPrinter::operator()(ForLoop const& _forLoop) const
)
delim = ' ';
return
locationComment +
("for " + move(pre) + delim + move(condition) + delim + move(post) + "\n") +
(*this)(_forLoop.body);
}
string AsmPrinter::operator()(Break const&) const
string AsmPrinter::operator()(Break const& _break)
{
return "break";
return formatSourceLocationComment(_break) + "break";
}
string AsmPrinter::operator()(Continue const&) const
string AsmPrinter::operator()(Continue const& _continue)
{
return "continue";
return formatSourceLocationComment(_continue) + "continue";
}
string AsmPrinter::operator()(Leave const&) const
// '_leave' and '__leave' is reserved in VisualStudio
string AsmPrinter::operator()(Leave const& leave_)
{
return "leave";
return formatSourceLocationComment(leave_) + "leave";
}
string AsmPrinter::operator()(Block const& _block) const
string AsmPrinter::operator()(Block const& _block)
{
string const locationComment = formatSourceLocationComment(_block);
if (_block.statements.empty())
return "{ }";
return locationComment + "{ }";
string body = boost::algorithm::join(
_block.statements | ranges::views::transform([&](auto&& _node) { return std::visit(*this, _node); }),
"\n"
);
if (body.size() < 30 && body.find('\n') == string::npos)
return "{ " + body + " }";
return locationComment + "{ " + body + " }";
else
{
boost::replace_all(body, "\n", "\n ");
return "{\n " + body + "\n}";
return locationComment + "{\n " + body + "\n}";
}
}
string AsmPrinter::formatTypedName(TypedName _variable) const
string AsmPrinter::formatTypedName(TypedName _variable)
{
yulAssert(!_variable.name.empty(), "Invalid variable name.");
return _variable.name.str() + appendTypeName(_variable.type);
return formatSourceLocationComment(_variable) + _variable.name.str() + appendTypeName(_variable.type);
}
string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
@@ -228,3 +255,31 @@ string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
else
return ":" + _type.str();
}
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;
string sourceIndex = "-1";
if (_debugData->location.sourceName)
sourceIndex = to_string(m_nameToSourceIndex.at(*_debugData->location.sourceName));
string sourceLocation =
"@src " +
sourceIndex +
":" +
to_string(_debugData->location.start) +
":" +
to_string(_debugData->location.end);
return
_statement ?
"/// " + sourceLocation + "\n" :
"/** " + sourceLocation + " */ ";
}