P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpf/codeGen.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_CODEGEN_H_
18#define BACKENDS_EBPF_CODEGEN_H_
19
20#include "frontends/p4/typeMap.h"
21#include "ir/ir.h"
22#include "lib/sourceCodeBuilder.h"
23#include "target.h"
24
25namespace P4 {
26
27class ReferenceMap;
28
29}
30
31namespace P4::EBPF {
32
34 public:
35 const Target *target;
36 explicit CodeBuilder(const Target *target) : target(target) {}
37};
38
42 protected:
43 CodeBuilder *builder;
44 P4::ReferenceMap *refMap;
45 P4::TypeMap *typeMap;
46 std::map<const IR::Parameter *, const IR::Parameter *> substitution;
49 std::set<cstring> asPointerVariables;
50
54
55 public:
56 int expressionPrecedence;
58 : builder(nullptr),
59 refMap(refMap),
60 typeMap(typeMap),
61 expressionPrecedence(DBPrint::Prec_Low) {
62 CHECK_NULL(refMap);
63 CHECK_NULL(typeMap);
64 visitDagOnce = false;
65 }
66
67 void setBuilder(CodeBuilder *builder) {
68 CHECK_NULL(builder);
69 this->builder = builder;
70 }
71
72 void substitute(const IR::Parameter *p, const IR::Parameter *with);
73 void copySubstitutions(CodeGenInspector *other) {
74 for (auto s : other->substitution) substitute(s.first, s.second);
75 }
76
77 void useAsPointerVariable(cstring name) { this->asPointerVariables.insert(name); }
78 void copyPointerVariables(CodeGenInspector *other) {
79 for (auto s : other->asPointerVariables) {
80 this->asPointerVariables.insert(s);
81 }
82 }
83 bool isPointerVariable(cstring name) { return asPointerVariables.count(name) > 0; }
84
85 bool notSupported(const IR::Expression *expression) {
86 ::P4::error(ErrorType::ERR_UNSUPPORTED, "%1%: not yet implemented", expression);
87 return false;
88 }
89
90 bool preorder(const IR::Expression *expression) override { return notSupported(expression); }
91 bool preorder(const IR::Range *expression) override { return notSupported(expression); }
92 bool preorder(const IR::Mask *expression) override { return notSupported(expression); }
93 bool preorder(const IR::Slice *expression) override // should not happen
94 {
95 return notSupported(expression);
96 }
97 bool preorder(const IR::StringLiteral *expression) override;
98 bool preorder(const IR::ListExpression *expression) override;
99 bool preorder(const IR::PathExpression *expression) override;
100 bool preorder(const IR::Constant *expression) override;
101 bool preorder(const IR::Declaration_Variable *decl) override;
102 bool preorder(const IR::BoolLiteral *b) override;
103 bool preorder(const IR::Cast *c) override;
104 bool preorder(const IR::Operation_Binary *b) override;
105 bool preorder(const IR::Operation_Unary *u) override;
106 bool preorder(const IR::ArrayIndex *a) override;
107 bool preorder(const IR::Mux *a) override;
108 bool preorder(const IR::Member *e) override;
109 bool preorder(const IR::MethodCallExpression *expression) override;
110 bool comparison(const IR::Operation_Relation *comp);
111 bool preorder(const IR::Equ *e) override { return comparison(e); }
112 bool preorder(const IR::Neq *e) override { return comparison(e); }
113 bool preorder(const IR::Path *path) override;
114 bool preorder(const IR::StructExpression *expr) override;
115
116 bool preorder(const IR::Type_Typedef *type) override;
117 bool preorder(const IR::Type_Enum *type) override;
118 void emitAssignStatement(const IR::Type *ltype, const IR::Expression *lexpr, cstring lpath,
119 const IR::Expression *rexpr);
120 bool preorder(const IR::AssignmentStatement *s) override;
121 bool preorder(const IR::BlockStatement *s) override;
122 bool preorder(const IR::MethodCallStatement *s) override;
123 bool preorder(const IR::EmptyStatement *s) override;
124 bool preorder(const IR::ReturnStatement *s) override;
125 bool preorder(const IR::ExitStatement *s) override;
126 bool preorder(const IR::IfStatement *s) override;
127
128 void widthCheck(const IR::Node *node) const;
129 void emitAndConvertByteOrder(const IR::Expression *expr, cstring byte_order);
130 void emitTCBinaryOperation(const IR::Operation_Binary *b, bool isScalar);
131 void emitTCAssignmentEndianessConversion(const IR::Expression *lexpr,
132 const IR::Expression *rexpr);
133};
134
136 public:
138 static unsigned ebpfTypeWidth(P4::TypeMap *typeMap, const IR::Expression *expr);
139
141 static cstring genHexStr(const big_int &value, unsigned width, const IR::Expression *expr);
142};
143
144} // namespace P4::EBPF
145
146#endif /* BACKENDS_EBPF_CODEGEN_H_ */
Definition ebpf/codeGen.h:33
Definition ebpf/codeGen.h:41
CodeGenInspector(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
precedence of current IR::Operation
Definition ebpf/codeGen.h:57
int commentDescriptionDepth
Definition ebpf/codeGen.h:53
std::set< cstring > asPointerVariables
Definition ebpf/codeGen.h:49
Definition ebpf/codeGen.h:135
static unsigned ebpfTypeWidth(P4::TypeMap *typeMap, const IR::Expression *expr)
return real number of bits required by type
Definition codeGen.cpp:614
static cstring genHexStr(const big_int &value, unsigned width, const IR::Expression *expr)
Generate hex string and prepend it with zeroes when shorter than required width.
Definition codeGen.cpp:633
Definition ebpf/target.h:44
Definition visitor.h:400
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition sourceCodeBuilder.h:29
Definition cstring.h:85
Definition codeGen.cpp:25
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition error.h:51