-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlower.h
134 lines (117 loc) · 4.87 KB
/
lower.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _BACKENDS_BMV2_LOWER_H_
#define _BACKENDS_BMV2_LOWER_H_
#include "ir/ir.h"
#include "frontends/p4/typeMap.h"
#include "frontends/common/resolveReferences/resolveReferences.h"
namespace BMV2 {
/**
This pass rewrites expressions which are not supported natively on BMv2.
*/
class LowerExpressions : public Transform {
P4::TypeMap* typeMap;
// Cannot shift with a value larger than 8 bits
const int maxShiftWidth = 8;
const IR::Expression* shift(const IR::Operation_Binary* expression) const;
public:
explicit LowerExpressions(P4::TypeMap* typeMap) : typeMap(typeMap)
{ CHECK_NULL(typeMap); setName("LowerExpressions"); }
const IR::Node* postorder(IR::Expression* expression) override;
const IR::Node* postorder(IR::Shl* expression) override
{ return shift(expression); }
const IR::Node* postorder(IR::Shr* expression) override
{ return shift(expression); }
const IR::Node* postorder(IR::Cast* expression) override;
const IR::Node* postorder(IR::Neg* expression) override;
const IR::Node* postorder(IR::Slice* expression) override;
const IR::Node* postorder(IR::Concat* expression) override;
const IR::Node* preorder(IR::P4Table* table) override
{ prune(); return table; } // don't simplify expressions in table
};
/**
This pass is a hack to work around current BMv2 limitations:
checksum computations must be expressed in a restricted way, since
the JSON code generator uses simple pattern-matching.
The real solution to this problem is to have the BMv2 simulator use a
real extern for computing and verifying checksums. Then this hack
would not be necessary anymore.
*/
class FixupChecksum : public Transform {
const std::set<cstring>* updateChecksumBlocks;
public:
explicit FixupChecksum(const std::set<cstring>* updateChecksumBlocks) :
updateChecksumBlocks(updateChecksumBlocks)
{ setName("FixupChecksum"); }
const IR::Node* preorder(IR::P4Control* control) override;
};
/**
Policy which selects the control blocks where remove
complex expression is applied.
*/
class RemoveComplexExpressionsPolicy {
public:
virtual ~RemoveComplexExpressionsPolicy() {}
/**
If the policy returns true the control block is processed,
otherwise it is left unchanged.
*/
virtual bool convert(const IR::P4Control* control) const = 0;
};
/**
BMv2 does not support complex expressions for a select
or as arguments to external functions.
Such expressions are lifted into a temporaries.
*/
class RemoveComplexExpressions : public Transform {
P4::ReferenceMap* refMap;
P4::TypeMap* typeMap;
RemoveComplexExpressionsPolicy* policy;
IR::IndexedVector<IR::Declaration> newDecls;
IR::IndexedVector<IR::StatOrDecl> assignments;
const IR::PathExpression* createTemporary(const IR::Expression* expression);
const IR::Vector<IR::Expression>* simplifyExpressions(
const IR::Vector<IR::Expression>* vec, bool force = false);
public:
RemoveComplexExpressions(P4::ReferenceMap* refMap, P4::TypeMap* typeMap,
RemoveComplexExpressionsPolicy* policy = nullptr) :
refMap(refMap), typeMap(typeMap), policy(policy) {
CHECK_NULL(refMap); CHECK_NULL(typeMap);
setName("RemoveComplexExpressions"); }
const IR::Node* postorder(IR::SelectExpression* expression) override;
const IR::Node* preorder(IR::ParserState* state) override
{ assignments.clear(); return state; }
const IR::Node* postorder(IR::ParserState* state) override {
state->components.append(assignments);
return state;
}
const IR::Node* postorder(IR::MethodCallExpression* expression) override;
const IR::Node* preorder(IR::P4Parser* parser) override
{ newDecls.clear(); return parser; }
const IR::Node* postorder(IR::P4Parser* parser) override {
parser->parserLocals.append(newDecls);
return parser;
}
const IR::Node* preorder(IR::P4Control* control) override;
const IR::Node* postorder(IR::P4Control* control) override {
if (newDecls.size() != 0) {
// prepend declarations
newDecls.append(control->controlLocals);
control->controlLocals = newDecls;
}
return control;
}
const IR::Node* postorder(IR::Statement* statement) override;
};
} // namespace BMV2
#endif /* _BACKENDS_BMV2_LOWER_H_ */