OSDN Git Service

fadc466bc0e9d3623e1ebbc6200a5990710b858d
[pf3gnuchains/gcc-fork.git] / libjava / prims.cc
1 // prims.cc - Code for core of runtime environment.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  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 #include <java-interp.h>
29
30 #ifdef ENABLE_JVMPI
31 #include <jvmpi.h>
32 #include <java/lang/ThreadGroup.h>
33 #endif
34
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
42
43 #include <java/lang/Class.h>
44 #include <java/lang/ClassLoader.h>
45 #include <java/lang/Runtime.h>
46 #include <java/lang/String.h>
47 #include <java/lang/Thread.h>
48 #include <java/lang/ThreadGroup.h>
49 #include <java/lang/ArrayIndexOutOfBoundsException.h>
50 #include <java/lang/ArithmeticException.h>
51 #include <java/lang/ClassFormatError.h>
52 #include <java/lang/InternalError.h>
53 #include <java/lang/NegativeArraySizeException.h>
54 #include <java/lang/NullPointerException.h>
55 #include <java/lang/OutOfMemoryError.h>
56 #include <java/lang/System.h>
57 #include <java/lang/VMThrowable.h>
58 #include <java/lang/VMClassLoader.h>
59 #include <java/lang/reflect/Modifier.h>
60 #include <java/io/PrintStream.h>
61 #include <java/lang/UnsatisfiedLinkError.h>
62 #include <java/lang/VirtualMachineError.h>
63 #include <gnu/gcj/runtime/ExtensionClassLoader.h>
64 #include <gnu/gcj/runtime/FinalizerThread.h>
65 #include <execution.h>
66 #include <gnu/java/lang/MainThread.h>
67
68 #ifdef USE_LTDL
69 #include <ltdl.h>
70 #endif
71
72 // Execution engine for compiled code.
73 _Jv_CompiledEngine _Jv_soleCompiledEngine;
74
75 // We allocate a single OutOfMemoryError exception which we keep
76 // around for use if we run out of memory.
77 static java::lang::OutOfMemoryError *no_memory;
78
79 // Number of bytes in largest array object we create.  This could be
80 // increased to the largest size_t value, so long as the appropriate
81 // functions are changed to take a size_t argument instead of jint.
82 #define MAX_OBJECT_SIZE ((1<<31) - 1)
83
84 // Properties set at compile time.
85 const char **_Jv_Compiler_Properties = NULL;
86 int _Jv_Properties_Count = 0;
87
88 #ifndef DISABLE_GETENV_PROPERTIES
89 // Property key/value pairs.
90 property_pair *_Jv_Environment_Properties;
91 #endif
92
93 // Stash the argv pointer to benefit native libraries that need it.
94 const char **_Jv_argv;
95 int _Jv_argc;
96
97 // Argument support.
98 int
99 _Jv_GetNbArgs (void)
100 {
101   // _Jv_argc is 0 if not explicitly initialized.
102   return _Jv_argc;
103 }
104
105 const char *
106 _Jv_GetSafeArg (int index)
107 {
108   if (index >=0 && index < _Jv_GetNbArgs ())
109     return _Jv_argv[index];
110   else
111     return "";
112 }
113
114 void
115 _Jv_SetArgs (int argc, const char **argv)
116 {
117   _Jv_argc = argc;
118   _Jv_argv = argv;
119 }
120
121 #ifdef ENABLE_JVMPI
122 // Pointer to JVMPI notification functions.
123 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
124 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
125 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
126 #endif
127 \f
128
129 #if defined (HANDLE_SEGV) || defined(HANDLE_FPE)
130 /* Unblock a signal.  Unless we do this, the signal may only be sent
131    once.  */
132 static void 
133 unblock_signal (int signum __attribute__ ((__unused__)))
134 {
135 #ifdef _POSIX_VERSION
136   sigset_t sigs;
137
138   sigemptyset (&sigs);
139   sigaddset (&sigs, signum);
140   sigprocmask (SIG_UNBLOCK, &sigs, NULL);
141 #endif
142 }
143 #endif
144
145 #ifdef HANDLE_SEGV
146 SIGNAL_HANDLER (catch_segv)
147 {
148   unblock_signal (SIGSEGV);
149   MAKE_THROW_FRAME (nullp);
150   java::lang::NullPointerException *nullp 
151     = new java::lang::NullPointerException;
152   throw nullp;
153 }
154 #endif
155
156 #ifdef HANDLE_FPE
157 SIGNAL_HANDLER (catch_fpe)
158 {
159   unblock_signal (SIGFPE);
160 #ifdef HANDLE_DIVIDE_OVERFLOW
161   HANDLE_DIVIDE_OVERFLOW;
162 #else
163   MAKE_THROW_FRAME (arithexception);
164 #endif
165   java::lang::ArithmeticException *arithexception 
166     = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));
167   throw arithexception;
168 }
169 #endif
170
171 \f
172
173 jboolean
174 _Jv_equalUtf8Consts (const Utf8Const* a, const Utf8Const *b)
175 {
176   int len;
177   const _Jv_ushort *aptr, *bptr;
178   if (a == b)
179     return true;
180   if (a->hash != b->hash)
181     return false;
182   len = a->length;
183   if (b->length != len)
184     return false;
185   aptr = (const _Jv_ushort *)a->data;
186   bptr = (const _Jv_ushort *)b->data;
187   len = (len + 1) >> 1;
188   while (--len >= 0)
189     if (*aptr++ != *bptr++)
190       return false;
191   return true;
192 }
193
194 /* True iff A is equal to STR.
195    HASH is STR->hashCode().  
196 */
197
198 jboolean
199 _Jv_equal (Utf8Const* a, jstring str, jint hash)
200 {
201   if (a->hash != (_Jv_ushort) hash)
202     return false;
203   jint len = str->length();
204   jint i = 0;
205   jchar *sptr = _Jv_GetStringChars (str);
206   unsigned char* ptr = (unsigned char*) a->data;
207   unsigned char* limit = ptr + a->length;
208   for (;; i++, sptr++)
209     {
210       int ch = UTF8_GET (ptr, limit);
211       if (i == len)
212         return ch < 0;
213       if (ch != *sptr)
214         return false;
215     }
216   return true;
217 }
218
219 /* Like _Jv_equal, but stop after N characters.  */
220 jboolean
221 _Jv_equaln (Utf8Const *a, jstring str, jint n)
222 {
223   jint len = str->length();
224   jint i = 0;
225   jchar *sptr = _Jv_GetStringChars (str);
226   unsigned char* ptr = (unsigned char*) a->data;
227   unsigned char* limit = ptr + a->length;
228   for (; n-- > 0; i++, sptr++)
229     {
230       int ch = UTF8_GET (ptr, limit);
231       if (i == len)
232         return ch < 0;
233       if (ch != *sptr)
234         return false;
235     }
236   return true;
237 }
238
239 /* Count the number of Unicode chars encoded in a given Ut8 string. */
240 int
241 _Jv_strLengthUtf8(char* str, int len)
242 {
243   unsigned char* ptr;
244   unsigned char* limit;
245   int str_length;
246
247   ptr = (unsigned char*) str;
248   limit = ptr + len;
249   str_length = 0;
250   for (; ptr < limit; str_length++)
251     {
252       if (UTF8_GET (ptr, limit) < 0)
253         return (-1);
254     }
255   return (str_length);
256 }
257
258 /* Calculate a hash value for a string encoded in Utf8 format.
259  * This returns the same hash value as specified or java.lang.String.hashCode.
260  */
261 jint
262 _Jv_hashUtf8String (char* str, int len)
263 {
264   unsigned char* ptr = (unsigned char*) str;
265   unsigned char* limit = ptr + len;
266   jint hash = 0;
267
268   for (; ptr < limit;)
269     {
270       int ch = UTF8_GET (ptr, limit);
271       /* Updated specification from
272          http://www.javasoft.com/docs/books/jls/clarify.html. */
273       hash = (31 * hash) + ch;
274     }
275   return hash;
276 }
277
278 void
279 _Jv_Utf8Const::init(char *s, int len)
280 {
281   ::memcpy (data, s, len);
282   data[len] = 0;
283   length = len;
284   hash = _Jv_hashUtf8String (s, len) & 0xFFFF;
285 }
286
287 _Jv_Utf8Const *
288 _Jv_makeUtf8Const (char* s, int len)
289 {
290   if (len < 0)
291     len = strlen (s);
292   Utf8Const* m
293     = (Utf8Const*) _Jv_AllocBytes (_Jv_Utf8Const::space_needed(s, len));
294   m->init(s, len);
295   return m;
296 }
297
298 _Jv_Utf8Const *
299 _Jv_makeUtf8Const (jstring string)
300 {
301   jint hash = string->hashCode ();
302   jint len = _Jv_GetStringUTFLength (string);
303
304   Utf8Const* m = (Utf8Const*)
305     _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
306
307   m->hash = hash;
308   m->length = len;
309
310   _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
311   m->data[len] = 0;
312   
313   return m;
314 }
315
316 \f
317
318 #ifdef DEBUG
319 void
320 _Jv_Abort (const char *function, const char *file, int line,
321            const char *message)
322 #else
323 void
324 _Jv_Abort (const char *, const char *, int, const char *message)
325 #endif
326 {
327 #ifdef DEBUG
328   fprintf (stderr,
329            "libgcj failure: %s\n   in function %s, file %s, line %d\n",
330            message, function, file, line);
331 #else
332   fprintf (stderr, "libgcj failure: %s\n", message);
333 #endif
334   abort ();
335 }
336
337 static void
338 fail_on_finalization (jobject)
339 {
340   JvFail ("object was finalized");
341 }
342
343 void
344 _Jv_GCWatch (jobject obj)
345 {
346   _Jv_RegisterFinalizer (obj, fail_on_finalization);
347 }
348
349 void
350 _Jv_ThrowBadArrayIndex(jint bad_index)
351 {
352   throw new java::lang::ArrayIndexOutOfBoundsException
353     (java::lang::String::valueOf (bad_index));
354 }
355
356 void
357 _Jv_ThrowNullPointerException ()
358 {
359   throw new java::lang::NullPointerException;
360 }
361
362 // Explicitly throw a no memory exception.
363 // The collector calls this when it encounters an out-of-memory condition.
364 void _Jv_ThrowNoMemory()
365 {
366   throw no_memory;
367 }
368
369 #ifdef ENABLE_JVMPI
370 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) \
371     if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false)) \
372       jvmpi_notify_alloc(klass,size,obj);
373 static void
374 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
375 {
376   // Service JVMPI allocation request.
377   JVMPI_Event event;
378
379   event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
380   event.env_id = NULL;
381   event.u.obj_alloc.arena_id = 0;
382   event.u.obj_alloc.class_id = (jobjectID) klass;
383   event.u.obj_alloc.is_array = 0;
384   event.u.obj_alloc.size = size;
385   event.u.obj_alloc.obj_id = (jobjectID) obj;
386
387   // FIXME:  This doesn't look right for the Boehm GC.  A GC may
388   // already be in progress.  _Jv_DisableGC () doesn't wait for it.
389   // More importantly, I don't see the need for disabling GC, since we
390   // blatantly have a pointer to obj on our stack, ensuring that the
391   // object can't be collected.  Even for a nonconservative collector,
392   // it appears to me that this must be true, since we are about to
393   // return obj. Isn't this whole approach way too intrusive for
394   // a useful profiling interface?                      - HB
395   _Jv_DisableGC ();
396   (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
397   _Jv_EnableGC ();
398 }
399 #else /* !ENABLE_JVMPI */
400 # define JVMPI_NOTIFY_ALLOC(klass,size,obj) /* do nothing */
401 #endif
402
403 // Allocate a new object of class KLASS.
404 // First a version that assumes that we have no finalizer, and that
405 // the class is already initialized.
406 // If we know that JVMPI is disabled, this can be replaced by a direct call
407 // to the allocator for the appropriate GC.
408 jobject
409 _Jv_AllocObjectNoInitNoFinalizer (jclass klass)
410 {
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 a version that initializes if necessary.
418 jobject
419 _Jv_AllocObjectNoFinalizer (jclass klass)
420 {
421   _Jv_InitClass (klass);
422   jint size = klass->size ();
423   jobject obj = (jobject) _Jv_AllocObj (size, klass);
424   JVMPI_NOTIFY_ALLOC (klass, size, obj);
425   return obj;
426 }
427
428 // And now the general version that registers a finalizer if necessary.
429 jobject
430 _Jv_AllocObject (jclass klass)
431 {
432   jobject obj = _Jv_AllocObjectNoFinalizer (klass);
433   
434   // We assume that the compiler only generates calls to this routine
435   // if there really is an interesting finalizer.
436   // Unfortunately, we still have to the dynamic test, since there may
437   // be cni calls to this routine.
438   // Note that on IA64 get_finalizer() returns the starting address of the
439   // function, not a function pointer.  Thus this still works.
440   if (klass->vtable->get_finalizer ()
441       != java::lang::Object::class$.vtable->get_finalizer ())
442     _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
443   return obj;
444 }
445
446 // Allocate a String, including variable length storage.
447 jstring
448 _Jv_AllocString(jsize len)
449 {
450   using namespace java::lang;
451
452   jsize sz = sizeof(java::lang::String) + len * sizeof(jchar);
453
454   // We assert that for strings allocated this way, the data field
455   // will always point to the object itself.  Thus there is no reason
456   // for the garbage collector to scan any of it.
457   // Furthermore, we're about to overwrite the string data, so
458   // initialization of the object is not an issue.
459
460   // String needs no initialization, and there is no finalizer, so
461   // we can go directly to the collector's allocator interface.
462   jstring obj = (jstring) _Jv_AllocPtrFreeObj(sz, &String::class$);
463
464   obj->data = obj;
465   obj->boffset = sizeof(java::lang::String);
466   obj->count = len;
467   obj->cachedHashCode = 0;
468
469   JVMPI_NOTIFY_ALLOC (&String::class$, sz, obj);
470   
471   return obj;
472 }
473
474 // A version of the above that assumes the object contains no pointers,
475 // and requires no finalization.  This can't happen if we need pointers
476 // to locks.
477 #ifdef JV_HASH_SYNCHRONIZATION
478 jobject
479 _Jv_AllocPtrFreeObject (jclass klass)
480 {
481   _Jv_InitClass (klass);
482   jint size = klass->size ();
483
484   jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
485
486   JVMPI_NOTIFY_ALLOC (klass, size, obj);
487
488   return obj;
489 }
490 #endif /* JV_HASH_SYNCHRONIZATION */
491
492
493 // Allocate a new array of Java objects.  Each object is of type
494 // `elementClass'.  `init' is used to initialize each slot in the
495 // array.
496 jobjectArray
497 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
498 {
499   if (__builtin_expect (count < 0, false))
500     throw new java::lang::NegativeArraySizeException;
501
502   JvAssert (! elementClass->isPrimitive ());
503
504   // Ensure that elements pointer is properly aligned.
505   jobjectArray obj = NULL;
506   size_t size = (size_t) elements (obj);
507   // Check for overflow.
508   if (__builtin_expect ((size_t) count > 
509                         (MAX_OBJECT_SIZE - 1 - size) / sizeof (jobject), false))
510     throw no_memory;
511
512   size += count * sizeof (jobject);
513
514   jclass klass = _Jv_GetArrayClass (elementClass,
515                                     elementClass->getClassLoaderInternal());
516
517   obj = (jobjectArray) _Jv_AllocArray (size, klass);
518   // Cast away const.
519   jsize *lp = const_cast<jsize *> (&obj->length);
520   *lp = count;
521   // We know the allocator returns zeroed memory.  So don't bother
522   // zeroing it again.
523   if (init)
524     {
525       jobject *ptr = elements(obj);
526       while (--count >= 0)
527         *ptr++ = init;
528     }
529   return obj;
530 }
531
532 // Allocate a new array of primitives.  ELTYPE is the type of the
533 // element, COUNT is the size of the array.
534 jobject
535 _Jv_NewPrimArray (jclass eltype, jint count)
536 {
537   int elsize = eltype->size();
538   if (__builtin_expect (count < 0, false))
539     throw new java::lang::NegativeArraySizeException;
540
541   JvAssert (eltype->isPrimitive ());
542   jobject dummy = NULL;
543   size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
544
545   // Check for overflow.
546   if (__builtin_expect ((size_t) count > 
547                         (MAX_OBJECT_SIZE - size) / elsize, false))
548     throw no_memory;
549
550   jclass klass = _Jv_GetArrayClass (eltype, 0);
551
552 # ifdef JV_HASH_SYNCHRONIZATION
553   // Since the vtable is always statically allocated,
554   // these are completely pointerfree!  Make sure the GC doesn't touch them.
555   __JArray *arr =
556     (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
557   memset((char *)arr + size, 0, elsize * count);
558 # else
559   __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
560   // Note that we assume we are given zeroed memory by the allocator.
561 # endif
562   // Cast away const.
563   jsize *lp = const_cast<jsize *> (&arr->length);
564   *lp = count;
565
566   return arr;
567 }
568
569 jobject
570 _Jv_NewArray (jint type, jint size)
571 {
572   switch (type)
573     {
574       case  4:  return JvNewBooleanArray (size);
575       case  5:  return JvNewCharArray (size);
576       case  6:  return JvNewFloatArray (size);
577       case  7:  return JvNewDoubleArray (size);
578       case  8:  return JvNewByteArray (size);
579       case  9:  return JvNewShortArray (size);
580       case 10:  return JvNewIntArray (size);
581       case 11:  return JvNewLongArray (size);
582     }
583   throw new java::lang::InternalError
584     (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
585 }
586
587 // Allocate a possibly multi-dimensional array but don't check that
588 // any array length is <0.
589 static jobject
590 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
591 {
592   JvAssert (type->isArray());
593   jclass element_type = type->getComponentType();
594   jobject result;
595   if (element_type->isPrimitive())
596     result = _Jv_NewPrimArray (element_type, sizes[0]);
597   else
598     result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
599
600   if (dimensions > 1)
601     {
602       JvAssert (! element_type->isPrimitive());
603       JvAssert (element_type->isArray());
604       jobject *contents = elements ((jobjectArray) result);
605       for (int i = 0; i < sizes[0]; ++i)
606         contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
607                                                   sizes + 1);
608     }
609
610   return result;
611 }
612
613 jobject
614 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
615 {
616   for (int i = 0; i < dimensions; ++i)
617     if (sizes[i] < 0)
618       throw new java::lang::NegativeArraySizeException;
619
620   return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
621 }
622
623 jobject
624 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
625 {
626   va_list args;
627   jint sizes[dimensions];
628   va_start (args, dimensions);
629   for (int i = 0; i < dimensions; ++i)
630     {
631       jint size = va_arg (args, jint);
632       if (size < 0)
633         throw new java::lang::NegativeArraySizeException;
634       sizes[i] = size;
635     }
636   va_end (args);
637
638   return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
639 }
640
641 \f
642
643 // Ensure 8-byte alignment, for hash synchronization.
644 #define DECLARE_PRIM_TYPE(NAME)                 \
645   java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
646
647 DECLARE_PRIM_TYPE(byte)
648 DECLARE_PRIM_TYPE(short)
649 DECLARE_PRIM_TYPE(int)
650 DECLARE_PRIM_TYPE(long)
651 DECLARE_PRIM_TYPE(boolean)
652 DECLARE_PRIM_TYPE(char)
653 DECLARE_PRIM_TYPE(float)
654 DECLARE_PRIM_TYPE(double)
655 DECLARE_PRIM_TYPE(void)
656
657 void
658 _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len)
659 {    
660   using namespace java::lang::reflect;
661
662   // We must set the vtable for the class; the Java constructor
663   // doesn't do this.
664   (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
665
666   // Initialize the fields we care about.  We do this in the same
667   // order they are declared in Class.h.
668   cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
669   cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
670   cl->method_count = sig;
671   cl->size_in_bytes = len;
672   cl->vtable = JV_PRIMITIVE_VTABLE;
673   cl->state = JV_STATE_DONE;
674   cl->depth = -1;
675 }
676
677 jclass
678 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader,
679                             char **endp)
680 {
681   // First count arrays.
682   int array_count = 0;
683   while (*sig == '[')
684     {
685       ++sig;
686       ++array_count;
687     }
688
689   jclass result = NULL;
690   switch (*sig)
691     {
692     case 'B':
693       result = JvPrimClass (byte);
694       break;
695     case 'S':
696       result = JvPrimClass (short);
697       break;
698     case 'I':
699       result = JvPrimClass (int);
700       break;
701     case 'J':
702       result = JvPrimClass (long);
703       break;
704     case 'Z':
705       result = JvPrimClass (boolean);
706       break;
707     case 'C':
708       result = JvPrimClass (char);
709       break;
710     case 'F':
711       result = JvPrimClass (float);
712       break;
713     case 'D':
714       result = JvPrimClass (double);
715       break;
716     case 'V':
717       result = JvPrimClass (void);
718       break;
719     case 'L':
720       {
721         char *save = ++sig;
722         while (*sig && *sig != ';')
723           ++sig;
724         // Do nothing if signature appears to be malformed.
725         if (*sig == ';')
726           {
727             _Jv_Utf8Const *name = _Jv_makeUtf8Const (save, sig - save);
728             result = _Jv_FindClass (name, loader);
729           }
730         break;
731       }
732     default:
733       // Do nothing -- bad signature.
734       break;
735     }
736
737   if (endp)
738     {
739       // Not really the "end", but the last valid character that we
740       // looked at.
741       *endp = sig;
742     }
743
744   if (! result)
745     return NULL;
746
747   // Find arrays.
748   while (array_count-- > 0)
749     result = _Jv_GetArrayClass (result, loader);
750   return result;
751 }
752
753 \f
754
755 JArray<jstring> *
756 JvConvertArgv (int argc, const char **argv)
757 {
758   if (argc < 0)
759     argc = 0;
760   jobjectArray ar = JvNewObjectArray(argc, &java::lang::String::class$, NULL);
761   jobject *ptr = elements(ar);
762   jbyteArray bytes = NULL;
763   for (int i = 0;  i < argc;  i++)
764     {
765       const char *arg = argv[i];
766       int len = strlen (arg);
767       if (bytes == NULL || bytes->length < len)
768         bytes = JvNewByteArray (len);
769       jbyte *bytePtr = elements (bytes);
770       // We assume jbyte == char.
771       memcpy (bytePtr, arg, len);
772
773       // Now convert using the default encoding.
774       *ptr++ = new java::lang::String (bytes, 0, len);
775     }
776   return (JArray<jstring>*) ar;
777 }
778
779 // FIXME: These variables are static so that they will be
780 // automatically scanned by the Boehm collector.  This is needed
781 // because with qthreads the collector won't scan the initial stack --
782 // it will only scan the qthreads stacks.
783
784 // Command line arguments.
785 static JArray<jstring> *arg_vec;
786
787 // The primary thread.
788 static java::lang::Thread *main_thread;
789
790 #ifndef DISABLE_GETENV_PROPERTIES
791
792 static char *
793 next_property_key (char *s, size_t *length)
794 {
795   size_t l = 0;
796
797   JvAssert (s);
798
799   // Skip over whitespace
800   while (isspace (*s))
801     s++;
802
803   // If we've reached the end, return NULL.  Also return NULL if for
804   // some reason we've come across a malformed property string.
805   if (*s == 0
806       || *s == ':'
807       || *s == '=')
808     return NULL;
809
810   // Determine the length of the property key.
811   while (s[l] != 0
812          && ! isspace (s[l])
813          && s[l] != ':'
814          && s[l] != '=')
815     {
816       if (s[l] == '\\'
817           && s[l+1] != 0)
818         l++;
819       l++;
820     }
821
822   *length = l;
823
824   return s;
825 }
826
827 static char *
828 next_property_value (char *s, size_t *length)
829 {
830   size_t l = 0;
831
832   JvAssert (s);
833
834   while (isspace (*s))
835     s++;
836
837   if (*s == ':'
838       || *s == '=')
839     s++;
840
841   while (isspace (*s))
842     s++;
843
844   // If we've reached the end, return NULL.
845   if (*s == 0)
846     return NULL;
847
848   // Determine the length of the property value.
849   while (s[l] != 0
850          && ! isspace (s[l])
851          && s[l] != ':'
852          && s[l] != '=')
853     {
854       if (s[l] == '\\'
855           && s[l+1] != 0)
856         l += 2;
857       else
858         l++;
859     }
860
861   *length = l;
862
863   return s;
864 }
865
866 static void
867 process_gcj_properties ()
868 {
869   char *props = getenv("GCJ_PROPERTIES");
870   char *p = props;
871   size_t length;
872   size_t property_count = 0;
873
874   if (NULL == props)
875     return;
876
877   // Whip through props quickly in order to count the number of
878   // property values.
879   while (p && (p = next_property_key (p, &length)))
880     {
881       // Skip to the end of the key
882       p += length;
883
884       p = next_property_value (p, &length);
885       if (p)
886         p += length;
887       
888       property_count++;
889     }
890
891   // Allocate an array of property value/key pairs.
892   _Jv_Environment_Properties = 
893     (property_pair *) malloc (sizeof(property_pair) 
894                               * (property_count + 1));
895
896   // Go through the properties again, initializing _Jv_Properties
897   // along the way.
898   p = props;
899   property_count = 0;
900   while (p && (p = next_property_key (p, &length)))
901     {
902       _Jv_Environment_Properties[property_count].key = p;
903       _Jv_Environment_Properties[property_count].key_length = length;
904
905       // Skip to the end of the key
906       p += length;
907
908       p = next_property_value (p, &length);
909       
910       _Jv_Environment_Properties[property_count].value = p;
911       _Jv_Environment_Properties[property_count].value_length = length;
912
913       if (p)
914         p += length;
915
916       property_count++;
917     }
918   memset ((void *) &_Jv_Environment_Properties[property_count], 
919           0, sizeof (property_pair));
920
921   // Null terminate the strings.
922   for (property_pair *prop = &_Jv_Environment_Properties[0];
923        prop->key != NULL;
924        prop++)
925     {
926       prop->key[prop->key_length] = 0;
927       prop->value[prop->value_length] = 0;
928     }
929 }
930 #endif // DISABLE_GETENV_PROPERTIES
931
932 namespace gcj
933 {
934   _Jv_Utf8Const *void_signature;
935   _Jv_Utf8Const *clinit_name;
936   _Jv_Utf8Const *init_name;
937   _Jv_Utf8Const *finit_name;
938   
939   bool runtimeInitialized = false;
940 }
941
942 // We accept all non-standard options accepted by Sun's java command,
943 // for compatibility with existing application launch scripts.
944 static jint
945 parse_x_arg (char* option_string)
946 {
947   if (strlen (option_string) <= 0)
948     return -1;
949
950   if (! strcmp (option_string, "int"))
951     {
952       // FIXME: this should cause the vm to never load shared objects
953     }
954   else if (! strcmp (option_string, "mixed"))
955     {
956       // FIXME: allow interpreted and native code
957     }
958   else if (! strcmp (option_string, "batch"))
959     {
960       // FIXME: disable background JIT'ing
961     }
962   else if (! strcmp (option_string, "debug"))
963     {
964       // FIXME: add JDWP/JVMDI support
965     }
966   else if (! strncmp (option_string, "bootclasspath:", 14))
967     {
968       // FIXME: add a parse_bootclasspath_arg function
969     }
970   else if (! strncmp (option_string, "bootclasspath/a:", 16))
971     {
972     }
973   else if (! strncmp (option_string, "bootclasspath/p:", 16))
974     {
975     }
976   else if (! strcmp (option_string, "check:jni"))
977     {
978       // FIXME: enable strict JNI checking
979     }
980   else if (! strcmp (option_string, "future"))
981     {
982       // FIXME: enable strict class file format checks
983     }
984   else if (! strcmp (option_string, "noclassgc"))
985     {
986       // FIXME: disable garbage collection for classes
987     }
988   else if (! strcmp (option_string, "incgc"))
989     {
990       // FIXME: incremental garbage collection
991     }
992   else if (! strncmp (option_string, "loggc:", 6))
993     {
994       if (option_string[6] == '\0')
995         {
996           fprintf (stderr,
997                    "libgcj: filename argument expected for loggc option\n");
998           return -1;
999         }
1000       // FIXME: set gc logging filename
1001     }
1002   else if (! strncmp (option_string, "ms", 2))
1003     {
1004       // FIXME: ignore this option until PR 20699 is fixed.
1005       // _Jv_SetInitialHeapSize (option_string + 2);
1006     }
1007   else if (! strncmp (option_string, "mx", 2))
1008     _Jv_SetMaximumHeapSize (option_string + 2);
1009   else if (! strcmp (option_string, "prof"))
1010     {
1011       // FIXME: enable profiling of program running in vm
1012     }
1013   else if (! strncmp (option_string, "runhprof:", 9))
1014     {
1015       // FIXME: enable specific type of vm profiling.  add a
1016       // parse_runhprof_arg function
1017     }
1018   else if (! strcmp (option_string, "rs"))
1019     {
1020       // FIXME: reduced system signal usage.  disable thread dumps,
1021       // only terminate in response to user-initiated calls,
1022       // e.g. System.exit()
1023     }
1024   else if (! strncmp (option_string, "ss", 2))
1025     {
1026       // FIXME: set thread stack size
1027     }
1028   else if (! strcmp (option_string, "X:+UseAltSigs"))
1029     {
1030       // FIXME: use signals other than SIGUSR1 and SIGUSR2
1031     }
1032   else if (! strcmp (option_string, "share:off"))
1033     {
1034       // FIXME: don't share class data
1035     }
1036   else if (! strcmp (option_string, "share:auto"))
1037     {
1038       // FIXME: share class data where possible
1039     }
1040   else if (! strcmp (option_string, "share:on"))
1041     {
1042       // FIXME: fail if impossible to share class data
1043     }
1044
1045   return 0;
1046 }
1047
1048 static jint
1049 parse_verbose_args (char* option_string,
1050                     bool ignore_unrecognized)
1051 {
1052   size_t len = sizeof ("-verbose");
1053
1054   if (strlen (option_string) < len)
1055     return -1;
1056
1057   if (option_string[len] == ':'
1058       && option_string[len + 1] != '\0')
1059     {
1060       char* verbose_args = option_string + len + 1;
1061       size_t last = 0;
1062
1063       do
1064         {
1065           if (! strncmp (verbose_args,
1066                          "gc", (last = sizeof ("gc")) - 1)
1067               && (verbose_args[last] == '\0'
1068                   || verbose_args[last] == ','))
1069             {
1070               // FIXME: we should add functions to boehm-gc that
1071               // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1072               // GC_print_back_height.
1073
1074             }
1075           else if (! strncmp (verbose_args,
1076                               "class",
1077                               (last = sizeof ("class")) - 1)
1078                    && (verbose_args[last] == '\0'
1079                        || verbose_args[last] == ','))
1080             {
1081               gcj::verbose_class_flag = true;
1082             }
1083           else if (! strncmp (verbose_args, "jni",
1084                               (last = sizeof ("jni")) - 1)
1085                    && (verbose_args[last] == '\0'
1086                        || verbose_args[last] == ','))
1087             {
1088               // FIXME: enable JNI messages.
1089             }
1090           else if (ignore_unrecognized
1091                    && verbose_args[0] == 'X')
1092             {
1093               // ignore unrecognized non-standard verbose option
1094               last = 0;
1095               while (verbose_args[last] != '\0'
1096                      && verbose_args[last++] != ',');
1097             }
1098
1099           if (strlen (verbose_args) >= last)
1100             {
1101               if (verbose_args[last] == ',')
1102                 {
1103                   if (verbose_args[last + 1] == '\0')
1104                     // trailing comma
1105                     return -1;
1106                   else
1107                     {
1108                       verbose_args = verbose_args + last + 1;
1109                       last = 0;
1110                     }
1111                 }
1112               // here verbose_args[last] is either '\0' or
1113               // the first character in the next verbose
1114               // argument.
1115             }
1116           else
1117             // partial option
1118             return -1;
1119
1120           // verbose_args[last] will be '\0' here if we're
1121           // done.
1122         }
1123       while (verbose_args[last] != '\0');
1124     }
1125   else if (option_string[len] == 'g'
1126            && option_string[len + 1] == 'c'
1127            && option_string[len + 2] == '\0')
1128     {
1129       // FIXME: we should add functions to boehm-gc that
1130       // toggle GC_print_stats, GC_PRINT_ADDRESS_MAP and
1131       // GC_print_back_height.
1132       return 0;
1133     }
1134   else if (option_string[len] == '\0')
1135     {
1136       gcj::verbose_class_flag = true;
1137       return 0;
1138     }
1139   else
1140     {
1141       // unrecognized option beginning with -verbose
1142       return -1;
1143     }
1144   return 0;
1145 }
1146
1147 static jint
1148 parse_init_args (JvVMInitArgs* vm_args)
1149 {
1150   // if _Jv_Compiler_Properties is non-NULL then it needs to be
1151   // re-allocated dynamically.
1152   if (_Jv_Compiler_Properties)
1153     {
1154       const char** props = _Jv_Compiler_Properties;
1155       _Jv_Compiler_Properties = NULL;
1156
1157       for (int i = 0; props[i]; i++)
1158         {
1159           _Jv_Compiler_Properties = (const char**) _Jv_Realloc
1160             (_Jv_Compiler_Properties,
1161              (_Jv_Properties_Count + 1) * sizeof (const char*));
1162           _Jv_Compiler_Properties[_Jv_Properties_Count++] = props[i];
1163         }
1164     }
1165
1166   if (vm_args == NULL)
1167     return 0;
1168
1169   for (int i = 0; i < vm_args->nOptions; ++i)
1170     {
1171       char* option_string = vm_args->options[i].optionString;
1172       if (! strcmp (option_string, "vfprintf")
1173           || ! strcmp (option_string, "exit")
1174           || ! strcmp (option_string, "abort"))
1175         {
1176           // FIXME: we are required to recognize these, but for
1177           // now we don't handle them in any way.
1178           continue;
1179         }
1180       else if (! strncmp (option_string,
1181                           "-verbose", sizeof ("-verbose") - 1))
1182         {
1183           jint result = parse_verbose_args (option_string,
1184                                             vm_args->ignoreUnrecognized);
1185           if (result < 0)
1186             return result;
1187         }
1188       else if (! strncmp (option_string, "-D", 2))
1189         {
1190           _Jv_Compiler_Properties = (const char**) _Jv_Realloc
1191             (_Jv_Compiler_Properties,
1192              (_Jv_Properties_Count + 1) * sizeof (char*));
1193
1194           _Jv_Compiler_Properties[_Jv_Properties_Count++] =
1195             strdup (option_string + 2);
1196
1197           continue;
1198         }
1199       else if (vm_args->ignoreUnrecognized)
1200         {
1201           if (option_string[0] == '_')
1202             parse_x_arg (option_string + 1);
1203           else if (! strncmp (option_string, "-X", 2))
1204             parse_x_arg (option_string + 2);
1205           else
1206             {
1207             unknown_option:
1208               fprintf (stderr, "libgcj: unknown option: %s\n", option_string);
1209               return -1;
1210             }
1211         }
1212       else
1213         goto unknown_option;
1214     }
1215   return 0;
1216 }
1217
1218 jint
1219 _Jv_CreateJavaVM (JvVMInitArgs* vm_args)
1220 {
1221   using namespace gcj;
1222
1223   if (runtimeInitialized)
1224     return -1;
1225
1226   runtimeInitialized = true;
1227
1228   jint result = parse_init_args (vm_args);
1229   if (result < 0)
1230     return -1;
1231
1232   PROCESS_GCJ_PROPERTIES;
1233
1234   /* Threads must be initialized before the GC, so that it inherits the
1235      signal mask.  */
1236   _Jv_InitThreads ();
1237   _Jv_InitGC ();
1238   _Jv_InitializeSyncMutex ();
1239   
1240 #ifdef INTERPRETER
1241   _Jv_InitInterpreter ();
1242 #endif  
1243
1244 #ifdef HANDLE_SEGV
1245   INIT_SEGV;
1246 #endif
1247
1248 #ifdef HANDLE_FPE
1249   INIT_FPE;
1250 #endif
1251
1252   /* Initialize Utf8 constants declared in jvm.h. */
1253   void_signature = _Jv_makeUtf8Const ("()V", 3);
1254   clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
1255   init_name = _Jv_makeUtf8Const ("<init>", 6);
1256   finit_name = _Jv_makeUtf8Const ("finit$", 6);
1257
1258   /* Initialize built-in classes to represent primitive TYPEs. */
1259   _Jv_InitPrimClass (&_Jv_byteClass,    "byte",    'B', 1);
1260   _Jv_InitPrimClass (&_Jv_shortClass,   "short",   'S', 2);
1261   _Jv_InitPrimClass (&_Jv_intClass,     "int",     'I', 4);
1262   _Jv_InitPrimClass (&_Jv_longClass,    "long",    'J', 8);
1263   _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1);
1264   _Jv_InitPrimClass (&_Jv_charClass,    "char",    'C', 2);
1265   _Jv_InitPrimClass (&_Jv_floatClass,   "float",   'F', 4);
1266   _Jv_InitPrimClass (&_Jv_doubleClass,  "double",  'D', 8);
1267   _Jv_InitPrimClass (&_Jv_voidClass,    "void",    'V', 0);
1268
1269   // Turn stack trace generation off while creating exception objects.
1270   _Jv_InitClass (&java::lang::VMThrowable::class$);
1271   java::lang::VMThrowable::trace_enabled = 0;
1272   
1273   // We have to initialize this fairly early, to avoid circular class
1274   // initialization.  In particular we want to start the
1275   // initialization of ClassLoader before we start the initialization
1276   // of VMClassLoader.
1277   _Jv_InitClass (&java::lang::ClassLoader::class$);
1278
1279   // Set up the system class loader and the bootstrap class loader.
1280   gnu::gcj::runtime::ExtensionClassLoader::initialize();
1281   java::lang::VMClassLoader::initialize(JvNewStringLatin1(TOOLEXECLIBDIR));
1282
1283   _Jv_RegisterBootstrapPackages();
1284
1285   no_memory = new java::lang::OutOfMemoryError;
1286
1287   java::lang::VMThrowable::trace_enabled = 1;
1288
1289 #ifdef USE_LTDL
1290   LTDL_SET_PRELOADED_SYMBOLS ();
1291 #endif
1292
1293   _Jv_platform_initialize ();
1294
1295   _Jv_JNI_Init ();
1296
1297   _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
1298
1299   // Start the GC finalizer thread.  A VirtualMachineError can be
1300   // thrown by the runtime if, say, threads aren't available.
1301   try
1302     {
1303       using namespace gnu::gcj::runtime;
1304       FinalizerThread *ft = new FinalizerThread ();
1305       ft->start ();
1306     }
1307   catch (java::lang::VirtualMachineError *ignore)
1308     {
1309     }
1310
1311   return 0;
1312 }
1313
1314 void
1315 _Jv_RunMain (JvVMInitArgs *vm_args, jclass klass, const char *name, int argc,
1316              const char **argv, bool is_jar)
1317 {
1318 #ifndef DISABLE_MAIN_ARGS
1319   _Jv_SetArgs (argc, argv);
1320 #endif
1321
1322   java::lang::Runtime *runtime = NULL;
1323
1324   try
1325     {
1326       if (_Jv_CreateJavaVM (vm_args) < 0)
1327         {
1328           fprintf (stderr, "libgcj: couldn't create virtual machine\n");
1329           exit (1);
1330         }
1331
1332       // Get the Runtime here.  We want to initialize it before searching
1333       // for `main'; that way it will be set up if `main' is a JNI method.
1334       runtime = java::lang::Runtime::getRuntime ();
1335
1336 #ifdef DISABLE_MAIN_ARGS
1337       arg_vec = JvConvertArgv (0, 0);
1338 #else      
1339       arg_vec = JvConvertArgv (argc - 1, argv + 1);
1340 #endif
1341
1342       using namespace gnu::java::lang;
1343       if (klass)
1344         main_thread = new MainThread (klass, arg_vec);
1345       else
1346         main_thread = new MainThread (JvNewStringLatin1 (name),
1347                                       arg_vec, is_jar);
1348     }
1349   catch (java::lang::Throwable *t)
1350     {
1351       java::lang::System::err->println (JvNewStringLatin1 
1352         ("Exception during runtime initialization"));
1353       t->printStackTrace();
1354       if (runtime)
1355         runtime->exit (1);
1356       // In case the runtime creation failed.
1357       ::exit (1);
1358     }
1359
1360   _Jv_AttachCurrentThread (main_thread);
1361   _Jv_ThreadRun (main_thread);
1362   _Jv_ThreadWait ();
1363
1364   int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1365   runtime->exit (status);
1366 }
1367
1368 void
1369 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv, 
1370              bool is_jar)
1371 {
1372   _Jv_RunMain (NULL, klass, name, argc, argv, is_jar);
1373 }
1374
1375 void
1376 JvRunMain (jclass klass, int argc, const char **argv)
1377 {
1378   _Jv_RunMain (klass, NULL, argc, argv, false);
1379 }
1380
1381 \f
1382
1383 // Parse a string and return a heap size.
1384 static size_t
1385 parse_heap_size (const char *spec)
1386 {
1387   char *end;
1388   unsigned long val = strtoul (spec, &end, 10);
1389   if (*end == 'k' || *end == 'K')
1390     val *= 1024;
1391   else if (*end == 'm' || *end == 'M')
1392     val *= 1048576;
1393   return (size_t) val;
1394 }
1395
1396 // Set the initial heap size.  This might be ignored by the GC layer.
1397 // This must be called before _Jv_RunMain.
1398 void
1399 _Jv_SetInitialHeapSize (const char *arg)
1400 {
1401   size_t size = parse_heap_size (arg);
1402   _Jv_GCSetInitialHeapSize (size);
1403 }
1404
1405 // Set the maximum heap size.  This might be ignored by the GC layer.
1406 // This must be called before _Jv_RunMain.
1407 void
1408 _Jv_SetMaximumHeapSize (const char *arg)
1409 {
1410   size_t size = parse_heap_size (arg);
1411   _Jv_GCSetMaximumHeapSize (size);
1412 }
1413
1414 \f
1415
1416 void *
1417 _Jv_Malloc (jsize size)
1418 {
1419   if (__builtin_expect (size == 0, false))
1420     size = 1;
1421   void *ptr = malloc ((size_t) size);
1422   if (__builtin_expect (ptr == NULL, false))
1423     throw no_memory;
1424   return ptr;
1425 }
1426
1427 void *
1428 _Jv_Realloc (void *ptr, jsize size)
1429 {
1430   if (__builtin_expect (size == 0, false))
1431     size = 1;
1432   ptr = realloc (ptr, (size_t) size);
1433   if (__builtin_expect (ptr == NULL, false))
1434     throw no_memory;
1435   return ptr;
1436 }
1437
1438 void *
1439 _Jv_MallocUnchecked (jsize size)
1440 {
1441   if (__builtin_expect (size == 0, false))
1442     size = 1;
1443   return malloc ((size_t) size);
1444 }
1445
1446 void
1447 _Jv_Free (void* ptr)
1448 {
1449   return free (ptr);
1450 }
1451
1452 \f
1453
1454 // In theory, these routines can be #ifdef'd away on machines which
1455 // support divide overflow signals.  However, we never know if some
1456 // code might have been compiled with "-fuse-divide-subroutine", so we
1457 // always include them in libgcj.
1458
1459 jint
1460 _Jv_divI (jint dividend, jint divisor)
1461 {
1462   if (__builtin_expect (divisor == 0, false))
1463     {
1464       java::lang::ArithmeticException *arithexception 
1465         = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));      
1466       throw arithexception;
1467     }
1468   
1469   if (dividend == (jint) 0x80000000L && divisor == -1)
1470     return dividend;
1471
1472   return dividend / divisor;
1473 }
1474
1475 jint
1476 _Jv_remI (jint dividend, jint divisor)
1477 {
1478   if (__builtin_expect (divisor == 0, false))
1479     {
1480       java::lang::ArithmeticException *arithexception 
1481         = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));      
1482       throw arithexception;
1483     }
1484   
1485   if (dividend == (jint) 0x80000000L && divisor == -1)
1486     return 0;
1487   
1488   return dividend % divisor;
1489 }
1490
1491 jlong
1492 _Jv_divJ (jlong dividend, jlong divisor)
1493 {
1494   if (__builtin_expect (divisor == 0, false))
1495     {
1496       java::lang::ArithmeticException *arithexception 
1497         = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));      
1498       throw arithexception;
1499     }
1500
1501   if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1502     return dividend;
1503
1504   return dividend / divisor;
1505 }
1506
1507 jlong
1508 _Jv_remJ (jlong dividend, jlong divisor)
1509 {
1510   if (__builtin_expect (divisor == 0, false))
1511     {
1512       java::lang::ArithmeticException *arithexception 
1513         = new java::lang::ArithmeticException (JvNewStringLatin1 ("/ by zero"));      
1514       throw arithexception;
1515     }
1516
1517   if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1518     return 0;
1519
1520   return dividend % divisor;
1521 }
1522
1523 \f
1524
1525 // Return true if SELF_KLASS can access a field or method in
1526 // OTHER_KLASS.  The field or method's access flags are specified in
1527 // FLAGS.
1528 jboolean
1529 _Jv_CheckAccess (jclass self_klass, jclass other_klass, jint flags)
1530 {
1531   using namespace java::lang::reflect;
1532   return ((self_klass == other_klass)
1533           || ((flags & Modifier::PUBLIC) != 0)
1534           || (((flags & Modifier::PROTECTED) != 0)
1535               && _Jv_IsAssignableFromSlow (other_klass, self_klass))
1536           || (((flags & Modifier::PRIVATE) == 0)
1537               && _Jv_ClassNameSamePackage (self_klass->name,
1538                                            other_klass->name)));
1539 }