1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 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>
31 #include <java/lang/ThreadGroup.h>
34 #ifndef DISABLE_GETENV_PROPERTIES
36 #include <java-props.h>
37 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
39 #define PROCESS_GCJ_PROPERTIES
40 #endif // DISABLE_GETENV_PROPERTIES
42 #include <java/lang/Class.h>
43 #include <java/lang/ClassLoader.h>
44 #include <java/lang/Runtime.h>
45 #include <java/lang/String.h>
46 #include <java/lang/Thread.h>
47 #include <java/lang/ThreadGroup.h>
48 #include <java/lang/ArrayIndexOutOfBoundsException.h>
49 #include <java/lang/ArithmeticException.h>
50 #include <java/lang/ClassFormatError.h>
51 #include <java/lang/InternalError.h>
52 #include <java/lang/NegativeArraySizeException.h>
53 #include <java/lang/NullPointerException.h>
54 #include <java/lang/OutOfMemoryError.h>
55 #include <java/lang/System.h>
56 #include <java/lang/VMThrowable.h>
57 #include <java/lang/reflect/Modifier.h>
58 #include <java/io/PrintStream.h>
59 #include <java/lang/UnsatisfiedLinkError.h>
60 #include <java/lang/VirtualMachineError.h>
61 #include <gnu/gcj/runtime/VMClassLoader.h>
62 #include <gnu/gcj/runtime/FinalizerThread.h>
63 #include <gnu/gcj/runtime/FirstThread.h>
69 // We allocate a single OutOfMemoryError exception which we keep
70 // around for use if we run out of memory.
71 static java::lang::OutOfMemoryError *no_memory;
73 // Largest representable size_t.
74 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
76 static const char *no_properties[] = { NULL };
78 // Properties set at compile time.
79 const char **_Jv_Compiler_Properties = no_properties;
81 // The JAR file to add to the beginning of java.class.path.
82 const char *_Jv_Jar_Class_Path;
84 #ifndef DISABLE_GETENV_PROPERTIES
85 // Property key/value pairs.
86 property_pair *_Jv_Environment_Properties;
89 // Stash the argv pointer to benefit native libraries that need it.
90 const char **_Jv_argv;
97 // _Jv_argc is 0 if not explicitly initialized.
102 _Jv_GetSafeArg (int index)
104 if (index >=0 && index < _Jv_GetNbArgs ())
105 return _Jv_argv[index];
111 _Jv_SetArgs (int argc, const char **argv)
118 // Pointer to JVMPI notification functions.
119 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
120 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
121 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
125 /* Unblock a signal. Unless we do this, the signal may only be sent
128 unblock_signal (int signum)
130 #ifdef _POSIX_VERSION
134 sigaddset (&sigs, signum);
135 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
140 SIGNAL_HANDLER (catch_segv)
142 java::lang::NullPointerException *nullp
143 = new java::lang::NullPointerException;
144 unblock_signal (SIGSEGV);
145 MAKE_THROW_FRAME (nullp);
151 SIGNAL_HANDLER (catch_fpe)
153 java::lang::ArithmeticException *arithexception
154 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
155 unblock_signal (SIGFPE);
156 #ifdef HANDLE_DIVIDE_OVERFLOW
157 HANDLE_DIVIDE_OVERFLOW;
159 MAKE_THROW_FRAME (arithexception);
161 throw arithexception;
168 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
171 _Jv_ushort *aptr, *bptr;
174 if (a->hash != b->hash)
177 if (b->length != len)
179 aptr = (_Jv_ushort *)a->data;
180 bptr = (_Jv_ushort *)b->data;
181 len = (len + 1) >> 1;
183 if (*aptr++ != *bptr++)
188 /* True iff A is equal to STR.
189 HASH is STR->hashCode().
193 _Jv_equal (Utf8Const* a, jstring str, jint hash)
195 if (a->hash != (_Jv_ushort) hash)
197 jint len = str->length();
199 jchar *sptr = _Jv_GetStringChars (str);
200 unsigned char* ptr = (unsigned char*) a->data;
201 unsigned char* limit = ptr + a->length;
204 int ch = UTF8_GET (ptr, limit);
213 /* Like _Jv_equal, but stop after N characters. */
215 _Jv_equaln (Utf8Const *a, jstring str, jint n)
217 jint len = str->length();
219 jchar *sptr = _Jv_GetStringChars (str);
220 unsigned char* ptr = (unsigned char*) a->data;
221 unsigned char* limit = ptr + a->length;
222 for (; n-- > 0; i++, sptr++)
224 int ch = UTF8_GET (ptr, limit);
233 /* Count the number of Unicode chars encoded in a given Ut8 string. */
235 _Jv_strLengthUtf8(char* str, int len)
238 unsigned char* limit;
241 ptr = (unsigned char*) str;
244 for (; ptr < limit; str_length++)
246 if (UTF8_GET (ptr, limit) < 0)
252 /* Calculate a hash value for a string encoded in Utf8 format.
253 * This returns the same hash value as specified or java.lang.String.hashCode.
256 hashUtf8String (char* str, int len)
258 unsigned char* ptr = (unsigned char*) str;
259 unsigned char* limit = ptr + len;
264 int ch = UTF8_GET (ptr, limit);
265 /* Updated specification from
266 http://www.javasoft.com/docs/books/jls/clarify.html. */
267 hash = (31 * hash) + ch;
273 _Jv_makeUtf8Const (char* s, int len)
277 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
278 memcpy (m->data, s, len);
281 m->hash = hashUtf8String (s, len) & 0xFFFF;
286 _Jv_makeUtf8Const (jstring string)
288 jint hash = string->hashCode ();
289 jint len = _Jv_GetStringUTFLength (string);
291 Utf8Const* m = (Utf8Const*)
292 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
297 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
307 _Jv_Abort (const char *function, const char *file, int line,
311 _Jv_Abort (const char *, const char *, int, const char *message)
316 "libgcj failure: %s\n in function %s, file %s, line %d\n",
317 message, function, file, line);
319 fprintf (stderr, "libgcj failure: %s\n", message);
325 fail_on_finalization (jobject)
327 JvFail ("object was finalized");
331 _Jv_GCWatch (jobject obj)
333 _Jv_RegisterFinalizer (obj, fail_on_finalization);
337 _Jv_ThrowBadArrayIndex(jint bad_index)
339 throw new java::lang::ArrayIndexOutOfBoundsException
340 (java::lang::String::valueOf (bad_index));
344 _Jv_ThrowNullPointerException ()
346 throw new java::lang::NullPointerException;
349 // Explicitly throw a no memory exception.
350 // The collector calls this when it encounters an out-of-memory condition.
351 void _Jv_ThrowNoMemory()
358 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
360 // Service JVMPI allocation request.
361 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
365 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
367 event.u.obj_alloc.arena_id = 0;
368 event.u.obj_alloc.class_id = (jobjectID) klass;
369 event.u.obj_alloc.is_array = 0;
370 event.u.obj_alloc.size = size;
371 event.u.obj_alloc.obj_id = (jobjectID) obj;
373 // FIXME: This doesn't look right for the Boehm GC. A GC may
374 // already be in progress. _Jv_DisableGC () doesn't wait for it.
375 // More importantly, I don't see the need for disabling GC, since we
376 // blatantly have a pointer to obj on our stack, ensuring that the
377 // object can't be collected. Even for a nonconservative collector,
378 // it appears to me that this must be true, since we are about to
379 // return obj. Isn't this whole approach way too intrusive for
380 // a useful profiling interface? - HB
382 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
386 #else /* !ENABLE_JVMPI */
387 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
390 // Allocate a new object of class KLASS. SIZE is the size of the object
391 // to allocate. You might think this is redundant, but it isn't; some
392 // classes, such as String, aren't of fixed size.
393 // First a version that assumes that we have no finalizer, and that
394 // the class is already initialized.
395 // If we know that JVMPI is disabled, this can be replaced by a direct call
396 // to the allocator for the appropriate GC.
398 _Jv_AllocObjectNoInitNoFinalizer (jclass klass, jint size)
400 jobject obj = (jobject) _Jv_AllocObj (size, klass);
401 jvmpi_notify_alloc (klass, size, obj);
405 // And now a version that initializes if necessary.
407 _Jv_AllocObjectNoFinalizer (jclass klass, jint size)
409 _Jv_InitClass (klass);
410 jobject obj = (jobject) _Jv_AllocObj (size, klass);
411 jvmpi_notify_alloc (klass, size, obj);
415 // And now the general version that registers a finalizer if necessary.
417 _Jv_AllocObject (jclass klass, jint size)
419 jobject obj = _Jv_AllocObjectNoFinalizer (klass, size);
421 // We assume that the compiler only generates calls to this routine
422 // if there really is an interesting finalizer.
423 // Unfortunately, we still have to the dynamic test, since there may
424 // be cni calls to this routine.
425 // Note that on IA64 get_finalizer() returns the starting address of the
426 // function, not a function pointer. Thus this still works.
427 if (klass->vtable->get_finalizer ()
428 != java::lang::Object::class$.vtable->get_finalizer ())
429 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
433 // A version of the above that assumes the object contains no pointers,
434 // and requires no finalization. This can't happen if we need pointers
436 #ifdef JV_HASH_SYNCHRONIZATION
438 _Jv_AllocPtrFreeObject (jclass klass, jint size)
440 _Jv_InitClass (klass);
442 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
445 // Service JVMPI request.
447 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
451 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
453 event.u.obj_alloc.arena_id = 0;
454 event.u.obj_alloc.class_id = (jobjectID) klass;
455 event.u.obj_alloc.is_array = 0;
456 event.u.obj_alloc.size = size;
457 event.u.obj_alloc.obj_id = (jobjectID) obj;
460 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
467 #endif /* JV_HASH_SYNCHRONIZATION */
470 // Allocate a new array of Java objects. Each object is of type
471 // `elementClass'. `init' is used to initialize each slot in the
474 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
476 if (__builtin_expect (count < 0, false))
477 throw new java::lang::NegativeArraySizeException;
479 JvAssert (! elementClass->isPrimitive ());
481 // Ensure that elements pointer is properly aligned.
482 jobjectArray obj = NULL;
483 size_t size = (size_t) elements (obj);
484 size += count * sizeof (jobject);
486 jclass klass = _Jv_GetArrayClass (elementClass,
487 elementClass->getClassLoaderInternal());
489 obj = (jobjectArray) _Jv_AllocArray (size, klass);
491 jsize *lp = const_cast<jsize *> (&obj->length);
493 // We know the allocator returns zeroed memory. So don't bother
497 jobject *ptr = elements(obj);
504 // Allocate a new array of primitives. ELTYPE is the type of the
505 // element, COUNT is the size of the array.
507 _Jv_NewPrimArray (jclass eltype, jint count)
509 int elsize = eltype->size();
510 if (__builtin_expect (count < 0, false))
511 throw new java::lang::NegativeArraySizeException;
513 JvAssert (eltype->isPrimitive ());
514 jobject dummy = NULL;
515 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
517 // Check for overflow.
518 if (__builtin_expect ((size_t) count >
519 (SIZE_T_MAX - size) / elsize, false))
522 jclass klass = _Jv_GetArrayClass (eltype, 0);
524 # ifdef JV_HASH_SYNCHRONIZATION
525 // Since the vtable is always statically allocated,
526 // these are completely pointerfree! Make sure the GC doesn't touch them.
528 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
529 memset((char *)arr + size, 0, elsize * count);
531 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
532 // Note that we assume we are given zeroed memory by the allocator.
535 jsize *lp = const_cast<jsize *> (&arr->length);
542 _Jv_NewArray (jint type, jint size)
546 case 4: return JvNewBooleanArray (size);
547 case 5: return JvNewCharArray (size);
548 case 6: return JvNewFloatArray (size);
549 case 7: return JvNewDoubleArray (size);
550 case 8: return JvNewByteArray (size);
551 case 9: return JvNewShortArray (size);
552 case 10: return JvNewIntArray (size);
553 case 11: return JvNewLongArray (size);
555 throw new java::lang::InternalError
556 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
559 // Allocate a possibly multi-dimensional array but don't check that
560 // any array length is <0.
562 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
564 JvAssert (type->isArray());
565 jclass element_type = type->getComponentType();
567 if (element_type->isPrimitive())
568 result = _Jv_NewPrimArray (element_type, sizes[0]);
570 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
574 JvAssert (! element_type->isPrimitive());
575 JvAssert (element_type->isArray());
576 jobject *contents = elements ((jobjectArray) result);
577 for (int i = 0; i < sizes[0]; ++i)
578 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
586 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
588 for (int i = 0; i < dimensions; ++i)
590 throw new java::lang::NegativeArraySizeException;
592 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
596 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
599 jint sizes[dimensions];
600 va_start (args, dimensions);
601 for (int i = 0; i < dimensions; ++i)
603 jint size = va_arg (args, jint);
605 throw new java::lang::NegativeArraySizeException;
610 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
615 // Ensure 8-byte alignment, for hash synchronization.
616 #define DECLARE_PRIM_TYPE(NAME) \
617 _Jv_ArrayVTable _Jv_##NAME##VTable; \
618 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
620 DECLARE_PRIM_TYPE(byte)
621 DECLARE_PRIM_TYPE(short)
622 DECLARE_PRIM_TYPE(int)
623 DECLARE_PRIM_TYPE(long)
624 DECLARE_PRIM_TYPE(boolean)
625 DECLARE_PRIM_TYPE(char)
626 DECLARE_PRIM_TYPE(float)
627 DECLARE_PRIM_TYPE(double)
628 DECLARE_PRIM_TYPE(void)
631 _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len,
632 _Jv_ArrayVTable *array_vtable)
634 using namespace java::lang::reflect;
636 _Jv_InitNewClassFields (cl);
638 // We must set the vtable for the class; the Java constructor
640 (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
642 // Initialize the fields we care about. We do this in the same
643 // order they are declared in Class.h.
644 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
645 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
646 cl->method_count = sig;
647 cl->size_in_bytes = len;
648 cl->vtable = JV_PRIMITIVE_VTABLE;
649 cl->state = JV_STATE_DONE;
652 _Jv_NewArrayClass (cl, NULL, (_Jv_VTable *) array_vtable);
656 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
661 return JvPrimClass (byte);
663 return JvPrimClass (short);
665 return JvPrimClass (int);
667 return JvPrimClass (long);
669 return JvPrimClass (boolean);
671 return JvPrimClass (char);
673 return JvPrimClass (float);
675 return JvPrimClass (double);
677 return JvPrimClass (void);
681 for (i = 1; sig[i] && sig[i] != ';'; ++i)
683 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
684 return _Jv_FindClass (name, loader);
688 jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
691 return _Jv_GetArrayClass (klass, loader);
695 return NULL; // Placate compiler.
701 JvConvertArgv (int argc, const char **argv)
705 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
706 jobject *ptr = elements(ar);
707 jbyteArray bytes = NULL;
708 for (int i = 0; i < argc; i++)
710 const char *arg = argv[i];
711 int len = strlen (arg);
712 if (bytes == NULL || bytes->length < len)
713 bytes = JvNewByteArray (len);
714 jbyte *bytePtr = elements (bytes);
715 // We assume jbyte == char.
716 memcpy (bytePtr, arg, len);
718 // Now convert using the default encoding.
719 *ptr++ = new java::lang::String (bytes, 0, len);
721 return (JArray<jstring>*) ar;
724 // FIXME: These variables are static so that they will be
725 // automatically scanned by the Boehm collector. This is needed
726 // because with qthreads the collector won't scan the initial stack --
727 // it will only scan the qthreads stacks.
729 // Command line arguments.
730 static JArray<jstring> *arg_vec;
732 // The primary thread.
733 static java::lang::Thread *main_thread;
735 #ifndef DISABLE_GETENV_PROPERTIES
738 next_property_key (char *s, size_t *length)
744 // Skip over whitespace
748 // If we've reached the end, return NULL. Also return NULL if for
749 // some reason we've come across a malformed property string.
755 // Determine the length of the property key.
773 next_property_value (char *s, size_t *length)
789 // If we've reached the end, return NULL.
793 // Determine the length of the property value.
812 process_gcj_properties ()
814 char *props = getenv("GCJ_PROPERTIES");
817 size_t property_count = 0;
822 // Whip through props quickly in order to count the number of
824 while (p && (p = next_property_key (p, &length)))
826 // Skip to the end of the key
829 p = next_property_value (p, &length);
836 // Allocate an array of property value/key pairs.
837 _Jv_Environment_Properties =
838 (property_pair *) malloc (sizeof(property_pair)
839 * (property_count + 1));
841 // Go through the properties again, initializing _Jv_Properties
845 while (p && (p = next_property_key (p, &length)))
847 _Jv_Environment_Properties[property_count].key = p;
848 _Jv_Environment_Properties[property_count].key_length = length;
850 // Skip to the end of the key
853 p = next_property_value (p, &length);
855 _Jv_Environment_Properties[property_count].value = p;
856 _Jv_Environment_Properties[property_count].value_length = length;
863 memset ((void *) &_Jv_Environment_Properties[property_count],
864 0, sizeof (property_pair));
868 // Null terminate the strings.
869 while (_Jv_Environment_Properties[i].key)
871 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
872 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
876 #endif // DISABLE_GETENV_PROPERTIES
880 _Jv_Utf8Const *void_signature;
881 _Jv_Utf8Const *clinit_name;
882 _Jv_Utf8Const *init_name;
883 _Jv_Utf8Const *finit_name;
885 bool runtimeInitialized = false;
889 _Jv_CreateJavaVM (void* /*vm_args*/)
893 if (runtimeInitialized)
896 runtimeInitialized = true;
898 PROCESS_GCJ_PROPERTIES;
902 _Jv_InitializeSyncMutex ();
904 /* Initialize Utf8 constants declared in jvm.h. */
905 void_signature = _Jv_makeUtf8Const ("()V", 3);
906 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
907 init_name = _Jv_makeUtf8Const ("<init>", 6);
908 finit_name = _Jv_makeUtf8Const ("finit$", 6);
910 /* Initialize built-in classes to represent primitive TYPEs. */
911 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1, &_Jv_byteVTable);
912 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2, &_Jv_shortVTable);
913 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4, &_Jv_intVTable);
914 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8, &_Jv_longVTable);
915 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1, &_Jv_booleanVTable);
916 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2, &_Jv_charVTable);
917 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4, &_Jv_floatVTable);
918 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8, &_Jv_doubleVTable);
919 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0, &_Jv_voidVTable);
921 // Turn stack trace generation off while creating exception objects.
922 _Jv_InitClass (&java::lang::VMThrowable::class$);
923 java::lang::VMThrowable::trace_enabled = 0;
930 no_memory = new java::lang::OutOfMemoryError;
932 java::lang::VMThrowable::trace_enabled = 1;
935 LTDL_SET_PRELOADED_SYMBOLS ();
938 _Jv_platform_initialize ();
942 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
944 // Start the GC finalizer thread. A VirtualMachineError can be
945 // thrown by the runtime if, say, threads aren't available. In this
946 // case finalizers simply won't run.
949 using namespace gnu::gcj::runtime;
950 FinalizerThread *ft = new FinalizerThread ();
953 catch (java::lang::VirtualMachineError *ignore)
961 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
964 _Jv_SetArgs (argc, argv);
966 java::lang::Runtime *runtime = NULL;
970 // Set this very early so that it is seen when java.lang.System
973 _Jv_Jar_Class_Path = strdup (name);
974 _Jv_CreateJavaVM (NULL);
976 // Get the Runtime here. We want to initialize it before searching
977 // for `main'; that way it will be set up if `main' is a JNI method.
978 runtime = java::lang::Runtime::getRuntime ();
980 #ifdef DISABLE_MAIN_ARGS
981 arg_vec = JvConvertArgv (0, 0);
983 arg_vec = JvConvertArgv (argc - 1, argv + 1);
986 using namespace gnu::gcj::runtime;
988 main_thread = new FirstThread (klass, arg_vec);
990 main_thread = new FirstThread (JvNewStringLatin1 (name),
993 catch (java::lang::Throwable *t)
995 java::lang::System::err->println (JvNewStringLatin1
996 ("Exception during runtime initialization"));
997 t->printStackTrace();
1001 _Jv_AttachCurrentThread (main_thread);
1002 _Jv_ThreadRun (main_thread);
1005 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1006 runtime->exit (status);
1010 JvRunMain (jclass klass, int argc, const char **argv)
1012 _Jv_RunMain (klass, NULL, argc, argv, false);
1017 // Parse a string and return a heap size.
1019 parse_heap_size (const char *spec)
1022 unsigned long val = strtoul (spec, &end, 10);
1023 if (*end == 'k' || *end == 'K')
1025 else if (*end == 'm' || *end == 'M')
1027 return (size_t) val;
1030 // Set the initial heap size. This might be ignored by the GC layer.
1031 // This must be called before _Jv_RunMain.
1033 _Jv_SetInitialHeapSize (const char *arg)
1035 size_t size = parse_heap_size (arg);
1036 _Jv_GCSetInitialHeapSize (size);
1039 // Set the maximum heap size. This might be ignored by the GC layer.
1040 // This must be called before _Jv_RunMain.
1042 _Jv_SetMaximumHeapSize (const char *arg)
1044 size_t size = parse_heap_size (arg);
1045 _Jv_GCSetMaximumHeapSize (size);
1051 _Jv_Malloc (jsize size)
1053 if (__builtin_expect (size == 0, false))
1055 void *ptr = malloc ((size_t) size);
1056 if (__builtin_expect (ptr == NULL, false))
1062 _Jv_Realloc (void *ptr, jsize size)
1064 if (__builtin_expect (size == 0, false))
1066 ptr = realloc (ptr, (size_t) size);
1067 if (__builtin_expect (ptr == NULL, false))
1073 _Jv_MallocUnchecked (jsize size)
1075 if (__builtin_expect (size == 0, false))
1077 return malloc ((size_t) size);
1081 _Jv_Free (void* ptr)
1088 // In theory, these routines can be #ifdef'd away on machines which
1089 // support divide overflow signals. However, we never know if some
1090 // code might have been compiled with "-fuse-divide-subroutine", so we
1091 // always include them in libgcj.
1094 _Jv_divI (jint dividend, jint divisor)
1096 if (__builtin_expect (divisor == 0, false))
1098 java::lang::ArithmeticException *arithexception
1099 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1100 throw arithexception;
1103 if (dividend == (jint) 0x80000000L && divisor == -1)
1106 return dividend / divisor;
1110 _Jv_remI (jint dividend, jint divisor)
1112 if (__builtin_expect (divisor == 0, false))
1114 java::lang::ArithmeticException *arithexception
1115 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1116 throw arithexception;
1119 if (dividend == (jint) 0x80000000L && divisor == -1)
1122 return dividend % divisor;
1126 _Jv_divJ (jlong dividend, jlong divisor)
1128 if (__builtin_expect (divisor == 0, false))
1130 java::lang::ArithmeticException *arithexception
1131 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1132 throw arithexception;
1135 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1138 return dividend / divisor;
1142 _Jv_remJ (jlong dividend, jlong divisor)
1144 if (__builtin_expect (divisor == 0, false))
1146 java::lang::ArithmeticException *arithexception
1147 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1148 throw arithexception;
1151 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1154 return dividend % divisor;
1159 // Return true if SELF_KLASS can access a field or method in
1160 // OTHER_KLASS. The field or method's access flags are specified in
1163 _Jv_CheckAccess (jclass self_klass, jclass other_klass, jint flags)
1165 using namespace java::lang::reflect;
1166 return ((self_klass == other_klass)
1167 || ((flags & Modifier::PUBLIC) != 0)
1168 || (((flags & Modifier::PROTECTED) != 0)
1169 && other_klass->isAssignableFrom (self_klass))
1170 || (((flags & Modifier::PRIVATE) == 0)
1171 && _Jv_ClassNameSamePackage (self_klass->name,
1172 other_klass->name)));