cprover
remove_returns.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Remove function return values
4 
5 Author: Daniel Kroening
6 
7 Date: September 2009
8 
9 \*******************************************************************/
10 
13 
14 #include "remove_returns.h"
15 
16 #include <util/std_expr.h>
17 #include <util/suffix.h>
18 
19 #include "goto_model.h"
20 
21 #include "remove_skip.h"
22 
23 #define RETURN_VALUE_SUFFIX "#return_value"
24 
26 {
27 public:
28  explicit remove_returnst(symbol_table_baset &_symbol_table):
29  symbol_table(_symbol_table)
30  {
31  }
32 
33  void operator()(
34  goto_functionst &goto_functions);
35 
36  void operator()(
37  goto_model_functiont &model_function,
38  function_is_stubt function_is_stub);
39 
40  void restore(
41  goto_functionst &goto_functions);
42 
43 protected:
45 
46  void replace_returns(
47  const irep_idt &function_id,
49 
50  bool do_function_calls(
51  function_is_stubt function_is_stub,
52  goto_programt &goto_program);
53 
54  bool
55  restore_returns(const irep_idt &function_id, goto_programt &goto_program);
56 
58  goto_programt &goto_program);
59 
61  get_or_create_return_value_symbol(const irep_idt &function_id);
62 };
63 
66 {
67  const namespacet ns(symbol_table);
68  const auto symbol_expr = return_value_symbol(function_id, ns);
69  const auto symbol_name = symbol_expr.get_identifier();
70  if(symbol_table.has_symbol(symbol_name))
71  return symbol_expr;
72 
73  const symbolt &function_symbol = symbol_table.lookup_ref(function_id);
74  const typet &return_type = to_code_type(function_symbol.type).return_type();
75 
76  if(return_type == empty_typet())
77  return {};
78 
79  auxiliary_symbolt new_symbol;
80  new_symbol.is_static_lifetime = true;
81  new_symbol.module = function_symbol.module;
82  new_symbol.base_name =
83  id2string(function_symbol.base_name) + RETURN_VALUE_SUFFIX;
84  new_symbol.name = symbol_name;
85  new_symbol.mode = function_symbol.mode;
86  // If we're creating this for the first time, the target function cannot have
87  // been remove_return'd yet, so this will still be the "true" return type:
88  new_symbol.type = return_type;
89  // Return-value symbols will always be written before they are read, so there
90  // is no need for __CPROVER_initialize to do anything:
91  new_symbol.type.set(ID_C_no_initialization_required, true);
92 
93  symbol_table.add(new_symbol);
94  return new_symbol.symbol_expr();
95 }
96 
101  const irep_idt &function_id,
103 {
104  // look up the function symbol
105  symbolt &function_symbol = *symbol_table.get_writeable(function_id);
106 
107  // returns something but void?
108  if(to_code_type(function_symbol.type).return_type() == empty_typet())
109  return;
110 
111  // add return_value symbol to symbol_table, if not already created:
112  const auto return_symbol = get_or_create_return_value_symbol(function_id);
113 
114  goto_programt &goto_program = function.body;
115 
116  for(auto &instruction : goto_program.instructions)
117  {
118  if(instruction.is_return())
119  {
120  INVARIANT(
121  instruction.code.operands().size() == 1,
122  "return instructions should have one operand");
123 
124  if(return_symbol.has_value())
125  {
126  // replace "return x;" by "fkt#return_value=x;"
127  code_assignt assignment(*return_symbol, instruction.code.op0());
128 
129  // now turn the `return' into `assignment'
130  instruction = goto_programt::make_assignment(
131  assignment, instruction.source_location);
132  }
133  else
134  instruction.turn_into_skip();
135  }
136  }
137 }
138 
146  function_is_stubt function_is_stub,
147  goto_programt &goto_program)
148 {
149  bool requires_update = false;
150 
151  Forall_goto_program_instructions(i_it, goto_program)
152  {
153  if(i_it->is_function_call())
154  {
155  code_function_callt function_call = i_it->get_function_call();
156 
157  INVARIANT(
158  function_call.function().id() == ID_symbol,
159  "indirect function calls should have been removed prior to running "
160  "remove-returns");
161 
162  const irep_idt function_id =
163  to_symbol_expr(function_call.function()).get_identifier();
164 
165  // Do we return anything?
166  if(does_function_call_return(function_call))
167  {
168  // replace "lhs=f(...)" by
169  // "f(...); lhs=f#return_value; DEAD f#return_value;"
170 
171  exprt rhs;
172 
173  bool is_stub = function_is_stub(function_id);
174  optionalt<symbol_exprt> return_value;
175 
176  if(!is_stub)
177  {
178  return_value = get_or_create_return_value_symbol(function_id);
179  CHECK_RETURN(return_value.has_value());
180 
181  // The return type in the definition of the function may differ
182  // from the return type in the declaration. We therefore do a
183  // cast.
185  *return_value, function_call.lhs().type());
186  }
187  else
188  {
190  function_call.lhs().type(), i_it->source_location);
191  }
192 
193  goto_programt::targett t_a = goto_program.insert_after(
194  i_it,
196  code_assignt(function_call.lhs(), rhs), i_it->source_location));
197 
198  // fry the previous assignment
199  function_call.lhs().make_nil();
200 
201  if(!is_stub)
202  {
203  goto_program.insert_after(
204  t_a,
205  goto_programt::make_dead(*return_value, i_it->source_location));
206  }
207 
208  // update the call
209  i_it->set_function_call(function_call);
210 
211  requires_update = true;
212  }
213  }
214  }
215 
216  return requires_update;
217 }
218 
220 {
221  for(auto &gf_entry : goto_functions.function_map)
222  {
223  // NOLINTNEXTLINE
224  auto function_is_stub = [&goto_functions](const irep_idt &function_id) {
225  auto findit = goto_functions.function_map.find(function_id);
226  INVARIANT(
227  findit != goto_functions.function_map.end(),
228  "called function `" + id2string(function_id) +
229  "' should have an entry in the function map");
230  return !findit->second.body_available();
231  };
232 
233  replace_returns(gf_entry.first, gf_entry.second);
234  if(do_function_calls(function_is_stub, gf_entry.second.body))
235  goto_functions.compute_location_numbers(gf_entry.second.body);
236  }
237 }
238 
240  goto_model_functiont &model_function,
241  function_is_stubt function_is_stub)
242 {
243  goto_functionst::goto_functiont &goto_function =
244  model_function.get_goto_function();
245 
246  // If this is a stub it doesn't have a corresponding #return_value,
247  // not any return instructions to alter:
248  if(goto_function.body.empty())
249  return;
250 
251  replace_returns(model_function.get_function_id(), goto_function);
252  if(do_function_calls(function_is_stub, goto_function.body))
253  model_function.compute_location_numbers();
254 }
255 
258  symbol_table_baset &symbol_table,
259  goto_functionst &goto_functions)
260 {
261  remove_returnst rr(symbol_table);
262  rr(goto_functions);
263 }
264 
277  goto_model_functiont &goto_model_function,
278  function_is_stubt function_is_stub)
279 {
280  remove_returnst rr(goto_model_function.get_symbol_table());
281  rr(goto_model_function, function_is_stub);
282 }
283 
285 void remove_returns(goto_modelt &goto_model)
286 {
287  remove_returnst rr(goto_model.symbol_table);
288  rr(goto_model.goto_functions);
289 }
290 
293  const irep_idt &function_id,
294  goto_programt &goto_program)
295 {
296  // do we have X#return_value?
297  auto rv_name = return_value_identifier(function_id);
298  symbol_tablet::symbolst::const_iterator rv_it=
299  symbol_table.symbols.find(rv_name);
300  if(rv_it==symbol_table.symbols.end())
301  return true;
302 
303  // remove the return_value symbol from the symbol_table
304  irep_idt rv_name_id=rv_it->second.name;
305  symbol_table.erase(rv_it);
306 
307  bool did_something = false;
308 
309  for(auto &instruction : goto_program.instructions)
310  {
311  if(instruction.is_assign())
312  {
313  const auto &assign = instruction.get_assign();
314 
315  if(assign.lhs().id()!=ID_symbol ||
316  to_symbol_expr(assign.lhs()).get_identifier()!=rv_name_id)
317  continue;
318 
319  // replace "fkt#return_value=x;" by "return x;"
320  const exprt rhs = assign.rhs();
321  instruction = goto_programt::make_return(
322  code_returnt(rhs), instruction.source_location);
323  did_something = true;
324  }
325  }
326 
327  if(did_something)
328  remove_skip(goto_program);
329 
330  return false;
331 }
332 
335  goto_programt &goto_program)
336 {
338 
339  Forall_goto_program_instructions(i_it, goto_program)
340  {
341  if(i_it->is_function_call())
342  {
343  code_function_callt function_call = i_it->get_function_call();
344 
345  // ignore function pointers
346  if(function_call.function().id()!=ID_symbol)
347  continue;
348 
349  const irep_idt function_id=
350  to_symbol_expr(function_call.function()).get_identifier();
351 
352  // find "f(...); lhs=f#return_value; DEAD f#return_value;"
353  // and revert to "lhs=f(...);"
354  goto_programt::instructionst::iterator next = std::next(i_it);
355 
356  INVARIANT(
357  next!=goto_program.instructions.end(),
358  "non-void function call must be followed by #return_value read");
359 
360  if(!next->is_assign())
361  continue;
362 
363  const code_assignt &assign = next->get_assign();
364 
365  const auto rv_symbol = return_value_symbol(function_id, ns);
366  if(assign.rhs() != rv_symbol)
367  continue;
368 
369  // restore the previous assignment
370  function_call.lhs()=assign.lhs();
371 
372  i_it->set_function_call(function_call);
373 
374  // remove the assignment and subsequent dead
375  // i_it remains valid
376  next=goto_program.instructions.erase(next);
377  INVARIANT(
378  next!=goto_program.instructions.end() && next->is_dead(),
379  "read from #return_value should be followed by DEAD #return_value");
380  // i_it remains valid
381  goto_program.instructions.erase(next);
382  }
383  }
384 }
385 
387 {
388  // restore all types first
389  bool unmodified=true;
390  for(auto &gf_entry : goto_functions.function_map)
391  {
392  unmodified =
393  restore_returns(gf_entry.first, gf_entry.second.body) && unmodified;
394  }
395 
396  if(!unmodified)
397  {
398  for(auto &gf_entry : goto_functions.function_map)
399  undo_function_calls(gf_entry.second.body);
400  }
401 }
402 
404 void restore_returns(goto_modelt &goto_model)
405 {
406  remove_returnst rr(goto_model.symbol_table);
407  rr.restore(goto_model.goto_functions);
408 }
409 
411 {
412  return id2string(identifier) + RETURN_VALUE_SUFFIX;
413 }
414 
416 return_value_symbol(const irep_idt &identifier, const namespacet &ns)
417 {
418  const symbolt &function_symbol = ns.lookup(identifier);
419  const typet &return_type = to_code_type(function_symbol.type).return_type();
420  return symbol_exprt(return_value_identifier(identifier), return_type);
421 }
422 
424 {
426 }
427 
428 bool is_return_value_symbol(const symbol_exprt &symbol_expr)
429 {
430  return is_return_value_identifier(symbol_expr.get_identifier());
431 }
432 
434 {
435  return to_code_type(function_call.function().type()).return_type() !=
436  empty_typet() &&
437  function_call.lhs().is_not_nil();
438 }
Internally generated symbol table entryThis is a symbol generated as part of translation to or modifi...
Definition: symbol.h:147
A codet representing an assignment in the program.
Definition: std_code.h:295
exprt & rhs()
Definition: std_code.h:317
exprt & lhs()
Definition: std_code.h:312
codet representation of a function call statement.
Definition: std_code.h:1215
exprt & function()
Definition: std_code.h:1250
codet representation of a "return from a function" statement.
Definition: std_code.h:1342
const typet & return_type() const
Definition: std_types.h:850
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
The empty type.
Definition: std_types.h:46
Base class for all expressions.
Definition: expr.h:54
const source_locationt & source_location() const
Definition: expr.h:234
typet & type()
Return the type of the expression.
Definition: expr.h:82
A collection of goto functions.
function_mapt function_map
::goto_functiont goto_functiont
void compute_location_numbers()
Interface providing access to a single function in a GOTO model, plus its associated symbol table.
Definition: goto_model.h:183
const irep_idt & get_function_id()
Get function id.
Definition: goto_model.h:232
goto_functionst::goto_functiont & get_goto_function()
Get GOTO function.
Definition: goto_model.h:225
void compute_location_numbers()
Re-number our goto_function.
Definition: goto_model.h:209
journalling_symbol_tablet & get_symbol_table()
Get symbol table.
Definition: goto_model.h:218
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:556
static instructiont make_return(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:809
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:898
instructionst::iterator targett
Definition: goto_program.h:550
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
Definition: goto_program.h:995
targett insert_after(const_targett target)
Insertion after the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:626
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:431
bool is_not_nil() const
Definition: irep.h:391
const irep_idt & id() const
Definition: irep.h:407
void make_nil()
Definition: irep.h:464
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
bool restore_returns(const irep_idt &function_id, goto_programt &goto_program)
turns an assignment to fkt::return_value back into 'return x'
void operator()(goto_functionst &goto_functions)
symbol_table_baset & symbol_table
optionalt< symbol_exprt > get_or_create_return_value_symbol(const irep_idt &function_id)
remove_returnst(symbol_table_baset &_symbol_table)
void undo_function_calls(goto_programt &goto_program)
turns f(...); lhs=f::return_value; into lhs=f(...)
void replace_returns(const irep_idt &function_id, goto_functionst::goto_functiont &function)
turns 'return x' into an assignment to fkt::return_value
void restore(goto_functionst &goto_functions)
bool do_function_calls(function_is_stubt function_is_stub, goto_programt &goto_program)
turns x=f(...) into f(...); lhs=f::return_value;
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1968
Expression to hold a symbol (variable)
Definition: std_expr.h:81
const irep_idt & get_identifier() const
Definition: std_expr.h:110
The symbol table base class interface.
virtual void erase(const symbolst::const_iterator &entry)=0
Remove a symbol from the symbol table.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
virtual symbolt * get_writeable(const irep_idt &name)=0
Find a symbol in the symbol table for read-write access.
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
bool is_static_lifetime
Definition: symbol.h:65
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
typet type
Type of symbol.
Definition: symbol.h:31
irep_idt name
The unique identifier.
Definition: symbol.h:40
irep_idt mode
Language mode.
Definition: symbol.h:49
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition: std_expr.h:1789
The type of an expression, extends irept.
Definition: type.h:28
const source_locationt & source_location() const
Definition: type.h:71
Symbol Table + CFG.
#define Forall_goto_program_instructions(it, program)
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
nonstd::optional< T > optionalt
Definition: optional.h:35
void restore_returns(goto_modelt &goto_model)
restores return statements
#define RETURN_VALUE_SUFFIX
symbol_exprt return_value_symbol(const irep_idt &identifier, const namespacet &ns)
produces the symbol that is used to store the return value of the function with the given identifier
bool is_return_value_identifier(const irep_idt &id)
Returns true if id is a special return-value symbol produced by return_value_identifier.
void remove_returns(symbol_table_baset &symbol_table, goto_functionst &goto_functions)
removes returns
bool is_return_value_symbol(const symbol_exprt &symbol_expr)
Returns true if symbol_expr is a special return-value symbol produced by return_value_symbol.
bool does_function_call_return(const code_function_callt &function_call)
Check if the function_call returns anything.
irep_idt return_value_identifier(const irep_idt &identifier)
produces the identifier that is used to store the return value of the function with the given identif...
Replace function returns by assignments to global variables.
std::function< bool(const irep_idt &)> function_is_stubt
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
Definition: remove_skip.cpp:85
Program Transformation.
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
API to expression classes.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:190
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:949
bool has_suffix(const std::string &s, const std::string &suffix)
Definition: suffix.h:17