P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
coreLibrary.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 FRONTENDS_P4_CORELIBRARY_H_
18#define FRONTENDS_P4_CORELIBRARY_H_
19
20#include "frontends/common/model.h"
21#include "ir/ir.h"
22#include "lib/cstring.h"
23
24namespace P4 {
25enum class StandardExceptions {
26 NoError,
27 PacketTooShort,
28 NoMatch,
29 StackOutOfBounds,
30 OverwritingHeader,
31 HeaderTooShort,
32 ParserTimeout,
33};
34} // namespace P4
35
36inline std::ostream &operator<<(std::ostream &out, P4::StandardExceptions e) {
37 switch (e) {
38 case P4::StandardExceptions::NoError:
39 out << "NoError";
40 break;
41 case P4::StandardExceptions::PacketTooShort:
42 out << "PacketTooShort";
43 break;
44 case P4::StandardExceptions::NoMatch:
45 out << "NoMatch";
46 break;
47 case P4::StandardExceptions::StackOutOfBounds:
48 out << "StackOutOfBounds";
49 break;
50 case P4::StandardExceptions::OverwritingHeader:
51 out << "OverwritingHeader";
52 break;
53 case P4::StandardExceptions::HeaderTooShort:
54 out << "HeaderTooShort";
55 break;
56 case P4::StandardExceptions::ParserTimeout:
57 out << "ParserTimeout";
58 break;
59 default:
60 BUG("Unhandled case");
61 }
62 return out;
63}
64
65namespace P4 {
66
68 public:
69 PacketIn()
70 : Extern_Model("packet_in"),
71 extract("extract"),
72 lookahead("lookahead"),
73 advance("advance"),
74 length("length") {}
75 Model::Elem extract;
76 Model::Elem lookahead;
77 Model::Elem advance;
78 Model::Elem length;
79 int extractSecondArgSize = 32;
80};
81
83 public:
84 PacketOut() : Extern_Model("packet_out"), emit("emit") {}
85 Model::Elem emit;
86};
87
89 public:
90 const StandardExceptions exc;
91 explicit P4Exception_Model(StandardExceptions exc) : ::Model::Elem(""), exc(exc) {
92 std::stringstream str;
93 str << exc;
94 name = str.str();
95 }
96};
97
98// Model of P4 core library
99// To be kept in sync with core.p4
101 protected:
102 // NOLINTBEGIN(bugprone-throw-keyword-missing)
104 : noAction("NoAction"),
105 exactMatch("exact"),
106 ternaryMatch("ternary"),
107 lpmMatch("lpm"),
108 noError(StandardExceptions::NoError),
109 packetTooShort(StandardExceptions::PacketTooShort),
110 noMatch(StandardExceptions::NoMatch),
111 stackOutOfBounds(StandardExceptions::StackOutOfBounds),
112 overwritingHeader(StandardExceptions::OverwritingHeader),
113 headerTooShort(StandardExceptions::HeaderTooShort) {}
114 // NOLINTEND(bugprone-throw-keyword-missing)
115
116 public:
117 static P4CoreLibrary &instance() {
118 static P4CoreLibrary *corelib = new P4CoreLibrary();
119 return *corelib;
120 }
121 ::Model::Elem noAction;
122
123 ::Model::Elem exactMatch;
124 ::Model::Elem ternaryMatch;
125 ::Model::Elem lpmMatch;
126
127 PacketIn packetIn;
128 PacketOut packetOut;
129
130 P4Exception_Model noError;
131 P4Exception_Model packetTooShort;
132 P4Exception_Model noMatch;
133 P4Exception_Model stackOutOfBounds;
134 P4Exception_Model overwritingHeader;
135 P4Exception_Model headerTooShort;
136};
137
138} // namespace P4
139
140#endif /* FRONTENDS_P4_CORELIBRARY_H_ */
Definition model.h:64
Definition coreLibrary.h:100
Definition coreLibrary.h:88
Definition coreLibrary.h:67
Definition coreLibrary.h:82
Definition applyOptionsPragmas.cpp:24
Definition model.h:28
Extern_Model : Type_Model.
Definition model.h:52