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