* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / java / jvgenmain.c
blobdcdcb397877a96477b051a01d5152157ec2e649b
1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC 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 3, or (at your option)
9 any later version.
11 GCC 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 GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Per Bothner <bothner@cygnus.com> */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "obstack.h"
30 #include "jcf.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "tree.h"
35 #include "java-tree.h"
36 #include "intl.h"
37 #include "diagnostic.h"
39 static char * do_mangle_classname (const char *string);
41 struct obstack name_obstack;
42 struct obstack *mangle_obstack = &name_obstack;
44 static void usage (const char *) ATTRIBUTE_NORETURN;
46 static void
47 usage (const char *name)
49 fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
50 name);
51 exit (1);
54 int
55 main (int argc, char **argv)
57 char *classname, *p;
58 FILE *stream;
59 const char *mangled_classname;
60 int i, last_arg;
61 int indirect = 0;
62 char *prog_name = argv[0];
64 p = argv[0] + strlen (argv[0]);
65 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
66 --p;
67 progname = p;
69 xmalloc_set_program_name (progname);
71 /* Unlock the stdio streams. */
72 unlock_std_streams ();
74 gcc_init_libintl ();
76 diagnostic_initialize (global_dc, 0);
78 if (argc > 1 && ! strcmp (argv[1], "-findirect-dispatch"))
80 indirect = 1;
81 ++argv;
82 --argc;
85 if (argc < 2)
86 usage (prog_name);
88 for (i = 1; i < argc; ++i)
90 if (! strncmp (argv[i], "-D", 2))
92 /* Handled later. Check "-D XXX=YYY". */
93 if (argv[i][2] == '\0')
94 i++;
96 else
97 break;
100 if (i < argc - 2 || i == argc)
101 usage (prog_name);
102 last_arg = i;
104 classname = argv[i];
106 /* gcj always appends `main' to classname. We need to strip this here. */
107 p = strrchr (classname, 'm');
108 if (p == NULL || p == classname || strcmp (p, "main") != 0)
109 usage (prog_name);
110 else
111 *p = '\0';
113 gcc_obstack_init (mangle_obstack);
114 mangled_classname = do_mangle_classname (classname);
116 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
118 const char *outfile = argv[i + 1];
119 stream = fopen (outfile, "w");
120 if (stream == NULL)
122 fprintf (stderr, _("%s: Cannot open output file: %s\n"),
123 prog_name, outfile);
124 exit (1);
127 else
128 stream = stdout;
130 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
131 option. Process them appropriately. */
132 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
133 if (indirect)
134 fprintf (stream, "extern void JvRunMainName ();\n");
135 else
136 fprintf (stream, "extern void JvRunMain ();\n");
137 fprintf (stream, "static const char *props[] =\n{\n");
138 for (i = 1; i < last_arg; ++i)
140 const char *p;
142 if (strcmp (argv[i], "-D") == 0)
143 continue;
145 fprintf (stream, " \"");
146 for (p = argv[i]; *p; ++p)
148 if (! ISPRINT (*p))
149 fprintf (stream, "\\%o", *p);
150 else if (*p == '\\' || *p == '"')
151 fprintf (stream, "\\%c", *p);
152 else
153 putc (*p, stream);
155 fprintf (stream, "\",\n");
157 fprintf (stream, " 0\n};\n\n");
159 fprintf (stream, "int main (int argc, const char **argv)\n");
160 fprintf (stream, "{\n");
161 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
162 if (indirect)
163 fprintf (stream, " JvRunMainName (\"%s\", argc, argv);\n", classname);
164 else
166 fprintf (stream, " extern char %s;\n", mangled_classname);
167 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
169 fprintf (stream, "}\n");
170 if (stream != stdout && fclose (stream) != 0)
172 fprintf (stderr, _("%s: Failed to close output file %s\n"),
173 prog_name, argv[2]);
174 exit (1);
176 return 0;
180 static char *
181 do_mangle_classname (const char *string)
183 const char *ptr;
184 int count = 0;
186 obstack_grow (&name_obstack, "_ZN", 3);
188 for (ptr = string; *ptr; ptr++ )
190 if (*ptr == '.')
192 append_gpp_mangled_name (ptr - count, count);
193 count = 0;
195 else
196 count++;
198 append_gpp_mangled_name (&ptr [-count], count);
199 obstack_grow (mangle_obstack, "6class$E", strlen ("6class$E"));
200 obstack_1grow (mangle_obstack, '\0');
201 return XOBFINISH (mangle_obstack, char *);