cprover
endianness_map.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 "endianness_map.h"
10 
11 #include <ostream>
12 
13 #include "invariant.h"
14 #include "std_types.h"
15 #include "pointer_offset_size.h"
16 #include "arith_tools.h"
17 #include "namespace.h"
18 
19 void endianness_mapt::output(std::ostream &out) const
20 {
21  for(std::vector<size_t>::const_iterator it=map.begin();
22  it!=map.end();
23  ++it)
24  {
25  if(it!=map.begin())
26  out << ", ";
27  out << *it;
28  }
29 }
30 
31 void endianness_mapt::build(const typet &src, bool little_endian)
32 {
33  if(little_endian)
35  else
36  build_big_endian(src);
37 }
38 
40 {
41  mp_integer s=pointer_offset_bits(src, ns); // error is -1
42  if(s<=0)
43  return;
44 
45  std::size_t new_size=map.size()+integer2size_t(s);
46  map.reserve(new_size);
47 
48  for(std::size_t i=map.size(); i<new_size; ++i)
49  map.push_back(i);
50 }
51 
53 {
54  if(src.id()==ID_symbol)
56  else if(src.id()==ID_c_enum_tag)
58  else if(src.id()==ID_unsignedbv ||
59  src.id()==ID_signedbv ||
60  src.id()==ID_fixedbv ||
61  src.id()==ID_floatbv ||
62  src.id()==ID_c_enum ||
63  src.id()==ID_c_bit_field)
64  {
65  // these do get re-ordered!
66  mp_integer bits=pointer_offset_bits(src, ns); // error is -1
67  CHECK_RETURN(bits>=0);
68 
69  size_t bits_int=integer2size_t(bits), base=map.size();
70 
71  for(size_t bit=0; bit<bits_int; bit++)
72  {
73  map.push_back(base+bits_int-1-bit);
74  }
75  }
76  else if(src.id()==ID_struct)
77  {
78  const struct_typet &struct_type=to_struct_type(src);
79 
80  // todo: worry about padding being in wrong order
81  for(struct_typet::componentst::const_iterator
82  it=struct_type.components().begin();
83  it!=struct_type.components().end();
84  it++)
85  {
86  build_big_endian(it->type());
87  }
88  }
89  else if(src.id()==ID_array)
90  {
91  const array_typet &array_type=to_array_type(src);
92 
93  // array size constant?
94  mp_integer s;
95  if(!to_integer(array_type.size(), s))
96  {
97  while(s>0)
98  {
99  build_big_endian(array_type.subtype());
100  --s;
101  }
102  }
103  }
104  else if(src.id()==ID_vector)
105  {
106  const vector_typet &vector_type=to_vector_type(src);
107 
108  mp_integer s;
109  if(to_integer(vector_type.size(), s))
110  CHECK_RETURN(false);
111 
112  while(s>0)
113  {
114  build_big_endian(vector_type.subtype());
115  --s;
116  }
117  }
118  else
119  {
120  // everything else (unions in particular)
121  // is treated like a byte-array
122  mp_integer s=pointer_offset_bits(src, ns); // error is -1
123  if(s<=0)
124  return;
125 
126  std::size_t new_size=map.size()+integer2size_t(s);
127  map.reserve(new_size);
128 
129  for(std::size_t i=map.size(); i<new_size; ++i)
130  map.push_back(i);
131  }
132 }
The type of an expression.
Definition: type.h:22
BigInt mp_integer
Definition: mp_arith.h:22
const componentst & components() const
Definition: std_types.h:245
mp_integer pointer_offset_bits(const typet &type, const namespacet &ns)
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:245
Structure type.
Definition: std_types.h:297
const typet & follow_tag(const union_tag_typet &) const
Definition: namespace.cpp:74
void build(const typet &type, bool little_endian)
const irep_idt & id() const
Definition: irep.h:189
void output(std::ostream &) const
A constant-size array type.
Definition: std_types.h:1629
const exprt & size() const
Definition: std_types.h:1639
const vector_typet & to_vector_type(const typet &type)
Cast a generic typet to a vector_typet.
Definition: std_types.h:1660
const exprt & size() const
Definition: std_types.h:1014
const typet & follow(const typet &) const
Definition: namespace.cpp:55
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:318
virtual void build_big_endian(const typet &type)
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a generic typet to a c_enum_tag_typet.
Definition: std_types.h:747
Pointer Logic.
API to type classes.
std::vector< size_t > map
const array_typet & to_array_type(const typet &type)
Cast a generic typet to an array_typet.
Definition: std_types.h:1045
const namespacet & ns
arrays with given size
Definition: std_types.h:1004
bool to_integer(const exprt &expr, mp_integer &int_value)
Definition: arith_tools.cpp:17
std::size_t integer2size_t(const mp_integer &n)
Definition: mp_arith.cpp:195
const typet & subtype() const
Definition: type.h:33
virtual void build_little_endian(const typet &type)