-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedActionSelectorCheck.cpp
83 lines (71 loc) · 3.11 KB
/
sharedActionSelectorCheck.cpp
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
/*
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.
*/
#include "sharedActionSelectorCheck.h"
#include <algorithm>
namespace BMV2 {
const SelectorInput*
SharedActionSelectorCheck::get_selector_input(const IR::Declaration_Instance* selector) {
auto it = selector_input_map.find(selector);
if (it == selector_input_map.end()) return nullptr; // selector never used
return &it->second;
}
bool
SharedActionSelectorCheck::preorder(const IR::P4Table* table) {
auto implementation = table->properties->getProperty("implementation");
if (implementation == nullptr) return false;
if (!implementation->value->is<IR::ExpressionValue>()) {
::error("%1%: expected expression for property", implementation);
return false;
}
auto propv = implementation->value->to<IR::ExpressionValue>();
if (!propv->expression->is<IR::PathExpression>()) return false;
auto pathe = propv->expression->to<IR::PathExpression>();
auto decl = refMap->getDeclaration(pathe->path, true);
if (!decl->is<IR::Declaration_Instance>()) {
::error("%1%: expected a reference to an instance", pathe);
return false;
}
auto dcltype = typeMap->getType(pathe, true);
if (!dcltype->is<IR::Type_Extern>()) {
::error("%1%: unexpected type for implementation", dcltype);
return false;
}
auto type_extern_name = dcltype->to<IR::Type_Extern>()->name;
if (type_extern_name != BMV2::TableImplementation::actionSelectorName) return false;
auto key = table->getKey();
SelectorInput input;
for (auto ke : key->keyElements) {
auto mt = refMap->getDeclaration(ke->matchType->path, true)->to<IR::Declaration_ID>();
BUG_CHECK(mt != nullptr, "%1%: could not find declaration", ke->matchType);
if (mt->name.name != BMV2::MatchImplementation::selectorMatchTypeName) continue;
input.push_back(ke->expression);
}
auto decl_instance = decl->to<IR::Declaration_Instance>();
auto it = selector_input_map.find(decl_instance);
if (it == selector_input_map.end()) {
selector_input_map[decl_instance] = input;
return false;
}
// returns true if inputs are the same, false otherwise
auto cmp_inputs = [](const SelectorInput &i1, const SelectorInput &i2) {
if (i1.size() != i2.size()) return false;
return std::equal(i1.begin(), i1.end(), i2.begin(), checkSameKeyExpr);
};
if (!cmp_inputs(it->second, input)) {
::error(
"Action selector '%1%' is used by multiple tables with different selector inputs",
decl);
}
return false;
}
} // namespace BMV2