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