P4C
The P4 Compiler
Loading...
Searching...
No Matches
tc/pnaProgramStructure.h
1/*
2 * Copyright (C) 2023 Intel Corporation
3 * SPDX-FileCopyrightText: 2023 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_
9#define BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_
10
11#include "backends/common/programStructure.h"
12#include "frontends/common/resolveReferences/referenceMap.h"
13#include "frontends/p4/typeMap.h"
14#include "ir/ir.h"
15#include "lib/cstring.h"
16
17namespace P4::TC {
18
19enum block_t {
20 PARSER,
21 PIPELINE,
22 DEPARSER,
23};
24
25class PnaProgramStructure : public P4::ProgramStructure {
26 protected:
27 P4::ReferenceMap *refMap;
28 P4::TypeMap *typeMap;
29
30 public:
31 // We place scalar user metadata fields (i.e., bit<>, bool)
32 // in the scalars map.
34 unsigned scalars_width = 0;
35 unsigned error_width = 32;
36 unsigned bool_width = 1;
37
38 // architecture related information
40
56
57 std::vector<const IR::ExternBlock *> globals;
58
59 public:
60 PnaProgramStructure(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
61 : refMap(refMap), typeMap(typeMap) {
62 CHECK_NULL(refMap);
63 CHECK_NULL(typeMap);
64 }
65
66 std::set<cstring> non_pipeline_controls;
67 std::set<cstring> pipeline_controls;
68
69 bool hasVisited(const IR::Type_StructLike *st) {
70 if (auto h = st->to<IR::Type_Header>())
71 return header_types.count(h->getName());
72 else if (auto s = st->to<IR::Type_Struct>())
73 return metadata_types.count(s->getName());
74 else if (auto u = st->to<IR::Type_HeaderUnion>())
75 return header_union_types.count(u->getName());
76 return false;
77 }
78
83 static bool isCounterMetadata(cstring ptName) { return ptName == "PNA_CounterType_t"; }
84
89 static bool isStandardMetadata(cstring ptName) {
90 return ptName == "pna_main_parser_input_metadata_t" ||
91 ptName == "pna_main_input_metadata_t" || ptName == "pna_main_output_metadata_t";
92 }
93};
94
95class ParsePnaArchitecture : public Inspector {
96 PnaProgramStructure *structure;
97
98 public:
99 explicit ParsePnaArchitecture(PnaProgramStructure *structure) : structure(structure) {
100 CHECK_NULL(structure);
101 }
102
103 void modelError(const char *format, const IR::INode *node) {
104 ::P4::error(ErrorType::ERR_MODEL,
105 (cstring(format) + "\nAre you using an up-to-date 'pna.p4'?").c_str(),
106 node->getNode());
107 }
108
109 bool preorder(const IR::ToplevelBlock *block) override;
110 bool preorder(const IR::PackageBlock *block) override;
111 bool preorder(const IR::ExternBlock *block) override;
112
113 profile_t init_apply(const IR::Node *root) override {
114 structure->block_type.clear();
115 structure->globals.clear();
116 return Inspector::init_apply(root);
117 }
118};
119
120class InspectPnaProgram : public Inspector {
121 P4::ReferenceMap *refMap;
122 P4::TypeMap *typeMap;
123 PnaProgramStructure *pinfo;
124
125 public:
126 InspectPnaProgram(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, PnaProgramStructure *pinfo)
127 : refMap(refMap), typeMap(typeMap), pinfo(pinfo) {
128 CHECK_NULL(refMap);
129 CHECK_NULL(typeMap);
130 CHECK_NULL(pinfo);
131 setName("InspectPnaProgram");
132 }
133
134 void postorder(const IR::P4Parser *p) override;
135 void postorder(const IR::P4Control *c) override;
136 void postorder(const IR::Declaration_Instance *di) override;
137
138 bool isHeaders(const IR::Type_StructLike *st);
139 void addTypesAndInstances(const IR::Type_StructLike *type, bool meta);
140 void addHeaderType(const IR::Type_StructLike *st);
141 void addHeaderInstance(const IR::Type_StructLike *st, cstring name);
142 bool preorder(const IR::Declaration_Variable *dv) override;
143 bool preorder(const IR::Parameter *parameter) override;
144};
145
146} // namespace P4::TC
147
148#endif /* BACKENDS_TC_PNAPROGRAMSTRUCTURE_H_ */
Definition inode.h:42
Definition node.h:53
Definition backends/common/programStructure.h:23
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
bool preorder(const IR::ToplevelBlock *block) override
Definition tc/pnaProgramStructure.cpp:210
Definition tc/pnaProgramStructure.h:25
static bool isStandardMetadata(cstring ptName)
Definition tc/pnaProgramStructure.h:89
static bool isCounterMetadata(cstring ptName)
Definition tc/pnaProgramStructure.h:83
Definition typeMap.h:32
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:17
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58