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 {"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
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,
00077 const char *ext)
00078 {
00079 INT16 nlen = strlen(name);
00080 INT16 elen = strlen(ext);
00081
00082
00083 if ( elen > nlen ) return FALSE;
00084
00085
00086 return ( strcmp ( &name[nlen-elen], ext ) == 0 );
00087 }
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 }
00164
00165
00166 int
00167 main (INT argc,
00168 const char *const argv[],
00169 const char *const envp[])
00170 {
00171
00172
00173
00174
00175
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;
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;
00203
00204 env = getenv (libpath[i]);
00205
00206 if (env) {
00207 len += strlen (env) + 1;
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 }
00226
00227
00228
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
00246
00247
00248
00249 argidx = argc-1;
00250 while (argidx > 0)
00251 {
00252 if (new_argv[argidx][0] != '-' &&
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
00260
00261
00262
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;
00272 }
00273 argidx--;
00274 }
00275 }
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 }
00286
00287