P4C
The P4 Compiler
Loading...
Searching...
No Matches
pattern.h
1/*
2Copyright 2018-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef IR_PATTERN_H_
18#define IR_PATTERN_H_
19
20#include "ir/ir.h"
21
28class Pattern {
29 class Base {
30 public:
31 virtual bool match(const IR::Node *) = 0;
32 } *pattern;
33 Pattern(Base *p) : pattern(p) {} // NOLINT(runtime/explicit)
34
35 template <class T>
36 class MatchExt : public Base {
37 const T *&m;
38
39 public:
40 bool match(const IR::Node *n) override { return (m = n->to<T>()); }
41 MatchExt(const T *&m) : m(m) {} // NOLINT(runtime/explicit)
42 };
43
44 class Const : public Base {
45 big_int value;
46
47 public:
48 bool match(const IR::Node *n) override {
49 if (auto k = n->to<IR::Constant>()) return k->value == value;
50 return false;
51 }
52 Const(big_int v) : value(v) {} // NOLINT(runtime/explicit)
53 Const(int v) : value(v) {} // NOLINT(runtime/explicit)
54 };
55 template <class T>
56 class Unary : public Base {
57 Base *expr;
58
59 public:
60 bool match(const IR::Node *n) override {
61 if (auto b = n->to<T>()) return expr->match(b->expr);
62 return false;
63 }
64 Unary(Base *e) : expr(e) {} // NOLINT(runtime/explicit)
65 };
66 template <class T>
67 class Binary : public Base {
68 Base *left, *right;
69 bool commutative;
70
71 public:
72 bool match(const IR::Node *n) override {
73 if (auto b = n->to<T>()) {
74 if (left->match(b->left) && right->match(b->right)) return true;
75 if (commutative && left->match(b->right) && right->match(b->left)) return true;
76 }
77 return false;
78 }
79 Binary(Base *l, Base *r, bool commute = false) : left(l), right(r), commutative(commute) {}
80 };
81
82 public:
83 template <class T>
84 class Match : public Base {
85 const T *m;
86
87 public:
88 bool match(const IR::Node *n) override { return (m = n->to<T>()); }
89 Match() : m(nullptr) {}
90 const T *operator->() const { return m; }
91 operator const T *() const { return m; } // NOLINT(runtime/explicit)
92 Pattern operator*(const Pattern &a) { return Pattern(*this) * a; }
93 Pattern operator/(const Pattern &a) { return Pattern(*this) / a; }
94 Pattern operator%(const Pattern &a) { return Pattern(*this) % a; }
95 Pattern operator+(const Pattern &a) { return Pattern(*this) + a; }
96 Pattern operator-(const Pattern &a) { return Pattern(*this) - a; }
97 Pattern operator<<(const Pattern &a) { return Pattern(*this) << a; }
98 Pattern operator>>(const Pattern &a) { return Pattern(*this) >> a; }
99 Pattern operator==(const Pattern &a) { return Pattern(*this) == a; }
100 Pattern operator!=(const Pattern &a) { return Pattern(*this) != a; }
101 Pattern operator<(const Pattern &a) { return Pattern(*this) < a; }
102 Pattern operator<=(const Pattern &a) { return Pattern(*this) <= a; }
103 Pattern operator>(const Pattern &a) { return Pattern(*this) > a; }
104 Pattern operator>=(const Pattern &a) { return Pattern(*this) >= a; }
105 Pattern Relation(const Pattern &a) { return Pattern(*this).Relation(a); }
106 Pattern operator&(const Pattern &a) { return Pattern(*this) & a; }
107 Pattern operator|(const Pattern &a) { return Pattern(*this) | a; }
108 Pattern operator^(const Pattern &a) { return Pattern(*this) ^ a; }
109 Pattern operator&&(const Pattern &a) { return Pattern(*this) && a; }
110 Pattern operator||(const Pattern &a) { return Pattern(*this) || a; }
111 // avoid ambiguous overloads with operator const T * above
112 Pattern operator+(int a) { return Pattern(*this) + Pattern(a); }
113 Pattern operator-(int a) { return Pattern(*this) - Pattern(a); }
114 Pattern operator==(int a) { return Pattern(*this) == Pattern(a); }
115 Pattern operator!=(int a) { return Pattern(*this) != Pattern(a); }
116 };
117
118 template <class T>
119 Pattern(const T *&m) : pattern(new MatchExt<T>(m)) {} // NOLINT(runtime/explicit)
120 template <class T>
121 Pattern(Match<T> &m) : pattern(&m) {} // NOLINT(runtime/explicit)
122 explicit Pattern(big_int v) : pattern(new Const(v)) {} // NOLINT(runtime/explicit)
123 explicit Pattern(int v) : pattern(new Const(v)) {} // NOLINT(runtime/explicit)
124 Pattern operator-() const { return Pattern(new Unary<IR::Neg>(pattern)); }
125 Pattern operator~() const { return Pattern(new Unary<IR::Cmpl>(pattern)); }
126 Pattern operator!() const { return Pattern(new Unary<IR::LNot>(pattern)); }
127 Pattern operator*(const Pattern &r) const {
128 return Pattern(new Binary<IR::Mul>(pattern, r.pattern, true));
129 }
130 Pattern operator/(const Pattern &r) const {
131 return Pattern(new Binary<IR::Div>(pattern, r.pattern));
132 }
133 Pattern operator%(const Pattern &r) const {
134 return Pattern(new Binary<IR::Mod>(pattern, r.pattern));
135 }
136 Pattern operator+(const Pattern &r) const {
137 return Pattern(new Binary<IR::Add>(pattern, r.pattern, true));
138 }
139 Pattern operator-(const Pattern &r) const {
140 return Pattern(new Binary<IR::Sub>(pattern, r.pattern));
141 }
142 Pattern operator<<(const Pattern &r) const {
143 return Pattern(new Binary<IR::Shl>(pattern, r.pattern));
144 }
145 Pattern operator>>(const Pattern &r) const {
146 return Pattern(new Binary<IR::Shr>(pattern, r.pattern));
147 }
148 Pattern operator==(const Pattern &r) const {
149 return Pattern(new Binary<IR::Equ>(pattern, r.pattern, true));
150 }
151 Pattern operator!=(const Pattern &r) const {
152 return Pattern(new Binary<IR::Neq>(pattern, r.pattern, true));
153 }
154 Pattern operator<(const Pattern &r) const {
155 return Pattern(new Binary<IR::Lss>(pattern, r.pattern));
156 }
157 Pattern operator<=(const Pattern &r) const {
158 return Pattern(new Binary<IR::Leq>(pattern, r.pattern));
159 }
160 Pattern operator>(const Pattern &r) const {
161 return Pattern(new Binary<IR::Grt>(pattern, r.pattern));
162 }
163 Pattern operator>=(const Pattern &r) const {
164 return Pattern(new Binary<IR::Geq>(pattern, r.pattern));
165 }
166 Pattern Relation(const Pattern &r) const {
167 return Pattern(new Binary<IR::Operation::Relation>(pattern, r.pattern, true));
168 }
169 Pattern operator&(const Pattern &r) const {
170 return Pattern(new Binary<IR::BAnd>(pattern, r.pattern, true));
171 }
172 Pattern operator|(const Pattern &r) const {
173 return Pattern(new Binary<IR::BOr>(pattern, r.pattern, true));
174 }
175 Pattern operator^(const Pattern &r) const {
176 return Pattern(new Binary<IR::BXor>(pattern, r.pattern, true));
177 }
178 Pattern operator&&(const Pattern &r) const {
179 return Pattern(new Binary<IR::LAnd>(pattern, r.pattern));
180 }
181 Pattern operator||(const Pattern &r) const {
182 return Pattern(new Binary<IR::LOr>(pattern, r.pattern));
183 }
184
185 bool match(const IR::Node *n) { return pattern->match(n); }
186};
187
188inline Pattern operator*(int v, const Pattern &a) { return Pattern(v) * a; }
189inline Pattern operator/(int v, const Pattern &a) { return Pattern(v) / a; }
190inline Pattern operator%(int v, const Pattern &a) { return Pattern(v) % a; }
191inline Pattern operator+(int v, const Pattern &a) { return Pattern(v) + a; }
192inline Pattern operator-(int v, const Pattern &a) { return Pattern(v) - a; }
193inline Pattern operator<<(int v, const Pattern &a) { return Pattern(v) << a; }
194inline Pattern operator>>(int v, const Pattern &a) { return Pattern(v) >> a; }
195inline Pattern operator==(int v, const Pattern &a) { return Pattern(v) == a; }
196inline Pattern operator!=(int v, const Pattern &a) { return Pattern(v) != a; }
197inline Pattern operator<(int v, const Pattern &a) { return Pattern(v) < a; }
198inline Pattern operator<=(int v, const Pattern &a) { return Pattern(v) <= a; }
199inline Pattern operator>(int v, const Pattern &a) { return Pattern(v) > a; }
200inline Pattern operator>=(int v, const Pattern &a) { return Pattern(v) >= a; }
201inline Pattern operator&(int v, const Pattern &a) { return Pattern(v) & a; }
202inline Pattern operator|(int v, const Pattern &a) { return Pattern(v) | a; }
203inline Pattern operator^(int v, const Pattern &a) { return Pattern(v) ^ a; }
204inline Pattern operator&&(int v, const Pattern &a) { return Pattern(v) && a; }
205inline Pattern operator||(int v, const Pattern &a) { return Pattern(v) || a; }
206
207#endif /* IR_PATTERN_H_ */
Definition node.h:93
Definition pattern.h:84
Definition pattern.h:28
T * to() noexcept
Definition rtti.h:226