OpenADFortTk (basic)
src/xaif2whirl/AdjustInterfaces.cxx
Go to the documentation of this file.
00001 #include "AdjustInterfaces.h"
00002 #include "Diagnostics.h"
00003 #include "Open64IRInterface/WhirlGlobalStateUtils.h"
00004 
00005 void AdjustInterfaces::doIt() { 
00006   if (!myPUInfoForest_p) { return; }
00007   // Loop through all the PU_Infos 
00008   PU_Info* aPUInfoTree_p;
00009   for (aPUInfoTree_p=myPUInfoForest_p; aPUInfoTree_p!= NULL; aPUInfoTree_p=PU_Info_next(aPUInfoTree_p)) {
00010     forPUInfoTree(aPUInfoTree_p);
00011   }
00012 } 
00013 
00014 void AdjustInterfaces::forPUInfoTree(PU_Info* aPUInfoTree_p) { 
00015   if (!aPUInfoTree_p) { return; }
00016   forPUInfo(aPUInfoTree_p);
00017   for (PU_Info *aPUInfosubtree_p = PU_Info_child(aPUInfoTree_p); 
00018        aPUInfosubtree_p != NULL; 
00019        aPUInfosubtree_p = PU_Info_next(aPUInfosubtree_p)) {
00020     forPUInfoTree(aPUInfosubtree_p);
00021   }
00022 }
00023 
00024 TY_IDX copyTypeAdjust(TY_IDX dummyTypeIdx,
00025                       TY_IDX properPUTypeIdx) {
00026   if (dummyTypeIdx==properPUTypeIdx) { 
00027     FORTTK_MSG(2, "copyType: identical type index >" 
00028                << TY_IDX_index(properPUTypeIdx)
00029                << "<");
00030   }
00031   FORTTK_ASSERT(TY_kind(dummyTypeIdx) != KIND_SCALAR 
00032                 && 
00033                 TY_kind(properPUTypeIdx) != KIND_SCALAR,
00034                 "copyType: scalar type");
00035   TY_IDX copiedChildTypeIdx, ty_idx;
00036   TY& ty = New_TY(ty_idx); // sets 'ty_idx'
00037   TY_Init(ty, 
00038           TY_size(dummyTypeIdx), 
00039           TY_kind(dummyTypeIdx),
00040           TY_mtype(dummyTypeIdx),
00041           TY_name_idx(dummyTypeIdx));
00042   if (TY_kind(dummyTypeIdx) == KIND_POINTER ) { 
00043     FORTTK_ASSERT(TY_kind(properPUTypeIdx) == KIND_POINTER,"copyType: not a pointer");
00044     if (TY_kind(TY_pointed(properPUTypeIdx)) != KIND_SCALAR) {
00045       copiedChildTypeIdx = copyTypeAdjust(TY_pointed(dummyTypeIdx),
00046                                           TY_pointed(properPUTypeIdx));
00047       Set_TY_pointed(ty,copiedChildTypeIdx);
00048     }
00049     else { 
00050       FORTTK_ASSERT(TY_kind(TY_pointed(dummyTypeIdx)) == KIND_SCALAR,"copyType: not a scalar");
00051       Set_TY_pointed(ty,TY_pointed(properPUTypeIdx));
00052     }
00053   }
00054   else if (TY_kind(dummyTypeIdx) == KIND_ARRAY ) { 
00055     FORTTK_ASSERT(TY_kind(properPUTypeIdx) == KIND_ARRAY,"copyType: not an array");
00056     if (TY_kind(TY_etype(properPUTypeIdx)) != KIND_SCALAR) {
00057       copiedChildTypeIdx = copyTypeAdjust(TY_etype(dummyTypeIdx),
00058                                           TY_etype(properPUTypeIdx));
00059       Set_TY_etype(ty,copiedChildTypeIdx);
00060     }
00061     else { 
00062       FORTTK_ASSERT(TY_kind(TY_etype(dummyTypeIdx)) == KIND_SCALAR,"copyType: not a scalar");
00063       Set_TY_etype(ty,TY_etype(properPUTypeIdx));
00064     }
00065     Set_TY_arb(ty,TY_arb(dummyTypeIdx));
00066   }
00067   else 
00068     FORTTK_DIE("copyType: no logic to handle type kind: " <<  TY_kind(dummyTypeIdx)); 
00069   return ty_idx;
00070 } 
00071 
00072 void AdjustInterfaces::forPUInfo(PU_Info* aPUInfo_p) { 
00073   PU_SetGlobalState(aPUInfo_p);
00074   WN* thePU_WN_p = PU_Info_tree_ptr(aPUInfo_p);
00075   WN_TREE_CONTAINER<PRE_ORDER> aWNPtree(thePU_WN_p);
00076   WN_TREE_CONTAINER<PRE_ORDER>::iterator aWNPtreeIterator=aWNPtree.begin();
00077   bool skipKids=false;
00078   while (aWNPtreeIterator != aWNPtree.end()) { 
00079     WN* curWN_p = aWNPtreeIterator.Wn();
00080     OPERATOR opr = WN_operator(curWN_p);
00081     if (opr==OPR_INTERFACE) {  // interfaces
00082       skipKids=true;
00083       WN* interfaceFuncWN_p=WN_kid0(curWN_p);
00084       if (interfaceFuncWN_p) {
00085         ST* puName_ST_p=WN_st(interfaceFuncWN_p); 
00086         if (!ST_is_in_module(puName_ST_p)) {  // leave  module procedure interfaces alone
00087           for (INT kidIdx = 0; kidIdx < WN_kid_count(interfaceFuncWN_p); ++kidIdx) {
00088             ST* dummyLocal_ST_p=WN_st(WN_kid(interfaceFuncWN_p, kidIdx));
00089             TY_IDX properPUTypeIndex=findPUSymbolType(puName_ST_p,
00090                                                     dummyLocal_ST_p,
00091                                                     kidIdx);
00092             TY_IDX dummyLocalTypeIndex=ST_type(dummyLocal_ST_p);
00093             if (properPUTypeIndex && properPUTypeIndex!=dummyLocalTypeIndex){
00094               FORTTK_MSG(2,"considering adjustments in interface named " 
00095                        << ST_name(puName_ST_p) << " for variable " 
00096                        << ST_name(dummyLocal_ST_p) 
00097                        << " from "
00098                        << TY_IDX_index(ST_type(dummyLocal_ST_p))
00099                        << " to " 
00100                        << TY_IDX_index(properPUTypeIndex)); 
00101               if (TY_kind(dummyLocalTypeIndex) != KIND_SCALAR) { 
00102                 properPUTypeIndex=copyTypeAdjust(dummyLocalTypeIndex,
00103                                                properPUTypeIndex);
00104               }
00105               else { 
00106                 FORTTK_ASSERT(TY_kind(properPUTypeIndex) == KIND_SCALAR, 
00107                             "AdjustInterfaces::forPUInfo: type kind mismatch for symbol " 
00108                             << ST_name(dummyLocal_ST_p) << " referenced in " << ST_name(puName_ST_p));
00109               }
00110               FORTTK_MSG(2,"in interface named " 
00111                        << ST_name(puName_ST_p) << " adjusting type for variable " 
00112                        << ST_name(dummyLocal_ST_p) 
00113                        << " from "
00114                        << TY_IDX_index(ST_type(dummyLocal_ST_p))
00115                        << " to " 
00116                        << TY_IDX_index(properPUTypeIndex)); 
00117               // do the surgery on the type in the symbol table 
00118               Set_ST_type(dummyLocal_ST_p,properPUTypeIndex);
00119             }
00120           }
00121         }
00122       }
00123     }
00124     // advance the iterator
00125     if (skipKids){
00126       aWNPtreeIterator.WN_TREE_next_skip();
00127       skipKids=false;
00128     }
00129     else
00130       ++aWNPtreeIterator;
00131   }
00132 } 
00133 
00134 TY_IDX AdjustInterfaces::findPUSymbolType(ST* puName_ST_p,
00135                                           ST* dummyLocal_ST_p,
00136                                           INT wnKidIdx) { 
00137   TY_IDX theTypeIndex=0;
00138   PU_Info* thePU=findPU(puName_ST_p);
00139   if (!thePU)
00140     return theTypeIndex; 
00141   const char* dummyLocalName=ST_name(dummyLocal_ST_p);
00142   const char* puName=ST_name(puName_ST_p);
00143   // temporarily reset the global state
00144   PU_Info* currentPUI=Current_PU_Info;
00145   PU_SetGlobalState(thePU);
00146   WN* thePU_WN_p = PU_Info_tree_ptr(thePU);
00147   WN_TREE_CONTAINER<PRE_ORDER> aWNPtree(thePU_WN_p);
00148   WN_TREE_CONTAINER<PRE_ORDER>::iterator aWNPtreeIterator=aWNPtree.begin();
00149   bool skipKids=false;
00150   while (aWNPtreeIterator != aWNPtree.end()) { 
00151     WN* curWN_p = aWNPtreeIterator.Wn();
00152     OPERATOR opr = WN_operator(curWN_p);
00153     if (opr==OPR_FUNC_ENTRY && strcmp(ST_name(WN_st(curWN_p)),puName)==0) {
00154       // found it, now go by position: 
00155       if (WN_kid(curWN_p,wnKidIdx) && WN_has_sym(WN_kid(curWN_p,wnKidIdx))) { 
00156         FORTTK_MSG(2,"for " << puName << " matched " << dummyLocalName << " to " << ST_name(WN_st(WN_kid(curWN_p,wnKidIdx))) << " for position " << wnKidIdx);
00157         theTypeIndex=ST_type(WN_st(WN_kid(curWN_p,wnKidIdx)));
00158         PU_SetGlobalState(currentPUI);
00159         return theTypeIndex; 
00160       }
00161     }
00162     ++aWNPtreeIterator;
00163   }
00164   PU_SetGlobalState(currentPUI);
00165   FORTTK_ASSERT_WARN(0, "AdjustInterfaces::findPUSymbol: symbol " << dummyLocalName << " referenced in interface " << puName << " not found in the definition, go by parameter position ");
00166   return theTypeIndex;  
00167 } 
00168 
00169 PU_Info* AdjustInterfaces::findPU(ST* puName_ST_p) { 
00170   PU_Info* aPUInfoTree_p;
00171   for (aPUInfoTree_p=myPUInfoForest_p; aPUInfoTree_p!= NULL; aPUInfoTree_p=PU_Info_next(aPUInfoTree_p)) {
00172     if (aPUInfoTree_p->proc_sym==ST_st_idx(*puName_ST_p))
00173       return aPUInfoTree_p;
00174   }
00175   FORTTK_ASSERT_WARN(0,"AdjustInterfaces::findPU: cannot find definition for interface " << ST_name(puName_ST_p));
00176   return 0; 
00177 }
00178                 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines