]> gcc.gnu.org Git - gcc.git/blame - libjava/prims.cc
configure.in: Define GC_GCJ_SUPPORT.
[gcc.git] / libjava / prims.cc
CommitLineData
ee9dd372
TT
1// prims.cc - Code for core of runtime environment.
2
2ba5f774 3/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
ee9dd372
TT
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11#include <config.h>
12
878885b4
TT
13#ifdef USE_WIN32_SIGNALLING
14#include <windows.h>
15#endif /* USE_WIN32_SIGNALLING */
16
17#ifdef USE_WINSOCK
18#undef __INSIDE_CYGWIN__
19#include <winsock.h>
20#endif /* USE_WINSOCK */
21
ee9dd372
TT
22#include <stdlib.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <string.h>
9b0cb287 26#include <signal.h>
ee9dd372 27
283a159f
AH
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31
27e934d8 32#include <gcj/cni.h>
ee9dd372 33#include <jvm.h>
a4e44caa 34#include <java-signal.h>
27e934d8 35#include <java-threads.h>
ee9dd372 36
54c2f04b
AG
37#ifdef ENABLE_JVMPI
38#include <jvmpi.h>
39#endif
40
ffccc6be
AG
41#ifndef DISABLE_GETENV_PROPERTIES
42#include <ctype.h>
43#include <java-props.h>
44#define PROCESS_GCJ_PROPERTIES process_gcj_properties()
45#else
46#define PROCESS_GCJ_PROPERTIES
47#endif // DISABLE_GETENV_PROPERTIES
48
ee9dd372 49#include <java/lang/Class.h>
6aaeb975 50#include <java/lang/ClassLoader.h>
ee9dd372
TT
51#include <java/lang/Runtime.h>
52#include <java/lang/String.h>
53#include <java/lang/Thread.h>
54#include <java/lang/ThreadGroup.h>
6cc96432 55#include <gnu/gcj/runtime/FirstThread.h>
ee9dd372 56#include <java/lang/ArrayIndexOutOfBoundsException.h>
a4e44caa 57#include <java/lang/ArithmeticException.h>
ee9dd372 58#include <java/lang/ClassFormatError.h>
ee9dd372
TT
59#include <java/lang/NegativeArraySizeException.h>
60#include <java/lang/NullPointerException.h>
61#include <java/lang/OutOfMemoryError.h>
ee9dd372
TT
62#include <java/lang/System.h>
63#include <java/lang/reflect/Modifier.h>
64#include <java/io/PrintStream.h>
65
9096b279
TT
66#ifdef USE_LTDL
67#include <ltdl.h>
68#endif
a4e44caa 69
ee9dd372
TT
70#define ObjectClass _CL_Q34java4lang6Object
71extern java::lang::Class ObjectClass;
72
73// We allocate a single OutOfMemoryError exception which we keep
74// around for use if we run out of memory.
75static java::lang::OutOfMemoryError *no_memory;
76
77// Largest representable size_t.
78#define SIZE_T_MAX ((size_t) (~ (size_t) 0))
79
b8c3c4f0
TT
80// Properties set at compile time.
81const char **_Jv_Compiler_Properties;
82
1a558147
AG
83// The JAR file to add to the beginning of java.class.path.
84const char *_Jv_Jar_Class_Path;
85
ffccc6be
AG
86#ifndef DISABLE_GETENV_PROPERTIES
87// Property key/value pairs.
88property_pair *_Jv_Environment_Properties;
89#endif
283a159f
AH
90
91// The name of this executable.
92static char * _Jv_execName;
93
54c2f04b
AG
94#ifdef ENABLE_JVMPI
95// Pointer to JVMPI notification functions.
96void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
97void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
98void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
99#endif
ee9dd372
TT
100\f
101
6c80c45e
TT
102extern "C" void _Jv_ThrowSignal (void *) __attribute ((noreturn));
103
104// Just like _Jv_Throw, but fill in the stack trace first. Although
105// this is declared extern in order that its name not be mangled, it
106// is not intended to be used outside this file.
107void
108_Jv_ThrowSignal (void *e)
109{
110 java::lang::Throwable *throwable = (java::lang::Throwable *)e;
111 throwable->fillInStackTrace ();
112 _Jv_Throw (throwable);
113}
114
01f78a02
AH
115#ifdef HANDLE_SEGV
116static java::lang::NullPointerException *nullp;
6c80c45e 117
01f78a02
AH
118SIGNAL_HANDLER (catch_segv)
119{
6c80c45e
TT
120 MAKE_THROW_FRAME (nullp);
121 _Jv_ThrowSignal (nullp);
01f78a02
AH
122}
123#endif
124
01f78a02 125static java::lang::ArithmeticException *arithexception;
d342a2e1
AH
126
127#ifdef HANDLE_FPE
01f78a02
AH
128SIGNAL_HANDLER (catch_fpe)
129{
554b61bb
AH
130#ifdef HANDLE_DIVIDE_OVERFLOW
131 HANDLE_DIVIDE_OVERFLOW;
132#else
6c80c45e 133 MAKE_THROW_FRAME (arithexception);
554b61bb 134#endif
6c80c45e 135 _Jv_ThrowSignal (arithexception);
01f78a02
AH
136}
137#endif
138
139\f
140
ee9dd372
TT
141jboolean
142_Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
143{
6c80c45e
TT
144 int len;
145 _Jv_ushort *aptr, *bptr;
ee9dd372
TT
146 if (a == b)
147 return true;
148 if (a->hash != b->hash)
149 return false;
150 len = a->length;
151 if (b->length != len)
152 return false;
153 aptr = (_Jv_ushort *)a->data;
154 bptr = (_Jv_ushort *)b->data;
155 len = (len + 1) >> 1;
156 while (--len >= 0)
157 if (*aptr++ != *bptr++)
158 return false;
159 return true;
160}
161
162/* True iff A is equal to STR.
554b61bb 163 HASH is STR->hashCode().
ee9dd372
TT
164*/
165
166jboolean
167_Jv_equal (Utf8Const* a, jstring str, jint hash)
168{
169 if (a->hash != (_Jv_ushort) hash)
170 return false;
171 jint len = str->length();
172 jint i = 0;
173 jchar *sptr = _Jv_GetStringChars (str);
6c80c45e
TT
174 unsigned char* ptr = (unsigned char*) a->data;
175 unsigned char* limit = ptr + a->length;
ee9dd372 176 for (;; i++, sptr++)
0f918fea
TT
177 {
178 int ch = UTF8_GET (ptr, limit);
179 if (i == len)
180 return ch < 0;
181 if (ch != *sptr)
182 return false;
183 }
184 return true;
185}
186
187/* Like _Jv_equal, but stop after N characters. */
188jboolean
189_Jv_equaln (Utf8Const *a, jstring str, jint n)
190{
191 jint len = str->length();
192 jint i = 0;
193 jchar *sptr = _Jv_GetStringChars (str);
6c80c45e
TT
194 unsigned char* ptr = (unsigned char*) a->data;
195 unsigned char* limit = ptr + a->length;
0f918fea 196 for (; n-- > 0; i++, sptr++)
ee9dd372
TT
197 {
198 int ch = UTF8_GET (ptr, limit);
199 if (i == len)
200 return ch < 0;
201 if (ch != *sptr)
202 return false;
203 }
204 return true;
205}
206
207/* Count the number of Unicode chars encoded in a given Ut8 string. */
208int
209_Jv_strLengthUtf8(char* str, int len)
210{
6c80c45e
TT
211 unsigned char* ptr;
212 unsigned char* limit;
ee9dd372
TT
213 int str_length;
214
215 ptr = (unsigned char*) str;
216 limit = ptr + len;
217 str_length = 0;
218 for (; ptr < limit; str_length++) {
219 if (UTF8_GET (ptr, limit) < 0) {
220 return (-1);
221 }
222 }
223 return (str_length);
224}
225
226/* Calculate a hash value for a string encoded in Utf8 format.
227 * This returns the same hash value as specified or java.lang.String.hashCode.
228 */
229static jint
230hashUtf8String (char* str, int len)
231{
6c80c45e
TT
232 unsigned char* ptr = (unsigned char*) str;
233 unsigned char* limit = ptr + len;
ee9dd372
TT
234 jint hash = 0;
235
236 for (; ptr < limit;)
237 {
238 int ch = UTF8_GET (ptr, limit);
239 /* Updated specification from
240 http://www.javasoft.com/docs/books/jls/clarify.html. */
241 hash = (31 * hash) + ch;
242 }
243 return hash;
244}
245
246_Jv_Utf8Const *
247_Jv_makeUtf8Const (char* s, int len)
248{
249 if (len < 0)
250 len = strlen (s);
251 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
252 if (! m)
253 JvThrow (no_memory);
254 memcpy (m->data, s, len);
255 m->data[len] = 0;
256 m->length = len;
257 m->hash = hashUtf8String (s, len) & 0xFFFF;
258 return (m);
259}
260
58eb6e7c
AG
261_Jv_Utf8Const *
262_Jv_makeUtf8Const (jstring string)
263{
264 jint hash = string->hashCode ();
265 jint len = _Jv_GetStringUTFLength (string);
266
267 Utf8Const* m = (Utf8Const*)
268 _Jv_AllocBytesChecked (sizeof(Utf8Const) + len + 1);
269
270 m->hash = hash;
271 m->length = len;
272
273 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
274 m->data[len] = 0;
275
276 return m;
277}
278
ee9dd372
TT
279\f
280
281#ifdef DEBUG
282void
283_Jv_Abort (const char *function, const char *file, int line,
284 const char *message)
285#else
286void
287_Jv_Abort (const char *, const char *, int, const char *message)
288#endif
289{
290#ifdef DEBUG
291 fprintf (stderr,
292 "libgcj failure: %s\n in function %s, file %s, line %d\n",
293 message, function, file, line);
294#else
295 java::io::PrintStream *err = java::lang::System::err;
296 err->print(JvNewStringLatin1 ("libgcj failure: "));
297 err->println(JvNewStringLatin1 (message));
298 err->flush();
299#endif
300 abort ();
301}
302
303static void
304fail_on_finalization (jobject)
305{
306 JvFail ("object was finalized");
307}
308
309void
310_Jv_GCWatch (jobject obj)
311{
312 _Jv_RegisterFinalizer (obj, fail_on_finalization);
313}
314
315void
316_Jv_ThrowBadArrayIndex(jint bad_index)
317{
318 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
319 (java::lang::String::valueOf(bad_index)));
320}
321
40f4ae32
TT
322void
323_Jv_ThrowNullPointerException ()
324{
325 throw new java::lang::NullPointerException ();
326}
327
ee9dd372
TT
328// Allocate some unscanned memory and throw an exception if no memory.
329void *
330_Jv_AllocBytesChecked (jsize size)
331{
332 void *r = _Jv_AllocBytes (size);
333 if (! r)
334 _Jv_Throw (no_memory);
335 return r;
336}
337
338// Allocate a new object of class C. SIZE is the size of the object
339// to allocate. You might think this is redundant, but it isn't; some
340// classes, such as String, aren't of fixed size.
341jobject
342_Jv_AllocObject (jclass c, jint size)
343{
344 _Jv_InitClass (c);
345
346 jobject obj = (jobject) _Jv_AllocObj (size);
9d4c2148 347 if (__builtin_expect (! obj, false))
ee9dd372
TT
348 JvThrow (no_memory);
349 *((_Jv_VTable **) obj) = c->vtable;
350
351 // If this class has inherited finalize from Object, then don't
352 // bother registering a finalizer. We know that finalize() is the
353 // very first method after the dummy entry. If this turns out to be
354 // unreliable, a more robust implementation can be written. Such an
355 // implementation would look for Object.finalize in Object's method
356 // table at startup, and then use that information to find the
357 // appropriate index in the method vector.
358 if (c->vtable->method[1] != ObjectClass.vtable->method[1])
359 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
360
54c2f04b
AG
361#ifdef ENABLE_JVMPI
362 // Service JVMPI request.
363
9d4c2148 364 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
54c2f04b
AG
365 {
366 JVMPI_Event event;
367
368 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
369 event.env_id = NULL;
370 event.u.obj_alloc.arena_id = 0;
371 event.u.obj_alloc.class_id = (jobjectID) c;
372 event.u.obj_alloc.is_array = 0;
373 event.u.obj_alloc.size = size;
374 event.u.obj_alloc.obj_id = (jobjectID) obj;
375
376 _Jv_DisableGC ();
377 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
378 _Jv_EnableGC ();
379 }
380#endif
381
ee9dd372
TT
382 return obj;
383}
384
385// Allocate a new array of Java objects. Each object is of type
386// `elementClass'. `init' is used to initialize each slot in the
387// array.
388jobjectArray
389_Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
390{
9d4c2148 391 if (__builtin_expect (count < 0, false))
ee9dd372
TT
392 JvThrow (new java::lang::NegativeArraySizeException);
393
93d45562
TT
394 JvAssert (! elementClass->isPrimitive ());
395
396 jobjectArray obj = NULL;
397 size_t size = (size_t) _Jv_GetArrayElementFromElementType (obj,
398 elementClass);
399
ee9dd372 400 // Check for overflow.
1143c0a0 401 if (__builtin_expect ((size_t) count >
9d4c2148 402 (SIZE_T_MAX - size) / sizeof (jobject), false))
ee9dd372
TT
403 JvThrow (no_memory);
404
93d45562 405 size += count * sizeof (jobject);
58eb6e7c
AG
406
407 // FIXME: second argument should be "current loader" //
408 jclass clas = _Jv_FindArrayClass (elementClass, 0);
409
93d45562 410 obj = (jobjectArray) _Jv_AllocArray (size);
9d4c2148 411 if (__builtin_expect (! obj, false))
ee9dd372
TT
412 JvThrow (no_memory);
413 obj->length = count;
414 jobject* ptr = elements(obj);
415 // We know the allocator returns zeroed memory. So don't bother
416 // zeroing it again.
417 if (init)
418 {
419 while (--count >= 0)
420 *ptr++ = init;
421 }
422 // Set the vtbl last to avoid problems if the GC happens during the
423 // window in this function between the allocation and this
424 // assignment.
425 *((_Jv_VTable **) obj) = clas->vtable;
426 return obj;
427}
428
429// Allocate a new array of primitives. ELTYPE is the type of the
430// element, COUNT is the size of the array.
431jobject
432_Jv_NewPrimArray (jclass eltype, jint count)
433{
434 int elsize = eltype->size();
9d4c2148 435 if (__builtin_expect (count < 0, false))
ee9dd372
TT
436 JvThrow (new java::lang::NegativeArraySizeException ());
437
93d45562
TT
438 JvAssert (eltype->isPrimitive ());
439 jobject dummy = NULL;
440 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
441
ee9dd372 442 // Check for overflow.
1143c0a0 443 if (__builtin_expect ((size_t) count >
9d4c2148 444 (SIZE_T_MAX - size) / elsize, false))
ee9dd372
TT
445 JvThrow (no_memory);
446
93d45562 447 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count);
9d4c2148 448 if (__builtin_expect (! arr, false))
ee9dd372
TT
449 JvThrow (no_memory);
450 arr->length = count;
451 // Note that we assume we are given zeroed memory by the allocator.
452
58eb6e7c 453 jclass klass = _Jv_FindArrayClass (eltype, 0);
ee9dd372
TT
454 // Set the vtbl last to avoid problems if the GC happens during the
455 // window in this function between the allocation and this
456 // assignment.
457 *((_Jv_VTable **) arr) = klass->vtable;
458 return arr;
459}
460
ee9dd372
TT
461jobject
462_Jv_NewArray (jint type, jint size)
463{
464 switch (type)
465 {
466 case 4: return JvNewBooleanArray (size);
467 case 5: return JvNewCharArray (size);
468 case 6: return JvNewFloatArray (size);
469 case 7: return JvNewDoubleArray (size);
470 case 8: return JvNewByteArray (size);
471 case 9: return JvNewShortArray (size);
472 case 10: return JvNewIntArray (size);
473 case 11: return JvNewLongArray (size);
474 }
475 JvFail ("newarray - bad type code");
476 return NULL; // Placate compiler.
477}
478
479jobject
480_Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
481{
482 JvAssert (type->isArray());
483 jclass element_type = type->getComponentType();
484 jobject result;
485 if (element_type->isPrimitive())
486 result = _Jv_NewPrimArray (element_type, sizes[0]);
487 else
488 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
489
490 if (dimensions > 1)
491 {
492 JvAssert (! element_type->isPrimitive());
493 JvAssert (element_type->isArray());
494 jobject *contents = elements ((jobjectArray) result);
495 for (int i = 0; i < sizes[0]; ++i)
496 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
497 sizes + 1);
498 }
499
500 return result;
501}
502
503jobject
504_Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
505{
506 va_list args;
507 jint sizes[dimensions];
508 va_start (args, dimensions);
509 for (int i = 0; i < dimensions; ++i)
510 {
511 jint size = va_arg (args, jint);
512 sizes[i] = size;
513 }
514 va_end (args);
515
516 return _Jv_NewMultiArray (array_type, dimensions, sizes);
517}
518
519\f
520
ee9dd372
TT
521class _Jv_PrimClass : public java::lang::Class
522{
523public:
524 // FIXME: calling convention is weird. If we use the natural types
525 // then the compiler will complain because they aren't Java types.
c74e2214 526 _Jv_PrimClass (jobject cname, jbyte sig, jint len, jobject array_vtable)
ee9dd372
TT
527 {
528 using namespace java::lang::reflect;
529
530 // We must initialize every field of the class. We do this in
531 // the same order they are declared in Class.h.
532 next = NULL;
533 name = _Jv_makeUtf8Const ((char *) cname, -1);
534 accflags = Modifier::PUBLIC | Modifier::FINAL;
535 superclass = NULL;
536 constants.size = 0;
537 constants.tags = NULL;
538 constants.data = NULL;
539 methods = NULL;
540 method_count = sig;
541 vtable_method_count = 0;
542 fields = NULL;
543 size_in_bytes = len;
544 field_count = 0;
545 static_field_count = 0;
546 vtable = JV_PRIMITIVE_VTABLE;
547 interfaces = NULL;
548 loader = NULL;
549 interface_count = 0;
d655f87d 550 state = JV_STATE_DONE;
ee9dd372 551 thread = NULL;
c74e2214
TT
552
553 // Note that we have to set `methods' to NULL.
554 if (sig != 'V')
555 _Jv_FindArrayClass (this, NULL, (_Jv_VTable *) array_vtable);
ee9dd372
TT
556 }
557};
558
c74e2214
TT
559// We use this to define both primitive classes and the vtables for
560// arrays of primitive classes. The latter are given names so that we
561// can refer to them from the compiler, allowing us to construct
562// arrays of primitives statically.
ee9dd372 563#define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
c74e2214
TT
564 _Jv_ArrayVTable _Jv_##NAME##VTable; \
565 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN, \
566 (jobject) &_Jv_##NAME##VTable)
ee9dd372
TT
567
568DECLARE_PRIM_TYPE(byte, 'B', 1);
569DECLARE_PRIM_TYPE(short, 'S', 2);
570DECLARE_PRIM_TYPE(int, 'I', 4);
571DECLARE_PRIM_TYPE(long, 'J', 8);
572DECLARE_PRIM_TYPE(boolean, 'Z', 1);
573DECLARE_PRIM_TYPE(char, 'C', 2);
574DECLARE_PRIM_TYPE(float, 'F', 4);
575DECLARE_PRIM_TYPE(double, 'D', 8);
576DECLARE_PRIM_TYPE(void, 'V', 0);
577
578jclass
579_Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
580{
581 switch (*sig)
582 {
583 case 'B':
584 return JvPrimClass (byte);
585 case 'S':
586 return JvPrimClass (short);
587 case 'I':
588 return JvPrimClass (int);
589 case 'J':
590 return JvPrimClass (long);
591 case 'Z':
592 return JvPrimClass (boolean);
593 case 'C':
594 return JvPrimClass (char);
595 case 'F':
596 return JvPrimClass (float);
597 case 'D':
598 return JvPrimClass (double);
599 case 'V':
600 return JvPrimClass (void);
601 case 'L':
602 {
603 int i;
604 for (i = 1; sig[i] && sig[i] != ';'; ++i)
605 ;
606 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
607 return _Jv_FindClass (name, loader);
58eb6e7c 608
ee9dd372
TT
609 }
610 case '[':
58eb6e7c
AG
611 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader),
612 loader);
ee9dd372
TT
613 }
614 JvFail ("couldn't understand class signature");
615 return NULL; // Placate compiler.
616}
617
618\f
619
620JArray<jstring> *
621JvConvertArgv (int argc, const char **argv)
622{
623 if (argc < 0)
624 argc = 0;
625 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
626 jobject* ptr = elements(ar);
627 for (int i = 0; i < argc; i++)
628 {
629 const char *arg = argv[i];
630 // FIXME - should probably use JvNewStringUTF.
631 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
632 }
633 return (JArray<jstring>*) ar;
634}
635
636// FIXME: These variables are static so that they will be
637// automatically scanned by the Boehm collector. This is needed
638// because with qthreads the collector won't scan the initial stack --
639// it will only scan the qthreads stacks.
640
641// Command line arguments.
642static jobject arg_vec;
643
ee9dd372
TT
644// The primary thread.
645static java::lang::Thread *main_thread;
646
283a159f
AH
647char *
648_Jv_ThisExecutable (void)
649{
650 return _Jv_execName;
651}
652
653void
654_Jv_ThisExecutable (const char *name)
655{
656 if (name)
657 {
658 _Jv_execName = new char[strlen (name) + 1];
659 strcpy (_Jv_execName, name);
660 }
661}
662
878885b4
TT
663#ifdef USE_WIN32_SIGNALLING
664
665extern "C" int* win32_get_restart_frame (void *);
666
667LONG CALLBACK
668win32_exception_handler (LPEXCEPTION_POINTERS e)
669{
670 int* setjmp_buf;
671 if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
672 setjmp_buf = win32_get_restart_frame (nullp);
673 else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
674 setjmp_buf = win32_get_restart_frame (arithexception);
675 else
676 return EXCEPTION_CONTINUE_SEARCH;
677
678 e->ContextRecord->Ebp = setjmp_buf[0];
679 // FIXME: Why does i386-signal.h increment the PC here, do we need to do it?
680 e->ContextRecord->Eip = setjmp_buf[1];
681 // FIXME: Is this the stack pointer? Do we need it?
682 e->ContextRecord->Esp = setjmp_buf[2];
683
684 return EXCEPTION_CONTINUE_EXECUTION;
685}
686
687#endif
688
9b0cb287 689static void
283a159f 690main_init ()
ee9dd372 691{
01f78a02 692 INIT_SEGV;
d342a2e1 693#ifdef HANDLE_FPE
01f78a02 694 INIT_FPE;
d342a2e1
AH
695#else
696 arithexception = new java::lang::ArithmeticException
697 (JvNewStringLatin1 ("/ by zero"));
698#endif
ee9dd372
TT
699
700 no_memory = new java::lang::OutOfMemoryError;
701
9096b279
TT
702#ifdef USE_LTDL
703 LTDL_SET_PRELOADED_SYMBOLS ();
704#endif
705
878885b4
TT
706#ifdef USE_WINSOCK
707 // Initialise winsock for networking
708 WSADATA data;
709 if (WSAStartup (MAKEWORD (1, 1), &data))
710 MessageBox (NULL, "Error initialising winsock library.", "Error", MB_OK | MB_ICONEXCLAMATION);
711#endif /* USE_WINSOCK */
712
713#ifdef USE_WIN32_SIGNALLING
714 // Install exception handler
715 SetUnhandledExceptionFilter (win32_exception_handler);
716#else
717 // We only want this on POSIX systems.
9b0cb287
TT
718 struct sigaction act;
719 act.sa_handler = SIG_IGN;
720 sigemptyset (&act.sa_mask);
721 act.sa_flags = 0;
722 sigaction (SIGPIPE, &act, NULL);
878885b4 723#endif /* USE_WIN32_SIGNALLING */
7e648cf9
TT
724
725 _Jv_JNI_Init ();
9b0cb287
TT
726}
727
ffccc6be
AG
728#ifndef DISABLE_GETENV_PROPERTIES
729
730static char *
731next_property_key (char *s, size_t *length)
732{
733 size_t l = 0;
734
735 JvAssert (s);
736
737 // Skip over whitespace
738 while (isspace (*s))
739 s++;
740
741 // If we've reached the end, return NULL. Also return NULL if for
742 // some reason we've come across a malformed property string.
743 if (*s == 0
744 || *s == ':'
745 || *s == '=')
746 return NULL;
747
748 // Determine the length of the property key.
749 while (s[l] != 0
750 && ! isspace (s[l])
751 && s[l] != ':'
752 && s[l] != '=')
753 {
754 if (s[l] == '\\'
755 && s[l+1] != 0)
756 l++;
757 l++;
758 }
759
760 *length = l;
761
762 return s;
763}
764
765static char *
766next_property_value (char *s, size_t *length)
767{
768 size_t l = 0;
769
770 JvAssert (s);
771
772 while (isspace (*s))
773 s++;
774
775 if (*s == ':'
776 || *s == '=')
777 s++;
778
779 while (isspace (*s))
780 s++;
781
782 // If we've reached the end, return NULL.
783 if (*s == 0)
784 return NULL;
785
786 // Determine the length of the property value.
787 while (s[l] != 0
788 && ! isspace (s[l])
789 && s[l] != ':'
790 && s[l] != '=')
791 {
792 if (s[l] == '\\'
793 && s[l+1] != 0)
794 l += 2;
795 else
796 l++;
797 }
798
799 *length = l;
800
801 return s;
802}
803
804static void
805process_gcj_properties ()
806{
807 char *props = getenv("GCJ_PROPERTIES");
808 char *p = props;
809 size_t length;
810 size_t property_count = 0;
811
812 if (NULL == props)
813 return;
814
815 // Whip through props quickly in order to count the number of
816 // property values.
817 while (p && (p = next_property_key (p, &length)))
818 {
819 // Skip to the end of the key
820 p += length;
821
822 p = next_property_value (p, &length);
823 if (p)
824 p += length;
825
826 property_count++;
827 }
828
829 // Allocate an array of property value/key pairs.
830 _Jv_Environment_Properties =
831 (property_pair *) malloc (sizeof(property_pair)
832 * (property_count + 1));
833
834 // Go through the properties again, initializing _Jv_Properties
835 // along the way.
836 p = props;
837 property_count = 0;
838 while (p && (p = next_property_key (p, &length)))
839 {
840 _Jv_Environment_Properties[property_count].key = p;
841 _Jv_Environment_Properties[property_count].key_length = length;
842
843 // Skip to the end of the key
844 p += length;
845
846 p = next_property_value (p, &length);
847
848 _Jv_Environment_Properties[property_count].value = p;
849 _Jv_Environment_Properties[property_count].value_length = length;
850
851 if (p)
852 p += length;
853
854 property_count++;
855 }
856 memset ((void *) &_Jv_Environment_Properties[property_count],
857 0, sizeof (property_pair));
858 {
859 size_t i = 0;
860
861 // Null terminate the strings.
862 while (_Jv_Environment_Properties[i].key)
863 {
864 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
865 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
866 }
867 }
868}
869#endif // DISABLE_GETENV_PROPERTIES
870
9b0cb287
TT
871void
872JvRunMain (jclass klass, int argc, const char **argv)
873{
ffccc6be
AG
874 PROCESS_GCJ_PROPERTIES;
875
9b0cb287 876 main_init ();
283a159f
AH
877#ifdef HAVE_PROC_SELF_EXE
878 char exec_name[20];
879 sprintf (exec_name, "/proc/%d/exe", getpid ());
880 _Jv_ThisExecutable (exec_name);
881#else
882 _Jv_ThisExecutable (argv[0]);
883#endif
9b0cb287 884
eb4534a6 885 arg_vec = JvConvertArgv (argc - 1, argv + 1);
6dfd8a77 886 main_thread = new gnu::gcj::runtime::FirstThread (klass, arg_vec);
eb4534a6
KKT
887
888 main_thread->start();
889 _Jv_ThreadWait ();
890
0f94c029
BM
891 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
892
893 java::lang::Runtime::getRuntime ()->exit (status);
eb4534a6
KKT
894}
895
896void
1a558147 897_Jv_RunMain (const char *name, int argc, const char **argv, bool is_jar)
eb4534a6 898{
1a558147 899 jstring class_name;
ffccc6be
AG
900 PROCESS_GCJ_PROPERTIES;
901
9b0cb287 902 main_init ();
ee9dd372 903
04476544
AH
904#ifdef HAVE_PROC_SELF_EXE
905 char exec_name[20];
906 sprintf (exec_name, "/proc/%d/exe", getpid ());
907 _Jv_ThisExecutable (exec_name);
908#endif
909
1a558147
AG
910 if (is_jar)
911 {
6aaeb975
AG
912 // name specifies a jar file. We must now extract the
913 // Main-Class attribute from the jar's manifest file. This is
914 // done by gnu.gcj.runtime.FirstThread.main.
1a558147
AG
915 _Jv_Jar_Class_Path = strdup (name);
916 arg_vec = JvConvertArgv (1, &_Jv_Jar_Class_Path);
917
918 main_thread =
919 new gnu::gcj::runtime::FirstThread (&_CL_Q43gnu3gcj7runtime11FirstThread,
920 arg_vec);
921 main_thread->start();
922 _Jv_ThreadWait ();
923
6aaeb975
AG
924 // FirstThread.main extracts the main class name and stores it
925 // here.
1a558147 926 class_name = gnu::gcj::runtime::FirstThread::jarMainClassName;
6aaeb975
AG
927
928 // We need a new ClassLoader because the classpath must be the
929 // jar file only. The easiest way to do this is to lose our
930 // reference to the previous classloader.
931 java::lang::ClassLoader::system = NULL;
1a558147
AG
932 }
933 else
934 class_name = JvNewStringLatin1 (name);
935
eb4534a6 936 arg_vec = JvConvertArgv (argc - 1, argv + 1);
ee9dd372 937
1a558147
AG
938 if (class_name)
939 {
940 main_thread = new gnu::gcj::runtime::FirstThread (class_name, arg_vec);
941 main_thread->start();
942 _Jv_ThreadWait ();
943 }
0f94c029
BM
944
945 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
946
947 java::lang::Runtime::getRuntime ()->exit (status);
ee9dd372
TT
948}
949
b8c3c4f0
TT
950\f
951
952// Parse a string and return a heap size.
953static size_t
954parse_heap_size (const char *spec)
955{
956 char *end;
957 unsigned long val = strtoul (spec, &end, 10);
3948f9d0
JS
958 if (*end == 'k' || *end == 'K')
959 val *= 1024;
960 else if (*end == 'm' || *end == 'M')
961 val *= 1048576;
b8c3c4f0
TT
962 return (size_t) val;
963}
964
965// Set the initial heap size. This might be ignored by the GC layer.
966// This must be called before _Jv_RunMain.
967void
968_Jv_SetInitialHeapSize (const char *arg)
969{
970 size_t size = parse_heap_size (arg);
971 _Jv_GCSetInitialHeapSize (size);
972}
973
974// Set the maximum heap size. This might be ignored by the GC layer.
975// This must be called before _Jv_RunMain.
976void
977_Jv_SetMaximumHeapSize (const char *arg)
978{
979 size_t size = parse_heap_size (arg);
980 _Jv_GCSetMaximumHeapSize (size);
981}
58eb6e7c 982
ee9dd372
TT
983\f
984
7e648cf9 985void *
ddf0fc6c 986_Jv_Malloc (jsize size)
7e648cf9 987{
9d4c2148 988 if (__builtin_expect (size == 0, false))
7e648cf9 989 size = 1;
ddf0fc6c 990 void *ptr = malloc ((size_t) size);
9d4c2148 991 if (__builtin_expect (ptr == NULL, false))
ddf0fc6c
BM
992 JvThrow (no_memory);
993 return ptr;
7e648cf9
TT
994}
995
ee9dd372 996void *
ddf0fc6c 997_Jv_Realloc (void *ptr, jsize size)
ee9dd372 998{
9d4c2148 999 if (__builtin_expect (size == 0, false))
ee9dd372 1000 size = 1;
ddf0fc6c 1001 ptr = realloc (ptr, (size_t) size);
9d4c2148 1002 if (__builtin_expect (ptr == NULL, false))
ee9dd372
TT
1003 JvThrow (no_memory);
1004 return ptr;
1005}
1006
ddf0fc6c
BM
1007void *
1008_Jv_MallocUnchecked (jsize size)
1009{
9d4c2148 1010 if (__builtin_expect (size == 0, false))
ddf0fc6c
BM
1011 size = 1;
1012 return malloc ((size_t) size);
1013}
1014
ee9dd372
TT
1015void
1016_Jv_Free (void* ptr)
1017{
1018 return free (ptr);
1019}
d342a2e1
AH
1020
1021\f
1022
1023// In theory, these routines can be #ifdef'd away on machines which
1024// support divide overflow signals. However, we never know if some
1025// code might have been compiled with "-fuse-divide-subroutine", so we
1026// always include them in libgcj.
1027
1028jint
1029_Jv_divI (jint dividend, jint divisor)
1030{
9d4c2148 1031 if (__builtin_expect (divisor == 0, false))
6c80c45e 1032 _Jv_ThrowSignal (arithexception);
d342a2e1 1033
58eb6e7c 1034 if (dividend == (jint) 0x80000000L && divisor == -1)
d342a2e1
AH
1035 return dividend;
1036
1037 return dividend / divisor;
1038}
1039
1040jint
1041_Jv_remI (jint dividend, jint divisor)
1042{
9d4c2148 1043 if (__builtin_expect (divisor == 0, false))
6c80c45e 1044 _Jv_ThrowSignal (arithexception);
d342a2e1 1045
58eb6e7c 1046 if (dividend == (jint) 0x80000000L && divisor == -1)
d342a2e1
AH
1047 return 0;
1048
1049 return dividend % divisor;
1050}
1051
1052jlong
1053_Jv_divJ (jlong dividend, jlong divisor)
1054{
9d4c2148 1055 if (__builtin_expect (divisor == 0, false))
6c80c45e 1056 _Jv_ThrowSignal (arithexception);
d342a2e1 1057
58eb6e7c 1058 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
d342a2e1
AH
1059 return dividend;
1060
1061 return dividend / divisor;
1062}
1063
1064jlong
1065_Jv_remJ (jlong dividend, jlong divisor)
1066{
9d4c2148 1067 if (__builtin_expect (divisor == 0, false))
6c80c45e 1068 _Jv_ThrowSignal (arithexception);
d342a2e1 1069
58eb6e7c 1070 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
d342a2e1
AH
1071 return 0;
1072
1073 return dividend % divisor;
1074}
This page took 0.241584 seconds and 5 git commands to generate.