2016-05-19 17:32:14 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-05-19 17:32:14 +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,
|
2016-05-19 17:32:14 +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/>.
|
2016-05-19 17:32:14 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2016-05-19 17:32:14 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Solidity compiler.
|
|
|
|
*/
|
|
|
|
|
2020-02-04 11:01:13 +00:00
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
2019-12-03 00:02:33 +00:00
|
|
|
#include <libsolidity/ast/ASTUtils.h>
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2016-05-19 17:32:14 +00:00
|
|
|
#include <libsolidity/codegen/ContractCompiler.h>
|
|
|
|
#include <libsolidity/codegen/ExpressionCompiler.h>
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2020-02-04 11:01:13 +00:00
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2020-02-06 15:32:33 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-02-13 10:35:49 +00:00
|
|
|
#include <libyul/backends/evm/AsmCodeGen.h>
|
2020-02-04 11:01:13 +00:00
|
|
|
#include <libyul/backends/evm/EVMMetrics.h>
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
|
|
|
#include <libyul/optimiser/Suite.h>
|
|
|
|
#include <libyul/Object.h>
|
|
|
|
#include <libyul/optimiser/ASTCopier.h>
|
|
|
|
#include <libyul/YulString.h>
|
2019-02-13 10:35:49 +00:00
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
|
|
|
#include <libevmasm/Assembly.h>
|
|
|
|
#include <libevmasm/GasMeter.h>
|
2019-02-13 10:35:49 +00:00
|
|
|
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Whiskers.h>
|
2020-10-12 14:01:45 +00:00
|
|
|
#include <libsolutil/FunctionSelector.h>
|
2022-05-04 21:55:40 +00:00
|
|
|
#include <libsolutil/StackTooDeepString.h>
|
2019-09-05 18:02:58 +00:00
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/reverse.hpp>
|
2020-02-04 11:01:13 +00:00
|
|
|
|
2020-02-06 15:32:33 +00:00
|
|
|
#include <algorithm>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
2020-02-04 11:01:13 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
|
|
|
|
using solidity::util::FixedHash;
|
|
|
|
using solidity::util::h256;
|
|
|
|
using solidity::util::errinfo_comment;
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2017-08-29 00:01:47 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
/**
|
|
|
|
* Simple helper class to ensure that the stack height is the same at certain places in the code.
|
|
|
|
*/
|
|
|
|
class StackHeightChecker
|
|
|
|
{
|
|
|
|
public:
|
2017-08-21 14:42:17 +00:00
|
|
|
explicit StackHeightChecker(CompilerContext const& _context):
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context(_context), stackHeight(m_context.stackHeight()) {}
|
2019-06-03 10:01:11 +00:00
|
|
|
void check()
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
m_context.stackHeight() == stackHeight,
|
2023-08-14 19:37:31 +00:00
|
|
|
std::string("I sense a disturbance in the stack: ") + std::to_string(m_context.stackHeight()) + " vs " + std::to_string(stackHeight)
|
2019-06-03 10:01:11 +00:00
|
|
|
);
|
|
|
|
}
|
2016-05-19 17:32:14 +00:00
|
|
|
private:
|
|
|
|
CompilerContext const& m_context;
|
|
|
|
unsigned stackHeight;
|
|
|
|
};
|
|
|
|
|
2017-08-29 00:01:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
void ContractCompiler::compileContract(
|
|
|
|
ContractDefinition const& _contract,
|
2023-08-14 19:37:31 +00:00
|
|
|
std::map<ContractDefinition const*, std::shared_ptr<Compiler const>> const& _otherCompilers
|
2016-05-19 17:32:14 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _contract);
|
2017-11-14 11:58:04 +00:00
|
|
|
|
|
|
|
if (_contract.isLibrary())
|
|
|
|
// Check whether this is a call (true) or a delegatecall (false).
|
|
|
|
// This has to be the first code in the contract.
|
|
|
|
appendDelegatecallCheck();
|
|
|
|
|
2019-01-10 15:44:31 +00:00
|
|
|
initializeContext(_contract, _otherCompilers);
|
2018-06-19 17:30:38 +00:00
|
|
|
// This generates the dispatch function for externally visible functions
|
|
|
|
// and adds the function to the compilation queue. Additionally internal functions,
|
|
|
|
// which are referenced directly or indirectly will be added.
|
2016-05-19 17:32:14 +00:00
|
|
|
appendFunctionSelector(_contract);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ContractCompiler::compileConstructor(
|
|
|
|
ContractDefinition const& _contract,
|
2023-08-14 19:37:31 +00:00
|
|
|
std::map<ContractDefinition const*, std::shared_ptr<Compiler const>> const& _otherCompilers
|
2016-05-19 17:32:14 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _contract);
|
2017-11-14 11:58:04 +00:00
|
|
|
if (_contract.isLibrary())
|
|
|
|
return deployLibrary(_contract);
|
|
|
|
else
|
|
|
|
{
|
2019-01-10 15:44:31 +00:00
|
|
|
initializeContext(_contract, _otherCompilers);
|
2017-11-14 11:58:04 +00:00
|
|
|
return packIntoContractCreator(_contract);
|
|
|
|
}
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContractCompiler::initializeContext(
|
|
|
|
ContractDefinition const& _contract,
|
2023-08-14 19:37:31 +00:00
|
|
|
std::map<ContractDefinition const*, std::shared_ptr<Compiler const>> const& _otherCompilers
|
2016-05-19 17:32:14 +00:00
|
|
|
)
|
|
|
|
{
|
2020-10-22 17:19:14 +00:00
|
|
|
m_context.setUseABICoderV2(*_contract.sourceUnit().annotation().useABICoderV2);
|
2019-01-10 15:44:31 +00:00
|
|
|
m_context.setOtherCompilers(_otherCompilers);
|
2020-03-24 17:25:59 +00:00
|
|
|
m_context.setMostDerivedContract(_contract);
|
2020-03-10 13:30:04 +00:00
|
|
|
if (m_runtimeCompiler)
|
|
|
|
registerImmutableVariables(_contract);
|
2016-05-19 17:32:14 +00:00
|
|
|
CompilerUtils(m_context).initialiseFreeMemoryPointer();
|
|
|
|
registerStateVariables(_contract);
|
|
|
|
m_context.resetVisitedNodes(&_contract);
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:23:31 +00:00
|
|
|
void ContractCompiler::appendCallValueCheck()
|
|
|
|
{
|
|
|
|
// Throw if function is not payable but call contained ether.
|
|
|
|
m_context << Instruction::CALLVALUE;
|
2020-01-22 14:48:56 +00:00
|
|
|
m_context.appendConditionalRevert(false, "Ether sent to non-payable function");
|
2016-11-17 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
void ContractCompiler::appendInitAndConstructorCode(ContractDefinition const& _contract)
|
|
|
|
{
|
2017-11-14 11:58:04 +00:00
|
|
|
solAssert(!_contract.isLibrary(), "Tried to initialize library.");
|
2017-08-25 16:58:12 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _contract);
|
2018-04-05 14:25:20 +00:00
|
|
|
|
|
|
|
m_baseArguments = &_contract.annotation().baseConstructorArguments;
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
// Initialization of state variables in base-to-derived order.
|
2021-03-31 18:03:04 +00:00
|
|
|
for (ContractDefinition const* contract: _contract.annotation().linearizedBaseContracts | ranges::views::reverse)
|
2016-05-19 17:32:14 +00:00
|
|
|
initializeStateVariables(*contract);
|
|
|
|
|
|
|
|
if (FunctionDefinition const* constructor = _contract.constructor())
|
|
|
|
appendConstructor(*constructor);
|
2016-11-15 10:24:53 +00:00
|
|
|
else
|
2020-05-06 15:21:44 +00:00
|
|
|
{
|
|
|
|
// Implicit constructors are always non-payable.
|
2016-11-17 17:23:31 +00:00
|
|
|
appendCallValueCheck();
|
2020-05-06 15:21:44 +00:00
|
|
|
if (auto c = _contract.nextConstructor(m_context.mostDerivedContract()))
|
|
|
|
appendBaseConstructor(*c);
|
|
|
|
}
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
size_t ContractCompiler::packIntoContractCreator(ContractDefinition const& _contract)
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
2016-11-10 17:16:21 +00:00
|
|
|
solAssert(!!m_runtimeCompiler, "");
|
2017-11-14 11:58:04 +00:00
|
|
|
solAssert(!_contract.isLibrary(), "Tried to use contract creator or library.");
|
2016-11-10 17:16:21 +00:00
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
appendInitAndConstructorCode(_contract);
|
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
// We jump to the deploy routine because we first have to append all missing functions,
|
|
|
|
// which can cause further functions to be added to the runtime context.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem deployRoutine = m_context.appendJumpToNew();
|
2016-11-10 17:16:21 +00:00
|
|
|
|
|
|
|
// We have to include copies of functions in the construction time and runtime context
|
|
|
|
// because of absolute jumps.
|
|
|
|
appendMissingFunctions();
|
|
|
|
m_runtimeCompiler->appendMissingFunctions();
|
|
|
|
|
2017-08-25 16:58:12 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _contract);
|
2016-11-10 17:16:21 +00:00
|
|
|
m_context << deployRoutine;
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
solAssert(m_context.runtimeSub() != std::numeric_limits<size_t>::max(), "Runtime sub not registered");
|
2020-03-10 13:30:04 +00:00
|
|
|
|
|
|
|
ContractType contractType(_contract);
|
|
|
|
auto const& immutables = contractType.immutableVariables();
|
|
|
|
// Push all immutable values on the stack.
|
|
|
|
for (auto const& immutable: immutables)
|
2021-09-28 14:41:32 +00:00
|
|
|
CompilerUtils(m_context).loadFromMemory(
|
|
|
|
static_cast<unsigned>(m_context.immutableMemoryOffset(*immutable)),
|
|
|
|
*immutable->annotation().type,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
2016-11-11 10:41:50 +00:00
|
|
|
m_context.pushSubroutineSize(m_context.runtimeSub());
|
2020-03-10 13:30:04 +00:00
|
|
|
if (immutables.empty())
|
|
|
|
m_context << Instruction::DUP1;
|
2016-11-11 10:41:50 +00:00
|
|
|
m_context.pushSubroutineOffset(m_context.runtimeSub());
|
|
|
|
m_context << u256(0) << Instruction::CODECOPY;
|
2020-03-10 13:30:04 +00:00
|
|
|
// Assign immutable values from stack in reversed order.
|
2021-03-31 18:03:04 +00:00
|
|
|
for (auto const& immutable: immutables | ranges::views::reverse)
|
2020-03-10 13:30:04 +00:00
|
|
|
{
|
|
|
|
auto slotNames = m_context.immutableVariableSlotNames(*immutable);
|
2021-03-31 18:03:04 +00:00
|
|
|
for (auto&& slotName: slotNames | ranges::views::reverse)
|
2020-10-14 11:20:20 +00:00
|
|
|
{
|
|
|
|
m_context << u256(0);
|
2020-03-10 13:30:04 +00:00
|
|
|
m_context.appendImmutableAssignment(slotName);
|
2020-10-14 11:20:20 +00:00
|
|
|
}
|
2020-03-10 13:30:04 +00:00
|
|
|
}
|
|
|
|
if (!immutables.empty())
|
|
|
|
m_context.pushSubroutineSize(m_context.runtimeSub());
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context << u256(0) << Instruction::RETURN;
|
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
return m_context.runtimeSub();
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:58:04 +00:00
|
|
|
size_t ContractCompiler::deployLibrary(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
solAssert(!!m_runtimeCompiler, "");
|
|
|
|
solAssert(_contract.isLibrary(), "Tried to deploy contract as library.");
|
|
|
|
|
2020-05-05 14:53:30 +00:00
|
|
|
appendMissingFunctions();
|
|
|
|
m_runtimeCompiler->appendMissingFunctions();
|
|
|
|
|
2017-11-14 11:58:04 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _contract);
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
solAssert(m_context.runtimeSub() != std::numeric_limits<size_t>::max(), "Runtime sub not registered");
|
2017-11-14 11:58:04 +00:00
|
|
|
m_context.pushSubroutineSize(m_context.runtimeSub());
|
|
|
|
m_context.pushSubroutineOffset(m_context.runtimeSub());
|
2019-02-25 23:59:13 +00:00
|
|
|
// This code replaces the address added by appendDeployTimeAddress().
|
2020-10-12 14:01:45 +00:00
|
|
|
m_context.appendInlineAssembly(
|
2022-03-07 04:25:35 +00:00
|
|
|
util::Whiskers(R"(
|
2020-10-12 14:01:45 +00:00
|
|
|
{
|
|
|
|
// If code starts at 11, an mstore(0) writes to the full PUSH20 plus data
|
|
|
|
// without the need for a shift.
|
|
|
|
let codepos := 11
|
|
|
|
codecopy(codepos, subOffset, subSize)
|
|
|
|
// Check that the first opcode is a PUSH20
|
|
|
|
if iszero(eq(0x73, byte(0, mload(codepos)))) {
|
2021-05-23 22:40:50 +00:00
|
|
|
mstore(0, <panicSelector>)
|
|
|
|
mstore(4, <panicCode>)
|
2020-10-12 14:01:45 +00:00
|
|
|
revert(0, 0x24)
|
|
|
|
}
|
|
|
|
mstore(0, address())
|
|
|
|
mstore8(codepos, 0x73)
|
|
|
|
return(codepos, subSize)
|
|
|
|
}
|
2021-05-23 22:40:50 +00:00
|
|
|
)")
|
2022-09-27 12:13:44 +00:00
|
|
|
("panicSelector", util::selectorFromSignatureU256("Panic(uint256)").str())
|
2021-05-23 22:40:50 +00:00
|
|
|
("panicCode", "0")
|
|
|
|
.render(),
|
2020-10-12 14:01:45 +00:00
|
|
|
{"subSize", "subOffset"}
|
|
|
|
);
|
2017-11-14 11:58:04 +00:00
|
|
|
|
|
|
|
return m_context.runtimeSub();
|
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
void ContractCompiler::appendBaseConstructor(FunctionDefinition const& _constructor)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _constructor);
|
|
|
|
FunctionType constructorType(_constructor);
|
|
|
|
if (!constructorType.parameterTypes().empty())
|
|
|
|
{
|
2018-04-05 14:25:20 +00:00
|
|
|
solAssert(m_baseArguments, "");
|
|
|
|
solAssert(m_baseArguments->count(&_constructor), "");
|
|
|
|
std::vector<ASTPointer<Expression>> const* arguments = nullptr;
|
|
|
|
ASTNode const* baseArgumentNode = m_baseArguments->at(&_constructor);
|
|
|
|
if (auto inheritanceSpecifier = dynamic_cast<InheritanceSpecifier const*>(baseArgumentNode))
|
|
|
|
arguments = inheritanceSpecifier->arguments();
|
|
|
|
else if (auto modifierInvocation = dynamic_cast<ModifierInvocation const*>(baseArgumentNode))
|
2018-04-10 09:22:26 +00:00
|
|
|
arguments = modifierInvocation->arguments();
|
2016-05-19 17:32:14 +00:00
|
|
|
solAssert(arguments, "");
|
2018-03-16 18:02:15 +00:00
|
|
|
solAssert(arguments->size() == constructorType.parameterTypes().size(), "");
|
2016-05-19 17:32:14 +00:00
|
|
|
for (unsigned i = 0; i < arguments->size(); ++i)
|
|
|
|
compileExpression(*(arguments->at(i)), constructorType.parameterTypes()[i]);
|
|
|
|
}
|
|
|
|
_constructor.accept(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContractCompiler::appendConstructor(FunctionDefinition const& _constructor)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _constructor);
|
2016-11-15 10:24:53 +00:00
|
|
|
if (!_constructor.isPayable())
|
2016-11-17 17:23:31 +00:00
|
|
|
appendCallValueCheck();
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
// copy constructor arguments from code to memory and then to stack, they are supplied after the actual program
|
|
|
|
if (!_constructor.parameters().empty())
|
|
|
|
{
|
|
|
|
CompilerUtils(m_context).fetchFreeMemoryPointer();
|
2019-05-15 16:31:07 +00:00
|
|
|
// CODESIZE returns the actual size of the code,
|
|
|
|
// which is the size of the generated code (``programSize``)
|
|
|
|
// plus the constructor arguments added to the transaction payload.
|
|
|
|
m_context.appendProgramSize();
|
|
|
|
m_context << Instruction::CODESIZE << Instruction::SUB;
|
2016-05-19 17:32:14 +00:00
|
|
|
// stack: <memptr> <argument size>
|
|
|
|
m_context << Instruction::DUP1;
|
|
|
|
m_context.appendProgramSize();
|
|
|
|
m_context << Instruction::DUP4 << Instruction::CODECOPY;
|
2019-05-15 16:31:07 +00:00
|
|
|
// stack: <memptr> <argument size>
|
|
|
|
m_context << Instruction::DUP2 << Instruction::DUP2 << Instruction::ADD;
|
|
|
|
// stack: <memptr> <argument size> <mem end>
|
2016-05-19 17:32:14 +00:00
|
|
|
CompilerUtils(m_context).storeFreeMemoryPointer();
|
2019-05-15 16:31:07 +00:00
|
|
|
// stack: <memptr> <argument size>
|
2018-02-20 09:13:58 +00:00
|
|
|
CompilerUtils(m_context).abiDecode(FunctionType(_constructor).parameterTypes(), true);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
_constructor.accept(*this);
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:58:04 +00:00
|
|
|
void ContractCompiler::appendDelegatecallCheck()
|
|
|
|
{
|
|
|
|
// Special constant that will be replaced by the address at deploy time.
|
|
|
|
// At compilation time, this is just "PUSH20 00...000".
|
|
|
|
m_context.appendDeployTimeAddress();
|
|
|
|
m_context << Instruction::ADDRESS << Instruction::EQ;
|
|
|
|
// The result on the stack is
|
|
|
|
// "We have not been called via DELEGATECALL".
|
|
|
|
}
|
|
|
|
|
2018-09-10 10:09:42 +00:00
|
|
|
void ContractCompiler::appendInternalSelector(
|
2023-08-14 19:37:31 +00:00
|
|
|
std::map<FixedHash<4>, evmasm::AssemblyItem const> const& _entryPoints,
|
|
|
|
std::vector<FixedHash<4>> const& _ids,
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem const& _notFoundTag,
|
2018-09-10 10:09:42 +00:00
|
|
|
size_t _runs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Code for selecting from n functions without split:
|
|
|
|
// n times: dup1, push4 <id_i>, eq, push2/3 <tag_i>, jumpi
|
|
|
|
// push2/3 <notfound> jump
|
|
|
|
// (called SELECT[n])
|
|
|
|
// Code for selecting from n functions with split:
|
|
|
|
// dup1, push4 <pivot>, gt, push2/3<tag_less>, jumpi
|
|
|
|
// SELECT[n/2]
|
|
|
|
// tag_less:
|
|
|
|
// SELECT[n/2]
|
|
|
|
//
|
|
|
|
// This means each split adds 16-18 bytes of additional code (note the additional jump out!)
|
|
|
|
// The average execution cost if we do not split at all are:
|
|
|
|
// (3 + 3 + 3 + 3 + 10) * n/2 = 24 * n/2 = 12 * n
|
|
|
|
// If we split once:
|
|
|
|
// (3 + 3 + 3 + 3 + 10) + 24 * n/4 = 24 * (n/4 + 1) = 6 * n + 24;
|
|
|
|
//
|
|
|
|
// We should split if
|
|
|
|
// _runs * 12 * n > _runs * (6 * n + 24) + 17 * createDataGas
|
|
|
|
// <=> _runs * 6 * (n - 4) > 17 * createDataGas
|
|
|
|
//
|
|
|
|
// Which also means that the execution itself is not profitable
|
|
|
|
// unless we have at least 5 functions.
|
|
|
|
|
|
|
|
// Start with some comparisons to avoid overflow, then do the actual comparison.
|
|
|
|
bool split = false;
|
|
|
|
if (_ids.size() <= 4)
|
|
|
|
split = false;
|
2019-12-11 16:31:36 +00:00
|
|
|
else if (_runs > (17 * evmasm::GasCosts::createDataGas) / 6)
|
2018-09-10 10:09:42 +00:00
|
|
|
split = true;
|
|
|
|
else
|
2019-12-11 16:31:36 +00:00
|
|
|
split = (_runs * 6 * (_ids.size() - 4) > 17 * evmasm::GasCosts::createDataGas);
|
2018-09-10 10:09:42 +00:00
|
|
|
|
|
|
|
if (split)
|
|
|
|
{
|
|
|
|
size_t pivotIndex = _ids.size() / 2;
|
|
|
|
FixedHash<4> pivot{_ids.at(pivotIndex)};
|
|
|
|
m_context << dupInstruction(1) << u256(FixedHash<4>::Arith(pivot)) << Instruction::GT;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem lessTag{m_context.appendConditionalJump()};
|
2018-09-10 10:09:42 +00:00
|
|
|
// Here, we have funid >= pivot
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<FixedHash<4>> larger{_ids.begin() + static_cast<ptrdiff_t>(pivotIndex), _ids.end()};
|
2018-09-10 10:09:42 +00:00
|
|
|
appendInternalSelector(_entryPoints, larger, _notFoundTag, _runs);
|
|
|
|
m_context << lessTag;
|
|
|
|
// Here, we have funid < pivot
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<FixedHash<4>> smaller{_ids.begin(), _ids.begin() + static_cast<ptrdiff_t>(pivotIndex)};
|
2018-09-10 10:09:42 +00:00
|
|
|
appendInternalSelector(_entryPoints, smaller, _notFoundTag, _runs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto const& id: _ids)
|
|
|
|
{
|
|
|
|
m_context << dupInstruction(1) << u256(FixedHash<4>::Arith(id)) << Instruction::EQ;
|
|
|
|
m_context.appendConditionalJumpTo(_entryPoints.at(id));
|
|
|
|
}
|
|
|
|
m_context.appendJumpTo(_notFoundTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:42:09 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// Helper function to check if any function is payable
|
|
|
|
bool hasPayableFunctions(ContractDefinition const& _contract)
|
|
|
|
{
|
2019-09-09 16:22:02 +00:00
|
|
|
if (_contract.receiveFunction())
|
|
|
|
return true;
|
|
|
|
|
2018-12-11 15:42:09 +00:00
|
|
|
FunctionDefinition const* fallback = _contract.fallbackFunction();
|
|
|
|
if (fallback && fallback->isPayable())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto const& it: _contract.interfaceFunctions())
|
|
|
|
if (it.second->isPayable())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
void ContractCompiler::appendFunctionSelector(ContractDefinition const& _contract)
|
|
|
|
{
|
2023-08-14 19:37:31 +00:00
|
|
|
std::map<FixedHash<4>, FunctionTypePointer> interfaceFunctions = _contract.interfaceFunctions();
|
|
|
|
std::map<FixedHash<4>, evmasm::AssemblyItem const> callDataUnpackerEntryPoints;
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2017-11-14 11:58:04 +00:00
|
|
|
if (_contract.isLibrary())
|
|
|
|
{
|
|
|
|
solAssert(m_context.stackHeight() == 1, "CALL / DELEGATECALL flag expected.");
|
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
FunctionDefinition const* fallback = _contract.fallbackFunction();
|
2018-12-11 15:42:09 +00:00
|
|
|
solAssert(!_contract.isLibrary() || !fallback, "Libraries can't have fallback functions");
|
|
|
|
|
2019-09-09 16:22:02 +00:00
|
|
|
FunctionDefinition const* etherReceiver = _contract.receiveFunction();
|
2020-08-17 14:49:13 +00:00
|
|
|
solAssert(!_contract.isLibrary() || !etherReceiver, "Libraries can't have ether receiver functions");
|
2019-09-09 16:22:02 +00:00
|
|
|
|
2018-12-11 15:42:09 +00:00
|
|
|
bool needToAddCallvalueCheck = true;
|
|
|
|
if (!hasPayableFunctions(_contract) && !interfaceFunctions.empty() && !_contract.isLibrary())
|
|
|
|
{
|
|
|
|
appendCallValueCheck();
|
|
|
|
needToAddCallvalueCheck = false;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem notFoundOrReceiveEther = m_context.newTag();
|
2019-09-09 16:22:02 +00:00
|
|
|
// If there is neither a fallback nor a receive ether function, we only need one label to jump to, which
|
|
|
|
// always reverts.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem notFound = (!fallback && !etherReceiver) ? notFoundOrReceiveEther : m_context.newTag();
|
2019-09-09 16:22:02 +00:00
|
|
|
|
|
|
|
// directly jump to fallback or ether receiver if the data is too short to contain a function selector
|
2017-10-11 08:45:24 +00:00
|
|
|
// also guards against short data
|
|
|
|
m_context << u256(4) << Instruction::CALLDATASIZE << Instruction::LT;
|
2019-09-09 16:22:02 +00:00
|
|
|
m_context.appendConditionalJumpTo(notFoundOrReceiveEther);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
// retrieve the function signature hash from the calldata
|
|
|
|
if (!interfaceFunctions.empty())
|
2018-12-11 15:42:09 +00:00
|
|
|
{
|
2021-09-28 14:41:32 +00:00
|
|
|
CompilerUtils(m_context).loadFromMemory(0, IntegerType(CompilerUtils::dataStartOffset * 8), true, false);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2018-12-11 15:42:09 +00:00
|
|
|
// stack now is: <can-call-non-view-functions>? <funhash>
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<FixedHash<4>> sortedIDs;
|
2018-12-11 15:42:09 +00:00
|
|
|
for (auto const& it: interfaceFunctions)
|
|
|
|
{
|
|
|
|
callDataUnpackerEntryPoints.emplace(it.first, m_context.newTag());
|
|
|
|
sortedIDs.emplace_back(it.first);
|
|
|
|
}
|
|
|
|
std::sort(sortedIDs.begin(), sortedIDs.end());
|
2019-02-21 17:13:52 +00:00
|
|
|
appendInternalSelector(callDataUnpackerEntryPoints, sortedIDs, notFound, m_optimiserSettings.expectedExecutionsPerDeployment);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 16:22:02 +00:00
|
|
|
m_context << notFoundOrReceiveEther;
|
2018-12-11 15:42:09 +00:00
|
|
|
|
2019-09-09 16:22:02 +00:00
|
|
|
if (!fallback && !etherReceiver)
|
2020-01-22 14:48:56 +00:00
|
|
|
m_context.appendRevert("Contract does not have fallback nor receive functions");
|
2019-09-09 16:22:02 +00:00
|
|
|
else
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
2019-09-09 16:22:02 +00:00
|
|
|
if (etherReceiver)
|
|
|
|
{
|
|
|
|
// directly jump to fallback, if there is calldata
|
|
|
|
m_context << Instruction::CALLDATASIZE;
|
|
|
|
m_context.appendConditionalJumpTo(notFound);
|
|
|
|
|
|
|
|
solAssert(!_contract.isLibrary(), "");
|
|
|
|
solAssert(etherReceiver->isReceive(), "");
|
|
|
|
solAssert(FunctionType(*etherReceiver).parameterTypes().empty(), "");
|
|
|
|
solAssert(FunctionType(*etherReceiver).returnParameterTypes().empty(), "");
|
|
|
|
etherReceiver->accept(*this);
|
|
|
|
m_context << Instruction::STOP;
|
|
|
|
}
|
2016-11-17 17:23:31 +00:00
|
|
|
|
2019-09-09 16:22:02 +00:00
|
|
|
m_context << notFound;
|
|
|
|
if (fallback)
|
|
|
|
{
|
|
|
|
solAssert(!_contract.isLibrary(), "");
|
|
|
|
if (!fallback->isPayable() && needToAddCallvalueCheck)
|
|
|
|
appendCallValueCheck();
|
|
|
|
|
|
|
|
solAssert(fallback->isFallback(), "");
|
2020-11-05 17:03:32 +00:00
|
|
|
m_context.setStackOffset(0);
|
|
|
|
|
|
|
|
if (!FunctionType(*fallback).parameterTypes().empty())
|
|
|
|
m_context << u256(0) << Instruction::CALLDATASIZE;
|
|
|
|
|
2019-09-09 16:22:02 +00:00
|
|
|
fallback->accept(*this);
|
2020-11-05 17:03:32 +00:00
|
|
|
|
|
|
|
if (FunctionType(*fallback).returnParameterTypes().empty())
|
|
|
|
m_context << Instruction::STOP;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_context << Instruction::DUP1 << Instruction::MLOAD << Instruction::SWAP1;
|
|
|
|
m_context << u256(0x20) << Instruction::ADD;
|
|
|
|
m_context << Instruction::RETURN;
|
|
|
|
}
|
2019-09-09 16:22:02 +00:00
|
|
|
}
|
|
|
|
else
|
2020-01-22 14:48:56 +00:00
|
|
|
m_context.appendRevert("Unknown signature and no fallback defined");
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
2019-09-09 16:22:02 +00:00
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
for (auto const& it: interfaceFunctions)
|
|
|
|
{
|
2020-11-05 17:03:32 +00:00
|
|
|
m_context.setStackOffset(1);
|
2016-05-19 17:32:14 +00:00
|
|
|
FunctionTypePointer const& functionType = it.second;
|
|
|
|
solAssert(functionType->hasDeclaration(), "");
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, functionType->declaration());
|
2016-06-18 10:11:55 +00:00
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context << callDataUnpackerEntryPoints.at(it.first);
|
2017-11-14 11:58:04 +00:00
|
|
|
if (_contract.isLibrary() && functionType->stateMutability() > StateMutability::View)
|
|
|
|
{
|
|
|
|
// If the function is not a view function and is called without DELEGATECALL,
|
|
|
|
// we revert.
|
|
|
|
m_context << dupInstruction(2);
|
2020-01-22 14:48:56 +00:00
|
|
|
m_context.appendConditionalRevert(false, "Non-view function of library called without DELEGATECALL");
|
2017-11-14 11:58:04 +00:00
|
|
|
}
|
2017-06-23 15:20:07 +00:00
|
|
|
m_context.setStackOffset(0);
|
2016-09-15 15:44:32 +00:00
|
|
|
// We have to allow this for libraries, because value of the previous
|
|
|
|
// call is still visible in the delegatecall.
|
2018-12-11 15:42:09 +00:00
|
|
|
if (!functionType->isPayable() && !_contract.isLibrary() && needToAddCallvalueCheck)
|
2016-11-17 17:23:31 +00:00
|
|
|
appendCallValueCheck();
|
2016-06-18 10:11:55 +00:00
|
|
|
|
2017-06-28 15:55:20 +00:00
|
|
|
// Return tag is used to jump out of the function.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem returnTag = m_context.pushNewTag();
|
2017-06-30 12:09:35 +00:00
|
|
|
if (!functionType->parameterTypes().empty())
|
|
|
|
{
|
|
|
|
// Parameter for calldataUnpacker
|
|
|
|
m_context << CompilerUtils::dataStartOffset;
|
2017-12-13 16:23:37 +00:00
|
|
|
m_context << Instruction::DUP1 << Instruction::CALLDATASIZE << Instruction::SUB;
|
2018-02-20 09:13:58 +00:00
|
|
|
CompilerUtils(m_context).abiDecode(functionType->parameterTypes());
|
2017-06-30 12:09:35 +00:00
|
|
|
}
|
2018-11-13 09:53:40 +00:00
|
|
|
m_context.appendJumpTo(
|
|
|
|
m_context.functionEntryLabel(functionType->declaration()),
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem::JumpType::IntoFunction
|
2018-11-13 09:53:40 +00:00
|
|
|
);
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context << returnTag;
|
2017-06-28 15:55:20 +00:00
|
|
|
// Return tag and input parameters get consumed.
|
2017-06-23 15:20:07 +00:00
|
|
|
m_context.adjustStackOffset(
|
2020-06-02 13:42:46 +00:00
|
|
|
static_cast<int>(CompilerUtils::sizeOnStack(functionType->returnParameterTypes())) -
|
|
|
|
static_cast<int>(CompilerUtils::sizeOnStack(functionType->parameterTypes())) -
|
2017-06-23 15:20:07 +00:00
|
|
|
1
|
|
|
|
);
|
2017-06-28 15:55:20 +00:00
|
|
|
// Consumes the return parameters.
|
2016-05-19 17:32:14 +00:00
|
|
|
appendReturnValuePacker(functionType->returnParameterTypes(), _contract.isLibrary());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContractCompiler::appendReturnValuePacker(TypePointers const& _typeParameters, bool _isLibrary)
|
|
|
|
{
|
|
|
|
CompilerUtils utils(m_context);
|
|
|
|
if (_typeParameters.empty())
|
|
|
|
m_context << Instruction::STOP;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
utils.fetchFreeMemoryPointer();
|
|
|
|
//@todo optimization: if we return a single memory array, there should be enough space before
|
|
|
|
// its data to add the needed parts and we avoid a memory copy.
|
2017-09-26 21:10:30 +00:00
|
|
|
utils.abiEncode(_typeParameters, _typeParameters, _isLibrary);
|
2016-05-19 17:32:14 +00:00
|
|
|
utils.toSizeAfterFreeMemoryPointer();
|
|
|
|
m_context << Instruction::RETURN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContractCompiler::registerStateVariables(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
for (auto const& var: ContractType(_contract).stateVariables())
|
2023-08-14 19:37:31 +00:00
|
|
|
m_context.addStateVariable(*std::get<0>(var), std::get<1>(var), std::get<2>(var));
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:30:04 +00:00
|
|
|
void ContractCompiler::registerImmutableVariables(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
solAssert(m_runtimeCompiler, "Attempted to register immutables for runtime code generation.");
|
|
|
|
for (auto const& var: ContractType(_contract).immutableVariables())
|
|
|
|
m_context.addImmutable(*var);
|
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
void ContractCompiler::initializeStateVariables(ContractDefinition const& _contract)
|
|
|
|
{
|
2017-11-14 11:58:04 +00:00
|
|
|
solAssert(!_contract.isLibrary(), "Tried to initialize state variables of library.");
|
2016-05-19 17:32:14 +00:00
|
|
|
for (VariableDeclaration const* variable: _contract.stateVariables())
|
2020-04-02 06:34:20 +00:00
|
|
|
if (variable->value() && !variable->isConstant())
|
2020-01-22 14:48:56 +00:00
|
|
|
ExpressionCompiler(m_context, m_optimiserSettings.runOrderLiterals).appendStateVariableInitialization(*variable);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(VariableDeclaration const& _variableDeclaration)
|
|
|
|
{
|
|
|
|
solAssert(_variableDeclaration.isStateVariable(), "Compiler visit to non-state variable declaration.");
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclaration);
|
|
|
|
|
|
|
|
m_context.startFunction(_variableDeclaration);
|
|
|
|
m_breakTags.clear();
|
|
|
|
m_continueTags.clear();
|
|
|
|
|
|
|
|
if (_variableDeclaration.isConstant())
|
2020-01-22 14:48:56 +00:00
|
|
|
ExpressionCompiler(m_context, m_optimiserSettings.runOrderLiterals)
|
2019-09-18 14:44:36 +00:00
|
|
|
.appendConstStateVariableAccessor(_variableDeclaration);
|
2016-05-19 17:32:14 +00:00
|
|
|
else
|
2020-01-22 14:48:56 +00:00
|
|
|
ExpressionCompiler(m_context, m_optimiserSettings.runOrderLiterals)
|
2019-09-18 14:44:36 +00:00
|
|
|
.appendStateVariableAccessor(_variableDeclaration);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(FunctionDefinition const& _function)
|
|
|
|
{
|
2021-06-01 15:13:50 +00:00
|
|
|
solAssert(_function.isImplemented(), "");
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _function);
|
|
|
|
|
|
|
|
m_context.startFunction(_function);
|
|
|
|
|
|
|
|
// stack upon entry: [return address] [arg0] [arg1] ... [argn]
|
2018-05-03 14:40:33 +00:00
|
|
|
// reserve additional slots: [retarg0] ... [retargm]
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
unsigned parametersSize = CompilerUtils::sizeOnStack(_function.parameters());
|
2020-11-05 17:03:32 +00:00
|
|
|
if (_function.isFallback())
|
|
|
|
m_context.adjustStackOffset(static_cast<int>(parametersSize));
|
|
|
|
else if (!_function.isConstructor())
|
2016-05-19 17:32:14 +00:00
|
|
|
// adding 1 for return address.
|
2020-06-02 13:42:46 +00:00
|
|
|
m_context.adjustStackOffset(static_cast<int>(parametersSize) + 1);
|
2020-05-11 12:59:06 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: _function.parameters())
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
|
|
|
m_context.addVariable(*variable, parametersSize);
|
|
|
|
parametersSize -= variable->annotation().type->sizeOnStack();
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:59:06 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: _function.returnParameters())
|
2020-11-19 10:00:18 +00:00
|
|
|
appendStackVariableInitialisation(*variable, /* _provideDefaultValue = */ true);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
if (_function.isConstructor())
|
2020-04-06 15:30:01 +00:00
|
|
|
if (auto c = dynamic_cast<ContractDefinition const&>(*_function.scope()).nextConstructor(
|
|
|
|
m_context.mostDerivedContract()
|
|
|
|
))
|
2016-05-19 17:32:14 +00:00
|
|
|
appendBaseConstructor(*c);
|
|
|
|
|
2018-07-04 12:14:41 +00:00
|
|
|
solAssert(m_returnTags.empty(), "");
|
2016-05-19 17:32:14 +00:00
|
|
|
m_breakTags.clear();
|
|
|
|
m_continueTags.clear();
|
|
|
|
m_currentFunction = &_function;
|
2023-08-14 19:37:31 +00:00
|
|
|
m_modifierDepth = std::numeric_limits<unsigned>::max();
|
2018-05-03 14:40:33 +00:00
|
|
|
m_scopeStackHeight.clear();
|
2019-10-08 09:00:40 +00:00
|
|
|
m_context.setModifierDepth(0);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
appendModifierOrFunctionCode();
|
2019-10-08 09:00:40 +00:00
|
|
|
m_context.setModifierDepth(0);
|
2016-08-06 12:48:59 +00:00
|
|
|
solAssert(m_returnTags.empty(), "");
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
// Now we need to re-shuffle the stack. For this we keep a record of the stack layout
|
|
|
|
// that shows the target positions of the elements, where "-1" denotes that this element needs
|
|
|
|
// to be removed from the stack.
|
|
|
|
// Note that the fact that the return arguments are of increasing index is vital for this
|
|
|
|
// algorithm to work.
|
|
|
|
|
|
|
|
unsigned const c_argumentsSize = CompilerUtils::sizeOnStack(_function.parameters());
|
|
|
|
unsigned const c_returnValuesSize = CompilerUtils::sizeOnStack(_function.returnParameters());
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<int> stackLayout;
|
2020-11-05 17:03:32 +00:00
|
|
|
if (!_function.isConstructor() && !_function.isFallback())
|
|
|
|
stackLayout.push_back(static_cast<int>(c_returnValuesSize)); // target of return address
|
2023-08-14 19:37:31 +00:00
|
|
|
stackLayout += std::vector<int>(c_argumentsSize, -1); // discard all arguments
|
2020-06-02 13:42:46 +00:00
|
|
|
for (size_t i = 0; i < c_returnValuesSize; ++i)
|
|
|
|
stackLayout.push_back(static_cast<int>(i));
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2017-01-20 18:01:19 +00:00
|
|
|
if (stackLayout.size() > 17)
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2020-07-01 07:46:06 +00:00
|
|
|
StackTooDeepError() <<
|
2017-01-20 18:01:19 +00:00
|
|
|
errinfo_sourceLocation(_function.location()) <<
|
2022-05-04 21:55:40 +00:00
|
|
|
util::errinfo_comment(util::stackTooDeepString)
|
2017-01-20 18:01:19 +00:00
|
|
|
);
|
2020-11-05 17:03:32 +00:00
|
|
|
while (!stackLayout.empty() && stackLayout.back() != static_cast<int>(stackLayout.size() - 1))
|
2016-05-19 17:32:14 +00:00
|
|
|
if (stackLayout.back() < 0)
|
|
|
|
{
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
stackLayout.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
m_context << swapInstruction(static_cast<unsigned>(stackLayout.size()) - static_cast<unsigned>(stackLayout.back()) - 1u);
|
2023-08-14 19:37:31 +00:00
|
|
|
std::swap(stackLayout[static_cast<size_t>(stackLayout.back())], stackLayout.back());
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
2020-06-02 13:42:46 +00:00
|
|
|
for (size_t i = 0; i < stackLayout.size(); ++i)
|
|
|
|
if (stackLayout[i] != static_cast<int>(i))
|
2018-05-03 14:40:33 +00:00
|
|
|
solAssert(false, "Invalid stack layout on cleanup.");
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2020-05-11 12:59:06 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: _function.parameters() + _function.returnParameters())
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context.removeVariable(*variable);
|
|
|
|
|
|
|
|
m_context.adjustStackOffset(-(int)c_returnValuesSize);
|
|
|
|
|
2017-06-29 08:48:53 +00:00
|
|
|
/// The constructor and the fallback function doesn't to jump out.
|
2018-07-10 16:39:26 +00:00
|
|
|
if (!_function.isConstructor())
|
|
|
|
{
|
|
|
|
solAssert(m_context.numberOfLocalVariables() == 0, "");
|
2019-09-09 16:22:02 +00:00
|
|
|
if (!_function.isFallback() && !_function.isReceive())
|
2019-12-11 16:31:36 +00:00
|
|
|
m_context.appendJump(evmasm::AssemblyItem::JumpType::OutOfFunction);
|
2018-07-10 16:39:26 +00:00
|
|
|
}
|
2018-05-03 14:40:33 +00:00
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly)
|
|
|
|
{
|
|
|
|
unsigned startStackHeight = m_context.stackHeight();
|
2021-08-04 16:38:10 +00:00
|
|
|
yul::ExternalIdentifierAccess::CodeGenerator identifierAccessCodeGen = [&](
|
|
|
|
yul::Identifier const& _identifier,
|
|
|
|
yul::IdentifierContext _context,
|
|
|
|
yul::AbstractAssembly& _assembly
|
|
|
|
)
|
2017-03-14 14:41:23 +00:00
|
|
|
{
|
2021-08-04 16:11:00 +00:00
|
|
|
solAssert(_context == yul::IdentifierContext::RValue || _context == yul::IdentifierContext::LValue, "");
|
2017-03-14 14:41:23 +00:00
|
|
|
auto ref = _inlineAssembly.annotation().externalReferences.find(&_identifier);
|
|
|
|
solAssert(ref != _inlineAssembly.annotation().externalReferences.end(), "");
|
|
|
|
Declaration const* decl = ref->second.declaration;
|
|
|
|
solAssert(!!decl, "");
|
2018-10-15 09:58:51 +00:00
|
|
|
if (_context == yul::IdentifierContext::RValue)
|
2017-03-14 14:41:23 +00:00
|
|
|
{
|
2017-04-28 16:15:18 +00:00
|
|
|
int const depositBefore = _assembly.stackHeight();
|
2017-03-14 14:41:23 +00:00
|
|
|
solAssert(!!decl->type(), "Type of declaration required but not yet determined.");
|
2020-11-05 13:43:29 +00:00
|
|
|
if (auto variable = dynamic_cast<VariableDeclaration const*>(decl))
|
2017-03-14 14:41:23 +00:00
|
|
|
{
|
2020-02-27 15:13:55 +00:00
|
|
|
solAssert(!variable->immutable(), "");
|
2019-07-01 13:34:34 +00:00
|
|
|
if (variable->isConstant())
|
|
|
|
{
|
2020-03-10 23:32:01 +00:00
|
|
|
variable = rootConstVariableDeclaration(*variable);
|
|
|
|
// If rootConstVariableDeclaration fails and returns nullptr,
|
|
|
|
// it should have failed in TypeChecker already, causing a compilation error.
|
|
|
|
// In such case we should not get here.
|
|
|
|
solAssert(variable, "");
|
|
|
|
|
2019-07-01 13:34:34 +00:00
|
|
|
u256 value;
|
|
|
|
if (variable->value()->annotation().type->category() == Type::Category::RationalNumber)
|
|
|
|
{
|
|
|
|
value = dynamic_cast<RationalNumberType const&>(*variable->value()->annotation().type).literalValue(nullptr);
|
|
|
|
if (FixedBytesType const* bytesType = dynamic_cast<FixedBytesType const*>(variable->type()))
|
|
|
|
value = value << (256 - 8 * bytesType->numBytes());
|
|
|
|
else
|
|
|
|
solAssert(variable->type()->category() == Type::Category::Integer, "");
|
|
|
|
}
|
|
|
|
else if (Literal const* literal = dynamic_cast<Literal const*>(variable->value().get()))
|
|
|
|
{
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* type = literal->annotation().type;
|
2019-07-01 13:34:34 +00:00
|
|
|
|
|
|
|
switch (type->category())
|
|
|
|
{
|
|
|
|
case Type::Category::Bool:
|
|
|
|
case Type::Category::Address:
|
2020-04-27 19:01:01 +00:00
|
|
|
// Either both the literal and the variable are bools, or they are both addresses.
|
|
|
|
// If they are both bools, comparing category is the same as comparing the types.
|
|
|
|
// If they are both addresses, compare category so that payable/nonpayable is not compared.
|
|
|
|
solAssert(type->category() == variable->annotation().type->category(), "");
|
2019-07-01 13:34:34 +00:00
|
|
|
value = type->literalValue(literal);
|
|
|
|
break;
|
|
|
|
case Type::Category::StringLiteral:
|
|
|
|
{
|
|
|
|
StringLiteralType const& stringLiteral = dynamic_cast<StringLiteralType const&>(*type);
|
|
|
|
solAssert(variable->type()->category() == Type::Category::FixedBytes, "");
|
|
|
|
unsigned const numBytes = dynamic_cast<FixedBytesType const&>(*variable->type()).numBytes();
|
|
|
|
solAssert(stringLiteral.value().size() <= numBytes, "");
|
|
|
|
value = u256(h256(stringLiteral.value(), h256::AlignLeft));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(false, "Invalid constant in inline assembly.");
|
|
|
|
m_context << value;
|
|
|
|
}
|
|
|
|
else if (m_context.isStateVariable(decl))
|
2017-04-21 17:13:46 +00:00
|
|
|
{
|
|
|
|
auto const& location = m_context.storageLocationOfVariable(*decl);
|
2020-11-05 13:39:39 +00:00
|
|
|
if (ref->second.suffix == "slot")
|
2017-04-21 17:13:46 +00:00
|
|
|
m_context << location.first;
|
2020-11-05 13:39:39 +00:00
|
|
|
else if (ref->second.suffix == "offset")
|
2017-04-21 17:13:46 +00:00
|
|
|
m_context << u256(location.second);
|
|
|
|
else
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
else if (m_context.isLocalVariable(decl))
|
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
unsigned stackDiff = static_cast<unsigned>(_assembly.stackHeight()) - m_context.baseStackOffsetOfVariable(*variable);
|
2020-11-05 13:39:39 +00:00
|
|
|
if (!ref->second.suffix.empty())
|
2017-04-21 17:13:46 +00:00
|
|
|
{
|
2023-08-14 19:37:31 +00:00
|
|
|
std::string const& suffix = ref->second.suffix;
|
2020-11-05 13:39:39 +00:00
|
|
|
if (variable->type()->dataStoredIn(DataLocation::Storage))
|
2017-04-25 10:56:34 +00:00
|
|
|
{
|
2020-11-05 13:39:39 +00:00
|
|
|
solAssert(suffix == "offset" || suffix == "slot", "");
|
|
|
|
unsigned size = variable->type()->sizeOnStack();
|
|
|
|
if (size == 2)
|
2017-04-25 10:56:34 +00:00
|
|
|
{
|
2020-11-05 13:39:39 +00:00
|
|
|
// slot plus offset
|
|
|
|
if (suffix == "offset")
|
|
|
|
stackDiff--;
|
2017-04-25 10:56:34 +00:00
|
|
|
}
|
2020-11-05 13:39:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(size == 1, "");
|
|
|
|
// only slot, offset is zero
|
|
|
|
if (suffix == "offset")
|
|
|
|
{
|
|
|
|
_assembly.appendConstant(u256(0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (variable->type()->dataStoredIn(DataLocation::CallData))
|
|
|
|
{
|
|
|
|
auto const* arrayType = dynamic_cast<ArrayType const*>(variable->type());
|
|
|
|
solAssert(
|
|
|
|
arrayType && arrayType->isDynamicallySized() && arrayType->dataStoredIn(DataLocation::CallData),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
solAssert(suffix == "offset" || suffix == "length", "");
|
|
|
|
solAssert(variable->type()->sizeOnStack() == 2, "");
|
|
|
|
if (suffix == "length")
|
|
|
|
stackDiff--;
|
2017-04-25 10:56:34 +00:00
|
|
|
}
|
2021-09-22 10:06:57 +00:00
|
|
|
else if (
|
|
|
|
auto const* functionType = dynamic_cast<FunctionType const*>(variable->type());
|
|
|
|
functionType && functionType->kind() == FunctionType::Kind::External
|
|
|
|
)
|
|
|
|
{
|
|
|
|
solAssert(suffix == "selector" || suffix == "address", "");
|
|
|
|
solAssert(variable->type()->sizeOnStack() == 2, "");
|
|
|
|
if (suffix == "selector")
|
|
|
|
stackDiff--;
|
|
|
|
}
|
2020-11-05 13:39:39 +00:00
|
|
|
else
|
|
|
|
solAssert(false, "");
|
2017-04-21 17:13:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(variable->type()->sizeOnStack() == 1, "");
|
|
|
|
if (stackDiff < 1 || stackDiff > 16)
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2020-07-01 07:46:06 +00:00
|
|
|
StackTooDeepError() <<
|
2017-04-21 17:13:46 +00:00
|
|
|
errinfo_sourceLocation(_inlineAssembly.location()) <<
|
2022-05-04 21:55:40 +00:00
|
|
|
util::errinfo_comment(util::stackTooDeepString)
|
2017-04-21 17:13:46 +00:00
|
|
|
);
|
2017-04-28 16:15:18 +00:00
|
|
|
_assembly.appendInstruction(dupInstruction(stackDiff));
|
2017-04-21 17:13:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(false, "");
|
2017-03-14 14:41:23 +00:00
|
|
|
}
|
|
|
|
else if (auto contract = dynamic_cast<ContractDefinition const*>(decl))
|
|
|
|
{
|
2020-11-05 13:39:39 +00:00
|
|
|
solAssert(ref->second.suffix.empty(), "");
|
2017-03-14 14:41:23 +00:00
|
|
|
solAssert(contract->isLibrary(), "");
|
2017-04-28 16:15:18 +00:00
|
|
|
_assembly.appendLinkerSymbol(contract->fullyQualifiedName());
|
2017-03-14 14:41:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
solAssert(false, "Invalid declaration type.");
|
2020-06-02 13:42:46 +00:00
|
|
|
solAssert(_assembly.stackHeight() - depositBefore == static_cast<int>(ref->second.valueSize), "");
|
2017-03-22 18:02:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-14 14:41:23 +00:00
|
|
|
// lvalue context
|
|
|
|
auto variable = dynamic_cast<VariableDeclaration const*>(decl);
|
2020-06-02 13:42:46 +00:00
|
|
|
unsigned stackDiff = static_cast<unsigned>(_assembly.stackHeight()) - m_context.baseStackOffsetOfVariable(*variable) - 1;
|
2023-08-14 19:37:31 +00:00
|
|
|
std::string const& suffix = ref->second.suffix;
|
2020-11-05 13:39:39 +00:00
|
|
|
if (variable->type()->dataStoredIn(DataLocation::Storage))
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
!!variable && m_context.isLocalVariable(variable),
|
|
|
|
"Can only assign to stack variables in inline assembly."
|
2016-05-19 17:32:14 +00:00
|
|
|
);
|
2020-11-05 13:39:39 +00:00
|
|
|
solAssert(variable->type()->sizeOnStack() == 1, "");
|
|
|
|
solAssert(suffix == "slot", "");
|
|
|
|
}
|
|
|
|
else if (variable->type()->dataStoredIn(DataLocation::CallData))
|
|
|
|
{
|
2021-09-10 14:04:49 +00:00
|
|
|
if (auto const* arrayType = dynamic_cast<ArrayType const*>(variable->type()))
|
|
|
|
{
|
|
|
|
if (arrayType->isDynamicallySized())
|
|
|
|
{
|
|
|
|
solAssert(suffix == "offset" || suffix == "length", "");
|
|
|
|
solAssert(variable->type()->sizeOnStack() == 2, "");
|
|
|
|
if (suffix == "length")
|
|
|
|
stackDiff--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(variable->type()->sizeOnStack() == 1, "");
|
|
|
|
solAssert(suffix.empty(), "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto const* structType = dynamic_cast<StructType const*>(variable->type());
|
|
|
|
solAssert(structType, "");
|
|
|
|
solAssert(variable->type()->sizeOnStack() == 1, "");
|
|
|
|
solAssert(suffix.empty(), "");
|
|
|
|
}
|
2020-11-05 13:39:39 +00:00
|
|
|
}
|
2021-09-22 10:06:57 +00:00
|
|
|
else if (
|
|
|
|
auto const* functionType = dynamic_cast<FunctionType const*>(variable->type());
|
|
|
|
functionType && functionType->kind() == FunctionType::Kind::External
|
|
|
|
)
|
|
|
|
{
|
|
|
|
solAssert(suffix == "selector" || suffix == "address", "");
|
|
|
|
solAssert(variable->type()->sizeOnStack() == 2, "");
|
|
|
|
if (suffix == "selector")
|
|
|
|
stackDiff--;
|
|
|
|
}
|
2020-11-05 13:39:39 +00:00
|
|
|
else
|
|
|
|
solAssert(suffix.empty(), "");
|
|
|
|
|
2021-04-26 17:28:24 +00:00
|
|
|
if (stackDiff > 16 || stackDiff < 1)
|
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
StackTooDeepError() <<
|
|
|
|
errinfo_sourceLocation(_inlineAssembly.location()) <<
|
2022-05-04 21:55:40 +00:00
|
|
|
util::errinfo_comment(util::stackTooDeepString)
|
2021-04-26 17:28:24 +00:00
|
|
|
);
|
2017-04-28 16:15:18 +00:00
|
|
|
_assembly.appendInstruction(swapInstruction(stackDiff));
|
|
|
|
_assembly.appendInstruction(Instruction::POP);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
2017-03-14 14:41:23 +00:00
|
|
|
};
|
2020-02-04 11:01:13 +00:00
|
|
|
|
|
|
|
yul::Block const* code = &_inlineAssembly.operations();
|
|
|
|
yul::AsmAnalysisInfo* analysisInfo = _inlineAssembly.annotation().analysisInfo.get();
|
|
|
|
|
|
|
|
// Only used in the scope below, but required to live outside to keep the
|
2023-08-14 19:37:31 +00:00
|
|
|
// std::shared_ptr's alive
|
2020-02-06 15:32:33 +00:00
|
|
|
yul::Object object = {};
|
2020-02-04 11:01:13 +00:00
|
|
|
|
|
|
|
// The optimiser cannot handle external references
|
|
|
|
if (
|
|
|
|
m_optimiserSettings.runYulOptimiser &&
|
|
|
|
_inlineAssembly.annotation().externalReferences.empty()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
yul::EVMDialect const* dialect = dynamic_cast<decltype(dialect)>(&_inlineAssembly.dialect());
|
|
|
|
solAssert(dialect, "");
|
|
|
|
|
2020-02-06 15:32:33 +00:00
|
|
|
// Create a modifiable copy of the code and analysis
|
2023-08-14 19:37:31 +00:00
|
|
|
object.code = std::make_shared<yul::Block>(yul::ASTCopier().translate(*code));
|
|
|
|
object.analysisInfo = std::make_shared<yul::AsmAnalysisInfo>(yul::AsmAnalyzer::analyzeStrictAssertCorrect(*dialect, object));
|
2020-02-06 15:32:33 +00:00
|
|
|
|
|
|
|
m_context.optimizeYul(object, *dialect, m_optimiserSettings);
|
2020-02-04 11:01:13 +00:00
|
|
|
|
|
|
|
code = object.code.get();
|
|
|
|
analysisInfo = object.analysisInfo.get();
|
|
|
|
}
|
|
|
|
|
2019-02-13 10:35:49 +00:00
|
|
|
yul::CodeGenerator::assemble(
|
2020-02-04 11:01:13 +00:00
|
|
|
*code,
|
|
|
|
*analysisInfo,
|
2019-01-16 10:44:11 +00:00
|
|
|
*m_context.assemblyPtr(),
|
2019-02-21 20:56:30 +00:00
|
|
|
m_context.evmVersion(),
|
2021-08-04 16:38:10 +00:00
|
|
|
identifierAccessCodeGen,
|
2019-02-26 18:55:13 +00:00
|
|
|
false,
|
|
|
|
m_optimiserSettings.optimizeStackAllocation
|
2016-05-19 17:32:14 +00:00
|
|
|
);
|
2020-06-02 13:42:46 +00:00
|
|
|
m_context.setStackOffset(static_cast<int>(startStackHeight));
|
2016-05-19 17:32:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:02:58 +00:00
|
|
|
bool ContractCompiler::visit(TryStatement const& _tryStatement)
|
|
|
|
{
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _tryStatement);
|
|
|
|
|
|
|
|
compileExpression(_tryStatement.externalCall());
|
2019-10-23 14:32:41 +00:00
|
|
|
int const returnSize = static_cast<int>(_tryStatement.externalCall().annotation().type->sizeOnStack());
|
2019-09-05 18:02:58 +00:00
|
|
|
|
|
|
|
// Stack: [ return values] <success flag>
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem successTag = m_context.appendConditionalJump();
|
2019-09-05 18:02:58 +00:00
|
|
|
|
|
|
|
// Catch case.
|
|
|
|
m_context.adjustStackOffset(-returnSize);
|
|
|
|
|
|
|
|
handleCatch(_tryStatement.clauses());
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem endTag = m_context.appendJumpToNew();
|
2019-09-05 18:02:58 +00:00
|
|
|
|
|
|
|
m_context << successTag;
|
|
|
|
m_context.adjustStackOffset(returnSize);
|
|
|
|
{
|
|
|
|
// Success case.
|
|
|
|
// Stack: return values
|
|
|
|
TryCatchClause const& successClause = *_tryStatement.clauses().front();
|
|
|
|
if (successClause.parameters())
|
|
|
|
{
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<Type const*> exprTypes{_tryStatement.externalCall().annotation().type};
|
2019-09-05 18:02:58 +00:00
|
|
|
if (auto tupleType = dynamic_cast<TupleType const*>(exprTypes.front()))
|
|
|
|
exprTypes = tupleType->components();
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& params = successClause.parameters()->parameters();
|
2019-09-05 18:02:58 +00:00
|
|
|
solAssert(exprTypes.size() == params.size(), "");
|
|
|
|
for (size_t i = 0; i < exprTypes.size(); ++i)
|
|
|
|
solAssert(params[i] && exprTypes[i] && *params[i]->annotation().type == *exprTypes[i], "");
|
|
|
|
}
|
|
|
|
else
|
2020-06-02 13:42:46 +00:00
|
|
|
CompilerUtils(m_context).popStackSlots(static_cast<size_t>(returnSize));
|
2019-09-05 18:02:58 +00:00
|
|
|
|
|
|
|
_tryStatement.clauses().front()->accept(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_context << endTag;
|
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
void ContractCompiler::handleCatch(std::vector<ASTPointer<TryCatchClause>> const& _catchClauses)
|
2019-09-05 18:02:58 +00:00
|
|
|
{
|
|
|
|
// Stack is empty.
|
2020-10-20 13:30:46 +00:00
|
|
|
ASTPointer<TryCatchClause> error{};
|
|
|
|
ASTPointer<TryCatchClause> panic{};
|
2019-09-05 18:02:58 +00:00
|
|
|
ASTPointer<TryCatchClause> fallback{};
|
|
|
|
for (size_t i = 1; i < _catchClauses.size(); ++i)
|
|
|
|
if (_catchClauses[i]->errorName() == "Error")
|
2020-10-20 13:30:46 +00:00
|
|
|
error = _catchClauses[i];
|
|
|
|
else if (_catchClauses[i]->errorName() == "Panic")
|
|
|
|
panic = _catchClauses[i];
|
2019-09-05 18:02:58 +00:00
|
|
|
else if (_catchClauses[i]->errorName().empty())
|
|
|
|
fallback = _catchClauses[i];
|
|
|
|
else
|
|
|
|
solAssert(false, "");
|
|
|
|
|
2020-10-20 13:30:46 +00:00
|
|
|
solAssert(_catchClauses.size() == 1ul + (error ? 1 : 0) + (panic ? 1 : 0) + (fallback ? 1 : 0), "");
|
2019-09-05 18:02:58 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem endTag = m_context.newTag();
|
|
|
|
evmasm::AssemblyItem fallbackTag = m_context.newTag();
|
2020-10-20 13:30:46 +00:00
|
|
|
evmasm::AssemblyItem panicTag = m_context.newTag();
|
|
|
|
if (error || panic)
|
|
|
|
// Note that this function returns zero on failure, which is not a problem yet,
|
|
|
|
// but will be a problem once we allow user-defined errors.
|
|
|
|
m_context.callYulFunction(m_context.utilFunctions().returnDataSelectorFunction(), 0, 1);
|
|
|
|
// stack: <selector>
|
|
|
|
if (error)
|
2019-09-05 18:02:58 +00:00
|
|
|
{
|
|
|
|
solAssert(
|
2020-10-20 13:30:46 +00:00
|
|
|
error->parameters() &&
|
|
|
|
error->parameters()->parameters().size() == 1 &&
|
|
|
|
error->parameters()->parameters().front() &&
|
|
|
|
*error->parameters()->parameters().front()->annotation().type == *TypeProvider::stringMemory(),
|
2019-09-05 18:02:58 +00:00
|
|
|
""
|
|
|
|
);
|
|
|
|
solAssert(m_context.evmVersion().supportsReturndata(), "");
|
|
|
|
|
2020-10-20 13:30:46 +00:00
|
|
|
// stack: <selector>
|
2022-09-27 12:13:44 +00:00
|
|
|
m_context << Instruction::DUP1 << util::selectorFromSignatureU32("Error(string)") << Instruction::EQ;
|
2020-10-20 13:30:46 +00:00
|
|
|
m_context << Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(panicTag);
|
|
|
|
m_context << Instruction::POP; // remove selector
|
|
|
|
|
2019-09-05 18:02:58 +00:00
|
|
|
// Try to decode the error message.
|
|
|
|
// If this fails, leaves 0 on the stack, otherwise the pointer to the data string.
|
2020-02-10 10:58:36 +00:00
|
|
|
m_context.callYulFunction(m_context.utilFunctions().tryDecodeErrorMessageFunction(), 0, 1);
|
2019-09-05 18:02:58 +00:00
|
|
|
m_context << Instruction::DUP1;
|
|
|
|
AssemblyItem decodeSuccessTag = m_context.appendConditionalJump();
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
m_context.appendJumpTo(fallbackTag);
|
|
|
|
m_context.adjustStackOffset(1);
|
|
|
|
|
|
|
|
m_context << decodeSuccessTag;
|
2020-10-20 13:30:46 +00:00
|
|
|
error->accept(*this);
|
|
|
|
m_context.appendJumpTo(endTag);
|
|
|
|
m_context.adjustStackOffset(1);
|
|
|
|
}
|
|
|
|
m_context << panicTag;
|
|
|
|
if (panic)
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
panic->parameters() &&
|
|
|
|
panic->parameters()->parameters().size() == 1 &&
|
|
|
|
panic->parameters()->parameters().front() &&
|
|
|
|
*panic->parameters()->parameters().front()->annotation().type == *TypeProvider::uint256(),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
solAssert(m_context.evmVersion().supportsReturndata(), "");
|
|
|
|
|
|
|
|
// stack: <selector>
|
2022-09-27 12:13:44 +00:00
|
|
|
m_context << util::selectorFromSignatureU32("Panic(uint256)") << Instruction::EQ;
|
2020-10-20 13:30:46 +00:00
|
|
|
m_context << Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(fallbackTag);
|
|
|
|
|
|
|
|
m_context.callYulFunction(m_context.utilFunctions().tryDecodePanicDataFunction(), 0, 2);
|
|
|
|
m_context << Instruction::SWAP1;
|
|
|
|
// stack: <code> <success>
|
|
|
|
AssemblyItem decodeSuccessTag = m_context.appendConditionalJump();
|
|
|
|
m_context << Instruction::POP;
|
|
|
|
m_context.appendJumpTo(fallbackTag);
|
|
|
|
m_context.adjustStackOffset(1);
|
|
|
|
|
|
|
|
m_context << decodeSuccessTag;
|
|
|
|
panic->accept(*this);
|
2019-09-05 18:02:58 +00:00
|
|
|
m_context.appendJumpTo(endTag);
|
2020-10-20 13:30:46 +00:00
|
|
|
m_context.adjustStackOffset(1);
|
2019-09-05 18:02:58 +00:00
|
|
|
}
|
2020-10-20 13:30:46 +00:00
|
|
|
if (error || panic)
|
|
|
|
m_context << Instruction::POP; // selector
|
2019-09-05 18:02:58 +00:00
|
|
|
m_context << fallbackTag;
|
|
|
|
if (fallback)
|
|
|
|
{
|
|
|
|
if (fallback->parameters())
|
|
|
|
{
|
|
|
|
solAssert(m_context.evmVersion().supportsReturndata(), "");
|
|
|
|
solAssert(
|
|
|
|
fallback->parameters()->parameters().size() == 1 &&
|
|
|
|
fallback->parameters()->parameters().front() &&
|
|
|
|
*fallback->parameters()->parameters().front()->annotation().type == *TypeProvider::bytesMemory(),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
CompilerUtils(m_context).returnDataToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
fallback->accept(*this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// re-throw
|
|
|
|
if (m_context.evmVersion().supportsReturndata())
|
|
|
|
m_context.appendInlineAssembly(R"({
|
|
|
|
returndatacopy(0, 0, returndatasize())
|
|
|
|
revert(0, returndatasize())
|
|
|
|
})");
|
|
|
|
else
|
2020-01-22 14:48:56 +00:00
|
|
|
// Since both returndata and revert are >=byzantium, this should be unreachable.
|
|
|
|
solAssert(false, "");
|
2019-09-05 18:02:58 +00:00
|
|
|
}
|
|
|
|
m_context << endTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(TryCatchClause const& _clause)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _clause);
|
|
|
|
|
|
|
|
unsigned varSize = 0;
|
|
|
|
|
|
|
|
if (_clause.parameters())
|
2021-03-31 18:03:04 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& varDecl: _clause.parameters()->parameters() | ranges::views::reverse)
|
2019-09-05 18:02:58 +00:00
|
|
|
{
|
|
|
|
solAssert(varDecl, "");
|
|
|
|
varSize += varDecl->annotation().type->sizeOnStack();
|
|
|
|
m_context.addVariable(*varDecl, varSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
_clause.block().accept(*this);
|
|
|
|
|
|
|
|
m_context.removeVariablesAboveStackHeight(m_context.stackHeight() - varSize);
|
|
|
|
CompilerUtils(m_context).popStackSlots(varSize);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
bool ContractCompiler::visit(IfStatement const& _ifStatement)
|
|
|
|
{
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _ifStatement);
|
|
|
|
compileExpression(_ifStatement.condition());
|
|
|
|
m_context << Instruction::ISZERO;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem falseTag = m_context.appendConditionalJump();
|
|
|
|
evmasm::AssemblyItem endTag = falseTag;
|
2016-05-19 17:32:14 +00:00
|
|
|
_ifStatement.trueStatement().accept(*this);
|
|
|
|
if (_ifStatement.falseStatement())
|
|
|
|
{
|
|
|
|
endTag = m_context.appendJumpToNew();
|
|
|
|
m_context << falseTag;
|
|
|
|
_ifStatement.falseStatement()->accept(*this);
|
|
|
|
}
|
|
|
|
m_context << endTag;
|
|
|
|
|
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(WhileStatement const& _whileStatement)
|
|
|
|
{
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _whileStatement);
|
2018-05-14 13:26:10 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem loopStart = m_context.newTag();
|
|
|
|
evmasm::AssemblyItem loopEnd = m_context.newTag();
|
2018-12-10 18:02:39 +00:00
|
|
|
m_breakTags.emplace_back(loopEnd, m_context.stackHeight());
|
2018-05-03 14:40:33 +00:00
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context << loopStart;
|
2016-07-30 07:13:05 +00:00
|
|
|
|
2018-05-14 13:26:10 +00:00
|
|
|
if (_whileStatement.isDoWhile())
|
2016-07-30 07:13:05 +00:00
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem condition = m_context.newTag();
|
2018-12-10 18:02:39 +00:00
|
|
|
m_continueTags.emplace_back(condition, m_context.stackHeight());
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2018-05-14 13:26:10 +00:00
|
|
|
_whileStatement.body().accept(*this);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2018-05-14 13:26:10 +00:00
|
|
|
m_context << condition;
|
|
|
|
compileExpression(_whileStatement.condition());
|
|
|
|
m_context << Instruction::ISZERO << Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(loopStart);
|
|
|
|
}
|
|
|
|
else
|
2016-07-30 07:13:05 +00:00
|
|
|
{
|
2018-12-10 18:02:39 +00:00
|
|
|
m_continueTags.emplace_back(loopStart, m_context.stackHeight());
|
2016-07-30 07:13:05 +00:00
|
|
|
compileExpression(_whileStatement.condition());
|
|
|
|
m_context << Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(loopEnd);
|
|
|
|
|
2018-05-14 13:26:10 +00:00
|
|
|
_whileStatement.body().accept(*this);
|
|
|
|
|
|
|
|
m_context.appendJumpTo(loopStart);
|
|
|
|
}
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context << loopEnd;
|
|
|
|
|
|
|
|
m_continueTags.pop_back();
|
|
|
|
m_breakTags.pop_back();
|
|
|
|
|
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(ForStatement const& _forStatement)
|
|
|
|
{
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _forStatement);
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem loopStart = m_context.newTag();
|
|
|
|
evmasm::AssemblyItem loopEnd = m_context.newTag();
|
|
|
|
evmasm::AssemblyItem loopNext = m_context.newTag();
|
2018-05-03 14:40:33 +00:00
|
|
|
|
2018-07-04 12:14:41 +00:00
|
|
|
storeStackHeight(&_forStatement);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
|
|
|
if (_forStatement.initializationExpression())
|
|
|
|
_forStatement.initializationExpression()->accept(*this);
|
|
|
|
|
2018-12-10 18:02:39 +00:00
|
|
|
m_breakTags.emplace_back(loopEnd, m_context.stackHeight());
|
|
|
|
m_continueTags.emplace_back(loopNext, m_context.stackHeight());
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context << loopStart;
|
|
|
|
|
|
|
|
// if there is no terminating condition in for, default is to always be true
|
|
|
|
if (_forStatement.condition())
|
|
|
|
{
|
|
|
|
compileExpression(*_forStatement.condition());
|
|
|
|
m_context << Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(loopEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
_forStatement.body().accept(*this);
|
|
|
|
|
|
|
|
m_context << loopNext;
|
|
|
|
|
|
|
|
// for's loop expression if existing
|
|
|
|
if (_forStatement.loopExpression())
|
|
|
|
_forStatement.loopExpression()->accept(*this);
|
|
|
|
|
|
|
|
m_context.appendJumpTo(loopStart);
|
2018-05-03 14:40:33 +00:00
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
m_context << loopEnd;
|
|
|
|
|
|
|
|
m_continueTags.pop_back();
|
|
|
|
m_breakTags.pop_back();
|
|
|
|
|
2018-05-03 14:40:33 +00:00
|
|
|
// For the case where no break/return is executed:
|
|
|
|
// loop initialization variables have to be freed
|
|
|
|
popScopedVariables(&_forStatement);
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(Continue const& _continueStatement)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _continueStatement);
|
2018-06-11 23:46:23 +00:00
|
|
|
solAssert(!m_continueTags.empty(), "");
|
2018-07-04 12:14:41 +00:00
|
|
|
CompilerUtils(m_context).popAndJump(m_continueTags.back().second, m_continueTags.back().first);
|
2016-05-19 17:32:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(Break const& _breakStatement)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _breakStatement);
|
2018-06-11 23:46:23 +00:00
|
|
|
solAssert(!m_breakTags.empty(), "");
|
2018-07-04 12:14:41 +00:00
|
|
|
CompilerUtils(m_context).popAndJump(m_breakTags.back().second, m_breakTags.back().first);
|
2016-05-19 17:32:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(Return const& _return)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _return);
|
|
|
|
if (Expression const* expression = _return.expression())
|
|
|
|
{
|
|
|
|
solAssert(_return.annotation().functionReturnParameters, "Invalid return parameters pointer.");
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& returnParameters =
|
2016-05-19 17:32:14 +00:00
|
|
|
_return.annotation().functionReturnParameters->parameters();
|
|
|
|
TypePointers types;
|
|
|
|
for (auto const& retVariable: returnParameters)
|
|
|
|
types.push_back(retVariable->annotation().type);
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* expectedType;
|
2016-05-19 17:32:14 +00:00
|
|
|
if (expression->annotation().type->category() == Type::Category::Tuple || types.size() != 1)
|
2022-08-23 17:28:45 +00:00
|
|
|
expectedType = TypeProvider::tuple(std::move(types));
|
2016-05-19 17:32:14 +00:00
|
|
|
else
|
|
|
|
expectedType = types.front();
|
|
|
|
compileExpression(*expression, expectedType);
|
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
for (auto const& retVariable: returnParameters | ranges::views::reverse)
|
2016-05-19 17:32:14 +00:00
|
|
|
CompilerUtils(m_context).moveToStackVariable(*retVariable);
|
|
|
|
}
|
2018-05-03 14:40:33 +00:00
|
|
|
|
2018-07-04 12:14:41 +00:00
|
|
|
CompilerUtils(m_context).popAndJump(m_returnTags.back().second, m_returnTags.back().first);
|
2016-05-19 17:32:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:38:08 +00:00
|
|
|
bool ContractCompiler::visit(Throw const&)
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
2018-08-07 22:38:08 +00:00
|
|
|
solAssert(false, "Throw statement is disallowed.");
|
2016-05-19 17:32:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-16 15:55:21 +00:00
|
|
|
bool ContractCompiler::visit(EmitStatement const& _emit)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _emit);
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
compileExpression(_emit.eventCall());
|
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-28 11:56:22 +00:00
|
|
|
bool ContractCompiler::visit(RevertStatement const& _revert)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _revert);
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
compileExpression(_revert.errorCall());
|
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
bool ContractCompiler::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
|
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
|
2018-05-03 14:40:33 +00:00
|
|
|
|
|
|
|
// Local variable slots are reserved when their declaration is visited,
|
|
|
|
// and freed in the end of their scope.
|
2019-06-03 10:01:11 +00:00
|
|
|
for (auto decl: _variableDeclarationStatement.declarations())
|
|
|
|
if (decl)
|
2020-11-19 10:00:18 +00:00
|
|
|
appendStackVariableInitialisation(*decl, !_variableDeclarationStatement.initialValue());
|
2018-05-03 14:40:33 +00:00
|
|
|
|
|
|
|
StackHeightChecker checker(m_context);
|
2016-05-19 17:32:14 +00:00
|
|
|
if (Expression const* expression = _variableDeclarationStatement.initialValue())
|
|
|
|
{
|
|
|
|
CompilerUtils utils(m_context);
|
|
|
|
compileExpression(*expression);
|
|
|
|
TypePointers valueTypes;
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto tupleType = dynamic_cast<TupleType const*>(expression->annotation().type))
|
2016-05-19 17:32:14 +00:00
|
|
|
valueTypes = tupleType->components();
|
|
|
|
else
|
|
|
|
valueTypes = TypePointers{expression->annotation().type};
|
2018-07-04 16:34:24 +00:00
|
|
|
auto const& declarations = _variableDeclarationStatement.declarations();
|
|
|
|
solAssert(declarations.size() == valueTypes.size(), "");
|
|
|
|
for (size_t i = 0; i < declarations.size(); ++i)
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
2018-07-04 16:34:24 +00:00
|
|
|
size_t j = declarations.size() - i - 1;
|
2016-05-19 17:32:14 +00:00
|
|
|
solAssert(!!valueTypes[j], "");
|
2018-07-04 16:34:24 +00:00
|
|
|
if (VariableDeclaration const* varDecl = declarations[j].get())
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
|
|
|
utils.convertType(*valueTypes[j], *varDecl->annotation().type);
|
|
|
|
utils.moveToStackVariable(*varDecl);
|
|
|
|
}
|
2018-07-04 16:34:24 +00:00
|
|
|
else
|
|
|
|
utils.popStackElement(*valueTypes[j]);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(ExpressionStatement const& _expressionStatement)
|
|
|
|
{
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _expressionStatement);
|
|
|
|
Expression const& expression = _expressionStatement.expression();
|
|
|
|
compileExpression(expression);
|
|
|
|
CompilerUtils(m_context).popStackElement(*expression.annotation().type);
|
|
|
|
checker.check();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractCompiler::visit(PlaceholderStatement const& _placeholderStatement)
|
|
|
|
{
|
|
|
|
StackHeightChecker checker(m_context);
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _placeholderStatement);
|
2020-07-22 08:28:04 +00:00
|
|
|
solAssert(m_context.arithmetic() == Arithmetic::Checked, "Placeholder cannot be used inside checked block.");
|
2016-05-19 17:32:14 +00:00
|
|
|
appendModifierOrFunctionCode();
|
2020-07-22 08:28:04 +00:00
|
|
|
solAssert(m_context.arithmetic() == Arithmetic::Checked, "Arithmetic not reset to 'checked'.");
|
2016-05-19 17:32:14 +00:00
|
|
|
checker.check();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:40:33 +00:00
|
|
|
bool ContractCompiler::visit(Block const& _block)
|
|
|
|
{
|
2021-05-26 08:35:18 +00:00
|
|
|
m_context.pushVisitedNodes(&_block);
|
2020-07-22 08:28:04 +00:00
|
|
|
if (_block.unchecked())
|
|
|
|
{
|
|
|
|
solAssert(m_context.arithmetic() == Arithmetic::Checked, "");
|
|
|
|
m_context.setArithmetic(Arithmetic::Wrapping);
|
|
|
|
}
|
2018-07-04 12:14:41 +00:00
|
|
|
storeStackHeight(&_block);
|
2018-05-03 14:40:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContractCompiler::endVisit(Block const& _block)
|
|
|
|
{
|
2020-07-22 08:28:04 +00:00
|
|
|
if (_block.unchecked())
|
|
|
|
{
|
|
|
|
solAssert(m_context.arithmetic() == Arithmetic::Wrapping, "");
|
|
|
|
m_context.setArithmetic(Arithmetic::Checked);
|
|
|
|
}
|
2018-05-03 14:40:33 +00:00
|
|
|
// Frees local variables declared in the scope of this block.
|
|
|
|
popScopedVariables(&_block);
|
2021-05-26 08:35:18 +00:00
|
|
|
m_context.popVisitedNodes();
|
2018-05-03 14:40:33 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
void ContractCompiler::appendMissingFunctions()
|
|
|
|
{
|
|
|
|
while (Declaration const* function = m_context.nextFunctionToCompile())
|
|
|
|
{
|
|
|
|
m_context.setStackOffset(0);
|
|
|
|
function->accept(*this);
|
|
|
|
solAssert(m_context.nextFunctionToCompile() != function, "Compiled the wrong function?");
|
|
|
|
}
|
2017-01-19 16:21:55 +00:00
|
|
|
m_context.appendMissingLowLevelFunctions();
|
2020-05-28 11:17:16 +00:00
|
|
|
m_context.appendYulUtilityFunctions(m_optimiserSettings);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContractCompiler::appendModifierOrFunctionCode()
|
|
|
|
{
|
|
|
|
solAssert(m_currentFunction, "");
|
2016-08-06 12:48:59 +00:00
|
|
|
unsigned stackSurplus = 0;
|
|
|
|
Block const* codeBlock = nullptr;
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<VariableDeclaration const*> addedVariables;
|
2016-08-06 12:48:59 +00:00
|
|
|
|
|
|
|
m_modifierDepth++;
|
2019-10-08 09:00:40 +00:00
|
|
|
m_context.setModifierDepth(m_modifierDepth);
|
2016-08-06 12:48:59 +00:00
|
|
|
|
2016-05-19 17:32:14 +00:00
|
|
|
if (m_modifierDepth >= m_currentFunction->modifiers().size())
|
2016-06-06 17:36:19 +00:00
|
|
|
{
|
|
|
|
solAssert(m_currentFunction->isImplemented(), "");
|
2016-08-06 12:48:59 +00:00
|
|
|
codeBlock = &m_currentFunction->body();
|
2016-06-06 17:36:19 +00:00
|
|
|
}
|
2016-05-19 17:32:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ASTPointer<ModifierInvocation> const& modifierInvocation = m_currentFunction->modifiers()[m_modifierDepth];
|
|
|
|
|
|
|
|
// constructor call should be excluded
|
2020-08-11 09:18:22 +00:00
|
|
|
if (dynamic_cast<ContractDefinition const*>(modifierInvocation->name().annotation().referencedDeclaration))
|
2016-05-19 17:32:14 +00:00
|
|
|
appendModifierOrFunctionCode();
|
2016-08-06 12:48:59 +00:00
|
|
|
else
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
2020-11-16 15:42:17 +00:00
|
|
|
ModifierDefinition const& referencedModifier = dynamic_cast<ModifierDefinition const&>(
|
2020-08-11 09:18:22 +00:00
|
|
|
*modifierInvocation->name().annotation().referencedDeclaration
|
2020-11-16 15:42:17 +00:00
|
|
|
);
|
|
|
|
VirtualLookup lookup = *modifierInvocation->name().annotation().requiredLookup;
|
|
|
|
solAssert(lookup == VirtualLookup::Virtual || lookup == VirtualLookup::Static, "");
|
|
|
|
ModifierDefinition const& modifier =
|
|
|
|
lookup == VirtualLookup::Virtual ?
|
|
|
|
referencedModifier.resolveVirtual(m_context.mostDerivedContract()) :
|
|
|
|
referencedModifier;
|
|
|
|
|
2016-08-06 12:48:59 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, modifier);
|
2018-04-10 09:22:26 +00:00
|
|
|
std::vector<ASTPointer<Expression>> const& modifierArguments =
|
|
|
|
modifierInvocation->arguments() ? *modifierInvocation->arguments() : std::vector<ASTPointer<Expression>>();
|
|
|
|
|
|
|
|
solAssert(modifier.parameters().size() == modifierArguments.size(), "");
|
2016-08-06 12:48:59 +00:00
|
|
|
for (unsigned i = 0; i < modifier.parameters().size(); ++i)
|
|
|
|
{
|
|
|
|
m_context.addVariable(*modifier.parameters()[i]);
|
2017-06-23 16:55:47 +00:00
|
|
|
addedVariables.push_back(modifier.parameters()[i].get());
|
2016-08-06 12:48:59 +00:00
|
|
|
compileExpression(
|
2018-04-10 09:22:26 +00:00
|
|
|
*modifierArguments[i],
|
2016-08-06 12:48:59 +00:00
|
|
|
modifier.parameters()[i]->annotation().type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:40:33 +00:00
|
|
|
stackSurplus = CompilerUtils::sizeOnStack(modifier.parameters());
|
2016-08-06 12:48:59 +00:00
|
|
|
codeBlock = &modifier.body();
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
2016-08-06 12:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (codeBlock)
|
|
|
|
{
|
2020-07-22 08:28:04 +00:00
|
|
|
m_context.setArithmetic(Arithmetic::Checked);
|
|
|
|
|
2020-10-22 17:19:14 +00:00
|
|
|
bool coderV2Outside = m_context.useABICoderV2();
|
|
|
|
m_context.setUseABICoderV2(*codeBlock->sourceUnit().annotation().useABICoderV2);
|
2020-10-06 14:32:05 +00:00
|
|
|
|
2018-12-10 18:02:39 +00:00
|
|
|
m_returnTags.emplace_back(m_context.newTag(), m_context.stackHeight());
|
2016-08-06 12:48:59 +00:00
|
|
|
codeBlock->accept(*this);
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2020-10-22 17:19:14 +00:00
|
|
|
m_context.setUseABICoderV2(coderV2Outside);
|
2020-10-06 14:32:05 +00:00
|
|
|
|
2016-08-06 12:48:59 +00:00
|
|
|
solAssert(!m_returnTags.empty(), "");
|
2018-05-03 14:40:33 +00:00
|
|
|
m_context << m_returnTags.back().first;
|
2016-08-06 12:48:59 +00:00
|
|
|
m_returnTags.pop_back();
|
2016-05-19 17:32:14 +00:00
|
|
|
|
2016-08-06 12:48:59 +00:00
|
|
|
CompilerUtils(m_context).popStackSlots(stackSurplus);
|
2017-06-23 16:55:47 +00:00
|
|
|
for (auto var: addedVariables)
|
|
|
|
m_context.removeVariable(*var);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
2016-08-06 12:48:59 +00:00
|
|
|
m_modifierDepth--;
|
2019-10-08 09:00:40 +00:00
|
|
|
m_context.setModifierDepth(m_modifierDepth);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 10:00:18 +00:00
|
|
|
void ContractCompiler::appendStackVariableInitialisation(
|
|
|
|
VariableDeclaration const& _variable,
|
|
|
|
bool _provideDefaultValue
|
|
|
|
)
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
|
|
|
CompilerContext::LocationSetter location(m_context, _variable);
|
|
|
|
m_context.addVariable(_variable);
|
2020-11-19 10:00:18 +00:00
|
|
|
if (!_provideDefaultValue && _variable.type()->dataStoredIn(DataLocation::Memory))
|
|
|
|
{
|
|
|
|
solAssert(_variable.type()->sizeOnStack() == 1, "");
|
|
|
|
m_context << u256(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
CompilerUtils(m_context).pushZeroValue(*_variable.annotation().type);
|
2016-05-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 16:12:05 +00:00
|
|
|
void ContractCompiler::compileExpression(Expression const& _expression, Type const* _targetType)
|
2016-05-19 17:32:14 +00:00
|
|
|
{
|
2020-01-22 14:48:56 +00:00
|
|
|
ExpressionCompiler expressionCompiler(m_context, m_optimiserSettings.runOrderLiterals);
|
2016-05-19 17:32:14 +00:00
|
|
|
expressionCompiler.compile(_expression);
|
|
|
|
if (_targetType)
|
|
|
|
CompilerUtils(m_context).convertType(*_expression.annotation().type, *_targetType);
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:40:33 +00:00
|
|
|
void ContractCompiler::popScopedVariables(ASTNode const* _node)
|
|
|
|
{
|
2018-07-04 12:14:41 +00:00
|
|
|
unsigned blockHeight = m_scopeStackHeight.at(m_modifierDepth).at(_node);
|
2018-07-10 16:39:26 +00:00
|
|
|
m_context.removeVariablesAboveStackHeight(blockHeight);
|
2018-07-06 07:56:27 +00:00
|
|
|
solAssert(m_context.stackHeight() >= blockHeight, "");
|
2018-05-03 14:40:33 +00:00
|
|
|
unsigned stackDiff = m_context.stackHeight() - blockHeight;
|
|
|
|
CompilerUtils(m_context).popStackSlots(stackDiff);
|
|
|
|
m_scopeStackHeight[m_modifierDepth].erase(_node);
|
2018-10-09 03:29:37 +00:00
|
|
|
if (m_scopeStackHeight[m_modifierDepth].empty())
|
2018-05-03 14:40:33 +00:00
|
|
|
m_scopeStackHeight.erase(m_modifierDepth);
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:14:41 +00:00
|
|
|
void ContractCompiler::storeStackHeight(ASTNode const* _node)
|
2018-05-03 14:40:33 +00:00
|
|
|
{
|
2018-07-04 12:14:41 +00:00
|
|
|
m_scopeStackHeight[m_modifierDepth][_node] = m_context.stackHeight();
|
2018-05-03 14:40:33 +00:00
|
|
|
}
|