P4C
The P4 Compiler
Loading...
Searching...
No Matches
irutils.h
1/*
2 * SPDX-FileCopyrightText: 2022 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef IR_IRUTILS_H_
8#define IR_IRUTILS_H_
9
10#include <initializer_list>
11#include <vector>
12
13#include "lib/source_file.h"
14
15namespace P4 {
16
17class Transform;
18
19namespace IR {
20
21// Forward-declare some IR classes that are used in function declarations.
22class BoolLiteral;
23class StringLiteral;
24class Constant;
25class Expression;
26class BaseListExpression;
27class Literal;
28class Node;
29class StatOrDecl;
30class StructExpression;
31class Type;
32class Type_Bits;
33template <typename T>
34class IndexedVector;
35
37//
38// Some of these are just thin wrappers around functions in the IR, but it's nice having everything
39// in one place.
40
41/* =========================================================================================
42 * Types
43 * ========================================================================================= */
44
46const Type_Bits *getBitTypeToFit(int value);
47
48/* =========================================================================================
49 * Expressions
50 * ========================================================================================= */
51
54const IR::Constant *getMaxValueConstant(const Type *t, const Util::SourceInfo &srcInfo = {});
55
73const IR::Expression *getDefaultValue(const Type *type, const Util::SourceInfo &srcInfo = {},
74 bool valueRequired = false);
75
78const IR::Constant *convertBoolLiteral(const IR::BoolLiteral *lit);
79
84std::vector<const Expression *> flattenStructExpression(const StructExpression *structExpr);
85
88std::vector<const Expression *> flattenListExpression(const BaseListExpression *listExpr);
89
93std::vector<const Expression *> flattenListOrStructExpression(const Expression *listLikeExpr);
94
95/* =========================================================================================
96 * Other helper functions
97 * ========================================================================================= */
98
100big_int getBigIntFromLiteral(const Literal *);
101
103int getIntFromLiteral(const Literal *);
104
107// This is 2^(t->size) - 1 for unsigned and 2^(t->size - 1) - 1 for signed.
108big_int getMaxBvVal(const Type *t);
109
111big_int getMaxBvVal(int bitWidth);
112
114// This is 0 for unsigned and -(2^(t->size - 1)) for signed.
115big_int getMinBvVal(const Type *t);
116
134const IR::Node *inlineBlock(const Transform &, std::initializer_list<const IR::StatOrDecl *>);
137
138} // namespace IR
139
140} // namespace P4
141
142#endif /* IR_IRUTILS_H_ */
Definition indexed_vector.h:31
Definition node.h:53
Definition visitor.h:442
Definition source_file.h:132
Definition constantParsing.h:22
std::vector< const Expression * > flattenListOrStructExpression(const Expression *listLikeExpr)
Definition irutils.cpp:221
const IR::Constant * getMaxValueConstant(const Type *t, const Util::SourceInfo &srcInfo)
Definition irutils.cpp:34
int getIntFromLiteral(const Literal *l)
Definition irutils.cpp:186
big_int getMinBvVal(const Type *t)
Definition irutils.cpp:211
const IR::Node * inlineBlock(const Transform &t, std::initializer_list< const IR::StatOrDecl * > stmts)
Definition irutils.cpp:252
const Type_Bits * getBitTypeToFit(int value)
Utility functions for generating IR nodes.
Definition irutils.cpp:24
const IR::Constant * convertBoolLiteral(const IR::BoolLiteral *lit)
Definition irutils.cpp:44
std::vector< const Expression * > flattenStructExpression(const StructExpression *structExpr)
Definition irutils.cpp:133
big_int getMaxBvVal(int bitWidth)
Definition irutils.cpp:199
big_int getBigIntFromLiteral(const Literal *l)
Definition irutils.cpp:176
std::vector< const Expression * > flattenListExpression(const BaseListExpression *listExpr)
Definition irutils.cpp:156
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13