P4C
The P4 Compiler
Loading...
Searching...
No Matches
common/psaProgramStructure.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3Copyright 2022 VMware Inc.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17
18#ifndef BACKENDS_COMMON_PSAPROGRAMSTRUCTURE_H_
19#define BACKENDS_COMMON_PSAPROGRAMSTRUCTURE_H_
20
21#include "backends/common/programStructure.h"
22#include "frontends/common/resolveReferences/referenceMap.h"
23#include "frontends/p4/typeMap.h"
24#include "ir/ir.h"
25#include "lib/cstring.h"
26
27namespace P4 {
28
29enum gress_t { INGRESS, EGRESS };
30enum block_t {
31 PARSER,
32 PIPELINE,
33 DEPARSER,
34};
35
37 protected:
38 P4::ReferenceMap *refMap;
39 P4::TypeMap *typeMap;
40
41 public:
45 unsigned scalars_width = 0;
46 unsigned error_width = 32;
47 unsigned bool_width = 1;
48
51
67
68 std::vector<const IR::ExternBlock *> globals;
69
70 public:
72 : refMap(refMap), typeMap(typeMap) {
73 CHECK_NULL(refMap);
74 CHECK_NULL(typeMap);
75 }
76
77 std::set<cstring> non_pipeline_controls;
78 std::set<cstring> pipeline_controls;
79
80 bool hasVisited(const IR::Type_StructLike *st) {
81 if (auto h = st->to<IR::Type_Header>())
82 return header_types.count(h->getName());
83 else if (auto s = st->to<IR::Type_Struct>())
84 return metadata_types.count(s->getName());
85 else if (auto u = st->to<IR::Type_HeaderUnion>())
86 return header_union_types.count(u->getName());
87 return false;
88 }
89
92 static bool isCounterMetadata(cstring ptName) { return !strcmp(ptName, "PSA_CounterType_t"); }
93
96 static bool isStandardMetadata(cstring ptName) {
97 return (!strcmp(ptName, "psa_ingress_parser_input_metadata_t") ||
98 !strcmp(ptName, "psa_egress_parser_input_metadata_t") ||
99 !strcmp(ptName, "psa_ingress_input_metadata_t") ||
100 !strcmp(ptName, "psa_ingress_output_metadata_t") ||
101 !strcmp(ptName, "psa_egress_input_metadata_t") ||
102 !strcmp(ptName, "psa_egress_deparser_input_metadata_t") ||
103 !strcmp(ptName, "psa_egress_output_metadata_t"));
104 }
105};
106
108 PsaProgramStructure *structure;
109
110 public:
111 explicit ParsePsaArchitecture(PsaProgramStructure *structure) : structure(structure) {
112 CHECK_NULL(structure);
113 }
114
115 void modelError(const char *format, const IR::INode *node) {
116 ::error(ErrorType::ERR_MODEL,
117 (cstring(format) + "\nAre you using an up-to-date 'psa.p4'?").c_str(),
118 node->getNode());
119 }
120
121 bool preorder(const IR::ToplevelBlock *block) override;
122 bool preorder(const IR::PackageBlock *block) override;
123 bool preorder(const IR::ExternBlock *block) override;
124
125 profile_t init_apply(const IR::Node *root) override {
126 structure->block_type.clear();
127 structure->globals.clear();
128 return Inspector::init_apply(root);
129 }
130};
131
133 P4::ReferenceMap *refMap;
134 P4::TypeMap *typeMap;
135 PsaProgramStructure *pinfo;
136
137 public:
139 : refMap(refMap), typeMap(typeMap), pinfo(pinfo) {
140 CHECK_NULL(refMap);
141 CHECK_NULL(typeMap);
142 CHECK_NULL(pinfo);
143 setName("InspectPsaProgram");
144 }
145
146 void postorder(const IR::P4Parser *p) override;
147 void postorder(const IR::P4Control *c) override;
148 void postorder(const IR::Declaration_Instance *di) override;
149
150 bool isHeaders(const IR::Type_StructLike *st);
151 void addTypesAndInstances(const IR::Type_StructLike *type, bool meta);
152 void addHeaderType(const IR::Type_StructLike *st);
153 void addHeaderInstance(const IR::Type_StructLike *st, cstring name);
154 bool preorder(const IR::Declaration_Variable *dv) override;
155 bool preorder(const IR::Parameter *parameter) override;
156};
157
158} // namespace P4
159
160#endif /* BACKENDS_COMMON_PSAPROGRAMSTRUCTURE_H_ */
Definition node.h:64
Definition node.h:93
Definition visitor.h:396
Definition common/psaProgramStructure.h:132
Definition common/psaProgramStructure.h:107
Definition backends/common/programStructure.h:32
Definition common/psaProgramStructure.h:36
ordered_map< cstring, const IR::Declaration_Variable * > scalars
Definition common/psaProgramStructure.h:44
static bool isCounterMetadata(cstring ptName)
Definition common/psaProgramStructure.h:92
ordered_map< const IR::Node *, std::pair< gress_t, block_t > > block_type
Architecture related information.
Definition common/psaProgramStructure.h:50
static bool isStandardMetadata(cstring ptName)
Definition common/psaProgramStructure.h:96
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition visitor.h:76
Definition cstring.h:80
Definition ordered_map.h:30
Definition applyOptionsPragmas.cpp:24