1*c87b03e5Sespie /* Program to generate "main" a Java(TM) class containing a main method.
2*c87b03e5Sespie Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3*c87b03e5Sespie
4*c87b03e5Sespie This file is part of GNU CC.
5*c87b03e5Sespie
6*c87b03e5Sespie GNU CC is free software; you can redistribute it and/or modify
7*c87b03e5Sespie it under the terms of the GNU General Public License as published by
8*c87b03e5Sespie the Free Software Foundation; either version 2, or (at your option)
9*c87b03e5Sespie any later version.
10*c87b03e5Sespie
11*c87b03e5Sespie GNU CC is distributed in the hope that it will be useful,
12*c87b03e5Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c87b03e5Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*c87b03e5Sespie GNU General Public License for more details.
15*c87b03e5Sespie
16*c87b03e5Sespie You should have received a copy of the GNU General Public License
17*c87b03e5Sespie along with GNU CC; see the file COPYING. If not, write to
18*c87b03e5Sespie the Free Software Foundation, 59 Temple Place - Suite 330,
19*c87b03e5Sespie Boston, MA 02111-1307, USA.
20*c87b03e5Sespie
21*c87b03e5Sespie Java and all Java-based marks are trademarks or registered trademarks
22*c87b03e5Sespie of Sun Microsystems, Inc. in the United States and other countries.
23*c87b03e5Sespie The Free Software Foundation is independent of Sun Microsystems, Inc. */
24*c87b03e5Sespie
25*c87b03e5Sespie /* Written by Per Bothner <bothner@cygnus.com> */
26*c87b03e5Sespie
27*c87b03e5Sespie #include "config.h"
28*c87b03e5Sespie #include "system.h"
29*c87b03e5Sespie #include "obstack.h"
30*c87b03e5Sespie #include "jcf.h"
31*c87b03e5Sespie #include "tree.h"
32*c87b03e5Sespie #include "java-tree.h"
33*c87b03e5Sespie
34*c87b03e5Sespie static char * do_mangle_classname PARAMS ((const char *string));
35*c87b03e5Sespie
36*c87b03e5Sespie struct obstack name_obstack;
37*c87b03e5Sespie struct obstack *mangle_obstack = &name_obstack;
38*c87b03e5Sespie
39*c87b03e5Sespie void
gcc_obstack_init(obstack)40*c87b03e5Sespie gcc_obstack_init (obstack)
41*c87b03e5Sespie struct obstack *obstack;
42*c87b03e5Sespie {
43*c87b03e5Sespie /* Let particular systems override the size of a chunk. */
44*c87b03e5Sespie #ifndef OBSTACK_CHUNK_SIZE
45*c87b03e5Sespie #define OBSTACK_CHUNK_SIZE 0
46*c87b03e5Sespie #endif
47*c87b03e5Sespie /* Let them override the alloc and free routines too. */
48*c87b03e5Sespie #ifndef OBSTACK_CHUNK_ALLOC
49*c87b03e5Sespie #define OBSTACK_CHUNK_ALLOC xmalloc
50*c87b03e5Sespie #endif
51*c87b03e5Sespie #ifndef OBSTACK_CHUNK_FREE
52*c87b03e5Sespie #define OBSTACK_CHUNK_FREE free
53*c87b03e5Sespie #endif
54*c87b03e5Sespie _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
55*c87b03e5Sespie (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
56*c87b03e5Sespie (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
57*c87b03e5Sespie }
58*c87b03e5Sespie
59*c87b03e5Sespie static void usage (const char *) ATTRIBUTE_NORETURN;
60*c87b03e5Sespie
61*c87b03e5Sespie static void
usage(const char * name)62*c87b03e5Sespie usage (const char *name)
63*c87b03e5Sespie {
64*c87b03e5Sespie fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n", name);
65*c87b03e5Sespie exit (1);
66*c87b03e5Sespie }
67*c87b03e5Sespie
68*c87b03e5Sespie int
main(int argc,char ** argv)69*c87b03e5Sespie main (int argc, char **argv)
70*c87b03e5Sespie {
71*c87b03e5Sespie char *classname, *p;
72*c87b03e5Sespie FILE *stream;
73*c87b03e5Sespie const char *mangled_classname;
74*c87b03e5Sespie int i, last_arg;
75*c87b03e5Sespie
76*c87b03e5Sespie if (argc < 2)
77*c87b03e5Sespie usage (argv[0]);
78*c87b03e5Sespie
79*c87b03e5Sespie for (i = 1; i < argc; ++i)
80*c87b03e5Sespie {
81*c87b03e5Sespie if (! strncmp (argv[i], "-D", 2))
82*c87b03e5Sespie {
83*c87b03e5Sespie /* Handled later. */
84*c87b03e5Sespie }
85*c87b03e5Sespie else
86*c87b03e5Sespie break;
87*c87b03e5Sespie }
88*c87b03e5Sespie
89*c87b03e5Sespie if (i < argc - 2 || i == argc)
90*c87b03e5Sespie usage (argv[0]);
91*c87b03e5Sespie last_arg = i;
92*c87b03e5Sespie
93*c87b03e5Sespie classname = argv[i];
94*c87b03e5Sespie
95*c87b03e5Sespie /* gcj always appends `main' to classname. We need to strip this here. */
96*c87b03e5Sespie p = strrchr (classname, 'm');
97*c87b03e5Sespie if (p == NULL || p == classname || strcmp (p, "main") != 0)
98*c87b03e5Sespie usage (argv[0]);
99*c87b03e5Sespie else
100*c87b03e5Sespie *p = '\0';
101*c87b03e5Sespie
102*c87b03e5Sespie gcc_obstack_init (mangle_obstack);
103*c87b03e5Sespie mangled_classname = do_mangle_classname (classname);
104*c87b03e5Sespie
105*c87b03e5Sespie if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
106*c87b03e5Sespie {
107*c87b03e5Sespie const char *outfile = argv[i + 1];
108*c87b03e5Sespie stream = fopen (outfile, "w");
109*c87b03e5Sespie if (stream == NULL)
110*c87b03e5Sespie {
111*c87b03e5Sespie fprintf (stderr, "%s: Cannot open output file: %s\n",
112*c87b03e5Sespie argv[0], outfile);
113*c87b03e5Sespie exit (1);
114*c87b03e5Sespie }
115*c87b03e5Sespie }
116*c87b03e5Sespie else
117*c87b03e5Sespie stream = stdout;
118*c87b03e5Sespie
119*c87b03e5Sespie /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
120*c87b03e5Sespie option. Process them appropriately. */
121*c87b03e5Sespie fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
122*c87b03e5Sespie fprintf (stream, "static const char *props[] =\n{\n");
123*c87b03e5Sespie for (i = 1; i < last_arg; ++i)
124*c87b03e5Sespie {
125*c87b03e5Sespie const char *p;
126*c87b03e5Sespie fprintf (stream, " \"");
127*c87b03e5Sespie for (p = &argv[i][2]; *p; ++p)
128*c87b03e5Sespie {
129*c87b03e5Sespie if (! ISPRINT (*p))
130*c87b03e5Sespie fprintf (stream, "\\%o", *p);
131*c87b03e5Sespie else if (*p == '\\' || *p == '"')
132*c87b03e5Sespie fprintf (stream, "\\%c", *p);
133*c87b03e5Sespie else
134*c87b03e5Sespie putc (*p, stream);
135*c87b03e5Sespie }
136*c87b03e5Sespie fprintf (stream, "\",\n");
137*c87b03e5Sespie }
138*c87b03e5Sespie fprintf (stream, " 0\n};\n\n");
139*c87b03e5Sespie
140*c87b03e5Sespie fprintf (stream, "extern int %s;\n", mangled_classname);
141*c87b03e5Sespie fprintf (stream, "int main (int argc, const char **argv)\n");
142*c87b03e5Sespie fprintf (stream, "{\n");
143*c87b03e5Sespie fprintf (stream, " _Jv_Compiler_Properties = props;\n");
144*c87b03e5Sespie fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
145*c87b03e5Sespie fprintf (stream, "}\n");
146*c87b03e5Sespie if (stream != stdout && fclose (stream) != 0)
147*c87b03e5Sespie {
148*c87b03e5Sespie fprintf (stderr, "%s: Failed to close output file %s\n",
149*c87b03e5Sespie argv[0], argv[2]);
150*c87b03e5Sespie exit (1);
151*c87b03e5Sespie }
152*c87b03e5Sespie return 0;
153*c87b03e5Sespie }
154*c87b03e5Sespie
155*c87b03e5Sespie
156*c87b03e5Sespie static char *
do_mangle_classname(string)157*c87b03e5Sespie do_mangle_classname (string)
158*c87b03e5Sespie const char *string;
159*c87b03e5Sespie {
160*c87b03e5Sespie const char *ptr;
161*c87b03e5Sespie int count = 0;
162*c87b03e5Sespie
163*c87b03e5Sespie obstack_grow (&name_obstack, "_ZN", 3);
164*c87b03e5Sespie
165*c87b03e5Sespie for (ptr = string; *ptr; ptr++ )
166*c87b03e5Sespie {
167*c87b03e5Sespie if (ptr[0] == '.')
168*c87b03e5Sespie {
169*c87b03e5Sespie append_gpp_mangled_name (&ptr [-count], count);
170*c87b03e5Sespie count = 0;
171*c87b03e5Sespie }
172*c87b03e5Sespie else
173*c87b03e5Sespie count++;
174*c87b03e5Sespie }
175*c87b03e5Sespie append_gpp_mangled_name (&ptr [-count], count);
176*c87b03e5Sespie obstack_grow (mangle_obstack, "6class$E", 8);
177*c87b03e5Sespie obstack_1grow (mangle_obstack, '\0');
178*c87b03e5Sespie return obstack_finish (mangle_obstack);
179*c87b03e5Sespie }
180