-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathunwindset.cpp
More file actions
229 lines (196 loc) · 6.24 KB
/
unwindset.cpp
File metadata and controls
229 lines (196 loc) · 6.24 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
222
223
224
225
226
227
228
229
/*******************************************************************\
Module: Loop unwinding setup
Author: Daniel Kroening, kroening@kroening.com
\*******************************************************************/
#include "unwindset.h"
#include <util/exception_utils.h>
#include <util/message.h>
#include <util/string2int.h>
#include <util/string_utils.h>
#include <util/symbol_table.h>
#include <util/unicode.h>
#include "abstract_goto_model.h"
#include <algorithm>
#include <fstream>
void unwindsett::parse_unwind(const std::string &unwind)
{
if(!unwind.empty())
global_limit = unsafe_string2unsigned(unwind);
}
void unwindsett::parse_unwindset_one_loop(
std::string val,
message_handlert &message_handler)
{
if(val.empty())
return;
std::optional<unsigned> thread_nr;
if(isdigit(val[0]))
{
auto c_pos = val.find(':');
if(c_pos != std::string::npos)
{
std::string nr = val.substr(0, c_pos);
thread_nr = unsafe_string2unsigned(nr);
val.erase(0, nr.size() + 1);
}
}
auto last_c_pos = val.rfind(':');
if(last_c_pos != std::string::npos)
{
std::string id = val.substr(0, last_c_pos);
// The loop id can take three forms:
// 1) Just a function name to limit recursion.
// 2) F.N where F is a function name and N is a loop number.
// 3) F.L where F is a function name and L is a label.
const symbol_table_baset &symbol_table = goto_model.get_symbol_table();
const symbolt *maybe_fn = symbol_table.lookup(id);
if(maybe_fn && maybe_fn->type.id() == ID_code)
{
// ok, recursion limit
}
else
{
auto last_dot_pos = val.rfind('.');
if(last_dot_pos == std::string::npos)
{
throw invalid_command_line_argument_exceptiont{
"invalid loop identifier " + id, "unwindset"};
}
std::string function_id = id.substr(0, last_dot_pos);
std::string loop_nr_label = id.substr(last_dot_pos + 1);
if(loop_nr_label.empty())
{
throw invalid_command_line_argument_exceptiont{
"invalid loop identifier " + id, "unwindset"};
}
if(!goto_model.can_produce_function(function_id))
{
messaget log{message_handler};
log.warning() << "loop identifier " << id
<< " for non-existent function provided with unwindset"
<< messaget::eom;
return;
}
const goto_functiont &goto_function =
goto_model.get_goto_function(function_id);
if(isdigit(loop_nr_label[0]))
{
auto nr = string2optional_unsigned(loop_nr_label);
if(!nr.has_value())
{
throw invalid_command_line_argument_exceptiont{
"invalid loop identifier " + id, "unwindset"};
}
bool found = std::any_of(
goto_function.body.instructions.begin(),
goto_function.body.instructions.end(),
[&nr](const goto_programt::instructiont &instruction) {
return instruction.is_backwards_goto() &&
instruction.loop_number == nr;
});
if(!found)
{
messaget log{message_handler};
log.warning() << "loop identifier " << id
<< " provided with unwindset does not match any loop"
<< messaget::eom;
return;
}
}
else
{
std::optional<unsigned> nr;
std::optional<source_locationt> location;
for(const auto &instruction : goto_function.body.instructions)
{
if(
std::find(
instruction.labels.begin(),
instruction.labels.end(),
loop_nr_label) != instruction.labels.end())
{
location = instruction.source_location();
// the label may be attached to the DECL part of an initializing
// declaration, which we may have marked as hidden
location->remove(ID_hide);
}
if(
location.has_value() && instruction.is_backwards_goto() &&
instruction.source_location() == *location)
{
if(nr.has_value())
{
messaget log{message_handler};
log.warning()
<< "loop identifier " << id
<< " provided with unwindset is ambiguous" << messaget::eom;
}
nr = instruction.loop_number;
}
}
if(!nr.has_value())
{
messaget log{message_handler};
log.warning() << "loop identifier " << id
<< " provided with unwindset does not match any loop"
<< messaget::eom;
return;
}
else
id = function_id + "." + std::to_string(*nr);
}
}
std::string uw_string = val.substr(last_c_pos + 1);
// the below initialisation makes g++-5 happy
std::optional<unsigned> uw(0);
if(uw_string.empty())
uw = {};
else
uw = unsafe_string2unsigned(uw_string);
if(thread_nr.has_value())
{
thread_loop_map[std::pair<irep_idt, unsigned>(id, *thread_nr)] = uw;
}
else
{
loop_map[id] = uw;
}
}
}
void unwindsett::parse_unwindset(
const std::list<std::string> &unwindset,
message_handlert &message_handler)
{
for(auto &element : unwindset)
parse_unwindset_one_loop(element, message_handler);
}
std::optional<unsigned>
unwindsett::get_limit(const irep_idt &loop_id, unsigned thread_nr) const
{
// We use the most specific limit we have
// thread x loop
auto tl_it =
thread_loop_map.find(std::pair<irep_idt, unsigned>(loop_id, thread_nr));
if(tl_it != thread_loop_map.end())
return tl_it->second;
// loop
auto l_it = loop_map.find(loop_id);
if(l_it != loop_map.end())
return l_it->second;
// global, if any
return global_limit;
}
void unwindsett::parse_unwindset_file(
const std::string &file_name,
message_handlert &message_handler)
{
std::ifstream file(widen_if_needed(file_name));
if(!file)
throw "cannot open file " + file_name;
std::stringstream buffer;
buffer << file.rdbuf();
std::vector<std::string> unwindset_elements =
split_string(buffer.str(), ',', true, true);
for(auto &element : unwindset_elements)
parse_unwindset_one_loop(element, message_handler);
}