17#ifndef BACKENDS_EBPF_EBPFTYPE_H_
18#define BACKENDS_EBPF_EBPFTYPE_H_
20#include "ebpfObject.h"
22#include "lib/algorithm.h"
24#include "lib/sourceCodeBuilder.h"
31 explicit EBPFType(
const IR::Type *type) : type(type) {}
38 virtual void emitInitializer(
CodeBuilder *builder) = 0;
40 BUG(
"%1%: unsupported array", type);
65 static void createFactory(
const P4::TypeMap *typeMap) {
68 virtual EBPFType *create(
const IR::Type *type);
74 void emit(
CodeBuilder *builder)
override { builder->append(
"u8"); }
77 void emitInitializer(
CodeBuilder *builder)
override { builder->append(
"0"); }
90 :
EBPFType(type), elementType(elementType), size(type->getSize()) {
92 CHECK_NULL(elementType);
93 BUG_CHECK(elementType->
is<
IHasWidth>(),
"Unexpected element type %1%",
94 typeid(*elementType).name());
99 void emitInitializer(
CodeBuilder *builder)
override;
108 const unsigned width;
111 :
EBPFType(bits), width(bits->size), isSigned(bits->isSigned) {}
113 :
EBPFType(bits), width(bits->size), isSigned(
false) {}
114 unsigned bytesRequired()
const {
return ROUNDUP(width, 8); }
115 unsigned alignment()
const;
119 void emitInitializer(
CodeBuilder *builder)
override;
123 static bool generatesScalar(
unsigned width) {
return width <= 64; }
125 DECLARE_TYPEINFO(EBPFScalarType, EBPFType, IHasWidth);
130 const IR::Type_Name *type;
135 :
EBPFType(type), type(type), canonical(canonical) {}
136 void emit(
CodeBuilder *builder)
override { canonical->emit(builder); }
139 void emitInitializer(
CodeBuilder *builder)
override;
144 template <
typename T>
145 bool canonicalTypeIs()
const {
146 return this->canonical->
is<T>();
158 const IR::StructField *field;
160 EBPFField(
EBPFType *type,
const IR::StructField *field,
cstring comment =
nullptr)
161 : comment(comment), type(type), field(field) {}
167 std::vector<EBPFField *> fields;
174 void emitInitializer(
CodeBuilder *builder)
override;
189 void emitInitializer(
CodeBuilder *builder)
override { builder->append(
"0"); }
192 const IR::Type_Enum *getType()
const {
return type->to<IR::Type_Enum>(); }
194 DECLARE_TYPEINFO(EBPFEnumType, EBPFType, IHasWidth);
203 void emitInitializer(
CodeBuilder *builder)
override { builder->append(
"0"); }
206 const IR::Type_Error *getType()
const {
return type->to<IR::Type_Error>(); }
208 DECLARE_TYPEINFO(EBPFErrorType, EBPFType, IHasWidth);
215 const IR::Method *method_;
Definition ebpf/codeGen.h:33
unsigned implementationWidthInBits() const override
Definition ebpfType.h:79
unsigned widthInBits() const override
P4 width.
Definition ebpfType.h:78
Definition ebpfType.h:183
unsigned widthInBits() const override
P4 width.
Definition ebpfType.h:190
unsigned implementationWidthInBits() const override
Definition ebpfType.h:191
Definition ebpfType.h:197
unsigned implementationWidthInBits() const override
Definition ebpfType.h:205
unsigned widthInBits() const override
P4 width.
Definition ebpfType.h:204
Methods are function signatures.
Definition ebpfType.h:212
void emit(CodeBuilder *builder)
Emit the signature declaration of this method in C-style form.
Definition ebpfType.cpp:365
Base class for EBPF objects.
Definition ebpfObject.h:31
Definition ebpfType.h:106
unsigned widthInBits() const override
P4 width.
Definition ebpfType.h:120
unsigned implementationWidthInBits() const override
Definition ebpfType.h:121
unsigned widthInBits() const override
P4 width.
Definition ebpfType.cpp:91
unsigned implementationWidthInBits() const override
Definition ebpfType.cpp:95
Also represents headers and unions.
Definition ebpfType.h:153
unsigned implementationWidthInBits() const override
Definition ebpfType.h:176
unsigned widthInBits() const override
P4 width.
Definition ebpfType.h:175
Base class for EBPF types.
Definition ebpfType.h:29
This should not always implement IHasWidth, but it may...
Definition ebpfType.h:129
unsigned implementationWidthInBits() const override
Definition ebpfType.cpp:292
unsigned widthInBits() const override
P4 width.
Definition ebpfType.cpp:282
virtual unsigned implementationWidthInBits() const =0
virtual unsigned widthInBits() const =0
P4 width.
Definition codeGen.cpp:25
bool is() const noexcept
Definition rtti.h:216