CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ssa_expr.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "ssa_expr.h"
10
11#include <sstream>
12
13#include "pointer_expr.h"
14
21static std::ostream &
22initialize_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
40ssa_exprt::ssa_exprt(const exprt &expr) : symbol_exprt(expr.type())
41{
42 set(ID_C_SSA_symbol, true);
43 add(ID_expression, expr);
45 std::ostringstream os;
47 const std::string id = os.str();
50}
51
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
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
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;
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
111}
112
113static 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
123
124 return std::make_pair(irep_idt(oss.str()), irep_idt(l1_object_oss.str()));
125}
126
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);
136}
137
139{
140 type() = as_const(expr).type();
141 add(ID_expression, std::move(expr));
142 ::update_identifier(*this);
143}
144
146{
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));
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
178#endif
179}
180
181void ssa_exprt::set_level_0(std::size_t i)
182{
183 set(ID_L0, i);
184 ::update_identifier(*this);
185}
186
187void ssa_exprt::set_level_1(std::size_t i)
188{
189 set(ID_L1, i);
190 ::update_identifier(*this);
191}
192
193void ssa_exprt::set_level_2(std::size_t i)
194{
195 set(ID_L2, i);
196 ::update_identifier(*this);
197}
198
204
205/* Used to determine whether or not an identifier can be built
206 * before trying and getting an exception */
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
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Base class for all expressions.
Definition expr.h:56
typet & type()
Return the type of the expression.
Definition expr.h:84
const source_locationt & source_location() const
Definition expr.h:231
Array index operator.
Definition std_expr.h:1470
exprt & index()
Definition std_expr.h:1510
exprt & array()
Definition std_expr.h:1500
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:2971
const exprt & struct_op() const
Definition std_expr.h:3009
irep_idt get_component_name() const
Definition std_expr.h:2993
Split an expression into a base object and a (byte) offset.
const exprt & root_object() const
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
Definition ssa_expr.cpp:170
void set_level_1(std::size_t i)
Definition ssa_expr.cpp:187
void set_level_2(std::size_t i)
Definition ssa_expr.cpp:193
void remove_level_2()
Definition ssa_expr.cpp:199
const ssa_exprt get_l1_object() const
Definition ssa_expr.cpp:156
void set_expression(exprt expr)
Replace the underlying, original expression by expr while maintaining SSA indices.
Definition ssa_expr.cpp:138
const irep_idt get_level_0() const
Definition ssa_expr.h:63
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.
Definition ssa_expr.cpp:207
const exprt & get_original_expr() const
Definition ssa_expr.h:33
void set_level_0(std::size_t i)
Definition ssa_expr.cpp:181
ssa_exprt(const exprt &expr)
Constructor.
Definition ssa_expr.cpp:40
irep_idt get_object_name() const
Definition ssa_expr.cpp:145
Expression to hold a symbol (variable)
Definition std_expr.h:131
void set_identifier(const irep_idt &identifier)
Definition std_expr.h:155
symbol_exprt & with_source_location(source_locationt location) &
Add the source location from location, if it is non-nil.
Definition std_expr.h:166
API to expression classes for Pointers.
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
static std::pair< irep_idt, irep_idt > build_identifier(const exprt &expr, const irep_idt &l0, const irep_idt &l1, const irep_idt &l2)
Definition ssa_expr.cpp:113
static void update_identifier(ssa_exprt &ssa)
Definition ssa_expr.cpp:127
static void build_ssa_identifier_rec(const exprt &expr, const irep_idt &l0, const irep_idt &l1, const irep_idt &l2, std::ostream &os, std::ostream &l1_object_os)
If expr is a symbol "s" add to os "s!l0@l1#l2" and to l1_object_os "s!l0@l1".
Definition ssa_expr.cpp:56
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)
Definition ssa_expr.cpp:219
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition std_expr.h:1538
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition std_expr.h:3063
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:3172
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:272
dstringt irep_idt