Skip to content

Commit 522cb83

Browse files
committed
Central implementation of syntactic equality of instructions and goto programs
1 parent d224446 commit 522cb83

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/goto-programs/goto_program.cpp

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

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

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)