mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Extract VariableReferenceCounter and StackTooDeep error from EVMCodeTransform.
This commit is contained in:
parent
124db22f04
commit
5bebbca273
@ -33,6 +33,7 @@
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <libsolutil/StringUtils.h>
|
||||
#include <libsolutil/Visitor.h>
|
||||
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
@ -65,6 +65,8 @@ add_library(yul
|
||||
backends/evm/EVMMetrics.h
|
||||
backends/evm/NoOutputAssembly.h
|
||||
backends/evm/NoOutputAssembly.cpp
|
||||
backends/evm/VariableReferenceCounter.h
|
||||
backends/evm/VariableReferenceCounter.cpp
|
||||
backends/wasm/EVMToEwasmTranslator.cpp
|
||||
backends/wasm/EVMToEwasmTranslator.h
|
||||
backends/wasm/BinaryTransform.cpp
|
||||
|
@ -24,6 +24,8 @@
|
||||
#include <libsolutil/Exceptions.h>
|
||||
#include <libsolutil/Assertions.h>
|
||||
|
||||
#include <libyul/YulString.h>
|
||||
|
||||
namespace solidity::yul
|
||||
{
|
||||
|
||||
@ -32,6 +34,23 @@ struct OptimizerException: virtual YulException {};
|
||||
struct CodegenException: virtual YulException {};
|
||||
struct YulAssertion: virtual YulException {};
|
||||
|
||||
struct StackTooDeepError: virtual YulException
|
||||
{
|
||||
StackTooDeepError(YulString _variable, int _depth, std::string const& _message):
|
||||
variable(_variable), depth(_depth)
|
||||
{
|
||||
*this << util::errinfo_comment(_message);
|
||||
}
|
||||
StackTooDeepError(YulString _functionName, YulString _variable, int _depth, std::string const& _message):
|
||||
functionName(_functionName), variable(_variable), depth(_depth)
|
||||
{
|
||||
*this << util::errinfo_comment(_message);
|
||||
}
|
||||
YulString functionName;
|
||||
YulString variable;
|
||||
int depth;
|
||||
};
|
||||
|
||||
/// Assertion that throws an YulAssertion containing the given description if it is not met.
|
||||
#define yulAssert(CONDITION, DESCRIPTION) \
|
||||
assertThrow(CONDITION, ::solidity::yul::YulAssertion, DESCRIPTION)
|
||||
|
@ -21,6 +21,8 @@
|
||||
|
||||
#include <libyul/Scope.h>
|
||||
|
||||
#include <libsolutil/Visitor.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
|
@ -25,8 +25,6 @@
|
||||
|
||||
#include <libyul/YulString.h>
|
||||
|
||||
#include <libsolutil/Visitor.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
/**
|
||||
* Common code generator for translating Yul / inline assembly to EVM and EVM1.5.
|
||||
* Code generator for translating Yul / inline assembly to EVM.
|
||||
*/
|
||||
|
||||
#include <libyul/backends/evm/EVMCodeTransform.h>
|
||||
@ -25,6 +25,8 @@
|
||||
#include <libyul/AsmAnalysisInfo.h>
|
||||
#include <libyul/Utilities.h>
|
||||
|
||||
#include <libsolutil/Visitor.h>
|
||||
|
||||
#include <liblangutil/Exceptions.h>
|
||||
|
||||
#include <range/v3/view/reverse.hpp>
|
||||
@ -42,62 +44,6 @@ using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
using namespace solidity::util;
|
||||
|
||||
void VariableReferenceCounter::operator()(Identifier const& _identifier)
|
||||
{
|
||||
increaseRefIfFound(_identifier.name);
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::operator()(FunctionDefinition const& _function)
|
||||
{
|
||||
Scope* originalScope = m_scope;
|
||||
|
||||
yulAssert(m_info.virtualBlocks.at(&_function), "");
|
||||
m_scope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
|
||||
yulAssert(m_scope, "Variable scope does not exist.");
|
||||
|
||||
for (auto const& v: _function.returnVariables)
|
||||
increaseRefIfFound(v.name);
|
||||
|
||||
VariableReferenceCounter{m_context, m_info}(_function.body);
|
||||
|
||||
m_scope = originalScope;
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::operator()(ForLoop const& _forLoop)
|
||||
{
|
||||
Scope* originalScope = m_scope;
|
||||
// Special scoping rules.
|
||||
m_scope = m_info.scopes.at(&_forLoop.pre).get();
|
||||
|
||||
walkVector(_forLoop.pre.statements);
|
||||
visit(*_forLoop.condition);
|
||||
(*this)(_forLoop.body);
|
||||
(*this)(_forLoop.post);
|
||||
|
||||
m_scope = originalScope;
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::operator()(Block const& _block)
|
||||
{
|
||||
Scope* originalScope = m_scope;
|
||||
m_scope = m_info.scopes.at(&_block).get();
|
||||
|
||||
ASTWalker::operator()(_block);
|
||||
|
||||
m_scope = originalScope;
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::increaseRefIfFound(YulString _variableName)
|
||||
{
|
||||
m_scope->lookup(_variableName, GenericVisitor{
|
||||
[&](Scope::Variable const& _var)
|
||||
{
|
||||
++m_context.variableReferences[&_var];
|
||||
},
|
||||
[](Scope::Function const&) { }
|
||||
});
|
||||
}
|
||||
|
||||
CodeTransform::CodeTransform(
|
||||
AbstractAssembly& _assembly,
|
||||
AsmAnalysisInfo& _analysisInfo,
|
||||
@ -127,7 +73,7 @@ CodeTransform::CodeTransform(
|
||||
// initialize
|
||||
m_context = make_shared<Context>();
|
||||
if (m_allowStackOpt)
|
||||
VariableReferenceCounter{*m_context, m_info}(_block);
|
||||
m_context->variableReferences = VariableReferenceCounter::run(m_info, _block);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
/**
|
||||
* Common code generator for translating Yul / inline assembly to EVM and EVM1.5.
|
||||
* Code generator for translating Yul / inline assembly to EVM.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@ -24,6 +24,7 @@
|
||||
#include <libyul/backends/evm/EVMAssembly.h>
|
||||
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
#include <libyul/backends/evm/VariableReferenceCounter.h>
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/AST.h>
|
||||
#include <libyul/Scope.h>
|
||||
@ -41,23 +42,6 @@ namespace solidity::yul
|
||||
struct AsmAnalysisInfo;
|
||||
class EVMAssembly;
|
||||
|
||||
struct StackTooDeepError: virtual YulException
|
||||
{
|
||||
StackTooDeepError(YulString _variable, int _depth, std::string const& _message):
|
||||
variable(_variable), depth(_depth)
|
||||
{
|
||||
*this << util::errinfo_comment(_message);
|
||||
}
|
||||
StackTooDeepError(YulString _functionName, YulString _variable, int _depth, std::string const& _message):
|
||||
functionName(_functionName), variable(_variable), depth(_depth)
|
||||
{
|
||||
*this << util::errinfo_comment(_message);
|
||||
}
|
||||
YulString functionName;
|
||||
YulString variable;
|
||||
int depth;
|
||||
};
|
||||
|
||||
struct CodeTransformContext
|
||||
{
|
||||
std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs;
|
||||
@ -79,38 +63,6 @@ struct CodeTransformContext
|
||||
std::stack<ForLoopLabels> forLoopStack;
|
||||
};
|
||||
|
||||
/**
|
||||
* Counts the number of references to a variable. This includes actual (read) references
|
||||
* but also assignments to the variable. It does not include the declaration itself or
|
||||
* function parameters, but it does include function return parameters.
|
||||
*
|
||||
* This component can handle multiple variables of the same name.
|
||||
*
|
||||
* Can only be applied to strict assembly.
|
||||
*/
|
||||
class VariableReferenceCounter: public yul::ASTWalker
|
||||
{
|
||||
public:
|
||||
explicit VariableReferenceCounter(
|
||||
CodeTransformContext& _context,
|
||||
AsmAnalysisInfo const& _assemblyInfo
|
||||
): m_context(_context), m_info(_assemblyInfo)
|
||||
{}
|
||||
|
||||
public:
|
||||
void operator()(Identifier const& _identifier) override;
|
||||
void operator()(FunctionDefinition const&) override;
|
||||
void operator()(ForLoop const&) override;
|
||||
void operator()(Block const& _block) override;
|
||||
|
||||
private:
|
||||
void increaseRefIfFound(YulString _variableName);
|
||||
|
||||
CodeTransformContext& m_context;
|
||||
AsmAnalysisInfo const& m_info;
|
||||
Scope* m_scope = nullptr;
|
||||
};
|
||||
|
||||
class CodeTransform
|
||||
{
|
||||
public:
|
||||
|
83
libyul/backends/evm/VariableReferenceCounter.cpp
Normal file
83
libyul/backends/evm/VariableReferenceCounter.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
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
|
||||
/**
|
||||
* Counts the number of references to a variable.
|
||||
*/
|
||||
#include <libyul/backends/evm/VariableReferenceCounter.h>
|
||||
#include <libyul/AsmAnalysisInfo.h>
|
||||
#include <libyul/AST.h>
|
||||
|
||||
#include <libsolutil/Visitor.h>
|
||||
|
||||
using namespace solidity::yul;
|
||||
|
||||
void VariableReferenceCounter::operator()(Identifier const& _identifier)
|
||||
{
|
||||
increaseRefIfFound(_identifier.name);
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::operator()(FunctionDefinition const& _function)
|
||||
{
|
||||
Scope* originalScope = m_scope;
|
||||
|
||||
yulAssert(m_info.virtualBlocks.at(&_function), "");
|
||||
m_scope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
|
||||
yulAssert(m_scope, "Variable scope does not exist.");
|
||||
|
||||
for (auto const& v: _function.returnVariables)
|
||||
increaseRefIfFound(v.name);
|
||||
|
||||
(*this)(_function.body);
|
||||
|
||||
m_scope = originalScope;
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::operator()(ForLoop const& _forLoop)
|
||||
{
|
||||
Scope* originalScope = m_scope;
|
||||
// Special scoping rules.
|
||||
m_scope = m_info.scopes.at(&_forLoop.pre).get();
|
||||
|
||||
walkVector(_forLoop.pre.statements);
|
||||
visit(*_forLoop.condition);
|
||||
(*this)(_forLoop.body);
|
||||
(*this)(_forLoop.post);
|
||||
|
||||
m_scope = originalScope;
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::operator()(Block const& _block)
|
||||
{
|
||||
Scope* originalScope = m_scope;
|
||||
m_scope = m_info.scopes.at(&_block).get();
|
||||
|
||||
ASTWalker::operator()(_block);
|
||||
|
||||
m_scope = originalScope;
|
||||
}
|
||||
|
||||
void VariableReferenceCounter::increaseRefIfFound(YulString _variableName)
|
||||
{
|
||||
m_scope->lookup(_variableName, util::GenericVisitor{
|
||||
[&](Scope::Variable const& _var)
|
||||
{
|
||||
++m_variableReferences[&_var];
|
||||
},
|
||||
[](Scope::Function const&) { }
|
||||
});
|
||||
}
|
68
libyul/backends/evm/VariableReferenceCounter.h
Normal file
68
libyul/backends/evm/VariableReferenceCounter.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
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
|
||||
/**
|
||||
* Counts the number of references to a variable.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/Scope.h>
|
||||
|
||||
namespace solidity::yul
|
||||
{
|
||||
struct AsmAnalysisInfo;
|
||||
|
||||
/**
|
||||
* Counts the number of references to a variable. This includes actual (read) references
|
||||
* but also assignments to the variable. It does not include the declaration itself or
|
||||
* function parameters, but it does include function return parameters.
|
||||
*
|
||||
* This component can handle multiple variables of the same name.
|
||||
*
|
||||
* Can only be applied to strict assembly.
|
||||
*/
|
||||
struct VariableReferenceCounter: public yul::ASTWalker
|
||||
{
|
||||
public:
|
||||
static std::map<Scope::Variable const*, unsigned> run(AsmAnalysisInfo const& _assemblyInfo, Block const& _block)
|
||||
{
|
||||
VariableReferenceCounter variableReferenceCounter(_assemblyInfo);
|
||||
variableReferenceCounter(_block);
|
||||
return std::move(variableReferenceCounter.m_variableReferences);
|
||||
}
|
||||
|
||||
protected:
|
||||
void operator()(Block const& _block) override;
|
||||
void operator()(Identifier const& _identifier) override;
|
||||
void operator()(FunctionDefinition const&) override;
|
||||
void operator()(ForLoop const&) override;
|
||||
|
||||
private:
|
||||
explicit VariableReferenceCounter(
|
||||
AsmAnalysisInfo const& _assemblyInfo
|
||||
): m_info(_assemblyInfo)
|
||||
{}
|
||||
|
||||
void increaseRefIfFound(YulString _variableName);
|
||||
|
||||
AsmAnalysisInfo const& m_info;
|
||||
Scope* m_scope = nullptr;
|
||||
std::map<Scope::Variable const*, unsigned> m_variableReferences;
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user