1 /* native/native.c - table of native functions
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
31 The .hh files created with the header file generator are all
32 included here as are the C functions implementing these methods.
34 $Id: native.c 1625 2004-11-30 15:12:42Z motse $
48 /* Include files for IO functions */
52 #include <sys/types.h>
59 #include "mm/memory.h"
60 #include "native/jni.h"
61 #include "native/native.h"
62 #include "native/include/java_lang_Throwable.h"
63 #include "toolbox/logging.h"
64 #include "vm/builtin.h"
65 #include "vm/exceptions.h"
66 #include "vm/global.h"
67 #include "vm/loader.h"
68 #include "vm/options.h"
69 #include "vm/tables.h"
70 #include "vm/jit/asmpart.h"
71 #include "vm/jit/jit.h"
73 #include "threads/thread.h"
74 #include "threads/threadio.h"
75 #include "threads/locks.h"
79 /* include table of native functions ******************************************/
81 #include "nativetable.inc"
84 /* for java-string to char conversion */
85 #define MAXSTRINGSIZE 1000
88 /******************** systemclasses required for native methods ***************/
90 classinfo *class_java_lang_Class;
91 classinfo *class_java_lang_VMClass;
92 classinfo *class_java_lang_System;
93 classinfo *class_java_lang_ClassLoader;
94 classinfo *class_gnu_java_lang_SystemClassLoader;
95 classinfo *class_java_lang_SecurityManager;
96 classinfo *class_java_lang_Double;
97 classinfo *class_java_lang_Float;
98 classinfo *class_java_lang_Long;
99 classinfo *class_java_lang_Byte;
100 classinfo *class_java_lang_Short;
101 classinfo *class_java_lang_Boolean;
102 classinfo *class_java_lang_Void;
103 classinfo *class_java_lang_Character;
104 classinfo *class_java_lang_Integer;
106 methodinfo *method_vmclass_init;
109 /* the system classloader object */
110 struct java_lang_ClassLoader *SystemClassLoader = NULL;
112 /* for raising exceptions from native methods */
113 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
114 java_objectheader* _exceptionptr = NULL;
117 /************* use classinfo structure as java.lang.Class object **************/
119 void use_class_as_object(classinfo *c)
121 if (!c->classvftbl) {
122 c->header.vftbl = class_java_lang_Class->vftbl;
123 c->classvftbl = true;
129 /*************************** include native methods ***************************/
132 #include "vm/GtkComponentPeer.c"
133 #include "vm/GtkScrollPanePeer.c"
134 #include "vm/GtkFileDialogPeer.c"
138 /************************** tables for methods ********************************/
143 #ifdef STATIC_CLASSPATH
144 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
146 /* table for fast string comparison */
147 static nativecompref nativecomptable[NATIVETABLESIZE];
149 /* string comparsion table initialized */
150 static bool nativecompdone = false;
153 /******************************************************************************/
155 #include "natcalls.h"
158 /*********************** function: native_loadclasses **************************
160 load classes required for native methods
162 *******************************************************************************/
164 void native_loadclasses()
166 static int classesLoaded = 0; /*temporary hack JoWenn*/
173 class_java_lang_Cloneable =
174 class_new(utf_new_char("java/lang/Cloneable"));
175 class_load(class_java_lang_Cloneable);
176 class_link(class_java_lang_Cloneable);
178 class_java_lang_Class =
179 class_new(utf_new_char("java/lang/Class"));
180 class_load(class_java_lang_Class);
181 class_link(class_java_lang_Class);
183 class_java_lang_VMClass =
184 class_new(utf_new_char("java/lang/VMClass"));
185 class_load(class_java_lang_VMClass);
186 class_link(class_java_lang_VMClass);
188 class_java_lang_ClassLoader =
189 class_new(utf_new_char("java/lang/ClassLoader"));
190 class_load(class_java_lang_ClassLoader);
191 class_link(class_java_lang_ClassLoader);
193 /* load classes for wrapping primitive types */
194 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
195 class_load(class_java_lang_Double);
196 class_link(class_java_lang_Double);
198 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
199 class_load(class_java_lang_Float);
200 class_link(class_java_lang_Float);
202 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
203 class_load(class_java_lang_Character);
204 class_link(class_java_lang_Character);
206 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
207 class_load(class_java_lang_Integer);
208 class_link(class_java_lang_Integer);
210 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
211 class_load(class_java_lang_Long);
212 class_link(class_java_lang_Long);
214 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
215 class_load(class_java_lang_Byte);
216 class_link(class_java_lang_Byte);
218 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
219 class_load(class_java_lang_Short);
220 class_link(class_java_lang_Short);
222 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
223 class_load(class_java_lang_Boolean);
224 class_link(class_java_lang_Boolean);
226 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
227 class_load(class_java_lang_Void);
228 class_link(class_java_lang_Void);
233 /*****************************************************************************
235 create systemclassloader object and initialize instance fields
237 ******************************************************************************/
239 void init_systemclassloader()
241 log_text("init_systemclassloader");
242 if (!SystemClassLoader) {
243 native_loadclasses();
244 log_text("Initializing new system class loader");
245 /* create object and call initializer */
246 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
248 /* systemclassloader has no parent */
249 SystemClassLoader->parent = NULL;
250 SystemClassLoader->initialized = true;
252 log_text("leaving system class loader");
256 /*********************** Function: native_findfunction *************************
258 Looks up a method (must have the same class name, method name, descriptor
259 and 'static'ness) and returns a function pointer to it.
260 Returns: function pointer or NULL (if there is no such method)
262 Remark: For faster operation, the names/descriptors are converted from C
263 strings to Unicode the first time this function is called.
265 *******************************************************************************/
267 functionptr native_findfunction(utf *cname, utf *mname,
268 utf *desc, bool isstatic)
270 #ifdef STATIC_CLASSPATH
272 /* entry of table for fast string comparison */
273 struct nativecompref *n;
274 /* for warning message if no function is found */
278 isstatic = isstatic ? true : false;
280 if (!nativecompdone) {
281 for (i = 0; i < NATIVETABLESIZE; i++) {
282 nativecomptable[i].classname =
283 utf_new_char(nativetable[i].classname);
284 nativecomptable[i].methodname =
285 utf_new_char(nativetable[i].methodname);
286 nativecomptable[i].descriptor =
287 utf_new_char(nativetable[i].descriptor);
288 nativecomptable[i].isstatic =
289 nativetable[i].isstatic;
290 nativecomptable[i].func =
293 nativecompdone = true;
298 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
300 buffer = MNEW(char, buffer_len);
302 strcpy(buffer, "searching matching function in native table:");
303 utf_sprint(buffer+strlen(buffer), mname);
304 strcpy(buffer+strlen(buffer), ": ");
305 utf_sprint(buffer+strlen(buffer), desc);
306 strcpy(buffer+strlen(buffer), " for class ");
307 utf_sprint(buffer+strlen(buffer), cname);
311 MFREE(buffer, char, buffer_len);
314 for (i = 0; i < NATIVETABLESIZE; i++) {
315 n = &(nativecomptable[i]);
317 if (cname == n->classname && mname == n->methodname &&
318 desc == n->descriptor && isstatic == n->isstatic)
322 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
326 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
328 buffer = MNEW(char, buffer_len);
330 strcpy(buffer, "comparing with:");
331 utf_sprint(buffer+strlen(buffer), n->methodname);
332 strcpy (buffer+strlen(buffer), ": ");
333 utf_sprint(buffer+strlen(buffer), n->descriptor);
334 strcpy(buffer+strlen(buffer), " for class ");
335 utf_sprint(buffer+strlen(buffer), n->classname);
339 MFREE(buffer, char, buffer_len);
346 /* no function was found, display warning */
349 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
351 buffer = MNEW(char, buffer_len);
353 strcpy(buffer, "warning: native function ");
354 utf_sprint(buffer + strlen(buffer), mname);
355 strcpy(buffer + strlen(buffer), ": ");
356 utf_sprint(buffer + strlen(buffer), desc);
357 strcpy(buffer + strlen(buffer), " not found in class ");
358 utf_sprint(buffer + strlen(buffer), cname);
362 MFREE(buffer, char, buffer_len);
366 /* keep compiler happy */
369 /* dynamic classpath */
375 /********************** function: javastring_new *******************************
377 creates a new object of type java/lang/String with the text of
378 the specified utf8-string
380 return: pointer to the string or NULL if memory is exhausted.
382 *******************************************************************************/
384 java_lang_String *javastring_new(utf *u)
386 char *utf_ptr; /* current utf character in utf string */
387 u4 utflength; /* length of utf-string if uncompressed */
388 java_lang_String *s; /* result-string */
393 *exceptionptr = new_nullpointerexception();
398 utflength = utf_strlen(u);
400 s = (java_lang_String *) builtin_new(class_java_lang_String);
401 a = builtin_newarray_char(utflength);
403 /* javastring or character-array could not be created */
407 /* decompress utf-string */
408 for (i = 0; i < utflength; i++)
409 a->data[i] = utf_nextu2(&utf_ptr);
411 /* set fields of the javastring-object */
414 s->count = utflength;
420 /********************** function: javastring_new_char **************************
422 creates a new java/lang/String object which contains the convertet
423 C-string passed via text.
425 return: the object pointer or NULL if memory is exhausted.
427 *******************************************************************************/
429 java_lang_String *javastring_new_char(char *text)
432 s4 len; /* length of the string */
433 java_lang_String *s; /* result-string */
437 *exceptionptr = new_nullpointerexception();
443 s = (java_lang_String *) builtin_new(class_java_lang_String);
444 a = builtin_newarray_char(len);
446 /* javastring or character-array could not be created */
451 for (i = 0; i < len; i++)
452 a->data[i] = text[i];
454 /* set fields of the javastring-object */
463 /************************* function javastring_tochar **************************
465 converts a Java string into a C string.
467 return: pointer to C string
469 Caution: every call of this function overwrites the previous string !!!
471 *******************************************************************************/
473 static char stringbuffer[MAXSTRINGSIZE];
475 char *javastring_tochar(java_objectheader *so)
477 java_lang_String *s = (java_lang_String *) so;
489 if (s->count > MAXSTRINGSIZE)
492 for (i = 0; i < s->count; i++)
493 stringbuffer[i] = a->data[s->offset + i];
495 stringbuffer[i] = '\0';
501 /****************** function class_findfield_approx ****************************
503 searches in 'classinfo'-structure for a field with the
506 *******************************************************************************/
508 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
512 for (i = 0; i < c->fieldscount; i++) {
513 /* compare field names */
514 if ((c->fields[i].name == name))
515 return &(c->fields[i]);
518 /* field was not found, raise exception */
519 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
525 s4 class_findfield_index_approx(classinfo *c, utf *name)
529 for (i = 0; i < c->fieldscount; i++) {
530 /* compare field names */
531 if ((c->fields[i].name == name))
535 /* field was not found, raise exception */
536 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
542 /********************** function: native_new_and_init *************************
544 Creates a new object on the heap and calls the initializer.
545 Returns the object pointer or NULL if memory is exhausted.
547 *******************************************************************************/
549 java_objectheader *native_new_and_init(classinfo *c)
552 java_objectheader *o;
555 return *exceptionptr;
557 o = builtin_new(c); /* create object */
562 /* find initializer */
564 m = class_findmethod(c,
565 utf_new_char("<init>"),
566 utf_new_char("()V"));
568 if (!m) { /* initializer not found */
570 char logtext[MAXLOGTEXT];
571 sprintf(logtext, "Warning: class has no instance-initializer: ");
572 utf_sprint_classname(logtext + strlen(logtext), c->name);
578 /* call initializer */
580 asm_calljavafunction(m, o, NULL, NULL, NULL);
586 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
589 java_objectheader *o;
592 return *exceptionptr;
594 o = builtin_new(c); /* create object */
599 /* find initializer */
601 m = class_findmethod(c,
602 utf_new_char("<init>"),
603 utf_new_char("(Ljava/lang/String;)V"));
605 if (!m) { /* initializer not found */
607 char logtext[MAXLOGTEXT];
608 sprintf(logtext, "Warning: class has no instance-initializer: ");
609 utf_sprint_classname(logtext + strlen(logtext), c->name);
615 /* call initializer */
617 asm_calljavafunction(m, o, s, NULL, NULL);
623 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
626 java_objectheader *o;
629 return *exceptionptr;
631 o = builtin_new(c); /* create object */
635 /* find initializer */
637 m = class_findmethod(c,
638 utf_new_char("<init>"),
639 utf_new_char("(I)V"));
641 if (!m) { /* initializer not found */
643 char logtext[MAXLOGTEXT];
644 sprintf(logtext, "Warning: class has no instance-initializer: ");
645 utf_sprint_classname(logtext + strlen(logtext), c->name);
651 /* call initializer */
653 #if defined(__I386__) || defined(__POWERPC__)
654 asm_calljavafunction(m, o, (void *) i, NULL, NULL);
656 asm_calljavafunction(m, o, (void *) (s8) i, NULL, NULL);
663 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
666 java_objectheader *o;
669 return *exceptionptr;
671 o = builtin_new(c); /* create object */
675 /* find initializer */
677 m = class_findmethod(c,
678 utf_new_char("<init>"),
679 utf_new_char("(Ljava/lang/Throwable;)V"));
681 if (!m) { /* initializer not found */
683 char logtext[MAXLOGTEXT];
684 sprintf(logtext, "Warning: class has no instance-initializer: ");
685 utf_sprint_classname(logtext + strlen(logtext), c->name);
691 /* call initializer */
693 asm_calljavafunction(m, o, t, NULL, NULL);
699 /******************** function: stringtable_update ****************************
701 traverses the javastring hashtable and sets the vftbl-entries of
702 javastrings which were temporarily set to NULL, because
703 java.lang.Object was not yet loaded
705 *******************************************************************************/
707 void stringtable_update ()
709 java_lang_String *js;
711 literalstring *s; /* hashtable entry */
714 for (i = 0; i < string_hash.size; i++) {
715 s = string_hash.ptr[i];
719 js = (java_lang_String *) s->string;
721 if (!js || !js->value)
722 /* error in hashtable found */
723 panic("invalid literalstring in hashtable");
727 if (!js->header.vftbl)
728 /* vftbl of javastring is NULL */
729 js->header.vftbl = class_java_lang_String->vftbl;
731 if (!a->header.objheader.vftbl)
732 /* vftbl of character-array is NULL */
733 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
735 /* follow link in external hash chain */
743 /************************* function: u2_utflength ***************************
745 returns the utf length in bytes of a u2 array
747 *****************************************************************************/
749 u4 u2_utflength(u2 *text, u4 u2_length)
751 u4 result_len = 0; /* utf length in bytes */
752 u2 ch; /* current unicode character */
755 for (len = 0; len < u2_length; len++) {
756 /* next unicode character */
759 /* determine bytes required to store unicode character as utf */
760 if (ch && (ch < 0x80))
772 /********************* function: utf_new_u2 ***********************************
774 make utf symbol from u2 array,
775 if isclassname is true '.' is replaced by '/'
777 *******************************************************************************/
779 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
781 char *buffer; /* memory buffer for unicode characters */
782 char *pos; /* pointer to current position in buffer */
783 u4 left; /* unicode characters left */
784 u4 buflength; /* utf length in bytes of the u2 array */
785 utf *result; /* resulting utf-string */
788 /* determine utf length in bytes and allocate memory */
789 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
790 buflength = u2_utflength(unicode_pos, unicode_length);
791 buffer = MNEW(char, buflength);
796 for (i = 0; i++ < unicode_length; unicode_pos++) {
797 /* next unicode character */
800 if ((c != 0) && (c < 0x80)) {
803 if ((int) left < 0) break;
804 /* convert classname */
805 if (isclassname && c == '.')
810 } else if (c < 0x800) {
812 unsigned char high = c >> 6;
813 unsigned char low = c & 0x3F;
815 if ((int) left < 0) break;
816 *pos++ = high | 0xC0;
822 char mid = (c >> 6) & 0x3F;
825 if ((int) left < 0) break;
826 *pos++ = high | 0xE0;
832 /* insert utf-string into symbol-table */
833 result = utf_new(buffer,buflength);
835 MFREE(buffer, char, buflength);
841 /********************* function: javastring_toutf *****************************
843 make utf symbol from javastring
845 *******************************************************************************/
847 utf *javastring_toutf(java_lang_String *string, bool isclassname)
849 java_lang_String *str = (java_lang_String *) string;
851 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
852 /* fflush(stdout); */
854 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
858 /********************* function: literalstring_u2 *****************************
860 searches for the javastring with the specified u2-array in
861 the string hashtable, if there is no such string a new one is
864 if copymode is true a copy of the u2-array is made
866 *******************************************************************************/
868 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
871 literalstring *s; /* hashtable element */
872 java_lang_String *js; /* u2-array wrapped in javastring */
873 java_chararray *stringdata; /* copy of u2-array */
878 /* #define DEBUG_LITERALSTRING_U2 */
879 #ifdef DEBUG_LITERALSTRING_U2
880 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
884 /* find location in hashtable */
885 key = unicode_hashkey(a->data + offset, length);
886 slot = key & (string_hash.size - 1);
887 s = string_hash.ptr[slot];
890 js = (java_lang_String *) s->string;
892 if (length == js->count) {
894 for (i = 0; i < length; i++) {
895 if (a->data[offset + i] != js->value->data[i])
899 /* string already in hashtable, free memory */
901 mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
903 #ifdef DEBUG_LITERALSTRING_U2
904 printf("literalstring_u2: foundentry at %p\n", js);
905 utf_display(javastring_toutf(js, 0));
909 return (java_objectheader *) js;
913 /* follow link in external hash chain */
918 /* create copy of u2-array for new javastring */
919 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
920 stringdata = mem_alloc(arraysize);
921 /* memcpy(stringdata, a, arraysize); */
922 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
923 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
929 /* location in hashtable found, complete arrayheader */
930 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
931 stringdata->header.size = length;
933 /* if we use eager loading, we have to check loaded String class */
935 class_java_lang_String =
936 class_new_intern(utf_new_char("java/lang/String"));
938 if (!class_load(class_java_lang_String))
941 list_addfirst(&unlinkedclasses, class_java_lang_String);
944 /* create new javastring */
945 js = NEW(java_lang_String);
946 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
947 initObjectLock(&js->header);
949 js->header.vftbl = class_java_lang_String->vftbl;
950 js->value = stringdata;
954 #ifdef DEBUG_LITERALSTRING_U2
955 printf("literalstring_u2: newly created at %p\n", js);
956 utf_display(javastring_toutf(js, 0));
961 /* create new literalstring */
962 s = NEW(literalstring);
963 s->hashlink = string_hash.ptr[slot];
964 s->string = (java_objectheader *) js;
965 string_hash.ptr[slot] = s;
967 /* update number of hashtable entries */
968 string_hash.entries++;
970 /* reorganization of hashtable */
971 if (string_hash.entries > (string_hash.size * 2)) {
972 /* reorganization of hashtable, average length of
973 the external chains is approx. 2 */
977 hashtable newhash; /* the new hashtable */
979 /* create new hashtable, double the size */
980 init_hashtable(&newhash, string_hash.size * 2);
981 newhash.entries = string_hash.entries;
983 /* transfer elements to new hashtable */
984 for (i = 0; i < string_hash.size; i++) {
985 s = string_hash.ptr[i];
987 literalstring *nexts = s->hashlink;
988 js = (java_lang_String *) s->string;
989 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
991 s->hashlink = newhash.ptr[slot];
992 newhash.ptr[slot] = s;
994 /* follow link in external hash chain */
999 /* dispose old table */
1000 MFREE(string_hash.ptr, void*, string_hash.size);
1001 string_hash = newhash;
1004 return (java_objectheader *) js;
1008 /******************** Function: literalstring_new *****************************
1010 creates a new javastring with the text of the utf-symbol
1011 and inserts it into the string hashtable
1013 *******************************************************************************/
1015 java_objectheader *literalstring_new(utf *u)
1017 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1018 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1019 java_chararray *a; /* u2-array constructed from utf string */
1022 /* allocate memory */
1023 a = mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1025 /* convert utf-string to u2-array */
1026 for (i = 0; i < utflength; i++)
1027 a->data[i] = utf_nextu2(&utf_ptr);
1029 return literalstring_u2(a, utflength, 0, false);
1033 /********************** function: literalstring_free **************************
1035 removes a javastring from memory
1037 ******************************************************************************/
1039 void literalstring_free(java_objectheader* sobj)
1041 java_lang_String *s = (java_lang_String *) sobj;
1042 java_chararray *a = s->value;
1044 /* dispose memory of java.lang.String object */
1045 FREE(s, java_lang_String);
1047 /* dispose memory of java-characterarray */
1048 FREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1052 void copy_vftbl(vftbl_t **dest, vftbl_t *src)
1056 /* XXX this kind of copying does not work (in the general
1057 * case). The interface tables would have to be copied, too. I
1058 * don't see why we should make a copy anyway. -Edwin
1060 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1061 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1062 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1067 /******************************************************************************************
1069 creates method signature (excluding return type) from array of
1070 class-objects representing the parameters of the method
1072 *******************************************************************************************/
1075 utf *create_methodsig(java_objectarray* types, char *retType)
1077 char *buffer; /* buffer for building the desciptor */
1078 char *pos; /* current position in buffer */
1079 utf *result; /* the method signature */
1080 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1083 if (!types) return NULL;
1085 /* determine required buffer-size */
1086 for (i = 0; i < types->header.size; i++) {
1087 classinfo *c = (classinfo *) types->data[i];
1088 buffer_size = buffer_size + c->name->blength + 2;
1091 if (retType) buffer_size += strlen(retType);
1093 /* allocate buffer */
1094 buffer = MNEW(char, buffer_size);
1097 /* method-desciptor starts with parenthesis */
1100 for (i = 0; i < types->header.size; i++) {
1103 /* current argument */
1104 classinfo *c = (classinfo *) types->data[i];
1106 /* current position in utf-text */
1107 char *utf_ptr = c->name->text;
1109 /* determine type of argument */
1110 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1112 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1113 *pos++ = *utf_ptr; /* copy text */
1117 /* check for primitive types */
1118 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1119 char *utf_pos = utf_ptr - 1;
1120 char *primitive = primitivetype_table[j].wrapname;
1123 while (utf_pos < utf_end(c->name)) {
1124 if (*utf_pos++ != *primitive++) goto nomatch;
1127 /* primitive type found */
1128 *pos++ = primitivetype_table[j].typesig;
1135 /* no primitive type and no arrayclass, so must be object */
1139 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1153 for (i = 0; i < strlen(retType); i++) {
1154 *pos++ = retType[i];
1158 /* create utf-string */
1159 result = utf_new(buffer, (pos - buffer));
1160 MFREE(buffer, char, buffer_size);
1166 /******************************************************************************************
1168 retrieve the next argument or returntype from a descriptor
1169 and return the corresponding class
1171 *******************************************************************************************/
1173 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1175 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1176 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1179 panic("illegal descriptor");
1181 if (skip) return NULL;
1183 use_class_as_object(c);
1188 /* get_parametertypes **********************************************************
1190 use the descriptor of a method to generate a java/lang/Class array
1191 which contains the classes of the parametertypes of the method
1193 *******************************************************************************/
1195 java_objectarray* get_parametertypes(methodinfo *m)
1197 utf *descr = m->descriptor; /* method-descriptor */
1198 char *utf_ptr = descr->text; /* current position in utf-text */
1199 char *desc_end = utf_end(descr); /* points behind utf string */
1200 java_objectarray* result;
1201 int parametercount = 0;
1205 utf_nextu2(&utf_ptr);
1207 /* determine number of parameters */
1208 while (*utf_ptr != ')') {
1209 get_type(&utf_ptr, desc_end, true);
1213 /* create class-array */
1214 result = builtin_anewarray(parametercount, class_java_lang_Class);
1216 utf_ptr = descr->text;
1217 utf_nextu2(&utf_ptr);
1219 /* get returntype classes */
1220 for (i = 0; i < parametercount; i++)
1222 (java_objectheader *) get_type(&utf_ptr, desc_end, false);
1228 /* get_exceptiontypes **********************************************************
1230 get the exceptions which can be thrown by a method
1232 *******************************************************************************/
1234 java_objectarray* get_exceptiontypes(methodinfo *m)
1238 java_objectarray *result;
1240 excount = m->thrownexceptionscount;
1242 /* create class-array */
1243 result = builtin_anewarray(excount, class_java_lang_Class);
1245 for (i = 0; i < excount; i++) {
1246 java_objectheader *o = (java_objectheader *) (m->thrownexceptions[i]);
1247 use_class_as_object((classinfo *) o);
1248 result->data[i] = o;
1258 /******************************************************************************************
1260 get the returntype class of a method
1262 *******************************************************************************************/
1264 classinfo *get_returntype(methodinfo *m)
1266 char *utf_ptr; /* current position in utf-text */
1267 char *desc_end; /* points behind utf string */
1268 utf *desc = m->descriptor; /* method-descriptor */
1270 utf_ptr = desc->text;
1271 desc_end = utf_end(desc);
1273 /* ignore parametertypes */
1274 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1277 return get_type(&utf_ptr,desc_end, false);
1281 /*****************************************************************************/
1282 /*****************************************************************************/
1285 /*--------------------------------------------------------*/
1286 void printNativeCall(nativeCall nc) {
1289 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1290 for (i=0; i<nc.methCnt; i++) {
1291 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1293 for (j=0; j<nc.callCnt[i]; j++) {
1294 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1295 nc.methods[i].methodCalls[j].classname,
1296 nc.methods[i].methodCalls[j].methodname,
1297 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1300 printf("-+++++--------------------\n");fflush(stdout);
1303 /*--------------------------------------------------------*/
1304 void printCompNativeCall(nativeCompCall nc) {
1306 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1307 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1308 utf_display(nc.classname); fflush(stdout);
1310 for (i=0; i<nc.methCnt; i++) {
1311 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1312 utf_display(nc.methods[i].methodname); fflush(stdout);
1313 utf_display(nc.methods[i].descriptor);fflush(stdout);
1314 printf("\n");fflush(stdout);
1316 for (j=0; j<nc.callCnt[i]; j++) {
1317 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1318 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1319 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1320 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1321 printf("\n");fflush(stdout);
1324 printf("---------------------\n");fflush(stdout);
1328 /*--------------------------------------------------------*/
1329 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1342 /*--------------------------------------------------------*/
1343 nativeCall* findNativeClassCalls(char *aclassname ) {
1346 for (i=0;i<NATIVECALLSSIZE; i++) {
1347 /* convert table to utf later to speed up search */
1348 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1349 return &nativeCalls[i];
1354 /*--------------------------------------------------------*/
1355 /*--------------------------------------------------------*/
1356 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1360 ncc->classname = utf_new_char(nc.classname);
1361 ncc->methCnt = nc.methCnt;
1363 for (i=0; i<nc.methCnt; i++) {
1364 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1365 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1366 ncc->callCnt[i] = nc.callCnt[i];
1368 for (j=0; j<nc.callCnt[i]; j++) {
1370 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1372 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1373 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1374 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1377 ncc->methods[i].methodCalls[j].methodname = NULL;
1378 ncc->methods[i].methodCalls[j].descriptor = NULL;
1386 /*--------------------------------------------------------*/
1388 bool natcall2utf(bool natcallcompdone) {
1391 if (natcallcompdone)
1394 for (i=0;i<NATIVECALLSSIZE; i++) {
1395 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1401 /*--------------------------------------------------------*/
1404 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end, classinfo **start)
1406 #if defined(__GNUC__)
1407 #warning platform dependend
1409 java_objectarray *tmpArray;
1411 classinfo **current;
1415 size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
1417 /*printf("end %p, start %p, size %ld\n",end,start,size);*/
1418 if (!class_java_lang_Class)
1419 class_java_lang_Class = class_new(utf_new_char("java/lang/Class"));
1421 if (!class_java_lang_SecurityManager)
1422 class_java_lang_SecurityManager =
1423 class_new(utf_new_char("java/lang/SecurityManager"));
1426 if (start == class_java_lang_SecurityManager) {
1433 builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1435 for(i = 0, current = start; i < size; i++, current--) {
1437 /* printf("%d\n",i);
1438 utf_display(c->name);*/
1439 use_class_as_object(c);
1440 tmpArray->data[i] = (java_objectheader *) c;
1447 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end, classinfo **start)
1449 #if defined(__GNUC__)
1450 #warning platform dependend
1453 classinfo **current;
1455 classinfo *privilegedAction;
1458 size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
1460 /* log_text("builtin_asm_getclassloader");
1461 printf("end %p, start %p, size %ld\n",end,start,size);*/
1463 if (!class_java_lang_SecurityManager)
1464 class_java_lang_SecurityManager =
1465 class_new(utf_new_char("java/lang/SecurityManager"));
1468 if (start == class_java_lang_SecurityManager) {
1474 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1476 for(i = 0, current = start; i < size; i++, current--) {
1479 if (c == privilegedAction)
1483 return (java_lang_ClassLoader *) c->classloader;
1489 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1490 init_systemclassloader();
1492 return SystemClassLoader;*/
1497 * These are local overrides for various environment variables in Emacs.
1498 * Please do not remove this and leave it at the end of the file, where
1499 * Emacs will automagically detect them.
1500 * ---------------------------------------------------------------------
1503 * indent-tabs-mode: t