From fe35512febff0a5453d14fe8e00dfee7dbbe712a Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 8 Jun 2021 18:41:22 +0200 Subject: [PATCH] Remove EVMAssembly. --- libyul/AssemblyStack.cpp | 1 - libyul/CMakeLists.txt | 2 - libyul/backends/evm/EVMAssembly.cpp | 190 ----------------------- libyul/backends/evm/EVMAssembly.h | 104 ------------- libyul/backends/evm/EVMCodeTransform.cpp | 2 +- libyul/backends/evm/EVMCodeTransform.h | 3 - 6 files changed, 1 insertion(+), 301 deletions(-) delete mode 100644 libyul/backends/evm/EVMAssembly.cpp delete mode 100644 libyul/backends/evm/EVMAssembly.h diff --git a/libyul/AssemblyStack.cpp b/libyul/AssemblyStack.cpp index 9f401cdd4..55682cc95 100644 --- a/libyul/AssemblyStack.cpp +++ b/libyul/AssemblyStack.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt index 80c83ebc2..18a96049d 100644 --- a/libyul/CMakeLists.txt +++ b/libyul/CMakeLists.txt @@ -55,8 +55,6 @@ add_library(yul backends/evm/ConstantOptimiser.h backends/evm/EthAssemblyAdapter.cpp backends/evm/EthAssemblyAdapter.h - backends/evm/EVMAssembly.cpp - backends/evm/EVMAssembly.h backends/evm/EVMCodeTransform.cpp backends/evm/EVMCodeTransform.h backends/evm/EVMDialect.cpp diff --git a/libyul/backends/evm/EVMAssembly.cpp b/libyul/backends/evm/EVMAssembly.cpp deleted file mode 100644 index 2e843b268..000000000 --- a/libyul/backends/evm/EVMAssembly.cpp +++ /dev/null @@ -1,190 +0,0 @@ -/* - 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 . -*/ -// SPDX-License-Identifier: GPL-3.0 -/** - * Assembly interface for EVM and EVM1.5. - */ - -#include -#include - -#include - -using namespace std; -using namespace solidity; -using namespace solidity::yul; -using namespace solidity::util; -using namespace solidity::langutil; - -namespace -{ -/// Size of labels in bytes. Four-byte labels are required by some EVM1.5 instructions. -size_t constexpr labelReferenceSize = 4; - -size_t constexpr assemblySizeReferenceSize = 4; -} - - -void EVMAssembly::setSourceLocation(langutil::SourceLocation const&) -{ - // Ignored for now; -} - -void EVMAssembly::appendInstruction(evmasm::Instruction _instr) -{ - m_bytecode.push_back(uint8_t(_instr)); - m_stackHeight += instructionInfo(_instr).ret - instructionInfo(_instr).args; -} - -void EVMAssembly::appendConstant(u256 const& _constant) -{ - bytes data = toCompactBigEndian(_constant, 1); - appendInstruction(evmasm::pushInstruction(static_cast(data.size()))); - m_bytecode += data; -} - -void EVMAssembly::appendLabel(LabelID _labelId) -{ - setLabelToCurrentPosition(_labelId); - appendInstruction(evmasm::Instruction::JUMPDEST); -} - -void EVMAssembly::appendLabelReference(LabelID _labelId) -{ - // @TODO we now always use labelReferenceSize for all labels, it could be shortened - // for some of them. - appendInstruction(evmasm::pushInstruction(labelReferenceSize)); - m_labelReferences[m_bytecode.size()] = _labelId; - m_bytecode += bytes(labelReferenceSize); -} - -EVMAssembly::LabelID EVMAssembly::newLabelId() -{ - m_labelPositions[m_nextLabelId] = numeric_limits::max(); - return m_nextLabelId++; -} - -AbstractAssembly::LabelID EVMAssembly::namedLabel(string const& _name, size_t, size_t, std::optional) -{ - yulAssert(!_name.empty(), ""); - if (!m_namedLabels.count(_name)) - m_namedLabels[_name] = newLabelId(); - return m_namedLabels[_name]; -} - -void EVMAssembly::appendLinkerSymbol(string const&) -{ - yulAssert(false, "Linker symbols not yet implemented."); -} - -void EVMAssembly::appendJump(int _stackDiffAfter, JumpType) -{ - appendInstruction(evmasm::Instruction::JUMP); - m_stackHeight += _stackDiffAfter; -} - -void EVMAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType) -{ - appendLabelReference(_labelId); - appendJump(_stackDiffAfter, _jumpType); -} - -void EVMAssembly::appendJumpToIf(LabelID _labelId, JumpType) -{ - appendLabelReference(_labelId); - appendInstruction(evmasm::Instruction::JUMPI); -} - -evmasm::LinkerObject EVMAssembly::finalize() -{ - yulAssert(!m_invalid, "Attempted to finalize invalid assembly object."); - size_t bytecodeSize = m_bytecode.size(); - for (auto const& ref: m_assemblySizePositions) - updateReference(ref, assemblySizeReferenceSize, u256(bytecodeSize)); - - for (auto const& ref: m_labelReferences) - { - size_t referencePos = ref.first; - yulAssert(m_labelPositions.count(ref.second), ""); - size_t labelPos = m_labelPositions.at(ref.second); - yulAssert(labelPos != numeric_limits::max(), "Undefined but allocated label used."); - updateReference(referencePos, labelReferenceSize, u256(labelPos)); - } - - evmasm::LinkerObject obj; - obj.bytecode = m_bytecode; - return obj; -} - -void EVMAssembly::setLabelToCurrentPosition(LabelID _labelId) -{ - yulAssert(m_labelPositions.count(_labelId), "Label not found."); - yulAssert(m_labelPositions[_labelId] == numeric_limits::max(), "Label already set."); - m_labelPositions[_labelId] = m_bytecode.size(); -} - -void EVMAssembly::appendLabelReferenceInternal(LabelID _labelId) -{ - m_labelReferences[m_bytecode.size()] = _labelId; - m_bytecode += bytes(labelReferenceSize); -} - -void EVMAssembly::appendAssemblySize() -{ - appendInstruction(evmasm::pushInstruction(assemblySizeReferenceSize)); - m_assemblySizePositions.push_back(m_bytecode.size()); - m_bytecode += bytes(assemblySizeReferenceSize); -} - -pair, AbstractAssembly::SubID> EVMAssembly::createSubAssembly(string) -{ - yulAssert(false, "Sub assemblies not implemented."); - return {}; -} - -void EVMAssembly::appendDataOffset(vector const&) -{ - yulAssert(false, "Data not implemented."); -} - -void EVMAssembly::appendDataSize(vector const&) -{ - yulAssert(false, "Data not implemented."); -} - -AbstractAssembly::SubID EVMAssembly::appendData(bytes const&) -{ - yulAssert(false, "Data not implemented."); -} - -void EVMAssembly::appendImmutable(std::string const&) -{ - yulAssert(false, "loadimmutable not implemented."); -} - -void EVMAssembly::appendImmutableAssignment(std::string const&) -{ - yulAssert(false, "setimmutable not implemented."); -} - -void EVMAssembly::updateReference(size_t pos, size_t size, u256 value) -{ - yulAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, ""); - yulAssert(value < (u256(1) << (8 * size)), ""); - for (size_t i = 0; i < size; i++) - m_bytecode[pos + i] = uint8_t((value >> (8 * (size - i - 1))) & 0xff); -} diff --git a/libyul/backends/evm/EVMAssembly.h b/libyul/backends/evm/EVMAssembly.h deleted file mode 100644 index 25c2765f5..000000000 --- a/libyul/backends/evm/EVMAssembly.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - 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 . -*/ -// SPDX-License-Identifier: GPL-3.0 -/** - * Assembly interface for EVM and EVM1.5. - */ - -#pragma once - -#include - -#include - -#include - -namespace solidity::langutil -{ -struct SourceLocation; -} - -namespace solidity::yul -{ - -class EVMAssembly: public AbstractAssembly -{ -public: - explicit EVMAssembly() { } - ~EVMAssembly() override = default; - - /// Set a new source location valid starting from the next instruction. - void setSourceLocation(langutil::SourceLocation const& _location) override; - /// Retrieve the current height of the stack. This does not have to be zero - /// at the beginning. - int stackHeight() const override { return m_stackHeight; } - void setStackHeight(int height) override { m_stackHeight = height; } - /// Append an EVM instruction. - void appendInstruction(evmasm::Instruction _instruction) override; - /// Append a constant. - void appendConstant(u256 const& _constant) override; - /// Append a label. - void appendLabel(LabelID _labelId) override; - /// Append a label reference. - void appendLabelReference(LabelID _labelId) override; - /// Generate a new unique label. - LabelID newLabelId() override; - /// Returns a label identified by the given name. Creates it if it does not yet exist. - LabelID namedLabel(std::string const& _name, size_t _params, size_t _returns, std::optional _sourceID) override; - /// Append a reference to a to-be-linked symbol. - /// Currently, we assume that the value is always a 20 byte number. - void appendLinkerSymbol(std::string const& _name) override; - - /// Append a jump instruction. - /// @param _stackDiffAfter the stack adjustment after this instruction. - void appendJump(int _stackDiffAfter, JumpType _jumpType) override; - /// Append a jump-to-immediate operation. - void appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType) override; - /// Append a jump-to-if-immediate operation. - void appendJumpToIf(LabelID _labelId, JumpType _jumpType) override; - - /// Append the assembled size as a constant. - void appendAssemblySize() override; - std::pair, SubID> createSubAssembly(std::string _name = "") override; - void appendDataOffset(std::vector const& _subPath) override; - void appendDataSize(std::vector const& _subPath) override; - SubID appendData(bytes const& _data) override; - - void appendImmutable(std::string const& _identifier) override; - void appendImmutableAssignment(std::string const& _identifier) override; - - void markAsInvalid() override { m_invalid = true; } - - /// Resolves references inside the bytecode and returns the linker object. - evmasm::LinkerObject finalize(); - -private: - void setLabelToCurrentPosition(AbstractAssembly::LabelID _labelId); - void appendLabelReferenceInternal(AbstractAssembly::LabelID _labelId); - void updateReference(size_t pos, size_t size, u256 value); - - LabelID m_nextLabelId = 0; - int m_stackHeight = 0; - bytes m_bytecode; - std::map m_namedLabels; - std::map m_labelPositions; - std::map m_labelReferences; - std::vector m_assemblySizePositions; - bool m_invalid = false; -}; - -} diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp index c54a5e9a9..e4ba54b2a 100644 --- a/libyul/backends/evm/EVMCodeTransform.cpp +++ b/libyul/backends/evm/EVMCodeTransform.cpp @@ -236,7 +236,7 @@ void CodeTransform::operator()(FunctionCall const& _call) else { m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_call.debugData)); - EVMAssembly::LabelID returnLabel(numeric_limits::max()); // only used for evm 1.0 + AbstractAssembly::LabelID returnLabel(numeric_limits::max()); // only used for evm 1.0 returnLabel = m_assembly.newLabelId(); m_assembly.appendLabelReference(returnLabel); diff --git a/libyul/backends/evm/EVMCodeTransform.h b/libyul/backends/evm/EVMCodeTransform.h index 531861a6e..a76638850 100644 --- a/libyul/backends/evm/EVMCodeTransform.h +++ b/libyul/backends/evm/EVMCodeTransform.h @@ -21,8 +21,6 @@ #pragma once -#include - #include #include #include @@ -41,7 +39,6 @@ namespace solidity::yul { struct AsmAnalysisInfo; -class EVMAssembly; struct CodeTransformContext {