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/VMClassLoader.h>
59 #include <java/lang/reflect/Modifier.h>
60 #include <java/io/PrintStream.h>
61 #include <java/lang/UnsatisfiedLinkError.h>
62 #include <java/lang/VirtualMachineError.h>
63 #include <gnu/gcj/runtime/ExtensionClassLoader.h>
64 #include <gnu/gcj/runtime/FinalizerThread.h>
65 #include <execution.h>
66 #include <gnu/java/lang/MainThread.h>
72 // Execution engine for compiled code.
73 _Jv_CompiledEngine _Jv_soleCompiledEngine
;
75 // We allocate a single OutOfMemoryError exception which we keep
76 // around for use if we run out of memory.
77 static java::lang::OutOfMemoryError
*no_memory
;
79 // Number of bytes in largest array object we create. This could be
80 // increased to the largest size_t value, so long as the appropriate
81 // functions are changed to take a size_t argument instead of jint.
82 #define MAX_OBJECT_SIZE ((1<<31) - 1)
84 // Properties set at compile time.
85 const char **_Jv_Compiler_Properties
= NULL
;
86 int _Jv_Properties_Count
= 0;
88 #ifndef DISABLE_GETENV_PROPERTIES
89 // Property key/value pairs.
90 property_pair
*_Jv_Environment_Properties
;
93 // Stash the argv pointer to benefit native libraries that need it.
94 const char **_Jv_argv
;
101 // _Jv_argc is 0 if not explicitly initialized.
106 _Jv_GetSafeArg (int index
)
108 if (index
>=0 && index
< _Jv_GetNbArgs ())
109 return _Jv_argv
[index
];
115 _Jv_SetArgs (int argc
, const char **argv
)
122 // Pointer to JVMPI notification functions.
123 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (JVMPI_Event
*event
);
124 void (*_Jv_JVMPI_Notify_THREAD_START
) (JVMPI_Event
*event
);
125 void (*_Jv_JVMPI_Notify_THREAD_END
) (JVMPI_Event
*event
);
129 #if defined (HANDLE_SEGV) || defined(HANDLE_FPE)
130 /* Unblock a signal. Unless we do this, the signal may only be sent
133 unblock_signal (int signum
__attribute__ ((__unused__
)))
135 #ifdef _POSIX_VERSION
139 sigaddset (&sigs
, signum
);
140 sigprocmask (SIG_UNBLOCK
, &sigs
, NULL
);
146 SIGNAL_HANDLER (catch_segv
)
148 unblock_signal (SIGSEGV
);
149 MAKE_THROW_FRAME (nullp
);
150 java::lang::NullPointerException
*nullp
151 = new java::lang::NullPointerException
;
157 SIGNAL_HANDLER (catch_fpe
)
159 unblock_signal (SIGFPE
);
160 #ifdef HANDLE_DIVIDE_OVERFLOW
161 HANDLE_DIVIDE_OVERFLOW
;
163 MAKE_THROW_FRAME (arithexception
);
165 java::lang::ArithmeticException
*arithexception
166 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
167 throw arithexception
;
174 _Jv_equalUtf8Consts (const Utf8Const
* a
, const Utf8Const
*b
)
177 const _Jv_ushort
*aptr
, *bptr
;
180 if (a
->hash
!= b
->hash
)
183 if (b
->length
!= len
)
185 aptr
= (const _Jv_ushort
*)a
->data
;
186 bptr
= (const _Jv_ushort
*)b
->data
;
187 len
= (len
+ 1) >> 1;
189 if (*aptr
++ != *bptr
++)
194 /* True iff A is equal to STR.
195 HASH is STR->hashCode().
199 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
201 if (a
->hash
!= (_Jv_ushort
) hash
)
203 jint len
= str
->length();
205 jchar
*sptr
= _Jv_GetStringChars (str
);
206 unsigned char* ptr
= (unsigned char*) a
->data
;
207 unsigned char* limit
= ptr
+ a
->length
;
210 int ch
= UTF8_GET (ptr
, limit
);
219 /* Like _Jv_equal, but stop after N characters. */
221 _Jv_equaln (Utf8Const
*a
, jstring str
, jint n
)
223 jint len
= str
->length();
225 jchar
*sptr
= _Jv_GetStringChars (str
);
226 unsigned char* ptr
= (unsigned char*) a
->data
;
227 unsigned char* limit
= ptr
+ a
->length
;
228 for (; n
-- > 0; i
++, sptr
++)
230 int ch
= UTF8_GET (ptr
, limit
);
239 /* Count the number of Unicode chars encoded in a given Ut8 string. */
241 _Jv_strLengthUtf8(char* str
, int len
)
244 unsigned char* limit
;
247 ptr
= (unsigned char*) str
;
250 for (; ptr
< limit
; str_length
++)
252 if (UTF8_GET (ptr
, limit
) < 0)
258 /* Calculate a hash value for a string encoded in Utf8 format.
259 * This returns the same hash value as specified or java.lang.String.hashCode.
262 _Jv_hashUtf8String (char* str
, int len
)
264 unsigned char* ptr
= (unsigned char*) str
;
265 unsigned char* limit
= ptr
+ len
;
270 int ch
= UTF8_GET (ptr
, limit
);
271 /* Updated specification from
272 http://www.javasoft.com/docs/books/jls/clarify.html. */
273 hash
= (31 * hash
) + ch
;
279 _Jv_Utf8Const::init(char *s
, int len
)
281 ::memcpy (data
, s
, len
);
284 hash
= _Jv_hashUtf8String (s
, len
) & 0xFFFF;
288 _Jv_makeUtf8Const (char* s
, int len
)
293 = (Utf8Const
*) _Jv_AllocBytes (_Jv_Utf8Const::space_needed(s
, len
));
299 _Jv_makeUtf8Const (jstring string
)
301 jint hash
= string
->hashCode ();
302 jint len
= _Jv_GetStringUTFLength (string
);
304 Utf8Const
* m
= (Utf8Const
*)
305 _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
310 _Jv_GetStringUTFRegion (string
, 0, string
->length (), m
->data
);
320 _Jv_Abort (const char *function
, const char *file
, int line
,
324 _Jv_Abort (const char *, const char *, int, const char *message
)
329 "libgcj failure: %s\n in function %s, file %s, line %d\n",
330 message
, function
, file
, line
);
332 fprintf (stderr
, "libgcj failure: %s\n", message
);
338 fail_on_finalization (jobject
)
340 JvFail ("object was finalized");
344 _Jv_GCWatch (jobject obj
)
346 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
350 _Jv_ThrowBadArrayIndex(jint bad_index
)
352 throw new java::lang::ArrayIndexOutOfBoundsException
353 (java::lang::String::valueOf (bad_index
));
357 _Jv_ThrowNullPointerException ()
359 throw new java::lang::NullPointerException
;
362 // Resolve an entry in the constant pool and return the target
365 _Jv_ResolvePoolEntry (jclass this_class
, jint index
)
367 _Jv_Constants
*pool
= &this_class
->constants
;
369 if ((pool
->tags
[index
] & JV_CONSTANT_ResolvedFlag
) != 0)
370 return pool
->data
[index
].field
->u
.addr
;
372 JvSynchronize
sync (this_class
);
373 return (_Jv_Linker::resolve_pool_entry (this_class
, index
))
378 // Explicitly throw a no memory exception.
379 // The collector calls this when it encounters an out-of-memory condition.
380 void _Jv_ThrowNoMemory()
386 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) \
387 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false)) \
388 jvmpi_notify_alloc(klass,size,obj);
390 jvmpi_notify_alloc(jclass klass
, jint size
, jobject obj
)
392 // Service JVMPI allocation request.
395 event
.event_type
= JVMPI_EVENT_OBJECT_ALLOC
;
397 event
.u
.obj_alloc
.arena_id
= 0;
398 event
.u
.obj_alloc
.class_id
= (jobjectID
) klass
;
399 event
.u
.obj_alloc
.is_array
= 0;
400 event
.u
.obj_alloc
.size
= size
;
401 event
.u
.obj_alloc
.obj_id
= (jobjectID
) obj
;
403 // FIXME: This doesn't look right for the Boehm GC. A GC may
404 // already be in progress. _Jv_DisableGC () doesn't wait for it.
405 // More importantly, I don't see the need for disabling GC, since we
406 // blatantly have a pointer to obj on our stack, ensuring that the
407 // object can't be collected. Even for a nonconservative collector,
408 // it appears to me that this must be true, since we are about to
409 // return obj. Isn't this whole approach way too intrusive for
410 // a useful profiling interface? - HB
412 (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (&event
);
415 #else /* !ENABLE_JVMPI */
416 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) /* do nothing */
419 // Allocate a new object of class KLASS.
420 // First a version that assumes that we have no finalizer, and that
421 // the class is already initialized.
422 // If we know that JVMPI is disabled, this can be replaced by a direct call
423 // to the allocator for the appropriate GC.
425 _Jv_AllocObjectNoInitNoFinalizer (jclass klass
)
427 jint size
= klass
->size ();
428 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
429 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
433 // And now a version that initializes if necessary.
435 _Jv_AllocObjectNoFinalizer (jclass klass
)
437 _Jv_InitClass (klass
);
438 jint size
= klass
->size ();
439 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
440 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
444 // And now the general version that registers a finalizer if necessary.
446 _Jv_AllocObject (jclass klass
)
448 jobject obj
= _Jv_AllocObjectNoFinalizer (klass
);
450 // We assume that the compiler only generates calls to this routine
451 // if there really is an interesting finalizer.
452 // Unfortunately, we still have to the dynamic test, since there may
453 // be cni calls to this routine.
454 // Note that on IA64 get_finalizer() returns the starting address of the
455 // function, not a function pointer. Thus this still works.
456 if (klass
->vtable
->get_finalizer ()
457 != java::lang::Object::class$
.vtable
->get_finalizer ())
458 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
462 // Allocate a String, including variable length storage.
464 _Jv_AllocString(jsize len
)
466 using namespace java::lang
;
468 jsize sz
= sizeof(java::lang::String
) + len
* sizeof(jchar
);
470 // We assert that for strings allocated this way, the data field
471 // will always point to the object itself. Thus there is no reason
472 // for the garbage collector to scan any of it.
473 // Furthermore, we're about to overwrite the string data, so
474 // initialization of the object is not an issue.
476 // String needs no initialization, and there is no finalizer, so
477 // we can go directly to the collector's allocator interface.
478 jstring obj
= (jstring
) _Jv_AllocPtrFreeObj(sz
, &String::class$
);
481 obj
->boffset
= sizeof(java::lang::String
);
483 obj
->cachedHashCode
= 0;
485 JVMPI_NOTIFY_ALLOC (&String::class$
, sz
, obj
);
490 // A version of the above that assumes the object contains no pointers,
491 // and requires no finalization. This can't happen if we need pointers
493 #ifdef JV_HASH_SYNCHRONIZATION
495 _Jv_AllocPtrFreeObject (jclass klass
)
497 _Jv_InitClass (klass
);
498 jint size
= klass
->size ();
500 jobject obj
= (jobject
) _Jv_AllocPtrFreeObj (size
, klass
);
502 JVMPI_NOTIFY_ALLOC (klass
, size
, obj
);
506 #endif /* JV_HASH_SYNCHRONIZATION */
509 // Allocate a new array of Java objects. Each object is of type
510 // `elementClass'. `init' is used to initialize each slot in the
513 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
515 if (__builtin_expect (count
< 0, false))
516 throw new java::lang::NegativeArraySizeException
;
518 JvAssert (! elementClass
->isPrimitive ());
520 // Ensure that elements pointer is properly aligned.
521 jobjectArray obj
= NULL
;
522 size_t size
= (size_t) elements (obj
);
523 // Check for overflow.
524 if (__builtin_expect ((size_t) count
>
525 (MAX_OBJECT_SIZE
- 1 - size
) / sizeof (jobject
), false))
528 size
+= count
* sizeof (jobject
);
530 jclass klass
= _Jv_GetArrayClass (elementClass
,
531 elementClass
->getClassLoaderInternal());
533 obj
= (jobjectArray
) _Jv_AllocArray (size
, klass
);
535 jsize
*lp
= const_cast<jsize
*> (&obj
->length
);
537 // We know the allocator returns zeroed memory. So don't bother
541 jobject
*ptr
= elements(obj
);
548 // Allocate a new array of primitives. ELTYPE is the type of the
549 // element, COUNT is the size of the array.
551 _Jv_NewPrimArray (jclass eltype
, jint count
)
553 int elsize
= eltype
->size();
554 if (__builtin_expect (count
< 0, false))
555 throw new java::lang::NegativeArraySizeException
;
557 JvAssert (eltype
->isPrimitive ());
558 jobject dummy
= NULL
;
559 size_t size
= (size_t) _Jv_GetArrayElementFromElementType (dummy
, eltype
);
561 // Check for overflow.
562 if (__builtin_expect ((size_t) count
>
563 (MAX_OBJECT_SIZE
- size
) / elsize
, false))
566 jclass klass
= _Jv_GetArrayClass (eltype
, 0);
568 # ifdef JV_HASH_SYNCHRONIZATION
569 // Since the vtable is always statically allocated,
570 // these are completely pointerfree! Make sure the GC doesn't touch them.
572 (__JArray
*) _Jv_AllocPtrFreeObj (size
+ elsize
* count
, klass
);
573 memset((char *)arr
+ size
, 0, elsize
* count
);
575 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (size
+ elsize
* count
, klass
);
576 // Note that we assume we are given zeroed memory by the allocator.
579 jsize
*lp
= const_cast<jsize
*> (&arr
->length
);
586 _Jv_NewArray (jint type
, jint size
)
590 case 4: return JvNewBooleanArray (size
);
591 case 5: return JvNewCharArray (size
);
592 case 6: return JvNewFloatArray (size
);
593 case 7: return JvNewDoubleArray (size
);
594 case 8: return JvNewByteArray (size
);
595 case 9: return JvNewShortArray (size
);
596 case 10: return JvNewIntArray (size
);
597 case 11: return JvNewLongArray (size
);
599 throw new java::lang::InternalError
600 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
603 // Allocate a possibly multi-dimensional array but don't check that
604 // any array length is <0.
606 _Jv_NewMultiArrayUnchecked (jclass type
, jint dimensions
, jint
*sizes
)
608 JvAssert (type
->isArray());
609 jclass element_type
= type
->getComponentType();
611 if (element_type
->isPrimitive())
612 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
614 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
618 JvAssert (! element_type
->isPrimitive());
619 JvAssert (element_type
->isArray());
620 jobject
*contents
= elements ((jobjectArray
) result
);
621 for (int i
= 0; i
< sizes
[0]; ++i
)
622 contents
[i
] = _Jv_NewMultiArrayUnchecked (element_type
, dimensions
- 1,
630 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
632 for (int i
= 0; i
< dimensions
; ++i
)
634 throw new java::lang::NegativeArraySizeException
;
636 return _Jv_NewMultiArrayUnchecked (type
, dimensions
, sizes
);
640 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
643 jint sizes
[dimensions
];
644 va_start (args
, dimensions
);
645 for (int i
= 0; i
< dimensions
; ++i
)
647 jint size
= va_arg (args
, jint
);
649 throw new java::lang::NegativeArraySizeException
;
654 return _Jv_NewMultiArrayUnchecked (array_type
, dimensions
, sizes
);
659 // Ensure 8-byte alignment, for hash synchronization.
660 #define DECLARE_PRIM_TYPE(NAME) \
661 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
663 DECLARE_PRIM_TYPE(byte
)
664 DECLARE_PRIM_TYPE(short)
665 DECLARE_PRIM_TYPE(int)
666 DECLARE_PRIM_TYPE(long)
667 DECLARE_PRIM_TYPE(boolean
)
668 DECLARE_PRIM_TYPE(char)
669 DECLARE_PRIM_TYPE(float)
670 DECLARE_PRIM_TYPE(double)
671 DECLARE_PRIM_TYPE(void)
674 _Jv_InitPrimClass (jclass cl
, char *cname
, char sig
, int len
)
676 using namespace java::lang::reflect
;
678 // We must set the vtable for the class; the Java constructor
680 (*(_Jv_VTable
**) cl
) = java::lang::Class::class$
.vtable
;
682 // Initialize the fields we care about. We do this in the same
683 // order they are declared in Class.h.
684 cl
->name
= _Jv_makeUtf8Const ((char *) cname
, -1);
685 cl
->accflags
= Modifier::PUBLIC
| Modifier::FINAL
| Modifier::ABSTRACT
;
686 cl
->method_count
= sig
;
687 cl
->size_in_bytes
= len
;
688 cl
->vtable
= JV_PRIMITIVE_VTABLE
;
689 cl
->state
= JV_STATE_DONE
;
694 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
,
697 // First count arrays.
705 jclass result
= NULL
;
709 result
= JvPrimClass (byte
);
712 result
= JvPrimClass (short);
715 result
= JvPrimClass (int);
718 result
= JvPrimClass (long);
721 result
= JvPrimClass (boolean
);
724 result
= JvPrimClass (char);
727 result
= JvPrimClass (float);
730 result
= JvPrimClass (double);
733 result
= JvPrimClass (void);
738 while (*sig
&& *sig
!= ';')
740 // Do nothing if signature appears to be malformed.
743 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (save
, sig
- save
);
744 result
= _Jv_FindClass (name
, loader
);
749 // Do nothing -- bad signature.
755 // Not really the "end", but the last valid character that we
764 while (array_count
-- > 0)
765 result
= _Jv_GetArrayClass (result
, loader
);
772 JvConvertArgv (int argc
, const char **argv
)
776 jobjectArray ar
= JvNewObjectArray(argc
, &java::lang::String::class$
, NULL
);
777 jobject
*ptr
= elements(ar
);
778 jbyteArray bytes
= NULL
;
779 for (int i
= 0; i
< argc
; i
++)
781 const char *arg
= argv
[i
];
782 int len
= strlen (arg
);
783 if (bytes
== NULL
|| bytes
->length
< len
)
784 bytes
= JvNewByteArray (len
);
785 jbyte
*bytePtr
= elements (bytes
);
786 // We assume jbyte == char.
787 memcpy (bytePtr
, arg
, len
);
789 // Now convert using the default encoding.
790 *ptr
++ = new java::lang::String (bytes
, 0, len
);
792 return (JArray
<jstring
>*) ar
;
795 // FIXME: These variables are static so that they will be
796 // automatically scanned by the Boehm collector. This is needed
797 // because with qthreads the collector won't scan the initial stack --
798 // it will only scan the qthreads stacks.
800 // Command line arguments.
801 static JArray
<jstring
> *arg_vec
;
803 // The primary thread.
804 static java::lang::Thread
*main_thread
;
806 #ifndef DISABLE_GETENV_PROPERTIES
809 next_property_key (char *s
, size_t *length
)
815 // Skip over whitespace
819 // If we've reached the end, return NULL. Also return NULL if for
820 // some reason we've come across a malformed property string.
826 // Determine the length of the property key.
844 next_property_value (char *s
, size_t *length
)
860 // If we've reached the end, return NULL.
864 // Determine the length of the property value.
883 process_gcj_properties ()
885 char *props
= getenv("GCJ_PROPERTIES");
888 size_t property_count
= 0;
893 // Whip through props quickly in order to count the number of
895 while (p
&& (p
= next_property_key (p
, &length
)))
897 // Skip to the end of the key
900 p
= next_property_value (p
, &length
);
907 // Allocate an array of property value/key pairs.
908 _Jv_Environment_Properties
=
909 (property_pair
*) malloc (sizeof(property_pair
)
910 * (property_count
+ 1));
912 // Go through the properties again, initializing _Jv_Properties
916 while (p
&& (p
= next_property_key (p
, &length
)))
918 _Jv_Environment_Properties
[property_count
].key
= p
;
919 _Jv_Environment_Properties
[property_count
].key_length
= length
;
921 // Skip to the end of the key
924 p
= next_property_value (p
, &length
);
926 _Jv_Environment_Properties
[property_count
].value
= p
;
927 _Jv_Environment_Properties
[property_count
].value_length
= length
;
934 memset ((void *) &_Jv_Environment_Properties
[property_count
],
935 0, sizeof (property_pair
));
937 // Null terminate the strings.
938 for (property_pair
*prop
= &_Jv_Environment_Properties
[0];
942 prop
->key
[prop
->key_length
] = 0;
943 prop
->value
[prop
->value_length
] = 0;
946 #endif // DISABLE_GETENV_PROPERTIES
950 _Jv_Utf8Const
*void_signature
;
951 _Jv_Utf8Const
*clinit_name
;
952 _Jv_Utf8Const
*init_name
;
953 _Jv_Utf8Const
*finit_name
;
955 bool runtimeInitialized
= false;
957 // When true, print debugging information about class loading.
958 bool verbose_class_flag
;
960 // When true, enable the bytecode verifier and BC-ABI type verification.
961 bool verifyClasses
= true;
964 // We accept all non-standard options accepted by Sun's java command,
965 // for compatibility with existing application launch scripts.
967 parse_x_arg (char* option_string
)
969 if (strlen (option_string
) <= 0)
972 if (! strcmp (option_string
, "int"))
974 // FIXME: this should cause the vm to never load shared objects
976 else if (! strcmp (option_string
, "mixed"))
978 // FIXME: allow interpreted and native code
980 else if (! strcmp (option_string
, "batch"))
982 // FIXME: disable background JIT'ing
984 else if (! strcmp (option_string
, "debug"))
986 // FIXME: add JDWP/JVMDI support
988 else if (! strncmp (option_string
, "bootclasspath:", 14))
990 // FIXME: add a parse_bootclasspath_arg function
992 else if (! strncmp (option_string
, "bootclasspath/a:", 16))
995 else if (! strncmp (option_string
, "bootclasspath/p:", 16))
998 else if (! strcmp (option_string
, "check:jni"))
1000 // FIXME: enable strict JNI checking
1002 else if (! strcmp (option_string
, "future"))
1004 // FIXME: enable strict class file format checks
1006 else if (! strcmp (option_string
, "noclassgc"))
1008 // FIXME: disable garbage collection for classes
1010 else if (! strcmp (option_string
, "incgc"))
1012 // FIXME: incremental garbage collection
1014 else if (! strncmp (option_string
, "loggc:", 6))
1016 if (option_string
[6] == '\0')
1019 "libgcj: filename argument expected for loggc option\n");
1022 // FIXME: set gc logging filename
1024 else if (! strncmp (option_string
, "ms", 2))
1026 // FIXME: ignore this option until PR 20699 is fixed.
1027 // _Jv_SetInitialHeapSize (option_string + 2);
1029 else if (! strncmp (option_string
, "mx", 2))
1030 _Jv_SetMaximumHeapSize (option_string
+ 2);
1031 else if (! strcmp (option_string
, "prof"))
1033 // FIXME: enable profiling of program running in vm
1035 else if (! strncmp (option_string
, "runhprof:", 9))
1037 // FIXME: enable specific type of vm profiling. add a
1038 // parse_runhprof_arg function
1040 else if (! strcmp (option_string
, "rs"))
1042 // FIXME: reduced system signal usage. disable thread dumps,
1043 // only terminate in response to user-initiated calls,
1044 // e.g. System.exit()
1046 else if (! strncmp (option_string
, "ss", 2))
1048 // FIXME: set thread stack size
1050 else if (! strcmp (option_string
, "X:+UseAltSigs"))
1052 // FIXME: use signals other than SIGUSR1 and SIGUSR2
1054 else if (! strcmp (option_string
, "share:off"))
1056 // FIXME: don't share class data
1058 else if (! strcmp (option_string
, "share:auto"))
1060 // FIXME: share class data where possible
1062 else if (! strcmp (option_string
, "share:on"))
1064 // FIXME: fail if impossible to share class data
1071 parse_verbose_args (char* option_string
,
1072 bool ignore_unrecognized
)
1074 size_t len
= sizeof ("-verbose") - 1;
1076 if (strlen (option_string
) < len
)
1079 if (option_string
[len
] == ':'
1080 && option_string
[len
+ 1] != '\0')
1082 char* verbose_args
= option_string
+ len
+ 1;
1086 if (! strncmp (verbose_args
,
1087 "gc", sizeof ("gc") - 1))
1089 if (verbose_args
[sizeof ("gc") - 1] == '\0'
1090 || verbose_args
[sizeof ("gc") - 1] == ',')
1092 // FIXME: we should add functions to boehm-gc that
1093 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1094 // GC_print_back_height.
1095 verbose_args
+= sizeof ("gc") - 1;
1100 fprintf (stderr
, "libgcj: unknown verbose option: %s\n",
1105 else if (! strncmp (verbose_args
,
1107 sizeof ("class") - 1))
1109 if (verbose_args
[sizeof ("class") - 1] == '\0'
1110 || verbose_args
[sizeof ("class") - 1] == ',')
1112 gcj::verbose_class_flag
= true;
1113 verbose_args
+= sizeof ("class") - 1;
1116 goto verbose_arg_err
;
1118 else if (! strncmp (verbose_args
, "jni",
1119 sizeof ("jni") - 1))
1121 if (verbose_args
[sizeof ("jni") - 1] == '\0'
1122 || verbose_args
[sizeof ("jni") - 1] == ',')
1124 // FIXME: enable JNI messages.
1125 verbose_args
+= sizeof ("jni") - 1;
1128 goto verbose_arg_err
;
1130 else if (ignore_unrecognized
1131 && verbose_args
[0] == 'X')
1133 // ignore unrecognized non-standard verbose option
1134 while (verbose_args
[0] != '\0'
1135 && verbose_args
[0] != ',')
1138 else if (verbose_args
[0] == ',')
1143 goto verbose_arg_err
;
1145 if (verbose_args
[0] == ',')
1148 while (verbose_args
[0] != '\0');
1150 else if (option_string
[len
] == 'g'
1151 && option_string
[len
+ 1] == 'c'
1152 && option_string
[len
+ 2] == '\0')
1154 // FIXME: we should add functions to boehm-gc that
1155 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1156 // GC_print_back_height.
1159 else if (option_string
[len
] == '\0')
1161 gcj::verbose_class_flag
= true;
1166 // unrecognized option beginning with -verbose
1173 parse_init_args (JvVMInitArgs
* vm_args
)
1175 // if _Jv_Compiler_Properties is non-NULL then it needs to be
1176 // re-allocated dynamically.
1177 if (_Jv_Compiler_Properties
)
1179 const char** props
= _Jv_Compiler_Properties
;
1180 _Jv_Compiler_Properties
= NULL
;
1182 for (int i
= 0; props
[i
]; i
++)
1184 _Jv_Compiler_Properties
= (const char**) _Jv_Realloc
1185 (_Jv_Compiler_Properties
,
1186 (_Jv_Properties_Count
+ 1) * sizeof (const char*));
1187 _Jv_Compiler_Properties
[_Jv_Properties_Count
++] = props
[i
];
1191 if (vm_args
== NULL
)
1194 for (int i
= 0; i
< vm_args
->nOptions
; ++i
)
1196 char* option_string
= vm_args
->options
[i
].optionString
;
1197 if (! strcmp (option_string
, "vfprintf")
1198 || ! strcmp (option_string
, "exit")
1199 || ! strcmp (option_string
, "abort"))
1201 // FIXME: we are required to recognize these, but for
1202 // now we don't handle them in any way.
1205 else if (! strncmp (option_string
,
1206 "-verbose", sizeof ("-verbose") - 1))
1208 jint result
= parse_verbose_args (option_string
,
1209 vm_args
->ignoreUnrecognized
);
1213 else if (! strncmp (option_string
, "-D", 2))
1215 _Jv_Compiler_Properties
= (const char**) _Jv_Realloc
1216 (_Jv_Compiler_Properties
,
1217 (_Jv_Properties_Count
+ 1) * sizeof (char*));
1219 _Jv_Compiler_Properties
[_Jv_Properties_Count
++] =
1220 strdup (option_string
+ 2);
1224 else if (vm_args
->ignoreUnrecognized
)
1226 if (option_string
[0] == '_')
1227 parse_x_arg (option_string
+ 1);
1228 else if (! strncmp (option_string
, "-X", 2))
1229 parse_x_arg (option_string
+ 2);
1233 fprintf (stderr
, "libgcj: unknown option: %s\n", option_string
);
1238 goto unknown_option
;
1244 _Jv_CreateJavaVM (JvVMInitArgs
* vm_args
)
1246 using namespace gcj
;
1248 if (runtimeInitialized
)
1251 runtimeInitialized
= true;
1253 jint result
= parse_init_args (vm_args
);
1257 PROCESS_GCJ_PROPERTIES
;
1259 /* Threads must be initialized before the GC, so that it inherits the
1263 _Jv_InitializeSyncMutex ();
1266 _Jv_InitInterpreter ();
1277 /* Initialize Utf8 constants declared in jvm.h. */
1278 void_signature
= _Jv_makeUtf8Const ("()V", 3);
1279 clinit_name
= _Jv_makeUtf8Const ("<clinit>", 8);
1280 init_name
= _Jv_makeUtf8Const ("<init>", 6);
1281 finit_name
= _Jv_makeUtf8Const ("finit$", 6);
1283 /* Initialize built-in classes to represent primitive TYPEs. */
1284 _Jv_InitPrimClass (&_Jv_byteClass
, "byte", 'B', 1);
1285 _Jv_InitPrimClass (&_Jv_shortClass
, "short", 'S', 2);
1286 _Jv_InitPrimClass (&_Jv_intClass
, "int", 'I', 4);
1287 _Jv_InitPrimClass (&_Jv_longClass
, "long", 'J', 8);
1288 _Jv_InitPrimClass (&_Jv_booleanClass
, "boolean", 'Z', 1);
1289 _Jv_InitPrimClass (&_Jv_charClass
, "char", 'C', 2);
1290 _Jv_InitPrimClass (&_Jv_floatClass
, "float", 'F', 4);
1291 _Jv_InitPrimClass (&_Jv_doubleClass
, "double", 'D', 8);
1292 _Jv_InitPrimClass (&_Jv_voidClass
, "void", 'V', 0);
1294 // Turn stack trace generation off while creating exception objects.
1295 _Jv_InitClass (&java::lang::VMThrowable::class$
);
1296 java::lang::VMThrowable::trace_enabled
= 0;
1298 // We have to initialize this fairly early, to avoid circular class
1299 // initialization. In particular we want to start the
1300 // initialization of ClassLoader before we start the initialization
1301 // of VMClassLoader.
1302 _Jv_InitClass (&java::lang::ClassLoader::class$
);
1304 // Set up the system class loader and the bootstrap class loader.
1305 gnu::gcj::runtime::ExtensionClassLoader::initialize();
1306 java::lang::VMClassLoader::initialize(JvNewStringLatin1(TOOLEXECLIBDIR
));
1308 _Jv_RegisterBootstrapPackages();
1310 no_memory
= new java::lang::OutOfMemoryError
;
1312 java::lang::VMThrowable::trace_enabled
= 1;
1315 LTDL_SET_PRELOADED_SYMBOLS ();
1318 _Jv_platform_initialize ();
1322 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady
);
1324 // Start the GC finalizer thread. A VirtualMachineError can be
1325 // thrown by the runtime if, say, threads aren't available.
1328 using namespace gnu::gcj::runtime
;
1329 FinalizerThread
*ft
= new FinalizerThread ();
1332 catch (java::lang::VirtualMachineError
*ignore
)
1340 _Jv_RunMain (JvVMInitArgs
*vm_args
, jclass klass
, const char *name
, int argc
,
1341 const char **argv
, bool is_jar
)
1343 #ifndef DISABLE_MAIN_ARGS
1344 _Jv_SetArgs (argc
, argv
);
1347 java::lang::Runtime
*runtime
= NULL
;
1351 if (_Jv_CreateJavaVM (vm_args
) < 0)
1353 fprintf (stderr
, "libgcj: couldn't create virtual machine\n");
1357 // Get the Runtime here. We want to initialize it before searching
1358 // for `main'; that way it will be set up if `main' is a JNI method.
1359 runtime
= java::lang::Runtime::getRuntime ();
1361 #ifdef DISABLE_MAIN_ARGS
1362 arg_vec
= JvConvertArgv (0, 0);
1364 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
1367 using namespace gnu::java::lang
;
1369 main_thread
= new MainThread (klass
, arg_vec
);
1371 main_thread
= new MainThread (JvNewStringLatin1 (name
),
1374 catch (java::lang::Throwable
*t
)
1376 java::lang::System::err
->println (JvNewStringLatin1
1377 ("Exception during runtime initialization"));
1378 t
->printStackTrace();
1381 // In case the runtime creation failed.
1385 _Jv_AttachCurrentThread (main_thread
);
1386 _Jv_ThreadRun (main_thread
);
1389 int status
= (int) java::lang::ThreadGroup::had_uncaught_exception
;
1390 runtime
->exit (status
);
1394 _Jv_RunMain (jclass klass
, const char *name
, int argc
, const char **argv
,
1397 _Jv_RunMain (NULL
, klass
, name
, argc
, argv
, is_jar
);
1401 JvRunMain (jclass klass
, int argc
, const char **argv
)
1403 _Jv_RunMain (klass
, NULL
, argc
, argv
, false);
1408 // Parse a string and return a heap size.
1410 parse_heap_size (const char *spec
)
1413 unsigned long val
= strtoul (spec
, &end
, 10);
1414 if (*end
== 'k' || *end
== 'K')
1416 else if (*end
== 'm' || *end
== 'M')
1418 return (size_t) val
;
1421 // Set the initial heap size. This might be ignored by the GC layer.
1422 // This must be called before _Jv_RunMain.
1424 _Jv_SetInitialHeapSize (const char *arg
)
1426 size_t size
= parse_heap_size (arg
);
1427 _Jv_GCSetInitialHeapSize (size
);
1430 // Set the maximum heap size. This might be ignored by the GC layer.
1431 // This must be called before _Jv_RunMain.
1433 _Jv_SetMaximumHeapSize (const char *arg
)
1435 size_t size
= parse_heap_size (arg
);
1436 _Jv_GCSetMaximumHeapSize (size
);
1442 _Jv_Malloc (jsize size
)
1444 if (__builtin_expect (size
== 0, false))
1446 void *ptr
= malloc ((size_t) size
);
1447 if (__builtin_expect (ptr
== NULL
, false))
1453 _Jv_Realloc (void *ptr
, jsize size
)
1455 if (__builtin_expect (size
== 0, false))
1457 ptr
= realloc (ptr
, (size_t) size
);
1458 if (__builtin_expect (ptr
== NULL
, false))
1464 _Jv_MallocUnchecked (jsize size
)
1466 if (__builtin_expect (size
== 0, false))
1468 return malloc ((size_t) size
);
1472 _Jv_Free (void* ptr
)
1479 // In theory, these routines can be #ifdef'd away on machines which
1480 // support divide overflow signals. However, we never know if some
1481 // code might have been compiled with "-fuse-divide-subroutine", so we
1482 // always include them in libgcj.
1485 _Jv_divI (jint dividend
, jint divisor
)
1487 if (__builtin_expect (divisor
== 0, false))
1489 java::lang::ArithmeticException
*arithexception
1490 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1491 throw arithexception
;
1494 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1497 return dividend
/ divisor
;
1501 _Jv_remI (jint dividend
, jint divisor
)
1503 if (__builtin_expect (divisor
== 0, false))
1505 java::lang::ArithmeticException
*arithexception
1506 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1507 throw arithexception
;
1510 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1513 return dividend
% divisor
;
1517 _Jv_divJ (jlong dividend
, jlong divisor
)
1519 if (__builtin_expect (divisor
== 0, false))
1521 java::lang::ArithmeticException
*arithexception
1522 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1523 throw arithexception
;
1526 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1529 return dividend
/ divisor
;
1533 _Jv_remJ (jlong dividend
, jlong divisor
)
1535 if (__builtin_expect (divisor
== 0, false))
1537 java::lang::ArithmeticException
*arithexception
1538 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1539 throw arithexception
;
1542 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1545 return dividend
% divisor
;
1550 // Return true if SELF_KLASS can access a field or method in
1551 // OTHER_KLASS. The field or method's access flags are specified in
1554 _Jv_CheckAccess (jclass self_klass
, jclass other_klass
, jint flags
)
1556 using namespace java::lang::reflect
;
1557 return ((self_klass
== other_klass
)
1558 || ((flags
& Modifier::PUBLIC
) != 0)
1559 || (((flags
& Modifier::PROTECTED
) != 0)
1560 && _Jv_IsAssignableFromSlow (self_klass
, other_klass
))
1561 || (((flags
& Modifier::PRIVATE
) == 0)
1562 && _Jv_ClassNameSamePackage (self_klass
->name
,
1563 other_klass
->name
)));