// defineclass.cc - defining a class from .class format.
-/* Copyright (C) 1999, 2000, 2001 Free Software Foundation
+/* Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
This file is part of libgcj.
#include <java-interp.h>
-#ifdef INTERPRETER
-
#include <stdlib.h>
#include <java-cpool.h>
#include <gcj/cni.h>
#include <java/lang/ClassFormatError.h>
#include <java/lang/NoClassDefFoundError.h>
#include <java/lang/ClassCircularityError.h>
-#include <java/lang/ClassNotFoundException.h>
#include <java/lang/IncompatibleClassChangeError.h>
#include <java/lang/reflect/Modifier.h>
-// we don't verify method names that match these.
-static _Jv_Utf8Const *clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
-static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
+using namespace gcj;
+#ifdef INTERPRETER
-// these go in some seperate functions, to avoid having _Jv_InitClass
+// these go in some separate functions, to avoid having _Jv_InitClass
// inserted all over the place.
static void throw_internal_error (char *msg)
__attribute__ ((__noreturn__));
static void throw_class_circularity_error (jstring msg)
__attribute__ ((__noreturn__));
-static jdouble long_bits_to_double (jlong);
-static jfloat int_bits_to_float (jint);
-
/**
* We define class reading using a class. It is practical, since then
* the entire class-reader can be a friend of class Class (it needs to
// allways on. You always want this as far as I can see, but it also
// controls weither identifiers and type descriptors/signatures are
// verified as legal. This could be somewhat more expensive since it
- // will call Characher.isJavaIdentifier{Start,Part} for each character
+ // will call Character.isJavaIdentifier{Start,Part} for each character
// in any identifier (field name or method name) it comes by. Thus,
// it might be useful to turn off this verification for classes that
// come from a trusted source. However, for GCJ, trusted classes are
*/
};
-/* This is used for the isJavaIdentifierStart & isJavaIdentifierPart
- methods, so we avoid doing _Jv_InitClass all the time */
-
-static const java::lang::Character *character = 0;
-static void prepare_character ();
-
void
_Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length)
{
- if (character == 0)
- prepare_character ();
-
_Jv_ClassReader reader (klass, data, offset, length);
reader.parse();
/* that's it! */
}
-/** put it after _Jv_DefineClass, so it doesn't get inlined */
-static void prepare_character ()
-{
- character = new java::lang::Character ('!');
-}
-
\f
/** This section defines the parsing/scanning of the class data */
int name_index = read2u ();
int descriptor_index = read2u ();
int attributes_count = read2u ();
-
+
check_tag (name_index, JV_CONSTANT_Utf8);
prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
check_tag (descriptor_index, JV_CONSTANT_Utf8);
prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
-
+
handleField (i, access_flags, name_index, descriptor_index);
-
+
for (int j = 0; j < attributes_count; j++)
{
read_one_field_attribute (i);
check_tag (name_index, JV_CONSTANT_Utf8);
prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
-
+
handleMethod (i, access_flags, name_index,
descriptor_index);
-
+
for (int j = 0; j < attributes_count; j++)
{
read_one_method_attribute (i);
if (start_pc > end_pc
|| start_pc < 0
- || end_pc >= code_length
+ // END_PC can be equal to CODE_LENGTH.
+ // See JVM Spec 4.7.4.
+ || end_pc > code_length
|| handler_pc >= code_length)
throw_class_format_error ("erroneous exception handler info");
case JV_CONSTANT_Float:
{
- jfloat f = int_bits_to_float ((jint) get4 (this_data));
+ jfloat f = java::lang::Float::intBitsToFloat ((jint) get4 (this_data));
_Jv_storeFloat (&pool_data[index], f);
pool_tags[index] = JV_CONSTANT_Float;
}
case JV_CONSTANT_Double:
{
- jdouble d = long_bits_to_double ((jlong) get8 (this_data));
+ jdouble d
+ = java::lang::Double::longBitsToDouble ((jlong) get8 (this_data));
_Jv_storeDouble (&pool_data[index], d);
pool_tags[index] = JV_CONSTANT_Double;
}
throw_no_class_def_found_error (msg);
}
- def->accflags = access_flags;
+ def->accflags = access_flags | java::lang::reflect::Modifier::INTERPRETED;
pool_data[this_class].clazz = def;
pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
- if (super_class == 0)
+ if (super_class == 0 && ! (access_flags & Modifier::INTERFACE))
{
- // interfaces have java.lang.Object as super.
- if (access_flags & Modifier::INTERFACE)
- {
- def->superclass = (jclass)&java::lang::Class::class$;
- }
-
// FIXME: Consider this carefully!
- else if (!_Jv_equalUtf8Consts (def->name,
- java::lang::Class::class$.name))
- {
- throw_no_class_def_found_error ("loading java.lang.Object");
- }
+ if (! _Jv_equalUtf8Consts (def->name, java::lang::Object::class$.name))
+ throw_no_class_def_found_error ("loading java.lang.Object");
}
// In the pre-loading state, it can be looked up in the
// to include references to this class.
def->state = JV_STATE_PRELOADING;
- _Jv_RegisterClass (def);
+
+ {
+ JvSynchronize sync (&java::lang::Class::class$);
+ _Jv_RegisterClass (def);
+ }
if (super_class != 0)
{
- // load the super class
+ // Load the superclass.
check_tag (super_class, JV_CONSTANT_Class);
_Jv_Utf8Const* super_name = pool_data[super_class].utf8;
- // load the super class using our defining loader
+ // Load the superclass using our defining loader.
jclass the_super = _Jv_FindClass (super_name,
def->loader);
// This will establish that we are allowed to be a subclass,
- // and check for class circularity error
+ // and check for class circularity error.
checkExtends (def, the_super);
- def->superclass = the_super;
+ // Note: for an interface we will find Object as the
+ // superclass. We still check it above to ensure class file
+ // validity, but we simply assign `null' to the actual field in
+ // this case.
+ def->superclass = (((access_flags & Modifier::INTERFACE))
+ ? NULL : the_super);
pool_data[super_class].clazz = the_super;
pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
}
-
- // now we've come past the circularity problem, we can
- // now say that we're loading...
+
+ // Now we've come past the circularity problem, we can
+ // now say that we're loading.
def->state = JV_STATE_LOADING;
def->notifyAll ();
field->nameIndex = name;
#endif
- if (verify)
- verify_identifier (field_name);
-
- // ignore flags we don't know about.
+ // Ignore flags we don't know about.
field->flags = flags & Modifier::ALL_FLAGS;
+ _Jv_Utf8Const* sig = pool_data[desc].utf8;
+
if (verify)
{
+ verify_identifier (field_name);
+
+ for (int i = 0; i < field_no; ++i)
+ {
+ if (_Jv_equalUtf8Consts (field_name, def->fields[i].name)
+ && _Jv_equalUtf8Consts (sig,
+ // We know the other fields are
+ // unresolved.
+ (_Jv_Utf8Const *) def->fields[i].type))
+ throw_class_format_error ("duplicate field name");
+ }
+
if (field->flags & (Modifier::SYNCHRONIZED
| Modifier::NATIVE
| Modifier::INTERFACE
throw_class_format_error ("erroneous field access flags");
}
- _Jv_Utf8Const* sig = pool_data[desc].utf8;
-
if (verify)
_Jv_VerifyFieldSignature (sig);
void
_Jv_ClassReader::handleMethodsBegin (int count)
{
- def->methods = (_Jv_Method*)
- _Jv_AllocBytes (sizeof (_Jv_Method)*count);
+ def->methods = (_Jv_Method *) _Jv_AllocBytes (sizeof (_Jv_Method) * count);
def->interpreted_methods
= (_Jv_MethodBase **) _Jv_AllocBytes (sizeof (_Jv_MethodBase *)
* count);
for (int i = 0; i < count; i++)
- def->interpreted_methods[i] = 0;
+ {
+ def->interpreted_methods[i] = 0;
+ def->methods[i].index = (_Jv_ushort) -1;
+ }
def->method_count = count;
}
_Jv_VerifyMethodSignature (method->signature);
+ for (int i = 0; i < mth_index; ++i)
+ {
+ if (_Jv_equalUtf8Consts (method->name, def->methods[i].name)
+ && _Jv_equalUtf8Consts (method->signature,
+ def->methods[i].signature))
+ throw_class_format_error ("duplicate method");
+ }
+
if (method->accflags & (Modifier::VOLATILE
| Modifier::TRANSIENT
| Modifier::INTERFACE))
_Jv_InterpMethod *method =
(_Jv_InterpMethod*) (_Jv_AllocBytes (size));
+ method->deferred = NULL;
method->max_stack = max_stack;
method->max_locals = max_locals;
method->code_length = code_length;
method->exc_count = exc_table_length;
method->defining_class = def;
method->self = &def->methods[method_index];
+ method->prepared = NULL;
// grab the byte code!
memcpy ((void*) method->bytecode (),
(void*) (bytes+code_start),
code_length);
-
+
def->interpreted_methods[method_index] = method;
- /* that's all we do for now */
+ if ((method->self->accflags & java::lang::reflect::Modifier::STATIC))
+ {
+ // Precompute the ncode field for a static method. This lets us
+ // call a static method of an interpreted class from precompiled
+ // code without first resolving the class (that will happen
+ // during class initialization instead).
+ method->self->ncode = method->ncode ();
+ }
}
-void _Jv_ClassReader::handleExceptionTableEntry
+void _Jv_ClassReader::handleExceptionTableEntry
(int method_index, int exc_index,
int start_pc, int end_pc, int handler_pc, int catch_type)
{
(def->interpreted_methods[method_index]);
_Jv_InterpException *exc = method->exceptions ();
- exc[exc_index].start_pc = start_pc;
- exc[exc_index].end_pc = end_pc;
- exc[exc_index].handler_pc = handler_pc;
- exc[exc_index].handler_type = catch_type;
+ exc[exc_index].start_pc.i = start_pc;
+ exc[exc_index].end_pc.i = end_pc;
+ exc[exc_index].handler_pc.i = handler_pc;
+ exc[exc_index].handler_type.i = catch_type;
}
void _Jv_ClassReader::handleMethodsEnd ()
m->self = method;
m->function = NULL;
def->interpreted_methods[i] = m;
+ m->deferred = NULL;
+
+ if ((method->accflags & Modifier::STATIC))
+ {
+ // Precompute the ncode field for a static method.
+ // This lets us call a static method of an
+ // interpreted class from precompiled code without
+ // first resolving the class (that will happen
+ // during class initialization instead).
+ method->ncode = m->ncode ();
+ }
}
}
else if ((method->accflags & Modifier::ABSTRACT) != 0)
throw_class_format_error ("method with no code");
}
}
-
}
void _Jv_ClassReader::throw_class_format_error (char *msg)
::throw_class_format_error (str);
}
\f
+/** Here we define the exceptions that can be thrown */
+
+static void
+throw_no_class_def_found_error (jstring msg)
+{
+ throw (msg
+ ? new java::lang::NoClassDefFoundError (msg)
+ : new java::lang::NoClassDefFoundError);
+}
+
+static void
+throw_no_class_def_found_error (char *msg)
+{
+ throw_no_class_def_found_error (JvNewStringLatin1 (msg));
+}
+
+static void
+throw_class_format_error (jstring msg)
+{
+ throw (msg
+ ? new java::lang::ClassFormatError (msg)
+ : new java::lang::ClassFormatError);
+}
+
+static void
+throw_internal_error (char *msg)
+{
+ throw new java::lang::InternalError (JvNewStringLatin1 (msg));
+}
+
+static void
+throw_incompatible_class_change_error (jstring msg)
+{
+ throw new java::lang::IncompatibleClassChangeError (msg);
+}
+
+static void
+throw_class_circularity_error (jstring msg)
+{
+ throw new java::lang::ClassCircularityError (msg);
+}
+
+#endif /* INTERPRETER */
+
+\f
+
/** This section takes care of verifying integrity of identifiers,
signatures, field ddescriptors, and class names */
int xxch = UTF8_GET(PTR,LIMIT); \
PTR = xxkeep; xxch; })
-/* verify one element of a type descriptor or signature */
+/* Verify one element of a type descriptor or signature. */
static unsigned char*
_Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
{
switch (ch)
{
case 'V':
- if (! void_ok) return 0;
+ if (! void_ok)
+ return 0;
case 'S': case 'B': case 'I': case 'J':
case 'Z': case 'C': case 'F': case 'D':
case 'L':
{
unsigned char *start = ptr, *end;
- do {
- if (ptr > limit)
- return 0;
-
- end = ptr;
-
- if ((ch = UTF8_GET (ptr, limit)) == -1)
- return 0;
-
- } while (ch != ';');
+ do
+ {
+ if (ptr > limit)
+ return 0;
+
+ end = ptr;
+
+ if ((ch = UTF8_GET (ptr, limit)) == -1)
+ return 0;
+
+ }
+ while (ch != ';');
if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
return 0;
}
case '[':
return _Jv_VerifyOne (ptr, limit, false);
break;
-
+
default:
return 0;
}
return ptr;
-
}
-
-/** verification and loading procedures **/
-
+/* Verification and loading procedures. */
bool
_Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
{
while (ptr && UTF8_PEEK (ptr, limit) != ')')
ptr = _Jv_VerifyOne (ptr, limit, false);
-
+
if (UTF8_GET (ptr, limit) != ')')
return false;
return ptr == limit;
}
-/* we try to avoid calling the Character methods all the time,
- in fact, they will only be called for non-standard things */
-
+/* We try to avoid calling the Character methods all the time, in
+ fact, they will only be called for non-standard things. */
static __inline__ int
is_identifier_start (int c)
{
if (ch == 0x5FU) /* _ */
return 1;
- return character->isJavaIdentifierStart ((jchar) ch);
+ return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
}
static __inline__ int
if (ch == 0x5FU || ch == 0x24U) /* _ $ */
return 1;
- return character->isJavaIdentifierStart ((jchar) ch);
+ return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
}
bool
if ('[' == UTF8_PEEK (ptr, limit))
{
- if (! _Jv_VerifyOne (++ptr, limit, false))
+ unsigned char *end = _Jv_VerifyOne (++ptr, limit, false);
+ // _Jv_VerifyOne must leave us looking at the terminating nul
+ // byte.
+ if (! end || *end)
return false;
else
return true;
(_Jv_ushort) name->length);
}
-/** returns true, if name1 and name2 represents classes in the same
- package. */
-
+/* Returns true, if NAME1 and NAME2 represent classes in the same
+ package. */
bool
_Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
{
if (ch1 == '.')
last1 = ptr1;
-
+
else if (ch1 == -1)
return false;
}
- // now the length of name1's package name is len
+ // Now the length of NAME1's package name is LEN.
int len = last1 - (unsigned char*) name1->data;
- // if this is longer than name2, then we're off
+ // If this is longer than NAME2, then we're off.
if (len > name2->length)
return false;
- // then compare the first len bytes for equality
+ // Then compare the first len bytes for equality.
if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0)
{
- // check that there are no .'s after position len in name2
+ // Check that there are no .'s after position LEN in NAME2.
unsigned char* ptr2 = (unsigned char*) name2->data + len;
unsigned char* limit2 =
}
return false;
}
-
-
-\f
-/** Here we define the exceptions that can be thrown */
-
-static void
-throw_no_class_def_found_error (jstring msg)
-{
- throw (msg
- ? new java::lang::NoClassDefFoundError (msg)
- : new java::lang::NoClassDefFoundError);
-}
-
-static void
-throw_no_class_def_found_error (char *msg)
-{
- throw_no_class_def_found_error (JvNewStringLatin1 (msg));
-}
-
-static void
-throw_class_format_error (jstring msg)
-{
- throw (msg
- ? new java::lang::ClassFormatError (msg)
- : new java::lang::ClassFormatError);
-}
-
-static void
-throw_internal_error (char *msg)
-{
- throw new java::lang::InternalError (JvNewStringLatin1 (msg));
-}
-
-static jfloat int_bits_to_float (jint value)
-{
- return java::lang::Float::intBitsToFloat (value);
-}
-
-static jdouble long_bits_to_double (jlong value)
-{
- return java::lang::Double::longBitsToDouble (value);
-}
-
-static void throw_incompatible_class_change_error (jstring msg)
-{
- throw new java::lang::IncompatibleClassChangeError (msg);
-}
-
-static void throw_class_circularity_error (jstring msg)
-{
- throw new java::lang::ClassCircularityError (msg);
-}
-
-#endif /* INTERPRETER */
-