Add a function to check if two defs overlap.

Very useful for alias handling :)
This commit is contained in:
Bill Currie 2012-12-10 13:23:45 +09:00
parent fa45ab842f
commit 4f70b48370
2 changed files with 31 additions and 0 deletions

View file

@ -247,6 +247,13 @@ void initialize_def (struct symbol_s *sym, struct type_s *type,
struct expr_s *init, struct defspace_s *space,
storage_class_t storage);
/** Determine if two defs overlap.
\param d1 The first def to check. May be an alias def.
\param d2 The second def to check. May be an alias def.
\return 1 if the defs overlap, 0 otherwise.
*/
int def_overlap (def_t *d1, def_t *d2);
//@}
#endif//__def_h

View file

@ -587,3 +587,27 @@ initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
}
sym->s.def->initializer = init;
}
int
def_overlap (def_t *d1, def_t *d2)
{
int offs1, size1;
int offs2, size2;
/// Defs in different spaces never overlap.
if (d1->space != d2->space)
return 0;
offs1 = d1->offset;
if (d1->alias)
offs1 += d1->alias->offset;
size1 = type_size (d1->type);
offs2 = d2->offset;
if (d2->alias)
offs2 += d2->alias->offset;
size2 = type_size (d2->type);
if (offs1 < offs2 + size2 && offs2 < offs1 + size1)
return 1;
return 0;
}