2014-10-30 00:20:32 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-10-30 00:20:32 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-10-30 00:20:32 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-30 00:20:32 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-10-30 00:20:32 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Utilities for the solidity compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libsolidity/ast/ASTAnnotations.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/ASTForward.h>
|
|
|
|
#include <libsolidity/ast/Types.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libsolidity/codegen/ABIFunctions.h>
|
2017-01-19 16:21:55 +00:00
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
#include <libsolidity/interface/DebugSettings.h>
|
2017-07-17 11:12:00 +00:00
|
|
|
#include <libsolidity/interface/OptimiserSettings.h>
|
|
|
|
|
2017-01-19 16:21:55 +00:00
|
|
|
#include <libevmasm/Assembly.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2020-02-06 15:32:33 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
2020-10-12 14:01:45 +00:00
|
|
|
#include <libsolutil/ErrorCodes.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2020-02-06 15:32:33 +00:00
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <functional>
|
2017-01-19 16:21:55 +00:00
|
|
|
#include <ostream>
|
|
|
|
#include <stack>
|
|
|
|
#include <queue>
|
|
|
|
#include <utility>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
2017-01-19 16:21:55 +00:00
|
|
|
|
2021-05-27 15:41:04 +00:00
|
|
|
namespace solidity::frontend
|
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2019-01-10 15:44:31 +00:00
|
|
|
class Compiler;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Context to be shared by all units that compile the same contract.
|
|
|
|
* It stores the generated bytecode and the position of identifiers in memory and on the stack.
|
|
|
|
*/
|
|
|
|
class CompilerContext
|
|
|
|
{
|
|
|
|
public:
|
2020-01-22 14:48:56 +00:00
|
|
|
explicit CompilerContext(
|
|
|
|
langutil::EVMVersion _evmVersion,
|
|
|
|
RevertStrings _revertStrings,
|
|
|
|
CompilerContext* _runtimeContext = nullptr
|
|
|
|
):
|
2022-03-07 11:49:23 +00:00
|
|
|
m_asm(std::make_shared<evmasm::Assembly>(_runtimeContext != nullptr, std::string{})),
|
2018-02-21 22:56:42 +00:00
|
|
|
m_evmVersion(_evmVersion),
|
2020-01-22 14:48:56 +00:00
|
|
|
m_revertStrings(_revertStrings),
|
2020-03-10 13:30:04 +00:00
|
|
|
m_reservedMemory{0},
|
2018-04-05 14:56:02 +00:00
|
|
|
m_runtimeContext(_runtimeContext),
|
2020-03-02 16:23:58 +00:00
|
|
|
m_abiFunctions(m_evmVersion, m_revertStrings, m_yulFunctionCollector),
|
|
|
|
m_yulUtilFunctions(m_evmVersion, m_revertStrings, m_yulFunctionCollector)
|
2016-11-08 14:25:32 +00:00
|
|
|
{
|
2016-11-10 17:16:21 +00:00
|
|
|
if (m_runtimeContext)
|
2016-11-11 10:41:50 +00:00
|
|
|
m_runtimeSub = size_t(m_asm->newSub(m_runtimeContext->m_asm).data());
|
2016-11-08 14:25:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion const& evmVersion() const { return m_evmVersion; }
|
2018-02-21 22:56:42 +00:00
|
|
|
|
2020-10-22 17:19:14 +00:00
|
|
|
void setUseABICoderV2(bool _value) { m_useABICoderV2 = _value; }
|
|
|
|
bool useABICoderV2() const { return m_useABICoderV2; }
|
2017-08-10 15:54:08 +00:00
|
|
|
|
2015-03-13 18:48:24 +00:00
|
|
|
void addStateVariable(VariableDeclaration const& _declaration, u256 const& _storageOffset, unsigned _byteOffset);
|
2020-03-10 13:30:04 +00:00
|
|
|
void addImmutable(VariableDeclaration const& _declaration);
|
|
|
|
|
|
|
|
/// @returns the reserved memory for storing the value of the immutable @a _variable during contract creation.
|
|
|
|
size_t immutableMemoryOffset(VariableDeclaration const& _variable) const;
|
|
|
|
/// @returns a list of slot names referring to the stack slots of an immutable variable.
|
|
|
|
static std::vector<std::string> immutableVariableSlotNames(VariableDeclaration const& _variable);
|
|
|
|
|
|
|
|
/// @returns the reserved memory and resets it to mark it as used.
|
|
|
|
size_t reservedMemory();
|
|
|
|
|
2015-01-23 01:35:27 +00:00
|
|
|
void addVariable(VariableDeclaration const& _declaration, unsigned _offsetToCurrent = 0);
|
2018-05-03 14:40:33 +00:00
|
|
|
void removeVariable(Declaration const& _declaration);
|
|
|
|
/// Removes all local variables currently allocated above _stackHeight.
|
|
|
|
void removeVariablesAboveStackHeight(unsigned _stackHeight);
|
2018-07-10 16:39:26 +00:00
|
|
|
/// Returns the number of currently allocated local variables.
|
|
|
|
unsigned numberOfLocalVariables() const;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2019-01-10 15:44:31 +00:00
|
|
|
void setOtherCompilers(std::map<ContractDefinition const*, std::shared_ptr<Compiler const>> const& _otherCompilers) { m_otherCompilers = _otherCompilers; }
|
2019-12-11 16:31:36 +00:00
|
|
|
std::shared_ptr<evmasm::Assembly> compiledContract(ContractDefinition const& _contract) const;
|
|
|
|
std::shared_ptr<evmasm::Assembly> compiledContractRuntime(ContractDefinition const& _contract) const;
|
2014-12-12 15:49:26 +00:00
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
void setStackOffset(int _offset) { m_asm->setDeposit(_offset); }
|
|
|
|
void adjustStackOffset(int _adjustment) { m_asm->adjustDeposit(_adjustment); }
|
|
|
|
unsigned stackHeight() const { solAssert(m_asm->deposit() >= 0, ""); return unsigned(m_asm->deposit()); }
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-11-07 01:06:37 +00:00
|
|
|
bool isLocalVariable(Declaration const* _declaration) const;
|
2015-01-13 10:18:08 +00:00
|
|
|
bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration) != 0; }
|
2014-11-07 01:06:37 +00:00
|
|
|
|
2015-05-26 09:27:59 +00:00
|
|
|
/// @returns the entry label of the given function and creates it if it does not exist yet.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem functionEntryLabel(Declaration const& _declaration);
|
2015-05-26 09:27:59 +00:00
|
|
|
/// @returns the entry label of the given function. Might return an AssemblyItem of type
|
|
|
|
/// UndefinedItem if it does not exist yet.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem functionEntryLabelIfExists(Declaration const& _declaration) const;
|
2016-11-10 17:16:21 +00:00
|
|
|
/// @returns the function that overrides the given declaration from the most derived class just
|
2015-01-27 13:32:59 +00:00
|
|
|
/// above _base in the current inheritance hierarchy.
|
2016-11-10 17:16:21 +00:00
|
|
|
FunctionDefinition const& superFunction(FunctionDefinition const& _function, ContractDefinition const& _base);
|
2020-03-24 17:25:59 +00:00
|
|
|
/// Sets the contract currently being compiled - the most derived one.
|
|
|
|
void setMostDerivedContract(ContractDefinition const& _contract) { m_mostDerivedContract = &_contract; }
|
|
|
|
ContractDefinition const& mostDerivedContract() const;
|
2015-02-27 16:41:22 +00:00
|
|
|
|
2020-07-22 08:28:04 +00:00
|
|
|
void setArithmetic(Arithmetic _value) { m_arithmetic = _value; }
|
|
|
|
Arithmetic arithmetic() const { return m_arithmetic; }
|
|
|
|
|
2016-05-02 23:13:41 +00:00
|
|
|
/// @returns the next function in the queue of functions that are still to be compiled
|
|
|
|
/// (i.e. that were referenced during compilation but where we did not yet generate code for).
|
|
|
|
/// Returns nullptr if the queue is empty. Does not remove the function from the queue,
|
|
|
|
/// that will only be done by startFunction below.
|
|
|
|
Declaration const* nextFunctionToCompile() const;
|
2015-01-27 13:32:59 +00:00
|
|
|
/// Resets function specific members, inserts the function entry label and marks the function
|
|
|
|
/// as "having code".
|
|
|
|
void startFunction(Declaration const& _function);
|
|
|
|
|
2017-01-19 16:21:55 +00:00
|
|
|
/// Appends a call to the named low-level function and inserts the generator into the
|
|
|
|
/// list of low-level-functions to be generated, unless it already exists.
|
|
|
|
/// Note that the generator should not assume that objects are still alive when it is called,
|
|
|
|
/// unless they are guaranteed to be alive for the whole run of the compiler (AST nodes, for example).
|
|
|
|
void callLowLevelFunction(
|
|
|
|
std::string const& _name,
|
|
|
|
unsigned _inArgs,
|
|
|
|
unsigned _outArgs,
|
|
|
|
std::function<void(CompilerContext&)> const& _generator
|
|
|
|
);
|
2020-03-02 15:32:30 +00:00
|
|
|
|
2020-03-02 16:23:58 +00:00
|
|
|
/// Appends a call to a yul function and registers the function as externally used.
|
|
|
|
void callYulFunction(
|
2020-03-02 15:32:30 +00:00
|
|
|
std::string const& _name,
|
|
|
|
unsigned _inArgs,
|
|
|
|
unsigned _outArgs
|
|
|
|
);
|
|
|
|
|
2017-01-26 15:35:51 +00:00
|
|
|
/// Returns the tag of the named low-level function and inserts the generator into the
|
|
|
|
/// list of low-level-functions to be generated, unless it already exists.
|
|
|
|
/// Note that the generator should not assume that objects are still alive when it is called,
|
|
|
|
/// unless they are guaranteed to be alive for the whole run of the compiler (AST nodes, for example).
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem lowLevelFunctionTag(
|
2017-01-26 15:35:51 +00:00
|
|
|
std::string const& _name,
|
|
|
|
unsigned _inArgs,
|
|
|
|
unsigned _outArgs,
|
|
|
|
std::function<void(CompilerContext&)> const& _generator
|
|
|
|
);
|
2017-01-19 16:21:55 +00:00
|
|
|
/// Generates the code for missing low-level functions, i.e. calls the generators passed above.
|
|
|
|
void appendMissingLowLevelFunctions();
|
2017-08-25 16:58:12 +00:00
|
|
|
ABIFunctions& abiFunctions() { return m_abiFunctions; }
|
2020-03-02 15:32:30 +00:00
|
|
|
YulUtilFunctions& utilFunctions() { return m_yulUtilFunctions; }
|
2020-05-28 11:17:16 +00:00
|
|
|
|
|
|
|
/// Appends concatenation of all generated Yul functions to the bytecode
|
|
|
|
/// and stores the Yul source code to be returned by @a generatedYulUtilityCode.
|
|
|
|
/// Should be called exactly once on each context.
|
|
|
|
void appendYulUtilityFunctions(OptimiserSettings const& _optimiserSettings);
|
|
|
|
bool appendYulUtilityFunctionsRan() const { return m_appendYulUtilityFunctionsRan; }
|
|
|
|
std::string const& generatedYulUtilityCode() const { return m_generatedYulUtilityCode; }
|
|
|
|
static std::string yulUtilityFileName() { return "#utility.yul"; }
|
2017-01-17 09:47:57 +00:00
|
|
|
|
2015-01-23 01:35:27 +00:00
|
|
|
/// Returns the distance of the given local variable from the bottom of the stack (of the current function).
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned baseStackOffsetOfVariable(Declaration const& _declaration) const;
|
|
|
|
/// If supplied by a value returned by @ref baseStackOffsetOfVariable(variable), returns
|
2014-11-07 01:06:37 +00:00
|
|
|
/// the distance of that variable from the current top of the stack.
|
|
|
|
unsigned baseToCurrentStackOffset(unsigned _baseOffset) const;
|
2015-01-14 10:57:22 +00:00
|
|
|
/// Converts an offset relative to the current stack height to a value that can be used later
|
|
|
|
/// with baseToCurrentStackOffset to point to the same stack element.
|
|
|
|
unsigned currentToBaseStackOffset(unsigned _offset) const;
|
2015-03-31 09:07:10 +00:00
|
|
|
/// @returns pair of slot and byte offset of the value inside this slot.
|
2015-08-31 16:44:29 +00:00
|
|
|
std::pair<u256, unsigned> storageLocationOfVariable(Declaration const& _declaration) const;
|
2014-11-07 01:06:37 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
/// Appends a JUMPI instruction to a new tag and @returns the tag
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem appendConditionalJump() { return m_asm->appendJumpI().tag(); }
|
2014-10-30 00:20:32 +00:00
|
|
|
/// Appends a JUMPI instruction to @a _tag
|
2019-12-11 16:31:36 +00:00
|
|
|
CompilerContext& appendConditionalJumpTo(evmasm::AssemblyItem const& _tag) { m_asm->appendJumpI(_tag); return *this; }
|
2014-10-30 00:20:32 +00:00
|
|
|
/// Appends a JUMP to a new tag and @returns the tag
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem appendJumpToNew() { return m_asm->appendJump().tag(); }
|
2014-11-10 16:31:09 +00:00
|
|
|
/// Appends a JUMP to a tag already on the stack
|
2019-12-11 16:31:36 +00:00
|
|
|
CompilerContext& appendJump(evmasm::AssemblyItem::JumpType _jumpType = evmasm::AssemblyItem::JumpType::Ordinary);
|
2020-10-12 14:01:45 +00:00
|
|
|
/// Appends code to revert with a Panic(uint256) error.
|
|
|
|
CompilerContext& appendPanic(util::PanicCode _code);
|
|
|
|
/// Appends code to revert with a Panic(uint256) error if the topmost stack element is nonzero.
|
|
|
|
CompilerContext& appendConditionalPanic(util::PanicCode _code);
|
2017-05-24 09:47:43 +00:00
|
|
|
/// Appends a REVERT(0, 0) call
|
2020-01-22 14:48:56 +00:00
|
|
|
/// @param _message is an optional revert message used in debug mode
|
|
|
|
CompilerContext& appendRevert(std::string const& _message = "");
|
2017-12-30 19:13:41 +00:00
|
|
|
/// Appends a conditional REVERT-call, either forwarding the RETURNDATA or providing the
|
|
|
|
/// empty string. Consumes the condition.
|
2018-03-02 16:26:16 +00:00
|
|
|
/// If the current EVM version does not support RETURNDATA, uses REVERT but does not forward
|
|
|
|
/// the data.
|
2020-01-22 14:48:56 +00:00
|
|
|
/// @param _message is an optional revert message used in debug mode
|
|
|
|
CompilerContext& appendConditionalRevert(bool _forwardReturnData = false, std::string const& _message = "");
|
2014-10-30 00:20:32 +00:00
|
|
|
/// Appends a JUMP to a specific tag
|
2018-11-13 09:53:40 +00:00
|
|
|
CompilerContext& appendJumpTo(
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem const& _tag,
|
|
|
|
evmasm::AssemblyItem::JumpType _jumpType = evmasm::AssemblyItem::JumpType::Ordinary
|
2018-11-13 09:53:40 +00:00
|
|
|
) { *m_asm << _tag.pushTag(); return appendJump(_jumpType); }
|
2014-10-30 00:20:32 +00:00
|
|
|
/// Appends pushing of a new tag and @returns the new tag.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem pushNewTag() { return m_asm->append(m_asm->newPushTag()).tag(); }
|
2014-10-30 00:20:32 +00:00
|
|
|
/// @returns a new tag without pushing any opcodes or data
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem newTag() { return m_asm->newTag(); }
|
2017-08-25 15:04:31 +00:00
|
|
|
/// @returns a new tag identified by name.
|
2020-05-07 12:46:47 +00:00
|
|
|
evmasm::AssemblyItem namedTag(std::string const& _name, size_t _params, size_t _returns, std::optional<uint64_t> _sourceID)
|
|
|
|
{
|
|
|
|
return m_asm->namedTag(_name, _params, _returns, _sourceID);
|
|
|
|
}
|
2014-10-30 21:52:15 +00:00
|
|
|
/// Adds a subroutine to the code (in the data section) and pushes its size (via a tag)
|
2016-11-10 17:16:21 +00:00
|
|
|
/// on the stack. @returns the pushsub assembly item.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem addSubroutine(evmasm::AssemblyPointer const& _assembly) { return m_asm->appendSubroutine(_assembly); }
|
2017-08-29 12:26:23 +00:00
|
|
|
/// Pushes the size of the subroutine.
|
2017-06-14 19:38:59 +00:00
|
|
|
void pushSubroutineSize(size_t _subRoutine) { m_asm->pushSubroutineSize(_subRoutine); }
|
2016-11-11 10:41:50 +00:00
|
|
|
/// Pushes the offset of the subroutine.
|
2017-06-14 19:38:59 +00:00
|
|
|
void pushSubroutineOffset(size_t _subRoutine) { m_asm->pushSubroutineOffset(_subRoutine); }
|
2014-12-15 11:59:17 +00:00
|
|
|
/// Pushes the size of the final program
|
2016-11-11 10:41:50 +00:00
|
|
|
void appendProgramSize() { m_asm->appendProgramSize(); }
|
2014-12-12 15:49:26 +00:00
|
|
|
/// Adds data to the data section, pushes a reference to the stack
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem appendData(bytes const& _data) { return m_asm->append(_data); }
|
2015-09-10 17:40:07 +00:00
|
|
|
/// Appends the address (virtual, will be filled in by linker) of a library.
|
2016-11-11 10:41:50 +00:00
|
|
|
void appendLibraryAddress(std::string const& _identifier) { m_asm->appendLibraryAddress(_identifier); }
|
2020-03-10 13:30:04 +00:00
|
|
|
/// Appends an immutable variable. The value will be filled in by the constructor.
|
|
|
|
void appendImmutable(std::string const& _identifier) { m_asm->appendImmutable(_identifier); }
|
|
|
|
/// Appends an assignment to an immutable variable. Only valid in creation code.
|
|
|
|
void appendImmutableAssignment(std::string const& _identifier) { m_asm->appendImmutableAssignment(_identifier); }
|
2017-11-14 11:58:04 +00:00
|
|
|
/// Appends a zero-address that can be replaced by something else at deploy time (if the
|
|
|
|
/// position in bytecode is known).
|
2019-12-11 16:31:36 +00:00
|
|
|
void appendDeployTimeAddress() { m_asm->append(evmasm::PushDeployTimeAddress); }
|
2015-02-24 11:08:51 +00:00
|
|
|
/// Resets the stack of visited nodes with a new stack having only @c _node
|
|
|
|
void resetVisitedNodes(ASTNode const* _node);
|
2015-02-23 15:31:36 +00:00
|
|
|
/// Pops the stack of visited nodes
|
2015-03-07 22:08:39 +00:00
|
|
|
void popVisitedNodes() { m_visitedNodes.pop(); updateSourceLocation(); }
|
2015-02-23 15:31:36 +00:00
|
|
|
/// Pushes an ASTNode to the stack of visited nodes
|
2015-03-07 22:08:39 +00:00
|
|
|
void pushVisitedNodes(ASTNode const* _node) { m_visitedNodes.push(_node); updateSourceLocation(); }
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
/// Append elements to the current instruction list and adjust @a m_stackOffset.
|
2019-12-11 16:31:36 +00:00
|
|
|
CompilerContext& operator<<(evmasm::AssemblyItem const& _item) { m_asm->append(_item); return *this; }
|
|
|
|
CompilerContext& operator<<(evmasm::Instruction _instruction) { m_asm->append(_instruction); return *this; }
|
2016-11-11 10:41:50 +00:00
|
|
|
CompilerContext& operator<<(u256 const& _value) { m_asm->append(_value); return *this; }
|
|
|
|
CompilerContext& operator<<(bytes const& _data) { m_asm->append(_data); return *this; }
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2020-05-28 11:17:16 +00:00
|
|
|
/// Appends inline assembly (strict-EVM dialect for the current version).
|
|
|
|
/// @param _assembly the assembly text, should be a block.
|
2016-08-16 14:27:20 +00:00
|
|
|
/// @param _localVariables assigns stack positions to variables with the last one being the stack top
|
2018-01-17 11:05:43 +00:00
|
|
|
/// @param _externallyUsedFunctions a set of function names that are not to be renamed or removed.
|
2020-05-28 11:17:16 +00:00
|
|
|
/// @param _system if true, this is a "system-level" assembly where all functions use named labels
|
|
|
|
/// and the code is marked to be exported as "compiler-generated assembly utility file".
|
|
|
|
/// @param _optimiserSettings settings for the Yul optimiser, which is run in this function already.
|
|
|
|
/// @param _sourceName the name of the assembly file to be used for source locations
|
2016-08-16 14:27:20 +00:00
|
|
|
void appendInlineAssembly(
|
|
|
|
std::string const& _assembly,
|
|
|
|
std::vector<std::string> const& _localVariables = std::vector<std::string>(),
|
2018-01-17 11:05:43 +00:00
|
|
|
std::set<std::string> const& _externallyUsedFunctions = std::set<std::string>(),
|
2019-02-21 17:23:46 +00:00
|
|
|
bool _system = false,
|
2020-05-28 11:17:16 +00:00
|
|
|
OptimiserSettings const& _optimiserSettings = OptimiserSettings::none(),
|
|
|
|
std::string _sourceName = "--CODEGEN--"
|
2016-08-16 14:27:20 +00:00
|
|
|
);
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
/// If m_revertStrings is debug, @returns inline assembly code that
|
2021-03-15 15:45:00 +00:00
|
|
|
/// stores @param _message at the free memory pointer and reverts.
|
2020-01-22 14:48:56 +00:00
|
|
|
/// Otherwise returns "revert(0, 0)".
|
|
|
|
std::string revertReasonIfDebug(std::string const& _message = "");
|
|
|
|
|
2020-02-06 15:32:33 +00:00
|
|
|
void optimizeYul(yul::Object& _object, yul::EVMDialect const& _dialect, OptimiserSettings const& _optimiserSetting, std::set<yul::YulString> const& _externalIdentifiers = {});
|
|
|
|
|
2016-11-24 09:32:52 +00:00
|
|
|
/// Appends arbitrary data to the end of the bytecode.
|
2021-06-08 14:35:37 +00:00
|
|
|
void appendToAuxiliaryData(bytes const& _data) { m_asm->appendToAuxiliaryData(_data); }
|
2016-11-14 10:46:43 +00:00
|
|
|
|
2017-08-29 12:26:23 +00:00
|
|
|
/// Run optimisation step.
|
2017-07-17 11:12:00 +00:00
|
|
|
void optimise(OptimiserSettings const& _settings) { m_asm->optimise(translateOptimiserSettings(_settings)); }
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
/// @returns the runtime context if in creation mode and runtime context is set, nullptr otherwise.
|
2019-01-16 10:44:11 +00:00
|
|
|
CompilerContext* runtimeContext() const { return m_runtimeContext; }
|
2016-11-10 17:16:21 +00:00
|
|
|
/// @returns the identifier of the runtime subroutine.
|
|
|
|
size_t runtimeSub() const { return m_runtimeSub; }
|
|
|
|
|
2017-08-29 12:26:23 +00:00
|
|
|
/// @returns a const reference to the underlying assembly.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::Assembly const& assembly() const { return *m_asm; }
|
2019-01-16 10:44:11 +00:00
|
|
|
/// @returns a shared pointer to the assembly.
|
|
|
|
/// Should be avoided except when adding sub-assemblies.
|
2019-12-11 16:31:36 +00:00
|
|
|
std::shared_ptr<evmasm::Assembly> assemblyPtr() const { return m_asm; }
|
2016-05-08 14:24:47 +00:00
|
|
|
|
2015-02-23 15:31:36 +00:00
|
|
|
/**
|
|
|
|
* Helper class to pop the visited nodes stack when a scope closes
|
|
|
|
*/
|
|
|
|
class LocationSetter: public ScopeGuard
|
|
|
|
{
|
|
|
|
public:
|
2015-03-07 22:08:39 +00:00
|
|
|
LocationSetter(CompilerContext& _compilerContext, ASTNode const& _node):
|
2015-03-09 12:28:25 +00:00
|
|
|
ScopeGuard([&]{ _compilerContext.popVisitedNodes(); }) { _compilerContext.pushVisitedNodes(&_node); }
|
2015-02-23 15:31:36 +00:00
|
|
|
};
|
2015-02-27 16:41:22 +00:00
|
|
|
|
2019-10-08 09:00:40 +00:00
|
|
|
void setModifierDepth(size_t _modifierDepth) { m_asm->m_currentModifierDepth = _modifierDepth; }
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
RevertStrings revertStrings() const { return m_revertStrings; }
|
|
|
|
|
2015-02-23 15:31:36 +00:00
|
|
|
private:
|
2015-03-07 22:08:39 +00:00
|
|
|
/// Updates source location set in the assembly.
|
|
|
|
void updateSourceLocation();
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::Assembly::OptimiserSettings translateOptimiserSettings(OptimiserSettings const& _settings);
|
2017-07-17 11:12:00 +00:00
|
|
|
|
2016-05-02 23:13:41 +00:00
|
|
|
/**
|
|
|
|
* Helper class that manages function labels and ensures that referenced functions are
|
|
|
|
* compiled in a specific order.
|
|
|
|
*/
|
|
|
|
struct FunctionCompilationQueue
|
|
|
|
{
|
|
|
|
/// @returns the entry label of the given function and creates it if it does not exist yet.
|
|
|
|
/// @param _context compiler context used to create a new tag if needed
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem entryLabel(Declaration const& _declaration, CompilerContext& _context);
|
2016-05-02 23:13:41 +00:00
|
|
|
/// @returns the entry label of the given function. Might return an AssemblyItem of type
|
|
|
|
/// UndefinedItem if it does not exist yet.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem entryLabelIfExists(Declaration const& _declaration) const;
|
2016-05-02 23:13:41 +00:00
|
|
|
|
|
|
|
/// @returns the next function in the queue of functions that are still to be compiled
|
|
|
|
/// (i.e. that were referenced during compilation but where we did not yet generate code for).
|
|
|
|
/// Returns nullptr if the queue is empty. Does not remove the function from the queue,
|
|
|
|
/// that will only be done by startFunction below.
|
|
|
|
Declaration const* nextFunctionToCompile() const;
|
|
|
|
/// Informs the queue that we are about to compile the given function, i.e. removes
|
|
|
|
/// the function from the queue of functions to compile.
|
2019-02-13 15:22:42 +00:00
|
|
|
void startFunction(Declaration const& _function);
|
2016-05-02 23:13:41 +00:00
|
|
|
|
|
|
|
/// Labels pointing to the entry points of functions.
|
2019-12-11 16:31:36 +00:00
|
|
|
std::map<Declaration const*, evmasm::AssemblyItem> m_entryLabels;
|
2016-05-02 23:13:41 +00:00
|
|
|
/// Set of functions for which we did not yet generate code.
|
|
|
|
std::set<Declaration const*> m_alreadyCompiledFunctions;
|
|
|
|
/// Queue of functions that still need to be compiled (important to be a queue to maintain
|
|
|
|
/// determinism even in the presence of a non-deterministic allocator).
|
|
|
|
/// Mutable because we will throw out some functions earlier than needed.
|
|
|
|
mutable std::queue<Declaration const*> m_functionsToCompile;
|
|
|
|
} m_functionCompilationQueue;
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyPointer m_asm;
|
2018-02-21 22:56:42 +00:00
|
|
|
/// Version of the EVM to compile against.
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion m_evmVersion;
|
2020-01-22 14:48:56 +00:00
|
|
|
RevertStrings const m_revertStrings;
|
2020-10-22 17:19:14 +00:00
|
|
|
bool m_useABICoderV2 = false;
|
2014-12-12 15:49:26 +00:00
|
|
|
/// Other already compiled contracts to be used in contract creation calls.
|
2019-01-10 15:44:31 +00:00
|
|
|
std::map<ContractDefinition const*, std::shared_ptr<Compiler const>> m_otherCompilers;
|
2014-11-07 01:06:37 +00:00
|
|
|
/// Storage offsets of state variables
|
2015-03-13 18:48:24 +00:00
|
|
|
std::map<Declaration const*, std::pair<u256, unsigned>> m_stateVariables;
|
2020-03-10 13:30:04 +00:00
|
|
|
/// Memory offsets reserved for the values of immutable variables during contract creation.
|
|
|
|
std::map<VariableDeclaration const*, size_t> m_immutableVariables;
|
|
|
|
/// Total amount of reserved memory. Reserved memory is used to store immutable variables during contract creation.
|
|
|
|
/// This has to be finalized before initialiseFreeMemoryPointer() is called. That function
|
|
|
|
/// will reset the optional to verify that.
|
|
|
|
std::optional<size_t> m_reservedMemory = {0};
|
2015-01-22 16:40:22 +00:00
|
|
|
/// Offsets of local variables on the stack (relative to stack base).
|
2017-07-27 09:52:42 +00:00
|
|
|
/// This needs to be a stack because if a modifier contains a local variable and this
|
|
|
|
/// modifier is applied twice, the position of the variable needs to be restored
|
|
|
|
/// after the nested modifier is left.
|
|
|
|
std::map<Declaration const*, std::vector<unsigned>> m_localVariables;
|
2020-03-24 17:25:59 +00:00
|
|
|
/// The contract currently being compiled. Virtual function lookup starts from this contarct.
|
|
|
|
ContractDefinition const* m_mostDerivedContract = nullptr;
|
2020-07-22 08:28:04 +00:00
|
|
|
/// Whether to use checked arithmetic.
|
|
|
|
Arithmetic m_arithmetic = Arithmetic::Checked;
|
2015-02-23 15:31:36 +00:00
|
|
|
/// Stack of current visited AST nodes, used for location attachment
|
|
|
|
std::stack<ASTNode const*> m_visitedNodes;
|
2016-11-08 14:25:32 +00:00
|
|
|
/// The runtime context if in Creation mode, this is used for generating tags that would be stored into the storage and then used at runtime.
|
|
|
|
CompilerContext *m_runtimeContext;
|
2016-11-10 17:16:21 +00:00
|
|
|
/// The index of the runtime subroutine.
|
2020-06-02 13:42:46 +00:00
|
|
|
size_t m_runtimeSub = std::numeric_limits<size_t>::max();
|
2017-01-17 09:47:57 +00:00
|
|
|
/// An index of low-level function labels by name.
|
2019-12-11 16:31:36 +00:00
|
|
|
std::map<std::string, evmasm::AssemblyItem> m_lowLevelFunctions;
|
2020-03-02 16:23:58 +00:00
|
|
|
/// Collector for yul functions.
|
2020-03-02 17:08:19 +00:00
|
|
|
MultiUseYulFunctionCollector m_yulFunctionCollector;
|
2020-03-02 16:20:49 +00:00
|
|
|
/// Set of externally used yul functions.
|
2020-03-02 16:23:58 +00:00
|
|
|
std::set<std::string> m_externallyUsedYulFunctions;
|
2020-05-28 11:17:16 +00:00
|
|
|
/// Generated Yul code used as utility. Source references from the bytecode can point here.
|
|
|
|
/// Produced from @a m_yulFunctionCollector.
|
|
|
|
std::string m_generatedYulUtilityCode;
|
2017-08-25 16:58:12 +00:00
|
|
|
/// Container for ABI functions to be generated.
|
|
|
|
ABIFunctions m_abiFunctions;
|
2020-03-02 15:32:30 +00:00
|
|
|
/// Container for Yul Util functions to be generated.
|
|
|
|
YulUtilFunctions m_yulUtilFunctions;
|
2017-01-19 16:21:55 +00:00
|
|
|
/// The queue of low-level functions to generate.
|
|
|
|
std::queue<std::tuple<std::string, unsigned, unsigned, std::function<void(CompilerContext&)>>> m_lowLevelFunctionGenerationQueue;
|
2020-05-28 11:17:16 +00:00
|
|
|
/// Flag to check that appendYulUtilityFunctions() was called exactly once
|
|
|
|
bool m_appendYulUtilityFunctionsRan = false;
|
2014-10-30 00:20:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|