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.
|
|
|
|
*/
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/CompilerContext.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libsolidity/codegen/Compiler.h>
|
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/interface/Version.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
|
2021-04-27 14:53:04 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmParser.h>
|
2020-09-01 15:39:51 +00:00
|
|
|
#include <libyul/AsmPrinter.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2019-02-13 10:35:49 +00:00
|
|
|
#include <libyul/backends/evm/AsmCodeGen.h>
|
2018-12-06 23:56:16 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2019-05-28 10:57:15 +00:00
|
|
|
#include <libyul/backends/evm/EVMMetrics.h>
|
2019-02-21 17:23:46 +00:00
|
|
|
#include <libyul/optimiser/Suite.h>
|
2019-07-09 15:23:14 +00:00
|
|
|
#include <libyul/Object.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <libyul/YulString.h>
|
2020-05-28 11:17:16 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
#include <libsolutil/Whiskers.h>
|
2020-10-12 14:01:45 +00:00
|
|
|
#include <libsolutil/FunctionSelector.h>
|
2020-01-22 14:48:56 +00:00
|
|
|
|
2018-11-23 10:31:45 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
|
|
|
#include <liblangutil/Scanner.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2017-01-19 16:21:55 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2017-09-25 15:00:16 +00:00
|
|
|
// Change to "define" to output all intermediate code
|
|
|
|
#undef SOL_OUTPUT_ASM
|
|
|
|
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
2020-01-22 14:48:56 +00:00
|
|
|
using namespace solidity::util;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2015-03-13 18:48:24 +00:00
|
|
|
void CompilerContext::addStateVariable(
|
|
|
|
VariableDeclaration const& _declaration,
|
|
|
|
u256 const& _storageOffset,
|
|
|
|
unsigned _byteOffset
|
|
|
|
)
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
2015-03-13 18:48:24 +00:00
|
|
|
m_stateVariables[&_declaration] = make_pair(_storageOffset, _byteOffset);
|
2014-11-07 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:30:04 +00:00
|
|
|
void CompilerContext::addImmutable(VariableDeclaration const& _variable)
|
|
|
|
{
|
|
|
|
solAssert(_variable.immutable(), "Attempted to register a non-immutable variable as immutable.");
|
|
|
|
solUnimplementedAssert(_variable.annotation().type->isValueType(), "Only immutable variables of value type are supported.");
|
|
|
|
solAssert(m_runtimeContext, "Attempted to register an immutable variable for runtime code generation.");
|
|
|
|
m_immutableVariables[&_variable] = CompilerUtils::generalPurposeMemoryStart + *m_reservedMemory;
|
|
|
|
solAssert(_variable.annotation().type->memoryHeadSize() == 32, "Memory writes might overlap.");
|
|
|
|
*m_reservedMemory += _variable.annotation().type->memoryHeadSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CompilerContext::immutableMemoryOffset(VariableDeclaration const& _variable) const
|
|
|
|
{
|
|
|
|
solAssert(m_immutableVariables.count(&_variable), "Memory offset of unknown immutable queried.");
|
|
|
|
solAssert(m_runtimeContext, "Attempted to fetch the memory offset of an immutable variable during runtime code generation.");
|
|
|
|
return m_immutableVariables.at(&_variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> CompilerContext::immutableVariableSlotNames(VariableDeclaration const& _variable)
|
|
|
|
{
|
2020-04-02 15:27:35 +00:00
|
|
|
string baseName = to_string(_variable.id());
|
2020-03-10 13:30:04 +00:00
|
|
|
solAssert(_variable.annotation().type->sizeOnStack() > 0, "");
|
|
|
|
if (_variable.annotation().type->sizeOnStack() == 1)
|
|
|
|
return {baseName};
|
|
|
|
vector<string> names;
|
2021-03-22 16:12:05 +00:00
|
|
|
auto collectSlotNames = [&](string const& _baseName, Type const* type, auto const& _recurse) -> void {
|
2020-03-10 13:30:04 +00:00
|
|
|
for (auto const& [slot, type]: type->stackItems())
|
|
|
|
if (type)
|
|
|
|
_recurse(_baseName + " " + slot, type, _recurse);
|
|
|
|
else
|
|
|
|
names.emplace_back(_baseName);
|
|
|
|
};
|
|
|
|
collectSlotNames(baseName, _variable.annotation().type, collectSlotNames);
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CompilerContext::reservedMemory()
|
|
|
|
{
|
|
|
|
solAssert(m_reservedMemory.has_value(), "Reserved memory was used before ");
|
|
|
|
size_t reservedMemory = *m_reservedMemory;
|
|
|
|
m_reservedMemory = std::nullopt;
|
|
|
|
return reservedMemory;
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:32:59 +00:00
|
|
|
void CompilerContext::startFunction(Declaration const& _function)
|
|
|
|
{
|
2016-05-02 23:13:41 +00:00
|
|
|
m_functionCompilationQueue.startFunction(_function);
|
2015-08-31 16:44:29 +00:00
|
|
|
*this << functionEntryLabel(_function);
|
2015-01-27 13:32:59 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 16:21:55 +00:00
|
|
|
void CompilerContext::callLowLevelFunction(
|
|
|
|
string const& _name,
|
|
|
|
unsigned _inArgs,
|
|
|
|
unsigned _outArgs,
|
|
|
|
function<void(CompilerContext&)> const& _generator
|
|
|
|
)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem retTag = pushNewTag();
|
2017-01-19 16:21:55 +00:00
|
|
|
CompilerUtils(*this).moveIntoStack(_inArgs);
|
|
|
|
|
2017-01-26 15:35:51 +00:00
|
|
|
*this << lowLevelFunctionTag(_name, _inArgs, _outArgs, _generator);
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
appendJump(evmasm::AssemblyItem::JumpType::IntoFunction);
|
2020-06-02 13:42:46 +00:00
|
|
|
adjustStackOffset(static_cast<int>(_outArgs) - 1 - static_cast<int>(_inArgs));
|
2017-01-26 15:35:51 +00:00
|
|
|
*this << retTag.tag();
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:23:58 +00:00
|
|
|
void CompilerContext::callYulFunction(
|
2020-03-02 15:32:30 +00:00
|
|
|
string const& _name,
|
|
|
|
unsigned _inArgs,
|
|
|
|
unsigned _outArgs
|
|
|
|
)
|
|
|
|
{
|
2020-03-02 16:23:58 +00:00
|
|
|
m_externallyUsedYulFunctions.insert(_name);
|
|
|
|
auto const retTag = pushNewTag();
|
2020-03-02 15:32:30 +00:00
|
|
|
CompilerUtils(*this).moveIntoStack(_inArgs);
|
2020-05-07 12:46:47 +00:00
|
|
|
appendJumpTo(namedTag(_name, _inArgs, _outArgs, {}), evmasm::AssemblyItem::JumpType::IntoFunction);
|
2020-06-02 13:42:46 +00:00
|
|
|
adjustStackOffset(static_cast<int>(_outArgs) - 1 - static_cast<int>(_inArgs));
|
2020-03-02 15:32:30 +00:00
|
|
|
*this << retTag.tag();
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem CompilerContext::lowLevelFunctionTag(
|
2017-01-26 15:35:51 +00:00
|
|
|
string const& _name,
|
|
|
|
unsigned _inArgs,
|
|
|
|
unsigned _outArgs,
|
|
|
|
function<void(CompilerContext&)> const& _generator
|
|
|
|
)
|
|
|
|
{
|
2017-01-19 16:21:55 +00:00
|
|
|
auto it = m_lowLevelFunctions.find(_name);
|
|
|
|
if (it == m_lowLevelFunctions.end())
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem tag = newTag().pushTag();
|
2017-01-19 16:21:55 +00:00
|
|
|
m_lowLevelFunctions.insert(make_pair(_name, tag));
|
|
|
|
m_lowLevelFunctionGenerationQueue.push(make_tuple(_name, _inArgs, _outArgs, _generator));
|
2017-01-26 15:35:51 +00:00
|
|
|
return tag;
|
2017-01-19 16:21:55 +00:00
|
|
|
}
|
|
|
|
else
|
2017-01-26 15:35:51 +00:00
|
|
|
return it->second;
|
2017-01-19 16:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerContext::appendMissingLowLevelFunctions()
|
|
|
|
{
|
|
|
|
while (!m_lowLevelFunctionGenerationQueue.empty())
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
unsigned inArgs;
|
|
|
|
unsigned outArgs;
|
|
|
|
function<void(CompilerContext&)> generator;
|
|
|
|
tie(name, inArgs, outArgs, generator) = m_lowLevelFunctionGenerationQueue.front();
|
|
|
|
m_lowLevelFunctionGenerationQueue.pop();
|
|
|
|
|
2020-06-02 13:42:46 +00:00
|
|
|
setStackOffset(static_cast<int>(inArgs) + 1);
|
2017-01-19 16:21:55 +00:00
|
|
|
*this << m_lowLevelFunctions.at(name).tag();
|
|
|
|
generator(*this);
|
|
|
|
CompilerUtils(*this).moveToStackTop(outArgs);
|
2019-12-11 16:31:36 +00:00
|
|
|
appendJump(evmasm::AssemblyItem::JumpType::OutOfFunction);
|
2017-01-19 16:21:55 +00:00
|
|
|
solAssert(stackHeight() == outArgs, "Invalid stack height in low-level function " + name + ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 11:17:16 +00:00
|
|
|
void CompilerContext::appendYulUtilityFunctions(OptimiserSettings const& _optimiserSettings)
|
2020-03-02 16:20:49 +00:00
|
|
|
{
|
2020-05-28 11:17:16 +00:00
|
|
|
solAssert(!m_appendYulUtilityFunctionsRan, "requestedYulFunctions called more than once.");
|
|
|
|
m_appendYulUtilityFunctionsRan = true;
|
2020-05-05 14:53:30 +00:00
|
|
|
|
2020-05-28 11:17:16 +00:00
|
|
|
string code = m_yulFunctionCollector.requestedFunctions();
|
|
|
|
if (!code.empty())
|
|
|
|
{
|
|
|
|
appendInlineAssembly(
|
2020-09-01 15:39:51 +00:00
|
|
|
yul::reindent("{\n" + move(code) + "\n}"),
|
2020-05-28 11:17:16 +00:00
|
|
|
{},
|
|
|
|
m_externallyUsedYulFunctions,
|
|
|
|
true,
|
|
|
|
_optimiserSettings,
|
|
|
|
yulUtilityFileName()
|
|
|
|
);
|
2020-09-01 15:39:51 +00:00
|
|
|
solAssert(!m_generatedYulUtilityCode.empty(), "");
|
2020-05-28 11:17:16 +00:00
|
|
|
}
|
2020-03-02 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
void CompilerContext::addVariable(
|
|
|
|
VariableDeclaration const& _declaration,
|
|
|
|
unsigned _offsetToCurrent
|
|
|
|
)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2016-11-11 10:41:50 +00:00
|
|
|
solAssert(m_asm->deposit() >= 0 && unsigned(m_asm->deposit()) >= _offsetToCurrent, "");
|
2018-07-06 07:56:27 +00:00
|
|
|
unsigned sizeOnStack = _declaration.annotation().type->sizeOnStack();
|
2018-07-10 16:39:26 +00:00
|
|
|
// Variables should not have stack size other than [1, 2],
|
|
|
|
// but that might change when new types are introduced.
|
2018-07-06 07:56:27 +00:00
|
|
|
solAssert(sizeOnStack == 1 || sizeOnStack == 2, "");
|
2017-07-27 09:52:42 +00:00
|
|
|
m_localVariables[&_declaration].push_back(unsigned(m_asm->deposit()) - _offsetToCurrent);
|
2014-12-08 15:56:41 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 14:40:33 +00:00
|
|
|
void CompilerContext::removeVariable(Declaration const& _declaration)
|
2015-02-27 16:41:22 +00:00
|
|
|
{
|
2017-07-27 09:52:42 +00:00
|
|
|
solAssert(m_localVariables.count(&_declaration) && !m_localVariables[&_declaration].empty(), "");
|
|
|
|
m_localVariables[&_declaration].pop_back();
|
|
|
|
if (m_localVariables[&_declaration].empty())
|
|
|
|
m_localVariables.erase(&_declaration);
|
2015-02-27 16:41:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 14:40:33 +00:00
|
|
|
void CompilerContext::removeVariablesAboveStackHeight(unsigned _stackHeight)
|
|
|
|
{
|
|
|
|
vector<Declaration const*> toRemove;
|
|
|
|
for (auto _var: m_localVariables)
|
|
|
|
{
|
|
|
|
solAssert(!_var.second.empty(), "");
|
2018-07-10 16:39:26 +00:00
|
|
|
solAssert(_var.second.back() <= stackHeight(), "");
|
2018-05-03 14:40:33 +00:00
|
|
|
if (_var.second.back() >= _stackHeight)
|
|
|
|
toRemove.push_back(_var.first);
|
|
|
|
}
|
|
|
|
for (auto _var: toRemove)
|
|
|
|
removeVariable(*_var);
|
|
|
|
}
|
|
|
|
|
2018-07-10 16:39:26 +00:00
|
|
|
unsigned CompilerContext::numberOfLocalVariables() const
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
return static_cast<unsigned>(m_localVariables.size());
|
2018-07-10 16:39:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
shared_ptr<evmasm::Assembly> CompilerContext::compiledContract(ContractDefinition const& _contract) const
|
2014-12-12 15:49:26 +00:00
|
|
|
{
|
2019-01-10 15:44:31 +00:00
|
|
|
auto ret = m_otherCompilers.find(&_contract);
|
|
|
|
solAssert(ret != m_otherCompilers.end(), "Compiled contract not found.");
|
2019-01-16 10:44:11 +00:00
|
|
|
return ret->second->assemblyPtr();
|
2019-01-10 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
shared_ptr<evmasm::Assembly> CompilerContext::compiledContractRuntime(ContractDefinition const& _contract) const
|
2019-01-10 15:44:31 +00:00
|
|
|
{
|
|
|
|
auto ret = m_otherCompilers.find(&_contract);
|
|
|
|
solAssert(ret != m_otherCompilers.end(), "Compiled contract not found.");
|
2019-01-16 10:44:11 +00:00
|
|
|
return ret->second->runtimeAssemblyPtr();
|
2014-12-12 15:49:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 01:06:37 +00:00
|
|
|
bool CompilerContext::isLocalVariable(Declaration const* _declaration) const
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-02-11 19:20:37 +00:00
|
|
|
return !!m_localVariables.count(_declaration);
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem CompilerContext::functionEntryLabel(Declaration const& _declaration)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2016-05-02 23:13:41 +00:00
|
|
|
return m_functionCompilationQueue.entryLabel(_declaration, *this);
|
2015-01-27 13:32:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem CompilerContext::functionEntryLabelIfExists(Declaration const& _declaration) const
|
2015-05-26 09:27:59 +00:00
|
|
|
{
|
2016-05-02 23:13:41 +00:00
|
|
|
return m_functionCompilationQueue.entryLabelIfExists(_declaration);
|
2015-05-26 09:27:59 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
FunctionDefinition const& CompilerContext::superFunction(FunctionDefinition const& _function, ContractDefinition const& _base)
|
2015-01-27 13:32:59 +00:00
|
|
|
{
|
2020-03-24 17:25:59 +00:00
|
|
|
solAssert(m_mostDerivedContract, "No most derived contract set.");
|
2020-04-06 15:30:01 +00:00
|
|
|
ContractDefinition const* super = _base.superContract(mostDerivedContract());
|
2020-03-24 17:25:59 +00:00
|
|
|
solAssert(super, "Super contract not available.");
|
2021-06-01 15:13:50 +00:00
|
|
|
|
|
|
|
FunctionDefinition const& resolvedFunction = _function.resolveVirtual(mostDerivedContract(), super);
|
|
|
|
solAssert(resolvedFunction.isImplemented(), "");
|
|
|
|
|
|
|
|
return resolvedFunction;
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:25:59 +00:00
|
|
|
ContractDefinition const& CompilerContext::mostDerivedContract() const
|
2015-01-15 19:04:24 +00:00
|
|
|
{
|
2020-03-24 17:25:59 +00:00
|
|
|
solAssert(m_mostDerivedContract, "Most derived contract not set.");
|
|
|
|
return *m_mostDerivedContract;
|
2015-01-15 19:04:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:25:59 +00:00
|
|
|
Declaration const* CompilerContext::nextFunctionToCompile() const
|
2015-01-23 01:46:31 +00:00
|
|
|
{
|
2020-03-24 17:25:59 +00:00
|
|
|
return m_functionCompilationQueue.nextFunctionToCompile();
|
2015-01-23 01:46:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
unsigned CompilerContext::baseStackOffsetOfVariable(Declaration const& _declaration) const
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
2014-12-08 15:56:41 +00:00
|
|
|
auto res = m_localVariables.find(&_declaration);
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(res != m_localVariables.end(), "Variable not found on stack.");
|
2017-07-27 09:52:42 +00:00
|
|
|
solAssert(!res->second.empty(), "");
|
|
|
|
return res->second.back();
|
2014-11-07 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned CompilerContext::baseToCurrentStackOffset(unsigned _baseOffset) const
|
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
return static_cast<unsigned>(m_asm->deposit()) - _baseOffset - 1;
|
2014-11-07 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 10:57:22 +00:00
|
|
|
unsigned CompilerContext::currentToBaseStackOffset(unsigned _offset) const
|
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
return static_cast<unsigned>(m_asm->deposit()) - _offset - 1;
|
2015-01-14 10:57:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 15:22:42 +00:00
|
|
|
pair<u256, unsigned> CompilerContext::storageLocationOfVariable(Declaration const& _declaration) const
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
|
|
|
auto it = m_stateVariables.find(&_declaration);
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(it != m_stateVariables.end(), "Variable not found in storage.");
|
2014-11-07 01:06:37 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
CompilerContext& CompilerContext::appendJump(evmasm::AssemblyItem::JumpType _jumpType)
|
2015-03-09 18:22:24 +00:00
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem item(Instruction::JUMP);
|
2015-03-09 18:22:24 +00:00
|
|
|
item.setJumpType(_jumpType);
|
|
|
|
return *this << item;
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:01:45 +00:00
|
|
|
CompilerContext& CompilerContext::appendPanic(util::PanicCode _code)
|
2017-01-22 19:49:12 +00:00
|
|
|
{
|
2021-05-24 10:06:13 +00:00
|
|
|
callYulFunction(utilFunctions().panicFunction(_code), 0, 0);
|
2020-10-12 14:01:45 +00:00
|
|
|
return *this;
|
2017-01-22 19:49:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:01:45 +00:00
|
|
|
CompilerContext& CompilerContext::appendConditionalPanic(util::PanicCode _code)
|
2017-01-22 19:49:12 +00:00
|
|
|
{
|
2017-01-23 09:46:50 +00:00
|
|
|
*this << Instruction::ISZERO;
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem afterTag = appendConditionalJump();
|
2020-10-12 14:01:45 +00:00
|
|
|
appendPanic(_code);
|
2017-01-26 14:59:48 +00:00
|
|
|
*this << afterTag;
|
|
|
|
return *this;
|
2017-01-22 19:49:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
CompilerContext& CompilerContext::appendRevert(string const& _message)
|
2017-05-24 09:47:43 +00:00
|
|
|
{
|
2020-01-22 14:48:56 +00:00
|
|
|
appendInlineAssembly("{ " + revertReasonIfDebug(_message) + " }");
|
|
|
|
return *this;
|
2017-05-24 09:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
CompilerContext& CompilerContext::appendConditionalRevert(bool _forwardReturnData, string const& _message)
|
2017-05-24 09:47:43 +00:00
|
|
|
{
|
2018-03-02 16:26:16 +00:00
|
|
|
if (_forwardReturnData && m_evmVersion.supportsReturndata())
|
2017-12-30 19:13:41 +00:00
|
|
|
appendInlineAssembly(R"({
|
|
|
|
if condition {
|
|
|
|
returndatacopy(0, 0, returndatasize())
|
|
|
|
revert(0, returndatasize())
|
|
|
|
}
|
|
|
|
})", {"condition"});
|
|
|
|
else
|
2020-01-22 14:48:56 +00:00
|
|
|
appendInlineAssembly("{ if condition { " + revertReasonIfDebug(_message) + " } }", {"condition"});
|
2017-12-30 19:13:41 +00:00
|
|
|
*this << Instruction::POP;
|
2017-05-24 09:47:43 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-24 11:08:51 +00:00
|
|
|
void CompilerContext::resetVisitedNodes(ASTNode const* _node)
|
|
|
|
{
|
|
|
|
stack<ASTNode const*> newStack;
|
|
|
|
newStack.push(_node);
|
|
|
|
std::swap(m_visitedNodes, newStack);
|
2015-03-07 22:08:39 +00:00
|
|
|
updateSourceLocation();
|
2015-02-23 15:31:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 14:27:20 +00:00
|
|
|
void CompilerContext::appendInlineAssembly(
|
|
|
|
string const& _assembly,
|
|
|
|
vector<string> const& _localVariables,
|
2019-02-21 17:23:46 +00:00
|
|
|
set<string> const& _externallyUsedFunctions,
|
|
|
|
bool _system,
|
2020-05-28 11:17:16 +00:00
|
|
|
OptimiserSettings const& _optimiserSettings,
|
|
|
|
string _sourceName
|
2016-08-16 14:27:20 +00:00
|
|
|
)
|
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
unsigned startStackHeight = stackHeight();
|
2017-03-14 14:41:23 +00:00
|
|
|
|
2019-02-21 17:23:46 +00:00
|
|
|
set<yul::YulString> externallyUsedIdentifiers;
|
|
|
|
for (auto const& fun: _externallyUsedFunctions)
|
|
|
|
externallyUsedIdentifiers.insert(yul::YulString(fun));
|
|
|
|
for (auto const& var: _localVariables)
|
|
|
|
externallyUsedIdentifiers.insert(yul::YulString(var));
|
|
|
|
|
2018-10-15 09:58:51 +00:00
|
|
|
yul::ExternalIdentifierAccess identifierAccess;
|
2017-03-14 14:41:23 +00:00
|
|
|
identifierAccess.resolve = [&](
|
2018-11-21 11:42:34 +00:00
|
|
|
yul::Identifier const& _identifier,
|
2018-10-15 09:58:51 +00:00
|
|
|
yul::IdentifierContext,
|
2019-01-28 13:41:33 +00:00
|
|
|
bool _insideFunction
|
2020-06-11 16:46:31 +00:00
|
|
|
) -> bool
|
2017-04-11 17:24:38 +00:00
|
|
|
{
|
2019-01-28 13:41:33 +00:00
|
|
|
if (_insideFunction)
|
2020-06-11 16:46:31 +00:00
|
|
|
return false;
|
2022-03-07 04:25:35 +00:00
|
|
|
return util::contains(_localVariables, _identifier.name.str());
|
2017-03-14 14:41:23 +00:00
|
|
|
};
|
|
|
|
identifierAccess.generateCode = [&](
|
2018-11-21 11:42:34 +00:00
|
|
|
yul::Identifier const& _identifier,
|
2018-10-15 09:58:51 +00:00
|
|
|
yul::IdentifierContext _context,
|
|
|
|
yul::AbstractAssembly& _assembly
|
2017-04-11 17:24:38 +00:00
|
|
|
)
|
|
|
|
{
|
2021-08-04 16:11:00 +00:00
|
|
|
solAssert(_context == yul::IdentifierContext::RValue || _context == yul::IdentifierContext::LValue, "");
|
2018-10-29 14:12:02 +00:00
|
|
|
auto it = std::find(_localVariables.begin(), _localVariables.end(), _identifier.name.str());
|
2017-03-14 14:41:23 +00:00
|
|
|
solAssert(it != _localVariables.end(), "");
|
2020-06-02 13:42:46 +00:00
|
|
|
auto stackDepth = static_cast<size_t>(distance(it, _localVariables.end()));
|
|
|
|
size_t stackDiff = static_cast<size_t>(_assembly.stackHeight()) - startStackHeight + stackDepth;
|
2018-10-15 09:58:51 +00:00
|
|
|
if (_context == yul::IdentifierContext::LValue)
|
2016-12-11 16:50:59 +00:00
|
|
|
stackDiff -= 1;
|
2016-08-16 14:27:20 +00:00
|
|
|
if (stackDiff < 1 || stackDiff > 16)
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2020-07-01 07:46:06 +00:00
|
|
|
StackTooDeepError() <<
|
2021-09-20 15:51:01 +00:00
|
|
|
errinfo_sourceLocation(nativeLocationOf(_identifier)) <<
|
2019-12-11 16:31:36 +00:00
|
|
|
util::errinfo_comment("Stack too deep (" + to_string(stackDiff) + "), try removing local variables.")
|
2016-08-16 14:27:20 +00:00
|
|
|
);
|
2018-10-15 09:58:51 +00:00
|
|
|
if (_context == yul::IdentifierContext::RValue)
|
2019-12-12 23:39:29 +00:00
|
|
|
_assembly.appendInstruction(dupInstruction(static_cast<unsigned>(stackDiff)));
|
2016-08-16 14:27:20 +00:00
|
|
|
else
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
_assembly.appendInstruction(swapInstruction(static_cast<unsigned>(stackDiff)));
|
2017-04-28 16:15:18 +00:00
|
|
|
_assembly.appendInstruction(Instruction::POP);
|
2016-08-16 14:27:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-09 10:36:36 +00:00
|
|
|
ErrorList errors;
|
|
|
|
ErrorReporter errorReporter(errors);
|
2021-07-14 10:53:39 +00:00
|
|
|
langutil::CharStream charStream(_assembly, _sourceName);
|
2019-05-15 08:33:35 +00:00
|
|
|
yul::EVMDialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(m_evmVersion);
|
2020-05-26 15:24:52 +00:00
|
|
|
optional<langutil::SourceLocation> locationOverride;
|
|
|
|
if (!_system)
|
|
|
|
locationOverride = m_asm->currentSourceLocation();
|
|
|
|
shared_ptr<yul::Block> parserResult =
|
|
|
|
yul::Parser(errorReporter, dialect, std::move(locationOverride))
|
2021-08-03 13:36:00 +00:00
|
|
|
.parse(charStream);
|
2017-09-25 15:00:16 +00:00
|
|
|
#ifdef SOL_OUTPUT_ASM
|
2020-01-16 11:03:19 +00:00
|
|
|
cout << yul::AsmPrinter(&dialect)(*parserResult) << endl;
|
2017-09-25 15:00:16 +00:00
|
|
|
#endif
|
2019-02-21 17:23:46 +00:00
|
|
|
|
|
|
|
auto reportError = [&](string const& _context)
|
2017-09-18 12:47:46 +00:00
|
|
|
{
|
|
|
|
string message =
|
2019-02-21 17:23:46 +00:00
|
|
|
"Error parsing/analyzing inline assembly block:\n" +
|
|
|
|
_context + "\n"
|
2017-09-18 12:47:46 +00:00
|
|
|
"------------------ Input: -----------------\n" +
|
|
|
|
_assembly + "\n"
|
|
|
|
"------------------ Errors: ----------------\n";
|
|
|
|
for (auto const& error: errorReporter.errors())
|
2021-06-29 12:38:59 +00:00
|
|
|
// TODO if we have "locationOverride", it will be the wrong char stream,
|
|
|
|
// but we do not have access to the solidity scanner.
|
2021-07-14 10:53:39 +00:00
|
|
|
message += SourceReferenceFormatter::formatErrorInformation(*error, charStream);
|
2017-09-18 12:47:46 +00:00
|
|
|
message += "-------------------------------------------\n";
|
|
|
|
|
|
|
|
solAssert(false, message);
|
2019-02-21 17:23:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
yul::AsmAnalysisInfo analysisInfo;
|
|
|
|
bool analyzerResult = false;
|
|
|
|
if (parserResult)
|
|
|
|
analyzerResult = yul::AsmAnalyzer(
|
|
|
|
analysisInfo,
|
|
|
|
errorReporter,
|
2019-05-15 08:33:35 +00:00
|
|
|
dialect,
|
2019-02-21 17:23:46 +00:00
|
|
|
identifierAccess.resolve
|
|
|
|
).analyze(*parserResult);
|
|
|
|
if (!parserResult || !errorReporter.errors().empty() || !analyzerResult)
|
|
|
|
reportError("Invalid assembly generated by code generator.");
|
|
|
|
|
|
|
|
// Several optimizer steps cannot handle externally supplied stack variables,
|
|
|
|
// so we essentially only optimize the ABI functions.
|
2019-02-26 18:55:13 +00:00
|
|
|
if (_optimiserSettings.runYulOptimiser && _localVariables.empty())
|
2019-02-21 17:23:46 +00:00
|
|
|
{
|
2019-07-09 15:23:14 +00:00
|
|
|
yul::Object obj;
|
|
|
|
obj.code = parserResult;
|
|
|
|
obj.analysisInfo = make_shared<yul::AsmAnalysisInfo>(analysisInfo);
|
2020-02-06 15:32:33 +00:00
|
|
|
|
|
|
|
optimizeYul(obj, dialect, _optimiserSettings, externallyUsedIdentifiers);
|
|
|
|
|
2020-09-01 15:39:51 +00:00
|
|
|
if (_system)
|
|
|
|
{
|
|
|
|
// Store as generated sources, but first re-parse to update the source references.
|
|
|
|
solAssert(m_generatedYulUtilityCode.empty(), "");
|
|
|
|
m_generatedYulUtilityCode = yul::AsmPrinter(dialect)(*obj.code);
|
|
|
|
string code = yul::AsmPrinter{dialect}(*obj.code);
|
2021-07-14 10:53:39 +00:00
|
|
|
langutil::CharStream charStream(m_generatedYulUtilityCode, _sourceName);
|
2021-08-03 13:36:00 +00:00
|
|
|
obj.code = yul::Parser(errorReporter, dialect).parse(charStream);
|
2020-09-01 15:39:51 +00:00
|
|
|
*obj.analysisInfo = yul::AsmAnalyzer::analyzeStrictAssertCorrect(dialect, obj);
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:23:14 +00:00
|
|
|
analysisInfo = std::move(*obj.analysisInfo);
|
|
|
|
parserResult = std::move(obj.code);
|
|
|
|
|
2019-03-19 16:22:56 +00:00
|
|
|
#ifdef SOL_OUTPUT_ASM
|
2019-11-21 19:08:50 +00:00
|
|
|
cout << "After optimizer:" << endl;
|
2020-01-16 11:03:19 +00:00
|
|
|
cout << yul::AsmPrinter(&dialect)(*parserResult) << endl;
|
2019-03-19 16:22:56 +00:00
|
|
|
#endif
|
2017-09-18 12:47:46 +00:00
|
|
|
}
|
2020-09-01 15:39:51 +00:00
|
|
|
else if (_system)
|
|
|
|
{
|
|
|
|
// Store as generated source.
|
|
|
|
solAssert(m_generatedYulUtilityCode.empty(), "");
|
|
|
|
m_generatedYulUtilityCode = _assembly;
|
|
|
|
}
|
2017-06-09 10:36:36 +00:00
|
|
|
|
2019-02-21 17:23:46 +00:00
|
|
|
if (!errorReporter.errors().empty())
|
|
|
|
reportError("Failed to analyze inline assembly block.");
|
|
|
|
|
2017-06-09 10:36:36 +00:00
|
|
|
solAssert(errorReporter.errors().empty(), "Failed to analyze inline assembly block.");
|
2019-02-26 18:55:13 +00:00
|
|
|
yul::CodeGenerator::assemble(
|
|
|
|
*parserResult,
|
|
|
|
analysisInfo,
|
|
|
|
*m_asm,
|
|
|
|
m_evmVersion,
|
2021-08-04 16:38:10 +00:00
|
|
|
identifierAccess.generateCode,
|
2019-02-26 18:55:13 +00:00
|
|
|
_system,
|
|
|
|
_optimiserSettings.optimizeStackAllocation
|
|
|
|
);
|
2018-01-04 13:24:19 +00:00
|
|
|
|
|
|
|
// Reset the source location to the one of the node (instead of the CODEGEN source location)
|
|
|
|
updateSourceLocation();
|
2016-08-16 14:27:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 15:32:33 +00:00
|
|
|
|
|
|
|
void CompilerContext::optimizeYul(yul::Object& _object, yul::EVMDialect const& _dialect, OptimiserSettings const& _optimiserSettings, std::set<yul::YulString> const& _externalIdentifiers)
|
|
|
|
{
|
|
|
|
#ifdef SOL_OUTPUT_ASM
|
|
|
|
cout << yul::AsmPrinter(*dialect)(*_object.code) << endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool const isCreation = runtimeContext() != nullptr;
|
|
|
|
yul::GasMeter meter(_dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment);
|
|
|
|
yul::OptimiserSuite::run(
|
|
|
|
_dialect,
|
|
|
|
&meter,
|
|
|
|
_object,
|
|
|
|
_optimiserSettings.optimizeStackAllocation,
|
2020-04-24 12:19:36 +00:00
|
|
|
_optimiserSettings.yulOptimiserSteps,
|
2021-03-11 11:42:59 +00:00
|
|
|
isCreation? nullopt : make_optional(_optimiserSettings.expectedExecutionsPerDeployment),
|
2020-02-06 15:32:33 +00:00
|
|
|
_externalIdentifiers
|
|
|
|
);
|
|
|
|
|
|
|
|
#ifdef SOL_OUTPUT_ASM
|
|
|
|
cout << "After optimizer:" << endl;
|
|
|
|
cout << yul::AsmPrinter(*dialect)(*object.code) << endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
string CompilerContext::revertReasonIfDebug(string const& _message)
|
|
|
|
{
|
2021-03-15 15:45:00 +00:00
|
|
|
return YulUtilFunctions::revertReasonIfDebugBody(
|
|
|
|
m_revertStrings,
|
|
|
|
"mload(" + to_string(CompilerUtils::freeMemoryPointer) + ")",
|
|
|
|
_message
|
|
|
|
);
|
2020-01-22 14:48:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 22:08:39 +00:00
|
|
|
void CompilerContext::updateSourceLocation()
|
|
|
|
{
|
2016-11-11 10:41:50 +00:00
|
|
|
m_asm->setSourceLocation(m_visitedNodes.empty() ? SourceLocation() : m_visitedNodes.top()->location());
|
2015-03-07 22:08:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::Assembly::OptimiserSettings CompilerContext::translateOptimiserSettings(OptimiserSettings const& _settings)
|
2017-07-17 11:12:00 +00:00
|
|
|
{
|
|
|
|
// Constructing it this way so that we notice changes in the fields.
|
2022-03-07 16:39:52 +00:00
|
|
|
evmasm::Assembly::OptimiserSettings asmSettings{false, false, false, false, false, false, m_evmVersion, 0};
|
2021-01-14 12:02:14 +00:00
|
|
|
asmSettings.runInliner = _settings.runInliner;
|
2017-07-17 11:12:00 +00:00
|
|
|
asmSettings.runJumpdestRemover = _settings.runJumpdestRemover;
|
|
|
|
asmSettings.runPeephole = _settings.runPeephole;
|
|
|
|
asmSettings.runDeduplicate = _settings.runDeduplicate;
|
|
|
|
asmSettings.runCSE = _settings.runCSE;
|
|
|
|
asmSettings.runConstantOptimiser = _settings.runConstantOptimiser;
|
|
|
|
asmSettings.expectedExecutionsPerDeployment = _settings.expectedExecutionsPerDeployment;
|
|
|
|
asmSettings.evmVersion = m_evmVersion;
|
|
|
|
return asmSettings;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem CompilerContext::FunctionCompilationQueue::entryLabel(
|
2016-05-02 23:13:41 +00:00
|
|
|
Declaration const& _declaration,
|
|
|
|
CompilerContext& _context
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto res = m_entryLabels.find(&_declaration);
|
|
|
|
if (res == m_entryLabels.end())
|
|
|
|
{
|
2020-05-07 12:46:47 +00:00
|
|
|
size_t params = 0;
|
|
|
|
size_t returns = 0;
|
|
|
|
if (auto const* function = dynamic_cast<FunctionDefinition const*>(&_declaration))
|
|
|
|
{
|
|
|
|
FunctionType functionType(*function, FunctionType::Kind::Internal);
|
|
|
|
params = CompilerUtils::sizeOnStack(functionType.parameterTypes());
|
|
|
|
returns = CompilerUtils::sizeOnStack(functionType.returnParameterTypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
// some name that cannot clash with yul function names.
|
|
|
|
string labelName = "@" + _declaration.name() + "_" + to_string(_declaration.id());
|
|
|
|
evmasm::AssemblyItem tag = _context.namedTag(
|
|
|
|
labelName,
|
|
|
|
params,
|
|
|
|
returns,
|
|
|
|
_declaration.id()
|
|
|
|
);
|
2016-05-02 23:13:41 +00:00
|
|
|
m_entryLabels.insert(make_pair(&_declaration, tag));
|
|
|
|
m_functionsToCompile.push(&_declaration);
|
|
|
|
return tag.tag();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return res->second.tag();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::AssemblyItem CompilerContext::FunctionCompilationQueue::entryLabelIfExists(Declaration const& _declaration) const
|
2016-05-02 23:13:41 +00:00
|
|
|
{
|
|
|
|
auto res = m_entryLabels.find(&_declaration);
|
2019-12-11 16:31:36 +00:00
|
|
|
return res == m_entryLabels.end() ? evmasm::AssemblyItem(evmasm::UndefinedItem) : res->second.tag();
|
2016-05-02 23:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Declaration const* CompilerContext::FunctionCompilationQueue::nextFunctionToCompile() const
|
|
|
|
{
|
|
|
|
while (!m_functionsToCompile.empty())
|
|
|
|
{
|
|
|
|
if (m_alreadyCompiledFunctions.count(m_functionsToCompile.front()))
|
|
|
|
m_functionsToCompile.pop();
|
|
|
|
else
|
|
|
|
return m_functionsToCompile.front();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerContext::FunctionCompilationQueue::startFunction(Declaration const& _function)
|
|
|
|
{
|
|
|
|
if (!m_functionsToCompile.empty() && m_functionsToCompile.front() == &_function)
|
|
|
|
m_functionsToCompile.pop();
|
|
|
|
m_alreadyCompiledFunctions.insert(&_function);
|
|
|
|
}
|