Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
whirl2c.c
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 (whirl2c), which parses the command-
00046  *   line options and translates an intermediate Very-High or 
00047  *   High level WHIRL file into C.  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  *      whirl2c [-CLIST:<opts>] {<Whirl_File_Name>}
00053  *
00054  *      <opts> ::= <single_opt>[:<opts>]
00055  *      <single_opt> ::= show |
00056  *                       no_pragmas |
00057  *                       emit_adims | 
00058  *                       emit_pfetch | 
00059  *                       emit_linedir |
00060  *                       ftn |
00061  *                       src_file=<Src_File_Name> |
00062  *                       doth_file=<Header_OutFile_Name> |
00063  *                       dotc_file=<C_OutFile_Name> |
00064  *                       loc_file=<LocationMap_OutFile_Name>
00065  *
00066  *   Note that the <Whirl_File_Name> is a mandatory option, and in
00067  *   its abscence a message about usage will be emitted.  If the 
00068  *   <Src_File_Name> is not given it will be derived from the
00069  *   <Whirl_File_Name>.  The output file-names will be derived
00070  *   from the <Src_File_Name>, unless they are explicitly given.
00071  *
00072  * ====================================================================
00073  * ====================================================================
00074  */
00075 #ifdef _KEEP_RCS_ID
00076 #endif /* _KEEP_RCS_ID */
00077 
00078 #include <elf.h>              /* for wn.h */
00079 #include "whirl2c_common.h"
00080 #include "glob.h"             /* Irb_File_Name */
00081 #include "flags.h"            /* for OPTION_GROUP */
00082 #include "pu_info.h"          /* For PU_Info */
00083 #include "ir_bread.h"         /* Binary WHIRL reader */
00084 #include "err_host.tab"       /* Include the error tables in the driver */
00085 
00086 #include "pu_info.h"          /* For PU_Info */
00087 #include "file_util.h"        /* for New_Extension () */
00088 #include "wn_lower.h"         /* for WN_Lower() */
00089 
00090 
00091 /* ====================================================================
00092  *
00093  * Local data.
00094  *
00095  * ====================================================================
00096  */
00097 
00098 /* Default file extensions: */
00099 #define C_FILE_EXTENSION ".c" /* C file */
00100 #define IRB_FILE_EXTENSION ".B"  /* WHIRL file */
00101 
00102 
00103 /* ====================================================================
00104  *
00105  * Usage
00106  *
00107  * Give a warning about the usage of this tool, in terms of the
00108  * command-line options it accepts.
00109  *
00110  * ====================================================================
00111  */
00112 
00113 static void
00114 Usage (char *progname)
00115 {
00116    fprintf(stderr, 
00117            "USAGE: in EBNF notation, where '|' indicates choice and '['\n"
00118            "indicates an optional item:\n"
00119            "\n"
00120            "\t%s [-CLIST:<opts>] [-TARG:<t>] [-TENV:<e>] <Whirl_File_Name>\n"
00121            "\n"
00122            "\t<opts> ::= <single_opt>[:<opts>]\n"
00123            "\n"
00124            "The <Whirl_File_Name> is a mandatory command-line argument.\n"
00125            "We recommend always using the common option -TARG:abi=[32|64].\n" 
00126            "\n",
00127            progname);
00128    
00129    fprintf(stderr,
00130            "Each -CLIST:<single_opt> is described below:\n"
00131            "\n"
00132            "-CLIST:show\n"
00133            "\tIndicate the input/output file-names to stderr.\n"
00134            "-CLIST:no_pragmas\n"
00135            "\tTurn off emission of pragmas into the C output file.\n"
00136            "-CLIST:emit_adims\n"
00137            "\tComment multi-dimensional array indexing expressions to\n"
00138            "\tindicate which subexpression denotes which dimension.\n"
00139            "-CLIST:emit_pfetch\n"
00140            "\tEmit comments to indicate prefetch instructions.\n"
00141            "-CLIST:emit_linedir\n"
00142            "\tEmit #line directives to map the generated statements back to\n"
00143            "\tthe corresponding original source statements.\n"
00144            "-CLIST:ftn\n"
00145            "\tThe intermediate is for a Fortran source program; intrinsics\n"
00146            "\tand IO-statements must be lowered for translation to C.\n"
00147            "-CLIST:src_file=<Src_File_Name>\n"
00148            "\tThe name of the original source program.  When not given,\n"
00149            "\tthe <Src_File_Name> is derived from the <Whirl_File_Name>.\n"
00150            "-CLIST:doth_file=<H_File_Name>\n"
00151            "\tThe file into which file-level declarations will be emitted.\n"
00152            "\tWhen not given, <H_File_Name> is derived from <Src_File_Name>.\n"
00153            "-CLIST:dotc_file=<C_File_Name>\n"
00154            "\tThe file into which program units will be emitted.  When\n"
00155            "\tnot given, <C_File_Name> is derived from <Src_File_Name>.\n"
00156            "-CLIST:loc_file=<Loc_File_Name>\n"
00157            "\tThe file for emission of a mapping from positions in the\n"
00158            "\tsource to corresponding positions in the original source\n"
00159            "\tfile.  Without this option, no such file is created.\n"
00160            "\n");
00161    fprintf(stderr,
00162            "Compile the generated <C_File_Name> with \"-dollar -lm\".  When\n"
00163            "-CLIST:ftn, then compile the generated <C_File_Name> with\n"
00164            "\"-D_FORTRAN2C -dollar -lftn\" and possibly other \"-l\" options\n"
00165            "to account for libraries referenced in the source.\n"
00166            "\n");
00167 } /* Usage */
00168 
00169 
00170 /* ====================================================================
00171  *
00172  * Process_Common_Options
00173  *
00174  * Process the Common_Option_Groups, ignoring all other options.
00175  *
00176  * ====================================================================
00177  */
00178 
00179 static void
00180 Process_Common_Options(INT argc,     /* Number of command line arguments */
00181                        char *argv[], /* Array of command line arguments */
00182                        char *envp[]) /* Array of environment pointers */
00183 {
00184    INT argidx;
00185    
00186    for (argidx = 1; argidx < argc; argidx++ )
00187    {
00188       if (argv[argidx] != NULL)
00189       {
00190          if (argv[argidx][0] == '-' )
00191          {
00192             (void)Process_Command_Line_Group(&argv[argidx][1], 
00193                                              Common_Option_Groups);
00194          }
00195       }
00196    }
00197 } /* Process_Common_Options */
00198 
00199 
00200 /* ====================================================================
00201  *
00202  * Local_Terminate
00203  *
00204  * Do any necessary cleanup at the start of Terminate().
00205  *
00206  * ====================================================================
00207  */
00208 
00209 void
00210 Local_Terminate ( void )
00211 {
00212   /* Close and delete whirl2c specific files before calling Terminate() */
00213   W2C_Fini(/*FALSE*//*emit_global_decls*/);
00214 }
00215 
00216 
00217 /* ====================================================================
00218  *
00219  * Get_Irb_File_Name
00220  *
00221  * Process the command-line arguments to get the input file name,
00222  * and set Irb_File_Name to this name.  Return the number of
00223  * input file-name arguments found on this command-line (where
00224  * Irb_File_Name will be set to the first one encountered).
00225  *
00226  * ====================================================================
00227  */
00228 
00229 static INT32
00230 Get_Irb_File_Name(INT argc, char *argv[])
00231 {
00232    /* Find the WHIRL input file-name.  There should only be one. */
00233    #define MAX_FNAME_LENGTH 256-7 /* allow for suffix ".w2c.c\0" */
00234    static char filename[MAX_FNAME_LENGTH+7];
00235    const char *fname;
00236    INT32       argidx;
00237    INT32       src_count = 0;
00238    
00239    for (argidx = 1; argidx < argc; argidx++)
00240    {
00241       /* Null argument => end of list: */
00242       if (argv[argidx][0] == '-' ) 
00243       {
00244          /* Ignore regular options */
00245       }
00246       else if (src_count > 0)
00247          src_count += 1;
00248       else if (src_count == 0) /* Presumably a WHIRL input file name */
00249       {
00250          /* Copy the original file-name to a static buffer */
00251          Irb_File_Name = argv[argidx];
00252          if (strlen(Irb_File_Name) > MAX_FNAME_LENGTH)
00253          {
00254             Irb_File_Name = strncpy(filename, Irb_File_Name, MAX_FNAME_LENGTH);
00255             filename[MAX_FNAME_LENGTH] = '\0';
00256             fprintf(stderr, 
00257                     "WARNING: input file-name truncated to "
00258                     "(max=%d chars): \"%s\"\n",
00259                     MAX_FNAME_LENGTH, Irb_File_Name);
00260          }
00261          else
00262             Irb_File_Name = strcpy(filename, Irb_File_Name);
00263 
00264          src_count = 1;
00265       } /* If source-file argument */
00266    } /* While not found */
00267    return src_count;
00268 } /* Get_Irb_File_Name */
00269 
00270 
00271 /* ====================================================================
00272  *
00273  * main
00274  *
00275  * Main entry point and driver for the whirl2c translator.
00276  *
00277  * ====================================================================
00278  */
00279 
00280 INT
00281 main (INT argc,       /* Number of command line arguments */
00282       char *argv[],   /* Array of command line arguments */
00283       char *envp[])   /* Array of environment pointers */
00284 {
00285    WN          *pu;
00286    INT32        ecount = 0, wcount = 0, local_ecount = 0, local_wcount = 0;
00287    INT32        inp_file_count;
00288    PU_Info     *pu_tree, *current_pu;
00289 
00290    /* Here are things that every process driver should do as soon as
00291    * possible upon start-up.
00292    */
00293    MEM_Initialize();
00294    Init_Error_Handler(10);
00295    Set_Error_Line(ERROR_LINE_UNKNOWN);
00296    Set_Error_Phase("Whirl2c");
00297    Set_Error_File(NULL); /* Use stderr */
00298 
00299    Preconfigure(); /* Setup target and host specific info before flags */
00300    Process_Common_Options(argc, argv, envp); /* Common options */
00301    Configure();    /* Setup target and host specific info after flags */
00302 
00303    Init_Operator_To_Opcode_Table();
00304     
00305    /* Process the source files */
00306    inp_file_count = Get_Irb_File_Name(argc, argv);
00307    if (inp_file_count == 0)
00308    {
00309       Usage(argv[0]);
00310       fprintf(stderr, "ERROR: missing input file on command-line\n");
00311    }
00312    else if (inp_file_count > 1)
00313    {
00314       Usage(argv[0]);
00315       fprintf(stderr, "ERROR: too many input files on command-line\n");
00316    }
00317    else
00318    {
00319       /* Get the global symbol table, the string table, the constant table,
00320        * and the initialization table, before initializing the translation
00321        * to C.
00322        */
00323       (void)Open_Input_Info(Irb_File_Name);
00324       /* Costin  */
00325       Initialize_Symbol_Tables (FALSE);
00326       New_Scope (GLOBAL_SYMTAB, Malloc_Mem_Pool, FALSE);
00327 
00328       /* end C*/
00329 
00330       pu_tree = Read_Global_Info(NULL);
00331       Initialize_Special_Global_Symbols ();
00332 
00333       W2C_Process_Command_Line(argc, argv, argc, argv);
00334       /* W2C_Init(); */
00335       W2C_Outfile_Init(TRUE); 
00336       /* Loop thru all the PUs */
00337       for (current_pu = pu_tree;
00338            current_pu != NULL;
00339            current_pu = PU_Info_next (current_pu)) {
00340 
00341          Current_PU_Info = current_pu;
00342          MEM_POOL_Push (MEM_pu_nz_pool_ptr);
00343          Read_Local_Info (MEM_pu_nz_pool_ptr, current_pu);
00344          pu = PU_Info_tree_ptr(current_pu);
00345 
00346          W2C_Outfile_Translate_Pu(pu, TRUE/*emit_global_decls*/);
00347 
00348          if (PU_Info_child(current_pu)) {
00349             fprintf(stderr, "WARNING: ignoring nested procedures in \"%s\"\n",
00350                     ST_name(PU_Info_proc_sym(current_pu)));
00351          }
00352 
00353          Free_Local_Info (current_pu);
00354          MEM_POOL_Pop (MEM_pu_nz_pool_ptr);
00355       } /* for each PU */
00356 
00357        W2C_Outfile_Fini(TRUE/*emit_global_decls*/);
00358       Cleanup_Files(TRUE, FALSE);
00359    }
00360    
00361    /* If we've seen errors, note them and terminate: */
00362    if (Get_Error_Count(&local_ecount, &local_wcount))
00363    {
00364       ecount += local_ecount;
00365       wcount += local_wcount;
00366    }
00367    if ( ecount > 0 )
00368    {
00369       Local_Terminate();
00370       Terminate(1);
00371    }
00372    
00373    exit(0);
00374 } /* main */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines