CBMC
c_expr.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: API to expression classes that are internal to the C frontend
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "c_expr.h"
10 
11 #include <util/arith_tools.h>
12 #include <util/c_types.h>
13 #include <util/namespace.h>
14 #include <util/simplify_expr.h>
15 
17  exprt vector1,
18  std::optional<exprt> vector2,
19  exprt::operandst indices)
21  ID_shuffle_vector,
22  {std::move(vector1), nil_exprt{}, exprt{}},
23  typet{})
24 {
25  if(vector2.has_value())
26  op1() = std::move(*vector2);
27 
28  const vector_typet &vt = to_vector_type(op0().type());
29  type() = vector_typet{
30  vt.index_type(),
31  vt.element_type(),
32  from_integer(indices.size(), vt.size().type())};
33 
34  op2().operands().swap(indices);
35 }
36 
38 {
40  !has_two_input_vectors() || vector1().type() == vector2().type());
41 
42  if(indices().empty())
43  return vector_exprt{exprt::operandst{}, type()};
44 
45  auto input_size =
46  numeric_cast<mp_integer>(to_vector_type(vector1().type()).size());
47  CHECK_RETURN(input_size.has_value());
48 
50  operands.reserve(indices().size());
51 
52  for(const exprt &index : indices())
53  {
55  {
56  operands.push_back(if_exprt{
58  index, ID_lt, from_integer(*input_size, index.type())},
59  index_exprt{vector1(), index},
61  vector2(),
62  minus_exprt{index, from_integer(*input_size, index.type())}}});
63  }
64  else
65  operands.push_back(index_exprt{vector1(), index});
66  }
67 
68  return vector_exprt{std::move(operands), type()};
69 }
70 
72 {
73  const auto &enum_type = to_c_enum_tag_type(op().type());
74  const c_enum_typet &c_enum_type = ns.follow_tag(enum_type);
75  const c_enum_typet::memberst enum_values = c_enum_type.members();
76 
77  exprt::operandst disjuncts;
78  for(const auto &enum_value : enum_values)
79  {
80  constant_exprt val{enum_value.get_value(), enum_type};
81  disjuncts.push_back(equal_exprt(op(), std::move(val)));
82  }
83 
84  return simplify_expr(disjunction(disjuncts), ns);
85 }
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
API to expression classes that are internal to the C frontend.
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a typet to a c_enum_tag_typet.
Definition: c_types.h:377
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition: std_expr.h:731
The type of C enums.
Definition: c_types.h:239
memberst & members()
Definition: c_types.h:283
std::vector< c_enum_membert > memberst
Definition: c_types.h:275
A constant literal expression.
Definition: std_expr.h:2987
const irep_idt & get_value() const
Definition: std_expr.h:2995
exprt lower(const namespacet &ns) const
Definition: c_expr.cpp:71
Equality.
Definition: std_expr.h:1361
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
The trinary if-then-else operator.
Definition: std_expr.h:2370
Array index operator.
Definition: std_expr.h:1465
Binary minus.
Definition: std_expr.h:1061
A base class for multi-ary expressions Associativity is not specified.
Definition: std_expr.h:912
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:63
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
The NIL expression.
Definition: std_expr.h:3073
operandst & operands()=delete
remove all operand methods
const vector_typet & type() const
Definition: c_expr.h:27
vector_exprt lower() const
Definition: c_expr.cpp:37
shuffle_vector_exprt(exprt vector1, std::optional< exprt > vector2, exprt::operandst indices)
Definition: c_expr.cpp:16
const exprt & vector2() const
Definition: c_expr.h:47
const exprt & vector1() const
Definition: c_expr.h:37
const exprt::operandst & indices() const
Definition: c_expr.h:62
bool has_two_input_vectors() const
Definition: c_expr.h:57
The type of an expression, extends irept.
Definition: type.h:29
const exprt & op() const
Definition: std_expr.h:391
Vector constructor from list of elements.
Definition: std_expr.h:1729
The vector type.
Definition: std_types.h:1052
const constant_exprt & size() const
Definition: std_types.cpp:275
const typet & element_type() const
The type of the elements of the vector.
Definition: std_types.h:1068
typet index_type() const
The type of any index expression into an instance of this type.
Definition: std_types.cpp:263
exprt simplify_expr(exprt src, const namespacet &ns)
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
exprt disjunction(const exprt::operandst &op)
1) generates a disjunction for two or more operands 2) for one operand, returns the operand 3) return...
Definition: std_expr.cpp:54
const vector_typet & to_vector_type(const typet &type)
Cast a typet to a vector_typet.
Definition: std_types.h:1104