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
)
68 static java::lang::ArithmeticException
*arithexception
;
69 SIGNAL_HANDLER (catch_fpe
)
71 #ifdef HANDLE_DIVIDE_OVERFLOW
72 HANDLE_DIVIDE_OVERFLOW
;
76 _Jv_Throw (arithexception
);
83 _Jv_equalUtf8Consts (Utf8Const
* a
, Utf8Const
*b
)
86 register _Jv_ushort
*aptr
, *bptr
;
89 if (a
->hash
!= b
->hash
)
94 aptr
= (_Jv_ushort
*)a
->data
;
95 bptr
= (_Jv_ushort
*)b
->data
;
98 if (*aptr
++ != *bptr
++)
103 /* True iff A is equal to STR.
104 HASH is STR->hashCode().
108 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
110 if (a
->hash
!= (_Jv_ushort
) hash
)
112 jint len
= str
->length();
114 jchar
*sptr
= _Jv_GetStringChars (str
);
115 register unsigned char* ptr
= (unsigned char*) a
->data
;
116 register unsigned char* limit
= ptr
+ a
->length
;
119 int ch
= UTF8_GET (ptr
, limit
);
128 /* Count the number of Unicode chars encoded in a given Ut8 string. */
130 _Jv_strLengthUtf8(char* str
, int len
)
132 register unsigned char* ptr
;
133 register unsigned char* limit
;
136 ptr
= (unsigned char*) str
;
139 for (; ptr
< limit
; str_length
++) {
140 if (UTF8_GET (ptr
, limit
) < 0) {
147 /* Calculate a hash value for a string encoded in Utf8 format.
148 * This returns the same hash value as specified or java.lang.String.hashCode.
151 hashUtf8String (char* str
, int len
)
153 register unsigned char* ptr
= (unsigned char*) str
;
154 register unsigned char* limit
= ptr
+ len
;
159 int ch
= UTF8_GET (ptr
, limit
);
160 /* Updated specification from
161 http://www.javasoft.com/docs/books/jls/clarify.html. */
162 hash
= (31 * hash
) + ch
;
168 _Jv_makeUtf8Const (char* s
, int len
)
172 Utf8Const
* m
= (Utf8Const
*) _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
175 memcpy (m
->data
, s
, len
);
178 m
->hash
= hashUtf8String (s
, len
) & 0xFFFF;
186 _Jv_Abort (const char *function
, const char *file
, int line
,
190 _Jv_Abort (const char *, const char *, int, const char *message
)
195 "libgcj failure: %s\n in function %s, file %s, line %d\n",
196 message
, function
, file
, line
);
198 java::io::PrintStream
*err
= java::lang::System::err
;
199 err
->print(JvNewStringLatin1 ("libgcj failure: "));
200 err
->println(JvNewStringLatin1 (message
));
207 fail_on_finalization (jobject
)
209 JvFail ("object was finalized");
213 _Jv_GCWatch (jobject obj
)
215 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
219 _Jv_ThrowBadArrayIndex(jint bad_index
)
221 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
222 (java::lang::String::valueOf(bad_index
)));
226 _Jv_CheckCast (jclass c
, jobject obj
)
228 if (obj
!= NULL
&& ! c
->isAssignableFrom(obj
->getClass()))
229 JvThrow (new java::lang::ClassCastException
);
234 _Jv_CheckArrayStore (jobject arr
, jobject obj
)
238 JvAssert (arr
!= NULL
);
239 jclass arr_class
= arr
->getClass();
240 JvAssert (arr_class
->isArray());
241 jclass elt_class
= arr_class
->getComponentType();
242 jclass obj_class
= obj
->getClass();
243 if (! elt_class
->isAssignableFrom(obj_class
))
244 JvThrow (new java::lang::ArrayStoreException
);
250 // Allocate some unscanned memory and throw an exception if no memory.
252 _Jv_AllocBytesChecked (jsize size
)
254 void *r
= _Jv_AllocBytes (size
);
256 _Jv_Throw (no_memory
);
260 // Allocate a new object of class C. SIZE is the size of the object
261 // to allocate. You might think this is redundant, but it isn't; some
262 // classes, such as String, aren't of fixed size.
264 _Jv_AllocObject (jclass c
, jint size
)
268 jobject obj
= (jobject
) _Jv_AllocObj (size
);
271 *((_Jv_VTable
**) obj
) = c
->vtable
;
273 // If this class has inherited finalize from Object, then don't
274 // bother registering a finalizer. We know that finalize() is the
275 // very first method after the dummy entry. If this turns out to be
276 // unreliable, a more robust implementation can be written. Such an
277 // implementation would look for Object.finalize in Object's method
278 // table at startup, and then use that information to find the
279 // appropriate index in the method vector.
280 if (c
->vtable
->method
[1] != ObjectClass
.vtable
->method
[1])
281 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
286 // Allocate a new array of Java objects. Each object is of type
287 // `elementClass'. `init' is used to initialize each slot in the
290 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
293 JvThrow (new java::lang::NegativeArraySizeException
);
295 // Check for overflow.
296 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / sizeof (jobject
))
299 size_t size
= count
* sizeof (jobject
) + sizeof (__JArray
);
300 jclass clas
= _Jv_FindArrayClass (elementClass
);
301 jobjectArray obj
= (jobjectArray
) _Jv_AllocArray (size
);
305 jobject
* ptr
= elements(obj
);
306 // We know the allocator returns zeroed memory. So don't bother
313 // Set the vtbl last to avoid problems if the GC happens during the
314 // window in this function between the allocation and this
316 *((_Jv_VTable
**) obj
) = clas
->vtable
;
320 // Allocate a new array of primitives. ELTYPE is the type of the
321 // element, COUNT is the size of the array.
323 _Jv_NewPrimArray (jclass eltype
, jint count
)
325 int elsize
= eltype
->size();
327 JvThrow (new java::lang::NegativeArraySizeException ());
329 // Check for overflow.
330 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / elsize
)
333 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (sizeof (__JArray
)
338 // Note that we assume we are given zeroed memory by the allocator.
340 jclass klass
= _Jv_FindArrayClass (eltype
);
341 // Set the vtbl last to avoid problems if the GC happens during the
342 // window in this function between the allocation and this
344 *((_Jv_VTable
**) arr
) = klass
->vtable
;
349 JvNewCharArray (jint length
)
351 return (jcharArray
) _Jv_NewPrimArray (JvPrimClass (char), length
);
355 JvNewBooleanArray (jint length
)
357 return (jbooleanArray
) _Jv_NewPrimArray (JvPrimClass (boolean
), length
);
361 JvNewByteArray (jint length
)
363 return (jbyteArray
) _Jv_NewPrimArray (JvPrimClass (byte
), length
);
367 JvNewShortArray (jint length
)
369 return (jshortArray
) _Jv_NewPrimArray (JvPrimClass (short), length
);
373 JvNewIntArray (jint length
)
375 return (jintArray
) _Jv_NewPrimArray (JvPrimClass (int), length
);
379 JvNewLongArray (jint length
)
381 return (jlongArray
) _Jv_NewPrimArray (JvPrimClass (long), length
);
385 JvNewFloatArray (jint length
)
387 return (jfloatArray
) _Jv_NewPrimArray (JvPrimClass (float), length
);
391 JvNewDoubleArray (jint length
)
393 return (jdoubleArray
) _Jv_NewPrimArray (JvPrimClass (double), length
);
397 _Jv_NewArray (jint type
, jint size
)
401 case 4: return JvNewBooleanArray (size
);
402 case 5: return JvNewCharArray (size
);
403 case 6: return JvNewFloatArray (size
);
404 case 7: return JvNewDoubleArray (size
);
405 case 8: return JvNewByteArray (size
);
406 case 9: return JvNewShortArray (size
);
407 case 10: return JvNewIntArray (size
);
408 case 11: return JvNewLongArray (size
);
410 JvFail ("newarray - bad type code");
411 return NULL
; // Placate compiler.
415 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
417 JvAssert (type
->isArray());
418 jclass element_type
= type
->getComponentType();
420 if (element_type
->isPrimitive())
421 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
423 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
427 JvAssert (! element_type
->isPrimitive());
428 JvAssert (element_type
->isArray());
429 jobject
*contents
= elements ((jobjectArray
) result
);
430 for (int i
= 0; i
< sizes
[0]; ++i
)
431 contents
[i
] = _Jv_NewMultiArray (element_type
, dimensions
- 1,
439 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
442 jint sizes
[dimensions
];
443 va_start (args
, dimensions
);
444 for (int i
= 0; i
< dimensions
; ++i
)
446 jint size
= va_arg (args
, jint
);
451 return _Jv_NewMultiArray (array_type
, dimensions
, sizes
);
456 class _Jv_PrimClass
: public java::lang::Class
459 // FIXME: calling convention is weird. If we use the natural types
460 // then the compiler will complain because they aren't Java types.
461 _Jv_PrimClass (jobject cname
, jbyte sig
, jint len
)
463 using namespace java::lang::reflect
;
465 // We must initialize every field of the class. We do this in
466 // the same order they are declared in Class.h.
468 name
= _Jv_makeUtf8Const ((char *) cname
, -1);
469 accflags
= Modifier::PUBLIC
| Modifier::FINAL
;
472 constants
.tags
= NULL
;
473 constants
.data
= NULL
;
476 vtable_method_count
= 0;
480 static_field_count
= 0;
481 vtable
= JV_PRIMITIVE_VTABLE
;
490 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
491 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
493 DECLARE_PRIM_TYPE(byte
, 'B', 1);
494 DECLARE_PRIM_TYPE(short, 'S', 2);
495 DECLARE_PRIM_TYPE(int, 'I', 4);
496 DECLARE_PRIM_TYPE(long, 'J', 8);
497 DECLARE_PRIM_TYPE(boolean
, 'Z', 1);
498 DECLARE_PRIM_TYPE(char, 'C', 2);
499 DECLARE_PRIM_TYPE(float, 'F', 4);
500 DECLARE_PRIM_TYPE(double, 'D', 8);
501 DECLARE_PRIM_TYPE(void, 'V', 0);
504 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
509 return JvPrimClass (byte
);
511 return JvPrimClass (short);
513 return JvPrimClass (int);
515 return JvPrimClass (long);
517 return JvPrimClass (boolean
);
519 return JvPrimClass (char);
521 return JvPrimClass (float);
523 return JvPrimClass (double);
525 return JvPrimClass (void);
529 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
531 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
532 return _Jv_FindClass (name
, loader
);
535 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig
[1], loader
));
537 JvFail ("couldn't understand class signature");
538 return NULL
; // Placate compiler.
544 JvConvertArgv (int argc
, const char **argv
)
548 jobjectArray ar
= JvNewObjectArray(argc
, &StringClass
, NULL
);
549 jobject
* ptr
= elements(ar
);
550 for (int i
= 0; i
< argc
; i
++)
552 const char *arg
= argv
[i
];
553 // FIXME - should probably use JvNewStringUTF.
554 *ptr
++ = JvNewStringLatin1(arg
, strlen(arg
));
556 return (JArray
<jstring
>*) ar
;
559 // FIXME: These variables are static so that they will be
560 // automatically scanned by the Boehm collector. This is needed
561 // because with qthreads the collector won't scan the initial stack --
562 // it will only scan the qthreads stacks.
564 // Command line arguments.
565 static jobject arg_vec
;
567 // The primary threadgroup.
568 static java::lang::ThreadGroup
*main_group
;
570 // The primary thread.
571 static java::lang::Thread
*main_thread
;
574 JvRunMain (jclass klass
, int argc
, const char **argv
)
579 no_memory
= new java::lang::OutOfMemoryError
;
582 LTDL_SET_PRELOADED_SYMBOLS ();
585 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
586 main_group
= new java::lang::ThreadGroup (23);
587 main_thread
= new java::lang::FirstThread (main_group
, klass
, arg_vec
);
589 main_thread
->start();
592 java::lang::Runtime::getRuntime ()->exit (0);
598 _Jv_Malloc (jsize size
)
602 void *ptr
= malloc ((size_t) size
);