1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
26 #include <java-signal.h>
27 #include <java-threads.h>
28 #include <java-interp.h>
32 #include <java/lang/ThreadGroup.h>
35 #ifndef DISABLE_GETENV_PROPERTIES
37 #include <java-props.h>
38 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
40 #define PROCESS_GCJ_PROPERTIES
41 #endif // DISABLE_GETENV_PROPERTIES
43 #include <java/lang/Class.h>
44 #include <java/lang/ClassLoader.h>
45 #include <java/lang/Runtime.h>
46 #include <java/lang/String.h>
47 #include <java/lang/Thread.h>
48 #include <java/lang/ThreadGroup.h>
49 #include <java/lang/ArrayIndexOutOfBoundsException.h>
50 #include <java/lang/ArithmeticException.h>
51 #include <java/lang/ClassFormatError.h>
52 #include <java/lang/InternalError.h>
53 #include <java/lang/NegativeArraySizeException.h>
54 #include <java/lang/NullPointerException.h>
55 #include <java/lang/OutOfMemoryError.h>
56 #include <java/lang/System.h>
57 #include <java/lang/VMThrowable.h>
58 #include <java/lang/reflect/Modifier.h>
59 #include <java/io/PrintStream.h>
60 #include <java/lang/UnsatisfiedLinkError.h>
61 #include <java/lang/VirtualMachineError.h>
62 #include <gnu/gcj/runtime/VMClassLoader.h>
63 #include <gnu/gcj/runtime/FinalizerThread.h>
64 #include <gnu/java/lang/MainThread.h>
70 // We allocate a single OutOfMemoryError exception which we keep
71 // around for use if we run out of memory.
72 static java::lang::OutOfMemoryError *no_memory;
74 // Number of bytes in largest array object we create. This could be
75 // increased to the largest size_t value, so long as the appropriate
76 // functions are changed to take a size_t argument instead of jint.
77 #define MAX_OBJECT_SIZE ((1<<31) - 1)
79 static const char *no_properties[] = { NULL };
81 // Properties set at compile time.
82 const char **_Jv_Compiler_Properties = no_properties;
84 // The JAR file to add to the beginning of java.class.path.
85 const char *_Jv_Jar_Class_Path;
87 #ifndef DISABLE_GETENV_PROPERTIES
88 // Property key/value pairs.
89 property_pair *_Jv_Environment_Properties;
92 // Stash the argv pointer to benefit native libraries that need it.
93 const char **_Jv_argv;
100 // _Jv_argc is 0 if not explicitly initialized.
105 _Jv_GetSafeArg (int index)
107 if (index >=0 && index < _Jv_GetNbArgs ())
108 return _Jv_argv[index];
114 _Jv_SetArgs (int argc, const char **argv)
121 // Pointer to JVMPI notification functions.
122 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
123 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
124 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
128 /* Unblock a signal. Unless we do this, the signal may only be sent
131 unblock_signal (int signum)
133 #ifdef _POSIX_VERSION
137 sigaddset (&sigs, signum);
138 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
143 SIGNAL_HANDLER (catch_segv)
145 java::lang::NullPointerException *nullp
146 = new java::lang::NullPointerException;
147 unblock_signal (SIGSEGV);
148 MAKE_THROW_FRAME (nullp);
154 SIGNAL_HANDLER (catch_fpe)
156 java::lang::ArithmeticException *arithexception
157 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
158 unblock_signal (SIGFPE);
159 #ifdef HANDLE_DIVIDE_OVERFLOW
160 HANDLE_DIVIDE_OVERFLOW;
162 MAKE_THROW_FRAME (arithexception);
164 throw arithexception;
171 _Jv_equalUtf8Consts (const Utf8Const* a, const Utf8Const *b)
174 const _Jv_ushort *aptr, *bptr;
177 if (a->hash != b->hash)
180 if (b->length != len)
182 aptr = (const _Jv_ushort *)a->data;
183 bptr = (const _Jv_ushort *)b->data;
184 len = (len + 1) >> 1;
186 if (*aptr++ != *bptr++)
191 /* True iff A is equal to STR.
192 HASH is STR->hashCode().
196 _Jv_equal (Utf8Const* a, jstring str, jint hash)
198 if (a->hash != (_Jv_ushort) hash)
200 jint len = str->length();
202 jchar *sptr = _Jv_GetStringChars (str);
203 unsigned char* ptr = (unsigned char*) a->data;
204 unsigned char* limit = ptr + a->length;
207 int ch = UTF8_GET (ptr, limit);
216 /* Like _Jv_equal, but stop after N characters. */
218 _Jv_equaln (Utf8Const *a, jstring str, jint n)
220 jint len = str->length();
222 jchar *sptr = _Jv_GetStringChars (str);
223 unsigned char* ptr = (unsigned char*) a->data;
224 unsigned char* limit = ptr + a->length;
225 for (; n-- > 0; i++, sptr++)
227 int ch = UTF8_GET (ptr, limit);
236 /* Count the number of Unicode chars encoded in a given Ut8 string. */
238 _Jv_strLengthUtf8(char* str, int len)
241 unsigned char* limit;
244 ptr = (unsigned char*) str;
247 for (; ptr < limit; str_length++)
249 if (UTF8_GET (ptr, limit) < 0)
255 /* Calculate a hash value for a string encoded in Utf8 format.
256 * This returns the same hash value as specified or java.lang.String.hashCode.
259 hashUtf8String (char* str, int len)
261 unsigned char* ptr = (unsigned char*) str;
262 unsigned char* limit = ptr + len;
267 int ch = UTF8_GET (ptr, limit);
268 /* Updated specification from
269 http://www.javasoft.com/docs/books/jls/clarify.html. */
270 hash = (31 * hash) + ch;
276 _Jv_makeUtf8Const (char* s, int len)
280 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
281 memcpy (m->data, s, len);
284 m->hash = hashUtf8String (s, len) & 0xFFFF;
289 _Jv_makeUtf8Const (jstring string)
291 jint hash = string->hashCode ();
292 jint len = _Jv_GetStringUTFLength (string);
294 Utf8Const* m = (Utf8Const*)
295 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
300 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
310 _Jv_Abort (const char *function, const char *file, int line,
314 _Jv_Abort (const char *, const char *, int, const char *message)
319 "libgcj failure: %s\n in function %s, file %s, line %d\n",
320 message, function, file, line);
322 fprintf (stderr, "libgcj failure: %s\n", message);
328 fail_on_finalization (jobject)
330 JvFail ("object was finalized");
334 _Jv_GCWatch (jobject obj)
336 _Jv_RegisterFinalizer (obj, fail_on_finalization);
340 _Jv_ThrowBadArrayIndex(jint bad_index)
342 throw new java::lang::ArrayIndexOutOfBoundsException
343 (java::lang::String::valueOf (bad_index));
347 _Jv_ThrowNullPointerException ()
349 throw new java::lang::NullPointerException;
352 // Explicitly throw a no memory exception.
353 // The collector calls this when it encounters an out-of-memory condition.
354 void _Jv_ThrowNoMemory()
361 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
363 // Service JVMPI allocation request.
364 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
368 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
370 event.u.obj_alloc.arena_id = 0;
371 event.u.obj_alloc.class_id = (jobjectID) klass;
372 event.u.obj_alloc.is_array = 0;
373 event.u.obj_alloc.size = size;
374 event.u.obj_alloc.obj_id = (jobjectID) obj;
376 // FIXME: This doesn't look right for the Boehm GC. A GC may
377 // already be in progress. _Jv_DisableGC () doesn't wait for it.
378 // More importantly, I don't see the need for disabling GC, since we
379 // blatantly have a pointer to obj on our stack, ensuring that the
380 // object can't be collected. Even for a nonconservative collector,
381 // it appears to me that this must be true, since we are about to
382 // return obj. Isn't this whole approach way too intrusive for
383 // a useful profiling interface? - HB
385 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
389 #else /* !ENABLE_JVMPI */
390 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
393 // Allocate a new object of class KLASS.
394 // First a version that assumes that we have no finalizer, and that
395 // the class is already initialized.
396 // If we know that JVMPI is disabled, this can be replaced by a direct call
397 // to the allocator for the appropriate GC.
399 _Jv_AllocObjectNoInitNoFinalizer (jclass klass)
401 jint size = klass->size ();
402 jobject obj = (jobject) _Jv_AllocObj (size, klass);
403 jvmpi_notify_alloc (klass, size, obj);
407 // And now a version that initializes if necessary.
409 _Jv_AllocObjectNoFinalizer (jclass klass)
411 _Jv_InitClass (klass);
412 jint size = klass->size ();
413 jobject obj = (jobject) _Jv_AllocObj (size, klass);
414 jvmpi_notify_alloc (klass, size, obj);
418 // And now the general version that registers a finalizer if necessary.
420 _Jv_AllocObject (jclass klass)
422 jobject obj = _Jv_AllocObjectNoFinalizer (klass);
424 // We assume that the compiler only generates calls to this routine
425 // if there really is an interesting finalizer.
426 // Unfortunately, we still have to the dynamic test, since there may
427 // be cni calls to this routine.
428 // Note that on IA64 get_finalizer() returns the starting address of the
429 // function, not a function pointer. Thus this still works.
430 if (klass->vtable->get_finalizer ()
431 != java::lang::Object::class$.vtable->get_finalizer ())
432 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
436 // Allocate a String, including variable length storage.
438 _Jv_AllocString(jsize len)
440 using namespace java::lang;
442 jsize sz = sizeof(java::lang::String) + len * sizeof(jchar);
444 // We assert that for strings allocated this way, the data field
445 // will always point to the object itself. Thus there is no reason
446 // for the garbage collector to scan any of it.
447 // Furthermore, we're about to overwrite the string data, so
448 // initialization of the object is not an issue.
450 // String needs no initialization, and there is no finalizer, so
451 // we can go directly to the collector's allocator interface.
452 jstring obj = (jstring) _Jv_AllocPtrFreeObj(sz, &String::class$);
455 obj->boffset = sizeof(java::lang::String);
457 obj->cachedHashCode = 0;
460 // Service JVMPI request.
462 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
466 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
468 event.u.obj_alloc.arena_id = 0;
469 event.u.obj_alloc.class_id = (jobjectID) &String::class$;
470 event.u.obj_alloc.is_array = 0;
471 event.u.obj_alloc.size = sz;
472 event.u.obj_alloc.obj_id = (jobjectID) obj;
475 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
483 // A version of the above that assumes the object contains no pointers,
484 // and requires no finalization. This can't happen if we need pointers
486 #ifdef JV_HASH_SYNCHRONIZATION
488 _Jv_AllocPtrFreeObject (jclass klass)
490 _Jv_InitClass (klass);
491 jint size = klass->size ();
493 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
496 // Service JVMPI request.
498 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
502 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
504 event.u.obj_alloc.arena_id = 0;
505 event.u.obj_alloc.class_id = (jobjectID) klass;
506 event.u.obj_alloc.is_array = 0;
507 event.u.obj_alloc.size = size;
508 event.u.obj_alloc.obj_id = (jobjectID) obj;
511 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
518 #endif /* JV_HASH_SYNCHRONIZATION */
521 // Allocate a new array of Java objects. Each object is of type
522 // `elementClass'. `init' is used to initialize each slot in the
525 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
527 if (__builtin_expect (count < 0, false))
528 throw new java::lang::NegativeArraySizeException;
530 JvAssert (! elementClass->isPrimitive ());
532 // Ensure that elements pointer is properly aligned.
533 jobjectArray obj = NULL;
534 size_t size = (size_t) elements (obj);
535 // Check for overflow.
536 if (__builtin_expect ((size_t) count >
537 (MAX_OBJECT_SIZE - 1 - size) / sizeof (jobject), false))
540 size += count * sizeof (jobject);
542 jclass klass = _Jv_GetArrayClass (elementClass,
543 elementClass->getClassLoaderInternal());
545 obj = (jobjectArray) _Jv_AllocArray (size, klass);
547 jsize *lp = const_cast<jsize *> (&obj->length);
549 // We know the allocator returns zeroed memory. So don't bother
553 jobject *ptr = elements(obj);
560 // Allocate a new array of primitives. ELTYPE is the type of the
561 // element, COUNT is the size of the array.
563 _Jv_NewPrimArray (jclass eltype, jint count)
565 int elsize = eltype->size();
566 if (__builtin_expect (count < 0, false))
567 throw new java::lang::NegativeArraySizeException;
569 JvAssert (eltype->isPrimitive ());
570 jobject dummy = NULL;
571 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
573 // Check for overflow.
574 if (__builtin_expect ((size_t) count >
575 (MAX_OBJECT_SIZE - size) / elsize, false))
578 jclass klass = _Jv_GetArrayClass (eltype, 0);
580 # ifdef JV_HASH_SYNCHRONIZATION
581 // Since the vtable is always statically allocated,
582 // these are completely pointerfree! Make sure the GC doesn't touch them.
584 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
585 memset((char *)arr + size, 0, elsize * count);
587 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
588 // Note that we assume we are given zeroed memory by the allocator.
591 jsize *lp = const_cast<jsize *> (&arr->length);
598 _Jv_NewArray (jint type, jint size)
602 case 4: return JvNewBooleanArray (size);
603 case 5: return JvNewCharArray (size);
604 case 6: return JvNewFloatArray (size);
605 case 7: return JvNewDoubleArray (size);
606 case 8: return JvNewByteArray (size);
607 case 9: return JvNewShortArray (size);
608 case 10: return JvNewIntArray (size);
609 case 11: return JvNewLongArray (size);
611 throw new java::lang::InternalError
612 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
615 // Allocate a possibly multi-dimensional array but don't check that
616 // any array length is <0.
618 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
620 JvAssert (type->isArray());
621 jclass element_type = type->getComponentType();
623 if (element_type->isPrimitive())
624 result = _Jv_NewPrimArray (element_type, sizes[0]);
626 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
630 JvAssert (! element_type->isPrimitive());
631 JvAssert (element_type->isArray());
632 jobject *contents = elements ((jobjectArray) result);
633 for (int i = 0; i < sizes[0]; ++i)
634 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
642 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
644 for (int i = 0; i < dimensions; ++i)
646 throw new java::lang::NegativeArraySizeException;
648 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
652 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
655 jint sizes[dimensions];
656 va_start (args, dimensions);
657 for (int i = 0; i < dimensions; ++i)
659 jint size = va_arg (args, jint);
661 throw new java::lang::NegativeArraySizeException;
666 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
671 // Ensure 8-byte alignment, for hash synchronization.
672 #define DECLARE_PRIM_TYPE(NAME) \
673 _Jv_ArrayVTable _Jv_##NAME##VTable; \
674 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
676 DECLARE_PRIM_TYPE(byte)
677 DECLARE_PRIM_TYPE(short)
678 DECLARE_PRIM_TYPE(int)
679 DECLARE_PRIM_TYPE(long)
680 DECLARE_PRIM_TYPE(boolean)
681 DECLARE_PRIM_TYPE(char)
682 DECLARE_PRIM_TYPE(float)
683 DECLARE_PRIM_TYPE(double)
684 DECLARE_PRIM_TYPE(void)
687 _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len,
688 _Jv_ArrayVTable *array_vtable)
690 using namespace java::lang::reflect;
692 // We must set the vtable for the class; the Java constructor
694 (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
696 // Initialize the fields we care about. We do this in the same
697 // order they are declared in Class.h.
698 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
699 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
700 cl->method_count = sig;
701 cl->size_in_bytes = len;
702 cl->vtable = JV_PRIMITIVE_VTABLE;
703 cl->state = JV_STATE_DONE;
706 _Jv_NewArrayClass (cl, NULL, (_Jv_VTable *) array_vtable);
710 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
715 return JvPrimClass (byte);
717 return JvPrimClass (short);
719 return JvPrimClass (int);
721 return JvPrimClass (long);
723 return JvPrimClass (boolean);
725 return JvPrimClass (char);
727 return JvPrimClass (float);
729 return JvPrimClass (double);
731 return JvPrimClass (void);
735 for (i = 1; sig[i] && sig[i] != ';'; ++i)
737 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
738 return _Jv_FindClass (name, loader);
742 jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
745 return _Jv_GetArrayClass (klass, loader);
749 return NULL; // Placate compiler.
755 JvConvertArgv (int argc, const char **argv)
759 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
760 jobject *ptr = elements(ar);
761 jbyteArray bytes = NULL;
762 for (int i = 0; i < argc; i++)
764 const char *arg = argv[i];
765 int len = strlen (arg);
766 if (bytes == NULL || bytes->length < len)
767 bytes = JvNewByteArray (len);
768 jbyte *bytePtr = elements (bytes);
769 // We assume jbyte == char.
770 memcpy (bytePtr, arg, len);
772 // Now convert using the default encoding.
773 *ptr++ = new java::lang::String (bytes, 0, len);
775 return (JArray<jstring>*) ar;
778 // FIXME: These variables are static so that they will be
779 // automatically scanned by the Boehm collector. This is needed
780 // because with qthreads the collector won't scan the initial stack --
781 // it will only scan the qthreads stacks.
783 // Command line arguments.
784 static JArray<jstring> *arg_vec;
786 // The primary thread.
787 static java::lang::Thread *main_thread;
789 #ifndef DISABLE_GETENV_PROPERTIES
792 next_property_key (char *s, size_t *length)
798 // Skip over whitespace
802 // If we've reached the end, return NULL. Also return NULL if for
803 // some reason we've come across a malformed property string.
809 // Determine the length of the property key.
827 next_property_value (char *s, size_t *length)
843 // If we've reached the end, return NULL.
847 // Determine the length of the property value.
866 process_gcj_properties ()
868 char *props = getenv("GCJ_PROPERTIES");
871 size_t property_count = 0;
876 // Whip through props quickly in order to count the number of
878 while (p && (p = next_property_key (p, &length)))
880 // Skip to the end of the key
883 p = next_property_value (p, &length);
890 // Allocate an array of property value/key pairs.
891 _Jv_Environment_Properties =
892 (property_pair *) malloc (sizeof(property_pair)
893 * (property_count + 1));
895 // Go through the properties again, initializing _Jv_Properties
899 while (p && (p = next_property_key (p, &length)))
901 _Jv_Environment_Properties[property_count].key = p;
902 _Jv_Environment_Properties[property_count].key_length = length;
904 // Skip to the end of the key
907 p = next_property_value (p, &length);
909 _Jv_Environment_Properties[property_count].value = p;
910 _Jv_Environment_Properties[property_count].value_length = length;
917 memset ((void *) &_Jv_Environment_Properties[property_count],
918 0, sizeof (property_pair));
922 // Null terminate the strings.
923 while (_Jv_Environment_Properties[i].key)
925 property_pair *prop = &_Jv_Environment_Properties[i];
926 prop->key[prop->key_length] = 0;
927 prop->value[prop->value_length] = 0;
932 #endif // DISABLE_GETENV_PROPERTIES
936 _Jv_Utf8Const *void_signature;
937 _Jv_Utf8Const *clinit_name;
938 _Jv_Utf8Const *init_name;
939 _Jv_Utf8Const *finit_name;
941 bool runtimeInitialized = false;
945 _Jv_CreateJavaVM (void* /*vm_args*/)
949 if (runtimeInitialized)
952 runtimeInitialized = true;
954 PROCESS_GCJ_PROPERTIES;
958 _Jv_InitializeSyncMutex ();
961 _Jv_InitInterpreter ();
972 /* Initialize Utf8 constants declared in jvm.h. */
973 void_signature = _Jv_makeUtf8Const ("()V", 3);
974 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
975 init_name = _Jv_makeUtf8Const ("<init>", 6);
976 finit_name = _Jv_makeUtf8Const ("finit$", 6);
978 /* Initialize built-in classes to represent primitive TYPEs. */
979 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1, &_Jv_byteVTable);
980 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2, &_Jv_shortVTable);
981 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4, &_Jv_intVTable);
982 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8, &_Jv_longVTable);
983 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1, &_Jv_booleanVTable);
984 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2, &_Jv_charVTable);
985 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4, &_Jv_floatVTable);
986 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8, &_Jv_doubleVTable);
987 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0, &_Jv_voidVTable);
989 // Turn stack trace generation off while creating exception objects.
990 _Jv_InitClass (&java::lang::VMThrowable::class$);
991 java::lang::VMThrowable::trace_enabled = 0;
993 // We have to initialize this fairly early, to avoid circular class
994 // initialization. In particular we want to start the
995 // initialization of ClassLoader before we start the initialization
997 _Jv_InitClass (&java::lang::ClassLoader::class$);
999 // Once the bootstrap loader is in place, change it into a kind of
1000 // system loader, by having it read the class path.
1001 gnu::gcj::runtime::VMClassLoader::initialize();
1003 no_memory = new java::lang::OutOfMemoryError;
1005 java::lang::VMThrowable::trace_enabled = 1;
1008 LTDL_SET_PRELOADED_SYMBOLS ();
1011 _Jv_platform_initialize ();
1015 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
1017 // Start the GC finalizer thread. A VirtualMachineError can be
1018 // thrown by the runtime if, say, threads aren't available.
1021 using namespace gnu::gcj::runtime;
1022 FinalizerThread *ft = new FinalizerThread ();
1025 catch (java::lang::VirtualMachineError *ignore)
1033 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
1036 #ifndef DISABLE_MAIN_ARGS
1037 _Jv_SetArgs (argc, argv);
1040 java::lang::Runtime *runtime = NULL;
1044 // Set this very early so that it is seen when java.lang.System
1047 _Jv_Jar_Class_Path = strdup (name);
1048 _Jv_CreateJavaVM (NULL);
1050 // Get the Runtime here. We want to initialize it before searching
1051 // for `main'; that way it will be set up if `main' is a JNI method.
1052 runtime = java::lang::Runtime::getRuntime ();
1054 #ifdef DISABLE_MAIN_ARGS
1055 arg_vec = JvConvertArgv (0, 0);
1057 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1060 using namespace gnu::java::lang;
1062 main_thread = new MainThread (klass, arg_vec);
1064 main_thread = new MainThread (JvNewStringLatin1 (name),
1067 catch (java::lang::Throwable *t)
1069 java::lang::System::err->println (JvNewStringLatin1
1070 ("Exception during runtime initialization"));
1071 t->printStackTrace();
1075 _Jv_AttachCurrentThread (main_thread);
1076 _Jv_ThreadRun (main_thread);
1079 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1080 runtime->exit (status);
1084 JvRunMain (jclass klass, int argc, const char **argv)
1086 _Jv_RunMain (klass, NULL, argc, argv, false);
1091 // Parse a string and return a heap size.
1093 parse_heap_size (const char *spec)
1096 unsigned long val = strtoul (spec, &end, 10);
1097 if (*end == 'k' || *end == 'K')
1099 else if (*end == 'm' || *end == 'M')
1101 return (size_t) val;
1104 // Set the initial heap size. This might be ignored by the GC layer.
1105 // This must be called before _Jv_RunMain.
1107 _Jv_SetInitialHeapSize (const char *arg)
1109 size_t size = parse_heap_size (arg);
1110 _Jv_GCSetInitialHeapSize (size);
1113 // Set the maximum heap size. This might be ignored by the GC layer.
1114 // This must be called before _Jv_RunMain.
1116 _Jv_SetMaximumHeapSize (const char *arg)
1118 size_t size = parse_heap_size (arg);
1119 _Jv_GCSetMaximumHeapSize (size);
1125 _Jv_Malloc (jsize size)
1127 if (__builtin_expect (size == 0, false))
1129 void *ptr = malloc ((size_t) size);
1130 if (__builtin_expect (ptr == NULL, false))
1136 _Jv_Realloc (void *ptr, jsize size)
1138 if (__builtin_expect (size == 0, false))
1140 ptr = realloc (ptr, (size_t) size);
1141 if (__builtin_expect (ptr == NULL, false))
1147 _Jv_MallocUnchecked (jsize size)
1149 if (__builtin_expect (size == 0, false))
1151 return malloc ((size_t) size);
1155 _Jv_Free (void* ptr)
1162 // In theory, these routines can be #ifdef'd away on machines which
1163 // support divide overflow signals. However, we never know if some
1164 // code might have been compiled with "-fuse-divide-subroutine", so we
1165 // always include them in libgcj.
1168 _Jv_divI (jint dividend, jint divisor)
1170 if (__builtin_expect (divisor == 0, false))
1172 java::lang::ArithmeticException *arithexception
1173 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1174 throw arithexception;
1177 if (dividend == (jint) 0x80000000L && divisor == -1)
1180 return dividend / divisor;
1184 _Jv_remI (jint dividend, jint divisor)
1186 if (__builtin_expect (divisor == 0, false))
1188 java::lang::ArithmeticException *arithexception
1189 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1190 throw arithexception;
1193 if (dividend == (jint) 0x80000000L && divisor == -1)
1196 return dividend % divisor;
1200 _Jv_divJ (jlong dividend, jlong divisor)
1202 if (__builtin_expect (divisor == 0, false))
1204 java::lang::ArithmeticException *arithexception
1205 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1206 throw arithexception;
1209 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1212 return dividend / divisor;
1216 _Jv_remJ (jlong dividend, jlong divisor)
1218 if (__builtin_expect (divisor == 0, false))
1220 java::lang::ArithmeticException *arithexception
1221 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1222 throw arithexception;
1225 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1228 return dividend % divisor;
1233 // Return true if SELF_KLASS can access a field or method in
1234 // OTHER_KLASS. The field or method's access flags are specified in
1237 _Jv_CheckAccess (jclass self_klass, jclass other_klass, jint flags)
1239 using namespace java::lang::reflect;
1240 return ((self_klass == other_klass)
1241 || ((flags & Modifier::PUBLIC) != 0)
1242 || (((flags & Modifier::PROTECTED) != 0)
1243 && other_klass->isAssignableFrom (self_klass))
1244 || (((flags & Modifier::PRIVATE) == 0)
1245 && _Jv_ClassNameSamePackage (self_klass->name,
1246 other_klass->name)));