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 <random>
16#include <string>
17#include <vector>
18
19#include "ir/ir.h"
20
21namespace P4::P4Tools {
22
24class Utils {
25 /* =========================================================================================
26 * Seeds, timestamps, randomness.
27 * ========================================================================================= */
28 private:
30 static std::mt19937 rng;
31
33 static std::optional<uint32_t> currentSeed;
34
35 public:
39 static std::string getTimeStamp();
40
43 static void setRandomSeed(int seed);
44
46 static std::optional<uint32_t> getCurrentSeed();
47
49 static uint64_t getRandInt(uint64_t max);
50
52 static int64_t getRandInt(int64_t min, int64_t max);
53
55 static int64_t getRandInt(const std::vector<int64_t> &percent);
56
59 static big_int getRandBigInt(const big_int &max);
60
62 static big_int getRandBigInt(const big_int &min, const big_int &max);
63
66 static const IR::Constant *getRandConstantForWidth(int bitWidth);
67
69 static const IR::Constant *getRandConstantForType(const IR::Type_Bits *type);
70
71 /* =========================================================================================
72 * Other.
73 * ========================================================================================= */
74 public:
77 static const IR::MethodCallExpression *generateInternalMethodCall(
78 std::string_view methodName, const std::vector<const IR::Expression *> &argVector,
79 const IR::Type *returnType = IR::Type_Void::get(),
80 const IR::ParameterList *paramList = new IR::ParameterList());
81
83 template <typename T>
84 static void shuffle(T *inp) {
85 std::shuffle(inp->begin(), inp->end(), rng);
86 }
87
89 template <typename Iter>
90 static Iter pickRandom(Iter start, Iter end) {
91 int random = getRandInt(std::distance(start, end) - 1);
92 std::advance(start, random);
93 return start;
94 }
95
98 template <typename ContainerType>
99 static std::string containerToString(const ContainerType &container) {
100 std::stringstream stringStream;
101
102 stringStream << '[';
103 auto val = container.cbegin();
104 if (val != container.cend()) {
105 stringStream << *val++;
106 while (val != container.cend()) {
107 stringStream << ", " << *val++;
108 }
109 }
110 stringStream << ']';
111 return stringStream.str();
112 }
113};
114
118std::vector<const IR::Type_Declaration *> argumentsToTypeDeclarations(
119 const IR::IGeneralNamespace *ns, const IR::Vector<IR::Argument> *inputArgs);
120
122const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path);
123
125const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns,
126 const IR::PathExpression *pathExpr);
127
129const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns,
130 const IR::Type_Name *type);
131
132} // namespace P4::P4Tools
133
134#endif /* BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_ */
The Declaration interface, representing objects with names.
Definition declaration.h:17
Definition ir/vector.h:50
General utility functions that are not present in the compiler framework.
Definition common/lib/util.h:24
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:90
static const IR::Constant * getRandConstantForWidth(int bitWidth)
Definition common/lib/util.cpp:125
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:142
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:84
static const IR::Constant * getRandConstantForType(const IR::Type_Bits *type)
Definition common/lib/util.cpp:132
static std::string containerToString(const ContainerType &container)
Definition common/lib/util.h:99
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:195
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:209
std::vector< const IR::Type_Declaration * > argumentsToTypeDeclarations(const IR::IGeneralNamespace *ns, const IR::Vector< IR::Argument > *inputArgs)
Definition common/lib/util.cpp:157