Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
ir_gdar.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  *  20-Jun-97 - Original Version
00042  *
00043  * Description:
00044  *
00045  *   This program is just a stand-alone driver for the GDAR capability.
00046  *   The GDAR option is built into each of the front-ends so that this
00047  *   program is needed when you only have *.B files to work with.
00048  *
00049  *      ir_gdar [-v] [-o <Whirl_File_Name_out>] -g <GDAR_File_Name>
00050  *                                                      <Whirl_File_Name>
00051  *
00052  *   Note that the <Whirl_File_Name> is a mandatory argument, and in
00053  *   its absence a message about usage will be emitted.  If the program
00054  *   is written to output a modified WHIRL file then it will either be
00055  *   written to the optional -o argument or to the original WHIRL file.
00056  *
00057  * ====================================================================
00058  * ====================================================================
00059  */
00060 
00061 #include <elf.h>
00062 #include <stdio.h>
00063 #include <sys/types.h>
00064 #include <sys/stat.h>
00065 #include <search.h>
00066 #include <string.h>
00067 #include <stdlib.h>
00068 #include <unistd.h>
00069 
00070 #include "wn.h"
00071 #include "stab.h"
00072 #include "aux_stab.h"
00073 #include "irbdata.h"
00074 #include "wintrinsic.h"
00075 #include "glob.h"
00076 #include "pu_info.h"
00077 #include "ir_bread.h"
00078 #include "ir_bwrite.h"
00079 #include "file_util.h"
00080 #include "gdar.h"
00081 
00082 
00083 /* ====================================================================
00084  *
00085  * Local data.
00086  *
00087  * ====================================================================
00088  */
00089 
00090 #define MAX_FNAME_LENGTH 256
00091 
00092 char filename_in[MAX_FNAME_LENGTH];
00093 char filename_out[MAX_FNAME_LENGTH];
00094 char gdar_filename[MAX_FNAME_LENGTH];
00095 char temp_filename[MAX_FNAME_LENGTH+16];
00096 
00097 char *GDAR_File_Name = NULL;
00098 char *Irb_File_Name_out = NULL;
00099 
00100 struct stat statbuf;
00101 
00102 BOOL gflag = FALSE;
00103 BOOL oflag = FALSE;
00104 BOOL vflag = FALSE;
00105 
00106 
00107 /* ====================================================================
00108  *
00109  * Usage
00110  *
00111  * Give a warning about the usage of this tool, in terms of the
00112  * command line options it accepts.
00113  *
00114  * ====================================================================
00115  */
00116 
00117 static void
00118 Usage (void)
00119 {
00120    fprintf (stderr, 
00121             "USAGE:\n"
00122             "\n"
00123             "\t [-v] [-o <Whirl_File_Name_out>] -g <GDAR_File_Name> <Whirl_File_Name>\n"
00124             "\n"
00125             "The optional -v flag controls verbose trace messages.\n"
00126             "The optional -o flag is used to specify an alternate out file.\n"
00127             "The -g <GDAR_File_Name> is a mandatory command line argument.\n"
00128             "The <Whirl_File_Name> is a mandatory command line argument.\n"
00129             "\n"
00130             );
00131    
00132 } /* Usage */
00133 
00134 
00135 /* ====================================================================
00136  *
00137  * Get_Irb_File_Name
00138  *
00139  * Process the command line arguments to get the input file name,
00140  * the optional output file name and any other options.  Return the
00141  * number of input filename arguments found on this command line
00142  * (where Irb_File_Name will be set to the first one encountered).
00143  *
00144  * ====================================================================
00145  */
00146 
00147 static INT32
00148 Get_Irb_File_Name (INT argc, char *argv[])
00149 {
00150    /* Find the WHIRL input filename.  There should only be one. */
00151    char *fname;
00152    INT32       argidx;
00153    INT32       src_count = 0;
00154    
00155    for (argidx = 1; argidx < argc; argidx++)
00156    {
00157       /* Null argument => end of list: */
00158       if (argv[argidx][0] == '-' ) 
00159       {
00160          /* Process regular options */
00161         if (strcmp (argv[argidx], "-o") == 0) {
00162           oflag = TRUE;
00163           fname = argv[++argidx];
00164           if (strlen(fname) > MAX_FNAME_LENGTH)
00165           {
00166             Irb_File_Name_out = strncpy (filename_out, fname, MAX_FNAME_LENGTH);
00167             filename_out[MAX_FNAME_LENGTH] = '\0';
00168             fprintf (stderr, 
00169                      "WARNING: output filename truncated to "
00170                      "(max=%d chars): \"%s\"\n",
00171                      MAX_FNAME_LENGTH, fname);
00172           }
00173           else
00174             Irb_File_Name_out = strcpy (filename_out, fname);
00175         } else if (strcmp (argv[argidx], "-g") == 0) {
00176           gflag = TRUE;
00177           fname = argv[++argidx];
00178           if (strlen(fname) > MAX_FNAME_LENGTH)
00179           {
00180             GDAR_File_Name = strncpy (gdar_filename, fname, MAX_FNAME_LENGTH);
00181             gdar_filename[MAX_FNAME_LENGTH] = '\0';
00182             fprintf (stderr, 
00183                      "WARNING: GDAR filename truncated to "
00184                      "(max=%d chars): \"%s\"\n",
00185                      MAX_FNAME_LENGTH, fname);
00186           }
00187           else
00188             GDAR_File_Name = strcpy (gdar_filename, fname);
00189         } else if (strcmp (argv[argidx], "-v") == 0) {
00190           vflag = TRUE;
00191         } else {
00192           fprintf (stderr,
00193                    "WARNING: unrecognized command option "
00194                    "%s\n",
00195                    argv[argidx]);
00196         }
00197       }
00198       else if (src_count > 0)
00199          src_count += 1;
00200       else if (src_count == 0) /* Presumably a WHIRL input file name */
00201       {
00202          /* Copy the original filename to a static buffer */
00203          fname = argv[argidx];
00204          if (strlen(fname) > MAX_FNAME_LENGTH)
00205          {
00206             Irb_File_Name = strncpy (filename_in, fname, MAX_FNAME_LENGTH);
00207             filename_in[MAX_FNAME_LENGTH] = '\0';
00208             fprintf (stderr, 
00209                      "WARNING: input filename truncated to "
00210                      "(max=%d chars): \"%s\"\n",
00211                      MAX_FNAME_LENGTH, fname);
00212          }
00213          else
00214             Irb_File_Name = strcpy (filename_in, fname);
00215 
00216          src_count = 1;
00217       } /* If source file argument */
00218    } /* While not found */
00219    return src_count;
00220 } /* Get_Irb_File_Name */
00221 
00222 
00223 /* ====================================================================
00224  *
00225  * read_pu
00226  *
00227  * Read each PU.
00228  *
00229  * ====================================================================
00230  */
00231 
00232 static void
00233 read_pu (PU_Info *pu_tree)
00234 {
00235     PU_Info *pu;
00236 
00237     for (pu = pu_tree; pu != NULL; pu = PU_Info_next(pu)) {
00238         Current_PU_Info = pu;
00239         Read_Local_Info (MEM_pu_nz_pool_ptr, pu);
00240 
00241         if (PU_Info_child(pu)) {
00242             read_pu (PU_Info_child(pu));
00243         }
00244     }
00245 }
00246 
00247 
00248 /* ====================================================================
00249  *
00250  * write_pu
00251  *
00252  * Write each PU.
00253  *
00254  * ====================================================================
00255  */
00256 
00257 static void
00258 write_pu (PU_Info *pu_tree)
00259 {
00260     PU_Info *pu;
00261 
00262     for (pu = pu_tree; pu != NULL; pu = PU_Info_next(pu)) {
00263         Current_PU_Info = pu;
00264         Write_PU_Info (pu);
00265     }
00266 }
00267 
00268 
00269 /* ====================================================================
00270  *
00271  * main
00272  *
00273  * Main entry point and driver for the ir_walker program.
00274  *
00275  * ====================================================================
00276  */
00277 
00278 void
00279 main (INT argc,       /* Number of command line arguments */
00280       char *argv[],   /* Array of command line arguments */
00281       char *envp[])   /* Array of environment pointers */
00282 {
00283    WN          *pu;
00284    INT32        inp_file_count;
00285    PU_Info     *pu_tree, *current_pu;
00286 
00287    /* Here are things that every process driver should do as soon as
00288     * possible upon start-up.
00289     */
00290    MEM_Initialize ();
00291    Init_Error_Handler (10);
00292    Set_Error_Line (ERROR_LINE_UNKNOWN);
00293    Set_Error_Phase ("IR Walker");
00294    Set_Error_File (NULL);
00295 
00296    Init_Operator_To_Opcode_Table ();
00297     
00298    /* Process the input file */
00299    inp_file_count = Get_Irb_File_Name (argc, argv);
00300    if (inp_file_count == 0)
00301    {
00302       Usage ();
00303       fprintf (stderr, "ERROR: missing input file on command line\n");
00304    }
00305    else if (inp_file_count > 1)
00306    {
00307       Usage ();
00308       fprintf (stderr, "ERROR: too many input files on command line\n");
00309    }
00310    else if (stat (Irb_File_Name, &statbuf) != 0)
00311    {
00312       fprintf (stderr, "ERROR: input file (%s) does not exist\n",
00313                Irb_File_Name);
00314    }
00315    else if (!gflag)
00316    {
00317       Usage ();
00318       fprintf (stderr, "ERROR: missing GDAR file on command line\n");
00319    }
00320    else if (stat (GDAR_File_Name, &statbuf) != 0)
00321    {
00322       fprintf (stderr, "ERROR: GDAR file (%s) does not exist\n",
00323                GDAR_File_Name);
00324    }
00325    else
00326    {
00327 
00328       /* Setup output file */
00329       if (!oflag)
00330         (void) strcpy (filename_out, filename_in);
00331       (void) sprintf (temp_filename, "%s$%d", filename_out, (INT32) getpid ());
00332       (void) remove (temp_filename);
00333 
00334       /* Get the global symbol table, the string table, the constant table,
00335        * and the initialization table.
00336        */
00337       (void) Open_Input_Info (Irb_File_Name);
00338       pu_tree = Read_Global_Info (NULL);
00339 
00340       /* Open output file */
00341       (void) Open_Output_Info (temp_filename);
00342 
00343       /* Read all the PUs */
00344       read_pu (pu_tree);
00345 
00346       /* Process global symbol table. */
00347       Process_GDAR (GDAR_File_Name, Global_Symtab, &pu_tree);
00348 
00349       /* Write all the PUs */
00350       write_pu (pu_tree);
00351 
00352       /* Finish up output file */
00353       Write_Global_Info (pu_tree);
00354       Close_Output_Info ();
00355       (void) remove (filename_out);
00356       (void) rename (temp_filename, filename_out);
00357 
00358    }
00359    
00360    exit (0);
00361 } /* main */
00362 
00363 
00364 /* Dummy definitions to satisify references from routines that got pulled
00365  * in by the header files but are never called
00366  */
00367 void Signal_Cleanup (INT sig) { }
00368 
00369 char * Host_Format_Parm (INT kind, MEM_PTR parm) { return NULL; }
00370 
00371 INT8 Debug_Level = 0;
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines