P4C
The P4 Compiler
Loading...
Searching...
No Matches
irutils.h
1#ifndef IR_IRUTILS_H_
2#define IR_IRUTILS_H_
3
4#include <initializer_list>
5#include <vector>
6
7#include "lib/big_int_util.h"
8#include "lib/source_file.h"
9
10class Transform;
11
12namespace P4::IR {
13
14// Forward-declare some IR classes that are used in function declarations.
15class BoolLiteral;
16class StringLiteral;
17class Constant;
18class Expression;
19class BaseListExpression;
20class Literal;
21class Node;
22class StatOrDecl;
23class StructExpression;
24class Type;
25class Type_Bits;
26template <typename T>
27class IndexedVector;
28
30//
31// Some of these are just thin wrappers around functions in the IR, but it's nice having everything
32// in one place.
33
34/* =========================================================================================
35 * Types
36 * ========================================================================================= */
37
39const Type_Bits *getBitTypeToFit(int value);
40
41/* =========================================================================================
42 * Expressions
43 * ========================================================================================= */
44
47const IR::Constant *getMaxValueConstant(const Type *t, const Util::SourceInfo &srcInfo = {});
48
66const IR::Expression *getDefaultValue(const Type *type, const Util::SourceInfo &srcInfo = {},
67 bool valueRequired = false);
68
71const IR::Constant *convertBoolLiteral(const IR::BoolLiteral *lit);
72
77std::vector<const Expression *> flattenStructExpression(const StructExpression *structExpr);
78
81std::vector<const Expression *> flattenListExpression(const BaseListExpression *listExpr);
82
86std::vector<const Expression *> flattenListOrStructExpression(const Expression *listLikeExpr);
87
88/* =========================================================================================
89 * Other helper functions
90 * ========================================================================================= */
91
93big_int getBigIntFromLiteral(const Literal *);
94
96int getIntFromLiteral(const Literal *);
97
100// This is 2^(t->size) - 1 for unsigned and 2^(t->size - 1) - 1 for signed.
101big_int getMaxBvVal(const Type *t);
102
104big_int getMaxBvVal(int bitWidth);
105
107// This is 0 for unsigned and -(2^(t->size - 1)) for signed.
108big_int getMinBvVal(const Type *t);
109
127const IR::Node *inlineBlock(const Transform &, std::initializer_list<const IR::StatOrDecl *>);
128const IR::Node *inlineBlock(const Transform &, const IR::IndexedVector<IR::StatOrDecl> &);
129const IR::Node *inlineBlock(const Transform &, IR::IndexedVector<IR::StatOrDecl> &&);
130
131} // namespace P4::IR
132
133#endif /* IR_IRUTILS_H_ */