OpenADFortTk (basic)
src/xaif2whirl/Args.cxx
Go to the documentation of this file.
00001 // ##########################################################
00002 // # This file is part of OpenADFortTk.                     #
00003 // # The full COPYRIGHT notice can be found in the top      #
00004 // # level directory of the OpenADFortTk source tree.       #
00005 // # For more information visit                             #
00006 // # http://www.mcs.anl.gov/openad                          #
00007 // ##########################################################
00008 
00009 #include "Open64IRInterface/Open64BasicTypes.h"
00010 #include "file_util.h"      // New_Extension, Last_Pathname_Component
00011 
00012 #include "Args.h"
00013 
00014 using std::cerr;
00015 using std::endl;
00016 using std::string;
00017 
00018 
00019 TYPE_ID Args::ourDefaultMTypeInt  = MTYPE_I8;
00020 TYPE_ID Args::ourDefaultMTypeUInt = MTYPE_U8;
00021 TYPE_ID Args::ourDefaultMTypeReal = MTYPE_F8;
00022 std::string Args::ourActiveTypeNm("oadactive");
00023 
00024 static const char* version_info = "version .289";
00025 
00026 static const char* usage_summary =
00027 "[options] <whirl-file> <xaif-file>\n";
00028 
00029 static const char* usage_details =
00030 "Given a WHIRL file and a *corresponding* XAIF file, generates new WHIRL.\n"
00031 "By default, the output is sent to the filename formed by replacing the\n"
00032 "extension of <xaif-file> with 'x2w.B'.\n"
00033 "\n"
00034 "Algorithms:\n"
00035 "  --bb-patching     TEMPORARY: use basic-block patch algorithm\n"
00036 "\n"
00037 "Options:\n"
00038 "  -o, --output <file> send output to <file> instead of default file\n"
00039 "      --i4            make integers 4 byte where not specified \n"
00040 "                      (default 8 bytes)\n"
00041 "      --u4            make unsigned integers 4 byte where not specified \n"
00042 "                      (default 8 bytes)\n"
00043 "      --r4            make reals 4 byte where not specified (default 8 bytes)\n"
00044 "  -t, --type <name>   abstract active type name (default oadactive), no longer\n" 
00045 "                      than 26 characters\n"
00046 "  -V, --version       print version information and exit\n"
00047 "  -v, --validate      validate agains schema\n"
00048 "  -h, --help          print this help and exit\n"
00049 "  -n, --noCleanUp     only for development: do not perform whirl cleanup \n"
00050 "                      needed for OpenAD\n"
00051 "      --debug [lvl]   only for development: debug mode at level `lvl'\n";
00052 
00053 
00054 #define CLP CmdLineParser
00055 
00056 CmdLineParser::OptArgDesc Args::optArgs[] = {
00057   // Options
00058   {  0 , "bb-patching",     CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00059   {  0 , "i4",              CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00060   {  0 , "u4",              CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00061   {  0 , "r4",              CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00062   { 'n', "noCleanUp",CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00063   { 'o', "output",   CLP::ARG_REQ , CLP::DUPOPT_ERR,  NULL },
00064   { 't', "type",     CLP::ARG_REQ , CLP::DUPOPT_ERR,  NULL },
00065   { 'V', "version",  CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00066   { 'v', "validate", CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00067   { 'h', "help",     CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00068   {  0 , "debug",    CLP::ARG_OPT,  CLP::DUPOPT_CLOB, NULL },
00069   CmdLineParser::OptArgDesc_NULL
00070 };
00071 
00072 #undef CLP
00073 
00074 //***************************************************************************
00075 // Args
00076 //***************************************************************************
00077 
00078 Args::Args()
00079 {
00080   Ctor();
00081 }
00082 
00083 Args::Args(int argc, const char* const argv[])
00084 {
00085   Ctor();
00086   Parse(argc, argv);
00087 }
00088 
00089 void
00090 Args::Ctor()
00091 {
00092   algorithm = xaif2whirl::ALG_NULL;
00093   validate  = false; 
00094   debug     = 0; // default: 0 (off)
00095   myNoCleanUpFlag = false;
00096 }
00097 
00098 Args::~Args()
00099 {
00100 }
00101 
00102 
00103 void 
00104 Args::PrintVersion(std::ostream& os) const
00105 {
00106   os << GetCmd() << ": " << version_info << endl;
00107 }
00108 
00109 
00110 void 
00111 Args::PrintUsage(std::ostream& os) const
00112 {
00113   os << "Usage: " << GetCmd() << " " << usage_summary << endl
00114      << usage_details << endl;
00115 } 
00116 
00117 
00118 void 
00119 Args::PrintError(std::ostream& os, const char* msg) const
00120 {
00121   os << GetCmd() << ": " << msg << endl
00122      << "Try `" << GetCmd() << " --help' for more information." << endl;
00123 }
00124 
00125 void 
00126 Args::PrintError(std::ostream& os, const std::string& msg) const
00127 {
00128   PrintError(os, msg.c_str());
00129 }
00130 
00131 
00132 void
00133 Args::Parse(int argc, const char* const argv[])
00134 {
00135   try {
00136     // -------------------------------------------------------
00137     // Parse the command line
00138     // -------------------------------------------------------
00139     parser.Parse(optArgs, argc, argv);
00140     
00141     // -------------------------------------------------------
00142     // Sift through results, checking for semantic errors
00143     // -------------------------------------------------------
00144     
00145     // Special options that should be checked first
00146     if (parser.IsOpt("debug")) { 
00147       debug = 1; 
00148       if (parser.IsOptArg("debug")) {
00149         const string& arg = parser.GetOptArg("debug");
00150         debug = (int)CmdLineParser::ToLong(arg);
00151       }
00152     }
00153     if (parser.IsOpt("help")) { 
00154       PrintUsage(std::cerr); 
00155       exit(0);
00156     }
00157     if (parser.IsOpt("validate")) { 
00158       validate=true;
00159     }
00160     if (parser.IsOpt("version")) { 
00161       PrintVersion(std::cerr);
00162       exit(0);
00163     }
00164     
00165     if (parser.IsOpt("bb-patching")) { 
00166       algorithm = xaif2whirl::ALG_BB_PATCHING;
00167     }
00168 
00169     // Check for other options    
00170     if (parser.IsOpt("output")) { 
00171       outWhirlFileNm = parser.GetOptArg("output");
00172     }
00173 
00174     if (parser.IsOpt("type")) { 
00175       ourActiveTypeNm = parser.GetOptArg("type");
00176       if (ourActiveTypeNm.length()>ourActiveTypeNmLength-1) {
00177         PrintError(std::cerr, "-t argument: " + ourActiveTypeNm + " too long." );
00178         exit(1);
00179       }
00180     }
00181 
00182     if (parser.IsOpt("i4")) { 
00183       ourDefaultMTypeInt=MTYPE_I4;
00184     }
00185     
00186     if (parser.IsOpt("u4")) { 
00187       ourDefaultMTypeUInt=MTYPE_U4;
00188     }
00189  
00190     if (parser.IsOpt("r4")) { 
00191       ourDefaultMTypeReal=MTYPE_F4;
00192     }
00193     
00194     if (parser.IsOpt("noCleanUp")) { 
00195       myNoCleanUpFlag = true;
00196     }
00197 
00198     // Check for required arguments
00199     if (parser.GetNumArgs() != 2) {
00200       PrintError(std::cerr, "Invalid number of arguments!");
00201       exit(1);
00202     }
00203     inWhirlFileNm = parser.GetArg(0);
00204     xaifFileNm = parser.GetArg(1);
00205   } 
00206   catch (CmdLineParser::ParseError& e) {
00207     PrintError(std::cerr, e.GetMessage());
00208     exit(1);
00209   }
00210 
00211   // -------------------------------------------------------
00212   // Postprocess
00213   // -------------------------------------------------------
00214   if (outWhirlFileNm.empty()) {
00215     // FIXME: should we place in current directory?
00216     outWhirlFileNm = New_Extension(xaifFileNm.c_str(), ".x2w.B");
00217   }
00218 }
00219 
00220 
00221 void 
00222 Args::Dump(std::ostream& os) const
00223 {
00224   parser.Dump(os);
00225 }
00226 
00227 void 
00228 Args::DDump() const
00229 {
00230   Dump(std::cerr);
00231 }
00232 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines