| 1 | // prims.cc - Code for core of runtime environment.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 | #include <platform.h>
|
|---|
| 13 |
|
|---|
| 14 | #include <stdlib.h>
|
|---|
| 15 | #include <stdarg.h>
|
|---|
| 16 | #include <stdio.h>
|
|---|
| 17 | #include <string.h>
|
|---|
| 18 | #include <signal.h>
|
|---|
| 19 |
|
|---|
| 20 | #ifdef HAVE_UNISTD_H
|
|---|
| 21 | #include <unistd.h>
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | #include <gcj/cni.h>
|
|---|
| 25 | #include <jvm.h>
|
|---|
| 26 | #include <java-signal.h>
|
|---|
| 27 | #include <java-threads.h>
|
|---|
| 28 |
|
|---|
| 29 | #ifdef ENABLE_JVMPI
|
|---|
| 30 | #include <jvmpi.h>
|
|---|
| 31 | #include <java/lang/ThreadGroup.h>
|
|---|
| 32 | #endif
|
|---|
| 33 |
|
|---|
| 34 | #ifndef DISABLE_GETENV_PROPERTIES
|
|---|
| 35 | #include <ctype.h>
|
|---|
| 36 | #include <java-props.h>
|
|---|
| 37 | #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
|
|---|
| 38 | #else
|
|---|
| 39 | #define PROCESS_GCJ_PROPERTIES
|
|---|
| 40 | #endif // DISABLE_GETENV_PROPERTIES
|
|---|
| 41 |
|
|---|
| 42 | #include <java/lang/Class.h>
|
|---|
| 43 | #include <java/lang/ClassLoader.h>
|
|---|
| 44 | #include <java/lang/Runtime.h>
|
|---|
| 45 | #include <java/lang/String.h>
|
|---|
| 46 | #include <java/lang/Thread.h>
|
|---|
| 47 | #include <java/lang/ThreadGroup.h>
|
|---|
| 48 | #include <java/lang/ArrayIndexOutOfBoundsException.h>
|
|---|
| 49 | #include <java/lang/ArithmeticException.h>
|
|---|
| 50 | #include <java/lang/ClassFormatError.h>
|
|---|
| 51 | #include <java/lang/InternalError.h>
|
|---|
| 52 | #include <java/lang/NegativeArraySizeException.h>
|
|---|
| 53 | #include <java/lang/NullPointerException.h>
|
|---|
| 54 | #include <java/lang/OutOfMemoryError.h>
|
|---|
| 55 | #include <java/lang/System.h>
|
|---|
| 56 | #include <java/lang/reflect/Modifier.h>
|
|---|
| 57 | #include <java/io/PrintStream.h>
|
|---|
| 58 | #include <java/lang/UnsatisfiedLinkError.h>
|
|---|
| 59 | #include <java/lang/VirtualMachineError.h>
|
|---|
| 60 | #include <gnu/gcj/runtime/VMClassLoader.h>
|
|---|
| 61 | #include <gnu/gcj/runtime/FinalizerThread.h>
|
|---|
| 62 | #include <gnu/gcj/runtime/FirstThread.h>
|
|---|
| 63 |
|
|---|
| 64 | #ifdef USE_LTDL
|
|---|
| 65 | #include <ltdl.h>
|
|---|
| 66 | #endif
|
|---|
| 67 |
|
|---|
| 68 | // We allocate a single OutOfMemoryError exception which we keep
|
|---|
| 69 | // around for use if we run out of memory.
|
|---|
| 70 | static java::lang::OutOfMemoryError *no_memory;
|
|---|
| 71 |
|
|---|
| 72 | // Largest representable size_t.
|
|---|
| 73 | #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
|
|---|
| 74 |
|
|---|
| 75 | static const char *no_properties[] = { NULL };
|
|---|
| 76 |
|
|---|
| 77 | // Properties set at compile time.
|
|---|
| 78 | const char **_Jv_Compiler_Properties = no_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 | |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | extern "C" void _Jv_ThrowSignal (jthrowable) __attribute ((noreturn));
|
|---|
| 105 |
|
|---|
| 106 | // Just like _Jv_Throw, but fill in the stack trace first. Although
|
|---|
| 107 | // this is declared extern in order that its name not be mangled, it
|
|---|
| 108 | // is not intended to be used outside this file.
|
|---|
| 109 | void
|
|---|
| 110 | _Jv_ThrowSignal (jthrowable throwable)
|
|---|
| 111 | {
|
|---|
| 112 | throwable->fillInStackTrace ();
|
|---|
| 113 | 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 | |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | jboolean
|
|---|
| 144 | _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
|
|---|
| 145 | {
|
|---|
| 146 | int len;
|
|---|
| 147 | _Jv_ushort *aptr, *bptr;
|
|---|
| 148 | if (a == b)
|
|---|
| 149 | return true;
|
|---|
| 150 | if (a->hash != b->hash)
|
|---|
| 151 | return false;
|
|---|
| 152 | len = a->length;
|
|---|
| 153 | if (b->length != len)
|
|---|
| 154 | return false;
|
|---|
| 155 | aptr = (_Jv_ushort *)a->data;
|
|---|
| 156 | bptr = (_Jv_ushort *)b->data;
|
|---|
| 157 | len = (len + 1) >> 1;
|
|---|
| 158 | while (--len >= 0)
|
|---|
| 159 | if (*aptr++ != *bptr++)
|
|---|
| 160 | return false;
|
|---|
| 161 | return true;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /* True iff A is equal to STR.
|
|---|
| 165 | HASH is STR->hashCode().
|
|---|
| 166 | */
|
|---|
| 167 |
|
|---|
| 168 | jboolean
|
|---|
| 169 | _Jv_equal (Utf8Const* a, jstring str, jint hash)
|
|---|
| 170 | {
|
|---|
| 171 | if (a->hash != (_Jv_ushort) hash)
|
|---|
| 172 | return false;
|
|---|
| 173 | jint len = str->length();
|
|---|
| 174 | jint i = 0;
|
|---|
| 175 | jchar *sptr = _Jv_GetStringChars (str);
|
|---|
| 176 | unsigned char* ptr = (unsigned char*) a->data;
|
|---|
| 177 | unsigned char* limit = ptr + a->length;
|
|---|
| 178 | for (;; i++, sptr++)
|
|---|
| 179 | {
|
|---|
| 180 | int ch = UTF8_GET (ptr, limit);
|
|---|
| 181 | if (i == len)
|
|---|
| 182 | return ch < 0;
|
|---|
| 183 | if (ch != *sptr)
|
|---|
| 184 | return false;
|
|---|
| 185 | }
|
|---|
| 186 | return true;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /* Like _Jv_equal, but stop after N characters. */
|
|---|
| 190 | jboolean
|
|---|
| 191 | _Jv_equaln (Utf8Const *a, jstring str, jint n)
|
|---|
| 192 | {
|
|---|
| 193 | jint len = str->length();
|
|---|
| 194 | jint i = 0;
|
|---|
| 195 | jchar *sptr = _Jv_GetStringChars (str);
|
|---|
| 196 | unsigned char* ptr = (unsigned char*) a->data;
|
|---|
| 197 | unsigned char* limit = ptr + a->length;
|
|---|
| 198 | for (; n-- > 0; i++, sptr++)
|
|---|
| 199 | {
|
|---|
| 200 | int ch = UTF8_GET (ptr, limit);
|
|---|
| 201 | if (i == len)
|
|---|
| 202 | return ch < 0;
|
|---|
| 203 | if (ch != *sptr)
|
|---|
| 204 | return false;
|
|---|
| 205 | }
|
|---|
| 206 | return true;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /* Count the number of Unicode chars encoded in a given Ut8 string. */
|
|---|
| 210 | int
|
|---|
| 211 | _Jv_strLengthUtf8(char* str, int len)
|
|---|
| 212 | {
|
|---|
| 213 | unsigned char* ptr;
|
|---|
| 214 | unsigned char* limit;
|
|---|
| 215 | int str_length;
|
|---|
| 216 |
|
|---|
| 217 | ptr = (unsigned char*) str;
|
|---|
| 218 | limit = ptr + len;
|
|---|
| 219 | str_length = 0;
|
|---|
| 220 | for (; ptr < limit; str_length++)
|
|---|
| 221 | {
|
|---|
| 222 | if (UTF8_GET (ptr, limit) < 0)
|
|---|
| 223 | return (-1);
|
|---|
| 224 | }
|
|---|
| 225 | return (str_length);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /* Calculate a hash value for a string encoded in Utf8 format.
|
|---|
| 229 | * This returns the same hash value as specified or java.lang.String.hashCode.
|
|---|
| 230 | */
|
|---|
| 231 | static jint
|
|---|
| 232 | hashUtf8String (char* str, int len)
|
|---|
| 233 | {
|
|---|
| 234 | unsigned char* ptr = (unsigned char*) str;
|
|---|
| 235 | unsigned char* limit = ptr + len;
|
|---|
| 236 | jint hash = 0;
|
|---|
| 237 |
|
|---|
| 238 | for (; ptr < limit;)
|
|---|
| 239 | {
|
|---|
| 240 | int ch = UTF8_GET (ptr, limit);
|
|---|
| 241 | /* Updated specification from
|
|---|
| 242 | http://www.javasoft.com/docs/books/jls/clarify.html. */
|
|---|
| 243 | hash = (31 * hash) + ch;
|
|---|
| 244 | }
|
|---|
| 245 | return hash;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | _Jv_Utf8Const *
|
|---|
| 249 | _Jv_makeUtf8Const (char* s, int len)
|
|---|
| 250 | {
|
|---|
| 251 | if (len < 0)
|
|---|
| 252 | len = strlen (s);
|
|---|
| 253 | Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
|
|---|
| 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 |
|
|---|
| 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_AllocBytes (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 |
|
|---|
| 279 | |
|---|
| 280 |
|
|---|
| 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 | fprintf (stderr, "libgcj failure: %s\n", message);
|
|---|
| 297 | #endif
|
|---|
| 298 | abort ();
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | static void
|
|---|
| 302 | fail_on_finalization (jobject)
|
|---|
| 303 | {
|
|---|
| 304 | JvFail ("object was finalized");
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | void
|
|---|
| 308 | _Jv_GCWatch (jobject obj)
|
|---|
| 309 | {
|
|---|
| 310 | _Jv_RegisterFinalizer (obj, fail_on_finalization);
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | void
|
|---|
| 314 | _Jv_ThrowBadArrayIndex(jint bad_index)
|
|---|
| 315 | {
|
|---|
| 316 | throw new java::lang::ArrayIndexOutOfBoundsException
|
|---|
| 317 | (java::lang::String::valueOf (bad_index));
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | void
|
|---|
| 321 | _Jv_ThrowNullPointerException ()
|
|---|
| 322 | {
|
|---|
| 323 | throw new java::lang::NullPointerException;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | // Explicitly throw a no memory exception.
|
|---|
| 327 | // The collector calls this when it encounters an out-of-memory condition.
|
|---|
| 328 | void _Jv_ThrowNoMemory()
|
|---|
| 329 | {
|
|---|
| 330 | throw no_memory;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | #ifdef ENABLE_JVMPI
|
|---|
| 334 | static void
|
|---|
| 335 | jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
|
|---|
| 336 | {
|
|---|
| 337 | // Service JVMPI allocation request.
|
|---|
| 338 | if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
|
|---|
| 339 | {
|
|---|
| 340 | JVMPI_Event event;
|
|---|
| 341 |
|
|---|
| 342 | event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
|
|---|
| 343 | event.env_id = NULL;
|
|---|
| 344 | event.u.obj_alloc.arena_id = 0;
|
|---|
| 345 | event.u.obj_alloc.class_id = (jobjectID) klass;
|
|---|
| 346 | event.u.obj_alloc.is_array = 0;
|
|---|
| 347 | event.u.obj_alloc.size = size;
|
|---|
| 348 | event.u.obj_alloc.obj_id = (jobjectID) obj;
|
|---|
| 349 |
|
|---|
| 350 | // FIXME: This doesn't look right for the Boehm GC. A GC may
|
|---|
| 351 | // already be in progress. _Jv_DisableGC () doesn't wait for it.
|
|---|
| 352 | // More importantly, I don't see the need for disabling GC, since we
|
|---|
| 353 | // blatantly have a pointer to obj on our stack, ensuring that the
|
|---|
| 354 | // object can't be collected. Even for a nonconservative collector,
|
|---|
| 355 | // it appears to me that this must be true, since we are about to
|
|---|
| 356 | // return obj. Isn't this whole approach way too intrusive for
|
|---|
| 357 | // a useful profiling interface? - HB
|
|---|
| 358 | _Jv_DisableGC ();
|
|---|
| 359 | (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
|
|---|
| 360 | _Jv_EnableGC ();
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 | #else /* !ENABLE_JVMPI */
|
|---|
| 364 | # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
|
|---|
| 365 | #endif
|
|---|
| 366 |
|
|---|
| 367 | // Allocate a new object of class KLASS. SIZE is the size of the object
|
|---|
| 368 | // to allocate. You might think this is redundant, but it isn't; some
|
|---|
| 369 | // classes, such as String, aren't of fixed size.
|
|---|
| 370 | // First a version that assumes that we have no finalizer, and that
|
|---|
| 371 | // the class is already initialized.
|
|---|
| 372 | // If we know that JVMPI is disabled, this can be replaced by a direct call
|
|---|
| 373 | // to the allocator for the appropriate GC.
|
|---|
| 374 | jobject
|
|---|
| 375 | _Jv_AllocObjectNoInitNoFinalizer (jclass klass, jint size)
|
|---|
| 376 | {
|
|---|
| 377 | jobject obj = (jobject) _Jv_AllocObj (size, klass);
|
|---|
| 378 | jvmpi_notify_alloc (klass, size, obj);
|
|---|
| 379 | return obj;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | // And now a version that initializes if necessary.
|
|---|
| 383 | jobject
|
|---|
| 384 | _Jv_AllocObjectNoFinalizer (jclass klass, jint size)
|
|---|
| 385 | {
|
|---|
| 386 | _Jv_InitClass (klass);
|
|---|
| 387 | jobject obj = (jobject) _Jv_AllocObj (size, klass);
|
|---|
| 388 | jvmpi_notify_alloc (klass, size, obj);
|
|---|
| 389 | return obj;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | // And now the general version that registers a finalizer if necessary.
|
|---|
| 393 | jobject
|
|---|
| 394 | _Jv_AllocObject (jclass klass, jint size)
|
|---|
| 395 | {
|
|---|
| 396 | jobject obj = _Jv_AllocObjectNoFinalizer (klass, size);
|
|---|
| 397 |
|
|---|
| 398 | // We assume that the compiler only generates calls to this routine
|
|---|
| 399 | // if there really is an interesting finalizer.
|
|---|
| 400 | // Unfortunately, we still have to the dynamic test, since there may
|
|---|
| 401 | // be cni calls to this routine.
|
|---|
| 402 | // Nore that on IA64 get_finalizer() returns the starting address of the
|
|---|
| 403 | // function, not a function pointer. Thus this still works.
|
|---|
| 404 | if (klass->vtable->get_finalizer ()
|
|---|
| 405 | != java::lang::Object::class$.vtable->get_finalizer ())
|
|---|
| 406 | _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
|
|---|
| 407 | return obj;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | // A version of the above that assumes the object contains no pointers,
|
|---|
| 411 | // and requires no finalization. This can't happen if we need pointers
|
|---|
| 412 | // to locks.
|
|---|
| 413 | #ifdef JV_HASH_SYNCHRONIZATION
|
|---|
| 414 | jobject
|
|---|
| 415 | _Jv_AllocPtrFreeObject (jclass klass, jint size)
|
|---|
| 416 | {
|
|---|
| 417 | _Jv_InitClass (klass);
|
|---|
| 418 |
|
|---|
| 419 | jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
|
|---|
| 420 |
|
|---|
| 421 | #ifdef ENABLE_JVMPI
|
|---|
| 422 | // Service JVMPI request.
|
|---|
| 423 |
|
|---|
| 424 | if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
|
|---|
| 425 | {
|
|---|
| 426 | JVMPI_Event event;
|
|---|
| 427 |
|
|---|
| 428 | event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
|
|---|
| 429 | event.env_id = NULL;
|
|---|
| 430 | event.u.obj_alloc.arena_id = 0;
|
|---|
| 431 | event.u.obj_alloc.class_id = (jobjectID) klass;
|
|---|
| 432 | event.u.obj_alloc.is_array = 0;
|
|---|
| 433 | event.u.obj_alloc.size = size;
|
|---|
| 434 | event.u.obj_alloc.obj_id = (jobjectID) obj;
|
|---|
| 435 |
|
|---|
| 436 | _Jv_DisableGC ();
|
|---|
| 437 | (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
|
|---|
| 438 | _Jv_EnableGC ();
|
|---|
| 439 | }
|
|---|
| 440 | #endif
|
|---|
| 441 |
|
|---|
| 442 | return obj;
|
|---|
| 443 | }
|
|---|
| 444 | #endif /* JV_HASH_SYNCHRONIZATION */
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 | // Allocate a new array of Java objects. Each object is of type
|
|---|
| 448 | // `elementClass'. `init' is used to initialize each slot in the
|
|---|
| 449 | // array.
|
|---|
| 450 | jobjectArray
|
|---|
| 451 | _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
|
|---|
| 452 | {
|
|---|
| 453 | if (__builtin_expect (count < 0, false))
|
|---|
| 454 | throw new java::lang::NegativeArraySizeException;
|
|---|
| 455 |
|
|---|
| 456 | JvAssert (! elementClass->isPrimitive ());
|
|---|
| 457 |
|
|---|
| 458 | // Ensure that elements pointer is properly aligned.
|
|---|
| 459 | jobjectArray obj = NULL;
|
|---|
| 460 | size_t size = (size_t) elements (obj);
|
|---|
| 461 | size += count * sizeof (jobject);
|
|---|
| 462 |
|
|---|
| 463 | // FIXME: second argument should be "current loader"
|
|---|
| 464 | jclass klass = _Jv_GetArrayClass (elementClass, 0);
|
|---|
| 465 |
|
|---|
| 466 | obj = (jobjectArray) _Jv_AllocArray (size, klass);
|
|---|
| 467 | // Cast away const.
|
|---|
| 468 | jsize *lp = const_cast<jsize *> (&obj->length);
|
|---|
| 469 | *lp = count;
|
|---|
| 470 | // We know the allocator returns zeroed memory. So don't bother
|
|---|
| 471 | // zeroing it again.
|
|---|
| 472 | if (init)
|
|---|
| 473 | {
|
|---|
| 474 | jobject *ptr = elements(obj);
|
|---|
| 475 | while (--count >= 0)
|
|---|
| 476 | *ptr++ = init;
|
|---|
| 477 | }
|
|---|
| 478 | return obj;
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | // Allocate a new array of primitives. ELTYPE is the type of the
|
|---|
| 482 | // element, COUNT is the size of the array.
|
|---|
| 483 | jobject
|
|---|
| 484 | _Jv_NewPrimArray (jclass eltype, jint count)
|
|---|
| 485 | {
|
|---|
| 486 | int elsize = eltype->size();
|
|---|
| 487 | if (__builtin_expect (count < 0, false))
|
|---|
| 488 | throw new java::lang::NegativeArraySizeException;
|
|---|
| 489 |
|
|---|
| 490 | JvAssert (eltype->isPrimitive ());
|
|---|
| 491 | jobject dummy = NULL;
|
|---|
| 492 | size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
|
|---|
| 493 |
|
|---|
| 494 | // Check for overflow.
|
|---|
| 495 | if (__builtin_expect ((size_t) count >
|
|---|
| 496 | (SIZE_T_MAX - size) / elsize, false))
|
|---|
| 497 | throw no_memory;
|
|---|
| 498 |
|
|---|
| 499 | jclass klass = _Jv_GetArrayClass (eltype, 0);
|
|---|
| 500 |
|
|---|
| 501 | # ifdef JV_HASH_SYNCHRONIZATION
|
|---|
| 502 | // Since the vtable is always statically allocated,
|
|---|
| 503 | // these are completely pointerfree! Make sure the GC doesn't touch them.
|
|---|
| 504 | __JArray *arr =
|
|---|
| 505 | (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
|
|---|
| 506 | memset((char *)arr + size, 0, elsize * count);
|
|---|
| 507 | # else
|
|---|
| 508 | __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
|
|---|
| 509 | // Note that we assume we are given zeroed memory by the allocator.
|
|---|
| 510 | # endif
|
|---|
| 511 | // Cast away const.
|
|---|
| 512 | jsize *lp = const_cast<jsize *> (&arr->length);
|
|---|
| 513 | *lp = count;
|
|---|
| 514 |
|
|---|
| 515 | return arr;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | jobject
|
|---|
| 519 | _Jv_NewArray (jint type, jint size)
|
|---|
| 520 | {
|
|---|
| 521 | switch (type)
|
|---|
| 522 | {
|
|---|
| 523 | case 4: return JvNewBooleanArray (size);
|
|---|
| 524 | case 5: return JvNewCharArray (size);
|
|---|
| 525 | case 6: return JvNewFloatArray (size);
|
|---|
| 526 | case 7: return JvNewDoubleArray (size);
|
|---|
| 527 | case 8: return JvNewByteArray (size);
|
|---|
| 528 | case 9: return JvNewShortArray (size);
|
|---|
| 529 | case 10: return JvNewIntArray (size);
|
|---|
| 530 | case 11: return JvNewLongArray (size);
|
|---|
| 531 | }
|
|---|
| 532 | throw new java::lang::InternalError
|
|---|
| 533 | (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | // Allocate a possibly multi-dimensional array but don't check that
|
|---|
| 537 | // any array length is <0.
|
|---|
| 538 | static jobject
|
|---|
| 539 | _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
|
|---|
| 540 | {
|
|---|
| 541 | JvAssert (type->isArray());
|
|---|
| 542 | jclass element_type = type->getComponentType();
|
|---|
| 543 | jobject result;
|
|---|
| 544 | if (element_type->isPrimitive())
|
|---|
| 545 | result = _Jv_NewPrimArray (element_type, sizes[0]);
|
|---|
| 546 | else
|
|---|
| 547 | result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
|
|---|
| 548 |
|
|---|
| 549 | if (dimensions > 1)
|
|---|
| 550 | {
|
|---|
| 551 | JvAssert (! element_type->isPrimitive());
|
|---|
| 552 | JvAssert (element_type->isArray());
|
|---|
| 553 | jobject *contents = elements ((jobjectArray) result);
|
|---|
| 554 | for (int i = 0; i < sizes[0]; ++i)
|
|---|
| 555 | contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
|
|---|
| 556 | sizes + 1);
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | return result;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | jobject
|
|---|
| 563 | _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
|
|---|
| 564 | {
|
|---|
| 565 | for (int i = 0; i < dimensions; ++i)
|
|---|
| 566 | if (sizes[i] < 0)
|
|---|
| 567 | throw new java::lang::NegativeArraySizeException;
|
|---|
| 568 |
|
|---|
| 569 | return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | jobject
|
|---|
| 573 | _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
|
|---|
| 574 | {
|
|---|
| 575 | va_list args;
|
|---|
| 576 | jint sizes[dimensions];
|
|---|
| 577 | va_start (args, dimensions);
|
|---|
| 578 | for (int i = 0; i < dimensions; ++i)
|
|---|
| 579 | {
|
|---|
| 580 | jint size = va_arg (args, jint);
|
|---|
| 581 | if (size < 0)
|
|---|
| 582 | throw new java::lang::NegativeArraySizeException;
|
|---|
| 583 | sizes[i] = size;
|
|---|
| 584 | }
|
|---|
| 585 | va_end (args);
|
|---|
| 586 |
|
|---|
| 587 | return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | |
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 | // Ensure 8-byte alignment, for hash synchronization.
|
|---|
| 594 | #define DECLARE_PRIM_TYPE(NAME) \
|
|---|
| 595 | _Jv_ArrayVTable _Jv_##NAME##VTable; \
|
|---|
| 596 | java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
|
|---|
| 597 |
|
|---|
| 598 | DECLARE_PRIM_TYPE(byte);
|
|---|
| 599 | DECLARE_PRIM_TYPE(short);
|
|---|
| 600 | DECLARE_PRIM_TYPE(int);
|
|---|
| 601 | DECLARE_PRIM_TYPE(long);
|
|---|
| 602 | DECLARE_PRIM_TYPE(boolean);
|
|---|
| 603 | DECLARE_PRIM_TYPE(char);
|
|---|
| 604 | DECLARE_PRIM_TYPE(float);
|
|---|
| 605 | DECLARE_PRIM_TYPE(double);
|
|---|
| 606 | DECLARE_PRIM_TYPE(void);
|
|---|
| 607 |
|
|---|
| 608 | void
|
|---|
| 609 | _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len,
|
|---|
| 610 | _Jv_ArrayVTable *array_vtable)
|
|---|
| 611 | {
|
|---|
| 612 | using namespace java::lang::reflect;
|
|---|
| 613 |
|
|---|
| 614 | _Jv_InitNewClassFields (cl);
|
|---|
| 615 |
|
|---|
| 616 | // We must set the vtable for the class; the Java constructor
|
|---|
| 617 | // doesn't do this.
|
|---|
| 618 | (*(_Jv_VTable **) cl) = java::lang::Class::class$.vtable;
|
|---|
| 619 |
|
|---|
| 620 | // Initialize the fields we care about. We do this in the same
|
|---|
| 621 | // order they are declared in Class.h.
|
|---|
| 622 | cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
|
|---|
| 623 | cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
|
|---|
| 624 | cl->method_count = sig;
|
|---|
| 625 | cl->size_in_bytes = len;
|
|---|
| 626 | cl->vtable = JV_PRIMITIVE_VTABLE;
|
|---|
| 627 | cl->state = JV_STATE_DONE;
|
|---|
| 628 | cl->depth = -1;
|
|---|
| 629 | if (sig != 'V')
|
|---|
| 630 | _Jv_NewArrayClass (cl, NULL, (_Jv_VTable *) array_vtable);
|
|---|
| 631 | }
|
|---|
| 632 |
|
|---|
| 633 | jclass
|
|---|
| 634 | _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
|
|---|
| 635 | {
|
|---|
| 636 | switch (*sig)
|
|---|
| 637 | {
|
|---|
| 638 | case 'B':
|
|---|
| 639 | return JvPrimClass (byte);
|
|---|
| 640 | case 'S':
|
|---|
| 641 | return JvPrimClass (short);
|
|---|
| 642 | case 'I':
|
|---|
| 643 | return JvPrimClass (int);
|
|---|
| 644 | case 'J':
|
|---|
| 645 | return JvPrimClass (long);
|
|---|
| 646 | case 'Z':
|
|---|
| 647 | return JvPrimClass (boolean);
|
|---|
| 648 | case 'C':
|
|---|
| 649 | return JvPrimClass (char);
|
|---|
| 650 | case 'F':
|
|---|
| 651 | return JvPrimClass (float);
|
|---|
| 652 | case 'D':
|
|---|
| 653 | return JvPrimClass (double);
|
|---|
| 654 | case 'V':
|
|---|
| 655 | return JvPrimClass (void);
|
|---|
| 656 | case 'L':
|
|---|
| 657 | {
|
|---|
| 658 | int i;
|
|---|
| 659 | for (i = 1; sig[i] && sig[i] != ';'; ++i)
|
|---|
| 660 | ;
|
|---|
| 661 | _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
|
|---|
| 662 | return _Jv_FindClass (name, loader);
|
|---|
| 663 |
|
|---|
| 664 | }
|
|---|
| 665 | case '[':
|
|---|
| 666 | {
|
|---|
| 667 | jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
|
|---|
| 668 | if (! klass)
|
|---|
| 669 | return NULL;
|
|---|
| 670 | return _Jv_GetArrayClass (klass, loader);
|
|---|
| 671 | }
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | return NULL; // Placate compiler.
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | |
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 | JArray<jstring> *
|
|---|
| 681 | JvConvertArgv (int argc, const char **argv)
|
|---|
| 682 | {
|
|---|
| 683 | if (argc < 0)
|
|---|
| 684 | argc = 0;
|
|---|
| 685 | jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
|
|---|
| 686 | jobject *ptr = elements(ar);
|
|---|
| 687 | jbyteArray bytes = NULL;
|
|---|
| 688 | for (int i = 0; i < argc; i++)
|
|---|
| 689 | {
|
|---|
| 690 | const char *arg = argv[i];
|
|---|
| 691 | int len = strlen (arg);
|
|---|
| 692 | if (bytes == NULL || bytes->length < len)
|
|---|
| 693 | bytes = JvNewByteArray (len);
|
|---|
| 694 | jbyte *bytePtr = elements (bytes);
|
|---|
| 695 | // We assume jbyte == char.
|
|---|
| 696 | memcpy (bytePtr, arg, len);
|
|---|
| 697 |
|
|---|
| 698 | // Now convert using the default encoding.
|
|---|
| 699 | *ptr++ = new java::lang::String (bytes, 0, len);
|
|---|
| 700 | }
|
|---|
| 701 | return (JArray<jstring>*) ar;
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | // FIXME: These variables are static so that they will be
|
|---|
| 705 | // automatically scanned by the Boehm collector. This is needed
|
|---|
| 706 | // because with qthreads the collector won't scan the initial stack --
|
|---|
| 707 | // it will only scan the qthreads stacks.
|
|---|
| 708 |
|
|---|
| 709 | // Command line arguments.
|
|---|
| 710 | static JArray<jstring> *arg_vec;
|
|---|
| 711 |
|
|---|
| 712 | // The primary thread.
|
|---|
| 713 | static java::lang::Thread *main_thread;
|
|---|
| 714 |
|
|---|
| 715 | char *
|
|---|
| 716 | _Jv_ThisExecutable (void)
|
|---|
| 717 | {
|
|---|
| 718 | return _Jv_execName;
|
|---|
| 719 | }
|
|---|
| 720 |
|
|---|
| 721 | void
|
|---|
| 722 | _Jv_ThisExecutable (const char *name)
|
|---|
| 723 | {
|
|---|
| 724 | if (name)
|
|---|
| 725 | {
|
|---|
| 726 | _Jv_execName = (char *) _Jv_Malloc (strlen (name) + 1);
|
|---|
| 727 | strcpy (_Jv_execName, name);
|
|---|
| 728 | }
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | #ifndef DISABLE_GETENV_PROPERTIES
|
|---|
| 732 |
|
|---|
| 733 | static char *
|
|---|
| 734 | next_property_key (char *s, size_t *length)
|
|---|
| 735 | {
|
|---|
| 736 | size_t l = 0;
|
|---|
| 737 |
|
|---|
| 738 | JvAssert (s);
|
|---|
| 739 |
|
|---|
| 740 | // Skip over whitespace
|
|---|
| 741 | while (isspace (*s))
|
|---|
| 742 | s++;
|
|---|
| 743 |
|
|---|
| 744 | // If we've reached the end, return NULL. Also return NULL if for
|
|---|
| 745 | // some reason we've come across a malformed property string.
|
|---|
| 746 | if (*s == 0
|
|---|
| 747 | || *s == ':'
|
|---|
| 748 | || *s == '=')
|
|---|
| 749 | return NULL;
|
|---|
| 750 |
|
|---|
| 751 | // Determine the length of the property key.
|
|---|
| 752 | while (s[l] != 0
|
|---|
| 753 | && ! isspace (s[l])
|
|---|
| 754 | && s[l] != ':'
|
|---|
| 755 | && s[l] != '=')
|
|---|
| 756 | {
|
|---|
| 757 | if (s[l] == '\\'
|
|---|
| 758 | && s[l+1] != 0)
|
|---|
| 759 | l++;
|
|---|
| 760 | l++;
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | *length = l;
|
|---|
| 764 |
|
|---|
| 765 | return s;
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | static char *
|
|---|
| 769 | next_property_value (char *s, size_t *length)
|
|---|
| 770 | {
|
|---|
| 771 | size_t l = 0;
|
|---|
| 772 |
|
|---|
| 773 | JvAssert (s);
|
|---|
| 774 |
|
|---|
| 775 | while (isspace (*s))
|
|---|
| 776 | s++;
|
|---|
| 777 |
|
|---|
| 778 | if (*s == ':'
|
|---|
| 779 | || *s == '=')
|
|---|
| 780 | s++;
|
|---|
| 781 |
|
|---|
| 782 | while (isspace (*s))
|
|---|
| 783 | s++;
|
|---|
| 784 |
|
|---|
| 785 | // If we've reached the end, return NULL.
|
|---|
| 786 | if (*s == 0)
|
|---|
| 787 | return NULL;
|
|---|
| 788 |
|
|---|
| 789 | // Determine the length of the property value.
|
|---|
| 790 | while (s[l] != 0
|
|---|
| 791 | && ! isspace (s[l])
|
|---|
| 792 | && s[l] != ':'
|
|---|
| 793 | && s[l] != '=')
|
|---|
| 794 | {
|
|---|
| 795 | if (s[l] == '\\'
|
|---|
| 796 | && s[l+1] != 0)
|
|---|
| 797 | l += 2;
|
|---|
| 798 | else
|
|---|
| 799 | l++;
|
|---|
| 800 | }
|
|---|
| 801 |
|
|---|
| 802 | *length = l;
|
|---|
| 803 |
|
|---|
| 804 | return s;
|
|---|
| 805 | }
|
|---|
| 806 |
|
|---|
| 807 | static void
|
|---|
| 808 | process_gcj_properties ()
|
|---|
| 809 | {
|
|---|
| 810 | char *props = getenv("GCJ_PROPERTIES");
|
|---|
| 811 | char *p = props;
|
|---|
| 812 | size_t length;
|
|---|
| 813 | size_t property_count = 0;
|
|---|
| 814 |
|
|---|
| 815 | if (NULL == props)
|
|---|
| 816 | return;
|
|---|
| 817 |
|
|---|
| 818 | // Whip through props quickly in order to count the number of
|
|---|
| 819 | // property values.
|
|---|
| 820 | while (p && (p = next_property_key (p, &length)))
|
|---|
| 821 | {
|
|---|
| 822 | // Skip to the end of the key
|
|---|
| 823 | p += length;
|
|---|
| 824 |
|
|---|
| 825 | p = next_property_value (p, &length);
|
|---|
| 826 | if (p)
|
|---|
| 827 | p += length;
|
|---|
| 828 |
|
|---|
| 829 | property_count++;
|
|---|
| 830 | }
|
|---|
| 831 |
|
|---|
| 832 | // Allocate an array of property value/key pairs.
|
|---|
| 833 | _Jv_Environment_Properties =
|
|---|
| 834 | (property_pair *) malloc (sizeof(property_pair)
|
|---|
| 835 | * (property_count + 1));
|
|---|
| 836 |
|
|---|
| 837 | // Go through the properties again, initializing _Jv_Properties
|
|---|
| 838 | // along the way.
|
|---|
| 839 | p = props;
|
|---|
| 840 | property_count = 0;
|
|---|
| 841 | while (p && (p = next_property_key (p, &length)))
|
|---|
| 842 | {
|
|---|
| 843 | _Jv_Environment_Properties[property_count].key = p;
|
|---|
| 844 | _Jv_Environment_Properties[property_count].key_length = length;
|
|---|
| 845 |
|
|---|
| 846 | // Skip to the end of the key
|
|---|
| 847 | p += length;
|
|---|
| 848 |
|
|---|
| 849 | p = next_property_value (p, &length);
|
|---|
| 850 |
|
|---|
| 851 | _Jv_Environment_Properties[property_count].value = p;
|
|---|
| 852 | _Jv_Environment_Properties[property_count].value_length = length;
|
|---|
| 853 |
|
|---|
| 854 | if (p)
|
|---|
| 855 | p += length;
|
|---|
| 856 |
|
|---|
| 857 | property_count++;
|
|---|
| 858 | }
|
|---|
| 859 | memset ((void *) &_Jv_Environment_Properties[property_count],
|
|---|
| 860 | 0, sizeof (property_pair));
|
|---|
| 861 | {
|
|---|
| 862 | size_t i = 0;
|
|---|
| 863 |
|
|---|
| 864 | // Null terminate the strings.
|
|---|
| 865 | while (_Jv_Environment_Properties[i].key)
|
|---|
| 866 | {
|
|---|
| 867 | _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
|
|---|
| 868 | _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
|
|---|
| 869 | }
|
|---|
| 870 | }
|
|---|
| 871 | }
|
|---|
| 872 | #endif // DISABLE_GETENV_PROPERTIES
|
|---|
| 873 |
|
|---|
| 874 | namespace gcj
|
|---|
| 875 | {
|
|---|
| 876 | _Jv_Utf8Const *void_signature;
|
|---|
| 877 | _Jv_Utf8Const *clinit_name;
|
|---|
| 878 | _Jv_Utf8Const *init_name;
|
|---|
| 879 | _Jv_Utf8Const *finit_name;
|
|---|
| 880 |
|
|---|
| 881 | bool runtimeInitialized = false;
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | jint
|
|---|
| 885 | _Jv_CreateJavaVM (void* /*vm_args*/)
|
|---|
| 886 | {
|
|---|
| 887 | using namespace gcj;
|
|---|
| 888 |
|
|---|
| 889 | if (runtimeInitialized)
|
|---|
| 890 | return -1;
|
|---|
| 891 |
|
|---|
| 892 | runtimeInitialized = true;
|
|---|
| 893 |
|
|---|
| 894 | PROCESS_GCJ_PROPERTIES;
|
|---|
| 895 |
|
|---|
| 896 | _Jv_InitThreads ();
|
|---|
| 897 | _Jv_InitGC ();
|
|---|
| 898 | _Jv_InitializeSyncMutex ();
|
|---|
| 899 |
|
|---|
| 900 | /* Initialize Utf8 constants declared in jvm.h. */
|
|---|
| 901 | void_signature = _Jv_makeUtf8Const ("()V", 3);
|
|---|
| 902 | clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
|
|---|
| 903 | init_name = _Jv_makeUtf8Const ("<init>", 6);
|
|---|
| 904 | finit_name = _Jv_makeUtf8Const ("finit$", 6);
|
|---|
| 905 |
|
|---|
| 906 | /* Initialize built-in classes to represent primitive TYPEs. */
|
|---|
| 907 | _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1, &_Jv_byteVTable);
|
|---|
| 908 | _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2, &_Jv_shortVTable);
|
|---|
| 909 | _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4, &_Jv_intVTable);
|
|---|
| 910 | _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8, &_Jv_longVTable);
|
|---|
| 911 | _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1, &_Jv_booleanVTable);
|
|---|
| 912 | _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2, &_Jv_charVTable);
|
|---|
| 913 | _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4, &_Jv_floatVTable);
|
|---|
| 914 | _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8, &_Jv_doubleVTable);
|
|---|
| 915 | _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0, &_Jv_voidVTable);
|
|---|
| 916 |
|
|---|
| 917 | // Turn stack trace generation off while creating exception objects.
|
|---|
| 918 | _Jv_InitClass (&java::lang::Throwable::class$);
|
|---|
| 919 | java::lang::Throwable::trace_enabled = 0;
|
|---|
| 920 |
|
|---|
| 921 | INIT_SEGV;
|
|---|
| 922 | #ifdef HANDLE_FPE
|
|---|
| 923 | INIT_FPE;
|
|---|
| 924 | #else
|
|---|
| 925 | arithexception = new java::lang::ArithmeticException
|
|---|
| 926 | (JvNewStringLatin1 ("/ by zero"));
|
|---|
| 927 | #endif
|
|---|
| 928 |
|
|---|
| 929 | no_memory = new java::lang::OutOfMemoryError;
|
|---|
| 930 |
|
|---|
| 931 | java::lang::Throwable::trace_enabled = 1;
|
|---|
| 932 |
|
|---|
| 933 | #ifdef USE_LTDL
|
|---|
| 934 | LTDL_SET_PRELOADED_SYMBOLS ();
|
|---|
| 935 | #endif
|
|---|
| 936 |
|
|---|
| 937 | _Jv_platform_initialize ();
|
|---|
| 938 |
|
|---|
| 939 | _Jv_JNI_Init ();
|
|---|
| 940 |
|
|---|
| 941 | _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
|
|---|
| 942 |
|
|---|
| 943 | // Start the GC finalizer thread. A VirtualMachineError can be
|
|---|
| 944 | // thrown by the runtime if, say, threads aren't available. In this
|
|---|
| 945 | // case finalizers simply won't run.
|
|---|
| 946 | try
|
|---|
| 947 | {
|
|---|
| 948 | using namespace gnu::gcj::runtime;
|
|---|
| 949 | FinalizerThread *ft = new FinalizerThread ();
|
|---|
| 950 | ft->start ();
|
|---|
| 951 | }
|
|---|
| 952 | catch (java::lang::VirtualMachineError *ignore)
|
|---|
| 953 | {
|
|---|
| 954 | }
|
|---|
| 955 |
|
|---|
| 956 | return 0;
|
|---|
| 957 | }
|
|---|
| 958 |
|
|---|
| 959 | void
|
|---|
| 960 | _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
|
|---|
| 961 | bool is_jar)
|
|---|
| 962 | {
|
|---|
| 963 | _Jv_argv = argv;
|
|---|
| 964 | _Jv_argc = argc;
|
|---|
| 965 |
|
|---|
| 966 | java::lang::Runtime *runtime = NULL;
|
|---|
| 967 |
|
|---|
| 968 |
|
|---|
| 969 | #ifdef DISABLE_MAIN_ARGS
|
|---|
| 970 | _Jv_ThisExecutable ("[Embedded App]");
|
|---|
| 971 | #else
|
|---|
| 972 | #ifdef HAVE_PROC_SELF_EXE
|
|---|
| 973 | char exec_name[20];
|
|---|
| 974 | sprintf (exec_name, "/proc/%d/exe", getpid ());
|
|---|
| 975 | _Jv_ThisExecutable (exec_name);
|
|---|
| 976 | #else
|
|---|
| 977 | _Jv_ThisExecutable (argv[0]);
|
|---|
| 978 | #endif /* HAVE_PROC_SELF_EXE */
|
|---|
| 979 | #endif /* DISABLE_MAIN_ARGS */
|
|---|
| 980 |
|
|---|
| 981 | try
|
|---|
| 982 | {
|
|---|
| 983 | // Set this very early so that it is seen when java.lang.System
|
|---|
| 984 | // is initialized.
|
|---|
| 985 | if (is_jar)
|
|---|
| 986 | _Jv_Jar_Class_Path = strdup (name);
|
|---|
| 987 | _Jv_CreateJavaVM (NULL);
|
|---|
| 988 |
|
|---|
| 989 | // Get the Runtime here. We want to initialize it before searching
|
|---|
| 990 | // for `main'; that way it will be set up if `main' is a JNI method.
|
|---|
| 991 | runtime = java::lang::Runtime::getRuntime ();
|
|---|
| 992 |
|
|---|
| 993 | #ifdef DISABLE_MAIN_ARGS
|
|---|
| 994 | arg_vec = JvConvertArgv (0, 0);
|
|---|
| 995 | #else
|
|---|
| 996 | arg_vec = JvConvertArgv (argc - 1, argv + 1);
|
|---|
| 997 | #endif
|
|---|
| 998 |
|
|---|
| 999 | using namespace gnu::gcj::runtime;
|
|---|
| 1000 | if (klass)
|
|---|
| 1001 | main_thread = new FirstThread (klass, arg_vec);
|
|---|
| 1002 | else
|
|---|
| 1003 | main_thread = new FirstThread (JvNewStringLatin1 (name),
|
|---|
| 1004 | arg_vec, is_jar);
|
|---|
| 1005 | }
|
|---|
| 1006 | catch (java::lang::Throwable *t)
|
|---|
| 1007 | {
|
|---|
| 1008 | java::lang::System::err->println (JvNewStringLatin1
|
|---|
| 1009 | ("Exception during runtime initialization"));
|
|---|
| 1010 | t->printStackTrace();
|
|---|
| 1011 | runtime->exit (1);
|
|---|
| 1012 | }
|
|---|
| 1013 |
|
|---|
| 1014 | _Jv_AttachCurrentThread (main_thread);
|
|---|
| 1015 | _Jv_ThreadRun (main_thread);
|
|---|
| 1016 | _Jv_ThreadWait ();
|
|---|
| 1017 |
|
|---|
| 1018 | int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
|
|---|
| 1019 | runtime->exit (status);
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | void
|
|---|
| 1023 | JvRunMain (jclass klass, int argc, const char **argv)
|
|---|
| 1024 | {
|
|---|
| 1025 | _Jv_RunMain (klass, NULL, argc, argv, false);
|
|---|
| 1026 | }
|
|---|
| 1027 |
|
|---|
| 1028 | |
|---|
| 1029 |
|
|---|
| 1030 |
|
|---|
| 1031 | // Parse a string and return a heap size.
|
|---|
| 1032 | static size_t
|
|---|
| 1033 | parse_heap_size (const char *spec)
|
|---|
| 1034 | {
|
|---|
| 1035 | char *end;
|
|---|
| 1036 | unsigned long val = strtoul (spec, &end, 10);
|
|---|
| 1037 | if (*end == 'k' || *end == 'K')
|
|---|
| 1038 | val *= 1024;
|
|---|
| 1039 | else if (*end == 'm' || *end == 'M')
|
|---|
| 1040 | val *= 1048576;
|
|---|
| 1041 | return (size_t) val;
|
|---|
| 1042 | }
|
|---|
| 1043 |
|
|---|
| 1044 | // Set the initial heap size. This might be ignored by the GC layer.
|
|---|
| 1045 | // This must be called before _Jv_RunMain.
|
|---|
| 1046 | void
|
|---|
| 1047 | _Jv_SetInitialHeapSize (const char *arg)
|
|---|
| 1048 | {
|
|---|
| 1049 | size_t size = parse_heap_size (arg);
|
|---|
| 1050 | _Jv_GCSetInitialHeapSize (size);
|
|---|
| 1051 | }
|
|---|
| 1052 |
|
|---|
| 1053 | // Set the maximum heap size. This might be ignored by the GC layer.
|
|---|
| 1054 | // This must be called before _Jv_RunMain.
|
|---|
| 1055 | void
|
|---|
| 1056 | _Jv_SetMaximumHeapSize (const char *arg)
|
|---|
| 1057 | {
|
|---|
| 1058 | size_t size = parse_heap_size (arg);
|
|---|
| 1059 | _Jv_GCSetMaximumHeapSize (size);
|
|---|
| 1060 | }
|
|---|
| 1061 |
|
|---|
| 1062 | |
|---|
| 1063 |
|
|---|
| 1064 |
|
|---|
| 1065 | void *
|
|---|
| 1066 | _Jv_Malloc (jsize size)
|
|---|
| 1067 | {
|
|---|
| 1068 | if (__builtin_expect (size == 0, false))
|
|---|
| 1069 | size = 1;
|
|---|
| 1070 | void *ptr = malloc ((size_t) size);
|
|---|
| 1071 | if (__builtin_expect (ptr == NULL, false))
|
|---|
| 1072 | throw no_memory;
|
|---|
| 1073 | return ptr;
|
|---|
| 1074 | }
|
|---|
| 1075 |
|
|---|
| 1076 | void *
|
|---|
| 1077 | _Jv_Realloc (void *ptr, jsize size)
|
|---|
| 1078 | {
|
|---|
| 1079 | if (__builtin_expect (size == 0, false))
|
|---|
| 1080 | size = 1;
|
|---|
| 1081 | ptr = realloc (ptr, (size_t) size);
|
|---|
| 1082 | if (__builtin_expect (ptr == NULL, false))
|
|---|
| 1083 | throw no_memory;
|
|---|
| 1084 | return ptr;
|
|---|
| 1085 | }
|
|---|
| 1086 |
|
|---|
| 1087 | void *
|
|---|
| 1088 | _Jv_MallocUnchecked (jsize size)
|
|---|
| 1089 | {
|
|---|
| 1090 | if (__builtin_expect (size == 0, false))
|
|---|
| 1091 | size = 1;
|
|---|
| 1092 | return malloc ((size_t) size);
|
|---|
| 1093 | }
|
|---|
| 1094 |
|
|---|
| 1095 | void
|
|---|
| 1096 | _Jv_Free (void* ptr)
|
|---|
| 1097 | {
|
|---|
| 1098 | return free (ptr);
|
|---|
| 1099 | }
|
|---|
| 1100 |
|
|---|
| 1101 | |
|---|
| 1102 |
|
|---|
| 1103 |
|
|---|
| 1104 | // In theory, these routines can be #ifdef'd away on machines which
|
|---|
| 1105 | // support divide overflow signals. However, we never know if some
|
|---|
| 1106 | // code might have been compiled with "-fuse-divide-subroutine", so we
|
|---|
| 1107 | // always include them in libgcj.
|
|---|
| 1108 |
|
|---|
| 1109 | jint
|
|---|
| 1110 | _Jv_divI (jint dividend, jint divisor)
|
|---|
| 1111 | {
|
|---|
| 1112 | if (__builtin_expect (divisor == 0, false))
|
|---|
| 1113 | _Jv_ThrowSignal (arithexception);
|
|---|
| 1114 |
|
|---|
| 1115 | if (dividend == (jint) 0x80000000L && divisor == -1)
|
|---|
| 1116 | return dividend;
|
|---|
| 1117 |
|
|---|
| 1118 | return dividend / divisor;
|
|---|
| 1119 | }
|
|---|
| 1120 |
|
|---|
| 1121 | jint
|
|---|
| 1122 | _Jv_remI (jint dividend, jint divisor)
|
|---|
| 1123 | {
|
|---|
| 1124 | if (__builtin_expect (divisor == 0, false))
|
|---|
| 1125 | _Jv_ThrowSignal (arithexception);
|
|---|
| 1126 |
|
|---|
| 1127 | if (dividend == (jint) 0x80000000L && divisor == -1)
|
|---|
| 1128 | return 0;
|
|---|
| 1129 |
|
|---|
| 1130 | return dividend % divisor;
|
|---|
| 1131 | }
|
|---|
| 1132 |
|
|---|
| 1133 | jlong
|
|---|
| 1134 | _Jv_divJ (jlong dividend, jlong divisor)
|
|---|
| 1135 | {
|
|---|
| 1136 | if (__builtin_expect (divisor == 0, false))
|
|---|
| 1137 | _Jv_ThrowSignal (arithexception);
|
|---|
| 1138 |
|
|---|
| 1139 | if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
|
|---|
| 1140 | return dividend;
|
|---|
| 1141 |
|
|---|
| 1142 | return dividend / divisor;
|
|---|
| 1143 | }
|
|---|
| 1144 |
|
|---|
| 1145 | jlong
|
|---|
| 1146 | _Jv_remJ (jlong dividend, jlong divisor)
|
|---|
| 1147 | {
|
|---|
| 1148 | if (__builtin_expect (divisor == 0, false))
|
|---|
| 1149 | _Jv_ThrowSignal (arithexception);
|
|---|
| 1150 |
|
|---|
| 1151 | if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
|
|---|
| 1152 | return 0;
|
|---|
| 1153 |
|
|---|
| 1154 | return dividend % divisor;
|
|---|
| 1155 | }
|
|---|