Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
linklist.h
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 #ifndef linklist_INCLUDED
00037 #define linklist_INCLUDED
00038 #ifdef __cplusplus
00039 extern "C" {
00040 #endif
00041 /* ====================================================================
00042  * ====================================================================
00043  *
00044  *
00045  * Revision history:
00046  *  01-Feb-89 - Original Version
00047  *  26-May-91 - Integrated from Josie
00048  *
00049  * Description:
00050  *
00051  * This file contains definitions of linked list data structures and
00052  * macros that manipulate them.  Both simple linked lists and arrays
00053  * of simple linked lists are defined.  Macros that manipulate arrays
00054  * of linked lists are always prefaced with 'ARY_'.  These lists are
00055  * singly-linked,  i.e. they have a 'next' pointer, but not a 'prev'
00056  * pointer.
00057  *
00058  * The definitions are set up for linked lists of 32-bit integers.
00059  * Additional sets of macros may be defined to extend the definitions
00060  * to apply to linked lists of any objects that can be represented
00061  * in 32 bits or less.  The files "bb_lst.h" and "tn_lst.h" contain
00062  * examples of these extentions.  E.g. "bb_lst.h" defines names that
00063  * can be used to work with linked lists of BB numbers.  It builds
00064  * these names from the generic linked-list names defined in this file.
00065  *
00066  * This file defines types, macros and functions for linked list
00067  * objects.  The types defined are:
00068  *    tlst_val ... a data value in a linked list (it is a 32-bit object)
00069  *    LST_ITM  ... item in a linked list (data val and ptr to next item)
00070  *    LNK_LST  ... a header of a single linked list
00071  *    LNK_LST_ARY ...  a header of an array of linked lists
00072  *
00073  *
00074  * A variety of macros and functions are defined in this file to
00075  * manipulate linked lists.  Some of these are low-level utilities
00076  * used only internally by the linked list package.  Those that are
00077  * meant to be visible externally, are described in the table, below.
00078  * In the table descriptions, the following variable names are used
00079  * as arguments to the functions and macros.
00080  *
00081  * Macro and function parameters:
00082  *    l   ... ptr of a LNK_LST object (header of a single list)
00083  *    a   ... ptr of a LNK_LST_ARY object (header of an array of lists)
00084  *    i   ... index into the array of lists
00085  *    n   ... an integer (e.g. the number of elements in the array
00086  *    s   ... storage for recursively traversing lists (ptr to LST_ITM)
00087  *    msg ... a printf-style character string message
00088  *
00089  *
00090  * An 'M' in the left column of the following table indicates the
00091  * operation is implemented as a macro, rather than a function.  Some
00092  * examples of how these macros/functions are used appear to manipulate
00093  * the linked lists are given after the table.
00094  *
00095  * Initialization, allocation, and freeing:
00096  * ----------------------------------------
00097  * M  void Init_List(l)          Initialize pointers and count for
00098  *                               list 'l'
00099  *    void Free_List(l)          Free all items in the list 'l'
00100  * M  void ARY_Del_List(a,i)     Delete (Free) the list 'a[i]'
00101  *    void ARY_Init_List(a,n)    Allocate and init an array of 'n' lists
00102  *    void ARY_Free_List(a)      Free an array of lists, 'a'
00103  *
00104  *
00105  * List queries:
00106  * -------------
00107  * M  INT32 LST_Len(l)             The number of items in the list 'l'
00108  *    BOOL Item_In_List(l,v)       Check if item 'v' is in list 'l'
00109  * M  BOOL LST_Empty(l)            Check to see if the list is empty
00110  * M  INT32 ARY_LST_Len(a,i)       For the list a[i], the number of
00111  *                                 items in it
00112  *    BOOL ARY_Item_In_List(a,i,v) Check to see if item 'v' is in
00113  *                                 list 'a[i]'
00114  * M  INT32 ARY_LST_n_elems(a)     The number of lists in the array of
00115  *                                 lists 'a'
00116  *    BOOL Validate_List(l)        Verify non-cyclic, correct count.
00117  *
00118  *
00119  * Stepping through lists:
00120  * -----------------------
00121  * M  tlst_val First_Item(l)       Get the value of the first item in
00122  *                                 the list 'l'
00123  * M  tlst_val Next_Item(l)        Get the value of the next item in the
00124  *                                 list 'l'
00125  * M  tlst_val ARY_First_Item(a,i) Get val of first item in list 'a[i]'
00126  * M  tlst_val ARY_Next_Item(a,i)  Get val of next item in list 'a[i]'
00127  *
00128  *
00129  * Stepping through lists, using explicit storage to remember position:
00130  * --------------------------------------------------------------------
00131  * M  tlst_val First_Item_Strg(l,s)       Get the value of the first
00132  *                                        item in the list 'l'
00133  * M  tlst_val Next_Item_Strg(l,s)        Get the value of the next
00134  *                                        item in the list 'l'
00135  * M  tlst_val ARY_First_Item_Strg(a,i,s) For the list a[i], get val of
00136  *                                        first item
00137  * M  tlst_val ARY_Next_Item_Strg(a,i,s)  For the list a[i], get val of
00138  *                                        next item
00139  *
00140  *
00141  * Testing the values of list items:
00142  * ---------------------------------
00143  * M  BOOL Valid_Item(v)        Check to see if the 'v' is valid
00144  * M  BOOL Invalid_Item(v)      Check to see if the 'v' is invalid
00145  *
00146  *
00147  * Adding/Deleting items:
00148  * ----------------------
00149  *    BOOL Add_Item(l,v)             Append value 'v' to list 'l' (if it
00150  *                                   is not already there)
00151  *    BOOL Add_Item_Validate(l,v)    Add_Item with Validate_List
00152  *    void Add_Item_Dup(l,v)         Append value 'v' to 'l' (even if
00153  *                                   it is already there)
00154  *    void Add_First_Item(l,v)       Insert value 'v' to beginning of
00155  *                                   list 'l'
00156  *    BOOL Del_Item(l,v)             Delete value 'v' from list 'l'
00157  *    BOOL Del_Cur_Item(l)           Delete "current" item from list 'l'
00158  * M  BOOL ARY_Add_Item(a,i,v)       Append value 'v' to list 'a[i]' (if
00159  *                                   not already there)
00160  * M  BOOL ARY_Add_Item_Validate(a,i,v)
00161  * M  void ARY_Add_Item_Dup(a,i,v)   Append value 'v' to list 'a[i]'
00162  *                                   (even if it is already there)
00163  * M  void ARY_Del_Item(a,i,v)       Delete value 'v' from list 'a[i]'
00164  * M  void ARY_Del_Cur_Item(a,i)     Delete current item from list 'a[i]'
00165  * M  void ARY_Add_First_Item(a,i,v) Insert value 'v' to beginning of
00166  *                                   list 'a[i]'
00167  *
00168  *
00169  * Some miscellaneous routines:
00170  * ----------------------------
00171  *    void List_Print(l,msg,...)      Print message 'msg' followed by
00172  *                                    list 'l'
00173  *    void ARY_List_Print(a,msg,...)  Print message 'msg' followed by
00174  *                                    array of lists
00175  *    void Free_All_List_Items()      Free blocks of mememory allocated
00176  *                                    for linked lists
00177  *
00178  * Examples:
00179  *    -- to create (declare) a simple linked list object and a pointer
00180  *       to a list object, use:
00181  *         LNK_LST lst, *lst_ptr;
00182  *
00183  *    -- to initialize a list to be empty:
00184  *         List_Init(&lst);
00185  *               or
00186  *         List_Init(lst_ptr);
00187  *
00188  *    -- to create a header for an array of linked lists (and a
00189  *       pointer to an array), use:
00190  *         LNK_LST_ARY Succ, *Succ_Ptr;
00191  *
00192  *    -- to allocate and initialize the array of list headers for, say,
00193  *       'BB_Count' elemnts (i.e. an array of 'BB_Count' lists), use:
00194  *         ARY_Init_List(&Succ,BB_Count);
00195  *
00196  *    -- to add BB number 'bbno' to the end of a simple list, use:
00197  *         Add_Item(lst_ptr,bbno);
00198  *       (Note: if 'bbno' is already in the list, it will not be added.
00199  *        Use 'Add_Item_Dup()', if duplicate list entries are desired.)
00200  *
00201  *    -- to insert BB number 'bbno' at the beginning of a simple
00202  *       list, use:
00203  *         Add_First_Item(lst_ptr,bbno);
00204  *       (Note: this adds 'bbno' regardless of whether it is already in
00205  *        the list.)
00206  *
00207  *    -- to add BB number 'bbno' to the end of the i'th list in
00208  *       the array of lists 'Succ', use:
00209  *         ARY_Add_Item(&Succ,i,bbno);
00210  *
00211  *    -- to obtain the number of lists in the 'Succ' array of
00212  *       lists, use:
00213  *         num_lists = ARY_LST_n_elems(&Succ);
00214  *
00215  *    -- to obtain the number of items in a simple list, use:
00216  *         num_items = LST_Len(&lst);
00217  *
00218  *    -- to obtain the number of items in the i'th list in an array
00219  *       of lists named 'Succ', use:
00220  *         num_items = ARY_LST_Len(&Succ,i);
00221  *
00222  *    -- to check if BB number 'bbno' is in the list the 'lst', use:
00223  *       number of which is given by cur_bbnum:
00224  *         if (Item_In_List(&lst,bbno)) {
00225  *           ................
00226  *           ................
00227  *         }
00228  *
00229  *    -- to check if BB number 'bbno' is in the i'th element of the
00230  *       array of lists named 'Succ', use:
00231  *         if (ARY_Item_In_List(&Succ,i,bbno)) {
00232  *           ................
00233  *           ................
00234  *         }
00235  *
00236  *    -- to delete the item containing BB number 'bbno' from the
00237  *       i'th element of the array of lists named 'Succ', use:
00238  *         ARY_Del_Item(&Succ,i,bbno);
00239  *
00240  *    -- to loop over all the BB numbers 'bbno' in a simple list 'lst':
00241  *         for (bbno=First_Item(&lst); Valid_Item(bbno);
00242  *                                            bbno=Next_Item(&lst)) {
00243  *           ................
00244  *           ................
00245  *         }
00246  *
00247  *    -- to loop over all the BB numbers 'bbno' in the i'th list of
00248  *       the array of lists named 'Succ', use:
00249  *         for (bbno=ARY_First_Item(&Succ,i); Valid_Item(bbno);
00250  *                                       bbno=ARY_Next_Item(&Succ,i)) {
00251  *           ................
00252  *           ................
00253  *         }
00254  *
00255  *
00256  * NOTE: In the above examples, Basic Block numbers were used as the
00257  * list data values.  In a real program, we would not use the generic
00258  * list objects defined here.  Instead, we would use the specific
00259  * instances of the list objects define in "bb_lst.h".
00260  * As a rule, we should never see the simple references given in the
00261  * above examples; we should always see a specific instance.  For
00262  * example, rather than:
00263  *        bbno = ARY_First_Item(&Succ,i);
00264  * we would have:
00265  *        bbno = BB_ARY_First_Item(&Succ,i);
00266  * (see "bb_lst.h")
00267  *
00268  * ====================================================================
00269  * ====================================================================
00270  */
00271 
00272 
00273 /* Define a flag that enables some linked-list sanity checking: */
00274 #ifdef Is_True_On
00275 # define LNK_LST_CHECK
00276 #endif /* Is_True_On */
00277 
00278 /* Include erglob.h to make sure Is_True is available: */
00279 #ifdef LNK_LST_CHECK
00280 # include "erglob.h"
00281 #endif
00282 
00283 /*
00284  *  Defining STORE_LIST_LEN causes the length of the list to be stored
00285  *  directly in the linked list data structure.  This allows list length
00286  *  determination to be trivial.  If STORE_LIST_LEN is not defined, the
00287  *  calculation of a list length is done by a function.  We can
00288  *  experiment with the tradeoff between space (to store the list length
00289  *  directly) and time (to compute the list length), by changing the
00290  *  define STORE_LIST_LEN.
00291  *
00292  *  Note: all dependencies on STORE_LIST_LEN are in this file.
00293  *        Alternating between the two modes of list length determination
00294  *        is done by defining or un-defining STORE_LIST_LEN right here.
00295  */
00296 #define STORE_LIST_LEN
00297 
00298 /* data values in lists are 32-bit quantities: */
00299 typedef mINT32 tlst_val;
00300 
00301 typedef struct lst_itm {    /* linked list items */
00302   tlst_val val;               /* the data value of the item */
00303   struct lst_itm *next;       /* ptr to the next item of data */
00304 } LST_ITM;
00305 
00306 typedef struct lnk_lst {      /* linked list header */
00307 #ifdef STORE_LIST_LEN
00308   mINT32 len;         /* the number of data items in the list */
00309 #endif      /* STORE_LIST_LEN */
00310   LST_ITM *nxt;        /* next list item */
00311   LST_ITM *first;      /* ptr to the first data item in the list */
00312 } LNK_LST;
00313 
00314 typedef struct lnk_lst_ary {        /* array of linked lists */
00315   mINT32 n_elems;  /* number of elements in the array of linked lists */
00316   LNK_LST *lists;  /* ptr to array linked list headers */
00317 } LNK_LST_ARY;
00318 
00319 extern void ARY_Init_List(LNK_LST_ARY *ary, INT32 n_elems);
00320 extern void Free_List(LNK_LST *lst);
00321 extern void ARY_Free_List(LNK_LST_ARY *ary);
00322 extern BOOL Validate_List ( LNK_LST *lst );
00323 extern BOOL Add_Item ( LNK_LST *lst, tlst_val val );
00324 extern BOOL Add_Item_Validate ( LNK_LST *lst, tlst_val val);
00325 extern void Add_Item_Dup(LNK_LST *lst, tlst_val val);
00326 extern void Add_First_Item(LNK_LST *lst, tlst_val val);
00327 extern BOOL Add_Ordered_Item(LNK_LST *lst, tlst_val val);
00328 extern void Add_Ordered_Item_Dupl(LNK_LST *lst, tlst_val val);
00329 extern BOOL Del_Item(LNK_LST *lst, tlst_val val);
00330 extern BOOL Del_Cur_Item(LNK_LST *lst);
00331 extern BOOL Item_In_List(LNK_LST *lst, tlst_val val);
00332 extern BOOL ARY_Item_In_List(LNK_LST_ARY *ary, INT32 i, tlst_val val);
00333 extern void Free_All_List_Items(void);
00334 extern void List_Print (
00335 #ifndef NO_VARARGS_PROTOTYPES
00336   LNK_LST *lst, char *msg, ...
00337 #endif
00338   );
00339 extern void ARY_List_Print (
00340 #ifndef NO_VARARGS_PROTOTYPES
00341   LNK_LST_ARY *ary, char *msg, ...
00342 #endif
00343   );
00344 
00345 /*
00346  *  Here are the macros that manipulate the above linked list data
00347  *  structures.  We declare a temporary '_tmp_lst' that will be 'static'
00348  *  in each file that includes this file -- i.e., we will get multiple
00349  *  copies of it.  That is OK (and possibly desireable).  We use this
00350  *  only to hold very temproary information (we could completely
00351  *  eliminate it), to avoid re-evaluating some index operations.  By
00352  *  making it 'static' (rather than a simple global) it may be possible
00353  *  for some C compilers to better optimize references to it.
00354  *
00355  *  Note that we define the null list item 'NULL_ITEM' to be the
00356  *  value '-1'.  This limits the generality with which these data
00357  *  structures/macros can be used and applied.  Specifically, if '-1'
00358  *  is a valid data value, then these macros cannot be used.  This lack
00359  *  of generality is a result of the technique with which we manipulate
00360  *  the lists.  We never deal with the pointers directly, we just use
00361  *  macros that return data values, and one of the data values is
00362  *  reserved to be the end marker.
00363  *
00364  */
00365 extern LST_ITM *_lst_itm;
00366 
00367 #ifdef STORE_LIST_LEN
00368 #  define LST_Len(lst)      ((lst)->len)
00369 #  define incr_LST_len(l)   ( ++((l)->len) )
00370 #  define decr_LST_len(l)   ( --((l)->len) )
00371 #  define clr_LST_len(l)    ((l)->len = 0)
00372 #else /* STORE_LIST_LEN not defined */
00373 #  ifdef PROTOTYPE
00374      extern INT32 LST_Len(LNK_LST *lst);
00375 #  else                /* PROTOTYPE checking not supported */
00376      extern INT32 LST_Len();
00377 #  endif /* PROTOTYPE checking not supported */
00378 #  define incr_LST_len(l)   /* no-op */
00379 #  define decr_LST_len(l)   /* no-op */
00380 #  define clr_LST_len(l)    /* no-op */
00381 #endif       /* STORE_LIST_LEN not defined */
00382 
00383 /* null list item is -1 -- this limits generality */
00384 #define NULL_ITEM ((tlst_val) -1)
00385 #define Valid_Item(val)     (((tlst_val) (val)) != NULL_ITEM)
00386 #define Invalid_Item(val)   (((tlst_val) (val)) == NULL_ITEM)
00387 #define LST_val(itm)        ((itm)->val)
00388 #define LST_next(itm)       ((itm)->next)
00389 #define LST_first(lst)      ((lst)->first)
00390 #define LST_nxt(lst)        ((lst)->nxt)
00391 #define LST_Empty(lst)      ( LST_first(lst) == NULL )
00392 #define ARY_LST_n_elems(a)  ((a)->n_elems)
00393 #define LST_lists(a)        ((a)->lists)
00394 
00395 #ifdef LNK_LST_CHECK
00396    extern char *_ary_lst_bounds_msg;
00397 #  define _ary_lst_bnds_chk(a,i) \
00398         Is_True ( ((i) >= 0) && ((i)<ARY_LST_n_elems(a)), \
00399                 (_ary_lst_bounds_msg, (i), (ARY_LST_n_elems(a)-1) ) )
00400 #else   /* LNK_LST_CHECK not defined */
00401 #  define _ary_lst_bnds_chk(a,i) ((void) 1)
00402 #endif  /* LNK_LST_CHECK not defined */
00403 
00404 #define ARY_LST_Elem(a,i)   ( _ary_lst_bnds_chk((a),(i)), \
00405                                 (((a)->lists)+(i)) )
00406 #define First_Item(l)       ( (_lst_itm = LST_first(l)) ? \
00407                                  ( (LST_nxt(l) = LST_next(_lst_itm)), \
00408                                    LST_val(_lst_itm) ) : NULL_ITEM )
00409 #define Next_Item(l)        ( (_lst_itm = LST_nxt(l)) ? \
00410                                  ( (LST_nxt(l) = LST_next(_lst_itm)), \
00411                                       LST_val(_lst_itm) ) : NULL_ITEM )
00412 /* "Fast" forms of above, for simple, readonly scans of a list.
00413  * There must be no modifications to the list (additions or deletions)
00414  * during the scan.  Notice that Next_Item_Fast does not use 'l' and
00415  * Valid_Item_Fast does not use 'v'; they are there for interface
00416  * consistency.  The Fast versions of First_Item and Next_Item do
00417  * not return the item; they only control list traversal.  The user
00418  * of the Fast macros must do a Get_Item_Fast to get the value. */
00419 #define First_Item_Fast(l,t) ( (t) = LST_first(l) )
00420 #define Next_Item_Fast(l,t)  ( (t) = LST_next(t)  )
00421 #define Valid_Item_Fast(v,t) ( t )
00422 #define Get_Item_Fast(l,t)   ( LST_val(t) )
00423 #define ARY_First_Item(a,i) ( First_Item(ARY_LST_Elem(a,i)) )
00424 #define ARY_Next_Item(a,i)  ( Next_Item(ARY_LST_Elem(a,i)) )
00425 #define ARY_Del_List(a,i)   Free_List(ARY_LST_Elem(a,i))
00426 #define First_Item_Strg(l,s)       ( (s = _lst_itm = LST_first(l)) ? \
00427                                         ((s = LST_next(s)), \
00428                                         LST_val(_lst_itm)) : NULL_ITEM )
00429 /* 
00430  *  NOTE: 'l' is not used in the following macro -- it is here for
00431  *  to provide an inteface that appears consistent to the outside world.
00432  */
00433 #define Next_Item_Strg(l,s)        ( (_lst_itm = s) ? \
00434                                         ((s = LST_next(s)), \
00435                                         LST_val(_lst_itm)) : NULL_ITEM )
00436 #define ARY_First_Item_Strg(a,i,s) ( First_Item_Strg(ARY_LST_Elem(a,i),s) )
00437 #define ARY_Next_Item_Strg(a,i,s)  ( Next_Item_Strg(ARY_LST_Elem(a,i),s) )
00438 #define ARY_LST_Len(a,i)    LST_Len(ARY_LST_Elem(a,i))
00439 #define ARY_Add_Item(a,i,v) Add_Item(ARY_LST_Elem(a,i),v)
00440 #define ARY_Add_Item_Validate(a,i,v) Add_Item_Validate(ARY_LST_Elem(a,i),v)
00441 #define ARY_Add_Item_Dup(a,i,v)   Add_Item_Dup(ARY_LST_Elem(a,i),v)
00442 #define ARY_Add_First_Item(a,i,v) Add_First_Item(ARY_LST_Elem(a,i),v)
00443 #define ARY_Add_Ordered_Item(a,i,v) \
00444                         Add_Ordered_Item(ARY_LST_Elem(a,i),v)
00445 #define ARY_Add_Ordered_Item_Dupl(a,i,v) \
00446                         Add_Ordered_Item_Dupl(ARY_LST_Elem(a,i),v)
00447 #define ARY_Del_Item(a,i,v)       Del_Item(ARY_LST_Elem(a,i),v);
00448 #define ARY_Del_Cur_Item(a,i)     Del_Cur_Item(ARY_LST_Elem(a,i));
00449 #ifdef STORE_LIST_LEN
00450 #  define Init_List(l)      ( (LST_nxt(l) = LST_first(l) = NULL), \
00451                                         clr_LST_len(l) )
00452 #else      /* STORE_LIST_LEN not defined */
00453 #  define Init_List(l)      ( LST_nxt(l) = LST_first(l) = NULL )
00454 #endif      /* STORE_LIST_LEN not defined */
00455 
00456 #ifdef __cplusplus
00457 }
00458 #endif
00459 #endif /* linklist_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines