P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpfType.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_EBPF_EBPFTYPE_H_
18#define BACKENDS_EBPF_EBPFTYPE_H_
19
20#include "ebpfObject.h"
21#include "ir/ir.h"
22#include "lib/algorithm.h"
23#include "lib/rtti.h"
24#include "lib/sourceCodeBuilder.h"
25
26namespace P4::EBPF {
27
29class EBPFType : public EBPFObject {
30 protected:
31 explicit EBPFType(const IR::Type *type) : type(type) {}
32
33 public:
34 const IR::Type *type;
35 virtual void emit(CodeBuilder *builder) = 0;
36 virtual void declare(CodeBuilder *builder, cstring id, bool asPointer) = 0;
37 virtual void declareInit(CodeBuilder *builder, cstring id, bool asPointer) = 0;
38 virtual void emitInitializer(CodeBuilder *builder) = 0;
39 virtual void declareArray(CodeBuilder * /*builder*/, cstring /*id*/, unsigned /*size*/) {
40 BUG("%1%: unsupported array", type);
41 }
42
43 DECLARE_TYPEINFO(EBPFType, EBPFObject);
44};
45
46class IHasWidth : public ICastable {
47 public:
48 virtual ~IHasWidth() {}
50 virtual unsigned widthInBits() const = 0;
53 virtual unsigned implementationWidthInBits() const = 0;
54
55 DECLARE_TYPEINFO(IHasWidth);
56};
57
59 protected:
60 const P4::TypeMap *typeMap;
61 explicit EBPFTypeFactory(const P4::TypeMap *typeMap) : typeMap(typeMap) { CHECK_NULL(typeMap); }
62
63 public:
64 static EBPFTypeFactory *instance;
65 static void createFactory(const P4::TypeMap *typeMap) {
66 EBPFTypeFactory::instance = new EBPFTypeFactory(typeMap);
67 }
68 virtual EBPFType *create(const IR::Type *type);
69};
70
71class EBPFBoolType : public EBPFType, public IHasWidth {
72 public:
73 EBPFBoolType() : EBPFType(IR::Type_Boolean::get()) {}
74 void emit(CodeBuilder *builder) override { builder->append("u8"); }
75 void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
76 void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
77 void emitInitializer(CodeBuilder *builder) override { builder->append("0"); }
78 unsigned widthInBits() const override { return 1; }
79 unsigned implementationWidthInBits() const override { return 8; }
80
81 DECLARE_TYPEINFO(EBPFBoolType, EBPFType, IHasWidth);
82};
83
84class EBPFStackType : public EBPFType, public IHasWidth {
85 EBPFType *elementType;
86 unsigned size;
87
88 public:
89 EBPFStackType(const IR::Type_Stack *type, EBPFType *elementType)
90 : EBPFType(type), elementType(elementType), size(type->getSize()) {
91 CHECK_NULL(type);
92 CHECK_NULL(elementType);
93 BUG_CHECK(elementType->is<IHasWidth>(), "Unexpected element type %1%",
94 typeid(*elementType).name());
95 }
96 void emit(CodeBuilder *) override {}
97 void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
98 void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
99 void emitInitializer(CodeBuilder *builder) override;
100 unsigned widthInBits() const override;
101 unsigned implementationWidthInBits() const override;
102
103 DECLARE_TYPEINFO(EBPFStackType, EBPFType, IHasWidth);
104};
105
106class EBPFScalarType : public EBPFType, public IHasWidth {
107 public:
108 const unsigned width;
109 const bool isSigned;
110 explicit EBPFScalarType(const IR::Type_Bits *bits)
111 : EBPFType(bits), width(bits->size), isSigned(bits->isSigned) {}
112 explicit EBPFScalarType(const IR::Type_Varbits *bits)
113 : EBPFType(bits), width(bits->size), isSigned(false) {}
114 unsigned bytesRequired() const { return ROUNDUP(width, 8); }
115 unsigned alignment() const;
116 void emit(CodeBuilder *builder) override;
117 void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
118 void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
119 void emitInitializer(CodeBuilder *builder) override;
120 unsigned widthInBits() const override { return width; }
121 unsigned implementationWidthInBits() const override { return bytesRequired() * 8; }
122 // True if this width is small enough to store in a machine scalar
123 static bool generatesScalar(unsigned width) { return width <= 64; }
124
125 DECLARE_TYPEINFO(EBPFScalarType, EBPFType, IHasWidth);
126};
127
129class EBPFTypeName : public EBPFType, public IHasWidth {
130 const IR::Type_Name *type;
131 EBPFType *canonical;
132
133 public:
134 EBPFTypeName(const IR::Type_Name *type, EBPFType *canonical)
135 : EBPFType(type), type(type), canonical(canonical) {}
136 void emit(CodeBuilder *builder) override { canonical->emit(builder); }
137 void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
138 void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
139 void emitInitializer(CodeBuilder *builder) override;
140 unsigned widthInBits() const override;
141 unsigned implementationWidthInBits() const override;
142 void declareArray(CodeBuilder *builder, cstring id, unsigned size) override;
143
144 template <typename T>
145 bool canonicalTypeIs() const {
146 return this->canonical->is<T>();
147 }
148
149 DECLARE_TYPEINFO(EBPFTypeName, EBPFType, IHasWidth);
150};
151
153class EBPFStructType : public EBPFType, public IHasWidth {
154 class EBPFField {
155 public:
156 cstring comment;
157 EBPFType *type;
158 const IR::StructField *field;
159
160 EBPFField(EBPFType *type, const IR::StructField *field, cstring comment = nullptr)
161 : comment(comment), type(type), field(field) {}
162 };
163
164 public:
165 cstring kind;
166 cstring name;
167 std::vector<EBPFField *> fields;
168 unsigned width;
169 unsigned implWidth;
170
171 explicit EBPFStructType(const IR::Type_StructLike *strct);
172 void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
173 void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
174 void emitInitializer(CodeBuilder *builder) override;
175 unsigned widthInBits() const override { return width; }
176 unsigned implementationWidthInBits() const override { return implWidth; }
177 void emit(CodeBuilder *builder) override;
178 void declareArray(CodeBuilder *builder, cstring id, unsigned size) override;
179
180 DECLARE_TYPEINFO(EBPFStructType, EBPFType, IHasWidth);
181};
182
183class EBPFEnumType : public EBPFType, public EBPF::IHasWidth {
184 public:
185 explicit EBPFEnumType(const IR::Type_Enum *type) : EBPFType(type) {}
186 void emit(CodeBuilder *builder) override;
187 void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
188 void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
189 void emitInitializer(CodeBuilder *builder) override { builder->append("0"); }
190 unsigned widthInBits() const override { return 32; }
191 unsigned implementationWidthInBits() const override { return 32; }
192 const IR::Type_Enum *getType() const { return type->to<IR::Type_Enum>(); }
193
194 DECLARE_TYPEINFO(EBPFEnumType, EBPFType, IHasWidth);
195};
196
197class EBPFErrorType : public EBPFType, public EBPF::IHasWidth {
198 public:
199 explicit EBPFErrorType(const IR::Type_Error *type) : EBPFType(type) {}
200 void emit(CodeBuilder *builder) override;
201 void declare(CodeBuilder *builder, cstring id, bool asPointer) override;
202 void declareInit(CodeBuilder *builder, cstring id, bool asPointer) override;
203 void emitInitializer(CodeBuilder *builder) override { builder->append("0"); }
204 unsigned widthInBits() const override { return 32; }
205 unsigned implementationWidthInBits() const override { return 32; }
206 const IR::Type_Error *getType() const { return type->to<IR::Type_Error>(); }
207
208 DECLARE_TYPEINFO(EBPFErrorType, EBPFType, IHasWidth);
209};
210
213 private:
215 const IR::Method *method_;
216
217 public:
218 explicit EBPFMethodDeclaration(const IR::Method *method);
219
221 void emit(CodeBuilder *builder);
222
223 DECLARE_TYPEINFO(EBPFMethodDeclaration, EBPFObject);
224};
225
226} // namespace P4::EBPF
227
228#endif /* BACKENDS_EBPF_EBPFTYPE_H_ */
Definition ebpf/codeGen.h:33
Definition ebpfType.h:71
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
Definition ebpfType.h:84
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
Definition ebpfType.h:58
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
Definition ebpfType.h:46
virtual unsigned implementationWidthInBits() const =0
virtual unsigned widthInBits() const =0
P4 width.
Definition castable.h:36
Definition typeMap.h:41
Definition cstring.h:85
Definition codeGen.cpp:25
bool is() const noexcept
Definition rtti.h:216