cprover
boolbv_unary_minus.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/std_types.h>
12 
14 
15 #include "boolbv_type.h"
16 
18 {
19  const typet &type=ns.follow(expr.type());
20 
21  std::size_t width=boolbv_width(type);
22 
23  if(width==0)
24  return conversion_failed(expr);
25 
26  const exprt::operandst &operands=expr.operands();
27 
28  if(operands.size()!=1)
29  throw "unary minus takes one operand";
30 
31  const exprt &op0=expr.op0();
32 
33  const bvt &op_bv=convert_bv(op0);
34 
35  bvtypet bvtype=get_bvtype(type);
36  bvtypet op_bvtype=get_bvtype(op0.type());
37  std::size_t op_width=op_bv.size();
38 
39  bool no_overflow=(expr.id()=="no-overflow-unary-minus");
40 
41  if(op_width==0 || op_width!=width)
42  return conversion_failed(expr);
43 
44  if(bvtype==bvtypet::IS_UNKNOWN &&
45  (type.id()==ID_vector || type.id()==ID_complex))
46  {
47  const typet &subtype=ns.follow(type.subtype());
48 
49  std::size_t sub_width=boolbv_width(subtype);
50 
51  if(sub_width==0 || width%sub_width!=0)
52  throw "unary-: unexpected vector operand width";
53 
54  std::size_t size=width/sub_width;
55  bvt bv;
56  bv.resize(width);
57 
58  for(std::size_t i=0; i<size; i++)
59  {
60  bvt tmp_op;
61  tmp_op.resize(sub_width);
62 
63  for(std::size_t j=0; j<tmp_op.size(); j++)
64  {
65  assert(i*sub_width+j<op_bv.size());
66  tmp_op[j]=op_bv[i*sub_width+j];
67  }
68 
69  bvt tmp_result;
70 
71  if(type.subtype().id()==ID_floatbv)
72  {
73  float_utilst float_utils(prop, to_floatbv_type(subtype));
74  tmp_result=float_utils.negate(tmp_op);
75  }
76  else
77  tmp_result=bv_utils.negate(tmp_op);
78 
79  assert(tmp_result.size()==sub_width);
80 
81  for(std::size_t j=0; j<tmp_result.size(); j++)
82  {
83  assert(i*sub_width+j<bv.size());
84  bv[i*sub_width+j]=tmp_result[j];
85  }
86  }
87 
88  return bv;
89  }
90  else if(bvtype==bvtypet::IS_FIXED && op_bvtype==bvtypet::IS_FIXED)
91  {
92  if(no_overflow)
93  return bv_utils.negate_no_overflow(op_bv);
94  else
95  return bv_utils.negate(op_bv);
96  }
97  else if(bvtype==bvtypet::IS_FLOAT && op_bvtype==bvtypet::IS_FLOAT)
98  {
99  assert(!no_overflow);
100  float_utilst float_utils(prop, to_floatbv_type(expr.type()));
101  return float_utils.negate(op_bv);
102  }
103  else if((op_bvtype==bvtypet::IS_SIGNED || op_bvtype==bvtypet::IS_UNSIGNED) &&
104  (bvtype==bvtypet::IS_SIGNED || bvtype==bvtypet::IS_UNSIGNED))
105  {
106  if(no_overflow)
107  prop.l_set_to(bv_utils.overflow_negate(op_bv), false);
108 
109  if(no_overflow)
110  return bv_utils.negate_no_overflow(op_bv);
111  else
112  return bv_utils.negate(op_bv);
113  }
114 
115  return conversion_failed(expr);
116 }
The type of an expression.
Definition: type.h:22
bv_utilst bv_utils
Definition: boolbv.h:93
exprt & op0()
Definition: expr.h:72
boolbv_widtht boolbv_width
Definition: boolbv.h:90
typet & type()
Definition: expr.h:56
const irep_idt & id() const
Definition: irep.h:189
literalt overflow_negate(const bvt &op)
Definition: bv_utils.cpp:546
virtual const bvt & convert_bv(const exprt &expr)
Definition: boolbv.cpp:116
virtual void l_set_to(literalt a, bool value)
Definition: prop.h:44
bvt negate(const bvt &)
bvt negate(const bvt &op)
Definition: bv_utils.cpp:532
void conversion_failed(const exprt &expr, bvt &bv)
Definition: boolbv.h:108
Generic base class for unary expressions.
Definition: std_expr.h:303
const namespacet & ns
const typet & follow(const typet &) const
Definition: namespace.cpp:55
bvtypet get_bvtype(const typet &type)
Definition: boolbv_type.cpp:12
bvtypet
Definition: boolbv_type.h:16
std::vector< exprt > operandst
Definition: expr.h:45
API to type classes.
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a generic typet to a floatbv_typet.
Definition: std_types.h:1373
Base class for all expressions.
Definition: expr.h:42
bvt negate_no_overflow(const bvt &op)
Definition: bv_utils.cpp:540
virtual bvt convert_unary_minus(const unary_exprt &expr)
const typet & subtype() const
Definition: type.h:33
operandst & operands()
Definition: expr.h:66
std::vector< literalt > bvt
Definition: literal.h:200