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;
33class Type_Extern;
34template <typename T>
35class IndexedVector;
36
38//
39// Some of these are just thin wrappers around functions in the IR, but it's nice having everything
40// in one place.
41
42/* =========================================================================================
43 * Types
44 * ========================================================================================= */
45
47const Type_Bits *getBitTypeToFit(int value);
48
52const Type_Extern *getExternType(const Type *type);
53
54/* =========================================================================================
55 * Expressions
56 * ========================================================================================= */
57
60const IR::Constant *getMaxValueConstant(const Type *t, const Util::SourceInfo &srcInfo = {});
61
79const IR::Expression *getDefaultValue(const Type *type, const Util::SourceInfo &srcInfo = {},
80 bool valueRequired = false);
81
84const IR::Constant *convertBoolLiteral(const IR::BoolLiteral *lit);
85
90std::vector<const Expression *> flattenStructExpression(const StructExpression *structExpr);
91
94std::vector<const Expression *> flattenListExpression(const BaseListExpression *listExpr);
95
99std::vector<const Expression *> flattenListOrStructExpression(const Expression *listLikeExpr);
100
101/* =========================================================================================
102 * Other helper functions
103 * ========================================================================================= */
104
106big_int getBigIntFromLiteral(const Literal *);
107
109int getIntFromLiteral(const Literal *);
110
113// This is 2^(t->size) - 1 for unsigned and 2^(t->size - 1) - 1 for signed.
114big_int getMaxBvVal(const Type *t);
115
117big_int getMaxBvVal(int bitWidth);
118
120// This is 0 for unsigned and -(2^(t->size - 1)) for signed.
121big_int getMinBvVal(const Type *t);
122
140const IR::Node *inlineBlock(const Transform &, std::initializer_list<const IR::StatOrDecl *>);
143
144} // namespace IR
145
146} // namespace P4
147
148#endif /* IR_IRUTILS_H_ */
Definition indexed_vector.h:31
Definition node.h:44
Definition visitor.h:433
Definition source_file.h:123
Definition constantParsing.h:13
std::vector< const Expression * > flattenListOrStructExpression(const Expression *listLikeExpr)
Definition irutils.cpp:236
const IR::Constant * getMaxValueConstant(const Type *t, const Util::SourceInfo &srcInfo)
Definition irutils.cpp:48
int getIntFromLiteral(const Literal *l)
Definition irutils.cpp:201
big_int getMinBvVal(const Type *t)
Definition irutils.cpp:226
const IR::Node * inlineBlock(const Transform &t, std::initializer_list< const IR::StatOrDecl * > stmts)
Definition irutils.cpp:267
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:58
const Type_Extern * getExternType(const Type *type)
Definition irutils.cpp:30
std::vector< const Expression * > flattenStructExpression(const StructExpression *structExpr)
Definition irutils.cpp:148
big_int getMaxBvVal(int bitWidth)
Definition irutils.cpp:214
big_int getBigIntFromLiteral(const Literal *l)
Definition irutils.cpp:191
std::vector< const Expression * > flattenListExpression(const BaseListExpression *listExpr)
Definition irutils.cpp:171
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13