CBMC
byte_operators.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_BYTE_OPERATORS_H
11 #define CPROVER_UTIL_BYTE_OPERATORS_H
12 
20 #include "invariant.h"
21 #include "std_expr.h"
22 
29 {
30 public:
32  irep_idt _id,
33  const exprt &_op,
34  const exprt &_offset,
35  const typet &_type)
36  : binary_exprt(_op, _id, _offset, _type)
37  {
38  INVARIANT(
39  _id == ID_byte_extract_little_endian || _id == ID_byte_extract_big_endian,
40  "byte_extract_exprt: Invalid ID");
41  }
42 
43  exprt &op() { return op0(); }
44  exprt &offset() { return op1(); }
45 
46  const exprt &op() const { return op0(); }
47  const exprt &offset() const { return op1(); }
48 };
49 
50 template <>
51 inline bool can_cast_expr<byte_extract_exprt>(const exprt &base)
52 {
53  return base.id() == ID_byte_extract_little_endian ||
54  base.id() == ID_byte_extract_big_endian;
55 }
56 
57 inline const byte_extract_exprt &to_byte_extract_expr(const exprt &expr)
58 {
59  PRECONDITION(expr.operands().size() == 2);
60  return static_cast<const byte_extract_exprt &>(expr);
61 }
62 
64 {
65  PRECONDITION(expr.operands().size() == 2);
66  return static_cast<byte_extract_exprt &>(expr);
67 }
68 
69 inline void validate_expr(const byte_extract_exprt &value)
70 {
71  validate_operands(value, 2, "Byte extract must have two operands");
72 }
73 
78 {
79 public:
81  irep_idt _id,
82  const exprt &_op,
83  const exprt &_offset,
84  const exprt &_value)
85  : ternary_exprt(_id, _op, _offset, _value, _op.type())
86  {
87  INVARIANT(
88  _id == ID_byte_update_little_endian || _id == ID_byte_update_big_endian,
89  "byte_update_exprt: Invalid ID");
90  }
91 
92  void set_op(exprt e)
93  {
94  op0() = std::move(e);
95  }
96  void set_offset(exprt e)
97  {
98  op1() = std::move(e);
99  }
100  void set_value(exprt e)
101  {
102  op2() = std::move(e);
103  }
104 
105  const exprt &op() const { return op0(); }
106  const exprt &offset() const { return op1(); }
107  const exprt &value() const { return op2(); }
108 };
109 
110 template <>
111 inline bool can_cast_expr<byte_update_exprt>(const exprt &base)
112 {
113  return base.id() == ID_byte_update_little_endian ||
114  base.id() == ID_byte_update_big_endian;
115 }
116 
117 inline const byte_update_exprt &to_byte_update_expr(const exprt &expr)
118 {
119  PRECONDITION(expr.operands().size() == 3);
120  return static_cast<const byte_update_exprt &>(expr);
121 }
122 
124 {
125  PRECONDITION(expr.operands().size() == 3);
126  return static_cast<byte_update_exprt &>(expr);
127 }
128 
132 make_byte_extract(const exprt &_op, const exprt &_offset, const typet &_type);
133 
137 make_byte_update(const exprt &_op, const exprt &_offset, const exprt &_value);
138 
139 #endif // CPROVER_UTIL_BYTE_OPERATORS_H
make_byte_update
byte_update_exprt make_byte_update(const exprt &_op, const exprt &_offset, const exprt &_value)
Construct a byte_update_exprt with endianness and byte width matching the current configuration.
Definition: byte_operators.cpp:54
exprt::op2
exprt & op2()
Definition: expr.h:131
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
byte_update_exprt
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
Definition: byte_operators.h:77
byte_extract_exprt::op
const exprt & op() const
Definition: byte_operators.h:46
typet
The type of an expression, extends irept.
Definition: type.h:28
ternary_exprt
An expression with three operands.
Definition: std_expr.h:48
to_byte_extract_expr
const byte_extract_exprt & to_byte_extract_expr(const exprt &expr)
Definition: byte_operators.h:57
byte_update_exprt::offset
const exprt & offset() const
Definition: byte_operators.h:106
validate_operands
void validate_operands(const exprt &value, exprt::operandst::size_type number, const char *message, bool allow_more=false)
Definition: expr_cast.h:250
make_byte_extract
byte_extract_exprt make_byte_extract(const exprt &_op, const exprt &_offset, const typet &_type)
Construct a byte_extract_exprt with endianness and byte width matching the current configuration.
Definition: byte_operators.cpp:48
invariant.h
byte_update_exprt::set_value
void set_value(exprt e)
Definition: byte_operators.h:100
exprt
Base class for all expressions.
Definition: expr.h:55
binary_exprt
A base class for binary expressions.
Definition: std_expr.h:582
byte_update_exprt::set_op
void set_op(exprt e)
Definition: byte_operators.h:92
exprt::op0
exprt & op0()
Definition: expr.h:125
can_cast_expr< byte_update_exprt >
bool can_cast_expr< byte_update_exprt >(const exprt &base)
Definition: byte_operators.h:111
byte_extract_exprt::byte_extract_exprt
byte_extract_exprt(irep_idt _id, const exprt &_op, const exprt &_offset, const typet &_type)
Definition: byte_operators.h:31
can_cast_expr< byte_extract_exprt >
bool can_cast_expr< byte_extract_exprt >(const exprt &base)
Definition: byte_operators.h:51
validate_expr
void validate_expr(const byte_extract_exprt &value)
Definition: byte_operators.h:69
byte_extract_exprt::offset
const exprt & offset() const
Definition: byte_operators.h:47
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
byte_extract_exprt::op
exprt & op()
Definition: byte_operators.h:43
to_byte_update_expr
const byte_update_exprt & to_byte_update_expr(const exprt &expr)
Definition: byte_operators.h:117
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
exprt::op1
exprt & op1()
Definition: expr.h:128
irept::id
const irep_idt & id() const
Definition: irep.h:396
byte_extract_exprt::offset
exprt & offset()
Definition: byte_operators.h:44
byte_update_exprt::value
const exprt & value() const
Definition: byte_operators.h:107
byte_extract_exprt
Expression of type type extracted from some object op starting at position offset (given in number of...
Definition: byte_operators.h:28
exprt::operands
operandst & operands()
Definition: expr.h:94
std_expr.h
byte_update_exprt::op
const exprt & op() const
Definition: byte_operators.h:105
byte_update_exprt::byte_update_exprt
byte_update_exprt(irep_idt _id, const exprt &_op, const exprt &_offset, const exprt &_value)
Definition: byte_operators.h:80
validation_modet::INVARIANT
@ INVARIANT
byte_update_exprt::set_offset
void set_offset(exprt e)
Definition: byte_operators.h:96