Simplify patterns.

This commit is contained in:
chriseth
2019-11-15 00:34:01 +01:00
parent 3e04fd6efa
commit e70ef0b820
6 changed files with 310 additions and 264 deletions
+97 -75
View File
@@ -21,6 +21,7 @@
#pragma once
#include <libevmasm/Instruction.h>
#include <libdevcore/CommonData.h>
#include <functional>
namespace dev
@@ -55,84 +56,105 @@ struct SimplificationRule
std::function<bool()> feasible;
};
template <typename Pattern>
struct EVMBuiltins
{
using InstrType = Instruction;
static auto constexpr STOP = Instruction::STOP;
static auto constexpr ADD = Instruction::ADD;
static auto constexpr SUB = Instruction::SUB;
static auto constexpr MUL = Instruction::MUL;
static auto constexpr DIV = Instruction::DIV;
static auto constexpr SDIV = Instruction::SDIV;
static auto constexpr MOD = Instruction::MOD;
static auto constexpr SMOD = Instruction::SMOD;
static auto constexpr EXP = Instruction::EXP;
static auto constexpr NOT = Instruction::NOT;
static auto constexpr LT = Instruction::LT;
static auto constexpr GT = Instruction::GT;
static auto constexpr SLT = Instruction::SLT;
static auto constexpr SGT = Instruction::SGT;
static auto constexpr EQ = Instruction::EQ;
static auto constexpr ISZERO = Instruction::ISZERO;
static auto constexpr AND = Instruction::AND;
static auto constexpr OR = Instruction::OR;
static auto constexpr XOR = Instruction::XOR;
static auto constexpr BYTE = Instruction::BYTE;
static auto constexpr SHL = Instruction::SHL;
static auto constexpr SHR = Instruction::SHR;
static auto constexpr SAR = Instruction::SAR;
static auto constexpr ADDMOD = Instruction::ADDMOD;
static auto constexpr MULMOD = Instruction::MULMOD;
static auto constexpr SIGNEXTEND = Instruction::SIGNEXTEND;
static auto constexpr KECCAK256 = Instruction::KECCAK256;
static auto constexpr ADDRESS = Instruction::ADDRESS;
static auto constexpr BALANCE = Instruction::BALANCE;
static auto constexpr ORIGIN = Instruction::ORIGIN;
static auto constexpr CALLER = Instruction::CALLER;
static auto constexpr CALLVALUE = Instruction::CALLVALUE;
static auto constexpr CALLDATALOAD = Instruction::CALLDATALOAD;
static auto constexpr CALLDATASIZE = Instruction::CALLDATASIZE;
static auto constexpr CALLDATACOPY = Instruction::CALLDATACOPY;
static auto constexpr CODESIZE = Instruction::CODESIZE;
static auto constexpr CODECOPY = Instruction::CODECOPY;
static auto constexpr GASPRICE = Instruction::GASPRICE;
static auto constexpr EXTCODESIZE = Instruction::EXTCODESIZE;
static auto constexpr EXTCODECOPY = Instruction::EXTCODECOPY;
static auto constexpr RETURNDATASIZE = Instruction::RETURNDATASIZE;
static auto constexpr RETURNDATACOPY = Instruction::RETURNDATACOPY;
static auto constexpr EXTCODEHASH = Instruction::EXTCODEHASH;
static auto constexpr BLOCKHASH = Instruction::BLOCKHASH;
static auto constexpr COINBASE = Instruction::COINBASE;
static auto constexpr TIMESTAMP = Instruction::TIMESTAMP;
static auto constexpr NUMBER = Instruction::NUMBER;
static auto constexpr DIFFICULTY = Instruction::DIFFICULTY;
static auto constexpr GASLIMIT = Instruction::GASLIMIT;
static auto constexpr CHAINID = Instruction::CHAINID;
static auto constexpr SELFBALANCE = Instruction::SELFBALANCE;
static auto constexpr POP = Instruction::POP;
static auto constexpr MLOAD = Instruction::MLOAD;
static auto constexpr MSTORE = Instruction::MSTORE;
static auto constexpr MSTORE8 = Instruction::MSTORE8;
static auto constexpr SLOAD = Instruction::SLOAD;
static auto constexpr SSTORE = Instruction::SSTORE;
static auto constexpr PC = Instruction::PC;
static auto constexpr MSIZE = Instruction::MSIZE;
static auto constexpr GAS = Instruction::GAS;
static auto constexpr LOG0 = Instruction::LOG0;
static auto constexpr LOG1 = Instruction::LOG1;
static auto constexpr LOG2 = Instruction::LOG2;
static auto constexpr LOG3 = Instruction::LOG3;
static auto constexpr LOG4 = Instruction::LOG4;
static auto constexpr CREATE = Instruction::CREATE;
static auto constexpr CALL = Instruction::CALL;
static auto constexpr CALLCODE = Instruction::CALLCODE;
static auto constexpr STATICCALL = Instruction::STATICCALL;
static auto constexpr RETURN = Instruction::RETURN;
static auto constexpr DELEGATECALL = Instruction::DELEGATECALL;
static auto constexpr CREATE2 = Instruction::CREATE2;
static auto constexpr REVERT = Instruction::REVERT;
static auto constexpr INVALID = Instruction::INVALID;
static auto constexpr SELFDESTRUCT = Instruction::SELFDESTRUCT;
template<Instruction inst>
struct PatternGenerator
{
template<typename... Args> constexpr Pattern operator()(Args&&... _args) const
{
return {inst, {std::forward<Args>(_args)...}};
};
};
struct PatternGeneratorInstance
{
Instruction instruction;
template<typename... Args> constexpr Pattern operator()(Args&&... _args) const
{
return {instruction, {std::forward<Args>(_args)...}};
};
};
static auto constexpr STOP = PatternGenerator<Instruction::STOP>{};
static auto constexpr ADD = PatternGenerator<Instruction::ADD>{};
static auto constexpr SUB = PatternGenerator<Instruction::SUB>{};
static auto constexpr MUL = PatternGenerator<Instruction::MUL>{};
static auto constexpr DIV = PatternGenerator<Instruction::DIV>{};
static auto constexpr SDIV = PatternGenerator<Instruction::SDIV>{};
static auto constexpr MOD = PatternGenerator<Instruction::MOD>{};
static auto constexpr SMOD = PatternGenerator<Instruction::SMOD>{};
static auto constexpr EXP = PatternGenerator<Instruction::EXP>{};
static auto constexpr NOT = PatternGenerator<Instruction::NOT>{};
static auto constexpr LT = PatternGenerator<Instruction::LT>{};
static auto constexpr GT = PatternGenerator<Instruction::GT>{};
static auto constexpr SLT = PatternGenerator<Instruction::SLT>{};
static auto constexpr SGT = PatternGenerator<Instruction::SGT>{};
static auto constexpr EQ = PatternGenerator<Instruction::EQ>{};
static auto constexpr ISZERO = PatternGenerator<Instruction::ISZERO>{};
static auto constexpr AND = PatternGenerator<Instruction::AND>{};
static auto constexpr OR = PatternGenerator<Instruction::OR>{};
static auto constexpr XOR = PatternGenerator<Instruction::XOR>{};
static auto constexpr BYTE = PatternGenerator<Instruction::BYTE>{};
static auto constexpr SHL = PatternGenerator<Instruction::SHL>{};
static auto constexpr SHR = PatternGenerator<Instruction::SHR>{};
static auto constexpr SAR = PatternGenerator<Instruction::SAR>{};
static auto constexpr ADDMOD = PatternGenerator<Instruction::ADDMOD>{};
static auto constexpr MULMOD = PatternGenerator<Instruction::MULMOD>{};
static auto constexpr SIGNEXTEND = PatternGenerator<Instruction::SIGNEXTEND>{};
static auto constexpr KECCAK256 = PatternGenerator<Instruction::KECCAK256>{};
static auto constexpr ADDRESS = PatternGenerator<Instruction::ADDRESS>{};
static auto constexpr BALANCE = PatternGenerator<Instruction::BALANCE>{};
static auto constexpr ORIGIN = PatternGenerator<Instruction::ORIGIN>{};
static auto constexpr CALLER = PatternGenerator<Instruction::CALLER>{};
static auto constexpr CALLVALUE = PatternGenerator<Instruction::CALLVALUE>{};
static auto constexpr CALLDATALOAD = PatternGenerator<Instruction::CALLDATALOAD>{};
static auto constexpr CALLDATASIZE = PatternGenerator<Instruction::CALLDATASIZE>{};
static auto constexpr CALLDATACOPY = PatternGenerator<Instruction::CALLDATACOPY>{};
static auto constexpr CODESIZE = PatternGenerator<Instruction::CODESIZE>{};
static auto constexpr CODECOPY = PatternGenerator<Instruction::CODECOPY>{};
static auto constexpr GASPRICE = PatternGenerator<Instruction::GASPRICE>{};
static auto constexpr EXTCODESIZE = PatternGenerator<Instruction::EXTCODESIZE>{};
static auto constexpr EXTCODECOPY = PatternGenerator<Instruction::EXTCODECOPY>{};
static auto constexpr RETURNDATASIZE = PatternGenerator<Instruction::RETURNDATASIZE>{};
static auto constexpr RETURNDATACOPY = PatternGenerator<Instruction::RETURNDATACOPY>{};
static auto constexpr EXTCODEHASH = PatternGenerator<Instruction::EXTCODEHASH>{};
static auto constexpr BLOCKHASH = PatternGenerator<Instruction::BLOCKHASH>{};
static auto constexpr COINBASE = PatternGenerator<Instruction::COINBASE>{};
static auto constexpr TIMESTAMP = PatternGenerator<Instruction::TIMESTAMP>{};
static auto constexpr NUMBER = PatternGenerator<Instruction::NUMBER>{};
static auto constexpr DIFFICULTY = PatternGenerator<Instruction::DIFFICULTY>{};
static auto constexpr GASLIMIT = PatternGenerator<Instruction::GASLIMIT>{};
static auto constexpr CHAINID = PatternGenerator<Instruction::CHAINID>{};
static auto constexpr SELFBALANCE = PatternGenerator<Instruction::SELFBALANCE>{};
static auto constexpr POP = PatternGenerator<Instruction::POP>{};
static auto constexpr MLOAD = PatternGenerator<Instruction::MLOAD>{};
static auto constexpr MSTORE = PatternGenerator<Instruction::MSTORE>{};
static auto constexpr MSTORE8 = PatternGenerator<Instruction::MSTORE8>{};
static auto constexpr SLOAD = PatternGenerator<Instruction::SLOAD>{};
static auto constexpr SSTORE = PatternGenerator<Instruction::SSTORE>{};
static auto constexpr PC = PatternGenerator<Instruction::PC>{};
static auto constexpr MSIZE = PatternGenerator<Instruction::MSIZE>{};
static auto constexpr GAS = PatternGenerator<Instruction::GAS>{};
static auto constexpr LOG0 = PatternGenerator<Instruction::LOG0>{};
static auto constexpr LOG1 = PatternGenerator<Instruction::LOG1>{};
static auto constexpr LOG2 = PatternGenerator<Instruction::LOG2>{};
static auto constexpr LOG3 = PatternGenerator<Instruction::LOG3>{};
static auto constexpr LOG4 = PatternGenerator<Instruction::LOG4>{};
static auto constexpr CREATE = PatternGenerator<Instruction::CREATE>{};
static auto constexpr CALL = PatternGenerator<Instruction::CALL>{};
static auto constexpr CALLCODE = PatternGenerator<Instruction::CALLCODE>{};
static auto constexpr STATICCALL = PatternGenerator<Instruction::STATICCALL>{};
static auto constexpr RETURN = PatternGenerator<Instruction::RETURN>{};
static auto constexpr DELEGATECALL = PatternGenerator<Instruction::DELEGATECALL>{};
static auto constexpr CREATE2 = PatternGenerator<Instruction::CREATE2>{};
static auto constexpr REVERT = PatternGenerator<Instruction::REVERT>{};
static auto constexpr INVALID = PatternGenerator<Instruction::INVALID>{};
static auto constexpr SELFDESTRUCT = PatternGenerator<Instruction::SELFDESTRUCT>{};
};
}