P4C
The P4 Compiler
Loading...
Searching...
No Matches
backends/common/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_COMMON_PROGRAMSTRUCTURE_H_
18#define BACKENDS_COMMON_PROGRAMSTRUCTURE_H_
19
20#include "backends/common/metermap.h"
21#include "ir/ir.h"
22#include "ir/visitor.h"
23#include "lib/ordered_map.h"
24#include "lib/ordered_set.h"
25
26namespace P4 {
27
28using ResourceMap = ordered_map<const IR::Node *, const IR::CompileTimeValue *>;
29
62
64 public:
65 ProgramStructure *structure;
66
67 explicit DiscoverStructure(ProgramStructure *structure) : structure(structure) {
68 setName("DiscoverStructure");
69 }
70 void postorder(const IR::ParameterList *paramList) override;
71 void postorder(const IR::P4Action *action) override;
72 void postorder(const IR::Declaration_Variable *decl) override;
73 void postorder(const IR::Type_Error *errors) override;
74 void postorder(const IR::Declaration_MatchKind *kind) override;
75};
76
82 ResourceMap *resourceMap;
83
84 public:
85 explicit BuildResourceMap(ResourceMap *resourceMap) : resourceMap(resourceMap) {
86 CHECK_NULL(resourceMap);
87 }
88
89 bool preorder(const IR::ControlBlock *control) override {
90 resourceMap->emplace(control->container, control);
91 for (auto cv : control->constantValue) {
92 resourceMap->emplace(cv.first, cv.second);
93 }
94
95 for (const auto *c : control->container->controlLocals) {
96 if (c->is<IR::InstantiatedBlock>()) {
97 resourceMap->emplace(c, control->getValue(c));
98 }
99 }
100 return false;
101 }
102
103 bool preorder(const IR::ParserBlock *parser) override {
104 resourceMap->emplace(parser->container, parser);
105 for (auto cv : parser->constantValue) {
106 resourceMap->emplace(cv.first, cv.second);
107 if (cv.second->is<IR::Block>()) {
108 visit(cv.second->getNode());
109 }
110 }
111
112 for (const auto *c : parser->container->parserLocals) {
113 if (c->is<IR::InstantiatedBlock>()) {
114 resourceMap->emplace(c, parser->getValue(c));
115 }
116 }
117 return false;
118 }
119
120 bool preorder(const IR::TableBlock *table) override {
121 resourceMap->emplace(table->container, table);
122 for (auto cv : table->constantValue) {
123 resourceMap->emplace(cv.first, cv.second);
124 if (cv.second->is<IR::Block>()) {
125 visit(cv.second->getNode());
126 }
127 }
128 return false;
129 }
130
131 bool preorder(const IR::PackageBlock *package) override {
132 for (auto cv : package->constantValue) {
133 if (cv.second->is<IR::Block>()) {
134 visit(cv.second->getNode());
135 }
136 }
137 return false;
138 }
139
140 bool preorder(const IR::ToplevelBlock *tlb) override {
141 const auto *package = tlb->getMain();
142 visit(package);
143 return false;
144 }
145};
146
147} // namespace P4
148
149#endif /* BACKENDS_COMMON_PROGRAMSTRUCTURE_H_ */
Definition backends/common/programStructure.h:81
Definition metermap.h:25
Definition backends/common/programStructure.h:63
Definition visitor.h:400
Definition backends/common/programStructure.h:32
ordered_map< cstring, const IR::P4Table * > directCounterMap
All the direct counters. (TODO: This should be PSA-specific or V1MODEL-specific.)
Definition backends/common/programStructure.h:50
ordered_map< const IR::Parameter *, unsigned > index
Definition backends/common/programStructure.h:38
std::set< cstring > match_kinds
All match kinds.
Definition backends/common/programStructure.h:56
ordered_set< const IR::Parameter * > nonActionParameters
Parameters of controls/parsers.
Definition backends/common/programStructure.h:40
DirectMeterMap directMeterMap
All the direct meters. (TODO: This should be PSA-specific or V1MODEL-specific.)
Definition backends/common/programStructure.h:48
ordered_map< const IR::IDeclaration *, unsigned int > errorCodesMap
All error codes.
Definition backends/common/programStructure.h:46
std::vector< const IR::Declaration_Variable * > variables
All local variables.
Definition backends/common/programStructure.h:44
ResourceMap resourceMap
map IR node to compile-time allocated resource blocks.
Definition backends/common/programStructure.h:58
ordered_map< const IR::P4Action *, const IR::P4Control * > actions
Map action to parent control.
Definition backends/common/programStructure.h:35
std::map< const IR::StructField *, cstring > scalarMetadataFields
Definition backends/common/programStructure.h:54
ordered_map< const IR::P4Action *, unsigned > ids
For each action its json id.
Definition backends/common/programStructure.h:42
Definition ordered_map.h:32
Definition ordered_set.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24