OpenADFortTk (basic)
src/whirl2sexp/main.cxx
Go to the documentation of this file.
00001 // -*-Mode: C++;-*-
00002 // $Header: /Volumes/cvsrep/developer/OpenADFortTk/src/whirl2sexp/main.cxx,v 1.2 2004/08/23 18:44:42 eraxxon Exp $
00003 
00004 #include <iostream>
00005 #include <string>
00006 
00007 #include "Open64IRInterface/Open64BasicTypes.h"
00008 #include "cmplrs/rcodes.h"  // return codes
00009 #include "tracing.h"        // trace routines
00010 #include "ir_reader.h"      // fdump_tree
00011 #include "Open64IRInterface/WhirlIO.h"
00012 #include "Open64IRInterface/IFDiagnostics.h"
00013 
00014 #include "Diagnostics.h"
00015 #include "sexpostream.h"
00016 
00017 #include "Args.h"
00018 #include "whirl2sexp.h"
00019 #include "Exception.h"
00020 
00021 //************************** Forward Declarations ***************************
00022 
00023 static int
00024 real_main(int argc, char **argv);
00025 
00026 static std::ostream*
00027 InitOutputStream(Args& args);
00028 
00029 static void
00030 FiniOutputStream(std::ostream* os);
00031 
00032 static void 
00033 OpenFile(std::ofstream& fs, const char* filename);
00034 
00035 static void 
00036 CloseFile(std::ofstream& fs);
00037 
00038 
00039 //***************************************************************************
00040 
00041 int
00042 main(int argc, char **argv)
00043 {
00044   try {
00045     return real_main(argc, argv);
00046   }
00047   catch (CmdLineParser::Exception& e) {
00048     e.Report(cerr); // fatal error
00049     exit(1);
00050   }
00051   catch (fortTkSupport::BaseException& e) {
00052     e.Report(cerr);
00053     exit(1);
00054   }
00055   catch (sexp::ostream::Exception& e) {
00056     e.Report(cerr);
00057     exit(1);
00058   }
00059   catch (...) {
00060     cerr << "Unknown exception caught\n";
00061     exit(1);
00062   }
00063   // FIXME: catch badalloc?
00064 }
00065 
00066 static int
00067 real_main(int argc, char **argv)
00068 {
00069   // -------------------------------------------------------
00070   // 1. Open64 Initialization
00071   // -------------------------------------------------------
00072   Handle_Signals();
00073   MEM_Initialize();
00074   Init_Error_Handler( 100 );
00075   Set_Error_Line( ERROR_LINE_UNKNOWN );
00076   Set_Error_File( NULL );
00077   Set_Error_Phase("whirl2sexp");
00078   IR_set_dump_order(TRUE /*pre*/); // pre-order trees when debugging, please!
00079   
00080 #ifdef Is_True_On
00081   if (Get_Trace(TKIND_ALLOC, TP_MISC)) {
00082     MEM_Tracing_Enable();
00083   }
00084 #endif
00085   
00086   Preconfigure();         // from config.cxx...
00087   Configure();            // needed for WN_lower!
00088   Configure_Source(NULL); // Most config variables set here
00089 
00090   Init_Operator_To_Opcode_Table(); // FIXME
00091     
00092   // -------------------------------------------------------
00093   // 2. Local initialization (options, etc.)
00094   // -------------------------------------------------------
00095   Diag_Init();
00096   Diag_Set_Max_Diags(100); // Maximum 100 warnings by default
00097   Diag_Set_Phase("WHIRL to sexp: driver");
00098   
00099   Args args(argc, argv);
00100   std::ostream* os = InitOutputStream(args);
00101   fortTkSupport::Diagnostics::setDiagnosticFilterLevel(args.debug);
00102   
00103   // -------------------------------------------------------
00104   // 3. Read WHIRL and Translate into S-expressions
00105   // -------------------------------------------------------
00106   PU_Info* pu_forest = ReadIR(args.whirlFileNm.c_str(),args.myNoCleanUpFlag);
00107   whirl2sexp::TranslateIR(*os, pu_forest);
00108   FreeIR(pu_forest); // Writing frees some of the WHIRL maps
00109   
00110   // -------------------------------------------------------
00111   // 4. Finalization
00112   // -------------------------------------------------------
00113   FiniOutputStream(os);
00114   
00115   // If we've seen errors, note them and terminate
00116   INT local_ecount, local_wcount;
00117   if ( Get_Error_Count ( &local_ecount, &local_wcount ) ) {
00118     Terminate(Had_Internal_Error() ? RC_INTERNAL_ERROR : 
00119               RC_NORECOVER_USER_ERROR);
00120   }
00121 
00122   Diag_Exit();
00123   Cleanup_Files(TRUE, FALSE); // Open64
00124   
00125   return RC_OKAY;
00126 }
00127 
00128 
00129 //***************************************************************************
00130 // 
00131 //***************************************************************************
00132 
00133 static std::ostream*
00134 InitOutputStream(Args& args)
00135 {
00136   if (args.sexpFileNm.empty()) {
00137     // Use cout
00138     return &(std::cout);
00139   } else {
00140     ofstream* ofs = new ofstream;
00141     OpenFile(*ofs, args.sexpFileNm.c_str());
00142     return ofs;
00143   }
00144 }
00145 
00146 static void
00147 FiniOutputStream(std::ostream* os)
00148 {
00149   if (os != &std::cout) {
00150     delete os;
00151   }
00152 }
00153 
00154 
00155 static void 
00156 OpenFile(std::ofstream& fs, const char* filename)
00157 {
00158   using namespace std;
00159 
00160   fs.open(filename, ios::out | ios::trunc);
00161   if (!fs.is_open() || fs.fail()) {
00162     ErrMsg(EC_IR_Open, filename, 0/*FIXME*/);
00163   } 
00164 }
00165 
00166 static void
00167 CloseFile(std::ofstream& fs)
00168 {
00169   fs.close();
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines