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>
43 #define ObjectClass _CL_Q34java4lang6Object
44 extern java::lang::Class ObjectClass;
46 // We allocate a single OutOfMemoryError exception which we keep
47 // around for use if we run out of memory.
48 static java::lang::OutOfMemoryError *no_memory;
50 // Largest representable size_t.
51 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
56 static java::lang::NullPointerException *nullp;
57 SIGNAL_HANDLER (catch_segv)
65 static java::lang::ArithmeticException *arithexception;
66 SIGNAL_HANDLER (catch_fpe)
69 _Jv_Throw (arithexception);
76 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
79 register _Jv_ushort *aptr, *bptr;
82 if (a->hash != b->hash)
87 aptr = (_Jv_ushort *)a->data;
88 bptr = (_Jv_ushort *)b->data;
91 if (*aptr++ != *bptr++)
96 /* True iff A is equal to STR.
97 HASH is STR->hashCode().
101 _Jv_equal (Utf8Const* a, jstring str, jint hash)
103 if (a->hash != (_Jv_ushort) hash)
105 jint len = str->length();
107 jchar *sptr = _Jv_GetStringChars (str);
108 register unsigned char* ptr = (unsigned char*) a->data;
109 register unsigned char* limit = ptr + a->length;
112 int ch = UTF8_GET (ptr, limit);
121 /* Count the number of Unicode chars encoded in a given Ut8 string. */
123 _Jv_strLengthUtf8(char* str, int len)
125 register unsigned char* ptr;
126 register unsigned char* limit;
129 ptr = (unsigned char*) str;
132 for (; ptr < limit; str_length++) {
133 if (UTF8_GET (ptr, limit) < 0) {
140 /* Calculate a hash value for a string encoded in Utf8 format.
141 * This returns the same hash value as specified or java.lang.String.hashCode.
144 hashUtf8String (char* str, int len)
146 register unsigned char* ptr = (unsigned char*) str;
147 register unsigned char* limit = ptr + len;
152 int ch = UTF8_GET (ptr, limit);
153 /* Updated specification from
154 http://www.javasoft.com/docs/books/jls/clarify.html. */
155 hash = (31 * hash) + ch;
161 _Jv_makeUtf8Const (char* s, int len)
165 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
168 memcpy (m->data, s, len);
171 m->hash = hashUtf8String (s, len) & 0xFFFF;
179 _Jv_Abort (const char *function, const char *file, int line,
183 _Jv_Abort (const char *, const char *, int, const char *message)
188 "libgcj failure: %s\n in function %s, file %s, line %d\n",
189 message, function, file, line);
191 java::io::PrintStream *err = java::lang::System::err;
192 err->print(JvNewStringLatin1 ("libgcj failure: "));
193 err->println(JvNewStringLatin1 (message));
200 fail_on_finalization (jobject)
202 JvFail ("object was finalized");
206 _Jv_GCWatch (jobject obj)
208 _Jv_RegisterFinalizer (obj, fail_on_finalization);
212 _Jv_ThrowBadArrayIndex(jint bad_index)
214 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
215 (java::lang::String::valueOf(bad_index)));
219 _Jv_CheckCast (jclass c, jobject obj)
221 if (obj != NULL && ! c->isAssignableFrom(obj->getClass()))
222 JvThrow (new java::lang::ClassCastException);
227 _Jv_CheckArrayStore (jobject arr, jobject obj)
231 JvAssert (arr != NULL);
232 jclass arr_class = arr->getClass();
233 JvAssert (arr_class->isArray());
234 jclass elt_class = arr_class->getComponentType();
235 jclass obj_class = obj->getClass();
236 if (! elt_class->isAssignableFrom(obj_class))
237 JvThrow (new java::lang::ArrayStoreException);
243 // Allocate some unscanned memory and throw an exception if no memory.
245 _Jv_AllocBytesChecked (jsize size)
247 void *r = _Jv_AllocBytes (size);
249 _Jv_Throw (no_memory);
253 // Allocate a new object of class C. SIZE is the size of the object
254 // to allocate. You might think this is redundant, but it isn't; some
255 // classes, such as String, aren't of fixed size.
257 _Jv_AllocObject (jclass c, jint size)
261 jobject obj = (jobject) _Jv_AllocObj (size);
264 *((_Jv_VTable **) obj) = c->vtable;
266 // If this class has inherited finalize from Object, then don't
267 // bother registering a finalizer. We know that finalize() is the
268 // very first method after the dummy entry. If this turns out to be
269 // unreliable, a more robust implementation can be written. Such an
270 // implementation would look for Object.finalize in Object's method
271 // table at startup, and then use that information to find the
272 // appropriate index in the method vector.
273 if (c->vtable->method[1] != ObjectClass.vtable->method[1])
274 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
279 // Allocate a new array of Java objects. Each object is of type
280 // `elementClass'. `init' is used to initialize each slot in the
283 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
286 JvThrow (new java::lang::NegativeArraySizeException);
288 // Check for overflow.
289 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / sizeof (jobject))
292 size_t size = count * sizeof (jobject) + sizeof (__JArray);
293 jclass clas = _Jv_FindArrayClass (elementClass);
294 jobjectArray obj = (jobjectArray) _Jv_AllocArray (size);
298 jobject* ptr = elements(obj);
299 // We know the allocator returns zeroed memory. So don't bother
306 // Set the vtbl last to avoid problems if the GC happens during the
307 // window in this function between the allocation and this
309 *((_Jv_VTable **) obj) = clas->vtable;
313 // Allocate a new array of primitives. ELTYPE is the type of the
314 // element, COUNT is the size of the array.
316 _Jv_NewPrimArray (jclass eltype, jint count)
318 int elsize = eltype->size();
320 JvThrow (new java::lang::NegativeArraySizeException ());
322 // Check for overflow.
323 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / elsize)
326 __JArray *arr = (__JArray*) _Jv_AllocObj (sizeof (__JArray)
331 // Note that we assume we are given zeroed memory by the allocator.
333 jclass klass = _Jv_FindArrayClass (eltype);
334 // Set the vtbl last to avoid problems if the GC happens during the
335 // window in this function between the allocation and this
337 *((_Jv_VTable **) arr) = klass->vtable;
342 JvNewCharArray (jint length)
344 return (jcharArray) _Jv_NewPrimArray (JvPrimClass (char), length);
348 JvNewBooleanArray (jint length)
350 return (jbooleanArray) _Jv_NewPrimArray (JvPrimClass (boolean), length);
354 JvNewByteArray (jint length)
356 return (jbyteArray) _Jv_NewPrimArray (JvPrimClass (byte), length);
360 JvNewShortArray (jint length)
362 return (jshortArray) _Jv_NewPrimArray (JvPrimClass (short), length);
366 JvNewIntArray (jint length)
368 return (jintArray) _Jv_NewPrimArray (JvPrimClass (int), length);
372 JvNewLongArray (jint length)
374 return (jlongArray) _Jv_NewPrimArray (JvPrimClass (long), length);
378 JvNewFloatArray (jint length)
380 return (jfloatArray) _Jv_NewPrimArray (JvPrimClass (float), length);
384 JvNewDoubleArray (jint length)
386 return (jdoubleArray) _Jv_NewPrimArray (JvPrimClass (double), length);
390 _Jv_NewArray (jint type, jint size)
394 case 4: return JvNewBooleanArray (size);
395 case 5: return JvNewCharArray (size);
396 case 6: return JvNewFloatArray (size);
397 case 7: return JvNewDoubleArray (size);
398 case 8: return JvNewByteArray (size);
399 case 9: return JvNewShortArray (size);
400 case 10: return JvNewIntArray (size);
401 case 11: return JvNewLongArray (size);
403 JvFail ("newarray - bad type code");
404 return NULL; // Placate compiler.
408 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
410 JvAssert (type->isArray());
411 jclass element_type = type->getComponentType();
413 if (element_type->isPrimitive())
414 result = _Jv_NewPrimArray (element_type, sizes[0]);
416 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
420 JvAssert (! element_type->isPrimitive());
421 JvAssert (element_type->isArray());
422 jobject *contents = elements ((jobjectArray) result);
423 for (int i = 0; i < sizes[0]; ++i)
424 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
432 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
435 jint sizes[dimensions];
436 va_start (args, dimensions);
437 for (int i = 0; i < dimensions; ++i)
439 jint size = va_arg (args, jint);
444 return _Jv_NewMultiArray (array_type, dimensions, sizes);
449 class _Jv_PrimClass : public java::lang::Class
452 // FIXME: calling convention is weird. If we use the natural types
453 // then the compiler will complain because they aren't Java types.
454 _Jv_PrimClass (jobject cname, jbyte sig, jint len)
456 using namespace java::lang::reflect;
458 // We must initialize every field of the class. We do this in
459 // the same order they are declared in Class.h.
461 name = _Jv_makeUtf8Const ((char *) cname, -1);
462 accflags = Modifier::PUBLIC | Modifier::FINAL;
465 constants.tags = NULL;
466 constants.data = NULL;
469 vtable_method_count = 0;
473 static_field_count = 0;
474 vtable = JV_PRIMITIVE_VTABLE;
483 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
484 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
486 DECLARE_PRIM_TYPE(byte, 'B', 1);
487 DECLARE_PRIM_TYPE(short, 'S', 2);
488 DECLARE_PRIM_TYPE(int, 'I', 4);
489 DECLARE_PRIM_TYPE(long, 'J', 8);
490 DECLARE_PRIM_TYPE(boolean, 'Z', 1);
491 DECLARE_PRIM_TYPE(char, 'C', 2);
492 DECLARE_PRIM_TYPE(float, 'F', 4);
493 DECLARE_PRIM_TYPE(double, 'D', 8);
494 DECLARE_PRIM_TYPE(void, 'V', 0);
497 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
502 return JvPrimClass (byte);
504 return JvPrimClass (short);
506 return JvPrimClass (int);
508 return JvPrimClass (long);
510 return JvPrimClass (boolean);
512 return JvPrimClass (char);
514 return JvPrimClass (float);
516 return JvPrimClass (double);
518 return JvPrimClass (void);
522 for (i = 1; sig[i] && sig[i] != ';'; ++i)
524 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
525 return _Jv_FindClass (name, loader);
528 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader));
530 JvFail ("couldn't understand class signature");
531 return NULL; // Placate compiler.
537 JvConvertArgv (int argc, const char **argv)
541 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
542 jobject* ptr = elements(ar);
543 for (int i = 0; i < argc; i++)
545 const char *arg = argv[i];
546 // FIXME - should probably use JvNewStringUTF.
547 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
549 return (JArray<jstring>*) ar;
552 // FIXME: These variables are static so that they will be
553 // automatically scanned by the Boehm collector. This is needed
554 // because with qthreads the collector won't scan the initial stack --
555 // it will only scan the qthreads stacks.
557 // Command line arguments.
558 static jobject arg_vec;
560 // The primary threadgroup.
561 static java::lang::ThreadGroup *main_group;
563 // The primary thread.
564 static java::lang::Thread *main_thread;
567 JvRunMain (jclass klass, int argc, const char **argv)
572 no_memory = new java::lang::OutOfMemoryError;
574 arg_vec = JvConvertArgv (argc - 1, argv + 1);
575 main_group = new java::lang::ThreadGroup (23);
576 main_thread = new java::lang::FirstThread (main_group, klass, arg_vec);
578 main_thread->start();
581 java::lang::Runtime::getRuntime ()->exit (0);
587 _Jv_Malloc (jsize size)
591 void *ptr = malloc ((size_t) size);