Skip to content

Commit aab3e8f

Browse files
authored
Merge pull request #2785 from danpoe/doc/goto-models
Add more documentation for goto programs
2 parents 4311fe1 + e65f315 commit aab3e8f

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

src/goto-programs/abstract_goto_model.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class abstract_goto_modelt
3434
/// get_goto_functions, or the symbol table returned by get_symbol_table,
3535
/// so iterators pointing into either may be invalidated.
3636
/// \param id: function to get
37-
/// \return function body
37+
/// \return goto function
3838
virtual const goto_functionst::goto_functiont &get_goto_function(
3939
const irep_idt &id) = 0;
4040

src/goto-programs/goto_function.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ Date: May 2018
88
99
\*******************************************************************/
1010

11+
/// \file
12+
/// Goto Function
13+
1114
#include "goto_function.h"
1215

16+
/// Return in \p dest the identifiers of the local variables declared in the \p
17+
/// goto_function and the identifiers of the paramters of the \p goto_function.
1318
void get_local_identifiers(
1419
const goto_functiont &goto_function,
1520
std::set<irep_idt> &dest)

src/goto-programs/goto_function.h

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Date: May 2018
88
99
\*******************************************************************/
1010

11+
/// \file
12+
/// Goto Function
13+
1114
#ifndef CPROVER_GOTO_PROGRAMS_GOTO_FUNCTION_H
1215
#define CPROVER_GOTO_PROGRAMS_GOTO_FUNCTION_H
1316

@@ -17,13 +20,24 @@ Date: May 2018
1720

1821
#include "goto_program.h"
1922

23+
/// A goto function, consisting of function type (see #type), function body (see
24+
/// #body), and parameter identifiers (see #parameter_identifiers).
2025
class goto_functiont
2126
{
2227
public:
2328
goto_programt body;
29+
30+
/// The type of the function, indicating the return type and parameter types
2431
code_typet type;
2532

2633
typedef std::vector<irep_idt> parameter_identifierst;
34+
35+
/// The identifiers of the parameters of this function
36+
///
37+
/// Note: This variable is currently unused and the vector is thus always
38+
/// empty. In the future the code base may be refactored to fill in the
39+
/// parameter identifiers here when creating a `goto_functiont`. For now the
40+
/// parameter identifiers should be retrieved from the type (`code_typet`).
2741
parameter_identifierst parameter_identifiers;
2842

2943
bool body_available() const

src/goto-programs/goto_functions.h

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Date: June 2003
1818

1919
#include <util/cprover_prefix.h>
2020

21+
/// A collection of goto functions
2122
class goto_functionst
2223
{
2324
public:
@@ -63,6 +64,7 @@ class goto_functionst
6364
return *this;
6465
}
6566

67+
/// Remove function from the function map
6668
void unload(const irep_idt &name) { function_map.erase(name); }
6769

6870
void clear()
@@ -99,6 +101,7 @@ class goto_functionst
99101
update_instructions_function();
100102
}
101103

104+
/// Get the identifier of the entry point to a goto model
102105
static inline irep_idt entry_point()
103106
{
104107
// do not confuse with C's "int main()"

src/goto-programs/goto_program.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ std::string as_string(
487487
}
488488
}
489489

490+
/// Assign each loop in the goto program a unique index. Every backwards goto is
491+
/// considered a loop. The loops are numbered starting from zero and in the
492+
/// order they appear in the goto program.
490493
void goto_programt::compute_loop_numbers()
491494
{
492495
unsigned nr=0;
@@ -516,6 +519,16 @@ std::ostream &goto_programt::output(
516519
return out;
517520
}
518521

522+
/// Assign each target (i.e., an instruction that is in the `targets` list of
523+
/// another instruction) a unique index.
524+
///
525+
/// Instructions that are not targets get target number instructiont::nil_target. The
526+
/// targets are numbered starting from one and in the order they appear in the
527+
/// goto program. An instruction is considered a target if it is the target of a
528+
/// control-flow instruction (either GOTO or START_THREAD), i.e., it is
529+
/// contained in the `targets` list of those instructions. Instructions that are
530+
/// reached via straight-line control flow (fall-through for GOTO instructions)
531+
/// only are not considered targets.
519532
void goto_programt::compute_target_numbers()
520533
{
521534
// reset marking
@@ -564,6 +577,10 @@ void goto_programt::compute_target_numbers()
564577
}
565578
}
566579

580+
/// Copy other goto program into this goto program. The current goto program is
581+
/// cleared, and targets are adjusted as needed
582+
///
583+
/// \param src: the goto program to copy from
567584
void goto_programt::copy_from(const goto_programt &src)
568585
{
569586
// Definitions for mapping between the two programs
@@ -604,7 +621,8 @@ void goto_programt::copy_from(const goto_programt &src)
604621
compute_target_numbers();
605622
}
606623

