P4C
The P4 Compiler
Loading...
Searching...
No Matches
unique_id.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_IR_UNIQUE_ID_H_
20#define BACKENDS_TOFINO_BF_P4C_IR_UNIQUE_ID_H_
21
22#include <iostream>
23
24#include "lib/cstring.h"
25#include "lib/exceptions.h"
26
27namespace P4 {
28class JSONGenerator;
29class JSONLoader;
30} // namespace P4
31
32namespace P4 {
33
45 public:
46 cstring name;
47 enum type_t {
48 INVALID,
49 TERNARY_INDIRECT,
50 IDLETIME,
51 COUNTER,
52 METER,
53 SELECTOR,
54 STATEFUL_ALU,
55 ACTION_DATA
56 } type = INVALID;
57
58 enum pre_placed_type_t { NO_PP, TIND_PP, ADATA_PP };
59
60 bool operator==(const UniqueAttachedId &uai) const {
61 return name == uai.name && type == uai.type;
62 }
63
64 bool operator!=(const UniqueAttachedId &uai) const { return !(*this == uai); }
65
66 bool operator<(const UniqueAttachedId &uai) const {
67 if (name != uai.name) return name < uai.name;
68 if (type != uai.type) return type < uai.type;
69 return false;
70 }
71
72 operator bool() const { return type != INVALID; }
73 bool name_valid() const { return !name.isNull(); }
74
76 UniqueAttachedId(cstring n, bool direct, type_t t) {
77 type = t;
78 if (!(type == IDLETIME || type == TERNARY_INDIRECT || (type == ACTION_DATA && direct))) {
79 name = n;
80 }
81 }
82
83 explicit UniqueAttachedId(pre_placed_type_t ppt) {
84 BUG_CHECK(ppt != NO_PP, "Attached ID not initialized correct");
85 if (ppt == TIND_PP) type = TERNARY_INDIRECT;
86 if (ppt == ADATA_PP) type = ACTION_DATA;
87 }
88
89 void toJSON(P4::JSONGenerator &json) const;
90 static UniqueAttachedId fromJSON(P4::JSONLoader &json);
91
92 bool has_meter_type() const {
93 return type == METER || type == STATEFUL_ALU || type == SELECTOR;
94 }
95
96 std::string build_name() const;
97};
98
99inline std::ostream &operator<<(std::ostream &out, const UniqueAttachedId::type_t type) {
100 switch (type) {
101 case UniqueAttachedId::TERNARY_INDIRECT:
102 out << "TERNARY_INDIRECT";
103 break;
104 case UniqueAttachedId::IDLETIME:
105 out << "IDLETIME";
106 break;
107 case UniqueAttachedId::COUNTER:
108 out << "COUNTER";
109 break;
110 case UniqueAttachedId::METER:
111 out << "METER";
112 break;
113 case UniqueAttachedId::SELECTOR:
114 out << "SELECTOR";
115 break;
116 case UniqueAttachedId::STATEFUL_ALU:
117 out << "STATEFUL_ALU";
118 break;
119 case UniqueAttachedId::ACTION_DATA:
120 out << "ACTION_DATA";
121 break;
122 default:
123 out << "INVALID";
124 break;
125 }
126 return out;
127}
128
129inline bool operator>>(cstring s, UniqueAttachedId::type_t &type) {
130 if (!s || s == "" || s == "INVALID")
131 type = UniqueAttachedId::INVALID;
132 else if (s == "TERNARY_INDIRECT")
133 type = UniqueAttachedId::TERNARY_INDIRECT;
134 else if (s == "IDLETIME")
135 type = UniqueAttachedId::IDLETIME;
136 else if (s == "COUNTER")
137 type = UniqueAttachedId::COUNTER;
138 else if (s == "METER")
139 type = UniqueAttachedId::METER;
140 else if (s == "SELECTOR")
141 type = UniqueAttachedId::SELECTOR;
142 else if (s == "STATEFUL_ALU")
143 type = UniqueAttachedId::STATEFUL_ALU;
144 else if (s == "ACTION_DATA")
145 type = UniqueAttachedId::ACTION_DATA;
146 else
147 return false;
148 return true;
149}
150
151inline std::ostream &operator<<(std::ostream &out, const UniqueAttachedId &uai) {
152 out << uai.name << " " << uai.type;
153 return out;
154}
155
168class UniqueId {
169 public:
170 cstring name;
171 // The stage table index of a table across multiple stages
172 int stage_table = -1;
173 // The logical table index of a table in a single stage (i.e. ATCAM tables have multiple
174 // logical tables per stage)
175 int logical_table = -1;
176
177 bool stage_table_used() const { return stage_table != -1; }
178 bool logical_table_used() const { return logical_table != -1; }
179 bool is_gw = false;
180
181 // An attached Id for determining a unique name for an Attached Table Name
182 UniqueAttachedId a_id;
183 enum speciality_t { NONE, ATCAM, DLEFT } speciality = NONE;
184
185 bool operator==(const UniqueId &ui) const;
186 bool operator!=(const UniqueId &ui) const { return !(*this == ui); }
187 bool operator<(const UniqueId &ui) const;
188
189 bool equal_table(const UniqueId &ui) const;
190
191 std::string build_name() const;
192 UniqueId base_match_id() const;
193
194 UniqueId() {}
195 explicit UniqueId(cstring n) : name(n) {}
196 cstring toString() const { return build_name(); }
197};
198
199std::ostream &operator<<(std::ostream &out, const UniqueId &ui);
200
201} // namespace P4
202
203#endif /* BACKENDS_TOFINO_BF_P4C_IR_UNIQUE_ID_H_ */
Definition json_generator.h:36
Definition json_loader.h:38
Definition unique_id.h:44
Definition unique_id.h:168
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24