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 
14  exprt vector1,
15  optionalt<exprt> vector2,
16  exprt::operandst indices)
18  ID_shuffle_vector,
19  {std::move(vector1), nil_exprt{}, exprt{}},
20  typet{})
21 {
22  if(vector2.has_value())
23  op1() = std::move(*vector2);
24 
25  const vector_typet &vt = to_vector_type(op0().type());
26  type() = vector_typet{
27  vt.index_type(),
28  vt.element_type(),
29  from_integer(indices.size(), vt.size().type())};
30 
31  op2().operands().swap(indices);
32 }
33 
35 {
37  !has_two_input_vectors() || vector1().type() == vector2().type());
38 
39  if(indices().empty())
40  return vector_exprt{exprt::operandst{}, type()};
41 
42  auto input_size =
43  numeric_cast<mp_integer>(to_vector_type(vector1().type()).size());
44  CHECK_RETURN(input_size.has_value());
45 
47  operands.reserve(indices().size());
48 
49  for(const exprt &index : indices())
50  {
52  {
53  operands.push_back(if_exprt{
55  index, ID_lt, from_integer(*input_size, index.type())},
56  index_exprt{vector1(), index},
58  vector2(),
59  minus_exprt{index, from_integer(*input_size, index.type())}}});
60  }
61  else
62  operands.push_back(index_exprt{vector1(), index});
63  }
64 
65  return vector_exprt{std::move(operands), type()};
66 }
multi_ary_exprt
A base class for multi-ary expressions Associativity is not specified.
Definition: std_expr.h:856
arith_tools.h
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
typet
The type of an expression, extends irept.
Definition: type.h:28
vector_typet::size
const constant_exprt & size() const
Definition: std_types.cpp:269
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2322
exprt
Base class for all expressions.
Definition: expr.h:55
vector_typet
The vector type.
Definition: std_types.h:1007
shuffle_vector_exprt::type
const vector_typet & type() const
Definition: c_expr.h:27
vector_exprt
Vector constructor from list of elements.
Definition: std_expr.h:1671
shuffle_vector_exprt::vector2
const exprt & vector2() const
Definition: c_expr.h:47
vector_typet::index_type
typet index_type() const
The type of any index expression into an instance of this type.
Definition: std_types.cpp:257
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
vector_typet::element_type
const typet & element_type() const
The type of the elements of the vector.
Definition: std_types.h:1026
binary_predicate_exprt
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition: std_expr.h:675
c_expr.h
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
nullary_exprt::operands
operandst & operands()=delete
remove all operand methods
nil_exprt
The NIL expression.
Definition: std_expr.h:3025
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:58
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
minus_exprt
Binary minus.
Definition: std_expr.h:1005
shuffle_vector_exprt::lower
vector_exprt lower() const
Definition: c_expr.cpp:34
shuffle_vector_exprt::indices
const exprt::operandst & indices() const
Definition: c_expr.h:62
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:100
shuffle_vector_exprt::vector1
const exprt & vector1() const
Definition: c_expr.h:37
to_vector_type
const vector_typet & to_vector_type(const typet &type)
Cast a typet to a vector_typet.
Definition: std_types.h:1060
exprt::operands
operandst & operands()
Definition: expr.h:94
index_exprt
Array index operator.
Definition: std_expr.h:1409
shuffle_vector_exprt::has_two_input_vectors
bool has_two_input_vectors() const
Definition: c_expr.h:57
shuffle_vector_exprt::shuffle_vector_exprt
shuffle_vector_exprt(exprt vector1, optionalt< exprt > vector2, exprt::operandst indices)
Definition: c_expr.cpp:13