P4C
The P4 Compiler
Loading...
Searching...
No Matches
symbolic_bitvec.h
1
19#ifndef BF_P4C_PHV_SOLVER_SYMBOLIC_BITVEC_H_
20#define BF_P4C_PHV_SOLVER_SYMBOLIC_BITVEC_H_
21
22#include <sstream>
23#include <vector>
24
25#include "lib/bitvec.h"
26#include "lib/cstring.h"
27
28namespace solver {
29
30namespace symbolic_bitvec {
31
32using namespace P4;
33
39using BitID = int;
40
41enum ExprNodeType { BIT_NODE, AND_NODE, OR_NODE, NEG_NODE };
42
43class Expr {
44 public:
45 const Expr *left;
46 const Expr *right;
47 ExprNodeType type;
48 BitID value;
49
50 Expr(const Expr *left, const Expr *right, ExprNodeType type, BitID value = -1);
51 Expr(ExprNodeType type, BitID value, const Expr *left = nullptr, const Expr *right = nullptr);
52 bool eq(const Expr *other) const;
53 cstring to_cstring() const;
54
55 private:
56 void simplify();
57};
58
59// Bitvec is a vector of Bits. The size is fixed after construction.
60class BitVec {
61 private:
62 std::vector<const Expr *> bits;
63
64 private:
65 void size_check(const BitVec &other) const;
66
67 BitVec bin_op(const BitVec &other, ExprNodeType type) const;
68
69 public:
70 explicit BitVec(std::vector<const Expr *> &bits) : bits(bits) {}
71 void set(int i, bool value) { bits[i] = new Expr(ExprNodeType::BIT_NODE, int(value)); }
72 const Expr *get(int i) const { return bits[i]; }
73 BitVec slice(int start, int sz) const;
74 bool eq(const BitVec &other) const;
75 cstring to_cstring() const;
76 BitVec bv_and(const BitVec &other) const;
77 BitVec bv_or(const BitVec &other) const;
78 BitVec bv_neg() const;
79 BitVec rotate_right(int amount) const;
80 BitVec rotate_left(int amount) const;
81 bool operator==(const BitVec &other) const { return eq(other); }
82 bool operator!=(const BitVec &other) const { return !eq(other); }
83 BitVec operator<<(int v) const { return rotate_left(v); }
84 BitVec operator>>(int v) const { return rotate_right(v); }
85 BitVec operator~() const { return bv_neg(); }
86 BitVec operator&(const BitVec &other) const { return bv_and(other); }
87 BitVec operator|(const BitVec &other) const { return bv_or(other); }
88};
89
90// BvContext maintains a context for symbolic bit vectors and bit expressions created.
91// Expressions and BitVecs are comparable only if they were created by the same context.
92class BvContext {
93 private:
94 BitID uid = 1; // 0 and 1 are reserved for const zero and one.
95 BitID new_uid() { return ++uid; };
96
97 public:
98 BitVec new_bv(int sz);
99 BitVec new_bv_const(int sz, bitvec val);
100};
101
102} // namespace symbolic_bitvec
103
104} // namespace solver
105
106#endif /* BF_P4C_PHV_SOLVER_SYMBOLIC_BITVEC_H_ */
Definition bitvec.h:120
Definition cstring.h:85
Definition symbolic_bitvec.h:60
Definition symbolic_bitvec.h:92
Definition symbolic_bitvec.h:43
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition phv/solver/action_constraint_solver.cpp:33