Add EVM instructions as builtin functions.

This commit is contained in:
chriseth 2019-05-16 11:43:33 +02:00
parent e08f521b7e
commit 003c170989
4 changed files with 92 additions and 4 deletions

View File

@ -6,6 +6,7 @@ set(sources
ErrorReporter.cpp
ErrorReporter.h
EVMVersion.h
EVMVersion.cpp
Exceptions.cpp
Exceptions.h
ParserBase.cpp

View File

@ -0,0 +1,47 @@
/*
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/>.
*/
/**
* EVM versioning.
*/
#include <liblangutil/EVMVersion.h>
using namespace langutil;
using namespace dev::eth;
bool EVMVersion::hasOpcode(Instruction _opcode) const
{
switch (_opcode)
{
case Instruction::RETURNDATACOPY:
case Instruction::RETURNDATASIZE:
return supportsReturndata();
case Instruction::STATICCALL:
return hasStaticCall();
case Instruction::SHL:
case Instruction::SHR:
case Instruction::SAR:
return hasBitwiseShifting();
case Instruction::CREATE2:
return hasCreate2();
case Instruction::EXTCODEHASH:
return hasExtCodeHash();
default:
return true;
}
}

View File

@ -20,11 +20,14 @@
#pragma once
#include <libevmasm/Instruction.h>
#include <string>
#include <boost/optional.hpp>
#include <boost/operators.hpp>
namespace langutil
{
@ -78,6 +81,8 @@ public:
bool hasCreate2() const { return *this >= constantinople(); }
bool hasExtCodeHash() const { return *this >= constantinople(); }
bool hasOpcode(dev::eth::Instruction _opcode) const;
/// Whether we have to retain the costs for the call opcode itself (false),
/// or whether we can just forward easily all remaining gas (true).
bool canOverchargeGasForCall() const { return *this >= tangerineWhistle(); }
@ -90,5 +95,4 @@ private:
Version m_version = Version::Petersburg;
};
}

View File

@ -23,11 +23,14 @@
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h>
#include <libyul/Object.h>
#include <libyul/Exceptions.h>
#include <libyul/AsmParser.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <liblangutil/Exceptions.h>
#include <libevmasm/SemanticInformation.h>
#include <libevmasm/Instruction.h>
#include <libyul/Exceptions.h>
#include <liblangutil/Exceptions.h>
#include <boost/range/adaptor/reversed.hpp>
@ -37,6 +40,31 @@ using namespace yul;
namespace
{
pair<YulString, BuiltinFunctionForEVM> createEVMFunction(
string const& _name,
dev::eth::Instruction _instruction
)
{
eth::InstructionInfo info = dev::eth::instructionInfo(_instruction);
BuiltinFunctionForEVM f;
f.name = YulString{_name};
f.parameters.resize(info.args);
f.returns.resize(info.ret);
f.movable = eth::SemanticInformation::movable(_instruction);
f.literalArguments = false;
f.generateCode = [_instruction](
FunctionCall const&,
AbstractAssembly& _assembly,
BuiltinContext&,
std::function<void()> _visitArguments
) {
_visitArguments();
_assembly.appendInstruction(_instruction);
};
return {f.name, move(f)};
}
pair<YulString, BuiltinFunctionForEVM> createFunction(
string _name,
size_t _params,
@ -59,9 +87,17 @@ pair<YulString, BuiltinFunctionForEVM> createFunction(
return {name, f};
}
map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion, bool _objectAccess)
map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVersion, bool _objectAccess)
{
map<YulString, BuiltinFunctionForEVM> builtins;
for (auto const& instr: Parser::instructions())
if (
!dev::eth::isDupInstruction(instr.second) &&
!dev::eth::isSwapInstruction(instr.second) &&
_evmVersion.hasOpcode(instr.second)
)
builtins.emplace(createEVMFunction(instr.first, instr.second));
if (_objectAccess)
{
builtins.emplace(createFunction("datasize", 1, 1, true, true, true, [](