Skip to content

Commit e807026

Browse files
committed
Implement basic toString method for ebpf types.
Signed-off-by: fruffy <[email protected]>
1 parent 745318f commit e807026

File tree

9 files changed

+81
-11
lines changed

9 files changed

+81
-11
lines changed

backends/ebpf/ebpfControl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class EBPFControl : public EBPFObject {
8080
virtual void emitTableInitializers(CodeBuilder *builder);
8181
virtual void emitTableInstances(CodeBuilder *builder);
8282
virtual bool build();
83+
cstring toString() const override {
84+
return "EBPFControl:"_cs + controlBlock->container->name.name;
85+
}
8386
EBPFTable *getTable(cstring name) const {
8487
auto result = ::P4::get(tables, name);
8588
BUG_CHECK(result != nullptr, "No table named %1%", name);

backends/ebpf/ebpfDeparser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class EBPFDeparser : public EBPFControl {
8888

8989
void emitBufferAdjusts(CodeBuilder *builder) const;
9090

91+
cstring toString() const override {
92+
return "EBPFDeparser:"_cs + controlBlock->container->name.name;
93+
}
94+
9195
DECLARE_TYPEINFO(EBPFDeparser, EBPFControl);
9296
};
9397

backends/ebpf/ebpfObject.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ limitations under the License.
2323
#include "frontends/p4/typeMap.h"
2424
#include "ir/ir.h"
2525
#include "lib/castable.h"
26+
#include "lib/stringify.h"
2627
#include "target.h"
2728

2829
namespace P4::EBPF {
2930

3031
/// Base class for EBPF objects.
31-
class EBPFObject : public ICastable {
32+
class EBPFObject : virtual public ICastable, public IHasDbPrint {
3233
public:
3334
virtual ~EBPFObject() {}
3435

36+
/// Each EBPFObject must be able to be represented as a string.
37+
[[nodiscard]] virtual cstring toString() const { return "EBPFObject"_cs; }
38+
39+
void dbprint(std::ostream &out) const override { out << toString(); }
40+
3541
static cstring externalName(const IR::IDeclaration *declaration) {
3642
cstring name = declaration->externalName();
3743
return name.replace('.', '_');
@@ -60,4 +66,8 @@ class EBPFObject : public ICastable {
6066

6167
} // namespace P4::EBPF
6268

69+
inline std::ostream &operator<<(std::ostream &out, const P4::EBPF::EBPFObject &obj) {
70+
return out << obj.toString();
71+
}
72+
6373
#endif /* BACKENDS_EBPF_EBPFOBJECT_H_ */

backends/ebpf/ebpfParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class EBPFParserState : public EBPFObject {
7474
EBPFParserState(const IR::ParserState *state, EBPFParser *parser)
7575
: state(state), parser(parser) {}
7676
void emit(CodeBuilder *builder);
77+
cstring toString() const override { return "EBPFParserState:"_cs + state->name.name; }
7778

7879
DECLARE_TYPEINFO(EBPFParserState, EBPFObject);
7980
};
@@ -102,6 +103,7 @@ class EBPFParser : public EBPFObject {
102103
virtual void emitTypes(CodeBuilder *builder);
103104
virtual void emitValueSetInstances(CodeBuilder *builder);
104105
virtual void emitRejectState(CodeBuilder *builder);
106+
cstring toString() const override { return "EBPFParser"_cs; }
105107

106108
EBPFValueSet *getValueSet(cstring name) const { return ::P4::get(valueSets, name); }
107109

backends/ebpf/ebpfProgram.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class EBPFProgram : public EBPFObject {
8888
errorEnum = EBPFModel::reserved("errorCodes"_cs);
8989
}
9090

91+
cstring toString() const override { return "EBPFProgram"_cs; }
92+
9193
protected:
9294
virtual void emitPreamble(CodeBuilder *builder);
9395
virtual void emitTypes(CodeBuilder *builder);

backends/ebpf/ebpfTable.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class EBPFTableBase : public EBPFObject {
6666
dataMapName = instanceName;
6767
}
6868

69+
cstring toString() const override { return "EBPFTableBase<" + instanceName + ">"_cs; }
70+
6971
DECLARE_TYPEINFO(EBPFTableBase, EBPFObject);
7072
};
7173

@@ -143,6 +145,7 @@ class EBPFTable : public EBPFTableBase {
143145
(void)key;
144146
(void)value;
145147
}
148+
cstring toString() const override { return "EBPFTable<" + instanceName + ">"_cs; }
146149

147150
DECLARE_TYPEINFO(EBPFTable, EBPFTableBase);
148151
};
@@ -164,6 +167,7 @@ class EBPFCounterTable : public EBPFTableBase {
164167
const IR::MethodCallExpression *expression);
165168
virtual void emitCounterAdd(CodeBuilder *builder, const IR::MethodCallExpression *expression);
166169
virtual void emitMethodInvocation(CodeBuilder *builder, const P4::ExternMethod *method);
170+
cstring toString() const override { return "EBPFCounterTable<" + instanceName + ">"_cs; }
167171

168172
DECLARE_TYPEINFO(EBPFCounterTable, EBPFTableBase);
169173
};
@@ -184,6 +188,7 @@ class EBPFValueSet : public EBPFTableBase {
184188
void emitKeyInitializer(CodeBuilder *builder, const IR::SelectExpression *expression,
185189
cstring varName);
186190
void emitLookup(CodeBuilder *builder);
191+
cstring toString() const override { return "EBPFValueSet<" + instanceName + ">"_cs; }
187192

188193
DECLARE_TYPEINFO(EBPFValueSet, EBPFTableBase);
189194
};

backends/ebpf/ebpfType.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ class EBPFType : public EBPFObject {
3939
virtual void declareArray(CodeBuilder * /*builder*/, cstring /*id*/, unsigned /*size*/) {
4040
BUG("%1%: unsupported array", type);
4141
}
42+
cstring toString() const override { return "EBPFType"_cs; }
4243

4344
DECLARE_TYPEINFO(EBPFType, EBPFObject);
4445
};
4546

46-
class IHasWidth : public ICastable {
47+
class IHasWidth : virtual public ICastable {
4748
public:
4849
virtual ~IHasWidth() {}
4950
/// P4 width
@@ -79,6 +80,7 @@ class EBPFBoolType : public EBPFType, public IHasWidth {
7980
void emitInitializer(CodeBuilder *builder) override { builder->append("0"); }
8081
unsigned widthInBits() const override { return 1; }
8182
unsigned implementationWidthInBits() const override { return 8; }
83+
cstring toString() const override { return "EBPFBoolType"_cs; }
8284

8385
DECLARE_TYPEINFO(EBPFBoolType, EBPFType, IHasWidth);
8486
};
@@ -101,6 +103,7 @@ class EBPFStackType : public EBPFType, public IHasWidth {
101103
void emitInitializer(CodeBuilder *builder) override;
102104
unsigned widthInBits() const override;
103105
unsigned implementationWidthInBits() const override;
106+
cstring toString() const override { return "EBPFStackType"_cs; }
104107

105108
DECLARE_TYPEINFO(EBPFStackType, EBPFType, IHasWidth);
106109
};
@@ -123,6 +126,9 @@ class EBPFScalarType : public EBPFType, public IHasWidth {
123126
unsigned implementationWidthInBits() const override { return bytesRequired() * 8; }
124127
// True if this width is small enough to store in a machine scalar
125128
static bool generatesScalar(unsigned width) { return width <= 64; }
129+
cstring toString() const override {
130+
return "EBPFScalarType<width=" + Util::toString(width) + ">";
131+
}
126132

127133
DECLARE_TYPEINFO(EBPFScalarType, EBPFType, IHasWidth);
128134
};
@@ -142,6 +148,7 @@ class EBPFTypeName : public EBPFType, public IHasWidth {
142148
unsigned widthInBits() const override;
143149
unsigned implementationWidthInBits() const override;
144150
void declareArray(CodeBuilder *builder, cstring id, unsigned size) override;
151+
cstring toString() const override { return "EBPFTypeName<"_cs + type->path->name.name + ">"; }
145152

146153
template <typename T>
147154
bool canonicalTypeIs() const {
@@ -179,6 +186,7 @@ class EBPFStructType : public EBPFType, public IHasWidth {
179186
unsigned implementationWidthInBits() const override { return implWidth; }
180187
void emit(CodeBuilder *builder) override;
181188
void declareArray(CodeBuilder *builder, cstring id, unsigned size) override;
189+
cstring toString() const override { return "EBPFStructType<"_cs + name + ">"; }
182190

183191
DECLARE_TYPEINFO(EBPFStructType, EBPFType, IHasWidth);
184192
};
@@ -193,6 +201,7 @@ class EBPFEnumType : public EBPFType, public EBPF::IHasWidth {
193201
unsigned widthInBits() const override { return 32; }
194202
unsigned implementationWidthInBits() const override { return 32; }
195203
const IR::Type_Enum *getType() const { return type->to<IR::Type_Enum>(); }
204+
cstring toString() const override { return "EBPFEnumType<"_cs + getType()->name.name + ">"; }
196205

197206
DECLARE_TYPEINFO(EBPFEnumType, EBPFType, IHasWidth);
198207
};
@@ -207,6 +216,7 @@ class EBPFErrorType : public EBPFType, public EBPF::IHasWidth {
207216
unsigned widthInBits() const override { return 32; }
208217
unsigned implementationWidthInBits() const override { return 32; }
209218
const IR::Type_Error *getType() const { return type->to<IR::Type_Error>(); }
219+
cstring toString() const override { return "EBPFErrorType"_cs; }
210220

211221
DECLARE_TYPEINFO(EBPFErrorType, EBPFType, IHasWidth);
212222
};
@@ -222,6 +232,7 @@ class EBPFMethodDeclaration : public EBPFObject {
222232

223233
/// Emit the signature declaration of this method in C-style form.
224234
void emit(CodeBuilder *builder);
235+
cstring toString() const override { return "EBPFMethodDeclaration"_cs; }
225236

226237
DECLARE_TYPEINFO(EBPFMethodDeclaration, EBPFObject);
227238
};
@@ -230,14 +241,17 @@ class EBPFScalarTypePNA : public EBPFScalarType {
230241
bool isPrimitiveByteAligned = false;
231242

232243
public:
233-
explicit EBPFScalarTypePNA(const IR::Type_Bits *bits) : EBPFScalarType(bits) {
234-
isPrimitiveByteAligned = (width <= 8 || width <= 16 || (width > 24 && width <= 32) ||
235-
(width > 56 && width <= 64));
236-
}
244+
explicit EBPFScalarTypePNA(const IR::Type_Bits *bits)
245+
: EBPFScalarType(bits),
246+
isPrimitiveByteAligned(width <= 8 || width <= 16 || (width > 24 && width <= 32) ||
247+
(width > 56 && width <= 64)) {}
237248
unsigned alignment() const;
238-
void declare(CodeBuilder *builder, cstring id, bool asPointer);
239-
void declareInit(CodeBuilder *builder, cstring id, bool asPointer);
240-
void emitInitializer(CodeBuilder *builder);
249+
void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
250+
void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
251+
void emitInitializer(CodeBuilder *builder) override;
252+
cstring toString() const override {
253+
return "EBPFScalarTypePNA<width="_cs + Util::toString(width) + ">";
254+
}
241255
};
242256

243257
} // namespace P4::EBPF

backends/ebpf/target.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@ enum TableKind {
4141
TableDevmap
4242
};
4343

44+
/// Operator<< for TableKind
45+
inline std::ostream &operator<<(std::ostream &os, TableKind kind) {
46+
switch (kind) {
47+
case TableHash:
48+
os << "hash";
49+
break;
50+
case TableArray:
51+
os << "array";
52+
break;
53+
case TablePerCPUArray:
54+
os << "percpu_array";
55+
break;
56+
case TableProgArray:
57+
os << "prog_array";
58+
break;
59+
case TableLPMTrie:
60+
os << "lpm_trie";
61+
break;
62+
case TableHashLRU:
63+
os << "hash_lru";
64+
break;
65+
case TableDevmap:
66+
os << "devmap";
67+
break;
68+
default:
69+
BUG("Unknown table kind");
70+
}
71+
return os;
72+
}
73+
4474
class Target {
4575
protected:
4676
explicit Target(cstring name) : name(name) {}

cmake/Abseil.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ macro(p4c_obtain_abseil)
1919
set(CMAKE_FIND_LIBRARY_SUFFIXES ${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES})
2020
endif()
2121
else()
22-
set(P4C_ABSEIL_VERSION "20240116.1")
22+
set(P4C_ABSEIL_VERSION "20250814.0")
2323
message(STATUS "Fetching Abseil version ${P4C_ABSEIL_VERSION} for P4C...")
2424

2525
# Unity builds do not work for Abseil...
@@ -40,7 +40,7 @@ macro(p4c_obtain_abseil)
4040
FetchContent_Declare(
4141
abseil
4242
URL https://github.com/abseil/abseil-cpp/releases/download/${P4C_ABSEIL_VERSION}/abseil-cpp-${P4C_ABSEIL_VERSION}.tar.gz
43-
URL_HASH SHA256=3c743204df78366ad2eaf236d6631d83f6bc928d1705dd0000b872e53b73dc6a
43+
URL_HASH SHA256=9b2b72d4e8367c0b843fa2bcfa2b08debbe3cee34f7aaa27de55a6cbb3e843db
4444
USES_TERMINAL_DOWNLOAD TRUE
4545
GIT_PROGRESS TRUE
4646
DOWNLOAD_EXTRACT_TIMESTAMP TRUE

0 commit comments

Comments
 (0)