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.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/backends/evm/EVMAssembly.h>
|
2019-11-26 21:52:09 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
#include <libevmasm/Instruction.h>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
2017-05-24 16:34:19 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2017-06-08 13:52:15 +00:00
|
|
|
/// Size of labels in bytes. Four-byte labels are required by some EVM1.5 instructions.
|
2017-05-24 16:34:19 +00:00
|
|
|
size_t constexpr labelReferenceSize = 4;
|
2017-06-13 22:55:34 +00:00
|
|
|
|
|
|
|
size_t constexpr assemblySizeReferenceSize = 4;
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EVMAssembly::setSourceLocation(SourceLocation const&)
|
|
|
|
{
|
|
|
|
// Ignored for now;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
void EVMAssembly::appendInstruction(evmasm::Instruction _instr)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2018-11-07 11:04:46 +00:00
|
|
|
m_bytecode.push_back(uint8_t(_instr));
|
2019-03-28 11:47:21 +00:00
|
|
|
m_stackHeight += instructionInfo(_instr).ret - instructionInfo(_instr).args;
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EVMAssembly::appendConstant(u256 const& _constant)
|
|
|
|
{
|
|
|
|
bytes data = toCompactBigEndian(_constant, 1);
|
2019-12-12 23:39:29 +00:00
|
|
|
appendInstruction(evmasm::pushInstruction(static_cast<unsigned>(data.size())));
|
2017-05-24 16:34:19 +00:00
|
|
|
m_bytecode += data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVMAssembly::appendLabel(LabelID _labelId)
|
|
|
|
{
|
|
|
|
setLabelToCurrentPosition(_labelId);
|
2019-12-11 16:31:36 +00:00
|
|
|
appendInstruction(evmasm::Instruction::JUMPDEST);
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EVMAssembly::appendLabelReference(LabelID _labelId)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode.");
|
2017-05-24 16:34:19 +00:00
|
|
|
// @TODO we now always use labelReferenceSize for all labels, it could be shortened
|
|
|
|
// for some of them.
|
2019-12-11 16:31:36 +00:00
|
|
|
appendInstruction(evmasm::pushInstruction(labelReferenceSize));
|
2017-05-24 16:34:19 +00:00
|
|
|
m_labelReferences[m_bytecode.size()] = _labelId;
|
|
|
|
m_bytecode += bytes(labelReferenceSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
EVMAssembly::LabelID EVMAssembly::newLabelId()
|
|
|
|
{
|
2020-06-02 13:34:28 +00:00
|
|
|
m_labelPositions[m_nextLabelId] = numeric_limits<size_t>::max();
|
2017-06-08 13:52:15 +00:00
|
|
|
return m_nextLabelId++;
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 15:04:31 +00:00
|
|
|
AbstractAssembly::LabelID EVMAssembly::namedLabel(string const& _name)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!_name.empty(), "");
|
2017-08-25 15:04:31 +00:00
|
|
|
if (!m_namedLabels.count(_name))
|
|
|
|
m_namedLabels[_name] = newLabelId();
|
|
|
|
return m_namedLabels[_name];
|
|
|
|
}
|
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
void EVMAssembly::appendLinkerSymbol(string const&)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "Linker symbols not yet implemented.");
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:22:01 +00:00
|
|
|
void EVMAssembly::appendJump(int _stackDiffAfter, JumpType)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
|
2019-12-11 16:31:36 +00:00
|
|
|
appendInstruction(evmasm::Instruction::JUMP);
|
2017-05-24 16:34:19 +00:00
|
|
|
m_stackHeight += _stackDiffAfter;
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:22:01 +00:00
|
|
|
void EVMAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
if (m_evm15)
|
|
|
|
{
|
2020-07-13 14:20:29 +00:00
|
|
|
m_bytecode.push_back(uint8_t(evmasm::Instruction::EIP615_JUMPTO));
|
2017-05-24 16:34:19 +00:00
|
|
|
appendLabelReferenceInternal(_labelId);
|
|
|
|
m_stackHeight += _stackDiffAfter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
appendLabelReference(_labelId);
|
2020-06-22 12:22:01 +00:00
|
|
|
appendJump(_stackDiffAfter, _jumpType);
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:22:01 +00:00
|
|
|
void EVMAssembly::appendJumpToIf(LabelID _labelId, JumpType)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
if (m_evm15)
|
|
|
|
{
|
2020-07-13 14:20:29 +00:00
|
|
|
m_bytecode.push_back(uint8_t(evmasm::Instruction::EIP615_JUMPIF));
|
2017-05-24 16:34:19 +00:00
|
|
|
appendLabelReferenceInternal(_labelId);
|
|
|
|
m_stackHeight--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
appendLabelReference(_labelId);
|
2019-12-11 16:31:36 +00:00
|
|
|
appendInstruction(evmasm::Instruction::JUMPI);
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:16:38 +00:00
|
|
|
void EVMAssembly::appendBeginsub(LabelID _labelId, int _arguments)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_evm15, "BEGINSUB used for EVM 1.0");
|
|
|
|
yulAssert(_arguments >= 0, "");
|
2017-05-24 16:34:19 +00:00
|
|
|
setLabelToCurrentPosition(_labelId);
|
2020-07-13 14:20:29 +00:00
|
|
|
m_bytecode.push_back(uint8_t(evmasm::Instruction::EIP615_BEGINSUB));
|
2017-05-24 16:34:19 +00:00
|
|
|
m_stackHeight += _arguments;
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:16:38 +00:00
|
|
|
void EVMAssembly::appendJumpsub(LabelID _labelId, int _arguments, int _returns)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_evm15, "JUMPSUB used for EVM 1.0");
|
|
|
|
yulAssert(_arguments >= 0 && _returns >= 0, "");
|
2020-07-13 14:20:29 +00:00
|
|
|
m_bytecode.push_back(uint8_t(evmasm::Instruction::EIP615_JUMPSUB));
|
2017-05-24 16:34:19 +00:00
|
|
|
appendLabelReferenceInternal(_labelId);
|
|
|
|
m_stackHeight += _returns - _arguments;
|
|
|
|
}
|
|
|
|
|
2017-05-31 11:06:51 +00:00
|
|
|
void EVMAssembly::appendReturnsub(int _returns, int _stackDiffAfter)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_evm15, "RETURNSUB used for EVM 1.0");
|
|
|
|
yulAssert(_returns >= 0, "");
|
2020-07-13 14:20:29 +00:00
|
|
|
m_bytecode.push_back(uint8_t(evmasm::Instruction::EIP615_RETURNSUB));
|
2017-05-31 11:06:51 +00:00
|
|
|
m_stackHeight += _stackDiffAfter - _returns;
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::LinkerObject EVMAssembly::finalize()
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2020-07-15 09:32:34 +00:00
|
|
|
yulAssert(!m_invalid, "Attempted to finalize invalid assembly object.");
|
2017-06-13 22:55:34 +00:00
|
|
|
size_t bytecodeSize = m_bytecode.size();
|
|
|
|
for (auto const& ref: m_assemblySizePositions)
|
2017-06-23 00:00:14 +00:00
|
|
|
updateReference(ref, assemblySizeReferenceSize, u256(bytecodeSize));
|
2017-06-13 22:55:34 +00:00
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
for (auto const& ref: m_labelReferences)
|
|
|
|
{
|
|
|
|
size_t referencePos = ref.first;
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_labelPositions.count(ref.second), "");
|
2017-05-24 16:34:19 +00:00
|
|
|
size_t labelPos = m_labelPositions.at(ref.second);
|
2020-06-02 13:34:28 +00:00
|
|
|
yulAssert(labelPos != numeric_limits<size_t>::max(), "Undefined but allocated label used.");
|
2017-06-23 00:00:14 +00:00
|
|
|
updateReference(referencePos, labelReferenceSize, u256(labelPos));
|
2017-05-24 16:34:19 +00:00
|
|
|
}
|
2017-06-13 22:55:34 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::LinkerObject obj;
|
2017-05-24 16:34:19 +00:00
|
|
|
obj.bytecode = m_bytecode;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:16:38 +00:00
|
|
|
void EVMAssembly::setLabelToCurrentPosition(LabelID _labelId)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_labelPositions.count(_labelId), "Label not found.");
|
2020-06-02 13:34:28 +00:00
|
|
|
yulAssert(m_labelPositions[_labelId] == numeric_limits<size_t>::max(), "Label already set.");
|
2017-05-24 16:34:19 +00:00
|
|
|
m_labelPositions[_labelId] = m_bytecode.size();
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:16:38 +00:00
|
|
|
void EVMAssembly::appendLabelReferenceInternal(LabelID _labelId)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
m_labelReferences[m_bytecode.size()] = _labelId;
|
|
|
|
m_bytecode += bytes(labelReferenceSize);
|
|
|
|
}
|
2017-06-13 22:55:34 +00:00
|
|
|
|
|
|
|
void EVMAssembly::appendAssemblySize()
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
appendInstruction(evmasm::pushInstruction(assemblySizeReferenceSize));
|
2017-06-13 22:55:34 +00:00
|
|
|
m_assemblySizePositions.push_back(m_bytecode.size());
|
|
|
|
m_bytecode += bytes(assemblySizeReferenceSize);
|
|
|
|
}
|
2017-06-23 00:00:14 +00:00
|
|
|
|
2018-12-04 17:57:07 +00:00
|
|
|
pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EVMAssembly::createSubAssembly()
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "Sub assemblies not implemented.");
|
2018-12-04 17:57:07 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:17:35 +00:00
|
|
|
void EVMAssembly::appendDataOffset(vector<AbstractAssembly::SubID> const&)
|
2018-12-04 17:57:07 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "Data not implemented.");
|
2018-12-04 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:17:35 +00:00
|
|
|
void EVMAssembly::appendDataSize(vector<AbstractAssembly::SubID> const&)
|
2018-12-04 17:57:07 +00:00
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "Data not implemented.");
|
2018-12-04 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AbstractAssembly::SubID EVMAssembly::appendData(bytes const&)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "Data not implemented.");
|
2018-12-04 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 08:52:27 +00:00
|
|
|
void EVMAssembly::appendImmutable(std::string const&)
|
|
|
|
{
|
|
|
|
yulAssert(false, "loadimmutable not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVMAssembly::appendImmutableAssignment(std::string const&)
|
|
|
|
{
|
|
|
|
yulAssert(false, "setimmutable not implemented.");
|
|
|
|
}
|
|
|
|
|
2017-06-23 00:00:14 +00:00
|
|
|
void EVMAssembly::updateReference(size_t pos, size_t size, u256 value)
|
|
|
|
{
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, "");
|
|
|
|
yulAssert(value < (u256(1) << (8 * size)), "");
|
2017-06-23 00:00:14 +00:00
|
|
|
for (size_t i = 0; i < size; i++)
|
2018-11-07 11:04:46 +00:00
|
|
|
m_bytecode[pos + i] = uint8_t((value >> (8 * (size - i - 1))) & 0xff);
|
2017-06-23 00:00:14 +00:00
|
|
|
}
|