1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 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 <execution.h>
65 #include <gnu/java/lang/MainThread.h>
71 // Execution engine for compiled code.
72 _Jv_CompiledEngine _Jv_soleCompiledEngine
;
74 // We allocate a single OutOfMemoryError exception which we keep
75 // around for use if we run out of memory.
76 static java::lang::OutOfMemoryError
*no_memory
;
78 // Number of bytes in largest array object we create. This could be
79 // increased to the largest size_t value, so long as the appropriate
80 // functions are changed to take a size_t argument instead of jint.
81 #define MAX_OBJECT_SIZE ((1<<31) - 1)
83 static const char *no_properties
[] = { NULL
};
85 // Properties set at compile time.
86 const char **_Jv_Compiler_Properties
= no_properties
;
88 // The JAR file to add to the beginning of java.class.path.
89 const char *_Jv_Jar_Class_Path
;
91 #ifndef DISABLE_GETENV_PROPERTIES
92 // Property key/value pairs.
93 property_pair
*_Jv_Environment_Properties
;
96 // Stash the argv pointer to benefit native libraries that need it.
97 const char **_Jv_argv
;
104 // _Jv_argc is 0 if not explicitly initialized.
109 _Jv_GetSafeArg (int index
)
111 if (index
>=0 && index
< _Jv_GetNbArgs ())
112 return _Jv_argv
[index
];
118 _Jv_SetArgs (int argc
, const char **argv
)
125 // Pointer to JVMPI notification functions.
126 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (JVMPI_Event
*event
);
127 void (*_Jv_JVMPI_Notify_THREAD_START
) (JVMPI_Event
*event
);
128 void (*_Jv_JVMPI_Notify_THREAD_END
) (JVMPI_Event
*event
);
132 #if defined (HANDLE_SEGV) || defined(HANDLE_FPE)
133 /* Unblock a signal. Unless we do this, the signal may only be sent
136 unblock_signal (int signum
__attribute__ ((__unused__
)))
138 #ifdef _POSIX_VERSION
142 sigaddset (&sigs
, signum
);
143 sigprocmask (SIG_UNBLOCK
, &sigs
, NULL
);
149 SIGNAL_HANDLER (catch_segv
)
151 java::lang::NullPointerException
*nullp
152 = new java::lang::NullPointerException
;
153 unblock_signal (SIGSEGV
);
154 MAKE_THROW_FRAME (nullp
);
160 SIGNAL_HANDLER (catch_fpe
)
162 java::lang::ArithmeticException
*arithexception
163 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
164 unblock_signal (SIGFPE
);
165 #ifdef HANDLE_DIVIDE_OVERFLOW
166 HANDLE_DIVIDE_OVERFLOW
;
168 MAKE_THROW_FRAME (arithexception
);
170 throw arithexception
;
177 _Jv_equalUtf8Consts (const Utf8Const
* a
, const Utf8Const
*b
)
180 const _Jv_ushort
*aptr
, *bptr
;
183 if (a
->hash
!= b
->hash
)
186 if (b
->length
!= len
)
188 aptr
= (const _Jv_ushort
*)a
->data
;
189 bptr
= (const _Jv_ushort
*)b
->data
;
190 len
= (len
+ 1) >> 1;
192 if (*aptr
++ != *bptr
++)
197 /* True iff A is equal to STR.
198 HASH is STR->hashCode().
202 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
204 if (a
->hash
!= (_Jv_ushort
) hash
)
206 jint len
= str
->length();
208 jchar
*sptr
= _Jv_GetStringChars (str
);
209 unsigned char* ptr
= (unsigned char*) a
->data
;
210 unsigned char* limit
= ptr
+ a
->length
;
213 int ch
= UTF8_GET (ptr
, limit
);
222 /* Like _Jv_equal, but stop after N characters. */
224 _Jv_equaln (Utf8Const
*a
, jstring str
, jint n
)
226 jint len
= str
->length();
228 jchar
*sptr
= _Jv_GetStringChars (str
);
229 unsigned char* ptr
= (unsigned char*) a
->data
;
230 unsigned char* limit
= ptr
+ a
->length
;
231 for (; n
-- > 0; i
++, sptr
++)
233 int ch
= UTF8_GET (ptr
, limit
);
242 /* Count the number of Unicode chars encoded in a given Ut8 string. */
244 _Jv_strLengthUtf8(char* str
, int len
)
247 unsigned char* limit
;
250 ptr
= (unsigned char*) str
;
253 for (; ptr
< limit
; str_length
++)
255 if (UTF8_GET (ptr
, limit
) < 0)
261 /* Calculate a hash value for a string encoded in Utf8 format.
262 * This returns the same hash value as specified or java.lang.String.hashCode.
265 _Jv_hashUtf8String (char* str
, int len
)
267 unsigned char* ptr
= (unsigned char*) str
;
268 unsigned char* limit
= ptr
+ len
;
273 int ch
= UTF8_GET (ptr
, limit
);
274 /* Updated specification from
275 http://www.javasoft.com/docs/books/jls/clarify.html. */
276 hash
= (31 * hash
) + ch
;
282 _Jv_Utf8Const::init(char *s
, int len
)
284 ::memcpy (data
, s
, len
);
287 hash
= _Jv_hashUtf8String (s
, len
) & 0xFFFF;
291 _Jv_makeUtf8Const (char* s
, int len
)
296 = (Utf8Const
*) _Jv_AllocBytes (_Jv_Utf8Const::space_needed(s
, len
));
302 _Jv_makeUtf8Const (jstring string
)
304 jint hash
= string
->hashCode ();
305 jint len
= _Jv_GetStringUTFLength (string
);
307 Utf8Const
* m
= (Utf8Const
*)
308 _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
313 _Jv_GetStringUTFRegion (string
, 0, string
->length (), m
->data
);
323 _Jv_Abort (const char *function
, const char *file
, int line
,
327 _Jv_Abort (const char *, const char *, int, const char *message
)
332 "libgcj failure: %s\n in function %s, file %s, line %d\n",
333 message
, function
, file
, line
);
335 fprintf (stderr
, "libgcj failure: %s\n", message
);
341 fail_on_finalization (jobject
)
343 JvFail ("object was finalized");
347 _Jv_GCWatch (jobject obj
)
349 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
353 _Jv_ThrowBadArrayIndex(jint bad_index
)
355 throw new java::lang::ArrayIndexOutOfBoundsException
356 (java::lang::String::valueOf (bad_index
));
360 _Jv_ThrowNullPointerException ()
362 throw new java::lang::NullPointerException
;
365 // Explicitly throw a no memory exception.
366 // The collector calls this when it encounters an out-of-memory condition.
367 void _Jv_ThrowNoMemory()
373 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) \
374 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false)) \
375 jvmpi_notify_alloc(klass,size,obj);
377 jvmpi_notify_alloc(jclass klass
, jint size
, jobject obj
)
379 // Service JVMPI allocation request.
382 event
.event_type
= JVMPI_EVENT_OBJECT_ALLOC
;
384 event
.u
.obj_alloc
.arena_id
= 0;
385 event
.u
.obj_alloc
.class_id
= (jobjectID
) klass
;
386 event
.u
.obj_alloc
.is_array
= 0;
387 event
.u
.obj_alloc
.size
= size
;
388 event
.u
.obj_alloc
.obj_id
= (jobjectID
) obj
;
390 // FIXME: This doesn't look right for the Boehm GC. A GC may
391 // already be in progress. _Jv_DisableGC () doesn't wait for it.
392 // More importantly, I don't see the need for disabling GC, since we
393 // blatantly have a pointer to obj on our stack, ensuring that the
394 // object can't be collected. Even for a nonconservative collector,
395 // it appears to me that this must be true, since we are about to
396 // return obj. Isn't this whole approach way too intrusive for
397 // a useful profiling interface? - HB
399 (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (&event
);
402 #else /* !ENABLE_JVMPI */
403 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) /* do nothing */
406 // Allocate a new object of class KLASS.
407 // First a version that assumes that we have no finalizer, and that
408 // the class is already initialized.
409 // If we know that JVMPI is disabled, this can be replaced by a direct call
410 // to the allocator for the appropriate GC.
412 _Jv_AllocObjectNoInitNoFinalizer (jclass klass
)
414 jint size
= klass
->size ();
415 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
416 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
420 // And now a version that initializes if necessary.
422 _Jv_AllocObjectNoFinalizer (jclass klass
)
424 _Jv_InitClass (klass
);
425 jint size
= klass
->size ();
426 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
427 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
431 // And now the general version that registers a finalizer if necessary.
433 _Jv_AllocObject (jclass klass
)
435 jobject obj
= _Jv_AllocObjectNoFinalizer (klass
);
437 // We assume that the compiler only generates calls to this routine
438 // if there really is an interesting finalizer.
439 // Unfortunately, we still have to the dynamic test, since there may
440 // be cni calls to this routine.
441 // Note that on IA64 get_finalizer() returns the starting address of the
442 // function, not a function pointer. Thus this still works.
443 if (klass
->vtable
->get_finalizer ()
444 != java::lang::Object::class$
.vtable
->get_finalizer ())
445 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
449 // Allocate a String, including variable length storage.
451 _Jv_AllocString(jsize len
)
453 using namespace java::lang
;
455 jsize sz
= sizeof(java::lang::String
) + len
* sizeof(jchar
);
457 // We assert that for strings allocated this way, the data field
458 // will always point to the object itself. Thus there is no reason
459 // for the garbage collector to scan any of it.
460 // Furthermore, we're about to overwrite the string data, so
461 // initialization of the object is not an issue.
463 // String needs no initialization, and there is no finalizer, so
464 // we can go directly to the collector's allocator interface.
465 jstring obj
= (jstring
) _Jv_AllocPtrFreeObj(sz
, &String::class$
);
468 obj
->boffset
= sizeof(java::lang::String
);
470 obj
->cachedHashCode
= 0;
472 JVMPI_NOTIFY_ALLOC (&String::class$
, sz
, obj
);
477 // A version of the above that assumes the object contains no pointers,
478 // and requires no finalization. This can't happen if we need pointers
480 #ifdef JV_HASH_SYNCHRONIZATION
482 _Jv_AllocPtrFreeObject (jclass klass
)
484 _Jv_InitClass (klass
);
485 jint size
= klass
->size ();
487 jobject obj
= (jobject
) _Jv_AllocPtrFreeObj (size
, klass
);
489 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
493 #endif /* JV_HASH_SYNCHRONIZATION */
496 // Allocate a new array of Java objects. Each object is of type
497 // `elementClass'. `init' is used to initialize each slot in the
500 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
502 if (__builtin_expect (count
< 0, false))
503 throw new java::lang::NegativeArraySizeException
;
505 JvAssert (! elementClass
->isPrimitive ());
507 // Ensure that elements pointer is properly aligned.
508 jobjectArray obj
= NULL
;
509 size_t size
= (size_t) elements (obj
);
510 // Check for overflow.
511 if (__builtin_expect ((size_t) count
>
512 (MAX_OBJECT_SIZE
- 1 - size
) / sizeof (jobject
), false))
515 size
+= count
* sizeof (jobject
);
517 jclass klass
= _Jv_GetArrayClass (elementClass
,
518 elementClass
->getClassLoaderInternal());
520 obj
= (jobjectArray
) _Jv_AllocArray (size
, klass
);
522 jsize
*lp
= const_cast<jsize
*> (&obj
->length
);
524 // We know the allocator returns zeroed memory. So don't bother
528 jobject
*ptr
= elements(obj
);
535 // Allocate a new array of primitives. ELTYPE is the type of the
536 // element, COUNT is the size of the array.
538 _Jv_NewPrimArray (jclass eltype
, jint count
)
540 int elsize
= eltype
->size();
541 if (__builtin_expect (count
< 0, false))
542 throw new java::lang::NegativeArraySizeException
;
544 JvAssert (eltype
->isPrimitive ());
545 jobject dummy
= NULL
;
546 size_t size
= (size_t) _Jv_GetArrayElementFromElementType (dummy
, eltype
);
548 // Check for overflow.
549 if (__builtin_expect ((size_t) count
>
550 (MAX_OBJECT_SIZE
- size
) / elsize
, false))
553 jclass klass
= _Jv_GetArrayClass (eltype
, 0);
555 # ifdef JV_HASH_SYNCHRONIZATION
556 // Since the vtable is always statically allocated,
557 // these are completely pointerfree! Make sure the GC doesn't touch them.
559 (__JArray
*) _Jv_AllocPtrFreeObj (size
+ elsize
* count
, klass
);
560 memset((char *)arr
+ size
, 0, elsize
* count
);
562 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (size
+ elsize
* count
, klass
);
563 // Note that we assume we are given zeroed memory by the allocator.
566 jsize
*lp
= const_cast<jsize
*> (&arr
->length
);
573 _Jv_NewArray (jint type
, jint size
)
577 case 4: return JvNewBooleanArray (size
);
578 case 5: return JvNewCharArray (size
);
579 case 6: return JvNewFloatArray (size
);
580 case 7: return JvNewDoubleArray (size
);
581 case 8: return JvNewByteArray (size
);
582 case 9: return JvNewShortArray (size
);
583 case 10: return JvNewIntArray (size
);
584 case 11: return JvNewLongArray (size
);
586 throw new java::lang::InternalError
587 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
590 // Allocate a possibly multi-dimensional array but don't check that
591 // any array length is <0.
593 _Jv_NewMultiArrayUnchecked (jclass type
, jint dimensions
, jint
*sizes
)
595 JvAssert (type
->isArray());
596 jclass element_type
= type
->getComponentType();
598 if (element_type
->isPrimitive())
599 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
601 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
605 JvAssert (! element_type
->isPrimitive());
606 JvAssert (element_type
->isArray());
607 jobject
*contents
= elements ((jobjectArray
) result
);
608 for (int i
= 0; i
< sizes
[0]; ++i
)
609 contents
[i
] = _Jv_NewMultiArrayUnchecked (element_type
, dimensions
- 1,
617 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
619 for (int i
= 0; i
< dimensions
; ++i
)
621 throw new java::lang::NegativeArraySizeException
;
623 return _Jv_NewMultiArrayUnchecked (type
, dimensions
, sizes
);
627 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
630 jint sizes
[dimensions
];
631 va_start (args
, dimensions
);
632 for (int i
= 0; i
< dimensions
; ++i
)
634 jint size
= va_arg (args
, jint
);
636 throw new java::lang::NegativeArraySizeException
;
641 return _Jv_NewMultiArrayUnchecked (array_type
, dimensions
, sizes
);
646 // Ensure 8-byte alignment, for hash synchronization.
647 #define DECLARE_PRIM_TYPE(NAME) \
648 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
650 DECLARE_PRIM_TYPE(byte
)
651 DECLARE_PRIM_TYPE(short)
652 DECLARE_PRIM_TYPE(int)
653 DECLARE_PRIM_TYPE(long)
654 DECLARE_PRIM_TYPE(boolean
)
655 DECLARE_PRIM_TYPE(char)
656 DECLARE_PRIM_TYPE(float)
657 DECLARE_PRIM_TYPE(double)
658 DECLARE_PRIM_TYPE(void)
661 _Jv_InitPrimClass (jclass cl
, char *cname
, char sig
, int len
)
663 using namespace java::lang::reflect
;
665 // We must set the vtable for the class; the Java constructor
667 (*(_Jv_VTable
**) cl
) = java::lang::Class::class$
.vtable
;
669 // Initialize the fields we care about. We do this in the same
670 // order they are declared in Class.h.
671 cl
->name
= _Jv_makeUtf8Const ((char *) cname
, -1);
672 cl
->accflags
= Modifier::PUBLIC
| Modifier::FINAL
| Modifier::ABSTRACT
;
673 cl
->method_count
= sig
;
674 cl
->size_in_bytes
= len
;
675 cl
->vtable
= JV_PRIMITIVE_VTABLE
;
676 cl
->state
= JV_STATE_DONE
;
681 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
686 return JvPrimClass (byte
);
688 return JvPrimClass (short);
690 return JvPrimClass (int);
692 return JvPrimClass (long);
694 return JvPrimClass (boolean
);
696 return JvPrimClass (char);
698 return JvPrimClass (float);
700 return JvPrimClass (double);
702 return JvPrimClass (void);
706 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
708 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
709 return _Jv_FindClass (name
, loader
);
713 jclass klass
= _Jv_FindClassFromSignature (&sig
[1], loader
);
716 return _Jv_GetArrayClass (klass
, loader
);
720 return NULL
; // Placate compiler.
726 JvConvertArgv (int argc
, const char **argv
)
730 jobjectArray ar
= JvNewObjectArray(argc
, &java::lang::String::class$
, NULL
);
731 jobject
*ptr
= elements(ar
);
732 jbyteArray bytes
= NULL
;
733 for (int i
= 0; i
< argc
; i
++)
735 const char *arg
= argv
[i
];
736 int len
= strlen (arg
);
737 if (bytes
== NULL
|| bytes
->length
< len
)
738 bytes
= JvNewByteArray (len
);
739 jbyte
*bytePtr
= elements (bytes
);
740 // We assume jbyte == char.
741 memcpy (bytePtr
, arg
, len
);
743 // Now convert using the default encoding.
744 *ptr
++ = new java::lang::String (bytes
, 0, len
);
746 return (JArray
<jstring
>*) ar
;
749 // FIXME: These variables are static so that they will be
750 // automatically scanned by the Boehm collector. This is needed
751 // because with qthreads the collector won't scan the initial stack --
752 // it will only scan the qthreads stacks.
754 // Command line arguments.
755 static JArray
<jstring
> *arg_vec
;
757 // The primary thread.
758 static java::lang::Thread
*main_thread
;
760 #ifndef DISABLE_GETENV_PROPERTIES
763 next_property_key (char *s
, size_t *length
)
769 // Skip over whitespace
773 // If we've reached the end, return NULL. Also return NULL if for
774 // some reason we've come across a malformed property string.
780 // Determine the length of the property key.
798 next_property_value (char *s
, size_t *length
)
814 // If we've reached the end, return NULL.
818 // Determine the length of the property value.
837 process_gcj_properties ()
839 char *props
= getenv("GCJ_PROPERTIES");
842 size_t property_count
= 0;
847 // Whip through props quickly in order to count the number of
849 while (p
&& (p
= next_property_key (p
, &length
)))
851 // Skip to the end of the key
854 p
= next_property_value (p
, &length
);
861 // Allocate an array of property value/key pairs.
862 _Jv_Environment_Properties
=
863 (property_pair
*) malloc (sizeof(property_pair
)
864 * (property_count
+ 1));
866 // Go through the properties again, initializing _Jv_Properties
870 while (p
&& (p
= next_property_key (p
, &length
)))
872 _Jv_Environment_Properties
[property_count
].key
= p
;
873 _Jv_Environment_Properties
[property_count
].key_length
= length
;
875 // Skip to the end of the key
878 p
= next_property_value (p
, &length
);
880 _Jv_Environment_Properties
[property_count
].value
= p
;
881 _Jv_Environment_Properties
[property_count
].value_length
= length
;
888 memset ((void *) &_Jv_Environment_Properties
[property_count
],
889 0, sizeof (property_pair
));
891 // Null terminate the strings.
892 for (property_pair
*prop
= &_Jv_Environment_Properties
[0];
896 prop
->key
[prop
->key_length
] = 0;
897 prop
->value
[prop
->value_length
] = 0;
900 #endif // DISABLE_GETENV_PROPERTIES
904 _Jv_Utf8Const
*void_signature
;
905 _Jv_Utf8Const
*clinit_name
;
906 _Jv_Utf8Const
*init_name
;
907 _Jv_Utf8Const
*finit_name
;
909 bool runtimeInitialized
= false;
913 _Jv_CreateJavaVM (void* /*vm_args*/)
917 if (runtimeInitialized
)
920 runtimeInitialized
= true;
922 PROCESS_GCJ_PROPERTIES
;
926 _Jv_InitializeSyncMutex ();
929 _Jv_InitInterpreter ();
940 /* Initialize Utf8 constants declared in jvm.h. */
941 void_signature
= _Jv_makeUtf8Const ("()V", 3);
942 clinit_name
= _Jv_makeUtf8Const ("<clinit>", 8);
943 init_name
= _Jv_makeUtf8Const ("<init>", 6);
944 finit_name
= _Jv_makeUtf8Const ("finit$", 6);
946 /* Initialize built-in classes to represent primitive TYPEs. */
947 _Jv_InitPrimClass (&_Jv_byteClass
, "byte", 'B', 1);
948 _Jv_InitPrimClass (&_Jv_shortClass
, "short", 'S', 2);
949 _Jv_InitPrimClass (&_Jv_intClass
, "int", 'I', 4);
950 _Jv_InitPrimClass (&_Jv_longClass
, "long", 'J', 8);
951 _Jv_InitPrimClass (&_Jv_booleanClass
, "boolean", 'Z', 1);
952 _Jv_InitPrimClass (&_Jv_charClass
, "char", 'C', 2);
953 _Jv_InitPrimClass (&_Jv_floatClass
, "float", 'F', 4);
954 _Jv_InitPrimClass (&_Jv_doubleClass
, "double", 'D', 8);
955 _Jv_InitPrimClass (&_Jv_voidClass
, "void", 'V', 0);
957 // Turn stack trace generation off while creating exception objects.
958 _Jv_InitClass (&java::lang::VMThrowable::class$
);
959 java::lang::VMThrowable::trace_enabled
= 0;
961 // We have to initialize this fairly early, to avoid circular class
962 // initialization. In particular we want to start the
963 // initialization of ClassLoader before we start the initialization
965 _Jv_InitClass (&java::lang::ClassLoader::class$
);
967 // Once the bootstrap loader is in place, change it into a kind of
968 // system loader, by having it read the class path.
969 gnu::gcj::runtime::VMClassLoader::initialize();
971 _Jv_RegisterBootstrapPackages();
973 no_memory
= new java::lang::OutOfMemoryError
;
975 java::lang::VMThrowable::trace_enabled
= 1;
978 LTDL_SET_PRELOADED_SYMBOLS ();
981 _Jv_platform_initialize ();
985 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady
);
987 // Start the GC finalizer thread. A VirtualMachineError can be
988 // thrown by the runtime if, say, threads aren't available.
991 using namespace gnu::gcj::runtime
;
992 FinalizerThread
*ft
= new FinalizerThread ();
995 catch (java::lang::VirtualMachineError
*ignore
)
1003 _Jv_RunMain (jclass klass
, const char *name
, int argc
, const char **argv
,
1006 #ifndef DISABLE_MAIN_ARGS
1007 _Jv_SetArgs (argc
, argv
);
1010 java::lang::Runtime
*runtime
= NULL
;
1014 // Set this very early so that it is seen when java.lang.System
1017 _Jv_Jar_Class_Path
= strdup (name
);
1018 _Jv_CreateJavaVM (NULL
);
1020 // Get the Runtime here. We want to initialize it before searching
1021 // for `main'; that way it will be set up if `main' is a JNI method.
1022 runtime
= java::lang::Runtime::getRuntime ();
1024 #ifdef DISABLE_MAIN_ARGS
1025 arg_vec
= JvConvertArgv (0, 0);
1027 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
1030 using namespace gnu::java::lang
;
1032 main_thread
= new MainThread (klass
, arg_vec
);
1034 main_thread
= new MainThread (JvNewStringLatin1 (name
),
1037 catch (java::lang::Throwable
*t
)
1039 java::lang::System::err
->println (JvNewStringLatin1
1040 ("Exception during runtime initialization"));
1041 t
->printStackTrace();
1045 _Jv_AttachCurrentThread (main_thread
);
1046 _Jv_ThreadRun (main_thread
);
1049 int status
= (int) java::lang::ThreadGroup::had_uncaught_exception
;
1050 runtime
->exit (status
);
1054 JvRunMain (jclass klass
, int argc
, const char **argv
)
1056 _Jv_RunMain (klass
, NULL
, argc
, argv
, false);
1061 // Parse a string and return a heap size.
1063 parse_heap_size (const char *spec
)
1066 unsigned long val
= strtoul (spec
, &end
, 10);
1067 if (*end
== 'k' || *end
== 'K')
1069 else if (*end
== 'm' || *end
== 'M')
1071 return (size_t) val
;
1074 // Set the initial heap size. This might be ignored by the GC layer.
1075 // This must be called before _Jv_RunMain.
1077 _Jv_SetInitialHeapSize (const char *arg
)
1079 size_t size
= parse_heap_size (arg
);
1080 _Jv_GCSetInitialHeapSize (size
);
1083 // Set the maximum heap size. This might be ignored by the GC layer.
1084 // This must be called before _Jv_RunMain.
1086 _Jv_SetMaximumHeapSize (const char *arg
)
1088 size_t size
= parse_heap_size (arg
);
1089 _Jv_GCSetMaximumHeapSize (size
);
1095 _Jv_Malloc (jsize size
)
1097 if (__builtin_expect (size
== 0, false))
1099 void *ptr
= malloc ((size_t) size
);
1100 if (__builtin_expect (ptr
== NULL
, false))
1106 _Jv_Realloc (void *ptr
, jsize size
)
1108 if (__builtin_expect (size
== 0, false))
1110 ptr
= realloc (ptr
, (size_t) size
);
1111 if (__builtin_expect (ptr
== NULL
, false))
1117 _Jv_MallocUnchecked (jsize size
)
1119 if (__builtin_expect (size
== 0, false))
1121 return malloc ((size_t) size
);
1125 _Jv_Free (void* ptr
)
1132 // In theory, these routines can be #ifdef'd away on machines which
1133 // support divide overflow signals. However, we never know if some
1134 // code might have been compiled with "-fuse-divide-subroutine", so we
1135 // always include them in libgcj.
1138 _Jv_divI (jint dividend
, jint divisor
)
1140 if (__builtin_expect (divisor
== 0, false))
1142 java::lang::ArithmeticException
*arithexception
1143 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1144 throw arithexception
;
1147 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1150 return dividend
/ divisor
;
1154 _Jv_remI (jint dividend
, jint divisor
)
1156 if (__builtin_expect (divisor
== 0, false))
1158 java::lang::ArithmeticException
*arithexception
1159 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1160 throw arithexception
;
1163 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1166 return dividend
% divisor
;
1170 _Jv_divJ (jlong dividend
, jlong divisor
)
1172 if (__builtin_expect (divisor
== 0, false))
1174 java::lang::ArithmeticException
*arithexception
1175 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1176 throw arithexception
;
1179 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1182 return dividend
/ divisor
;
1186 _Jv_remJ (jlong dividend
, jlong divisor
)
1188 if (__builtin_expect (divisor
== 0, false))
1190 java::lang::ArithmeticException
*arithexception
1191 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1192 throw arithexception
;
1195 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1198 return dividend
% divisor
;
1203 // Return true if SELF_KLASS can access a field or method in
1204 // OTHER_KLASS. The field or method's access flags are specified in
1207 _Jv_CheckAccess (jclass self_klass
, jclass other_klass
, jint flags
)
1209 using namespace java::lang::reflect
;
1210 return ((self_klass
== other_klass
)
1211 || ((flags
& Modifier::PUBLIC
) != 0)
1212 || (((flags
& Modifier::PROTECTED
) != 0)
1213 && _Jv_IsAssignableFromSlow (other_klass
, self_klass
))
1214 || (((flags
& Modifier::PRIVATE
) == 0)
1215 && _Jv_ClassNameSamePackage (self_klass
->name
,
1216 other_klass
->name
)));