P4C
The P4 Compiler
Loading...
Searching...
No Matches
common/lib/util.h
1#ifndef BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_
2#define BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_
3
4#include <algorithm>
5#include <cstdint>
6#include <iterator>
7#include <optional>
8#include <ostream>
9#include <string>
10#include <vector>
11
12#include <boost/random/mersenne_twister.hpp>
13
14#include "ir/ir.h"
15
16namespace P4::P4Tools {
17
19class Utils {
20 /* =========================================================================================
21 * Seeds, timestamps, randomness.
22 * ========================================================================================= */
23 private:
25 static boost::random::mt19937 rng;
26
28 static std::optional<uint32_t> currentSeed;
29
30 public:
34 static std::string getTimeStamp();
35
38 static void setRandomSeed(int seed);
39
41 static std::optional<uint32_t> getCurrentSeed();
42
44 static uint64_t getRandInt(uint64_t max);
45
47 static int64_t getRandInt(int64_t min, int64_t max);
48
50 static int64_t getRandInt(const std::vector<int64_t> &percent);
51
54 static big_int getRandBigInt(const big_int &max);
55
57 static big_int getRandBigInt(const big_int &min, const big_int &max);
58
61 static const IR::Constant *getRandConstantForWidth(int bitWidth);
62
64 static const IR::Constant *getRandConstantForType(const IR::Type_Bits *type);
65
66 /* =========================================================================================
67 * Other.
68 * ========================================================================================= */
69 public:
72 static const IR::MethodCallExpression *generateInternalMethodCall(
73 std::string_view methodName, const std::vector<const IR::Expression *> &argVector,
74 const IR::Type *returnType = IR::Type_Void::get(),
75 const IR::ParameterList *paramList = new IR::ParameterList());
76
78 template <typename T>
79 static void shuffle(T *inp) {
80 std::shuffle(inp->begin(), inp->end(), rng);
81 }
82
84 template <typename Iter>
85 static Iter pickRandom(Iter start, Iter end) {
86 int random = getRandInt(std::distance(start, end) - 1);
87 std::advance(start, random);
88 return start;
89 }
90
93 template <typename ContainerType>
94 static std::string containerToString(const ContainerType &container) {
95 std::stringstream stringStream;
96
97 stringStream << '[';
98 auto val = container.cbegin();
99 if (val != container.cend()) {
100 stringStream << *val++;
101 while (val != container.cend()) {
102 stringStream << ", " << *val++;
103 }
104 }
105 stringStream << ']';
106 return stringStream.str();
107 }
108};
109
113std::vector<const IR::Type_Declaration *> argumentsToTypeDeclarations(
114 const IR::IGeneralNamespace *ns, const IR::Vector<IR::Argument> *inputArgs);
115
117const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path);
118
120const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
121 const IR::PathExpression *pathExpr);
122
124const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns,
125 const IR::Type_Name *type);
126
127} // namespace P4::P4Tools
128
129#endif /* BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_ */
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition vector.h:58
General utility functions that are not present in the compiler framework.
Definition common/lib/util.h:19
static uint64_t getRandInt(uint64_t max)
Definition common/lib/util.cpp:59
static Iter pickRandom(Iter start, Iter end)
Definition common/lib/util.h:85
static const IR::Constant * getRandConstantForWidth(int bitWidth)
Definition common/lib/util.cpp:106
static std::string getTimeStamp()
Definition common/lib/util.cpp:32
static const IR::MethodCallExpression * generateInternalMethodCall(std::string_view methodName, const std::vector< const IR::Expression * > &argVector, const IR::Type *returnType=IR::Type_Void::get(), const IR::ParameterList *paramList=new IR::ParameterList())
Definition common/lib/util.cpp:123
static big_int getRandBigInt(const big_int &max)
Definition common/lib/util.cpp:90
static std::optional< uint32_t > getCurrentSeed()
Definition common/lib/util.cpp:57
static void shuffle(T *inp)
Shuffles the given iterable.
Definition common/lib/util.h:79
static const IR::Constant * getRandConstantForType(const IR::Type_Bits *type)
Definition common/lib/util.cpp:113
static std::string containerToString(const ContainerType &container)
Definition common/lib/util.h:94
Definition common/compiler/compiler_result.cpp:3
const IR::IDeclaration * findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path)
Looks up a declaration from a path. A BUG occurs if no declaration is found.
Definition common/lib/util.cpp:177
const IR::Type_Declaration * resolveProgramType(const IR::IGeneralNamespace *ns, const IR::Type_Name *type)
Resolves a Type_Name in the top-level namespace.
Definition common/lib/util.cpp:191
std::vector< const IR::Type_Declaration * > argumentsToTypeDeclarations(const IR::IGeneralNamespace *ns, const IR::Vector< IR::Argument > *inputArgs)
Definition common/lib/util.cpp:138