P4C
The P4 Compiler
Loading...
Searching...
No Matches
common/lib/util.h
1/*
2 * SPDX-FileCopyrightText: 2022 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_
8#define BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_
9
10#include <algorithm>
11#include <cstdint>
12#include <iterator>
13#include <optional>
14#include <ostream>
15#include <string>
16#include <vector>
17
18#include <boost/random/mersenne_twister.hpp>
19
20#include "ir/ir.h"
21
22namespace P4::P4Tools {
23
25class Utils {
26 /* =========================================================================================
27 * Seeds, timestamps, randomness.
28 * ========================================================================================= */
29 private:
31 static boost::random::mt19937 rng;
32
34 static std::optional<uint32_t> currentSeed;
35
36 public:
40 static std::string getTimeStamp();
41
44 static void setRandomSeed(int seed);
45
47 static std::optional<uint32_t> getCurrentSeed();
48
50 static uint64_t getRandInt(uint64_t max);
51
53 static int64_t getRandInt(int64_t min, int64_t max);
54
56 static int64_t getRandInt(const std::vector<int64_t> &percent);
57
60 static big_int getRandBigInt(const big_int &max);
61
63 static big_int getRandBigInt(const big_int &min, const big_int &max);
64
67 static const IR::Constant *getRandConstantForWidth(int bitWidth);
68
70 static const IR::Constant *getRandConstantForType(const IR::Type_Bits *type);
71
72 /* =========================================================================================
73 * Other.
74 * ========================================================================================= */
75 public:
78 static const IR::MethodCallExpression *generateInternalMethodCall(
79 std::string_view methodName, const std::vector<const IR::Expression *> &argVector,
80 const IR::Type *returnType = IR::Type_Void::get(),
81 const IR::ParameterList *paramList = new IR::ParameterList());
82
84 template <typename T>
85 static void shuffle(T *inp) {
86 std::shuffle(inp->begin(), inp->end(), rng);
87 }
88
90 template <typename Iter>
91 static Iter pickRandom(Iter start, Iter end) {
92 int random = getRandInt(std::distance(start, end) - 1);
93 std::advance(start, random);
94 return start;
95 }
96
99 template <typename ContainerType>
100 static std::string containerToString(const ContainerType &container) {
101 std::stringstream stringStream;
102
103 stringStream << '[';
104 auto val = container.cbegin();
105 if (val != container.cend()) {
106 stringStream << *val++;
107 while (val != container.cend()) {
108 stringStream << ", " << *val++;
109 }
110 }
111 stringStream << ']';
112 return stringStream.str();
113 }
114};
115
119std::vector<const IR::Type_Declaration *> argumentsToTypeDeclarations(
120 const IR::IGeneralNamespace *ns, const IR::Vector<IR::Argument> *inputArgs);
121
123const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path);
124
126const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
127 const IR::PathExpression *pathExpr);
128
130const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns,
131 const IR::Type_Name *type);
132
133} // namespace P4::P4Tools
134
135#endif /* BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_ */
The Declaration interface, representing objects with names.
Definition declaration.h:17
Definition ir/vector.h:59
General utility functions that are not present in the compiler framework.
Definition common/lib/util.h:25
static uint64_t getRandInt(uint64_t max)
Definition common/lib/util.cpp:63
static Iter pickRandom(Iter start, Iter end)
Definition common/lib/util.h:91
static const IR::Constant * getRandConstantForWidth(int bitWidth)
Definition common/lib/util.cpp:110
static std::string getTimeStamp()
Definition common/lib/util.cpp:36
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:127
static big_int getRandBigInt(const big_int &max)
Definition common/lib/util.cpp:94
static std::optional< uint32_t > getCurrentSeed()
Definition common/lib/util.cpp:61
static void shuffle(T *inp)
Shuffles the given iterable.
Definition common/lib/util.h:85
static const IR::Constant * getRandConstantForType(const IR::Type_Bits *type)
Definition common/lib/util.cpp:117
static std::string containerToString(const ContainerType &container)
Definition common/lib/util.h:100
Definition common/compiler/compiler_result.cpp:7
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:180
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:194
std::vector< const IR::Type_Declaration * > argumentsToTypeDeclarations(const IR::IGeneralNamespace *ns, const IR::Vector< IR::Argument > *inputArgs)
Definition common/lib/util.cpp:142