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)
72 _Jv_Throw (arithexception);
79 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
82 register _Jv_ushort *aptr, *bptr;
85 if (a->hash != b->hash)
90 aptr = (_Jv_ushort *)a->data;
91 bptr = (_Jv_ushort *)b->data;
94 if (*aptr++ != *bptr++)
99 /* True iff A is equal to STR.
100 HASH is STR->hashCode().
104 _Jv_equal (Utf8Const* a, jstring str, jint hash)
106 if (a->hash != (_Jv_ushort) hash)
108 jint len = str->length();
110 jchar *sptr = _Jv_GetStringChars (str);
111 register unsigned char* ptr = (unsigned char*) a->data;
112 register unsigned char* limit = ptr + a->length;
115 int ch = UTF8_GET (ptr, limit);
124 /* Count the number of Unicode chars encoded in a given Ut8 string. */
126 _Jv_strLengthUtf8(char* str, int len)
128 register unsigned char* ptr;
129 register unsigned char* limit;
132 ptr = (unsigned char*) str;
135 for (; ptr < limit; str_length++) {
136 if (UTF8_GET (ptr, limit) < 0) {
143 /* Calculate a hash value for a string encoded in Utf8 format.
144 * This returns the same hash value as specified or java.lang.String.hashCode.
147 hashUtf8String (char* str, int len)
149 register unsigned char* ptr = (unsigned char*) str;
150 register unsigned char* limit = ptr + len;
155 int ch = UTF8_GET (ptr, limit);
156 /* Updated specification from
157 http://www.javasoft.com/docs/books/jls/clarify.html. */
158 hash = (31 * hash) + ch;
164 _Jv_makeUtf8Const (char* s, int len)
168 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
171 memcpy (m->data, s, len);
174 m->hash = hashUtf8String (s, len) & 0xFFFF;
182 _Jv_Abort (const char *function, const char *file, int line,
186 _Jv_Abort (const char *, const char *, int, const char *message)
191 "libgcj failure: %s\n in function %s, file %s, line %d\n",
192 message, function, file, line);
194 java::io::PrintStream *err = java::lang::System::err;
195 err->print(JvNewStringLatin1 ("libgcj failure: "));
196 err->println(JvNewStringLatin1 (message));
203 fail_on_finalization (jobject)
205 JvFail ("object was finalized");
209 _Jv_GCWatch (jobject obj)
211 _Jv_RegisterFinalizer (obj, fail_on_finalization);
215 _Jv_ThrowBadArrayIndex(jint bad_index)
217 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
218 (java::lang::String::valueOf(bad_index)));
222 _Jv_CheckCast (jclass c, jobject obj)
224 if (obj != NULL && ! c->isAssignableFrom(obj->getClass()))
225 JvThrow (new java::lang::ClassCastException);
230 _Jv_CheckArrayStore (jobject arr, jobject obj)
234 JvAssert (arr != NULL);
235 jclass arr_class = arr->getClass();
236 JvAssert (arr_class->isArray());
237 jclass elt_class = arr_class->getComponentType();
238 jclass obj_class = obj->getClass();
239 if (! elt_class->isAssignableFrom(obj_class))
240 JvThrow (new java::lang::ArrayStoreException);
246 // Allocate some unscanned memory and throw an exception if no memory.
248 _Jv_AllocBytesChecked (jsize size)
250 void *r = _Jv_AllocBytes (size);
252 _Jv_Throw (no_memory);
256 // Allocate a new object of class C. SIZE is the size of the object
257 // to allocate. You might think this is redundant, but it isn't; some
258 // classes, such as String, aren't of fixed size.
260 _Jv_AllocObject (jclass c, jint size)
264 jobject obj = (jobject) _Jv_AllocObj (size);
267 *((_Jv_VTable **) obj) = c->vtable;
269 // If this class has inherited finalize from Object, then don't
270 // bother registering a finalizer. We know that finalize() is the
271 // very first method after the dummy entry. If this turns out to be
272 // unreliable, a more robust implementation can be written. Such an
273 // implementation would look for Object.finalize in Object's method
274 // table at startup, and then use that information to find the
275 // appropriate index in the method vector.
276 if (c->vtable->method[1] != ObjectClass.vtable->method[1])
277 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
282 // Allocate a new array of Java objects. Each object is of type
283 // `elementClass'. `init' is used to initialize each slot in the
286 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
289 JvThrow (new java::lang::NegativeArraySizeException);
291 // Check for overflow.
292 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / sizeof (jobject))
295 size_t size = count * sizeof (jobject) + sizeof (__JArray);
296 jclass clas = _Jv_FindArrayClass (elementClass);
297 jobjectArray obj = (jobjectArray) _Jv_AllocArray (size);
301 jobject* ptr = elements(obj);
302 // We know the allocator returns zeroed memory. So don't bother
309 // Set the vtbl last to avoid problems if the GC happens during the
310 // window in this function between the allocation and this
312 *((_Jv_VTable **) obj) = clas->vtable;
316 // Allocate a new array of primitives. ELTYPE is the type of the
317 // element, COUNT is the size of the array.
319 _Jv_NewPrimArray (jclass eltype, jint count)
321 int elsize = eltype->size();
323 JvThrow (new java::lang::NegativeArraySizeException ());
325 // Check for overflow.
326 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / elsize)
329 __JArray *arr = (__JArray*) _Jv_AllocObj (sizeof (__JArray)
334 // Note that we assume we are given zeroed memory by the allocator.
336 jclass klass = _Jv_FindArrayClass (eltype);
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 **) arr) = klass->vtable;
345 JvNewCharArray (jint length)
347 return (jcharArray) _Jv_NewPrimArray (JvPrimClass (char), length);
351 JvNewBooleanArray (jint length)
353 return (jbooleanArray) _Jv_NewPrimArray (JvPrimClass (boolean), length);
357 JvNewByteArray (jint length)
359 return (jbyteArray) _Jv_NewPrimArray (JvPrimClass (byte), length);
363 JvNewShortArray (jint length)
365 return (jshortArray) _Jv_NewPrimArray (JvPrimClass (short), length);
369 JvNewIntArray (jint length)
371 return (jintArray) _Jv_NewPrimArray (JvPrimClass (int), length);
375 JvNewLongArray (jint length)
377 return (jlongArray) _Jv_NewPrimArray (JvPrimClass (long), length);
381 JvNewFloatArray (jint length)
383 return (jfloatArray) _Jv_NewPrimArray (JvPrimClass (float), length);
387 JvNewDoubleArray (jint length)
389 return (jdoubleArray) _Jv_NewPrimArray (JvPrimClass (double), length);
393 _Jv_NewArray (jint type, jint size)
397 case 4: return JvNewBooleanArray (size);
398 case 5: return JvNewCharArray (size);
399 case 6: return JvNewFloatArray (size);
400 case 7: return JvNewDoubleArray (size);
401 case 8: return JvNewByteArray (size);
402 case 9: return JvNewShortArray (size);
403 case 10: return JvNewIntArray (size);
404 case 11: return JvNewLongArray (size);
406 JvFail ("newarray - bad type code");
407 return NULL; // Placate compiler.
411 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
413 JvAssert (type->isArray());
414 jclass element_type = type->getComponentType();
416 if (element_type->isPrimitive())
417 result = _Jv_NewPrimArray (element_type, sizes[0]);
419 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
423 JvAssert (! element_type->isPrimitive());
424 JvAssert (element_type->isArray());
425 jobject *contents = elements ((jobjectArray) result);
426 for (int i = 0; i < sizes[0]; ++i)
427 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
435 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
438 jint sizes[dimensions];
439 va_start (args, dimensions);
440 for (int i = 0; i < dimensions; ++i)
442 jint size = va_arg (args, jint);
447 return _Jv_NewMultiArray (array_type, dimensions, sizes);
452 class _Jv_PrimClass : public java::lang::Class
455 // FIXME: calling convention is weird. If we use the natural types
456 // then the compiler will complain because they aren't Java types.
457 _Jv_PrimClass (jobject cname, jbyte sig, jint len)
459 using namespace java::lang::reflect;
461 // We must initialize every field of the class. We do this in
462 // the same order they are declared in Class.h.
464 name = _Jv_makeUtf8Const ((char *) cname, -1);
465 accflags = Modifier::PUBLIC | Modifier::FINAL;
468 constants.tags = NULL;
469 constants.data = NULL;
472 vtable_method_count = 0;
476 static_field_count = 0;
477 vtable = JV_PRIMITIVE_VTABLE;
486 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
487 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
489 DECLARE_PRIM_TYPE(byte, 'B', 1);
490 DECLARE_PRIM_TYPE(short, 'S', 2);
491 DECLARE_PRIM_TYPE(int, 'I', 4);
492 DECLARE_PRIM_TYPE(long, 'J', 8);
493 DECLARE_PRIM_TYPE(boolean, 'Z', 1);
494 DECLARE_PRIM_TYPE(char, 'C', 2);
495 DECLARE_PRIM_TYPE(float, 'F', 4);
496 DECLARE_PRIM_TYPE(double, 'D', 8);
497 DECLARE_PRIM_TYPE(void, 'V', 0);
500 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
505 return JvPrimClass (byte);
507 return JvPrimClass (short);
509 return JvPrimClass (int);
511 return JvPrimClass (long);
513 return JvPrimClass (boolean);
515 return JvPrimClass (char);
517 return JvPrimClass (float);
519 return JvPrimClass (double);
521 return JvPrimClass (void);
525 for (i = 1; sig[i] && sig[i] != ';'; ++i)
527 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
528 return _Jv_FindClass (name, loader);
531 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader));
533 JvFail ("couldn't understand class signature");
534 return NULL; // Placate compiler.
540 JvConvertArgv (int argc, const char **argv)
544 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
545 jobject* ptr = elements(ar);
546 for (int i = 0; i < argc; i++)
548 const char *arg = argv[i];
549 // FIXME - should probably use JvNewStringUTF.
550 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
552 return (JArray<jstring>*) ar;
555 // FIXME: These variables are static so that they will be
556 // automatically scanned by the Boehm collector. This is needed
557 // because with qthreads the collector won't scan the initial stack --
558 // it will only scan the qthreads stacks.
560 // Command line arguments.
561 static jobject arg_vec;
563 // The primary threadgroup.
564 static java::lang::ThreadGroup *main_group;
566 // The primary thread.
567 static java::lang::Thread *main_thread;
570 JvRunMain (jclass klass, int argc, const char **argv)
575 no_memory = new java::lang::OutOfMemoryError;
578 LTDL_SET_PRELOADED_SYMBOLS ();
581 arg_vec = JvConvertArgv (argc - 1, argv + 1);
582 main_group = new java::lang::ThreadGroup (23);
583 main_thread = new java::lang::FirstThread (main_group, klass, arg_vec);
585 main_thread->start();
588 java::lang::Runtime::getRuntime ()->exit (0);
594 _Jv_Malloc (jsize size)
598 void *ptr = malloc ((size_t) size);