]> gcc.gnu.org Git - gcc.git/blob - libjava/prims.cc
prims.cc (catch_fpe): Call to HANDLE_DIVIDE_OVERFLOW added.
[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 "java-array.h"
19
20 #include <cni.h>
21 #include <jvm.h>
22 #include <java-signal.h>
23
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>
41
42 #ifdef USE_LTDL
43 #include <ltdl.h>
44 #endif
45
46 #define ObjectClass _CL_Q34java4lang6Object
47 extern java::lang::Class ObjectClass;
48
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;
52
53 // Largest representable size_t.
54 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
55
56 \f
57
58 #ifdef HANDLE_SEGV
59 static java::lang::NullPointerException *nullp;
60 SIGNAL_HANDLER (catch_segv)
61 {
62 MAKE_THROW_FRAME;
63 _Jv_Throw (nullp);
64 }
65 #endif
66
67 #ifdef HANDLE_FPE
68 static java::lang::ArithmeticException *arithexception;
69 SIGNAL_HANDLER (catch_fpe)
70 {
71 #ifdef HANDLE_DIVIDE_OVERFLOW
72 HANDLE_DIVIDE_OVERFLOW;
73 #else
74 MAKE_THROW_FRAME;
75 #endif
76 _Jv_Throw (arithexception);
77 }
78 #endif
79
80 \f
81
82 jboolean
83 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
84 {
85 register int len;
86 register _Jv_ushort *aptr, *bptr;
87 if (a == b)
88 return true;
89 if (a->hash != b->hash)
90 return false;
91 len = a->length;
92 if (b->length != len)
93 return false;
94 aptr = (_Jv_ushort *)a->data;
95 bptr = (_Jv_ushort *)b->data;
96 len = (len + 1) >> 1;
97 while (--len >= 0)
98 if (*aptr++ != *bptr++)
99 return false;
100 return true;
101 }
102
103 /* True iff A is equal to STR.
104 HASH is STR->hashCode().
105 */
106
107 jboolean
108 _Jv_equal (Utf8Const* a, jstring str, jint hash)
109 {
110 if (a->hash != (_Jv_ushort) hash)
111 return false;
112 jint len = str->length();
113 jint i = 0;
114 jchar *sptr = _Jv_GetStringChars (str);
115 register unsigned char* ptr = (unsigned char*) a->data;
116 register unsigned char* limit = ptr + a->length;
117 for (;; i++, sptr++)
118 {
119 int ch = UTF8_GET (ptr, limit);
120 if (i == len)
121 return ch < 0;
122 if (ch != *sptr)
123 return false;
124 }
125 return true;
126 }
127
128 /* Count the number of Unicode chars encoded in a given Ut8 string. */
129 int
130 _Jv_strLengthUtf8(char* str, int len)
131 {
132 register unsigned char* ptr;
133 register unsigned char* limit;
134 int str_length;
135
136 ptr = (unsigned char*) str;
137 limit = ptr + len;
138 str_length = 0;
139 for (; ptr < limit; str_length++) {
140 if (UTF8_GET (ptr, limit) < 0) {
141 return (-1);
142 }
143 }
144 return (str_length);
145 }
146
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.
149 */
150 static jint
151 hashUtf8String (char* str, int len)
152 {
153 register unsigned char* ptr = (unsigned char*) str;
154 register unsigned char* limit = ptr + len;
155 jint hash = 0;
156
157 for (; ptr < limit;)
158 {
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;
163 }
164 return hash;
165 }
166
167 _Jv_Utf8Const *
168 _Jv_makeUtf8Const (char* s, int len)
169 {
170 if (len < 0)
171 len = strlen (s);
172 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
173 if (! m)
174 JvThrow (no_memory);
175 memcpy (m->data, s, len);
176 m->data[len] = 0;
177 m->length = len;
178 m->hash = hashUtf8String (s, len) & 0xFFFF;
179 return (m);
180 }
181
182 \f
183
184 #ifdef DEBUG
185 void
186 _Jv_Abort (const char *function, const char *file, int line,
187 const char *message)
188 #else
189 void
190 _Jv_Abort (const char *, const char *, int, const char *message)
191 #endif
192 {
193 #ifdef DEBUG
194 fprintf (stderr,
195 "libgcj failure: %s\n in function %s, file %s, line %d\n",
196 message, function, file, line);
197 #else
198 java::io::PrintStream *err = java::lang::System::err;
199 err->print(JvNewStringLatin1 ("libgcj failure: "));
200 err->println(JvNewStringLatin1 (message));
201 err->flush();
202 #endif
203 abort ();
204 }
205
206 static void
207 fail_on_finalization (jobject)
208 {
209 JvFail ("object was finalized");
210 }
211
212 void
213 _Jv_GCWatch (jobject obj)
214 {
215 _Jv_RegisterFinalizer (obj, fail_on_finalization);
216 }
217
218 void
219 _Jv_ThrowBadArrayIndex(jint bad_index)
220 {
221 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
222 (java::lang::String::valueOf(bad_index)));
223 }
224
225 void*
226 _Jv_CheckCast (jclass c, jobject obj)
227 {
228 if (obj != NULL && ! c->isAssignableFrom(obj->getClass()))
229 JvThrow (new java::lang::ClassCastException);
230 return obj;
231 }
232
233 void
234 _Jv_CheckArrayStore (jobject arr, jobject obj)
235 {
236 if (obj)
237 {
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);
245 }
246 }
247
248 \f
249
250 // Allocate some unscanned memory and throw an exception if no memory.
251 void *
252 _Jv_AllocBytesChecked (jsize size)
253 {
254 void *r = _Jv_AllocBytes (size);
255 if (! r)
256 _Jv_Throw (no_memory);
257 return r;
258 }
259
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.
263 jobject
264 _Jv_AllocObject (jclass c, jint size)
265 {
266 _Jv_InitClass (c);
267
268 jobject obj = (jobject) _Jv_AllocObj (size);
269 if (! obj)
270 JvThrow (no_memory);
271 *((_Jv_VTable **) obj) = c->vtable;
272
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);
282
283 return obj;
284 }
285
286 // Allocate a new array of Java objects. Each object is of type
287 // `elementClass'. `init' is used to initialize each slot in the
288 // array.
289 jobjectArray
290 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
291 {
292 if (count < 0)
293 JvThrow (new java::lang::NegativeArraySizeException);
294
295 // Check for overflow.
296 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / sizeof (jobject))
297 JvThrow (no_memory);
298
299 size_t size = count * sizeof (jobject) + sizeof (__JArray);
300 jclass clas = _Jv_FindArrayClass (elementClass);
301 jobjectArray obj = (jobjectArray) _Jv_AllocArray (size);
302 if (! obj)
303 JvThrow (no_memory);
304 obj->length = count;
305 jobject* ptr = elements(obj);
306 // We know the allocator returns zeroed memory. So don't bother
307 // zeroing it again.
308 if (init)
309 {
310 while (--count >= 0)
311 *ptr++ = init;
312 }
313 // Set the vtbl last to avoid problems if the GC happens during the
314 // window in this function between the allocation and this
315 // assignment.
316 *((_Jv_VTable **) obj) = clas->vtable;
317 return obj;
318 }
319
320 // Allocate a new array of primitives. ELTYPE is the type of the
321 // element, COUNT is the size of the array.
322 jobject
323 _Jv_NewPrimArray (jclass eltype, jint count)
324 {
325 int elsize = eltype->size();
326 if (count < 0)
327 JvThrow (new java::lang::NegativeArraySizeException ());
328
329 // Check for overflow.
330 if ((size_t) count > (SIZE_T_MAX - sizeof (__JArray)) / elsize)
331 JvThrow (no_memory);
332
333 __JArray *arr = (__JArray*) _Jv_AllocObj (sizeof (__JArray)
334 + elsize * count);
335 if (! arr)
336 JvThrow (no_memory);
337 arr->length = count;
338 // Note that we assume we are given zeroed memory by the allocator.
339
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
343 // assignment.
344 *((_Jv_VTable **) arr) = klass->vtable;
345 return arr;
346 }
347
348 jcharArray
349 JvNewCharArray (jint length)
350 {
351 return (jcharArray) _Jv_NewPrimArray (JvPrimClass (char), length);
352 }
353
354 jbooleanArray
355 JvNewBooleanArray (jint length)
356 {
357 return (jbooleanArray) _Jv_NewPrimArray (JvPrimClass (boolean), length);
358 }
359
360 jbyteArray
361 JvNewByteArray (jint length)
362 {
363 return (jbyteArray) _Jv_NewPrimArray (JvPrimClass (byte), length);
364 }
365
366 jshortArray
367 JvNewShortArray (jint length)
368 {
369 return (jshortArray) _Jv_NewPrimArray (JvPrimClass (short), length);
370 }
371
372 jintArray
373 JvNewIntArray (jint length)
374 {
375 return (jintArray) _Jv_NewPrimArray (JvPrimClass (int), length);
376 }
377
378 jlongArray
379 JvNewLongArray (jint length)
380 {
381 return (jlongArray) _Jv_NewPrimArray (JvPrimClass (long), length);
382 }
383
384 jfloatArray
385 JvNewFloatArray (jint length)
386 {
387 return (jfloatArray) _Jv_NewPrimArray (JvPrimClass (float), length);
388 }
389
390 jdoubleArray
391 JvNewDoubleArray (jint length)
392 {
393 return (jdoubleArray) _Jv_NewPrimArray (JvPrimClass (double), length);
394 }
395
396 jobject
397 _Jv_NewArray (jint type, jint size)
398 {
399 switch (type)
400 {
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);
409 }
410 JvFail ("newarray - bad type code");
411 return NULL; // Placate compiler.
412 }
413
414 jobject
415 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
416 {
417 JvAssert (type->isArray());
418 jclass element_type = type->getComponentType();
419 jobject result;
420 if (element_type->isPrimitive())
421 result = _Jv_NewPrimArray (element_type, sizes[0]);
422 else
423 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
424
425 if (dimensions > 1)
426 {
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,
432 sizes + 1);
433 }
434
435 return result;
436 }
437
438 jobject
439 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
440 {
441 va_list args;
442 jint sizes[dimensions];
443 va_start (args, dimensions);
444 for (int i = 0; i < dimensions; ++i)
445 {
446 jint size = va_arg (args, jint);
447 sizes[i] = size;
448 }
449 va_end (args);
450
451 return _Jv_NewMultiArray (array_type, dimensions, sizes);
452 }
453
454 \f
455
456 class _Jv_PrimClass : public java::lang::Class
457 {
458 public:
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)
462 {
463 using namespace java::lang::reflect;
464
465 // We must initialize every field of the class. We do this in
466 // the same order they are declared in Class.h.
467 next = NULL;
468 name = _Jv_makeUtf8Const ((char *) cname, -1);
469 accflags = Modifier::PUBLIC | Modifier::FINAL;
470 superclass = NULL;
471 constants.size = 0;
472 constants.tags = NULL;
473 constants.data = NULL;
474 methods = NULL;
475 method_count = sig;
476 vtable_method_count = 0;
477 fields = NULL;
478 size_in_bytes = len;
479 field_count = 0;
480 static_field_count = 0;
481 vtable = JV_PRIMITIVE_VTABLE;
482 interfaces = NULL;
483 loader = NULL;
484 interface_count = 0;
485 state = 0; // FIXME.
486 thread = NULL;
487 }
488 };
489
490 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
491 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
492
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);
502
503 jclass
504 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
505 {
506 switch (*sig)
507 {
508 case 'B':
509 return JvPrimClass (byte);
510 case 'S':
511 return JvPrimClass (short);
512 case 'I':
513 return JvPrimClass (int);
514 case 'J':
515 return JvPrimClass (long);
516 case 'Z':
517 return JvPrimClass (boolean);
518 case 'C':
519 return JvPrimClass (char);
520 case 'F':
521 return JvPrimClass (float);
522 case 'D':
523 return JvPrimClass (double);
524 case 'V':
525 return JvPrimClass (void);
526 case 'L':
527 {
528 int i;
529 for (i = 1; sig[i] && sig[i] != ';'; ++i)
530 ;
531 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
532 return _Jv_FindClass (name, loader);
533 }
534 case '[':
535 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader));
536 }
537 JvFail ("couldn't understand class signature");
538 return NULL; // Placate compiler.
539 }
540
541 \f
542
543 JArray<jstring> *
544 JvConvertArgv (int argc, const char **argv)
545 {
546 if (argc < 0)
547 argc = 0;
548 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
549 jobject* ptr = elements(ar);
550 for (int i = 0; i < argc; i++)
551 {
552 const char *arg = argv[i];
553 // FIXME - should probably use JvNewStringUTF.
554 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
555 }
556 return (JArray<jstring>*) ar;
557 }
558
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.
563
564 // Command line arguments.
565 static jobject arg_vec;
566
567 // The primary threadgroup.
568 static java::lang::ThreadGroup *main_group;
569
570 // The primary thread.
571 static java::lang::Thread *main_thread;
572
573 void
574 JvRunMain (jclass klass, int argc, const char **argv)
575 {
576 INIT_SEGV;
577 INIT_FPE;
578
579 no_memory = new java::lang::OutOfMemoryError;
580
581 #ifdef USE_LTDL
582 LTDL_SET_PRELOADED_SYMBOLS ();
583 #endif
584
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);
588
589 main_thread->start();
590 _Jv_ThreadWait ();
591
592 java::lang::Runtime::getRuntime ()->exit (0);
593 }
594
595 \f
596
597 void *
598 _Jv_Malloc (jsize size)
599 {
600 if (size == 0)
601 size = 1;
602 void *ptr = malloc ((size_t) size);
603 if (ptr == NULL)
604 JvThrow (no_memory);
605 return ptr;
606 }
607
608 void
609 _Jv_Free (void* ptr)
610 {
611 return free (ptr);
612 }
This page took 0.071459 seconds and 6 git commands to generate.