2017-02-15 13:52:53 +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-15 13:52:53 +00:00
|
|
|
/**
|
|
|
|
* Analyzer part of inline assembly.
|
|
|
|
*/
|
|
|
|
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2019-01-15 12:40:10 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2019-03-04 14:38:05 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2019-07-09 15:23:14 +00:00
|
|
|
#include <libyul/Object.h>
|
2020-11-25 18:10:31 +00:00
|
|
|
#include <libyul/Scope.h>
|
|
|
|
#include <libyul/ScopeFiller.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2021-02-09 13:24:26 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
#include <libsolutil/StringUtils.h>
|
2021-04-12 10:33:28 +00:00
|
|
|
#include <libsolutil/Visitor.h>
|
2021-02-09 13:24:26 +00:00
|
|
|
|
2017-06-08 14:44:03 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2020-08-04 15:37:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
inline string to_string(LiteralKind _kind)
|
|
|
|
{
|
|
|
|
switch (_kind)
|
|
|
|
{
|
|
|
|
case LiteralKind::Number: return "number";
|
|
|
|
case LiteralKind::Boolean: return "boolean";
|
|
|
|
case LiteralKind::String: return "string";
|
|
|
|
default: yulAssert(false, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
bool AsmAnalyzer::analyze(Block const& _block)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2020-05-15 01:20:54 +00:00
|
|
|
auto watcher = m_errorReporter.errorWatcher();
|
2019-04-25 09:34:56 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!(ScopeFiller(m_info, m_errorReporter))(_block))
|
|
|
|
return false;
|
2017-03-22 12:21:58 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
(*this)(_block);
|
2019-04-25 09:34:56 +00:00
|
|
|
}
|
|
|
|
catch (FatalError const&)
|
|
|
|
{
|
|
|
|
// This FatalError con occur if the errorReporter has too many errors.
|
2020-05-15 01:20:54 +00:00
|
|
|
yulAssert(!watcher.ok(), "Fatal error detected, but no error is reported.");
|
2019-04-25 09:34:56 +00:00
|
|
|
}
|
2020-05-15 01:20:54 +00:00
|
|
|
return watcher.ok();
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:23:14 +00:00
|
|
|
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect, Object const& _object)
|
2019-01-29 14:01:30 +00:00
|
|
|
{
|
|
|
|
ErrorList errorList;
|
|
|
|
langutil::ErrorReporter errors(errorList);
|
2019-07-09 15:23:14 +00:00
|
|
|
AsmAnalysisInfo analysisInfo;
|
2019-01-29 14:01:30 +00:00
|
|
|
bool success = yul::AsmAnalyzer(
|
|
|
|
analysisInfo,
|
|
|
|
errors,
|
2019-07-09 15:23:14 +00:00
|
|
|
_dialect,
|
|
|
|
{},
|
2020-06-17 09:17:35 +00:00
|
|
|
_object.qualifiedDataNames()
|
2019-07-09 15:23:14 +00:00
|
|
|
).analyze(*_object.code);
|
2020-05-12 15:09:25 +00:00
|
|
|
yulAssert(success && !errors.hasErrors(), "Invalid assembly/yul code.");
|
2019-01-29 14:01:30 +00:00
|
|
|
return analysisInfo;
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> AsmAnalyzer::operator()(Literal const& _literal)
|
2017-02-16 22:44:22 +00:00
|
|
|
{
|
2021-04-27 14:53:04 +00:00
|
|
|
expectValidType(_literal.type, _literal.debugData->location);
|
2018-11-21 11:42:34 +00:00
|
|
|
if (_literal.kind == LiteralKind::String && _literal.value.str().size() > 32)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
3069_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_literal.debugData->location,
|
2018-10-29 14:12:02 +00:00
|
|
|
"String literal too long (" + to_string(_literal.value.str().size()) + " > 32)"
|
2017-05-11 13:26:35 +00:00
|
|
|
);
|
2018-11-21 11:42:34 +00:00
|
|
|
else if (_literal.kind == LiteralKind::Number && bigint(_literal.value.str()) > u256(-1))
|
2021-04-27 14:53:04 +00:00
|
|
|
m_errorReporter.typeError(6708_error, _literal.debugData->location, "Number literal too large (> 256 bits)");
|
2018-11-21 11:42:34 +00:00
|
|
|
else if (_literal.kind == LiteralKind::Boolean)
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "");
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
if (!m_dialect.validTypeForLiteral(_literal.kind, _literal.value, _literal.type))
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
5170_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_literal.debugData->location,
|
2020-01-16 17:56:05 +00:00
|
|
|
"Invalid type \"" + _literal.type.str() + "\" for literal \"" + _literal.value.str() + "\"."
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
return {_literal.type};
|
2017-02-16 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_identifier.name.empty(), "");
|
2020-05-15 01:20:54 +00:00
|
|
|
auto watcher = m_errorReporter.errorWatcher();
|
2020-01-16 17:56:05 +00:00
|
|
|
YulString type = m_dialect.defaultType;
|
|
|
|
|
2019-11-24 17:46:43 +00:00
|
|
|
if (m_currentScope->lookup(_identifier.name, GenericVisitor{
|
2017-02-17 15:05:22 +00:00
|
|
|
[&](Scope::Variable const& _var)
|
2017-02-17 11:03:55 +00:00
|
|
|
{
|
2017-06-13 22:07:13 +00:00
|
|
|
if (!m_activeVariables.count(&_var))
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
4990_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_identifier.debugData->location,
|
2018-10-29 14:12:02 +00:00
|
|
|
"Variable " + _identifier.name.str() + " used before it was declared."
|
2017-05-11 13:26:35 +00:00
|
|
|
);
|
2020-01-16 17:56:05 +00:00
|
|
|
type = _var.type;
|
2017-03-22 12:21:58 +00:00
|
|
|
},
|
2017-02-17 15:05:22 +00:00
|
|
|
[&](Scope::Function const&)
|
|
|
|
{
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
6041_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_identifier.debugData->location,
|
2018-10-29 14:12:02 +00:00
|
|
|
"Function " + _identifier.name.str() + " used without being called."
|
2017-05-11 13:26:35 +00:00
|
|
|
);
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2019-11-24 17:46:43 +00:00
|
|
|
}))
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
else
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2020-06-11 16:46:31 +00:00
|
|
|
bool found = m_resolver && m_resolver(
|
|
|
|
_identifier,
|
|
|
|
yul::IdentifierContext::RValue,
|
|
|
|
m_currentScope->insideFunction()
|
|
|
|
);
|
2020-05-15 01:20:54 +00:00
|
|
|
if (!found && watcher.ok())
|
2017-04-25 11:15:42 +00:00
|
|
|
// Only add an error message if the callback did not do it.
|
2021-02-09 13:24:26 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
8198_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_identifier.debugData->location,
|
2021-02-09 13:24:26 +00:00
|
|
|
"Identifier \"" + _identifier.name.str() + "\" not found."
|
|
|
|
);
|
|
|
|
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
2020-01-16 17:56:05 +00:00
|
|
|
|
|
|
|
return {type};
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(ExpressionStatement const& _statement)
|
2017-12-08 13:01:22 +00:00
|
|
|
{
|
2020-05-15 01:20:54 +00:00
|
|
|
auto watcher = m_errorReporter.errorWatcher();
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> types = std::visit(*this, _statement.expression);
|
2020-05-15 01:20:54 +00:00
|
|
|
if (watcher.ok() && !types.empty())
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
3083_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_statement.debugData->location,
|
2018-02-15 14:18:09 +00:00
|
|
|
"Top-level expressions are not supposed to return values (this expression returns " +
|
2020-01-16 17:56:05 +00:00
|
|
|
to_string(types.size()) +
|
2018-02-15 14:18:09 +00:00
|
|
|
" value" +
|
2020-01-16 17:56:05 +00:00
|
|
|
(types.size() == 1 ? "" : "s") +
|
|
|
|
"). Use ``pop()`` or assign them."
|
|
|
|
);
|
2017-12-08 13:01:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(Assignment const& _assignment)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_assignment.value, "");
|
2020-01-16 17:56:05 +00:00
|
|
|
size_t const numVariables = _assignment.variableNames.size();
|
|
|
|
yulAssert(numVariables >= 1, "");
|
|
|
|
|
2020-07-06 15:25:55 +00:00
|
|
|
set<YulString> variables;
|
|
|
|
for (auto const& _variableName: _assignment.variableNames)
|
|
|
|
if (!variables.insert(_variableName.name).second)
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
9005_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_assignment.debugData->location,
|
2020-07-06 15:25:55 +00:00
|
|
|
"Variable " +
|
|
|
|
_variableName.name.str() +
|
|
|
|
" occurs multiple times on the left-hand side of the assignment."
|
|
|
|
);
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> types = std::visit(*this, *_assignment.value);
|
|
|
|
|
|
|
|
if (types.size() != numVariables)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
8678_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_assignment.debugData->location,
|
2021-02-09 13:24:26 +00:00
|
|
|
"Variable count for assignment to \"" +
|
|
|
|
joinHumanReadable(applyMap(_assignment.variableNames, [](auto const& _identifier){ return _identifier.name.str(); })) +
|
|
|
|
"\" does not match number of values (" +
|
2020-01-16 17:56:05 +00:00
|
|
|
to_string(numVariables) +
|
2017-05-19 16:06:26 +00:00
|
|
|
" vs. " +
|
2020-01-16 17:56:05 +00:00
|
|
|
to_string(types.size()) +
|
2017-05-19 16:06:26 +00:00
|
|
|
")"
|
|
|
|
);
|
2020-01-16 17:56:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < numVariables; ++i)
|
2020-01-16 17:56:05 +00:00
|
|
|
if (i < types.size())
|
|
|
|
checkAssignment(_assignment.variableNames[i], types[i]);
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2020-01-16 17:56:05 +00:00
|
|
|
size_t const numVariables = _varDecl.variables.size();
|
2019-11-06 17:06:36 +00:00
|
|
|
if (m_resolver)
|
|
|
|
for (auto const& variable: _varDecl.variables)
|
|
|
|
// Call the resolver for variable declarations to allow it to raise errors on shadowing.
|
|
|
|
m_resolver(
|
2021-04-27 14:53:04 +00:00
|
|
|
yul::Identifier{variable.debugData, variable.name},
|
2019-11-06 17:06:36 +00:00
|
|
|
yul::IdentifierContext::VariableDeclaration,
|
|
|
|
m_currentScope->insideFunction()
|
|
|
|
);
|
2020-01-16 17:56:05 +00:00
|
|
|
for (auto const& variable: _varDecl.variables)
|
2020-07-02 17:23:36 +00:00
|
|
|
{
|
2021-04-27 14:53:04 +00:00
|
|
|
expectValidIdentifier(variable.name, variable.debugData->location);
|
|
|
|
expectValidType(variable.type, variable.debugData->location);
|
2020-07-02 17:23:36 +00:00
|
|
|
}
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2017-05-05 15:46:26 +00:00
|
|
|
if (_varDecl.value)
|
2017-05-05 20:10:05 +00:00
|
|
|
{
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> types = std::visit(*this, *_varDecl.value);
|
|
|
|
if (types.size() != numVariables)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
3812_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_varDecl.debugData->location,
|
2021-02-09 13:24:26 +00:00
|
|
|
"Variable count mismatch for declaration of \"" +
|
|
|
|
joinHumanReadable(applyMap(_varDecl.variables, [](auto const& _identifier){ return _identifier.name.str(); })) +
|
|
|
|
+ "\": " +
|
2018-12-04 10:22:49 +00:00
|
|
|
to_string(numVariables) +
|
|
|
|
" variables and " +
|
2020-01-16 17:56:05 +00:00
|
|
|
to_string(types.size()) +
|
2018-12-04 10:22:49 +00:00
|
|
|
" values."
|
|
|
|
);
|
2020-01-16 17:56:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < _varDecl.variables.size(); ++i)
|
|
|
|
{
|
|
|
|
YulString givenType = m_dialect.defaultType;
|
|
|
|
if (i < types.size())
|
|
|
|
givenType = types[i];
|
|
|
|
TypedName const& variable = _varDecl.variables[i];
|
|
|
|
if (variable.type != givenType)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
3947_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
variable.debugData->location,
|
2020-07-10 20:06:09 +00:00
|
|
|
"Assigning value of type \"" + givenType.str() + "\" to variable of type \"" + variable.type.str() + "\"."
|
2020-01-16 17:56:05 +00:00
|
|
|
);
|
|
|
|
}
|
2017-05-05 20:10:05 +00:00
|
|
|
}
|
2017-05-05 17:35:40 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
for (TypedName const& variable: _varDecl.variables)
|
|
|
|
m_activeVariables.insert(&std::get<Scope::Variable>(
|
|
|
|
m_currentScope->identifiers.at(variable.name))
|
|
|
|
);
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(FunctionDefinition const& _funDef)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_funDef.name.empty(), "");
|
2021-04-27 14:53:04 +00:00
|
|
|
expectValidIdentifier(_funDef.name, _funDef.debugData->location);
|
2017-05-26 19:46:02 +00:00
|
|
|
Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get();
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(virtualBlock, "");
|
2017-05-26 19:46:02 +00:00
|
|
|
Scope& varScope = scope(virtualBlock);
|
2017-12-01 13:10:49 +00:00
|
|
|
for (auto const& var: _funDef.parameters + _funDef.returnVariables)
|
2017-05-24 23:27:48 +00:00
|
|
|
{
|
2021-04-27 14:53:04 +00:00
|
|
|
expectValidIdentifier(var.name, var.debugData->location);
|
|
|
|
expectValidType(var.type, var.debugData->location);
|
2019-11-19 15:42:49 +00:00
|
|
|
m_activeVariables.insert(&std::get<Scope::Variable>(varScope.identifiers.at(var.name)));
|
2017-05-24 23:27:48 +00:00
|
|
|
}
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
(*this)(_funDef.body);
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
|
2017-02-17 11:03:55 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_funCall.functionName.name.empty(), "");
|
2020-05-15 01:20:54 +00:00
|
|
|
auto watcher = m_errorReporter.errorWatcher();
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> const* parameterTypes = nullptr;
|
|
|
|
vector<YulString> const* returnTypes = nullptr;
|
2020-08-04 15:30:16 +00:00
|
|
|
vector<optional<LiteralKind>> const* literalArguments = nullptr;
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
if (BuiltinFunction const* f = m_dialect.builtin(_funCall.functionName.name))
|
2018-12-04 10:23:28 +00:00
|
|
|
{
|
2020-01-16 17:56:05 +00:00
|
|
|
parameterTypes = &f->parameters;
|
|
|
|
returnTypes = &f->returns;
|
2020-08-04 15:30:16 +00:00
|
|
|
if (!f->literalArguments.empty())
|
|
|
|
literalArguments = &f->literalArguments;
|
2020-05-12 15:09:25 +00:00
|
|
|
|
2020-07-02 17:34:39 +00:00
|
|
|
validateInstructions(_funCall);
|
2018-12-04 10:23:28 +00:00
|
|
|
}
|
2019-11-24 17:46:43 +00:00
|
|
|
else if (!m_currentScope->lookup(_funCall.functionName.name, GenericVisitor{
|
2017-02-17 15:05:22 +00:00
|
|
|
[&](Scope::Variable const&)
|
|
|
|
{
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
4202_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_funCall.functionName.debugData->location,
|
2017-05-11 13:26:35 +00:00
|
|
|
"Attempt to call variable instead of function."
|
|
|
|
);
|
2017-02-17 15:05:22 +00:00
|
|
|
},
|
|
|
|
[&](Scope::Function const& _fun)
|
|
|
|
{
|
2020-01-16 17:56:05 +00:00
|
|
|
parameterTypes = &_fun.arguments;
|
|
|
|
returnTypes = &_fun.returns;
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
2019-11-24 17:46:43 +00:00
|
|
|
}))
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2020-07-02 17:34:39 +00:00
|
|
|
if (!validateInstructions(_funCall))
|
2021-02-09 13:24:26 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
4619_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_funCall.functionName.debugData->location,
|
2021-02-09 13:24:26 +00:00
|
|
|
"Function \"" + _funCall.functionName.name.str() + "\" not found."
|
|
|
|
);
|
2020-05-15 01:20:54 +00:00
|
|
|
yulAssert(!watcher.ok(), "Expected a reported error.");
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
2020-05-12 15:09:25 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
if (parameterTypes && _funCall.arguments.size() != parameterTypes->size())
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
7000_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_funCall.functionName.debugData->location,
|
2021-02-09 13:24:26 +00:00
|
|
|
"Function \"" + _funCall.functionName.name.str() + "\" expects " +
|
2020-01-16 17:56:05 +00:00
|
|
|
to_string(parameterTypes->size()) +
|
|
|
|
" arguments but got " +
|
|
|
|
to_string(_funCall.arguments.size()) + "."
|
|
|
|
);
|
2018-12-04 10:22:49 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> argTypes;
|
2020-04-06 12:47:44 +00:00
|
|
|
for (size_t i = _funCall.arguments.size(); i > 0; i--)
|
2018-12-20 16:22:17 +00:00
|
|
|
{
|
2020-04-06 12:47:44 +00:00
|
|
|
Expression const& arg = _funCall.arguments[i - 1];
|
2020-12-15 14:38:59 +00:00
|
|
|
if (
|
|
|
|
auto literalArgumentKind = (literalArguments && i <= literalArguments->size()) ?
|
|
|
|
literalArguments->at(i - 1) :
|
|
|
|
std::nullopt
|
|
|
|
)
|
2019-07-09 12:06:33 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
if (!holds_alternative<Literal>(arg))
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
9114_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_funCall.functionName.debugData->location,
|
2019-07-09 12:06:33 +00:00
|
|
|
"Function expects direct literals as arguments."
|
|
|
|
);
|
2020-08-04 15:37:36 +00:00
|
|
|
else if (*literalArgumentKind != get<Literal>(arg).kind)
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
5859_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
get<Literal>(arg).debugData->location,
|
2020-08-04 15:37:36 +00:00
|
|
|
"Function expects " + to_string(*literalArgumentKind) + " literal."
|
|
|
|
);
|
|
|
|
else if (*literalArgumentKind == LiteralKind::String)
|
|
|
|
{
|
2021-03-17 18:37:39 +00:00
|
|
|
string functionName = _funCall.functionName.name.str();
|
|
|
|
if (functionName == "datasize" || functionName == "dataoffset")
|
|
|
|
{
|
2020-08-04 15:37:36 +00:00
|
|
|
if (!m_dataNames.count(get<Literal>(arg).value))
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
3517_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
get<Literal>(arg).debugData->location,
|
2020-08-04 15:37:36 +00:00
|
|
|
"Unknown data object \"" + std::get<Literal>(arg).value.str() + "\"."
|
|
|
|
);
|
2021-03-17 18:37:39 +00:00
|
|
|
}
|
|
|
|
else if (functionName.substr(0, "verbatim_"s.size()) == "verbatim_")
|
|
|
|
{
|
|
|
|
if (get<Literal>(arg).value.empty())
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
1844_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
get<Literal>(arg).debugData->location,
|
2021-03-17 18:37:39 +00:00
|
|
|
"The \"verbatim_*\" builtins cannot be used with empty bytecode."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-04 15:37:36 +00:00
|
|
|
argTypes.emplace_back(expectUnlimitedStringLiteral(get<Literal>(arg)));
|
|
|
|
continue;
|
|
|
|
}
|
2019-07-09 12:06:33 +00:00
|
|
|
}
|
2020-08-04 15:37:36 +00:00
|
|
|
argTypes.emplace_back(expectExpression(arg));
|
2018-12-20 16:22:17 +00:00
|
|
|
}
|
2020-01-16 17:56:05 +00:00
|
|
|
std::reverse(argTypes.begin(), argTypes.end());
|
|
|
|
|
|
|
|
if (parameterTypes && parameterTypes->size() == argTypes.size())
|
|
|
|
for (size_t i = 0; i < parameterTypes->size(); ++i)
|
|
|
|
expectType((*parameterTypes)[i], argTypes[i], locationOf(_funCall.arguments[i]));
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2020-05-15 01:20:54 +00:00
|
|
|
if (watcher.ok())
|
2020-01-16 17:56:05 +00:00
|
|
|
{
|
|
|
|
yulAssert(parameterTypes && parameterTypes->size() == argTypes.size(), "");
|
|
|
|
yulAssert(returnTypes, "");
|
|
|
|
return *returnTypes;
|
|
|
|
}
|
|
|
|
else if (returnTypes)
|
|
|
|
return vector<YulString>(returnTypes->size(), m_dialect.defaultType);
|
|
|
|
else
|
|
|
|
return {};
|
2017-02-16 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(If const& _if)
|
2017-11-21 12:36:41 +00:00
|
|
|
{
|
2020-01-16 17:56:05 +00:00
|
|
|
expectBoolExpression(*_if.condition);
|
2017-11-21 12:36:41 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
(*this)(_if.body);
|
2017-11-21 12:36:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(Switch const& _switch)
|
2017-04-28 13:27:56 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_switch.expression, "");
|
2018-05-10 08:43:39 +00:00
|
|
|
|
2020-05-13 15:13:05 +00:00
|
|
|
if (_switch.cases.size() == 1 && !_switch.cases[0].value)
|
|
|
|
m_errorReporter.warning(
|
2020-05-27 14:30:48 +00:00
|
|
|
9592_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_switch.debugData->location,
|
2020-05-13 15:13:05 +00:00
|
|
|
"\"switch\" statement with only a default case."
|
|
|
|
);
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
YulString valueType = expectExpression(*_switch.expression);
|
2019-01-15 12:40:10 +00:00
|
|
|
|
2019-03-25 11:30:05 +00:00
|
|
|
set<u256> cases;
|
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-04-28 13:27:56 +00:00
|
|
|
{
|
2020-05-15 01:20:54 +00:00
|
|
|
auto watcher = m_errorReporter.errorWatcher();
|
|
|
|
|
2021-04-27 14:53:04 +00:00
|
|
|
expectType(valueType, _case.value->type, _case.value->debugData->location);
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
// We cannot use "expectExpression" here because *_case.value is not an
|
|
|
|
// Expression and would be converted to an Expression otherwise.
|
|
|
|
(*this)(*_case.value);
|
2017-05-17 10:21:37 +00:00
|
|
|
|
|
|
|
/// Note: the parser ensures there is only one default case
|
2020-05-15 01:20:54 +00:00
|
|
|
if (watcher.ok() && !cases.insert(valueOfLiteral(*_case.value)).second)
|
2021-02-09 13:24:26 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
6792_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_case.debugData->location,
|
2021-02-09 13:24:26 +00:00
|
|
|
"Duplicate case \"" +
|
|
|
|
valueOfLiteral(*_case.value).str() +
|
|
|
|
"\" defined."
|
|
|
|
);
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
(*this)(_case.body);
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(ForLoop const& _for)
|
2017-04-28 13:33:52 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(_for.condition, "");
|
2018-05-10 08:43:39 +00:00
|
|
|
|
2019-03-04 14:38:05 +00:00
|
|
|
Scope* outerScope = m_currentScope;
|
2017-06-01 12:16:12 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
(*this)(_for.pre);
|
2019-05-22 14:08:49 +00:00
|
|
|
|
2017-06-01 12:16:12 +00:00
|
|
|
// The block was closed already, but we re-open it again and stuff the
|
|
|
|
// condition, the body and the post part inside.
|
|
|
|
m_currentScope = &scope(&_for.pre);
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
expectBoolExpression(*_for.condition);
|
2019-03-04 14:38:05 +00:00
|
|
|
// backup outer for-loop & create new state
|
|
|
|
auto outerForLoop = m_currentForLoop;
|
|
|
|
m_currentForLoop = &_for;
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
(*this)(_for.body);
|
|
|
|
(*this)(_for.post);
|
2017-06-01 12:16:12 +00:00
|
|
|
|
2019-03-04 14:38:05 +00:00
|
|
|
m_currentScope = outerScope;
|
|
|
|
m_currentForLoop = outerForLoop;
|
2017-04-28 13:33:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::operator()(Block const& _block)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2017-05-26 19:46:02 +00:00
|
|
|
auto previousScope = m_currentScope;
|
2017-02-17 11:03:55 +00:00
|
|
|
m_currentScope = &scope(&_block);
|
2017-02-15 13:52:53 +00:00
|
|
|
|
|
|
|
for (auto const& s: _block.statements)
|
2020-01-16 17:56:05 +00:00
|
|
|
std::visit(*this, s);
|
2017-03-22 12:21:58 +00:00
|
|
|
|
2017-05-26 19:46:02 +00:00
|
|
|
m_currentScope = previousScope;
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
2017-02-17 11:03:55 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
YulString AsmAnalyzer::expectExpression(Expression const& _expr)
|
2017-06-01 12:16:01 +00:00
|
|
|
{
|
2020-01-16 17:56:05 +00:00
|
|
|
vector<YulString> types = std::visit(*this, _expr);
|
|
|
|
if (types.size() != 1)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
3950_error,
|
2020-01-16 17:56:05 +00:00
|
|
|
locationOf(_expr),
|
|
|
|
"Expected expression to evaluate to one value, but got " +
|
|
|
|
to_string(types.size()) +
|
|
|
|
" values instead."
|
2017-06-01 12:16:01 +00:00
|
|
|
);
|
2020-01-16 17:56:05 +00:00
|
|
|
return types.empty() ? m_dialect.defaultType : types.front();
|
2017-06-01 12:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 11:39:01 +00:00
|
|
|
YulString AsmAnalyzer::expectUnlimitedStringLiteral(Literal const& _literal)
|
|
|
|
{
|
|
|
|
yulAssert(_literal.kind == LiteralKind::String, "");
|
|
|
|
yulAssert(m_dialect.validTypeForLiteral(LiteralKind::String, _literal.value, _literal.type), "");
|
|
|
|
|
|
|
|
return {_literal.type};
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::expectBoolExpression(Expression const& _expr)
|
|
|
|
{
|
|
|
|
YulString type = expectExpression(_expr);
|
|
|
|
if (type != m_dialect.boolType)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
1733_error,
|
|
|
|
locationOf(_expr),
|
2020-01-16 17:56:05 +00:00
|
|
|
"Expected a value of boolean type \"" +
|
|
|
|
m_dialect.boolType.str() +
|
|
|
|
"\" but got \"" +
|
|
|
|
type.str() +
|
|
|
|
"\""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsmAnalyzer::checkAssignment(Identifier const& _variable, YulString _valueType)
|
2017-02-17 11:03:55 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_variable.name.empty(), "");
|
2020-05-15 01:20:54 +00:00
|
|
|
auto watcher = m_errorReporter.errorWatcher();
|
2020-01-16 17:56:05 +00:00
|
|
|
YulString const* variableType = nullptr;
|
2020-01-16 17:56:05 +00:00
|
|
|
bool found = false;
|
2017-03-21 18:38:37 +00:00
|
|
|
if (Scope::Identifier const* var = m_currentScope->lookup(_variable.name))
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2021-02-23 09:57:49 +00:00
|
|
|
if (!holds_alternative<Scope::Variable>(*var))
|
2021-04-27 14:53:04 +00:00
|
|
|
m_errorReporter.typeError(2657_error, _variable.debugData->location, "Assignment requires variable.");
|
2021-02-23 09:57:49 +00:00
|
|
|
else if (!m_activeVariables.count(&std::get<Scope::Variable>(*var)))
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
1133_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_variable.debugData->location,
|
2018-10-29 14:12:02 +00:00
|
|
|
"Variable " + _variable.name.str() + " used before it was declared."
|
2017-05-11 13:26:35 +00:00
|
|
|
);
|
2020-01-16 17:56:05 +00:00
|
|
|
else
|
|
|
|
variableType = &std::get<Scope::Variable>(*var).type;
|
2020-01-16 17:56:05 +00:00
|
|
|
found = true;
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
else if (m_resolver)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
bool insideFunction = m_currentScope->insideFunction();
|
2020-06-11 16:46:31 +00:00
|
|
|
if (m_resolver(_variable, yul::IdentifierContext::LValue, insideFunction))
|
2020-01-16 17:56:05 +00:00
|
|
|
{
|
|
|
|
found = true;
|
2020-01-16 17:56:05 +00:00
|
|
|
variableType = &m_dialect.defaultType;
|
2020-01-16 17:56:05 +00:00
|
|
|
}
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2020-05-15 01:20:54 +00:00
|
|
|
if (!found && watcher.ok())
|
2017-04-25 11:15:42 +00:00
|
|
|
// Only add message if the callback did not.
|
2021-04-27 14:53:04 +00:00
|
|
|
m_errorReporter.declarationError(4634_error, _variable.debugData->location, "Variable not found or variable not lvalue.");
|
2020-01-16 17:56:05 +00:00
|
|
|
if (variableType && *variableType != _valueType)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
9547_error,
|
2021-04-27 14:53:04 +00:00
|
|
|
_variable.debugData->location,
|
2020-01-16 17:56:05 +00:00
|
|
|
"Assigning a value of type \"" +
|
|
|
|
_valueType.str() +
|
|
|
|
"\" to a variable of type \"" +
|
|
|
|
variableType->str() +
|
|
|
|
"\"."
|
|
|
|
);
|
|
|
|
|
2020-05-15 01:20:54 +00:00
|
|
|
yulAssert(!watcher.ok() || variableType, "");
|
2017-03-22 12:21:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 11:03:55 +00:00
|
|
|
Scope& AsmAnalyzer::scope(Block const* _block)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_info.scopes.count(_block) == 1, "Scope requested but not present.");
|
2017-04-26 13:41:08 +00:00
|
|
|
auto scopePtr = m_info.scopes.at(_block);
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(scopePtr, "Scope requested but not present.");
|
2017-02-17 15:05:22 +00:00
|
|
|
return *scopePtr;
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2020-01-16 17:56:05 +00:00
|
|
|
|
2020-07-02 17:23:36 +00:00
|
|
|
void AsmAnalyzer::expectValidIdentifier(YulString _identifier, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
// NOTE: the leading dot case is handled by the parser not allowing it.
|
|
|
|
|
|
|
|
if (boost::ends_with(_identifier.str(), "."))
|
|
|
|
m_errorReporter.syntaxError(
|
|
|
|
3384_error,
|
|
|
|
_location,
|
|
|
|
"\"" + _identifier.str() + "\" is not a valid identifier (ends with a dot)."
|
|
|
|
);
|
|
|
|
|
|
|
|
if (_identifier.str().find("..") != std::string::npos)
|
|
|
|
m_errorReporter.syntaxError(
|
|
|
|
7771_error,
|
|
|
|
_location,
|
|
|
|
"\"" + _identifier.str() + "\" is not a valid identifier (contains consecutive dots)."
|
|
|
|
);
|
|
|
|
|
2020-07-29 17:55:04 +00:00
|
|
|
if (m_dialect.reservedIdentifier(_identifier))
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
5017_error,
|
|
|
|
_location,
|
|
|
|
"The identifier \"" + _identifier.str() + "\" is reserved and can not be used."
|
|
|
|
);
|
2020-07-02 17:23:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:58:20 +00:00
|
|
|
void AsmAnalyzer::expectValidType(YulString _type, SourceLocation const& _location)
|
2017-05-24 23:27:48 +00:00
|
|
|
{
|
2020-01-16 17:56:05 +00:00
|
|
|
if (!m_dialect.types.count(_type))
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
5473_error,
|
2017-05-11 13:26:35 +00:00
|
|
|
_location,
|
2019-12-19 16:58:20 +00:00
|
|
|
"\"" + _type.str() + "\" is not a valid type (user defined types are not yet supported)."
|
2017-05-11 13:26:35 +00:00
|
|
|
);
|
2017-05-24 23:27:48 +00:00
|
|
|
}
|
2017-05-24 20:51:54 +00:00
|
|
|
|
2020-01-16 17:56:05 +00:00
|
|
|
void AsmAnalyzer::expectType(YulString _expectedType, YulString _givenType, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
if (_expectedType != _givenType)
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
3781_error,
|
|
|
|
_location,
|
2020-01-16 17:56:05 +00:00
|
|
|
"Expected a value of type \"" +
|
|
|
|
_expectedType.str() +
|
|
|
|
"\" but got \"" +
|
|
|
|
_givenType.str() +
|
|
|
|
"\""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-02 17:34:39 +00:00
|
|
|
bool AsmAnalyzer::validateInstructions(std::string const& _instructionIdentifier, langutil::SourceLocation const& _location)
|
2019-09-18 09:36:20 +00:00
|
|
|
{
|
2019-10-25 13:00:11 +00:00
|
|
|
auto const builtin = EVMDialect::strictAssemblyForEVM(EVMVersion{}).builtin(YulString(_instructionIdentifier));
|
2020-06-19 11:32:40 +00:00
|
|
|
if (builtin && builtin->instruction.has_value())
|
2020-07-02 17:34:39 +00:00
|
|
|
return validateInstructions(builtin->instruction.value(), _location);
|
2019-09-18 09:36:20 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-02 17:34:39 +00:00
|
|
|
bool AsmAnalyzer::validateInstructions(evmasm::Instruction _instr, SourceLocation const& _location)
|
2017-05-24 20:51:54 +00:00
|
|
|
{
|
2018-02-23 10:42:53 +00:00
|
|
|
// We assume that returndatacopy, returndatasize and staticcall are either all available
|
|
|
|
// or all not available.
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_evmVersion.supportsReturndata() == m_evmVersion.hasStaticCall(), "");
|
2018-09-18 22:42:33 +00:00
|
|
|
// Similarly we assume bitwise shifting and create2 go together.
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_evmVersion.hasBitwiseShifting() == m_evmVersion.hasCreate2(), "");
|
2018-02-23 10:42:53 +00:00
|
|
|
|
2020-07-06 13:46:13 +00:00
|
|
|
// These instructions are disabled in the dialect.
|
|
|
|
yulAssert(
|
|
|
|
_instr != evmasm::Instruction::JUMP &&
|
|
|
|
_instr != evmasm::Instruction::JUMPI &&
|
|
|
|
_instr != evmasm::Instruction::JUMPDEST,
|
|
|
|
"");
|
|
|
|
|
2020-07-02 17:33:27 +00:00
|
|
|
auto errorForVM = [&](ErrorId _errorId, string const& vmKindMessage) {
|
2020-05-28 00:02:53 +00:00
|
|
|
m_errorReporter.typeError(
|
2020-07-02 17:33:27 +00:00
|
|
|
_errorId,
|
2019-02-25 18:04:55 +00:00
|
|
|
_location,
|
|
|
|
"The \"" +
|
|
|
|
boost::to_lower_copy(instructionInfo(_instr).name)
|
|
|
|
+ "\" instruction is " +
|
|
|
|
vmKindMessage +
|
2019-02-26 18:21:32 +00:00
|
|
|
" VMs " +
|
2020-06-03 09:28:01 +00:00
|
|
|
"(you are currently compiling for \"" +
|
2019-02-25 18:04:55 +00:00
|
|
|
m_evmVersion.name() +
|
2019-02-26 18:21:32 +00:00
|
|
|
"\")."
|
2019-02-25 18:04:55 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-07-03 12:27:32 +00:00
|
|
|
if (_instr == evmasm::Instruction::RETURNDATACOPY && !m_evmVersion.supportsReturndata())
|
2020-07-02 17:33:27 +00:00
|
|
|
errorForVM(7756_error, "only available for Byzantium-compatible");
|
2020-07-03 12:27:32 +00:00
|
|
|
else if (_instr == evmasm::Instruction::RETURNDATASIZE && !m_evmVersion.supportsReturndata())
|
|
|
|
errorForVM(4778_error, "only available for Byzantium-compatible");
|
2019-12-11 16:31:36 +00:00
|
|
|
else if (_instr == evmasm::Instruction::STATICCALL && !m_evmVersion.hasStaticCall())
|
2020-07-02 17:33:27 +00:00
|
|
|
errorForVM(1503_error, "only available for Byzantium-compatible");
|
2020-07-03 12:27:32 +00:00
|
|
|
else if (_instr == evmasm::Instruction::SHL && !m_evmVersion.hasBitwiseShifting())
|
2020-07-02 17:33:27 +00:00
|
|
|
errorForVM(6612_error, "only available for Constantinople-compatible");
|
2020-07-03 12:27:32 +00:00
|
|
|
else if (_instr == evmasm::Instruction::SHR && !m_evmVersion.hasBitwiseShifting())
|
|
|
|
errorForVM(7458_error, "only available for Constantinople-compatible");
|
|
|
|
else if (_instr == evmasm::Instruction::SAR && !m_evmVersion.hasBitwiseShifting())
|
|
|
|
errorForVM(2054_error, "only available for Constantinople-compatible");
|
2019-12-11 16:31:36 +00:00
|
|
|
else if (_instr == evmasm::Instruction::CREATE2 && !m_evmVersion.hasCreate2())
|
2020-07-02 17:33:27 +00:00
|
|
|
errorForVM(6166_error, "only available for Constantinople-compatible");
|
2019-12-11 16:31:36 +00:00
|
|
|
else if (_instr == evmasm::Instruction::EXTCODEHASH && !m_evmVersion.hasExtCodeHash())
|
2020-07-02 17:33:27 +00:00
|
|
|
errorForVM(7110_error, "only available for Constantinople-compatible");
|
2019-12-11 16:31:36 +00:00
|
|
|
else if (_instr == evmasm::Instruction::CHAINID && !m_evmVersion.hasChainID())
|
2020-07-02 17:33:27 +00:00
|
|
|
errorForVM(1561_error, "only available for Istanbul-compatible");
|
2020-05-27 14:03:01 +00:00
|
|
|
else if (_instr == evmasm::Instruction::SELFBALANCE && !m_evmVersion.hasSelfBalance())
|
2020-07-06 13:25:25 +00:00
|
|
|
errorForVM(7721_error, "only available for Istanbul-compatible");
|
2020-05-12 15:09:25 +00:00
|
|
|
else if (_instr == evmasm::Instruction::PC)
|
2020-05-27 14:03:01 +00:00
|
|
|
m_errorReporter.error(
|
2020-06-10 16:25:19 +00:00
|
|
|
2450_error,
|
2020-05-27 14:03:01 +00:00
|
|
|
Error::Type::SyntaxError,
|
2020-05-12 15:09:25 +00:00
|
|
|
_location,
|
2020-05-27 14:03:01 +00:00
|
|
|
"PC instruction is a low-level EVM feature. "
|
|
|
|
"Because of that PC is disallowed in strict assembly."
|
2020-05-12 15:09:25 +00:00
|
|
|
);
|
2019-09-18 09:36:20 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2018-02-15 14:18:09 +00:00
|
|
|
}
|
2020-10-29 14:00:27 +00:00
|
|
|
|
|
|
|
bool AsmAnalyzer::validateInstructions(FunctionCall const& _functionCall)
|
|
|
|
{
|
2021-04-27 14:53:04 +00:00
|
|
|
return validateInstructions(_functionCall.functionName.name.str(), _functionCall.functionName.debugData->location);
|
2020-10-29 14:00:27 +00:00
|
|
|
}
|