P4C
The P4 Compiler
Loading...
Searching...
No Matches
coreLibrary.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_CORELIBRARY_H_
9#define FRONTENDS_P4_CORELIBRARY_H_
10
11#include "frontends/common/model.h"
12#include "ir/ir.h"
13#include "lib/cstring.h"
14
15namespace P4 {
16enum class StandardExceptions {
17 NoError,
18 PacketTooShort,
19 NoMatch,
20 StackOutOfBounds,
21 HeaderTooShort,
22 ParserTimeout,
23};
24} // namespace P4
25
26namespace P4 {
27
28inline std::ostream &operator<<(std::ostream &out, P4::StandardExceptions e) {
29 switch (e) {
30 case P4::StandardExceptions::NoError:
31 out << "NoError";
32 break;
33 case P4::StandardExceptions::PacketTooShort:
34 out << "PacketTooShort";
35 break;
36 case P4::StandardExceptions::NoMatch:
37 out << "NoMatch";
38 break;
39 case P4::StandardExceptions::StackOutOfBounds:
40 out << "StackOutOfBounds";
41 break;
42 case P4::StandardExceptions::HeaderTooShort:
43 out << "HeaderTooShort";
44 break;
45 case P4::StandardExceptions::ParserTimeout:
46 out << "ParserTimeout";
47 break;
48 default:
49 BUG("Unhandled case");
50 }
51 return out;
52}
53
54} // namespace P4
55
56namespace P4 {
57
58using namespace literals;
59
60class PacketIn : public Model::Extern_Model {
61 public:
62 PacketIn()
63 : Extern_Model("packet_in"_cs),
64 extract("extract"_cs),
65 lookahead("lookahead"_cs),
66 advance("advance"_cs),
67 length("length"_cs) {}
68 Model::Elem extract;
69 Model::Elem lookahead;
70 Model::Elem advance;
71 Model::Elem length;
72 int extractSecondArgSize = 32;
73};
74
75class PacketOut : public Model::Extern_Model {
76 public:
77 PacketOut() : Extern_Model("packet_out"_cs), emit("emit"_cs) {}
78 Model::Elem emit;
79};
80
81class P4Exception_Model : public ::P4::Model::Elem {
82 public:
83 const StandardExceptions exc;
84 explicit P4Exception_Model(StandardExceptions exc)
85 : ::P4::Model::Elem(cstring::empty), exc(exc) {
86 std::stringstream str;
87 str << exc;
88 name = str.str();
89 }
90};
91
92// Model of P4 core library
93// To be kept in sync with core.p4
94class P4CoreLibrary : public ::P4::Model::Model {
95 protected:
96 // NOLINTBEGIN(bugprone-throw-keyword-missing)
97 P4CoreLibrary()
98 : noAction("NoAction"_cs),
99 exactMatch("exact"_cs),
100 ternaryMatch("ternary"_cs),
101 lpmMatch("lpm"_cs),
102 noError(StandardExceptions::NoError),
103 packetTooShort(StandardExceptions::PacketTooShort),
104 noMatch(StandardExceptions::NoMatch),
105 stackOutOfBounds(StandardExceptions::StackOutOfBounds),
106 headerTooShort(StandardExceptions::HeaderTooShort) {}
107 // NOLINTEND(bugprone-throw-keyword-missing)
108
109 public:
110 static P4CoreLibrary &instance() {
111 static P4CoreLibrary *corelib = new P4CoreLibrary();
112 return *corelib;
113 }
114 ::P4::Model::Elem noAction;
115
116 ::P4::Model::Elem exactMatch;
117 ::P4::Model::Elem ternaryMatch;
118 ::P4::Model::Elem lpmMatch;
119
120 PacketIn packetIn;
121 PacketOut packetOut;
122
123 P4Exception_Model noError;
124 P4Exception_Model packetTooShort;
125 P4Exception_Model noMatch;
126 P4Exception_Model stackOutOfBounds;
127 P4Exception_Model headerTooShort;
128};
129
130} // namespace P4
131
132#endif /* FRONTENDS_P4_CORELIBRARY_H_ */
Definition frontends/common/model.h:55
Definition coreLibrary.h:81
Definition coreLibrary.h:60
Definition coreLibrary.h:75
Definition cstring.h:71
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
Definition frontends/common/model.h:19
Extern_Model : Type_Model.
Definition frontends/common/model.h:43