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