OSDN Git Service

* c-tree.h (define_label): Replace filename and lineno arguments
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-dump.c
index 8a66def..a87dd40 100644 (file)
@@ -1,20 +1,23 @@
 /* Program to dump out a Java(TM) .class file.
    Functionally similar to Sun's javap.
 
-   Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+   Free Software Foundation, Inc.
 
-This program is free software; you can redistribute it and/or modify
+This file is part of GCC.
+
+GCC 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.
 
-This program is distributed in the hope that it will be useful,
+GCC 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
+along with GCC; see the file COPYING.  If not, write to
 the Free Software Foundation, 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  
 
@@ -48,11 +51,19 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "ggc.h"
 
 #include "jcf.h"
 #include "tree.h"
 #include "java-tree.h"
 
+#include "version.h"
+
+#include <getopt.h>
+#include <math.h>
+
 /* Outout file. */
 FILE *out;
 /* Name of output file, if NULL if stdout. */
@@ -67,6 +78,10 @@ int flag_print_fields = 1;
 int flag_print_methods = 1;
 int flag_print_attributes = 1;
 
+/* When nonzero, warn when source file is newer than matching class
+   file.  */
+int flag_newer = 1;
+
 /* Print names of classes that have a "main" method. */
 int flag_print_main = 0;
 
@@ -78,26 +93,27 @@ int class_access_flags = 0;
 /* Print in format similar to javap.  VERY IMCOMPLETE. */
 int flag_javap_compatible = 0;
 
-static void print_access_flags PROTO ((FILE *, uint16, char));
-static void print_constant_terse PROTO ((FILE*, JCF*, int, int));
-static void print_constant PROTO ((FILE *, JCF *, int, int));
-static void print_constant_ref PROTO ((FILE *, JCF *, int));
-static void disassemble_method PROTO ((JCF*, const unsigned char *, int));
-static void print_name PROTO ((FILE*, JCF*, int));
-static void print_signature PROTO ((FILE*, JCF*, int, int));
-static int utf8_equal_string PROTO ((struct JCF*, int, const char *));
-static int usage PROTO ((void)) ATTRIBUTE_NORETURN;
-static void process_class PROTO ((struct JCF *));
-static void print_constant_pool PROTO ((struct JCF *));
-static void print_exception_table PROTO ((struct JCF *,
-                                         const unsigned char *entries, int));
+static void print_access_flags (FILE *, uint16, char);
+static void print_constant_terse (FILE*, JCF*, int, int);
+static void print_constant (FILE *, JCF *, int, int);
+static void print_constant_ref (FILE *, JCF *, int);
+static void disassemble_method (JCF*, const unsigned char *, int);
+static void print_name (FILE*, JCF*, int);
+static void print_signature (FILE*, JCF*, int, int);
+static int utf8_equal_string (struct JCF*, int, const char *);
+static void usage (void) ATTRIBUTE_NORETURN;
+static void help (void) ATTRIBUTE_NORETURN;
+static void version (void) ATTRIBUTE_NORETURN;
+static void process_class (struct JCF *);
+static void print_constant_pool (struct JCF *);
+static void print_exception_table (struct JCF *, const unsigned char *entries,
+                                  int);
 
 #define PRINT_SIGNATURE_RESULT_ONLY 1
 #define PRINT_SIGNATURE_ARGS_ONLY 2
 
 static int
