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
21 #pragma implementation "java-array.h"
26 #include <java/lang/Class.h>
27 #include <java/lang/Runtime.h>
28 #include <java/lang/String.h>
29 #include <java/lang/Thread.h>
30 #include <java/lang/ThreadGroup.h>
31 #include <java/lang/FirstThread.h>
32 #include <java/lang/ArrayIndexOutOfBoundsException.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>
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 _Jv_equalUtf8Consts (Utf8Const
* a
, Utf8Const
*b
)
59 register _Jv_ushort
*aptr
, *bptr
;
62 if (a
->hash
!= b
->hash
)
67 aptr
= (_Jv_ushort
*)a
->data
;
68 bptr
= (_Jv_ushort
*)b
->data
;
71 if (*aptr
++ != *bptr
++)
76 /* True iff A is equal to STR.
77 HASH is STR->hashCode().
81 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
83 if (a
->hash
!= (_Jv_ushort
) hash
)
85 jint len
= str
->length();
87 jchar
*sptr
= _Jv_GetStringChars (str
);
88 register unsigned char* ptr
= (unsigned char*) a
->data
;
89 register unsigned char* limit
= ptr
+ a
->length
;
92 int ch
= UTF8_GET (ptr
, limit
);
101 /* Count the number of Unicode chars encoded in a given Ut8 string. */
103 _Jv_strLengthUtf8(char* str
, int len
)
105 register unsigned char* ptr
;
106 register unsigned char* limit
;
109 ptr
= (unsigned char*) str
;
112 for (; ptr
< limit
; str_length
++) {
113 if (UTF8_GET (ptr
, limit
) < 0) {
120 /* Calculate a hash value for a string encoded in Utf8 format.
121 * This returns the same hash value as specified or java.lang.String.hashCode.
124 hashUtf8String (char* str
, int len
)
126 register unsigned char* ptr
= (unsigned char*) str
;
127 register unsigned char* limit
= ptr
+ len
;
132 int ch
= UTF8_GET (ptr
, limit
);
133 /* Updated specification from
134 http://www.javasoft.com/docs/books/jls/clarify.html. */
135 hash
= (31 * hash
) + ch
;
141 _Jv_makeUtf8Const (char* s
, int len
)
145 Utf8Const
* m
= (Utf8Const
*) _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
148 memcpy (m
->data
, s
, len
);
151 m
->hash
= hashUtf8String (s
, len
) & 0xFFFF;
159 _Jv_Abort (const char *function
, const char *file
, int line
,
163 _Jv_Abort (const char *, const char *, int, const char *message
)
168 "libgcj failure: %s\n in function %s, file %s, line %d\n",
169 message
, function
, file
, line
);
171 java::io::PrintStream
*err
= java::lang::System::err
;
172 err
->print(JvNewStringLatin1 ("libgcj failure: "));
173 err
->println(JvNewStringLatin1 (message
));
180 fail_on_finalization (jobject
)
182 JvFail ("object was finalized");
186 _Jv_GCWatch (jobject obj
)
188 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
192 _Jv_ThrowBadArrayIndex(jint bad_index
)
194 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
195 (java::lang::String::valueOf(bad_index
)));
199 _Jv_CheckCast (jclass c
, jobject obj
)
201 if (obj
!= NULL
&& ! c
->isAssignableFrom(obj
->getClass()))
202 JvThrow (new java::lang::ClassCastException
);
207 _Jv_CheckArrayStore (jobject arr
, jobject obj
)
211 JvAssert (arr
!= NULL
);
212 jclass arr_class
= arr
->getClass();
213 JvAssert (arr_class
->isArray());
214 jclass elt_class
= arr_class
->getComponentType();
215 jclass obj_class
= obj
->getClass();
216 if (! elt_class
->isAssignableFrom(obj_class
))
217 JvThrow (new java::lang::ArrayStoreException
);
223 // Allocate some unscanned memory and throw an exception if no memory.
225 _Jv_AllocBytesChecked (jsize size
)
227 void *r
= _Jv_AllocBytes (size
);
229 _Jv_Throw (no_memory
);
233 // Allocate a new object of class C. SIZE is the size of the object
234 // to allocate. You might think this is redundant, but it isn't; some
235 // classes, such as String, aren't of fixed size.
237 _Jv_AllocObject (jclass c
, jint size
)
241 jobject obj
= (jobject
) _Jv_AllocObj (size
);
244 *((_Jv_VTable
**) obj
) = c
->vtable
;
246 // If this class has inherited finalize from Object, then don't
247 // bother registering a finalizer. We know that finalize() is the
248 // very first method after the dummy entry. If this turns out to be
249 // unreliable, a more robust implementation can be written. Such an
250 // implementation would look for Object.finalize in Object's method
251 // table at startup, and then use that information to find the
252 // appropriate index in the method vector.
253 if (c
->vtable
->method
[1] != ObjectClass
.vtable
->method
[1])
254 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
259 // Allocate a new array of Java objects. Each object is of type
260 // `elementClass'. `init' is used to initialize each slot in the
263 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
266 JvThrow (new java::lang::NegativeArraySizeException
);
268 // Check for overflow.
269 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / sizeof (jobject
))
272 size_t size
= count
* sizeof (jobject
) + sizeof (__JArray
);
273 jclass clas
= _Jv_FindArrayClass (elementClass
);
274 jobjectArray obj
= (jobjectArray
) _Jv_AllocArray (size
);
278 jobject
* ptr
= elements(obj
);
279 // We know the allocator returns zeroed memory. So don't bother
286 // Set the vtbl last to avoid problems if the GC happens during the
287 // window in this function between the allocation and this
289 *((_Jv_VTable
**) obj
) = clas
->vtable
;
293 // Allocate a new array of primitives. ELTYPE is the type of the
294 // element, COUNT is the size of the array.
296 _Jv_NewPrimArray (jclass eltype
, jint count
)
298 int elsize
= eltype
->size();
300 JvThrow (new java::lang::NegativeArraySizeException ());
302 // Check for overflow.
303 if ((size_t) count
> (SIZE_T_MAX
- sizeof (__JArray
)) / elsize
)
306 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (sizeof (__JArray
)
311 // Note that we assume we are given zeroed memory by the allocator.
313 jclass klass
= _Jv_FindArrayClass (eltype
);
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
**) arr
) = klass
->vtable
;
322 JvNewCharArray (jint length
)
324 return (jcharArray
) _Jv_NewPrimArray (JvPrimClass (char), length
);
328 JvNewBooleanArray (jint length
)
330 return (jbooleanArray
) _Jv_NewPrimArray (JvPrimClass (boolean
), length
);
334 JvNewByteArray (jint length
)
336 return (jbyteArray
) _Jv_NewPrimArray (JvPrimClass (byte
), length
);
340 JvNewShortArray (jint length
)
342 return (jshortArray
) _Jv_NewPrimArray (JvPrimClass (short), length
);
346 JvNewIntArray (jint length
)
348 return (jintArray
) _Jv_NewPrimArray (JvPrimClass (int), length
);
352 JvNewLongArray (jint length
)
354 return (jlongArray
) _Jv_NewPrimArray (JvPrimClass (long), length
);
358 JvNewFloatArray (jint length
)
360 return (jfloatArray
) _Jv_NewPrimArray (JvPrimClass (float), length
);
364 JvNewDoubleArray (jint length
)
366 return (jdoubleArray
) _Jv_NewPrimArray (JvPrimClass (double), length
);
370 _Jv_NewArray (jint type
, jint size
)
374 case 4: return JvNewBooleanArray (size
);
375 case 5: return JvNewCharArray (size
);
376 case 6: return JvNewFloatArray (size
);
377 case 7: return JvNewDoubleArray (size
);
378 case 8: return JvNewByteArray (size
);
379 case 9: return JvNewShortArray (size
);
380 case 10: return JvNewIntArray (size
);
381 case 11: return JvNewLongArray (size
);
383 JvFail ("newarray - bad type code");
384 return NULL
; // Placate compiler.
388 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
390 JvAssert (type
->isArray());
391 jclass element_type
= type
->getComponentType();
393 if (element_type
->isPrimitive())
394 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
396 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
400 JvAssert (! element_type
->isPrimitive());
401 JvAssert (element_type
->isArray());
402 jobject
*contents
= elements ((jobjectArray
) result
);
403 for (int i
= 0; i
< sizes
[0]; ++i
)
404 contents
[i
] = _Jv_NewMultiArray (element_type
, dimensions
- 1,
412 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
415 jint sizes
[dimensions
];
416 va_start (args
, dimensions
);
417 for (int i
= 0; i
< dimensions
; ++i
)
419 jint size
= va_arg (args
, jint
);
424 return _Jv_NewMultiArray (array_type
, dimensions
, sizes
);
431 static java::lang::NullPointerException
*nullp
;
436 // Don't run `new' in a signal handler, so we always throw the same
437 // null pointer exception.
441 #endif /* HANDLE_SEGV */
443 class _Jv_PrimClass
: public java::lang::Class
446 // FIXME: calling convention is weird. If we use the natural types
447 // then the compiler will complain because they aren't Java types.
448 _Jv_PrimClass (jobject cname
, jbyte sig
, jint len
)
450 using namespace java::lang::reflect
;
452 // We must initialize every field of the class. We do this in
453 // the same order they are declared in Class.h.
455 name
= _Jv_makeUtf8Const ((char *) cname
, -1);
456 accflags
= Modifier::PUBLIC
| Modifier::FINAL
;
459 constants
.tags
= NULL
;
460 constants
.data
= NULL
;
463 vtable_method_count
= 0;
467 static_field_count
= 0;
468 vtable
= JV_PRIMITIVE_VTABLE
;
477 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
478 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
480 DECLARE_PRIM_TYPE(byte
, 'B', 1);
481 DECLARE_PRIM_TYPE(short, 'S', 2);
482 DECLARE_PRIM_TYPE(int, 'I', 4);
483 DECLARE_PRIM_TYPE(long, 'J', 8);
484 DECLARE_PRIM_TYPE(boolean
, 'Z', 1);
485 DECLARE_PRIM_TYPE(char, 'C', 2);
486 DECLARE_PRIM_TYPE(float, 'F', 4);
487 DECLARE_PRIM_TYPE(double, 'D', 8);
488 DECLARE_PRIM_TYPE(void, 'V', 0);
491 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
496 return JvPrimClass (byte
);
498 return JvPrimClass (short);
500 return JvPrimClass (int);
502 return JvPrimClass (long);
504 return JvPrimClass (boolean
);
506 return JvPrimClass (char);
508 return JvPrimClass (float);
510 return JvPrimClass (double);
512 return JvPrimClass (void);
516 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
518 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
519 return _Jv_FindClass (name
, loader
);
522 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig
[1], loader
));
524 JvFail ("couldn't understand class signature");
525 return NULL
; // Placate compiler.
531 JvConvertArgv (int argc
, const char **argv
)
535 jobjectArray ar
= JvNewObjectArray(argc
, &StringClass
, NULL
);
536 jobject
* ptr
= elements(ar
);
537 for (int i
= 0; i
< argc
; i
++)
539 const char *arg
= argv
[i
];
540 // FIXME - should probably use JvNewStringUTF.
541 *ptr
++ = JvNewStringLatin1(arg
, strlen(arg
));
543 return (JArray
<jstring
>*) ar
;
546 // FIXME: These variables are static so that they will be
547 // automatically scanned by the Boehm collector. This is needed
548 // because with qthreads the collector won't scan the initial stack --
549 // it will only scan the qthreads stacks.
551 // Command line arguments.
552 static jobject arg_vec
;
554 // The primary threadgroup.
555 static java::lang::ThreadGroup
*main_group
;
557 // The primary thread.
558 static java::lang::Thread
*main_thread
;
561 JvRunMain (jclass klass
, int argc
, const char **argv
)
564 nullp
= new java::lang::NullPointerException ();
566 struct sigaction act
;
567 act
.sa_handler
= catch_segv
;
568 sigemptyset (&act
.sa_mask
);
570 sigaction (SIGSEGV
, &act
, NULL
);
573 no_memory
= new java::lang::OutOfMemoryError
;
575 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
576 main_group
= new java::lang::ThreadGroup (23);
577 main_thread
= new java::lang::FirstThread (main_group
, klass
, arg_vec
);
579 main_thread
->start();
582 java::lang::Runtime::getRuntime ()->exit (0);
588 _Jv_Malloc (jsize size
)
592 void *ptr
= malloc ((size_t) size
);