* config/arm/arm.c (is_load_address): Rename to... (arm_memory_load_p) ... this
[official-gcc.git] / gcc / f / g77spec.c
blob3df506285395e8b20253fc7e6435e4ce3c62b4fe
1 /* Specific flags and argument handling of the Fortran front-end.
2 Copyright (C) 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This file contains a filter for the main `gcc' driver, which is
22 replicated for the `g77' driver by adding this filter. The purpose
23 of this filter is to be basically identical to gcc (in that
24 it faithfully passes all of the original arguments to gcc) but,
25 unless explicitly overridden by the user in certain ways, ensure
26 that the needs of the language supported by this wrapper are met.
28 For GNU Fortran (g77), we do the following to the argument list
29 before passing it to `gcc':
31 1. Make sure `-lg2c -lm' is at the end of the list.
33 2. Make sure each time `-lg2c' or `-lm' is seen, it forms
34 part of the series `-lg2c -lm'.
36 #1 and #2 are not done if `-nostdlib' or any option that disables
37 the linking phase is present, or if `-xfoo' is in effect. Note that
38 a lack of source files or -l options disables linking.
40 This program was originally made out of gcc/cp/g++spec.c, but the
41 way it builds the new argument list was rewritten so it is much
42 easier to maintain, improve the way it decides to add or not add
43 extra arguments, etc. And several improvements were made in the
44 handling of arguments, primarily to make it more consistent with
45 `gcc' itself. */
47 #include "config.h"
48 #include "system.h"
49 #include "coretypes.h"
50 #include "tm.h"
51 #include "gcc.h"
53 #ifndef MATH_LIBRARY
54 #define MATH_LIBRARY "-lm"
55 #endif
57 #ifndef FORTRAN_INIT
58 #define FORTRAN_INIT "-lfrtbegin"
59 #endif
61 #ifndef FORTRAN_LIBRARY
62 #define FORTRAN_LIBRARY "-lg2c"
63 #endif
65 /* Options this driver needs to recognize, not just know how to
66 skip over. */
67 typedef enum
69 OPTION_b, /* Aka --prefix. */
70 OPTION_B, /* Aka --target. */
71 OPTION_c, /* Aka --compile. */
72 OPTION_driver, /* Wrapper-specific option. */
73 OPTION_E, /* Aka --preprocess. */
74 OPTION_help, /* --help. */
75 OPTION_i, /* -imacros, -include, -include-*. */
76 OPTION_l,
77 OPTION_L, /* Aka --library-directory. */
78 OPTION_M, /* Aka --dependencies. */
79 OPTION_MM, /* Aka --user-dependencies. */
80 OPTION_nostdlib, /* Aka --no-standard-libraries, or
81 -nodefaultlibs. */
82 OPTION_o, /* Aka --output. */
83 OPTION_S, /* Aka --assemble. */
84 OPTION_syntax_only, /* -fsyntax-only. */
85 OPTION_v, /* Aka --verbose. */
86 OPTION_version, /* --version. */
87 OPTION_V, /* Aka --use-version. */
88 OPTION_x, /* Aka --language. */
89 OPTION_ /* Unrecognized or unimportant. */
90 } Option;
92 /* The original argument list and related info is copied here. */
93 static int g77_xargc;
94 static const char *const *g77_xargv;
95 static void lookup_option PARAMS ((Option *, int *, const char **,
96 const char *));
97 static void append_arg PARAMS ((const char *));
99 /* The new argument list will be built here. */
100 static int g77_newargc;
101 static const char **g77_newargv;
103 /* --- This comes from gcc.c (2.8.1) verbatim: */
105 /* This defines which switch letters take arguments. */
107 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
108 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
109 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
110 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
111 || (CHAR) == 'L' || (CHAR) == 'A')
113 #ifndef SWITCH_TAKES_ARG
114 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
115 #endif
117 /* This defines which multi-letter switches take arguments. */
119 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
120 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
121 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
122 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
123 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
124 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
125 || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
127 #ifndef WORD_SWITCH_TAKES_ARG
128 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
129 #endif
131 /* --- End of verbatim. */
133 /* Assumes text[0] == '-'. Returns number of argv items that belong to
134 (and follow) this one, an option id for options important to the
135 caller, and a pointer to the first char of the arg, if embedded (else
136 returns NULL, meaning no arg or it's the next argv).
138 Note that this also assumes gcc.c's pass converting long options
139 to short ones, where available, has already been run. */
141 static void
142 lookup_option (xopt, xskip, xarg, text)
143 Option *xopt;
144 int *xskip;
145 const char **xarg;
146 const char *text;
148 Option opt = OPTION_;
149 int skip;
150 const char *arg = NULL;
152 if ((skip = SWITCH_TAKES_ARG (text[1])))
153 skip -= (text[2] != '\0'); /* See gcc.c. */
155 if (text[1] == 'B')
156 opt = OPTION_B, skip = (text[2] == '\0'), arg = text + 2;
157 else if (text[1] == 'b')
158 opt = OPTION_b, skip = (text[2] == '\0'), arg = text + 2;
159 else if ((text[1] == 'c') && (text[2] == '\0'))
160 opt = OPTION_c, skip = 0;
161 else if ((text[1] == 'E') && (text[2] == '\0'))
162 opt = OPTION_E, skip = 0;
163 else if (text[1] == 'i')
164 opt = OPTION_i, skip = 0;
165 else if (text[1] == 'l')
166 opt = OPTION_l;
167 else if (text[1] == 'L')
168 opt = OPTION_L, arg = text + 2;
169 else if (text[1] == 'o')
170 opt = OPTION_o;
171 else if ((text[1] == 'S') && (text[2] == '\0'))
172 opt = OPTION_S, skip = 0;
173 else if (text[1] == 'V')
174 opt = OPTION_V, skip = (text[2] == '\0');
175 else if ((text[1] == 'v') && (text[2] == '\0'))
176 opt = OPTION_v, skip = 0;
177 else if (text[1] == 'x')
178 opt = OPTION_x, arg = text + 2;
179 else
181 if ((skip = WORD_SWITCH_TAKES_ARG (text + 1)) != 0) /* See gcc.c. */
183 else if (! strncmp (text, "-fdriver", 8)) /* Really --driver!! */
184 opt = OPTION_driver; /* Never mind arg, this is unsupported. */
185 else if (! strcmp (text, "-fhelp")) /* Really --help!! */
186 opt = OPTION_help;
187 else if (! strcmp (text, "-M"))
188 opt = OPTION_M;
189 else if (! strcmp (text, "-MM"))
190 opt = OPTION_MM;
191 else if (! strcmp (text, "-nostdlib")
192 || ! strcmp (text, "-nodefaultlibs"))
193 opt = OPTION_nostdlib;
194 else if (! strcmp (text, "-fsyntax-only"))
195 opt = OPTION_syntax_only;
196 else if (! strcmp (text, "-dumpversion"))
197 opt = OPTION_version;
198 else if (! strcmp (text, "-fversion")) /* Really --version!! */
199 opt = OPTION_version;
200 else if (! strcmp (text, "-Xlinker")
201 || ! strcmp (text, "-specs"))
202 skip = 1;
203 else
204 skip = 0;
207 if (xopt != NULL)
208 *xopt = opt;
209 if (xskip != NULL)
210 *xskip = skip;
211 if (xarg != NULL)
213 if ((arg != NULL)
214 && (arg[0] == '\0'))
215 *xarg = NULL;
216 else
217 *xarg = arg;
221 /* Append another argument to the list being built. As long as it is
222 identical to the corresponding arg in the original list, just increment
223 the new arg count. Otherwise allocate a new list, etc. */
225 static void
226 append_arg (arg)
227 const char *arg;
229 static int newargsize;
231 #if 0
232 fprintf (stderr, "`%s'\n", arg);
233 #endif
235 if (g77_newargv == g77_xargv
236 && g77_newargc < g77_xargc
237 && (arg == g77_xargv[g77_newargc]
238 || ! strcmp (arg, g77_xargv[g77_newargc])))
240 ++g77_newargc;
241 return; /* Nothing new here. */
244 if (g77_newargv == g77_xargv)
245 { /* Make new arglist. */
246 int i;
248 newargsize = (g77_xargc << 2) + 20; /* This should handle all. */
249 g77_newargv = (const char **) xmalloc (newargsize * sizeof (char *));
251 /* Copy what has been done so far. */
252 for (i = 0; i < g77_newargc; ++i)
253 g77_newargv[i] = g77_xargv[i];
256 if (g77_newargc == newargsize)
257 fatal ("overflowed output arg list for `%s'", arg);
259 g77_newargv[g77_newargc++] = arg;
262 void
263 lang_specific_driver (in_argc, in_argv, in_added_libraries)
264 int *in_argc;
265 const char *const **in_argv;
266 int *in_added_libraries ATTRIBUTE_UNUSED;
268 int argc = *in_argc;
269 const char *const *argv = *in_argv;
270 int i;
271 int verbose = 0;
272 Option opt;
273 int skip;
274 const char *arg;
276 /* This will be NULL if we encounter a situation where we should not
277 link in libf2c. */
278 const char *library = FORTRAN_LIBRARY;
280 /* 0 => -xnone in effect.
281 1 => -xfoo in effect. */
282 int saw_speclang = 0;
284 /* 0 => initial/reset state
285 1 => last arg was -l<library>
286 2 => last two args were -l<library> -lm. */
287 int saw_library = 0;
289 /* 0 => initial/reset state
290 1 => FORTRAN_INIT linked in */
291 int use_init = 0;
292 /* By default, we throw on the math library if we have one. */
293 int need_math = (MATH_LIBRARY[0] != '\0');
295 /* The number of input and output files in the incoming arg list. */
296 int n_infiles = 0;
297 int n_outfiles = 0;
299 #if 0
300 fprintf (stderr, "Incoming:");
301 for (i = 0; i < argc; i++)
302 fprintf (stderr, " %s", argv[i]);
303 fprintf (stderr, "\n");
304 #endif
306 g77_xargc = argc;
307 g77_xargv = argv;
308 g77_newargc = 0;
309 g77_newargv = (const char **) argv;
311 /* First pass through arglist.
313 If -nostdlib or a "turn-off-linking" option is anywhere in the
314 command line, don't do any library-option processing (except
315 relating to -x). Also, if -v is specified, but no other options
316 that do anything special (allowing -V version, etc.), remember
317 to add special stuff to make gcc command actually invoke all
318 the different phases of the compilation process so all the version
319 numbers can be seen.
321 Also, here is where all problems with missing arguments to options
322 are caught. If this loop is exited normally, it means all options
323 have the appropriate number of arguments as far as the rest of this
324 program is concerned. */
326 for (i = 1; i < argc; ++i)
328 if ((argv[i][0] == '+') && (argv[i][1] == 'e'))
330 continue;
333 if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
335 ++n_infiles;
336 continue;
339 lookup_option (&opt, &skip, NULL, argv[i]);
341 switch (opt)
343 case OPTION_nostdlib:
344 case OPTION_c:
345 case OPTION_S:
346 case OPTION_syntax_only:
347 case OPTION_E:
348 case OPTION_M:
349 case OPTION_MM:
350 /* These options disable linking entirely or linking of the
351 standard libraries. */
352 library = 0;
353 break;
355 case OPTION_l:
356 ++n_infiles;
357 break;
359 case OPTION_o:
360 ++n_outfiles;
361 break;
363 case OPTION_v:
364 verbose = 1;
365 break;
367 case OPTION_b:
368 case OPTION_B:
369 case OPTION_L:
370 case OPTION_i:
371 case OPTION_V:
372 /* These options are useful in conjunction with -v to get
373 appropriate version info. */
374 break;
376 case OPTION_version:
377 printf ("\
378 GNU Fortran (GCC) %s\n\
379 Copyright (C) 2002 Free Software Foundation, Inc.\n\
381 GNU Fortran comes with NO WARRANTY, to the extent permitted by law.\n\
382 You may redistribute copies of GNU Fortran\n\
383 under the terms of the GNU General Public License.\n\
384 For more information about these matters, see the file named COPYING\n\
385 or type the command `info -f g77 Copying'.\n\
386 ", version_string);
387 exit (0);
388 break;
390 case OPTION_help:
391 /* Let gcc.c handle this, as it has a really
392 cool facility for handling --help and --verbose --help. */
393 return;
395 case OPTION_driver:
396 fatal ("--driver no longer supported");
397 break;
399 default:
400 break;
403 /* This is the one place we check for missing arguments in the
404 program. */
406 if (i + skip < argc)
407 i += skip;
408 else
409 fatal ("argument to `%s' missing", argv[i]);
412 if ((n_outfiles != 0) && (n_infiles == 0))
413 fatal ("no input files; unwilling to write output files");
415 /* If there are no input files, no need for the library. */
416 if (n_infiles == 0)
417 library = 0;
419 /* Second pass through arglist, transforming arguments as appropriate. */
421 append_arg (argv[0]); /* Start with command name, of course. */
423 for (i = 1; i < argc; ++i)
425 if (argv[i][0] == '\0')
427 append_arg (argv[i]); /* Interesting. Just append as is. */
428 continue;
431 if ((argv[i][0] == '-') && (argv[i][1] != 'l'))
433 /* Not a filename or library. */
435 if (saw_library == 1 && need_math) /* -l<library>. */
436 append_arg (MATH_LIBRARY);
438 saw_library = 0;
440 lookup_option (&opt, &skip, &arg, argv[i]);
442 if (argv[i][1] == '\0')
444 append_arg (argv[i]); /* "-" == Standard input. */
445 continue;
448 if (opt == OPTION_x)
450 /* Track input language. */
451 const char *lang;
453 if (arg == NULL)
454 lang = argv[i+1];
455 else
456 lang = arg;
458 saw_speclang = (strcmp (lang, "none") != 0);
461 append_arg (argv[i]);
463 for (; skip != 0; --skip)
464 append_arg (argv[++i]);
466 continue;
469 /* A filename/library, not an option. */
471 if (saw_speclang)
472 saw_library = 0; /* -xfoo currently active. */
473 else
474 { /* -lfoo or filename. */
475 if (strcmp (argv[i], MATH_LIBRARY) == 0
476 #ifdef ALT_LIBM
477 || strcmp (argv[i], ALT_LIBM) == 0
478 #endif
481 if (saw_library == 1)
482 saw_library = 2; /* -l<library> -lm. */
483 else
485 if (0 == use_init)
487 append_arg (FORTRAN_INIT);
488 use_init = 1;
490 append_arg (FORTRAN_LIBRARY);
493 else if (strcmp (argv[i], FORTRAN_LIBRARY) == 0)
494 saw_library = 1; /* -l<library>. */
495 else
496 { /* Other library, or filename. */
497 if (saw_library == 1 && need_math)
498 append_arg (MATH_LIBRARY);
499 saw_library = 0;
502 append_arg (argv[i]);
505 /* Append `-lg2c -lm' as necessary. */
507 if (library)
508 { /* Doing a link and no -nostdlib. */
509 if (saw_speclang)
510 append_arg ("-xnone");
512 switch (saw_library)
514 case 0:
515 if (0 == use_init)
517 append_arg (FORTRAN_INIT);
518 use_init = 1;
520 append_arg (library);
521 case 1:
522 if (need_math)
523 append_arg (MATH_LIBRARY);
524 default:
525 break;
529 #ifdef ENABLE_SHARED_LIBGCC
530 if (library)
532 int i;
534 for (i = 1; i < g77_newargc; i++)
535 if (g77_newargv[i][0] == '-')
536 if (strcmp (g77_newargv[i], "-static-libgcc") == 0
537 || strcmp (g77_newargv[i], "-static") == 0)
538 break;
540 if (i == g77_newargc)
541 append_arg ("-shared-libgcc");
544 #endif
546 if (verbose
547 && g77_newargv != g77_xargv)
549 fprintf (stderr, "Driving:");
550 for (i = 0; i < g77_newargc; i++)
551 fprintf (stderr, " %s", g77_newargv[i]);
552 fprintf (stderr, "\n");
555 *in_argc = g77_newargc;
556 *in_argv = g77_newargv;
559 /* Called before linking. Returns 0 on success and -1 on failure. */
560 int lang_specific_pre_link () /* Not used for F77. */
562 return 0;
565 /* Number of extra output files that lang_specific_pre_link may generate. */
566 int lang_specific_extra_outfiles = 0; /* Not used for F77. */
568 /* Table of language-specific spec functions. */
569 const struct spec_function lang_specific_spec_functions[] =
571 { 0, 0 }