prims.cc (_Jv_AllocObject): Remove `size' argument.
[gcc.git] / libjava / prims.cc
1 // prims.cc - Code for core of runtime environment.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 #include <config.h>
12 #include <platform.h>
13
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <signal.h>
19
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23
24 #include <gcj/cni.h>
25 #include <jvm.h>
26 #include <java-signal.h>
27 #include <java-threads.h>
28
29 #ifdef ENABLE_JVMPI
30 #include <jvmpi.h>
31 #include <java/lang/ThreadGroup.h>
32 #endif
33
34 #ifndef DISABLE_GETENV_PROPERTIES
35 #include <ctype.h>
36 #include <java-props.h>
37 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
38 #else
39 #define PROCESS_GCJ_PROPERTIES
40 #endif // DISABLE_GETENV_PROPERTIES
41
42 #include <java/lang/Class.h>
43 #include <java/lang/ClassLoader.h>
44 #include <java/lang/Runtime.h>
45 #include <java/lang/String.h>
46 #include <java/lang/Thread.h>
47 #include <java/lang/ThreadGroup.h>
48 #include <java/lang/ArrayIndexOutOfBoundsException.h>
49 #include <java/lang/ArithmeticException.h>
50 #include <java/lang/ClassFormatError.h>
51 #include <java/lang/InternalError.h>
52 #include <java/lang/NegativeArraySizeException.h>
53 #include <java/lang/NullPointerException.h>
54 #include <java/lang/OutOfMemoryError.h>
55 #include <java/lang/System.h>
56 #include <java/lang/VMThrowable.h>
57 #include <java/lang/reflect/Modifier.h>
58 #include <java/io/PrintStream.h>
59 #include <java/lang/UnsatisfiedLinkError.h>
60 #include <java/lang/VirtualMachineError.h>
61 #include <gnu/gcj/runtime/VMClassLoader.h>
62 #include <gnu/gcj/runtime/FinalizerThread.h>
63 #include <gnu/gcj/runtime/FirstThread.h>
64
65 #ifdef USE_LTDL
66 #include <ltdl.h>
67 #endif
68
69 // We allocate a single OutOfMemoryError exception which we keep
70 // around for use if we run out of memory.
71 static java::lang::OutOfMemoryError *no_memory;
72
73 // Number of bytes in largest array object we create. This could be
74 // increased to the largest size_t value, so long as the appropriate
75 // functions are changed to take a size_t argument instead of jint.
76 #define MAX_OBJECT_SIZE ((1<<31) - 1)
77
78 static const char *no_properties[] = { NULL };
79
80 // Properties set at compile time.
81 const char **_Jv_Compiler_Properties = no_properties;
82
83 // The JAR file to add to the beginning of java.class.path.
84 const char *_Jv_Jar_Class_Path;
85
86 #ifndef DISABLE_GETENV_PROPERTIES
87 // Property key/value pairs.
88 property_pair *_Jv_Environment_Properties;
89 #endif
90
91 // Stash the argv pointer to benefit native libraries that need it.
92 const char **_Jv_argv;
93 int _Jv_argc;
94
95 // Argument support.
96 int
97 _Jv_GetNbArgs (void)
98 {
99 // _Jv_argc is 0 if not explicitly initialized.
100 return _Jv_argc;
101 }
102
103 const char *
104 _Jv_GetSafeArg (int index)
105 {
106 if (index >=0 && index < _Jv_GetNbArgs ())
107 return _Jv_argv[index];
108 else
109 return "";
110 }
111
112 void
113 _Jv_SetArgs (int argc, const char **argv)
114 {
115 _Jv_argc = argc;
116 _Jv_argv = argv;
117 }
118
119 #ifdef ENABLE_JVMPI
120 // Pointer to JVMPI notification functions.
121 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
122 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
123 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
124 #endif
125 \f
126
127 /* Unblock a signal. Unless we do this, the signal may only be sent
128 once. */
129 static void
130 unblock_signal (int signum)
131 {
132 #ifdef _POSIX_VERSION
133 sigset_t sigs;
134
135 sigemptyset (&sigs);
136 sigaddset (&sigs, signum);
137 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
138 #endif
139 }
140
141 #ifdef HANDLE_SEGV
142 SIGNAL_HANDLER (catch_segv)
143 {
144 java::lang::NullPointerException *nullp
145 = new java::lang::NullPointerException;
146 unblock_signal (SIGSEGV);
147 MAKE_THROW_FRAME (nullp);
148 throw nullp;
149 }
150 #endif
151
152 #ifdef HANDLE_FPE
153 SIGNAL_HANDLER (catch_fpe)
154 {
155 java::lang::ArithmeticException *arithexception
156 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
157 unblock_signal (SIGFPE);
158 #ifdef HANDLE_DIVIDE_OVERFLOW
159 HANDLE_DIVIDE_OVERFLOW;
160 #else
161 MAKE_THROW_FRAME (arithexception);
162 #endif
163 throw arithexception;
164 }
165 #endif
166
167 \f
168
169 jboolean
170 _Jv_equalUtf8Consts (const Utf8Const* a, const Utf8Const *b)
171 {
172 int len;
173 const _Jv_ushort *aptr, *bptr;
174 if (a == b)
175 return true;
176 if (a->hash != b->hash)
177 return false;
178 len = a->length;
179 if (b->length != len)
180 return false;
181 aptr = (const _Jv_ushort *)a->data;
182 bptr = (const _Jv_ushort *)b->data;
183 len = (len + 1) >> 1;
184 while (--len >= 0)
185 if (*aptr++ != *bptr++)
186 return false;
187 return true;
188 }
189
190 /* True iff A is equal to STR.
191 HASH is STR->hashCode().
192 */
193
194 jboolean
195 _Jv_equal (Utf8Const* a, jstring str, jint hash)
196 {
197 if (a->hash != (_Jv_ushort) hash)
198 return false;
199 jint len = str->length();
200 jint i = 0;
201 jchar *sptr = _Jv_GetStringChars (str);
202 unsigned char* ptr = (unsigned char*) a->data;
203 unsigned char* limit = ptr + a->length;
204 for (;; i++, sptr++)
205 {
206 int ch = UTF8_GET (ptr, limit);
207 if (i == len)
208 return ch < 0;
209 if (ch != *sptr)
210 return false;
211 }
212 return true;
213 }
214
215 /* Like _Jv_equal, but stop after N characters. */
216 jboolean
217 _Jv_equaln (Utf8Const *a, jstring str, jint n)
218 {
219 jint len = str->length();
220 jint i = 0;
221 jchar *sptr = _Jv_GetStringChars (str);
222 unsigned char* ptr = (unsigned char*) a->data;
223 unsigned char* limit = ptr + a->length;
224 for (; n-- > 0; i++, sptr++)
225 {
226 int ch = UTF8_GET (ptr, limit);
227 if (i == len)
228 return ch < 0;
229 if (ch != *sptr)
230 return false;
231 }
232 return true;
233 }
234
235 /* Count the number of Unicode chars encoded in a given Ut8 string. */
236 int
237 _Jv_strLengthUtf8(char* str, int len)
238 {
239 unsigned char* ptr;
240 unsigned char* limit;
241 int str_length;
242
243 ptr = (unsigned char*) str;
244 limit = ptr + len;
245 str_length = 0;
246 for (; ptr < limit; str_length++)
247 {
248 if (UTF8_GET (ptr, limit) < 0)
249 return (-1);
250 }
251 return (str_length);
252 }
253
254 /* Calculate a hash value for a string encoded in Utf8 format.
255 * This returns the same hash value as specified or java.lang.String.hashCode.
256 */
257 static jint
258 hashUtf8String (char* str, int len)
259 {
260 unsigned char* ptr = (unsigned char*) str;
261 unsigned char* limit = ptr + len;
262 jint hash = 0;
263
264 for (; ptr < limit;)
265 {
266 int ch = UTF8_GET (ptr, limit);
267 /* Updated specification from
268 http://www.javasoft.com/docs/books/jls/clarify.html. */
269 hash = (31 * hash) + ch;
270 }
271 return hash;
272 }
273
274 _Jv_Utf8Const *
275 _Jv_makeUtf8Const (char* s, int len)
276 {
277 if (len < 0)
278 len = strlen (s);
279 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
280 memcpy (m->data, s, len);
281 m->data[len] = 0;
282 m->length = len;
283 m->hash = hashUtf8String (s, len) & 0xFFFF;
284 return (m);
285 }
286
287 _Jv_Utf8Const *
288 _Jv_makeUtf8Const (jstring string)
289 {
290 jint hash = string->hashCode ();
291 jint len = _Jv_GetStringUTFLength (string);
292
293 Utf8Const* m = (Utf8Const*)
294 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
295
296 m->hash = hash;
297 m->length = len;
298
299 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
300 m->data[len] = 0;
301
302 return m;
303 }
304
305 \f
306
307 #ifdef DEBUG
308 void
309 _Jv_Abort (const char *function, const char *file, int line,
310 const char *message)
311 #else
312 void
313 _Jv_Abort (const char *, const char *, int, const char *message)
314 #endif
315 {
316 #ifdef DEBUG
317 fprintf (stderr,
318 "libgcj failure: %s\n in function %s, file %s, line %d\n",
319 message, function, file, line);
320 #else
321 fprintf (stderr, "libgcj failure: %s\n", message);
322 #endif
323 abort ();
324 }
325
326 static void
327 fail_on_finalization (jobject)
328 {
329 JvFail ("object was finalized");
330 }
331
332 void
333 _Jv_GCWatch (jobject obj)
334 {
335 _Jv_RegisterFinalizer (obj, fail_on_finalization);
336 }
337
338 void
339 _Jv_ThrowBadArrayIndex(jint bad_index)
340 {
341 throw new java::lang::ArrayIndexOutOfBoundsException
342 (java::lang::String::valueOf (bad_index));
343 }
344
345 void
346 _Jv_ThrowNullPointerException ()
347 {
348 throw new java::lang::NullPointerException;
349 }
350
351 // Explicitly throw a no memory exception.
352 // The collector calls this when it encounters an out-of-memory condition.
353 void _Jv_ThrowNoMemory()
354 {
355 throw no_memory;
356 }
357
358 #ifdef ENABLE_JVMPI
359 static void
360 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
361 {
362 // Service JVMPI allocation request.
363 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
364 {
365 JVMPI_Event event;
366
367 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
368 event.env_id = NULL;
369 event.u.obj_alloc.arena_id = 0;
370 event.u.obj_alloc.class_id = (jobjectID) klass;
371 event.u.obj_alloc.is_array = 0;
372 event.u.obj_alloc.size = size;
373 event.u.obj_alloc.obj_id = (jobjectID) obj;
374
375 // FIXME: This doesn't look right for the Boehm GC. A GC may
376 // already be in progress. _Jv_DisableGC () doesn't wait for it.
377 // More importantly, I don't see the need for disabling GC, since we
378 // blatantly have a pointer to obj on our stack, ensuring that the
379 // object can't be collected. Even for a nonconservative collector,
380 // it appears to me that this must be true, since we are about to
381 // return obj. Isn't this whole approach way too intrusive for
382 // a useful profiling interface? - HB
383 _Jv_DisableGC ();
384 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
385 _Jv_EnableGC ();
386 }
387 }
388 #else /* !ENABLE_JVMPI */
389 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
390 #endif
391
392 // Allocate a new object of class KLASS.
393 // First a version that assumes that we have no finalizer, and that
394 // the class is already initialized.
395 // If we know that JVMPI is disabled, this can be replaced by a direct call
396 // to the allocator for the appropriate GC.
397 jobject
398 _Jv_AllocObjectNoInitNoFinalizer (jclass klass)
399 {
400 jint size = klass->size ();
401 jobject obj = (jobject) _Jv_AllocObj (size, klass);
402 jvmpi_notify_alloc (klass, size, obj);
403 return obj;
404 }
405
406 // And now a version that initializes if necessary.
407 jobject
408 _Jv_AllocObjectNoFinalizer (jclass klass)
409 {
410 _Jv_InitClass (klass);
411 jint size = klass->size ();
412 jobject obj = (jobject) _Jv_AllocObj (size, klass);
413 jvmpi_notify_alloc (klass, size, obj);
414 return obj;
415 }
416
417 // And now the general version that registers a finalizer if necessary.
418 jobject
419 _Jv_AllocObject (jclass klass)
420 {
421 jobject obj = _Jv_AllocObjectNoFinalizer (klass);
422
423 // We assume that the compiler only generates calls to this routine
424 // if there really is an interesting finalizer.
425 // Unfortunately, we still have to the dynamic test, since there may
426 // be cni calls to this routine.
427 // Note that on IA64 get_finalizer() returns the starting address of the
428 // function, not a function pointer. Thus this still works.
429 if (klass->vtable->get_finalizer ()
430 != java::lang::Object::class$.vtable->get_finalizer ())
431 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
432 return obj;
433 }
434
435 // Allocate a String, including variable length storage.
436 jstring
437 _Jv_AllocString(jsize len)
438 {
439 using namespace java::lang;
440
441 jsize sz = sizeof(java::lang::String) + len * sizeof(jchar);
442
443 // We assert that for strings allocated this way, the data field
444 // will always point to the object itself. Thus there is no reason
445 // for the garbage collector to scan any of it.
446 // Furthermore, we're about to overwrite the string data, so
447 // initialization of the object is not an issue.
448
449 // String needs no initialization, and there is no finalizer, so
450 // we can go directly to the collector's allocator interface.
451 jstring obj = (jstring) _Jv_AllocPtrFreeObj(sz, &String::class$);
452
453 obj->data = obj;
454 obj->boffset = sizeof(java::lang::String);
455 obj->count = len;
456 obj->cachedHashCode = 0;
457
458 #ifdef ENABLE_JVMPI
459 // Service JVMPI request.
460
461 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
462 {
463 JVMPI_Event event;
464
465 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
466 event.env_id = NULL;
467 event.u.obj_alloc.arena_id = 0;
468 event.u.obj_alloc.class_id = (jobjectID) &String::class$;
469 event.u.obj_alloc.is_array = 0;
470 event.u.obj_alloc.size = sz;
471 event.u.obj_alloc.obj_id = (jobjectID) obj;
472
473 _Jv_DisableGC ();
474 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
475 _Jv_EnableGC ();
476 }
477 #endif
478
479 return obj;
480 }
481
482 // A version of the above that assumes the object contains no pointers,
483 // and requires no finalization. This can't happen if we need pointers
484 // to locks.
485 #ifdef JV_HASH_SYNCHRONIZATION
486 jobject
487 _Jv_AllocPtrFreeObject (jclass klass)
488 {
489 _Jv_InitClass (klass);
490 jint size = klass->size ();
491
492 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
493
494 #ifdef ENABLE_JVMPI
495 // Service JVMPI request.
496
497 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
498 {
499 JVMPI_Event event;
500
501 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
502 event.env_id = NULL;
503 event.u.obj_alloc.arena_id = 0;
504 event.u.obj_alloc.class_id = (jobjectID) klass;
505 event.u.obj_alloc.is_array = 0;
506 event.u.obj_alloc.size = size;
507 event.u.obj_alloc.obj_id = (jobjectID) obj;
508
509 _Jv_DisableGC ();
510 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
511 _Jv_EnableGC ();
512 }
513 #endif
514
515 return obj;
516 }
517 #endif /* JV_HASH_SYNCHRONIZATION */
518
519
520 // Allocate a new array of Java objects. Each object is of type
521 // `elementClass'. `init' is used to initialize each slot in the
522 // array.
523 jobjectArray
524 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
525 {
526 if (__builtin_expect (count < 0, false))
527 throw new java::lang::NegativeArraySizeException;
528
529 JvAssert (! elementClass->isPrimitive ());
530
531 // Ensure that elements pointer is properly aligned.
532 jobjectArray obj = NULL;
533 size_t size = (size_t) elements (obj);
534 // Check for overflow.
535 if (__builtin_expect ((size_t) count >
536 (MAX_OBJECT_SIZE - 1 - size) / sizeof (jobject), false))
537 throw no_memory;
538
539 size += count * sizeof (jobject);
540
541 jclass klass = _Jv_GetArrayClass (elementClass,
542 elementClass->getClassLoaderInternal());
543
544 obj = (jobjectArray) _Jv_AllocArray (size, klass);
545 // Cast away const.
546 jsize *lp = const_cast<jsize *> (&obj->length);
547 *lp = count;
548 // We know the allocator returns zeroed memory. So don't bother
549 // zeroing it again.
550 if (init)
551 {
552 jobject *ptr = elements(obj);
553 while (--count >= 0)
554 *ptr++ = init;
555 }
556 return obj;
557 }
558
559 // Allocate a new array of primitives. ELTYPE is the type of the
560 // element, COUNT is the size of the array.
561 jobject
562 _Jv_NewPrimArray (jclass eltype, jint count)
563 {
564 int elsize = eltype->size();
565 if (__builtin_expect (count < 0, false))
566 throw new java::lang::NegativeArraySizeException;
567
568 JvAssert (eltype->isPrimitive ());
569 jobject dummy = NULL;
570 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
571
572 // Check for overflow.
573 if (__builtin_expect ((size_t) count >
574 (MAX_OBJECT_SIZE - size) / elsize, false))
575 throw no_memory;
576
577 jclass klass = _Jv_GetArrayClass (eltype, 0);
578
579 # ifdef JV_HASH_SYNCHRONIZATION
580 // Since the vtable is always statically allocated,
581 // these are completely pointerfree! Make sure the GC doesn't touch them.
582 __JArray *arr =
583 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
584 memset((char *)arr + size, 0, elsize * count);
585 # else
586 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
587 // Note that we assume we are given zeroed memory by the allocator.
588 # endif
589 // Cast away const.
590 jsize *lp = const_cast<jsize *> (&arr->length);
591 *lp = count;
592
593 return arr;
594 }
595
596 jobject
597 _Jv_NewArray (jint type, jint size)
598 {
599 switch (type)
600 {
601 case 4: return JvNewBooleanArray (size);
602 case 5: return JvNewCharArray (size);
603 case 6: return JvNewFloatArray (size);
604 case 7: return JvNewDoubleArray (size);
605 case 8: return JvNewByteArray (size);
606 case 9: return JvNewShortArray (size);
607 case 10: return JvNewIntArray (size);
608 case 11: return JvNewLongArray (size);
609 }
610 throw new java::lang::InternalError
611 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
612 }
613
614 // Allocate a possibly multi-dimensional array but don't check that
615 // any array length is <0.
616 static jobject
617 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
618 {
619 JvAssert (type->isArray());
620 jclass element_type = type->getComponentType();
621 jobject result;
622 if (element_type->isPrimitive())
623 result = _Jv_NewPrimArray (element_type, sizes[0]);
624 else
625 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
626
627 if (dimensions > 1)
628 {
629 JvAssert (! element_type->isPrimitive());
630 JvAssert (element_type->isArray());
631 jobject *contents = elements ((jobjectArray) result);
632 for (int i = 0; i < sizes[0]; ++i)
633 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
634 sizes + 1);
635 }
636
637 return result;
638 }
639
640 jobject
641 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
642 {
643 for (int i = 0; i < dimensions; ++i)
644 if (sizes[i] < 0)
645 throw new java::lang::NegativeArraySizeException;
646
647 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
648 }
649
650 jobject
651 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
652 {
653 va_list args;
654 jint sizes[dimensions];
655 va_start (args, dimensions);
656 for (int i = 0; i < dimensions; ++i)
657 {
658 jint size = va_arg (args, jint);
659 if (size < 0)
660 throw new java::lang::NegativeArraySizeException;
661 sizes[i] = size;
662 }
663 va_end (args);
664
665 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
666 }
667
668 \f
669
670 // Ensure 8-byte alignment, for hash synchronization.
671 #define DECLARE_PRIM_TYPE(NAME) \
672 _Jv_ArrayVTable _Jv_##NAME##VTable; \
673 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
674
675 DECLARE_PRIM_TYPE(byte)
676 DECLARE_PRIM_TYPE(short)
677 DECLARE_PRIM_TYPE(int)
678 DECLARE_PRIM_TYPE(long)
679 DECLARE_PRIM_TYPE(boolean)
680 DECLARE_PRIM_TYPE(char)
681 DECLARE_PRIM_TYPE(float)
682 DECLARE_PRIM_TYPE(double)
683 DECLARE_PRIM_TYPE(void)
684
685 void
686 _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len,
687 _Jv_ArrayVTable *array_vtable)
688 {
689 using namespace java::lang::reflect;
690
691 // We must set the vtable for the class; the Java constructor
692 // doesn't do this.
693 (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
694
695 // Initialize the fields we care about. We do this in the same
696 // order they are declared in Class.h.
697 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
698 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
699 cl->method_count = sig;
700 cl->size_in_bytes = len;
701 cl->vtable = JV_PRIMITIVE_VTABLE;
702 cl->state = JV_STATE_DONE;
703 cl->depth = -1;
704 if (sig != 'V')
705 _Jv_NewArrayClass (cl, NULL, (_Jv_VTable *) array_vtable);
706 }
707
708 jclass
709 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
710 {
711 switch (*sig)
712 {
713 case 'B':
714 return JvPrimClass (byte);
715 case 'S':
716 return JvPrimClass (short);
717 case 'I':
718 return JvPrimClass (int);
719 case 'J':
720 return JvPrimClass (long);
721 case 'Z':
722 return JvPrimClass (boolean);
723 case 'C':
724 return JvPrimClass (char);
725 case 'F':
726 return JvPrimClass (float);
727 case 'D':
728 return JvPrimClass (double);
729 case 'V':
730 return JvPrimClass (void);
731 case 'L':
732 {
733 int i;
734 for (i = 1; sig[i] && sig[i] != ';'; ++i)
735 ;
736 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
737 return _Jv_FindClass (name, loader);
738 }
739 case '[':
740 {
741 jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
742 if (! klass)
743 return NULL;
744 return _Jv_GetArrayClass (klass, loader);
745 }
746 }
747
748 return NULL; // Placate compiler.
749 }
750
751 \f
752
753 JArray<jstring> *
754 JvConvertArgv (int argc, const char **argv)
755 {
756 if (argc < 0)
757 argc = 0;
758 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
759 jobject *ptr = elements(ar);
760 jbyteArray bytes = NULL;
761 for (int i = 0; i < argc; i++)
762 {
763 const char *arg = argv[i];
764 int len = strlen (arg);
765 if (bytes == NULL || bytes->length < len)
766 bytes = JvNewByteArray (len);
767 jbyte *bytePtr = elements (bytes);
768 // We assume jbyte == char.
769 memcpy (bytePtr, arg, len);
770
771 // Now convert using the default encoding.
772 *ptr++ = new java::lang::String (bytes, 0, len);
773 }
774 return (JArray<jstring>*) ar;
775 }
776
777 // FIXME: These variables are static so that they will be
778 // automatically scanned by the Boehm collector. This is needed
779 // because with qthreads the collector won't scan the initial stack --
780 // it will only scan the qthreads stacks.
781
782 // Command line arguments.
783 static JArray<jstring> *arg_vec;
784
785 // The primary thread.
786 static java::lang::Thread *main_thread;
787
788 #ifndef DISABLE_GETENV_PROPERTIES
789
790 static char *
791 next_property_key (char *s, size_t *length)
792 {
793 size_t l = 0;
794
795 JvAssert (s);
796
797 // Skip over whitespace
798 while (isspace (*s))
799 s++;
800
801 // If we've reached the end, return NULL. Also return NULL if for
802 // some reason we've come across a malformed property string.
803 if (*s == 0
804 || *s == ':'
805 || *s == '=')
806 return NULL;
807
808 // Determine the length of the property key.
809 while (s[l] != 0
810 && ! isspace (s[l])
811 && s[l] != ':'
812 && s[l] != '=')
813 {
814 if (s[l] == '\\'
815 && s[l+1] != 0)
816 l++;
817 l++;
818 }
819
820 *length = l;
821
822 return s;
823 }
824
825 static char *
826 next_property_value (char *s, size_t *length)
827 {
828 size_t l = 0;
829
830 JvAssert (s);
831
832 while (isspace (*s))
833 s++;
834
835 if (*s == ':'
836 || *s == '=')
837 s++;
838
839 while (isspace (*s))
840 s++;
841
842 // If we've reached the end, return NULL.
843 if (*s == 0)
844 return NULL;
845
846 // Determine the length of the property value.
847 while (s[l] != 0
848 && ! isspace (s[l])
849 && s[l] != ':'
850 && s[l] != '=')
851 {
852 if (s[l] == '\\'
853 && s[l+1] != 0)
854 l += 2;
855 else
856 l++;
857 }
858
859 *length = l;
860
861 return s;
862 }
863
864 static void
865 process_gcj_properties ()
866 {
867 char *props = getenv("GCJ_PROPERTIES");
868 char *p = props;
869 size_t length;
870 size_t property_count = 0;
871
872 if (NULL == props)
873 return;
874
875 // Whip through props quickly in order to count the number of
876 // property values.
877 while (p && (p = next_property_key (p, &length)))
878 {
879 // Skip to the end of the key
880 p += length;
881
882 p = next_property_value (p, &length);
883 if (p)
884 p += length;
885
886 property_count++;
887 }
888
889 // Allocate an array of property value/key pairs.
890 _Jv_Environment_Properties =
891 (property_pair *) malloc (sizeof(property_pair)
892 * (property_count + 1));
893
894 // Go through the properties again, initializing _Jv_Properties
895 // along the way.
896 p = props;
897 property_count = 0;
898 while (p && (p = next_property_key (p, &length)))
899 {
900 _Jv_Environment_Properties[property_count].key = p;
901 _Jv_Environment_Properties[property_count].key_length = length;
902
903 // Skip to the end of the key
904 p += length;
905
906 p = next_property_value (p, &length);
907
908 _Jv_Environment_Properties[property_count].value = p;
909 _Jv_Environment_Properties[property_count].value_length = length;
910
911 if (p)
912 p += length;
913
914 property_count++;
915 }
916 memset ((void *) &_Jv_Environment_Properties[property_count],
917 0, sizeof (property_pair));
918 {
919 size_t i = 0;
920
921 // Null terminate the strings.
922 while (_Jv_Environment_Properties[i].key)
923 {
924 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
925 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
926 }
927 }
928 }
929 #endif // DISABLE_GETENV_PROPERTIES
930
931 namespace gcj
932 {
933 _Jv_Utf8Const *void_signature;
934 _Jv_Utf8Const *clinit_name;
935 _Jv_Utf8Const *init_name;
936 _Jv_Utf8Const *finit_name;
937
938 bool runtimeInitialized = false;
939 }
940
941 jint
942 _Jv_CreateJavaVM (void* /*vm_args*/)
943 {
944 using namespace gcj;
945
946 if (runtimeInitialized)
947 return -1;
948
949 runtimeInitialized = true;
950
951 PROCESS_GCJ_PROPERTIES;
952
953 _Jv_InitThreads ();
954 _Jv_InitGC ();
955 _Jv_InitializeSyncMutex ();
956
957 /* Initialize Utf8 constants declared in jvm.h. */
958 void_signature = _Jv_makeUtf8Const ("()V", 3);
959 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
960 init_name = _Jv_makeUtf8Const ("<init>", 6);
961 finit_name = _Jv_makeUtf8Const ("finit$", 6);
962
963 /* Initialize built-in classes to represent primitive TYPEs. */
964 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1, &_Jv_byteVTable);
965 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2, &_Jv_shortVTable);
966 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4, &_Jv_intVTable);
967 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8, &_Jv_longVTable);
968 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1, &_Jv_booleanVTable);
969 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2, &_Jv_charVTable);
970 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4, &_Jv_floatVTable);
971 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8, &_Jv_doubleVTable);
972 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0, &_Jv_voidVTable);
973
974 // Turn stack trace generation off while creating exception objects.
975 _Jv_InitClass (&java::lang::VMThrowable::class$);
976 java::lang::VMThrowable::trace_enabled = 0;
977
978 // We have to initialize this fairly early, to avoid circular class
979 // initialization. In particular we want to start the
980 // initialization of ClassLoader before we start the initialization
981 // of VMClassLoader.
982 _Jv_InitClass (&java::lang::ClassLoader::class$);
983 // Once the bootstrap loader is in place, change it into a kind of
984 // system loader, by having it read the class path.
985 gnu::gcj::runtime::VMClassLoader::initialize();
986
987 INIT_SEGV;
988 #ifdef HANDLE_FPE
989 INIT_FPE;
990 #endif
991
992 no_memory = new java::lang::OutOfMemoryError;
993
994 java::lang::VMThrowable::trace_enabled = 1;
995
996 #ifdef USE_LTDL
997 LTDL_SET_PRELOADED_SYMBOLS ();
998 #endif
999
1000 _Jv_platform_initialize ();
1001
1002 _Jv_JNI_Init ();
1003
1004 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
1005
1006 // Start the GC finalizer thread. A VirtualMachineError can be
1007 // thrown by the runtime if, say, threads aren't available. In this
1008 // case finalizers simply won't run.
1009 try
1010 {
1011 using namespace gnu::gcj::runtime;
1012 FinalizerThread *ft = new FinalizerThread ();
1013 ft->start ();
1014 }
1015 catch (java::lang::VirtualMachineError *ignore)
1016 {
1017 }
1018
1019 return 0;
1020 }
1021
1022 void
1023 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
1024 bool is_jar)
1025 {
1026 _Jv_SetArgs (argc, argv);
1027
1028 java::lang::Runtime *runtime = NULL;
1029
1030 try
1031 {
1032 // Set this very early so that it is seen when java.lang.System
1033 // is initialized.
1034 if (is_jar)
1035 _Jv_Jar_Class_Path = strdup (name);
1036 _Jv_CreateJavaVM (NULL);
1037
1038 // Get the Runtime here. We want to initialize it before searching
1039 // for `main'; that way it will be set up if `main' is a JNI method.
1040 runtime = java::lang::Runtime::getRuntime ();
1041
1042 #ifdef DISABLE_MAIN_ARGS
1043 arg_vec = JvConvertArgv (0, 0);
1044 #else
1045 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1046 #endif
1047
1048 using namespace gnu::gcj::runtime;
1049 if (klass)
1050 main_thread = new FirstThread (klass, arg_vec);
1051 else
1052 main_thread = new FirstThread (JvNewStringLatin1 (name),
1053 arg_vec, is_jar);
1054 }
1055 catch (java::lang::Throwable *t)
1056 {
1057 java::lang::System::err->println (JvNewStringLatin1
1058 ("Exception during runtime initialization"));
1059 t->printStackTrace();
1060 runtime->exit (1);
1061 }
1062
1063 _Jv_AttachCurrentThread (main_thread);
1064 _Jv_ThreadRun (main_thread);
1065 _Jv_ThreadWait ();
1066
1067 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1068 runtime->exit (status);
1069 }
1070
1071 void
1072 JvRunMain (jclass klass, int argc, const char **argv)
1073 {
1074 _Jv_RunMain (klass, NULL, argc, argv, false);
1075 }
1076
1077 \f
1078
1079 // Parse a string and return a heap size.
1080 static size_t
1081 parse_heap_size (const char *spec)
1082 {
1083 char *end;
1084 unsigned long val = strtoul (spec, &end, 10);
1085 if (*end == 'k' || *end == 'K')
1086 val *= 1024;
1087 else if (*end == 'm' || *end == 'M')
1088 val *= 1048576;
1089 return (size_t) val;
1090 }
1091
1092 // Set the initial heap size. This might be ignored by the GC layer.
1093 // This must be called before _Jv_RunMain.
1094 void
1095 _Jv_SetInitialHeapSize (const char *arg)
1096 {
1097 size_t size = parse_heap_size (arg);
1098 _Jv_GCSetInitialHeapSize (size);
1099 }
1100
1101 // Set the maximum heap size. This might be ignored by the GC layer.
1102 // This must be called before _Jv_RunMain.
1103 void
1104 _Jv_SetMaximumHeapSize (const char *arg)
1105 {
1106 size_t size = parse_heap_size (arg);
1107 _Jv_GCSetMaximumHeapSize (size);
1108 }
1109
1110 \f
1111
1112 void *
1113 _Jv_Malloc (jsize size)
1114 {
1115 if (__builtin_expect (size == 0, false))
1116 size = 1;
1117 void *ptr = malloc ((size_t) size);
1118 if (__builtin_expect (ptr == NULL, false))
1119 throw no_memory;
1120 return ptr;
1121 }
1122
1123 void *
1124 _Jv_Realloc (void *ptr, jsize size)
1125 {
1126 if (__builtin_expect (size == 0, false))
1127 size = 1;
1128 ptr = realloc (ptr, (size_t) size);
1129 if (__builtin_expect (ptr == NULL, false))
1130 throw no_memory;
1131 return ptr;
1132 }
1133
1134 void *
1135 _Jv_MallocUnchecked (jsize size)
1136 {
1137 if (__builtin_expect (size == 0, false))
1138 size = 1;
1139 return malloc ((size_t) size);
1140 }
1141
1142 void
1143 _Jv_Free (void* ptr)
1144 {
1145 return free (ptr);
1146 }
1147
1148 \f
1149
1150 // In theory, these routines can be #ifdef'd away on machines which
1151 // support divide overflow signals. However, we never know if some
1152 // code might have been compiled with "-fuse-divide-subroutine", so we
1153 // always include them in libgcj.
1154
1155 jint
1156 _Jv_divI (jint dividend, jint divisor)
1157 {
1158 if (__builtin_expect (divisor == 0, false))
1159 {
1160 java::lang::ArithmeticException *arithexception
1161 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1162 throw arithexception;
1163 }
1164
1165 if (dividend == (jint) 0x80000000L && divisor == -1)
1166 return dividend;
1167
1168 return dividend / divisor;
1169 }
1170
1171 jint
1172 _Jv_remI (jint dividend, jint divisor)
1173 {
1174 if (__builtin_expect (divisor == 0, false))
1175 {
1176 java::lang::ArithmeticException *arithexception
1177 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1178 throw arithexception;
1179 }
1180
1181 if (dividend == (jint) 0x80000000L && divisor == -1)
1182 return 0;
1183
1184 return dividend % divisor;
1185 }
1186
1187 jlong
1188 _Jv_divJ (jlong dividend, jlong divisor)
1189 {
1190 if (__builtin_expect (divisor == 0, false))
1191 {
1192 java::lang::ArithmeticException *arithexception
1193 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1194 throw arithexception;
1195 }
1196
1197 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1198 return dividend;
1199
1200 return dividend / divisor;
1201 }
1202
1203 jlong
1204 _Jv_remJ (jlong dividend, jlong divisor)
1205 {
1206 if (__builtin_expect (divisor == 0, false))
1207 {
1208 java::lang::ArithmeticException *arithexception
1209 = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
1210 throw arithexception;
1211 }
1212
1213 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1214 return 0;
1215
1216 return dividend % divisor;
1217 }
1218
1219 \f
1220
1221 // Return true if SELF_KLASS can access a field or method in
1222 // OTHER_KLASS. The field or method's access flags are specified in
1223 // FLAGS.
1224 jboolean
1225 _Jv_CheckAccess (jclass self_klass, jclass other_klass, jint flags)
1226 {
1227 using namespace java::lang::reflect;
1228 return ((self_klass == other_klass)
1229 || ((flags & Modifier::PUBLIC) != 0)
1230 || (((flags & Modifier::PROTECTED) != 0)
1231 && other_klass->isAssignableFrom (self_klass))
1232 || (((flags & Modifier::PRIVATE) == 0)
1233 && _Jv_ClassNameSamePackage (self_klass->name,
1234 other_klass->name)));
1235 }