2017-05-23 17:11:14 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-05-23 17:11:14 +00:00
|
|
|
/**
|
2021-04-12 10:33:28 +00:00
|
|
|
* Code generator for translating Yul / inline assembly to EVM.
|
2017-05-23 17:11:14 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/backends/evm/EVMCodeTransform.h>
|
2017-05-23 17:21:14 +00:00
|
|
|
|
2018-09-25 02:47:25 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2019-03-05 16:26:28 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2017-05-23 17:21:14 +00:00
|
|
|
|
2021-04-12 10:33:28 +00:00
|
|
|
#include <libsolutil/Visitor.h>
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2017-05-23 17:21:14 +00:00
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/reverse.hpp>
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
#include <range/v3/algorithm/max.hpp>
|
|
|
|
#include <range/v3/algorithm/none_of.hpp>
|
|
|
|
#include <range/v3/view/enumerate.hpp>
|
|
|
|
#include <range/v3/view/transform.hpp>
|
|
|
|
|
2020-04-01 03:04:29 +00:00
|
|
|
#include <utility>
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
|
|
|
|
2017-05-23 17:21:14 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
2017-11-30 18:12:39 +00:00
|
|
|
|
2018-09-25 02:47:25 +00:00
|
|
|
CodeTransform::CodeTransform(
|
|
|
|
AbstractAssembly& _assembly,
|
|
|
|
AsmAnalysisInfo& _analysisInfo,
|
|
|
|
Block const& _block,
|
|
|
|
bool _allowStackOpt,
|
2018-12-06 23:56:16 +00:00
|
|
|
EVMDialect const& _dialect,
|
2019-05-15 21:40:30 +00:00
|
|
|
BuiltinContext& _builtinContext,
|
2020-04-01 03:04:29 +00:00
|
|
|
ExternalIdentifierAccess _identifierAccess,
|
2018-09-25 02:47:25 +00:00
|
|
|
bool _useNamedLabelsForFunctions,
|
2021-02-04 20:52:07 +00:00
|
|
|
shared_ptr<Context> _context,
|
|
|
|
vector<TypedName> _delayedReturnVariables,
|
|
|
|
optional<AbstractAssembly::LabelID> _functionExitLabel
|
2018-09-25 02:47:25 +00:00
|
|
|
):
|
|
|
|
m_assembly(_assembly),
|
|
|
|
m_info(_analysisInfo),
|
2018-12-06 23:56:16 +00:00
|
|
|
m_dialect(_dialect),
|
2019-05-15 21:40:30 +00:00
|
|
|
m_builtinContext(_builtinContext),
|
2018-09-25 02:47:25 +00:00
|
|
|
m_allowStackOpt(_allowStackOpt),
|
|
|
|
m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions),
|
2021-02-04 20:52:07 +00:00
|
|
|
m_identifierAccess(move(_identifierAccess)),
|
|
|
|
m_context(move(_context)),
|
|
|
|
m_delayedReturnVariables(move(_delayedReturnVariables)),
|
|
|
|
m_functionExitLabel(_functionExitLabel)
|
2018-09-25 02:47:25 +00:00
|
|
|
{
|
|
|
|
if (!m_context)
|
|
|
|
{
|
|
|
|
// initialize
|
|
|
|
m_context = make_shared<Context>();
|
|
|
|
if (m_allowStackOpt)
|
2021-04-12 10:33:28 +00:00
|
|
|
m_context->variableReferences = VariableReferenceCounter::run(m_info, _block);
|
2018-09-25 02:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTransform::decreaseReference(YulString, Scope::Variable const& _var)
|
|
|
|
{
|
|
|
|
if (!m_allowStackOpt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned& ref = m_context->variableReferences.at(&_var);
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(ref >= 1, "");
|
2018-09-25 02:47:25 +00:00
|
|
|
--ref;
|
|
|
|
if (ref == 0)
|
|
|
|
m_variablesScheduledForDeletion.insert(&_var);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeTransform::unreferenced(Scope::Variable const& _var) const
|
|
|
|
{
|
|
|
|
return !m_context->variableReferences.count(&_var) || m_context->variableReferences[&_var] == 0;
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:39:23 +00:00
|
|
|
void CodeTransform::freeUnusedVariables(bool _popUnusedSlotsAtStackTop)
|
2018-09-25 02:47:25 +00:00
|
|
|
{
|
|
|
|
if (!m_allowStackOpt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto const& identifier: m_scope->identifiers)
|
2021-02-04 20:52:07 +00:00
|
|
|
if (Scope::Variable const* var = get_if<Scope::Variable>(&identifier.second))
|
|
|
|
if (m_variablesScheduledForDeletion.count(var))
|
|
|
|
deleteVariable(*var);
|
|
|
|
// Directly in a function body block, we can also delete the function arguments,
|
|
|
|
// which live in the virtual function scope.
|
2021-03-11 16:42:19 +00:00
|
|
|
// However, doing so after the return variables are already allocated, seems to have an adverse
|
|
|
|
// effect, so we only do it before that.
|
|
|
|
if (!returnVariablesAndFunctionExitAreSetup() && !m_scope->functionScope && m_scope->superScope && m_scope->superScope->functionScope)
|
2021-02-04 20:52:07 +00:00
|
|
|
for (auto const& identifier: m_scope->superScope->identifiers)
|
|
|
|
if (Scope::Variable const* var = get_if<Scope::Variable>(&identifier.second))
|
|
|
|
if (m_variablesScheduledForDeletion.count(var))
|
|
|
|
deleteVariable(*var);
|
2018-09-25 02:47:25 +00:00
|
|
|
|
2020-07-10 15:39:23 +00:00
|
|
|
if (_popUnusedSlotsAtStackTop)
|
|
|
|
while (m_unusedStackSlots.count(m_assembly.stackHeight() - 1))
|
|
|
|
{
|
|
|
|
yulAssert(m_unusedStackSlots.erase(m_assembly.stackHeight() - 1), "");
|
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
|
|
|
}
|
2018-09-25 02:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTransform::deleteVariable(Scope::Variable const& _var)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_allowStackOpt, "");
|
|
|
|
yulAssert(m_context->variableStackHeights.count(&_var) > 0, "");
|
2020-06-15 10:51:58 +00:00
|
|
|
m_unusedStackSlots.insert(static_cast<int>(m_context->variableStackHeights[&_var]));
|
2018-09-25 02:47:25 +00:00
|
|
|
m_context->variableStackHeights.erase(&_var);
|
|
|
|
m_context->variableReferences.erase(&_var);
|
|
|
|
m_variablesScheduledForDeletion.erase(&_var);
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:21:14 +00:00
|
|
|
void CodeTransform::operator()(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_scope, "");
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t const numVariables = _varDecl.variables.size();
|
|
|
|
auto heightAtStart = static_cast<size_t>(m_assembly.stackHeight());
|
2017-07-02 10:21:34 +00:00
|
|
|
if (_varDecl.value)
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
std::visit(*this, *_varDecl.value);
|
2020-06-02 13:34:28 +00:00
|
|
|
expectDeposit(static_cast<int>(numVariables), static_cast<int>(heightAtStart));
|
2020-07-10 15:39:23 +00:00
|
|
|
freeUnusedVariables(false);
|
2017-07-02 10:21:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-04 23:03:41 +00:00
|
|
|
m_assembly.setSourceLocation(_varDecl.location);
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t variablesLeft = numVariables;
|
2017-07-02 10:21:34 +00:00
|
|
|
while (variablesLeft--)
|
|
|
|
m_assembly.appendConstant(u256(0));
|
|
|
|
}
|
2018-09-25 02:47:25 +00:00
|
|
|
|
2020-05-04 23:03:41 +00:00
|
|
|
m_assembly.setSourceLocation(_varDecl.location);
|
2018-09-25 02:47:25 +00:00
|
|
|
bool atTopOfStack = true;
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t varIndex = 0; varIndex < numVariables; ++varIndex)
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t varIndexReverse = numVariables - 1 - varIndex;
|
|
|
|
YulString varName = _varDecl.variables[varIndexReverse].name;
|
2019-11-19 15:42:49 +00:00
|
|
|
auto& var = std::get<Scope::Variable>(m_scope->identifiers.at(varName));
|
2020-06-02 13:34:28 +00:00
|
|
|
m_context->variableStackHeights[&var] = heightAtStart + varIndexReverse;
|
2018-09-25 02:47:25 +00:00
|
|
|
if (!m_allowStackOpt)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (unreferenced(var))
|
|
|
|
{
|
|
|
|
if (atTopOfStack)
|
|
|
|
{
|
|
|
|
m_context->variableStackHeights.erase(&var);
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2018-09-25 02:47:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_variablesScheduledForDeletion.insert(&var);
|
|
|
|
}
|
|
|
|
else if (m_unusedStackSlots.empty())
|
|
|
|
atTopOfStack = false;
|
|
|
|
else
|
|
|
|
{
|
2020-06-02 13:34:28 +00:00
|
|
|
auto slot = static_cast<size_t>(*m_unusedStackSlots.begin());
|
2018-09-25 02:47:25 +00:00
|
|
|
m_unusedStackSlots.erase(m_unusedStackSlots.begin());
|
|
|
|
m_context->variableStackHeights[&var] = slot;
|
2020-06-02 13:34:28 +00:00
|
|
|
if (size_t heightDiff = variableHeightDiff(var, varName, true))
|
2019-12-12 23:39:29 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::swapInstruction(static_cast<unsigned>(heightDiff - 1)));
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2018-09-25 02:47:25 +00:00
|
|
|
}
|
2017-05-23 17:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 09:51:25 +00:00
|
|
|
void CodeTransform::stackError(StackTooDeepError _error, int _targetStackHeight)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::INVALID);
|
2019-01-29 09:51:25 +00:00
|
|
|
// Correct the stack.
|
|
|
|
while (m_assembly.stackHeight() > _targetStackHeight)
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2019-01-29 09:51:25 +00:00
|
|
|
while (m_assembly.stackHeight() < _targetStackHeight)
|
|
|
|
m_assembly.appendConstant(u256(0));
|
|
|
|
// Store error.
|
|
|
|
m_stackErrors.emplace_back(std::move(_error));
|
2020-07-15 09:32:34 +00:00
|
|
|
m_assembly.markAsInvalid();
|
2019-01-29 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 17:21:14 +00:00
|
|
|
void CodeTransform::operator()(Assignment const& _assignment)
|
|
|
|
{
|
2017-05-19 16:06:26 +00:00
|
|
|
int height = m_assembly.stackHeight();
|
2019-11-19 15:42:49 +00:00
|
|
|
std::visit(*this, *_assignment.value);
|
2020-06-02 13:34:28 +00:00
|
|
|
expectDeposit(static_cast<int>(_assignment.variableNames.size()), height);
|
2017-05-19 16:06:26 +00:00
|
|
|
|
2017-05-23 17:21:14 +00:00
|
|
|
m_assembly.setSourceLocation(_assignment.location);
|
2017-09-20 09:16:07 +00:00
|
|
|
generateMultiAssignment(_assignment.variableNames);
|
2017-05-23 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 13:01:22 +00:00
|
|
|
void CodeTransform::operator()(ExpressionStatement const& _statement)
|
|
|
|
{
|
|
|
|
m_assembly.setSourceLocation(_statement.location);
|
2019-11-19 15:42:49 +00:00
|
|
|
std::visit(*this, _statement.expression);
|
2017-12-08 13:01:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
void CodeTransform::operator()(FunctionCall const& _call)
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_scope, "");
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2018-12-06 23:56:16 +00:00
|
|
|
if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name))
|
2020-04-14 08:52:27 +00:00
|
|
|
builtin->generateCode(_call, m_assembly, m_builtinContext, [&](Expression const& _expression) {
|
|
|
|
visitExpression(_expression);
|
2018-12-06 23:56:16 +00:00
|
|
|
});
|
2017-05-24 16:34:19 +00:00
|
|
|
else
|
|
|
|
{
|
2018-12-06 23:56:16 +00:00
|
|
|
m_assembly.setSourceLocation(_call.location);
|
2020-06-02 13:34:28 +00:00
|
|
|
EVMAssembly::LabelID returnLabel(numeric_limits<EVMAssembly::LabelID>::max()); // only used for evm 1.0
|
2021-03-09 11:46:33 +00:00
|
|
|
|
|
|
|
returnLabel = m_assembly.newLabelId();
|
|
|
|
m_assembly.appendLabelReference(returnLabel);
|
2018-12-06 23:56:16 +00:00
|
|
|
|
|
|
|
Scope::Function* function = nullptr;
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_scope->lookup(_call.functionName.name, GenericVisitor{
|
2020-05-11 14:53:45 +00:00
|
|
|
[](Scope::Variable&) { yulAssert(false, "Expected function name."); },
|
2018-12-06 23:56:16 +00:00
|
|
|
[&](Scope::Function& _function) { function = &_function; }
|
2019-11-24 17:46:43 +00:00
|
|
|
}), "Function name not found.");
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(function, "");
|
|
|
|
yulAssert(function->arguments.size() == _call.arguments.size(), "");
|
2021-03-31 18:03:04 +00:00
|
|
|
for (auto const& arg: _call.arguments | ranges::views::reverse)
|
2018-12-06 23:56:16 +00:00
|
|
|
visitExpression(arg);
|
|
|
|
m_assembly.setSourceLocation(_call.location);
|
2021-03-09 11:46:33 +00:00
|
|
|
m_assembly.appendJumpTo(
|
|
|
|
functionEntryID(_call.functionName.name, *function),
|
|
|
|
static_cast<int>(function->returns.size() - function->arguments.size()) - 1,
|
|
|
|
AbstractAssembly::JumpType::IntoFunction
|
|
|
|
);
|
|
|
|
m_assembly.appendLabel(returnLabel);
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
2017-05-23 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
void CodeTransform::operator()(Identifier const& _identifier)
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
|
|
|
m_assembly.setSourceLocation(_identifier.location);
|
|
|
|
// First search internals, then externals.
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_scope, "");
|
2019-11-24 17:46:43 +00:00
|
|
|
if (m_scope->lookup(_identifier.name, GenericVisitor{
|
2020-05-11 14:53:45 +00:00
|
|
|
[&](Scope::Variable& _var)
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
2018-09-25 02:47:25 +00:00
|
|
|
// TODO: opportunity for optimization: Do not DUP if this is the last reference
|
|
|
|
// to the top most element of the stack
|
2020-06-02 13:34:28 +00:00
|
|
|
if (size_t heightDiff = variableHeightDiff(_var, _identifier.name, false))
|
2019-12-12 23:39:29 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::dupInstruction(static_cast<unsigned>(heightDiff)));
|
2017-05-23 17:21:14 +00:00
|
|
|
else
|
|
|
|
// Store something to balance the stack
|
|
|
|
m_assembly.appendConstant(u256(0));
|
2018-09-25 02:47:25 +00:00
|
|
|
decreaseReference(_identifier.name, _var);
|
2017-05-23 17:21:14 +00:00
|
|
|
},
|
2020-05-11 14:53:45 +00:00
|
|
|
[](Scope::Function&)
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "Function not removed during desugaring.");
|
2017-05-23 17:21:14 +00:00
|
|
|
}
|
2019-11-24 17:46:43 +00:00
|
|
|
}))
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(
|
2017-05-23 17:21:14 +00:00
|
|
|
m_identifierAccess.generateCode,
|
|
|
|
"Identifier not found and no external access available."
|
|
|
|
);
|
|
|
|
m_identifierAccess.generateCode(_identifier, IdentifierContext::RValue, m_assembly);
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
void CodeTransform::operator()(Literal const& _literal)
|
2017-05-23 17:21:14 +00:00
|
|
|
{
|
|
|
|
m_assembly.setSourceLocation(_literal.location);
|
2019-03-05 16:26:28 +00:00
|
|
|
m_assembly.appendConstant(valueOfLiteral(_literal));
|
2017-05-23 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 12:36:41 +00:00
|
|
|
void CodeTransform::operator()(If const& _if)
|
|
|
|
{
|
|
|
|
visitExpression(*_if.condition);
|
|
|
|
m_assembly.setSourceLocation(_if.location);
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::ISZERO);
|
2017-11-21 12:36:41 +00:00
|
|
|
AbstractAssembly::LabelID end = m_assembly.newLabelId();
|
|
|
|
m_assembly.appendJumpToIf(end);
|
|
|
|
(*this)(_if.body);
|
|
|
|
m_assembly.setSourceLocation(_if.location);
|
|
|
|
m_assembly.appendLabel(end);
|
|
|
|
}
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
void CodeTransform::operator()(Switch const& _switch)
|
|
|
|
{
|
|
|
|
//@TODO use JUMPV in EVM1.5?
|
|
|
|
|
|
|
|
visitExpression(*_switch.expression);
|
|
|
|
int expressionHeight = m_assembly.stackHeight();
|
|
|
|
map<Case const*, AbstractAssembly::LabelID> caseBodies;
|
|
|
|
AbstractAssembly::LabelID end = m_assembly.newLabelId();
|
|
|
|
for (Case const& c: _switch.cases)
|
|
|
|
{
|
|
|
|
if (c.value)
|
|
|
|
{
|
|
|
|
(*this)(*c.value);
|
|
|
|
m_assembly.setSourceLocation(c.location);
|
|
|
|
AbstractAssembly::LabelID bodyLabel = m_assembly.newLabelId();
|
|
|
|
caseBodies[&c] = bodyLabel;
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_assembly.stackHeight() == expressionHeight + 1, "");
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::dupInstruction(2));
|
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::EQ);
|
2017-05-24 16:34:19 +00:00
|
|
|
m_assembly.appendJumpToIf(bodyLabel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// default case
|
|
|
|
(*this)(c.body);
|
|
|
|
}
|
|
|
|
m_assembly.setSourceLocation(_switch.location);
|
|
|
|
m_assembly.appendJumpTo(end);
|
|
|
|
|
|
|
|
size_t numCases = caseBodies.size();
|
|
|
|
for (auto const& c: caseBodies)
|
|
|
|
{
|
|
|
|
m_assembly.setSourceLocation(c.first->location);
|
|
|
|
m_assembly.appendLabel(c.second);
|
|
|
|
(*this)(c.first->body);
|
2017-06-01 16:16:38 +00:00
|
|
|
// Avoid useless "jump to next" for the last case.
|
2017-05-24 16:34:19 +00:00
|
|
|
if (--numCases > 0)
|
|
|
|
{
|
|
|
|
m_assembly.setSourceLocation(c.first->location);
|
|
|
|
m_assembly.appendJumpTo(end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_assembly.setSourceLocation(_switch.location);
|
|
|
|
m_assembly.appendLabel(end);
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTransform::operator()(FunctionDefinition const& _function)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_scope, "");
|
|
|
|
yulAssert(m_scope->identifiers.count(_function.name), "");
|
2019-11-19 15:42:49 +00:00
|
|
|
Scope::Function& function = std::get<Scope::Function>(m_scope->identifiers.at(_function.name));
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2021-03-09 11:46:33 +00:00
|
|
|
size_t height = 1;
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_info.scopes.at(&_function.body), "");
|
2021-02-04 20:52:07 +00:00
|
|
|
Scope* virtualFunctionScope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
|
|
|
|
yulAssert(virtualFunctionScope, "");
|
2021-03-31 18:03:04 +00:00
|
|
|
for (auto const& v: _function.parameters | ranges::views::reverse)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2021-02-04 20:52:07 +00:00
|
|
|
auto& var = std::get<Scope::Variable>(virtualFunctionScope->identifiers.at(v.name));
|
2017-06-13 22:01:06 +00:00
|
|
|
m_context->variableStackHeights[&var] = height++;
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_assembly.setSourceLocation(_function.location);
|
2019-05-25 10:37:51 +00:00
|
|
|
int const stackHeightBefore = m_assembly.stackHeight();
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2021-03-09 11:46:33 +00:00
|
|
|
m_assembly.appendLabel(functionEntryID(_function.name, function));
|
2019-05-25 10:37:51 +00:00
|
|
|
|
2020-06-15 10:51:58 +00:00
|
|
|
m_assembly.setStackHeight(static_cast<int>(height));
|
2019-05-25 10:37:51 +00:00
|
|
|
|
2020-07-08 10:54:52 +00:00
|
|
|
CodeTransform subTransform(
|
|
|
|
m_assembly,
|
|
|
|
m_info,
|
|
|
|
_function.body,
|
|
|
|
m_allowStackOpt,
|
|
|
|
m_dialect,
|
|
|
|
m_builtinContext,
|
|
|
|
m_identifierAccess,
|
|
|
|
m_useNamedLabelsForFunctions,
|
2021-02-04 20:52:07 +00:00
|
|
|
m_context,
|
|
|
|
_function.returnVariables,
|
|
|
|
m_assembly.newLabelId()
|
2020-07-08 10:54:52 +00:00
|
|
|
);
|
2021-02-04 20:52:07 +00:00
|
|
|
subTransform.m_scope = virtualFunctionScope;
|
|
|
|
|
|
|
|
if (m_allowStackOpt)
|
|
|
|
// Immediately delete entirely unused parameters.
|
|
|
|
for (auto const& v: _function.parameters | ranges::views::reverse)
|
|
|
|
{
|
|
|
|
auto& var = std::get<Scope::Variable>(virtualFunctionScope->identifiers.at(v.name));
|
|
|
|
if (util::valueOrDefault(m_context->variableReferences, &var, 0u) == 0)
|
|
|
|
subTransform.deleteVariable(var);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_allowStackOpt || _function.returnVariables.empty())
|
|
|
|
subTransform.setupReturnVariablesAndFunctionExit();
|
|
|
|
|
2020-07-08 10:54:52 +00:00
|
|
|
subTransform(_function.body);
|
2020-07-15 09:32:34 +00:00
|
|
|
if (!subTransform.m_stackErrors.empty())
|
2019-01-29 09:51:25 +00:00
|
|
|
{
|
2020-07-15 09:32:34 +00:00
|
|
|
m_assembly.markAsInvalid();
|
|
|
|
for (StackTooDeepError& stackError: subTransform.m_stackErrors)
|
|
|
|
{
|
|
|
|
if (stackError.functionName.empty())
|
|
|
|
stackError.functionName = _function.name;
|
|
|
|
m_stackErrors.emplace_back(std::move(stackError));
|
|
|
|
}
|
2019-01-29 09:51:25 +00:00
|
|
|
}
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
if (!subTransform.returnVariablesAndFunctionExitAreSetup())
|
|
|
|
subTransform.setupReturnVariablesAndFunctionExit();
|
|
|
|
appendPopUntil(*subTransform.m_functionExitStackHeight);
|
|
|
|
|
|
|
|
yulAssert(
|
|
|
|
subTransform.m_functionExitStackHeight &&
|
|
|
|
*subTransform.m_functionExitStackHeight == m_assembly.stackHeight(),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
|
|
|
|
m_assembly.appendLabel(*subTransform.m_functionExitLabel);
|
2019-10-28 14:25:02 +00:00
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2017-06-01 16:16:38 +00:00
|
|
|
// The stack layout here is:
|
|
|
|
// <return label>? <arguments...> <return values...>
|
|
|
|
// But we would like it to be:
|
|
|
|
// <return values...> <return label>?
|
|
|
|
// So we have to append some SWAP and POP instructions.
|
|
|
|
|
|
|
|
// This vector holds the desired target positions of all stack slots and is
|
|
|
|
// modified parallel to the actual stack.
|
2021-02-04 20:52:07 +00:00
|
|
|
vector<int> stackLayout(static_cast<size_t>(m_assembly.stackHeight()), -1);
|
|
|
|
stackLayout[0] = static_cast<int>(_function.returnVariables.size()); // Move return label to the top
|
|
|
|
for (auto&& [n, returnVariable]: ranges::views::enumerate(_function.returnVariables))
|
|
|
|
stackLayout.at(m_context->variableStackHeights.at(
|
|
|
|
&std::get<Scope::Variable>(virtualFunctionScope->identifiers.at(returnVariable.name))
|
|
|
|
)) = static_cast<int>(n);
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2019-01-28 16:19:41 +00:00
|
|
|
if (stackLayout.size() > 17)
|
2019-01-29 09:51:25 +00:00
|
|
|
{
|
2020-10-31 01:59:28 +00:00
|
|
|
StackTooDeepError error(
|
|
|
|
_function.name,
|
|
|
|
YulString{},
|
|
|
|
static_cast<int>(stackLayout.size()) - 17,
|
2019-01-28 16:19:41 +00:00
|
|
|
"The function " +
|
|
|
|
_function.name.str() +
|
|
|
|
" has " +
|
|
|
|
to_string(stackLayout.size() - 17) +
|
|
|
|
" parameters or return variables too many to fit the stack size."
|
2019-01-29 09:51:25 +00:00
|
|
|
);
|
2020-06-02 13:34:28 +00:00
|
|
|
stackError(std::move(error), m_assembly.stackHeight() - static_cast<int>(_function.parameters.size()));
|
2019-01-29 09:51:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-02 13:34:28 +00:00
|
|
|
while (!stackLayout.empty() && stackLayout.back() != static_cast<int>(stackLayout.size() - 1))
|
2019-01-29 09:51:25 +00:00
|
|
|
if (stackLayout.back() < 0)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2019-01-29 09:51:25 +00:00
|
|
|
stackLayout.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-15 10:51:58 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::swapInstruction(static_cast<unsigned>(stackLayout.size()) - static_cast<unsigned>(stackLayout.back()) - 1u));
|
2020-06-02 13:34:28 +00:00
|
|
|
swap(stackLayout[static_cast<size_t>(stackLayout.back())], stackLayout.back());
|
2019-01-29 09:51:25 +00:00
|
|
|
}
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t i = 0; i < stackLayout.size(); ++i)
|
|
|
|
yulAssert(i == static_cast<size_t>(stackLayout[i]), "Error reshuffling stack.");
|
2019-01-29 09:51:25 +00:00
|
|
|
}
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
2021-03-09 11:46:33 +00:00
|
|
|
m_assembly.appendJump(
|
|
|
|
stackHeightBefore - static_cast<int>(_function.returnVariables.size()),
|
|
|
|
AbstractAssembly::JumpType::OutOfFunction
|
|
|
|
);
|
2019-05-25 10:37:51 +00:00
|
|
|
m_assembly.setStackHeight(stackHeightBefore);
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 11:59:26 +00:00
|
|
|
void CodeTransform::operator()(ForLoop const& _forLoop)
|
|
|
|
{
|
|
|
|
Scope* originalScope = m_scope;
|
|
|
|
// We start with visiting the block, but not finalizing it.
|
|
|
|
m_scope = m_info.scopes.at(&_forLoop.pre).get();
|
|
|
|
int stackStartHeight = m_assembly.stackHeight();
|
|
|
|
|
2017-06-14 13:35:51 +00:00
|
|
|
visitStatements(_forLoop.pre.statements);
|
2017-06-09 11:59:26 +00:00
|
|
|
|
|
|
|
AbstractAssembly::LabelID loopStart = m_assembly.newLabelId();
|
|
|
|
AbstractAssembly::LabelID postPart = m_assembly.newLabelId();
|
2019-03-04 14:38:05 +00:00
|
|
|
AbstractAssembly::LabelID loopEnd = m_assembly.newLabelId();
|
2017-06-09 11:59:26 +00:00
|
|
|
|
2017-06-14 13:37:14 +00:00
|
|
|
m_assembly.setSourceLocation(_forLoop.location);
|
2017-06-09 11:59:26 +00:00
|
|
|
m_assembly.appendLabel(loopStart);
|
|
|
|
|
|
|
|
visitExpression(*_forLoop.condition);
|
|
|
|
m_assembly.setSourceLocation(_forLoop.location);
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::ISZERO);
|
2017-06-09 11:59:26 +00:00
|
|
|
m_assembly.appendJumpToIf(loopEnd);
|
|
|
|
|
2019-03-04 14:38:05 +00:00
|
|
|
int const stackHeightBody = m_assembly.stackHeight();
|
|
|
|
m_context->forLoopStack.emplace(Context::ForLoopLabels{ {postPart, stackHeightBody}, {loopEnd, stackHeightBody} });
|
2017-06-09 11:59:26 +00:00
|
|
|
(*this)(_forLoop.body);
|
|
|
|
|
|
|
|
m_assembly.setSourceLocation(_forLoop.location);
|
|
|
|
m_assembly.appendLabel(postPart);
|
|
|
|
|
|
|
|
(*this)(_forLoop.post);
|
|
|
|
|
|
|
|
m_assembly.setSourceLocation(_forLoop.location);
|
|
|
|
m_assembly.appendJumpTo(loopStart);
|
|
|
|
m_assembly.appendLabel(loopEnd);
|
|
|
|
|
|
|
|
finalizeBlock(_forLoop.pre, stackStartHeight);
|
2019-03-04 14:38:05 +00:00
|
|
|
m_context->forLoopStack.pop();
|
2017-06-09 11:59:26 +00:00
|
|
|
m_scope = originalScope;
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 15:20:18 +00:00
|
|
|
int CodeTransform::appendPopUntil(int _targetDepth)
|
2019-03-04 14:38:05 +00:00
|
|
|
{
|
2019-03-11 15:20:18 +00:00
|
|
|
int const stackDiffAfter = m_assembly.stackHeight() - _targetDepth;
|
|
|
|
for (int i = 0; i < stackDiffAfter; ++i)
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2019-03-11 15:20:18 +00:00
|
|
|
return stackDiffAfter;
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 15:20:18 +00:00
|
|
|
void CodeTransform::operator()(Break const& _break)
|
2019-03-04 14:38:05 +00:00
|
|
|
{
|
2019-03-11 15:20:18 +00:00
|
|
|
yulAssert(!m_context->forLoopStack.empty(), "Invalid break-statement. Requires surrounding for-loop in code generation.");
|
|
|
|
m_assembly.setSourceLocation(_break.location);
|
|
|
|
|
|
|
|
Context::JumpInfo const& jump = m_context->forLoopStack.top().done;
|
|
|
|
m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight));
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 15:20:18 +00:00
|
|
|
void CodeTransform::operator()(Continue const& _continue)
|
2019-03-04 14:38:05 +00:00
|
|
|
{
|
2019-03-11 15:20:18 +00:00
|
|
|
yulAssert(!m_context->forLoopStack.empty(), "Invalid continue-statement. Requires surrounding for-loop in code generation.");
|
|
|
|
m_assembly.setSourceLocation(_continue.location);
|
|
|
|
|
|
|
|
Context::JumpInfo const& jump = m_context->forLoopStack.top().post;
|
|
|
|
m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight));
|
2017-06-09 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:06:00 +00:00
|
|
|
void CodeTransform::operator()(Leave const& _leaveStatement)
|
2019-10-28 14:25:02 +00:00
|
|
|
{
|
2021-02-04 20:52:07 +00:00
|
|
|
yulAssert(m_functionExitLabel, "Invalid leave-statement. Requires surrounding function in code generation.");
|
|
|
|
yulAssert(m_functionExitStackHeight, "");
|
2019-11-06 01:06:00 +00:00
|
|
|
m_assembly.setSourceLocation(_leaveStatement.location);
|
2021-02-04 20:52:07 +00:00
|
|
|
m_assembly.appendJumpTo(*m_functionExitLabel, appendPopUntil(*m_functionExitStackHeight));
|
2019-10-28 14:25:02 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
void CodeTransform::operator()(Block const& _block)
|
|
|
|
{
|
2017-06-09 11:59:16 +00:00
|
|
|
Scope* originalScope = m_scope;
|
|
|
|
m_scope = m_info.scopes.at(&_block).get();
|
|
|
|
|
|
|
|
int blockStartStackHeight = m_assembly.stackHeight();
|
2017-06-14 13:35:51 +00:00
|
|
|
visitStatements(_block.statements);
|
2017-06-09 11:59:16 +00:00
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
bool isOutermostFunctionBodyBlock = m_scope && m_scope->superScope && m_scope->superScope->functionScope;
|
|
|
|
bool performValidation = !m_allowStackOpt || !isOutermostFunctionBodyBlock;
|
|
|
|
finalizeBlock(_block, performValidation ? make_optional(blockStartStackHeight) : nullopt);
|
2017-06-09 11:59:16 +00:00
|
|
|
m_scope = originalScope;
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
AbstractAssembly::LabelID CodeTransform::functionEntryID(YulString _name, Scope::Function const& _function)
|
2017-06-13 19:58:25 +00:00
|
|
|
{
|
|
|
|
if (!m_context->functionEntryIDs.count(&_function))
|
2017-08-25 15:04:31 +00:00
|
|
|
{
|
|
|
|
AbstractAssembly::LabelID id =
|
|
|
|
m_useNamedLabelsForFunctions ?
|
2018-10-29 14:12:02 +00:00
|
|
|
m_assembly.namedLabel(_name.str()) :
|
2017-08-25 15:04:31 +00:00
|
|
|
m_assembly.newLabelId();
|
|
|
|
m_context->functionEntryIDs[&_function] = id;
|
|
|
|
}
|
2017-06-13 19:58:25 +00:00
|
|
|
return m_context->functionEntryIDs[&_function];
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:01:22 +00:00
|
|
|
void CodeTransform::visitExpression(Expression const& _expression)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
int height = m_assembly.stackHeight();
|
2019-11-19 15:42:49 +00:00
|
|
|
std::visit(*this, _expression);
|
2017-05-24 16:34:19 +00:00
|
|
|
expectDeposit(1, height);
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
void CodeTransform::setupReturnVariablesAndFunctionExit()
|
|
|
|
{
|
|
|
|
yulAssert(!returnVariablesAndFunctionExitAreSetup(), "");
|
|
|
|
yulAssert(m_scope, "");
|
|
|
|
|
|
|
|
ScopeGuard scopeGuard([oldScope = m_scope, this] { m_scope = oldScope; });
|
|
|
|
if (!m_scope->functionScope)
|
|
|
|
{
|
|
|
|
yulAssert(m_scope->superScope && m_scope->superScope->functionScope, "");
|
|
|
|
m_scope = m_scope->superScope;
|
|
|
|
}
|
|
|
|
|
2021-03-11 16:42:19 +00:00
|
|
|
// We could reuse unused slots for return variables, but it turns out this is detrimental in practice.
|
|
|
|
m_unusedStackSlots.clear();
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
if (m_delayedReturnVariables.empty())
|
|
|
|
{
|
|
|
|
m_functionExitStackHeight = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate slots for return variables as if they were declared as variables in the virtual function scope.
|
|
|
|
for (TypedName const& var: m_delayedReturnVariables)
|
|
|
|
(*this)(VariableDeclaration{var.location, {var}, {}});
|
|
|
|
|
|
|
|
m_functionExitStackHeight = ranges::max(m_delayedReturnVariables | ranges::views::transform([&](TypedName const& _name) {
|
|
|
|
return variableStackHeight(_name.name);
|
|
|
|
})) + 1;
|
|
|
|
m_delayedReturnVariables.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
bool statementNeedsReturnVariableSetup(Statement const& _statement, vector<TypedName> const& _returnVariables)
|
|
|
|
{
|
|
|
|
if (holds_alternative<FunctionDefinition>(_statement))
|
|
|
|
return true;
|
|
|
|
if (
|
|
|
|
holds_alternative<ExpressionStatement>(_statement) ||
|
|
|
|
holds_alternative<Assignment>(_statement)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ReferencesCounter referencesCounter{ReferencesCounter::CountWhat::OnlyVariables};
|
|
|
|
referencesCounter.visit(_statement);
|
|
|
|
auto isReferenced = [&referencesCounter](TypedName const& _returnVariable) {
|
|
|
|
return referencesCounter.references().count(_returnVariable.name);
|
|
|
|
};
|
|
|
|
if (ranges::none_of(_returnVariables, isReferenced))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:35:51 +00:00
|
|
|
void CodeTransform::visitStatements(vector<Statement> const& _statements)
|
|
|
|
{
|
2019-10-28 10:39:30 +00:00
|
|
|
std::optional<AbstractAssembly::LabelID> jumpTarget = std::nullopt;
|
2019-05-25 10:37:51 +00:00
|
|
|
|
2017-06-14 13:35:51 +00:00
|
|
|
for (auto const& statement: _statements)
|
2018-09-25 02:47:25 +00:00
|
|
|
{
|
|
|
|
freeUnusedVariables();
|
2021-02-04 20:52:07 +00:00
|
|
|
if (
|
|
|
|
!m_delayedReturnVariables.empty() &&
|
|
|
|
statementNeedsReturnVariableSetup(statement, m_delayedReturnVariables)
|
|
|
|
)
|
|
|
|
setupReturnVariablesAndFunctionExit();
|
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
auto const* functionDefinition = std::get_if<FunctionDefinition>(&statement);
|
2019-05-25 10:37:51 +00:00
|
|
|
if (functionDefinition && !jumpTarget)
|
|
|
|
{
|
|
|
|
m_assembly.setSourceLocation(locationOf(statement));
|
|
|
|
jumpTarget = m_assembly.newLabelId();
|
|
|
|
m_assembly.appendJumpTo(*jumpTarget, 0);
|
|
|
|
}
|
|
|
|
else if (!functionDefinition && jumpTarget)
|
|
|
|
{
|
|
|
|
m_assembly.appendLabel(*jumpTarget);
|
2019-10-28 10:39:30 +00:00
|
|
|
jumpTarget = std::nullopt;
|
2019-05-25 10:37:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
std::visit(*this, statement);
|
2018-09-25 02:47:25 +00:00
|
|
|
}
|
2019-05-25 10:37:51 +00:00
|
|
|
// we may have a leftover jumpTarget
|
|
|
|
if (jumpTarget)
|
|
|
|
m_assembly.appendLabel(*jumpTarget);
|
|
|
|
|
2018-09-25 02:47:25 +00:00
|
|
|
freeUnusedVariables();
|
2017-06-14 13:35:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
void CodeTransform::finalizeBlock(Block const& _block, optional<int> blockStartStackHeight)
|
2017-06-09 11:59:16 +00:00
|
|
|
{
|
|
|
|
m_assembly.setSourceLocation(_block.location);
|
|
|
|
|
2018-09-25 02:47:25 +00:00
|
|
|
freeUnusedVariables();
|
|
|
|
|
2017-06-09 11:59:16 +00:00
|
|
|
// pop variables
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_info.scopes.at(&_block).get() == m_scope, "");
|
2018-09-25 02:47:25 +00:00
|
|
|
for (auto const& id: m_scope->identifiers)
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<Scope::Variable>(id.second))
|
2018-09-25 02:47:25 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
Scope::Variable const& var = std::get<Scope::Variable>(id.second);
|
2018-09-25 02:47:25 +00:00
|
|
|
if (m_allowStackOpt)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!m_context->variableStackHeights.count(&var), "");
|
|
|
|
yulAssert(!m_context->variableReferences.count(&var), "");
|
2018-09-25 02:47:25 +00:00
|
|
|
}
|
|
|
|
else
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2018-09-25 02:47:25 +00:00
|
|
|
}
|
2017-06-09 11:59:16 +00:00
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
if (blockStartStackHeight)
|
|
|
|
{
|
|
|
|
int deposit = m_assembly.stackHeight() - *blockStartStackHeight;
|
|
|
|
yulAssert(deposit == 0, "Invalid stack height at end of block: " + to_string(deposit));
|
|
|
|
}
|
2017-06-09 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 09:16:07 +00:00
|
|
|
void CodeTransform::generateMultiAssignment(vector<Identifier> const& _variableNames)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_scope, "");
|
2021-03-31 18:03:04 +00:00
|
|
|
for (auto const& variableName: _variableNames | ranges::views::reverse)
|
2017-09-20 09:16:07 +00:00
|
|
|
generateAssignment(variableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeTransform::generateAssignment(Identifier const& _variableName)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_scope, "");
|
2018-09-25 02:47:25 +00:00
|
|
|
if (auto var = m_scope->lookup(_variableName.name))
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
Scope::Variable const& _var = std::get<Scope::Variable>(*var);
|
2020-06-02 13:34:28 +00:00
|
|
|
if (size_t heightDiff = variableHeightDiff(_var, _variableName.name, true))
|
2019-12-12 23:39:29 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::swapInstruction(static_cast<unsigned>(heightDiff - 1)));
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly.appendInstruction(evmasm::Instruction::POP);
|
2018-09-25 02:47:25 +00:00
|
|
|
decreaseReference(_variableName.name, _var);
|
2017-09-20 09:16:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(
|
2017-09-20 09:16:07 +00:00
|
|
|
m_identifierAccess.generateCode,
|
|
|
|
"Identifier not found and no external access available."
|
|
|
|
);
|
|
|
|
m_identifierAccess.generateCode(_variableName, IdentifierContext::LValue, m_assembly);
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_context->variableStackHeights.count(&_var), "");
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t heightDiff = static_cast<size_t>(m_assembly.stackHeight()) - m_context->variableStackHeights[&_var];
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable.");
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t limit = _forSwap ? 17 : 16;
|
2019-01-28 16:19:41 +00:00
|
|
|
if (heightDiff > limit)
|
2019-01-31 14:06:20 +00:00
|
|
|
{
|
2020-10-31 01:59:28 +00:00
|
|
|
m_stackErrors.emplace_back(
|
|
|
|
_varName,
|
|
|
|
heightDiff - limit,
|
2019-01-28 16:19:41 +00:00
|
|
|
"Variable " +
|
|
|
|
_varName.str() +
|
|
|
|
" is " +
|
|
|
|
to_string(heightDiff - limit) +
|
|
|
|
" slot(s) too deep inside the stack."
|
2019-01-31 14:06:20 +00:00
|
|
|
);
|
2020-07-15 09:32:34 +00:00
|
|
|
m_assembly.markAsInvalid();
|
2020-07-08 10:54:52 +00:00
|
|
|
return _forSwap ? 2 : 1;
|
2019-01-31 14:06:20 +00:00
|
|
|
}
|
2019-01-28 16:19:41 +00:00
|
|
|
return heightDiff;
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
int CodeTransform::variableStackHeight(YulString _name) const
|
|
|
|
{
|
|
|
|
Scope::Variable const* var = get_if<Scope::Variable>(m_scope->lookup(_name));
|
|
|
|
yulAssert(var, "");
|
|
|
|
return static_cast<int>(m_context->variableStackHeights.at(var));
|
|
|
|
}
|
|
|
|
|
2017-09-20 07:52:10 +00:00
|
|
|
void CodeTransform::expectDeposit(int _deposit, int _oldHeight) const
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_assembly.stackHeight() == _oldHeight + _deposit, "Invalid stack deposit.");
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|