-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathconcurrency.cpp
More file actions
198 lines (159 loc) · 4.52 KB
/
Copy pathconcurrency.cpp
File metadata and controls
198 lines (159 loc) · 4.52 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
/*******************************************************************\
Module: Encoding for Threaded Goto Programs
Author: Daniel Kroening
Date: October 2012
\*******************************************************************/
/// \file
/// Encoding for Threaded Goto Programs
#include "concurrency.h"
#include <util/find_symbols.h>
#include <util/invariant.h>
#include <util/replace_symbol.h>
#include <util/std_expr.h>
#include <analyses/is_threaded.h>
class concurrency_instrumentationt
{
public:
concurrency_instrumentationt(
value_setst &_value_sets,
symbol_tablet &_symbol_table):
value_sets(_value_sets),
symbol_table(_symbol_table)
{
}
void operator()(goto_functionst &goto_functions)
{
instrument(goto_functions);
}
protected:
value_setst &value_sets;
symbol_tablet &symbol_table;
void instrument(goto_functionst &goto_functions);
void instrument(goto_programt &goto_program);
void instrument(exprt &expr);
void collect(
const goto_programt &goto_program,
const is_threadedt &is_threaded);
void collect(const exprt &expr);
void add_array_symbols();
class shared_vart
{
public:
typet type;
std::optional<symbol_exprt> array_symbol, w_index_symbol;
};
class thread_local_vart
{
public:
typet type;
std::optional<symbol_exprt> array_symbol;
};
typedef std::map<irep_idt, shared_vart> shared_varst;
shared_varst shared_vars;
typedef std::map<irep_idt, thread_local_vart> thread_local_varst;
thread_local_varst thread_local_vars;
};
void concurrency_instrumentationt::instrument(exprt &expr)
{
replace_symbolt replace_symbol;
for(const symbol_exprt &s : find_symbols(expr))
{
shared_varst::const_iterator v_it = shared_vars.find(s.identifier());
if(v_it != shared_vars.end())
{
UNIMPLEMENTED;
// not yet done: neither array_symbol nor w_index_symbol are actually
// initialized anywhere
const shared_vart &shared_var = v_it->second;
const index_exprt new_expr(
*shared_var.array_symbol, *shared_var.w_index_symbol);
replace_symbol.insert(s, new_expr);
}
}
}
void concurrency_instrumentationt::instrument(
goto_programt &goto_program)
{
for(goto_programt::instructionst::iterator
it=goto_program.instructions.begin();
it!=goto_program.instructions.end();
it++)
{
if(it->is_assign())
{
instrument(it->assign_rhs_nonconst());
}
else if(it->is_assume() || it->is_assert() || it->is_goto())
{
instrument(it->condition_nonconst());
}
else if(it->is_function_call())
{
code_function_callt &code = to_code_function_call(it->code_nonconst());
instrument(code.function());
// instrument(code.lhs(), LHS);
for(auto &arg : code.arguments())
instrument(arg);
}
}
}
void concurrency_instrumentationt::collect(const exprt &expr)
{
for(const auto &identifier : find_symbol_identifiers(expr))
{
namespacet ns(symbol_table);
const symbolt &symbol = ns.lookup(identifier);
if(!symbol.is_state_var)
continue;
if(symbol.is_thread_local)
{
if(thread_local_vars.find(identifier) != thread_local_vars.end())
continue;
thread_local_vart &thread_local_var = thread_local_vars[identifier];
thread_local_var.type = symbol.type;
}
else
{
if(shared_vars.find(identifier) != shared_vars.end())
continue;
shared_vart &shared_var = shared_vars[identifier];
shared_var.type = symbol.type;
}
}
}
void concurrency_instrumentationt::collect(
const goto_programt &goto_program,
const is_threadedt &is_threaded)
{
forall_goto_program_instructions(i_it, goto_program)
{
if(is_threaded(i_it))
i_it->apply([this](const exprt &e) { collect(e); });
}
}
void concurrency_instrumentationt::add_array_symbols()
{
// for(
}
void concurrency_instrumentationt::instrument(
goto_functionst &goto_functions)
{
namespacet ns(symbol_table);
is_threadedt is_threaded(goto_functions);
// this first collects all shared and thread-local variables
for(const auto &gf_entry : goto_functions.function_map)
collect(gf_entry.second.body, is_threaded);
// add array symbols
add_array_symbols();
// now instrument
for(auto &gf_entry : goto_functions.function_map)
instrument(gf_entry.second.body);
}
void concurrency(
value_setst &value_sets,
goto_modelt &goto_model)
{
concurrency_instrumentationt concurrency_instrumentation(
value_sets, goto_model.symbol_table);
concurrency_instrumentation(goto_model.goto_functions);
}