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
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};
26
27 = default;
28};
29
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 // This is the maximum bitwidth that
36 // can be specified for different targets.
37 int max_bitwidth{128};
39
40 = default;
41};
42
43struct Properties {
44 bool width_unknown{false};
45 bool has_methodcall{false};
46 bool in_action{false};
47 size_t depth = 0;
48 // This means we are in a block that returns.
49 // We need to return an expression with the specified type.
50 const IR::Type *ret_type = nullptr;
51 Properties() = default;
52};
53
54class P4Scope {
55 public:
57 static std::vector<IR::Vector<IR::Node> *> scope;
58
60 static std::set<cstring> usedNames;
61
63 static std::map<cstring, std::map<int, std::set<cstring>>> lvalMap;
64
66 static std::map<cstring, std::map<int, std::set<cstring>>> lvalMapRw;
67
70 static std::set<const IR::P4Table *> callableTables;
71
73 static std::set<cstring> notInitializedStructs;
74
78
81
84
85 P4Scope() = default;
86
87 ~P4Scope() = default;
88
89 static void addToScope(const IR::Node *n);
90 static void startLocalScope();
91 static void endLocalScope();
92
93 static void addLval(const IR::Type *tp, cstring name, bool read_only = false);
94 static bool checkLval(const IR::Type *tp, bool must_write = false);
95 static cstring pickLval(const IR::Type *tp, bool must_write = false);
96 static void deleteLval(const IR::Type *tp, cstring name);
97 static std::set<cstring> getCandidateLvals(const IR::Type *tp, bool must_write = true);
98 static bool hasWriteableLval(cstring typeKey);
99 static std::optional<std::map<int, std::set<cstring>>> getWriteableLvalForTypeKey(
100 cstring typeKey);
101
102 static const IR::Type_Bits *pickDeclaredBitType(bool must_write = false);
103
104 static const IR::Type_Declaration *getTypeByName(cstring name);
105
106 // template to get all declarations
107 // C++ is so shit... templates must be inlined to be generally usable.
108 template <typename T>
109 static std::vector<const T *> getDecls() {
110 std::vector<const T *> ret;
111
112 for (auto *subScope : scope) {
113 for (const auto *node : *subScope) {
114 if (const T *tmpObj = node->to<T>()) {
115 ret.push_back(tmpObj);
116 }
117 }
118 }
119 return ret;
120 }
121
122 static std::vector<const IR::Type_Declaration *> getFilteredDecls(std::set<cstring> filter);
123 static std::set<const IR::P4Table *> *getCallableTables();
124};
125} // namespace P4::P4Tools::P4Smith
126
127#endif /* BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_SCOPE_H_ */
Definition node.h:94
Definition scope.h:54
static std::set< const IR::P4Table * > callableTables
Definition scope.h:70
static std::set< cstring > notInitializedStructs
Structs that should not be initialized because they are incomplete.
Definition scope.h:73
static Properties prop
Definition scope.h:77
static std::map< cstring, std::map< int, std::set< cstring > > > lvalMapRw
A subset of the lval map that includes rw values.
Definition scope.h:66
static std::vector< IR::Vector< IR::Node > * > scope
This is a list of subscopes.
Definition scope.h:57
static Requirements req
Back-end or node-specific restrictions.
Definition scope.h:80
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:63
static Constraints constraints
This defines all constraints specific to various targets or back-ends.
Definition scope.h:83
static std::set< cstring > usedNames
Maintain a set of names we have already used to avoid duplicates.
Definition scope.h:60
Definition cstring.h:85
Definition tofino/bf-p4c/phv/constraints/constraints.cpp:21