2016-03-01 21:56:39 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-03-01 21:56:39 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-03-01 21:56:39 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2016-03-01 21:56:39 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-01 21:56:39 +00:00
|
|
|
*/
|
|
|
|
/**
|
2018-12-04 14:20:29 +00:00
|
|
|
* Adaptor between the abstract assembly and eth assembly.
|
2016-03-01 21:56:39 +00:00
|
|
|
*/
|
|
|
|
|
2019-02-13 10:35:49 +00:00
|
|
|
#include <libyul/backends/evm/AsmCodeGen.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmData.h>
|
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2018-11-23 10:31:45 +00:00
|
|
|
#include <libyul/backends/evm/AbstractAssembly.h>
|
|
|
|
#include <libyul/backends/evm/EVMCodeTransform.h>
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
#include <libevmasm/Assembly.h>
|
2018-12-04 17:57:07 +00:00
|
|
|
#include <libevmasm/AssemblyItem.h>
|
2016-04-02 12:56:43 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2018-11-23 10:31:45 +00:00
|
|
|
#include <liblangutil/SourceLocation.h>
|
2017-04-28 16:15:18 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/FixedHash.h>
|
2018-12-04 17:57:07 +00:00
|
|
|
|
2017-02-15 13:52:53 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
2016-03-01 21:56:39 +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;
|
2016-03-01 21:56:39 +00:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-05-25 10:37:51 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:22:01 +00:00
|
|
|
void EthAssemblyAdapter::appendJump(int _stackDiffAfter, JumpType _jumpType)
|
2018-12-04 15:26:38 +00:00
|
|
|
{
|
2020-06-22 12:22:01 +00:00
|
|
|
appendJumpInstruction(evmasm::Instruction::JUMP, _jumpType);
|
2018-12-04 15:26:38 +00:00
|
|
|
m_assembly.adjustDeposit(_stackDiffAfter);
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:22:01 +00:00
|
|
|
void EthAssemblyAdapter::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType)
|
2018-12-04 15:26:38 +00:00
|
|
|
{
|
|
|
|
appendLabelReference(_labelId);
|
2020-06-22 12:22:01 +00:00
|
|
|
appendJump(_stackDiffAfter, _jumpType);
|
2018-12-04 15:26:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:22:01 +00:00
|
|
|
void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId, JumpType _jumpType)
|
2018-12-04 15:26:38 +00:00
|
|
|
{
|
|
|
|
appendLabelReference(_labelId);
|
2020-06-22 12:22:01 +00:00
|
|
|
appendJumpInstruction(evmasm::Instruction::JUMPI, _jumpType);
|
2018-12-04 15:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EthAssemblyAdapter::appendBeginsub(LabelID, int)
|
|
|
|
{
|
|
|
|
// TODO we could emulate that, though
|
2019-11-26 21:52:09 +00:00
|
|
|
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
|
2019-11-26 21:52:09 +00:00
|
|
|
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
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(false, "RETURNSUB not implemented for EVM 1.0");
|
2018-12-04 15:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EthAssemblyAdapter::appendAssemblySize()
|
|
|
|
{
|
|
|
|
m_assembly.appendProgramSize();
|
|
|
|
}
|
|
|
|
|
2018-12-04 17:57:07 +00:00
|
|
|
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>()};
|
2018-12-04 17:57:07 +00:00
|
|
|
auto sub = m_assembly.newSub(assembly);
|
2020-06-02 13:34:28 +00:00
|
|
|
return {make_shared<EthAssemblyAdapter>(*assembly), static_cast<size_t>(sub.data())};
|
2018-12-04 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EthAssemblyAdapter::appendDataOffset(AbstractAssembly::SubID _sub)
|
|
|
|
{
|
|
|
|
auto it = m_dataHashBySubId.find(_sub);
|
|
|
|
if (it == m_dataHashBySubId.end())
|
2020-06-02 13:34:28 +00:00
|
|
|
m_assembly.pushSubroutineOffset(_sub);
|
2018-12-04 17:57:07 +00:00
|
|
|
else
|
2019-12-11 16:31:36 +00:00
|
|
|
m_assembly << evmasm::AssemblyItem(evmasm::PushData, it->second);
|
2018-12-04 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EthAssemblyAdapter::appendDataSize(AbstractAssembly::SubID _sub)
|
|
|
|
{
|
|
|
|
auto it = m_dataHashBySubId.find(_sub);
|
|
|
|
if (it == m_dataHashBySubId.end())
|
2020-06-02 13:34:28 +00:00
|
|
|
m_assembly.pushSubroutineSize(static_cast<size_t>(_sub));
|
2018-12-04 17:57:07 +00:00
|
|
|
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);
|
2018-12-04 17:57:07 +00:00
|
|
|
SubID subID = m_nextDataCounter++;
|
|
|
|
m_dataHashBySubId[subID] = pushData.data();
|
|
|
|
return subID;
|
|
|
|
}
|
|
|
|
|
2020-04-14 08:52:27 +00:00
|
|
|
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();
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(id <= std::numeric_limits<LabelID>::max(), "Tag id too large.");
|
2018-12-04 15:26:38 +00:00
|
|
|
return LabelID(id);
|
|
|
|
}
|
2016-03-01 21:56:39 +00:00
|
|
|
|
2020-06-22 12:22:01 +00:00
|
|
|
void EthAssemblyAdapter::appendJumpInstruction(evmasm::Instruction _instruction, JumpType _jumpType)
|
|
|
|
{
|
|
|
|
yulAssert(_instruction == evmasm::Instruction::JUMP || _instruction == evmasm::Instruction::JUMPI, "");
|
|
|
|
evmasm::AssemblyItem jump(_instruction);
|
|
|
|
switch (_jumpType)
|
|
|
|
{
|
|
|
|
case JumpType::Ordinary:
|
|
|
|
yulAssert(jump.getJumpType() == evmasm::AssemblyItem::JumpType::Ordinary, "");
|
|
|
|
break;
|
|
|
|
case JumpType::IntoFunction:
|
|
|
|
jump.setJumpType(evmasm::AssemblyItem::JumpType::IntoFunction);
|
|
|
|
break;
|
|
|
|
case JumpType::OutOfFunction:
|
|
|
|
jump.setJumpType(evmasm::AssemblyItem::JumpType::OutOfFunction);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_assembly.append(std::move(jump));
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:42:34 +00:00
|
|
|
void CodeGenerator::assemble(
|
2017-04-12 16:32:25 +00:00
|
|
|
Block const& _parsedData,
|
2017-04-26 13:41:08 +00:00
|
|
|
AsmAnalysisInfo& _analysisInfo,
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::Assembly& _assembly,
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion _evmVersion,
|
2018-11-21 11:42:34 +00:00
|
|
|
ExternalIdentifierAccess const& _identifierAccess,
|
2018-09-25 02:47:25 +00:00
|
|
|
bool _useNamedLabelsForFunctions,
|
2019-02-26 18:55:13 +00:00
|
|
|
bool _optimizeStackAllocation
|
2017-04-12 16:32:25 +00:00
|
|
|
)
|
2016-05-08 14:24:47 +00:00
|
|
|
{
|
2017-04-28 16:15:18 +00:00
|
|
|
EthAssemblyAdapter assemblyAdapter(_assembly);
|
2019-05-15 21:40:30 +00:00
|
|
|
BuiltinContext builtinContext;
|
2019-01-29 09:51:25 +00:00
|
|
|
CodeTransform transform(
|
|
|
|
assemblyAdapter,
|
|
|
|
_analysisInfo,
|
|
|
|
_parsedData,
|
2019-05-16 08:56:56 +00:00
|
|
|
EVMDialect::strictAssemblyForEVM(_evmVersion),
|
2019-05-15 21:40:30 +00:00
|
|
|
builtinContext,
|
2019-02-26 18:55:13 +00:00
|
|
|
_optimizeStackAllocation,
|
2019-01-29 09:51:25 +00:00
|
|
|
false,
|
|
|
|
_identifierAccess,
|
|
|
|
_useNamedLabelsForFunctions
|
|
|
|
);
|
2019-01-28 16:19:41 +00:00
|
|
|
try
|
|
|
|
{
|
2019-01-29 09:51:25 +00:00
|
|
|
transform(_parsedData);
|
2019-01-28 16:19:41 +00:00
|
|
|
}
|
|
|
|
catch (StackTooDeepError const& _e)
|
|
|
|
{
|
2020-07-01 07:46:06 +00:00
|
|
|
assertThrow(
|
2019-11-22 17:02:12 +00:00
|
|
|
false,
|
2020-07-01 07:46:06 +00:00
|
|
|
langutil::StackTooDeepError,
|
2019-11-22 17:02:12 +00:00
|
|
|
"Stack too deep when compiling inline assembly" +
|
|
|
|
(_e.comment() ? ": " + *_e.comment() : ".")
|
|
|
|
);
|
2019-01-28 16:19:41 +00:00
|
|
|
}
|
2019-11-26 21:52:09 +00:00
|
|
|
yulAssert(transform.stackErrors().empty(), "Stack errors present but not thrown.");
|
2016-03-01 21:56:39 +00:00
|
|
|
}
|