OpenADFortTk (basic)
src/lib/support/Open64IRInterface/WhirlIO.cpp
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 #include "Open64BasicTypes.h"
00010 
00011 #include "ir_bread.h"      // Read_Global_Info(), etc.
00012 #include "ir_bwrite.h"     // Write_Global_Info(), etc.
00013 #include "ir_reader.h"     // fdump_tree
00014 #include "tracing.h"       // trace routines
00015 
00016 #include "be_util.h"       // Advance_/Reset_Current_PU_Count(), etc
00017 
00018 #include "stblock.h"       // Create_Slink_Symbol()
00019 
00020 #include "WhirlIO.h"
00021 #include "WhirlGlobalStateUtils.h"
00022 #include "Open64IRInterface.hpp"
00023 
00024 #include "IFDiagnostics.h"
00025 
00026 #include "CleanUpWhirl.h"
00027 
00028 static void 
00029 ReadPU(PU_Info* pu);
00030 
00031 
00032 // ReadIR:
00033 PU_Info*
00034 ReadIR(const char* irfilename, bool noCleanUpWhirl)
00035 {
00036   Diag_Set_Phase("WHIRL IO: Load IR");
00037 
00038   MEM_POOL_Push(&MEM_src_pool);
00039   MEM_POOL_Push(&MEM_src_nz_pool);
00040   Set_Error_Source(Src_File_Name);
00041 
00042   MEM_POOL_Push(MEM_pu_nz_pool_ptr);
00043   MEM_POOL_Push(MEM_pu_pool_ptr);
00044 
00045   // -------------------------------------------------------
00046   // 1. Read pu tree info and global symbol table
00047   // -------------------------------------------------------
00048   
00049   // Open file, read PU info and setup symbol tables
00050   Open_Input_Info(irfilename);
00051   Read_Global_Data = (char*)"bogus-value-as-argument-to-Initialize_Symbol_Tables";
00052   Initialize_Symbol_Tables(FALSE /*reserve_index_zero*/);
00053   New_Scope(GLOBAL_SYMTAB, Malloc_Mem_Pool, FALSE);
00054   PU_Info *pu_forest = Read_Global_Info(NULL);
00055   Initialize_Special_Global_Symbols();
00056   
00057   // -------------------------------------------------------
00058   // 2. Read PUs and local symbol tables
00059   // -------------------------------------------------------
00060   // Perform our own iteration to ensure correctness and safety
00061   for (PU_Info* pu = pu_forest; pu != NULL; pu = PU_Info_next(pu)) {
00062     ReadPU(pu);
00063   }
00064   
00065   if (!noCleanUpWhirl)
00066     CleanUpWhirl::forPUInfoForest(pu_forest);
00067 
00068   // Free_Input_Info // should we do this?
00069 
00070   return pu_forest;
00071 }
00072 
00073 
00074 void
00075 ReadPU(PU_Info* pu)
00076 {
00077   Current_PU_Info = pu;
00078   
00079   // Read program unit (Reads the PUs (WHIRL trees), symbol tables;
00080   // sets CURRENT_SYMTAB and Scope_tab[]).
00081   Read_Local_Info(MEM_pu_nz_pool_ptr, pu);
00082   WN *wn_pu = PU_Info_tree_ptr(pu); // made possible by Read_Local_Info()
00083   
00084   Advance_Current_PU_Count();
00085   
00086   // Now recursively process the child PU's.
00087   for (PU_Info* child = PU_Info_child(pu); child != NULL;
00088        child = PU_Info_next(child)) {
00089     ReadPU(child);
00090   }
00091 
00092   WhirlGlobalStateUtils_hidden::PU_SaveGlobalState(pu);
00093 }
00094 
00095 
00096 //***************************************************************************
00097 // WriteIR
00098 //***************************************************************************
00099 
00100 static void
00101 WritePU(PU_Info* pu);
00102 
00103 static void
00104 SetPUInfoStateIR(PU_Info* pu_forest, Subsect_State state);
00105 
00106 static void
00107 SetPUInfoStatePU(PU_Info* pu, Subsect_State state);
00108 
00109 static void
00110 SetPUInfoState(PU_Info* pu, Subsect_State state);
00111 
00112 
00113 // WriteIR
00114 void 
00115 WriteIR(const char* irfilename, PU_Info* pu_forest)
00116 {
00117   Diag_Set_Phase("WHIRL IO: Write IR");
00118 
00119   Open_Output_Info(irfilename);
00120 
00121   // -------------------------------------------------------
00122   // 1. Write PUs and local symbol tables
00123   // -------------------------------------------------------
00124   // Perform our own iteration to ensure correctness and safety
00125   for (PU_Info* pu = pu_forest; pu != NULL; pu = PU_Info_next(pu)) {
00126     WritePU(pu);
00127   }
00128 
00129   // -------------------------------------------------------
00130   // 2. Write Global info
00131   // -------------------------------------------------------
00132   Write_Global_Info(pu_forest); // expects PU state to be Subsect_Written
00133   Close_Output_Info();
00134 }
00135 
00136 
00137 static void
00138 WritePU(PU_Info* pu)
00139 {
00140   PU_SetGlobalState(pu);
00141   
00142   Write_PU_Info(pu); // sets PU state to Subsect_Written
00143   
00144   // Recur
00145   for (PU_Info* child = PU_Info_child(pu); child != NULL;
00146        child = PU_Info_next(child)) {
00147     WritePU(child);
00148   }
00149 }
00150 
00151 
00152 // SetPUInfoStateIR: For each PU in IR 'pu_forest', sets each
00153 // subsection to have the state 'state' if the subsection is present
00154 // (i.e. not Subsect_Missing).
00155 static void
00156 SetPUInfoStateIR(PU_Info* pu_forest, Subsect_State state) 
00157 {
00158   for (PU_Info* pu = pu_forest; pu != NULL; pu = PU_Info_next(pu)) {
00159     SetPUInfoStatePU(pu, state);
00160   }
00161 }
00162 
00163 
00164 // SetPUInfoStatePU: Sets the state for the PU tree: the current PU
00165 // and all children.
00166 static void
00167 SetPUInfoStatePU(PU_Info* pu, Subsect_State state) 
00168 {
00169   // Set the state
00170   SetPUInfoState(pu, state);
00171   
00172   // Recur
00173   for (PU_Info* child = PU_Info_child(pu); child != NULL;
00174        child = PU_Info_next(child)) {
00175     SetPUInfoStatePU(child, state);
00176   }
00177 }
00178 
00179 
00180 // SetPUInfoState: Set the state for the current PU
00181 static void
00182 SetPUInfoState(PU_Info* pu, Subsect_State state) 
00183 {
00184   for (int subsec = 0; subsec < WT_SUBSECTIONS; ++subsec) {
00185     if (PU_Info_state(pu, subsec) != Subsect_Missing) {
00186       Set_PU_Info_state(pu, subsec, state);
00187     }
00188   }
00189 }
00190 
00191 
00192 //***************************************************************************
00193 // FreeIR
00194 //***************************************************************************
00195 
00196 static void 
00197 FreePU(PU_Info* pu);
00198 
00199 
00200 // FreeIR: 
00201 void
00202 FreeIR(PU_Info* pu_forest)
00203 {
00204   Diag_Set_Phase("WHIRL IO: Free IR");
00205   
00206   // -------------------------------------------------------
00207   // 1. Free PUs and local symbol tables
00208   // -------------------------------------------------------
00209   // Perform our own iteration to ensure correctness and safety
00210   for (PU_Info* pu = pu_forest; pu != NULL; pu = PU_Info_next(pu)) {
00211     FreePU(pu);
00212   }
00213   
00214   // -------------------------------------------------------
00215   // 2. Free pu tree info and global symbol tables
00216   // -------------------------------------------------------
00217   Verify_SYMTAB(GLOBAL_SYMTAB);
00218   
00219   MEM_POOL_Pop(MEM_pu_nz_pool_ptr);
00220   MEM_POOL_Pop(MEM_pu_pool_ptr);
00221   
00222   MEM_POOL_Pop(&MEM_src_pool);
00223   MEM_POOL_Pop(&MEM_src_nz_pool);
00224   
00225 #ifdef Is_True_On
00226   if (Get_Trace (TKIND_ALLOC, TP_MISC)) {
00227     fprintf (TFile, "\n%s\tMemory allocation info\n", DBar);
00228     MEM_Trace();
00229   }
00230 #endif
00231 }
00232 
00233 
00234 static void
00235 FreePU(PU_Info* pu)
00236 {
00237   PU_SetGlobalState(pu);
00238   
00239   Free_Local_Info(pu); // deletes all maps
00240   
00241   // Now recursively process the child PU's.
00242   for (PU_Info *child = PU_Info_child(pu); child != NULL;
00243        child = PU_Info_next(child)) {
00244     FreePU(child);
00245   }
00246 }
00247 
00248 
00249 //***************************************************************************
00250 // PrepareIR
00251 //***************************************************************************
00252 
00253 void 
00254 PrepareIR(PU_Info* pu_forest)
00255 {
00256   Diag_Set_Phase("WHIRL IO: Prepare IR");
00257 
00258   Open64IRProcIterator procIt(pu_forest);
00259   for ( ; procIt.isValid(); ++procIt) { 
00260     PU_Info* pu = (PU_Info*)procIt.current().hval();
00261     WN* wn_pu = PU_Info_tree_ptr(pu);
00262     Create_Slink_Symbol(); // FIXME: do we need?
00263   }
00264 }
00265 
00266 
00267 //***************************************************************************
00268 // DumpIR
00269 //***************************************************************************
00270 
00271 void 
00272 DumpIR(PU_Info* pu_forest)
00273 {
00274   bool dumpST = false;
00275   IR_set_dump_order(TRUE); // Preorder dump
00276 
00277   if (dumpST) {
00278     // Global 
00279     Print_global_symtab(stdout);
00280   }
00281 
00282   Open64IRProcIterator procIt(pu_forest);
00283   for ( ; procIt.isValid(); ++procIt) { 
00284     PU_Info* pu = (PU_Info*)procIt.current().hval();  
00285     WN* wn_pu = PU_Info_tree_ptr(pu);
00286     
00287     //IR_put_func(wn_pu, NULL);
00288     fdump_tree(stderr, wn_pu);
00289     
00290     if (dumpST) {
00291       Print_local_symtab(stdout, Scope_tab[CURRENT_SYMTAB]);
00292     }
00293   }
00294 }
00295 
00296 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines