P4C
The P4 Compiler
Loading...
Searching...
No Matches
scope.h
1#ifndef BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_SCOPE_H_
2#define BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_SCOPE_H_
3
4#include <cstddef>
5#include <map>
6#include <optional>
7#include <set>
8#include <vector>
9
10#include "ir/ir.h"
11#include "ir/node.h"
12#include "ir/vector.h"
13#include "lib/cstring.h"
14
15namespace P4::P4Tools::P4Smith {
16
17struct Requirements {
18 bool require_scalar{false};
19 bool compile_time_known{false};
20 bool no_methodcalls{false};
21 bool not_zero{false};
22 bool not_negative{false};
23 bool byte_align_headers{false};
24 int shift_width{8};
25 Requirements()
26
27 = default;
28};
29
30struct Constraints {
31 bool const_header_stack_index{false};
32 bool const_lshift_count{false};
33 bool single_stage_actions{false};
34 int max_phv_container_width{0};
35 Constraints()
36
37 = default;
38};
39
40struct Properties {
41 bool width_unknown{false};
42 bool has_methodcall{false};
43 bool in_action{false};
44 size_t depth = 0;
45 // This means we are in a block that returns.
46 // We need to return an expression with the specified type.
47 const IR::Type *ret_type = nullptr;
48 Properties() = default;
49};
50
51class P4Scope {
52 public:
54 static std::vector<IR::Vector<IR::Node> *> scope;
55
57 static std::set<cstring> usedNames;
58
60 static std::map<cstring, std::map<int, std::set<cstring>>> lvalMap;
61
63 static std::map<cstring, std::map<int, std::set<cstring>>> lvalMapRw;
64
67 static std::set<const IR::P4Table *> callableTables;
68
70 static std::set<cstring> notInitializedStructs;
71
75
78
81
82 P4Scope() = default;
83
84 ~P4Scope() = default;
85
86 static void addToScope(const IR::Node *n);
87 static void startLocalScope();
88 static void endLocalScope();
89
90 static void addLval(const IR::Type *tp, cstring name, bool read_only = false);
91 static bool checkLval(const IR::Type *tp, bool must_write = false);
92 static cstring pickLval(const IR::Type *tp, bool must_write = false);
93 static void deleteLval(const IR::Type *tp, cstring name);
94 static std::set<cstring> getCandidateLvals(const IR::Type *tp, bool must_write = true);
95 static bool hasWriteableLval(cstring typeKey);
96 static std::optional<std::map<int, std::set<cstring>>> getWriteableLvalForTypeKey(
97 cstring typeKey);
98
99 static const IR::Type_Bits *pickDeclaredBitType(bool must_write = false);
100
101 static const IR::Type_Declaration *getTypeByName(cstring name);
102
103 // template to get all declarations
104 // C++ is so shit... templates must be inlined to be generally usable.
105 template <typename T>
106 static std::vector<const T *> getDecls() {
107 std::vector<const T *> ret;
108
109 for (auto *subScope : scope) {
110 for (const auto *node : *subScope) {
111 if (const T *tmpObj = node->to<T>()) {
112 ret.push_back(tmpObj);
113 }
114 }
115 }
116 return ret;
117 }
118
119 static std::vector<const IR::Type_Declaration *> getFilteredDecls(std::set<cstring> filter);
120 static std::set<const IR::P4Table *> *getCallableTables();
121};
122} // namespace P4::P4Tools::P4Smith
123
124#endif /* BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_SCOPE_H_ */
Definition node.h:95
static std::set< const IR::P4Table * > callableTables
Definition scope.h:67
static std::set< cstring > notInitializedStructs
Structs that should not be initialized because they are incomplete.
Definition scope.h:70
static Properties prop
Definition scope.h:74
static std::map< cstring, std::map< int, std::set< cstring > > > lvalMapRw
A subset of the lval map that includes rw values.
Definition scope.h:63
static std::vector< IR::Vector< IR::Node > * > scope
This is a list of subscopes.
Definition scope.h:54
static Requirements req
Back-end or node-specific restrictions.
Definition scope.h:77
static std::map< cstring, std::map< int, std::set< cstring > > > lvalMap
This is a map of usable lvalues we store to be used for references.
Definition scope.h:60
static Constraints constraints
This defines all constraints specific to various targets or back-ends.
Definition scope.h:80
static std::set< cstring > usedNames
Maintain a set of names we have already used to avoid duplicates.
Definition scope.h:57
Definition cstring.h:85