P4C
The P4 Compiler
Loading...
Searching...
No Matches
ubpfType.h
1/*
2Copyright 2019 Orange
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_UBPF_UBPFTYPE_H_
18#define BACKENDS_UBPF_UBPFTYPE_H_
19
20#include "backends/ebpf/ebpfType.h"
21#include "lib/sourceCodeBuilder.h"
22
23namespace P4::UBPF {
24
26 public:
27 explicit UBPFTypeFactory(const P4::TypeMap *typeMap) : EBPF::EBPFTypeFactory(typeMap) {}
28
29 static void createFactory(const P4::TypeMap *typeMap) {
30 EBPF::EBPFTypeFactory::instance = new UBPFTypeFactory(typeMap);
31 }
32
33 static EBPFTypeFactory *getInstance() { return EBPF::EBPFTypeFactory::instance; }
34
35 EBPF::EBPFType *create(const IR::Type *type) override;
36};
37
39 public:
41
42 void emit(EBPF::CodeBuilder *builder) override { builder->append("uint8_t"); }
43
44 DECLARE_TYPEINFO(UBPFBoolType, EBPF::EBPFBoolType);
45};
46
48 public:
49 explicit UBPFScalarType(const IR::Type_Bits *bits) : EBPF::EBPFScalarType(bits) {}
50
51 void emit(EBPF::CodeBuilder *builder) override;
52
53 cstring getAsString();
54
55 void declare(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
56 void declareInit(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
57
58 DECLARE_TYPEINFO(UBPFScalarType, EBPF::EBPFScalarType);
59};
60
62 public:
63 explicit UBPFExternType(const IR::Type_Bits *bits) : EBPF::EBPFScalarType(bits) {}
64
65 void emit(EBPF::CodeBuilder *builder) override;
66
67 cstring getAsString();
68
69 void declare(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
70 void declareInit(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
71
72 DECLARE_TYPEINFO(UBPFExternType, EBPF::EBPFScalarType);
73};
74
76 public:
77 explicit UBPFStructType(const IR::Type_StructLike *strct) : EBPF::EBPFStructType(strct) {}
78 void emit(EBPF::CodeBuilder *builder) override;
79 void declare(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
80 void declareInit(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
81
82 DECLARE_TYPEINFO(UBPFStructType, EBPF::EBPFStructType);
83};
84
86 public:
87 explicit UBPFEnumType(const IR::Type_Enum *strct) : EBPF::EBPFEnumType(strct) {}
88
89 void emit(EBPF::CodeBuilder *builder) override;
90
91 DECLARE_TYPEINFO(UBPFEnumType, EBPF::EBPFEnumType);
92};
93
95 public:
96 explicit UBPFErrorType(const IR::Type_Error *strct) : EBPF::EBPFErrorType(strct) {}
97
98 void emit(EBPF::CodeBuilder *builder) override;
99
100 DECLARE_TYPEINFO(UBPFErrorType, EBPF::EBPFErrorType);
101};
102
104 class UBPFListElement : public ICastable {
105 public:
106 EBPFType *type;
107 const cstring name;
108
109 UBPFListElement(EBPFType *type, const cstring name) : type(type), name(name) {}
110 virtual ~UBPFListElement() {} // to make UBPFListElement polymorphic.
111
112 DECLARE_TYPEINFO(UBPFListElement);
113 };
114
115 class Padding : public UBPFListElement {
116 public:
117 unsigned widthInBytes;
118
119 Padding(const cstring name, unsigned widthInBytes)
120 : UBPFListElement(nullptr, name), widthInBytes(widthInBytes) {}
121
122 DECLARE_TYPEINFO(Padding, UBPFListElement);
123 };
124
125 public:
126 cstring kind;
127 cstring name;
128 std::vector<UBPFListElement *> elements;
129 unsigned width;
130 unsigned implWidth;
131
132 explicit UBPFListType(const IR::Type_List *lst);
133 void emitPadding(EBPF::CodeBuilder *builder, Padding *padding);
134 void declare(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
135 void declareInit(EBPF::CodeBuilder *builder, cstring id, bool asPointer) override;
136 void emitInitializer(EBPF::CodeBuilder *builder) override;
137 unsigned widthInBits() const override { return width; }
138 unsigned implementationWidthInBits() const override { return implWidth; }
139 void emit(EBPF::CodeBuilder *builder) override;
140
141 DECLARE_TYPEINFO(UBPFListType, EBPF::EBPFType, EBPF::IHasWidth);
142};
143
144} // namespace P4::UBPF
145
146#endif /* BACKENDS_UBPF_UBPFTYPE_H_ */
Definition ebpf/codeGen.h:33
Definition ebpfType.h:71
Definition ebpfType.h:183
Definition ebpfType.h:197
Definition ebpfType.h:106
Also represents headers and unions.
Definition ebpfType.h:153
Definition ebpfType.h:58
Base class for EBPF types.
Definition ebpfType.h:29
Definition ebpfType.h:46
Definition castable.h:36
Definition typeMap.h:41
Definition ubpfType.h:38
Definition ubpfType.h:85
Definition ubpfType.h:94
Definition ubpfType.h:61
Definition ubpfType.h:103
unsigned implementationWidthInBits() const override
Definition ubpfType.h:138
unsigned widthInBits() const override
P4 width.
Definition ubpfType.h:137
Definition ubpfType.h:47
Definition ubpfType.h:75
Definition ubpfType.h:25
Definition cstring.h:85