Skip to content

Commit cac0938

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

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/goto-programs/goto_program.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,47 @@ 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+
if(ins.targets.size() != other_it->targets.size())
661+
return false;
662+
663+
auto other_target_it = other_it->targets.begin();
664+
for(const t : ins.targets)
665+
{
666+
if(
667+
t->location_number - ins.location_number !=
668+
(*other_target_it)->location_number - other_it->location_number)
669+
{
670+
return false;
671+
}
672+
673+
++other_target_it;
674+
}
675+
676+
++other_it;
677+
}
678+
679+
return true;
680+
}

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)