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 "java-array.h"
22 #include <java-signal.h>
24 #include <java/lang/Class.h>
25 #include <java/lang/Runtime.h>
26 #include <java/lang/String.h>
27 #include <java/lang/Thread.h>
28 #include <java/lang/ThreadGroup.h>
29 #include <java/lang/FirstThread.h>
30 #include <java/lang/ArrayIndexOutOfBoundsException.h>
31 #include <java/lang/ArithmeticException.h>
32 #include <java/lang/ClassFormatError.h>
33 #include <java/lang/ClassCastException.h>
34 #include <java/lang/NegativeArraySizeException.h>
35 #include <java/lang/NullPointerException.h>
36 #include <java/lang/OutOfMemoryError.h>
37 #include <java/lang/ArrayStoreException.h>
38 #include <java/lang/System.h>
39 #include <java/lang/reflect/Modifier.h>
40 #include <java/io/PrintStream.h>
46 #define ObjectClass _CL_Q34java4lang6Object
47 extern java::lang::Class ObjectClass
;
49 // We allocate a single OutOfMemoryError exception which we keep
50 // around for use if we run out of memory.
51 static java::lang::OutOfMemoryError
*no_memory
;
53 // Largest representable size_t.
54 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
59 static java::lang::NullPointerException
*nullp
;
60 SIGNAL_HANDLER (catch_segv
)
67 static java::lang::ArithmeticException
*arithexception
;
70 SIGNAL_HANDLER (catch_fpe
)
72 #ifdef HANDLE_DIVIDE_OVERFLOW
73 HANDLE_DIVIDE_OVERFLOW
;
77 _Jv_Throw (arithexception
);
84 _Jv_equalUtf8Consts (Utf8Const
* a
, Utf8Const
*b
)
87 register _Jv_ushort
*aptr
, *bptr
;
90 if (a
->hash
!= b
->hash
)
95 aptr
= (_Jv_ushort
*)a
->data
;
96 bptr
= (_Jv_ushort
*)b
->data
;
99 if (*aptr
++ != *bptr
++)
104 /* True iff A is equal to STR.
105 HASH is STR->hashCode().
109 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
111 if (a
->hash
!= (_Jv_ushort
) hash
)
113 jint len
= str
->length();
115 jchar
*sptr
= _Jv_GetStringChars (str
);
116 register unsigned char* ptr
= (unsigned char*) a
->data
;
117 register unsigned char* limit
= ptr
+ a
->length
;
120 int ch
= UTF8_GET (ptr
, limit
);
129 /* Count the number of Unicode chars encoded in a given Ut8 string. */
131 _Jv_strLengthUtf8(char* str
, int len
)
133 register unsigned char* ptr
;
134 register unsigned char* limit
;
137 ptr
= (unsigned char*) str
;
140 for (; ptr
< limit
; str_length
++) {
141 if (UTF8_GET (ptr
, limit
) < 0) {
148 /* Calculate a hash value for a string encoded in Utf8 format.
149 * This returns the same hash value as specified or java.lang.String.hashCode.
152 hashUtf8String (char* str
, int len
)
154 register unsigned char* ptr
= (unsigned char*) str
;
155 register unsigned char* limit
= ptr
+ len
;
160 int ch
= UTF8_GET (ptr
, limit
);
161 /* Updated specification from
162 http://www.javasoft.com/docs/books/jls/clarify.html. */
163 hash
= (31 * hash
) + ch
;
169 _Jv_makeUtf8Const (char* s
, int len
)
173 Utf8Const
* m
= (Utf8Const
*) _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
176 memcpy (m
->data
, s
, len
);
179 m
->hash
= hashUtf8String (s
, len
) & 0xFFFF;
187 _Jv_Abort (const char *function
, const char *file
, int line
,
191 _Jv_Abort (const char *, const char *, int, const char *message
)
196 "libgcj failure: %s\n in function %s, file %s, line %d\n",
197 message
, function
, file
, line
);
199 java::io::PrintStream
*err
= java::lang::System::err
;
200 err
->print(JvNewStringLatin1 ("libgcj failure: "));
201 err
->println(JvNewStringLatin1 (message
));
208 fail_on_finalization (jobject
)
210 JvFail ("object was finalized");
214 _Jv_GCWatch (jobject obj
)
216 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
220 _Jv_ThrowBadArrayIndex(jint bad_index
)
222 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
223 (java::lang::String::valueOf(bad_index
)));
227 _Jv_CheckCast (jclass c
, jobject obj
)
229 if (obj
!= NULL
&& ! c
->isAssignableFrom(obj
->getClass()))
230 JvThrow (new java::lang::ClassCastException
);
235 _Jv_CheckArrayStore (jobject arr
, jobject obj
)
239 JvAssert (arr
!= NULL
);
240 jclass arr_class
= arr
->getClass();
241 JvAssert (arr_class
->isArray());
242 jclass elt_class
= arr_class
->getComponentType();
243 jclass obj_class
= obj
->getClass();
244 if (! elt_class
->isAssignableFrom(obj_class
))
245 JvThrow (new java::lang::ArrayStoreException
);
251 // Allocate some unscanned memory and throw an exception if no memory.
253 _Jv_AllocBytesChecked (jsize size
)
255 void *r
= _Jv_AllocBytes (size
);
257 _Jv_Throw (no_memory
);
261 // Allocate a new object of class C. SIZE is the size of the object
262 // to allocate. You might think this is redundant, but it isn't; some
263 // classes, such as String, aren't of fixed size.
265 _Jv_AllocObject (jclass c
, jint size
)
269 jobject obj
= (jobject
) _Jv_AllocObj (size
);
272 *((_Jv_VTable
**) obj
) = c
->vtable
;
274 // If this class has inherited finalize from Object, then don't
275 // bother registering a finalizer. We know that finalize() is the
276 // very first method after the dummy entry. If this turns out to be
277 // unreliable, a more robust implementation can be written. Such an
278 // implementation would look for Object.finalize in Object's method
279 // table at startup, and then use that information to find the
280 // appropriate index in the method vector.
281 if (c
->vtable
->method
[1] != ObjectClass
.vtable
->method
[1])
282 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
287 // Allocate a new array of Java objects. Each object is of type
288 // `elementClass'. `init' is used to initialize each slot in the
291 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
294 JvThrow (new java::lang::NegativeArraySizeException
);
296 // Check for overflow.
297 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / sizeof (jobject
))
300 size_t size
= count
* sizeof (jobject
) + sizeof (__JArray
);
301 jclass clas
= _Jv_FindArrayClass (elementClass
);
302 jobjectArray obj
= (jobjectArray
) _Jv_AllocArray (size
);
306 jobject
* ptr
= elements(obj
);
307 // We know the allocator returns zeroed memory. So don't bother
314 // Set the vtbl last to avoid problems if the GC happens during the
315 // window in this function between the allocation and this
317 *((_Jv_VTable
**) obj
) = clas
->vtable
;
321 // Allocate a new array of primitives. ELTYPE is the type of the
322 // element, COUNT is the size of the array.
324 _Jv_NewPrimArray (jclass eltype
, jint count
)
326 int elsize
= eltype
->size();
328 JvThrow (new java::lang::NegativeArraySizeException ());
330 // Check for overflow.
331 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / elsize
)
334 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (sizeof (__JArray
)
339 // Note that we assume we are given zeroed memory by the allocator.
341 jclass klass
= _Jv_FindArrayClass (eltype
);
342 // Set the vtbl last to avoid problems if the GC happens during the
343 // window in this function between the allocation and this
345 *((_Jv_VTable
**) arr
) = klass
->vtable
;
350 JvNewCharArray (jint length
)
352 return (jcharArray
) _Jv_NewPrimArray (JvPrimClass (char), length
);
356 JvNewBooleanArray (jint length
)
358 return (jbooleanArray
) _Jv_NewPrimArray (JvPrimClass (boolean
), length
);
362 JvNewByteArray (jint length
)
364 return (jbyteArray
) _Jv_NewPrimArray (JvPrimClass (byte
), length
);
368 JvNewShortArray (jint length
)
370 return (jshortArray
) _Jv_NewPrimArray (JvPrimClass (short), length
);
374 JvNewIntArray (jint length
)
376 return (jintArray
) _Jv_NewPrimArray (JvPrimClass (int), length
);
380 JvNewLongArray (jint length
)
382 return (jlongArray
) _Jv_NewPrimArray (JvPrimClass (long), length
);
386 JvNewFloatArray (jint length
)
388 return (jfloatArray
) _Jv_NewPrimArray (JvPrimClass (float), length
);
392 JvNewDoubleArray (jint length
)
394 return (jdoubleArray
) _Jv_NewPrimArray (JvPrimClass (double), length
);
398 _Jv_NewArray (jint type
, jint size
)
402 case 4: return JvNewBooleanArray (size
);
403 case 5: return JvNewCharArray (size
);
404 case 6: return JvNewFloatArray (size
);
405 case 7: return JvNewDoubleArray (size
);
406 case 8: return JvNewByteArray (size
);
407 case 9: return JvNewShortArray (size
);
408 case 10: return JvNewIntArray (size
);
409 case 11: return JvNewLongArray (size
);
411 JvFail ("newarray - bad type code");
412 return NULL
; // Placate compiler.
416 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
418 JvAssert (type
->isArray());
419 jclass element_type
= type
->getComponentType();
421 if (element_type
->isPrimitive())
422 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
424 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
428 JvAssert (! element_type
->isPrimitive());
429 JvAssert (element_type
->isArray());
430 jobject
*contents
= elements ((jobjectArray
) result
);
431 for (int i
= 0; i
< sizes
[0]; ++i
)
432 contents
[i
] = _Jv_NewMultiArray (element_type
, dimensions
- 1,
440 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
443 jint sizes
[dimensions
];
444 va_start (args
, dimensions
);
445 for (int i
= 0; i
< dimensions
; ++i
)
447 jint size
= va_arg (args
, jint
);
452 return _Jv_NewMultiArray (array_type
, dimensions
, sizes
);
457 class _Jv_PrimClass
: public java::lang::Class
460 // FIXME: calling convention is weird. If we use the natural types
461 // then the compiler will complain because they aren't Java types.
462 _Jv_PrimClass (jobject cname
, jbyte sig
, jint len
)
464 using namespace java::lang::reflect
;
466 // We must initialize every field of the class. We do this in
467 // the same order they are declared in Class.h.
469 name
= _Jv_makeUtf8Const ((char *) cname
, -1);
470 accflags
= Modifier::PUBLIC
| Modifier::FINAL
;
473 constants
.tags
= NULL
;
474 constants
.data
= NULL
;
477 vtable_method_count
= 0;
481 static_field_count
= 0;
482 vtable
= JV_PRIMITIVE_VTABLE
;
491 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
492 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
494 DECLARE_PRIM_TYPE(byte
, 'B', 1);
495 DECLARE_PRIM_TYPE(short, 'S', 2);
496 DECLARE_PRIM_TYPE(int, 'I', 4);
497 DECLARE_PRIM_TYPE(long, 'J', 8);
498 DECLARE_PRIM_TYPE(boolean
, 'Z', 1);
499 DECLARE_PRIM_TYPE(char, 'C', 2);
500 DECLARE_PRIM_TYPE(float, 'F', 4);
501 DECLARE_PRIM_TYPE(double, 'D', 8);
502 DECLARE_PRIM_TYPE(void, 'V', 0);
505 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
510 return JvPrimClass (byte
);
512 return JvPrimClass (short);
514 return JvPrimClass (int);
516 return JvPrimClass (long);
518 return JvPrimClass (boolean
);
520 return JvPrimClass (char);
522 return JvPrimClass (float);
524 return JvPrimClass (double);
526 return JvPrimClass (void);
530 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
532 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
533 return _Jv_FindClass (name
, loader
);
536 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig
[1], loader
));
538 JvFail ("couldn't understand class signature");
539 return NULL
; // Placate compiler.
545 JvConvertArgv (int argc
, const char **argv
)
549 jobjectArray ar
= JvNewObjectArray(argc
, &StringClass
, NULL
);
550 jobject
* ptr
= elements(ar
);
551 for (int i
= 0; i
< argc
; i
++)
553 const char *arg
= argv
[i
];
554 // FIXME - should probably use JvNewStringUTF.
555 *ptr
++ = JvNewStringLatin1(arg
, strlen(arg
));
557 return (JArray
<jstring
>*) ar
;
560 // FIXME: These variables are static so that they will be
561 // automatically scanned by the Boehm collector. This is needed
562 // because with qthreads the collector won't scan the initial stack --
563 // it will only scan the qthreads stacks.
565 // Command line arguments.
566 static jobject arg_vec
;
568 // The primary threadgroup.
569 static java::lang::ThreadGroup
*main_group
;
571 // The primary thread.
572 static java::lang::Thread
*main_thread
;
575 JvRunMain (jclass klass
, int argc
, const char **argv
)
581 arithexception
= new java::lang::ArithmeticException
582 (JvNewStringLatin1 ("/ by zero"));
585 no_memory
= new java::lang::OutOfMemoryError
;
588 LTDL_SET_PRELOADED_SYMBOLS ();
591 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
592 main_group
= new java::lang::ThreadGroup (23);
593 main_thread
= new java::lang::FirstThread (main_group
, klass
, arg_vec
);
595 main_thread
->start();
598 java::lang::Runtime::getRuntime ()->exit (0);
604 _Jv_Malloc (jsize size
)
608 void *ptr
= malloc ((size_t) size
);
622 // In theory, these routines can be #ifdef'd away on machines which
623 // support divide overflow signals. However, we never know if some
624 // code might have been compiled with "-fuse-divide-subroutine", so we
625 // always include them in libgcj.
628 _Jv_divI (jint dividend
, jint divisor
)
631 _Jv_Throw (arithexception
);
633 if (dividend
== 0x80000000L
&& divisor
== -1)
636 return dividend
/ divisor
;
640 _Jv_remI (jint dividend
, jint divisor
)
643 _Jv_Throw (arithexception
);
645 if (dividend
== 0x80000000L
&& divisor
== -1)
648 return dividend
% divisor
;
652 _Jv_divJ (jlong dividend
, jlong divisor
)
655 _Jv_Throw (arithexception
);
657 if (dividend
== 0x8000000000000000LL
&& divisor
== -1)
660 return dividend
/ divisor
;
664 _Jv_remJ (jlong dividend
, jlong divisor
)
667 _Jv_Throw (arithexception
);
669 if (dividend
== 0x8000000000000000LL
&& divisor
== -1)
672 return dividend
% divisor
;