CBMC
goto_convert_functions.cpp
Go to the documentation of this file.
1 /********************************************************************\
2 
3 Module: Goto Programs with Functions
4 
5 Author: Daniel Kroening
6 
7 Date: June 2003
8 
9 \*******************************************************************/
10 
11 #include "goto_convert_functions.h"
12 
13 #include <util/prefix.h>
14 #include <util/std_code.h>
15 #include <util/symbol_table.h>
17 
19 
20 #include "goto_model.h"
21 
23  symbol_table_baset &_symbol_table,
24  message_handlert &_message_handler)
25  : goto_convertt(_symbol_table, _message_handler)
26 {
27 }
28 
30 {
31 }
32 
34 {
35  // warning! hash-table iterators are not stable
36 
37  typedef std::list<irep_idt> symbol_listt;
38  symbol_listt symbol_list;
39 
40  for(const auto &symbol_pair : symbol_table.symbols)
41  {
42  if(
43  !symbol_pair.second.is_type && !symbol_pair.second.is_macro &&
44  symbol_pair.second.type.id() == ID_code &&
45  (symbol_pair.second.mode == ID_C || symbol_pair.second.mode == ID_cpp ||
46  symbol_pair.second.mode == ID_java ||
47  symbol_pair.second.mode == "jsil" ||
48  symbol_pair.second.mode == ID_statement_list))
49  {
50  symbol_list.push_back(symbol_pair.first);
51  }
52  }
53 
54  for(const auto &id : symbol_list)
55  {
56  convert_function(id, functions.function_map[id]);
57  }
58 
59  functions.compute_location_numbers();
60 
61 // this removes the parse tree of the bodies from memory
62 #if 0
63  for(const auto &symbol_pair, symbol_table.symbols)
64  {
65  if(!symbol_pair.second.is_type &&
66  symbol_pair.second.type.id()==ID_code &&
67  symbol_pair.second.value.is_not_nil())
68  {
69  symbol_pair.second.value=codet();
70  }
71  }
72 #endif
73 }
74 
76 {
77  for(const auto &instruction : goto_program.instructions)
78  {
79  for(const auto &label : instruction.labels)
80  {
81  if(label == CPROVER_PREFIX "HIDE")
82  return true;
83  }
84  }
85 
86  return false;
87 }
88 
91  const typet &return_type,
92  const source_locationt &source_location)
93 {
94 #if 0
95  if(!f.body.instructions.empty() &&
96  f.body.instructions.back().is_return())
97  return; // not needed, we have one already
98 
99  // see if we have an unconditional goto at the end
100  if(!f.body.instructions.empty() &&
101  f.body.instructions.back().is_goto() &&
102  f.body.instructions.back().guard.is_true())
103  return;
104 #else
105 
106  if(!f.body.instructions.empty())
107  {
108  goto_programt::const_targett last_instruction = f.body.instructions.end();
109  last_instruction--;
110 
111  while(true)
112  {
113  // unconditional goto, say from while(1)?
114  if(last_instruction->is_goto() && last_instruction->condition().is_true())
115  {
116  return;
117  }
118 
119  // return?
120  if(last_instruction->is_set_return_value())
121  return;
122 
123  // advance if it's a 'dead' without branch target
124  if(
125  last_instruction->is_dead() &&
126  last_instruction != f.body.instructions.begin() &&
127  !last_instruction->is_target())
128  last_instruction--;
129  else
130  break; // give up
131  }
132  }
133 
134 #endif
135 
136  side_effect_expr_nondett rhs(return_type, source_location);
137 
138  f.body.add(
139  goto_programt::make_set_return_value(std::move(rhs), source_location));
140 }
141 
143  const irep_idt &identifier,
145 {
146  const symbolt &symbol = ns.lookup(identifier);
147  const irep_idt mode = symbol.mode;
148 
149  if(f.body_available())
150  return; // already converted
151 
152  // make tmp variables local to function
153  tmp_symbol_prefix = id2string(symbol.name) + "::$tmp";
154 
155  // store the parameter identifiers in the goto functions
156  const code_typet &code_type = to_code_type(symbol.type);
157  f.set_parameter_identifiers(code_type);
158 
159  if(
160  symbol.value.is_nil() ||
161  symbol.is_compiled()) /* goto_inline may have removed the body */
162  return;
163 
164  // we have a body, make sure all parameter names are valid
165  for(const auto &p : f.parameter_identifiers)
166  {
168  !p.empty(),
169  "parameter identifier should not be empty",
170  "function:",
171  identifier);
172 
175  "parameter identifier must be a known symbol",
176  "function:",
177  identifier,
178  "parameter:",
179  p);
180  }
181 
182  lifetimet parent_lifetime = lifetime;
185 
186  const codet &code = to_code(symbol.value);
187 
188  source_locationt end_location;
189 
190  if(code.get_statement() == ID_block)
191  end_location = to_code_block(code).end_location();
192  else
193  end_location.make_nil();
194 
195  goto_programt tmp_end_function;
196  goto_programt::targett end_function =
197  tmp_end_function.add(goto_programt::make_end_function(end_location));
198 
199  targets = targetst();
200  targets.set_return(end_function);
201  targets.has_return_value = code_type.return_type().id() != ID_empty &&
202  code_type.return_type().id() != ID_constructor &&
203  code_type.return_type().id() != ID_destructor;
204 
205  goto_convert_rec(code, f.body, mode);
206 
207  // add non-det return value, if needed
209  add_return(f, code_type.return_type(), end_location);
210 
211  // handle SV-COMP's __VERIFIER_atomic_
212  if(
213  !f.body.instructions.empty() &&
214  has_prefix(id2string(identifier), "__VERIFIER_atomic_"))
215  {
218  a_begin.source_location_nonconst() =
219  f.body.instructions.front().source_location();
220  f.body.insert_before_swap(f.body.instructions.begin(), a_begin);
221 
222  goto_programt::targett a_end =
223  f.body.add(goto_programt::make_atomic_end(end_location));
224 
225  for(auto &instruction : f.body.instructions)
226  {
227  if(instruction.is_goto() && instruction.get_target()->is_end_function())
228  instruction.set_target(a_end);
229  }
230  }
231 
232  // add "end of function"
233  f.body.destructive_append(tmp_end_function);
234 
235  f.body.update();
236 
237  if(hide(f.body))
238  f.make_hidden();
239 
240  lifetime = parent_lifetime;
241 }
242 
243 void goto_convert(goto_modelt &goto_model, message_handlert &message_handler)
244 {
245  symbol_table_buildert symbol_table_builder =
247 
248  goto_convert(
249  symbol_table_builder, goto_model.goto_functions, message_handler);
250 }
251 
253  symbol_table_baset &symbol_table,
254  goto_functionst &functions,
255  message_handlert &message_handler)
256 {
257  symbol_table_buildert symbol_table_builder =
258  symbol_table_buildert::wrap(symbol_table);
259 
260  goto_convert_functionst goto_convert_functions(
261  symbol_table_builder, message_handler);
262 
263  goto_convert_functions.goto_convert(functions);
264 }
265 
267  const irep_idt &identifier,
268  symbol_table_baset &symbol_table,
269  goto_functionst &functions,
270  message_handlert &message_handler)
271 {
272  symbol_table_buildert symbol_table_builder =
273  symbol_table_buildert::wrap(symbol_table);
274 
275  goto_convert_functionst goto_convert_functions(
276  symbol_table_builder, message_handler);
277 
278  goto_convert_functions.convert_function(
279  identifier, functions.function_map[identifier]);
280 }
symbol_table_baset::has_symbol
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
Definition: symbol_table_base.h:87
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
goto_convertt::ns
namespacet ns
Definition: goto_convert_class.h:52
lifetimet::AUTOMATIC_LOCAL
@ AUTOMATIC_LOCAL
Allocate local objects with automatic lifetime.
goto_programt::instructiont::source_location_nonconst
source_locationt & source_location_nonconst()
Definition: goto_program.h:345
irept::make_nil
void make_nil()
Definition: irep.h:454
typet
The type of an expression, extends irept.
Definition: type.h:28
goto_programt::make_set_return_value
static instructiont make_set_return_value(exprt return_value, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:865
goto_convertt::goto_convert_rec
void goto_convert_rec(const codet &code, goto_programt &dest, const irep_idt &mode)
Definition: goto_convert.cpp:287
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_convertt::tmp_symbol_prefix
std::string tmp_symbol_prefix
Definition: goto_convert_class.h:53
goto_convert_functionst::convert_function
void convert_function(const irep_idt &identifier, goto_functionst::goto_functiont &result)
Definition: goto_convert_functions.cpp:142
prefix.h
goto_programt::make_end_function
static instructiont make_end_function(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:992
goto_programt::add
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:709
goto_model.h
goto_functionst::compute_location_numbers
void compute_location_numbers()
Definition: goto_functions.cpp:20
goto_modelt
Definition: goto_model.h:25
goto_convert_functionst::goto_convert
void goto_convert(goto_functionst &functions)
Definition: goto_convert_functions.cpp:33
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
goto_convertt::targetst::has_return_value
bool has_return_value
Definition: goto_convert_class.h:385
goto_convert_functionst::add_return
void add_return(goto_functionst::goto_functiont &, const typet &return_type, const source_locationt &)
Definition: goto_convert_functions.cpp:89
to_code
const codet & to_code(const exprt &expr)
Definition: std_code_base.h:104
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
goto_convertt::targets
struct goto_convertt::targetst targets
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
DATA_INVARIANT_WITH_DIAGNOSTICS
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:511
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
goto_convertt
Definition: goto_convert_class.h:30
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
symbol_table_baset
The symbol table base class interface.
Definition: symbol_table_base.h:21
goto_convert_functionst
Definition: goto_convert_functions.h:40
INITIALIZE_FUNCTION
#define INITIALIZE_FUNCTION
Definition: static_lifetime_init.h:22
goto_convertt::targetst::set_return
void set_return(goto_programt::targett _return_target)
Definition: goto_convert_class.h:437
goto_programt::make_atomic_begin
static instructiont make_atomic_begin(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:970
code_typet
Base type of functions.
Definition: std_types.h:538
irept::is_nil
bool is_nil() const
Definition: irep.h:376
irept::id
const irep_idt & id() const
Definition: irep.h:396
message_handlert
Definition: message.h:27
goto_convert
void goto_convert(goto_modelt &goto_model, message_handlert &message_handler)
Definition: goto_convert_functions.cpp:243
std_code.h
lifetimet
lifetimet
Selects the kind of objects allocated.
Definition: allocate_objects.h:16
source_locationt
Definition: source_location.h:18
side_effect_expr_nondett
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1519
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:27
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:592
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
goto_convertt::targetst
Definition: goto_convert_class.h:383
irept::add
irept & add(const irep_idt &name)
Definition: irep.cpp:116
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
goto_convert_functionst::~goto_convert_functionst
virtual ~goto_convert_functionst()
Definition: goto_convert_functions.cpp:29
symbolt
Symbol table entry.
Definition: symbol.h:27
symbol_table_buildert::wrap
static symbol_table_buildert wrap(symbol_table_baset &base_symbol_table)
Definition: symbol_table_builder.h:42
symbol_table_buildert
Author: Diffblue Ltd.
Definition: symbol_table_builder.h:13
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
lifetimet::STATIC_GLOBAL
@ STATIC_GLOBAL
Allocate global objects with static lifetime.
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
symbolt::is_compiled
bool is_compiled() const
Returns true iff the the symbol's value has been compiled to a goto program.
Definition: symbol.h:107
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
goto_convert_functionst::goto_convert_functionst
goto_convert_functionst(symbol_table_baset &_symbol_table, message_handlert &_message_handler)
Definition: goto_convert_functions.cpp:22
goto_convert_functionst::hide
static bool hide(const goto_programt &)
Definition: goto_convert_functions.cpp:75
goto_convertt::symbol_table
symbol_table_baset & symbol_table
Definition: goto_convert_class.h:51
code_typet::return_type
const typet & return_type() const
Definition: std_types.h:645
to_code_block
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:203
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:587
goto_convert_functions.h
static_lifetime_init.h
symbol_table.h
Author: Diffblue Ltd.
codet::get_statement
const irep_idt & get_statement() const
Definition: std_code_base.h:65
symbol_table_builder.h
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
goto_convertt::lifetime
lifetimet lifetime
Definition: goto_convert_class.h:54
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586
code_blockt::end_location
source_locationt end_location() const
Definition: std_code.h:187
goto_programt::make_atomic_end
static instructiont make_atomic_end(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:981
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:28