📄 gjavah.c
字号:
/* Program to write C++-suitable header files from a Java(TM) .class file. This is similar to SUN's javah.Copyright (C) 1996, 1998, 1999 Free Software Foundation, Inc.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. Java and all Java-based marks are trademarks or registered trademarksof 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>, February 1996. */#include "config.h"#include "system.h"#include <math.h>#include "jcf.h"#include "tree.h"#include "java-tree.h"#include "java-opcodes.h"/* The output file. */FILE *out = NULL;/* Nonzero on failure. */static int found_error = 0;/* Directory to place resulting files in. Set by -d option. */const char *output_directory = "";/* Directory to place temporary file. Set by -td option. Currently unused. */const char *temp_directory = "/tmp";/* Number of friend functions we have to declare. */static int friend_count;/* A class can optionally have a `friend' function declared. If non-NULL, this is that function. */static char **friend_specs = NULL;/* Number of lines we are prepending before the class. */static int prepend_count;/* We can prepend extra lines before the class's start. */static char **prepend_specs = NULL;/* Number of lines we are appending at the end of the class. */static int add_count;/* We can append extra lines just before the class's end. */static char **add_specs = NULL;/* Number of lines we are appending after the class. */static int append_count;/* We can append extra lines after the class's end. */static char **append_specs = NULL;int verbose = 0;int stubs = 0;struct JCF *current_jcf;/* This holds access information for the last field we examined. They let us generate "private:", "public:", and "protected:" properly. If 0 then we haven't previously examined any field. */static JCF_u2 last_access;#define ACC_VISIBILITY (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED)/* Pass this macro the flags for a class and for a method. It will return true if the method should be considered `final'. */#define METHOD_IS_FINAL(Class, Method) \ (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))/* We keep a linked list of all method names we have seen. This lets us determine if a method name and a field name are in conflict. */struct method_name{ unsigned char *name; int length; struct method_name *next;};/* List of method names we've seen. */static struct method_name *method_name_list;static void print_field_info PROTO ((FILE *, JCF*, int, int, JCF_u2));static void print_method_info PROTO ((FILE *, JCF*, int, int, JCF_u2));static void print_c_decl PROTO ((FILE*, JCF*, int, int, int, const char *));static void decompile_method PROTO ((FILE *, JCF *, int));static void add_class_decl PROTO ((FILE *, JCF *, JCF_u2));static int java_float_finite PROTO ((jfloat));static int java_double_finite PROTO ((jdouble));JCF_u2 current_field_name;JCF_u2 current_field_value;JCF_u2 current_field_signature;JCF_u2 current_field_flags;#define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \( current_field_name = (NAME), current_field_signature = (SIGNATURE), \ current_field_flags = (ACCESS_FLAGS), current_field_value = 0)/* We pass over fields twice. The first time we just note the types of the fields and then the start of the methods. Then we go back and parse the fields for real. This is ugly. */static int field_pass;/* Likewise we pass over methods twice. The first time we generate class decl information; the second time we generate actual method decls. */static int method_pass;#define HANDLE_END_FIELD() \ if (field_pass) \ { \ if (out) \ print_field_info (out, jcf, current_field_name, \ current_field_signature, \ current_field_flags); \ } \ else \ add_class_decl (out, jcf, current_field_signature);#define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)static int method_declared = 0;static int method_access = 0;static int method_printed = 0;#define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \ if (method_pass) \ { \ decompiled = 0; method_printed = 0; \ if (out) \ print_method_info (out, jcf, NAME, SIGNATURE, ACCESS_FLAGS); \ } \ else \ add_class_decl (out, jcf, SIGNATURE);#define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \ if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);static int decompiled = 0;#define HANDLE_END_METHOD() \ if (out && method_printed) fputs (decompiled ? "\n" : ";\n", out);#include "jcf-reader.c"/* Some useful constants. */#define F_NAN_MASK 0x7f800000#define D_NAN_MASK 0x7ff0000000000000LL/* Return 1 if F is not Inf or NaN. */static intjava_float_finite (f) jfloat f;{ union { jfloat f; int32 i; } u; u.f = f; /* We happen to know that F_NAN_MASK will match all NaN values, and also positive and negative infinity. That's why we only need one test here. See The Java Language Specification, section 20.9. */ return (u.i & F_NAN_MASK) != F_NAN_MASK;}/* Return 1 if D is not Inf or NaN. */static intjava_double_finite (d) jdouble d;{ union { jdouble d; int64 i; } u; u.d = d; /* Now check for all NaNs. */ return (u.i & D_NAN_MASK) != D_NAN_MASK;}voidDEFUN(print_name, (stream, jcf, name_index), FILE* stream AND JCF* jcf AND int name_index){ if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8) fprintf (stream, "<not a UTF8 constant>"); else jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index), JPOOL_UTF_LENGTH (jcf, name_index));}/* Print base name of class. The base name is everything after the final separator. */static voidprint_base_classname (stream, jcf, index) FILE *stream; JCF *jcf; int index;{ int name_index = JPOOL_USHORT1 (jcf, index); int len; unsigned char *s, *p, *limit; s = JPOOL_UTF_DATA (jcf, name_index); len = JPOOL_UTF_LENGTH (jcf, name_index); limit = s + len; p = s; while (s < limit) { int c = UTF8_GET (s, limit); if (c == '/') p = s; } while (p < limit) { int ch = UTF8_GET (p, limit); if (ch == '/') fputs ("::", stream); else jcf_print_char (stream, ch); }}/* Return 0 if NAME is equal to STR, nonzero otherwise. */static intutf8_cmp (str, length, name) unsigned char *str; int length; char *name;{ unsigned char *limit = str + length; int i; for (i = 0; name[i]; ++i) { int ch = UTF8_GET (str, limit); if (ch != name[i]) return 1; } return str != limit;}/* If NAME is the name of a C++ keyword, then return an override name. This is a name that can be used in place of the keyword. Otherwise, return NULL. FIXME: for now, we only handle those keywords we know to be a problem for libgcj. */static char *cxx_keyword_subst (str, length) unsigned char *str; int length;{ if (! utf8_cmp (str, length, "delete")) return "__dummy_delete"; else if (! utf8_cmp (str, length, "enum")) return "__dummy_enum"; return NULL;}/* Generate an access control keyword based on FLAGS. Returns 0 if FLAGS matches the saved access information, nonzero otherwise. */static voidgenerate_access (stream, flags) FILE *stream; JCF_u2 flags;{ if ((flags & ACC_VISIBILITY) == last_access) return; last_access = (flags & ACC_VISIBILITY); switch (last_access) { case 0: fputs ("public: // actually package-private\n", stream); break; case ACC_PUBLIC: fputs ("public:\n", stream); break; case ACC_PRIVATE: fputs ("private:\n", stream); break; case ACC_PROTECTED: fputs ("public: // actually protected\n", stream); break; default: found_error = 1; fprintf (stream, "#error unrecognized visibility %d\n", (flags & ACC_VISIBILITY)); break; }}/* See if NAME is already the name of a method. */static intname_is_method_p (name, length) unsigned char *name; int length;{ struct method_name *p; for (p = method_name_list; p != NULL; p = p->next) { if (p->length == length && ! memcmp (p->name, name, length)) return 1; } return 0;}/* Get name of a field. This handles renamings due to C++ clash. */static char *get_field_name (jcf, name_index, flags) JCF *jcf; int name_index; JCF_u2 flags;{ unsigned char *name = JPOOL_UTF_DATA (jcf, name_index); int length = JPOOL_UTF_LENGTH (jcf, name_index); char *override; if (name_is_method_p (name, length)) { /* This field name matches a method. So override the name with a dummy name. This is yucky, but it isn't clear what else to do. FIXME: if the field is static, then we'll be in real trouble. */ if ((flags & ACC_STATIC)) { fprintf (stderr, "static field has same name as method\n"); found_error = 1; return NULL; } override = (char *) malloc (length + 3); memcpy (override, name, length); strcpy (override + length, "__"); } else if ((override = cxx_keyword_subst (name, length)) != NULL) { /* Must malloc OVERRIDE. */ char *o2 = (char *) malloc (strlen (override) + 1); strcpy (o2, override); override = o2; } return override;}/* Print a field name. Convenience function for use with get_field_name. */static voidprint_field_name (stream, jcf, name_index, flags) FILE *stream; JCF *jcf; int name_index; JCF_u2 flags;{ char *override = get_field_name (jcf, name_index, flags); if (override) { fputs (override, stream); free (override); } else jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index), JPOOL_UTF_LENGTH (jcf, name_index));}static voidDEFUN(print_field_info, (stream, jcf, name_index, sig_index, flags), FILE *stream AND JCF* jcf AND int name_index AND int sig_index AND JCF_u2 flags){ char *override = NULL; generate_access (stream, flags); if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8) { fprintf (stream, "<not a UTF8 constant>"); found_error = 1; return; } if (flags & ACC_FINAL) { if (current_field_value > 0) { char buffer[25]; int done = 1; switch (JPOOL_TAG (jcf, current_field_value)) { case CONSTANT_Integer: { jint num; int most_negative = 0; fputs (" static const jint ", out); print_field_name (out, jcf, name_index); fputs (" = ", out); num = JPOOL_INT (jcf, current_field_value); /* We single out the most negative number to print specially. This avoids later warnings from g++. */ if (num == (jint) 0x80000000) { most_negative = 1; ++num; } format_int (buffer, (jlong) num, 10); fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : ""); } break; case CONSTANT_Long: { jlong num; int most_negative = 0; fputs (" static const jlong ", out); print_field_name (out, jcf, name_index); fputs (" = ", out); num = JPOOL_LONG (jcf, current_field_value); /* We single out the most negative number to print specially.. This avoids later warnings from g++. */ if (num == (jlong) 0x8000000000000000LL) { most_negative = 1; ++num; } format_int (buffer, num, 10); fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :""); } break; case CONSTANT_Float: { jfloat fnum = JPOOL_FLOAT (jcf, current_field_value); fputs (" static const jfloat ", out); print_field_name (out, jcf, name_index); if (! java_float_finite (fnum)) fputs (";\n", out); else fprintf (out, " = %.10g;\n", fnum); } break; case CONSTANT_Double: { jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value); fputs (" static const jdouble ", out); print_field_name (out, jcf, name_index); if (! java_double_finite (dnum)) fputs (";\n", out); else fprintf (out, " = %.17g;\n", dnum); } break; default: /* We can't print this as a constant, but we can still print something sensible. */ done = 0; break; } if (done) return; } } fputs (" ", out); if ((flags & ACC_STATIC)) fputs ("static ", out); override = get_field_name (jcf, name_index, flags); print_c_decl (out, jcf, name_index, sig_index, 0, override); fputs (";\n", out); if (override) free (override);}static voidDEFUN(print_method_info, (stream, jcf, name_index, sig_index, flags), FILE *stream AND JCF* jcf AND int name_index AND int sig_index AND JCF_u2 flags){ unsigned char *str; int length, is_init = 0; const char *override = NULL; method_declared = 0; method_access = flags; if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8) fprintf (stream, "<not a UTF8 constant>"); str = JPOOL_UTF_DATA (jcf, name_index); length = JPOOL_UTF_LENGTH (jcf, name_index); if (str[0] == '<' || str[0] == '$') { /* Ignore internally generated methods like <clinit> and $finit$. However, treat <init> as a constructor. */ if (! utf8_cmp (str, length, "<init>")) is_init = 1; else if (! METHOD_IS_FINAL (jcf->access_flags, flags) && ! (flags & ACC_STATIC)) { /* FIXME: i18n bug here. Order of prints should not be fixed. */ fprintf (stderr, "ignored method `"); jcf_print_utf8 (stderr, str, length); fprintf (stderr, "' marked virtual\n"); found_error = 1; return; } else return; } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -