P4C
The P4 Compiler
Loading...
Searching...
No Matches
introspection.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_INTROSPECTION_H_
18#define BACKENDS_TC_INTROSPECTION_H_
19
20#include "frontends/p4/parseAnnotations.h"
21#include "frontends/p4/parserCallGraph.h"
22#include "ir/ir.h"
23#include "lib/json.h"
24#include "lib/nullstream.h"
25#include "options.h"
26#include "tcAnnotations.h"
27
31namespace P4::TC {
32
33using namespace P4::literals;
34
35struct IntrospectionInfo {
36 cstring schemaVersion;
37 cstring pipelineName;
38 IntrospectionInfo() {
39 schemaVersion = nullptr;
40 pipelineName = nullptr;
41 }
42 void initIntrospectionInfo(IR::TCPipeline *tcPipeline) {
43 schemaVersion = "1.0.0"_cs;
44 pipelineName = tcPipeline->pipelineName;
45 }
46};
47
48struct KeyFieldAttributes {
49 unsigned int id;
50 cstring name;
51 cstring type;
52 cstring matchType;
53 cstring attribute;
54 unsigned int bitwidth;
55 KeyFieldAttributes() {
56 id = 0;
57 name = nullptr;
58 type = nullptr;
59 matchType = nullptr;
60 attribute = nullptr;
61 bitwidth = 0;
62 }
63};
64
65struct Annotation {
66 cstring name;
67 Annotation() { name = nullptr; }
68 explicit Annotation(cstring n) { name = n; }
69};
70
71struct ActionParam {
72 unsigned int id;
73 cstring name;
74 unsigned int dataType;
75 unsigned int bitwidth;
76 ActionParam() {
77 id = 0;
78 name = nullptr;
79 bitwidth = 0;
80 }
81};
82
83enum ActionScope { TableOnly, DefaultOnly, TableAndDefault };
84
85struct ActionAttributes {
86 unsigned int id;
87 cstring name;
88 ActionScope scope;
89 bool defaultHit;
90 bool defaultMiss;
93 ActionAttributes() {
94 id = 0;
95 name = nullptr;
96 scope = TableAndDefault;
97 defaultHit = false;
98 defaultMiss = false;
99 }
100};
101
102struct TableAttributes {
103 cstring name;
104 cstring permissions;
105 unsigned int id;
106 unsigned int tentries;
107 unsigned int numMask;
108 unsigned int keysize;
109 unsigned int keyid;
112 TableAttributes() {
113 name = nullptr;
114 permissions = nullptr;
115 id = 0;
116 tentries = 0;
117 numMask = 0;
118 keysize = 0;
119 keyid = 0;
120 }
121};
122
123struct ExternInstancesAttributes {
124 unsigned int id;
125 cstring name;
127 ExternInstancesAttributes() {
128 id = 0;
129 name = nullptr;
130 }
131};
132
133struct ExternAttributes {
134 cstring name;
135 cstring permissions;
136 cstring id;
138 ExternAttributes() {
139 name = nullptr;
140 permissions = nullptr;
141 id = 0;
142 }
143};
144
146class IntrospectionGenerator : public Inspector {
147 IR::TCPipeline *tcPipeline;
148 P4::ReferenceMap *refMap;
149 P4::TypeMap *typeMap;
153
154 public:
155 IntrospectionGenerator(IR::TCPipeline *tcPipeline, P4::ReferenceMap *refMap,
156 P4::TypeMap *typeMap)
157 : tcPipeline(tcPipeline), refMap(refMap), typeMap(typeMap) {}
158 void postorder(const IR::P4Table *t);
159 const Util::JsonObject *genIntrospectionJson();
160 void genExternJson(Util::JsonArray *externJson);
161 Util::JsonObject *genExternInfo(struct ExternAttributes *extn);
162 void genTableJson(Util::JsonArray *tablesJson);
163 Util::JsonObject *genTableInfo(struct TableAttributes *tbl);
164 void collectTableInfo();
165 void collectExternInfo();
166 void collectKeyInfo(const IR::Key *k, struct TableAttributes *tableinfo);
167 void collectActionInfo(const IR::ActionList *actionlist, struct TableAttributes *tableinfo,
168 const IR::P4Table *p4table, const IR::TCTable *table);
169 Util::JsonObject *genActionInfo(struct ActionAttributes *action);
170 Util::JsonObject *genKeyInfo(struct KeyFieldAttributes *keyField);
171 bool serializeIntrospectionJson(std::ostream &destination);
172 std::optional<cstring> checkValidTcType(const IR::StringLiteral *sl);
173 cstring externalName(const IR::IDeclaration *declaration);
174};
175
176} // namespace P4::TC
177
178#endif /* BACKENDS_TC_INTROSPECTION_H_ */
The Declaration interface, representing objects with names.
Definition declaration.h:26
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition json.h:115
Definition json.h:164
Definition cstring.h:85
Definition ordered_map.h:32
Definition safe_vector.h:27
This file defines functions for the pass to generate the introspection file.
Definition tc/backend.cpp:24
Definition introspection.h:85
Definition introspection.h:133
Definition introspection.h:48
Definition introspection.h:102