1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
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
19 #pragma implementation "gcj/array.h"
23 #include <java-signal.h>
24 #include <java-threads.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/Runtime.h>
28 #include <java/lang/String.h>
29 #include <java/lang/Thread.h>
30 #include <java/lang/ThreadGroup.h>
31 #include <java/lang/FirstThread.h>
32 #include <java/lang/ArrayIndexOutOfBoundsException.h>
33 #include <java/lang/ArithmeticException.h>
34 #include <java/lang/ClassFormatError.h>
35 #include <java/lang/ClassCastException.h>
36 #include <java/lang/NegativeArraySizeException.h>
37 #include <java/lang/NullPointerException.h>
38 #include <java/lang/OutOfMemoryError.h>
39 #include <java/lang/ArrayStoreException.h>
40 #include <java/lang/System.h>
41 #include <java/lang/reflect/Modifier.h>
42 #include <java/io/PrintStream.h>
48 #define ObjectClass _CL_Q34java4lang6Object
49 extern java::lang::Class ObjectClass
;
51 // We allocate a single OutOfMemoryError exception which we keep
52 // around for use if we run out of memory.
53 static java::lang::OutOfMemoryError
*no_memory
;
55 // Largest representable size_t.
56 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
61 static java::lang::NullPointerException
*nullp
;
62 SIGNAL_HANDLER (catch_segv
)
69 static java::lang::ArithmeticException
*arithexception
;
72 SIGNAL_HANDLER (catch_fpe
)
74 #ifdef HANDLE_DIVIDE_OVERFLOW
75 HANDLE_DIVIDE_OVERFLOW
;
79 _Jv_Throw (arithexception
);
86 _Jv_equalUtf8Consts (Utf8Const
* a
, Utf8Const
*b
)
89 register _Jv_ushort
*aptr
, *bptr
;
92 if (a
->hash
!= b
->hash
)
97 aptr
= (_Jv_ushort
*)a
->data
;
98 bptr
= (_Jv_ushort
*)b
->data
;
101 if (*aptr
++ != *bptr
++)
106 /* True iff A is equal to STR.
107 HASH is STR->hashCode().
111 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
113 if (a
->hash
!= (_Jv_ushort
) hash
)
115 jint len
= str
->length();
117 jchar
*sptr
= _Jv_GetStringChars (str
);
118 register unsigned char* ptr
= (unsigned char*) a
->data
;
119 register unsigned char* limit
= ptr
+ a
->length
;
122 int ch
= UTF8_GET (ptr
, limit
);
131 /* Count the number of Unicode chars encoded in a given Ut8 string. */
133 _Jv_strLengthUtf8(char* str
, int len
)
135 register unsigned char* ptr
;
136 register unsigned char* limit
;
139 ptr
= (unsigned char*) str
;
142 for (; ptr
< limit
; str_length
++) {
143 if (UTF8_GET (ptr
, limit
) < 0) {
150 /* Calculate a hash value for a string encoded in Utf8 format.
151 * This returns the same hash value as specified or java.lang.String.hashCode.
154 hashUtf8String (char* str
, int len
)
156 register unsigned char* ptr
= (unsigned char*) str
;
157 register unsigned char* limit
= ptr
+ len
;
162 int ch
= UTF8_GET (ptr
, limit
);
163 /* Updated specification from
164 http://www.javasoft.com/docs/books/jls/clarify.html. */
165 hash
= (31 * hash
) + ch
;
171 _Jv_makeUtf8Const (char* s
, int len
)
175 Utf8Const
* m
= (Utf8Const
*) _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
178 memcpy (m
->data
, s
, len
);
181 m
->hash
= hashUtf8String (s
, len
) & 0xFFFF;
186 _Jv_makeUtf8Const (jstring string
)
188 jint hash
= string
->hashCode ();
189 jint len
= _Jv_GetStringUTFLength (string
);
191 Utf8Const
* m
= (Utf8Const
*)
192 _Jv_AllocBytesChecked (sizeof(Utf8Const
) + len
+ 1);
197 _Jv_GetStringUTFRegion (string
, 0, string
->length (), m
->data
);
207 _Jv_Abort (const char *function
, const char *file
, int line
,
211 _Jv_Abort (const char *, const char *, int, const char *message
)
216 "libgcj failure: %s\n in function %s, file %s, line %d\n",
217 message
, function
, file
, line
);
219 java::io::PrintStream
*err
= java::lang::System::err
;
220 err
->print(JvNewStringLatin1 ("libgcj failure: "));
221 err
->println(JvNewStringLatin1 (message
));
228 fail_on_finalization (jobject
)
230 JvFail ("object was finalized");
234 _Jv_GCWatch (jobject obj
)
236 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
240 _Jv_ThrowBadArrayIndex(jint bad_index
)
242 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
243 (java::lang::String::valueOf(bad_index
)));
247 _Jv_CheckCast (jclass c
, jobject obj
)
249 if (obj
!= NULL
&& ! c
->isAssignableFrom(obj
->getClass()))
250 JvThrow (new java::lang::ClassCastException
);
255 _Jv_CheckArrayStore (jobject arr
, jobject obj
)
259 JvAssert (arr
!= NULL
);
260 jclass arr_class
= arr
->getClass();
261 JvAssert (arr_class
->isArray());
262 jclass elt_class
= arr_class
->getComponentType();
263 jclass obj_class
= obj
->getClass();
264 if (! elt_class
->isAssignableFrom(obj_class
))
265 JvThrow (new java::lang::ArrayStoreException
);
271 // Allocate some unscanned memory and throw an exception if no memory.
273 _Jv_AllocBytesChecked (jsize size
)
275 void *r
= _Jv_AllocBytes (size
);
277 _Jv_Throw (no_memory
);
281 // Allocate a new object of class C. SIZE is the size of the object
282 // to allocate. You might think this is redundant, but it isn't; some
283 // classes, such as String, aren't of fixed size.
285 _Jv_AllocObject (jclass c
, jint size
)
289 jobject obj
= (jobject
) _Jv_AllocObj (size
);
292 *((_Jv_VTable
**) obj
) = c
->vtable
;
294 // If this class has inherited finalize from Object, then don't
295 // bother registering a finalizer. We know that finalize() is the
296 // very first method after the dummy entry. If this turns out to be
297 // unreliable, a more robust implementation can be written. Such an
298 // implementation would look for Object.finalize in Object's method
299 // table at startup, and then use that information to find the
300 // appropriate index in the method vector.
301 if (c
->vtable
->method
[1] != ObjectClass
.vtable
->method
[1])
302 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
307 // Allocate a new array of Java objects. Each object is of type
308 // `elementClass'. `init' is used to initialize each slot in the
311 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
314 JvThrow (new java::lang::NegativeArraySizeException
);
316 // Check for overflow.
317 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / sizeof (jobject
))
320 size_t size
= count
* sizeof (jobject
) + sizeof (__JArray
);
322 // FIXME: second argument should be "current loader" //
323 jclass clas
= _Jv_FindArrayClass (elementClass
, 0);
325 jobjectArray obj
= (jobjectArray
) _Jv_AllocArray (size
);
329 jobject
* ptr
= elements(obj
);
330 // We know the allocator returns zeroed memory. So don't bother
337 // Set the vtbl last to avoid problems if the GC happens during the
338 // window in this function between the allocation and this
340 *((_Jv_VTable
**) obj
) = clas
->vtable
;
344 // Allocate a new array of primitives. ELTYPE is the type of the
345 // element, COUNT is the size of the array.
347 _Jv_NewPrimArray (jclass eltype
, jint count
)
349 int elsize
= eltype
->size();
351 JvThrow (new java::lang::NegativeArraySizeException ());
353 // Check for overflow.
354 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / elsize
)
357 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (sizeof (__JArray
)
362 // Note that we assume we are given zeroed memory by the allocator.
364 jclass klass
= _Jv_FindArrayClass (eltype
, 0);
365 // Set the vtbl last to avoid problems if the GC happens during the
366 // window in this function between the allocation and this
368 *((_Jv_VTable
**) arr
) = klass
->vtable
;
373 JvNewCharArray (jint length
)
375 return (jcharArray
) _Jv_NewPrimArray (JvPrimClass (char), length
);
379 JvNewBooleanArray (jint length
)
381 return (jbooleanArray
) _Jv_NewPrimArray (JvPrimClass (boolean
), length
);
385 JvNewByteArray (jint length
)
387 return (jbyteArray
) _Jv_NewPrimArray (JvPrimClass (byte
), length
);
391 JvNewShortArray (jint length
)
393 return (jshortArray
) _Jv_NewPrimArray (JvPrimClass (short), length
);
397 JvNewIntArray (jint length
)
399 return (jintArray
) _Jv_NewPrimArray (JvPrimClass (int), length
);
403 JvNewLongArray (jint length
)
405 return (jlongArray
) _Jv_NewPrimArray (JvPrimClass (long), length
);
409 JvNewFloatArray (jint length
)
411 return (jfloatArray
) _Jv_NewPrimArray (JvPrimClass (float), length
);
415 JvNewDoubleArray (jint length
)
417 return (jdoubleArray
) _Jv_NewPrimArray (JvPrimClass (double), length
);
421 _Jv_NewArray (jint type
, jint size
)
425 case 4: return JvNewBooleanArray (size
);
426 case 5: return JvNewCharArray (size
);
427 case 6: return JvNewFloatArray (size
);
428 case 7: return JvNewDoubleArray (size
);
429 case 8: return JvNewByteArray (size
);
430 case 9: return JvNewShortArray (size
);
431 case 10: return JvNewIntArray (size
);
432 case 11: return JvNewLongArray (size
);
434 JvFail ("newarray - bad type code");
435 return NULL
; // Placate compiler.
439 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
441 JvAssert (type
->isArray());
442 jclass element_type
= type
->getComponentType();
444 if (element_type
->isPrimitive())
445 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
447 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
451 JvAssert (! element_type
->isPrimitive());
452 JvAssert (element_type
->isArray());
453 jobject
*contents
= elements ((jobjectArray
) result
);
454 for (int i
= 0; i
< sizes
[0]; ++i
)
455 contents
[i
] = _Jv_NewMultiArray (element_type
, dimensions
- 1,
463 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
466 jint sizes
[dimensions
];
467 va_start (args
, dimensions
);
468 for (int i
= 0; i
< dimensions
; ++i
)
470 jint size
= va_arg (args
, jint
);
475 return _Jv_NewMultiArray (array_type
, dimensions
, sizes
);
480 class _Jv_PrimClass
: public java::lang::Class
483 // FIXME: calling convention is weird. If we use the natural types
484 // then the compiler will complain because they aren't Java types.
485 _Jv_PrimClass (jobject cname
, jbyte sig
, jint len
)
487 using namespace java::lang::reflect
;
489 // We must initialize every field of the class. We do this in
490 // the same order they are declared in Class.h.
492 name
= _Jv_makeUtf8Const ((char *) cname
, -1);
493 accflags
= Modifier::PUBLIC
| Modifier::FINAL
;
496 constants
.tags
= NULL
;
497 constants
.data
= NULL
;
500 vtable_method_count
= 0;
504 static_field_count
= 0;
505 vtable
= JV_PRIMITIVE_VTABLE
;
514 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
515 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
517 DECLARE_PRIM_TYPE(byte
, 'B', 1);
518 DECLARE_PRIM_TYPE(short, 'S', 2);
519 DECLARE_PRIM_TYPE(int, 'I', 4);
520 DECLARE_PRIM_TYPE(long, 'J', 8);
521 DECLARE_PRIM_TYPE(boolean
, 'Z', 1);
522 DECLARE_PRIM_TYPE(char, 'C', 2);
523 DECLARE_PRIM_TYPE(float, 'F', 4);
524 DECLARE_PRIM_TYPE(double, 'D', 8);
525 DECLARE_PRIM_TYPE(void, 'V', 0);
528 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
533 return JvPrimClass (byte
);
535 return JvPrimClass (short);
537 return JvPrimClass (int);
539 return JvPrimClass (long);
541 return JvPrimClass (boolean
);
543 return JvPrimClass (char);
545 return JvPrimClass (float);
547 return JvPrimClass (double);
549 return JvPrimClass (void);
553 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
555 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
556 return _Jv_FindClass (name
, loader
);
560 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig
[1], loader
),
563 JvFail ("couldn't understand class signature");
564 return NULL
; // Placate compiler.
570 JvConvertArgv (int argc
, const char **argv
)
574 jobjectArray ar
= JvNewObjectArray(argc
, &StringClass
, NULL
);
575 jobject
* ptr
= elements(ar
);
576 for (int i
= 0; i
< argc
; i
++)
578 const char *arg
= argv
[i
];
579 // FIXME - should probably use JvNewStringUTF.
580 *ptr
++ = JvNewStringLatin1(arg
, strlen(arg
));
582 return (JArray
<jstring
>*) ar
;
585 // FIXME: These variables are static so that they will be
586 // automatically scanned by the Boehm collector. This is needed
587 // because with qthreads the collector won't scan the initial stack --
588 // it will only scan the qthreads stacks.
590 // Command line arguments.
591 static jobject arg_vec
;
593 // The primary threadgroup.
594 static java::lang::ThreadGroup
*main_group
;
596 // The primary thread.
597 static java::lang::Thread
*main_thread
;
606 arithexception
= new java::lang::ArithmeticException
607 (JvNewStringLatin1 ("/ by zero"));
610 no_memory
= new java::lang::OutOfMemoryError
;
613 LTDL_SET_PRELOADED_SYMBOLS ();
616 // FIXME: we only want this on POSIX systems.
617 struct sigaction act
;
618 act
.sa_handler
= SIG_IGN
;
619 sigemptyset (&act
.sa_mask
);
621 sigaction (SIGPIPE
, &act
, NULL
);
625 JvRunMain (jclass klass
, int argc
, const char **argv
)
629 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
630 main_group
= new java::lang::ThreadGroup (23);
631 main_thread
= new java::lang::FirstThread (main_group
, klass
, arg_vec
);
633 main_thread
->start();
636 java::lang::Runtime::getRuntime ()->exit (0);
640 _Jv_RunMain (const char *class_name
, int argc
, const char **argv
)
644 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
645 main_group
= new java::lang::ThreadGroup (23);
646 main_thread
= new java::lang::FirstThread (main_group
,
647 JvNewStringLatin1 (class_name
),
649 main_thread
->start();
652 java::lang::Runtime::getRuntime ()->exit (0);
659 _Jv_Malloc (jsize size
)
663 void *ptr
= malloc ((size_t) size
);
677 // In theory, these routines can be #ifdef'd away on machines which
678 // support divide overflow signals. However, we never know if some
679 // code might have been compiled with "-fuse-divide-subroutine", so we
680 // always include them in libgcj.
683 _Jv_divI (jint dividend
, jint divisor
)
686 _Jv_Throw (arithexception
);
688 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
691 return dividend
/ divisor
;
695 _Jv_remI (jint dividend
, jint divisor
)
698 _Jv_Throw (arithexception
);
700 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
703 return dividend
% divisor
;
707 _Jv_divJ (jlong dividend
, jlong divisor
)
710 _Jv_Throw (arithexception
);
712 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
715 return dividend
/ divisor
;
719 _Jv_remJ (jlong dividend
, jlong divisor
)
722 _Jv_Throw (arithexception
);
724 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
727 return dividend
% divisor
;