OpenADFortTk (basic)
src/sexp2whirl/sexp2whirl.cxx
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /m_home/m_utkej/Argonne/cvs2svn/cvs/OpenADFortTk/src/sexp2whirl/sexp2whirl.cxx,v 1.6 2007-10-08 18:28:33 utke Exp $
00003 #include <sexp.h>
00004 
00005 #include "Open64IRInterface/Open64BasicTypes.h"
00006 #include "Open64IRInterface/WhirlGlobalStateUtils.h"
00007 
00008 #include "SexpTags.h"
00009 #include "sexputil.h"
00010 
00011 #include "sexp2whirl.h"
00012 using sexp2whirl::ErrIR;
00013 #include "sexp2wn.h"
00014 #include "sexp2symtab.h"
00015 
00016 static PU_Info*
00017 xlate_IR(sexp_t* ir_sx, int flags);
00018 
00019 static PU_Info*
00020 xlate_PUForest(sexp_t* pu_forest_sx, int flags);
00021 
00022 static PU_Info*
00023 xlate_PUTree(sexp_t* pu_tree_sx, int flags);
00024 
00025 static PU_Info* 
00026 xlate_PU(sexp_t* pu_sx, int flags);
00027 
00028 static WN* 
00029 xlate_WN(sexp_t* ast_sx, int flags);
00030 
00031 //***************************************************************************
00032 // Implementation of interface routines
00033 //***************************************************************************
00034 
00035 PU_Info*
00036 sexp2whirl::TranslateIR(sexp_t* ir, int flags)
00037 {
00038   return xlate_IR(ir, flags);
00039 }
00040 
00041 
00042 const char* 
00043 sexp2whirl::ErrIR(sexp_t* ir, int flags)
00044 {
00045   const int sz = 1 << 20;
00046   char* buf = new char[sz];
00047   
00048   if (ir) {
00049     int rval = print_sexp(buf, sz, ir);
00050   } 
00051   else {
00052     strcpy(buf, "null");
00053   }
00054   return buf; // must be deleted, but ignore for generating errors
00055 }
00056 
00057 
00058 void 
00059 sexp2whirl::DumpIR(sexp_t* ir, int flags)
00060 {
00061   const int sz = 1 << 20;
00062   char* buf = new char[sz];
00063   
00064   int rval = print_sexp(buf, sz, ir);
00065   std::cout << buf << std::endl;
00066   delete[] buf;
00067 }
00068 
00069 
00070 //***************************************************************************
00071 // Helper routines
00072 //***************************************************************************
00073 
00074 // xlate_IR: Translate the whole WHIRL_IR, return a PU_FOREST and set
00075 // GBL_SYMTAB state.
00076 PU_Info*
00077 xlate_IR(sexp_t* ir_sx, int flags) 
00078 {
00079   using namespace sexp;
00080   
00081   // Sanity check
00082   FORTTK_ASSERT(ir_sx && is_list(ir_sx), 
00083                 fortTkSupport::Diagnostics::UnexpectedInput << ErrIR(ir_sx));
00084 
00085   sexp_t* tag_sx = get_elem0(ir_sx);
00086   const char* tagstr = get_value(tag_sx);
00087   FORTTK_ASSERT(tag_sx && strcmp(tagstr, SexpTags::WHIRL) == 0,
00088                 fortTkSupport::Diagnostics::UnexpectedInput << ErrIR(tag_sx));
00089 
00090   // Translate GBL_SYMTAB and PU_FOREST
00091   sexp_t* gbl_symtab_sx = get_elem1(ir_sx);
00092   sexp_t* pu_forest_sx  = get_elem2(ir_sx);
00093   FORTTK_ASSERT(gbl_symtab_sx, 
00094                 fortTkSupport::Diagnostics::UnexpectedInput << ErrIR(gbl_symtab_sx));
00095   FORTTK_ASSERT(pu_forest_sx, 
00096                 fortTkSupport::Diagnostics::UnexpectedInput << ErrIR(pu_forest_sx));
00097   
00098   sexp2whirl::TranslateGlobalSymbolTables(gbl_symtab_sx, flags);
00099   PU_Info* pu_forest = xlate_PUForest(pu_forest_sx, flags);
00100   
00101   return pu_forest;
00102 }
00103 
00104 
00105 // xlate_PUForest: Translate and return a PU_FOREST, *assuming* that
00106 // GBL_SYMTAB has already been translated.
00107 PU_Info*
00108 xlate_PUForest(sexp_t* pu_forest_sx, int flags)
00109 {
00110   using namespace sexp;
00111 
00112   // Sanity check
00113   FORTTK_ASSERT(pu_forest_sx && is_list(pu_forest_sx), 
00114                 fortTkSupport::Diagnostics::UnexpectedInput);  
00115   
00116   sexp_t* tag_sx = get_elem0(pu_forest_sx);
00117   const char* tagstr = get_value(tag_sx);
00118   FORTTK_ASSERT(tag_sx && strcmp(tagstr, SexpTags::PU_FOREST) == 0,
00119                 fortTkSupport::Diagnostics::UnexpectedInput);
00120 
00121   MEM_POOL_Push(MEM_pu_nz_pool_ptr);
00122   MEM_POOL_Push(MEM_pu_pool_ptr);
00123   
00124   // Translate the list of PU_TREEs
00125   PU_Info* pu_forest = NULL;
00126   sexp_t* cur_pu_tree_sx = get_elem1(pu_forest_sx);
00127   if (cur_pu_tree_sx) {
00128     // Translate head of the PU_TREE list
00129     pu_forest = xlate_PUTree(cur_pu_tree_sx, flags);
00130     PU_Info* cur_pu_tree = pu_forest;
00131     
00132     // Translate rest of the PU_TREE list
00133     for (cur_pu_tree_sx = get_next(cur_pu_tree_sx) ; cur_pu_tree_sx; 
00134          cur_pu_tree_sx = get_next(cur_pu_tree_sx)) {
00135       PU_Info* pu_tree = xlate_PUTree(cur_pu_tree_sx, flags);
00136 
00137       PU_Info_next(cur_pu_tree) = pu_tree;
00138       cur_pu_tree = pu_tree;
00139     }
00140   }
00141   
00142   return pu_forest;
00143 }
00144 
00145 
00146 // xlate_PUTree: Translate and return a PU_TREE (a PU and all
00147 // children), *assuming* that GBL_SYMTAB has already been translated.
00148 PU_Info*
00149 xlate_PUTree(sexp_t* pu_tree_sx, int flags)
00150 {
00151   using namespace sexp;
00152   
00153   if (!pu_tree_sx) { return NULL; }
00154 
00155   // Sanity check
00156   FORTTK_ASSERT(is_list(pu_tree_sx), fortTkSupport::Diagnostics::UnexpectedInput);  
00157   
00158   sexp_t* tag_sx = get_elem0(pu_tree_sx);
00159   const char* tagstr = get_value(tag_sx);
00160   FORTTK_ASSERT(tag_sx && strcmp(tagstr, SexpTags::PU_TREE) == 0,
00161                 fortTkSupport::Diagnostics::UnexpectedInput);
00162   
00163   // Translate PU
00164   sexp_t*  pu_sx = get_elem1(pu_tree_sx);
00165   PU_Info* pu = xlate_PU(pu_sx, flags);
00166   
00167   // Recursively translate PUs children
00168   PU_Info* cur_child = NULL;
00169   for (sexp_t* child_sx = get_elem2(pu_tree_sx); child_sx; 
00170        child_sx = get_next(child_sx)) {
00171     PU_Info* child = xlate_PU(child_sx, flags);
00172     if (cur_child) {
00173       PU_Info_next(cur_child) = child;
00174     }
00175     else {
00176       PU_Info_child(pu) = child;
00177     }
00178     cur_child = child;
00179   }
00180 
00181   if (cur_child) { // if there was at least one child
00182     Set_PU_Info_flags(pu, PU_HAS_NESTED_PU);
00183   }
00184   return pu;
00185 }
00186 
00187 
00188 // xlate_PU: Translate and return a PU, setting PU_SYMTAB state but
00189 // *assuming* that GBL_SYMTAB has already been translated.
00190 PU_Info*
00191 xlate_PU(sexp_t* pu_sx, int flags)
00192 { 
00193   using namespace sexp;
00194 
00195   // Sanity check
00196   FORTTK_ASSERT(pu_sx && is_list(pu_sx), fortTkSupport::Diagnostics::UnexpectedInput);
00197   
00198   sexp_t* tag_sx = get_elem0(pu_sx);
00199   const char* tagstr = get_value(tag_sx);
00200   FORTTK_ASSERT(tag_sx && strcmp(tagstr, SexpTags::PU) == 0,
00201                 fortTkSupport::Diagnostics::UnexpectedInput);
00202 
00203   // Translate PU_SYMTAB and WHIRL_AST
00204   sexp_t* pu_sym_sx    = get_elem1(pu_sx);
00205   sexp_t* pu_symtab_sx = get_elem2(pu_sx);
00206   sexp_t* ast_sx       = get_elem3(pu_sx);
00207 
00208   ST_IDX st_idx = sexp2whirl::GetWhirlSymRef(pu_sym_sx);
00209   CURRENT_SYMTAB = PU_lexical_level(&St_Table[st_idx]);
00210   
00211   sexp2whirl::TranslateLocalSymbolTables(pu_symtab_sx, CURRENT_SYMTAB, flags);
00212   
00213   WN* ast = xlate_WN(ast_sx, flags);
00214 
00215   // Create the PU_Info
00216   PU_Info* pu = TYPE_MEM_POOL_ALLOC(PU_Info, MEM_pu_pool_ptr);
00217   PU_Info_init(pu);
00218   // N.B. 'next', 'child' and PU_HAS_NESTED_PU are set by caller routines
00219   PU_Info_proc_sym(pu) = st_idx;
00220   Set_PU_Info_tree_ptr(pu, ast);
00221   Set_PU_Info_state(pu, WT_TREE,     Subsect_InMem);
00222   Set_PU_Info_state(pu, WT_SYMTAB,   Subsect_InMem);
00223   Set_PU_Info_state(pu, WT_PROC_SYM, Subsect_InMem);
00224   Set_PU_Info_pu_dst(pu, DST_INVALID_IDX);
00225   Set_PU_Info_cu_dst(pu, DST_INVALID_IDX);
00226   PU_Info_maptab(pu) = WN_MAP_TAB_Create(MEM_pu_pool_ptr);
00227 
00228   // Set and save global state (cf. PU_SetGlobalState)
00229   //Advance_Current_PU_Count();
00230   Current_Map_Tab = PU_Info_maptab(pu);
00231   Current_pu = &PU_Info_pu(pu);
00232   Current_PU_Info = pu;
00233   WhirlGlobalStateUtils_hidden::PU_SaveGlobalState(pu);
00234 
00235   return pu;
00236 }
00237 
00238 
00239 // xlate_PUTree: Translate and return a WHIRL_AST, *assuming* that
00240 // GBL_SYMTAB and the relevant PU_SYMTABs have been translated.
00241 WN*
00242 xlate_WN(sexp_t* ast_sx, int flags)
00243 {
00244   sexp2whirl::TranslateWN(ast_sx);
00245 }
00246 
00247 //***************************************************************************
00248 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines