CBMC
ssa_expr.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 "ssa_expr.h"
10 
11 #include <sstream>
12 
13 #include "pointer_expr.h"
14 
21 static std::ostream &
22 initialize_ssa_identifier(std::ostream &os, const exprt &expr)
23 {
24  if(auto member = expr_try_dynamic_cast<member_exprt>(expr))
25  {
26  return initialize_ssa_identifier(os, member->struct_op())
27  << ".." << member->get_component_name();
28  }
29  if(auto index = expr_try_dynamic_cast<index_exprt>(expr))
30  {
31  const irep_idt &idx = to_constant_expr(index->index()).get_value();
32  return initialize_ssa_identifier(os, index->array()) << "[[" << idx << "]]";
33  }
34  if(auto symbol = expr_try_dynamic_cast<symbol_exprt>(expr))
35  return os << symbol->get_identifier();
36 
38 }
39 
40 ssa_exprt::ssa_exprt(const exprt &expr) : symbol_exprt(expr.type())
41 {
42  set(ID_C_SSA_symbol, true);
43  add(ID_expression, expr);
44  with_source_location(expr.source_location());
45  std::ostringstream os;
46  initialize_ssa_identifier(os, expr);
47  const std::string id = os.str();
48  set_identifier(id);
49  set(ID_L1_object_identifier, id);
50 }
51 
56 static void build_ssa_identifier_rec(
57  const exprt &expr,
58  const irep_idt &l0,
59  const irep_idt &l1,
60  const irep_idt &l2,
61  std::ostream &os,
62  std::ostream &l1_object_os)
63 {
64  if(expr.id()==ID_member)
65  {
66  const member_exprt &member=to_member_expr(expr);
67 
68  build_ssa_identifier_rec(member.struct_op(), l0, l1, l2, os, l1_object_os);
69 
70  os << ".." << member.get_component_name();
71  l1_object_os << ".." << member.get_component_name();
72  }
73  else if(expr.id()==ID_index)
74  {
75  const index_exprt &index=to_index_expr(expr);
76 
77  build_ssa_identifier_rec(index.array(), l0, l1, l2, os, l1_object_os);
78 
79  const irep_idt &idx = to_constant_expr(index.index()).get_value();
80  os << "[[" << idx << "]]";
81  l1_object_os << "[[" << idx << "]]";
82  }
83  else if(expr.id()==ID_symbol)
84  {
85  auto symid=to_symbol_expr(expr).get_identifier();
86  os << symid;
87  l1_object_os << symid;
88 
89  if(!l0.empty())
90  {
91  // Distinguish different threads of execution
92  os << '!' << l0;
93  l1_object_os << '!' << l0;
94  }
95 
96  if(!l1.empty())
97  {
98  // Distinguish different calls to the same function (~stack frame)
99  os << '@' << l1;
100  l1_object_os << '@' << l1;
101  }
102 
103  if(!l2.empty())
104  {
105  // Distinguish SSA steps for the same variable
106  os << '#' << l2;
107  }
108  }
109  else
110  UNREACHABLE;
111 }
112 
113 static std::pair<irep_idt, irep_idt> build_identifier(
114  const exprt &expr,
115  const irep_idt &l0,
116  const irep_idt &l1,
117  const irep_idt &l2)
118 {
119  std::ostringstream oss;
120  std::ostringstream l1_object_oss;
121 
122  build_ssa_identifier_rec(expr, l0, l1, l2, oss, l1_object_oss);
123 
124  return std::make_pair(irep_idt(oss.str()), irep_idt(l1_object_oss.str()));
125 }
126 
127 static void update_identifier(ssa_exprt &ssa)
128 {
129  const irep_idt &l0 = ssa.get_level_0();
130  const irep_idt &l1 = ssa.get_level_1();
131  const irep_idt &l2 = ssa.get_level_2();
132 
133  auto idpair = build_identifier(ssa.get_original_expr(), l0, l1, l2);
134  ssa.set_identifier(idpair.first);
135  ssa.set(ID_L1_object_identifier, idpair.second);
136 }
137 
139 {
140  type() = as_const(expr).type();
141  add(ID_expression, std::move(expr));
142  ::update_identifier(*this);
143 }
144 
146 {
147  const exprt &original_expr = get_original_expr();
148 
149  if(original_expr.id() == ID_symbol)
150  return to_symbol_expr(original_expr).get_identifier();
151 
153  .get_identifier();
154 }
155 
157 {
159 
160  ssa_exprt root(ode.root_object());
161  if(!get_level_0().empty())
162  root.set(ID_L0, get(ID_L0));
163  if(!get_level_1().empty())
164  root.set(ID_L1, get(ID_L1));
165  ::update_identifier(root);
166 
167  return root;
168 }
169 
171 {
172 #if 0
173  return get_l1_object().get_identifier();
174 #else
175  // the above is the clean version, this is the fast one, using
176  // an identifier cached during build_identifier
177  return get(ID_L1_object_identifier);
178 #endif
179 }
180 
181 void ssa_exprt::set_level_0(std::size_t i)
182 {
183  set(ID_L0, i);
184  ::update_identifier(*this);
185 }
186 
187 void ssa_exprt::set_level_1(std::size_t i)
188 {
189  set(ID_L1, i);
190  ::update_identifier(*this);
191 }
192 
193 void ssa_exprt::set_level_2(std::size_t i)
194 {
195  set(ID_L2, i);
196  ::update_identifier(*this);
197 }
198 
200 {
201  remove(ID_L2);
203 }
204 
205 /* Used to determine whether or not an identifier can be built
206  * before trying and getting an exception */
207 bool ssa_exprt::can_build_identifier(const exprt &expr)
208 {
209  if(expr.id() == ID_symbol)
210  return true;
211  else if(expr.id() == ID_member)
212  return can_build_identifier(to_member_expr(expr).compound());
213  else if(expr.id() == ID_index)
214  return can_build_identifier(to_index_expr(expr).array());
215  else
216  return false;
217 }
218 
220 {
221  ssa.remove_level_2();
222  return ssa;
223 }
const T & as_const(T &value)
Return a reference to the same object but ensures the type is const.
Definition: as_const.h:14
const irep_idt & get_value() const
Definition: std_expr.h:3003
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool empty() const
Definition: dstring.h:89
Base class for all expressions.
Definition: expr.h:56
const source_locationt & source_location() const
Definition: expr.h:231
typet & type()
Return the type of the expression.
Definition: expr.h:84
Array index operator.
Definition: std_expr.h:1470
exprt & array()
Definition: std_expr.h:1500
exprt & index()
Definition: std_expr.h:1510
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:44
void remove(const irep_idt &name)
Definition: irep.cpp:87
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:412
const irep_idt & id() const
Definition: irep.h:388
irept & add(const irep_idt &name)
Definition: irep.cpp:103
Extract member of struct or union.
Definition: std_expr.h:2849
const exprt & struct_op() const
Definition: std_expr.h:2887
irep_idt get_component_name() const
Definition: std_expr.h:2871
Split an expression into a base object and a (byte) offset.
Definition: pointer_expr.h:181
const exprt & root_object() const
Definition: pointer_expr.h:218
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:17
const irep_idt get_level_1() const
Definition: ssa_expr.h:68
const irep_idt get_l1_object_identifier() const
void set_level_1(std::size_t i)
void set_level_2(std::size_t i)
void remove_level_2()
const ssa_exprt get_l1_object() const
static bool can_build_identifier(const exprt &src)
Used to determine whether or not an identifier can be built before trying and getting an exception.
void set_expression(exprt expr)
Replace the underlying, original expression by expr while maintaining SSA indices.
const irep_idt get_level_2() const
Definition: ssa_expr.h:73
const irep_idt get_level_0() const
Definition: ssa_expr.h:63
void set_level_0(std::size_t i)
ssa_exprt(const exprt &expr)
Constructor.
const exprt & get_original_expr() const
Definition: ssa_expr.h:33
irep_idt get_object_name() const
Expression to hold a symbol (variable)
Definition: std_expr.h:131
void set_identifier(const irep_idt &identifier)
Definition: std_expr.h:155
const irep_idt & get_identifier() const
Definition: std_expr.h:160
API to expression classes for Pointers.
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
static std::ostream & initialize_ssa_identifier(std::ostream &os, const exprt &expr)
If expr is:
Definition: ssa_expr.cpp:22
ssa_exprt remove_level_2(ssa_exprt ssa)
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:3050
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:272
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition: std_expr.h:2941
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1538
dstringt irep_idt