00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <unistd.h>
00044 #include <string.h>
00045 #include <limits.h>
00046 #include <errno.h>
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 {
00055 #if defined (__MACH__) && defined(__APPLE__)
00056 "DYLD_LIBRARY_PATH",
00057 #elif defined(_AIX)
00058 "LIBPATH",
00059 #else
00060 "LD_LIBRARY_PATH",
00061 #endif
00062 "LD_LIBRARYN32_PATH",
00063 "LD_LIBRARY64_PATH"
00064 };
00065
00066 static const char * const errstring = "%s: can't allocate memory\n";
00067 static const char *program_name;
00068
00069
00070 void
00071 error(const char *format, ...)
00072 {
00073 va_list args;
00074 va_start (args, format);
00075 fprintf(stderr, "%s ERROR: ", program_name);
00076 vfprintf(stderr, format, args);
00077 fprintf(stderr, "\n");
00078 va_end (args);
00079 }
00080
00081
00082 static BOOL
00083 Has_Extension (const char *name,
00084 const char *ext)
00085 {
00086 INT16 nlen = strlen(name);
00087 INT16 elen = strlen(ext);
00088
00089
00090 if ( elen > nlen ) return FALSE;
00091
00092
00093 return ( strcmp ( &name[nlen-elen], ext ) == 0 );
00094 }
00095
00096
00097 static void
00098 Usage (const char *progname)
00099 {
00100 fprintf(stderr,
00101 "USAGE: in EBNF notation, where '|' indicates choice and '['\n"
00102 "indicates an optional item:\n"
00103 "\n"
00104 "\t%s [-CLIST:<opts>] [-TARG:<t>] [-TENV:<e>] <inp_files>\n"
00105 "\n"
00106 "\t<inp_files> ::= [-fB,<Whirl_File_Name>] <File_Name>\n"
00107 "\t<opts> ::= <single_opt>[:<opts>]\n"
00108 "\n"
00109 "We recommend always using the common option -TARG:abi=[32|64].\n"
00110 "\n"
00111 "The <File_Name> is a mandatory command-line argument, which may\n"
00112 "denote either a (Fortran) source filename or a WHIRL file.\n"
00113 "In the abscense of a -fB option, the <Whirl_File_Name> will be\n"
00114 "derived from the <File_Name>\n"
00115 "\n",
00116 progname);
00117
00118 fprintf(stderr,
00119 "Each -CLIST:<single_opt> is described below:\n"
00120 "\n"
00121 "-CLIST:show\n"
00122 "\tIndicate the input/output file-names to stderr.\n"
00123 "-CLIST:linelength=<n>\n"
00124 "\tSpecifies an upper limit on the number of characters we allow\n"
00125 "\ton each line in the output file\n"
00126 "-CLIST:emit_adims\n"
00127 "\tComment multi-dimensional array indexing expressions to\n"
00128 "\tindicate which subexpression denotes which dimension.\n"
00129 "-CLIST:emit_pfetch\n"
00130 "\tEmit comments to indicate prefetch instructions.\n"
00131 "-CLIST:emit_regions\n"
00132 "\tEmit all regions, whether user defined or compiler generated.\n"
00133 "\tThe default is to only emit user-defined regions.\n"
00134 "-CLIST:emit_linedirs\n"
00135 "\tEmit #line directives to map the generated statements back to\n"
00136 "\tthe corresponding original source statements.\n"
00137 "-CLIST:emit_nested_pu\n"
00138 "\tEmit code for PUs nested within other PUs. Currently, the\n"
00139 "\tsymbol-table context will not be correctly set up for this and\n"
00140 "\tthe nested PU will be emitted immediately after the parent PU.\n"
00141 "\tNote that this will also lower MP constructs.\n"
00142 "-CLIST:emit_frequency\n"
00143 "\tEmit feedback frequency numbers for each statement. The\n"
00144 "\tfrequency information will be emitted using the comment\n"
00145 "\tnotation, and will apply to statement up till the previous\n"
00146 "\tfrequency information seen.\n"
00147 "-CLIST:ftn\n"
00148 "\tThe intermediate is for a Fortran source program; intrinsics\n"
00149 "\tand IO-statements must be lowered for translation to C.\n"
00150 "-CLIST:src_file=<Src_File_Name>\n"
00151 "\tThe name of the original source program. When not given,\n"
00152 "\tthe <Src_File_Name> is derived from the <File_Name>.\n"
00153 "-CLIST:doth_file=<H_File_Name>\n"
00154 "\tThe file into which file-level declarations will be emitted.\n"
00155 "\tWhen not given, <H_File_Name> is derived from <Src_File_Name>.\n"
00156 "-CLIST:dotc_file=<C_File_Name>\n"
00157 "\tThe file into which program units will be emitted. When\n"
00158 "\tnot given, <C_File_Name> is derived from <Src_File_Name>.\n"
00159 "-CLIST:loc_file=<Loc_File_Name>\n"
00160 "\tThe file for emission of a mapping from positions in the\n"
00161 "\tsource to corresponding positions in the original source\n"
00162 "\tfile. Without this option, no such file is created.\n"
00163 "\n");
00164 fprintf(stderr,
00165 "Compile the generated <C_File_Name> with \"-dollar -lm\". When\n"
00166 "-CLIST:ftn, then compile the generated <C_File_Name> with\n"
00167 "\"-D_FORTRAN2C -dollar -lftn\" and possibly other \"-l\" options\n"
00168 "to account for libraries referenced in the source.\n"
00169 "\n");
00170 }
00171
00172
00173 int
00174 main (INT argc,
00175 const char *const argv[],
00176 const char *const envp[])
00177 {
00178
00179
00180
00181
00182
00183
00184 register char **new_argv;
00185 register char *p;
00186 register const char *env;
00187 register INT i, len;
00188 register INT argidx;
00189 register BOOL dash_fB_option = FALSE;
00190 char *newlibpath[3];
00191 char *TOOLRT;
00192
00193 program_name = argv[0];
00194
00195 if (argc == 1)
00196 {
00197 Usage(argv[0]);
00198 exit(RC_NORECOVER_USER_ERROR);
00199 }
00200 #if defined(TARG_IA64)
00201 TOOLRT = getenv("TOOLROOT");
00202 strcpy (path, TOOLRT);
00203 strcat (path, "/bin");
00204 #else
00205 strcpy (path, argv[0]);
00206 #endif
00207
00208 if (p = strrchr(path, '/'))
00209 p[0] = 0;
00210 else
00211 strcpy (path, ".");
00212
00213 for (i = 0; i<3; i++)
00214 {
00215 len = strlen (path) + 1;
00216 len += strlen (libpath[i]) + 1;
00217
00218 env = getenv (libpath[i]);
00219
00220 if (env) {
00221 len += strlen (env) + 1;
00222
00223 newlibpath[i] = (char *) malloc (len);
00224 if (newlibpath[i] == 0) {
00225 fprintf (stderr, errstring, argv[0]);
00226 exit(RC_NORECOVER_USER_ERROR);
00227 }
00228
00229 sprintf (newlibpath[i], "%s=%s:%s", libpath[i], env, path);
00230 } else {
00231 newlibpath[i] = (char *) malloc (len);
00232 if (newlibpath[i] == 0) {
00233 fprintf (stderr, errstring, argv[0]);
00234 exit(RC_NORECOVER_USER_ERROR);
00235 }
00236
00237 sprintf (newlibpath[i], "%s=%s", libpath[i], path);
00238 }
00239 }
00240
00241
00242
00243
00244 new_argv = (char **)malloc((argc+3)*sizeof(char *));
00245 for (argidx = 0; argidx < argc; argidx++)
00246 {
00247 new_argv[argidx] = (char *)malloc(strlen(argv[argidx]) + 1);
00248 new_argv[argidx] = strcpy(new_argv[argidx], argv[argidx]);
00249 if (new_argv[argidx][0] == '-' &&
00250 new_argv[argidx][1] == 'f' &&
00251 new_argv[argidx][2] == 'B')
00252 {
00253 dash_fB_option = TRUE;
00254 }
00255 }
00256
00257 if (!dash_fB_option)
00258 {
00259
00260
00261
00262
00263 argidx = argc-1;
00264 while (argidx > 0)
00265 {
00266 if (new_argv[argidx][0] != '-' &&
00267 (Has_Extension(new_argv[argidx], ".B") ||
00268 Has_Extension(new_argv[argidx], ".I") ||
00269 Has_Extension(new_argv[argidx], ".N") ||
00270 Has_Extension(new_argv[argidx], ".O") ||
00271 Has_Extension(new_argv[argidx], ".o")))
00272 {
00273
00274
00275
00276
00277
00278 dash_fB_option = TRUE;
00279 new_argv[argc] = (char *)malloc(strlen(new_argv[argidx]) + 5);
00280 (void)strcpy(new_argv[argc], "-fB,");
00281 (void)strcpy(&new_argv[argc][4], new_argv[argidx]);
00282 argc++;
00283
00284 new_argv[argidx][strlen(new_argv[argidx])-1] = 'c';
00285 argidx = 1;
00286 }
00287 argidx--;
00288 }
00289 }
00290
00291 #if defined(__MACH__) && defined(__APPLE__)
00292
00293 new_argv[argc] = (char *)malloc(20);
00294 strcpy(new_argv[argc],"-run-w2c");
00295 new_argv[argc+1] = NULL;
00296 # else
00297 new_argv[argc] = NULL;
00298 #endif
00299
00300 #if defined(TARG_IA64)
00301 strcat (newlibpath[0],"/lib/gcc-lib/ia64-open64-linux/4.0/");
00302 #endif
00303
00304 for (i = 0; i<3; i++)
00305 putenv (newlibpath[i]);
00306
00307 strcat (path, "/whirl2c_be");
00308
00309
00310 #if defined(__MACH__) && defined(__APPLE__)
00311
00312
00313 char abspath[PATH_MAX];
00314 if(!realpath(path,abspath)) perror("realpath");
00315 new_argv[0] = abspath;
00316 strcpy(path, abspath);
00317 #endif
00318
00319 execv (path, new_argv);
00320 error("%s: fail to execute %s: %s.\n", argv[0], new_argv[0], strerror(errno));
00321 exit(RC_SYSTEM_ERROR);
00322 }
00323
00324