cprover
boolbv_update.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "boolbv.h"
10 
11 #include <util/arith_tools.h>
12 
14 {
15  const exprt::operandst &ops=expr.operands();
16 
17  std::size_t width=boolbv_width(expr.type());
18 
19  if(width==0)
20  return conversion_failed(expr);
21 
22  bvt bv=convert_bv(ops[0]);
23 
24  if(bv.size()!=width)
25  throw "update: unexpected operand 0 width";
26 
27  // start the recursion
29  expr.op1().operands(), 0, expr.type(), 0, expr.op2(), bv);
30 
31  return bv;
32 }
33 
35  const exprt::operandst &designators,
36  std::size_t d,
37  const typet &type,
38  std::size_t offset,
39  const exprt &new_value,
40  bvt &bv)
41 {
42  if(d>=designators.size())
43  {
44  // done
45  bvt new_value_bv=convert_bv(new_value);
46  std::size_t new_value_width=boolbv_width(type);
47 
48  if(new_value_width!=new_value_bv.size())
49  throw "convert_update_rec: unexpected new_value size";
50 
51  // update
52  for(std::size_t i=0; i<new_value_width; i++)
53  {
54  assert(offset+i<bv.size());
55  bv[offset+i]=new_value_bv[i];
56  }
57 
58  return;
59  }
60 
61  const exprt &designator=designators[d];
62 
63  if(designator.id()==ID_index_designator)
64  {
65  if(type.id()!=ID_array)
66  throw "update: index designator needs array";
67 
68  if(designator.operands().size()!=1)
69  throw "update: index designator takes one operand";
70 
71  bvt index_bv = convert_bv(to_index_designator(designator).index());
72 
73  const array_typet &array_type=to_array_type(type);
74  const typet &subtype = array_type.subtype();
75  const exprt &size_expr = array_type.size();
76 
77  std::size_t element_size=boolbv_width(subtype);
78 
80  size_expr.id() == ID_constant,
81  "array in update expression should be constant-sized");
82 
83  // iterate over array
84  const std::size_t size =
85  numeric_cast_v<std::size_t>(to_constant_expr(size_expr));
86 
87  bvt tmp_bv=bv;
88 
89  for(std::size_t i = 0; i != size; ++i)
90  {
91  std::size_t new_offset=offset+i*element_size;
92 
94  designators, d+1, subtype, new_offset, new_value, tmp_bv);
95 
96  bvt const_bv=bv_utils.build_constant(i, index_bv.size());
97  literalt equal=bv_utils.equal(const_bv, index_bv);
98 
99  for(std::size_t j=0; j<element_size; j++)
100  {
101  std::size_t idx=new_offset+j;
102  assert(idx<bv.size());
103  bv[idx]=prop.lselect(equal, tmp_bv[idx], bv[idx]);
104  }
105  }
106  }
107  else if(designator.id()==ID_member_designator)
108  {
109  const irep_idt &component_name=designator.get(ID_component_name);
110 
111  if(ns.follow(type).id() == ID_struct)
112  {
113  const struct_typet &struct_type = to_struct_type(ns.follow(type));
114 
115  std::size_t struct_offset=0;
116 
118  component.make_nil();
119 
120  const struct_typet::componentst &components=
121  struct_type.components();
122 
123  for(const auto &c : components)
124  {
125  const typet &subtype = c.type();
126  std::size_t sub_width=boolbv_width(subtype);
127 
128  if(c.get_name() == component_name)
129  {
130  component = c;
131  break; // done
132  }
133 
134  struct_offset+=sub_width;
135  }
136 
137  if(component.is_nil())
138  throw "update: failed to find struct component";
139 
140  const typet &new_type = component.type();
141 
142  std::size_t new_offset=offset+struct_offset;
143 
144  // recursive call
146  designators, d+1, new_type, new_offset, new_value, bv);
147  }
148  else if(ns.follow(type).id() == ID_union)
149  {
150  const union_typet &union_type = to_union_type(ns.follow(type));
151 
153  union_type.get_component(component_name);
154 
155  if(component.is_nil())
156  throw "update: failed to find union component";
157 
158  // this only adjusts the type, the offset stays as-is
159 
160  const typet &new_type = component.type();
161 
162  // recursive call
164  designators, d+1, new_type, offset, new_value, bv);
165  }
166  else
167  throw "update: member designator needs struct or union";
168  }
169  else
170  throw "update: unexpected designator";
171 }
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:142
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
typet::subtype
const typet & subtype() const
Definition: type.h:47
arith_tools.h
struct_union_typet::get_component
const componentt & get_component(const irep_idt &component_name) const
Get the reference to a component with given name.
Definition: std_types.cpp:53
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
ternary_exprt::op2
exprt & op2()
Definition: expr.h:108
typet
The type of an expression, extends irept.
Definition: type.h:29
bvt
std::vector< literalt > bvt
Definition: literal.h:201
ternary_exprt::op1
exprt & op1()
Definition: expr.h:105
exprt
Base class for all expressions.
Definition: expr.h:53
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:135
component
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition: std_expr.cpp:192
array_typet::size
const exprt & size() const
Definition: std_types.h:973
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
boolbvt::boolbv_width
boolbv_widtht boolbv_width
Definition: boolbv.h:95
boolbvt::conversion_failed
void conversion_failed(const exprt &expr, bvt &bv)
Definition: boolbv.h:113
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
arrayst::ns
const namespacet & ns
Definition: arrays.h:51
boolbvt::convert_update_rec
void convert_update_rec(const exprt::operandst &designator, std::size_t d, const typet &type, std::size_t offset, const exprt &new_value, bvt &bv)
Definition: boolbv_update.cpp:34
irept::id
const irep_idt & id() const
Definition: irep.h:418
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:55
union_typet
The union type.
Definition: std_types.h:393
boolbvt::convert_bv
virtual const bvt & convert_bv(const exprt &expr, const optionalt< std::size_t > expected_width=nullopt)
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:119
update_exprt
Operator to update elements in structs and arrays.
Definition: std_expr.h:3234
struct_union_typet::componentt
Definition: std_types.h:64
bv_utilst::build_constant
bvt build_constant(const mp_integer &i, std::size_t width)
Definition: bv_utils.cpp:15
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:226
array_typet
Arrays with given size.
Definition: std_types.h:965
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:51
irept::get
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:51
boolbvt::bv_utils
bv_utilst bv_utils
Definition: boolbv.h:98
literalt
Definition: literal.h:26
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:1011
boolbv.h
exprt::operands
operandst & operands()
Definition: expr.h:95
to_union_type
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition: std_types.h:422
bv_utilst::equal
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1113
propt::lselect
virtual literalt lselect(literalt a, literalt b, literalt c)=0
prop_conv_solvert::prop
propt & prop
Definition: prop_conv_solver.h:131
boolbvt::convert_update
virtual bvt convert_update(const update_exprt &)
Definition: boolbv_update.cpp:13
to_index_designator
const index_designatort & to_index_designator(const exprt &expr)
Cast an exprt to an index_designatort.
Definition: std_expr.h:3165
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:3939