P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
pnaProgramStructure.h
1/*
2Copyright (C) 2023 Intel Corporation
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
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing,
11software distributed 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
14and limitations under the License.
15*/
16
17#ifndef BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_
18#define BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_
19
20#include "backends/bmv2/common/backend.h"
21#include "backends/bmv2/common/programStructure.h"
22#include "ir/ir.h"
23#include "lib/cstring.h"
24
25namespace TC {
26
28 protected:
29 P4::ReferenceMap *refMap;
30 P4::TypeMap *typeMap;
31
32 public:
33 // We place scalar user metadata fields (i.e., bit<>, bool)
34 // in the scalars map.
36 unsigned scalars_width = 0;
37 unsigned error_width = 32;
38 unsigned bool_width = 1;
39
40 // architecture related information
42
58
59 std::vector<const IR::ExternBlock *> globals;
60
61 public:
63 : refMap(refMap), typeMap(typeMap) {
64 CHECK_NULL(refMap);
65 CHECK_NULL(typeMap);
66 }
67
68 std::set<cstring> non_pipeline_controls;
69 std::set<cstring> pipeline_controls;
70
71 bool hasVisited(const IR::Type_StructLike *st) {
72 if (auto h = st->to<IR::Type_Header>())
73 return header_types.count(h->getName());
74 else if (auto s = st->to<IR::Type_Struct>())
75 return metadata_types.count(s->getName());
76 else if (auto u = st->to<IR::Type_HeaderUnion>())
77 return header_union_types.count(u->getName());
78 return false;
79 }
80
85 static bool isCounterMetadata(cstring ptName) { return !strcmp(ptName, "PNA_CounterType_t"); }
86
91 static bool isStandardMetadata(cstring ptName) {
92 return (!strcmp(ptName, "pna_main_parser_input_metadata_t") ||
93 !strcmp(ptName, "pna_main_input_metadata_t") ||
94 !strcmp(ptName, "pna_main_output_metadata_t"));
95 }
96};
97
98class ParsePnaArchitecture : public Inspector {
99 PnaProgramStructure *structure;
100
101 public:
102 explicit ParsePnaArchitecture(PnaProgramStructure *structure) : structure(structure) {
103 CHECK_NULL(structure);
104 }
105
106 void modelError(const char *format, const IR::INode *node) {
107 ::error(ErrorType::ERR_MODEL,
108 (cstring(format) + "\nAre you using an up-to-date 'pna.p4'?").c_str(),
109 node->getNode());
110 }
111
112 bool preorder(const IR::ToplevelBlock *block) override;
113 bool preorder(const IR::PackageBlock *block) override;
114 bool preorder(const IR::ExternBlock *block) override;
115
116 profile_t init_apply(const IR::Node *root) override {
117 structure->block_type.clear();
118 structure->globals.clear();
119 return Inspector::init_apply(root);
120 }
121};
122
123class InspectPnaProgram : public Inspector {
124 P4::ReferenceMap *refMap;
125 P4::TypeMap *typeMap;
126 PnaProgramStructure *pinfo;
127
128 public:
130 : refMap(refMap), typeMap(typeMap), pinfo(pinfo) {
131 CHECK_NULL(refMap);
132 CHECK_NULL(typeMap);
133 CHECK_NULL(pinfo);
134 setName("InspectPnaProgram");
135 }
136
137 void postorder(const IR::P4Parser *p) override;
138 void postorder(const IR::P4Control *c) override;
139 void postorder(const IR::Declaration_Instance *di) override;
140
141 bool isHeaders(const IR::Type_StructLike *st);
142 void addTypesAndInstances(const IR::Type_StructLike *type, bool meta);
143 void addHeaderType(const IR::Type_StructLike *st);
144 void addHeaderInstance(const IR::Type_StructLike *st, cstring name);
145 bool preorder(const IR::Declaration_Variable *dv) override;
146 bool preorder(const IR::Parameter *parameter) override;
147};
148
149} // namespace TC
150
151#endif /* BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_ */
Definition programStructure.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition pnaProgramStructure.h:123
Definition pnaProgramStructure.h:98
bool preorder(const IR::ToplevelBlock *block) override
Definition pnaProgramStructure.cpp:219
Definition pnaProgramStructure.h:27
static bool isStandardMetadata(cstring ptName)
Definition pnaProgramStructure.h:91
static bool isCounterMetadata(cstring ptName)
Definition pnaProgramStructure.h:85
Definition cstring.h:72
Definition ordered_map.h:30
This file defines functions for the pass to generate the introspection file.
Definition backend.cpp:24