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