builtins.c (compareAndSwapInt_builtin): Use flag_use_atomic_builtins.
[official-gcc.git] / gcc / java / jvspec.c
blob9e57dab651cc3dfb0eeb19d87e0032e06e592956
1 /* Specific flags and argument handling of the front-end of the
2 GNU compiler for the Java(TM) language.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "gcc.h"
31 #include "jcf.h"
33 /* Name of spec file. */
34 #define SPEC_FILE "libgcj.spec"
36 /* This bit is set if we saw a `-xfoo' language specification. */
37 #define LANGSPEC (1<<1)
38 /* True if this arg is a parameter to the previous option-taking arg. */
39 #define PARAM_ARG (1<<2)
40 /* True if this arg is a .java input file name. */
41 #define JAVA_FILE_ARG (1<<3)
42 /* True if this arg is a .class input file name. */
43 #define CLASS_FILE_ARG (1<<4)
44 /* True if this arg is a .zip or .jar input file name. */
45 #define ZIP_FILE_ARG (1<<5)
46 /* True if this arg is @FILE - where FILE contains a list of filenames. */
47 #define INDIRECT_FILE_ARG (1<<6)
48 /* True if this arg is a resource file. */
49 #define RESOURCE_FILE_ARG (1<<7)
51 static char *find_spec_file (const char *);
52 static int verify_class_name (const char *);
54 static const char *main_class_name = NULL;
55 int lang_specific_extra_outfiles = 0;
57 /* True if we should add -shared-libgcc to the command-line. */
58 int shared_libgcc = 1;
60 static const char jvgenmain_spec[] =
61 "jvgenmain %{findirect-dispatch} %{D*} %b %m.i |\n\
62 cc1 %m.i %1 \
63 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
64 %{g*} %{O*} \
65 %{v:-version} %{pg:-p} %{p}\
66 %<fbounds-check %<fno-bounds-check\
67 %<fassume-compiled* %<fno-assume-compiled*\
68 %<fcompile-resource* %<fassert %<fno-assert \
69 %<femit-class-file %<femit-class-files %<fencoding*\
70 %<fuse-boehm-gc %<fhash-synchronization %<fjni\
71 %<findirect-dispatch %<fnew-verifier\
72 %<fno-store-check %<foutput-class-dir\
73 %<fclasspath* %<fCLASSPATH* %<fbootclasspath*\
74 %<fextdirs*\
75 %<fuse-divide-subroutine %<fno-use-divide-subroutine\
76 %<fuse-atomic-builtins %<fno-use-atomic-builtins\
77 %<fcheck-references %<fno-check-references\
78 %<ffilelist-file %<fsaw-java-file %<fsource* %<ftarget*\
79 %{f*} -fdollars-in-identifiers\
80 %{aux-info*}\
81 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
82 %{S:%W{o*}%{!o*:-o %b.s}}\
83 %(invoke_as)";
85 /* Return full path name of spec file if it is in DIR, or NULL if
86 not. */
87 static char *
88 find_spec_file (const char *dir)
90 char *spec;
91 int x;
92 struct stat sb;
94 spec = XNEWVEC (char, strlen (dir) + sizeof (SPEC_FILE)
95 + sizeof ("-specs=") + 4);
96 strcpy (spec, "-specs=");
97 x = strlen (spec);
98 strcat (spec, dir);
99 strcat (spec, "/");
100 strcat (spec, SPEC_FILE);
101 if (! stat (spec + x, &sb))
102 return spec;
103 free (spec);
104 return NULL;
107 #define JAVA_START_CHAR_P(c) (c < 128 && (ISIDST (c) || c == '$'))
108 #define JAVA_PART_CHAR_P(c) (c < 128 \
109 && (ISIDNUM (c) \
110 || c == '$' \
111 || (c >= 0x00 && c <= 0x08) \
112 || (c >= 0x0e && c <= 0x1b) \
113 || c == 0x7f))
115 /* Verify that NAME is a valid Java class name that might contain
116 `main'. Return 0 on failure. */
117 static int
118 verify_class_name (const char *name)
120 /* FIXME: what encoding do we use for command-line arguments? For
121 now we assume plain ASCII, which of course is wrong. */
122 while (*name)
124 int ch = *name++;
125 if (ch < 0 || ! JAVA_START_CHAR_P (ch))
126 return 0;
127 while (*name)
129 ch = *name++;
130 if (ch < 0)
131 return 0;
132 /* We found a break between class names. Next character
133 must be an identifier start again. */
134 if (ch == '.')
135 break;
136 if (! JAVA_PART_CHAR_P (ch))
137 return 0;
141 return 1;
144 void
145 lang_specific_driver (int *in_argc, const char *const **in_argv,
146 int *in_added_libraries)
148 int i, j;
150 /* If nonzero, the user gave us the `-v' flag. */
151 int saw_verbose_flag = 0;
153 int saw_save_temps = 0;
155 /* This will be 0 if we encounter a situation where we should not
156 link in libgcj. */
157 int library = 1;
159 /* This will be 1 if multiple input files (.class and/or .java)
160 should be passed to a single jc1 invocation. */
161 int combine_inputs = 0;
163 /* Number of .java and .class source file arguments seen. */
164 int java_files_count = 0;
165 int class_files_count = 0;
166 /* Number of .zip or .jar file arguments seen. */
167 int zip_files_count = 0;
168 /* Number of '@FILES' arguments seen. */
169 int indirect_files_count = 0;
171 /* Name of file containing list of files to compile. */
172 char *filelist_filename = 0;
174 FILE *filelist_file = 0;
176 /* The number of arguments being added to what's in argv, other than
177 libraries. */
178 int added = 2;
180 /* Used to track options that take arguments, so we don't go wrapping
181 those with -xc++/-xnone. */
182 const char *quote = NULL;
184 /* The new argument list will be contained in this. */
185 const char **arglist;
187 /* Nonzero if we saw a `-xfoo' language specification on the
188 command line. Used to avoid adding our own -xc++ if the user
189 already gave a language for the file. */
190 int saw_speclang = 0;
192 /* Saw --resource, -C or -o options, respectively. */
193 int saw_resource = 0;
194 int saw_C = 0;
195 int saw_o = 0;
197 /* Saw some -O* or -g* option, respectively. */
198 int saw_O = 0;
199 int saw_g = 0;
201 /* Saw a `-D' option. */
202 int saw_D = 0;
204 /* An array used to flag each argument that needs a bit set for
205 LANGSPEC, MATHLIB, WITHLIBC, or GCLIB. */
206 int *args;
208 /* The total number of arguments with the new stuff. */
209 int argc;
211 /* The argument list. */
212 const char *const *argv;
214 /* The number of libraries added in. */
215 int added_libraries;
217 /* The total number of arguments with the new stuff. */
218 int num_args = 1;
220 /* Nonzero if linking is supposed to happen. */
221 int will_link = 1;
223 /* Nonzero if we want to find the spec file. */
224 int want_spec_file = 1;
226 /* The argument we use to specify the spec file. */
227 char *spec_file = NULL;
229 /* If linking, nonzero if the BC-ABI is in use. */
230 int link_for_bc_abi = 0;
232 argc = *in_argc;
233 argv = *in_argv;
234 added_libraries = *in_added_libraries;
236 args = XCNEWVEC (int, argc);
238 for (i = 1; i < argc; i++)
240 /* If the previous option took an argument, we swallow it here. */
241 if (quote)
243 quote = NULL;
244 args[i] |= PARAM_ARG;
245 continue;
248 /* We don't do this anymore, since we don't get them with minus
249 signs on them. */
250 if (argv[i][0] == '\0' || argv[i][1] == '\0')
251 continue;
253 if (argv[i][0] == '-')
255 if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
256 || strcmp (argv[i], "-nodefaultlibs") == 0))
258 library = 0;
260 else if (strncmp (argv[i], "-fmain=", 7) == 0)
262 main_class_name = argv[i] + 7;
263 added--;
265 else if (strcmp (argv[i], "-fhelp") == 0)
266 want_spec_file = 0;
267 else if (strcmp (argv[i], "-v") == 0)
269 saw_verbose_flag = 1;
270 if (argc == 2)
272 /* If they only gave us `-v', don't try to link
273 in libgcj. */
274 library = 0;
277 else if (strncmp (argv[i], "-x", 2) == 0)
278 saw_speclang = 1;
279 else if (strcmp (argv[i], "-C") == 0)
281 saw_C = 1;
282 want_spec_file = 0;
283 if (library != 0)
284 added -= 2;
285 library = 0;
286 will_link = 0;
288 else if (strncmp (argv[i], "-fcompile-resource=", 19) == 0)
290 saw_resource = 1;
291 want_spec_file = 0;
292 if (library != 0)
293 --added;
294 library = 0;
295 will_link = 0;
297 else if (argv[i][1] == 'D')
298 saw_D = 1;
299 else if (argv[i][1] == 'g')
300 saw_g = 1;
301 else if (argv[i][1] == 'O')
302 saw_O = 1;
303 else if ((argv[i][2] == '\0'
304 && strchr ("bBVDUoeTuIYmLiAI", argv[i][1]) != NULL)
305 || strcmp (argv[i], "-Tdata") == 0
306 || strcmp (argv[i], "-MT") == 0
307 || strcmp (argv[i], "-MF") == 0)
309 if (strcmp (argv[i], "-o") == 0)
310 saw_o = 1;
311 quote = argv[i];
313 else if (strcmp (argv[i], "-classpath") == 0
314 || strcmp (argv[i], "-bootclasspath") == 0
315 || strcmp (argv[i], "-CLASSPATH") == 0
316 || strcmp (argv[i], "-encoding") == 0
317 || strcmp (argv[i], "-extdirs") == 0)
319 quote = argv[i];
320 added -= 1;
322 else if (library != 0
323 && ((argv[i][2] == '\0'
324 && strchr ("cSEM", argv[i][1]) != NULL)
325 || strcmp (argv[i], "-MM") == 0))
327 /* Don't specify libraries if we won't link, since that would
328 cause a warning. */
329 library = 0;
330 added -= 2;
332 /* Remember this so we can confirm -fmain option. */
333 will_link = 0;
335 else if (strcmp (argv[i], "-d") == 0)
337 /* `-d' option is for javac compatibility. */
338 quote = argv[i];
339 added -= 1;
341 else if (strcmp (argv[i], "-fsyntax-only") == 0
342 || strcmp (argv[i], "--syntax-only") == 0)
344 library = 0;
345 will_link = 0;
346 continue;
348 else if (strcmp (argv[i], "-save-temps") == 0)
349 saw_save_temps = 1;
350 else if (strcmp (argv[i], "-static-libgcc") == 0
351 || strcmp (argv[i], "-static") == 0)
352 shared_libgcc = 0;
353 else if (strcmp (argv[i], "-findirect-dispatch") == 0
354 || strcmp (argv[i], "--indirect-dispatch") == 0)
356 link_for_bc_abi = 1;
358 else
359 /* Pass other options through. */
360 continue;
362 else
364 int len;
366 if (saw_speclang)
368 saw_speclang = 0;
369 continue;
372 if (saw_resource)
374 args[i] |= RESOURCE_FILE_ARG;
375 added += 2; /* for -xjava and -xnone */
378 if (argv[i][0] == '@')
380 args[i] |= INDIRECT_FILE_ARG;
381 indirect_files_count++;
382 added += 2; /* for -xjava and -xnone */
385 len = strlen (argv[i]);
386 if (len > 5 && strcmp (argv[i] + len - 5, ".java") == 0)
388 args[i] |= JAVA_FILE_ARG;
389 java_files_count++;
391 if (len > 6 && strcmp (argv[i] + len - 6, ".class") == 0)
393 args[i] |= CLASS_FILE_ARG;
394 class_files_count++;
396 if (len > 4
397 && (strcmp (argv[i] + len - 4, ".zip") == 0
398 || strcmp (argv[i] + len - 4, ".jar") == 0))
400 args[i] |= ZIP_FILE_ARG;
401 zip_files_count++;
406 if (quote)
407 fatal ("argument to '%s' missing\n", quote);
409 if (saw_D && ! main_class_name)
410 fatal ("can't specify '-D' without '--main'\n");
412 if (main_class_name && ! verify_class_name (main_class_name))
413 fatal ("'%s' is not a valid class name", main_class_name);
415 num_args = argc + added;
416 if (saw_resource)
418 if (! saw_o)
419 fatal ("--resource requires -o");
421 if (saw_C)
423 num_args += 3;
424 if (class_files_count + zip_files_count > 0)
426 error ("warning: already-compiled .class files ignored with -C");
427 num_args -= class_files_count + zip_files_count;
428 class_files_count = 0;
429 zip_files_count = 0;
431 num_args += 2; /* For -o NONE. */
432 if (saw_o)
433 fatal ("cannot specify both -C and -o");
435 if ((saw_o && java_files_count + class_files_count + zip_files_count > 1)
436 || (saw_C && java_files_count > 1)
437 || (indirect_files_count > 0
438 && java_files_count + class_files_count + zip_files_count > 0))
439 combine_inputs = 1;
441 if (combine_inputs)
443 filelist_filename = make_temp_file ("jx");
444 if (filelist_filename == NULL)
445 fatal ("cannot create temporary file");
446 record_temp_file (filelist_filename, ! saw_save_temps, 0);
447 filelist_file = fopen (filelist_filename, "w");
448 if (filelist_file == NULL)
449 pfatal_with_name (filelist_filename);
450 num_args -= java_files_count + class_files_count + zip_files_count;
451 num_args += 3; /* for the combined arg "-xjava", and "-xnone" */
454 if (main_class_name)
456 lang_specific_extra_outfiles++;
458 if (saw_g + saw_O == 0)
459 num_args++;
460 num_args++;
461 /* An additional entry for the classpath. */
462 num_args++;
464 if (combine_inputs || indirect_files_count > 0)
465 num_args += 1; /* for "-ffilelist-file" */
466 if (combine_inputs && indirect_files_count > 0)
467 fatal("using both @FILE with multiple files not implemented");
469 /* There's no point adding -shared-libgcc if we don't have a shared
470 libgcc. */
471 #ifndef ENABLE_SHARED_LIBGCC
472 shared_libgcc = 0;
473 #endif
475 if (java_files_count > 0)
476 ++num_args;
478 num_args += shared_libgcc;
480 num_args += link_for_bc_abi;
482 arglist = XNEWVEC (const char *, num_args + 1);
483 j = 0;
485 arglist[j++] = argv[0];
487 if (combine_inputs || indirect_files_count > 0)
488 arglist[j++] = "-ffilelist-file";
490 if (combine_inputs)
492 arglist[j++] = "-xjava";
493 arglist[j++] = filelist_filename;
494 arglist[j++] = "-xnone";
497 if (java_files_count > 0)
498 arglist[j++] = "-fsaw-java-file";
500 jcf_path_init ();
501 for (i = 1; i < argc; i++, j++)
503 arglist[j] = argv[i];
505 if ((args[i] & PARAM_ARG))
506 continue;
508 if ((args[i] & RESOURCE_FILE_ARG) != 0)
510 arglist[j++] = "-xjava";
511 arglist[j++] = argv[i];
512 arglist[j] = "-xnone";
515 if (argv[i][0] == '-' && argv[i][1] == 'I')
517 const char *arg;
518 if (argv[i][2] == '\0')
520 gcc_assert (i + 1 < argc && (args[i + 1] & PARAM_ARG) != 0);
521 arg = argv[i + 1];
522 /* Drop the argument. */
523 ++i;
525 else
526 arg = &argv[i][2];
527 jcf_path_include_arg (arg);
528 --j;
529 continue;
531 if (! strcmp (argv[i], "-classpath")
532 || ! strcmp (argv[i], "-CLASSPATH"))
534 jcf_path_classpath_arg (argv[i + 1]);
535 ++i;
536 --j;
537 continue;
539 if (! strcmp (argv[i], "-bootclasspath"))
541 jcf_path_bootclasspath_arg (argv[i + 1]);
542 ++i;
543 --j;
544 continue;
546 if (! strncmp (argv[i], "-fCLASSPATH=", 12)
547 || ! strncmp (argv[i], "-fclasspath=", 12))
549 char *p = strchr (argv[i], '=');
550 jcf_path_classpath_arg (p + 1);
551 --j;
552 continue;
554 if (! strncmp (argv[i], "-fbootclasspath=", 16))
556 char *p = strchr (argv[i], '=');
557 jcf_path_bootclasspath_arg (p + 1);
558 --j;
559 continue;
561 if (! strcmp (argv[i], "-extdirs"))
563 jcf_path_extdirs_arg (argv[i + 1]);
564 ++i;
565 --j;
566 continue;
569 if (strcmp (argv[i], "-encoding") == 0)
571 arglist[j] = concat ("-f", argv[i]+1, "=", argv[i+1], NULL);
572 i++;
573 continue;
576 if (strcmp (argv[i], "-d") == 0)
578 arglist[j] = concat ("-foutput-class-dir=", argv[i + 1], NULL);
579 ++i;
580 continue;
583 if (spec_file == NULL && strncmp (argv[i], "-L", 2) == 0)
584 spec_file = find_spec_file (argv[i] + 2);
586 if (strncmp (argv[i], "-fmain=", 7) == 0)
588 if (! will_link)
589 fatal ("cannot specify 'main' class when not linking");
590 --j;
591 continue;
594 if ((args[i] & INDIRECT_FILE_ARG) != 0)
596 arglist[j++] = "-xjava";
597 arglist[j++] = argv[i]+1; /* Drop '@'. */
598 arglist[j] = "-xnone";
601 if ((args[i] & (CLASS_FILE_ARG|ZIP_FILE_ARG)) && saw_C)
603 --j;
604 continue;
607 if (combine_inputs
608 && (args[i] & (CLASS_FILE_ARG|JAVA_FILE_ARG|ZIP_FILE_ARG)) != 0)
610 fputs (argv[i], filelist_file);
611 fputc ('\n', filelist_file);
612 --j;
613 continue;
617 /* Handle classpath setting. We specify the bootclasspath since
618 that requires the fewest changes to our existing code... */
619 jcf_path_seal (0);
620 arglist[j++] = jcf_path_compute ("-fbootclasspath=");
622 if (combine_inputs)
624 if (fclose (filelist_file))
625 pfatal_with_name (filelist_filename);
628 /* If we saw no -O or -g option, default to -g1, for javac compatibility. */
629 if (saw_g + saw_O == 0)
630 arglist[j++] = "-g1";
632 /* Read the specs file corresponding to libgcj.
633 If we didn't find the spec file on the -L path, then we hope it
634 is somewhere in the standard install areas. */
635 if (want_spec_file)
636 arglist[j++] = spec_file == NULL ? "-specs=libgcj.spec" : spec_file;
638 if (saw_C)
640 arglist[j++] = "-fsyntax-only";
641 arglist[j++] = "-femit-class-files";
642 arglist[j++] = "-S";
643 arglist[j++] = "-o";
644 arglist[j++] = "NONE";
647 if (shared_libgcc)
648 arglist[j++] = "-shared-libgcc";
650 if (link_for_bc_abi)
651 arglist[j++] = "-s-bc-abi";
653 arglist[j] = NULL;
655 *in_argc = j;
656 *in_argv = arglist;
657 *in_added_libraries = added_libraries;
661 lang_specific_pre_link (void)
663 int err;
664 if (main_class_name == NULL)
665 return 0;
666 /* Append `main' to make the filename unique and allow
668 gcj --main=hello -save-temps hello.java
670 to work. jvgenmain needs to strip this `main' to arrive at the correct
671 class name. Append dummy `.c' that can be stripped by set_input so %b
672 is correct. */
673 set_input (concat (main_class_name, "main.c", NULL));
674 err = do_spec (jvgenmain_spec);
675 if (err == 0)
677 /* Shift the outfiles array so the generated main comes first.
678 This is important when linking against (non-shared) libraries,
679 since otherwise we risk (a) nothing getting linked or
680 (b) 'main' getting picked up from a library. */
681 int i = n_infiles;
682 const char *generated = outfiles[i];
683 while (--i >= 0)
684 outfiles[i + 1] = outfiles[i];
685 outfiles[0] = generated;
687 return err;