Skip to content

Commit 82b1503

Browse files
committed
Introducing GOTO program statistics.
The module collects a basic summary statistical information about passed GOTO program. Data like number of variables, functions, loops, conditional jumps, etc. are collected. The statistical data are especially useful for raw comparison of two GOTO programs. For instance, it can tell us how effective was program slicing.
1 parent 5d75f2e commit 82b1503

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*******************************************************************\
2+
3+
Module: goto_statistics.cpp
4+
5+
Author: Marek Trtik
6+
7+
\*******************************************************************/
8+
9+
#include "goto_statistics.h"
10+
#include <sstream>
11+
12+
void goto_statisticst::extend(
13+
const goto_functionst &functions,
14+
const symbol_tablet & table)
15+
{
16+
for(auto const &elem : functions.function_map)
17+
{
18+
for(const auto &instr : elem.second.body.instructions)
19+
{
20+
switch(instr.type)
21+
{
22+
case FUNCTION_CALL:
23+
++num_function_calls;
24+
break;
25+
case ASSERT:
26+
++num_assertions;
27+
break;
28+
case ASSUME:
29+
++num_assumptions;
30+
break;
31+
case GOTO:
32+
if (instr.guard.is_true())
33+
++num_unconditional_gotos;
34+
else
35+
++num_conditional_gotos;
36+
if(instr.is_backwards_goto())
37+
++num_loops;
38+
break;
39+
default:
40+
break;
41+
}
42+
++num_instructions;
43+
}
44+
++num_functions;
45+
}
46+
for(const auto &name_symbol : table.symbols)
47+
{
48+
if(name_symbol.second.is_lvalue and name_symbol.second.is_state_var)
49+
{
50+
if(name_symbol.second.is_auxiliary)
51+
++num_auxiliary_variables;
52+
else
53+
++num_genuine_variables;
54+
}
55+
}
56+
}
57+
58+
jsont to_json(const goto_statisticst &stats)
59+
{
60+
struct localt
61+
{
62+
static std::string to_string(const std::size_t number)
63+
{
64+
std::stringstream sstr;
65+
sstr << number;
66+
return sstr.str();
67+
}
68+
};
69+
70+
json_objectt json_stats;
71+
json_stats["num_functions"]=
72+
json_numbert(localt::to_string(stats.get_num_functions()));
73+
json_stats["num_instructions"]=
74+
json_numbert(localt::to_string(stats.get_num_instructions()));
75+
json_stats["num_genuine_variables"]=
76+
json_numbert(localt::to_string(stats.get_num_genuine_variables()));
77+
json_stats["num_auxiliary_variables"]=
78+
json_numbert(localt::to_string(stats.get_num_auxiliary_variables()));
79+
json_stats["num_function_calls"]=
80+
json_numbert(localt::to_string(stats.get_num_function_calls()));
81+
json_stats["num_unconditional_gotos"]=
82+
json_numbert(localt::to_string(stats.get_num_unconditional_gotos()));
83+
json_stats["num_conditional_gotos"]=
84+
json_numbert(localt::to_string(stats.get_num_conditional_gotos()));
85+
json_stats["num_loops"]=
86+
json_numbert(localt::to_string(stats.get_num_loops()));
87+
88+
return json_stats;
89+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*******************************************************************\
2+
3+
Module: goto_statistics.h
4+
5+
Author: Marek Trtik
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_GOTO_PROGRAMS_GOTO_STATISTICS_H
10+
#define CPROVER_GOTO_PROGRAMS_GOTO_STATISTICS_H
11+
12+
/// \file goto-programs/goto_statistics.h
13+
/// \brief Computation of statistal properties of a GOTO program.
14+
15+
#include <cstddef>
16+
#include <util/json.h>
17+
#include "goto_model.h"
18+
19+
class goto_statisticst
20+
{
21+
public:
22+
goto_statisticst():
23+
num_functions(0UL),
24+
num_instructions(0UL),
25+
num_genuine_variables(0UL),
26+
num_auxiliary_variables(0UL),
27+
num_function_calls(0UL),
28+
num_unconditional_gotos(0UL),
29+
num_conditional_gotos(0UL),
30+
num_assumptions(0UL),
31+
num_assertions(0UL),
32+
num_loops(0UL)
33+
{}
34+
35+
void extend(
36+
const goto_functionst &functions,
37+
const symbol_tablet &table);
38+
39+
void extend(const goto_modelt &model)
40+
{
41+
extend(model.goto_functions, model.symbol_table);
42+
}
43+
44+
void clear() { *this=goto_statisticst(); }
45+
46+
std::size_t get_num_functions() const
47+
{
48+
return num_functions;
49+
}
50+
51+
std::size_t get_num_instructions() const
52+
{
53+
return num_instructions;
54+
}
55+
56+
std::size_t get_num_genuine_variables() const
57+
{
58+
return num_genuine_variables;
59+
}
60+
61+
std::size_t get_num_auxiliary_variables() const
62+
{
63+
return num_auxiliary_variables;
64+
}
65+
66+
std::size_t get_num_function_calls() const
67+
{
68+
return num_function_calls;
69+
}
70+
71+
std::size_t get_num_unconditional_gotos() const
72+
{
73+
return num_unconditional_gotos;
74+
}
75+
76+
std::size_t get_num_conditional_gotos() const
77+
{
78+
return num_conditional_gotos;
79+
}
80+
81+
std::size_t get_num_loops() const
82+
{
83+
return num_loops;
84+
}
85+
86+
private:
87+
std::size_t num_functions;
88+
std::size_t num_instructions;
89+
std::size_t num_genuine_variables;
90+
std::size_t num_auxiliary_variables;
91+
std::size_t num_function_calls;
92+
std::size_t num_unconditional_gotos;
93+
std::size_t num_conditional_gotos;
94+
std::size_t num_assumptions;
95+
std::size_t num_assertions;
96+
std::size_t num_loops;
97+
};
98+
99+
jsont to_json(const goto_statisticst &stats);
100+
101+
#endif
102+

0 commit comments

Comments
 (0)