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