Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / libjava / prims.cc
blob90f8dc5ca23cb388f21ee17dc4d3c449310d1b99
1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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
9 details. */
11 #include <config.h>
12 #include <platform.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <signal.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
24 #include <gcj/cni.h>
25 #include <jvm.h>
26 #include <java-signal.h>
27 #include <java-threads.h>
28 #include <java-interp.h>
30 #ifdef ENABLE_JVMPI
31 #include <jvmpi.h>
32 #include <java/lang/ThreadGroup.h>
33 #endif
35 #ifdef INTERPRETER
36 #include <jvmti.h>
37 #include "jvmti-int.h"
38 #endif
40 #ifndef DISABLE_GETENV_PROPERTIES
41 #include <ctype.h>
42 #include <java-props.h>
43 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
44 #else
45 #define PROCESS_GCJ_PROPERTIES
46 #endif // DISABLE_GETENV_PROPERTIES
48 #include <java/lang/Class.h>
49 #include <java/lang/ClassLoader.h>
50 #include <java/lang/Runtime.h>
51 #include <java/lang/String.h>
52 #include <java/lang/Thread.h>
53 #include <java/lang/ThreadGroup.h>
54 #include <java/lang/ArrayIndexOutOfBoundsException.h>
55 #include <java/lang/ArithmeticException.h>
56 #include <java/lang/ClassFormatError.h>
57 #include <java/lang/ClassNotFoundException.h>
58 #include <java/lang/InternalError.h>
59 #include <java/lang/NegativeArraySizeException.h>
60 #include <java/lang/NoClassDefFoundError.h>
61 #include <java/lang/NullPointerException.h>
62 #include <java/lang/OutOfMemoryError.h>
63 #include <java/lang/System.h>
64 #include <java/lang/VMClassLoader.h>
65 #include <java/lang/reflect/Modifier.h>
66 #include <java/io/PrintStream.h>
67 #include <java/lang/UnsatisfiedLinkError.h>
68 #include <java/lang/VirtualMachineError.h>
69 #include <gnu/gcj/runtime/ExtensionClassLoader.h>
70 #include <gnu/gcj/runtime/FinalizerThread.h>
71 #include <execution.h>
73 #ifdef INTERPRETER
74 #include <gnu/classpath/jdwp/Jdwp.h>
75 #include <gnu/classpath/jdwp/VMVirtualMachine.h>
76 #endif // INTERPRETER
78 #include <gnu/java/lang/MainThread.h>
80 #ifdef USE_LTDL
81 #include <ltdl.h>
82 #endif
84 // Execution engine for compiled code.
85 _Jv_CompiledEngine _Jv_soleCompiledEngine;
87 // Execution engine for code compiled with -findirect-classes
88 _Jv_IndirectCompiledEngine _Jv_soleIndirectCompiledEngine;
90 // We allocate a single OutOfMemoryError exception which we keep
91 // around for use if we run out of memory.
92 static java::lang::OutOfMemoryError *no_memory;
94 // Number of bytes in largest array object we create. This could be
95 // increased to the largest size_t value, so long as the appropriate
96 // functions are changed to take a size_t argument instead of jint.
97 #define MAX_OBJECT_SIZE (((size_t)1<<31) - 1)
99 // Properties set at compile time.
100 const char **_Jv_Compiler_Properties = NULL;
101 int _Jv_Properties_Count = 0;
103 #ifndef DISABLE_GETENV_PROPERTIES
104 // Property key/value pairs.
105 property_pair *_Jv_Environment_Properties;
106 #endif
108 // Stash the argv pointer to benefit native libraries that need it.
109 const char **_Jv_argv;
110 int _Jv_argc;
112 // Debugging options
113 static bool remoteDebug = false;
114 #ifdef INTERPRETER
115 static char defaultJdwpOptions[] = "";
116 static char *jdwpOptions = defaultJdwpOptions;
118 // Typedefs for JVMTI agent functions.
119 typedef jint jvmti_agent_onload_func (JavaVM *vm, char *options,
120 void *reserved);
121 typedef jint jvmti_agent_onunload_func (JavaVM *vm);
123 // JVMTI agent function pointers.
124 static jvmti_agent_onload_func *jvmti_agentonload = NULL;
125 static jvmti_agent_onunload_func *jvmti_agentonunload = NULL;
126 static char *jvmti_agent_opts;
127 #endif // INTERPRETER
129 // Argument support.
131 _Jv_GetNbArgs (void)
133 // _Jv_argc is 0 if not explicitly initialized.
134 return _Jv_argc;
137 const char *
138 _Jv_GetSafeArg (int index)
140 if (index >=0 && index < _Jv_GetNbArgs ())
141 return _Jv_argv[index];
142 else
143 return "";
146 void
147 _Jv_SetArgs (int argc, const char **argv)
149 _Jv_argc = argc;
150 _Jv_argv = argv;
153 #ifdef ENABLE_JVMPI
154 // Pointer to JVMPI notification functions.
155 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
156 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
157 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
158 #endif
161 #if defined (HANDLE_SEGV) || defined(HANDLE_FPE)
162 /* Unblock a signal. Unless we do this, the signal may only be sent
163 once. */
164 static void
165 unblock_signal (int signum __attribute__ ((__unused__)))
167 #ifdef _POSIX_VERSION
168 sigset_t sigs;
170 sigemptyset (&sigs);
171 sigaddset (&sigs, signum);
172 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
173 #endif
175 #endif
177 #ifdef HANDLE_SEGV
178 SIGNAL_HANDLER (catch_segv)
180 unblock_signal (SIGSEGV);
181 MAKE_THROW_FRAME (nullp);
182 java::lang::NullPointerException *nullp
183 = new java::lang::NullPointerException;
184 throw nullp;
186 #endif
188 #ifdef HANDLE_FPE
189 SIGNAL_HANDLER (catch_fpe)
191 unblock_signal (SIGFPE);
192 #ifdef HANDLE_DIVIDE_OVERFLOW
193 HANDLE_DIVIDE_OVERFLOW;
194 #else
195 MAKE_THROW_FRAME (arithexception);
196 #endif
197 java::lang::ArithmeticException *arithexception
198 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
199 throw arithexception;
201 #endif
204 jboolean
205 _Jv_equalUtf8Consts (const Utf8Const* a, const Utf8Const *b)
207 int len;
208 const _Jv_ushort *aptr, *bptr;
209 if (a == b)
210 return true;
211 if (a->hash != b->hash)
212 return false;
213 len = a->length;
214 if (b->length != len)
215 return false;
216 aptr = (const _Jv_ushort *)a->data;
217 bptr = (const _Jv_ushort *)b->data;
218 len = (len + 1) >> 1;
219 while (--len >= 0)
220 if (*aptr++ != *bptr++)
221 return false;
222 return true;
225 /* True iff A is equal to STR.
226 HASH is STR->hashCode().
229 jboolean
230 _Jv_equal (Utf8Const* a, jstring str, jint hash)
232 if (a->hash != (_Jv_ushort) hash)
233 return false;
234 jint len = str->length();
235 jint i = 0;
236 jchar *sptr = _Jv_GetStringChars (str);
237 unsigned char* ptr = (unsigned char*) a->data;
238 unsigned char* limit = ptr + a->length;
239 for (;; i++, sptr++)
241 int ch = UTF8_GET (ptr, limit);
242 if (i == len)
243 return ch < 0;
244 if (ch != *sptr)
245 return false;
247 return true;
250 /* Like _Jv_equal, but stop after N characters. */
251 jboolean
252 _Jv_equaln (Utf8Const *a, jstring str, jint n)
254 jint len = str->length();
255 jint i = 0;
256 jchar *sptr = _Jv_GetStringChars (str);
257 unsigned char* ptr = (unsigned char*) a->data;
258 unsigned char* limit = ptr + a->length;
259 for (; n-- > 0; i++, sptr++)
261 int ch = UTF8_GET (ptr, limit);
262 if (i == len)
263 return ch < 0;
264 if (ch != *sptr)
265 return false;
267 return true;
270 // Determines whether the given Utf8Const object contains
271 // a type which is primitive or some derived form of it, eg.
272 // an array or multi-dimensional array variant.
273 jboolean
274 _Jv_isPrimitiveOrDerived(const Utf8Const *a)
276 unsigned char *aptr = (unsigned char *) a->data;
277 unsigned char *alimit = aptr + a->length;
278 int ac = UTF8_GET(aptr, alimit);
280 // Skips any leading array marks.
281 while (ac == '[')
282 ac = UTF8_GET(aptr, alimit);
284 // There should not be another character. This implies that
285 // the type name is only one character long.
286 if (UTF8_GET(aptr, alimit) == -1)
287 switch ( ac )
289 case 'Z':
290 case 'B':
291 case 'C':
292 case 'S':
293 case 'I':
294 case 'J':
295 case 'F':
296 case 'D':
297 return true;
298 default:
299 break;
302 return false;
305 // Find out whether two _Jv_Utf8Const candidates contain the same
306 // classname.
307 // The method is written to handle the different formats of classnames.
308 // Eg. "Ljava/lang/Class;", "Ljava.lang.Class;", "java/lang/Class" and
309 // "java.lang.Class" will be seen as equal.
310 // Warning: This function is not smart enough to declare "Z" and "boolean"
311 // and similar cases as equal (and is not meant to be used this way)!
312 jboolean
313 _Jv_equalUtf8Classnames (const Utf8Const *a, const Utf8Const *b)
315 // If the class name's length differs by two characters
316 // it is possible that we have candidates which are given
317 // in the two different formats ("Lp1/p2/cn;" vs. "p1/p2/cn")
318 switch (a->length - b->length)
320 case -2:
321 case 0:
322 case 2:
323 break;
324 default:
325 return false;
328 unsigned char *aptr = (unsigned char *) a->data;
329 unsigned char *alimit = aptr + a->length;
330 unsigned char *bptr = (unsigned char *) b->data;
331 unsigned char *blimit = bptr + b->length;
333 if (alimit[-1] == ';')
334 alimit--;
336 if (blimit[-1] == ';')
337 blimit--;
339 int ac = UTF8_GET(aptr, alimit);
340 int bc = UTF8_GET(bptr, blimit);
342 // Checks whether both strings have the same amount of leading [ characters.
343 while (ac == '[')
345 if (bc == '[')
347 ac = UTF8_GET(aptr, alimit);
348 bc = UTF8_GET(bptr, blimit);
349 continue;
352 return false;
355 // Skips leading L character.
356 if (ac == 'L')
357 ac = UTF8_GET(aptr, alimit);
359 if (bc == 'L')
360 bc = UTF8_GET(bptr, blimit);
362 // Compares the remaining characters.
363 while (ac != -1 && bc != -1)
365 // Replaces package separating dots with slashes.
366 if (ac == '.')
367 ac = '/';
369 if (bc == '.')
370 bc = '/';
372 // Now classnames differ if there is at least one non-matching
373 // character.
374 if (ac != bc)
375 return false;
377 ac = UTF8_GET(aptr, alimit);
378 bc = UTF8_GET(bptr, blimit);
381 return (ac == bc);
384 /* Count the number of Unicode chars encoded in a given Ut8 string. */
386 _Jv_strLengthUtf8(const char* str, int len)
388 unsigned char* ptr;
389 unsigned char* limit;
390 int str_length;
392 ptr = (unsigned char*) str;
393 limit = ptr + len;
394 str_length = 0;
395 for (; ptr < limit; str_length++)
397 if (UTF8_GET (ptr, limit) < 0)
398 return (-1);
400 return (str_length);
403 /* Calculate a hash value for a string encoded in Utf8 format.
404 * This returns the same hash value as specified or java.lang.String.hashCode.
406 jint
407 _Jv_hashUtf8String (const char* str, int len)
409 unsigned char* ptr = (unsigned char*) str;
410 unsigned char* limit = ptr + len;
411 jint hash = 0;
413 for (; ptr < limit;)
415 int ch = UTF8_GET (ptr, limit);
416 /* Updated specification from
417 http://www.javasoft.com/docs/books/jls/clarify.html. */
418 hash = (31 * hash) + ch;
420 return hash;
423 void
424 _Jv_Utf8Const::init(const char *s, int len)
426 ::memcpy (data, s, len);
427 data[len] = 0;
428 length = len;
429 hash = _Jv_hashUtf8String (s, len) & 0xFFFF;
432 _Jv_Utf8Const *
433 _Jv_makeUtf8Const (const char* s, int len)
435 if (len < 0)
436 len = strlen (s);
437 Utf8Const* m
438 = (Utf8Const*) _Jv_AllocBytes (_Jv_Utf8Const::space_needed(s, len));
439 m->init(s, len);
440 return m;
443 _Jv_Utf8Const *
444 _Jv_makeUtf8Const (jstring string)
446 jint hash = string->hashCode ();
447 jint len = _Jv_GetStringUTFLength (string);
449 Utf8Const* m = (Utf8Const*)
450 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
452 m->hash = hash;
453 m->length = len;
455 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
456 m->data[len] = 0;
458 return m;
463 #ifdef __GCJ_DEBUG
464 void
465 _Jv_Abort (const char *function, const char *file, int line,
466 const char *message)
467 #else
468 void
469 _Jv_Abort (const char *, const char *, int, const char *message)
470 #endif
472 #ifdef __GCJ_DEBUG
473 fprintf (stderr,
474 "libgcj failure: %s\n in function %s, file %s, line %d\n",
475 message, function, file, line);
476 #else
477 fprintf (stderr, "libgcj failure: %s\n", message);
478 #endif
479 fflush (stderr);
480 abort ();
483 static void
484 fail_on_finalization (jobject)
486 JvFail ("object was finalized");
489 void
490 _Jv_GCWatch (jobject obj)
492 _Jv_RegisterFinalizer (obj, fail_on_finalization);
495 void
496 _Jv_ThrowBadArrayIndex(jint bad_index)
498 throw new java::lang::ArrayIndexOutOfBoundsException
499 (java::lang::String::valueOf (bad_index));
502 void
503 _Jv_ThrowNullPointerException ()
505 throw new java::lang::NullPointerException;
508 // Resolve an entry in the constant pool and return the target
509 // address.
510 void *
511 _Jv_ResolvePoolEntry (jclass this_class, jint index)
513 _Jv_Constants *pool = &this_class->constants;
515 if ((pool->tags[index] & JV_CONSTANT_ResolvedFlag) != 0)
516 return pool->data[index].field->u.addr;
518 JvSynchronize sync (this_class);
519 return (_Jv_Linker::resolve_pool_entry (this_class, index))
520 .field->u.addr;
524 // Explicitly throw a no memory exception.
525 // The collector calls this when it encounters an out-of-memory condition.
526 void _Jv_ThrowNoMemory()
528 throw no_memory;
531 #ifdef ENABLE_JVMPI
532 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) \
533 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false)) \
534 jvmpi_notify_alloc(klass,size,obj);
535 static void
536 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
538 // Service JVMPI allocation request.
539 JVMPI_Event event;
541 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
542 event.env_id = NULL;
543 event.u.obj_alloc.arena_id = 0;
544 event.u.obj_alloc.class_id = (jobjectID) klass;
545 event.u.obj_alloc.is_array = 0;
546 event.u.obj_alloc.size = size;
547 event.u.obj_alloc.obj_id = (jobjectID) obj;
549 // FIXME: This doesn't look right for the Boehm GC. A GC may
550 // already be in progress. _Jv_DisableGC () doesn't wait for it.
551 // More importantly, I don't see the need for disabling GC, since we
552 // blatantly have a pointer to obj on our stack, ensuring that the
553 // object can't be collected. Even for a nonconservative collector,
554 // it appears to me that this must be true, since we are about to
555 // return obj. Isn't this whole approach way too intrusive for
556 // a useful profiling interface? - HB
557 _Jv_DisableGC ();
558 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
559 _Jv_EnableGC ();
561 #else /* !ENABLE_JVMPI */
562 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) /* do nothing */
563 #endif
565 // Allocate a new object of class KLASS.
566 // First a version that assumes that we have no finalizer, and that
567 // the class is already initialized.
568 // If we know that JVMPI is disabled, this can be replaced by a direct call
569 // to the allocator for the appropriate GC.
570 jobject
571 _Jv_AllocObjectNoInitNoFinalizer (jclass klass)
573 jint size = klass->size ();
574 jobject obj = (jobject) _Jv_AllocObj (size, klass);
575 JVMPI_NOTIFY_ALLOC (klass, size, obj);
576 return obj;
579 // And now a version that initializes if necessary.
580 jobject
581 _Jv_AllocObjectNoFinalizer (jclass klass)
583 if (_Jv_IsPhantomClass(klass) )
584 throw new java::lang::NoClassDefFoundError(klass->getName());
586 _Jv_InitClass (klass);
587 jint size = klass->size ();
588 jobject obj = (jobject) _Jv_AllocObj (size, klass);
589 JVMPI_NOTIFY_ALLOC (klass, size, obj);
590 return obj;
593 // And now the general version that registers a finalizer if necessary.
594 jobject
595 _Jv_AllocObject (jclass klass)
597 jobject obj = _Jv_AllocObjectNoFinalizer (klass);
599 // We assume that the compiler only generates calls to this routine
600 // if there really is an interesting finalizer.
601 // Unfortunately, we still have to the dynamic test, since there may
602 // be cni calls to this routine.
603 // Note that on IA64 get_finalizer() returns the starting address of the
604 // function, not a function pointer. Thus this still works.
605 if (klass->vtable->get_finalizer ()
606 != java::lang::Object::class$.vtable->get_finalizer ())
607 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
608 return obj;
611 // Allocate a String, including variable length storage.
612 jstring
613 _Jv_AllocString(jsize len)
615 using namespace java::lang;
617 jsize sz = sizeof(java::lang::String) + len * sizeof(jchar);
619 // We assert that for strings allocated this way, the data field
620 // will always point to the object itself. Thus there is no reason
621 // for the garbage collector to scan any of it.
622 // Furthermore, we're about to overwrite the string data, so
623 // initialization of the object is not an issue.
625 // String needs no initialization, and there is no finalizer, so
626 // we can go directly to the collector's allocator interface.
627 jstring obj = (jstring) _Jv_AllocPtrFreeObj(sz, &String::class$);
629 obj->data = obj;
630 obj->boffset = sizeof(java::lang::String);
631 obj->count = len;
632 obj->cachedHashCode = 0;
634 JVMPI_NOTIFY_ALLOC (&String::class$, sz, obj);
636 return obj;
639 // A version of the above that assumes the object contains no pointers,
640 // and requires no finalization. This can't happen if we need pointers
641 // to locks.
642 #ifdef JV_HASH_SYNCHRONIZATION
643 jobject
644 _Jv_AllocPtrFreeObject (jclass klass)
646 _Jv_InitClass (klass);
647 jint size = klass->size ();
649 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
651 JVMPI_NOTIFY_ALLOC (klass, size, obj);
653 return obj;
655 #endif /* JV_HASH_SYNCHRONIZATION */
658 // Allocate a new array of Java objects. Each object is of type
659 // `elementClass'. `init' is used to initialize each slot in the
660 // array.
661 jobjectArray
662 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
664 // Creating an array of an unresolved type is impossible. So we throw
665 // the NoClassDefFoundError.
666 if ( _Jv_IsPhantomClass(elementClass) )
667 throw new java::lang::NoClassDefFoundError(elementClass->getName());
669 if (__builtin_expect (count < 0, false))
670 throw new java::lang::NegativeArraySizeException;
672 JvAssert (! elementClass->isPrimitive ());
674 // Ensure that elements pointer is properly aligned.
675 jobjectArray obj = NULL;
676 size_t size = (size_t) elements (obj);
677 // Check for overflow.
678 if (__builtin_expect ((size_t) count >
679 (MAX_OBJECT_SIZE - 1 - size) / sizeof (jobject), false))
680 throw no_memory;
682 size += count * sizeof (jobject);
684 jclass klass = _Jv_GetArrayClass (elementClass,
685 elementClass->getClassLoaderInternal());
687 obj = (jobjectArray) _Jv_AllocArray (size, klass);
688 // Cast away const.
689 jsize *lp = const_cast<jsize *> (&obj->length);
690 *lp = count;
691 // We know the allocator returns zeroed memory. So don't bother
692 // zeroing it again.
693 if (init)
695 jobject *ptr = elements(obj);
696 while (--count >= 0)
697 *ptr++ = init;
699 return obj;
702 // Allocate a new array of primitives. ELTYPE is the type of the
703 // element, COUNT is the size of the array.
704 jobject
705 _Jv_NewPrimArray (jclass eltype, jint count)
707 int elsize = eltype->size();
708 if (__builtin_expect (count < 0, false))
709 throw new java::lang::NegativeArraySizeException;
711 JvAssert (eltype->isPrimitive ());
712 jobject dummy = NULL;
713 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
715 // Check for overflow.
716 if (__builtin_expect ((size_t) count >
717 (MAX_OBJECT_SIZE - size) / elsize, false))
718 throw no_memory;
720 jclass klass = _Jv_GetArrayClass (eltype, 0);
722 # ifdef JV_HASH_SYNCHRONIZATION
723 // Since the vtable is always statically allocated,
724 // these are completely pointerfree! Make sure the GC doesn't touch them.
725 __JArray *arr =
726 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
727 memset((char *)arr + size, 0, elsize * count);
728 # else
729 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
730 // Note that we assume we are given zeroed memory by the allocator.
731 # endif
732 // Cast away const.
733 jsize *lp = const_cast<jsize *> (&arr->length);
734 *lp = count;
736 return arr;
739 jobject
740 _Jv_NewArray (jint type, jint size)
742 switch (type)
744 case 4: return JvNewBooleanArray (size);
745 case 5: return JvNewCharArray (size);
746 case 6: return JvNewFloatArray (size);
747 case 7: return JvNewDoubleArray (size);
748 case 8: return JvNewByteArray (size);
749 case 9: return JvNewShortArray (size);
750 case 10: return JvNewIntArray (size);
751 case 11: return JvNewLongArray (size);
753 throw new java::lang::InternalError
754 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
757 // Allocate a possibly multi-dimensional array but don't check that
758 // any array length is <0.
759 static jobject
760 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
762 JvAssert (type->isArray());
763 jclass element_type = type->getComponentType();
764 jobject result;
765 if (element_type->isPrimitive())
766 result = _Jv_NewPrimArray (element_type, sizes[0]);
767 else
768 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
770 if (dimensions > 1)
772 JvAssert (! element_type->isPrimitive());
773 JvAssert (element_type->isArray());
774 jobject *contents = elements ((jobjectArray) result);
775 for (int i = 0; i < sizes[0]; ++i)
776 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
777 sizes + 1);
780 return result;
783 jobject
784 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
786 for (int i = 0; i < dimensions; ++i)
787 if (sizes[i] < 0)
788 throw new java::lang::NegativeArraySizeException;
790 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
793 jobject
794 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
796 // Creating an array of an unresolved type is impossible. So we throw
797 // the NoClassDefFoundError.
798 if (_Jv_IsPhantomClass(array_type))
799 throw new java::lang::NoClassDefFoundError(array_type->getName());
801 va_list args;
802 jint sizes[dimensions];
803 va_start (args, dimensions);
804 for (int i = 0; i < dimensions; ++i)
806 jint size = va_arg (args, jint);
807 if (size < 0)
808 throw new java::lang::NegativeArraySizeException;
809 sizes[i] = size;
811 va_end (args);
813 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
818 // Ensure 8-byte alignment, for hash synchronization.
819 #define DECLARE_PRIM_TYPE(NAME) \
820 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
822 DECLARE_PRIM_TYPE(byte)
823 DECLARE_PRIM_TYPE(short)
824 DECLARE_PRIM_TYPE(int)
825 DECLARE_PRIM_TYPE(long)
826 DECLARE_PRIM_TYPE(boolean)
827 DECLARE_PRIM_TYPE(char)
828 DECLARE_PRIM_TYPE(float)
829 DECLARE_PRIM_TYPE(double)
830 DECLARE_PRIM_TYPE(void)
832 void
833 _Jv_InitPrimClass (jclass cl, const char *cname, char sig, int len)
835 using namespace java::lang::reflect;
837 // We must set the vtable for the class; the Java constructor
838 // doesn't do this.
839 (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
841 // Initialize the fields we care about. We do this in the same
842 // order they are declared in Class.h.
843 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
844 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
845 cl->method_count = sig;
846 cl->size_in_bytes = len;
847 cl->vtable = JV_PRIMITIVE_VTABLE;
848 cl->state = JV_STATE_DONE;
849 cl->depth = -1;
852 jclass
853 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader,
854 char **endp)
856 // First count arrays.
857 int array_count = 0;
858 while (*sig == '[')
860 ++sig;
861 ++array_count;
864 jclass result = NULL;
865 switch (*sig)
867 case 'B':
868 result = JvPrimClass (byte);
869 break;
870 case 'S':
871 result = JvPrimClass (short);
872 break;
873 case 'I':
874 result = JvPrimClass (int);
875 break;
876 case 'J':
877 result = JvPrimClass (long);
878 break;
879 case 'Z':
880 result = JvPrimClass (boolean);
881 break;
882 case 'C':
883 result = JvPrimClass (char);
884 break;
885 case 'F':
886 result = JvPrimClass (float);
887 break;
888 case 'D':
889 result = JvPrimClass (double);
890 break;
891 case 'V':
892 result = JvPrimClass (void);
893 break;
894 case 'L':
896 char *save = ++sig;
897 while (*sig && *sig != ';')
898 ++sig;
899 // Do nothing if signature appears to be malformed.
900 if (*sig == ';')
902 _Jv_Utf8Const *name = _Jv_makeUtf8Const (save, sig - save);
903 result = _Jv_FindClass (name, loader);
905 break;
907 default:
908 // Do nothing -- bad signature.
909 break;
912 if (endp)
914 // Not really the "end", but the last valid character that we
915 // looked at.
916 *endp = sig;
919 if (! result)
920 return NULL;
922 // Find arrays.
923 while (array_count-- > 0)
924 result = _Jv_GetArrayClass (result, loader);
925 return result;
929 jclass
930 _Jv_FindClassFromSignatureNoException (char *sig, java::lang::ClassLoader *loader,
931 char **endp)
933 jclass klass;
937 klass = _Jv_FindClassFromSignature(sig, loader, endp);
939 catch (java::lang::NoClassDefFoundError *ncdfe)
941 return NULL;
943 catch (java::lang::ClassNotFoundException *cnfe)
945 return NULL;
948 return klass;
951 JArray<jstring> *
952 JvConvertArgv (int argc, const char **argv)
954 if (argc < 0)
955 argc = 0;
956 jobjectArray ar = JvNewObjectArray(argc, &java::lang::String::class$, NULL);
957 jobject *ptr = elements(ar);
958 jbyteArray bytes = NULL;
959 for (int i = 0; i < argc; i++)
961 const char *arg = argv[i];
962 int len = strlen (arg);
963 if (bytes == NULL || bytes->length < len)
964 bytes = JvNewByteArray (len);
965 jbyte *bytePtr = elements (bytes);
966 // We assume jbyte == char.
967 memcpy (bytePtr, arg, len);
969 // Now convert using the default encoding.
970 *ptr++ = new java::lang::String (bytes, 0, len);
972 return (JArray<jstring>*) ar;
975 // FIXME: These variables are static so that they will be
976 // automatically scanned by the Boehm collector. This is needed
977 // because with qthreads the collector won't scan the initial stack --
978 // it will only scan the qthreads stacks.
980 // Command line arguments.
981 static JArray<jstring> *arg_vec;
983 // The primary thread.
984 static java::lang::Thread *main_thread;
986 #ifndef DISABLE_GETENV_PROPERTIES
988 static char *
989 next_property_key (char *s, size_t *length)
991 size_t l = 0;
993 JvAssert (s);
995 // Skip over whitespace
996 while (isspace (*s))
997 s++;
999 // If we've reached the end, return NULL. Also return NULL if for
1000 // some reason we've come across a malformed property string.
1001 if (*s == 0
1002 || *s == ':'
1003 || *s == '=')
1004 return NULL;
1006 // Determine the length of the property key.
1007 while (s[l] != 0
1008 && ! isspace (s[l])
1009 && s[l] != ':'
1010 && s[l] != '=')
1012 if (s[l] == '\\'
1013 && s[l+1] != 0)
1014 l++;
1015 l++;
1018 *length = l;
1020 return s;
1023 static char *
1024 next_property_value (char *s, size_t *length)
1026 size_t l = 0;
1028 JvAssert (s);
1030 while (isspace (*s))
1031 s++;
1033 if (*s == ':'
1034 || *s == '=')
1035 s++;
1037 while (isspace (*s))
1038 s++;
1040 // Determine the length of the property value.
1041 while (s[l] != 0
1042 && ! isspace (s[l])
1043 && s[l] != ':'
1044 && s[l] != '=')
1046 if (s[l] == '\\'
1047 && s[l+1] != 0)
1048 l += 2;
1049 else
1050 l++;
1053 *length = l;
1055 return s;
1058 static void
1059 process_gcj_properties ()
1061 char *props = getenv("GCJ_PROPERTIES");
1063 if (NULL == props)
1064 return;
1066 // Later on we will write \0s into this string. It is simplest to
1067 // just duplicate it here.
1068 props = strdup (props);
1070 char *p = props;
1071 size_t length;
1072 size_t property_count = 0;
1074 // Whip through props quickly in order to count the number of
1075 // property values.
1076 while (p && (p = next_property_key (p, &length)))
1078 // Skip to the end of the key
1079 p += length;
1081 p = next_property_value (p, &length);
1082 if (p)
1083 p += length;
1085 property_count++;
1088 // Allocate an array of property value/key pairs.
1089 _Jv_Environment_Properties =
1090 (property_pair *) malloc (sizeof(property_pair)
1091 * (property_count + 1));
1093 // Go through the properties again, initializing _Jv_Properties
1094 // along the way.
1095 p = props;
1096 property_count = 0;
1097 while (p && (p = next_property_key (p, &length)))
1099 _Jv_Environment_Properties[property_count].key = p;
1100 _Jv_Environment_Properties[property_count].key_length = length;
1102 // Skip to the end of the key
1103 p += length;
1105 p = next_property_value (p, &length);
1107 _Jv_Environment_Properties[property_count].value = p;
1108 _Jv_Environment_Properties[property_count].value_length = length;
1110 if (p)
1111 p += length;
1113 property_count++;
1115 memset ((void *) &_Jv_Environment_Properties[property_count],
1116 0, sizeof (property_pair));
1118 // Null terminate the strings.
1119 for (property_pair *prop = &_Jv_Environment_Properties[0];
1120 prop->key != NULL;
1121 prop++)
1123 prop->key[prop->key_length] = 0;
1124 prop->value[prop->value_length] = 0;
1127 #endif // DISABLE_GETENV_PROPERTIES
1129 namespace gcj
1131 _Jv_Utf8Const *void_signature;
1132 _Jv_Utf8Const *clinit_name;
1133 _Jv_Utf8Const *init_name;
1134 _Jv_Utf8Const *finit_name;
1136 bool runtimeInitialized = false;
1138 // When true, print debugging information about class loading.
1139 bool verbose_class_flag;
1141 // When true, enable the bytecode verifier and BC-ABI type verification.
1142 bool verifyClasses = true;
1144 // Thread stack size specified by the -Xss runtime argument.
1145 size_t stack_size = 0;
1147 // Start time of the VM
1148 jlong startTime = 0;
1150 // Arguments passed to the VM
1151 JArray<jstring>* vmArgs;
1153 // Currently loaded classes
1154 jint loadedClasses = 0;
1156 // Unloaded classes
1157 jlong unloadedClasses = 0;
1160 // We accept all non-standard options accepted by Sun's java command,
1161 // for compatibility with existing application launch scripts.
1162 static jint
1163 parse_x_arg (char* option_string)
1165 if (strlen (option_string) <= 0)
1166 return -1;
1168 if (! strcmp (option_string, "int"))
1170 // FIXME: this should cause the vm to never load shared objects
1172 else if (! strcmp (option_string, "mixed"))
1174 // FIXME: allow interpreted and native code
1176 else if (! strcmp (option_string, "batch"))
1178 // FIXME: disable background JIT'ing
1180 else if (! strcmp (option_string, "debug"))
1182 remoteDebug = true;
1184 #ifdef INTERPRETER
1185 else if (! strncmp (option_string, "runjdwp:", 8))
1187 if (strlen (option_string) > 8)
1188 jdwpOptions = &option_string[8];
1189 else
1191 fprintf (stderr,
1192 "libgcj: argument required for JDWP options");
1193 return -1;
1196 #endif // INTERPRETER
1197 else if (! strncmp (option_string, "bootclasspath:", 14))
1199 // FIXME: add a parse_bootclasspath_arg function
1201 else if (! strncmp (option_string, "bootclasspath/a:", 16))
1204 else if (! strncmp (option_string, "bootclasspath/p:", 16))
1207 else if (! strcmp (option_string, "check:jni"))
1209 // FIXME: enable strict JNI checking
1211 else if (! strcmp (option_string, "future"))
1213 // FIXME: enable strict class file format checks
1215 else if (! strcmp (option_string, "noclassgc"))
1217 // FIXME: disable garbage collection for classes
1219 else if (! strcmp (option_string, "incgc"))
1221 // FIXME: incremental garbage collection
1223 else if (! strncmp (option_string, "loggc:", 6))
1225 if (option_string[6] == '\0')
1227 fprintf (stderr,
1228 "libgcj: filename argument expected for loggc option\n");
1229 return -1;
1231 // FIXME: set gc logging filename
1233 else if (! strncmp (option_string, "ms", 2))
1235 // FIXME: ignore this option until PR 20699 is fixed.
1236 // _Jv_SetInitialHeapSize (option_string + 2);
1238 else if (! strncmp (option_string, "mx", 2))
1239 _Jv_SetMaximumHeapSize (option_string + 2);
1240 else if (! strcmp (option_string, "prof"))
1242 // FIXME: enable profiling of program running in vm
1244 else if (! strncmp (option_string, "runhprof:", 9))
1246 // FIXME: enable specific type of vm profiling. add a
1247 // parse_runhprof_arg function
1249 else if (! strcmp (option_string, "rs"))
1251 // FIXME: reduced system signal usage. disable thread dumps,
1252 // only terminate in response to user-initiated calls,
1253 // e.g. System.exit()
1255 else if (! strncmp (option_string, "ss", 2))
1257 _Jv_SetStackSize (option_string + 2);
1259 else if (! strcmp (option_string, "X:+UseAltSigs"))
1261 // FIXME: use signals other than SIGUSR1 and SIGUSR2
1263 else if (! strcmp (option_string, "share:off"))
1265 // FIXME: don't share class data
1267 else if (! strcmp (option_string, "share:auto"))
1269 // FIXME: share class data where possible
1271 else if (! strcmp (option_string, "share:on"))
1273 // FIXME: fail if impossible to share class data
1275 else
1277 // Unrecognized.
1278 return -1;
1280 return 0;
1283 static jint
1284 parse_verbose_args (char* option_string,
1285 bool ignore_unrecognized)
1287 size_t len = sizeof ("-verbose") - 1;
1289 if (strlen (option_string) < len)
1290 return -1;
1292 if (option_string[len] == ':'
1293 && option_string[len + 1] != '\0')
1295 char* verbose_args = option_string + len + 1;
1299 if (! strncmp (verbose_args,
1300 "gc", sizeof ("gc") - 1))
1302 if (verbose_args[sizeof ("gc") - 1] == '\0'
1303 || verbose_args[sizeof ("gc") - 1] == ',')
1305 // FIXME: we should add functions to boehm-gc that
1306 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1307 // GC_print_back_height.
1308 verbose_args += sizeof ("gc") - 1;
1310 else
1312 verbose_arg_err:
1313 fprintf (stderr, "libgcj: unknown verbose option: %s\n",
1314 option_string);
1315 return -1;
1318 else if (! strncmp (verbose_args,
1319 "class",
1320 sizeof ("class") - 1))
1322 if (verbose_args[sizeof ("class") - 1] == '\0'
1323 || verbose_args[sizeof ("class") - 1] == ',')
1325 gcj::verbose_class_flag = true;
1326 verbose_args += sizeof ("class") - 1;
1328 else
1329 goto verbose_arg_err;
1331 else if (! strncmp (verbose_args, "jni",
1332 sizeof ("jni") - 1))
1334 if (verbose_args[sizeof ("jni") - 1] == '\0'
1335 || verbose_args[sizeof ("jni") - 1] == ',')
1337 // FIXME: enable JNI messages.
1338 verbose_args += sizeof ("jni") - 1;
1340 else
1341 goto verbose_arg_err;
1343 else if (ignore_unrecognized
1344 && verbose_args[0] == 'X')
1346 // ignore unrecognized non-standard verbose option
1347 while (verbose_args[0] != '\0'
1348 && verbose_args[0] != ',')
1349 verbose_args++;
1351 else if (verbose_args[0] == ',')
1353 verbose_args++;
1355 else
1356 goto verbose_arg_err;
1358 if (verbose_args[0] == ',')
1359 verbose_args++;
1361 while (verbose_args[0] != '\0');
1363 else if (option_string[len] == 'g'
1364 && option_string[len + 1] == 'c'
1365 && option_string[len + 2] == '\0')
1367 // FIXME: we should add functions to boehm-gc that
1368 // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1369 // GC_print_back_height.
1370 return 0;
1372 else if (option_string[len] == '\0')
1374 gcj::verbose_class_flag = true;
1375 return 0;
1377 else
1379 // unrecognized option beginning with -verbose
1380 return -1;
1382 return 0;
1385 #ifdef INTERPRETER
1386 // This function loads the agent functions for JVMTI from the library indicated
1387 // by name. It returns a negative value on failure, the value of which
1388 // indicates where ltdl failed, it also prints an error message.
1389 static jint
1390 load_jvmti_agent (const char *name)
1392 #ifdef USE_LTDL
1393 if (lt_dlinit ())
1395 fprintf (stderr,
1396 "libgcj: Error in ltdl init while loading agent library.\n");
1397 return -1;
1400 lt_dlhandle lib = lt_dlopenext (name);
1401 if (!lib)
1403 fprintf (stderr,
1404 "libgcj: Error opening agent library.\n");
1405 return -2;
1408 if (lib)
1410 jvmti_agentonload
1411 = (jvmti_agent_onload_func *) lt_dlsym (lib, "Agent_OnLoad");
1413 if (!jvmti_agentonload)
1415 fprintf (stderr,
1416 "libgcj: Error finding agent function in library %s.\n",
1417 name);
1418 lt_dlclose (lib);
1419 lib = NULL;
1420 return -4;
1422 else
1424 jvmti_agentonunload
1425 = (jvmti_agent_onunload_func *) lt_dlsym (lib, "Agent_OnUnload");
1427 return 0;
1430 else
1432 fprintf (stderr, "libgcj: Library %s not found in library path.\n", name);
1433 return -3;
1436 #endif /* USE_LTDL */
1438 // If LTDL cannot be used, return an error code indicating this.
1439 return -99;
1441 #endif // INTERPRETER
1443 static jint
1444 parse_init_args (JvVMInitArgs* vm_args)
1446 // if _Jv_Compiler_Properties is non-NULL then it needs to be
1447 // re-allocated dynamically.
1448 if (_Jv_Compiler_Properties)
1450 const char** props = _Jv_Compiler_Properties;
1451 _Jv_Compiler_Properties = NULL;
1453 for (int i = 0; props[i]; i++)
1455 _Jv_Compiler_Properties = (const char**) _Jv_Realloc
1456 (_Jv_Compiler_Properties,
1457 (_Jv_Properties_Count + 1) * sizeof (const char*));
1458 _Jv_Compiler_Properties[_Jv_Properties_Count++] = props[i];
1462 if (vm_args == NULL)
1463 return 0;
1465 for (int i = 0; i < vm_args->nOptions; ++i)
1467 char* option_string = vm_args->options[i].optionString;
1469 if (! strcmp (option_string, "vfprintf")
1470 || ! strcmp (option_string, "exit")
1471 || ! strcmp (option_string, "abort"))
1473 // FIXME: we are required to recognize these, but for
1474 // now we don't handle them in any way.
1475 continue;
1477 else if (! strncmp (option_string,
1478 "-verbose", sizeof ("-verbose") - 1))
1480 jint result = parse_verbose_args (option_string,
1481 vm_args->ignoreUnrecognized);
1482 if (result < 0)
1483 return result;
1485 else if (! strncmp (option_string, "-D", 2))
1487 _Jv_Compiler_Properties = (const char**) _Jv_Realloc
1488 (_Jv_Compiler_Properties,
1489 (_Jv_Properties_Count + 1) * sizeof (char*));
1491 _Jv_Compiler_Properties[_Jv_Properties_Count++] =
1492 strdup (option_string + 2);
1494 continue;
1496 #ifdef INTERPRETER
1497 else if (! strncmp (option_string, "-agentlib", sizeof ("-agentlib") - 1))
1499 char *strPtr;
1501 if (strlen(option_string) > (sizeof ("-agentlib:") - 1))
1502 strPtr = &option_string[sizeof ("-agentlib:") - 1];
1503 else
1505 fprintf (stderr,
1506 "libgcj: Malformed agentlib argument %s: expected lib name\n",
1507 option_string);
1508 return -1;
1511 // These are optional arguments to pass to the agent library.
1512 jvmti_agent_opts = strchr (strPtr, '=');
1514 if (! strncmp (strPtr, "jdwp", 4))
1516 // We want to run JDWP here so set the correct variables.
1517 remoteDebug = true;
1518 jdwpOptions = ++jvmti_agent_opts;
1520 else
1522 jint nameLength;
1524 if (jvmti_agent_opts == NULL)
1525 nameLength = strlen (strPtr);
1526 else
1528 nameLength = jvmti_agent_opts - strPtr;
1529 jvmti_agent_opts++;
1532 char lib_name[nameLength + 3 + 1];
1533 strcpy (lib_name, "lib");
1534 strncat (lib_name, strPtr, nameLength);
1536 jint result = load_jvmti_agent (lib_name);
1538 if (result < 0)
1540 return -1;
1543 // Mark JVMTI active
1544 JVMTI::enabled = true;
1547 continue;
1549 else if (! strncmp (option_string, "-agentpath:",
1550 sizeof ("-agentpath:") - 1))
1552 char *strPtr;
1554 if (strlen(option_string) > 10)
1555 strPtr = &option_string[10];
1556 else
1558 fprintf (stderr,
1559 "libgcj: Malformed agentlib argument %s: expected lib path\n",
1560 option_string);
1561 return -1;
1564 // These are optional arguments to pass to the agent library.
1565 jvmti_agent_opts = strchr (strPtr, '=');
1567 jint nameLength;
1569 if (jvmti_agent_opts == NULL)
1570 nameLength = strlen (strPtr);
1571 else
1573 nameLength = jvmti_agent_opts - strPtr;
1574 jvmti_agent_opts++;
1577 char lib_name[nameLength + 3 + 1];
1578 strcpy (lib_name, "lib");
1579 strncat (lib_name, strPtr, nameLength);
1580 jint result = load_jvmti_agent (strPtr);
1582 if (result < 0)
1584 return -1;
1587 // Mark JVMTI active
1588 JVMTI::enabled = true;
1589 continue;
1591 #endif // INTERPRETER
1592 else
1594 int r = -1;
1595 if (option_string[0] == '_')
1596 r = parse_x_arg (option_string + 1);
1597 else if (! strncmp (option_string, "-X", 2))
1598 r = parse_x_arg (option_string + 2);
1600 if (r == -1 && ! vm_args->ignoreUnrecognized)
1602 fprintf (stderr, "libgcj: unknown option: %s\n", option_string);
1603 return -1;
1607 return 0;
1610 jint
1611 _Jv_CreateJavaVM (JvVMInitArgs* vm_args)
1613 using namespace gcj;
1615 if (runtimeInitialized)
1616 return -1;
1618 runtimeInitialized = true;
1619 startTime = _Jv_platform_gettimeofday();
1621 jint result = parse_init_args (vm_args);
1622 if (result < 0)
1623 return -1;
1625 PROCESS_GCJ_PROPERTIES;
1627 /* Threads must be initialized before the GC, so that it inherits the
1628 signal mask. */
1629 _Jv_InitThreads ();
1630 _Jv_InitGC ();
1631 _Jv_InitializeSyncMutex ();
1633 #ifdef INTERPRETER
1634 _Jv_InitInterpreter ();
1635 #endif
1637 #ifdef HANDLE_SEGV
1638 INIT_SEGV;
1639 #endif
1641 #ifdef HANDLE_FPE
1642 INIT_FPE;
1643 #endif
1645 /* Initialize Utf8 constants declared in jvm.h. */
1646 void_signature = _Jv_makeUtf8Const ("()V", 3);
1647 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
1648 init_name = _Jv_makeUtf8Const ("<init>", 6);
1649 finit_name = _Jv_makeUtf8Const ("finit$", 6);
1651 /* Initialize built-in classes to represent primitive TYPEs. */
1652 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1);
1653 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2);
1654 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4);
1655 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8);
1656 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1);
1657 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2);
1658 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4);
1659 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8);
1660 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0);
1662 // We have to initialize this fairly early, to avoid circular class
1663 // initialization. In particular we want to start the
1664 // initialization of ClassLoader before we start the initialization
1665 // of VMClassLoader.
1666 _Jv_InitClass (&java::lang::ClassLoader::class$);
1668 // Set up the system class loader and the bootstrap class loader.
1669 gnu::gcj::runtime::ExtensionClassLoader::initialize();
1670 java::lang::VMClassLoader::initialize(JvNewStringLatin1(TOOLEXECLIBDIR));
1672 _Jv_RegisterBootstrapPackages();
1674 no_memory = new java::lang::OutOfMemoryError;
1676 #ifdef USE_LTDL
1677 LTDL_SET_PRELOADED_SYMBOLS ();
1678 #endif
1680 _Jv_platform_initialize ();
1682 _Jv_JNI_Init ();
1684 #ifdef INTERPRETER
1685 _Jv_JVMTI_Init ();
1686 #endif
1688 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
1690 // Start the GC finalizer thread. A VirtualMachineError can be
1691 // thrown by the runtime if, say, threads aren't available.
1694 using namespace gnu::gcj::runtime;
1695 FinalizerThread *ft = new FinalizerThread ();
1696 ft->start ();
1698 catch (java::lang::VirtualMachineError *ignore)
1702 runtimeInitialized = true;
1704 return 0;
1707 void
1708 _Jv_RunMain (JvVMInitArgs *vm_args, jclass klass, const char *name, int argc,
1709 const char **argv, bool is_jar)
1711 #ifndef DISABLE_MAIN_ARGS
1712 _Jv_SetArgs (argc, argv);
1713 #endif
1715 java::lang::Runtime *runtime = NULL;
1719 if (_Jv_CreateJavaVM (vm_args) < 0)
1721 fprintf (stderr, "libgcj: couldn't create virtual machine\n");
1722 exit (1);
1725 if (vm_args == NULL)
1726 gcj::vmArgs = JvConvertArgv(0, NULL);
1727 else
1729 const char* vmArgs[vm_args->nOptions];
1730 const char** vmPtr = vmArgs;
1731 struct _Jv_VMOption* optionPtr = vm_args->options;
1732 for (int i = 0; i < vm_args->nOptions; ++i)
1733 *vmPtr++ = (*optionPtr++).optionString;
1734 gcj::vmArgs = JvConvertArgv(vm_args->nOptions, vmArgs);
1737 // Get the Runtime here. We want to initialize it before searching
1738 // for `main'; that way it will be set up if `main' is a JNI method.
1739 runtime = java::lang::Runtime::getRuntime ();
1741 #ifdef DISABLE_MAIN_ARGS
1742 arg_vec = JvConvertArgv (0, 0);
1743 #else
1744 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1745 #endif
1747 using namespace gnu::java::lang;
1748 if (klass)
1749 main_thread = new MainThread (klass, arg_vec);
1750 else
1751 main_thread = new MainThread (JvNewStringUTF (name),
1752 arg_vec, is_jar);
1753 _Jv_AttachCurrentThread (main_thread);
1755 #ifdef INTERPRETER
1756 // Start JVMTI if an agent function has been found.
1757 if (jvmti_agentonload)
1758 (*jvmti_agentonload) (_Jv_GetJavaVM (), jvmti_agent_opts, NULL);
1760 // Start JDWP
1761 if (remoteDebug)
1763 using namespace gnu::classpath::jdwp;
1764 VMVirtualMachine::initialize ();
1765 Jdwp *jdwp = new Jdwp ();
1766 jdwp->setDaemon (true);
1767 jdwp->configure (JvNewStringLatin1 (jdwpOptions));
1768 jdwp->start ();
1770 // Wait for JDWP to initialize and start
1771 jdwp->join ();
1773 // Send VMInit
1774 if (JVMTI_REQUESTED_EVENT (VMInit))
1775 _Jv_JVMTI_PostEvent (JVMTI_EVENT_VM_INIT, main_thread);
1776 #endif // INTERPRETER
1778 catch (java::lang::Throwable *t)
1780 java::lang::System::err->println (JvNewStringLatin1
1781 ("Exception during runtime initialization"));
1782 t->printStackTrace();
1783 if (runtime)
1784 java::lang::Runtime::exitNoChecksAccessor (1);
1785 // In case the runtime creation failed.
1786 ::exit (1);
1789 _Jv_ThreadRun (main_thread);
1791 #ifdef INTERPRETER
1792 // Send VMDeath
1793 if (JVMTI_REQUESTED_EVENT (VMDeath))
1795 java::lang::Thread *thread = java::lang::Thread::currentThread ();
1796 JNIEnv *jni_env = _Jv_GetCurrentJNIEnv ();
1797 _Jv_JVMTI_PostEvent (JVMTI_EVENT_VM_DEATH, thread, jni_env);
1800 // Run JVMTI AgentOnUnload if it exists and an agent is loaded.
1801 if (jvmti_agentonunload)
1802 (*jvmti_agentonunload) (_Jv_GetJavaVM ());
1803 #endif // INTERPRETER
1805 // If we got here then something went wrong, as MainThread is not
1806 // supposed to terminate.
1807 ::exit (1);
1810 void
1811 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
1812 bool is_jar)
1814 _Jv_RunMain (NULL, klass, name, argc, argv, is_jar);
1817 void
1818 JvRunMain (jclass klass, int argc, const char **argv)
1820 _Jv_RunMain (klass, NULL, argc, argv, false);
1823 void
1824 JvRunMainName (const char *name, int argc, const char **argv)
1826 _Jv_RunMain (NULL, name, argc, argv, false);
1831 // Parse a string and return a heap size.
1832 static size_t
1833 parse_memory_size (const char *spec)
1835 char *end;
1836 unsigned long val = strtoul (spec, &end, 10);
1837 if (*end == 'k' || *end == 'K')
1838 val *= 1024;
1839 else if (*end == 'm' || *end == 'M')
1840 val *= 1048576;
1841 return (size_t) val;
1844 // Set the initial heap size. This might be ignored by the GC layer.
1845 // This must be called before _Jv_RunMain.
1846 void
1847 _Jv_SetInitialHeapSize (const char *arg)
1849 size_t size = parse_memory_size (arg);
1850 _Jv_GCSetInitialHeapSize (size);
1853 // Set the maximum heap size. This might be ignored by the GC layer.
1854 // This must be called before _Jv_RunMain.
1855 void
1856 _Jv_SetMaximumHeapSize (const char *arg)
1858 size_t size = parse_memory_size (arg);
1859 _Jv_GCSetMaximumHeapSize (size);
1862 void
1863 _Jv_SetStackSize (const char *arg)
1865 size_t size = parse_memory_size (arg);
1866 gcj::stack_size = size;
1869 void *
1870 _Jv_Malloc (jsize size)
1872 if (__builtin_expect (size == 0, false))
1873 size = 1;
1874 void *ptr = malloc ((size_t) size);
1875 if (__builtin_expect (ptr == NULL, false))
1876 throw no_memory;
1877 return ptr;
1880 void *
1881 _Jv_Realloc (void *ptr, jsize size)
1883 if (__builtin_expect (size == 0, false))
1884 size = 1;
1885 ptr = realloc (ptr, (size_t) size);
1886 if (__builtin_expect (ptr == NULL, false))
1887 throw no_memory;
1888 return ptr;
1891 void *
1892 _Jv_MallocUnchecked (jsize size)
1894 if (__builtin_expect (size == 0, false))
1895 size = 1;
1896 return malloc ((size_t) size);
1899 void
1900 _Jv_Free (void* ptr)
1902 return free (ptr);
1907 // In theory, these routines can be #ifdef'd away on machines which
1908 // support divide overflow signals. However, we never know if some
1909 // code might have been compiled with "-fuse-divide-subroutine", so we
1910 // always include them in libgcj.
1912 jint
1913 _Jv_divI (jint dividend, jint divisor)
1915 if (__builtin_expect (divisor == 0, false))
1917 java::lang::ArithmeticException *arithexception
1918 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1919 throw arithexception;
1922 if (dividend == (jint) 0x80000000L && divisor == -1)
1923 return dividend;
1925 return dividend / divisor;
1928 jint
1929 _Jv_remI (jint dividend, jint divisor)
1931 if (__builtin_expect (divisor == 0, false))
1933 java::lang::ArithmeticException *arithexception
1934 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1935 throw arithexception;
1938 if (dividend == (jint) 0x80000000L && divisor == -1)
1939 return 0;
1941 return dividend % divisor;
1944 jlong
1945 _Jv_divJ (jlong dividend, jlong divisor)
1947 if (__builtin_expect (divisor == 0, false))
1949 java::lang::ArithmeticException *arithexception
1950 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1951 throw arithexception;
1954 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1955 return dividend;
1957 return dividend / divisor;
1960 jlong
1961 _Jv_remJ (jlong dividend, jlong divisor)
1963 if (__builtin_expect (divisor == 0, false))
1965 java::lang::ArithmeticException *arithexception
1966 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1967 throw arithexception;
1970 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1971 return 0;
1973 return dividend % divisor;
1978 // Return true if SELF_KLASS can access a field or method in
1979 // OTHER_KLASS. The field or method's access flags are specified in
1980 // FLAGS.
1981 jboolean
1982 _Jv_CheckAccess (jclass self_klass, jclass other_klass, jint flags)
1984 using namespace java::lang::reflect;
1985 return ((self_klass == other_klass)
1986 || ((flags & Modifier::PUBLIC) != 0)
1987 || (((flags & Modifier::PROTECTED) != 0)
1988 && _Jv_IsAssignableFromSlow (self_klass, other_klass))
1989 || (((flags & Modifier::PRIVATE) == 0)
1990 && _Jv_ClassNameSamePackage (self_klass->name,
1991 other_klass->name)));
1994 // Prepend GCJ_VERSIONED_LIBDIR to a module search path stored in a C
1995 // char array, if the path is not already prefixed by
1996 // GCJ_VERSIONED_LIBDIR. Return a newly JvMalloc'd char buffer. The
1997 // result should be freed using JvFree.
1998 char*
1999 _Jv_PrependVersionedLibdir (char* libpath)
2001 char* retval = 0;
2003 if (libpath && libpath[0] != '\0')
2005 if (! strncmp (libpath,
2006 GCJ_VERSIONED_LIBDIR,
2007 sizeof (GCJ_VERSIONED_LIBDIR) - 1))
2009 // LD_LIBRARY_PATH is already prefixed with
2010 // GCJ_VERSIONED_LIBDIR.
2011 retval = (char*) _Jv_Malloc (strlen (libpath) + 1);
2012 strcpy (retval, libpath);
2014 else
2016 // LD_LIBRARY_PATH is not prefixed with
2017 // GCJ_VERSIONED_LIBDIR.
2018 char path_sep[2];
2019 path_sep[0] = (char) _Jv_platform_path_separator;
2020 path_sep[1] = '\0';
2021 jsize total = ((sizeof (GCJ_VERSIONED_LIBDIR) - 1)
2022 + 1 /* path separator */ + strlen (libpath) + 1);
2023 retval = (char*) _Jv_Malloc (total);
2024 strcpy (retval, GCJ_VERSIONED_LIBDIR);
2025 strcat (retval, path_sep);
2026 strcat (retval, libpath);
2029 else
2031 // LD_LIBRARY_PATH was not specified or is empty.
2032 retval = (char*) _Jv_Malloc (sizeof (GCJ_VERSIONED_LIBDIR));
2033 strcpy (retval, GCJ_VERSIONED_LIBDIR);
2036 return retval;