P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpfParser.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_EBPFPARSER_H_
18#define BACKENDS_EBPF_EBPFPARSER_H_
19
20#include "ebpfObject.h"
21#include "ebpfProgram.h"
22#include "ebpfTable.h"
23#include "frontends/p4/methodInstance.h"
24#include "ir/ir.h"
25
26namespace P4::EBPF {
27
28class EBPFParser;
29class EBPFParserState;
30
32 protected:
35
36 P4::P4CoreLibrary &p4lib;
37 const EBPFParserState *state;
38
39 virtual void compileExtractField(const IR::Expression *expr, const IR::StructField *field,
40 unsigned hdrOffsetBits, EBPFType *type);
41 virtual void compileExtract(const IR::Expression *destination);
42 virtual void compileLookahead(const IR::Expression *destination);
43 void compileAdvance(const P4::ExternMethod *ext);
44 void compileVerify(const IR::MethodCallExpression *expression);
45
46 virtual void processFunction(const P4::ExternFunction *function);
47 virtual void processMethod(const P4::ExternMethod *method);
48
49 public:
50 explicit StateTranslationVisitor(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
51 : CodeGenInspector(refMap, typeMap), p4lib(P4::P4CoreLibrary::instance()), state(nullptr) {}
52
53 void setState(const EBPFParserState *state) { this->state = state; }
54 bool preorder(const IR::ParserState *state) override;
55 bool preorder(const IR::SelectCase *selectCase) override;
56 bool preorder(const IR::SelectExpression *expression) override;
57 bool preorder(const IR::Member *expression) override;
58 bool preorder(const IR::MethodCallExpression *expression) override;
59 bool preorder(const IR::MethodCallStatement *stat) override {
60 visit(stat->methodCall);
61 builder->endOfStatement(true);
62 return false;
63 }
64 bool preorder(const IR::AssignmentStatement *stat) override;
65};
66
68 public:
69 const IR::ParserState *state;
70 const EBPFParser *parser;
71
72 EBPFParserState(const IR::ParserState *state, EBPFParser *parser)
73 : state(state), parser(parser) {}
74 void emit(CodeBuilder *builder);
75
76 DECLARE_TYPEINFO(EBPFParserState, EBPFObject);
77};
78
79class EBPFParser : public EBPFObject {
80 public:
81 const EBPFProgram *program;
82 const P4::TypeMap *typeMap;
83 const IR::ParserBlock *parserBlock;
84 std::vector<EBPFParserState *> states;
85 const IR::Parameter *packet;
86 const IR::Parameter *headers;
87 const IR::Parameter *user_metadata;
88 EBPFType *headerType;
89
91
92 std::map<cstring, EBPFValueSet *> valueSets;
93
94 explicit EBPFParser(const EBPFProgram *program, const IR::ParserBlock *block,
95 const P4::TypeMap *typeMap);
96 virtual void emitDeclaration(CodeBuilder *builder, const IR::Declaration *decl);
97 virtual void emit(CodeBuilder *builder);
98 virtual bool build();
99
100 virtual void emitTypes(CodeBuilder *builder);
101 virtual void emitValueSetInstances(CodeBuilder *builder);
102 virtual void emitRejectState(CodeBuilder *builder);
103
104 EBPFValueSet *getValueSet(cstring name) const { return ::P4::get(valueSets, name); }
105
106 DECLARE_TYPEINFO(EBPFParser, EBPFObject);
107};
108
109} // namespace P4::EBPF
110
111#endif /* BACKENDS_EBPF_EBPFPARSER_H_ */
Definition ebpf/codeGen.h:33
Definition ebpf/codeGen.h:41
Base class for EBPF objects.
Definition ebpfObject.h:31
Definition ebpfParser.h:79
Definition ebpfParser.h:67
Definition ebpfProgram.h:39
Base class for EBPF types.
Definition ebpfType.h:29
Definition ebpfTable.h:171
Definition ebpfParser.h:31
cstring selectValue
Stores the result of evaluating the select argument.
Definition ebpfParser.h:34
Definition methodInstance.h:194
Definition methodInstance.h:168
Definition coreLibrary.h:103
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
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