solidity/libyul/backends/evm/EVMDialect.cpp

147 lines
4.2 KiB
C++
Raw Normal View History

2018-12-06 23:56:16 +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/>.
*/
/**
* Yul dialects for EVM.
*/
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h>
#include <libyul/Object.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <liblangutil/Exceptions.h>
#include <libyul/Exceptions.h>
#include <boost/range/adaptor/reversed.hpp>
using namespace std;
using namespace dev;
using namespace yul;
EVMDialect::EVMDialect(AsmFlavour _flavour, bool _objectAccess, langutil::EVMVersion _evmVersion):
2019-02-21 20:56:30 +00:00
Dialect{_flavour}, m_objectAccess(_objectAccess), m_evmVersion(_evmVersion)
2018-12-06 23:56:16 +00:00
{
// The EVM instructions will be moved to builtins at some point.
if (!m_objectAccess)
return;
addFunction("datasize", 1, 1, true, true, true, [](
2018-12-06 23:56:16 +00:00
FunctionCall const& _call,
AbstractAssembly& _assembly,
BuiltinContext& _context,
2018-12-06 23:56:16 +00:00
std::function<void()>
) {
yulAssert(_context.currentObject, "No object available.");
2018-12-06 23:56:16 +00:00
yulAssert(_call.arguments.size() == 1, "");
Expression const& arg = _call.arguments.front();
YulString dataName = boost::get<Literal>(arg).value;
if (_context.currentObject->name == dataName)
2018-12-06 23:56:16 +00:00
_assembly.appendAssemblySize();
else
{
yulAssert(
_context.subIDs.count(dataName) != 0,
"Could not find assembly object <" + dataName.str() + ">."
);
_assembly.appendDataSize(_context.subIDs.at(dataName));
}
2018-12-06 23:56:16 +00:00
});
addFunction("dataoffset", 1, 1, true, true, true, [](
2018-12-06 23:56:16 +00:00
FunctionCall const& _call,
AbstractAssembly& _assembly,
BuiltinContext& _context,
2018-12-06 23:56:16 +00:00
std::function<void()>
) {
yulAssert(_context.currentObject, "No object available.");
2018-12-06 23:56:16 +00:00
yulAssert(_call.arguments.size() == 1, "");
Expression const& arg = _call.arguments.front();
YulString dataName = boost::get<Literal>(arg).value;
if (_context.currentObject->name == dataName)
2018-12-06 23:56:16 +00:00
_assembly.appendConstant(0);
else
{
yulAssert(
_context.subIDs.count(dataName) != 0,
"Could not find assembly object <" + dataName.str() + ">."
);
_assembly.appendDataOffset(_context.subIDs.at(dataName));
}
2018-12-06 23:56:16 +00:00
});
addFunction("datacopy", 3, 0, false, false, false, [](
2018-12-06 23:56:16 +00:00
FunctionCall const&,
AbstractAssembly& _assembly,
BuiltinContext&,
2018-12-06 23:56:16 +00:00
std::function<void()> _visitArguments
) {
_visitArguments();
_assembly.appendInstruction(dev::eth::Instruction::CODECOPY);
2018-12-06 23:56:16 +00:00
});
}
BuiltinFunctionForEVM const* EVMDialect::builtin(YulString _name) const
{
auto it = m_functions.find(_name);
if (it != m_functions.end())
return &it->second;
else
return nullptr;
}
shared_ptr<EVMDialect> EVMDialect::looseAssemblyForEVM(langutil::EVMVersion _version)
2018-12-06 23:56:16 +00:00
{
2019-02-21 20:56:30 +00:00
return make_shared<EVMDialect>(AsmFlavour::Loose, false, _version);
2018-12-06 23:56:16 +00:00
}
shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVM(langutil::EVMVersion _version)
2018-12-06 23:56:16 +00:00
{
2019-02-21 20:56:30 +00:00
return make_shared<EVMDialect>(AsmFlavour::Strict, false, _version);
2018-12-06 23:56:16 +00:00
}
shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVMObjects(langutil::EVMVersion _version)
2018-12-06 23:56:16 +00:00
{
2019-02-21 20:56:30 +00:00
return make_shared<EVMDialect>(AsmFlavour::Strict, true, _version);
2018-12-06 23:56:16 +00:00
}
shared_ptr<yul::EVMDialect> EVMDialect::yulForEVM(langutil::EVMVersion _version)
2018-12-06 23:56:16 +00:00
{
2019-02-21 20:56:30 +00:00
return make_shared<EVMDialect>(AsmFlavour::Yul, false, _version);
2018-12-06 23:56:16 +00:00
}
void EVMDialect::addFunction(
string _name,
size_t _params,
size_t _returns,
bool _movable,
bool _sideEffectFree,
bool _literalArguments,
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&, std::function<void()>)> _generateCode
2018-12-06 23:56:16 +00:00
)
{
YulString name{std::move(_name)};
BuiltinFunctionForEVM& f = m_functions[name];
f.name = name;
f.parameters.resize(_params);
f.returns.resize(_returns);
f.movable = _movable;
f.sideEffectFree = _sideEffectFree;
f.literalArguments = _literalArguments;
2018-12-06 23:56:16 +00:00
f.generateCode = std::move(_generateCode);
}