P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
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 <utility>
11#include <vector>
12
13#include <boost/format.hpp>
14#include <boost/random/mersenne_twister.hpp>
15
16#include "ir/ir.h"
17#include "lib/big_int_util.h"
18#include "lib/cstring.h"
19#include "lib/log.h"
20
21namespace P4Tools {
22
24inline std::string logHelper(boost::format &f) { return f.str(); }
25
27template <class T, class... Args>
28std::string logHelper(boost::format &f, T &&t, Args &&...args) {
29 return logHelper(f % std::forward<T>(t), std::forward<Args>(args)...);
30}
31
34// https://stackoverflow.com/a/25859856
35template <typename... Arguments>
36void printFeature(const std::string &label, int level, const std::string &fmt,
37 Arguments &&...args) {
38 boost::format f(fmt);
39
40 auto result = logHelper(f, std::forward<Arguments>(args)...);
41
42 LOG_FEATURE(label.c_str(), level, result);
43}
44
46class Utils {
47 /* =========================================================================================
48 * Seeds, timestamps, randomness.
49 * ========================================================================================= */
50 private:
52 static boost::random::mt19937 rng;
53
55 static std::optional<uint32_t> currentSeed;
56
57 public:
61 static std::string getTimeStamp();
62
65 static void setRandomSeed(int seed);
66
68 static std::optional<uint32_t> getCurrentSeed();
69
71 static uint64_t getRandInt(uint64_t max);
72
75 static big_int getRandBigInt(big_int max);
76
79 static const IR::Constant *getRandConstantForWidth(int bitWidth);
80
82 static const IR::Constant *getRandConstantForType(const IR::Type_Bits *type);
83
84 /* =========================================================================================
85 * Other.
86 * ========================================================================================= */
87 public:
90 static const IR::MethodCallExpression *generateInternalMethodCall(
91 cstring methodName, const std::vector<const IR::Expression *> &argVector,
92 const IR::Type *returnType = IR::Type_Void::get(),
93 const IR::ParameterList *paramList = new IR::ParameterList());
94
96 template <typename T>
97 static void shuffle(T *inp) {
98 std::shuffle(inp->begin(), inp->end(), rng);
99 }
100
102 template <typename Iter>
103 static Iter pickRandom(Iter start, Iter end) {
104 int random = getRandInt(std::distance(start, end) - 1);
105 std::advance(start, random);
106 return start;
107 }
108
111 template <typename ContainerType>
112 static std::string containerToString(const ContainerType &container) {
113 std::stringstream stringStream;
114
115 stringStream << '[';
116 auto val = container.cbegin();
117 if (val != container.cend()) {
118 stringStream << *val++;
119 while (val != container.cend()) {
120 stringStream << ", " << *val++;
121 }
122 }
123 stringStream << ']';
124 return stringStream.str();
125 }
126};
127
131std::vector<const IR::Type_Declaration *> argumentsToTypeDeclarations(
132 const IR::IGeneralNamespace *ns, const IR::Vector<IR::Argument> *inputArgs);
133
135const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path);
136
138const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
139 const IR::PathExpression *pathExpr);
140
142const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns,
143 const IR::Type_Name *type);
144
145} // namespace P4Tools
146
147#endif /* BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_ */
Definition externInstance.h:33
General utility functions that are not present in the compiler framework.
Definition util.h:46
static Iter pickRandom(Iter start, Iter end)
Definition util.h:103
static void shuffle(T *inp)
Shuffles the given iterable.
Definition util.h:97
static uint64_t getRandInt(uint64_t max)
Definition util.cpp:59
static std::string getTimeStamp()
Definition util.cpp:32
static std::optional< uint32_t > getCurrentSeed()
Definition util.cpp:57
static const IR::Constant * getRandConstantForWidth(int bitWidth)
Definition util.cpp:75
static big_int getRandBigInt(big_int max)
Definition util.cpp:67
static const IR::Constant * getRandConstantForType(const IR::Type_Bits *type)
Definition util.cpp:82
static const IR::MethodCallExpression * generateInternalMethodCall(cstring methodName, const std::vector< const IR::Expression * > &argVector, const IR::Type *returnType=IR::Type_Void::get(), const IR::ParameterList *paramList=new IR::ParameterList())
Definition util.cpp:92
static std::string containerToString(const ContainerType &container)
Definition util.h:112
Definition cstring.h:72
Definition compiler_target.cpp:19
std::vector< const IR::Type_Declaration * > argumentsToTypeDeclarations(const IR::IGeneralNamespace *ns, const IR::Vector< IR::Argument > *inputArgs)
Definition util.cpp:107
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 util.cpp:146
void printFeature(const std::string &label, int level, const std::string &fmt, Arguments &&...args)
Definition util.h:36
std::string logHelper(boost::format &f)
Helper function for @printFeature.
Definition util.h:24
const IR::Type_Declaration * resolveProgramType(const IR::IGeneralNamespace *ns, const IR::Type_Name *type)
Resolves a Type_Name in the top-level namespace.
Definition util.cpp:158