P4C
The P4 Compiler
Loading...
Searching...
No Matches
p4RuntimeSymbolTable.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef CONTROL_PLANE_P4RUNTIMESYMBOLTABLE_H_
9#define CONTROL_PLANE_P4RUNTIMESYMBOLTABLE_H_
10
11#include "lib/cstring.h"
12#include "p4RuntimeArchHandler.h"
13#include "typeSpecConverter.h"
14
15namespace p4 {
16namespace config {
17namespace v1 {
18class P4Info;
19} // namespace v1
20} // namespace config
21namespace v1 {
22class WriteRequest;
23} // namespace v1
24} // namespace p4
25
26namespace P4::IR {
27class P4Program;
28} // namespace P4::IR
29
30namespace P4 {
31
32namespace ControlPlaneAPI {
33
34const p4rt_id_t INVALID_ID = ::p4::config::v1::P4Ids::UNSPECIFIED;
35
37bool isControllerHeader(const IR::Type_Header *type);
38
40bool isHidden(const IR::IAnnotated *node);
41
44std::optional<p4rt_id_t> getIdAnnotation(const IR::IAnnotated *node);
45
54 void addSymbol(const cstring &symbol);
55
56 cstring shortestUniqueSuffix(const cstring &symbol) const;
57
58 private:
59 // All symbols in the set. We store these separately to make sure that no
60 // symbol is added to the tree of suffixes more than once.
61 std::set<cstring> symbols;
62
63 // A node in the tree of suffixes. The tree of suffixes is a directed graph
64 // of path components, with the edges pointing from the each component to
65 // its predecessor, so that every suffix of every symbol corresponds to a
66 // path through the tree. For example, "foo.bar[1].baz" would be represented
67 // as "baz" -> "bar[1]" -> "foo".
68 struct SuffixNode {
69 // How many suffixes pass through this node? This includes suffixes that
70 // terminate at this node.
71 unsigned instances = 0;
72
73 // Outgoing edges from this node. The SuffixNode should never be null.
74 std::map<cstring, SuffixNode *> edges;
75 };
76
77 // The root of our tree of suffixes. Note that this is *not* the data
78 // structure known as a suffix tree.
79 SuffixNode *suffixesRoot = new SuffixNode;
80};
81
84class P4RuntimeSymbolTable : public P4RuntimeSymbolTableIface {
85 public:
99 template <typename Func>
100 static P4RuntimeSymbolTable *create(Func function) {
101 // Create and initialize the symbol table. At this stage, ids aren't
102 // available, because computing ids requires global knowledge of all the
103 // P4Runtime symbols in the program.
104 auto *symbols = new P4RuntimeSymbolTable();
105 function(*symbols);
106
107 // Now that the symbol table is initialized, we can compute ids.
108 for (auto &table : symbols->symbolTables) {
109 symbols->computeIdsForSymbols(table.first);
110 }
111
112 return symbols;
113 }
114
115 static P4RuntimeSymbolTable *generateSymbols(const IR::P4Program *program,
116 const IR::ToplevelBlock *evaluatedProgram,
117 ReferenceMap *refMap, TypeMap *typeMap,
118 P4RuntimeArchHandlerIface *archHandler);
119
121 void add(P4RuntimeSymbolType type, const IR::IDeclaration *declaration) override;
122
124 void add(P4RuntimeSymbolType type, cstring name,
125 std::optional<p4rt_id_t> id = std::nullopt) override;
126
129 p4rt_id_t getId(P4RuntimeSymbolType type, const IR::IDeclaration *declaration) const override;
130
132 p4rt_id_t getId(P4RuntimeSymbolType type, cstring name) const override;
133
138 cstring getAlias(cstring name) const override;
139
140 private:
141 // Rather than call this constructor, use P4RuntimeSymbolTable::create().
142 P4RuntimeSymbolTable() = default;
143
147 p4rt_id_t tryToAssignId(std::optional<p4rt_id_t> id);
148
154 void computeIdsForSymbols(P4RuntimeSymbolType type);
155
167 template <typename ConstructIdFunc>
168 std::optional<p4rt_id_t> probeForId(const uint32_t sourceValue, ConstructIdFunc constructId) {
169 uint32_t value = sourceValue;
170 while (assignedIds.find(constructId(value)) != assignedIds.end()) {
171 ++value;
172 if (value == sourceValue) {
173 return std::nullopt; // We wrapped around; there's no unassigned
174 // id left.
175 }
176 }
177
178 return constructId(value);
179 }
180
181 // The hash function used for resource names.
182 // Taken from: https://en.wikipedia.org/wiki/Jenkins_hash_function
183 static uint32_t jenkinsOneAtATimeHash(const char *key, size_t length);
184
185 // All the ids we've assigned so far. Used to avoid id collisions; this is
186 // especially crucial since ids can be set manually via the '@id'
187 // annotation.
188 std::set<p4rt_id_t> assignedIds;
189
190 // Symbol tables, mapping symbols to P4Runtime ids.
191 using SymbolTable = std::map<cstring, p4rt_id_t>;
192 std::map<P4RuntimeSymbolType, SymbolTable> symbolTables{};
193
194 // A set which contains all the symbols in the program. It's used to compute
195 // the shortest unique suffix of each symbol, which is the default alias we
196 // use for P4Runtime objects.
197 P4SymbolSuffixSet suffixSet;
198};
199
200void collectControlSymbols(P4RuntimeSymbolTable &symbols, P4RuntimeArchHandlerIface *archHandler,
201 const IR::ControlBlock *controlBlock, ReferenceMap *refMap,
202 TypeMap *typeMap);
203
204void collectExternSymbols(P4RuntimeSymbolTable &symbols, P4RuntimeArchHandlerIface *archHandler,
205 const IR::ExternBlock *externBlock);
206
207void collectTableSymbols(P4RuntimeSymbolTable &symbols, P4RuntimeArchHandlerIface *archHandler,
208 const IR::TableBlock *tableBlock);
209
210void collectParserSymbols(P4RuntimeSymbolTable &symbols, const IR::ParserBlock *parserBlock);
211
212} // namespace ControlPlaneAPI
213
214} // namespace P4
215
216#endif /* CONTROL_PLANE_P4RUNTIMESYMBOLTABLE_H_ */
Definition p4RuntimeArchHandler.h:130
Definition p4RuntimeSymbolTable.h:84
p4rt_id_t getId(P4RuntimeSymbolType type, const IR::IDeclaration *declaration) const override
Definition p4RuntimeSymbolTable.cpp:186
void add(P4RuntimeSymbolType type, const IR::IDeclaration *declaration) override
Add a @type symbol, extracting the name and id from @declaration.
Definition p4RuntimeSymbolTable.cpp:169
cstring getAlias(cstring name) const override
Definition p4RuntimeSymbolTable.cpp:206
static P4RuntimeSymbolTable * create(Func function)
Definition p4RuntimeSymbolTable.h:100
Definition p4RuntimeArchHandler.h:100
Definition p4RuntimeArchHandler.h:54
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 cstring.h:85
TODO(antonin): High level goals of the generator go here!!
Definition dpdk/control-plane/bfruntime_arch_handler.h:44
std::optional< p4rt_id_t > getIdAnnotation(const IR::IAnnotated *node)
Definition p4RuntimeSymbolTable.cpp:28
bool isControllerHeader(const IR::Type_Header *type)
Definition p4RuntimeSymbolTable.cpp:20
bool isHidden(const IR::IAnnotated *node)
Definition p4RuntimeSymbolTable.cpp:24
Definition constantParsing.h:22
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
Definition p4RuntimeSymbolTable.h:52
void addSymbol(const cstring &symbol)
Adds @symbol's suffixes to the set if it's not already present.
Definition p4RuntimeSymbolTable.cpp:327