-DEFUN(utf8_equal_string, (jcf, index, value),
-      JCF *jcf AND int index AND const char * value)
+utf8_equal_string (JCF *jcf, int index, const char * value)
 {
   if (CPOOL_INDEX_IN_RANGE (&jcf->cpool, index)
       && JPOOL_TAG (jcf, index) == CONSTANT_Utf8)
@@ -288,6 +304,38 @@ DEFUN(utf8_equal_string, (jcf, index, value),
   else \
     JCF_SKIP (jcf, 4 * n); }
 
+#define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT)                               \
+{ int n = (COUNT);                                                         \
+  COMMON_HANDLE_ATTRIBUTE(jcf, attribute_name, attribute_length);          \
+  while (n--)                                                              \
+    {                                                                      \
+      uint16 inner_class_info_index = JCF_readu2 (jcf);                            \
+      uint16 outer_class_info_index = JCF_readu2 (jcf);                            \
+      uint16 inner_name_index = JCF_readu2 (jcf);                          \
+      uint16 inner_class_access_flags = JCF_readu2 (jcf);                  \
+                                                                           \
+      if (flag_print_class_info)                                           \
+       {                                                                   \
+         fprintf (out, "\n  class: ");                                     \
+         if (flag_print_constant_pool)                                     \
+           fprintf (out, "%d=", inner_class_info_index);                   \
+         print_constant_terse (out, jcf,                                   \
+                               inner_class_info_index, CONSTANT_Class);    \
+         fprintf (out, " (%d=", inner_name_index);                         \
+         print_constant_terse (out, jcf, inner_name_index, CONSTANT_Utf8); \
+         fprintf (out, "), access flags: 0x%x", inner_class_access_flags); \
+         print_access_flags (out, inner_class_access_flags, 'c');          \
+         fprintf (out, ", outer class: ");                                 \
+         if (flag_print_constant_pool)                                     \
+           fprintf (out, "%d=", outer_class_info_index);                   \
+         print_constant_terse (out, jcf,                                   \
+                               outer_class_info_index, CONSTANT_Class);    \
+       }                                                                   \
+    }                                                                      \
+      if (flag_print_class_info)                                           \
+       fputc ('\n', out);                                                  \
+}
+
 #define PROCESS_OTHER_ATTRIBUTE(JCF, INDEX, LENGTH) \
 { COMMON_HANDLE_ATTRIBUTE(JCF, INDEX, LENGTH); \
   fputc ('\n', out); JCF_SKIP (JCF, LENGTH); }
@@ -299,8 +347,7 @@ DEFUN(utf8_equal_string, (jcf, index, value),
 #include "javaop.h"
 
 static void
-DEFUN(print_constant_ref, (stream, jcf, index),
-      FILE *stream AND JCF *jcf AND int index)
+print_constant_ref (FILE *stream, JCF *jcf, int index)
 {
   fprintf (stream, "#%d=<", index);
   if (index <= 0 || index >= JPOOL_SIZE(jcf))
@@ -315,14 +362,17 @@ DEFUN(print_constant_ref, (stream, jcf, index),
    or 'm' (method flags). */
 
 static void
-DEFUN (print_access_flags, (stream, flags, context),
-       FILE *stream AND uint16 flags AND char context)
+print_access_flags (FILE *stream, uint16 flags, char context)
 {
   if (flags & ACC_PUBLIC) fprintf (stream, " public");
   if (flags & ACC_PRIVATE) fprintf (stream, " private");
   if (flags & ACC_PROTECTED) fprintf (stream, " protected");
+  if (flags & ACC_ABSTRACT) fprintf (stream, " abstract");
   if (flags & ACC_STATIC) fprintf (stream, " static");
   if (flags & ACC_FINAL) fprintf (stream, " final");
+  if (flags & ACC_TRANSIENT) fprintf (stream, " transient");
+  if (flags & ACC_VOLATILE) fprintf (stream, " volatile");
+  if (flags & ACC_NATIVE) fprintf (stream, " native");
   if (flags & ACC_SYNCHRONIZED)
     {
       if (context == 'c')
@@ -330,17 +380,13 @@ DEFUN (print_access_flags, (stream, flags, context),
       else
        fprintf (stream, " synchronized");
     }
-  if (flags & ACC_VOLATILE) fprintf (stream, " volatile");
-  if (flags & ACC_TRANSIENT) fprintf (stream, " transient");
-  if (flags & ACC_NATIVE) fprintf (stream, " native");
   if (flags & ACC_INTERFACE) fprintf (stream, " interface");
-  if (flags & ACC_ABSTRACT) fprintf (stream, " abstract");
+  if (flags & ACC_STRICT) fprintf (stream, " strictfp");
 }
 
 
 static void
-DEFUN(print_name, (stream, jcf, name_index),
-      FILE* stream AND JCF* jcf AND int name_index)
+print_name (FILE* stream, JCF* jcf, int name_index)
 {
   if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
     fprintf (stream, "<not a UTF8 constant>");
@@ -353,8 +399,7 @@ DEFUN(print_name, (stream, jcf, name_index),
    print it tersely, otherwise more verbosely. */
 
 static void
-DEFUN(print_constant_terse, (out, jcf, index, expected),
-      FILE *out AND JCF *jcf AND int index AND int expected)
+print_constant_terse (FILE *out, JCF *jcf, int index, int expected)
 {
   if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
     fprintf (out, "<constant pool index %d not in range>", index);
@@ -374,8 +419,7 @@ DEFUN(print_constant_terse, (out, jcf, index, expected),
    If verbosity==2, add more descriptive text. */
 
 static void
-DEFUN(print_constant, (out, jcf, index, verbosity),
-      FILE *out AND JCF *jcf AND int index AND int verbosity)
+print_constant (FILE *out, JCF *jcf, int index, int verbosity)
 {
   int j, n;
   jlong num;
@@ -462,15 +506,85 @@ DEFUN(print_constant, (out, jcf, index, verbosity),
     case CONSTANT_Float:
       {
        jfloat fnum = JPOOL_FLOAT (jcf, index);
-       fprintf (out, "%s%.10g", verbosity > 1 ? "Float " : "", (double) fnum);
+
+       if (verbosity > 0)
+         fputs ("Float ", out);
+
+       if (fnum.negative)
+         putc ('-', out);
+
+       if (JFLOAT_FINITE (fnum))
+         {
+           int dummy;
+           int exponent = fnum.exponent - JFLOAT_EXP_BIAS;
+           double f;
+           uint32 mantissa = fnum.mantissa;
+           if (fnum.exponent == 0)
+             /* Denormal.  */
+             exponent++;
+           else
+             /* Normal; add the implicit bit.  */
+             mantissa |= ((uint32)1 << 23);
+           
+           f = frexp (mantissa, &dummy);
+           f = ldexp (f, exponent + 1);
+           fprintf (out, "%.10g", f);
+         }
+       else
+         {
+           if (fnum.mantissa == 0)
+             fputs ("Inf", out);
+           else if (fnum.mantissa & JFLOAT_QNAN_MASK)
+             fprintf (out, "QNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK));
+           else
+             fprintf (out, "SNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK));
+         }
+
        if (verbosity > 1)
-         fprintf (out, ", bits = 0x%08lx", (long) (* (int32 *) &fnum));
+         fprintf (out, ", bits = 0x%08lx", JPOOL_UINT (jcf, index));
+       
        break;
       }
     case CONSTANT_Double:
       {
        jdouble dnum = JPOOL_DOUBLE (jcf, index);
-       fprintf (out, "%s%.20g", verbosity > 1 ? "Double " : "", dnum);
+
+       if (verbosity > 0)
+         fputs ("Double ", out);
+
+       if (dnum.negative)
+         putc ('-', out);
+
+       if (JDOUBLE_FINITE (dnum))
+         {
+           int dummy;
+           int exponent = dnum.exponent - JDOUBLE_EXP_BIAS;
+           double d;
+           uint64 mantissa = ((((uint64) dnum.mantissa0) << 32)
+                              + dnum.mantissa1);
+           if (dnum.exponent == 0)
+             /* Denormal.  */
+             exponent++;
+           else
+             /* Normal; add the implicit bit.  */
+             mantissa |= ((uint64)1 << 52);
+
+           d = frexp (mantissa, &dummy);
+           d = ldexp (d, exponent + 1);
+           fprintf (out, "%.20g", d);
+         }
+       else
+         {
+           uint64 mantissa = dnum.mantissa0 & ~JDOUBLE_QNAN_MASK;
+           mantissa = (mantissa << 32) + dnum.mantissa1;
+
+           if (dnum.mantissa0 == 0 && dnum.mantissa1 == 0)
+             fputs ("Inf", out);
+           else if (dnum.mantissa0 & JDOUBLE_QNAN_MASK)
+             fprintf (out, "QNaN(%llu)", (unsigned long long)mantissa);
+           else
+             fprintf (out, "SNaN(%llu)", (unsigned long long)mantissa);
+         }
        if (verbosity > 1)
          {
            int32 hi, lo;
@@ -523,8 +637,7 @@ DEFUN(print_constant, (out, jcf, index, verbosity),
 }
 
 static void
-DEFUN(print_constant_pool, (jcf),
-      JCF *jcf)
+print_constant_pool (JCF *jcf)
 {
   int i;
   for (i = 1; i < JPOOL_SIZE(jcf); i++)
@@ -539,8 +652,8 @@ DEFUN(print_constant_pool, (jcf),
 }
 
 static void
-DEFUN(print_signature_type, (stream, ptr, limit),
-     FILE* stream AND const unsigned char **ptr AND const unsigned char *limit)
+print_signature_type (FILE* stream, const unsigned char **ptr,
+                     const unsigned char *limit)
 {
   int array_size;
   if ((*ptr) >= limit)
@@ -601,8 +714,7 @@ DEFUN(print_signature_type, (stream, ptr, limit),
 }
 
 static void
-DEFUN(print_signature, (stream, jcf, signature_index, int options),
-      FILE* stream AND JCF *jcf AND int signature_index AND int options)
+print_signature (FILE* stream, JCF *jcf, int signature_index, int options)
 {
   if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
     print_constant_terse (out, jcf, signature_index, CONSTANT_Utf8);
@@ -648,8 +760,7 @@ DEFUN(print_signature, (stream, jcf, signature_index, int options),
 
 
 static void
-DEFUN(print_exception_table, (jcf, entries, count),
-      JCF *jcf AND const unsigned char *entries AND int count)
+print_exception_table (JCF *jcf, const unsigned char *entries, int count)
 {
   /* Print exception table. */
   int i = count;
@@ -679,16 +790,8 @@ DEFUN(print_exception_table, (jcf, entries, count),
 
 #include "jcf-reader.c"
 
-static int
-DEFUN (usage, (), )
-{
-  fprintf (stderr, "Usage: jcf-dump [-o outputfile] [-c] classname\n");
-  exit(1);
-}
-
 static void
-DEFUN(process_class, (jcf),
-      JCF *jcf)
+process_class (JCF *jcf)
 {
   int code;
   if (jcf_parse_preamble (jcf) != 0)
@@ -732,58 +835,151 @@ DEFUN(process_class, (jcf),
   jcf->filename = NULL;
 }
 
+\f
+
+/* This is used to mark options with no short value.  */
+#define LONG_OPT(Num)  ((Num) + 128)
+
+#define OPT_classpath     LONG_OPT (0)
+#define OPT_CLASSPATH     OPT_classpath
+#define OPT_bootclasspath LONG_OPT (1)
+#define OPT_extdirs       LONG_OPT (2)
+#define OPT_HELP          LONG_OPT (3)
+#define OPT_VERSION       LONG_OPT (4)
+#define OPT_JAVAP         LONG_OPT (5)
+
+static const struct option options[] =
+{
+  { "classpath",     required_argument, NULL, OPT_classpath },
+  { "bootclasspath", required_argument, NULL, OPT_bootclasspath },
+  { "extdirs",       required_argument, NULL, OPT_extdirs },
+  { "CLASSPATH",     required_argument, NULL, OPT_CLASSPATH },
+  { "help",          no_argument,       NULL, OPT_HELP },
+  { "verbose",       no_argument,       NULL, 'v' },
+  { "version",       no_argument,       NULL, OPT_VERSION },
+  { "javap",         no_argument,       NULL, OPT_JAVAP },
+  { "print-main",    no_argument,      &flag_print_main, 1 },
+  { NULL,            no_argument,       NULL, 0 }
+};
+
+static void
+usage (void)
+{
+  fprintf (stderr, "Try `jcf-dump --help' for more information.\n");
+  exit (1);
+}
+
+static void
+help (void)
+{
+  printf ("Usage: jcf-dump [OPTION]... CLASS...\n\n");
+  printf ("Display contents of a class file in readable form.\n\n");
+  printf ("  -c                      Disassemble method bodies\n");
+  printf ("  --javap                 Generate output in `javap' format\n");
+  printf ("\n");
+  printf ("  --classpath PATH        Set path to find .class files\n");
+  printf ("  -IDIR                   Append directory to class path\n");
+  printf ("  --bootclasspath PATH    Override built-in class path\n");
+  printf ("  --extdirs PATH          Set extensions directory path\n");
+  printf ("  -o FILE                 Set output file name\n");
+  printf ("\n");
+  printf ("  --help                  Print this help, then exit\n");
+  printf ("  --version               Print version number, then exit\n");
+  printf ("  -v, --verbose           Print extra information while running\n");
+  printf ("\n");
+  printf ("For bug reporting instructions, please see:\n");
+  printf ("%s.\n", bug_report_url);
+  exit (0);
+}
+
+static void
+version (void)
+{
+  printf ("jcf-dump (GCC) %s\n\n", version_string);
+  printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
+  printf ("This is free software; see the source for copying conditions.  There is NO\n");
+  printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
+  exit (0);
+}
+
 int
-DEFUN(main, (argc, argv),
-      int argc AND char** argv)
+main (int argc, char** argv)
 {
   JCF jcf[1];
-  int argi;
+  int argi, opt;
+
   if (argc <= 1)
-    usage ();
+    {
+      fprintf (stderr, "jcf-dump: no classes specified\n");
+      usage ();
+    }
 
   jcf_path_init ();
 
-  for (argi = 1; argi < argc; argi++)
+  /* We use getopt_long_only to allow single `-' long options.  For
+     some of our options this is more natural.  */
+  while ((opt = getopt_long_only (argc, argv, "o:I:vc", options, NULL)) != -1)
     {
-      const char *arg = argv[argi];
+      switch (opt)
+       {
+       case 0:
+         /* Already handled.  */
+         break;
 
-      if (arg[0] != '-' || ! strcmp (arg, "--"))
-       break;
+        case 'o':
+         output_file = optarg;
+         break;
 
-      /* Just let all arguments be given in either "-" or "--" form.  */
-      if (arg[1] == '-')
-       ++arg;
-
-      if (strcmp (arg, "-o") == 0 && argi + 1 < argc)
-       output_file = argv[++argi];
-      else if (strcmp (arg, "-classpath") == 0 && argi + 1 < argc)
-       jcf_path_classpath_arg (argv[++argi]);
-      else if (strcmp (arg, "-CLASSPATH") == 0 && argi + 1 < argc)
-       jcf_path_CLASSPATH_arg (argv[++argi]);
-      else if (strncmp (arg, "-I", 2) == 0)
-       jcf_path_include_arg (arg + 2);
-      else if (strcmp (arg, "-verbose") == 0)
-       verbose++;
-      else if (strcmp (arg, "-print-main") == 0)
-       flag_print_main++;
-      else if (strcmp (arg, "-c") == 0)
-       flag_disassemble_methods++;
-      else if (strcmp (arg, "-javap") == 0)
-       {
+       case 'I':
+         jcf_path_include_arg (optarg);
+         break;
+
+       case 'v':
+         verbose++;
+         break;
+
+       case 'c':
+         flag_disassemble_methods = 1;
+         break;
+
+       case OPT_classpath:
+         jcf_path_classpath_arg (optarg);
+         break;
+
+       case OPT_bootclasspath:
+         jcf_path_bootclasspath_arg (optarg);
+         break;
+
+       case OPT_extdirs:
+         jcf_path_extdirs_arg (optarg);
+         break;
+
+       case OPT_HELP:
+         help ();
+         break;
+
+       case OPT_VERSION:
+         version ();
+         break;
+
+       case OPT_JAVAP:
          flag_javap_compatible++;
          flag_print_constant_pool = 0;
-       }
-      else
-       {
-         fprintf (stderr, "%s: illegal argument\n", argv[argi]);
-         return FATAL_EXIT_CODE;
+         flag_print_attributes = 0;
+         break;
+
+       default:
+         usage ();
        }
     }
 
-  if (argi == argc)
-    usage ();
+  if (optind == argc)
+    {
+      fprintf (stderr, "jcf-dump: no classes specified\n");
+      usage ();
+    }
 
-  jcf_path_seal ();
+  jcf_path_seal (verbose);
 
   if (flag_print_main)
     {
@@ -797,7 +993,7 @@ DEFUN(main, (argc, argv),
   if (output_file)
     {
       out = fopen (output_file, "w");
-      if (out)
+      if (out)
        {
          fprintf (stderr, "Cannot open '%s' for output.\n", output_file);
          return FATAL_EXIT_CODE;
@@ -806,19 +1002,15 @@ DEFUN(main, (argc, argv),
   else
     out = stdout;
 
-  if (argi >= argc)
+  if (optind >= argc)
     {
       fprintf (out, "Reading .class from <standard input>.\n");
-#if JCF_USE_STDIO
-      open_class ("<stdio>", jcf, stdin, NULL);
-#else
       open_class ("<stdio>", jcf, 0, NULL);
-#endif
       process_class (jcf);
     }
   else
     {
-      for (; argi < argc; argi++)
+      for (argi = optind; argi < argc; argi++)
        {
          char *arg = argv[argi];
          const char *class_filename = find_class (arg, strlen (arg), jcf, 0);
@@ -927,11 +1119,11 @@ DEFUN(main, (argc, argv),
   return SUCCESS_EXIT_CODE;
 }
 
+\f
+
 static void
-DEFUN(disassemble_method, (jcf, byte_ops, len),
-      JCF* jcf AND const unsigned char *byte_ops AND int len)
+disassemble_method (JCF* jcf, const unsigned char *byte_ops, int len)
 {
-#undef AND /* Causes problems with opcodes for iand and land. */
 #undef PTR
   int PC;
   int i;
@@ -950,7 +1142,7 @@ DEFUN(disassemble_method, (jcf, byte_ops, len),
 /* This is the actual code emitted for each of opcodes in javaops.def.
    The actual opcode-specific stuff is handled by the OPKIND macro.
    I.e. for an opcode whose OPKIND is BINOP, the BINOP will be called.
-   Those macros are defiend below.  The OPKINDs that do not have any
+   Those macros are defined below.  The OPKINDs that do not have any
    inline parameters (such as BINOP) and therefore do mot need anything
    else to me printed out just use an empty body. */
 
@@ -1081,7 +1273,7 @@ DEFUN(disassemble_method, (jcf, byte_ops, len),
 #define SPECIAL_IINC(OPERAND_TYPE) \
   i = saw_wide ? IMMEDIATE_u2 : IMMEDIATE_u1; \
   fprintf (out, " %d", i); \
-  INT_temp = saw_wide ? IMMEDIATE_s2 : IMMEDIATE_s1; \
+  i = saw_wide ? IMMEDIATE_s2 : IMMEDIATE_s1; \
   saw_wide = 0; \
   fprintf (out, " %d", i)