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 auto sorted_funcs = sorted();
25 for(auto &it : sorted_funcs)
26 {
27 // Side-effect: bumps unused_location_number.
28 it->second.body.compute_location_numbers(unused_location_number);
29 }
30}
31
33 goto_programt &program)
34{
35 // Renumber just this single function. Use fresh numbers in case it has
36 // grown since it was last numbered.
38}
39
41{
42 for(auto &func : function_map)
43 {
44 func.second.body.compute_incoming_edges();
45 }
46}
47
49{
50 for(auto &func : function_map)
51 {
52 func.second.body.compute_target_numbers();
53 }
54}
55
57{
58 for(auto &func : function_map)
59 {
60 func.second.body.compute_loop_numbers();
61 }
62}
63
65std::vector<goto_functionst::function_mapt::const_iterator>
67{
68 std::vector<function_mapt::const_iterator> result;
69
70 result.reserve(function_map.size());
71
72 for(auto it = function_map.begin(); it != function_map.end(); it++)
73 result.push_back(it);
74
75 std::sort(
76 result.begin(),
77 result.end(),
78 [](function_mapt::const_iterator a, function_mapt::const_iterator b) {
79 return id2string(a->first) < id2string(b->first);
80 });
81
82 return result;
83}
84
86std::vector<goto_functionst::function_mapt::iterator> goto_functionst::sorted()
87{
88 std::vector<function_mapt::iterator> result;
89
90 result.reserve(function_map.size());
91
92 for(auto it = function_map.begin(); it != function_map.end(); it++)
93 result.push_back(it);
94
95 std::sort(
96 result.begin(),
97 result.end(),
98 [](function_mapt::iterator a, function_mapt::iterator b) {
99 return id2string(a->first) < id2string(b->first);
100 });
101
102 return result;
103}
104
106 const
107{
108 for(const auto &entry : function_map)
109 {
110 const goto_functiont &goto_function = entry.second;
111 const auto &function_name = entry.first;
112 const symbolt &function_symbol = ns.lookup(function_name);
113 const code_typet::parameterst &parameters =
114 to_code_type(function_symbol.type).parameters();
115
117 vm,
118 goto_function.parameter_identifiers.size() == parameters.size(),
119 id2string(function_name) + " parameter count inconsistency\n" +
120 "goto program: " +
121 std::to_string(goto_function.parameter_identifiers.size()) +
122 "\nsymbol table: " + std::to_string(parameters.size()));
123
124 auto it = goto_function.parameter_identifiers.begin();
125 for(const auto &parameter : parameters)
126 {
128 vm,
129 it->empty() || ns.lookup(*it).type == parameter.type(),
130 id2string(function_name) + " parameter type inconsistency\n" +
131 "goto program: " + ns.lookup(*it).type.id_string() +
132 "\nsymbol table: " + parameter.type().id_string());
133 ++it;
134 }
135
136 goto_function.validate(ns, vm);
137
138 // Check that a void function does not contain any RETURN instructions
139 if(to_code_type(function_symbol.type).return_type().id() == ID_empty)
140 {
141 for(const auto &instruction : goto_function.body.instructions)
142 {
144 vm,
145 !instruction.is_set_return_value(),
146 "void function should not return a value");
147 }
148 }
149 }
150}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
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