Skip to content

Commit 33e1656

Browse files
authored
Merge pull request #4091 from danpoe/doc/update-remove-returns-documentation
Add description of return removal
2 parents 2220b6e + 39b453a commit 33e1656

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

src/goto-programs/remove_returns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Date: September 2009
99
\*******************************************************************/
1010

1111
/// \file
12-
/// Remove function return values
12+
/// Replace function returns by assignments to global variables
1313

1414
#include "remove_returns.h"
1515

src/goto-programs/remove_returns.h

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,75 @@ Date: September 2009
99
\*******************************************************************/
1010

1111
/// \file
12-
/// Remove function returns
12+
/// Replace function returns by assignments to global variables
13+
14+
/// The functions `remove_returns()` remove return instructions from a goto
15+
/// program. The `restore_returns()` functions restore the returns in a goto
16+
/// program in which they have been removed previously.
17+
///
18+
/// In a goto program in which returns have not been removed, functions return
19+
/// values via the `return` instruction, which is followed by a `GOTO` to the
20+
/// end of the function. Unlike in C, in a goto program the `return` instruction
21+
/// does not cause control-flow to return to the call site. Instead, it simply
22+
/// sets the return value that will be returned once the function returns to the
23+
/// call site when the `END_FUNCTION` instruction is encountered.
24+
///
25+
/// Consider the following C code (with ... indicating further code):
26+
///
27+
/// ```
28+
/// int func()
29+
/// {
30+
/// ...
31+
/// return 1;
32+
/// ...
33+
/// }
34+
///
35+
/// r = func();
36+
///
37+
/// ```
38+
///
39+
/// This code would appear in a goto program as follows:
40+
///
41+
/// ```
42+
/// func
43+
/// ...
44+
/// return 1;
45+
/// GOTO 1
46+
/// ...
47+
/// 1: END_FUNCTION
48+
///
49+
/// r = func();
50+
/// ```
51+
///
52+
/// The return removal pass introduces a thread-local global variable
53+
/// `func#return_value` (one for each non-void function) to which the return
54+
/// value is assigned, and this variable is then assigned to the lhs expression
55+
/// that takes the function return value:
56+
///
57+
/// ```
58+
/// func
59+
/// ...
60+
/// func#return_value = 1;
61+
/// GOTO 1
62+
/// ...
63+
/// 1: END_FUNCTION
64+
///
65+
/// func();
66+
/// r = func#return_value;
67+
///
68+
/// ```
69+
///
70+
/// As `return` instructions are removed, the return types of the function types
71+
/// are set to void as well (represented by the type `empty_typet`). This
72+
/// applies both to the functions (i.e., the member `type` of `goto_functiont`)
73+
/// and to the call sites (i.e., the type
74+
/// `to_code_function_call(code).function().type()` with `code` being the code
75+
/// member of the `instructiont` instance that represents the function call).
76+
///
77+
/// The types of function pointer expressions in the goto program are however
78+
/// not changed. For example, in an assignment where `func` is assigned to a
79+
/// function pointer, such as `int (*f)(void) = func`, the function types
80+
/// appearing in the lhs and rhs both retain the integer return type.
1381

1482
#ifndef CPROVER_GOTO_PROGRAMS_REMOVE_RETURNS_H
1583
#define CPROVER_GOTO_PROGRAMS_REMOVE_RETURNS_H
@@ -25,10 +93,6 @@ class goto_model_functiont;
2593
class goto_modelt;
2694
class symbol_table_baset;
2795

28-
// Turns 'return x' into an assignment to fkt#return_value,
29-
// unless the function returns void,
30-
// and a 'goto the_end_of_the_function'.
31-
3296
void remove_returns(symbol_table_baset &, goto_functionst &);
3397

3498
typedef std::function<bool(const irep_idt &)> function_is_stubt;

0 commit comments

Comments
 (0)