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 ptName == "PNA_CounterType_t"; }
93
98 static bool isStandardMetadata(cstring ptName) {
99 return ptName == "pna_main_parser_input_metadata_t" ||
100 ptName == "pna_main_input_metadata_t" || ptName == "pna_main_output_metadata_t";
101 }
102};
103
105 PnaProgramStructure *structure;
106
107 public:
108 explicit ParsePnaArchitecture(PnaProgramStructure *structure) : structure(structure) {
109 CHECK_NULL(structure);
110 }
111
112 void modelError(const char *format, const IR::INode *node) {
113 ::P4::error(ErrorType::ERR_MODEL,
114 (cstring(format) + "\nAre you using an up-to-date 'pna.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
130 P4::ReferenceMap *refMap;
131 P4::TypeMap *typeMap;
132 PnaProgramStructure *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("InspectPnaProgram");
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 P4::TC
156
157#endif /* BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_ */
Definition node.h:66
Definition node.h:94
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:129
Definition tc/pnaProgramStructure.h:104
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