aboutsummaryrefslogtreecommitdiff
path: root/gcc/java/jvgenmain.c
blob: 5e767afffe61fcf7d5c31ba25d86660052b1eacb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Program to generate "main" a Java(TM) class containing a main method.
   Copyright (C) 1998 Free Software Foundation, Inc.

This file is part of GNU CC.

GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. 

Java and all Java-based marks are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other countries.
The Free Software Foundation is independent of Sun Microsystems, Inc.  */

/* Written by Per Bothner <bothner@cygnus.com> */

#include "config.h"
#include "system.h"
#include "obstack.h"

const char main_method_prefix[] = "main__";
const char main_method_suffix[] = "Pt6JArray1ZPQ34java4lang6String";
const char class_mangling_prefix[] = "_CL_";

struct obstack name_obstack;

void
error (const char *str)
{
  fprintf (stderr, "%s\n", str);
  exit (-1);
}

void *
xmalloc (size)
     size_t size;
{
  void *ptr = malloc (size);
  if (ptr == NULL)
    {
      fprintf (stderr, "Not enough memory!\n");
      exit (-1);
    }
  return ptr;
}

void
gcc_obstack_init (obstack)
     struct obstack *obstack;
{
  /* Let particular systems override the size of a chunk.  */
#ifndef OBSTACK_CHUNK_SIZE
#define OBSTACK_CHUNK_SIZE 0
#endif
  /* Let them override the alloc and free routines too.  */
#ifndef OBSTACK_CHUNK_ALLOC
#define OBSTACK_CHUNK_ALLOC xmalloc
#endif
#ifndef OBSTACK_CHUNK_FREE
#define OBSTACK_CHUNK_FREE free
#endif
  _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
		  (void *(*) ()) OBSTACK_CHUNK_ALLOC,
		  (void (*) ()) OBSTACK_CHUNK_FREE);
}

int
main (int argc, const char **argv)
{
  const char *classname;
  FILE *stream;
  char *mangled_classname;

  if (argc < 2 || argc > 3)
    {
      fprintf (stderr, "Usage: %s CLASSNAME [OUTFILE]\n", argv[0]);
      exit(-1);
    }

  classname = argv[1];

  gcc_obstack_init (&name_obstack);
  append_gpp_mangled_classtype (&name_obstack, classname);
  mangled_classname = obstack_finish (&name_obstack);

  if (argc > 2 && strcmp (argv[2], "-") != 0)
    {
      const char *outfile = argv[2];
      stream = fopen (outfile, "w");
      if (stream == NULL)
	{
	  fprintf (stderr, "%s: Cannot open output file: %s\n",
		   argv[0], outfile);
	  exit (-1);
	}
    }
  else
    stream = stdout;
  fprintf (stream, "extern struct Class %s%s;\n",
	   class_mangling_prefix, mangled_classname);
  fprintf (stream, "int main (int argc, const char **argv)\n");
  fprintf (stream, "{\n");
  fprintf (stream, "   JvRunMain (&%s%s, argc, argv);\n",
	   class_mangling_prefix, mangled_classname);
  fprintf (stream, "}\n");
  if (stream != stdout && fclose (stream) != 0)
    {
      fprintf (stderr, "%s: Failed to close output file %s\n",
	       argv[0], argv[2]);
      exit (-1);
    }
  return 0;
}