jvgenmain.c 5.1 KB

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