fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / libjava / prims.cc
blobe4818c6ba34d7684672f5281282f9911765701b0
1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
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 #ifndef DISABLE_GETENV_PROPERTIES
36 #include <ctype.h>
37 #include <java-props.h>
38 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
39 #else
40 #define PROCESS_GCJ_PROPERTIES
41 #endif // DISABLE_GETENV_PROPERTIES
43 #include <java/lang/Class.h>
44 #include <java/lang/ClassLoader.h>
45 #include <java/lang/Runtime.h>
46 #include <java/lang/String.h>
47 #include <java/lang/Thread.h>
48 #include <java/lang/ThreadGroup.h>
49 #include <java/lang/ArrayIndexOutOfBoundsException.h>
50 #include <java/lang/ArithmeticException.h>
51 #include <java/lang/ClassFormatError.h>
52 #include <java/lang/InternalError.h>
53 #include <java/lang/NegativeArraySizeException.h>
54 #include <java/lang/NullPointerException.h>
55 #include <java/lang/OutOfMemoryError.h>
56 #include <java/lang/System.h>
57 #include <java/lang/VMThrowable.h>
58 #include <java/lang/reflect/Modifier.h>
59 #include <java/io/PrintStream.h>
60 #include <java/lang/UnsatisfiedLinkError.h>
61 #include <java/lang/VirtualMachineError.h>
62 #include <gnu/gcj/runtime/VMClassLoader.h>
63 #include <gnu/gcj/runtime/FinalizerThread.h>
64 #include <gnu/gcj/runtime/FirstThread.h>
66 #ifdef USE_LTDL
67 #include <ltdl.h>
68 #endif
70 // We allocate a single OutOfMemoryError exception which we keep
71 // around for use if we run out of memory.
72 static java::lang::OutOfMemoryError *no_memory;
74 // Number of bytes in largest array object we create. This could be
75 // increased to the largest size_t value, so long as the appropriate
76 // functions are changed to take a size_t argument instead of jint.
77 #define MAX_OBJECT_SIZE ((1<<31) - 1)
79 static const char *no_properties[] = { NULL };
81 // Properties set at compile time.
82 const char **_Jv_Compiler_Properties = no_properties;
84 // The JAR file to add to the beginning of java.class.path.
85 const char *_Jv_Jar_Class_Path;
87 #ifndef DISABLE_GETENV_PROPERTIES
88 // Property key/value pairs.
89 property_pair *_Jv_Environment_Properties;
90 #endif
92 // Stash the argv pointer to benefit native libraries that need it.
93 const char **_Jv_argv;
94 int _Jv_argc;
96 // Argument support.
97 int
98 _Jv_GetNbArgs (void)
100 // _Jv_argc is 0 if not explicitly initialized.
101 return _Jv_argc;
104 const char *
105 _Jv_GetSafeArg (int index)
107 if (index >=0 && index < _Jv_GetNbArgs ())
108 return _Jv_argv[index];
109 else
110 return "";
113 void
114 _Jv_SetArgs (int argc, const char **argv)
116 _Jv_argc = argc;
117 _Jv_argv = argv;
120 #ifdef ENABLE_JVMPI
121 // Pointer to JVMPI notification functions.
122 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
123 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
124 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
125 #endif
128 /* Unblock a signal. Unless we do this, the signal may only be sent
129 once. */
130 static void
131 unblock_signal (int signum)
133 #ifdef _POSIX_VERSION
134 sigset_t sigs;
136 sigemptyset (&sigs);
137 sigaddset (&sigs, signum);
138 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
139 #endif
142 #ifdef HANDLE_SEGV
143 SIGNAL_HANDLER (catch_segv)
145 java::lang::NullPointerException *nullp
146 = new java::lang::NullPointerException;
147 unblock_signal (SIGSEGV);
148 MAKE_THROW_FRAME (nullp);
149 throw nullp;
151 #endif
153 #ifdef HANDLE_FPE
154 SIGNAL_HANDLER (catch_fpe)
156 java::lang::ArithmeticException *arithexception
157 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
158 unblock_signal (SIGFPE);
159 #ifdef HANDLE_DIVIDE_OVERFLOW
160 HANDLE_DIVIDE_OVERFLOW;
161 #else
162 MAKE_THROW_FRAME (arithexception);
163 #endif
164 throw arithexception;
166 #endif
170 jboolean
171 _Jv_equalUtf8Consts (const Utf8Const* a, const Utf8Const *b)
173 int len;
174 const _Jv_ushort *aptr, *bptr;
175 if (a == b)
176 return true;
177 if (a->hash != b->hash)
178 return false;
179 len = a->length;
180 if (b->length != len)
181 return false;
182 aptr = (const _Jv_ushort *)a->data;
183 bptr = (const _Jv_ushort *)b->data;
184 len = (len + 1) >> 1;
185 while (--len >= 0)
186 if (*aptr++ != *bptr++)
187 return false;
188 return true;
191 /* True iff A is equal to STR.
192 HASH is STR->hashCode().
195 jboolean
196 _Jv_equal (Utf8Const* a, jstring str, jint hash)
198 if (a->hash != (_Jv_ushort) hash)
199 return false;
200 jint len = str->length();
201 jint i = 0;
202 jchar *sptr = _Jv_GetStringChars (str);
203 unsigned char* ptr = (unsigned char*) a->data;
204 unsigned char* limit = ptr + a->length;
205 for (;; i++, sptr++)
207 int ch = UTF8_GET (ptr, limit);
208 if (i == len)
209 return ch < 0;
210 if (ch != *sptr)
211 return false;
213 return true;
216 /* Like _Jv_equal, but stop after N characters. */
217 jboolean
218 _Jv_equaln (Utf8Const *a, jstring str, jint n)
220 jint len = str->length();
221 jint i = 0;
222 jchar *sptr = _Jv_GetStringChars (str);
223 unsigned char* ptr = (unsigned char*) a->data;
224 unsigned char* limit = ptr + a->length;
225 for (; n-- > 0; i++, sptr++)
227 int ch = UTF8_GET (ptr, limit);
228 if (i == len)
229 return ch < 0;
230 if (ch != *sptr)
231 return false;
233 return true;
236 /* Count the number of Unicode chars encoded in a given Ut8 string. */
238 _Jv_strLengthUtf8(char* str, int len)
240 unsigned char* ptr;
241 unsigned char* limit;
242 int str_length;
244 ptr = (unsigned char*) str;
245 limit = ptr + len;
246 str_length = 0;
247 for (; ptr < limit; str_length++)
249 if (UTF8_GET (ptr, limit) < 0)
250 return (-1);
252 return (str_length);
255 /* Calculate a hash value for a string encoded in Utf8 format.
256 * This returns the same hash value as specified or java.lang.String.hashCode.
258 static jint
259 hashUtf8String (char* str, int len)
261 unsigned char* ptr = (unsigned char*) str;
262 unsigned char* limit = ptr + len;
263 jint hash = 0;
265 for (; ptr < limit;)
267 int ch = UTF8_GET (ptr, limit);
268 /* Updated specification from
269 http://www.javasoft.com/docs/books/jls/clarify.html. */
270 hash = (31 * hash) + ch;
272 return hash;
275 _Jv_Utf8Const *
276 _Jv_makeUtf8Const (char* s, int len)
278 if (len < 0)
279 len = strlen (s);
280 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
281 memcpy (m->data, s, len);
282 m->data[len] = 0;
283 m->length = len;
284 m->hash = hashUtf8String (s, len) & 0xFFFF;
285 return (m);
288 _Jv_Utf8Const *
289 _Jv_makeUtf8Const (jstring string)
291 jint hash = string->hashCode ();
292 jint len = _Jv_GetStringUTFLength (string);
294 Utf8Const* m = (Utf8Const*)
295 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
297 m->hash = hash;
298 m->length = len;
300 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
301 m->data[len] = 0;
303 return m;
308 #ifdef DEBUG
309 void
310 _Jv_Abort (const char *function, const char *file, int line,
311 const char *message)
312 #else
313 void
314 _Jv_Abort (const char *, const char *, int, const char *message)
315 #endif
317 #ifdef DEBUG
318 fprintf (stderr,
319 "libgcj failure: %s\n in function %s, file %s, line %d\n",
320 message, function, file, line);
321 #else
322 fprintf (stderr, "libgcj failure: %s\n", message);
323 #endif
324 abort ();
327 static void
328 fail_on_finalization (jobject)
330 JvFail ("object was finalized");
333 void
334 _Jv_GCWatch (jobject obj)
336 _Jv_RegisterFinalizer (obj, fail_on_finalization);
339 void
340 _Jv_ThrowBadArrayIndex(jint bad_index)
342 throw new java::lang::ArrayIndexOutOfBoundsException
343 (java::lang::String::valueOf (bad_index));
346 void
347 _Jv_ThrowNullPointerException ()
349 throw new java::lang::NullPointerException;
352 // Explicitly throw a no memory exception.
353 // The collector calls this when it encounters an out-of-memory condition.
354 void _Jv_ThrowNoMemory()
356 throw no_memory;
359 #ifdef ENABLE_JVMPI
360 static void
361 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
363 // Service JVMPI allocation request.
364 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
366 JVMPI_Event event;
368 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
369 event.env_id = NULL;
370 event.u.obj_alloc.arena_id = 0;
371 event.u.obj_alloc.class_id = (jobjectID) klass;
372 event.u.obj_alloc.is_array = 0;
373 event.u.obj_alloc.size = size;
374 event.u.obj_alloc.obj_id = (jobjectID) obj;
376 // FIXME: This doesn't look right for the Boehm GC. A GC may
377 // already be in progress. _Jv_DisableGC () doesn't wait for it.
378 // More importantly, I don't see the need for disabling GC, since we
379 // blatantly have a pointer to obj on our stack, ensuring that the
380 // object can't be collected. Even for a nonconservative collector,
381 // it appears to me that this must be true, since we are about to
382 // return obj. Isn't this whole approach way too intrusive for
383 // a useful profiling interface? - HB
384 _Jv_DisableGC ();
385 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
386 _Jv_EnableGC ();
389 #else /* !ENABLE_JVMPI */
390 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
391 #endif
393 // Allocate a new object of class KLASS. SIZE is the size of the object
394 // to allocate. You might think this is redundant, but it isn't; some
395 // classes, such as String, aren't of fixed size.
396 // First a version that assumes that we have no finalizer, and that
397 // the class is already initialized.
398 // If we know that JVMPI is disabled, this can be replaced by a direct call
399 // to the allocator for the appropriate GC.
400 jobject
401 _Jv_AllocObjectNoInitNoFinalizer (jclass klass, jint size)
403 jobject obj = (jobject) _Jv_AllocObj (size, klass);
404 jvmpi_notify_alloc (klass, size, obj);
405 return obj;
408 // And now a version that initializes if necessary.
409 jobject
410 _Jv_AllocObjectNoFinalizer (jclass klass, jint size)
412 _Jv_InitClass (klass);
413 jobject obj = (jobject) _Jv_AllocObj (size, klass);
414 jvmpi_notify_alloc (klass, size, obj);
415 return obj;
418 // And now the general version that registers a finalizer if necessary.
419 jobject
420 _Jv_AllocObject (jclass klass, jint size)
422 jobject obj = _Jv_AllocObjectNoFinalizer (klass, size);
424 // We assume that the compiler only generates calls to this routine
425 // if there really is an interesting finalizer.
426 // Unfortunately, we still have to the dynamic test, since there may
427 // be cni calls to this routine.
428 // Note that on IA64 get_finalizer() returns the starting address of the
429 // function, not a function pointer. Thus this still works.
430 if (klass->vtable->get_finalizer ()
431 != java::lang::Object::class$.vtable->get_finalizer ())
432 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
433 return obj;
436 // A version of the above that assumes the object contains no pointers,
437 // and requires no finalization. This can't happen if we need pointers
438 // to locks.
439 #ifdef JV_HASH_SYNCHRONIZATION
440 jobject
441 _Jv_AllocPtrFreeObject (jclass klass, jint size)
443 _Jv_InitClass (klass);
445 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
447 #ifdef ENABLE_JVMPI
448 // Service JVMPI request.
450 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
452 JVMPI_Event event;
454 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
455 event.env_id = NULL;
456 event.u.obj_alloc.arena_id = 0;
457 event.u.obj_alloc.class_id = (jobjectID) klass;
458 event.u.obj_alloc.is_array = 0;
459 event.u.obj_alloc.size = size;
460 event.u.obj_alloc.obj_id = (jobjectID) obj;
462 _Jv_DisableGC ();
463 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
464 _Jv_EnableGC ();
466 #endif
468 return obj;
470 #endif /* JV_HASH_SYNCHRONIZATION */
473 // Allocate a new array of Java objects. Each object is of type
474 // `elementClass'. `init' is used to initialize each slot in the
475 // array.
476 jobjectArray
477 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
479 if (__builtin_expect (count < 0, false))
480 throw new java::lang::NegativeArraySizeException;
482 JvAssert (! elementClass->isPrimitive ());
484 // Ensure that elements pointer is properly aligned.
485 jobjectArray obj = NULL;
486 size_t size = (size_t) elements (obj);
487 // Check for overflow.
488 if (__builtin_expect ((size_t) count >
489 (MAX_OBJECT_SIZE - 1 - size) / sizeof (jobject), false))
490 throw no_memory;
492 size += count * sizeof (jobject);
494 jclass klass = _Jv_GetArrayClass (elementClass,
495 elementClass->getClassLoaderInternal());
497 obj = (jobjectArray) _Jv_AllocArray (size, klass);
498 // Cast away const.
499 jsize *lp = const_cast<jsize *> (&obj->length);
500 *lp = count;
501 // We know the allocator returns zeroed memory. So don't bother
502 // zeroing it again.
503 if (init)
505 jobject *ptr = elements(obj);
506 while (--count >= 0)
507 *ptr++ = init;
509 return obj;
512 // Allocate a new array of primitives. ELTYPE is the type of the
513 // element, COUNT is the size of the array.
514 jobject
515 _Jv_NewPrimArray (jclass eltype, jint count)
517 int elsize = eltype->size();
518 if (__builtin_expect (count < 0, false))
519 throw new java::lang::NegativeArraySizeException;
521 JvAssert (eltype->isPrimitive ());
522 jobject dummy = NULL;
523 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
525 // Check for overflow.
526 if (__builtin_expect ((size_t) count >
527 (MAX_OBJECT_SIZE - size) / elsize, false))
528 throw no_memory;
530 jclass klass = _Jv_GetArrayClass (eltype, 0);
532 # ifdef JV_HASH_SYNCHRONIZATION
533 // Since the vtable is always statically allocated,
534 // these are completely pointerfree! Make sure the GC doesn't touch them.
535 __JArray *arr =
536 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
537 memset((char *)arr + size, 0, elsize * count);
538 # else
539 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
540 // Note that we assume we are given zeroed memory by the allocator.
541 # endif
542 // Cast away const.
543 jsize *lp = const_cast<jsize *> (&arr->length);
544 *lp = count;
546 return arr;
549 jobject
550 _Jv_NewArray (jint type, jint size)
552 switch (type)
554 case 4: return JvNewBooleanArray (size);
555 case 5: return JvNewCharArray (size);
556 case 6: return JvNewFloatArray (size);
557 case 7: return JvNewDoubleArray (size);
558 case 8: return JvNewByteArray (size);
559 case 9: return JvNewShortArray (size);
560 case 10: return JvNewIntArray (size);
561 case 11: return JvNewLongArray (size);
563 throw new java::lang::InternalError
564 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
567 // Allocate a possibly multi-dimensional array but don't check that
568 // any array length is <0.
569 static jobject
570 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
572 JvAssert (type->isArray());
573 jclass element_type = type->getComponentType();
574 jobject result;
575 if (element_type->isPrimitive())
576 result = _Jv_NewPrimArray (element_type, sizes[0]);
577 else
578 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
580 if (dimensions > 1)
582 JvAssert (! element_type->isPrimitive());
583 JvAssert (element_type->isArray());
584 jobject *contents = elements ((jobjectArray) result);
585 for (int i = 0; i < sizes[0]; ++i)
586 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
587 sizes + 1);
590 return result;
593 jobject
594 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
596 for (int i = 0; i < dimensions; ++i)
597 if (sizes[i] < 0)
598 throw new java::lang::NegativeArraySizeException;
600 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
603 jobject
604 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
606 va_list args;
607 jint sizes[dimensions];
608 va_start (args, dimensions);
609 for (int i = 0; i < dimensions; ++i)
611 jint size = va_arg (args, jint);
612 if (size < 0)
613 throw new java::lang::NegativeArraySizeException;
614 sizes[i] = size;
616 va_end (args);
618 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
623 // Ensure 8-byte alignment, for hash synchronization.
624 #define DECLARE_PRIM_TYPE(NAME) \
625 _Jv_ArrayVTable _Jv_##NAME##VTable; \
626 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
628 DECLARE_PRIM_TYPE(byte)
629 DECLARE_PRIM_TYPE(short)
630 DECLARE_PRIM_TYPE(int)
631 DECLARE_PRIM_TYPE(long)
632 DECLARE_PRIM_TYPE(boolean)
633 DECLARE_PRIM_TYPE(char)
634 DECLARE_PRIM_TYPE(float)
635 DECLARE_PRIM_TYPE(double)
636 DECLARE_PRIM_TYPE(void)
638 void
639 _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len,
640 _Jv_ArrayVTable *array_vtable)
642 using namespace java::lang::reflect;
644 // We must set the vtable for the class; the Java constructor
645 // doesn't do this.
646 (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
648 // Initialize the fields we care about. We do this in the same
649 // order they are declared in Class.h.
650 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
651 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
652 cl->method_count = sig;
653 cl->size_in_bytes = len;
654 cl->vtable = JV_PRIMITIVE_VTABLE;
655 cl->state = JV_STATE_DONE;
656 cl->depth = -1;
657 if (sig != 'V')
658 _Jv_NewArrayClass (cl, NULL, (_Jv_VTable *) array_vtable);
661 jclass
662 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
664 switch (*sig)
666 case 'B':
667 return JvPrimClass (byte);
668 case 'S':
669 return JvPrimClass (short);
670 case 'I':
671 return JvPrimClass (int);
672 case 'J':
673 return JvPrimClass (long);
674 case 'Z':
675 return JvPrimClass (boolean);
676 case 'C':
677 return JvPrimClass (char);
678 case 'F':
679 return JvPrimClass (float);
680 case 'D':
681 return JvPrimClass (double);
682 case 'V':
683 return JvPrimClass (void);
684 case 'L':
686 int i;
687 for (i = 1; sig[i] && sig[i] != ';'; ++i)
689 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
690 return _Jv_FindClass (name, loader);
692 case '[':
694 jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
695 if (! klass)
696 return NULL;
697 return _Jv_GetArrayClass (klass, loader);
701 return NULL; // Placate compiler.
706 JArray<jstring> *
707 JvConvertArgv (int argc, const char **argv)
709 if (argc < 0)
710 argc = 0;
711 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
712 jobject *ptr = elements(ar);
713 jbyteArray bytes = NULL;
714 for (int i = 0; i < argc; i++)
716 const char *arg = argv[i];
717 int len = strlen (arg);
718 if (bytes == NULL || bytes->length < len)
719 bytes = JvNewByteArray (len);
720 jbyte *bytePtr = elements (bytes);
721 // We assume jbyte == char.
722 memcpy (bytePtr, arg, len);
724 // Now convert using the default encoding.
725 *ptr++ = new java::lang::String (bytes, 0, len);
727 return (JArray<jstring>*) ar;
730 // FIXME: These variables are static so that they will be
731 // automatically scanned by the Boehm collector. This is needed
732 // because with qthreads the collector won't scan the initial stack --
733 // it will only scan the qthreads stacks.
735 // Command line arguments.
736 static JArray<jstring> *arg_vec;
738 // The primary thread.
739 static java::lang::Thread *main_thread;
741 #ifndef DISABLE_GETENV_PROPERTIES
743 static char *
744 next_property_key (char *s, size_t *length)
746 size_t l = 0;
748 JvAssert (s);
750 // Skip over whitespace
751 while (isspace (*s))
752 s++;
754 // If we've reached the end, return NULL. Also return NULL if for
755 // some reason we've come across a malformed property string.
756 if (*s == 0
757 || *s == ':'
758 || *s == '=')
759 return NULL;
761 // Determine the length of the property key.
762 while (s[l] != 0
763 && ! isspace (s[l])
764 && s[l] != ':'
765 && s[l] != '=')
767 if (s[l] == '\\'
768 && s[l+1] != 0)
769 l++;
770 l++;
773 *length = l;
775 return s;
778 static char *
779 next_property_value (char *s, size_t *length)
781 size_t l = 0;
783 JvAssert (s);
785 while (isspace (*s))
786 s++;
788 if (*s == ':'
789 || *s == '=')
790 s++;
792 while (isspace (*s))
793 s++;
795 // If we've reached the end, return NULL.
796 if (*s == 0)
797 return NULL;
799 // Determine the length of the property value.
800 while (s[l] != 0
801 && ! isspace (s[l])
802 && s[l] != ':'
803 && s[l] != '=')
805 if (s[l] == '\\'
806 && s[l+1] != 0)
807 l += 2;
808 else
809 l++;
812 *length = l;
814 return s;
817 static void
818 process_gcj_properties ()
820 char *props = getenv("GCJ_PROPERTIES");
821 char *p = props;
822 size_t length;
823 size_t property_count = 0;
825 if (NULL == props)
826 return;
828 // Whip through props quickly in order to count the number of
829 // property values.
830 while (p && (p = next_property_key (p, &length)))
832 // Skip to the end of the key
833 p += length;
835 p = next_property_value (p, &length);
836 if (p)
837 p += length;
839 property_count++;
842 // Allocate an array of property value/key pairs.
843 _Jv_Environment_Properties =
844 (property_pair *) malloc (sizeof(property_pair)
845 * (property_count + 1));
847 // Go through the properties again, initializing _Jv_Properties
848 // along the way.
849 p = props;
850 property_count = 0;
851 while (p && (p = next_property_key (p, &length)))
853 _Jv_Environment_Properties[property_count].key = p;
854 _Jv_Environment_Properties[property_count].key_length = length;
856 // Skip to the end of the key
857 p += length;
859 p = next_property_value (p, &length);
861 _Jv_Environment_Properties[property_count].value = p;
862 _Jv_Environment_Properties[property_count].value_length = length;
864 if (p)
865 p += length;
867 property_count++;
869 memset ((void *) &_Jv_Environment_Properties[property_count],
870 0, sizeof (property_pair));
872 size_t i = 0;
874 // Null terminate the strings.
875 while (_Jv_Environment_Properties[i].key)
877 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
878 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
882 #endif // DISABLE_GETENV_PROPERTIES
884 namespace gcj
886 _Jv_Utf8Const *void_signature;
887 _Jv_Utf8Const *clinit_name;
888 _Jv_Utf8Const *init_name;
889 _Jv_Utf8Const *finit_name;
891 bool runtimeInitialized = false;
894 jint
895 _Jv_CreateJavaVM (void* /*vm_args*/)
897 using namespace gcj;
899 if (runtimeInitialized)
900 return -1;
902 runtimeInitialized = true;
904 PROCESS_GCJ_PROPERTIES;
906 _Jv_InitThreads ();
907 _Jv_InitGC ();
908 _Jv_InitializeSyncMutex ();
910 #ifdef INTERPRETER
911 _Jv_InitInterpreter ();
912 #endif
914 /* Initialize Utf8 constants declared in jvm.h. */
915 void_signature = _Jv_makeUtf8Const ("()V", 3);
916 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
917 init_name = _Jv_makeUtf8Const ("<init>", 6);
918 finit_name = _Jv_makeUtf8Const ("finit$", 6);
920 /* Initialize built-in classes to represent primitive TYPEs. */
921 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1, &_Jv_byteVTable);
922 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2, &_Jv_shortVTable);
923 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4, &_Jv_intVTable);
924 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8, &_Jv_longVTable);
925 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1, &_Jv_booleanVTable);
926 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2, &_Jv_charVTable);
927 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4, &_Jv_floatVTable);
928 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8, &_Jv_doubleVTable);
929 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0, &_Jv_voidVTable);
931 // Turn stack trace generation off while creating exception objects.
932 _Jv_InitClass (&java::lang::VMThrowable::class$);
933 java::lang::VMThrowable::trace_enabled = 0;
935 // We have to initialize this fairly early, to avoid circular class
936 // initialization. In particular we want to start the
937 // initialization of ClassLoader before we start the initialization
938 // of VMClassLoader.
939 _Jv_InitClass (&java::lang::ClassLoader::class$);
940 // Once the bootstrap loader is in place, change it into a kind of
941 // system loader, by having it read the class path.
942 gnu::gcj::runtime::VMClassLoader::initialize();
944 INIT_SEGV;
945 #ifdef HANDLE_FPE
946 INIT_FPE;
947 #endif
949 no_memory = new java::lang::OutOfMemoryError;
951 java::lang::VMThrowable::trace_enabled = 1;
953 #ifdef USE_LTDL
954 LTDL_SET_PRELOADED_SYMBOLS ();
955 #endif
957 _Jv_platform_initialize ();
959 _Jv_JNI_Init ();
961 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
963 // Start the GC finalizer thread. A VirtualMachineError can be
964 // thrown by the runtime if, say, threads aren't available.
967 using namespace gnu::gcj::runtime;
968 FinalizerThread *ft = new FinalizerThread ();
969 ft->start ();
971 catch (java::lang::VirtualMachineError *ignore)
975 return 0;
978 void
979 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
980 bool is_jar)
982 _Jv_SetArgs (argc, argv);
984 java::lang::Runtime *runtime = NULL;
988 // Set this very early so that it is seen when java.lang.System
989 // is initialized.
990 if (is_jar)
991 _Jv_Jar_Class_Path = strdup (name);
992 _Jv_CreateJavaVM (NULL);
994 // Get the Runtime here. We want to initialize it before searching
995 // for `main'; that way it will be set up if `main' is a JNI method.
996 runtime = java::lang::Runtime::getRuntime ();
998 #ifdef DISABLE_MAIN_ARGS
999 arg_vec = JvConvertArgv (0, 0);
1000 #else
1001 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1002 #endif
1004 using namespace gnu::gcj::runtime;
1005 if (klass)
1006 main_thread = new FirstThread (klass, arg_vec);
1007 else
1008 main_thread = new FirstThread (JvNewStringLatin1 (name),
1009 arg_vec, is_jar);
1011 catch (java::lang::Throwable *t)
1013 java::lang::System::err->println (JvNewStringLatin1
1014 ("Exception during runtime initialization"));
1015 t->printStackTrace();
1016 runtime->exit (1);
1019 _Jv_AttachCurrentThread (main_thread);
1020 _Jv_ThreadRun (main_thread);
1021 _Jv_ThreadWait ();
1023 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1024 runtime->exit (status);
1027 void
1028 JvRunMain (jclass klass, int argc, const char **argv)
1030 _Jv_RunMain (klass, NULL, argc, argv, false);
1035 // Parse a string and return a heap size.
1036 static size_t
1037 parse_heap_size (const char *spec)
1039 char *end;
1040 unsigned long val = strtoul (spec, &end, 10);
1041 if (*end == 'k' || *end == 'K')
1042 val *= 1024;
1043 else if (*end == 'm' || *end == 'M')
1044 val *= 1048576;
1045 return (size_t) val;
1048 // Set the initial heap size. This might be ignored by the GC layer.
1049 // This must be called before _Jv_RunMain.
1050 void
1051 _Jv_SetInitialHeapSize (const char *arg)
1053 size_t size = parse_heap_size (arg);
1054 _Jv_GCSetInitialHeapSize (size);
1057 // Set the maximum heap size. This might be ignored by the GC layer.
1058 // This must be called before _Jv_RunMain.
1059 void
1060 _Jv_SetMaximumHeapSize (const char *arg)
1062 size_t size = parse_heap_size (arg);
1063 _Jv_GCSetMaximumHeapSize (size);
1068 void *
1069 _Jv_Malloc (jsize size)
1071 if (__builtin_expect (size == 0, false))
1072 size = 1;
1073 void *ptr = malloc ((size_t) size);
1074 if (__builtin_expect (ptr == NULL, false))
1075 throw no_memory;
1076 return ptr;
1079 void *
1080 _Jv_Realloc (void *ptr, jsize size)
1082 if (__builtin_expect (size == 0, false))
1083 size = 1;
1084 ptr = realloc (ptr, (size_t) size);
1085 if (__builtin_expect (ptr == NULL, false))
1086 throw no_memory;
1087 return ptr;
1090 void *
1091 _Jv_MallocUnchecked (jsize size)
1093 if (__builtin_expect (size == 0, false))
1094 size = 1;
1095 return malloc ((size_t) size);
1098 void
1099 _Jv_Free (void* ptr)
1101 return free (ptr);
1106 // In theory, these routines can be #ifdef'd away on machines which
1107 // support divide overflow signals. However, we never know if some
1108 // code might have been compiled with "-fuse-divide-subroutine", so we
1109 // always include them in libgcj.
1111 jint
1112 _Jv_divI (jint dividend, jint divisor)
1114 if (__builtin_expect (divisor == 0, false))
1116 java::lang::ArithmeticException *arithexception
1117 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1118 throw arithexception;
1121 if (dividend == (jint) 0x80000000L && divisor == -1)
1122 return dividend;
1124 return dividend / divisor;
1127 jint
1128 _Jv_remI (jint dividend, jint divisor)
1130 if (__builtin_expect (divisor == 0, false))
1132 java::lang::ArithmeticException *arithexception
1133 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1134 throw arithexception;
1137 if (dividend == (jint) 0x80000000L && divisor == -1)
1138 return 0;
1140 return dividend % divisor;
1143 jlong
1144 _Jv_divJ (jlong dividend, jlong divisor)
1146 if (__builtin_expect (divisor == 0, false))
1148 java::lang::ArithmeticException *arithexception
1149 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1150 throw arithexception;
1153 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1154 return dividend;
1156 return dividend / divisor;
1159 jlong
1160 _Jv_remJ (jlong dividend, jlong divisor)
1162 if (__builtin_expect (divisor == 0, false))
1164 java::lang::ArithmeticException *arithexception
1165 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1166 throw arithexception;
1169 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1170 return 0;
1172 return dividend % divisor;
1177 // Return true if SELF_KLASS can access a field or method in
1178 // OTHER_KLASS. The field or method's access flags are specified in
1179 // FLAGS.
1180 jboolean
1181 _Jv_CheckAccess (jclass self_klass, jclass other_klass, jint flags)
1183 using namespace java::lang::reflect;
1184 return ((self_klass == other_klass)
1185 || ((flags & Modifier::PUBLIC) != 0)
1186 || (((flags & Modifier::PROTECTED) != 0)
1187 && other_klass->isAssignableFrom (self_klass))
1188 || (((flags & Modifier::PRIVATE) == 0)
1189 && _Jv_ClassNameSamePackage (self_klass->name,
1190 other_klass->name)));