Allow HIGH/LO_SUM in the prologue
[official-gcc.git] / libjava / prims.cc
blob4279b09f6ab07eeb4a18ccbd038152e8f9c0d399
1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
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
9 details. */
11 #include <config.h>
13 #ifdef USE_WIN32_SIGNALLING
14 #include <windows.h>
15 #endif /* USE_WIN32_SIGNALLING */
17 #ifdef USE_WINSOCK
18 #undef __INSIDE_CYGWIN__
19 #include <winsock.h>
20 #endif /* USE_WINSOCK */
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <signal.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
32 #include <gcj/cni.h>
33 #include <jvm.h>
34 #include <java-signal.h>
35 #include <java-threads.h>
37 #ifdef ENABLE_JVMPI
38 #include <jvmpi.h>
39 #endif
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
49 #include <java/lang/Class.h>
50 #include <java/lang/Runtime.h>
51 #include <java/lang/String.h>
52 #include <java/lang/Thread.h>
53 #include <java/lang/ThreadGroup.h>
54 #include <gnu/gcj/runtime/FirstThread.h>
55 #include <java/lang/ArrayIndexOutOfBoundsException.h>
56 #include <java/lang/ArithmeticException.h>
57 #include <java/lang/ClassFormatError.h>
58 #include <java/lang/NegativeArraySizeException.h>
59 #include <java/lang/NullPointerException.h>
60 #include <java/lang/OutOfMemoryError.h>
61 #include <java/lang/System.h>
62 #include <java/lang/reflect/Modifier.h>
63 #include <java/io/PrintStream.h>
65 #ifdef USE_LTDL
66 #include <ltdl.h>
67 #endif
69 #define ObjectClass _CL_Q34java4lang6Object
70 extern java::lang::Class ObjectClass;
72 // We allocate a single OutOfMemoryError exception which we keep
73 // around for use if we run out of memory.
74 static java::lang::OutOfMemoryError *no_memory;
76 // Largest representable size_t.
77 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
79 // Properties set at compile time.
80 const char **_Jv_Compiler_Properties;
82 #ifndef DISABLE_GETENV_PROPERTIES
83 // Property key/value pairs.
84 property_pair *_Jv_Environment_Properties;
85 #endif
87 // The name of this executable.
88 static char * _Jv_execName;
90 #ifdef ENABLE_JVMPI
91 // Pointer to JVMPI notification functions.
92 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
93 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
94 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
95 #endif
98 extern "C" void _Jv_ThrowSignal (void *) __attribute ((noreturn));
100 // Just like _Jv_Throw, but fill in the stack trace first. Although
101 // this is declared extern in order that its name not be mangled, it
102 // is not intended to be used outside this file.
103 void
104 _Jv_ThrowSignal (void *e)
106 java::lang::Throwable *throwable = (java::lang::Throwable *)e;
107 throwable->fillInStackTrace ();
108 _Jv_Throw (throwable);
111 #ifdef HANDLE_SEGV
112 static java::lang::NullPointerException *nullp;
114 SIGNAL_HANDLER (catch_segv)
116 MAKE_THROW_FRAME (nullp);
117 _Jv_ThrowSignal (nullp);
119 #endif
121 static java::lang::ArithmeticException *arithexception;
123 #ifdef HANDLE_FPE
124 SIGNAL_HANDLER (catch_fpe)
126 #ifdef HANDLE_DIVIDE_OVERFLOW
127 HANDLE_DIVIDE_OVERFLOW;
128 #else
129 MAKE_THROW_FRAME (arithexception);
130 #endif
131 _Jv_ThrowSignal (arithexception);
133 #endif
137 jboolean
138 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
140 int len;
141 _Jv_ushort *aptr, *bptr;
142 if (a == b)
143 return true;
144 if (a->hash != b->hash)
145 return false;
146 len = a->length;
147 if (b->length != len)
148 return false;
149 aptr = (_Jv_ushort *)a->data;
150 bptr = (_Jv_ushort *)b->data;
151 len = (len + 1) >> 1;
152 while (--len >= 0)
153 if (*aptr++ != *bptr++)
154 return false;
155 return true;
158 /* True iff A is equal to STR.
159 HASH is STR->hashCode().
162 jboolean
163 _Jv_equal (Utf8Const* a, jstring str, jint hash)
165 if (a->hash != (_Jv_ushort) hash)
166 return false;
167 jint len = str->length();
168 jint i = 0;
169 jchar *sptr = _Jv_GetStringChars (str);
170 unsigned char* ptr = (unsigned char*) a->data;
171 unsigned char* limit = ptr + a->length;
172 for (;; i++, sptr++)
174 int ch = UTF8_GET (ptr, limit);
175 if (i == len)
176 return ch < 0;
177 if (ch != *sptr)
178 return false;
180 return true;
183 /* Like _Jv_equal, but stop after N characters. */
184 jboolean
185 _Jv_equaln (Utf8Const *a, jstring str, jint n)
187 jint len = str->length();
188 jint i = 0;
189 jchar *sptr = _Jv_GetStringChars (str);
190 unsigned char* ptr = (unsigned char*) a->data;
191 unsigned char* limit = ptr + a->length;
192 for (; n-- > 0; i++, sptr++)
194 int ch = UTF8_GET (ptr, limit);
195 if (i == len)
196 return ch < 0;
197 if (ch != *sptr)
198 return false;
200 return true;
203 /* Count the number of Unicode chars encoded in a given Ut8 string. */
205 _Jv_strLengthUtf8(char* str, int len)
207 unsigned char* ptr;
208 unsigned char* limit;
209 int str_length;
211 ptr = (unsigned char*) str;
212 limit = ptr + len;
213 str_length = 0;
214 for (; ptr < limit; str_length++) {
215 if (UTF8_GET (ptr, limit) < 0) {
216 return (-1);
219 return (str_length);
222 /* Calculate a hash value for a string encoded in Utf8 format.
223 * This returns the same hash value as specified or java.lang.String.hashCode.
225 static jint
226 hashUtf8String (char* str, int len)
228 unsigned char* ptr = (unsigned char*) str;
229 unsigned char* limit = ptr + len;
230 jint hash = 0;
232 for (; ptr < limit;)
234 int ch = UTF8_GET (ptr, limit);
235 /* Updated specification from
236 http://www.javasoft.com/docs/books/jls/clarify.html. */
237 hash = (31 * hash) + ch;
239 return hash;
242 _Jv_Utf8Const *
243 _Jv_makeUtf8Const (char* s, int len)
245 if (len < 0)
246 len = strlen (s);
247 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
248 if (! m)
249 JvThrow (no_memory);
250 memcpy (m->data, s, len);
251 m->data[len] = 0;
252 m->length = len;
253 m->hash = hashUtf8String (s, len) & 0xFFFF;
254 return (m);
257 _Jv_Utf8Const *
258 _Jv_makeUtf8Const (jstring string)
260 jint hash = string->hashCode ();
261 jint len = _Jv_GetStringUTFLength (string);
263 Utf8Const* m = (Utf8Const*)
264 _Jv_AllocBytesChecked (sizeof(Utf8Const) + len + 1);
266 m->hash = hash;
267 m->length = len;
269 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
270 m->data[len] = 0;
272 return m;
277 #ifdef DEBUG
278 void
279 _Jv_Abort (const char *function, const char *file, int line,
280 const char *message)
281 #else
282 void
283 _Jv_Abort (const char *, const char *, int, const char *message)
284 #endif
286 #ifdef DEBUG
287 fprintf (stderr,
288 "libgcj failure: %s\n in function %s, file %s, line %d\n",
289 message, function, file, line);
290 #else
291 java::io::PrintStream *err = java::lang::System::err;
292 err->print(JvNewStringLatin1 ("libgcj failure: "));
293 err->println(JvNewStringLatin1 (message));
294 err->flush();
295 #endif
296 abort ();
299 static void
300 fail_on_finalization (jobject)
302 JvFail ("object was finalized");
305 void
306 _Jv_GCWatch (jobject obj)
308 _Jv_RegisterFinalizer (obj, fail_on_finalization);
311 void
312 _Jv_ThrowBadArrayIndex(jint bad_index)
314 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
315 (java::lang::String::valueOf(bad_index)));
318 void
319 _Jv_ThrowNullPointerException ()
321 throw new java::lang::NullPointerException ();
324 // Allocate some unscanned memory and throw an exception if no memory.
325 void *
326 _Jv_AllocBytesChecked (jsize size)
328 void *r = _Jv_AllocBytes (size);
329 if (! r)
330 _Jv_Throw (no_memory);
331 return r;
334 // Allocate a new object of class C. SIZE is the size of the object
335 // to allocate. You might think this is redundant, but it isn't; some
336 // classes, such as String, aren't of fixed size.
337 jobject
338 _Jv_AllocObject (jclass c, jint size)
340 _Jv_InitClass (c);
342 jobject obj = (jobject) _Jv_AllocObj (size);
343 if (__builtin_expect (! obj, false))
344 JvThrow (no_memory);
345 *((_Jv_VTable **) obj) = c->vtable;
347 // If this class has inherited finalize from Object, then don't
348 // bother registering a finalizer. We know that finalize() is the
349 // very first method after the dummy entry. If this turns out to be
350 // unreliable, a more robust implementation can be written. Such an
351 // implementation would look for Object.finalize in Object's method
352 // table at startup, and then use that information to find the
353 // appropriate index in the method vector.
354 if (c->vtable->method[1] != ObjectClass.vtable->method[1])
355 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
357 #ifdef ENABLE_JVMPI
358 // Service JVMPI request.
360 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
362 JVMPI_Event event;
364 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
365 event.env_id = NULL;
366 event.u.obj_alloc.arena_id = 0;
367 event.u.obj_alloc.class_id = (jobjectID) c;
368 event.u.obj_alloc.is_array = 0;
369 event.u.obj_alloc.size = size;
370 event.u.obj_alloc.obj_id = (jobjectID) obj;
372 _Jv_DisableGC ();
373 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
374 _Jv_EnableGC ();
376 #endif
378 return obj;
381 // Allocate a new array of Java objects. Each object is of type
382 // `elementClass'. `init' is used to initialize each slot in the
383 // array.
384 jobjectArray
385 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
387 if (__builtin_expect (count < 0, false))
388 JvThrow (new java::lang::NegativeArraySizeException);
390 JvAssert (! elementClass->isPrimitive ());
392 jobjectArray obj = NULL;
393 size_t size = (size_t) _Jv_GetArrayElementFromElementType (obj,
394 elementClass);
396 // Check for overflow.
397 if (__builtin_expect ((size_t) count >
398 (SIZE_T_MAX - size) / sizeof (jobject), false))
399 JvThrow (no_memory);
401 size += count * sizeof (jobject);
403 // FIXME: second argument should be "current loader" //
404 jclass clas = _Jv_FindArrayClass (elementClass, 0);
406 obj = (jobjectArray) _Jv_AllocArray (size);
407 if (__builtin_expect (! obj, false))
408 JvThrow (no_memory);
409 obj->length = count;
410 jobject* ptr = elements(obj);
411 // We know the allocator returns zeroed memory. So don't bother
412 // zeroing it again.
413 if (init)
415 while (--count >= 0)
416 *ptr++ = init;
418 // Set the vtbl last to avoid problems if the GC happens during the
419 // window in this function between the allocation and this
420 // assignment.
421 *((_Jv_VTable **) obj) = clas->vtable;
422 return obj;
425 // Allocate a new array of primitives. ELTYPE is the type of the
426 // element, COUNT is the size of the array.
427 jobject
428 _Jv_NewPrimArray (jclass eltype, jint count)
430 int elsize = eltype->size();
431 if (__builtin_expect (count < 0, false))
432 JvThrow (new java::lang::NegativeArraySizeException ());
434 JvAssert (eltype->isPrimitive ());
435 jobject dummy = NULL;
436 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
438 // Check for overflow.
439 if (__builtin_expect ((size_t) count >
440 (SIZE_T_MAX - size) / elsize, false))
441 JvThrow (no_memory);
443 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count);
444 if (__builtin_expect (! arr, false))
445 JvThrow (no_memory);
446 arr->length = count;
447 // Note that we assume we are given zeroed memory by the allocator.
449 jclass klass = _Jv_FindArrayClass (eltype, 0);
450 // Set the vtbl last to avoid problems if the GC happens during the
451 // window in this function between the allocation and this
452 // assignment.
453 *((_Jv_VTable **) arr) = klass->vtable;
454 return arr;
457 jobject
458 _Jv_NewArray (jint type, jint size)
460 switch (type)
462 case 4: return JvNewBooleanArray (size);
463 case 5: return JvNewCharArray (size);
464 case 6: return JvNewFloatArray (size);
465 case 7: return JvNewDoubleArray (size);
466 case 8: return JvNewByteArray (size);
467 case 9: return JvNewShortArray (size);
468 case 10: return JvNewIntArray (size);
469 case 11: return JvNewLongArray (size);
471 JvFail ("newarray - bad type code");
472 return NULL; // Placate compiler.
475 jobject
476 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
478 JvAssert (type->isArray());
479 jclass element_type = type->getComponentType();
480 jobject result;
481 if (element_type->isPrimitive())
482 result = _Jv_NewPrimArray (element_type, sizes[0]);
483 else
484 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
486 if (dimensions > 1)
488 JvAssert (! element_type->isPrimitive());
489 JvAssert (element_type->isArray());
490 jobject *contents = elements ((jobjectArray) result);
491 for (int i = 0; i < sizes[0]; ++i)
492 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
493 sizes + 1);
496 return result;
499 jobject
500 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
502 va_list args;
503 jint sizes[dimensions];
504 va_start (args, dimensions);
505 for (int i = 0; i < dimensions; ++i)
507 jint size = va_arg (args, jint);
508 sizes[i] = size;
510 va_end (args);
512 return _Jv_NewMultiArray (array_type, dimensions, sizes);
517 class _Jv_PrimClass : public java::lang::Class
519 public:
520 // FIXME: calling convention is weird. If we use the natural types
521 // then the compiler will complain because they aren't Java types.
522 _Jv_PrimClass (jobject cname, jbyte sig, jint len)
524 using namespace java::lang::reflect;
526 // We must initialize every field of the class. We do this in
527 // the same order they are declared in Class.h.
528 next = NULL;
529 name = _Jv_makeUtf8Const ((char *) cname, -1);
530 accflags = Modifier::PUBLIC | Modifier::FINAL;
531 superclass = NULL;
532 constants.size = 0;
533 constants.tags = NULL;
534 constants.data = NULL;
535 methods = NULL;
536 method_count = sig;
537 vtable_method_count = 0;
538 fields = NULL;
539 size_in_bytes = len;
540 field_count = 0;
541 static_field_count = 0;
542 vtable = JV_PRIMITIVE_VTABLE;
543 interfaces = NULL;
544 loader = NULL;
545 interface_count = 0;
546 state = JV_STATE_NOTHING;
547 thread = NULL;
551 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
552 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
554 DECLARE_PRIM_TYPE(byte, 'B', 1);
555 DECLARE_PRIM_TYPE(short, 'S', 2);
556 DECLARE_PRIM_TYPE(int, 'I', 4);
557 DECLARE_PRIM_TYPE(long, 'J', 8);
558 DECLARE_PRIM_TYPE(boolean, 'Z', 1);
559 DECLARE_PRIM_TYPE(char, 'C', 2);
560 DECLARE_PRIM_TYPE(float, 'F', 4);
561 DECLARE_PRIM_TYPE(double, 'D', 8);
562 DECLARE_PRIM_TYPE(void, 'V', 0);
564 jclass
565 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
567 switch (*sig)
569 case 'B':
570 return JvPrimClass (byte);
571 case 'S':
572 return JvPrimClass (short);
573 case 'I':
574 return JvPrimClass (int);
575 case 'J':
576 return JvPrimClass (long);
577 case 'Z':
578 return JvPrimClass (boolean);
579 case 'C':
580 return JvPrimClass (char);
581 case 'F':
582 return JvPrimClass (float);
583 case 'D':
584 return JvPrimClass (double);
585 case 'V':
586 return JvPrimClass (void);
587 case 'L':
589 int i;
590 for (i = 1; sig[i] && sig[i] != ';'; ++i)
592 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
593 return _Jv_FindClass (name, loader);
596 case '[':
597 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader),
598 loader);
600 JvFail ("couldn't understand class signature");
601 return NULL; // Placate compiler.
606 JArray<jstring> *
607 JvConvertArgv (int argc, const char **argv)
609 if (argc < 0)
610 argc = 0;
611 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
612 jobject* ptr = elements(ar);
613 for (int i = 0; i < argc; i++)
615 const char *arg = argv[i];
616 // FIXME - should probably use JvNewStringUTF.
617 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
619 return (JArray<jstring>*) ar;
622 // FIXME: These variables are static so that they will be
623 // automatically scanned by the Boehm collector. This is needed
624 // because with qthreads the collector won't scan the initial stack --
625 // it will only scan the qthreads stacks.
627 // Command line arguments.
628 static jobject arg_vec;
630 // The primary threadgroup.
631 static java::lang::ThreadGroup *main_group;
633 // The primary thread.
634 static java::lang::Thread *main_thread;
636 char *
637 _Jv_ThisExecutable (void)
639 return _Jv_execName;
642 void
643 _Jv_ThisExecutable (const char *name)
645 if (name)
647 _Jv_execName = new char[strlen (name) + 1];
648 strcpy (_Jv_execName, name);
652 #ifdef USE_WIN32_SIGNALLING
654 extern "C" int* win32_get_restart_frame (void *);
656 LONG CALLBACK
657 win32_exception_handler (LPEXCEPTION_POINTERS e)
659 int* setjmp_buf;
660 if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
661 setjmp_buf = win32_get_restart_frame (nullp);
662 else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
663 setjmp_buf = win32_get_restart_frame (arithexception);
664 else
665 return EXCEPTION_CONTINUE_SEARCH;
667 e->ContextRecord->Ebp = setjmp_buf[0];
668 // FIXME: Why does i386-signal.h increment the PC here, do we need to do it?
669 e->ContextRecord->Eip = setjmp_buf[1];
670 // FIXME: Is this the stack pointer? Do we need it?
671 e->ContextRecord->Esp = setjmp_buf[2];
673 return EXCEPTION_CONTINUE_EXECUTION;
676 #endif
678 static void
679 main_init ()
681 INIT_SEGV;
682 #ifdef HANDLE_FPE
683 INIT_FPE;
684 #else
685 arithexception = new java::lang::ArithmeticException
686 (JvNewStringLatin1 ("/ by zero"));
687 #endif
689 no_memory = new java::lang::OutOfMemoryError;
691 #ifdef USE_LTDL
692 LTDL_SET_PRELOADED_SYMBOLS ();
693 #endif
695 #ifdef USE_WINSOCK
696 // Initialise winsock for networking
697 WSADATA data;
698 if (WSAStartup (MAKEWORD (1, 1), &data))
699 MessageBox (NULL, "Error initialising winsock library.", "Error", MB_OK | MB_ICONEXCLAMATION);
700 #endif /* USE_WINSOCK */
702 #ifdef USE_WIN32_SIGNALLING
703 // Install exception handler
704 SetUnhandledExceptionFilter (win32_exception_handler);
705 #else
706 // We only want this on POSIX systems.
707 struct sigaction act;
708 act.sa_handler = SIG_IGN;
709 sigemptyset (&act.sa_mask);
710 act.sa_flags = 0;
711 sigaction (SIGPIPE, &act, NULL);
712 #endif /* USE_WIN32_SIGNALLING */
714 _Jv_JNI_Init ();
717 #ifndef DISABLE_GETENV_PROPERTIES
719 static char *
720 next_property_key (char *s, size_t *length)
722 size_t l = 0;
724 JvAssert (s);
726 // Skip over whitespace
727 while (isspace (*s))
728 s++;
730 // If we've reached the end, return NULL. Also return NULL if for
731 // some reason we've come across a malformed property string.
732 if (*s == 0
733 || *s == ':'
734 || *s == '=')
735 return NULL;
737 // Determine the length of the property key.
738 while (s[l] != 0
739 && ! isspace (s[l])
740 && s[l] != ':'
741 && s[l] != '=')
743 if (s[l] == '\\'
744 && s[l+1] != 0)
745 l++;
746 l++;
749 *length = l;
751 return s;
754 static char *
755 next_property_value (char *s, size_t *length)
757 size_t l = 0;
759 JvAssert (s);
761 while (isspace (*s))
762 s++;
764 if (*s == ':'
765 || *s == '=')
766 s++;
768 while (isspace (*s))
769 s++;
771 // If we've reached the end, return NULL.
772 if (*s == 0)
773 return NULL;
775 // Determine the length of the property value.
776 while (s[l] != 0
777 && ! isspace (s[l])
778 && s[l] != ':'
779 && s[l] != '=')
781 if (s[l] == '\\'
782 && s[l+1] != 0)
783 l += 2;
784 else
785 l++;
788 *length = l;
790 return s;
793 static void
794 process_gcj_properties ()
796 char *props = getenv("GCJ_PROPERTIES");
797 char *p = props;
798 size_t length;
799 size_t property_count = 0;
801 if (NULL == props)
802 return;
804 // Whip through props quickly in order to count the number of
805 // property values.
806 while (p && (p = next_property_key (p, &length)))
808 // Skip to the end of the key
809 p += length;
811 p = next_property_value (p, &length);
812 if (p)
813 p += length;
815 property_count++;
818 // Allocate an array of property value/key pairs.
819 _Jv_Environment_Properties =
820 (property_pair *) malloc (sizeof(property_pair)
821 * (property_count + 1));
823 // Go through the properties again, initializing _Jv_Properties
824 // along the way.
825 p = props;
826 property_count = 0;
827 while (p && (p = next_property_key (p, &length)))
829 _Jv_Environment_Properties[property_count].key = p;
830 _Jv_Environment_Properties[property_count].key_length = length;
832 // Skip to the end of the key
833 p += length;
835 p = next_property_value (p, &length);
837 _Jv_Environment_Properties[property_count].value = p;
838 _Jv_Environment_Properties[property_count].value_length = length;
840 if (p)
841 p += length;
843 property_count++;
845 memset ((void *) &_Jv_Environment_Properties[property_count],
846 0, sizeof (property_pair));
848 size_t i = 0;
850 // Null terminate the strings.
851 while (_Jv_Environment_Properties[i].key)
853 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
854 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
858 #endif // DISABLE_GETENV_PROPERTIES
860 void
861 JvRunMain (jclass klass, int argc, const char **argv)
863 PROCESS_GCJ_PROPERTIES;
865 main_init ();
866 #ifdef HAVE_PROC_SELF_EXE
867 char exec_name[20];
868 sprintf (exec_name, "/proc/%d/exe", getpid ());
869 _Jv_ThisExecutable (exec_name);
870 #else
871 _Jv_ThisExecutable (argv[0]);
872 #endif
874 arg_vec = JvConvertArgv (argc - 1, argv + 1);
875 main_group = new java::lang::ThreadGroup (23);
876 main_thread = new gnu::gcj::runtime::FirstThread (main_group,
877 klass, arg_vec);
879 main_thread->start();
880 _Jv_ThreadWait ();
882 java::lang::Runtime::getRuntime ()->exit (0);
885 void
886 _Jv_RunMain (const char *class_name, int argc, const char **argv)
888 PROCESS_GCJ_PROPERTIES;
890 main_init ();
892 #ifdef HAVE_PROC_SELF_EXE
893 char exec_name[20];
894 sprintf (exec_name, "/proc/%d/exe", getpid ());
895 _Jv_ThisExecutable (exec_name);
896 #endif
898 arg_vec = JvConvertArgv (argc - 1, argv + 1);
899 main_group = new java::lang::ThreadGroup (23);
900 main_thread = new gnu::gcj::runtime::FirstThread (main_group,
901 JvNewStringLatin1 (class_name),
902 arg_vec);
903 main_thread->start();
904 _Jv_ThreadWait ();
906 java::lang::Runtime::getRuntime ()->exit (0);
911 // Parse a string and return a heap size.
912 static size_t
913 parse_heap_size (const char *spec)
915 char *end;
916 unsigned long val = strtoul (spec, &end, 10);
917 if (*end == 'k' || *end == 'K')
918 val *= 1024;
919 else if (*end == 'm' || *end == 'M')
920 val *= 1048576;
921 return (size_t) val;
924 // Set the initial heap size. This might be ignored by the GC layer.
925 // This must be called before _Jv_RunMain.
926 void
927 _Jv_SetInitialHeapSize (const char *arg)
929 size_t size = parse_heap_size (arg);
930 _Jv_GCSetInitialHeapSize (size);
933 // Set the maximum heap size. This might be ignored by the GC layer.
934 // This must be called before _Jv_RunMain.
935 void
936 _Jv_SetMaximumHeapSize (const char *arg)
938 size_t size = parse_heap_size (arg);
939 _Jv_GCSetMaximumHeapSize (size);
944 void *
945 _Jv_Malloc (jsize size)
947 if (__builtin_expect (size == 0, false))
948 size = 1;
949 void *ptr = malloc ((size_t) size);
950 if (__builtin_expect (ptr == NULL, false))
951 JvThrow (no_memory);
952 return ptr;
955 void *
956 _Jv_Realloc (void *ptr, jsize size)
958 if (__builtin_expect (size == 0, false))
959 size = 1;
960 ptr = realloc (ptr, (size_t) size);
961 if (__builtin_expect (ptr == NULL, false))
962 JvThrow (no_memory);
963 return ptr;
966 void *
967 _Jv_MallocUnchecked (jsize size)
969 if (__builtin_expect (size == 0, false))
970 size = 1;
971 return malloc ((size_t) size);
974 void
975 _Jv_Free (void* ptr)
977 return free (ptr);
982 // In theory, these routines can be #ifdef'd away on machines which
983 // support divide overflow signals. However, we never know if some
984 // code might have been compiled with "-fuse-divide-subroutine", so we
985 // always include them in libgcj.
987 jint
988 _Jv_divI (jint dividend, jint divisor)
990 if (__builtin_expect (divisor == 0, false))
991 _Jv_ThrowSignal (arithexception);
993 if (dividend == (jint) 0x80000000L && divisor == -1)
994 return dividend;
996 return dividend / divisor;
999 jint
1000 _Jv_remI (jint dividend, jint divisor)
1002 if (__builtin_expect (divisor == 0, false))
1003 _Jv_ThrowSignal (arithexception);
1005 if (dividend == (jint) 0x80000000L && divisor == -1)
1006 return 0;
1008 return dividend % divisor;
1011 jlong
1012 _Jv_divJ (jlong dividend, jlong divisor)
1014 if (__builtin_expect (divisor == 0, false))
1015 _Jv_ThrowSignal (arithexception);
1017 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1018 return dividend;
1020 return dividend / divisor;
1023 jlong
1024 _Jv_remJ (jlong dividend, jlong divisor)
1026 if (__builtin_expect (divisor == 0, false))
1027 _Jv_ThrowSignal (arithexception);
1029 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1030 return 0;
1032 return dividend % divisor;