solidity/libyul/backends/evm/NoOutputAssembly.cpp

149 lines
3.9 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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2019-01-28 17:11:58 +00:00
/**
* Assembly interface that ignores everything. Can be used as a backend for a compilation dry-run.
*/
#include <libyul/backends/evm/NoOutputAssembly.h>
#include <libyul/AST.h>
#include <libyul/Exceptions.h>
2019-01-28 17:11:58 +00:00
#include <libevmasm/Instruction.h>
#include <range/v3/view/iota.hpp>
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, m_evmVersion).ret - instructionInfo(_instr, m_evmVersion).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)
{
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(std::string const&, size_t, size_t, std::optional<size_t>)
2019-01-28 17:11:58 +00:00
{
return 1;
}
void NoOutputAssembly::appendLinkerSymbol(std::string const&)
2019-01-28 17:11:58 +00:00
{
yulAssert(false, "Linker symbols not yet implemented.");
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendVerbatim(bytes, size_t _arguments, size_t _returnVariables)
2021-03-17 18:37:39 +00:00
{
m_stackHeight += static_cast<int>(_returnVariables - _arguments);
2021-03-17 18:37:39 +00:00
}
2020-06-22 12:22:01 +00:00
void NoOutputAssembly::appendJump(int _stackDiffAfter, JumpType)
2019-01-28 17:11:58 +00:00
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::JUMP);
2019-01-28 17:11:58 +00:00
m_stackHeight += _stackDiffAfter;
}
2020-06-22 12:22:01 +00:00
void NoOutputAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType)
2019-01-28 17:11:58 +00:00
{
2021-03-09 11:46:33 +00:00
appendLabelReference(_labelId);
appendJump(_stackDiffAfter, _jumpType);
2019-01-28 17:11:58 +00:00
}
2020-06-22 12:22:01 +00:00
void NoOutputAssembly::appendJumpToIf(LabelID _labelId, JumpType)
2019-01-28 17:11:58 +00:00
{
2021-03-09 11:46:33 +00:00
appendLabelReference(_labelId);
appendInstruction(evmasm::Instruction::JUMPI);
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendAssemblySize()
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::PUSH1);
2019-01-28 17:11:58 +00:00
}
std::pair<std::shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> NoOutputAssembly::createSubAssembly(bool, std::string)
2019-01-28 17:11:58 +00:00
{
yulAssert(false, "Sub assemblies not implemented.");
2019-01-28 17:11:58 +00:00
return {};
}
void NoOutputAssembly::appendDataOffset(std::vector<AbstractAssembly::SubID> const&)
2019-01-28 17:11:58 +00:00
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::PUSH1);
2019-01-28 17:11:58 +00:00
}
void NoOutputAssembly::appendDataSize(std::vector<AbstractAssembly::SubID> const&)
2019-01-28 17:11:58 +00:00
{
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 returns = fun.second.returns.size();
fun.second.generateCode = [=](FunctionCall const& _call, AbstractAssembly& _assembly, BuiltinContext&)
{
for (size_t i: ranges::views::iota(0u, _call.arguments.size()))
if (!fun.second.literalArgument(i))
_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));
};
}
}