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 1727 2004-12-07 10:56:02Z twisti $
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 ******************************************/
83 #include "native/vm/GtkComponentPeer.c"
84 #include "native/vm/GtkScrollPanePeer.c"
85 #include "native/vm/GtkFileDialogPeer.c"
88 #include "nativetable.inc"
91 /* for java-string to char conversion */
92 #define MAXSTRINGSIZE 1000
95 /******************** systemclasses required for native methods ***************/
97 classinfo *class_java_lang_Class;
98 classinfo *class_java_lang_VMClass;
99 classinfo *class_java_lang_System;
100 classinfo *class_java_lang_ClassLoader;
101 classinfo *class_gnu_java_lang_SystemClassLoader;
102 classinfo *class_java_lang_SecurityManager;
103 classinfo *class_java_lang_Double;
104 classinfo *class_java_lang_Float;
105 classinfo *class_java_lang_Long;
106 classinfo *class_java_lang_Byte;
107 classinfo *class_java_lang_Short;
108 classinfo *class_java_lang_Boolean;
109 classinfo *class_java_lang_Void;
110 classinfo *class_java_lang_Character;
111 classinfo *class_java_lang_Integer;
113 methodinfo *method_vmclass_init;
116 /* the system classloader object */
117 struct java_lang_ClassLoader *SystemClassLoader = NULL;
119 /* for raising exceptions from native methods */
120 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
121 java_objectheader* _exceptionptr = NULL;
124 /************* use classinfo structure as java.lang.Class object **************/
126 void use_class_as_object(classinfo *c)
128 if (!c->classvftbl) {
129 c->header.vftbl = class_java_lang_Class->vftbl;
130 c->classvftbl = true;
136 /************************** tables for methods ********************************/
141 #ifdef STATIC_CLASSPATH
142 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
144 /* table for fast string comparison */
145 static nativecompref nativecomptable[NATIVETABLESIZE];
147 /* string comparsion table initialized */
148 static bool nativecompdone = false;
152 /* XXX don't define this in a header file!!! */
154 static struct nativeCall nativeCalls[] =
156 #include "nativecalls.inc"
159 #define NATIVECALLSSIZE (sizeof(nativeCalls) / sizeof(struct nativeCall))
161 struct nativeCompCall nativeCompCalls[NATIVECALLSSIZE];
163 /******************************************************************************/
165 /**include "natcalls.h" **/
168 /*********************** function: native_loadclasses **************************
170 load classes required for native methods
172 *******************************************************************************/
174 void native_loadclasses()
176 static int classesLoaded = 0; /*temporary hack JoWenn*/
183 class_java_lang_Cloneable =
184 class_new(utf_new_char("java/lang/Cloneable"));
185 class_load(class_java_lang_Cloneable);
186 class_link(class_java_lang_Cloneable);
188 class_java_lang_Class =
189 class_new(utf_new_char("java/lang/Class"));
190 class_load(class_java_lang_Class);
191 class_link(class_java_lang_Class);
193 class_java_lang_VMClass =
194 class_new(utf_new_char("java/lang/VMClass"));
195 class_load(class_java_lang_VMClass);
196 class_link(class_java_lang_VMClass);
198 class_java_lang_ClassLoader =
199 class_new(utf_new_char("java/lang/ClassLoader"));
200 class_load(class_java_lang_ClassLoader);
201 class_link(class_java_lang_ClassLoader);
203 /* load classes for wrapping primitive types */
204 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
205 class_load(class_java_lang_Double);
206 class_link(class_java_lang_Double);
208 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
209 class_load(class_java_lang_Float);
210 class_link(class_java_lang_Float);
212 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
213 class_load(class_java_lang_Character);
214 class_link(class_java_lang_Character);
216 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
217 class_load(class_java_lang_Integer);
218 class_link(class_java_lang_Integer);
220 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
221 class_load(class_java_lang_Long);
222 class_link(class_java_lang_Long);
224 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
225 class_load(class_java_lang_Byte);
226 class_link(class_java_lang_Byte);
228 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
229 class_load(class_java_lang_Short);
230 class_link(class_java_lang_Short);
232 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
233 class_load(class_java_lang_Boolean);
234 class_link(class_java_lang_Boolean);
236 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
237 class_load(class_java_lang_Void);
238 class_link(class_java_lang_Void);
243 /*****************************************************************************
245 create systemclassloader object and initialize instance fields
247 ******************************************************************************/
249 void init_systemclassloader()
251 log_text("init_systemclassloader");
252 if (!SystemClassLoader) {
253 native_loadclasses();
254 log_text("Initializing new system class loader");
255 /* create object and call initializer */
256 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
258 /* systemclassloader has no parent */
259 SystemClassLoader->parent = NULL;
260 SystemClassLoader->initialized = true;
262 log_text("leaving system class loader");
266 /*********************** Function: native_findfunction *************************
268 Looks up a method (must have the same class name, method name, descriptor
269 and 'static'ness) and returns a function pointer to it.
270 Returns: function pointer or NULL (if there is no such method)
272 Remark: For faster operation, the names/descriptors are converted from C
273 strings to Unicode the first time this function is called.
275 *******************************************************************************/
277 functionptr native_findfunction(utf *cname, utf *mname,
278 utf *desc, bool isstatic)
280 #ifdef STATIC_CLASSPATH
282 /* entry of table for fast string comparison */
283 struct nativecompref *n;
284 /* for warning message if no function is found */
288 isstatic = isstatic ? true : false;
290 if (!nativecompdone) {
291 for (i = 0; i < NATIVETABLESIZE; i++) {
292 nativecomptable[i].classname =
293 utf_new_char(nativetable[i].classname);
294 nativecomptable[i].methodname =
295 utf_new_char(nativetable[i].methodname);
296 nativecomptable[i].descriptor =
297 utf_new_char(nativetable[i].descriptor);
298 nativecomptable[i].isstatic =
299 nativetable[i].isstatic;
300 nativecomptable[i].func =
303 nativecompdone = true;
308 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
310 buffer = MNEW(char, buffer_len);
312 strcpy(buffer, "searching matching function in native table:");
313 utf_sprint(buffer+strlen(buffer), mname);
314 strcpy(buffer+strlen(buffer), ": ");
315 utf_sprint(buffer+strlen(buffer), desc);
316 strcpy(buffer+strlen(buffer), " for class ");
317 utf_sprint(buffer+strlen(buffer), cname);
321 MFREE(buffer, char, buffer_len);
324 for (i = 0; i < NATIVETABLESIZE; i++) {
325 n = &(nativecomptable[i]);
327 if (cname == n->classname && mname == n->methodname &&
328 desc == n->descriptor && isstatic == n->isstatic)
332 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
336 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
338 buffer = MNEW(char, buffer_len);
340 strcpy(buffer, "comparing with:");
341 utf_sprint(buffer+strlen(buffer), n->methodname);
342 strcpy (buffer+strlen(buffer), ": ");
343 utf_sprint(buffer+strlen(buffer), n->descriptor);
344 strcpy(buffer+strlen(buffer), " for class ");
345 utf_sprint(buffer+strlen(buffer), n->classname);
349 MFREE(buffer, char, buffer_len);
356 /* no function was found, display warning */
359 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
361 buffer = MNEW(char, buffer_len);
363 strcpy(buffer, "warning: native function ");
364 utf_sprint(buffer + strlen(buffer), mname);
365 strcpy(buffer + strlen(buffer), ": ");
366 utf_sprint(buffer + strlen(buffer), desc);
367 strcpy(buffer + strlen(buffer), " not found in class ");
368 utf_sprint(buffer + strlen(buffer), cname);
372 MFREE(buffer, char, buffer_len);
376 /* keep compiler happy */
379 /* dynamic classpath */
385 /********************** function: javastring_new *******************************
387 creates a new object of type java/lang/String with the text of
388 the specified utf8-string
390 return: pointer to the string or NULL if memory is exhausted.
392 *******************************************************************************/
394 java_lang_String *javastring_new(utf *u)
396 char *utf_ptr; /* current utf character in utf string */
397 u4 utflength; /* length of utf-string if uncompressed */
398 java_lang_String *s; /* result-string */
403 *exceptionptr = new_nullpointerexception();
408 utflength = utf_strlen(u);
410 s = (java_lang_String *) builtin_new(class_java_lang_String);
411 a = builtin_newarray_char(utflength);
413 /* javastring or character-array could not be created */
417 /* decompress utf-string */
418 for (i = 0; i < utflength; i++)
419 a->data[i] = utf_nextu2(&utf_ptr);
421 /* set fields of the javastring-object */
424 s->count = utflength;
430 /********************** function: javastring_new_char **************************
432 creates a new java/lang/String object which contains the convertet
433 C-string passed via text.
435 return: the object pointer or NULL if memory is exhausted.
437 *******************************************************************************/
439 java_lang_String *javastring_new_char(char *text)
442 s4 len; /* length of the string */
443 java_lang_String *s; /* result-string */
447 *exceptionptr = new_nullpointerexception();
453 s = (java_lang_String *) builtin_new(class_java_lang_String);
454 a = builtin_newarray_char(len);
456 /* javastring or character-array could not be created */
461 for (i = 0; i < len; i++)
462 a->data[i] = text[i];
464 /* set fields of the javastring-object */
473 /************************* function javastring_tochar **************************
475 converts a Java string into a C string.
477 return: pointer to C string
479 Caution: every call of this function overwrites the previous string !!!
481 *******************************************************************************/
483 static char stringbuffer[MAXSTRINGSIZE];
485 char *javastring_tochar(java_objectheader *so)
487 java_lang_String *s = (java_lang_String *) so;
499 if (s->count > MAXSTRINGSIZE)
502 for (i = 0; i < s->count; i++)
503 stringbuffer[i] = a->data[s->offset + i];
505 stringbuffer[i] = '\0';
511 /****************** function class_findfield_approx ****************************
513 searches in 'classinfo'-structure for a field with the
516 *******************************************************************************/
518 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
522 for (i = 0; i < c->fieldscount; i++) {
523 /* compare field names */
524 if ((c->fields[i].name == name))
525 return &(c->fields[i]);
528 /* field was not found, raise exception */
529 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
535 s4 class_findfield_index_approx(classinfo *c, utf *name)
539 for (i = 0; i < c->fieldscount; i++) {
540 /* compare field names */
541 if ((c->fields[i].name == name))
545 /* field was not found, raise exception */
546 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
552 /********************** function: native_new_and_init *************************
554 Creates a new object on the heap and calls the initializer.
555 Returns the object pointer or NULL if memory is exhausted.
557 *******************************************************************************/
559 java_objectheader *native_new_and_init(classinfo *c)
562 java_objectheader *o;
565 return *exceptionptr;
567 o = builtin_new(c); /* create object */
572 /* find initializer */
574 m = class_findmethod(c,
575 utf_new_char("<init>"),
576 utf_new_char("()V"));
578 if (!m) { /* initializer not found */
580 char logtext[MAXLOGTEXT];
581 sprintf(logtext, "Warning: class has no instance-initializer: ");
582 utf_sprint_classname(logtext + strlen(logtext), c->name);
588 /* call initializer */
590 asm_calljavafunction(m, o, NULL, NULL, NULL);
596 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
599 java_objectheader *o;
602 return *exceptionptr;
604 o = builtin_new(c); /* create object */
609 /* find initializer */
611 m = class_findmethod(c,
612 utf_new_char("<init>"),
613 utf_new_char("(Ljava/lang/String;)V"));
615 if (!m) { /* initializer not found */
617 char logtext[MAXLOGTEXT];
618 sprintf(logtext, "Warning: class has no instance-initializer: ");
619 utf_sprint_classname(logtext + strlen(logtext), c->name);
625 /* call initializer */
627 asm_calljavafunction(m, o, s, NULL, NULL);
633 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
636 java_objectheader *o;
639 return *exceptionptr;
641 o = builtin_new(c); /* create object */
645 /* find initializer */
647 m = class_findmethod(c,
648 utf_new_char("<init>"),
649 utf_new_char("(I)V"));
651 if (!m) { /* initializer not found */
653 char logtext[MAXLOGTEXT];
654 sprintf(logtext, "Warning: class has no instance-initializer: ");
655 utf_sprint_classname(logtext + strlen(logtext), c->name);
661 /* call initializer */
663 #if defined(__I386__) || defined(__POWERPC__)
664 asm_calljavafunction(m, o, (void *) i, NULL, NULL);
666 asm_calljavafunction(m, o, (void *) (s8) i, NULL, NULL);
673 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
676 java_objectheader *o;
679 return *exceptionptr;
681 o = builtin_new(c); /* create object */
685 /* find initializer */
687 m = class_findmethod(c,
688 utf_new_char("<init>"),
689 utf_new_char("(Ljava/lang/Throwable;)V"));
691 if (!m) { /* initializer not found */
693 char logtext[MAXLOGTEXT];
694 sprintf(logtext, "Warning: class has no instance-initializer: ");
695 utf_sprint_classname(logtext + strlen(logtext), c->name);
701 /* call initializer */
703 asm_calljavafunction(m, o, t, NULL, NULL);
709 /******************** function: stringtable_update ****************************
711 traverses the javastring hashtable and sets the vftbl-entries of
712 javastrings which were temporarily set to NULL, because
713 java.lang.Object was not yet loaded
715 *******************************************************************************/
717 void stringtable_update ()
719 java_lang_String *js;
721 literalstring *s; /* hashtable entry */
724 for (i = 0; i < string_hash.size; i++) {
725 s = string_hash.ptr[i];
729 js = (java_lang_String *) s->string;
731 if (!js || !js->value)
732 /* error in hashtable found */
733 panic("invalid literalstring in hashtable");
737 if (!js->header.vftbl)
738 /* vftbl of javastring is NULL */
739 js->header.vftbl = class_java_lang_String->vftbl;
741 if (!a->header.objheader.vftbl)
742 /* vftbl of character-array is NULL */
743 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
745 /* follow link in external hash chain */
753 /************************* function: u2_utflength ***************************
755 returns the utf length in bytes of a u2 array
757 *****************************************************************************/
759 u4 u2_utflength(u2 *text, u4 u2_length)
761 u4 result_len = 0; /* utf length in bytes */
762 u2 ch; /* current unicode character */
765 for (len = 0; len < u2_length; len++) {
766 /* next unicode character */
769 /* determine bytes required to store unicode character as utf */
770 if (ch && (ch < 0x80))
782 /********************* function: utf_new_u2 ***********************************
784 make utf symbol from u2 array,
785 if isclassname is true '.' is replaced by '/'
787 *******************************************************************************/
789 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
791 char *buffer; /* memory buffer for unicode characters */
792 char *pos; /* pointer to current position in buffer */
793 u4 left; /* unicode characters left */
794 u4 buflength; /* utf length in bytes of the u2 array */
795 utf *result; /* resulting utf-string */
798 /* determine utf length in bytes and allocate memory */
799 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
800 buflength = u2_utflength(unicode_pos, unicode_length);
801 buffer = MNEW(char, buflength);
806 for (i = 0; i++ < unicode_length; unicode_pos++) {
807 /* next unicode character */
810 if ((c != 0) && (c < 0x80)) {
813 if ((int) left < 0) break;
814 /* convert classname */
815 if (isclassname && c == '.')
820 } else if (c < 0x800) {
822 unsigned char high = c >> 6;
823 unsigned char low = c & 0x3F;
825 if ((int) left < 0) break;
826 *pos++ = high | 0xC0;
832 char mid = (c >> 6) & 0x3F;
835 if ((int) left < 0) break;
836 *pos++ = high | 0xE0;
842 /* insert utf-string into symbol-table */
843 result = utf_new(buffer,buflength);
845 MFREE(buffer, char, buflength);
851 /********************* function: javastring_toutf *****************************
853 make utf symbol from javastring
855 *******************************************************************************/
857 utf *javastring_toutf(java_lang_String *string, bool isclassname)
859 java_lang_String *str = (java_lang_String *) string;
861 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
862 /* fflush(stdout); */
864 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
868 /********************* function: literalstring_u2 *****************************
870 searches for the javastring with the specified u2-array in
871 the string hashtable, if there is no such string a new one is
874 if copymode is true a copy of the u2-array is made
876 *******************************************************************************/
878 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
881 literalstring *s; /* hashtable element */
882 java_lang_String *js; /* u2-array wrapped in javastring */
883 java_chararray *stringdata; /* copy of u2-array */
888 /* #define DEBUG_LITERALSTRING_U2 */
889 #ifdef DEBUG_LITERALSTRING_U2
890 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
894 /* find location in hashtable */
895 key = unicode_hashkey(a->data + offset, length);
896 slot = key & (string_hash.size - 1);
897 s = string_hash.ptr[slot];
900 js = (java_lang_String *) s->string;
902 if (length == js->count) {
904 for (i = 0; i < length; i++) {
905 if (a->data[offset + i] != js->value->data[i])
909 /* string already in hashtable, free memory */
911 mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
913 #ifdef DEBUG_LITERALSTRING_U2
914 printf("literalstring_u2: foundentry at %p\n", js);
915 utf_display(javastring_toutf(js, 0));
919 return (java_objectheader *) js;
923 /* follow link in external hash chain */
928 /* create copy of u2-array for new javastring */
929 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
930 stringdata = mem_alloc(arraysize);
931 /* memcpy(stringdata, a, arraysize); */
932 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
933 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
939 /* location in hashtable found, complete arrayheader */
940 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
941 stringdata->header.size = length;
943 /* if we use eager loading, we have to check loaded String class */
945 class_java_lang_String =
946 class_new_intern(utf_new_char("java/lang/String"));
948 if (!class_load(class_java_lang_String))
951 list_addfirst(&unlinkedclasses, class_java_lang_String);
954 /* create new javastring */
955 js = NEW(java_lang_String);
956 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
957 initObjectLock(&js->header);
959 js->header.vftbl = class_java_lang_String->vftbl;
960 js->value = stringdata;
964 #ifdef DEBUG_LITERALSTRING_U2
965 printf("literalstring_u2: newly created at %p\n", js);
966 utf_display(javastring_toutf(js, 0));
971 /* create new literalstring */
972 s = NEW(literalstring);
973 s->hashlink = string_hash.ptr[slot];
974 s->string = (java_objectheader *) js;
975 string_hash.ptr[slot] = s;
977 /* update number of hashtable entries */
978 string_hash.entries++;
980 /* reorganization of hashtable */
981 if (string_hash.entries > (string_hash.size * 2)) {
982 /* reorganization of hashtable, average length of
983 the external chains is approx. 2 */
987 hashtable newhash; /* the new hashtable */
989 /* create new hashtable, double the size */
990 init_hashtable(&newhash, string_hash.size * 2);
991 newhash.entries = string_hash.entries;
993 /* transfer elements to new hashtable */
994 for (i = 0; i < string_hash.size; i++) {
995 s = string_hash.ptr[i];
997 literalstring *nexts = s->hashlink;
998 js = (java_lang_String *) s->string;
999 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
1001 s->hashlink = newhash.ptr[slot];
1002 newhash.ptr[slot] = s;
1004 /* follow link in external hash chain */
1009 /* dispose old table */
1010 MFREE(string_hash.ptr, void*, string_hash.size);
1011 string_hash = newhash;
1014 return (java_objectheader *) js;
1018 /******************** Function: literalstring_new *****************************
1020 creates a new javastring with the text of the utf-symbol
1021 and inserts it into the string hashtable
1023 *******************************************************************************/
1025 java_objectheader *literalstring_new(utf *u)
1027 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1028 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1029 java_chararray *a; /* u2-array constructed from utf string */
1032 /* allocate memory */
1033 a = mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1035 /* convert utf-string to u2-array */
1036 for (i = 0; i < utflength; i++)
1037 a->data[i] = utf_nextu2(&utf_ptr);
1039 return literalstring_u2(a, utflength, 0, false);
1043 /********************** function: literalstring_free **************************
1045 removes a javastring from memory
1047 ******************************************************************************/
1049 void literalstring_free(java_objectheader* sobj)
1051 java_lang_String *s = (java_lang_String *) sobj;
1052 java_chararray *a = s->value;
1054 /* dispose memory of java.lang.String object */
1055 FREE(s, java_lang_String);
1057 /* dispose memory of java-characterarray */
1058 FREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1062 void copy_vftbl(vftbl_t **dest, vftbl_t *src)
1066 /* XXX this kind of copying does not work (in the general
1067 * case). The interface tables would have to be copied, too. I
1068 * don't see why we should make a copy anyway. -Edwin
1070 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1071 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1072 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1077 /******************************************************************************************
1079 creates method signature (excluding return type) from array of
1080 class-objects representing the parameters of the method
1082 *******************************************************************************************/
1085 utf *create_methodsig(java_objectarray* types, char *retType)
1087 char *buffer; /* buffer for building the desciptor */
1088 char *pos; /* current position in buffer */
1089 utf *result; /* the method signature */
1090 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1093 if (!types) return NULL;
1095 /* determine required buffer-size */
1096 for (i = 0; i < types->header.size; i++) {
1097 classinfo *c = (classinfo *) types->data[i];
1098 buffer_size = buffer_size + c->name->blength + 2;
1101 if (retType) buffer_size += strlen(retType);
1103 /* allocate buffer */
1104 buffer = MNEW(char, buffer_size);
1107 /* method-desciptor starts with parenthesis */
1110 for (i = 0; i < types->header.size; i++) {
1113 /* current argument */
1114 classinfo *c = (classinfo *) types->data[i];
1116 /* current position in utf-text */
1117 char *utf_ptr = c->name->text;
1119 /* determine type of argument */
1120 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1122 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1123 *pos++ = *utf_ptr; /* copy text */
1127 /* check for primitive types */
1128 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1129 char *utf_pos = utf_ptr - 1;
1130 char *primitive = primitivetype_table[j].wrapname;
1133 while (utf_pos < utf_end(c->name)) {
1134 if (*utf_pos++ != *primitive++) goto nomatch;
1137 /* primitive type found */
1138 *pos++ = primitivetype_table[j].typesig;
1145 /* no primitive type and no arrayclass, so must be object */
1149 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1163 for (i = 0; i < strlen(retType); i++) {
1164 *pos++ = retType[i];
1168 /* create utf-string */
1169 result = utf_new(buffer, (pos - buffer));
1170 MFREE(buffer, char, buffer_size);
1176 /******************************************************************************************
1178 retrieve the next argument or returntype from a descriptor
1179 and return the corresponding class
1181 *******************************************************************************************/
1183 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1185 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1186 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1189 panic("illegal descriptor");
1191 if (skip) return NULL;
1193 use_class_as_object(c);
1198 /* get_parametertypes **********************************************************
1200 use the descriptor of a method to generate a java/lang/Class array
1201 which contains the classes of the parametertypes of the method
1203 *******************************************************************************/
1205 java_objectarray* get_parametertypes(methodinfo *m)
1207 utf *descr = m->descriptor; /* method-descriptor */
1208 char *utf_ptr = descr->text; /* current position in utf-text */
1209 char *desc_end = utf_end(descr); /* points behind utf string */
1210 java_objectarray* result;
1211 int parametercount = 0;
1215 utf_nextu2(&utf_ptr);
1217 /* determine number of parameters */
1218 while (*utf_ptr != ')') {
1219 get_type(&utf_ptr, desc_end, true);
1223 /* create class-array */
1224 result = builtin_anewarray(parametercount, class_java_lang_Class);
1226 utf_ptr = descr->text;
1227 utf_nextu2(&utf_ptr);
1229 /* get returntype classes */
1230 for (i = 0; i < parametercount; i++)
1232 (java_objectheader *) get_type(&utf_ptr, desc_end, false);
1238 /* get_exceptiontypes **********************************************************
1240 get the exceptions which can be thrown by a method
1242 *******************************************************************************/
1244 java_objectarray* get_exceptiontypes(methodinfo *m)
1248 java_objectarray *result;
1250 excount = m->thrownexceptionscount;
1252 /* create class-array */
1253 result = builtin_anewarray(excount, class_java_lang_Class);
1255 for (i = 0; i < excount; i++) {
1256 java_objectheader *o = (java_objectheader *) (m->thrownexceptions[i]);
1257 use_class_as_object((classinfo *) o);
1258 result->data[i] = o;
1268 /******************************************************************************************
1270 get the returntype class of a method
1272 *******************************************************************************************/
1274 classinfo *get_returntype(methodinfo *m)
1276 char *utf_ptr; /* current position in utf-text */
1277 char *desc_end; /* points behind utf string */
1278 utf *desc = m->descriptor; /* method-descriptor */
1280 utf_ptr = desc->text;
1281 desc_end = utf_end(desc);
1283 /* ignore parametertypes */
1284 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1287 return get_type(&utf_ptr,desc_end, false);
1291 /*****************************************************************************/
1292 /*****************************************************************************/
1295 /*--------------------------------------------------------*/
1296 void printNativeCall(nativeCall nc) {
1299 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1300 for (i=0; i<nc.methCnt; i++) {
1301 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1303 for (j=0; j<nc.callCnt[i]; j++) {
1304 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1305 nc.methods[i].methodCalls[j].classname,
1306 nc.methods[i].methodCalls[j].methodname,
1307 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1310 printf("-+++++--------------------\n");fflush(stdout);
1313 /*--------------------------------------------------------*/
1314 void printCompNativeCall(nativeCompCall nc) {
1316 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1317 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1318 utf_display(nc.classname); fflush(stdout);
1320 for (i=0; i<nc.methCnt; i++) {
1321 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1322 utf_display(nc.methods[i].methodname); fflush(stdout);
1323 utf_display(nc.methods[i].descriptor);fflush(stdout);
1324 printf("\n");fflush(stdout);
1326 for (j=0; j<nc.callCnt[i]; j++) {
1327 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1328 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1329 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1330 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1331 printf("\n");fflush(stdout);
1334 printf("---------------------\n");fflush(stdout);
1338 /*--------------------------------------------------------*/
1339 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1352 /*--------------------------------------------------------*/
1353 nativeCall* findNativeClassCalls(char *aclassname ) {
1356 for (i=0;i<NATIVECALLSSIZE; i++) {
1357 /* convert table to utf later to speed up search */
1358 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1359 return &nativeCalls[i];
1364 /*--------------------------------------------------------*/
1365 /*--------------------------------------------------------*/
1366 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1370 ncc->classname = utf_new_char(nc.classname);
1371 ncc->methCnt = nc.methCnt;
1373 for (i=0; i<nc.methCnt; i++) {
1374 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1375 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1376 ncc->callCnt[i] = nc.callCnt[i];
1378 for (j=0; j<nc.callCnt[i]; j++) {
1380 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1382 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1383 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1384 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1387 ncc->methods[i].methodCalls[j].methodname = NULL;
1388 ncc->methods[i].methodCalls[j].descriptor = NULL;
1396 /*--------------------------------------------------------*/
1398 bool natcall2utf(bool natcallcompdone) {
1401 if (natcallcompdone)
1404 for (i=0;i<NATIVECALLSSIZE; i++) {
1405 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1411 /*--------------------------------------------------------*/
1414 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end, classinfo **start)
1416 #if defined(__GNUC__)
1417 #warning platform dependend
1419 java_objectarray *tmpArray;
1421 classinfo **current;
1425 size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
1427 /*printf("end %p, start %p, size %ld\n",end,start,size);*/
1428 if (!class_java_lang_Class)
1429 class_java_lang_Class = class_new(utf_new_char("java/lang/Class"));
1431 if (!class_java_lang_SecurityManager)
1432 class_java_lang_SecurityManager =
1433 class_new(utf_new_char("java/lang/SecurityManager"));
1436 if (start == class_java_lang_SecurityManager) {
1443 builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1445 for(i = 0, current = start; i < size; i++, current--) {
1447 /* printf("%d\n",i);
1448 utf_display(c->name);*/
1449 use_class_as_object(c);
1450 tmpArray->data[i] = (java_objectheader *) c;
1457 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end, classinfo **start)
1459 #if defined(__GNUC__)
1460 #warning platform dependend
1463 classinfo **current;
1465 classinfo *privilegedAction;
1468 size = (((size_t) start) - ((size_t) end)) / sizeof(classinfo*);
1470 /* log_text("builtin_asm_getclassloader");
1471 printf("end %p, start %p, size %ld\n",end,start,size);*/
1473 if (!class_java_lang_SecurityManager)
1474 class_java_lang_SecurityManager =
1475 class_new(utf_new_char("java/lang/SecurityManager"));
1478 if (start == class_java_lang_SecurityManager) {
1484 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1486 for(i = 0, current = start; i < size; i++, current--) {
1489 if (c == privilegedAction)
1493 return (java_lang_ClassLoader *) c->classloader;
1499 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1500 init_systemclassloader();
1502 return SystemClassLoader;*/
1507 * These are local overrides for various environment variables in Emacs.
1508 * Please do not remove this and leave it at the end of the file, where
1509 * Emacs will automagically detect them.
1510 * ---------------------------------------------------------------------
1513 * indent-tabs-mode: t