P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
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 EBPF {
32
34 public:
35 const Target *target;
36 explicit CodeBuilder(const Target *target) : target(target) {}
37};
38
39// Visitor for generating C for EBPF
40// This visitor is invoked on various subtrees
41class CodeGenInspector : public Inspector {
42 protected:
43 CodeBuilder *builder;
44 P4::ReferenceMap *refMap;
45 P4::TypeMap *typeMap;
46 std::map<const IR::Parameter *, const IR::Parameter *> substitution;
47 // asPointerVariables stores the list of string expressions that
48 // should be emitted as pointer variables.
49 std::set<cstring> asPointerVariables;
50
51 // Since CodeGenInspector also generates C comments,
52 // this variable keeps track of the current comment depth.
53 int commentDescriptionDepth = 0;
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 ::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};
130
132 public:
133 // return *real* number of bits required by type
134 static unsigned ebpfTypeWidth(P4::TypeMap *typeMap, const IR::Expression *expr);
135
136 // Generate hex string and prepend it with zeroes when shorter than required width
137 static cstring genHexStr(const big_int &value, unsigned width, const IR::Expression *expr);
138};
139
140} // namespace EBPF
141
142#endif /* BACKENDS_EBPF_CODEGEN_H_ */
Definition codeGen.h:33
Definition codeGen.h:41
CodeGenInspector(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
precedence of current IR::Operation
Definition codeGen.h:57
Definition codeGen.h:131
Definition target.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition sourceCodeBuilder.h:29
Definition cstring.h:72
Definition applyOptionsPragmas.cpp:24