]> gcc.gnu.org Git - gcc.git/blob - libjava/prims.cc
configure: Rebuilt.
[gcc.git] / libjava / prims.cc
1 // prims.cc - Code for core of runtime environment.
2
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 #include <config.h>
12
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 #pragma implementation "gcj/array.h"
19
20 #include <gcj/cni.h>
21 #include <jvm.h>
22 #include <java-signal.h>
23 #include <java-threads.h>
24
25 #include <java/lang/Class.h>
26 #include <java/lang/Runtime.h>
27 #include <java/lang/String.h>
28 #include <java/lang/Thread.h>
29 #include <java/lang/ThreadGroup.h>
30 #include <java/lang/FirstThread.h>
31 #include <java/lang/ArrayIndexOutOfBoundsException.h>
32 #include <java/lang/ArithmeticException.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>
42
43 #ifdef USE_LTDL
44 #include <ltdl.h>
45 #endif
46
47 #define ObjectClass _CL_Q34java4lang6Object
48 extern java::lang::Class ObjectClass;
49
50 // We allocate a single OutOfMemoryError exception which we keep
51 // around for use if we run out of memory.
52 static java::lang::OutOfMemoryError *no_memory;
53
54 // Largest representable size_t.
55 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
56
57 \f
58
59 #ifdef HANDLE_SEGV
60 static java::lang::NullPointerException *nullp;
61 SIGNAL_HANDLER (catch_segv)
62 {
63 MAKE_THROW_FRAME;
64 _Jv_Throw (nullp);
65 }
66 #endif
67
68 static java::lang::ArithmeticException *arithexception;
69
70 #ifdef HANDLE_FPE
71 SIGNAL_HANDLER (catch_fpe)
72 {
73 #ifdef HANDLE_DIVIDE_OVERFLOW
74 HANDLE_DIVIDE_OVERFLOW;
75 #else
76 MAKE_THROW_FRAME;
77 #endif
78 _Jv_Throw (arithexception);
79 }
80 #endif
81
82 \f
83
84 jboolean
85 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
86 {
87 register int len;
88 register _Jv_ushort *aptr, *bptr;
89 if (a == b)
90 return true;
91 if (a->hash != b->hash)
92 return false;
93 len = a->length;
94 if (b->length != len)
95 return false;
96 aptr = (_Jv_ushort *)a->data;
97 bptr = (_Jv_ushort *)b->data;
98 len = (len + 1) >> 1;
99 while (--len >= 0)
100 if (*aptr++ != *bptr++)
101 return false;
102 return true;
103 }
104
105 /* True iff A is equal to STR.
106 HASH is STR->hashCode().
107 */
108
109 jboolean
110 _Jv_equal (Utf8Const* a, jstring str, jint hash)
111 {
112 if (a->hash != (_Jv_ushort) hash)
113 return false;
114 jint len = str->length();
115 jint i = 0;
116 jchar *sptr = _Jv_GetStringChars (str);
117 register unsigned char* ptr = (unsigned char*) a->data;
118 register unsigned char* limit = ptr + a->length;
119 for (;; i++, sptr++)
120 {
121 int ch = UTF8_GET (ptr, limit);
122 if (i == len)
123 return ch < 0;
124 if (ch != *sptr)
125 return false;
126 }
127 return true;
128 }
129
130 /* Count the number of Unicode chars encoded in a given Ut8 string. */
131 int
132 _Jv_strLengthUtf8(char* str, int len)
133 {
134 register unsigned char* ptr;
135 register unsigned char* limit;
136 int str_length;
137
138 ptr = (unsigned char*) str;
139 limit = ptr + len;
140 str_length = 0;
141 for (; ptr < limit; str_length++) {
142 if (UTF8_GET (ptr, limit) < 0) {
143 return (-1);
144 }
145 }
146 return (str_length);
147 }
148
149 /* Calculate a hash value for a string encoded in Utf8 format.
150 * This returns the same hash value as specified or java.lang.String.hashCode.
151 */
152 static jint
153 hashUtf8String (char* str, int len)
154 {
155 register unsigned char* ptr = (unsigned char*) str;
156 register unsigned char* limit = ptr + len;
157 jint hash = 0;
158
159 for (; ptr < limit;)
160 {
161 int ch = UTF8_GET (ptr, limit);
162 /* Updated specification from
163 http://www.javasoft.com/docs/books/jls/clarify.html. */
164 hash = (31 * hash) + ch;
165 }
166 return hash;
167 }
168
169 _Jv_Utf8Const *
170 _Jv_makeUtf8Const (char* s, int len)
171 {
172 if (len < 0)
173 len = strlen (s);
174 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
175 if (! m)
176 JvThrow (no_memory);
177 memcpy (m->data, s, len);
178 m->data[len] = 0;
179 m->length = len;
180 m->hash = hashUtf8String (s, len) & 0xFFFF;
181 return (m);
182 }
183
184 _Jv_Utf8Const *
185 _Jv_makeUtf8Const (jstring string)
186 {
187 jint hash = string->hashCode ();
188 jint len = _Jv_GetStringUTFLength (string);
189
190 Utf8Const* m = (Utf8Const*)
191 _Jv_AllocBytesChecked (sizeof(Utf8Const) + len + 1);
192
193 m->hash = hash;
194 m->length = len;
195
196 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
197 m->data[len] = 0;
198
199 return m;
200 }
201
202 \f
203
204 #ifdef DEBUG
205 void
206 _Jv_Abort (const char *function, const char *file, int line,
207 const char *message)
208 #else
209 void
210 _Jv_Abort (const char *, const char *, int, const char *message)
211 #endif
212 {
213 #ifdef DEBUG
214 fprintf (stderr,
215 "libgcj failure: %s\n in function %s, file %s, line %d\n",
216 message, function, file, line);
217 #else
218 java::io::PrintStream *err = java::lang::System::err;
219 err->print(JvNewStringLatin1 ("libgcj failure: "));
220 err->println(JvNewStringLatin1 (message));
221 err->flush();
222 #endif
223 abort ();
224 }
225
226 static void
227 fail_on_finalization (jobject)
228 {
229 JvFail ("object was finalized");
230 }
231
232 void
233 _Jv_GCWatch (jobject obj)
234 {
235 _Jv_RegisterFinalizer (obj, fail_on_finalization);
236 }
237
238 void
239 _Jv_ThrowBadArrayIndex(jint bad_index)
240 {
241 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
242 (java::lang::String::valueOf(bad_index)));
243 }
244
245 void*
246 _Jv_CheckCast (jclass c, jobject obj)
247 {
248 if (obj != NULL && ! c->isAssignableFrom(obj->getClass()))
249 JvThrow (new java::lang::ClassCastException);
250 return obj;
251 }
252
253 void
254 _Jv_CheckArrayStore (jobject arr, jobject obj)
255 {
256 if (obj)
257 {
258 JvAssert (arr != NULL);
259 jclass arr_class = arr->getClass();
260 JvAssert (arr_class->isArray());
261 jclass elt_class = arr_class->getComponentType();
262 jclass obj_class = obj->getClass();
263 if (! elt_class->isAssignableFrom(obj_class))
264 JvThrow (new java::lang::ArrayStoreException);
265 }
266 }
267
268 \f
269
270 // Allocate some unscanned memory and throw an exception if no memory.
271 void *
272 _Jv_AllocBytesChecked (jsize size)
273 {
274 void *r = _Jv_AllocBytes (size);
275 if (! r)
276 _Jv_Throw (no_memory);
277 return r;
278 }
279
280 // Allocate a new object of class C. SIZE is the size of the object
281 // to allocate. You might think this is redundant, but it isn't; some
282 // classes, such as String, aren't of fixed size.
283 jobject
284 _Jv_AllocObject (jclass c, jint size)
285 {
286 _Jv_InitClass (c);
287
288 jobject obj = (jobject) _Jv_AllocObj (size);
289 if (! obj)
290 JvThrow (no_memory);
291 *((_Jv_VTable **) obj) = c->vtable;
292
293 // If this class has inherited finalize from Object, then don't
294 // bother registering a finalizer. We know that finalize() is the
295 // very first method after the dummy entry. If this turns out to be
296 // unreliable, a more robust implementation can be written. Such an
297 // implementation would look for Object.finalize in Object's method
298 // table at startup, and then use that information to find the
299 // appropriate index in the method vector.
300 if (c->vtable->method[1] != ObjectClass.vtable->method[1])
301 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
302
303 return obj;
304 }
305
306 // Allocate a new array of Java objects. Each object is of type
307 // `elementClass'. `init' is used to initialize each slot in the
308 // array.
309 jobjectArray
310 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
311 {
312 if (count < 0)
313 JvThrow (new java::lang::NegativeArraySizeException);
314
315 // Check for overflow.
316 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / sizeof (jobject))
317 JvThrow (no_memory);
318
319 size_t size = count * sizeof (jobject) + sizeof (__JArray);
320
321 // FIXME: second argument should be "current loader" //
322 jclass clas = _Jv_FindArrayClass (elementClass, 0);
323
324 jobjectArray obj = (jobjectArray) _Jv_AllocArray (size);
325 if (! obj)
326 JvThrow (no_memory);
327 obj->length = count;
328 jobject* ptr = elements(obj);
329 // We know the allocator returns zeroed memory. So don't bother
330 // zeroing it again.
331 if (init)
332 {
333 while (--count >= 0)
334 *ptr++ = init;
335 }
336 // Set the vtbl last to avoid problems if the GC happens during the
337 // window in this function between the allocation and this
338 // assignment.
339 *((_Jv_VTable **) obj) = clas->vtable;
340 return obj;
341 }
342
343 // Allocate a new array of primitives. ELTYPE is the type of the
344 // element, COUNT is the size of the array.
345 jobject
346 _Jv_NewPrimArray (jclass eltype, jint count)
347 {
348 int elsize = eltype->size();
349 if (count < 0)
350 JvThrow (new java::lang::NegativeArraySizeException ());
351
352 // Check for overflow.
353 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / elsize)
354 JvThrow (no_memory);
355
356 __JArray *arr = (__JArray*) _Jv_AllocObj (sizeof (__JArray)
357 + elsize * count);
358 if (! arr)
359 JvThrow (no_memory);
360 arr->length = count;
361 // Note that we assume we are given zeroed memory by the allocator.
362
363 jclass klass = _Jv_FindArrayClass (eltype, 0);
364 // Set the vtbl last to avoid problems if the GC happens during the
365 // window in this function between the allocation and this
366 // assignment.
367 *((_Jv_VTable **) arr) = klass->vtable;
368 return arr;
369 }
370
371 jcharArray
372 JvNewCharArray (jint length)
373 {
374 return (jcharArray) _Jv_NewPrimArray (JvPrimClass (char), length);
375 }
376
377 jbooleanArray
378 JvNewBooleanArray (jint length)
379 {
380 return (jbooleanArray) _Jv_NewPrimArray (JvPrimClass (boolean), length);
381 }
382
383 jbyteArray
384 JvNewByteArray (jint length)
385 {
386 return (jbyteArray) _Jv_NewPrimArray (JvPrimClass (byte), length);
387 }
388
389 jshortArray
390 JvNewShortArray (jint length)
391 {
392 return (jshortArray) _Jv_NewPrimArray (JvPrimClass (short), length);
393 }
394
395 jintArray
396 JvNewIntArray (jint length)
397 {
398 return (jintArray) _Jv_NewPrimArray (JvPrimClass (int), length);
399 }
400
401 jlongArray
402 JvNewLongArray (jint length)
403 {
404 return (jlongArray) _Jv_NewPrimArray (JvPrimClass (long), length);
405 }
406
407 jfloatArray
408 JvNewFloatArray (jint length)
409 {
410 return (jfloatArray) _Jv_NewPrimArray (JvPrimClass (float), length);
411 }
412
413 jdoubleArray
414 JvNewDoubleArray (jint length)
415 {
416 return (jdoubleArray) _Jv_NewPrimArray (JvPrimClass (double), length);
417 }
418
419 jobject
420 _Jv_NewArray (jint type, jint size)
421 {
422 switch (type)
423 {
424 case 4: return JvNewBooleanArray (size);
425 case 5: return JvNewCharArray (size);
426 case 6: return JvNewFloatArray (size);
427 case 7: return JvNewDoubleArray (size);
428 case 8: return JvNewByteArray (size);
429 case 9: return JvNewShortArray (size);
430 case 10: return JvNewIntArray (size);
431 case 11: return JvNewLongArray (size);
432 }
433 JvFail ("newarray - bad type code");
434 return NULL; // Placate compiler.
435 }
436
437 jobject
438 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
439 {
440 JvAssert (type->isArray());
441 jclass element_type = type->getComponentType();
442 jobject result;
443 if (element_type->isPrimitive())
444 result = _Jv_NewPrimArray (element_type, sizes[0]);
445 else
446 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
447
448 if (dimensions > 1)
449 {
450 JvAssert (! element_type->isPrimitive());
451 JvAssert (element_type->isArray());
452 jobject *contents = elements ((jobjectArray) result);
453 for (int i = 0; i < sizes[0]; ++i)
454 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
455 sizes + 1);
456 }
457
458 return result;
459 }
460
461 jobject
462 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
463 {
464 va_list args;
465 jint sizes[dimensions];
466 va_start (args, dimensions);
467 for (int i = 0; i < dimensions; ++i)
468 {
469 jint size = va_arg (args, jint);
470 sizes[i] = size;
471 }
472 va_end (args);
473
474 return _Jv_NewMultiArray (array_type, dimensions, sizes);
475 }
476
477 \f
478
479 class _Jv_PrimClass : public java::lang::Class
480 {
481 public:
482 // FIXME: calling convention is weird. If we use the natural types
483 // then the compiler will complain because they aren't Java types.
484 _Jv_PrimClass (jobject cname, jbyte sig, jint len)
485 {
486 using namespace java::lang::reflect;
487
488 // We must initialize every field of the class. We do this in
489 // the same order they are declared in Class.h.
490 next = NULL;
491 name = _Jv_makeUtf8Const ((char *) cname, -1);
492 accflags = Modifier::PUBLIC | Modifier::FINAL;
493 superclass = NULL;
494 constants.size = 0;
495 constants.tags = NULL;
496 constants.data = NULL;
497 methods = NULL;
498 method_count = sig;
499 vtable_method_count = 0;
500 fields = NULL;
501 size_in_bytes = len;
502 field_count = 0;
503 static_field_count = 0;
504 vtable = JV_PRIMITIVE_VTABLE;
505 interfaces = NULL;
506 loader = NULL;
507 interface_count = 0;
508 state = 0; // FIXME.
509 thread = NULL;
510 }
511 };
512
513 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
514 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
515
516 DECLARE_PRIM_TYPE(byte, 'B', 1);
517 DECLARE_PRIM_TYPE(short, 'S', 2);
518 DECLARE_PRIM_TYPE(int, 'I', 4);
519 DECLARE_PRIM_TYPE(long, 'J', 8);
520 DECLARE_PRIM_TYPE(boolean, 'Z', 1);
521 DECLARE_PRIM_TYPE(char, 'C', 2);
522 DECLARE_PRIM_TYPE(float, 'F', 4);
523 DECLARE_PRIM_TYPE(double, 'D', 8);
524 DECLARE_PRIM_TYPE(void, 'V', 0);
525
526 jclass
527 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
528 {
529 switch (*sig)
530 {
531 case 'B':
532 return JvPrimClass (byte);
533 case 'S':
534 return JvPrimClass (short);
535 case 'I':
536 return JvPrimClass (int);
537 case 'J':
538 return JvPrimClass (long);
539 case 'Z':
540 return JvPrimClass (boolean);
541 case 'C':
542 return JvPrimClass (char);
543 case 'F':
544 return JvPrimClass (float);
545 case 'D':
546 return JvPrimClass (double);
547 case 'V':
548 return JvPrimClass (void);
549 case 'L':
550 {
551 int i;
552 for (i = 1; sig[i] && sig[i] != ';'; ++i)
553 ;
554 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
555 return _Jv_FindClass (name, loader);
556
557 }
558 case '[':
559 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader),
560 loader);
561 }
562 JvFail ("couldn't understand class signature");
563 return NULL; // Placate compiler.
564 }
565
566 \f
567
568 JArray<jstring> *
569 JvConvertArgv (int argc, const char **argv)
570 {
571 if (argc < 0)
572 argc = 0;
573 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
574 jobject* ptr = elements(ar);
575 for (int i = 0; i < argc; i++)
576 {
577 const char *arg = argv[i];
578 // FIXME - should probably use JvNewStringUTF.
579 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
580 }
581 return (JArray<jstring>*) ar;
582 }
583
584 // FIXME: These variables are static so that they will be
585 // automatically scanned by the Boehm collector. This is needed
586 // because with qthreads the collector won't scan the initial stack --
587 // it will only scan the qthreads stacks.
588
589 // Command line arguments.
590 static jobject arg_vec;
591
592 // The primary threadgroup.
593 static java::lang::ThreadGroup *main_group;
594
595 // The primary thread.
596 static java::lang::Thread *main_thread;
597
598 void
599 JvRunMain (jclass klass, int argc, const char **argv)
600 {
601 INIT_SEGV;
602 #ifdef HANDLE_FPE
603 INIT_FPE;
604 #else
605 arithexception = new java::lang::ArithmeticException
606 (JvNewStringLatin1 ("/ by zero"));
607 #endif
608
609 no_memory = new java::lang::OutOfMemoryError;
610
611 #ifdef USE_LTDL
612 LTDL_SET_PRELOADED_SYMBOLS ();
613 #endif
614
615 arg_vec = JvConvertArgv (argc - 1, argv + 1);
616 main_group = new java::lang::ThreadGroup (23);
617 main_thread = new java::lang::FirstThread (main_group, klass, arg_vec);
618
619 main_thread->start();
620 _Jv_ThreadWait ();
621
622 java::lang::Runtime::getRuntime ()->exit (0);
623 }
624
625 void
626 _Jv_RunMain (const char *class_name, int argc, const char **argv)
627 {
628 INIT_SEGV;
629 #ifdef HANDLE_FPE
630 INIT_FPE;
631 #else
632 arithexception = new java::lang::ArithmeticException
633 (JvNewStringLatin1 ("/ by zero"));
634 #endif
635
636 no_memory = new java::lang::OutOfMemoryError;
637
638 #ifdef USE_LTDL
639 LTDL_SET_PRELOADED_SYMBOLS ();
640 #endif
641
642 arg_vec = JvConvertArgv (argc - 1, argv + 1);
643 main_group = new java::lang::ThreadGroup (23);
644 main_thread = new java::lang::FirstThread (main_group,
645 JvNewStringLatin1 (class_name),
646 arg_vec);
647 main_thread->start();
648 _Jv_ThreadWait ();
649
650 java::lang::Runtime::getRuntime ()->exit (0);
651 }
652
653
654 \f
655
656 void *
657 _Jv_Malloc (jsize size)
658 {
659 if (size == 0)
660 size = 1;
661 void *ptr = malloc ((size_t) size);
662 if (ptr == NULL)
663 JvThrow (no_memory);
664 return ptr;
665 }
666
667 void
668 _Jv_Free (void* ptr)
669 {
670 return free (ptr);
671 }
672
673 \f
674
675 // In theory, these routines can be #ifdef'd away on machines which
676 // support divide overflow signals. However, we never know if some
677 // code might have been compiled with "-fuse-divide-subroutine", so we
678 // always include them in libgcj.
679
680 jint
681 _Jv_divI (jint dividend, jint divisor)
682 {
683 if (divisor == 0)
684 _Jv_Throw (arithexception);
685
686 if (dividend == (jint) 0x80000000L && divisor == -1)
687 return dividend;
688
689 return dividend / divisor;
690 }
691
692 jint
693 _Jv_remI (jint dividend, jint divisor)
694 {
695 if (divisor == 0)
696 _Jv_Throw (arithexception);
697
698 if (dividend == (jint) 0x80000000L && divisor == -1)
699 return 0;
700
701 return dividend % divisor;
702 }
703
704 jlong
705 _Jv_divJ (jlong dividend, jlong divisor)
706 {
707 if (divisor == 0)
708 _Jv_Throw (arithexception);
709
710 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
711 return dividend;
712
713 return dividend / divisor;
714 }
715
716 jlong
717 _Jv_remJ (jlong dividend, jlong divisor)
718 {
719 if (divisor == 0)
720 _Jv_Throw (arithexception);
721
722 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
723 return 0;
724
725 return dividend % divisor;
726 }
727
728
729
730
731
732
733
This page took 0.077331 seconds and 6 git commands to generate.