607-
// number them
624+
/// Returns true if the goto program includes an `ASSERT` instruction the guard
625+
/// of which is not trivially true.
608626
bool goto_programt::has_assertion() const
609627
{
610628
for(const auto &i : instructions)
@@ -614,6 +632,7 @@ bool goto_programt::has_assertion() const
614632
return false;
615633
}
616634

635+
/// Compute for each instruction the set of instructions it is a successor of.
617636
void goto_programt::compute_incoming_edges()
618637
{
619638
for(auto &i : instructions)

src/goto-programs/goto_program.h

+42-2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class goto_programt
170170
/// clears all the catch clauses established as per the above in this
171171
/// function?
172172
/// Many analysis tools remove these instructions before they start.
173+
/// - INCOMPLETE GOTO:
174+
/// goto for which the target is yet to be determined. The target set
175+
/// shall be empty
173176
class instructiont final
174177
{
175178
public:
@@ -421,15 +424,34 @@ class goto_programt
421424
return t;
422425
}
423426

427+
/// Get the id of the function that contains the instruction pointed-to by the
428+
/// given instruction iterator.
429+
///
430+
/// \param l: instruction iterator
431+
/// \return id of the function that contains the pointed-to goto instruction
424432
static const irep_idt get_function_id(
425433
const_targett l)
426434
{
435+
// The field `function` of an instruction may not always contain the id of
436+
// the function it is currently in, due to goto program modifications such
437+
// as inlining. For example, if an instruction in a function `f` is inlined
438+
// into a function `g`, the instruction may, depending on the arguments to
439+
// the inliner, retain the original value of `f` in the function field.
440+
// However, instructions of type END_FUNCTION are never inlined into other
441+
// functions, hence they contain the id of the function they are in. Thus,
442+
// this function takes the END_FUNCTION instruction of the goto program and
443+
// returns the value of its function field.
444+
427445
while(!l->is_end_function())
428446
++l;
429447

430448
return l->function;
431449
}
432450

451+
/// Get the id of the function that contains the given goto program.
452+
///
453+
/// \param p: the goto program
454+
/// \return id of the function that contains the goto program
433455
static const irep_idt get_function_id(
434456
const goto_programt &p)
435457
{
@@ -474,14 +496,16 @@ class goto_programt
474496
instructions.splice(next, p.instructions);
475497
}
476498

477-
/// Insertion before the given target
499+
/// Insertion before the instruction pointed-to by the given instruction
500+
/// iterator `target`.
478501
/// \return newly inserted location
479502
targett insert_before(const_targett target)
480503
{
481504
return instructions.insert(target, instructiont());
482505
}
483506

484-
/// Insertion after the given target
507+
/// Insertion after the instruction pointed-to by the given instruction
508+
/// iterator `target`.
485509
/// \return newly inserted location
486510
targett insert_after(const_targett target)
487511
{
@@ -619,6 +643,8 @@ class goto_programt
619643
instructions.clear();
620644
}
621645

646+
/// Get an instruction iterator pointing to the END_FUNCTION instruction of
647+
/// the goto program
622648
targett get_end_function()
623649
{
624650
PRECONDITION(!instructions.empty());
@@ -628,6 +654,8 @@ class goto_programt
628654
return end_function;
629655
}
630656

657+
/// Get an instruction iterator pointing to the END_FUNCTION instruction of
658+
/// the goto program
631659
const_targett get_end_function() const
632660
{
633661
PRECONDITION(!instructions.empty());
@@ -653,6 +681,18 @@ class goto_programt
653681
bool equals(const goto_programt &other) const;
654682
};
655683

684+
/// Get control-flow successors of a given instruction. The instruction is
685+
/// represented by a pointer `target` of type `Target`. An instruction has
686+
/// either 0, 1, or 2 successors (more than two successors is deprecated). For
687+
/// example, an `ASSUME` instruction with the `guard` being a `false_exprt` has
688+
/// 0 successors, and `ASSIGN` instruction has 1 successor, and a `GOTO`
689+
/// instruction with the `guard` not being a `true_exprt` has 2 successors.
690+
///
691+
/// \tparam Target: type used to represent a pointer to an instruction in a goto
692+
/// program
693+
/// \param target: pointer to the instruction of which to get the successors of
694+
/// \return List of control-flow successors of the pointed-to goto program
695+
/// instruction
656696
template <typename Target>
657697
std::list<Target> goto_programt::get_successors(
658698
Target target) const

0 commit comments

Comments
 (0)