OpenADFortTk (basic)
src/whirl2xaif/main.cxx
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /m_home/m_utkej/Argonne/cvs2svn/cvs/OpenADFortTk/src/whirl2xaif/main.cxx,v 1.29 2007-10-08 18:28:34 utke Exp $
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <string>
00007 
00008 #include <xmlostream.h>
00009 
00010 #include "OpenAnalysis/Utils/Exception.hpp"
00011 
00012 #include "Open64IRInterface/Open64BasicTypes.h"
00013 // extra from Open64: 
00014 #include "cmplrs/rcodes.h"  // return codes
00015 #include "tracing.h"        // trace routines
00016 #include "ir_reader.h"      // fdump_tree
00017 // from FortTk again: 
00018 #include "Open64IRInterface/WhirlIO.h"
00019 #include "Diagnostics.h"
00020 #include "Args.h"
00021 #include "whirl2xaif.h"
00022 
00023 // some forward decls
00024 static int real_main(int argc, char **argv);
00025 static std::ostream& InitOutputStream(Args& args);
00026 static void FiniOutputStream(std::ostream& os, Args& args);
00027 
00028 int
00029 main(int argc, char **argv) {
00030   try {
00031     return real_main(argc, argv);
00032   }
00033   catch (CmdLineParser::Exception& e) {
00034     e.Report(cerr); // fatal error
00035     exit(1);
00036   }
00037   catch (fortTkSupport::BaseException& e) {
00038     e.Report(cerr);
00039     exit(1);
00040   }
00041   catch (xml::ostream::Exception& e) {
00042     e.Report(cerr);
00043     exit(1);
00044   }
00045   catch (OA::Exception& e) {
00046     e.report(cerr);
00047     exit(1);
00048   }
00049   catch (...) {
00050     std::cerr << "Unknown exception caught\n";
00051     exit(1);
00052   }
00053 }
00054 
00055 static int real_main(int argc, char **argv) {
00056   // -------------------------------------------------------
00057   // 1. Open64 Initialization
00058   // -------------------------------------------------------
00059   Handle_Signals();
00060   MEM_Initialize();
00061   Init_Error_Handler( 100 );
00062   Set_Error_Line( ERROR_LINE_UNKNOWN );
00063   Set_Error_File( NULL );
00064   Set_Error_Phase("whirl2xaif");
00065   IR_set_dump_order(TRUE /*pre*/); // pre-order trees when debugging, please!
00066   
00067 #ifdef Is_True_On
00068   if (Get_Trace(TKIND_ALLOC, TP_MISC)) {
00069     MEM_Tracing_Enable();
00070   }
00071 #endif
00072   
00073   Preconfigure();         // from config.cxx...
00074   Configure();            // needed for WN_lower!
00075   Configure_Source(NULL); // Most config variables set here
00076 
00077   Init_Operator_To_Opcode_Table(); // FIXME
00078     
00079   // -------------------------------------------------------
00080   // 2. Local initialization (options, etc.)
00081   // -------------------------------------------------------
00082   Diag_Init();
00083   Diag_Set_Max_Diags(100); // Maximum 100 warnings by default
00084   Diag_Set_Phase("WHIRL to XAIF: driver");
00085   
00086   Args args(argc, argv);
00087   std::ostream& os(InitOutputStream(args));
00088   fortTkSupport::Diagnostics::setDiagnosticFilterLevel(args.debug);
00089   
00090   // -------------------------------------------------------
00091   // 3. Read WHIRL IR
00092   // -------------------------------------------------------
00093   PU_Info* pu_forest = ReadIR(args.whirlFileNm.c_str());
00094   PrepareIR(pu_forest); // FIXME (should this be part of translation?)
00095   
00096   // -------------------------------------------------------
00097   // 4. Translate WHIRL into XAIF
00098   // -------------------------------------------------------  
00099   
00100   os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n";
00101   whirl2xaif::Whirl2Xaif::translateIR(os, pu_forest, args.tmpVarPrefix.c_str());
00102   
00103   bool writeIR = false;
00104   if (writeIR) { 
00105     std::string file = "out.B"; // FIXME
00106     WriteIR(file.c_str(), pu_forest); 
00107   } 
00108   else {
00109     FreeIR(pu_forest); // Writing frees some of the WHIRL maps
00110   }
00111   
00112   // If we've seen errors, note them and terminate
00113   INT local_ecount, local_wcount;
00114   if ( Get_Error_Count ( &local_ecount, &local_wcount ) ) {
00115     Terminate(Had_Internal_Error() ? RC_INTERNAL_ERROR : 
00116               RC_NORECOVER_USER_ERROR);
00117   }
00118 
00119   Diag_Exit();
00120   Cleanup_Files(TRUE, FALSE); // Open64
00121 
00122   // -------------------------------------------------------
00123   // 5. Finalization
00124   // -------------------------------------------------------
00125   FiniOutputStream(os,args);
00126   return RC_OKAY;
00127 }
00128 
00129 static std::ostream& InitOutputStream(Args& args) {
00130   if (args.xaifFileNm.empty()) {
00131     return std::cout;
00132   } 
00133   else {
00134     std::ofstream* ofs = new std::ofstream;
00135     std::string filename(args.xaifFileNm+".tmp");
00136     int keepErrno;
00137     errno=0;
00138     ofs->open(filename.c_str(), ios::out | ios::trunc);
00139     if (keepErrno=errno || !ofs->is_open() || ofs->fail()) 
00140       FORTTK_DIE("cannot open temporary file " << filename.c_str() << " because: " << strerror(keepErrno));
00141     return *ofs;
00142   }
00143 }
00144 
00145 static void
00146 FiniOutputStream(std::ostream& os,Args& args) {
00147   if (!args.xaifFileNm.empty()) {
00148     delete &os;
00149     std::string tmpfilename(args.xaifFileNm+".tmp");
00150     int keepErrno;
00151     errno=0;
00152     rename(tmpfilename.c_str(), args.xaifFileNm.c_str());
00153     if (keepErrno=errno)
00154       FORTTK_DIE("cannot rename temporary file " << tmpfilename.c_str() << " to output file " <<  args.xaifFileNm.c_str() << " because: " << strerror(keepErrno));
00155   }
00156 }
00157 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines