OpenADFortTk (basic)
src/xaif2whirl/main.cxx
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /Volumes/cvsrep/developer/OpenADFortTk/src/xaif2whirl/main.cxx,v 1.28 2006/05/12 16:12:24 utke Exp $
00003 
00004 #include <iostream>
00005 #include <string>
00006 
00007 #include <xercesc/util/PlatformUtils.hpp>
00008 #include <xercesc/util/TransService.hpp>
00009 #include <xercesc/parsers/XercesDOMParser.hpp>
00010 #include <xercesc/parsers/AbstractDOMParser.hpp>
00011 #include <xercesc/dom/DOMBuilder.hpp>
00012 #include <xercesc/dom/DOMDocument.hpp>
00013 #include <xercesc/dom/DOMException.hpp>
00014 #include <xercesc/dom/DOMImplementation.hpp>
00015 #include <xercesc/dom/DOMNodeIterator.hpp>
00016 #include <xercesc/sax/SAXException.hpp>
00017 
00018 #include <OpenAnalysis/Utils/Exception.hpp>
00019 
00020 #include "Open64IRInterface/Open64BasicTypes.h"
00021 #include "cmplrs/rcodes.h"  // return codes
00022 #include "tracing.h"        // trace routines
00023 #include "ir_reader.h"      // fdump_tree
00024 #include "wn_simp.h"        // WN_Simplifier_Enable()
00025 #include "Open64IRInterface/IFDiagnostics.h"
00026 
00027 #include <Exception.h>
00028 #include "Open64IRInterface/WhirlIO.h"
00029 #include <XAIFStrings.h>
00030 #include "Args.h"
00031 #include "xaif2whirl.h"
00032 #include "XAIF_DOMErrorHandler.h"
00033 #include "XAIF_SAXErrorHandler.h"
00034 #include "XAIF_SAXHandler.h"
00035 #include "XercesStrX.h"
00036 #include "AdjustInterfaces.h"
00037 #include "AdjustFunctions.h"
00038 #include "InterfaceData.h"
00039 
00040 namespace xaif2whirl { 
00041 
00042   static int
00043   real_main(int argc, char **argv);
00044 
00045 
00046 }
00047 
00048 int main(int argc, char **argv) {
00049   try {
00050     return xaif2whirl::real_main(argc, argv);
00051   }
00052   catch (CmdLineParser::Exception& e) {
00053     e.Report(cerr); // fatal error
00054     exit(1);
00055   }
00056   catch (fortTkSupport::BaseException& e) {
00057     e.Report(cerr);
00058     exit(1);
00059   }
00060   catch (OA::Exception &e) {
00061     e.report(cerr);
00062     exit(1);
00063   }
00064   catch (...) {
00065     cerr << "Unknown exception caught\n";
00066     exit(1);
00067   }
00068   // FIXME: catch badalloc?
00069 }
00070 
00071 namespace xaif2whirl { 
00072 
00073   static int
00074   main_SAX(PU_Info* pu_forest, const char* xaiffilenm, bool validate);
00075 
00076   static int
00077   main_DOM(PU_Info* pu_forest, const char* xaiffilenm, bool validate);
00078 
00079   static int
00080   XercesInit();
00081 
00082   static int
00083   XercesFini();
00084 
00085   AlgorithmType opt_algorithm = ALG_NULL;
00086 
00087   static int
00088   real_main(int argc, char **argv) {
00089     int ret;
00090 
00091     // -------------------------------------------------------
00092     // 1. Open64 Initialization
00093     // -------------------------------------------------------
00094     Handle_Signals();
00095     MEM_Initialize();
00096     Init_Error_Handler( 100 );
00097     Set_Error_Line( ERROR_LINE_UNKNOWN );
00098     Set_Error_File( NULL );
00099     Set_Error_Phase("xaif2whirl");
00100     IR_set_dump_order(TRUE /*pre*/); // pre-order trees when debugging, please!
00101   
00102 #ifdef Is_True_On
00103     if (Get_Trace(TKIND_ALLOC, TP_MISC)) {
00104       MEM_Tracing_Enable();
00105     }
00106 #endif
00107     
00108     Preconfigure();         // from config.cxx...
00109     Configure();            // needed for WN_lower!
00110     Configure_Source(NULL); // Most config variables set here
00111   
00112     WN_Simplifier_Enable(FALSE); // turn off WHIRL expression simplifier
00113     Init_Operator_To_Opcode_Table(); // FIXME
00114 
00115     // -------------------------------------------------------
00116     // 2. Local initialization (options, etc.)
00117     // -------------------------------------------------------
00118     Diag_Init();
00119     Diag_Set_Max_Diags(100); // Maximum 100 warnings by default
00120     Diag_Set_Phase("XAIF to WHIRL: driver");
00121 
00122     if ( (ret = XercesInit()) != 0 ) { return ret; /* FIXME */ }
00123     XAIFStrings.XMLInitialize();
00124 
00125     Args args(argc, argv);
00126     opt_algorithm = args.algorithm; // FIXME
00127     fortTkSupport::Diagnostics::setDiagnosticFilterLevel(args.debug);
00128   
00129     // -------------------------------------------------------
00130     // 3. Read WHIRL IR as basis for translation
00131     // -------------------------------------------------------
00132     PU_Info* pu_forest = ReadIR(args.inWhirlFileNm.c_str(),args.myNoCleanUpFlag);
00133     PrepareIR(pu_forest); // FIXME (should this be part of translation?)
00134     InterfaceData::collect(pu_forest);
00135     if (fortTkSupport::Diagnostics::getDiagnosticFilterLevel()>1) { 
00136       InterfaceData::dump();
00137     }
00138 
00139     // -------------------------------------------------------
00140     // 4. Translate XAIF into WHIRL
00141     // -------------------------------------------------------
00142     if (ret = main_SAX(pu_forest, args.xaifFileNm.c_str(), args.validate) != 0) {return ret;} 
00143 
00144     if (!args.myNoCleanUpFlag) { 
00145       AdjustInterfaces ai(pu_forest);
00146       ai.doIt();
00147       AdjustFunctions af(pu_forest);
00148       af.doIt();
00149     }
00150 
00151     WriteIR(args.outWhirlFileNm.c_str(), pu_forest);
00152     //FreeIR(pu_forest);
00153   
00154     // -------------------------------------------------------
00155     // 5. Finalization
00156     // -------------------------------------------------------
00157 
00158     if ( (ret = XercesFini()) != 0 ) { return ret; /* FIXME */ }
00159 
00160     // If we've seen errors, note them and terminate
00161     INT local_ecount, local_wcount;
00162     if ( Get_Error_Count ( &local_ecount, &local_wcount ) ) {
00163       Terminate(Had_Internal_Error() ? RC_INTERNAL_ERROR : 
00164                 RC_NORECOVER_USER_ERROR);
00165     }
00166 
00167     Diag_Exit();
00168     Cleanup_Files(TRUE, FALSE); // Open64
00169 
00170     return RC_OKAY;
00171   }
00172 
00173 
00174   //****************************************************************************
00175 
00176   static int
00177   main_SAX(PU_Info* pu_forest, const char* xaiffilenm, bool validate) {
00178     int ret = 0;
00179 
00180     FORTTK_MSG(1, "progress: parsing input XAIF and translating to WHIRL (SAX2)");
00181 
00182     XAIF_SAXHandler theSAXHandler(pu_forest, XMLString::transcode("Core"));
00183     theSAXHandler.initialize(validate);
00184 
00185     bool errorsOccured = false;
00186     try {
00187       theSAXHandler.parse(xaiffilenm);
00188     }
00189     catch (const SAXException& e) {
00190       const unsigned int maxChars = 2047;
00191       XMLCh errText[maxChars + 1];
00192       cerr << "\nSAX Error during parsing: '" << xaiffilenm << "'\n" << "SAXException code is:  " << e.getMessage() << endl;
00193       errorsOccured = true;
00194     }
00195     catch (const XMLException& e) {
00196       cerr << "An error occurred during parsing\n   Message: " << XercesStrX(e.getMessage()) << endl;
00197       errorsOccured = true;
00198     }
00199     catch (fortTkSupport::BaseException& e) {
00200       cerr << "An error occurred during parsing of file " << xaiffilenm << ":\n" << e.GetMessage() << std::endl;
00201       errorsOccured = true;
00202     }
00203     catch (...) {
00204       cerr << "An (unknown) error occurred during parsing\n " << endl;
00205       errorsOccured = true;
00206     }
00207     FORTTK_ASSERT(!errorsOccured, "SAX2 Parse Error.");
00208     
00209     if (theSAXHandler.getParserErrorStatus())
00210       return 1; 
00211     
00212     theSAXHandler.deleteParser();
00213 
00214     return ret;
00215   }
00216 
00217   static int
00218   XercesInit() 
00219   {
00220     // Initialize the XML4C2 system
00221     try {
00222       XMLPlatformUtils::Initialize();
00223     }
00224     catch (const XMLException& toCatch) {
00225       cerr << "Error: unable to initialize XAIF processor!:\n"
00226            << "\t" << XercesStrX(toCatch.getMessage()) << endl;
00227       return 1;
00228     }
00229 
00230     return 0;
00231   }
00232 
00233   static int
00234   XercesFini()
00235   {
00236     // Terminate the XML4C2 system
00237     XMLPlatformUtils::Terminate();
00238     return 0;
00239   }
00240 
00241 } // end namespace xaif2whirl
00242 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines