From 4f70b48370a50ed58eee1f20af1aacb8666bf03b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 10 Dec 2012 13:23:45 +0900 Subject: [PATCH] Add a function to check if two defs overlap. Very useful for alias handling :) --- tools/qfcc/include/def.h | 7 +++++++ tools/qfcc/source/def.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tools/qfcc/include/def.h b/tools/qfcc/include/def.h index 77a839887..774a16242 100644 --- a/tools/qfcc/include/def.h +++ b/tools/qfcc/include/def.h @@ -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 diff --git a/tools/qfcc/source/def.c b/tools/qfcc/source/def.c index e1f164e80..82b79b9e1 100644 --- a/tools/qfcc/source/def.c +++ b/tools/qfcc/source/def.c @@ -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; +}