1 /* 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 1190 2004-06-19 12:44:12Z twisti $
53 #include "nativetypes.hh"
60 #include "toolbox/logging.h"
61 #include "toolbox/memory.h"
62 #include "threads/thread.h"
63 #include "threads/threadio.h"
64 #include "threads/locks.h"
65 #include "nat/java_lang_VMClass.h"
66 #include "nat/java_lang_Throwable.h"
68 /* Include files for IO functions */
72 #include <sys/types.h>
78 #include "threads/threadio.h"
80 /* searchpath for classfiles */
83 /* for java-string to char conversion */
84 #define MAXSTRINGSIZE 1000
87 /******************** systemclasses required for native methods ***************/
89 classinfo *class_java_lang_Class;
90 classinfo *class_java_lang_VMClass;
91 classinfo *class_java_lang_System;
92 classinfo *class_java_lang_ClassLoader;
93 classinfo *class_gnu_java_lang_SystemClassLoader;
94 classinfo *class_java_lang_SecurityManager;
95 classinfo *class_java_lang_Double;
96 classinfo *class_java_lang_Float;
97 classinfo *class_java_lang_Long;
98 classinfo *class_java_lang_Byte;
99 classinfo *class_java_lang_Short;
100 classinfo *class_java_lang_Boolean;
101 classinfo *class_java_lang_Void;
102 classinfo *class_java_lang_Character;
103 classinfo *class_java_lang_Integer;
105 methodinfo *method_vmclass_init;
108 /* system exception classes required while compiling */
110 classinfo *class_java_lang_Throwable;
113 /* exception/error super class */
115 char *string_java_lang_Throwable =
116 "java/lang/Throwable";
118 char *string_java_lang_VMThrowable =
119 "java/lang/VMThrowable";
122 /* specify some exception strings for code generation */
124 char *string_java_lang_ArithmeticException =
125 "java/lang/ArithmeticException";
127 char *string_java_lang_ArithmeticException_message =
130 char *string_java_lang_ArrayIndexOutOfBoundsException =
131 "java/lang/ArrayIndexOutOfBoundsException";
133 char *string_java_lang_ArrayStoreException =
134 "java/lang/ArrayStoreException";
136 char *string_java_lang_ClassCastException =
137 "java/lang/ClassCastException";
139 char *string_java_lang_ClassNotFoundException =
140 "java/lang/ClassNotFoundException";
142 char *string_java_lang_CloneNotSupportedException =
143 "java/lang/CloneNotSupportedException";
145 char *string_java_lang_Exception =
146 "java/lang/Exception";
148 char *string_java_lang_IllegalArgumentException =
149 "java/lang/IllegalArgumentException";
151 char *string_java_lang_IllegalMonitorStateException =
152 "java/lang/IllegalMonitorStateException";
154 char *string_java_lang_NegativeArraySizeException =
155 "java/lang/NegativeArraySizeException";
157 char *string_java_lang_NoSuchFieldException =
158 "java/lang/NoSuchFieldException";
160 char *string_java_lang_NoSuchMethodException =
161 "java/lang/NoSuchMethodException";
163 char *string_java_lang_NullPointerException =
164 "java/lang/NullPointerException";
167 /* specify some error strings for code generation */
169 char *string_java_lang_AbstractMethodError =
170 "java/lang/AbstractMethodError";
172 char *string_java_lang_ClassCircularityError =
173 "java/lang/ClassCircularityError";
175 char *string_java_lang_ClassFormatError =
176 "java/lang/ClassFormatError";
178 char *string_java_lang_Error =
181 char *string_java_lang_ExceptionInInitializerError =
182 "java/lang/ExceptionInInitializerError";
184 char *string_java_lang_IncompatibleClassChangeError =
185 "java/lang/IncompatibleClassChangeError";
187 char *string_java_lang_InternalError =
188 "java/lang/InternalError";
190 char *string_java_lang_LinkageError =
191 "java/lang/LinkageError";
193 char *string_java_lang_NoClassDefFoundError =
194 "java/lang/NoClassDefFoundError";
196 char *string_java_lang_NoSuchFieldError =
197 "java/lang/NoSuchFieldError";
199 char *string_java_lang_NoSuchMethodError =
200 "java/lang/NoSuchMethodError";
202 char *string_java_lang_OutOfMemoryError =
203 "java/lang/OutOfMemoryError";
205 char *string_java_lang_VerifyError =
206 "java/lang/VerifyError";
208 char *string_java_lang_VirtualMachineError =
209 "java/lang/VirtualMachineError";
212 /* the system classloader object */
213 struct java_lang_ClassLoader *SystemClassLoader = NULL;
215 /* for raising exceptions from native methods */
216 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
217 java_objectheader* _exceptionptr = NULL;
220 /************* use classinfo structure as java.lang.Class object **************/
222 void use_class_as_object(classinfo *c)
224 if (!c->classvftbl) {
225 c->classvftbl = true;
227 c->header.vftbl = class_java_lang_Class->vftbl;
233 /*************************** include native methods ***************************/
236 #include "nat/GdkGraphics.c"
237 #include "nat/GtkComponentPeer.c"
238 #include "nat/GdkPixbufDecoder.c"
239 #include "nat/GtkScrollPanePeer.c"
240 #include "nat/GtkFileDialogPeer.c"
241 #include "nat/GtkLabelPeer.c"
245 /************************** tables for methods ********************************/
250 /* table for locating native methods */
251 static struct nativeref {
259 #include "nativetable.hh"
264 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
266 /* table for fast string comparison */
267 static struct nativecompref {
273 } nativecomptable [NATIVETABLESIZE];
275 /* string comparsion table initialized */
276 static bool nativecompdone = false;
279 /******************************************************************************/
280 /******************************************************************************/
281 #include "natcalls.h"
283 /* string call comparison table initialized */
285 /******************************************************************************/
286 /******************************************************************************/
288 /* init_system_exceptions *****************************************************
290 load, link and compile exceptions used in the system
292 *******************************************************************************/
294 void init_system_exceptions()
298 /* java/lang/Throwable */
300 class_java_lang_Throwable =
301 class_new(utf_new_char(string_java_lang_Throwable));
302 class_load(class_java_lang_Throwable);
303 class_link(class_java_lang_Throwable);
305 compile_all_class_methods(class_java_lang_Throwable);
307 /* java/lang/VMThrowable */
309 c = class_new(utf_new_char(string_java_lang_VMThrowable));
312 compile_all_class_methods(c);
314 /* java/lang/ClassFormatError */
316 c = class_new(utf_new_char(string_java_lang_ClassFormatError));
319 compile_all_class_methods(c);
321 /* java/lang/Error */
323 c = class_new(utf_new_char(string_java_lang_Error));
326 compile_all_class_methods(c);
328 /* java/lang/Exception */
330 c = class_new(utf_new_char(string_java_lang_Exception));
333 compile_all_class_methods(c);
335 /* java/lang/IncompatibleClassChangeError */
337 c = class_new(utf_new_char(string_java_lang_IncompatibleClassChangeError));
340 compile_all_class_methods(c);
342 /* java/lang/LinkageError */
344 c = class_new(utf_new_char(string_java_lang_LinkageError));
347 compile_all_class_methods(c);
349 /* java/lang/NoClassDefFoundError */
351 c = class_new(utf_new_char(string_java_lang_NoClassDefFoundError));
354 compile_all_class_methods(c);
356 /* java/lang/NoSuchFieldError */
358 c = class_new(utf_new_char(string_java_lang_NoSuchFieldError));
361 compile_all_class_methods(c);
363 /* java/lang/NoSuchMethodError */
365 c = class_new(utf_new_char(string_java_lang_NoSuchMethodError));
368 compile_all_class_methods(c);
370 /* java/lang/OutOfMemoryError */
372 c = class_new(utf_new_char(string_java_lang_OutOfMemoryError));
375 compile_all_class_methods(c);
377 /* java/lang/VerifyError */
379 c = class_new(utf_new_char(string_java_lang_VerifyError));
382 compile_all_class_methods(c);
384 /* java/lang/VirtualMachineError */
386 c = class_new(utf_new_char(string_java_lang_VirtualMachineError));
389 compile_all_class_methods(c);
394 void compile_all_class_methods(classinfo *c)
398 for (i = 0; i < c->methodscount; i++) {
399 (void) jit_compile(&(c->methods[i]));
404 static void throw_exception_exit_intern(bool doexit)
406 java_objectheader *xptr;
411 xptr = *exceptionptr;
413 /* clear exception, because we are calling jit code again */
414 *exceptionptr = NULL;
416 c = xptr->vftbl->class;
418 pss = class_resolveclassmethod(c,
419 utf_new_char("printStackTrace"),
421 class_java_lang_Object,
424 /* print the stacktrace */
426 asm_calljavafunction(pss, xptr, NULL, NULL, NULL);
428 /* this normally means, we are EXTREMLY out of memory, but may be
429 any other exception */
431 utf_fprint_classname(stderr, c->name);
432 fprintf(stderr, "\n");
436 utf_fprint_classname(stderr, c->name);
437 fprintf(stderr, ": printStackTrace()V not found!\n");
450 void throw_exception()
452 throw_exception_exit_intern(false);
456 void throw_exception_exit()
458 throw_exception_exit_intern(true);
462 void throw_main_exception()
464 fprintf(stderr, "Exception in thread \"main\" ");
467 throw_exception_exit_intern(false);
471 void throw_main_exception_exit()
473 fprintf(stderr, "Exception in thread \"main\" ");
476 throw_exception_exit_intern(true);
480 void throw_cacao_exception_exit(char *exception, char *message)
486 len = strlen(exception);
487 tmp = MNEW(char, len);
488 strncpy(tmp, exception, len);
490 /* convert to classname */
492 for (i = len - 1; i >= 0; i--) {
493 if (tmp[i] == '/') tmp[i] = '.';
496 fprintf(stderr, "Exception in thread \"main\" %s", tmp);
498 MFREE(tmp, char, len);
500 if (strlen(message) > 0)
501 fprintf(stderr, ": %s", message);
503 fprintf(stderr, "\n");
511 #define CREATENEW_EXCEPTION(ex) \
512 java_objectheader *newEx; \
513 java_objectheader *oldexception=*exceptionptr;\
516 *exceptionptr=oldexception;\
519 java_objectheader *new_exception(char *classname)
521 classinfo *c = class_new(utf_new_char(classname));
523 CREATENEW_EXCEPTION(native_new_and_init(c));
526 java_objectheader *new_exception_message(char *classname, char *message)
528 classinfo *c = class_new(utf_new_char(classname));
530 CREATENEW_EXCEPTION(native_new_and_init_string(c, javastring_new_char(message)));
534 java_objectheader *new_exception_throwable(char *classname, java_lang_Throwable *throwable)
536 classinfo *c = class_new(utf_new_char(classname));
538 CREATENEW_EXCEPTION(native_new_and_init_throwable(c, throwable));
542 java_objectheader *new_exception_utfmessage(char *classname, utf *message)
544 classinfo *c = class_new(utf_new_char(classname));
546 CREATENEW_EXCEPTION(native_new_and_init_string(c, javastring_new(message)));
550 java_objectheader *new_exception_javastring(char *classname, java_lang_String *message)
552 classinfo *c = class_new(utf_new_char(classname));
554 CREATENEW_EXCEPTION(native_new_and_init_string(c, message));
558 java_objectheader *new_exception_int(char *classname, s4 i)
560 classinfo *c = class_new(utf_new_char(classname));
562 CREATENEW_EXCEPTION(native_new_and_init_int(c, i));
566 /*********************** function: native_loadclasses **************************
568 load classes required for native methods
570 *******************************************************************************/
572 void native_loadclasses()
574 static int classesLoaded = 0; /*temporary hack JoWenn*/
581 class_java_lang_Cloneable =
582 class_new(utf_new_char("java/lang/Cloneable"));
583 class_load(class_java_lang_Cloneable);
584 class_link(class_java_lang_Cloneable);
586 class_java_lang_Class =
587 class_new(utf_new_char("java/lang/Class"));
588 class_load(class_java_lang_Class);
589 class_link(class_java_lang_Class);
591 class_java_lang_VMClass =
592 class_new(utf_new_char("java/lang/VMClass"));
593 class_load(class_java_lang_VMClass);
594 class_link(class_java_lang_VMClass);
596 class_java_lang_ClassLoader =
597 class_new(utf_new_char("java/lang/ClassLoader"));
598 class_load(class_java_lang_ClassLoader);
599 class_link(class_java_lang_ClassLoader);
601 /* load classes for wrapping primitive types */
602 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
603 class_load(class_java_lang_Double);
604 class_link(class_java_lang_Double);
606 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
607 class_load(class_java_lang_Float);
608 class_link(class_java_lang_Float);
610 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
611 class_load(class_java_lang_Character);
612 class_link(class_java_lang_Character);
614 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
615 class_load(class_java_lang_Integer);
616 class_link(class_java_lang_Integer);
618 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
619 class_load(class_java_lang_Long);
620 class_link(class_java_lang_Long);
622 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
623 class_load(class_java_lang_Byte);
624 class_link(class_java_lang_Byte);
626 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
627 class_load(class_java_lang_Short);
628 class_link(class_java_lang_Short);
630 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
631 class_load(class_java_lang_Boolean);
632 class_link(class_java_lang_Boolean);
634 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
635 class_load(class_java_lang_Void);
636 class_link(class_java_lang_Void);
641 /*****************************************************************************
643 create systemclassloader object and initialize instance fields
645 ******************************************************************************/
647 void init_systemclassloader()
649 log_text("init_systemclassloader");
650 if (!SystemClassLoader) {
651 native_loadclasses();
652 log_text("Initializing new system class loader");
653 /* create object and call initializer */
654 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
656 /* systemclassloader has no parent */
657 SystemClassLoader->parent = NULL;
658 SystemClassLoader->initialized = true;
660 log_text("leaving system class loader");
664 /********************* function: native_setclasspath **************************/
666 void native_setclasspath(char *path)
668 /* set searchpath for classfiles */
673 /*********************** Function: native_findfunction *************************
675 Looks up a method (must have the same class name, method name, descriptor
676 and 'static'ness) and returns a function pointer to it.
677 Returns: function pointer or NULL (if there is no such method)
679 Remark: For faster operation, the names/descriptors are converted from C
680 strings to Unicode the first time this function is called.
682 *******************************************************************************/
684 functionptr native_findfunction(utf *cname, utf *mname,
685 utf *desc, bool isstatic)
688 /* entry of table for fast string comparison */
689 struct nativecompref *n;
690 /* for warning message if no function is found */
694 isstatic = isstatic ? true : false;
696 if (!nativecompdone) {
697 for (i = 0; i < NATIVETABLESIZE; i++) {
698 nativecomptable[i].classname =
699 utf_new_char(nativetable[i].classname);
700 nativecomptable[i].methodname =
701 utf_new_char(nativetable[i].methodname);
702 nativecomptable[i].descriptor =
703 utf_new_char(nativetable[i].descriptor);
704 nativecomptable[i].isstatic =
705 nativetable[i].isstatic;
706 nativecomptable[i].func =
709 nativecompdone = true;
714 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
716 buffer = MNEW(char, buffer_len);
718 strcpy(buffer, "searching matching function in native table:");
719 utf_sprint(buffer+strlen(buffer), mname);
720 strcpy(buffer+strlen(buffer), ": ");
721 utf_sprint(buffer+strlen(buffer), desc);
722 strcpy(buffer+strlen(buffer), " for class ");
723 utf_sprint(buffer+strlen(buffer), cname);
727 MFREE(buffer, char, buffer_len);
730 for (i = 0; i < NATIVETABLESIZE; i++) {
731 n = &(nativecomptable[i]);
733 if (cname == n->classname && mname == n->methodname &&
734 desc == n->descriptor && isstatic == n->isstatic)
738 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
742 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
744 buffer = MNEW(char, buffer_len);
746 strcpy(buffer, "comparing with:");
747 utf_sprint(buffer+strlen(buffer), n->methodname);
748 strcpy (buffer+strlen(buffer), ": ");
749 utf_sprint(buffer+strlen(buffer), n->descriptor);
750 strcpy(buffer+strlen(buffer), " for class ");
751 utf_sprint(buffer+strlen(buffer), n->classname);
755 MFREE(buffer, char, buffer_len);
762 /* no function was found, display warning */
765 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
767 buffer = MNEW(char, buffer_len);
769 strcpy(buffer, "warning: native function ");
770 utf_sprint(buffer + strlen(buffer), mname);
771 strcpy(buffer + strlen(buffer), ": ");
772 utf_sprint(buffer + strlen(buffer), desc);
773 strcpy(buffer + strlen(buffer), " not found in class ");
774 utf_sprint(buffer + strlen(buffer), cname);
778 MFREE(buffer, char, buffer_len);
782 /* keep compiler happy */
787 /********************** function: javastring_new *******************************
789 creates a new object of type java/lang/String with the text of
790 the specified utf8-string
792 return: pointer to the string or NULL if memory is exhausted.
794 *******************************************************************************/
796 java_lang_String *javastring_new(utf *u)
798 char *utf_ptr = u->text; /* current utf character in utf string */
799 int utflength = utf_strlen(u); /* length of utf-string if uncompressed */
800 java_lang_String *s; /* result-string */
804 s = (java_lang_String *) builtin_new(class_java_lang_String);
805 a = builtin_newarray_char(utflength);
807 /* javastring or character-array could not be created */
811 /* decompress utf-string */
812 for (i = 0; i < utflength; i++)
813 a->data[i] = utf_nextu2(&utf_ptr);
815 /* set fields of the javastring-object */
818 s->count = utflength;
824 /********************** function: javastring_new_char **************************
826 creates a new java/lang/String object which contains the convertet
827 C-string passed via text.
829 return: the object pointer or NULL if memory is exhausted.
831 *******************************************************************************/
833 java_lang_String *javastring_new_char(char *text)
836 s4 len = strlen(text); /* length of the string */
837 java_lang_String *s; /* result-string */
840 s = (java_lang_String *) builtin_new(class_java_lang_String);
841 a = builtin_newarray_char(len);
843 /* javastring or character-array could not be created */
848 for (i = 0; i < len; i++)
849 a->data[i] = text[i];
851 /* set fields of the javastring-object */
860 /************************* function javastring_tochar **************************
862 converts a Java string into a C string.
864 return: pointer to C string
866 Caution: every call of this function overwrites the previous string !!!
868 *******************************************************************************/
870 static char stringbuffer[MAXSTRINGSIZE];
872 char *javastring_tochar(java_objectheader *so)
874 java_lang_String *s = (java_lang_String *) so;
886 if (s->count > MAXSTRINGSIZE)
889 for (i = 0; i < s->count; i++)
890 stringbuffer[i] = a->data[s->offset + i];
892 stringbuffer[i] = '\0';
898 /****************** function class_findfield_approx ****************************
900 searches in 'classinfo'-structure for a field with the
903 *******************************************************************************/
905 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
909 for (i = 0; i < c->fieldscount; i++) {
910 /* compare field names */
911 if ((c->fields[i].name == name))
912 return &(c->fields[i]);
915 /* field was not found, raise exception */
916 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
922 s4 class_findfield_index_approx(classinfo *c, utf *name)
926 for (i = 0; i < c->fieldscount; i++) {
927 /* compare field names */
928 if ((c->fields[i].name == name))
932 /* field was not found, raise exception */
933 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
939 /********************** function: native_new_and_init *************************
941 Creates a new object on the heap and calls the initializer.
942 Returns the object pointer or NULL if memory is exhausted.
944 *******************************************************************************/
946 java_objectheader *native_new_and_init(classinfo *c)
949 java_objectheader *o;
952 return *exceptionptr;
954 o = builtin_new(c); /* create object */
959 /* find initializer */
961 m = class_findmethod(c,
962 utf_new_char("<init>"),
963 utf_new_char("()V"));
965 if (!m) { /* initializer not found */
967 char logtext[MAXLOGTEXT];
968 sprintf(logtext, "Warning: class has no instance-initializer: ");
969 utf_sprint_classname(logtext + strlen(logtext), c->name);
975 /* call initializer */
977 asm_calljavafunction(m, o, NULL, NULL, NULL);
983 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
986 java_objectheader *o;
989 return *exceptionptr;
991 o = builtin_new(c); /* create object */
996 /* find initializer */
998 m = class_findmethod(c,
999 utf_new_char("<init>"),
1000 utf_new_char("(Ljava/lang/String;)V"));
1002 if (!m) { /* initializer not found */
1004 char logtext[MAXLOGTEXT];
1005 sprintf(logtext, "Warning: class has no instance-initializer: ");
1006 utf_sprint_classname(logtext + strlen(logtext), c->name);
1012 /* call initializer */
1014 asm_calljavafunction(m, o, s, NULL, NULL);
1020 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1023 java_objectheader *o;
1026 return *exceptionptr;
1028 o = builtin_new(c); /* create object */
1030 if (!o) return NULL;
1032 /* find initializer */
1034 m = class_findmethod(c,
1035 utf_new_char("<init>"),
1036 utf_new_char("(I)V"));
1038 if (!m) { /* initializer not found */
1040 char logtext[MAXLOGTEXT];
1041 sprintf(logtext, "Warning: class has no instance-initializer: ");
1042 utf_sprint_classname(logtext + strlen(logtext), c->name);
1048 /* call initializer */
1050 #if defined(__I386__) || defined(__POWERPC__)
1051 asm_calljavafunction(m, o, (void *) i, NULL, NULL);
1053 asm_calljavafunction(m, o, (void *) (s8) i, NULL, NULL);
1060 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
1063 java_objectheader *o;
1066 return *exceptionptr;
1068 o = builtin_new(c); /* create object */
1070 if (!o) return NULL;
1072 /* find initializer */
1074 m = class_findmethod(c,
1075 utf_new_char("<init>"),
1076 utf_new_char("(Ljava/lang/Throwable;)V"));
1078 if (!m) { /* initializer not found */
1080 char logtext[MAXLOGTEXT];
1081 sprintf(logtext, "Warning: class has no instance-initializer: ");
1082 utf_sprint_classname(logtext + strlen(logtext), c->name);
1088 /* call initializer */
1090 asm_calljavafunction(m, o, t, NULL, NULL);
1096 /******************** function: stringtable_update ****************************
1098 traverses the javastring hashtable and sets the vftbl-entries of
1099 javastrings which were temporarily set to NULL, because
1100 java.lang.Object was not yet loaded
1102 *******************************************************************************/
1104 void stringtable_update ()
1106 java_lang_String *js;
1108 literalstring *s; /* hashtable entry */
1111 for (i = 0; i < string_hash.size; i++) {
1112 s = string_hash.ptr[i];
1116 js = (java_lang_String *) s->string;
1118 if (!js || !js->value)
1119 /* error in hashtable found */
1120 panic("invalid literalstring in hashtable");
1124 if (!js->header.vftbl)
1125 /* vftbl of javastring is NULL */
1126 js->header.vftbl = class_java_lang_String->vftbl;
1128 if (!a->header.objheader.vftbl)
1129 /* vftbl of character-array is NULL */
1130 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1132 /* follow link in external hash chain */
1140 /************************* function: u2_utflength ***************************
1142 returns the utf length in bytes of a u2 array
1144 *****************************************************************************/
1146 u4 u2_utflength(u2 *text, u4 u2_length)
1148 u4 result_len = 0; /* utf length in bytes */
1149 u2 ch; /* current unicode character */
1152 for (len = 0; len < u2_length; len++) {
1153 /* next unicode character */
1156 /* determine bytes required to store unicode character as utf */
1157 if (ch && (ch < 0x80))
1159 else if (ch < 0x800)
1169 /********************* function: utf_new_u2 ***********************************
1171 make utf symbol from u2 array,
1172 if isclassname is true '.' is replaced by '/'
1174 *******************************************************************************/
1176 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
1178 char *buffer; /* memory buffer for unicode characters */
1179 char *pos; /* pointer to current position in buffer */
1180 u4 left; /* unicode characters left */
1181 u4 buflength; /* utf length in bytes of the u2 array */
1182 utf *result; /* resulting utf-string */
1185 /* determine utf length in bytes and allocate memory */
1186 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
1187 buflength = u2_utflength(unicode_pos, unicode_length);
1188 buffer = MNEW(char, buflength);
1193 for (i = 0; i++ < unicode_length; unicode_pos++) {
1194 /* next unicode character */
1195 u2 c = *unicode_pos;
1197 if ((c != 0) && (c < 0x80)) {
1200 if ((int) left < 0) break;
1201 /* convert classname */
1202 if (isclassname && c == '.')
1207 } else if (c < 0x800) {
1209 unsigned char high = c >> 6;
1210 unsigned char low = c & 0x3F;
1212 if ((int) left < 0) break;
1213 *pos++ = high | 0xC0;
1214 *pos++ = low | 0x80;
1218 char low = c & 0x3f;
1219 char mid = (c >> 6) & 0x3F;
1220 char high = c >> 12;
1222 if ((int) left < 0) break;
1223 *pos++ = high | 0xE0;
1224 *pos++ = mid | 0x80;
1225 *pos++ = low | 0x80;
1229 /* insert utf-string into symbol-table */
1230 result = utf_new(buffer,buflength);
1232 MFREE(buffer, char, buflength);
1238 /********************* function: javastring_toutf *****************************
1240 make utf symbol from javastring
1242 *******************************************************************************/
1244 utf *javastring_toutf(java_lang_String *string, bool isclassname)
1246 java_lang_String *str = (java_lang_String *) string;
1248 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
1249 /* fflush(stdout); */
1251 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
1255 /********************* function: literalstring_u2 *****************************
1257 searches for the javastring with the specified u2-array in
1258 the string hashtable, if there is no such string a new one is
1261 if copymode is true a copy of the u2-array is made
1263 *******************************************************************************/
1265 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
1268 literalstring *s; /* hashtable element */
1269 java_lang_String *js; /* u2-array wrapped in javastring */
1270 java_chararray *stringdata; /* copy of u2-array */
1276 //#define DEBUG_LITERALSTRING_U2
1277 #ifdef DEBUG_LITERALSTRING_U2
1278 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
1282 /* find location in hashtable */
1283 key = unicode_hashkey(a->data + offset, length);
1284 slot = key & (string_hash.size - 1);
1285 s = string_hash.ptr[slot];
1288 js = (java_lang_String *) s->string;
1290 if (length == js->count) {
1292 for (i = 0; i < length; i++) {
1293 if (a->data[offset + i] != js->value->data[i])
1297 /* string already in hashtable, free memory */
1299 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
1301 #ifdef DEBUG_LITERALSTRING_U2
1302 printf("literalstring_u2: foundentry at %p\n", js);
1303 utf_display(javastring_toutf(js, 0));
1307 return (java_objectheader *) js;
1311 /* follow link in external hash chain */
1316 /* create copy of u2-array for new javastring */
1317 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
1318 stringdata = lit_mem_alloc(arraysize);
1319 /* memcpy(stringdata, a, arraysize); */
1320 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
1321 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
1327 /* location in hashtable found, complete arrayheader */
1328 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1329 stringdata->header.size = length;
1331 /* if we use eager loading, we have to check loaded String class */
1333 class_java_lang_String =
1334 class_new_intern(utf_new_char("java/lang/String"));
1336 if (!class_load(class_java_lang_String))
1339 list_addfirst(&unlinkedclasses, class_java_lang_String);
1342 /* create new javastring */
1343 js = LNEW(java_lang_String);
1344 js->header.vftbl = class_java_lang_String->vftbl;
1345 js->value = stringdata;
1349 #ifdef DEBUG_LITERALSTRING_U2
1350 printf("literalstring_u2: newly created at %p\n", js);
1351 utf_display(javastring_toutf(js, 0));
1356 /* create new literalstring */
1357 s = NEW(literalstring);
1358 s->hashlink = string_hash.ptr[slot];
1359 s->string = (java_objectheader *) js;
1360 string_hash.ptr[slot] = s;
1362 /* update number of hashtable entries */
1363 string_hash.entries++;
1365 /* reorganization of hashtable */
1366 if (string_hash.entries > (string_hash.size * 2)) {
1367 /* reorganization of hashtable, average length of
1368 the external chains is approx. 2 */
1372 hashtable newhash; /* the new hashtable */
1374 /* create new hashtable, double the size */
1375 init_hashtable(&newhash, string_hash.size * 2);
1376 newhash.entries = string_hash.entries;
1378 /* transfer elements to new hashtable */
1379 for (i = 0; i < string_hash.size; i++) {
1380 s = string_hash.ptr[i];
1382 literalstring *nexts = s->hashlink;
1383 js = (java_lang_String *) s->string;
1384 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
1386 s->hashlink = newhash.ptr[slot];
1387 newhash.ptr[slot] = s;
1389 /* follow link in external hash chain */
1394 /* dispose old table */
1395 MFREE(string_hash.ptr, void*, string_hash.size);
1396 string_hash = newhash;
1399 return (java_objectheader *) js;
1403 /******************** Function: literalstring_new *****************************
1405 creates a new javastring with the text of the utf-symbol
1406 and inserts it into the string hashtable
1408 *******************************************************************************/
1410 java_objectheader *literalstring_new(utf *u)
1412 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1413 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1414 java_chararray *a; /* u2-array constructed from utf string */
1417 /* allocate memory */
1418 a = lit_mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1420 /* convert utf-string to u2-array */
1421 for (i = 0; i < utflength; i++)
1422 a->data[i] = utf_nextu2(&utf_ptr);
1424 return literalstring_u2(a, utflength, 0, false);
1428 /********************** function: literalstring_free **************************
1430 removes a javastring from memory
1432 ******************************************************************************/
1434 void literalstring_free(java_objectheader* sobj)
1436 java_lang_String *s = (java_lang_String *) sobj;
1437 java_chararray *a = s->value;
1439 /* dispose memory of java.lang.String object */
1440 LFREE(s, java_lang_String);
1442 /* dispose memory of java-characterarray */
1443 LFREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1447 void copy_vftbl(vftbl **dest, vftbl *src)
1451 /* XXX this kind of copying does not work (in the general
1452 * case). The interface tables would have to be copied, too. I
1453 * don't see why we should make a copy anyway. -Edwin
1455 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1456 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1457 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1462 /******************************************************************************************
1464 creates method signature (excluding return type) from array of
1465 class-objects representing the parameters of the method
1467 *******************************************************************************************/
1470 utf *create_methodsig(java_objectarray* types, char *retType)
1472 char *buffer; /* buffer for building the desciptor */
1473 char *pos; /* current position in buffer */
1474 utf *result; /* the method signature */
1475 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1478 if (!types) return NULL;
1480 /* determine required buffer-size */
1481 for (i = 0; i < types->header.size; i++) {
1482 classinfo *c = (classinfo *) types->data[i];
1483 buffer_size = buffer_size + c->name->blength + 2;
1486 if (retType) buffer_size += strlen(retType);
1488 /* allocate buffer */
1489 buffer = MNEW(u1, buffer_size);
1492 /* method-desciptor starts with parenthesis */
1495 for (i = 0; i < types->header.size; i++) {
1498 /* current argument */
1499 classinfo *c = (classinfo *) types->data[i];
1501 /* current position in utf-text */
1502 char *utf_ptr = c->name->text;
1504 /* determine type of argument */
1505 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1507 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1508 *pos++ = *utf_ptr; /* copy text */
1512 /* check for primitive types */
1513 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1514 char *utf_pos = utf_ptr - 1;
1515 char *primitive = primitivetype_table[j].wrapname;
1518 while (utf_pos < utf_end(c->name)) {
1519 if (*utf_pos++ != *primitive++) goto nomatch;
1522 /* primitive type found */
1523 *pos++ = primitivetype_table[j].typesig;
1530 /* no primitive type and no arrayclass, so must be object */
1534 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1548 for (i = 0; i < strlen(retType); i++) {
1549 *pos++ = retType[i];
1553 /* create utf-string */
1554 result = utf_new(buffer, (pos - buffer));
1555 MFREE(buffer, u1, buffer_size);
1561 /******************************************************************************************
1563 retrieve the next argument or returntype from a descriptor
1564 and return the corresponding class
1566 *******************************************************************************************/
1568 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1570 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1571 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1574 panic("illegal descriptor");
1576 if (skip) return NULL;
1578 use_class_as_object(c);
1583 /******************************************************************************************
1585 use the descriptor of a method to generate a java/lang/Class array
1586 which contains the classes of the parametertypes of the method
1588 *******************************************************************************************/
1590 java_objectarray* get_parametertypes(methodinfo *m)
1592 utf *descr = m->descriptor; /* method-descriptor */
1593 char *utf_ptr = descr->text; /* current position in utf-text */
1594 char *desc_end = utf_end(descr); /* points behind utf string */
1595 java_objectarray* result;
1596 int parametercount = 0;
1600 utf_nextu2(&utf_ptr);
1602 /* determine number of parameters */
1603 while ( *utf_ptr != ')' ) {
1604 get_type(&utf_ptr,desc_end,true);
1608 /* create class-array */
1609 result = builtin_anewarray(parametercount, class_java_lang_Class);
1611 utf_ptr = descr->text;
1612 utf_nextu2(&utf_ptr);
1614 /* get returntype classes */
1615 for (i = 0; i < parametercount; i++)
1616 result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
1625 /******************************************************************************************
1627 get the exceptions which can be thrown by a method
1629 *******************************************************************************************/
1631 java_objectarray* get_exceptiontypes(methodinfo *m) {
1632 u2 exccount=m->thrownexceptionscount;
1634 java_objectarray *result;
1635 /* create class-array */
1636 result = builtin_anewarray(exccount, class_java_lang_Class);
1637 for (i=0;i<exccount;i++) {
1638 java_objectheader *oh=(java_objectheader*)(m->thrownexceptions[i]);
1639 use_class_as_object(oh);
1649 /******************************************************************************************
1651 get the returntype class of a method
1653 *******************************************************************************************/
1655 classinfo *get_returntype(methodinfo *m)
1657 char *utf_ptr; /* current position in utf-text */
1658 char *desc_end; /* points behind utf string */
1659 utf *desc = m->descriptor; /* method-descriptor */
1661 utf_ptr = desc->text;
1662 desc_end = utf_end(desc);
1664 /* ignore parametertypes */
1665 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1668 return get_type(&utf_ptr,desc_end, false);
1672 /*****************************************************************************/
1673 /*****************************************************************************/
1676 /*--------------------------------------------------------*/
1677 void printNativeCall(nativeCall nc) {
1680 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1681 for (i=0; i<nc.methCnt; i++) {
1682 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1684 for (j=0; j<nc.callCnt[i]; j++) {
1685 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1686 nc.methods[i].methodCalls[j].classname,
1687 nc.methods[i].methodCalls[j].methodname,
1688 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1691 printf("-+++++--------------------\n");fflush(stdout);
1694 /*--------------------------------------------------------*/
1695 void printCompNativeCall(nativeCompCall nc) {
1697 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1698 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1699 utf_display(nc.classname); fflush(stdout);
1701 for (i=0; i<nc.methCnt; i++) {
1702 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1703 utf_display(nc.methods[i].methodname); fflush(stdout);
1704 utf_display(nc.methods[i].descriptor);fflush(stdout);
1705 printf("\n");fflush(stdout);
1707 for (j=0; j<nc.callCnt[i]; j++) {
1708 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1709 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1710 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1711 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1712 printf("\n");fflush(stdout);
1715 printf("---------------------\n");fflush(stdout);
1719 /*--------------------------------------------------------*/
1720 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1733 /*--------------------------------------------------------*/
1734 nativeCall* findNativeClassCalls(char *aclassname ) {
1737 for (i=0;i<NATIVECALLSSIZE; i++) {
1738 /* convert table to utf later to speed up search */
1739 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1740 return &nativeCalls[i];
1745 /*--------------------------------------------------------*/
1746 /*--------------------------------------------------------*/
1747 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1751 ncc->classname = utf_new_char(nc.classname);
1752 ncc->methCnt = nc.methCnt;
1754 for (i=0; i<nc.methCnt; i++) {
1755 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1756 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1757 ncc->callCnt[i] = nc.callCnt[i];
1759 for (j=0; j<nc.callCnt[i]; j++) {
1761 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1763 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1764 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1765 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1768 ncc->methods[i].methodCalls[j].methodname = NULL;
1769 ncc->methods[i].methodCalls[j].descriptor = NULL;
1777 /*--------------------------------------------------------*/
1779 bool natcall2utf(bool natcallcompdone) {
1782 if (natcallcompdone)
1785 for (i=0;i<NATIVECALLSSIZE; i++) {
1786 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1792 /*--------------------------------------------------------*/
1795 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start)
1797 #if defined(__GNUC__)
1798 #warning platform dependend
1800 java_objectarray *tmpArray;
1802 classinfo **current;
1804 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1805 /*printf("end %p, start %p, size %ld\n",end,start,size);*/
1806 if (!class_java_lang_Class)
1807 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
1808 if (!class_java_lang_SecurityManager)
1809 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1811 if (start==class_java_lang_SecurityManager) {
1816 tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1818 for(i=0,current=start;i<size;i++,current--) {
1820 /* printf("%d\n",i);
1821 utf_display(c->name);*/
1822 use_class_as_object(c);
1823 tmpArray->data[i]=c;
1829 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start)
1831 #if defined(__GNUC__)
1832 #warning platform dependend
1835 classinfo **current;
1837 classinfo *privilegedAction;
1838 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1839 /* log_text("builtin_asm_getclassloader");
1840 printf("end %p, start %p, size %ld\n",end,start,size);*/
1842 if (!class_java_lang_SecurityManager)
1843 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1845 if (start==class_java_lang_SecurityManager) {
1851 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1853 for(i=0,current=start;i<size;i++,current--) {
1855 if (c==privilegedAction) return NULL;
1856 if (c->classloader) return c->classloader;
1864 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1865 init_systemclassloader();
1867 return SystemClassLoader;*/
1871 * These are local overrides for various environment variables in Emacs.
1872 * Please do not remove this and leave it at the end of the file, where
1873 * Emacs will automagically detect them.
1874 * ---------------------------------------------------------------------
1877 * indent-tabs-mode: t