P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
programStructure.h
1/*
2Copyright 2013-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
8 http://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 BACKENDS_BMV2_COMMON_PROGRAMSTRUCTURE_H_
18#define BACKENDS_BMV2_COMMON_PROGRAMSTRUCTURE_H_
19
20#include "ir/visitor.h"
21#include "lib/ordered_set.h"
22#include "metermap.h"
23
24namespace BMV2 {
25
27
28enum class BlockConverted {
29 None,
30 Parser,
31 Ingress,
32 Egress,
33 Deparser,
34 ChecksumCompute,
35 ChecksumVerify
36};
37
38// Represents all the compile-time information about a P4-16 program that
39// is common to all bmv2 targets (simple switch and psa switch).
41 public:
52 std::vector<const IR::Declaration_Variable *> variables;
55 // We place scalar user metadata fields (i.e., bit<>, bool)
56 // in the scalarsName metadata object, so we may need to rename
57 // these fields. This map holds the new names.
58 std::map<const IR::StructField *, cstring> scalarMetadataFields;
64 std::set<cstring> match_kinds;
67
69};
70
71class DiscoverStructure : public Inspector {
72 public:
73 ProgramStructure *structure;
74
75 explicit DiscoverStructure(ProgramStructure *structure) : structure(structure) {
76 setName("DiscoverStructure");
77 }
78 void postorder(const IR::ParameterList *paramList) override;
79 void postorder(const IR::P4Action *action) override;
80 void postorder(const IR::Declaration_Variable *decl) override;
81 void postorder(const IR::Type_Error *errors) override;
82 void postorder(const IR::Declaration_MatchKind *kind) override;
83};
84
85// The resource map represents the mapping from IR::Node to IR::Block. This
86// pass relies on the information generated by the most recent Evaluator pass.
87// The Evaluator pass generates a mapping from IR::Block to IR::Node. This pass
88// provides a reversed map.
89class BuildResourceMap : public Inspector {
90 ResourceMap *resourceMap;
91
92 public:
93 explicit BuildResourceMap(ResourceMap *resourceMap) : resourceMap(resourceMap) {
94 CHECK_NULL(resourceMap);
95 }
96
97 bool preorder(const IR::ControlBlock *control) override {
98 resourceMap->emplace(control->container, control);
99 for (auto cv : control->constantValue) {
100 resourceMap->emplace(cv.first, cv.second);
101 }
102
103 for (auto c : control->container->controlLocals) {
104 if (c->is<IR::InstantiatedBlock>()) {
105 resourceMap->emplace(c, control->getValue(c));
106 }
107 }
108 return false;
109 }
110
111 bool preorder(const IR::ParserBlock *parser) override {
112 resourceMap->emplace(parser->container, parser);
113 for (auto cv : parser->constantValue) {
114 resourceMap->emplace(cv.first, cv.second);
115 if (cv.second->is<IR::Block>()) {
116 visit(cv.second->getNode());
117 }
118 }
119
120 for (auto c : parser->container->parserLocals) {
121 if (c->is<IR::InstantiatedBlock>()) {
122 resourceMap->emplace(c, parser->getValue(c));
123 }
124 }
125 return false;
126 }
127
128 bool preorder(const IR::TableBlock *table) override {
129 resourceMap->emplace(table->container, table);
130 for (auto cv : table->constantValue) {
131 resourceMap->emplace(cv.first, cv.second);
132 if (cv.second->is<IR::Block>()) {
133 visit(cv.second->getNode());
134 }
135 }
136 return false;
137 }
138
139 bool preorder(const IR::PackageBlock *package) override {
140 for (auto cv : package->constantValue) {
141 if (cv.second->is<IR::Block>()) {
142 visit(cv.second->getNode());
143 }
144 }
145 return false;
146 }
147
148 bool preorder(const IR::ToplevelBlock *tlb) override {
149 auto package = tlb->getMain();
150 visit(package);
151 return false;
152 }
153};
154
155} // namespace BMV2
156
157#endif /* BACKENDS_BMV2_COMMON_PROGRAMSTRUCTURE_H_ */
Definition programStructure.h:89
Definition metermap.h:24
Definition programStructure.h:71
Definition programStructure.h:40
ResourceMap resourceMap
map IR node to compile-time allocated resource blocks.
Definition programStructure.h:66
ordered_map< const IR::P4Action *, unsigned > ids
For each action its json id.
Definition programStructure.h:50
std::set< cstring > match_kinds
All match kinds.
Definition programStructure.h:64
ordered_map< const IR::Parameter *, unsigned > index
Definition programStructure.h:46
ordered_map< const IR::IDeclaration *, unsigned int > errorCodesMap
All error codes.
Definition programStructure.h:54
std::vector< const IR::Declaration_Variable * > variables
All local variables.
Definition programStructure.h:52
ordered_map< const IR::P4Action *, const IR::P4Control * > actions
Map action to parent control.
Definition programStructure.h:43
ordered_set< const IR::Parameter * > nonActionParameters
Parameters of controls/parsers.
Definition programStructure.h:48
DirectMeterMap directMeterMap
All the direct meters.
Definition programStructure.h:60
ordered_map< cstring, const IR::P4Table * > directCounterMap
All the direct counters.
Definition programStructure.h:62
Definition sharedActionSelectorCheck.h:40
Definition ordered_map.h:30