Skip to content

Commit 8b2a36e

Browse files
committed
Central implementation of syntactic equality of instructions and goto programs
1 parent 70bca5e commit 8b2a36e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/goto-programs/goto_program.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,45 @@ void goto_programt::compute_incoming_edges()
634634
}
635635
}
636636

637+
bool goto_programt::instructiont::equals(const instructiont &other) const
638+
{
639+
// clang-format off
640+
return
641+
type == other.type &&
642+
code == other.code &&
643+
guard == other.guard &&
644+
targets.size() == other.targets.size() &&
645+
labels == other.labels;
646+
// clang-format on
647+
}
648+
649+
bool goto_programt::equals(const goto_programt &other) const
650+
{
651+
if(instructions.size() != other.instructions.size())
652+
return false;
653+
654+
goto_programt::const_targett other_it = other.instructions.begin();
655+
for(const auto &ins : instructions)
656+
{
657+
if(!ins.equals(*other_it))
658+
return false;
659+
660+
// the number of targets is the same as instructiont::equals returned "true"
661+
auto other_target_it = other_it->targets.begin();
662+
for(const auto t : ins.targets)
663+
{
664+
if(
665+
t->location_number - ins.location_number !=
666+
(*other_target_it)->location_number - other_it->location_number)
667+
{
668+
return false;
669+
}
670+
671+
++other_target_it;
672+
}
673+
674+
++other_it;
675+
}
676+
677+
return true;
678+
}

src/goto-programs/goto_program.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ class goto_programt
360360
instruction_id_builder << type;
361361
return instruction_id_builder.str();
362362
}
363+
364+
/// Syntactic equality: two instructiont are equal if they have the same
365+
/// type, code, guard, number of targets, and labels. All other members can
366+
/// only be evaluated in the context of a goto_programt (see
367+
/// goto_programt::equals).
368+
bool equals(const instructiont &other) const;
363369
};
364370

365371
// Never try to change this to vector-we mutate the list while iterating
@@ -611,6 +617,11 @@ class goto_programt
611617
typedef std::set<irep_idt> decl_identifierst;
612618
/// get the variables in decl statements
613619
void get_decl_identifiers(decl_identifierst &decl_identifiers) const;
620+
621+
/// Syntactic equality: two goto_programt are equal if, and only if, they have
622+
/// the same number of instructions, each pair of instructions compares equal,
623+
/// and relative jumps have the same distance.
624+
bool equals(const goto_programt &other) const;
614625
};
615626

616627
template <typename Target>

0 commit comments

Comments
 (0)