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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-12-06 23:56:16 +00:00
|
|
|
/**
|
|
|
|
* Yul dialects for EVM.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
|
|
|
|
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-12-06 23:56:16 +00:00
|
|
|
#include <libyul/Object.h>
|
2019-05-16 09:43:33 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
|
|
|
#include <libyul/AsmParser.h>
|
2021-09-03 13:55:17 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2018-12-06 23:56:16 +00:00
|
|
|
#include <libyul/backends/evm/AbstractAssembly.h>
|
2019-05-16 09:43:33 +00:00
|
|
|
#include <libevmasm/SemanticInformation.h>
|
|
|
|
#include <libevmasm/Instruction.h>
|
2018-12-06 23:56:16 +00:00
|
|
|
|
2019-05-16 09:43:33 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2018-12-06 23:56:16 +00:00
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/reverse.hpp>
|
2021-03-17 18:37:39 +00:00
|
|
|
#include <range/v3/view/tail.hpp>
|
|
|
|
|
|
|
|
#include <regex>
|
2018-12-06 23:56:16 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
2018-12-06 23:56:16 +00:00
|
|
|
|
2019-05-16 08:18:08 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-04-14 08:52:27 +00:00
|
|
|
|
2019-05-16 09:43:33 +00:00
|
|
|
pair<YulString, BuiltinFunctionForEVM> createEVMFunction(
|
|
|
|
string const& _name,
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::Instruction _instruction
|
2019-05-16 09:43:33 +00:00
|
|
|
)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::InstructionInfo info = evmasm::instructionInfo(_instruction);
|
2019-05-16 09:43:33 +00:00
|
|
|
BuiltinFunctionForEVM f;
|
|
|
|
f.name = YulString{_name};
|
2020-06-02 13:34:28 +00:00
|
|
|
f.parameters.resize(static_cast<size_t>(info.args));
|
|
|
|
f.returns.resize(static_cast<size_t>(info.ret));
|
2019-08-13 12:40:26 +00:00
|
|
|
f.sideEffects = EVMDialect::sideEffectsOfInstruction(_instruction);
|
2020-03-05 09:47:01 +00:00
|
|
|
f.controlFlowSideEffects.terminates = evmasm::SemanticInformation::terminatesControlFlow(_instruction);
|
|
|
|
f.controlFlowSideEffects.reverts = evmasm::SemanticInformation::reverts(_instruction);
|
2019-12-11 16:31:36 +00:00
|
|
|
f.isMSize = _instruction == evmasm::Instruction::MSIZE;
|
2020-08-04 15:30:16 +00:00
|
|
|
f.literalArguments.clear();
|
2019-05-16 19:19:50 +00:00
|
|
|
f.instruction = _instruction;
|
2019-05-16 09:43:33 +00:00
|
|
|
f.generateCode = [_instruction](
|
2021-09-03 13:55:17 +00:00
|
|
|
FunctionCall const&,
|
2019-05-16 09:43:33 +00:00
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2019-05-16 09:43:33 +00:00
|
|
|
) {
|
|
|
|
_assembly.appendInstruction(_instruction);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {f.name, move(f)};
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:18:08 +00:00
|
|
|
pair<YulString, BuiltinFunctionForEVM> createFunction(
|
|
|
|
string _name,
|
|
|
|
size_t _params,
|
|
|
|
size_t _returns,
|
2019-08-13 12:40:26 +00:00
|
|
|
SideEffects _sideEffects,
|
2020-08-04 15:30:16 +00:00
|
|
|
vector<optional<LiteralKind>> _literalArguments,
|
2021-09-03 13:55:17 +00:00
|
|
|
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&)> _generateCode
|
2019-05-16 08:18:08 +00:00
|
|
|
)
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(_literalArguments.size() == _params || _literalArguments.empty(), "");
|
2020-04-06 12:47:44 +00:00
|
|
|
|
2019-05-16 08:18:08 +00:00
|
|
|
YulString name{std::move(_name)};
|
|
|
|
BuiltinFunctionForEVM f;
|
|
|
|
f.name = name;
|
|
|
|
f.parameters.resize(_params);
|
|
|
|
f.returns.resize(_returns);
|
2019-08-13 12:40:26 +00:00
|
|
|
f.sideEffects = std::move(_sideEffects);
|
2020-08-04 15:30:16 +00:00
|
|
|
f.literalArguments = std::move(_literalArguments);
|
2019-05-27 11:11:00 +00:00
|
|
|
f.isMSize = false;
|
2019-05-16 19:19:50 +00:00
|
|
|
f.instruction = {};
|
2019-05-16 08:18:08 +00:00
|
|
|
f.generateCode = std::move(_generateCode);
|
|
|
|
return {name, f};
|
|
|
|
}
|
|
|
|
|
2021-08-09 10:40:14 +00:00
|
|
|
set<YulString> createReservedIdentifiers(langutil::EVMVersion _evmVersion)
|
2020-07-29 17:42:15 +00:00
|
|
|
{
|
2021-08-09 10:40:14 +00:00
|
|
|
// TODO remove this in 0.9.0. We allow creating functions or identifiers in Yul with the name
|
|
|
|
// basefee for VMs before london.
|
|
|
|
auto baseFeeException = [&](evmasm::Instruction _instr) -> bool
|
|
|
|
{
|
|
|
|
return _instr == evmasm::Instruction::BASEFEE && _evmVersion < langutil::EVMVersion::london();
|
|
|
|
};
|
|
|
|
|
2020-07-29 17:42:15 +00:00
|
|
|
set<YulString> reserved;
|
|
|
|
for (auto const& instr: evmasm::c_instructions)
|
|
|
|
{
|
|
|
|
string name = instr.first;
|
|
|
|
transform(name.begin(), name.end(), name.begin(), [](unsigned char _c) { return tolower(_c); });
|
2021-08-09 10:40:14 +00:00
|
|
|
if (!baseFeeException(instr.second))
|
|
|
|
reserved.emplace(name);
|
2020-07-29 17:42:15 +00:00
|
|
|
}
|
|
|
|
reserved += vector<YulString>{
|
|
|
|
"linkersymbol"_yulstring,
|
|
|
|
"datasize"_yulstring,
|
|
|
|
"dataoffset"_yulstring,
|
|
|
|
"datacopy"_yulstring,
|
|
|
|
"setimmutable"_yulstring,
|
|
|
|
"loadimmutable"_yulstring,
|
|
|
|
};
|
|
|
|
return reserved;
|
|
|
|
}
|
|
|
|
|
2019-05-22 11:57:48 +00:00
|
|
|
map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVersion, bool _objectAccess)
|
2019-05-16 08:18:08 +00:00
|
|
|
{
|
|
|
|
map<YulString, BuiltinFunctionForEVM> builtins;
|
2020-07-06 15:59:51 +00:00
|
|
|
for (auto const& instr: evmasm::c_instructions)
|
|
|
|
{
|
|
|
|
string name = instr.first;
|
|
|
|
transform(name.begin(), name.end(), name.begin(), [](unsigned char _c) { return tolower(_c); });
|
|
|
|
auto const opcode = instr.second;
|
|
|
|
|
2019-05-22 11:57:48 +00:00
|
|
|
if (
|
2020-07-06 15:59:51 +00:00
|
|
|
!evmasm::isDupInstruction(opcode) &&
|
|
|
|
!evmasm::isSwapInstruction(opcode) &&
|
|
|
|
!evmasm::isPushInstruction(opcode) &&
|
|
|
|
opcode != evmasm::Instruction::JUMP &&
|
|
|
|
opcode != evmasm::Instruction::JUMPI &&
|
|
|
|
opcode != evmasm::Instruction::JUMPDEST &&
|
|
|
|
_evmVersion.hasOpcode(opcode)
|
2019-05-22 11:57:48 +00:00
|
|
|
)
|
2020-07-06 15:59:51 +00:00
|
|
|
builtins.emplace(createEVMFunction(name, opcode));
|
|
|
|
}
|
2019-05-16 09:43:33 +00:00
|
|
|
|
2019-05-16 08:18:08 +00:00
|
|
|
if (_objectAccess)
|
|
|
|
{
|
2020-08-04 15:30:16 +00:00
|
|
|
builtins.emplace(createFunction("linkersymbol", 1, 1, SideEffects{}, {LiteralKind::String}, [](
|
2020-07-02 12:14:36 +00:00
|
|
|
FunctionCall const& _call,
|
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2020-07-02 12:14:36 +00:00
|
|
|
) {
|
|
|
|
yulAssert(_call.arguments.size() == 1, "");
|
|
|
|
Expression const& arg = _call.arguments.front();
|
|
|
|
_assembly.appendLinkerSymbol(std::get<Literal>(arg).value.str());
|
|
|
|
}));
|
2020-07-02 10:48:20 +00:00
|
|
|
|
|
|
|
builtins.emplace(createFunction(
|
|
|
|
"memoryguard",
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
SideEffects{},
|
|
|
|
{LiteralKind::Number},
|
|
|
|
[](
|
|
|
|
FunctionCall const& _call,
|
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2020-07-02 10:48:20 +00:00
|
|
|
) {
|
2021-09-03 13:55:17 +00:00
|
|
|
yulAssert(_call.arguments.size() == 1, "");
|
|
|
|
Literal const* literal = get_if<Literal>(&_call.arguments.front());
|
|
|
|
yulAssert(literal, "");
|
|
|
|
_assembly.appendConstant(valueOfLiteral(*literal));
|
2020-07-02 10:48:20 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-08-04 15:30:16 +00:00
|
|
|
builtins.emplace(createFunction("datasize", 1, 1, SideEffects{}, {LiteralKind::String}, [](
|
2019-05-16 08:18:08 +00:00
|
|
|
FunctionCall const& _call,
|
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext& _context
|
2019-05-16 08:18:08 +00:00
|
|
|
) {
|
|
|
|
yulAssert(_context.currentObject, "No object available.");
|
|
|
|
yulAssert(_call.arguments.size() == 1, "");
|
|
|
|
Expression const& arg = _call.arguments.front();
|
2019-11-19 15:42:49 +00:00
|
|
|
YulString dataName = std::get<Literal>(arg).value;
|
2019-05-16 08:18:08 +00:00
|
|
|
if (_context.currentObject->name == dataName)
|
|
|
|
_assembly.appendAssemblySize();
|
|
|
|
else
|
|
|
|
{
|
2020-06-17 09:17:35 +00:00
|
|
|
vector<size_t> subIdPath =
|
|
|
|
_context.subIDs.count(dataName) == 0 ?
|
|
|
|
_context.currentObject->pathToSubObject(dataName) :
|
|
|
|
vector<size_t>{_context.subIDs.at(dataName)};
|
|
|
|
yulAssert(!subIdPath.empty(), "Could not find assembly object <" + dataName.str() + ">.");
|
|
|
|
_assembly.appendDataSize(subIdPath);
|
2019-05-16 08:18:08 +00:00
|
|
|
}
|
|
|
|
}));
|
2020-08-04 15:30:16 +00:00
|
|
|
builtins.emplace(createFunction("dataoffset", 1, 1, SideEffects{}, {LiteralKind::String}, [](
|
2019-05-16 08:18:08 +00:00
|
|
|
FunctionCall const& _call,
|
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext& _context
|
2019-05-16 08:18:08 +00:00
|
|
|
) {
|
|
|
|
yulAssert(_context.currentObject, "No object available.");
|
|
|
|
yulAssert(_call.arguments.size() == 1, "");
|
|
|
|
Expression const& arg = _call.arguments.front();
|
2019-11-19 15:42:49 +00:00
|
|
|
YulString dataName = std::get<Literal>(arg).value;
|
2019-05-16 08:18:08 +00:00
|
|
|
if (_context.currentObject->name == dataName)
|
|
|
|
_assembly.appendConstant(0);
|
|
|
|
else
|
|
|
|
{
|
2020-06-17 09:17:35 +00:00
|
|
|
vector<size_t> subIdPath =
|
|
|
|
_context.subIDs.count(dataName) == 0 ?
|
|
|
|
_context.currentObject->pathToSubObject(dataName) :
|
|
|
|
vector<size_t>{_context.subIDs.at(dataName)};
|
|
|
|
yulAssert(!subIdPath.empty(), "Could not find assembly object <" + dataName.str() + ">.");
|
|
|
|
_assembly.appendDataOffset(subIdPath);
|
2019-05-16 08:18:08 +00:00
|
|
|
}
|
|
|
|
}));
|
2019-08-13 12:40:26 +00:00
|
|
|
builtins.emplace(createFunction(
|
|
|
|
"datacopy",
|
|
|
|
3,
|
|
|
|
0,
|
2020-07-01 10:07:00 +00:00
|
|
|
SideEffects{false, true, false, false, true, SideEffects::None, SideEffects::None, SideEffects::Write},
|
2020-04-06 12:47:44 +00:00
|
|
|
{},
|
2019-08-13 12:40:26 +00:00
|
|
|
[](
|
2021-09-03 13:55:17 +00:00
|
|
|
FunctionCall const&,
|
2019-08-13 12:40:26 +00:00
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2019-08-13 12:40:26 +00:00
|
|
|
) {
|
2019-12-11 16:31:36 +00:00
|
|
|
_assembly.appendInstruction(evmasm::Instruction::CODECOPY);
|
2019-08-13 12:40:26 +00:00
|
|
|
}
|
|
|
|
));
|
2020-04-14 08:52:27 +00:00
|
|
|
builtins.emplace(createFunction(
|
|
|
|
"setimmutable",
|
2020-10-14 11:20:20 +00:00
|
|
|
3,
|
2020-04-14 08:52:27 +00:00
|
|
|
0,
|
2020-07-01 10:07:00 +00:00
|
|
|
SideEffects{false, false, false, false, true, SideEffects::None, SideEffects::None, SideEffects::Write},
|
2020-10-14 11:20:20 +00:00
|
|
|
{std::nullopt, LiteralKind::String, std::nullopt},
|
2020-04-14 08:52:27 +00:00
|
|
|
[](
|
|
|
|
FunctionCall const& _call,
|
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2020-04-14 08:52:27 +00:00
|
|
|
) {
|
2020-10-14 11:20:20 +00:00
|
|
|
yulAssert(_call.arguments.size() == 3, "");
|
|
|
|
YulString identifier = std::get<Literal>(_call.arguments[1]).value;
|
2020-04-14 08:52:27 +00:00
|
|
|
_assembly.appendImmutableAssignment(identifier.str());
|
|
|
|
}
|
|
|
|
));
|
|
|
|
builtins.emplace(createFunction(
|
|
|
|
"loadimmutable",
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
SideEffects{},
|
2020-08-04 15:30:16 +00:00
|
|
|
{LiteralKind::String},
|
2020-04-14 08:52:27 +00:00
|
|
|
[](
|
|
|
|
FunctionCall const& _call,
|
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2020-04-14 08:52:27 +00:00
|
|
|
) {
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(_call.arguments.size() == 1, "");
|
2020-04-14 08:52:27 +00:00
|
|
|
_assembly.appendImmutable(std::get<Literal>(_call.arguments.front()).value.str());
|
|
|
|
}
|
|
|
|
));
|
2019-05-16 08:18:08 +00:00
|
|
|
}
|
|
|
|
return builtins;
|
|
|
|
}
|
|
|
|
|
2021-03-17 18:37:39 +00:00
|
|
|
regex const& verbatimPattern()
|
|
|
|
{
|
|
|
|
regex static const pattern{"verbatim_([1-9]?[0-9])i_([1-9]?[0-9])o"};
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:18:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 08:52:27 +00:00
|
|
|
|
2019-12-19 16:58:20 +00:00
|
|
|
EVMDialect::EVMDialect(langutil::EVMVersion _evmVersion, bool _objectAccess):
|
2019-05-16 08:18:08 +00:00
|
|
|
m_objectAccess(_objectAccess),
|
|
|
|
m_evmVersion(_evmVersion),
|
2020-07-29 17:42:15 +00:00
|
|
|
m_functions(createBuiltins(_evmVersion, _objectAccess)),
|
2021-08-09 10:40:14 +00:00
|
|
|
m_reserved(createReservedIdentifiers(_evmVersion))
|
2018-12-06 23:56:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BuiltinFunctionForEVM const* EVMDialect::builtin(YulString _name) const
|
|
|
|
{
|
2021-03-17 18:37:39 +00:00
|
|
|
if (m_objectAccess)
|
|
|
|
{
|
|
|
|
smatch match;
|
|
|
|
if (regex_match(_name.str(), match, verbatimPattern()))
|
|
|
|
return verbatimFunction(stoul(match[1]), stoul(match[2]));
|
|
|
|
}
|
2018-12-06 23:56:16 +00:00
|
|
|
auto it = m_functions.find(_name);
|
|
|
|
if (it != m_functions.end())
|
|
|
|
return &it->second;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:42:15 +00:00
|
|
|
bool EVMDialect::reservedIdentifier(YulString _name) const
|
|
|
|
{
|
2021-03-17 18:37:39 +00:00
|
|
|
if (m_objectAccess)
|
|
|
|
if (_name.str().substr(0, "verbatim"s.size()) == "verbatim")
|
|
|
|
return true;
|
2020-07-29 17:42:15 +00:00
|
|
|
return m_reserved.count(_name) != 0;
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
EVMDialect const& EVMDialect::strictAssemblyForEVM(langutil::EVMVersion _version)
|
2018-12-06 23:56:16 +00:00
|
|
|
{
|
2019-05-16 08:56:56 +00:00
|
|
|
static map<langutil::EVMVersion, unique_ptr<EVMDialect const>> dialects;
|
2019-04-24 12:03:09 +00:00
|
|
|
static YulStringRepository::ResetCallback callback{[&] { dialects.clear(); }};
|
2019-05-16 08:56:56 +00:00
|
|
|
if (!dialects[_version])
|
2019-12-19 16:58:20 +00:00
|
|
|
dialects[_version] = make_unique<EVMDialect>(_version, false);
|
2019-05-16 08:56:56 +00:00
|
|
|
return *dialects[_version];
|
2018-12-06 23:56:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
EVMDialect const& EVMDialect::strictAssemblyForEVMObjects(langutil::EVMVersion _version)
|
2018-12-06 23:56:16 +00:00
|
|
|
{
|
2019-05-16 08:56:56 +00:00
|
|
|
static map<langutil::EVMVersion, unique_ptr<EVMDialect const>> dialects;
|
2019-04-24 12:03:09 +00:00
|
|
|
static YulStringRepository::ResetCallback callback{[&] { dialects.clear(); }};
|
2019-05-16 08:56:56 +00:00
|
|
|
if (!dialects[_version])
|
2019-12-19 16:58:20 +00:00
|
|
|
dialects[_version] = make_unique<EVMDialect>(_version, true);
|
2019-05-16 08:56:56 +00:00
|
|
|
return *dialects[_version];
|
2018-12-06 23:56:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
SideEffects EVMDialect::sideEffectsOfInstruction(evmasm::Instruction _instruction)
|
2019-08-13 12:40:26 +00:00
|
|
|
{
|
2020-07-01 10:07:00 +00:00
|
|
|
auto translate = [](evmasm::SemanticInformation::Effect _e) -> SideEffects::Effect
|
|
|
|
{
|
|
|
|
return static_cast<SideEffects::Effect>(_e);
|
|
|
|
};
|
|
|
|
|
2019-08-13 12:40:26 +00:00
|
|
|
return SideEffects{
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::SemanticInformation::movable(_instruction),
|
2020-07-01 10:07:00 +00:00
|
|
|
evmasm::SemanticInformation::movableApartFromEffects(_instruction),
|
|
|
|
evmasm::SemanticInformation::canBeRemoved(_instruction),
|
|
|
|
evmasm::SemanticInformation::canBeRemovedIfNoMSize(_instruction),
|
|
|
|
true, // cannotLoop
|
|
|
|
translate(evmasm::SemanticInformation::otherState(_instruction)),
|
|
|
|
translate(evmasm::SemanticInformation::storage(_instruction)),
|
|
|
|
translate(evmasm::SemanticInformation::memory(_instruction)),
|
2019-08-13 12:40:26 +00:00
|
|
|
};
|
|
|
|
}
|
2019-12-19 15:01:28 +00:00
|
|
|
|
2021-03-17 18:37:39 +00:00
|
|
|
BuiltinFunctionForEVM const* EVMDialect::verbatimFunction(size_t _arguments, size_t _returnVariables) const
|
|
|
|
{
|
|
|
|
pair<size_t, size_t> key{_arguments, _returnVariables};
|
|
|
|
shared_ptr<BuiltinFunctionForEVM const>& function = m_verbatimFunctions[key];
|
|
|
|
if (!function)
|
|
|
|
{
|
|
|
|
BuiltinFunctionForEVM builtinFunction = createFunction(
|
|
|
|
"verbatim_" + to_string(_arguments) + "i_" + to_string(_returnVariables) + "o",
|
|
|
|
1 + _arguments,
|
|
|
|
_returnVariables,
|
|
|
|
SideEffects::worst(),
|
|
|
|
vector<optional<LiteralKind>>{LiteralKind::String} + vector<optional<LiteralKind>>(_arguments),
|
|
|
|
[=](
|
|
|
|
FunctionCall const& _call,
|
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2021-03-17 18:37:39 +00:00
|
|
|
) {
|
|
|
|
yulAssert(_call.arguments.size() == (1 + _arguments), "");
|
|
|
|
Expression const& bytecode = _call.arguments.front();
|
2021-07-06 10:23:12 +00:00
|
|
|
|
2021-04-20 11:20:26 +00:00
|
|
|
_assembly.appendVerbatim(
|
|
|
|
asBytes(std::get<Literal>(bytecode).value.str()),
|
|
|
|
_arguments,
|
|
|
|
_returnVariables
|
|
|
|
);
|
2021-03-17 18:37:39 +00:00
|
|
|
}
|
|
|
|
).second;
|
|
|
|
builtinFunction.isMSize = true;
|
|
|
|
function = make_shared<BuiltinFunctionForEVM const>(move(builtinFunction));
|
|
|
|
}
|
|
|
|
return function.get();
|
|
|
|
}
|
|
|
|
|
2019-12-19 15:01:28 +00:00
|
|
|
EVMDialectTyped::EVMDialectTyped(langutil::EVMVersion _evmVersion, bool _objectAccess):
|
|
|
|
EVMDialect(_evmVersion, _objectAccess)
|
|
|
|
{
|
|
|
|
defaultType = "u256"_yulstring;
|
2020-01-16 11:03:19 +00:00
|
|
|
boolType = "bool"_yulstring;
|
|
|
|
types = {defaultType, boolType};
|
|
|
|
|
2020-02-11 15:54:09 +00:00
|
|
|
// Set all types to ``defaultType``
|
|
|
|
for (auto& fun: m_functions)
|
|
|
|
{
|
|
|
|
for (auto& p: fun.second.parameters)
|
|
|
|
p = defaultType;
|
|
|
|
for (auto& r: fun.second.returns)
|
|
|
|
r = defaultType;
|
|
|
|
}
|
|
|
|
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["lt"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["gt"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["slt"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["sgt"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["eq"_yulstring].returns = {"bool"_yulstring};
|
2020-01-29 17:45:35 +00:00
|
|
|
|
|
|
|
// "not" and "bitnot" replace "iszero" and "not"
|
|
|
|
m_functions["bitnot"_yulstring] = m_functions["not"_yulstring];
|
|
|
|
m_functions["bitnot"_yulstring].name = "bitnot"_yulstring;
|
|
|
|
m_functions["not"_yulstring] = m_functions["iszero"_yulstring];
|
|
|
|
m_functions["not"_yulstring].name = "not"_yulstring;
|
|
|
|
m_functions["not"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["not"_yulstring].parameters = {"bool"_yulstring};
|
|
|
|
m_functions.erase("iszero"_yulstring);
|
|
|
|
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["bitand"_yulstring] = m_functions["and"_yulstring];
|
2020-01-29 17:45:35 +00:00
|
|
|
m_functions["bitand"_yulstring].name = "bitand"_yulstring;
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["bitor"_yulstring] = m_functions["or"_yulstring];
|
2020-01-29 17:45:35 +00:00
|
|
|
m_functions["bitor"_yulstring].name = "bitor"_yulstring;
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["bitxor"_yulstring] = m_functions["xor"_yulstring];
|
2020-01-29 17:45:35 +00:00
|
|
|
m_functions["bitxor"_yulstring].name = "bitxor"_yulstring;
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["and"_yulstring].parameters = {"bool"_yulstring, "bool"_yulstring};
|
|
|
|
m_functions["and"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["or"_yulstring].parameters = {"bool"_yulstring, "bool"_yulstring};
|
|
|
|
m_functions["or"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["xor"_yulstring].parameters = {"bool"_yulstring, "bool"_yulstring};
|
|
|
|
m_functions["xor"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
m_functions["popbool"_yulstring] = m_functions["pop"_yulstring];
|
2020-01-29 17:45:35 +00:00
|
|
|
m_functions["popbool"_yulstring].name = "popbool"_yulstring;
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["popbool"_yulstring].parameters = {"bool"_yulstring};
|
2020-04-06 12:47:44 +00:00
|
|
|
m_functions.insert(createFunction("bool_to_u256", 1, 1, {}, {}, [](
|
2021-09-03 13:55:17 +00:00
|
|
|
FunctionCall const&,
|
|
|
|
AbstractAssembly&,
|
|
|
|
BuiltinContext&
|
|
|
|
) {}));
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["bool_to_u256"_yulstring].parameters = {"bool"_yulstring};
|
2020-02-11 15:54:09 +00:00
|
|
|
m_functions["bool_to_u256"_yulstring].returns = {"u256"_yulstring};
|
2020-04-06 12:47:44 +00:00
|
|
|
m_functions.insert(createFunction("u256_to_bool", 1, 1, {}, {}, [](
|
2021-09-03 13:55:17 +00:00
|
|
|
FunctionCall const&,
|
2019-12-19 15:01:28 +00:00
|
|
|
AbstractAssembly& _assembly,
|
2021-09-03 13:55:17 +00:00
|
|
|
BuiltinContext&
|
2019-12-19 15:01:28 +00:00
|
|
|
) {
|
2020-10-12 14:01:45 +00:00
|
|
|
// TODO this should use a Panic.
|
2020-01-29 17:45:47 +00:00
|
|
|
// A value larger than 1 causes an invalid instruction.
|
|
|
|
_assembly.appendConstant(2);
|
|
|
|
_assembly.appendInstruction(evmasm::Instruction::DUP2);
|
|
|
|
_assembly.appendInstruction(evmasm::Instruction::LT);
|
|
|
|
AbstractAssembly::LabelID inRange = _assembly.newLabelId();
|
|
|
|
_assembly.appendJumpToIf(inRange);
|
|
|
|
_assembly.appendInstruction(evmasm::Instruction::INVALID);
|
|
|
|
_assembly.appendLabel(inRange);
|
2019-12-19 15:01:28 +00:00
|
|
|
}));
|
2020-02-11 15:54:09 +00:00
|
|
|
m_functions["u256_to_bool"_yulstring].parameters = {"u256"_yulstring};
|
2019-12-19 15:01:28 +00:00
|
|
|
m_functions["u256_to_bool"_yulstring].returns = {"bool"_yulstring};
|
|
|
|
}
|
|
|
|
|
2020-02-18 23:18:13 +00:00
|
|
|
BuiltinFunctionForEVM const* EVMDialectTyped::discardFunction(YulString _type) const
|
|
|
|
{
|
|
|
|
if (_type == "bool"_yulstring)
|
|
|
|
return builtin("popbool"_yulstring);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yulAssert(_type == defaultType, "");
|
|
|
|
return builtin("pop"_yulstring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BuiltinFunctionForEVM const* EVMDialectTyped::equalityFunction(YulString _type) const
|
|
|
|
{
|
|
|
|
if (_type == "bool"_yulstring)
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yulAssert(_type == defaultType, "");
|
|
|
|
return builtin("eq"_yulstring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 15:01:28 +00:00
|
|
|
EVMDialectTyped const& EVMDialectTyped::instance(langutil::EVMVersion _version)
|
|
|
|
{
|
|
|
|
static map<langutil::EVMVersion, unique_ptr<EVMDialectTyped const>> dialects;
|
|
|
|
static YulStringRepository::ResetCallback callback{[&] { dialects.clear(); }};
|
|
|
|
if (!dialects[_version])
|
|
|
|
dialects[_version] = make_unique<EVMDialectTyped>(_version, true);
|
|
|
|
return *dialects[_version];
|
|
|
|
}
|