solidity/libyul/backends/evm/NoOutputAssembly.cpp

180 lines
4.5 KiB
C++
Raw Normal View History

2019-01-28 17:11:58 +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 that ignores everything. Can be used as a backend for a compilation dry-run.
*/
#include <libyul/backends/evm/NoOutputAssembly.h>
#include <libyul/Exceptions.h>
2019-01-28 17:11:58 +00:00
#include <libevmasm/Instruction.h>
#include <boost/range/adaptor/reversed.hpp>
2019-01-28 17:11:58 +00:00
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;
2019-01-28 17:11:58 +00:00
2019-12-11 16:31:36 +00:00
void NoOutputAssembly::appendInstruction(evmasm::Instruction _instr)
2019-01-28 17:11:58 +00:00
{
m_stackHeight += instructionInfo(_instr).ret - instructionInfo(_instr).args;
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendConstant(u256 const&)
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::pushInstruction(1));
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendLabel(LabelID)
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::JUMPDEST);
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendLabelReference(LabelID)
{
yulAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode.");
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::pushInstruction(1));
2019-01-28 17:11:58 +00:00
}
NoOutputAssembly::LabelID NoOutputAssembly::newLabelId()
{
return 1;
}
AbstractAssembly::LabelID NoOutputAssembly::namedLabel(string const&)
{
return 1;
}
void NoOutputAssembly::appendLinkerSymbol(string const&)
{
yulAssert(false, "Linker symbols not yet implemented.");
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendJump(int _stackDiffAfter)
{
yulAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::JUMP);
2019-01-28 17:11:58 +00:00
m_stackHeight += _stackDiffAfter;
}
void NoOutputAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
{
if (m_evm15)
m_stackHeight += _stackDiffAfter;
else
{
appendLabelReference(_labelId);
appendJump(_stackDiffAfter);
}
}
void NoOutputAssembly::appendJumpToIf(LabelID _labelId)
{
if (m_evm15)
m_stackHeight--;
else
{
appendLabelReference(_labelId);
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::JUMPI);
2019-01-28 17:11:58 +00:00
}
}
void NoOutputAssembly::appendBeginsub(LabelID, int _arguments)
{
yulAssert(m_evm15, "BEGINSUB used for EVM 1.0");
yulAssert(_arguments >= 0, "");
2019-01-28 17:11:58 +00:00
m_stackHeight += _arguments;
}
void NoOutputAssembly::appendJumpsub(LabelID, int _arguments, int _returns)
{
yulAssert(m_evm15, "JUMPSUB used for EVM 1.0");
yulAssert(_arguments >= 0 && _returns >= 0, "");
2019-01-28 17:11:58 +00:00
m_stackHeight += _returns - _arguments;
}
void NoOutputAssembly::appendReturnsub(int _returns, int _stackDiffAfter)
{
yulAssert(m_evm15, "RETURNSUB used for EVM 1.0");
yulAssert(_returns >= 0, "");
2019-01-28 17:11:58 +00:00
m_stackHeight += _stackDiffAfter - _returns;
}
void NoOutputAssembly::appendAssemblySize()
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::PUSH1);
2019-01-28 17:11:58 +00:00
}
pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> NoOutputAssembly::createSubAssembly()
{
yulAssert(false, "Sub assemblies not implemented.");
2019-01-28 17:11:58 +00:00
return {};
}
void NoOutputAssembly::appendDataOffset(AbstractAssembly::SubID)
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::PUSH1);
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendDataSize(AbstractAssembly::SubID)
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::PUSH1);
2019-01-28 17:11:58 +00:00
}
AbstractAssembly::SubID NoOutputAssembly::appendData(bytes const&)
{
return 1;
}
void NoOutputAssembly::appendImmutable(std::string const&)
{
yulAssert(false, "loadimmutable not implemented.");
}
void NoOutputAssembly::appendImmutableAssignment(std::string const&)
{
yulAssert(false, "setimmutable not implemented.");
}
NoOutputEVMDialect::NoOutputEVMDialect(EVMDialect const& _copyFrom):
2019-12-19 16:58:20 +00:00
EVMDialect(_copyFrom.evmVersion(), _copyFrom.providesObjectAccess())
{
for (auto& fun: m_functions)
{
size_t parameters = fun.second.parameters.size();
size_t returns = fun.second.returns.size();
fun.second.generateCode = [=](FunctionCall const& _call, AbstractAssembly& _assembly, BuiltinContext&, std::function<void(Expression const&)> _visitExpression)
{
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
_visitExpression(arg);
for (size_t i = 0; i < parameters; i++)
2019-12-11 16:31:36 +00:00
_assembly.appendInstruction(evmasm::Instruction::POP);
for (size_t i = 0; i < returns; i++)
2019-02-26 18:43:24 +00:00
_assembly.appendConstant(u256(0));
};
}
}