2017-05-24 16:34:19 +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-24 16:34:19 +00:00
|
|
|
/**
|
|
|
|
* Assembly interface for EVM and EVM1.5.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/backends/evm/AbstractAssembly.h>
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
#include <libevmasm/LinkerObject.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::langutil
|
2018-11-14 16:11:55 +00:00
|
|
|
{
|
|
|
|
struct SourceLocation;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
class EVMAssembly: public AbstractAssembly
|
|
|
|
{
|
|
|
|
public:
|
2021-03-09 11:46:33 +00:00
|
|
|
explicit EVMAssembly() { }
|
2020-03-31 23:58:22 +00:00
|
|
|
~EVMAssembly() override = default;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
/// Set a new source location valid starting from the next instruction.
|
2018-11-14 16:11:55 +00:00
|
|
|
void setSourceLocation(langutil::SourceLocation const& _location) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Retrieve the current height of the stack. This does not have to be zero
|
|
|
|
/// at the beginning.
|
2018-11-16 01:09:04 +00:00
|
|
|
int stackHeight() const override { return m_stackHeight; }
|
2019-05-25 10:37:51 +00:00
|
|
|
void setStackHeight(int height) override { m_stackHeight = height; }
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append an EVM instruction.
|
2019-12-11 16:31:36 +00:00
|
|
|
void appendInstruction(evmasm::Instruction _instruction) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a constant.
|
2019-12-11 16:31:36 +00:00
|
|
|
void appendConstant(u256 const& _constant) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a label.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendLabel(LabelID _labelId) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a label reference.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendLabelReference(LabelID _labelId) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Generate a new unique label.
|
2018-11-16 01:09:04 +00:00
|
|
|
LabelID newLabelId() override;
|
2017-08-25 15:04:31 +00:00
|
|
|
/// Returns a label identified by the given name. Creates it if it does not yet exist.
|
2018-11-16 01:09:04 +00:00
|
|
|
LabelID namedLabel(std::string const& _name) override;
|
2018-07-10 07:18:59 +00:00
|
|
|
/// Append a reference to a to-be-linked symbol.
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Currently, we assume that the value is always a 20 byte number.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendLinkerSymbol(std::string const& _name) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
/// Append a jump instruction.
|
|
|
|
/// @param _stackDiffAfter the stack adjustment after this instruction.
|
2020-06-22 12:22:01 +00:00
|
|
|
void appendJump(int _stackDiffAfter, JumpType _jumpType) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a jump-to-immediate operation.
|
2020-06-22 12:22:01 +00:00
|
|
|
void appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a jump-to-if-immediate operation.
|
2020-06-22 12:22:01 +00:00
|
|
|
void appendJumpToIf(LabelID _labelId, JumpType _jumpType) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2017-06-13 22:55:34 +00:00
|
|
|
/// Append the assembled size as a constant.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendAssemblySize() override;
|
2021-02-03 10:47:16 +00:00
|
|
|
std::pair<std::shared_ptr<AbstractAssembly>, SubID> createSubAssembly(std::string _name = "") override;
|
2020-06-17 09:17:35 +00:00
|
|
|
void appendDataOffset(std::vector<SubID> const& _subPath) override;
|
|
|
|
void appendDataSize(std::vector<SubID> const& _subPath) override;
|
2019-12-11 16:31:36 +00:00
|
|
|
SubID appendData(bytes const& _data) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2020-04-14 08:52:27 +00:00
|
|
|
void appendImmutable(std::string const& _identifier) override;
|
|
|
|
void appendImmutableAssignment(std::string const& _identifier) override;
|
|
|
|
|
2020-07-15 09:32:34 +00:00
|
|
|
void markAsInvalid() override { m_invalid = true; }
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Resolves references inside the bytecode and returns the linker object.
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::LinkerObject finalize();
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void setLabelToCurrentPosition(AbstractAssembly::LabelID _labelId);
|
|
|
|
void appendLabelReferenceInternal(AbstractAssembly::LabelID _labelId);
|
2019-12-11 16:31:36 +00:00
|
|
|
void updateReference(size_t pos, size_t size, u256 value);
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2017-06-08 13:52:15 +00:00
|
|
|
LabelID m_nextLabelId = 0;
|
2017-05-24 16:34:19 +00:00
|
|
|
int m_stackHeight = 0;
|
2019-12-11 16:31:36 +00:00
|
|
|
bytes m_bytecode;
|
2017-08-25 15:04:31 +00:00
|
|
|
std::map<std::string, LabelID> m_namedLabels;
|
2017-05-24 16:34:19 +00:00
|
|
|
std::map<LabelID, size_t> m_labelPositions;
|
|
|
|
std::map<size_t, LabelID> m_labelReferences;
|
2017-06-13 22:55:34 +00:00
|
|
|
std::vector<size_t> m_assemblySizePositions;
|
2020-07-15 09:32:34 +00:00
|
|
|
bool m_invalid = false;
|
2017-05-24 16:34:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|