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