OpenADFortTk (basic)
src/testers/tester.cxx
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /m_home/m_utkej/Argonne/cvs2svn/cvs/OpenADFortTk/src/testers/tester.cxx,v 1.21 2007-10-08 18:28:34 utke Exp $
00003 
00004 #include <stdlib.h> // ANSI: cstdlib // for strtol
00005 #include <string.h> // ANSI: cstring // for strcmp, etc.
00006 #include <iostream>
00007 
00008 #include "OpenAnalysis/ExprTree/ExprTree.hpp"
00009 
00010 #include "Open64IRInterface/Open64BasicTypes.h"
00011 #include "ir_reader.h"      // fdump_tree
00012 #include "w2f_driver.h"     // W2F_*
00013 #include "Open64IRInterface/Open64IRInterface.hpp"
00014 #include "Open64IRInterface/SymTab.h"
00015 
00016 #include "ScalarizedRefTab.h"
00017 #include "Diagnostics.h"
00018 
00019 #include "tester.h"
00020 
00021 using std::cerr;
00022 using std::endl;
00023 
00024 static int
00025 DumpExprTree(std::ostream& os, WN* wn);
00026 
00027 static int
00028 DumpExprTree(std::ostream& os, OA::OA_ptr<OA::ExprTree> tree);
00029 
00030 static int
00031 TestForEachPU(std::ostream& os, PU_Info* pu_forest);
00032 
00033 static int
00034 TestForEachWNPU(std::ostream& os, WN* wn_pu);
00035 
00036 static int
00037 TestForEachWN(std::ostream& os, WN* wn);
00038 
00039 static void
00040 RecursiveFnWN(std::ostream& os, WN* wn);
00041 
00042 
00043 int
00044 whirltester::TestIR(std::ostream& os, PU_Info* pu_forest)
00045 {
00046   Diag_Set_Phase("WHIRL tester: TestIR");
00047   
00048   if (!pu_forest) { return 0; }
00049   
00050   // Here is where we can do something
00051   TestForEachPU(os, pu_forest);
00052 
00053   return 0;
00054 }
00055 
00056 static int
00057 TestForEachPU(std::ostream& os, PU_Info* pu_forest)
00058 {
00059   Open64IRProcIterator procIt(pu_forest);
00060   for ( ; procIt.isValid(); ++procIt) { 
00061     
00062     // The PU_Info* for this PU
00063     PU_Info* pu = (PU_Info*)procIt.current().hval();
00064 
00065     // The root of the WHIRL tree
00066     WN* wn_pu = PU_Info_tree_ptr(pu);
00067     
00068     TestForEachWNPU(os, wn_pu);
00069   }
00070   return 0;
00071 }
00072 
00073 static int
00074 TestForEachWNPU(std::ostream& os, WN* wn_pu)
00075 {
00076   // We may want to do something special with the top level WN (FUNC_ENTRY)
00077   //...
00078   
00079   // Iterate over each WN and do something
00080   WN_TREE_CONTAINER<PRE_ORDER> wtree(wn_pu);
00081   WN_TREE_CONTAINER<PRE_ORDER>::iterator it;
00082   
00083   for (it = wtree.begin(); it != wtree.end(); ++it) {
00084     WN* curWN = it.Wn();
00085 
00086     TestForEachWN(os, curWN);
00087   }
00088 
00089   // Alternatively we may want to recursively do something with the WHIRL tree
00090   RecursiveFnWN(os, wn_pu);
00091   
00092   return 0;
00093 }
00094 
00095 static int
00096 TestForEachWN(std::ostream& os, WN* wn)
00097 {
00098   if (wn) {
00099     DumpExprTree(os, wn);
00100     os << endl;
00101   }
00102   return 0;
00103 }
00104 
00105 static void
00106 RecursiveFnWN(std::ostream& os, WN* wn)
00107 {
00108   if (wn == NULL) {
00109     // Base case
00110 
00111   } 
00112   
00113   OPERATOR opr = WN_operator(wn);
00114 
00115   if ( /* some test */ false ) {
00116     // Base case
00117   } else if (!OPERATOR_is_leaf(opr)) {
00118     
00119     // General recursive case
00120     if (WN_operator(wn) == OPR_BLOCK) {
00121       WN *kid = WN_first(wn);
00122       while (kid) {
00123         RecursiveFnWN(os, kid);
00124         kid = WN_next(kid);
00125       }
00126     } else {
00127       for (INT kidno = 0; kidno < WN_kid_count(wn); kidno++) {
00128         WN* kid = WN_kid(wn, kidno);
00129         RecursiveFnWN(os, kid);
00130       }
00131     }
00132   }
00133 }
00134 
00135 //****************************************************************************
00136 
00137 static int
00138 DumpExprNode(std::ostream& os, OA::OA_ptr<OA::ExprTree::Node> node, 
00139              OA::OA_ptr<Open64IRInterface> ir);
00140 
00141 static int
00142 DumpExprTree(std::ostream& os, WN* wn)
00143 {
00144   OA::OA_ptr<Open64IRInterface> ir;
00145   ir = new Open64IRInterface;
00146 
00147   OPERATOR opr = WN_operator(wn);
00148   if (OPERATOR_is_expression(opr)) {
00149       OA::OA_ptr<OA::ExprTree> tree = 
00150       ir->getExprTree(OA::ExprHandle((OA::irhandle_t)wn));
00151     DumpExprTree(os, tree);
00152   }
00153   
00154   return 0;
00155 }
00156 
00157 static int
00158 DumpExprTree(std::ostream& os, OA::OA_ptr<OA::ExprTree> tree)
00159 {
00160   OA::OA_ptr<Open64IRInterface> ir;
00161   ir = new Open64IRInterface;
00162   
00163   OA::Tree::PreOrderIterator nodes_iter(*tree);
00164   for ( ; nodes_iter.isValid(); ++nodes_iter) {
00165     OA::OA_ptr<OA::Tree::Node> ntmp = nodes_iter.current();
00166     OA::OA_ptr<OA::ExprTree::Node> node = ntmp.convert<OA::ExprTree::Node>();
00167     DumpExprNode(os, node, ir);
00168   }
00169   
00170   return 0;
00171 }
00172 
00173 static int
00174 DumpExprNode(std::ostream& os, OA::OA_ptr<OA::ExprTree::Node> node, 
00175              OA::OA_ptr<Open64IRInterface> ir)
00176 {
00177 #if 0
00178   std::string& attr = node->getAttr();
00179   os << "{ " << attr;
00180   
00181   if (node->isSym()) {
00182     const char* nm = ir.toString(node->getSymHandle());
00183     os << " sym: " << nm; 
00184   } else if (node->isConst()) {
00185     os << " const"; 
00186   }
00187   os << " }";
00188 #endif
00189 
00190   return 0;
00191 }
00192 
00193 
00194 //****************************************************************************
00195 // TestIR_OA: 
00196 //****************************************************************************
00197 
00198 static int
00199 TestIR_OA_ForEachWNPU(std::ostream& os, WN* wn_pu);
00200 
00201 #if 0 
00202 static void
00203 TestIR_OA_ForEachVarRef(std::ostream& os, WN* wn, 
00204                         Open64IRInterface& ir, UJNumbers& vnmap);
00205 #endif
00206 
00207 int
00208 whirltester::TestIR_OA(std::ostream& os, PU_Info* pu_forest)
00209 {
00210   Diag_Set_Phase("WHIRL tester: TestIR_OA");
00211   
00212   if (!pu_forest) { return 0; }
00213 
00214   Open64IRProcIterator procIt(pu_forest);
00215   for ( ; procIt.isValid(); ++procIt) { 
00216     
00217     // The PU_Info* for this PU
00218     PU_Info* pu = (PU_Info*)procIt.current().hval();
00219 
00220     // The root of the WHIRL tree
00221     WN* wn_pu = PU_Info_tree_ptr(pu);
00222     
00223     TestIR_OA_ForEachWNPU(os, wn_pu);
00224   }
00225   return 0;
00226 }
00227 
00228 static int
00229 TestIR_OA_ForEachWNPU(std::ostream& os, WN* wn_pu)
00230 {
00231   WN* fbody = WN_func_body(wn_pu);
00232 
00233 #if 0  
00234   Open64IRInterface irInterface;
00235   Open64IRStmtIterator irStmtIter(fbody);
00236   CFG cfg(irInterface, &irStmtIter, (SymHandle)WN_st(wn_pu), true);
00237 
00238   // Accumulate the ST* for parameters
00239   std::set<SymHandle> params;
00240   INT nparam = WN_num_formals(wn_pu);
00241   for (int i = 0; i < nparam; ++i) {
00242     ST* st = WN_st(WN_formal(wn_pu, i));
00243     params.insert((SymHandle)st);
00244   }
00245 
00246   UJNumbers vnmap(cfg, params);
00247   TestIR_OA_ForEachVarRef(os, wn_pu, irInterface, vnmap);
00248 #endif
00249 
00250   return 0;
00251 }
00252 
00253 #if 0
00254 static void
00255 TestIR_OA_ForEachVarRef(std::ostream& os, WN* wn, 
00256                         Open64IRInterface& ir, UJNumbers& vnmap)
00257 {
00258   if (wn == NULL) {
00259     // Base case
00260   } 
00261 
00262   OPERATOR opr = WN_operator(wn);
00263   // OPR_STID is a special case.  It is a base case in the sense the
00264   // it represents a LHS definition; but it is also a recursive case
00265   // because uses are embedded in its RHS subtree.
00266   bool varref = IsRefTranslatableToXAIF(wn);
00267   bool recur = false;
00268 
00269   // Base case
00270   if (varref || opr == OPR_STID) {
00271     VN vn = vnmap.Find((ExprHandle)wn);
00272 
00273     ExprTree* tree = ir.GetExprTreeForExprHandle((ExprHandle)wn);    
00274     os << "VN = " << vn << endl;
00275     os << "  ";
00276     DumpExprTree(os, tree);
00277     os << endl;
00278     delete tree;
00279 
00280     if (opr == OPR_STID) { recur = true; }
00281   } 
00282 
00283   // General recursive case
00284   if ( (!varref || recur) && !OPERATOR_is_leaf(opr) ) {
00285     
00286     if (WN_operator(wn) == OPR_BLOCK) {
00287       WN *kid = WN_first(wn);
00288       while (kid) {
00289         TestIR_OA_ForEachVarRef(os, kid, ir, vnmap);
00290         kid = WN_next(kid);
00291       }
00292     } else {
00293       for (INT kidno = 0; kidno < WN_kid_count(wn); ++kidno) {
00294         WN* kid = WN_kid(wn, kidno);
00295         TestIR_OA_ForEachVarRef(os, kid, ir, vnmap);
00296       }
00297     }
00298   }
00299 }
00300 #endif
00301 
00302 
00303 //****************************************************************************
00304 // TestIR_whirl2f: 
00305 //****************************************************************************
00306 
00307 static WN*
00308 PleaseGetMeSomething(WN* wn_pu);
00309 
00310 int
00311 whirltester::TestIR_whirl2f(std::ostream& os, PU_Info* pu_forest)
00312 {
00313   static const int bufSZ = 1024 * 1024;
00314   static char buf[bufSZ];
00315   
00316   Diag_Set_Phase("WHIRL tester: TestIR_whirl2f");
00317   if (!pu_forest) { return 0; }
00318   
00319   PU_AllocBEGlobalSymtab();
00320   W2F_Init();
00321   
00322   Open64IRProcIterator procIt(pu_forest);
00323   for ( ; procIt.isValid(); ++procIt) { 
00324     
00325     // The PU_Info* for this PU
00326     PU_Info* pu = (PU_Info*)procIt.current().hval();
00327     
00328     // The root of the WHIRL tree and a statement to translate
00329     WN* wn_pu = PU_Info_tree_ptr(pu);
00330     WN* wn = PleaseGetMeSomething(wn_pu);
00331 
00332     PU_AllocBELocalSymtab(pu);
00333     W2F_Push_PU(wn_pu, wn);
00334     
00335     // Translate and output
00336     os << "----> The tree:" << endl;
00337     fdump_tree(stdout, wn);
00338     os << endl;
00339     
00340     os << "----> The translation:" << endl;
00341     W2F_Translate_Wn_Str(buf, bufSZ-1, wn);
00342     os << buf << endl;
00343     
00344     W2F_Pop_PU();
00345     PU_FreeBELocalSymtab(pu);
00346     break;
00347   }
00348   
00349   W2F_Fini();
00350   PU_FreeBEGlobalSymtab();
00351   return 0;
00352 }
00353 
00354 static WN*
00355 PleaseGetMeSomething(WN* wn_pu) 
00356 {
00357   WN_TREE_CONTAINER<PRE_ORDER> wtree(wn_pu);
00358   WN_TREE_CONTAINER<PRE_ORDER>::iterator it;
00359 
00360   // Find the first non-empty block
00361   WN* blk = NULL;
00362   for (it = wtree.begin(); it != wtree.end(); ++it) {
00363     WN* curWN = it.Wn();
00364     OPERATOR opr = WN_operator(curWN);
00365     if (opr == OPR_BLOCK && WN_first(curWN)) { // non-empty block
00366       blk = curWN;
00367       break;
00368     }
00369   }
00370   
00371   if (blk) {
00372     // Create a comment and insert it into the block
00373     WN* com1 = WN_CreateComment("$OpenAD BEGIN FRAGMENT(1)");
00374     WN_INSERT_BlockFirst(blk, com1);
00375     
00376     WN* com2 = WN_CreateComment("$OpenAD END FRAGMENT(1)");
00377     WN_INSERT_BlockLast(blk, com2);
00378   }
00379   
00380   // Return body of the wn_pu
00381   return WN_func_body(wn_pu); // blk;
00382 }
00383 
00384 
00385 //****************************************************************************
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines