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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
2018-10-15 09:58:51 +00:00
|
|
|
namespace yul
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
class EVMAssembly: public AbstractAssembly
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit EVMAssembly(bool _evm15 = false): m_evm15(_evm15) { }
|
|
|
|
virtual ~EVMAssembly() {}
|
|
|
|
|
|
|
|
/// Set a new source location valid starting from the next instruction.
|
|
|
|
virtual void setSourceLocation(SourceLocation const& _location) override;
|
|
|
|
/// Retrieve the current height of the stack. This does not have to be zero
|
|
|
|
/// at the beginning.
|
|
|
|
virtual int stackHeight() const override { return m_stackHeight; }
|
|
|
|
/// Append an EVM instruction.
|
|
|
|
virtual void appendInstruction(solidity::Instruction _instruction) override;
|
|
|
|
/// Append a constant.
|
|
|
|
virtual void appendConstant(u256 const& _constant) override;
|
|
|
|
/// Append a label.
|
|
|
|
virtual void appendLabel(LabelID _labelId) override;
|
|
|
|
/// Append a label reference.
|
|
|
|
virtual void appendLabelReference(LabelID _labelId) override;
|
|
|
|
/// Generate a new unique label.
|
|
|
|
virtual 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.
|
|
|
|
virtual 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.
|
|
|
|
virtual void appendLinkerSymbol(std::string const& _name) override;
|
|
|
|
|
|
|
|
/// Append a jump instruction.
|
|
|
|
/// @param _stackDiffAfter the stack adjustment after this instruction.
|
|
|
|
virtual void appendJump(int _stackDiffAfter) override;
|
|
|
|
/// Append a jump-to-immediate operation.
|
2017-06-08 13:52:15 +00:00
|
|
|
virtual void appendJumpTo(LabelID _labelId, int _stackDiffAfter) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a jump-to-if-immediate operation.
|
2017-06-08 13:52:15 +00:00
|
|
|
virtual void appendJumpToIf(LabelID _labelId) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Start a subroutine.
|
2017-06-08 13:52:15 +00:00
|
|
|
virtual void appendBeginsub(LabelID _labelId, int _arguments) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Call a subroutine.
|
2017-06-08 13:52:15 +00:00
|
|
|
virtual void appendJumpsub(LabelID _labelId, int _arguments, int _returns) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Return from a subroutine.
|
2017-05-31 11:06:51 +00:00
|
|
|
virtual void appendReturnsub(int _returns, int _stackDiffAfter) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
2017-06-13 22:55:34 +00:00
|
|
|
/// Append the assembled size as a constant.
|
|
|
|
virtual void appendAssemblySize() override;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
/// Resolves references inside the bytecode and returns the linker object.
|
|
|
|
eth::LinkerObject finalize();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setLabelToCurrentPosition(AbstractAssembly::LabelID _labelId);
|
|
|
|
void appendLabelReferenceInternal(AbstractAssembly::LabelID _labelId);
|
2017-06-23 00:00:14 +00:00
|
|
|
void updateReference(size_t pos, size_t size, u256 value);
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
bool m_evm15 = false; ///< if true, switch to evm1.5 mode
|
2017-06-08 13:52:15 +00:00
|
|
|
LabelID m_nextLabelId = 0;
|
2017-05-24 16:34:19 +00:00
|
|
|
int m_stackHeight = 0;
|
|
|
|
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;
|
2017-05-24 16:34:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|