P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
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_BMV2_PSA_SWITCH_PSAPROGRAMSTRUCTURE_H_
19#define BACKENDS_BMV2_PSA_SWITCH_PSAPROGRAMSTRUCTURE_H_
20
21#include "backends/bmv2/common/backend.h"
22#include "backends/bmv2/common/programStructure.h"
23#include "ir/ir.h"
24#include "lib/cstring.h"
25
26// TODO: this is not really specific to BMV2, it should reside somewhere else
27namespace BMV2 {
28
30 protected:
31 P4::ReferenceMap *refMap;
32 P4::TypeMap *typeMap;
33
34 public:
35 // We place scalar user metadata fields (i.e., bit<>, bool)
36 // in the scalars map.
38 unsigned scalars_width = 0;
39 unsigned error_width = 32;
40 unsigned bool_width = 1;
41
42 // architecture related information
44
60
61 std::vector<const IR::ExternBlock *> globals;
62
63 public:
65 : refMap(refMap), typeMap(typeMap) {
66 CHECK_NULL(refMap);
67 CHECK_NULL(typeMap);
68 }
69
70 std::set<cstring> non_pipeline_controls;
71 std::set<cstring> pipeline_controls;
72
73 bool hasVisited(const IR::Type_StructLike *st) {
74 if (auto h = st->to<IR::Type_Header>())
75 return header_types.count(h->getName());
76 else if (auto s = st->to<IR::Type_Struct>())
77 return metadata_types.count(s->getName());
78 else if (auto u = st->to<IR::Type_HeaderUnion>())
79 return header_union_types.count(u->getName());
80 return false;
81 }
82
87 static bool isCounterMetadata(cstring ptName) { return !strcmp(ptName, "PSA_CounterType_t"); }
88
94 return (!strcmp(ptName, "psa_ingress_parser_input_metadata_t") ||
95 !strcmp(ptName, "psa_egress_parser_input_metadata_t") ||
96 !strcmp(ptName, "psa_ingress_input_metadata_t") ||
97 !strcmp(ptName, "psa_ingress_output_metadata_t") ||
98 !strcmp(ptName, "psa_egress_input_metadata_t") ||
99 !strcmp(ptName, "psa_egress_deparser_input_metadata_t") ||
100 !strcmp(ptName, "psa_egress_output_metadata_t"));
101 }
102};
103
104class ParsePsaArchitecture : public Inspector {
105 PsaProgramStructure *structure;
106
107 public:
108 explicit ParsePsaArchitecture(PsaProgramStructure *structure) : structure(structure) {
109 CHECK_NULL(structure);
110 }
111
112 void modelError(const char *format, const IR::INode *node) {
113 ::error(ErrorType::ERR_MODEL,
114 (cstring(format) + "\nAre you using an up-to-date 'psa.p4'?").c_str(),
115 node->getNode());
116 }
117
118 bool preorder(const IR::ToplevelBlock *block) override;
119 bool preorder(const IR::PackageBlock *block) override;
120 bool preorder(const IR::ExternBlock *block) override;
121
122 profile_t init_apply(const IR::Node *root) override {
123 structure->block_type.clear();
124 structure->globals.clear();
125 return Inspector::init_apply(root);
126 }
127};
128
129class InspectPsaProgram : public Inspector {
130 P4::ReferenceMap *refMap;
131 P4::TypeMap *typeMap;
132 PsaProgramStructure *pinfo;
133
134 public:
136 : refMap(refMap), typeMap(typeMap), pinfo(pinfo) {
137 CHECK_NULL(refMap);
138 CHECK_NULL(typeMap);
139 CHECK_NULL(pinfo);
140 setName("InspectPsaProgram");
141 }
142
143 void postorder(const IR::P4Parser *p) override;
144 void postorder(const IR::P4Control *c) override;
145 void postorder(const IR::Declaration_Instance *di) override;
146
147 bool isHeaders(const IR::Type_StructLike *st);
148 void addTypesAndInstances(const IR::Type_StructLike *type, bool meta);
149 void addHeaderType(const IR::Type_StructLike *st);
150 void addHeaderInstance(const IR::Type_StructLike *st, cstring name);
151 bool preorder(const IR::Declaration_Variable *dv) override;
152 bool preorder(const IR::Parameter *parameter) override;
153};
154
155} // namespace BMV2
156
157#endif /* BACKENDS_BMV2_PSA_SWITCH_PSAPROGRAMSTRUCTURE_H_ */
Definition psaProgramStructure.h:129
Definition psaProgramStructure.h:104
bool preorder(const IR::ToplevelBlock *block) override
Definition psaProgramStructure.cpp:227
Definition programStructure.h:40
Definition psaProgramStructure.h:29
static bool isStandardMetadata(cstring ptName)
Definition psaProgramStructure.h:93
static bool isCounterMetadata(cstring ptName)
Definition psaProgramStructure.h:87
Definition sharedActionSelectorCheck.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition cstring.h:72