CBMC
Loading...
Searching...
No Matches
goto_functions.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Goto Programs with Functions
4
5Author: Daniel Kroening
6
7Date: June 2003
8
9\*******************************************************************/
10
13
14#include "goto_functions.h"
15
16#include <util/namespace.h>
17#include <util/symbol.h>
18
19#include <algorithm>
20
22{
24 for(auto &func : function_map)
25 {
26 // Side-effect: bumps unused_location_number.
27 func.second.body.compute_location_numbers(unused_location_number);
28 }
29}
30
32 goto_programt &program)
33{
34 // Renumber just this single function. Use fresh numbers in case it has
35 // grown since it was last numbered.
37}
38
40{
41 for(auto &func : function_map)
42 {
43 func.second.body.compute_incoming_edges();
44 }
45}
46
48{
49 for(auto &func : function_map)
50 {
51 func.second.body.compute_target_numbers();
52 }
53}
54
56{
57 for(auto &func : function_map)
58 {
59 func.second.body.compute_loop_numbers();
60 }
61}
62
64std::vector<goto_functionst::function_mapt::const_iterator>
66{
67 std::vector<function_mapt::const_iterator> result;
68
69 result.reserve(function_map.size());
70
71 for(auto it = function_map.begin(); it != function_map.end(); it++)
72 result.push_back(it);
73
74 std::sort(
75 result.begin(),
76 result.end(),
77 [](function_mapt::const_iterator a, function_mapt::const_iterator b) {
78 return id2string(a->first) < id2string(b->first);
79 });
80
81 return result;
82}
83
85std::vector<goto_functionst::function_mapt::iterator> goto_functionst::sorted()
86{
87 std::vector<function_mapt::iterator> result;
88
89 result.reserve(function_map.size());
90
91 for(auto it = function_map.begin(); it != function_map.end(); it++)
92 result.push_back(it);
93
94 std::sort(
95 result.begin(),
96 result.end(),
97 [](function_mapt::iterator a, function_mapt::iterator b) {
98 return id2string(a->first) < id2string(b->first);
99 });
100
101 return result;
102}
103
105 const
106{
107 for(const auto &entry : function_map)
108 {
109 const goto_functiont &goto_function = entry.second;
110 const auto &function_name = entry.first;
111 const symbolt &function_symbol = ns.lookup(function_name);
112 const code_typet::parameterst &parameters =
113 to_code_type(function_symbol.type).parameters();
114
116 vm,
117 goto_function.parameter_identifiers.size() == parameters.size(),
118 id2string(function_name) + " parameter count inconsistency\n" +
119 "goto program: " +
120 std::to_string(goto_function.parameter_identifiers.size()) +
121 "\nsymbol table: " + std::to_string(parameters.size()));
122
123 auto it = goto_function.parameter_identifiers.begin();
124 for(const auto &parameter : parameters)
125 {
127 vm,
128 it->empty() || ns.lookup(*it).type == parameter.type(),
129 id2string(function_name) + " parameter type inconsistency\n" +
130 "goto program: " + ns.lookup(*it).type.id_string() +
131 "\nsymbol table: " + parameter.type().id_string());
132 ++it;
133 }
134
135 goto_function.validate(ns, vm);
136
137 // Check that a void function does not contain any RETURN instructions
138 if(to_code_type(function_symbol.type).return_type().id() == ID_empty)
139 {
140 for(const auto &instruction : goto_function.body.instructions)
141 {
143 vm,
144 !instruction.is_set_return_value(),
145 "void function should not return a value");
146 }
147 }
148 }
149}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
std::vector< parametert > parameterst
Definition std_types.h:586
void compute_incoming_edges()
unsigned unused_location_number
A location number such that numbers in the interval [unused_location_number, MAX_UINT] are all unused...
function_mapt function_map
void compute_location_numbers()
void validate(const namespacet &, validation_modet) const
Check that the goto functions are well-formed.
std::vector< function_mapt::const_iterator > sorted() const
returns a vector of the iterators in alphabetical order
void compute_target_numbers()
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
goto_programt body
parameter_identifierst parameter_identifiers
The identifiers of the parameters of this function.
void validate(const namespacet &ns, const validation_modet vm) const
Check that the goto function is well-formed.
A generic container class for the GOTO intermediate representation of one function.
instructionst instructions
The list of instructions in the goto program.
void compute_location_numbers(unsigned &nr)
Compute location numbers.
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Symbol table entry.
Definition symbol.h:28
Goto Programs with Functions.
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition std_types.h:788
Symbol table entry.
#define DATA_CHECK(vm, condition, message)
This macro takes a condition which denotes a well-formedness criterion on goto programs,...
Definition validate.h:22
validation_modet