solidity/libyul/backends/evm/AsmCodeGen.cpp

229 lines
5.8 KiB
C++
Raw Normal View History

/*
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/>.
*/
/**
2018-12-04 14:20:29 +00:00
* Adaptor between the abstract assembly and eth assembly.
*/
2019-02-13 10:35:49 +00:00
#include <libyul/backends/evm/AsmCodeGen.h>
2017-02-15 13:52:53 +00:00
#include <libyul/AsmData.h>
#include <libyul/AsmAnalysisInfo.h>
2017-02-15 13:52:53 +00:00
#include <libyul/backends/evm/AbstractAssembly.h>
#include <libyul/backends/evm/EVMCodeTransform.h>
#include <libevmasm/Assembly.h>
#include <libevmasm/AssemblyItem.h>
#include <libevmasm/Instruction.h>
2017-02-15 13:52:53 +00:00
#include <liblangutil/SourceLocation.h>
#include <libsolutil/FixedHash.h>
2017-02-15 13:52:53 +00:00
#include <memory>
#include <functional>
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-12-11 16:31:36 +00:00
EthAssemblyAdapter::EthAssemblyAdapter(evmasm::Assembly& _assembly):
2018-12-04 15:26:38 +00:00
m_assembly(_assembly)
{
}
void EthAssemblyAdapter::setSourceLocation(SourceLocation const& _location)
{
m_assembly.setSourceLocation(_location);
}
int EthAssemblyAdapter::stackHeight() const
{
return m_assembly.deposit();
}
void EthAssemblyAdapter::setStackHeight(int height)
{
m_assembly.setDeposit(height);
}
2019-12-11 16:31:36 +00:00
void EthAssemblyAdapter::appendInstruction(evmasm::Instruction _instruction)
2018-12-04 15:26:38 +00:00
{
m_assembly.append(_instruction);
}
void EthAssemblyAdapter::appendConstant(u256 const& _constant)
{
m_assembly.append(_constant);
}
void EthAssemblyAdapter::appendLabel(LabelID _labelId)
{
2019-12-11 16:31:36 +00:00
m_assembly.append(evmasm::AssemblyItem(evmasm::Tag, _labelId));
2018-12-04 15:26:38 +00:00
}
void EthAssemblyAdapter::appendLabelReference(LabelID _labelId)
{
2019-12-11 16:31:36 +00:00
m_assembly.append(evmasm::AssemblyItem(evmasm::PushTag, _labelId));
2018-12-04 15:26:38 +00:00
}
size_t EthAssemblyAdapter::newLabelId()
{
return assemblyTagToIdentifier(m_assembly.newTag());
}
size_t EthAssemblyAdapter::namedLabel(std::string const& _name)
{
return assemblyTagToIdentifier(m_assembly.namedTag(_name));
}
void EthAssemblyAdapter::appendLinkerSymbol(std::string const& _linkerSymbol)
{
m_assembly.appendLibraryAddress(_linkerSymbol);
}
void EthAssemblyAdapter::appendJump(int _stackDiffAfter)
{
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::JUMP);
2018-12-04 15:26:38 +00:00
m_assembly.adjustDeposit(_stackDiffAfter);
}
void EthAssemblyAdapter::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
{
appendLabelReference(_labelId);
appendJump(_stackDiffAfter);
}
void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId)
{
appendLabelReference(_labelId);
2019-12-11 16:31:36 +00:00
appendInstruction(evmasm::Instruction::JUMPI);
2018-12-04 15:26:38 +00:00
}
void EthAssemblyAdapter::appendBeginsub(LabelID, int)
{
// TODO we could emulate that, though
yulAssert(false, "BEGINSUB not implemented for EVM 1.0");
2018-12-04 15:26:38 +00:00
}
void EthAssemblyAdapter::appendJumpsub(LabelID, int, int)
{
// TODO we could emulate that, though
yulAssert(false, "JUMPSUB not implemented for EVM 1.0");
2018-12-04 15:26:38 +00:00
}
void EthAssemblyAdapter::appendReturnsub(int, int)
{
// TODO we could emulate that, though
yulAssert(false, "RETURNSUB not implemented for EVM 1.0");
2018-12-04 15:26:38 +00:00
}
void EthAssemblyAdapter::appendAssemblySize()
{
m_assembly.appendProgramSize();
}
pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EthAssemblyAdapter::createSubAssembly()
{
2019-12-11 16:31:36 +00:00
shared_ptr<evmasm::Assembly> assembly{make_shared<evmasm::Assembly>()};
auto sub = m_assembly.newSub(assembly);
return {make_shared<EthAssemblyAdapter>(*assembly), size_t(sub.data())};
}
void EthAssemblyAdapter::appendDataOffset(AbstractAssembly::SubID _sub)
{
auto it = m_dataHashBySubId.find(_sub);
if (it == m_dataHashBySubId.end())
m_assembly.pushSubroutineOffset(size_t(_sub));
else
2019-12-11 16:31:36 +00:00
m_assembly << evmasm::AssemblyItem(evmasm::PushData, it->second);
}
void EthAssemblyAdapter::appendDataSize(AbstractAssembly::SubID _sub)
{
auto it = m_dataHashBySubId.find(_sub);
if (it == m_dataHashBySubId.end())
m_assembly.pushSubroutineSize(size_t(_sub));
else
m_assembly << u256(m_assembly.data(h256(it->second)).size());
}
AbstractAssembly::SubID EthAssemblyAdapter::appendData(bytes const& _data)
{
2019-12-11 16:31:36 +00:00
evmasm::AssemblyItem pushData = m_assembly.newData(_data);
SubID subID = m_nextDataCounter++;
m_dataHashBySubId[subID] = pushData.data();
return subID;
}
void EthAssemblyAdapter::appendImmutable(std::string const& _identifier)
{
m_assembly.appendImmutable(_identifier);
}
void EthAssemblyAdapter::appendImmutableAssignment(std::string const& _identifier)
{
m_assembly.appendImmutableAssignment(_identifier);
}
2019-12-11 16:31:36 +00:00
EthAssemblyAdapter::LabelID EthAssemblyAdapter::assemblyTagToIdentifier(evmasm::AssemblyItem const& _tag)
2018-12-04 15:26:38 +00:00
{
u256 id = _tag.data();
yulAssert(id <= std::numeric_limits<LabelID>::max(), "Tag id too large.");
2018-12-04 15:26:38 +00:00
return LabelID(id);
}
void CodeGenerator::assemble(
2017-04-12 16:32:25 +00:00
Block const& _parsedData,
AsmAnalysisInfo& _analysisInfo,
2019-12-11 16:31:36 +00:00
evmasm::Assembly& _assembly,
langutil::EVMVersion _evmVersion,
ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions,
2019-02-26 18:55:13 +00:00
bool _optimizeStackAllocation
2017-04-12 16:32:25 +00:00
)
{
EthAssemblyAdapter assemblyAdapter(_assembly);
BuiltinContext builtinContext;
2019-01-29 09:51:25 +00:00
CodeTransform transform(
assemblyAdapter,
_analysisInfo,
_parsedData,
EVMDialect::strictAssemblyForEVM(_evmVersion),
builtinContext,
2019-02-26 18:55:13 +00:00
_optimizeStackAllocation,
2019-01-29 09:51:25 +00:00
false,
_identifierAccess,
_useNamedLabelsForFunctions
);
try
{
2019-01-29 09:51:25 +00:00
transform(_parsedData);
}
catch (StackTooDeepError const& _e)
{
yulAssert(
false,
"Stack too deep when compiling inline assembly" +
(_e.comment() ? ": " + *_e.comment() : ".")
);
}
yulAssert(transform.stackErrors().empty(), "Stack errors present but not thrown.");
}