P4C
The P4 Compiler
Loading...
Searching...
No Matches
tc/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/common/programStructure.h"
21#include "frontends/common/resolveReferences/referenceMap.h"
22#include "frontends/p4/typeMap.h"
23#include "ir/ir.h"
24#include "lib/cstring.h"
25
26namespace P4::TC {
27
28enum block_t {
29 PARSER,
30 PIPELINE,
31 DEPARSER,
32};
33
35 protected:
36 P4::ReferenceMap *refMap;
37 P4::TypeMap *typeMap;
38
39 public:
40 // We place scalar user metadata fields (i.e., bit<>, bool)
41 // in the scalars map.
43 unsigned scalars_width = 0;
44 unsigned error_width = 32;
45 unsigned bool_width = 1;
46
47 // architecture related information
49
65
66 std::vector<const IR::ExternBlock *> globals;
67
68 public:
70 : refMap(refMap), typeMap(typeMap) {
71 CHECK_NULL(refMap);
72 CHECK_NULL(typeMap);
73 }
74
75 std::set<cstring> non_pipeline_controls;
76 std::set<cstring> pipeline_controls;
77
78 bool hasVisited(const IR::Type_StructLike *st) {
79 if (auto h = st->to<IR::Type_Header>())
80 return header_types.count(h->getName());
81 else if (auto s = st->to<IR::Type_Struct>())
82 return metadata_types.count(s->getName());
83 else if (auto u = st->to<IR::Type_HeaderUnion>())
84 return header_union_types.count(u->getName());
85 return false;
86 }
87
92 static bool isCounterMetadata(cstring ptName) { return !strcmp(ptName, "PNA_CounterType_t"); }
93
98 static bool isStandardMetadata(cstring ptName) {
99 return (!strcmp(ptName, "pna_main_parser_input_metadata_t") ||
100 !strcmp(ptName, "pna_main_input_metadata_t") ||
101 !strcmp(ptName, "pna_main_output_metadata_t"));
102 }
103};
104
106 PnaProgramStructure *structure;
107
108 public:
109 explicit ParsePnaArchitecture(PnaProgramStructure *structure) : structure(structure) {
110 CHECK_NULL(structure);
111 }
112
113 void modelError(const char *format, const IR::INode *node) {
114 ::P4::error(ErrorType::ERR_MODEL,
115 (cstring(format) + "\nAre you using an up-to-date 'pna.p4'?").c_str(),
116 node->getNode());
117 }
118
119 bool preorder(const IR::ToplevelBlock *block) override;
120 bool preorder(const IR::PackageBlock *block) override;
121 bool preorder(const IR::ExternBlock *block) override;
122
123 profile_t init_apply(const IR::Node *root) override {
124 structure->block_type.clear();
125 structure->globals.clear();
126 return Inspector::init_apply(root);
127 }
128};
129
131 P4::ReferenceMap *refMap;
132 P4::TypeMap *typeMap;
133 PnaProgramStructure *pinfo;
134
135 public:
137 : refMap(refMap), typeMap(typeMap), pinfo(pinfo) {
138 CHECK_NULL(refMap);
139 CHECK_NULL(typeMap);
140 CHECK_NULL(pinfo);
141 setName("InspectPnaProgram");
142 }
143
144 void postorder(const IR::P4Parser *p) override;
145 void postorder(const IR::P4Control *c) override;
146 void postorder(const IR::Declaration_Instance *di) override;
147
148 bool isHeaders(const IR::Type_StructLike *st);
149 void addTypesAndInstances(const IR::Type_StructLike *type, bool meta);
150 void addHeaderType(const IR::Type_StructLike *st);
151 void addHeaderInstance(const IR::Type_StructLike *st, cstring name);
152 bool preorder(const IR::Declaration_Variable *dv) override;
153 bool preorder(const IR::Parameter *parameter) override;
154};
155
156} // namespace P4::TC
157
158#endif /* BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_ */
Definition node.h:66
Definition node.h:95
Definition visitor.h:400
Definition backends/common/programStructure.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition tc/pnaProgramStructure.h:130
Definition tc/pnaProgramStructure.h:105
bool preorder(const IR::ToplevelBlock *block) override
Definition tc/pnaProgramStructure.cpp:221
Definition tc/pnaProgramStructure.h:34
static bool isStandardMetadata(cstring ptName)
Definition tc/pnaProgramStructure.h:98
static bool isCounterMetadata(cstring ptName)
Definition tc/pnaProgramStructure.h:92
Definition typeMap.h:41
Definition visitor.h:78
Definition cstring.h:85
Definition ordered_map.h:32
This file defines functions for the pass to generate the introspection file.
Definition tc/backend.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51