P4C
The P4 Compiler
Loading...
Searching...
No Matches
bmv2/psa_switch/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
27namespace BMV2 {
28
30 protected:
31 P4::ReferenceMap *refMap;
32 P4::TypeMap *typeMap;
33
34 public:
38 unsigned scalars_width = 0;
39 unsigned error_width = 32;
40 unsigned bool_width = 1;
41
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
85 static bool isCounterMetadata(cstring ptName) { return !strcmp(ptName, "PSA_CounterType_t"); }
86
89 static bool isStandardMetadata(cstring ptName) {
90 return (!strcmp(ptName, "psa_ingress_parser_input_metadata_t") ||
91 !strcmp(ptName, "psa_egress_parser_input_metadata_t") ||
92 !strcmp(ptName, "psa_ingress_input_metadata_t") ||
93 !strcmp(ptName, "psa_ingress_output_metadata_t") ||
94 !strcmp(ptName, "psa_egress_input_metadata_t") ||
95 !strcmp(ptName, "psa_egress_deparser_input_metadata_t") ||
96 !strcmp(ptName, "psa_egress_output_metadata_t"));
97 }
98};
99
101 PsaProgramStructure *structure;
102
103 public:
104 explicit ParsePsaArchitecture(PsaProgramStructure *structure) : structure(structure) {
105 CHECK_NULL(structure);
106 }
107
108 void modelError(const char *format, const IR::INode *node) {
109 ::error(ErrorType::ERR_MODEL,
110 (cstring(format) + "\nAre you using an up-to-date 'psa.p4'?").c_str(),
111 node->getNode());
112 }
113
114 bool preorder(const IR::ToplevelBlock *block) override;
115 bool preorder(const IR::PackageBlock *block) override;
116 bool preorder(const IR::ExternBlock *block) override;
117
118 profile_t init_apply(const IR::Node *root) override {
119 structure->block_type.clear();
120 structure->globals.clear();
121 return Inspector::init_apply(root);
122 }
123};
124
126 P4::ReferenceMap *refMap;
127 P4::TypeMap *typeMap;
128 PsaProgramStructure *pinfo;
129
130 public:
132 : refMap(refMap), typeMap(typeMap), pinfo(pinfo) {
133 CHECK_NULL(refMap);
134 CHECK_NULL(typeMap);
135 CHECK_NULL(pinfo);
136 setName("InspectPsaProgram");
137 }
138
139 void postorder(const IR::P4Parser *p) override;
140 void postorder(const IR::P4Control *c) override;
141 void postorder(const IR::Declaration_Instance *di) override;
142
143 bool isHeaders(const IR::Type_StructLike *st);
144 void addTypesAndInstances(const IR::Type_StructLike *type, bool meta);
145 void addHeaderType(const IR::Type_StructLike *st);
146 void addHeaderInstance(const IR::Type_StructLike *st, cstring name);
147 bool preorder(const IR::Declaration_Variable *dv) override;
148 bool preorder(const IR::Parameter *parameter) override;
149};
150
151} // namespace BMV2
152
153#endif /* BACKENDS_BMV2_PSA_SWITCH_PSAPROGRAMSTRUCTURE_H_ */
Definition bmv2/psa_switch/psaProgramStructure.h:125
Definition bmv2/psa_switch/psaProgramStructure.h:100
Definition backends/bmv2/common/programStructure.h:40
Definition bmv2/psa_switch/psaProgramStructure.h:29
ordered_map< const IR::Node *, std::pair< gress_t, block_t > > block_type
Architecture related information.
Definition bmv2/psa_switch/psaProgramStructure.h:43
static bool isStandardMetadata(cstring ptName)
Definition bmv2/psa_switch/psaProgramStructure.h:89
ordered_map< cstring, const IR::Declaration_Variable * > scalars
Definition bmv2/psa_switch/psaProgramStructure.h:37
static bool isCounterMetadata(cstring ptName)
Definition bmv2/psa_switch/psaProgramStructure.h:85
Definition node.h:64
Definition node.h:93
Definition visitor.h:396
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
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21