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
18 #pragma implementation "gcj/array.h"
22 #include <java-signal.h>
23 #include <java-threads.h>
25 #include <java/lang/Class.h>
26 #include <java/lang/Runtime.h>
27 #include <java/lang/String.h>
28 #include <java/lang/Thread.h>
29 #include <java/lang/ThreadGroup.h>
30 #include <java/lang/FirstThread.h>
31 #include <java/lang/ArrayIndexOutOfBoundsException.h>
32 #include <java/lang/ArithmeticException.h>
33 #include <java/lang/ClassFormatError.h>
34 #include <java/lang/ClassCastException.h>
35 #include <java/lang/NegativeArraySizeException.h>
36 #include <java/lang/NullPointerException.h>
37 #include <java/lang/OutOfMemoryError.h>
38 #include <java/lang/ArrayStoreException.h>
39 #include <java/lang/System.h>
40 #include <java/lang/reflect/Modifier.h>
41 #include <java/io/PrintStream.h>
47 #define ObjectClass _CL_Q34java4lang6Object
48 extern java::lang::Class ObjectClass
;
50 // We allocate a single OutOfMemoryError exception which we keep
51 // around for use if we run out of memory.
52 static java::lang::OutOfMemoryError
*no_memory
;
54 // Largest representable size_t.
55 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
60 static java::lang::NullPointerException
*nullp
;
61 SIGNAL_HANDLER (catch_segv
)
68 static java::lang::ArithmeticException
*arithexception
;
71 SIGNAL_HANDLER (catch_fpe
)
73 #ifdef HANDLE_DIVIDE_OVERFLOW
74 HANDLE_DIVIDE_OVERFLOW
;
78 _Jv_Throw (arithexception
);
85 _Jv_equalUtf8Consts (Utf8Const
* a
, Utf8Const
*b
)
88 register _Jv_ushort
*aptr
, *bptr
;
91 if (a
->hash
!= b
->hash
)
96 aptr
= (_Jv_ushort
*)a
->data
;
97 bptr
= (_Jv_ushort
*)b
->data
;
100 if (*aptr
++ != *bptr
++)
105 /* True iff A is equal to STR.
106 HASH is STR->hashCode().
110 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
112 if (a
->hash
!= (_Jv_ushort
) hash
)
114 jint len
= str
->length();
116 jchar
*sptr
= _Jv_GetStringChars (str
);
117 register unsigned char* ptr
= (unsigned char*) a
->data
;
118 register unsigned char* limit
= ptr
+ a
->length
;
121 int ch
= UTF8_GET (ptr
, limit
);
130 /* Count the number of Unicode chars encoded in a given Ut8 string. */
132 _Jv_strLengthUtf8(char* str
, int len
)
134 register unsigned char* ptr
;
135 register unsigned char* limit
;
138 ptr
= (unsigned char*) str
;
141 for (; ptr
< limit
; str_length
++) {
142 if (UTF8_GET (ptr
, limit
) < 0) {
149 /* Calculate a hash value for a string encoded in Utf8 format.
150 * This returns the same hash value as specified or java.lang.String.hashCode.
153 hashUtf8String (char* str
, int len
)
155 register unsigned char* ptr
= (unsigned char*) str
;
156 register unsigned char* limit
= ptr
+ len
;
161 int ch
= UTF8_GET (ptr
, limit
);
162 /* Updated specification from
163 http://www.javasoft.com/docs/books/jls/clarify.html. */
164 hash
= (31 * hash
) + ch
;
170 _Jv_makeUtf8Const (char* s
, int len
)
174 Utf8Const
* m
= (Utf8Const
*) _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
177 memcpy (m
->data
, s
, len
);
180 m
->hash
= hashUtf8String (s
, len
) & 0xFFFF;
185 _Jv_makeUtf8Const (jstring string
)
187 jint hash
= string
->hashCode ();
188 jint len
= _Jv_GetStringUTFLength (string
);
190 Utf8Const
* m
= (Utf8Const
*)
191 _Jv_AllocBytesChecked (sizeof(Utf8Const
) + len
+ 1);
196 _Jv_GetStringUTFRegion (string
, 0, string
->length (), m
->data
);
206 _Jv_Abort (const char *function
, const char *file
, int line
,
210 _Jv_Abort (const char *, const char *, int, const char *message
)
215 "libgcj failure: %s\n in function %s, file %s, line %d\n",
216 message
, function
, file
, line
);
218 java::io::PrintStream
*err
= java::lang::System::err
;
219 err
->print(JvNewStringLatin1 ("libgcj failure: "));
220 err
->println(JvNewStringLatin1 (message
));
227 fail_on_finalization (jobject
)
229 JvFail ("object was finalized");
233 _Jv_GCWatch (jobject obj
)
235 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
239 _Jv_ThrowBadArrayIndex(jint bad_index
)
241 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
242 (java::lang::String::valueOf(bad_index
)));
246 _Jv_CheckCast (jclass c
, jobject obj
)
248 if (obj
!= NULL
&& ! c
->isAssignableFrom(obj
->getClass()))
249 JvThrow (new java::lang::ClassCastException
);
254 _Jv_CheckArrayStore (jobject arr
, jobject obj
)
258 JvAssert (arr
!= NULL
);
259 jclass arr_class
= arr
->getClass();
260 JvAssert (arr_class
->isArray());
261 jclass elt_class
= arr_class
->getComponentType();
262 jclass obj_class
= obj
->getClass();
263 if (! elt_class
->isAssignableFrom(obj_class
))
264 JvThrow (new java::lang::ArrayStoreException
);
270 // Allocate some unscanned memory and throw an exception if no memory.
272 _Jv_AllocBytesChecked (jsize size
)
274 void *r
= _Jv_AllocBytes (size
);
276 _Jv_Throw (no_memory
);
280 // Allocate a new object of class C. SIZE is the size of the object
281 // to allocate. You might think this is redundant, but it isn't; some
282 // classes, such as String, aren't of fixed size.
284 _Jv_AllocObject (jclass c
, jint size
)
288 jobject obj
= (jobject
) _Jv_AllocObj (size
);
291 *((_Jv_VTable
**) obj
) = c
->vtable
;
293 // If this class has inherited finalize from Object, then don't
294 // bother registering a finalizer. We know that finalize() is the
295 // very first method after the dummy entry. If this turns out to be
296 // unreliable, a more robust implementation can be written. Such an
297 // implementation would look for Object.finalize in Object's method
298 // table at startup, and then use that information to find the
299 // appropriate index in the method vector.
300 if (c
->vtable
->method
[1] != ObjectClass
.vtable
->method
[1])
301 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
306 // Allocate a new array of Java objects. Each object is of type
307 // `elementClass'. `init' is used to initialize each slot in the
310 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
313 JvThrow (new java::lang::NegativeArraySizeException
);
315 // Check for overflow.
316 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / sizeof (jobject
))
319 size_t size
= count
* sizeof (jobject
) + sizeof (__JArray
);
321 // FIXME: second argument should be "current loader" //
322 jclass clas
= _Jv_FindArrayClass (elementClass
, 0);
324 jobjectArray obj
= (jobjectArray
) _Jv_AllocArray (size
);
328 jobject
* ptr
= elements(obj
);
329 // We know the allocator returns zeroed memory. So don't bother
336 // Set the vtbl last to avoid problems if the GC happens during the
337 // window in this function between the allocation and this
339 *((_Jv_VTable
**) obj
) = clas
->vtable
;
343 // Allocate a new array of primitives. ELTYPE is the type of the
344 // element, COUNT is the size of the array.
346 _Jv_NewPrimArray (jclass eltype
, jint count
)
348 int elsize
= eltype
->size();
350 JvThrow (new java::lang::NegativeArraySizeException ());
352 // Check for overflow.
353 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / elsize
)
356 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (sizeof (__JArray
)
361 // Note that we assume we are given zeroed memory by the allocator.
363 jclass klass
= _Jv_FindArrayClass (eltype
, 0);
364 // Set the vtbl last to avoid problems if the GC happens during the
365 // window in this function between the allocation and this
367 *((_Jv_VTable
**) arr
) = klass
->vtable
;
372 JvNewCharArray (jint length
)
374 return (jcharArray
) _Jv_NewPrimArray (JvPrimClass (char), length
);
378 JvNewBooleanArray (jint length
)
380 return (jbooleanArray
) _Jv_NewPrimArray (JvPrimClass (boolean
), length
);
384 JvNewByteArray (jint length
)
386 return (jbyteArray
) _Jv_NewPrimArray (JvPrimClass (byte
), length
);
390 JvNewShortArray (jint length
)
392 return (jshortArray
) _Jv_NewPrimArray (JvPrimClass (short), length
);
396 JvNewIntArray (jint length
)
398 return (jintArray
) _Jv_NewPrimArray (JvPrimClass (int), length
);
402 JvNewLongArray (jint length
)
404 return (jlongArray
) _Jv_NewPrimArray (JvPrimClass (long), length
);
408 JvNewFloatArray (jint length
)
410 return (jfloatArray
) _Jv_NewPrimArray (JvPrimClass (float), length
);
414 JvNewDoubleArray (jint length
)
416 return (jdoubleArray
) _Jv_NewPrimArray (JvPrimClass (double), length
);
420 _Jv_NewArray (jint type
, jint size
)
424 case 4: return JvNewBooleanArray (size
);
425 case 5: return JvNewCharArray (size
);
426 case 6: return JvNewFloatArray (size
);
427 case 7: return JvNewDoubleArray (size
);
428 case 8: return JvNewByteArray (size
);
429 case 9: return JvNewShortArray (size
);
430 case 10: return JvNewIntArray (size
);
431 case 11: return JvNewLongArray (size
);
433 JvFail ("newarray - bad type code");
434 return NULL
; // Placate compiler.
438 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
440 JvAssert (type
->isArray());
441 jclass element_type
= type
->getComponentType();
443 if (element_type
->isPrimitive())
444 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
446 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
450 JvAssert (! element_type
->isPrimitive());
451 JvAssert (element_type
->isArray());
452 jobject
*contents
= elements ((jobjectArray
) result
);
453 for (int i
= 0; i
< sizes
[0]; ++i
)
454 contents
[i
] = _Jv_NewMultiArray (element_type
, dimensions
- 1,
462 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
465 jint sizes
[dimensions
];
466 va_start (args
, dimensions
);
467 for (int i
= 0; i
< dimensions
; ++i
)
469 jint size
= va_arg (args
, jint
);
474 return _Jv_NewMultiArray (array_type
, dimensions
, sizes
);
479 class _Jv_PrimClass
: public java::lang::Class
482 // FIXME: calling convention is weird. If we use the natural types
483 // then the compiler will complain because they aren't Java types.
484 _Jv_PrimClass (jobject cname
, jbyte sig
, jint len
)
486 using namespace java::lang::reflect
;
488 // We must initialize every field of the class. We do this in
489 // the same order they are declared in Class.h.
491 name
= _Jv_makeUtf8Const ((char *) cname
, -1);
492 accflags
= Modifier::PUBLIC
| Modifier::FINAL
;
495 constants
.tags
= NULL
;
496 constants
.data
= NULL
;
499 vtable_method_count
= 0;
503 static_field_count
= 0;
504 vtable
= JV_PRIMITIVE_VTABLE
;
513 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
514 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
516 DECLARE_PRIM_TYPE(byte
, 'B', 1);
517 DECLARE_PRIM_TYPE(short, 'S', 2);
518 DECLARE_PRIM_TYPE(int, 'I', 4);
519 DECLARE_PRIM_TYPE(long, 'J', 8);
520 DECLARE_PRIM_TYPE(boolean
, 'Z', 1);
521 DECLARE_PRIM_TYPE(char, 'C', 2);
522 DECLARE_PRIM_TYPE(float, 'F', 4);
523 DECLARE_PRIM_TYPE(double, 'D', 8);
524 DECLARE_PRIM_TYPE(void, 'V', 0);
527 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
532 return JvPrimClass (byte
);
534 return JvPrimClass (short);
536 return JvPrimClass (int);
538 return JvPrimClass (long);
540 return JvPrimClass (boolean
);
542 return JvPrimClass (char);
544 return JvPrimClass (float);
546 return JvPrimClass (double);
548 return JvPrimClass (void);
552 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
554 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
555 return _Jv_FindClass (name
, loader
);
559 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig
[1], loader
),
562 JvFail ("couldn't understand class signature");
563 return NULL
; // Placate compiler.
569 JvConvertArgv (int argc
, const char **argv
)
573 jobjectArray ar
= JvNewObjectArray(argc
, &StringClass
, NULL
);
574 jobject
* ptr
= elements(ar
);
575 for (int i
= 0; i
< argc
; i
++)
577 const char *arg
= argv
[i
];
578 // FIXME - should probably use JvNewStringUTF.
579 *ptr
++ = JvNewStringLatin1(arg
, strlen(arg
));
581 return (JArray
<jstring
>*) ar
;
584 // FIXME: These variables are static so that they will be
585 // automatically scanned by the Boehm collector. This is needed
586 // because with qthreads the collector won't scan the initial stack --
587 // it will only scan the qthreads stacks.
589 // Command line arguments.
590 static jobject arg_vec
;
592 // The primary threadgroup.
593 static java::lang::ThreadGroup
*main_group
;
595 // The primary thread.
596 static java::lang::Thread
*main_thread
;
599 JvRunMain (jclass klass
, int argc
, const char **argv
)
605 arithexception
= new java::lang::ArithmeticException
606 (JvNewStringLatin1 ("/ by zero"));
609 no_memory
= new java::lang::OutOfMemoryError
;
612 LTDL_SET_PRELOADED_SYMBOLS ();
615 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
616 main_group
= new java::lang::ThreadGroup (23);
617 main_thread
= new java::lang::FirstThread (main_group
, klass
, arg_vec
);
619 main_thread
->start();
622 java::lang::Runtime::getRuntime ()->exit (0);
626 _Jv_RunMain (const char *class_name
, int argc
, const char **argv
)
632 arithexception
= new java::lang::ArithmeticException
633 (JvNewStringLatin1 ("/ by zero"));
636 no_memory
= new java::lang::OutOfMemoryError
;
639 LTDL_SET_PRELOADED_SYMBOLS ();
642 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
643 main_group
= new java::lang::ThreadGroup (23);
644 main_thread
= new java::lang::FirstThread (main_group
,
645 JvNewStringLatin1 (class_name
),
647 main_thread
->start();
650 java::lang::Runtime::getRuntime ()->exit (0);
657 _Jv_Malloc (jsize size
)
661 void *ptr
= malloc ((size_t) size
);
675 // In theory, these routines can be #ifdef'd away on machines which
676 // support divide overflow signals. However, we never know if some
677 // code might have been compiled with "-fuse-divide-subroutine", so we
678 // always include them in libgcj.
681 _Jv_divI (jint dividend
, jint divisor
)
684 _Jv_Throw (arithexception
);
686 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
689 return dividend
/ divisor
;
693 _Jv_remI (jint dividend
, jint divisor
)
696 _Jv_Throw (arithexception
);
698 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
701 return dividend
% divisor
;
705 _Jv_divJ (jlong dividend
, jlong divisor
)
708 _Jv_Throw (arithexception
);
710 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
713 return dividend
/ divisor
;
717 _Jv_remJ (jlong dividend
, jlong divisor
)
720 _Jv_Throw (arithexception
);
722 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
725 return dividend
% divisor
;