Coverage Report

Created: 2022-08-24 06:28

/src/solidity/libevmasm/SimplificationRules.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
  This file is part of solidity.
3
4
  solidity is free software: you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
9
  solidity is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
// SPDX-License-Identifier: GPL-3.0
18
/**
19
 * @file SimplificationRules
20
 * @author Christian <chris@ethereum.org>
21
 * @date 2017
22
 * Module for applying replacement rules against Expressions.
23
 */
24
25
#pragma once
26
27
#include <libevmasm/ExpressionClasses.h>
28
#include <libevmasm/SimplificationRule.h>
29
30
#include <libsolutil/CommonData.h>
31
32
#include <functional>
33
#include <vector>
34
35
namespace solidity::langutil
36
{
37
struct SourceLocation;
38
}
39
40
namespace solidity::evmasm
41
{
42
43
class Pattern;
44
45
/**
46
 * Container for all simplification rules.
47
 */
48
class Rules
49
{
50
public:
51
  /// Noncopyable.
52
  Rules(Rules const&) = delete;
53
  Rules& operator=(Rules const&) = delete;
54
55
  using Expression = ExpressionClasses::Expression;
56
57
  Rules();
58
59
  /// @returns a pointer to the first matching pattern and sets the match
60
  /// groups accordingly.
61
  SimplificationRule<Pattern> const* findFirstMatch(
62
    Expression const& _expr,
63
    ExpressionClasses const& _classes
64
  );
65
66
  /// Checks whether the rulelist is non-empty. This is usually enforced
67
  /// by the constructor, but we had some issues with static initialization.
68
  bool isInitialized() const;
69
70
private:
71
  void addRules(std::vector<SimplificationRule<Pattern>> const& _rules);
72
  void addRule(SimplificationRule<Pattern> const& _rule);
73
74
0
  void resetMatchGroups() { m_matchGroups.clear(); }
75
76
  std::map<unsigned, Expression const*> m_matchGroups;
77
  /// Pattern to match, replacement to be applied and flag indicating whether
78
  /// the replacement might remove some elements (except constants).
79
  std::vector<SimplificationRule<Pattern>> m_rules[256];
80
};
81
82
/**
83
 * Pattern to match against an expression.
84
 * Also stores matched expressions to retrieve them later, for constructing new expressions using
85
 * ExpressionTemplate.
86
 */
87
class Pattern
88
{
89
public:
90
  using Expression = ExpressionClasses::Expression;
91
  using Id = ExpressionClasses::Id;
92
93
  using Builtins = evmasm::EVMBuiltins<Pattern>;
94
  static constexpr size_t WordSize = 256;
95
  using Word = u256;
96
97
  // Matches a specific constant value.
98
0
  Pattern(unsigned _value): Pattern(u256(_value)) {}
99
0
  Pattern(int _value): Pattern(u256(_value)) {}
100
0
  Pattern(long unsigned _value): Pattern(u256(_value)) {}
101
  // Matches a specific constant value.
102
0
  Pattern(u256 const& _value): m_type(Push), m_requireDataMatch(true), m_data(std::make_shared<u256>(_value)) {}
103
  // Matches a specific assembly item type or anything if not given.
104
0
  Pattern(AssemblyItemType _type = UndefinedItem): m_type(_type) {}
105
  // Matches a given instruction with given arguments
106
  Pattern(Instruction _instruction, std::initializer_list<Pattern> _arguments = {});
107
  /// Sets this pattern to be part of the match group with the identifier @a _group.
108
  /// Inside one rule, all patterns in the same match group have to match expressions from the
109
  /// same expression equivalence class.
110
  void setMatchGroup(unsigned _group, std::map<unsigned, Expression const*>& _matchGroups);
111
0
  unsigned matchGroup() const { return m_matchGroup; }
112
  bool matches(Expression const& _expr, ExpressionClasses const& _classes) const;
113
114
  AssemblyItem toAssemblyItem(langutil::SourceLocation const& _location) const;
115
0
  std::vector<Pattern> arguments() const { return m_arguments; }
116
117
  /// @returns the id of the matched expression if this pattern is part of a match group.
118
0
  Id id() const { return matchGroupValue().id; }
119
  /// @returns the data of the matched expression if this pattern is part of a match group.
120
0
  u256 const& d() const { return matchGroupValue().item->data(); }
121
122
  std::string toString() const;
123
124
0
  AssemblyItemType type() const { return m_type; }
125
  Instruction instruction() const
126
0
  {
127
0
    assertThrow(type() == Operation, OptimizerException, "");
128
0
    return m_instruction;
129
0
  }
130
131
private:
132
  bool matchesBaseItem(AssemblyItem const* _item) const;
133
  Expression const& matchGroupValue() const;
134
  u256 const& data() const;
135
136
  AssemblyItemType m_type;
137
  bool m_requireDataMatch = false;
138
  Instruction m_instruction; ///< Only valid if m_type is Operation
139
  std::shared_ptr<u256> m_data; ///< Only valid if m_type is not Operation
140
  std::vector<Pattern> m_arguments;
141
  unsigned m_matchGroup = 0;
142
  std::map<unsigned, Expression const*>* m_matchGroups = nullptr;
143
};
144
145
/**
146
 * Template for a new expression that can be built from matched patterns.
147
 */
148
struct ExpressionTemplate
149
{
150
  using Expression = ExpressionClasses::Expression;
151
  using Id = ExpressionClasses::Id;
152
  explicit ExpressionTemplate(Pattern const& _pattern, langutil::SourceLocation const& _location);
153
  std::string toString() const;
154
  bool hasId = false;
155
  /// Id of the matched expression, if available.
156
  Id id = Id(-1);
157
  // Otherwise, assembly item.
158
  AssemblyItem item = UndefinedItem;
159
  std::vector<ExpressionTemplate> arguments;
160
};
161
162
}