Skip to content

Commit 86af99b

Browse files
committed
Central implementation of syntactic equality of instructions and goto programs
1 parent 61f14d8 commit 86af99b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/goto-programs/goto_program.cpp

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

637+
bool goto_programt::instructiont::equal(const instructiont &other) const
638+
{
639+
return
640+
type == other.type && code == other.code && guard == other.guard &&
641+
targets.size() == other.targets.size() && labels == other.labels;
642+
}
643+
644+
bool goto_programt::equal(const goto_programt &other) const
645+
{
646+
if(instructions.size() != other.instructions.size())
647+
return false;
648+
649+
goto_programt::const_targett other_it = other.instructions.begin();
650+
for(const auto &ins : instructions)
651+
{
652+
if(!ins.equal(*other_it))
653+
return false;
654+
655+
long jump_difference1 = 0;
656+
if(!ins.targets.empty())
657+
{
658+
jump_difference1 =
659+
ins.get_target()->location_number - ins.location_number;
660+
}
661+
long jump_difference2 = 0;
662+
if(!other_it->targets.empty())
663+
{
664+
jump_difference2 =
665+
other_it->get_target()->location_number - other_it->location_number;
666+
}
667+
if(jump_difference1 != jump_difference2)
668+
return false;
669+
670+
++other_it;
671+
}
672+
673+
return true;
674+
}

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::equal).
368+
bool equal(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 equal(const goto_programt &other) const;
614625
};
615626

616627
template <typename Target>

0 commit comments

Comments
 (0)