cprover
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/fresh_symbol.h>
14 #include <util/prefix.h>
15 #include <util/std_code.h>
16 #include <util/symbol_table.h>
18 
20 
21 #include "goto_inline.h"
22 
24  symbol_table_baset &_symbol_table,
25  message_handlert &_message_handler)
26  : goto_convertt(_symbol_table, _message_handler)
27 {
28 }
29 
31 {
32 }
33 
35 {
36  // warning! hash-table iterators are not stable
37 
38  typedef std::list<irep_idt> symbol_listt;
39  symbol_listt symbol_list;
40 
41  for(const auto &symbol_pair : symbol_table.symbols)
42  {
43  if(
44  !symbol_pair.second.is_type && !symbol_pair.second.is_macro &&
45  symbol_pair.second.type.id() == ID_code &&
46  (symbol_pair.second.mode == ID_C || symbol_pair.second.mode == ID_cpp ||
47  symbol_pair.second.mode == ID_java ||
48  symbol_pair.second.mode == "jsil" ||
49  symbol_pair.second.mode == ID_statement_list))
50  {
51  symbol_list.push_back(symbol_pair.first);
52  }
53  }
54 
55  for(const auto &id : symbol_list)
56  {
57  convert_function(id, functions.function_map[id]);
58  }
59 
60  functions.compute_location_numbers();
61 
62 // this removes the parse tree of the bodies from memory
63 #if 0
64  for(const auto &symbol_pair, symbol_table.symbols)
65  {
66  if(!symbol_pair.second.is_type &&
67  symbol_pair.second.type.id()==ID_code &&
68  symbol_pair.second.value.is_not_nil())
69  {
70  symbol_pair.second.value=codet();
71  }
72  }
73 #endif
74 }
75 
77 {
78  for(const auto &instruction : goto_program.instructions)
79  {
80  for(const auto &label : instruction.labels)
81  {
82  if(label == CPROVER_PREFIX "HIDE")
83  return true;
84  }
85  }
86 
87  return false;
88 }
89 
92  const typet &return_type,
93  const source_locationt &source_location)
94 {
95 #if 0
96  if(!f.body.instructions.empty() &&
97  f.body.instructions.back().is_return())
98  return; // not needed, we have one already
99 
100  // see if we have an unconditional goto at the end
101  if(!f.body.instructions.empty() &&
102  f.body.instructions.back().is_goto() &&
103  f.body.instructions.back().guard.is_true())
104  return;
105 #else
106 
107  if(!f.body.instructions.empty())
108  {
109  goto_programt::const_targett last_instruction = f.body.instructions.end();
110  last_instruction--;
111 
112  while(true)
113  {
114  // unconditional goto, say from while(1)?
115  if(
116  last_instruction->is_goto() &&
117  last_instruction->get_condition().is_true())
118  {
119  return;
120  }
121 
122  // return?
123  if(last_instruction->is_return())
124  return;
125 
126  // advance if it's a 'dead' without branch target
127  if(
128  last_instruction->is_dead() &&
129  last_instruction != f.body.instructions.begin() &&
130  !last_instruction->is_target())
131  last_instruction--;
132  else
133  break; // give up
134  }
135  }
136 
137 #endif
138 
139  side_effect_expr_nondett rhs(return_type, source_location);
140 
141  f.body.add(
142  goto_programt::make_return(code_returnt(std::move(rhs)), source_location));
143 }
144 
146  const irep_idt &identifier,
148 {
149  const symbolt &symbol = ns.lookup(identifier);
150  const irep_idt mode = symbol.mode;
151 
152  if(f.body_available())
153  return; // already converted
154 
155  // make tmp variables local to function
156  tmp_symbol_prefix = id2string(symbol.name) + "::$tmp";
157 
158  // store the parameter identifiers in the goto functions
159  const code_typet &code_type = to_code_type(symbol.type);
160  f.set_parameter_identifiers(code_type);
161 
162  if(
163  symbol.value.is_nil() ||
164  symbol.is_compiled()) /* goto_inline may have removed the body */
165  return;
166 
167  // we have a body, make sure all parameter names are valid
168  for(const auto &p : f.parameter_identifiers)
169  {
170  DATA_INVARIANT(!p.empty(), "parameter identifier should not be empty");
173  "parameter identifier must be a known symbol");
174  }
175 
176  lifetimet parent_lifetime = lifetime;
177  lifetime = identifier == INITIALIZE_FUNCTION ? lifetimet::STATIC_GLOBAL
178  : lifetimet::AUTOMATIC_LOCAL;
179 
180  const codet &code = to_code(symbol.value);
181 
182  source_locationt end_location;
183 
184  if(code.get_statement() == ID_block)
185  end_location = to_code_block(code).end_location();
186  else
187  end_location.make_nil();
188 
189  goto_programt tmp_end_function;
190  goto_programt::targett end_function =
191  tmp_end_function.add(goto_programt::make_end_function(end_location));
192 
193  targets = targetst();
194  targets.set_return(end_function);
195  targets.has_return_value = code_type.return_type().id() != ID_empty &&
196  code_type.return_type().id() != ID_constructor &&
197  code_type.return_type().id() != ID_destructor;
198 
199  goto_convert_rec(code, f.body, mode);
200 
201  // add non-det return value, if needed
203  add_return(f, code_type.return_type(), end_location);
204 
205  // handle SV-COMP's __VERIFIER_atomic_
206  if(
207  !f.body.instructions.empty() &&
208  has_prefix(id2string(identifier), "__VERIFIER_atomic_"))
209  {
212  a_begin.source_location = f.body.instructions.front().source_location;
213  f.body.insert_before_swap(f.body.instructions.begin(), a_begin);
214 
215  goto_programt::targett a_end =
216  f.body.add(goto_programt::make_atomic_end(end_location));
217 
218  for(auto &instruction : f.body.instructions)
219  {
220  if(instruction.is_goto() && instruction.get_target()->is_end_function())
221  instruction.set_target(a_end);
222  }
223  }
224 
225  // add "end of function"
226  f.body.destructive_append(tmp_end_function);
227 
228  f.body.update();
229 
230  if(hide(f.body))
231  f.make_hidden();
232 
233  lifetime = parent_lifetime;
234 }
235 
236 void goto_convert(goto_modelt &goto_model, message_handlert &message_handler)
237 {
238  symbol_table_buildert symbol_table_builder =
240 
241  goto_convert(
242  symbol_table_builder, goto_model.goto_functions, message_handler);
243 }
244 
246  symbol_table_baset &symbol_table,
247  goto_functionst &functions,
248  message_handlert &message_handler)
249 {
250  symbol_table_buildert symbol_table_builder =
251  symbol_table_buildert::wrap(symbol_table);
252 
253  goto_convert_functionst goto_convert_functions(
254  symbol_table_builder, message_handler);
255 
256  goto_convert_functions.goto_convert(functions);
257 }
258 
260  const irep_idt &identifier,
261  symbol_table_baset &symbol_table,
262  goto_functionst &functions,
263  message_handlert &message_handler)
264 {
265  symbol_table_buildert symbol_table_builder =
266  symbol_table_buildert::wrap(symbol_table);
267 
268  goto_convert_functionst goto_convert_functions(
269  symbol_table_builder, message_handler);
270 
271  goto_convert_functions.convert_function(
272  identifier, functions.function_map[identifier]);
273 }
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:37
goto_programt::instructiont::source_location
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:347
goto_convertt::ns
namespacet ns
Definition: goto_convert_class.h:52
goto_inline.h
Function Inlining.
irept::make_nil
void make_nil()
Definition: irep.h:465
typet
The type of an expression, extends irept.
Definition: type.h:28
fresh_symbol.h
Fresh auxiliary symbol creation.
goto_convertt::goto_convert_rec
void goto_convert_rec(const codet &code, goto_programt &dest, const irep_idt &mode)
Definition: goto_convert.cpp:286
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:122
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:145
prefix.h
goto_programt::make_end_function
static instructiont make_end_function(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:941
goto_programt::add
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:670
goto_functionst::compute_location_numbers
void compute_location_numbers()
Definition: goto_functions.cpp:18
goto_modelt
Definition: goto_model.h:26
goto_convert_functionst::goto_convert
void goto_convert(goto_functionst &functions)
Definition: goto_convert_functions.cpp:34
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
to_code
const codet & to_code(const exprt &expr)
Definition: std_code.h:155
goto_convertt::targetst::has_return_value
bool has_return_value
Definition: goto_convert_class.h:376
goto_convert_functionst::add_return
void add_return(goto_functionst::goto_functiont &, const typet &return_type, const source_locationt &)
Definition: goto_convert_functions.cpp:90
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:141
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:738
goto_convertt::targets
struct goto_convertt::targetst targets
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
goto_convertt
Definition: goto_convert_class.h:31
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
symbol_table_baset
The symbol table base class interface.
Definition: symbol_table_base.h:22
goto_convert_functionst
Definition: goto_convert_functions.h:40
INITIALIZE_FUNCTION
#define INITIALIZE_FUNCTION
Definition: static_lifetime_init.h:23
goto_convertt::targetst::set_return
void set_return(goto_programt::targett _return_target)
Definition: goto_convert_class.h:428
goto_programt::make_atomic_begin
static instructiont make_atomic_begin(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:919
code_typet
Base type of functions.
Definition: std_types.h:533
irept::is_nil
bool is_nil() const
Definition: irep.h:387
irept::id
const irep_idt & id() const
Definition: irep.h:407
message_handlert
Definition: message.h:28
goto_convert
void goto_convert(goto_modelt &goto_model, message_handlert &message_handler)
Definition: goto_convert_functions.cpp:236
std_code.h
lifetimet
lifetimet
Selects the kind of objects allocated.
Definition: allocate_objects.h:21
goto_programt::make_return
static instructiont make_return(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:822
source_locationt
Definition: source_location.h:20
side_effect_expr_nondett
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1968
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:25
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:569
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
goto_convertt::targetst
Definition: goto_convert_class.h:375
code_returnt
codet representation of a "return from a function" statement.
Definition: std_code.h:1342
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:30
symbolt
Symbol table entry.
Definition: symbol.h:28
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:14
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
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:74
goto_convert_functionst::goto_convert_functionst
goto_convert_functionst(symbol_table_baset &_symbol_table, message_handlert &_message_handler)
Definition: goto_convert_functions.cpp:23
goto_convert_functionst::hide
static bool hide(const goto_programt &)
Definition: goto_convert_functions.cpp:76
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:639
to_code_block
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:256
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:564
goto_convert_functions.h
Goto Programs with Functions.
static_lifetime_init.h
symbol_table.h
Author: Diffblue Ltd.
codet::get_statement
const irep_idt & get_statement() const
Definition: std_code.h:71
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:563
code_blockt::end_location
source_locationt end_location() const
Definition: std_code.h:227
goto_programt::make_atomic_end
static instructiont make_atomic_end(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:930
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:35