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>
|
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
namespace langutil
|
|
|
|
{
|
|
|
|
struct SourceLocation;
|
|
|
|
}
|
|
|
|
|
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.
|
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; }
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append an EVM instruction.
|
2018-11-21 11:42:34 +00:00
|
|
|
void appendInstruction(dev::solidity::Instruction _instruction) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a constant.
|
2018-11-21 11:42:34 +00:00
|
|
|
void appendConstant(dev::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.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendJump(int _stackDiffAfter) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a jump-to-immediate operation.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendJumpTo(LabelID _labelId, int _stackDiffAfter) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Append a jump-to-if-immediate operation.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendJumpToIf(LabelID _labelId) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Start a subroutine.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendBeginsub(LabelID _labelId, int _arguments) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Call a subroutine.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendJumpsub(LabelID _labelId, int _arguments, int _returns) override;
|
2017-05-24 16:34:19 +00:00
|
|
|
/// Return from a subroutine.
|
2018-11-16 01:09:04 +00:00
|
|
|
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.
|
2018-11-16 01:09:04 +00:00
|
|
|
void appendAssemblySize() override;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
/// Resolves references inside the bytecode and returns the linker object.
|
2018-11-21 11:42:34 +00:00
|
|
|
dev::eth::LinkerObject finalize();
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void setLabelToCurrentPosition(AbstractAssembly::LabelID _labelId);
|
|
|
|
void appendLabelReferenceInternal(AbstractAssembly::LabelID _labelId);
|
2018-11-21 11:42:34 +00:00
|
|
|
void updateReference(size_t pos, size_t size, dev::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;
|
2018-11-21 11:42:34 +00:00
|
|
|
dev::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
|
|
|
};
|
|
|
|
|
|
|
|
}
|