X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Fnative%2Fnative.c;h=1ab67dbe1d4f78ac3fb6227ad82dc8702afe37e0;hb=d75b6037acf17c342166b9c9bd6e657dfdd12cd9;hp=1a5badf57c6e9e246e66fc316b988710cf565bf9;hpb=adefdbd08d4990e4f043e14410b6f189d37b60f6;p=cacao.git diff --git a/src/native/native.c b/src/native/native.c index 1a5badf57..1ab67dbe1 100644 --- a/src/native/native.c +++ b/src/native/native.c @@ -1,9 +1,9 @@ -/* native.c - table of native functions +/* native/native.c - table of native functions - Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 - R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, - M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, - P. Tomsich, J. Wenninger + Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates, + R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner, + C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger, + Institut f. Computersprachen - TU Wien This file is part of CACAO. @@ -31,7 +31,7 @@ The .hh files created with the header file generator are all included here as are the C functions implementing these methods. - $Id: native.c 1331 2004-07-21 15:46:54Z twisti $ + $Id: native.c 1735 2004-12-07 14:33:27Z twisti $ */ @@ -45,26 +45,6 @@ #include #include -#include "config.h" -#include "exceptions.h" -#include "global.h" -#include "options.h" -#include "jni.h" -#include "native.h" -#include "builtin.h" -#include "asmpart.h" -#include "tables.h" -#include "loader.h" -#include "jni.h" -#include "jit/jit.h" -#include "toolbox/logging.h" -#include "toolbox/memory.h" -#include "threads/thread.h" -#include "threads/threadio.h" -#include "threads/locks.h" -#include "nat/java_lang_VMClass.h" -#include "nat/java_lang_Throwable.h" - /* Include files for IO functions */ #include @@ -75,15 +55,38 @@ #endif #include +#include "config.h" +#include "mm/memory.h" +#include "native/jni.h" +#include "native/native.h" +#include "native/include/java_lang_Throwable.h" +#include "toolbox/logging.h" +#include "vm/builtin.h" +#include "vm/exceptions.h" +#include "vm/global.h" +#include "vm/loader.h" +#include "vm/options.h" +#include "vm/tables.h" +#include "vm/jit/asmpart.h" +#include "vm/jit/jit.h" +#if 0 +#include "threads/thread.h" #include "threads/threadio.h" +#include "threads/locks.h" +#endif -/* include table of native functions */ -#include "nativetable.inc" +/* include table of native functions ******************************************/ +/* XXX quick hack? */ +#if defined(USE_GTK) +#include "native/vm/GtkComponentPeer.c" +#include "native/vm/GtkScrollPanePeer.c" +#include "native/vm/GtkFileDialogPeer.c" +#endif + +#include "nativetable.inc" -/* searchpath for classfiles */ -char *classpath; /* for java-string to char conversion */ #define MAXSTRINGSIZE 1000 @@ -130,22 +133,12 @@ void use_class_as_object(classinfo *c) } -/*************************** include native methods ***************************/ - -#ifdef USE_GTK -#include "nat/GdkGraphics.c" -#include "nat/GtkComponentPeer.c" -#include "nat/GdkPixbufDecoder.c" -#include "nat/GtkScrollPanePeer.c" -#include "nat/GtkFileDialogPeer.c" -#endif - - /************************** tables for methods ********************************/ #undef JOWENN_DEBUG #undef JOWENN_DEBUG1 +#ifdef STATIC_CLASSPATH #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref)) /* table for fast string comparison */ @@ -153,11 +146,23 @@ static nativecompref nativecomptable[NATIVETABLESIZE]; /* string comparsion table initialized */ static bool nativecompdone = false; +#endif + +/* XXX don't define this in a header file!!! */ + +static struct nativeCall nativeCalls[] = +{ +#include "nativecalls.inc" +}; + +#define NATIVECALLSSIZE (sizeof(nativeCalls) / sizeof(struct nativeCall)) + +struct nativeCompCall nativeCompCalls[NATIVECALLSSIZE]; /******************************************************************************/ -#include "natcalls.h" +/**include "natcalls.h" **/ /*********************** function: native_loadclasses ************************** @@ -258,15 +263,6 @@ void init_systemclassloader() } -/********************* function: native_setclasspath **************************/ - -void native_setclasspath(char *path) -{ - /* set searchpath for classfiles */ - classpath = path; -} - - /*********************** Function: native_findfunction ************************* Looks up a method (must have the same class name, method name, descriptor @@ -281,6 +277,7 @@ void native_setclasspath(char *path) functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic) { +#ifdef STATIC_CLASSPATH int i; /* entry of table for fast string comparison */ struct nativecompref *n; @@ -378,6 +375,10 @@ functionptr native_findfunction(utf *cname, utf *mname, /* keep compiler happy */ return NULL; +#else +/* dynamic classpath */ + return 0; +#endif } @@ -392,12 +393,20 @@ functionptr native_findfunction(utf *cname, utf *mname, java_lang_String *javastring_new(utf *u) { - char *utf_ptr = u->text; /* current utf character in utf string */ - int utflength = utf_strlen(u); /* length of utf-string if uncompressed */ + char *utf_ptr; /* current utf character in utf string */ + u4 utflength; /* length of utf-string if uncompressed */ java_lang_String *s; /* result-string */ java_chararray *a; s4 i; - + + if (!u) { + *exceptionptr = new_nullpointerexception(); + return NULL; + } + + utf_ptr = u->text; + utflength = utf_strlen(u); + s = (java_lang_String *) builtin_new(class_java_lang_String); a = builtin_newarray_char(utflength); @@ -430,10 +439,17 @@ java_lang_String *javastring_new(utf *u) java_lang_String *javastring_new_char(char *text) { s4 i; - s4 len = strlen(text); /* length of the string */ + s4 len; /* length of the string */ java_lang_String *s; /* result-string */ java_chararray *a; - + + if (!text) { + *exceptionptr = new_nullpointerexception(); + return NULL; + } + + len = strlen(text); + s = (java_lang_String *) builtin_new(class_java_lang_String); a = builtin_newarray_char(len); @@ -560,7 +576,7 @@ java_objectheader *native_new_and_init(classinfo *c) utf_new_char("()V")); if (!m) { /* initializer not found */ - if (verbose) { + if (opt_verbose) { char logtext[MAXLOGTEXT]; sprintf(logtext, "Warning: class has no instance-initializer: "); utf_sprint_classname(logtext + strlen(logtext), c->name); @@ -597,7 +613,7 @@ java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) utf_new_char("(Ljava/lang/String;)V")); if (!m) { /* initializer not found */ - if (verbose) { + if (opt_verbose) { char logtext[MAXLOGTEXT]; sprintf(logtext, "Warning: class has no instance-initializer: "); utf_sprint_classname(logtext + strlen(logtext), c->name); @@ -633,7 +649,7 @@ java_objectheader *native_new_and_init_int(classinfo *c, s4 i) utf_new_char("(I)V")); if (!m) { /* initializer not found */ - if (verbose) { + if (opt_verbose) { char logtext[MAXLOGTEXT]; sprintf(logtext, "Warning: class has no instance-initializer: "); utf_sprint_classname(logtext + strlen(logtext), c->name); @@ -673,7 +689,7 @@ java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwab utf_new_char("(Ljava/lang/Throwable;)V")); if (!m) { /* initializer not found */ - if (verbose) { + if (opt_verbose) { char logtext[MAXLOGTEXT]; sprintf(logtext, "Warning: class has no instance-initializer: "); utf_sprint_classname(logtext + strlen(logtext), c->name); @@ -865,12 +881,11 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset, literalstring *s; /* hashtable element */ java_lang_String *js; /* u2-array wrapped in javastring */ java_chararray *stringdata; /* copy of u2-array */ - classinfo *c; u4 key; u4 slot; u2 i; -//#define DEBUG_LITERALSTRING_U2 +/* #define DEBUG_LITERALSTRING_U2 */ #ifdef DEBUG_LITERALSTRING_U2 printf("literalstring_u2: length=%d, offset=%d\n", length, offset); fflush(stdout); @@ -893,7 +908,7 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset, /* string already in hashtable, free memory */ if (!copymode) - lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10); + mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10); #ifdef DEBUG_LITERALSTRING_U2 printf("literalstring_u2: foundentry at %p\n", js); @@ -912,7 +927,7 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset, if (copymode) { /* create copy of u2-array for new javastring */ u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10; - stringdata = lit_mem_alloc(arraysize); + stringdata = mem_alloc(arraysize); /* memcpy(stringdata, a, arraysize); */ memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader)); memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10); @@ -936,12 +951,15 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset, list_addfirst(&unlinkedclasses, class_java_lang_String); } - /* create new javastring */ - js = LNEW(java_lang_String); + /* create new javastring */ + js = NEW(java_lang_String); +#if defined(USE_THREADS) && defined(NATIVE_THREADS) + initObjectLock(&js->header); +#endif js->header.vftbl = class_java_lang_String->vftbl; - js->value = stringdata; - js->offset = 0; - js->count = length; + js->value = stringdata; + js->offset = 0; + js->count = length; #ifdef DEBUG_LITERALSTRING_U2 printf("literalstring_u2: newly created at %p\n", js); @@ -950,19 +968,19 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset, fflush(stdout); #endif - /* create new literalstring */ - s = NEW(literalstring); - s->hashlink = string_hash.ptr[slot]; - s->string = (java_objectheader *) js; - string_hash.ptr[slot] = s; + /* create new literalstring */ + s = NEW(literalstring); + s->hashlink = string_hash.ptr[slot]; + s->string = (java_objectheader *) js; + string_hash.ptr[slot] = s; - /* update number of hashtable entries */ - string_hash.entries++; + /* update number of hashtable entries */ + string_hash.entries++; - /* reorganization of hashtable */ - if (string_hash.entries > (string_hash.size * 2)) { + /* reorganization of hashtable */ + if (string_hash.entries > (string_hash.size * 2)) { /* reorganization of hashtable, average length of - the external chains is approx. 2 */ + the external chains is approx. 2 */ u4 i; literalstring *s; @@ -991,9 +1009,9 @@ java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset, /* dispose old table */ MFREE(string_hash.ptr, void*, string_hash.size); string_hash = newhash; - } + } - return (java_objectheader *) js; + return (java_objectheader *) js; } @@ -1012,7 +1030,7 @@ java_objectheader *literalstring_new(utf *u) u4 i; /* allocate memory */ - a = lit_mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10); + a = mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10); /* convert utf-string to u2-array */ for (i = 0; i < utflength; i++) @@ -1034,10 +1052,10 @@ void literalstring_free(java_objectheader* sobj) java_chararray *a = s->value; /* dispose memory of java.lang.String object */ - LFREE(s, java_lang_String); + FREE(s, java_lang_String); /* dispose memory of java-characterarray */ - LFREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */ + FREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */ } @@ -1083,7 +1101,7 @@ utf *create_methodsig(java_objectarray* types, char *retType) if (retType) buffer_size += strlen(retType); /* allocate buffer */ - buffer = MNEW(u1, buffer_size); + buffer = MNEW(char, buffer_size); pos = buffer; /* method-desciptor starts with parenthesis */ @@ -1149,7 +1167,7 @@ utf *create_methodsig(java_objectarray* types, char *retType) /* create utf-string */ result = utf_new(buffer, (pos - buffer)); - MFREE(buffer, u1, buffer_size); + MFREE(buffer, char, buffer_size); return result; } @@ -1223,17 +1241,23 @@ java_objectarray* get_parametertypes(methodinfo *m) *******************************************************************************/ -java_objectarray* get_exceptiontypes(methodinfo *m) { - u2 exccount=m->thrownexceptionscount; +java_objectarray* get_exceptiontypes(methodinfo *m) +{ + u2 excount; u2 i; java_objectarray *result; + + excount = m->thrownexceptionscount; + /* create class-array */ - result = builtin_anewarray(exccount, class_java_lang_Class); - for (i=0;ithrownexceptions[i]); - use_class_as_object(oh); - result->data[i]=oh; + result = builtin_anewarray(excount, class_java_lang_Class); + + for (i = 0; i < excount; i++) { + java_objectheader *o = (java_objectheader *) (m->thrownexceptions[i]); + use_class_as_object((classinfo *) o); + result->data[i] = o; } + return result; } @@ -1387,57 +1411,71 @@ return true; /*--------------------------------------------------------*/ -java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start) +java_objectarray *builtin_asm_createclasscontextarray(classinfo **end, classinfo **start) { #if defined(__GNUC__) #warning platform dependend #endif - java_objectarray *tmpArray; - int i; - classinfo **current; + java_objectarray *tmpArray; + int i; + classinfo **current; classinfo *c; - size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*); - /*printf("end %p, start %p, size %ld\n",end,start,size);*/ - if (!class_java_lang_Class) - class_java_lang_Class = class_new(utf_new_char ("java/lang/Class")); - if (!class_java_lang_SecurityManager) - class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager")); - if (size>0) { - if (start==class_java_lang_SecurityManager) { + size_t size; + + size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*); + + /*printf("end %p, start %p, size %ld\n",end,start,size);*/ + if (!class_java_lang_Class) + class_java_lang_Class = class_new(utf_new_char("java/lang/Class")); + + if (!class_java_lang_SecurityManager) + class_java_lang_SecurityManager = + class_new(utf_new_char("java/lang/SecurityManager")); + + if (size > 0) { + if (start == class_java_lang_SecurityManager) { size--; start--; } } - tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl); - for(i=0,current=start;ivftbl); + + for(i = 0, current = start; i < size; i++, current--) { + c = *current; + /* printf("%d\n",i); utf_display(c->name);*/ use_class_as_object(c); - tmpArray->data[i]=c; - } - return tmpArray; + tmpArray->data[i] = (java_objectheader *) c; + } + return tmpArray; } -java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start) + +java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end, classinfo **start) { #if defined(__GNUC__) #warning platform dependend #endif - int i; - classinfo **current; + int i; + classinfo **current; classinfo *c; classinfo *privilegedAction; - size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*); -/* log_text("builtin_asm_getclassloader"); + size_t size; + + size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*); + + /* log_text("builtin_asm_getclassloader"); printf("end %p, start %p, size %ld\n",end,start,size);*/ - if (!class_java_lang_SecurityManager) - class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager")); - if (size>0) { - if (start==class_java_lang_SecurityManager) { + if (!class_java_lang_SecurityManager) + class_java_lang_SecurityManager = + class_new(utf_new_char("java/lang/SecurityManager")); + + if (size > 0) { + if (start == class_java_lang_SecurityManager) { size--; start--; } @@ -1445,23 +1483,26 @@ java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **st privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction")); - for(i=0,current=start;iclassloader) return c->classloader; - } - return NULL; + for(i = 0, current = start; i < size; i++, current--) { + c = *current; + if (c == privilegedAction) + return NULL; + if (c->classloader) + return (java_lang_ClassLoader *) c->classloader; + } + return NULL; -/* + /* log_text("Java_java_lang_VMSecurityManager_currentClassLoader"); init_systemclassloader(); return SystemClassLoader;*/ } + /* * These are local overrides for various environment variables in Emacs. * Please do not remove this and leave it at the end of the file, where