OpenADFortTk (basic)
src/lib/support/Open64IRInterface/WhirlGlobalStateUtils.cpp
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 //************************* System Include Files ****************************
00003 
00004 #include <cassert>
00005 
00006 //*************************** User Include Files ****************************
00007 
00008 #include "WhirlGlobalStateUtils.h"
00009 
00010 //************************** Forward Declarations ***************************
00011 
00012 //***************************************************************************
00013 
00014 PUToScopedSymTabMap PUToScopeTabMap;
00015 
00016 //***************************************************************************
00017 
00018 // PU_SetGlobalState: 
00019 void
00020 PU_SetGlobalState(PU_Info* pu)
00021 {
00022   // 'pu' must be in memory (change some global pointers around)
00023   assert(PU_Info_state(pu, WT_TREE) == Subsect_InMem); // Subsect_Written?
00024   Current_Map_Tab = PU_Info_maptab(pu);
00025   Current_pu = &PU_Info_pu(pu);
00026   Current_PU_Info = pu;
00027   CURRENT_SYMTAB = PU_lexical_level(*Current_pu); // == Current_scope
00028   Scope_tab = PUToScopeTabMap.Find(pu);
00029   assert(Scope_tab);
00030 }
00031 
00032 
00033 // PU_AllocBEGlobalSymtab:
00034 void
00035 PU_AllocBEGlobalSymtab()
00036 {
00037   // Initialize back end symbol table (e.g. w2cf relies on this)
00038   BE_symtab_initialize_be_scopes();
00039   BE_symtab_alloc_scope_level(GLOBAL_SYMTAB);
00040   for (SYMTAB_IDX lvl = 0; lvl <= GLOBAL_SYMTAB; ++lvl) {
00041     if (Scope_tab[lvl].st_tab) {
00042       Scope_tab[lvl].st_tab->Register(*Be_scope_tab[lvl].be_st_tab);
00043     } else { // only level 0 should not have st_tab
00044       Is_True(lvl == 0, ("Nonexistent st_tab for level %d", lvl));
00045     }
00046   }
00047 }
00048 
00049 
00050 // PU_FreeBEGlobalSymtab: 
00051 void
00052 PU_FreeBEGlobalSymtab()
00053 {
00054   // Free back end symtabs (from 0 <= lvl <= GLOBAL_SYMTAB)
00055   for (SYMTAB_IDX lvl = GLOBAL_SYMTAB; lvl <= GLOBAL_SYMTAB; --lvl) {
00056     if (Scope_tab[lvl].st_tab) {
00057       Scope_tab[lvl].st_tab->Un_register(*Be_scope_tab[lvl].be_st_tab);
00058       Be_scope_tab[lvl].be_st_tab->Clear();
00059     } else { // only level 0 should not have st_tab
00060       Is_True(lvl == 0, ("Nonexistent st_tab for level %d", lvl));
00061     }
00062   }
00063   BE_symtab_free_be_scopes();
00064 }
00065 
00066 
00067 // PU_AllocBELocalSymtab:
00068 void
00069 PU_AllocBELocalSymtab_helper(PU_Info* pu);
00070 
00071 void
00072 PU_AllocBELocalSymtab(PU_Info* pu)
00073 {
00074   PU_SetGlobalState(pu); // just in case
00075   
00076   PU_AllocBELocalSymtab_helper(pu);
00077   for (PU_Info* child = PU_Info_child(pu); child != NULL;
00078        child = PU_Info_next(child)) {
00079     PU_AllocBELocalSymtab_helper(child);
00080   }
00081 }
00082 
00083 void
00084 PU_AllocBELocalSymtab_helper(PU_Info* pu)
00085 {
00086   // Initialize local back end symtab.  Assume symtab is already
00087   // prepared for pu.
00088   SYMTAB_IDX lvl = PU_lexical_level(PU_Info_pu(pu));
00089   BE_symtab_alloc_scope_level(lvl);
00090   Scope_tab[lvl].st_tab->Register(*Be_scope_tab[lvl].be_st_tab);
00091 }
00092 
00093 
00094 // PU_FreeBELocalSymtab:
00095 void
00096 PU_FreeBELocalSymtab_helper(PU_Info* pu);
00097 
00098 void
00099 PU_FreeBELocalSymtab(PU_Info* pu)
00100 {
00101   PU_SetGlobalState(pu); // just in case
00102 
00103   PU_FreeBELocalSymtab_helper(pu);
00104   for (PU_Info* child = PU_Info_child(pu); child != NULL;
00105        child = PU_Info_next(child)) {
00106     PU_FreeBELocalSymtab_helper(child);
00107   }
00108 }
00109 
00110 void
00111 PU_FreeBELocalSymtab_helper(PU_Info* pu)
00112 {
00113   // Free local back end symtab.  Assume symtab is already prepared
00114   // for pu.
00115   SYMTAB_IDX lvl = PU_lexical_level(PU_Info_pu(pu));
00116   Scope_tab[lvl].st_tab->Un_register(*Be_scope_tab[lvl].be_st_tab);
00117   Be_scope_tab[lvl].be_st_tab->Clear();
00118 }
00119 
00120 
00121 //***************************************************************************
00122 
00123 // PU_SaveGlobalState: <see header comments>
00124 //
00125 // The only state we need to save is the lexical scope table (SCOPE*).
00126 // We do this by creating a copy of the lexical table for each PU.
00127 // Each lexical table contains several pointers to the various tables
00128 // the comprise the symbols tables.  Because the pointers are simply
00129 // copied, THE ACTUAL ST TABLES MAY BE SHARED; THEY ARE NOT
00130 // DUPLICATED.  The global symbol table will be shared accros all PUs;
00131 // nested PUs will share symbol tables from the outer scopes.
00132 //
00133 // N.B. This function will likely create a small memory leak.  Because
00134 // each PU will be given its own lexical table, there will be no owner
00135 // for the lexical table created in Initialize_Symbol_Tables().
00136 //
00137 // See the following:
00138 //   ipa/main/analyze/ipa_cg.cxx :
00139 //     read_pu_including_parents(..)
00140 //     IPA_NODE::Scope(..)
00141 //     IPA_NODE::Set_Scope(..)
00142 //   be/com/clone.cxx
00143 //     IPO_SYMTAB::New_Symtab(..)
00144 void
00145 WhirlGlobalStateUtils_hidden::PU_SaveGlobalState(PU_Info* pu)
00146 {
00147   // We assume that the global state has already been properly prepared.
00148   
00149   // Create a scope table large enough for this lexical level and
00150   // insert it into the map. Note: we intentionally do not set
00151   // 'Scope_tab' to this copy in order to stave off the likely memory
00152   // leak as long as possible.
00153   SYMTAB_IDX lvl = PU_lexical_level(*Current_pu);
00154   INT sz = (lvl + 1) * sizeof(SCOPE);
00155   SCOPE* new_scope_tab = (SCOPE*)MEM_POOL_Alloc(Malloc_Mem_Pool, sz);
00156   memcpy(new_scope_tab, Scope_tab, sz);
00157   
00158   PUToScopeTabMap.Insert(pu, new_scope_tab);
00159 }
00160 
00161 //***************************************************************************
00162 
00163 PUToScopedSymTabMap::PUToScopedSymTabMap() 
00164 { 
00165 }
00166 
00167 PUToScopedSymTabMap::~PUToScopedSymTabMap() 
00168 {
00169   using namespace WhirlGlobalStateUtils_hidden;
00170   
00171   for (PUToScopedSymTabBaseMap::iterator it = this->begin();
00172        it != this->end(); ++it) {
00173     SCOPE* scopetab = (*it).second;
00174     MEM_POOL_FREE(Malloc_Mem_Pool, scopetab);
00175   }
00176 }
00177 
00178 //***************************************************************************
00179 
00180 #if 0
00181 
00182 // The following were the former versions of these functions.  The
00183 // user called Restore before using a pu and Save after using it.  If
00184 // this was not strictly followed very bad things could happen because
00185 // only the local symbol table was saved, using a gross hack.  I
00186 // couldn't get it to handle nested PUs.  
00187 
00188 // When using multiple PUs at a time, this should be called to
00189 // prepare the global symbol table pointers the current PU.
00190 void 
00191 RestoreOpen64PUGlobalVars(PU_Info* pu);
00192 
00193 // When using multiple PUs at a time, this should be called to save
00194 // the global symbol table pointer for later use.
00195 void
00196 SaveOpen64PUGlobalVars(PU_Info* pu);
00197 
00198 
00199 
00200 // cf. wn_mp.cxx 1278 ; dra_clone.cxx:974
00201 // PU_Info_symtab_ptr(pu)
00202 
00203 // (PU_lexical_level (&St_Table[PU_Info_proc_sym (pu)]))
00204 // Scope_tab[CURRENT_SYMTAB].st = WN_get_proc_sym(pu);
00205 
00206 // Callgraph: ipa_cg.cxx (1053)
00207 
00208 void 
00209 RestoreOpen64PUGlobalVars(PU_Info* pu)
00210 {
00211   ST_IDX st = PU_Info_proc_sym(pu);
00212   DBGMSG(2, "** Restoring Open64 global vars for '%s' **", ST_name(st));
00213   
00214   // The PU is in memory (change some global pointers around)
00215   assert(PU_Info_state(pu, WT_TREE) == Subsect_InMem);
00216   Current_Map_Tab = PU_Info_maptab(pu);
00217   Current_pu = &PU_Info_pu(pu);
00218   CURRENT_SYMTAB = PU_lexical_level(*Current_pu);
00219   
00220   Restore_Local_Symtab(pu);
00221   // Can we make this restore itself and all its parents?
00222 }
00223 
00224 void
00225 SaveOpen64PUGlobalVars(PU_Info* pu)
00226 {
00227   ST_IDX st = PU_Info_proc_sym(pu);
00228   DBGMSG(2, "** Saving Open64 global vars for '%s' **", ST_name(st));
00229   
00230   Set_PU_Info_symtab_ptr(pu, NULL);
00231   Save_Local_Symtab(CURRENT_SYMTAB, pu);
00232   // Can we make this save itself and all its parents?
00233 }
00234 
00235 #endif
00236 
00237 //***************************************************************************
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines