Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
whirl2f.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 /* ====================================================================
00037  * ====================================================================
00038  *
00039  *
00040  * Revision history:
00041  *  07-Nov-94 - Original Version
00042  *
00043  * Description:
00044  *
00045  *   Defines the main program (whirl2f), which parses the command-
00046  *   line options and translates an intermediate Very-High or High
00047  *   level WHIRL file into Fortran.  The command-line options are
00048  *   processed from left to right without any ordering constraint.
00049  *   In EBNF notation where '{' indicates 1 or more repititions,
00050  *   '|' indicates choice, and '[' indicates an optional item:
00051  *
00052  *      whirl2f [-FLIST:<opts>] {<Whirl_File_Name>}
00053  *
00054  *      <opts> ::= <single_opt>[:<opts>]
00055  *      <single_opt> ::= show | 
00056  *                       old_f77 |
00057  *                       ansi_format |
00058  *                       no_pragmas  |
00059  *                       emit_pfetch | 
00060  *                       emit_linedir |
00061  *                       src_file=<Src_OutFile_Name> |
00062  *                       ftn_file=<Ftn_OutFile_Name> |
00063  *                       loc_file=<LocationMap_OutFile_Name>
00064  *
00065  *   Note that the <Whirl_File_Name> is a mandatory option, and in
00066  *   its abscence a message about usage will be emitted.  If the 
00067  *   <Src_File_Name> is not given it will be derived from the
00068  *   <Whirl_File_Name>.  The output file-names will be derived
00069  *   from the <Src_File_Name>, unless they are explicitly given.
00070  *
00071  * ====================================================================
00072  * ====================================================================
00073  */
00074 static char *source_file = __FILE__;
00075 
00076 #include <unistd.h>                 /* for close(), etc. */
00077 #include <elf.h>              /* for wn.h */
00078 #include "whirl2f_common.h"
00079 #include "glob.h"             /* Irb_File_Name, Cur_PU_Name */
00080 #include "flags.h"            /* for OPTION_GROUP */
00081 #include "pu_info.h"          /* For PU_Info */
00082 #include "err_host.tab"       /* Include the error tables in the driver */
00083 
00084 #include "pu_info.h"          /* For PU_Info */
00085 #include "w2f_driver.h"
00086 #include "file_util.h"        /* for New_Extension () */
00087 #include "ir_bread.h"         /* Binary WHIRL reader */
00088 
00089 
00090 /* ====================================================================
00091  *
00092  * Local data.
00093  *
00094  * ====================================================================
00095  */
00096 
00097 /* Default file extensions: */
00098 #define FTN_FILE_EXTENSION ".f"  /* Fortran file */
00099 #define IRB_FILE_EXTENSION ".B"  /* WHIRL file */
00100 
00101 
00102 /* ====================================================================
00103  *
00104  * Usage
00105  *
00106  * Give a warning about the usage of this tool, in terms of the
00107  * command-line options it accepts.
00108  *
00109  * ====================================================================
00110  */
00111 
00112 static void
00113 Usage (char *progname)
00114 {
00115    fprintf(stderr, 
00116            "USAGE: in EBNF notation, where '|' indicates choice and '['\n"
00117            "indicates an optional item:\n"
00118            "\n"
00119            "\t%s [-FLIST:<opts>] [-TARG:<t>] [-TENV:<e>] <Whirl_File_Name>\n"
00120            "\n"
00121            "\t<opts> ::= <single_opt>[:<opts>]\n"
00122            "\n"
00123            "The <Whirl_File_Name> is a mandatory command-line argument.\n"
00124            "We recommend always using the common option -TARG:abi=[32|64].\n" 
00125            "\n",
00126            progname);
00127    
00128    fprintf(stderr,
00129            "Each -FLIST:<single_opt> is described below:\n"
00130            "\n"
00131            "-FLIST:show\n"
00132            "\tIndicate the input/output file-names to stderr.\n"
00133            "-FLIST:old_f77\n"
00134            "\tPrevents emission of calls to intrinsic functions that are not\n"
00135            "\tin compilers earlier than version v7.00.  The generated source\n"
00136            "\twill include <whirl2f.h>\n"
00137            "-FLIST:ansi_format\n"
00138            "\tFormat the output according to Fortran 77 rules, with at most\n"
00139            "\t72 columns per line.  Without this option, tab formatting is\n"
00140            "\temployed without any limit on the line-length.\n"
00141            "-FLIST:no_pragmas\n"
00142            "\tTurn off emission of pragmas into the Fortran output file.\n"
00143            "-FLIST:emit_pfetch\n"
00144            "\tEmit comments to indicate prefetch instructions.\n"
00145            "-FLIST:emit_linedir\n"
00146            "\tEmit #line directives to map the generated statements back to\n"
00147            "\tthe corresponding original source statements.\n"
00148            "-FLIST:src_file=<Src_File_Name>\n"
00149            "\tThe name of the original source program.  When not given,\n"
00150            "\tthe <Src_File_Name> is derived from the <Whirl_File_Name>.\n"
00151            "-FLIST:ftn_file=<Ftn_OutFile_Name>\n"
00152            "\tThe file into which program units will be emitted.  When not\n"
00153            "\tgiven, <Ftn_OutFile_Name> is derived from <Src_File_Name>.\n"
00154            "-FLIST:loc_file=<Loc_OutFile_Name>\n"
00155            "\tThe file for emission of a mapping from positions in the\n"
00156            "\tsource to corresponding positions in the original source\n"
00157            "\tfile.  Without this option, no such file is created.\n"
00158            "\n");
00159    fprintf(stderr,
00160            "Compile the generated <C_File_Name> with \"-dollar -lm\".  When\n"
00161            "-FLIST:ftn, then compile the generated <C_File_Name> with\n"
00162            "\"-D_FORTRAN2C -dollar -lftn\" and possibly other \"-l\" options\n"
00163            "to account for libraries referenced in the source.\n"
00164            "\n");
00165    fprintf(stderr,
00166            "The generated source will employ \"tab\" formatting, and\n"
00167            "identifiers may have the \'\$\' character in their names,\n"
00168            "so always compile with the \"-dollar\" option.\n"
00169            "\n");
00170 } /* Usage */
00171 
00172 
00173 /* ====================================================================
00174  *
00175  * Process_Common_Options
00176  *
00177  * Process the Common_Option_Groups, ignoring all other options.
00178  *
00179  * ====================================================================
00180  */
00181 
00182 static void
00183 Process_Common_Options(INT argc,     /* Number of command line arguments */
00184                        char *argv[], /* Array of command line arguments */
00185                        char *envp[]) /* Array of environment pointers */
00186 {
00187    INT argidx;
00188    
00189    for (argidx = 1; argidx < argc; argidx++ )
00190    {
00191       if (argv[argidx] != NULL)
00192       {
00193          if (argv[argidx][0] == '-' )
00194          {
00195             (void)Process_Command_Line_Group(&argv[argidx][1], 
00196                                              Common_Option_Groups);
00197          }
00198       }
00199    }
00200 } /* Process_Common_Options */
00201 
00202 
00203 /* ====================================================================
00204  *
00205  * Local_Terminate
00206  *
00207  * Do any necessary cleanup at the start of Terminate().
00208  *
00209  * ====================================================================
00210  */
00211 
00212 void
00213 Local_Terminate ( void )
00214 {
00215   /* Close and delete whirl2f specific files before calling Terminate() */
00216   W2F_Fini();
00217 }
00218 
00219 
00220 /* ====================================================================
00221  *
00222  * Get_Irb_File_Name
00223  *
00224  * Process the command-line arguments to get the input file name,
00225  * and set Irb_File_Name to this name.  Return the number of
00226  * input file-name arguments found on this command-line (where
00227  * Irb_File_Name will be set to the first one encountered).
00228  *
00229  * ====================================================================
00230  */
00231 
00232 static INT32
00233 Get_Irb_File_Name(INT argc, char *argv[])
00234 {
00235    /* Find the WHIRL input file-name.  There should only be one. */
00236    #define MAX_FNAME_LENGTH 256-7 /* allow for suffix ".W2F.c\0" */
00237    static char filename[MAX_FNAME_LENGTH+7];
00238    INT32       argidx;
00239    INT32       src_count = 0;
00240    
00241    for (argidx = 1; argidx < argc; argidx++)
00242    {
00243       /* Null argument => end of list: */
00244       if (argv[argidx][0] == '-' ) 
00245       {
00246          /* Ignore regular options */
00247       }
00248       else if (src_count > 0)
00249          src_count += 1;
00250       else if (src_count == 0) /* Presumably a WHIRL input file name */
00251       {
00252          /* Copy the original file-name to a static buffer */
00253          Irb_File_Name = argv[argidx];
00254          if (strlen(Irb_File_Name) > MAX_FNAME_LENGTH)
00255          {
00256             Irb_File_Name = strncpy(filename, Irb_File_Name, MAX_FNAME_LENGTH);
00257             filename[MAX_FNAME_LENGTH] = '\0';
00258             fprintf(stderr, 
00259                     "WARNING: input file-name truncated to "
00260                     "(max=%d chars): \"%s\"\n",
00261                     MAX_FNAME_LENGTH, Irb_File_Name);
00262          }
00263          else
00264             Irb_File_Name = strcpy(filename, Irb_File_Name);
00265 
00266          src_count = 1;
00267       } /* If source-file argument */
00268    } /* While not found */
00269    return src_count;
00270 } /* Get_Irb_File_Name */
00271 
00272 
00273 /* ====================================================================
00274  *
00275  * main
00276  *
00277  * Main entry point and driver for the whirl2c translator.
00278  *
00279  * ====================================================================
00280  */
00281 
00282 INT
00283 main (INT argc,       /* Number of command line arguments */
00284       char *argv[],   /* Array of command line arguments */
00285       char *envp[])   /* Array of environment pointers */
00286 {
00287    WN          *pu;
00288    INT32        ecount = 0, wcount = 0, local_ecount = 0, local_wcount = 0;
00289    INT32        inp_file_count;
00290    PU_Info     *pu_tree, *current_pu;
00291 
00292    /* Here are things that every process driver should do as soon as
00293    * possible upon start-up.
00294    */
00295    MEM_Initialize();
00296    Init_Error_Handler(10);
00297    Set_Error_Line(ERROR_LINE_UNKNOWN);
00298    Set_Error_Phase("Whirl2f");
00299    Set_Error_File(NULL); /* Use stderr */
00300 
00301    Preconfigure(); /* Setup target and host specific info before flags */
00302    Process_Common_Options(argc, argv, envp); /* Common options */
00303    Configure();    /* Setup target and host specific info after flags */
00304 
00305    Init_Operator_To_Opcode_Table();
00306     
00307    /* Process the source files */
00308    inp_file_count = Get_Irb_File_Name(argc, argv);
00309    if (inp_file_count == 0)
00310    {
00311       Usage(argv[0]);
00312       fprintf(stderr, "ERROR: missing input file on command-line\n");
00313    }
00314    else if (inp_file_count > 1)
00315    {
00316       Usage(argv[0]);
00317       fprintf(stderr, "ERROR: too many input files on command-line\n");
00318    }
00319    else
00320    {
00321       /* Get the global symbol table, the string table, the constant table,
00322        * and the initialization table, before initializing the translation
00323        * to C.
00324        */
00325       (void)Open_Input_Info(Irb_File_Name);
00326       pu_tree = Read_Global_Info(NULL);
00327       W2F_Process_Command_Line(argc, argv, argc, argv);
00328       W2F_Outfile_Init();
00329 
00330       /* Loop thru all the PUs */
00331       for (current_pu = pu_tree;
00332            current_pu != NULL;
00333            current_pu = PU_Info_next (current_pu)) {
00334 
00335          MEM_POOL_Push (MEM_pu_nz_pool_ptr);
00336          Read_Local_Info (MEM_pu_nz_pool_ptr, current_pu);
00337          pu = PU_Info_tree_ptr(current_pu);
00338 
00339 //some works on IR may generate new PUs and discard some old ones---fzhao
00340          if (PU_need_unparsed(pu)){
00341              W2F_Outfile_Translate_Pu(pu); 
00342              if (PU_Info_child(current_pu)) {
00343                  fprintf(stderr, "WARNING: ignoring nested procedures in \"%s\"\n",
00344                     ST_name(PU_Info_proc_sym(current_pu)));
00345               }
00346          }
00347 
00348          Free_Local_Info (current_pu);
00349          MEM_POOL_Pop (MEM_pu_nz_pool_ptr);
00350       } /* for each PU */
00351 
00352       W2F_Outfile_Fini();
00353       Cleanup_Files(TRUE, FALSE);
00354    }
00355    
00356    /* If we've seen errors, note them and terminate: */
00357    if (Get_Error_Count(&local_ecount, &local_wcount))
00358    {
00359       ecount += local_ecount;
00360       wcount += local_wcount;
00361    }
00362    if ( ecount > 0 )
00363    {
00364       Local_Terminate();
00365       Terminate(1);
00366    }
00367    
00368    exit(0);
00369 } /* main */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines