OpenADFortTk (basic)
src/lib/support/WhirlIDMaps.h
Go to the documentation of this file.
00001 // ##########################################################
00002 // # This file is part of OpenADFortTk.                     #
00003 // # The full COPYRIGHT notice can be found in the top      #
00004 // # level directory of the OpenADFortTk source tree.       #
00005 // # For more information visit                             #
00006 // # http://www.mcs.anl.gov/openad                          #
00007 // ##########################################################
00008 
00009 #ifndef WhirlIDMaps_INCLUDED_h
00010 #define WhirlIDMaps_INCLUDED_h
00011 
00012 #include <iostream>
00013 #include <map>  // STL
00014 #include <set>  // STL
00015 #include <list> // STL
00016 
00017 #include "Open64IRInterface/Open64BasicTypes.h"
00018 
00019 #include "BaseMap.h"
00020 #include "Diagnostics.h"
00021 
00022 
00023 namespace fortTkSupport { 
00024 
00025 typedef UINT SymTabId;
00026 typedef UINT SymId;
00027 typedef UINT PUId;
00028 typedef UINT WNId;
00029 
00030 // A list of ids: Should only be used with scalar id types above
00031 template <class T>
00032 class IdList : public std::list<T> {
00033 public:
00034   IdList() { }
00035   ~IdList() { }
00036 
00037   // Returns 0 if not found
00038   T Find(T id) const
00039   {
00040     typename std::list<T>::iterator it;
00041     for (it = this->begin(); it != this->end(); ++it) {
00042       T val = *it;
00043       if (id == val) { return val; }
00044     }
00045     return 0;
00046   }
00047   
00048 };
00049 
00050 
00051 //***************************************************************************
00052 // ST_TAB <-> SymTabId maps (global/interprocedural)
00053 //***************************************************************************
00054 
00055 class SymTabToSymTabIdMap 
00056   : public BaseMap<ST_TAB*, SymTabId>
00057 {
00058 public:
00059   SymTabToSymTabIdMap() { }
00060   SymTabToSymTabIdMap(PU_Info* pu_forest) { Create(pu_forest); }
00061   virtual ~SymTabToSymTabIdMap() { }
00062   
00063   void Create(PU_Info* pu_forest);
00064 };
00065 
00066 
00067 // SymTabIdToSymTabMap: In WHIRL, all ST_TAB* are associated with a
00068 // specific PU_Info*, except the global ST_TAB*.  Because of the way
00069 // the symbol table is implemented, it is usually not easy to access the
00070 // symbol table with the ST_TAB*: one needs the corresponding
00071 // PU_Info*.  Consequently, we map a SymTabId to a pair.  When
00072 // entering the global the global ST_TAB* in the map, PU_Info* should
00073 // be NULL.
00074 class SymTabIdToSymTabMap 
00075   : public std::map<SymTabId, pair<ST_TAB*, PU_Info*> > {
00076 
00077 protected:
00078   typedef std::map<SymTabId, pair<ST_TAB*, PU_Info*> > BaseMap;
00079   
00080 public:
00081   SymTabIdToSymTabMap() { }
00082   SymTabIdToSymTabMap(PU_Info* pu_forest) { Create(pu_forest); }
00083   virtual ~SymTabIdToSymTabMap() { }
00084   
00085   pair<ST_TAB*, PU_Info*>
00086   Find(SymTabId id, bool mustFind = false) const
00087   {
00088     pair<ST_TAB*, PU_Info*> result(NULL, NULL);
00089     
00090     const_iterator it = this->find(id);
00091     if (it != this->end()) {
00092       result = (*it).second;
00093     } 
00094     else if (mustFind) {
00095       FORTTK_DIE("SymTabIdToSymTabMap: Could not find entry for key '" 
00096                  << id << "'");
00097     }
00098     
00099     return result;
00100   }
00101 
00102   void
00103   Insert(SymTabId id, ST_TAB* stab, PU_Info* pu)
00104   {
00105     this->insert(make_pair(id, make_pair(stab, pu))); // do not add duplicates!
00106   }
00107 
00108   void Create(PU_Info* pu_forest);
00109 };
00110 
00111 
00112 //***************************************************************************
00113 // ST <-> SymId maps (intra-procedural)
00114 //***************************************************************************
00115 
00116 // Note: Instead of creating ST* <-> to SymId maps, we currently use a
00117 // ST's index -- ST_index(ST*) -- in the symbol table as a persistent
00118 // id.  While this is correct, it would be nice to add an interface
00119 // map, even if the map is really only the identity function without
00120 // any state.
00121 
00122 
00123 //***************************************************************************
00124 // PU <-> PUId maps (global/interprocedural)
00125 //***************************************************************************
00126 
00127 class PUToPUIdMap 
00128   : public BaseMap<PU_Info*, PUId>
00129 {
00130 public:  
00131   PUToPUIdMap() { }
00132   PUToPUIdMap(PU_Info* pu_forest) { Create(pu_forest); }
00133   virtual ~PUToPUIdMap() { }
00134 
00135   void Create(PU_Info* pu_forest);
00136 };
00137 
00138 
00139 class PUIdToPUMap
00140   : public BaseMap<PUId, PU_Info*>
00141 {
00142 public:
00143   PUIdToPUMap() { }
00144   PUIdToPUMap(PU_Info* pu_forest) { Create(pu_forest); }
00145   virtual ~PUIdToPUMap() { }
00146   
00147   void Create(PU_Info* pu_forest);
00148 };
00149 
00150 
00151 //***************************************************************************
00152 // WNId <-> WN map
00153 //***************************************************************************
00154 
00155 class WNToWNIdMap 
00156   : public BaseMap<WN*, WNId>
00157 {
00158 public:  
00159   WNToWNIdMap() { }
00160   WNToWNIdMap(WN* wn) { Create(wn); }
00161   virtual ~WNToWNIdMap() { }
00162 
00163   void Create(WN* wn);
00164 };
00165 
00166 class WNIdToWNMap 
00167   : public BaseMap<WNId, WN*>
00168 {
00169 public:
00170   WNIdToWNMap() { }
00171   WNIdToWNMap(WN* wn) { Create(wn); }
00172   virtual ~WNIdToWNMap() { }
00173   
00174   void Create(WN* wn);
00175 };
00176 
00177 
00178 // ---------------------------------------------------------
00179 // 
00180 // ---------------------------------------------------------
00181 
00182 // Note: Assumes ownership of the tables it creates
00183 class WNToWNIdTabMap 
00184   : public BaseMap<PU_Info*, WNToWNIdMap*> {
00185   
00186 public:
00187   WNToWNIdTabMap() { }
00188   WNToWNIdTabMap(PU_Info* pu_forest) { Create(pu_forest); }
00189   virtual ~WNToWNIdTabMap();
00190   
00191   void Create(PU_Info* pu_forest);
00192   void Destroy();
00193 };
00194 
00195 class WNIdToWNTabMap 
00196   : public BaseMap<PU_Info*, WNIdToWNMap*> {
00197   
00198 public:
00199   WNIdToWNTabMap() { }
00200   WNIdToWNTabMap(PU_Info* pu_forest) { Create(pu_forest); }
00201   virtual ~WNIdToWNTabMap();
00202   
00203   void Create(PU_Info* pu_forest);
00204   void Destroy();
00205 };
00206 
00207 
00208 //***************************************************************************
00209 // Optional routines for map creation
00210 //***************************************************************************
00211 
00212 // CreateSymTabIdMaps: Given a PU forest, initialize the non-NULL
00213 // persistent ID <-> ST_TAB* maps.  STTABIds are guaranteed to be
00214 // unique within the PU forest 'pu_forest'. (The global symbol table
00215 // is included.)
00216 void
00217 CreateSymTabIdMaps(PU_Info* pu_forest, 
00218                    SymTabToSymTabIdMap* x, SymTabIdToSymTabMap* y);
00219 
00220 // CreatePUIdMaps: Given a PU forest, initialize the non-NULL
00221 // persistent ID <-> PU_Info* maps.  PUIds are guaranteed to be unique
00222 // within the PU forest 'pu_forest'.
00223 void
00224 CreatePUIdMaps(PU_Info* pu_forest, PUToPUIdMap* x, PUIdToPUMap* y);
00225 
00226 // CreateWhirlIDMaps: Given a WN*, initialize the non-NULL persistent
00227 // ID <-> WN* maps.  WNIds are guaranteed to be unique within the
00228 // WHIRL tree rooted at 'wn'. (Note that 'wn' is usually the result of
00229 // PU_Info_tree_ptr(PU_Info*).)
00230 void
00231 CreateWhirlIdMaps(WN* wn, WNToWNIdMap* x, WNIdToWNMap* y);
00232 
00233 
00234 }
00235 
00236 #endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines