2017-05-23 17:11:14 +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-05-23 17:11:14 +00:00
|
|
|
/**
|
2018-06-18 21:26:31 +00:00
|
|
|
* Common code generator for translating Yul / inline assembly to EVM and EVM1.5.
|
2017-05-23 17:11:14 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-12 15:47:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/backends/evm/EVMAssembly.h>
|
2017-05-23 17:21:14 +00:00
|
|
|
|
2018-12-06 23:56:16 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2018-09-25 02:47:25 +00:00
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
2021-02-04 20:52:07 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-11-25 18:10:31 +00:00
|
|
|
#include <libyul/Scope.h>
|
2017-05-23 17:21:14 +00:00
|
|
|
|
2019-10-28 10:39:30 +00:00
|
|
|
#include <optional>
|
2019-03-04 14:38:05 +00:00
|
|
|
#include <stack>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::langutil
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
class ErrorReporter;
|
2017-05-23 17:21:14 +00:00
|
|
|
}
|
2018-11-21 11:42:34 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
2018-11-21 11:42:34 +00:00
|
|
|
struct AsmAnalysisInfo;
|
2017-05-24 16:34:19 +00:00
|
|
|
class EVMAssembly;
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2019-01-29 09:51:25 +00:00
|
|
|
struct StackTooDeepError: virtual YulException
|
|
|
|
{
|
2020-10-31 01:59:28 +00:00
|
|
|
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):
|
2019-01-29 09:51:25 +00:00
|
|
|
functionName(_functionName), variable(_variable), depth(_depth)
|
2020-10-31 01:59:28 +00:00
|
|
|
{
|
|
|
|
*this << util::errinfo_comment(_message);
|
|
|
|
}
|
2019-01-29 09:51:25 +00:00
|
|
|
YulString functionName;
|
|
|
|
YulString variable;
|
|
|
|
int depth;
|
|
|
|
};
|
2019-01-28 16:19:41 +00:00
|
|
|
|
2018-09-25 02:47:25 +00:00
|
|
|
struct CodeTransformContext
|
|
|
|
{
|
|
|
|
std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs;
|
2020-06-02 13:34:28 +00:00
|
|
|
std::map<Scope::Variable const*, size_t> variableStackHeights;
|
2018-09-25 02:47:25 +00:00
|
|
|
std::map<Scope::Variable const*, unsigned> variableReferences;
|
2019-03-04 14:38:05 +00:00
|
|
|
|
|
|
|
struct JumpInfo
|
|
|
|
{
|
|
|
|
AbstractAssembly::LabelID label; ///< Jump's LabelID to jump to.
|
|
|
|
int targetStackHeight; ///< Stack height after the jump.
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ForLoopLabels
|
|
|
|
{
|
|
|
|
JumpInfo post; ///< Jump info for jumping to post branch.
|
|
|
|
JumpInfo done; ///< Jump info for jumping to done branch.
|
|
|
|
};
|
|
|
|
|
|
|
|
std::stack<ForLoopLabels> forLoopStack;
|
2018-09-25 02:47:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
2020-03-31 23:58:22 +00:00
|
|
|
void operator()(Identifier const& _identifier) override;
|
|
|
|
void operator()(FunctionDefinition const&) override;
|
|
|
|
void operator()(ForLoop const&) override;
|
|
|
|
void operator()(Block const& _block) override;
|
2018-09-25 02:47:25 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void increaseRefIfFound(YulString _variableName);
|
|
|
|
|
|
|
|
CodeTransformContext& m_context;
|
|
|
|
AsmAnalysisInfo const& m_info;
|
|
|
|
Scope* m_scope = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
class CodeTransform
|
2017-05-23 17:11:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Create the code transformer.
|
2017-05-23 17:11:14 +00:00
|
|
|
/// @param _identifierAccess used to resolve identifiers external to the inline assembly
|
2019-01-28 16:19:41 +00:00
|
|
|
/// As a side-effect of its construction, translates the Yul code and appends it to the
|
|
|
|
/// given assembly.
|
|
|
|
/// Throws StackTooDeepError if a variable is not accessible or if a function has too
|
|
|
|
/// many parameters.
|
2017-05-23 17:21:14 +00:00
|
|
|
CodeTransform(
|
2018-11-21 11:42:34 +00:00
|
|
|
AbstractAssembly& _assembly,
|
|
|
|
AsmAnalysisInfo& _analysisInfo,
|
2018-09-25 02:47:25 +00:00
|
|
|
Block const& _block,
|
2018-12-06 23:56:16 +00:00
|
|
|
EVMDialect const& _dialect,
|
2019-05-15 21:40:30 +00:00
|
|
|
BuiltinContext& _builtinContext,
|
2018-09-25 02:47:25 +00:00
|
|
|
bool _allowStackOpt = false,
|
2017-08-25 15:04:31 +00:00
|
|
|
ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(),
|
|
|
|
bool _useNamedLabelsForFunctions = false
|
2017-06-13 19:58:25 +00:00
|
|
|
): CodeTransform(
|
|
|
|
_assembly,
|
|
|
|
_analysisInfo,
|
2018-09-25 02:47:25 +00:00
|
|
|
_block,
|
|
|
|
_allowStackOpt,
|
2018-12-06 17:07:08 +00:00
|
|
|
_dialect,
|
2019-05-15 21:40:30 +00:00
|
|
|
_builtinContext,
|
2017-06-13 19:58:25 +00:00
|
|
|
_identifierAccess,
|
2017-08-25 15:04:31 +00:00
|
|
|
_useNamedLabelsForFunctions,
|
2021-02-04 20:52:07 +00:00
|
|
|
nullptr,
|
|
|
|
{},
|
|
|
|
std::nullopt
|
2017-06-13 19:58:25 +00:00
|
|
|
)
|
2017-05-23 17:11:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-29 09:51:25 +00:00
|
|
|
std::vector<StackTooDeepError> const& stackErrors() const { return m_stackErrors; }
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
protected:
|
2018-09-25 02:47:25 +00:00
|
|
|
using Context = CodeTransformContext;
|
2017-06-13 19:58:25 +00:00
|
|
|
|
2017-05-23 17:11:14 +00:00
|
|
|
CodeTransform(
|
2018-11-21 11:42:34 +00:00
|
|
|
AbstractAssembly& _assembly,
|
|
|
|
AsmAnalysisInfo& _analysisInfo,
|
2018-09-25 02:47:25 +00:00
|
|
|
Block const& _block,
|
|
|
|
bool _allowStackOpt,
|
2018-12-06 23:56:16 +00:00
|
|
|
EVMDialect const& _dialect,
|
2019-05-15 21:40:30 +00:00
|
|
|
BuiltinContext& _builtinContext,
|
2020-04-01 03:04:29 +00:00
|
|
|
ExternalIdentifierAccess _identifierAccess,
|
2017-08-25 15:04:31 +00:00
|
|
|
bool _useNamedLabelsForFunctions,
|
2021-02-04 20:52:07 +00:00
|
|
|
std::shared_ptr<Context> _context,
|
|
|
|
std::vector<TypedName> _delayedReturnVariables,
|
|
|
|
std::optional<AbstractAssembly::LabelID> _functionExitLabel
|
2018-09-25 02:47:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
void decreaseReference(YulString _name, Scope::Variable const& _var);
|
|
|
|
bool unreferenced(Scope::Variable const& _var) const;
|
|
|
|
/// Marks slots of variables that are not used anymore
|
|
|
|
/// and were defined in the current scope for reuse.
|
2020-07-10 15:39:23 +00:00
|
|
|
/// Also POPs unused topmost stack slots,
|
|
|
|
/// unless @a _popUnusedSlotsAtStackTop is set to false.
|
|
|
|
void freeUnusedVariables(bool _popUnusedSlotsAtStackTop = true);
|
2018-09-25 02:47:25 +00:00
|
|
|
/// Marks the stack slot of @a _var to be reused.
|
|
|
|
void deleteVariable(Scope::Variable const& _var);
|
2017-05-23 17:11:14 +00:00
|
|
|
|
|
|
|
public:
|
2017-11-28 11:45:37 +00:00
|
|
|
void operator()(Literal const& _literal);
|
|
|
|
void operator()(Identifier const& _identifier);
|
|
|
|
void operator()(FunctionCall const&);
|
2017-12-08 13:01:22 +00:00
|
|
|
void operator()(ExpressionStatement const& _statement);
|
2017-11-28 11:45:37 +00:00
|
|
|
void operator()(Assignment const& _assignment);
|
|
|
|
void operator()(VariableDeclaration const& _varDecl);
|
|
|
|
void operator()(If const& _if);
|
|
|
|
void operator()(Switch const& _switch);
|
|
|
|
void operator()(FunctionDefinition const&);
|
|
|
|
void operator()(ForLoop const&);
|
2019-03-04 14:38:05 +00:00
|
|
|
void operator()(Break const&);
|
|
|
|
void operator()(Continue const&);
|
2019-10-28 14:25:02 +00:00
|
|
|
void operator()(Leave const&);
|
2017-11-28 11:45:37 +00:00
|
|
|
void operator()(Block const& _block);
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2017-06-01 16:16:38 +00:00
|
|
|
private:
|
2017-11-28 11:45:37 +00:00
|
|
|
AbstractAssembly::LabelID labelFromIdentifier(Identifier const& _identifier);
|
2018-11-21 11:42:34 +00:00
|
|
|
AbstractAssembly::LabelID functionEntryID(YulString _name, Scope::Function const& _function);
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Generates code for an expression that is supposed to return a single value.
|
2017-12-08 13:01:22 +00:00
|
|
|
void visitExpression(Expression const& _expression);
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2017-11-28 11:45:37 +00:00
|
|
|
void visitStatements(std::vector<Statement> const& _statements);
|
2017-06-14 13:35:51 +00:00
|
|
|
|
2017-06-09 11:59:16 +00:00
|
|
|
/// Pops all variables declared in the block and checks that the stack height is equal
|
2020-09-07 15:24:03 +00:00
|
|
|
/// to @a _blockStartStackHeight.
|
2021-02-04 20:52:07 +00:00
|
|
|
void finalizeBlock(Block const& _block, std::optional<int> _blockStartStackHeight);
|
2017-06-09 11:59:16 +00:00
|
|
|
|
2017-11-28 11:45:37 +00:00
|
|
|
void generateMultiAssignment(std::vector<Identifier> const& _variableNames);
|
|
|
|
void generateAssignment(Identifier const& _variableName);
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2017-06-06 13:37:43 +00:00
|
|
|
/// Determines the stack height difference to the given variables. Throws
|
|
|
|
/// if it is not yet in scope or the height difference is too large. Returns
|
|
|
|
/// the (positive) stack height difference otherwise.
|
2020-07-20 13:43:52 +00:00
|
|
|
/// @param _forSwap if true, produces stack error if the difference is invalid for a swap
|
|
|
|
/// opcode, otherwise checks for validity for a dup opcode.
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t variableHeightDiff(Scope::Variable const& _var, YulString _name, bool _forSwap);
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
/// Determines the stack height of the given variable. Throws if the variable is not in scope.
|
|
|
|
int variableStackHeight(YulString _name) const;
|
|
|
|
|
2017-09-20 07:52:10 +00:00
|
|
|
void expectDeposit(int _deposit, int _oldHeight) const;
|
2017-05-23 17:11:14 +00:00
|
|
|
|
2019-01-29 09:51:25 +00:00
|
|
|
/// Stores the stack error in the list of errors, appends an invalid opcode
|
|
|
|
/// and corrects the stack height to the target stack height.
|
|
|
|
void stackError(StackTooDeepError _error, int _targetStackSize);
|
|
|
|
|
2019-03-11 15:20:18 +00:00
|
|
|
/// Ensures stack height is down to @p _targetDepth by appending POP instructions to the output assembly.
|
|
|
|
/// Returns the number of POP statements that have been appended.
|
|
|
|
int appendPopUntil(int _targetDepth);
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
/// Allocates stack slots for remaining delayed return values and sets the function exit stack height.
|
|
|
|
void setupReturnVariablesAndFunctionExit();
|
|
|
|
bool returnVariablesAndFunctionExitAreSetup() const
|
|
|
|
{
|
|
|
|
return m_functionExitStackHeight.has_value();
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
AbstractAssembly& m_assembly;
|
|
|
|
AsmAnalysisInfo& m_info;
|
|
|
|
Scope* m_scope = nullptr;
|
2018-12-06 23:56:16 +00:00
|
|
|
EVMDialect const& m_dialect;
|
2019-05-15 21:40:30 +00:00
|
|
|
BuiltinContext& m_builtinContext;
|
2018-09-25 02:47:25 +00:00
|
|
|
bool const m_allowStackOpt = true;
|
2018-12-06 23:56:16 +00:00
|
|
|
bool const m_useNamedLabelsForFunctions = false;
|
2017-05-23 17:11:14 +00:00
|
|
|
ExternalIdentifierAccess m_identifierAccess;
|
2017-06-13 19:58:25 +00:00
|
|
|
std::shared_ptr<Context> m_context;
|
2018-09-25 02:47:25 +00:00
|
|
|
|
|
|
|
/// Set of variables whose reference counter has reached zero,
|
|
|
|
/// and whose stack slot will be marked as unused once we reach
|
|
|
|
/// statement level in the scope where the variable was defined.
|
|
|
|
std::set<Scope::Variable const*> m_variablesScheduledForDeletion;
|
|
|
|
std::set<int> m_unusedStackSlots;
|
2019-01-29 09:51:25 +00:00
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
/// A list of return variables for which no stack slots have been assigned yet.
|
|
|
|
std::vector<TypedName> m_delayedReturnVariables;
|
|
|
|
|
|
|
|
/// Function exit label. Used as jump target for ``leave``.
|
|
|
|
std::optional<AbstractAssembly::LabelID> m_functionExitLabel;
|
|
|
|
/// The required stack height at the function exit label.
|
|
|
|
/// This is the minimal stack height covering all return variables. Only set after all
|
|
|
|
/// return variables were assigned slots.
|
|
|
|
std::optional<int> m_functionExitStackHeight;
|
|
|
|
|
2019-01-29 09:51:25 +00:00
|
|
|
std::vector<StackTooDeepError> m_stackErrors;
|
2017-05-23 17:11:14 +00:00
|
|
|
};
|
2017-05-23 17:21:14 +00:00
|
|
|
|
|
|
|
}
|