Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
whirl2c_main.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  * Dynamically set up the right LD_LIBRARY_PATH before exec'ing the backend
00038  * proper.
00039  */
00040 
00041 #include <stdio.h>                  /* for fprintf () */
00042 #include <stdlib.h>                 /* for getenv() */
00043 #include <unistd.h>                 /* for execv() */
00044 #include <string.h>                 /* for strcpy(), strerror() */
00045 #include <limits.h>                 /* for PATH_MAX */
00046 #include <errno.h>                  /* for errno */
00047 #include <stdarg.h>
00048 #include <cmplrs/rcodes.h>
00049 #include "defs.h"
00050 
00051 
00052 char path[PATH_MAX];
00053 static const char *libpath[3] = 
00054 {"LD_LIBRARY_PATH",
00055  "LD_LIBRARYN32_PATH",
00056  "LD_LIBRARY64_PATH"
00057 };
00058 
00059 static const char * const errstring = "%s: can't allocate memory\n";
00060 static const char *program_name;
00061 
00062 /* Copied from driver/errors.c */
00063 void
00064 error(const char *format, ...)
00065 {
00066         va_list args;
00067         va_start (args, format);
00068         fprintf(stderr, "%s ERROR:  ", program_name);
00069         vfprintf(stderr, format, args);
00070         fprintf(stderr, "\n");
00071         va_end (args);
00072 }
00073 
00074 
00075 static BOOL
00076 Has_Extension (const char *name,  /* The filename to check */
00077                const char *ext)   /* The extension to look for */
00078 {
00079   INT16 nlen = strlen(name);
00080   INT16 elen = strlen(ext);
00081 
00082   /* If ext is longer than name, no chance: */
00083   if ( elen > nlen ) return FALSE;
00084 
00085   /* Otherwise compare the tail of name to ext: */
00086   return ( strcmp ( &name[nlen-elen], ext ) == 0 );
00087 } /* Has_Extension */
00088 
00089 
00090 static void
00091 Usage (const char *progname)
00092 {
00093    fprintf(stderr, 
00094            "USAGE: in EBNF notation, where '|' indicates choice and '['\n"
00095            "indicates an optional item:\n"
00096            "\n"
00097            "\t%s [-CLIST:<opts>] [-TARG:<t>] [-TENV:<e>] <inp_files>\n"
00098            "\n"
00099            "\t<inp_files> ::= [-fB,<Whirl_File_Name>] <File_Name>\n"
00100            "\t<opts> ::= <single_opt>[:<opts>]\n"
00101            "\n"
00102            "We recommend always using the common option -TARG:abi=[32|64].\n"
00103            "\n"
00104            "The <File_Name> is a mandatory command-line argument, which may\n"
00105            "denote either a (Fortran) source filename or a WHIRL file.\n"
00106            "In the abscense of a -fB option, the <Whirl_File_Name> will be\n"
00107            "derived from the <File_Name>\n" 
00108            "\n",
00109            progname);
00110    
00111    fprintf(stderr,
00112            "Each -CLIST:<single_opt> is described below:\n"
00113            "\n"
00114            "-CLIST:show\n"
00115            "\tIndicate the input/output file-names to stderr.\n"
00116            "-CLIST:linelength=<n>\n"
00117            "\tSpecifies an upper limit on the number of characters we allow\n"
00118            "\ton each line in the output file\n"
00119            "-CLIST:emit_adims\n"
00120            "\tComment multi-dimensional array indexing expressions to\n"
00121            "\tindicate which subexpression denotes which dimension.\n"
00122            "-CLIST:emit_pfetch\n"
00123            "\tEmit comments to indicate prefetch instructions.\n"
00124            "-CLIST:emit_regions\n"
00125            "\tEmit all regions, whether user defined or compiler generated.\n"
00126            "\tThe default is to only emit user-defined regions.\n"
00127            "-CLIST:emit_linedirs\n"
00128            "\tEmit #line directives to map the generated statements back to\n"
00129            "\tthe corresponding original source statements.\n"
00130            "-CLIST:emit_nested_pu\n"
00131            "\tEmit code for PUs nested within other PUs.  Currently, the\n"
00132            "\tsymbol-table context will not be correctly set up for this and\n"
00133            "\tthe nested PU will be emitted immediately after the parent PU.\n"
00134            "\tNote that this will also lower MP constructs.\n"
00135            "-CLIST:emit_frequency\n"
00136            "\tEmit feedback frequency numbers for each statement.  The\n"
00137            "\tfrequency information will be emitted using the comment\n"
00138            "\tnotation, and will apply to statement up till the previous\n"
00139            "\tfrequency information seen.\n"
00140            "-CLIST:ftn\n"
00141            "\tThe intermediate is for a Fortran source program; intrinsics\n"
00142            "\tand IO-statements must be lowered for translation to C.\n"
00143            "-CLIST:src_file=<Src_File_Name>\n"
00144            "\tThe name of the original source program.  When not given,\n"
00145            "\tthe <Src_File_Name> is derived from the <File_Name>.\n"
00146            "-CLIST:doth_file=<H_File_Name>\n"
00147            "\tThe file into which file-level declarations will be emitted.\n"
00148            "\tWhen not given, <H_File_Name> is derived from <Src_File_Name>.\n"
00149            "-CLIST:dotc_file=<C_File_Name>\n"
00150            "\tThe file into which program units will be emitted.  When\n"
00151            "\tnot given, <C_File_Name> is derived from <Src_File_Name>.\n"
00152            "-CLIST:loc_file=<Loc_File_Name>\n"
00153            "\tThe file for emission of a mapping from positions in the\n"
00154            "\tsource to corresponding positions in the original source\n"
00155            "\tfile.  Without this option, no such file is created.\n"
00156            "\n");
00157    fprintf(stderr,
00158            "Compile the generated <C_File_Name> with \"-dollar -lm\".  When\n"
00159            "-CLIST:ftn, then compile the generated <C_File_Name> with\n"
00160            "\"-D_FORTRAN2C -dollar -lftn\" and possibly other \"-l\" options\n"
00161            "to account for libraries referenced in the source.\n"
00162            "\n");
00163 } /* Usage */
00164 
00165 
00166 int
00167 main (INT argc,                   /* Number of command line arguments */
00168       const char *const argv[],   /* Array of command line arguments */
00169       const char *const envp[])   /* Array of environment pointers */
00170 {
00171    /* Take the current value for the environment variable LD_LIBRARY_PATH,
00172     * then add in the path where this executable was found, assuming any
00173     * necessary DSOs (be.so and whirl2c.so) are found in the same
00174     * directory.  Then execute "whirl2c_be", also assumed to be
00175     * in the directory where this executable resides.
00176     */
00177     register char      **new_argv;
00178     register char       *p;
00179     register const char *env;
00180     register INT         i, len;
00181     register INT         argidx;
00182     register BOOL        dash_fB_option = FALSE; /* Any -fB option? */
00183     char                *newlibpath[3];
00184 
00185     program_name = argv[0];
00186 
00187     if (argc == 1)
00188     {
00189        Usage(argv[0]);
00190        exit(RC_NORECOVER_USER_ERROR);
00191     }
00192     
00193     strcpy (path, argv[0]);
00194     if (p = strrchr(path, '/'))
00195         p[0] = 0;
00196     else
00197         strcpy (path, ".");
00198 
00199     for (i = 0; i<3; i++)
00200     {
00201        len = strlen (path) + 1;
00202        len += strlen (libpath[i]) + 1;    /* env. variable name plus '=' */
00203 
00204        env = getenv (libpath[i]);
00205 
00206        if (env) {
00207           len += strlen (env) + 1;    /* old path plus ':' */
00208 
00209           newlibpath[i] = (char *) malloc (len);
00210           if (newlibpath[i] == 0) {
00211              fprintf (stderr, errstring, argv[0]);
00212              exit(RC_NORECOVER_USER_ERROR);
00213           }
00214 
00215           sprintf (newlibpath[i], "%s=%s:%s", libpath[i], env, path);
00216        } else {
00217           newlibpath[i] = (char *) malloc (len);
00218           if (newlibpath[i] == 0) {
00219              fprintf (stderr, errstring, argv[0]);
00220              exit(RC_NORECOVER_USER_ERROR);
00221           }
00222 
00223           sprintf (newlibpath[i], "%s=%s", libpath[i], path);
00224        }
00225     } /* For each libpath kind */
00226 
00227     /* Copy the argument list into a new list of strings, with a spare
00228      * element for a missing -fB option.
00229      */
00230     new_argv = (char **)malloc((argc+2)*sizeof(char *));
00231     for (argidx = 0; argidx < argc; argidx++)
00232     {
00233        new_argv[argidx] = (char *)malloc(strlen(argv[argidx]) + 1);
00234        new_argv[argidx] = strcpy(new_argv[argidx], argv[argidx]);
00235        if (new_argv[argidx][0] == '-' &&
00236            new_argv[argidx][1] == 'f' &&
00237            new_argv[argidx][2] == 'B')
00238        {
00239           dash_fB_option = TRUE;
00240        }
00241     }
00242 
00243     if (!dash_fB_option)
00244     {
00245        /* Create a "-fB" option, provided the file-argument (only argument
00246         * not preceded by a '-') represents the WHIRL file if suffixed by
00247         * ".B", ".I", ".N" or ".o".
00248         */
00249        argidx = argc-1;
00250        while (argidx > 0)
00251        {
00252           if (new_argv[argidx][0] != '-' && /* A file argument */
00253               (Has_Extension(new_argv[argidx], ".B") ||
00254                Has_Extension(new_argv[argidx], ".I") ||
00255                Has_Extension(new_argv[argidx], ".N") ||
00256                Has_Extension(new_argv[argidx], ".O") ||
00257                Has_Extension(new_argv[argidx], ".o")))
00258           {
00259              /* A file argument representing the WHIRL input file.  We need
00260               * to change this around a little bit.  Put this filename under
00261               * a "-fB,filename" option and add a new filename with the
00262               * suffix substituted by ".c".
00263               */
00264              dash_fB_option = TRUE;
00265              new_argv[argc] = (char *)malloc(strlen(new_argv[argidx]) + 5);
00266              (void)strcpy(new_argv[argc], "-fB,");
00267              (void)strcpy(&new_argv[argc][4], new_argv[argidx]);
00268              argc++;
00269           
00270              new_argv[argidx][strlen(new_argv[argidx])-1] = 'c';
00271              argidx = 1; /* We are done! */
00272           }
00273           argidx--;
00274        } /*while*/
00275     } /*if (!dash_fB_option)*/
00276     new_argv[argc] = NULL;
00277     
00278     for (i = 0; i<3; i++)
00279        putenv (newlibpath[i]);
00280     strcat (path, "/whirl2c_be");
00281 
00282     execv (path, new_argv);
00283     error("%s: fail to execute %s: %s.\n", argv[0], path, strerror(errno));
00284     exit(RC_SYSTEM_ERROR);
00285 } /* main */
00286 
00287 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines