From a5a99620dc442949f2529b0dc9c1b9226c06c106 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Tue, 9 Mar 2021 17:51:53 +0100 Subject: [PATCH] Name and rename things. --- libyul/backends/evm/EVMCodeTransform.cpp | 29 ++++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp index d604167b3..c4de1de71 100644 --- a/libyul/backends/evm/EVMCodeTransform.cpp +++ b/libyul/backends/evm/EVMCodeTransform.cpp @@ -532,24 +532,30 @@ void CodeTransform::operator()(FunctionDefinition const& _function) { // This vector stores true for stack slots containing return labels or return values, // false otherwise. - vector stackLayout(static_cast(m_assembly.stackHeight()), false); - stackLayout[0] = true; + vector keep(static_cast(m_assembly.stackHeight()), false); + keep[0] = true; for (auto const& returnVariable: _function.returnVariables) - stackLayout.at(m_context->variableStackHeights.at( + keep.at(m_context->variableStackHeights.at( &std::get(virtualFunctionScope->identifiers.at(returnVariable.name)) )) = true; // Shuffle leftover arguments up and pop them. int stackDeficit = 0; - shuffle(stackLayout, [](bool _v) { return _v; }, [&](int _slot) { + auto keepSlot = [](bool _v) { return _v; }; + auto swapSlot = [&](int _slot) { if (m_assembly.stackHeight() - _slot > 17) stackDeficit = std::max(stackDeficit, m_assembly.stackHeight() - _slot - 17); else m_assembly.appendInstruction(evmasm::swapInstruction( static_cast(m_assembly.stackHeight() - _slot - 1) )); - swap(stackLayout.at(static_cast(_slot)), stackLayout.back()); - }, [&]() { stackLayout.pop_back(); m_assembly.appendInstruction(evmasm::Instruction::POP); }); + swap(keep.at(static_cast(_slot)), keep.back()); + }; + auto popSlot = [&]() { + keep.pop_back(); + m_assembly.appendInstruction(evmasm::Instruction::POP); + }; + shuffle(keep, keepSlot, swapSlot, popSlot); if (stackDeficit) { @@ -775,16 +781,19 @@ void CodeTransform::setupReturnVariablesAndFunctionExit() availableStackSlots.emplace(height++); int maxHeight = (*availableStackSlots.crbegin()) + 1; - // Create a mask that is true for return variables and the return label and false for any remaining arguments in between. - // Create a layout that has an increasing integer sequence in place of the return variables. + // Create a layout that has an increasing integer sequence in place of the return variables and + // contains -1 in place of arguments to be popped. vector layout(static_cast(maxHeight), -1); for (auto&& [n, slot]: ranges::views::enumerate(availableStackSlots)) layout.at(static_cast(slot)) = static_cast(n + 1); layout[0] = 0; - shuffle(layout, [](auto const& _v) { return _v != -1; }, [&](int slot) { + auto keepSlot = [](auto const& _v) { return _v != -1; }; + auto swapSlot = [&](int slot) { swap(layout.at(static_cast(slot)), layout.back()); - }, [&]() { layout.pop_back(); }); + }; + auto popSlot = [&]() { layout.pop_back(); }; + shuffle(layout, keepSlot, swapSlot, popSlot); yulAssert(layout.size() == m_delayedReturnVariables.size() + 1, "");