-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathvalue_set_analysis_fi.cpp
More file actions
221 lines (191 loc) · 5.49 KB
/
Copy pathvalue_set_analysis_fi.cpp
File metadata and controls
221 lines (191 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*******************************************************************\
Module: Value Set Propagation (Flow Insensitive)
Author: Daniel Kroening, kroening@kroening.com
CM Wintersteiger
\*******************************************************************/
/// \file
/// Value Set Propagation (Flow Insensitive)
#include "value_set_analysis_fi.h"
#include <util/c_types.h>
#include <util/namespace.h>
#include <util/pointer_expr.h>
#include <util/symbol_table_base.h>
void value_set_analysis_fit::initialize(
const goto_programt &goto_program)
{
baset::initialize(goto_program);
add_vars(goto_program);
}
void value_set_analysis_fit::initialize(
const goto_functionst &goto_functions)
{
baset::initialize(goto_functions);
add_vars(goto_functions);
}
void value_set_analysis_fit::add_vars(
const goto_programt &goto_program)
{
typedef std::list<value_set_fit::entryt> entry_listt;
// get the globals
entry_listt globals;
get_globals(globals);
// get the locals
goto_programt::decl_identifierst locals;
goto_program.get_decl_identifiers(locals);
// cache the list for the locals to speed things up
typedef std::unordered_map<irep_idt, entry_listt> entry_cachet;
entry_cachet entry_cache;
value_set_fit &v=state.value_set;
for(goto_programt::instructionst::const_iterator
i_it=goto_program.instructions.begin();
i_it!=goto_program.instructions.end();
i_it++)
{
v.add_vars(globals);
for(goto_programt::decl_identifierst::const_iterator
l_it=locals.begin();
l_it!=locals.end();
l_it++)
{
// cache hit?
entry_cachet::const_iterator e_it=entry_cache.find(*l_it);
if(e_it==entry_cache.end())
{
const symbolt &symbol=ns.lookup(*l_it);
std::list<value_set_fit::entryt> &entries=entry_cache[*l_it];
get_entries(symbol, entries);
v.add_vars(entries);
}
else
v.add_vars(e_it->second);
}
}
}
void value_set_analysis_fit::get_entries(
const symbolt &symbol,
std::list<value_set_fit::entryt> &dest)
{
get_entries_rec(symbol.name, "", symbol.type, dest);
}
void value_set_analysis_fit::get_entries_rec(
const irep_idt &identifier,
const std::string &suffix,
const typet &type,
std::list<value_set_fit::entryt> &dest)
{
if(type.id() == ID_struct_tag || type.id() == ID_union_tag)
{
const auto &components =
ns.follow_tag(to_struct_or_union_tag_type(type)).components();
for(const auto &c : components)
{
get_entries_rec(
identifier, suffix + "." + id2string(c.get_name()), c.type(), dest);
}
}
else if(type.id() == ID_struct || type.id() == ID_union)
{
const auto &components = to_struct_union_type(type).components();
for(const auto &c : components)
{
get_entries_rec(
identifier, suffix + "." + id2string(c.get_name()), c.type(), dest);
}
}
else if(type.id() == ID_array)
{
get_entries_rec(
identifier, suffix + "[]", to_array_type(type).element_type(), dest);
}
else if(check_type(type))
{
dest.push_back(value_set_fit::entryt(identifier, suffix));
}
}
void value_set_analysis_fit::add_vars(
const goto_functionst &goto_functions)
{
// get the globals
std::list<value_set_fit::entryt> globals;
get_globals(globals);
value_set_fit &v=state.value_set;
v.add_vars(globals);
for(const auto &gf_entry : goto_functions.function_map)
{
// get the locals
std::set<irep_idt> locals;
get_local_identifiers(gf_entry.second, locals);
for(auto l : locals)
{
const symbolt &symbol=ns.lookup(l);
std::list<value_set_fit::entryt> entries;
get_entries(symbol, entries);
v.add_vars(entries);
}
}
}
void value_set_analysis_fit::get_globals(
std::list<value_set_fit::entryt> &dest)
{
// static ones
for(const auto &symbol_pair : ns.get_symbol_table().symbols)
{
if(symbol_pair.second.is_lvalue && symbol_pair.second.is_static_lifetime)
{
get_entries(symbol_pair.second, dest);
}
}
}
bool value_set_analysis_fit::check_type(const typet &type)
{
if(type.id()==ID_pointer)
{
switch(track_options)
{
case TRACK_ALL_POINTERS:
{ return true; break; }
case TRACK_FUNCTION_POINTERS:
{
if(type.id()==ID_pointer)
{
const typet *t = &type;
while(t->id() == ID_pointer)
t = &(to_pointer_type(*t).base_type());
return (t->id()==ID_code);
}
break;
}
default: // don't track.
break;
}
}
else if(type.id()==ID_struct ||
type.id()==ID_union)
{
for(const auto &c : to_struct_union_type(type).components())
{
if(check_type(c.type()))
return true;
}
}
else if(type.id()==ID_array)
return check_type(to_array_type(type).element_type());
else if(type.id() == ID_struct_tag)
return check_type(ns.follow_tag(to_struct_tag_type(type)));
else if(type.id() == ID_union_tag)
return check_type(ns.follow_tag(to_union_tag_type(type)));
return false;
}
std::vector<exprt> value_set_analysis_fit::get_values(
const irep_idt &function_id,
flow_insensitive_analysis_baset::locationt l,
const exprt &expr)
{
state.value_set.from_function =
state.value_set.function_numbering.number(function_id);
state.value_set.to_function =
state.value_set.function_numbering.number(function_id);
state.value_set.from_target_index = l->location_number;
state.value_set.to_target_index = l->location_number;
return state.value_set.get_value_set(expr, ns);
}